#!/usr/bin/env python
'''
This script handles media key pressing events on DBus
(Fn+Play/Pause/Stop/Forward/Prev) by calling mpc with
corresponding argument.
Tested on Acer aspire 3935 with Ubuntu 10.10. Both laptop
media keys and bluetooth stereo garniture media buttons (BT56) work.
Author: Batalov Sergey, 2011-02-07 svbatalov at gmail dot com.
Here is a patch to use it with mpd_osd.py (see http://mpd.wikia.com/wiki/Mpd_osd.py_%28using_dbus%29 ):
--- mpd_osd.py-old
+++ mpd_osd.py-new
@@ -99,9 +99,13 @@
"info",
"stop",
"next",
- "previous"
+ "previous",
+ "prev"
]
+ def prev(self):
+ self.previous()
+
def info(self):
"""
Show song information only
'''
import dbus
from dbus.mainloop.glib import DBusGMainLoop
import gobject
import os
mpc = 'mpc' # Or mpd_osd.py modified to accept the 'prev' command.
def KeyHandler(x,y):
def run(cmd):
return lambda: os.system(mpc + ' ' + cmd)
{
'Play': run('toggle'),
'Stop': run('stop'),
'Next': run('next'),
'Previous': run('prev') }[y]()
DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
loop = gobject.MainLoop()
#dbus info obtained using dbus-monitor
bus.add_signal_receiver(KeyHandler,
None, #signal name. None means any
'org.gnome.SettingsDaemon.MediaKeys', #interface
None, #sender bus name. any. :1.7 usually
'/org/gnome/SettingsDaemon/MediaKeys' #path
)
loop.run()