【发布时间】:2016-07-16 13:08:57
【问题描述】:
我正在尝试从 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);
^
【问题讨论】:
-
你能在主循环之后添加一些
g_debug调用,以确保你没有退出它。您在编译或执行时收到警告了吗? -
在问题本身中添加了评论