Skip to content

Instantly share code, notes, and snippets.

@lottspot
Forked from flaviocdc/pianobar-mediakeys.py
Created September 22, 2012 19:25
Show Gist options
  • Save lottspot/3767508 to your computer and use it in GitHub Desktop.
Save lottspot/3767508 to your computer and use it in GitHub Desktop.
Quick python script to handle media keys for pianobar
#!/usr/bin/env python
import gobject
import dbus
import dbus.service
import dbus.mainloop.glib
import os.path
APP_ID="pianobar-mediakeys"
pianobar_key_bindings = {}
def on_mediakey(app, action):
if app == APP_ID:
if action == 'Play' or action == 'Pause':
write_pianobar_cmd(pianobar_key_bindings['pause_song'])
elif action == 'Next':
write_pianobar_cmd(pianobar_key_bindings['next_song'])
elif action == 'Stop':
write_pianobar_cmd(pianobar_key_bindings['quit_pianobar'])
def pianobar_config_file():
if "XDG_CONFIG_HOME" in os.environ:
config_path = os.environ("XDG_CONFIG_HOME")
else:
config_path = os.path.expanduser("~/.config")
return config_path + "/pianobar/config"
def pianobar_fifo_file():
return os.path.dirname(pianobar_config_file()) + "/ctl";
def write_pianobar_cmd(cmd):
fifo = open(pianobar_fifo_file(), 'a')
fifo.write(cmd)
fifo.close()
def parse_pianobar_config():
cfg = open(pianobar_config_file())
lines = cfg.readlines()
lines = map(str.rstrip, lines)
config = {}
for line in lines:
line = line.split('=')
config[line[0]] = line[1]
pianobar_key_bindings["next_song"] = config.get('act_songnext', 'n')
pianobar_key_bindings["pause_song"] = config.get('act_songpause', 'p')
pianobar_key_bindings["quit_pianobar"] = config.get('act_quit', 'q')
parse_pianobar_config()
# set up the glib main loop.
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.Bus(dbus.Bus.TYPE_SESSION)
bus_object = bus.get_object('org.gnome.SettingsDaemon', '/org/gnome/SettingsDaemon/MediaKeys')
# this is what gives us the multi media keys.
dbus_interface='org.gnome.SettingsDaemon.MediaKeys'
bus_object.GrabMediaPlayerKeys(APP_ID, 0, dbus_interface=dbus_interface)
# connect_to_signal registers our callback function.
bus_object.connect_to_signal('MediaPlayerKeyPressed', on_mediakey)
# and we start the main loop.
mainloop = gobject.MainLoop()
mainloop.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment