Add support for global keybinds

master
Dustin 2014-08-05 14:21:54 -05:00
parent ec665403ea
commit feadbbdcbb
1 changed files with 38 additions and 0 deletions

View File

@ -1,6 +1,7 @@
from gi.repository import GdkPixbuf
from gi.repository import GLib
from gi.repository import Gtk
from gi.repository import Keybinder
import argparse
import contextlib
import dbus.mainloop.glib
@ -143,6 +144,39 @@ class Notifier(object):
timer.set_callback(reconnect)
timer.attach()
def bind_keys(self):
def playpause():
status = self.client.status()
pause = int(status['state'] == 'play')
self.client.pause(pause)
def volume_down():
status = self.client.status()
self.client.setvol(int(status['volume']) - 1)
def volume_up():
status = self.client.status()
self.client.setvol(int(status['volume']) + 1)
handlers = {
'XF86AudioLowerVolume': volume_down,
'XF86AudioRaiseVolume': volume_up,
'XF86AudioPlay': playpause,
'XF86AudioNext': self.client.next,
'XF86AudioPrev': self.client.previous,
'XF86AudioStop': self.client.stop,
}
def callback(keystring):
self.client.noidle()
try:
handlers[keystring]()
except Exception as e:
sys.stderr.write('{0}\n'.format(e))
self.client.send_idle('player')
Keybinder.init()
for key in handlers.keys():
Keybinder.bind(key, callback)
def connect(self):
self.client._sock = None
self.client.connect(self.host, self.port)
@ -223,6 +257,8 @@ def _parse_args():
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument('--help', action='help',
help='show this help message and exit')
parser.add_argument('--no-keybinds', '-k', action='store_true',
default=False, help='Do not bind to multimedia keys')
parser.add_argument('--no-fork', '-f', action='store_true', default=False,
help='Do not fork into the background')
parser.add_argument('--no-status-icon', action='store_true', default=False,
@ -265,6 +301,8 @@ def main():
notifier = Notifier(host, args.port, password)
if not args.no_status_icon:
notifier.show_status_icon()
if not args.no_keybinds:
notifier.bind_keys()
notifier.start()
def quit():