Advertisement
Guest User

mmkeys-mate2mpris2

a guest
May 29th, 2012
1,316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. '''
  4. Created on 30.05.2012
  5.  
  6. @author: Matteo Italia <mi_1@mitalia.net>
  7. '''
  8.  
  9. import dbus
  10. import dbus.mainloop.glib
  11. import gobject
  12.  
  13. app_name = 'mmkeys-mate2mpris2'
  14. Version=0.1
  15.  
  16. MediaKeysObjName = 'org.mate.SettingsDaemon'
  17. MediaKeysObjectPath = '/org/mate/SettingsDaemon/MediaKeys'
  18. MediaKeysInterface = 'org.mate.SettingsDaemon.MediaKeys'
  19.  
  20. MPRIS2Prefix = 'org.mpris.MediaPlayer2'
  21.  
  22. ActionMappings = {
  23.         'Play': 'PlayPause',
  24.         'Pause': 'Pause',
  25.         'Stop': 'Stop',
  26.         'Next': 'Next',
  27.         'Previous': 'Previous'}
  28.  
  29.  
  30. def onMediaKeyPress(app_name, action):
  31.     sb = dbus.SessionBus()
  32.     # Get the compatible players
  33.     players = [n for n in sb.list_names() if n.startswith(MPRIS2Prefix + ".") ]
  34.  
  35.     # Send them the command
  36.     for n in players:
  37.         # TODO: it doesn't make sense to perform the action on *all* the players!
  38.         # find a sensible criterion to choose the "best one"
  39.         sb.get_object(n, '/org/mpris/MediaPlayer2').__getattr__(ActionMappings[action])()
  40.  
  41. if __name__ == '__main__':
  42.  
  43.     # DBUS boilerplate
  44.     dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
  45.     sb = dbus.SessionBus()
  46.  
  47.     # Get the media keys notificator object
  48.     mediaKeysObj = sb.get_object(MediaKeysObjName, MediaKeysObjectPath)
  49.  
  50.     # Register to receive media keys notifications
  51.     mediaKeysObj.GrabMediaPlayerKeys(app_name, 0, dbus_interface=MediaKeysInterface)
  52.     mediaKeysObj.connect_to_signal('MediaPlayerKeyPressed', onMediaKeyPress)
  53.  
  54.     # Start the main loop
  55.     mainLoop = gobject.MainLoop()
  56.     mainLoop.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement