Ninja Nichols

The discipline of programming

Controlling Banshee and RadioTray via DBus

I recently began using OpenBox as my window manager in Linux. One issue I ran into was that my media players were unable to automatically bind to the global media hotkeys. My solution was to write a little bash script to control the media players using DBus. Right now I have support for Banshee and RadioTray. VLC support should be fairly easy to add, but I haven’t had the motivation to search for documentation on their DBus interface.

#!/usr/bin/env bash
#
# Controls playback on any running media players.
#
# usage: media-controller <command>
#
# Command:
#   play
#   pause
#   stop
#   next
#   previous
#
# Supported commands by player:
#   banshee
#       play
#       pause
#       stop
#       next
#       previous
#
#   radiotray
#       play
#       pause
#       stop

if [ ! $# -eq 1 ]; then
    ech o "usage: media-controller <command>"
    exit
fi

COMMAND=$1

BANSHEE_PID=`pgrep -x "banshee"`
RADIO_TRAY_PID=`pgrep -x "radiotray"`

if [ $BANSHEE_PID ]; then
    case $COMMAND in
        play)
            dbus-send --session --type="method_call" 
               --dest='org.bansheeproject.Banshee' 
               '/org/bansheeproject/Banshee/PlayerEngine' 
               'org.bansheeproject.Banshee.PlayerEngine.TogglePlaying'
            ;;
        pause)
            dbus-send --session --type="method_call" 
               --dest='org.bansheeproject.Banshee' 
               '/org/bansheeproject/Banshee/PlayerEngine' 
               'org.bansheeproject.Banshee.PlayerEngine.Pause'
            ;;
        stop)
            dbus-send --session --type="method_call" 
               --dest='org.bansheeproject.Banshee' 
               '/org/bansheeproject/Banshee/PlayerEngine' 
               'org.bansheeproject.Banshee.PlayerEngine.Close'
            ;;
        next)
            dbus-send --session --type="method_call" 
                --dest='org.bansheeproject.Banshee' 
                '/org/bansheeproject/Banshee/PlaybackController' 
                'org.bansheeproject.Banshee.PlaybackController.Next' 
                'boolean:false'
            ;;
        previous)
            dbus-send --session --type="method_call" 
                --dest='org.bansheeproject.Banshee' 
                '/org/bansheeproject/Banshee/PlaybackController' 
                'org.bansheeproject.Banshee.PlaybackController.Previous' 
                'boolean:false'
            ;;
        *)
            ech o "Command not supported for this player"
            ;;
    esac
fi
if [ $RADIO_TRAY_PID ]; then
    case $COMMAND in
        play)
            STATUS=`dbus-send --print-reply --session --type="method_call" 
                --dest='net.sourceforge.radiotray' 
                '/net/sourceforge/radiotray' 
                'net.sourceforge.radiotray.getCurrentRadio'`

            CURRENT_SONG=`ech o '$STATUS' | sed 's/.*"(.*)"[^"]*$/1/'`

            dbus-send --session --type="method_call" 
                --dest='net.sourceforge.radiotray' 
                '/net/sourceforge/radiotray' 
                'net.sourceforge.radiotray.playRadio' 
                'string:'$CURRENT_SONG
            ;;
        pause)
            dbus-send --session --type="method_call" 
                --dest='net.sourceforge.radiotray' 
                '/net/sourceforge/radiotray' 
                'net.sourceforge.radiotray.turnOff'
            ;;
        stop)
            dbus-send --session --type="method_call" 
                --dest='net.sourceforge.radiotray' 
                '/net/sourceforge/radiotray' 
                'net.sourceforge.radiotray.turnOff'
            ;;
        *)
            ech o "Command not supported for this player"
            ;;
    esac
fi

Actually getting the desired effect from the media keys required adding the following XML to my ~/.config/openbox/rc.xml file:

    <keybind key="XF86AudioNext">
      <action name="Execute">
        <command>media-controller next</command>
      </action>
    </keybind>
    <keybind key="XF86AudioPlay">
      <action name="Execute">
        <command>media-controller play</command>
      </action>
    </keybind>
    <keybind key="XF86AudioPrev">
      <action name="Execute">
        <command>media-controller previous</command>
      </action>
    </keybind>
    <keybind key="XF86AudioStop">
      <action name="Execute">
        <command>media-controller stop</command>
      </action>
    </keybind>
    <keybind key="XF86AudioPause">
      <action name="Execute">
        <command>media-controller pause</command>
      </action>
    </keybind>

