I'm trying to make my own application that will listen to the media-keys in gnome using dbus. I have found a script that works in python and I'm trying to make it work by converting it to C. My c-code builds and runs correctly but it doesn't respond to the dbus signal.
this python code works:
#!/usr/bin/env python
"""Printing out gnome multi media keys via dbus-python.
"""
import gobject
import dbus
import dbus.service
import dbus.mainloop.glib
def on_mediakey(comes_from, what):
""" gets called when multimedia keys are pressed down.
"""
print ('comes from:%s what:%s') % (comes_from, what)
if what in ['Stop','Play','Next','Previous']:
print ('Got a multimedia key!')
else:
print ('Got a multimedia key...')
# set up the glib main loop.
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.Bus(dbus.Bus.TYPE_SESSION)
bus_object = bus.get_object('org.gnome.SettingsDaemon',
'/org/gnome/SettingsDaemon/MediaKeys')
# this is what gives us the multi media keys.
dbus_interface='org.gnome.SettingsDaemon.MediaKeys'
bus_object.GrabMediaPlayerKeys("MyMultimediaThingy", 0,
dbus_interface=dbus_interface)
# connect_to_signal registers our callback function.
bus_object.connect_to_signal('MediaPlayerKeyPressed',
on_mediakey)
# and we start the main loop.
mainloop = gobject.MainLoop()
mainloop.run()
my c-code does not work, what is missing?
#include <glib.h>
#include <dbus/dbus-glib.h>
static void media_key_pressed(DBusGProxy *proxy, const char *value_name, gpointer user_data) {
g_print("mediakey: %s",value_name);
}
int
main (int argc, char **argv)
{
DBusGConnection *bus;
DBusGProxy *proxy;
GError *error = NULL;
GMainLoop *loop;
g_type_init();
loop = g_main_loop_new (NULL, FALSE);
bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
if (!bus) {
g_printerr("Failed to connect to the D-BUS daemon: %s", error->message);
return 1;
}
proxy = dbus_g_proxy_new_for_name(bus,
"org.gnome.SettingsDaemon",
"/org/gnome/SettingsDaemon/MediaKeys",
"org.gnome.SettingsDaemon.MediaKeys");
if(!proxy) {
g_printerr("Could not create proxy object");
}
dbus_g_proxy_add_signal(proxy,
"MediaPlayerKeyPressed",
G_TYPE_STRING,
G_TYPE_INVALID);
dbus_g_proxy_connect_signal(proxy,
"MediaPlayerKeyPressed",
G_CALLBACK(media_key_pressed),
NULL,
NULL);
g_print("Starting media key listener");
g_main_loop_run (loop);
return 0;
}
I solved the problem! Must call dbus_g_proxy_call with the name "GrabMediaPlayerKeys" (which you also can see in the python code)
Must also register a marshaller which is done by first running this command:
and then use it in the dbus_g_object_register_marshaller function.
The complete C-code: