example-signal-recipient.c

00001 #include <dbus/dbus-glib.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 
00005 static void lose (const char *fmt, ...) G_GNUC_NORETURN G_GNUC_PRINTF (1, 2);
00006 static void lose_gerror (const char *prefix, GError *error) G_GNUC_NORETURN;
00007 
00008 static void
00009 lose (const char *str, ...)
00010 {
00011   va_list args;
00012 
00013   va_start (args, str);
00014 
00015   vfprintf (stderr, str, args);
00016   fputc ('\n', stderr);
00017 
00018   va_end (args);
00019 
00020   exit (1);
00021 }
00022 
00023 static void
00024 lose_gerror (const char *prefix, GError *error) 
00025 {
00026   lose ("%s: %s", prefix, error->message);
00027 }
00028 
00029 static gboolean
00030 emit_signal (gpointer arg)
00031 {
00032   DBusGProxy *proxy = arg;
00033   
00034   dbus_g_proxy_call_no_reply (proxy, "emitHelloSignal", G_TYPE_INVALID);
00035   return TRUE;
00036 }
00037 
00038 static void
00039 hello_signal_handler (DBusGProxy *proxy, const char *hello_string, gpointer user_data)
00040 {
00041   printf ("Received signal and it says: %s\n", hello_string);
00042 }
00043 
00044 int
00045 main (int argc, char **argv)
00046 {
00047   DBusGConnection *bus;
00048   DBusGProxy *remote_object;
00049   GError *error = NULL;
00050   GMainLoop *mainloop;
00051 
00052   g_type_init ();
00053 
00054   mainloop = g_main_loop_new (NULL, FALSE);
00055 
00056   bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
00057   if (!bus)
00058     lose_gerror ("Couldn't connect to session bus", error);
00059   
00060   /* We use _for_name_owner in order to track this particular service
00061    * instance, which lets us receive signals.
00062    */
00063   remote_object = dbus_g_proxy_new_for_name (bus,
00064                                              "org.designfu.TestService",
00065                                              "/org/designfu/TestService/object",
00066                                              "org.designfu.TestService");
00067   if (!remote_object)
00068     lose_gerror ("Failed to get name owner", error);
00069 
00070   /* IMPORTANT:
00071    *
00072    * Note because this signal's signature is VOID__STRING, we do not
00073    * need to register a marshaller, since there is a builtin one.
00074    * However for other signatures, you must generate a marshaller,
00075    * then call dbus_g_object_register_marshaller.  It would look like
00076    * this:
00077    * 
00078    * dbus_g_object_register_marshaller (g_cclosure_marshal_VOID__STRING, G_TYPE_NONE, G_TYPE_STRING, G_TYPE_INVALID);
00079    *
00080    */
00081 
00082   /* Tell DBus what the type signature of the signal callback is; this
00083    * allows us to sanity-check incoming messages before invoking the
00084    * callback.  You need to do this once for each proxy you create,
00085    * not every time you want to connect to the signal.
00086    */
00087   dbus_g_proxy_add_signal (remote_object, "HelloSignal", G_TYPE_STRING, G_TYPE_INVALID);
00088 
00089   /* Actually connect to the signal.  Note you can call
00090    * dbus_g_proxy_connect_signal multiple times for one invocation of
00091    * dbus_g_proxy_add_signal.
00092    */
00093   dbus_g_proxy_connect_signal (remote_object, "HelloSignal", G_CALLBACK (hello_signal_handler),
00094                                NULL, NULL);
00095   
00096 
00097   g_timeout_add (2000, emit_signal, remote_object);
00098 
00099   g_main_loop_run (mainloop);
00100 
00101   exit (0);
00102 }

Generated on Wed Oct 3 10:04:23 2007 for D-BUSGLibBindings by  doxygen 1.5.1