#!/usr/bin/python # -*- coding: UTF-8 -*- # vim: expandtab sw=4 ts=4 sts=4 tw=78: ''' Simple DBUS listener which catches button events and executes one of hard coded actions. ''' __author__ = 'Michal Čihař' __email__ = 'michal@cihar.com' __license__ = ''' Copyright © 2007 - 2008 Michal Čihař This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA ''' import dbus import dbus.glib import gobject import os import mpdclient2 def stop(): mpd = mpdclient2.connect() mpd.stop() def next(): mpd = mpdclient2.connect() mpd.next() def previous(): mpd = mpdclient2.connect() mpd.previous() def toggle(): mpd = mpdclient2.connect() s = mpd.status() if s['state'] == 'play': mpd.pause(True) else: mpd.play() KEYMAP = { 'stop' : (stop, ()), 'playpause' : (toggle, ()), 'nextsong' : (next, ()), 'previoussong' : (previous, ()), 'fn+1' : (os.system, ('sudo /usr/bin/chvt 1', )), 'fn+2' : (os.system, ('sudo /usr/bin/chvt 2', )), 'fn+3' : (os.system, ('sudo /usr/bin/chvt 3', )), 'fn+4' : (os.system, ('sudo /usr/bin/chvt 4', )), 'fn+5' : (os.system, ('sudo /usr/bin/chvt 5', )), 'fn+6' : (os.system, ('sudo /usr/bin/chvt 6', )), 'fn+7' : (os.system, ('sudo /usr/bin/chvt 7', )), 'fn+8' : (os.system, ('sudo /usr/bin/chvt 8', )), 'fn+tab' : (os.system, ('sudo ~/bin/toggle-fan', )), 'fn+space' : (os.system, ('sudo ~/bin/toggle-touchpad', )), } def condition_events_handler(type, key): ''' Handles Condition events from org.freedesktop.Hal.Device. ''' if type == 'ButtonPressed': key = key.lower() try: KEYMAP[key][0](*KEYMAP[key][1]) print 'Executed event for key: %s' % key except KeyError: print 'No event defined for key: %s' % key # Get system bus bus = dbus.SystemBus() # Attach to callback bus.add_signal_receiver(condition_events_handler, 'Condition', 'org.freedesktop.Hal.Device', None, None) # Main loop print 'Listening for events' loop = gobject.MainLoop() loop.run()