Even Microsoft wants IE6 to die

Today Microsoft launched a new website in an effort to speed the demise of Internet Explorer 6. In a twitter post to the official Microsoft account the company proclaimed,

It’s not often that we encourage you to stop using one of our products, but for #IE6, we’ll make an exception: http://bit.ly/g0wt4m

The link redirects to the site, http://ie6countdown.com, which shows a map of estimated IE6 usage by country and announces the company’s goal to reduce the browser share of IE6 from 12% to 1%. From the map, it appears that the worst IE6 offender is China with 34% of Internet traffic coming from the browser compared with the United States’ meager 2.9%.

The site also offers suggestions on how to help friends and neighbors upgrade, declaring, “Friends don’t let friends use Internet Explorer 6.”

For anyone whose ever worked in web development, the demise of IE6 can’t come soon enough.

How much power is your laptop using?

Products like Kill-A-Watt show how much power appliances are using. It’s good way to measure your computer’s power consumption — when it’s plugged in. But what if I want to know how much power my laptop is using when I’m not plugged in?

Turns out it’s fairly easy to get battery info. Running cat /proc/acpi/battery/BAT0/state should return the current discharge rate and total battery capacity.

The trouble is that the “current rate” field sometimes switches between mW and mA. I have no idea why this is the case. Oh well, we can easily convert mA to mW with the formula: $latex watts = amps times volts $.

The end result is this conky friendly bash script:

[bash]
#!/usr/bin/env bash
# Print the current power consumption in watts.
# The script pulls the power consumption info
# from /proc/acpi/battery/BAT0/state
#
# Usage: power.sh [battery_num]
#
# Add to conky:
# ${execi 10 ~/conkyscripts/power.sh 0}
#

# Default is BAT0
BATTERY=0
if [ $1 ]; then
BATTERY=$1
fi

# Sometimes the "present rate" is returned in milliwatts,
# but sometimes it is in milliamps. If it’s in milliwatts,
# we just convert to watts and return. Otherwise we
# convert to watts with the formula:
# Watts = Amps * Volts

UNIT=`cat /proc/acpi/battery/BAT$BATTERY/state |
grep "present rate:" | awk ‘{ print $(NF) }’`

if [ $UNIT == "mW" ]; then
MILLI_WATTS=`cat /proc/acpi/battery/BAT$BATTERY/state |
grep "present rate:" | awk ‘{ print $(NF-1) }’`
e cho $[ $MILLI_WATTS / 1000 ].$[ ($MILLI_WATTS % 1000) / 100 ]
else
MILLI_AMPS=`cat /proc/acpi/battery/BAT$BATTERY/state |
grep "present rate:" | awk ‘{ print $(NF-1) }’`
MILLI_VOLTS=`cat /proc/acpi/battery/BAT$BATTERY/state |
grep "present voltage:" | awk ‘{ print $(NF-1) }’`
POWER=$[$MILLI_AMPS * $MILLI_VOLTS]
echo $[ $POWER / 1000000 ].$[ ($POWER % 1000000) / 100000 ]
fi
[/bash]

Hardware AES: Windows vs. Linux

Intel’s newest chips include a new AES hardware acceleration feature. My favorite cross-platform encryption utility, TrueCrypt, recently added support for the new instructions (Turn it on in Setting->Performance in Windows or Settings->Preferences->Performance in Linux).

However, I noticed something interesting when comparing the benchmark performance in Windows 7 to that in Linux. It seems that Linux is significantly faster than Windows, even though the acceleration is hardware-based.

Each data point represents the average of three (3) runs. The operating systems used were Windows 7 Professional 64-bit, Arch Linux 64-bit (latest) and Ubuntu 10.10 64-bit. Tests were performed on a ThinkPad T510 with an Intel Core i5 M 560 processor and 4 GB of memory.

Block Size Windows 7 Arch Linux Ubuntu
1 MB 377 MB/s 1.5 GB/s 1.5 GB/s
5 MB 798 MB/s 1.6 GB/s 1.6 GB/s
50 MB 1.1 GB/s 1.7 GB/s 1.6 GB/s
200 MB 1.3 GB/s 1.6 GB/s 1.7 GB/s

