我正在尝试从c代码中侦听dbus-event。我能够通过dbus-monitor事件捕获它们。
dbus-monitor --session --monitor "type='signal',interface='org.jwz.XScreensaver'"
signal sender=org.freedesktop.DBus -> dest=:1.146 serial=2 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameAcquired
string ":1.146"
signal sender=:1.96 -> dest=(null destination) serial=40 path=/org/jwz/XScreensaver; interface=org.jwz.XScreensaver; member=ScreensaverStarted
signal sender=:1.96 -> dest=(null destination) serial=41 path=/org/jwz/XScreensaver; interface=org.jwz.XScreensaver; member=ScreensaverStopped
当我尝试使用c代码时,同样不可见。不知道我错过了哪里回调没有签名然后事件是期待或我的路径/接口是错误的。这是代码
static void
test_hint_signal_handler (DBusGProxy * proxy,
gpointer user_data)
{
printf("I am here");
}
gint
main (gint argc, gchar *argv[])
{
printf("In Main\n");
DBusGConnection *dbus_glib_connection = NULL;
GMainLoop *loop = NULL;
GError *error = NULL;
DBusGProxy *control_proxy = NULL;
g_type_init ();
loop = g_main_loop_new (NULL, FALSE);
dbus_glib_connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
if (error != NULL)
{
g_warning ("Failed connecting to system bus: %s", error->message);
dbus_glib_connection = NULL;
g_error_free (error);
}
if (dbus_glib_connection != NULL)
{
control_proxy = dbus_g_proxy_new_for_name( dbus_glib_connection,
"org.jwz.XScreensaver",
"/org/jwz/XScreensaver",
"org.jwz.XScreensaver"
);
if (control_proxy == NULL)
{
g_warning ("Failed to get proxy");
}
}
if (control_proxy != NULL)
{
dbus_g_proxy_add_signal( control_proxy,
"ScreensaverStarted",
G_TYPE_INVALID
);
dbus_g_proxy_connect_signal( control_proxy,
"ScreensaverStarted",
G_CALLBACK(test_hint_signal_handler),
NULL,
NULL
);
}
g_debug("Starting mainloop");
g_main_loop_run (loop);
return 0;
}
编辑:代码未退出。它正在等待即将到来的事件。这是编译时间警告
dbus-test-client.c: In function ‘main’:
dbus-test-client.c:20:3: warning: ‘g_type_init’ is deprecated [-Wdeprecated-declarations]
g_type_init ();
^
In file included from /usr/include/glib-2.0/gobject/gobject.h:24:0,
from /usr/include/glib-2.0/gobject/gbinding.h:29,
from /usr/include/glib-2.0/glib-object.h:23,
from /usr/include/dbus-1.0/dbus/dbus-glib.h:27,
from dbus-test-client.c:2:
/usr/include/glib-2.0/gobject/gtype.h:681:23: note: declared here
void g_type_init (void);
^
答案 0 :(得分:0)
在顶部代码中添加了--session参数(正在使用会话总线),而在底部C代码中,您连接至SYSTEM总线(DBUS_BUS_SYSTEM)。用C代码将SYSTEM总线切换到SESSION总线(DBUS_BUS_SESSION),它将起作用:
dbus_glib_connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);