我正在尝试从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);
^发布于 2019-04-19 16:56:43
在最上面的代码中添加了--会话参数(使用会话总线),而在最下面的C代码中连接到系统总线(DBUS_BUS_SYSTEM)。在C代码中将系统总线切换到会话总线(DBUS_BUS_SESSION),它将工作:
dbus_glib_connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);https://stackoverflow.com/questions/36276496
复制相似问题