Bottom line: If you plan to do a lot of encryption, Linux will give you noticeably better performance, assuming all your data is already in memory.

Replres.rll

Shortly after installing Visual Studio 2010 I started getting a DLL Load Error message informing me that replsync.dll “Cannot not load resource dll: REPLRES.RLL”. The error message seems to pop up whenever I loose or regain my internet connection. The usual Google search didn’t really turn up any thing. I reinstalled Visual Studio, but the error message came back as soon as I restarted.

The replsync.dll file seems to be associated with SQL Server. In my case it’s located in “C:Program FilesMicrosoft SQL Server100COM”. My solution was to rename “replsync.dll” to “replsync.bak.dll”. Perhaps heavy-handed but the message is gone and I can work in peace once again. It’s been two days and I haven’t noticed any ill effects.

Unwanted Shortcut: Shift + Backspace

My laptop recently died and I had to buy a new one. Since I ended up with an extra hard drive, I decided to use the space to install a KDE distro, as I’ve used Gnome pretty much exclusively. I ended up picking kubuntu 10.10 to try. The installation was smooth and polished as expected from Ubuntu, however, shortly after logging in, I was suddenly logged out and sent back to the KDE login screen.

This happened a few more times before I realized that X server was restarting. Some additional trial-and-error lead me to the problem: X server was restarting every time I pressed Shift + Backspace. I’ve head of CTRL + ALT + Backspace restarting X, but Shift + Backspace was new and all too easy to trigger by accident.

The fix is pretty easy though. Open your ~/.Xmodmap file and change this line:
[sourcecode light=”true”]
keycode 22 = BackSpace Terminate_Server BackSpace Terminate_Server
[/sourcecode]
To this:
[sourcecode light=”true”]
keycode 22 = BackSpace BackSpace BackSpace BackSpace
[/sourcecode]

BigInteger in C#

As an introduction to the world of parallel computation, my professor gave an assignment in which we were asked to write a program to find all the prime numbers between 1 and some arbitrarily large value. The restriction is that the program must have 10 threads and the work must be shared equally between the threads.

The first step however was to figure out how to represent arbitrarily large integers in C#. A unsigned long just isn’t going to cut it. I looked around and found that .NET 4.0 adds a BigInteger class in the System.Numerics namespace.

Being new to Visual Studio and C#, I was a little confused as to why a little red squiggle was appearing under “Numerics” in the line:

First I made sure that the target platform was set to “.NET Framework 4.0” by going to the Project menu, selecting “ Properties…” and double checking the Target dropdown box. That wasn’t the issue since the correct version was already selected.

Turns out I needed to manually add an assembler reference to System.Numerics.dll. The process is pretty easy. On the Project Explorer pane, right-click “References”. Click “Add Reference” and select “System.Numerics” from the “.NET” tab.

Success! The red squiggle went away.

Convenient Keys

As a programmer, I use all of my 104 keys but I use some a lot more than others. Take for instance the Home and End keys. On my old laptop those were pretty easy to get to, but on my new ThinkPad 510 I’m constantly reaching up above the Backspace key to hit them. It gets annoying. My solution involvesĀ re-purposingĀ the “Browser_forward” and “Browser_back” keys which are conveniently located to the right and left of my Up Arrow key respectively.

In Windows I created this AutoHotkey script:
<br /> Browser_forward::End<br /> Browser_back::Home<br />
Much better!

Update: I added the same functionality to Linux by adding the following lines to the end of my ~/.Xmodmap file:
<br /> !! Remap Browser_Forward -> End and Browser_Back -> Home<br /> keycode 166 = Home NoSymbol Home<br /> keycode 167 = End NoSymbol End<br />
It’s OK to have duplicate keycode entries as long as the one that you want is listed last.

If you don’t have a .Xmodmap, generate it by running “xmodmap -pke > ~/.Xmodmap”. Finally make sure it gets loaded by adding this to your ~/.xinitrc file:

if [ -f $HOME/.Xmodmap ]; then
    /usr/bin/xmodmap $HOME/.Xmodmap
fi

I found the exact keycode values for the Browser_Forward and Browser_Back keys by using sudo showkey. However, for some reason all the values returned were all off by -8. For example, in .Xmodmap the ‘A’ key should be 38 but running showkey returns 30.