problems with simple glib and dbus example

Diego Jacobi jacobidiego at gmail.com
Thu Sep 25 19:51:10 PDT 2008


Hello List, i am trying to do an application using dbus and glib, but
it seems like the signals that i request are never received, so i made
a much simpler example and also doesnt works.

Can you please tell me why?


/* Compile With:
** gcc `pkg-config --cflags --libs glib-2.0 dbus-glib-1` signals.c -o signals
**
** Sources:
** http://dbus.freedesktop.org/doc/dbus-tutorial.html
** https://stage.maemo.org/svn/maemo/projects/haf/doc/api/dbus-glib/example-signal-recipient_8c-source.html
*/

#include <dbus/dbus-glib.h>
#include <stdlib.h>

GMainLoop *mainloop;

static gboolean emit_signal (gpointer arg)
{
 DBusGProxy *proxy = arg;

 g_printf ("Signal Emitted\n");
 dbus_g_proxy_call_no_reply (proxy, "HelloSignal",
                             G_TYPE_STRING, "Hello world",
                             G_TYPE_INVALID,
                             G_TYPE_INVALID);
 return TRUE;
}

static void hello_signal_handler (DBusGProxy *proxy, GString
hello_string, gpointer user_data) {
 g_printf ("Received signal and it says: %s\n", hello_string);
 g_main_loop_quit(mainloop);
}

static void destroy_signal(GObject *sender, gpointer data)
{
       //g_debug("'destroy' signal triggered");
       g_main_loop_quit(mainloop);
}

int main (int argc, char **argv)
{
 DBusGConnection *bus;
 DBusGProxy *proxy;
 GError *error = NULL;

 g_type_init ();

 mainloop = g_main_loop_new (NULL, FALSE);

 bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
 if (!bus){
   g_printerr("Couldn't connect to session bus: %s", error->message);
   exit (1);
   }

 /* We use _for_name_owner in order to track this particular service
 * instance, which lets us receive signals.
 */
 proxy = dbus_g_proxy_new_for_name (bus,
                                           "org.designfu.TestService",
                                           "/org/designfu/TestService/object",
                                           "org.designfu.TestService");
 if (!proxy){
   g_printerr("Failed to get name owner: %s", error->message);
   exit (1);
   }

 /* IMPORTANT:
  * Note because this signal's signature is VOID__STRING, we do not
  * need to register a marshaller, since there is a builtin one.
  * However for other signatures, you must generate a marshaller,
  * then call dbus_g_object_register_marshaller.  It would look like
  * this:
  * dbus_g_object_register_marshaller
(g_cclosure_marshal_VOID__STRING, G_TYPE_NONE, G_TYPE_STRING,
G_TYPE_INVALID);
  */

  /* Tell DBus what the type signature of the signal callback is; this
   * allows us to sanity-check incoming messages before invoking the
   * callback.  You need to do this once for each proxy you create,
   * not every time you want to connect to the signal.
   */
 dbus_g_proxy_add_signal (proxy, "HelloSignal", G_TYPE_STRING, G_TYPE_INVALID);

 /* Actually connect to the signal.  Note you can call
  * dbus_g_proxy_connect_signal multiple times for one invocation of
  * dbus_g_proxy_add_signal.
  */
 dbus_g_proxy_connect_signal (proxy, "HelloSignal", G_CALLBACK
(hello_signal_handler),NULL, NULL);

 g_signal_connect (G_OBJECT (proxy), "destroy",G_CALLBACK
(destroy_signal), NULL);

 g_timeout_add (2000, emit_signal, proxy);

 g_main_loop_run (mainloop);

 exit (0);
}



The code just emits a signals every 2 seconds and should also receive
it and quit from the mainloop the first time, but it never quits.


More information about the dbus mailing list