Patch: fix multimedia keys (play/pause etc) in MATE desktop

Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
prophet36

Patch: fix multimedia keys (play/pause etc) in MATE desktop

Post by prophet36 »

It looks like the majority of multimedia apps will need to be upgraded to explicitly support multimedia hotkeys (i.e "Play", "Pause", "Stop", "Previous" & "Next") in MATE. I have written a couple of brain-dead patches for Sonata and Totem which may be of interest to someone; I suspect it will be straightforward to write similar patches for other multimedia apps.

You can just run these with sudo. They are pretty dumb and dirty patches, they just replace the namespace used (by Sonata and Totem) to issue hotkey notifications on DBUS to match the one now used by MATE. If you run these patches and subsequently move to a Gnome3 desktop you'll have to reinstall Sonata and Totem (sudo apt-get --reinstall install totem sonata).

I do wonder why the MATE developers changed the DBUS namespace used by mate-settings-daemon in the first place. Keeping the old org.gnome namespace in mate-settings-daemon would have meant existing stuff would have continued to work, and would have only caused problems presumably if someone somehow managed to run MATE and Gnome3 at the same time...

I'll file proper bug reports with upstream in a day or two.

Chris
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
Oscar799
Level 20
Level 20
Posts: 10393
Joined: Tue Aug 11, 2009 9:21 am
Location: United Kingdom

Re: Patch: fix multimedia keys (play/pause etc) in MATE desk

Post by Oscar799 »

Moved here by moderator
Image
matteo.italia

Re: Patch: fix multimedia keys (play/pause etc) in MATE desk

Post by matteo.italia »

I too found this problem, but I think that we can workaround it in a different manner: if the "real" gnome-settings-daemon isn't running (and it shouldn't be in a MATE session), we can create a faux-gnome-settings-daemon interface to make happy the apps that rely on it.

I put together this thing in Python (heavily based on mediakeys-daemon), at the moment it seems to run fine.

Code: Select all

#!/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()
matteo.italia

Re: Patch: fix multimedia keys (play/pause etc) in MATE desk

Post by matteo.italia »

Another idea that comes to mind: since we don't know if MATE will ever gain enough momentum to make media players support both gnome-settings-daemon and mate-settings-daemon, why can't we drop this interface inherited from Gnome and just use the standard MPRIS2 interface?
This way MATE will adhere to a freedesktop standard, already supported by most media players, without needing hacks like the script above.
Monsta
Level 10
Level 10
Posts: 3071
Joined: Fri Aug 19, 2011 3:46 am

Re: Patch: fix multimedia keys (play/pause etc) in MATE desk

Post by Monsta »

Finally, someone addressed this issue. :) Thank you guys. Have you contacted the developers already (via github, for example)?
matteo.italia

Re: Patch: fix multimedia keys (play/pause etc) in MATE desk

Post by matteo.italia »

I just created an issue on github.
matteo.italia

Re: Patch: fix multimedia keys (play/pause etc) in MATE desk

Post by matteo.italia »

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

Code: Select all

#!/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()
Currently it sends the notification to all the supported applications that are running, which isn't a big problem (who runs at the same time two media players?), but doesn't make much sense (except maybe for the "Stop" key). Does anyone have a better idea on how to choose the "best" target application (using only the data available via MPRIS)?
Santezq

Re: Patch: fix multimedia keys (play/pause etc) in MATE desk

Post by Santezq »

Thank you very much. It worked perfectly.
mat666

Re: Patch: fix multimedia keys (play/pause etc) in MATE desk

Post by mat666 »

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

Re: Patch: fix multimedia keys (play/pause etc) in MATE desk

Post by fstonedahl »

That's curious -- I downloaded it, made it executable, and ran it, and then my media control keys worked fine (for Banshee, which I am using...).

If you try running it from the terminal instead, does it give you any error messages?
User avatar
RobertLM78
Level 3
Level 3
Posts: 183
Joined: Sat Apr 28, 2012 4:19 am
Location: US

Re: Patch: fix multimedia keys (play/pause etc) in MATE desk

Post by RobertLM78 »

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?
I'm also in this position. I'm not quite that savvy enough yet to know how to make use of this script, unfortunately. It's too bad that this wasn't fixed in for MATE on Mint 13 as all the other keyboard shortcuts have now been fixed (I can even get custom keyboard shortcuts to work now).

So, if anyone can help out us noob's, it would be great :D ! (I need to learn how to use python scripts, anyway).
Gateway DX4860, Sapphire Radeon HD 5450, 8 GB RAM, Mint 17.3 64-bit (Rosa), MATE
AMD Ryzen 3-3100, AMD Radeon RX 570, 16 GB RAM, Mint 21 (Vanessa), MATE
fstonedahl

Re: Patch: fix multimedia keys (play/pause etc) in MATE desk

Post by fstonedahl »

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.

Here's a fairly easy way to convert files back and forth between unix and dos-based line-endings.

First, install the utility "flip":

$ sudo apt-get install flip

Then go to the folder your file is in:

$ cd ~/Downloads/

and run:

$ flip -u mmkeys-mate2mpris2.py

This will convert it to unix-based line endings. (If for some reason you need to convert files the other way, you can run "flip -m filename")

Now try running it:

$ ./mmkeys-mate2mpris2.py

Hope this helps...

P.S. You'll want to add the script to your "Startup Applications", so it starts every time your restart your computer.
User avatar
RobertLM78
Level 3
Level 3
Posts: 183
Joined: Sat Apr 28, 2012 4:19 am
Location: US

Re: Patch: fix multimedia keys (play/pause etc) in MATE desk

Post by RobertLM78 »

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.
Nada. I didn't get any error messages, but I'm not sure it did a thing. I am using Rhythmbox, so I don't know if that might have anything to do with it (although it shouldn't). Also, the file I downloaded was a .txt which I just renamed to .py. I'm a little disappointed... :?
Gateway DX4860, Sapphire Radeon HD 5450, 8 GB RAM, Mint 17.3 64-bit (Rosa), MATE
AMD Ryzen 3-3100, AMD Radeon RX 570, 16 GB RAM, Mint 21 (Vanessa), MATE
fstonedahl

Re: Patch: fix multimedia keys (play/pause etc) in MATE desk

Post by fstonedahl »

In order to see the error messages, I think you would need to run it from the terminal.

When you run it from the terminal, does it finish running (i.e., show you another prompt) or does it sit there with a blank line (still running)? It should remain running...

After further investigation, it seems that it's *only* working for me in Banshee. Totem & VLC both still ignore the media keys. I don't have rhythmbox installed, so I can't comment on it...
rdanner3

Re: Patch: fix multimedia keys (play/pause etc) in MATE desk

Post by rdanner3 »

matteo.italia wrote:Simpler script that uses MPRIS2 instead of emulating gnome-settings-daemon (shorter and cleaner IMO).
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. :lol:
fvdnabee

Re: Patch: fix multimedia keys (play/pause etc) in MATE desk

Post by fvdnabee »

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.
squeezy

Re: Patch: fix multimedia keys (play/pause etc) in MATE desk

Post by squeezy »

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.
Does the 'Next Track" button work too? Sometimes I'm in the mood for Peggy Lee, other times not :D
rdanner3

Re: Patch: fix multimedia keys (play/pause etc) in MATE desk

Post by rdanner3 »

So far, neither script works for me, oddly enough. Maybe it's the fact I'm using a Toshiba? (See my sigfile)
squeezy

Re: Patch: fix multimedia keys (play/pause etc) in MATE desk

Post by squeezy »

matteo.italia wrote:Simpler script that uses MPRIS2 instead of emulating gnome-settings-daemon (shorter and cleaner IMO).
Just tried this. Media keys work in Banshee now! Thank you, thank you!
pasu

Re: Patch: fix multimedia keys (play/pause etc) in MATE desk

Post by pasu »

It worked for both Totem and Banshee players. However, after updating totem from version 3.01-0ubuntu21 (precise) to 3.01-0ubuntu21.1 (precise-updates), the script stops working again. I had to downgrade to the older version in order to get the script working again.
Locked

Return to “MATE”