daily pastebin goal
59%
SHARE
TWEET

mmkeys-mate2gnome

a guest May 27th, 2012 360 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. '''
  4. Created on 11.03.2010
  5.  
  6. @author: Arthur Spitzer <arthapex@gmail.com>
  7.  
  8. Modified on 28.05.2012
  9.  
  10. @author: Matteo Italia <matteo@mitalia.net>
  11. '''
  12.  
  13.  
  14. import dbus.mainloop.glib
  15. import dbus.service
  16.  
  17. import gobject
  18.  
  19. app_name = 'mmkeys-mate2gnome'
  20. Version=0.1
  21.  
  22. Gnome_DbusBusName = 'org.gnome.SettingsDaemon'
  23. Gnome_DbusObjectPath = '/org/gnome/SettingsDaemon/MediaKeys'
  24. Gnome_DbusInterface = 'org.gnome.SettingsDaemon.MediaKeys'
  25.  
  26. Mate_DbusBusName = 'org.mate.SettingsDaemon'
  27. Mate_DbusObjectPath = '/org/mate/SettingsDaemon/MediaKeys'
  28. Mate_DbusInterface = 'org.mate.SettingsDaemon.MediaKeys'
  29.  
  30. class SettingsDaemonObject(dbus.service.Object):
  31.     def __init__(self, session_bus):
  32.         dbus.service.Object.__init__(self, session_bus, object_path=Gnome_DbusObjectPath)
  33.         self.__paused = False
  34.         self.__playing = False
  35.         self.__apps = []
  36.  
  37.     @dbus.service.method(dbus_interface=Gnome_DbusInterface, in_signature='sd', out_signature='')
  38.     def GrabMediaPlayerKeys(self, app_name, time):
  39.         self.__apps.append(app_name)
  40.    
  41.     @dbus.service.method(dbus_interface=Gnome_DbusInterface, in_signature='s', out_signature='')
  42.     def ReleaseMediaPlayerKeys(self, app_name):
  43.         self.__apps.remove(app_name)
  44.  
  45.     @dbus.service.signal(dbus_interface=Gnome_DbusInterface)
  46.     def MediaPlayerKeyPressed(self, app_name, action):
  47.         pass
  48.    
  49.     @dbus.service.method(dbus_interface=Gnome_DbusInterface, in_signature='s')
  50.     def PressedKey(self, action):
  51.         self.__send_action_to_all_apps(action)
  52.  
  53.     def __send_action_to_all_apps(self, action):
  54.         for app in self.__apps:
  55.             self.MediaPlayerKeyPressed(app, action)
  56.    
  57.     @dbus.service.method(dbus_interface=Gnome_DbusInterface)
  58.     def PressedPlay(self):
  59.         self.__send_action_to_all_apps('Play')
  60.         self.__playing = True
  61.    
  62.     @dbus.service.method(dbus_interface=Gnome_DbusInterface)
  63.     def PressedPause(self):
  64.         self.__send_action_to_all_apps('Pause')
  65.    
  66.     @dbus.service.method(dbus_interface=Gnome_DbusInterface)
  67.     def PressedStop(self):
  68.         self.__send_action_to_all_apps('Stop')
  69.    
  70.     @dbus.service.method(dbus_interface=Gnome_DbusInterface)
  71.     def PressedNext(self):
  72.         self.__send_action_to_all_apps('Next')
  73.    
  74.     @dbus.service.method(dbus_interface=Gnome_DbusInterface)
  75.     def PressedPrevious(self):
  76.         self.__send_action_to_all_apps('Previous')
  77.  
  78. class Translator:
  79.     def __init__(self):
  80.         service = dbus.SessionBus().get_object(Gnome_DbusBusName, Gnome_DbusObjectPath)
  81.         self.__dbusInterface = dbus.Interface(service, Gnome_DbusInterface)
  82.  
  83.     def translator(self, app_name, action):
  84.         # Async call, otherwise we get stuck (the server is running in this same thread)
  85.         self.__dbusInterface.PressedKey(action, reply_handler=lambda: None, error_handler=lambda *e: None)
  86.  
  87. if __name__ == '__main__':
  88.  
  89.     # DBUS boilerplate
  90.     dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
  91.  
  92.     session_bus = dbus.SessionBus()
  93.     name = dbus.service.BusName(Gnome_DbusBusName, session_bus)
  94.     object = SettingsDaemonObject(session_bus)
  95.  
  96.     # Input part
  97.     mate_settings_bus = session_bus.get_object(Mate_DbusBusName, Mate_DbusObjectPath)
  98.  
  99.     # this is what gives us the multi media keys.
  100.     mate_mmkeys_if=Mate_DbusInterface
  101.     mate_settings_bus.GrabMediaPlayerKeys(app_name, 0,
  102.                                    dbus_interface=mate_mmkeys_if)
  103.  
  104.     tr=Translator();
  105.  
  106.     # register the translator
  107.     mate_settings_bus.connect_to_signal('MediaPlayerKeyPressed', tr.translator)
  108.  
  109.     # start the main loop
  110.     mainloop = gobject.MainLoop()
  111.     mainloop.run()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top