



#!/usr/bin/env python
'''
Created on 11.03.2010
@author: Arthur Spitzer <arthapex@gmail.com>
Modified on 28.05.2012
@author: Matteo Italia
'''
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()


#!/usr/bin/env python
'''
Created on 30.05.2012
@author: Matteo Italia <mi_1@mitalia.net>
'''
import dbus
import dbus.mainloop.glib
import gobject
app_name = 'mmkeys-mate2mpris2'
Version=0.1
MediaKeysObjName = 'org.mate.SettingsDaemon'
MediaKeysObjectPath = '/org/mate/SettingsDaemon/MediaKeys'
MediaKeysInterface = 'org.mate.SettingsDaemon.MediaKeys'
MPRIS2Prefix = 'org.mpris.MediaPlayer2'
ActionMappings = {
'Play': 'PlayPause',
'Pause': 'Pause',
'Stop': 'Stop',
'Next': 'Next',
'Previous': 'Previous'}
def onMediaKeyPress(app_name, action):
sb = dbus.SessionBus()
# Get the compatible players
players = [n for n in sb.list_names() if n.startswith(MPRIS2Prefix + ".") ]
# Send them the command
for n in players:
# TODO: it doesn't make sense to perform the action on *all* the players!
# find a sensible criterion to choose the "best one"
sb.get_object(n, '/org/mpris/MediaPlayer2').__getattr__(ActionMappings[action])()
if __name__ == '__main__':
# DBUS boilerplate
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
sb = dbus.SessionBus()
# Get the media keys notificator object
mediaKeysObj = sb.get_object(MediaKeysObjName, MediaKeysObjectPath)
# Register to receive media keys notifications
mediaKeysObj.GrabMediaPlayerKeys(app_name, 0, dbus_interface=MediaKeysInterface)
mediaKeysObj.connect_to_signal('MediaPlayerKeyPressed', onMediaKeyPress)
# Start the main loop
mainLoop = gobject.MainLoop()
mainLoop.run()




mat666 wrote:Hi,
How should I use this script? I've downloaded it, made it executable and run, and nothing changed. Multimedia keys still doesn't work.
I've tried booth scripts.
I have no experience with scripts so maybe is something what i should do in special way?



fstonedahl wrote:Wait -- actually, I have an idea why it might not be working for you.
When I downloaded it (the second script) from pastebin, the file I got was using windows/dos line endings (\r\n) instead of unix-style ones (\n). I had downloaded the file and named it "mmkeys-mate2mpris2.py". When I ran the script by typing "python mmkeys-mate2mpris2.py", it worked justfine, but when I tried running it directly "./mmkeys-mate2mpris2.py", it gave me the error ": No such file or directory". After converting it to unix line-endings, it worked fine.



Which, unfortunately, doesn't seem to work for either Rhythmbox or Banshee. Tried Amarok, but it never cleanly ran, so can't say that media keys did or did not work. I never run more than one media player at a go; have discovered it can be (at best) noisy.matteo.italia wrote:Simpler script that uses MPRIS2 instead of emulating gnome-settings-daemon (shorter and cleaner IMO).


fvdnabee wrote:You also have to make sure that the mediabuttons are assigned to the correct actions in mate-keybinding-properties. You can also find this under Menu > Preferences > Keyboard shortcuts.
After assigning the play/pauze button to the correct action and running the script provided by Matteo(thx btw) I managed to pauze/resume Banshee playback using the button non my Logitech K200. Running Linux Mint 12 with Mate 1.2 btw.


matteo.italia wrote:Simpler script that uses MPRIS2 instead of emulating gnome-settings-daemon (shorter and cleaner IMO).


Users browsing this forum: FeodalherreN and 5 guests