#!/usr/bin/env python
'''
Created on 11.03.2010
@author: Arthur Spitzer <arthapex@gmail.com>
Modified on 28.05.2012
@author: Matteo Italia <matteo@mitalia.net>
'''
import dbus.mainloop.glib
import dbus.service
import gobject
app_name = 'mmkeys-mate2gnome'
Version=0.1
Gnome_DbusBusName = 'org.gnome.SettingsDaemon'
Gnome_DbusObjectPath = '/org/gnome/SettingsDaemon/MediaKeys'
Gnome_DbusInterface = 'org.gnome.SettingsDaemon.MediaKeys'
Mate_DbusBusName = 'org.mate.SettingsDaemon'
Mate_DbusObjectPath = '/org/mate/SettingsDaemon/MediaKeys'
Mate_DbusInterface = 'org.mate.SettingsDaemon.MediaKeys'
class SettingsDaemonObject(dbus.service.Object):
def __init__(self, session_bus):
dbus.service.Object.__init__(self, session_bus, object_path=Gnome_DbusObjectPath)
self.__paused = False
self.__playing = False
self.__apps = []
@dbus.service.method(dbus_interface=Gnome_DbusInterface, in_signature='sd', out_signature='')
def GrabMediaPlayerKeys(self, app_name, time):
self.__apps.append(app_name)
@dbus.service.method(dbus_interface=Gnome_DbusInterface, in_signature='s', out_signature='')
def ReleaseMediaPlayerKeys(self, app_name):
self.__apps.remove(app_name)
@dbus.service.signal(dbus_interface=Gnome_DbusInterface)
def MediaPlayerKeyPressed(self, app_name, action):
pass
@dbus.service.method(dbus_interface=Gnome_DbusInterface, in_signature='s')
def PressedKey(self, action):
self.__send_action_to_all_apps(action)
def __send_action_to_all_apps(self, action):
for app in self.__apps:
self.MediaPlayerKeyPressed(app, action)
@dbus.service.method(dbus_interface=Gnome_DbusInterface)
def PressedPlay(self):
self.__send_action_to_all_apps('Play')
self.__playing = True
@dbus.service.method(dbus_interface=Gnome_DbusInterface)
def PressedPause(self):
self.__send_action_to_all_apps('Pause')
@dbus.service.method(dbus_interface=Gnome_DbusInterface)
def PressedStop(self):
self.__send_action_to_all_apps('Stop')
@dbus.service.method(dbus_interface=Gnome_DbusInterface)
def PressedNext(self):
self.__send_action_to_all_apps('Next')
@dbus.service.method(dbus_interface=Gnome_DbusInterface)
def PressedPrevious(self):
self.__send_action_to_all_apps('Previous')
class Translator:
def __init__(self):
service = dbus.SessionBus().get_object(Gnome_DbusBusName, Gnome_DbusObjectPath)
self.__dbusInterface = dbus.Interface(service, Gnome_DbusInterface)
def translator(self, app_name, action):
# Async call, otherwise we get stuck (the server is running in this same thread)
self.__dbusInterface.PressedKey(action, reply_handler=lambda: None, error_handler=lambda *e: None)
if __name__ == '__main__':
# DBUS boilerplate
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
session_bus = dbus.SessionBus()
name = dbus.service.BusName(Gnome_DbusBusName, session_bus)
object = SettingsDaemonObject(session_bus)
# Input part
mate_settings_bus = session_bus.get_object(Mate_DbusBusName, Mate_DbusObjectPath)
# this is what gives us the multi media keys.
mate_mmkeys_if=Mate_DbusInterface
mate_settings_bus.GrabMediaPlayerKeys(app_name, 0,
dbus_interface=mate_mmkeys_if)
tr=Translator();
# register the translator
mate_settings_bus.connect_to_signal('MediaPlayerKeyPressed', tr.translator)
# start the main loop
mainloop = gobject.MainLoop()
mainloop.run()