问题
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;
}
回答1:
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:
glib-genmarshal --header --body mediaplayerkeypressed.list > marshal.h
and then use it in the dbus_g_object_register_marshaller function.
The complete C-code:
#include <glib.h>
#include <dbus/dbus-glib.h>
#include "marshal.h"
static void media_key_pressed(DBusGProxy *proxy, const char *value1, const char *value2, gpointer user_data) {
g_print("mediakey: %s\n",value2);
}
int
main (int argc, char **argv)
{
DBusGConnection *conn;
DBusGProxy *proxy;
GError *error;
GMainLoop *loop;
g_type_init();
error = NULL;
conn = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
if (!conn) {
g_printerr("Failed to connect to the D-BUS daemon: %s\n", error->message);
g_error_free(error);
return 1;
}
loop = g_main_loop_new (NULL, FALSE);
if(!loop) {
g_printerr("Could not create mainloop\n");
return 1;
}
proxy = dbus_g_proxy_new_for_name(conn,
"org.gnome.SettingsDaemon",
"/org/gnome/SettingsDaemon/MediaKeys",
"org.gnome.SettingsDaemon.MediaKeys");
if(!proxy) {
g_printerr("Could not create proxy object\n");
}
error = NULL;
if(!dbus_g_proxy_call(proxy,
"GrabMediaPlayerKeys", &error,
G_TYPE_STRING, "WebMediaKeys",
G_TYPE_UINT, 0,
G_TYPE_INVALID,
G_TYPE_INVALID)) {
g_printerr("Could not grab media player keys: %s\n", error->message);
}
dbus_g_object_register_marshaller (
g_cclosure_user_marshal_VOID__STRING_STRING,
G_TYPE_NONE,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_INVALID);
dbus_g_proxy_add_signal(proxy,
"MediaPlayerKeyPressed",
G_TYPE_STRING,
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\n");
g_main_loop_run (loop);
return 0;
}
来源:https://stackoverflow.com/questions/5744041/cant-get-dbus-signal-listener-to-work-in-c-with-gnome-multimedia-keys