Indentation fix, cleanup.
[AROS.git] / rom / dbus / test / dbus-ping-listen.c
blob6bc2466a7398fc5bffe2bb28b526e2c65bfb8a60
1 #include <glib.h>
2 #include <dbus/dbus.h>
3 #include <dbus/dbus-glib.h>
5 static DBusHandlerResult signal_filter
6 (DBusConnection *connection, DBusMessage *message, void *user_data);
8 int
9 main (int argc, char **argv)
11 GMainLoop *loop;
12 DBusConnection *bus;
13 DBusError error;
15 loop = g_main_loop_new (NULL, FALSE);
17 dbus_error_init (&error);
18 bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
19 if (!bus) {
20 g_warning ("Failed to connect to the D-BUS daemon: %s", error.message);
21 dbus_error_free (&error);
22 return 1;
24 dbus_connection_setup_with_g_main (bus, NULL);
26 /* listening to messages from all objects as no path is specified */
27 dbus_bus_add_match (bus, "type='signal',interface='com.burtonini.dbus.Signal'", &error);
28 dbus_connection_add_filter (bus, signal_filter, loop, NULL);
30 g_main_loop_run (loop);
31 return 0;
34 static DBusHandlerResult
35 signal_filter (DBusConnection *connection, DBusMessage *message, void *user_data)
37 /* User data is the event loop we are running in */
38 GMainLoop *loop = user_data;
40 /* A signal from the bus saying we are about to be disconnected */
41 if (dbus_message_is_signal
42 (message, DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL, "Disconnected")) {
43 /* Tell the main loop to quit */
44 g_main_loop_quit (loop);
45 /* We have handled this message, don't pass it on */
46 return DBUS_HANDLER_RESULT_HANDLED;
48 /* A Ping signal on the com.burtonini.dbus.Signal interface */
49 else if (dbus_message_is_signal (message, "com.burtonini.dbus.Signal", "Ping")) {
50 DBusError error;
51 char *s;
52 dbus_error_init (&error);
53 if (dbus_message_get_args
54 (message, &error, DBUS_TYPE_STRING, &s, DBUS_TYPE_INVALID)) {
55 g_print("Ping received: %s\n", s);
56 dbus_free (s);
57 } else {
58 g_print("Ping received, but error getting message: %s\n", error.message);
59 dbus_error_free (&error);
61 return DBUS_HANDLER_RESULT_HANDLED;
63 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;