0

I am working on a small project .My OS is Linux (Ubuntu 11.04). I want to connect to the DBus Daemon signal NameOwnerChanged in order to be indicated which app is shutdown or startup.I wrote a small program to do this. It failed though. I used glib dbus only, without dbus lowlevel.

Here is my code:

/gcc -o test main.c pkg-config --libs --cflags glib-2.0 dbus-1 dbus-glib-1/

#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-bindings.h>
#include <dbus/dbus-glib-lowlevel.h>

#define D_SERVICE     "org.freedesktop.DBus"
#define D_PATH        "/"
#define D_INTERFACE   "org.freedesktop.DBus"

GMainLoop* loop;
DBusGConnection* conn;
DBusGProxy*     d_proxy;

static void dbus_name_owner_changed(DBusGProxy *proxy, char* name, char* old, char* new, gpointer user_data)
{
    g_print("%s owner change \n", name);
}

int main(int argc,char** argv)
{
    g_type_init();
    loop = g_main_loop_new(NULL, FALSE);
    conn = dbus_g_bus_get(DBUS_BUS_SESSION,NULL);
    d_proxy = dbus_g_proxy_new_for_name(conn, D_SERVICE, D_PATH, D_INTERFACE);

    guint ret;
    GError * error = NULL;

    dbus_g_proxy_call(  d_proxy, "RequestName", &error,
                        G_TYPE_STRING, "com.asianux.test",
                        G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
                        G_TYPE_INVALID,
                        G_TYPE_UINT, &ret,
                        G_TYPE_INVALID);

    if(error==NULL && ret==DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
    {
        g_print("Request Name Success \n");
    }
    else
    {
        if(error)
        {
            g_print("Request Name Failed %s\n", error->message);
        }
    }

    dbus_g_proxy_add_signal(d_proxy,"NameOwnerChanged",
                            G_TYPE_STRING,
                            G_TYPE_STRING,
                            G_TYPE_STRING,
                            G_TYPE_INVALID);

    dbus_g_proxy_connect_signal(d_proxy,"NameOwnerChanged",
                                G_CALLBACK(dbus_name_owner_changed),
                                NULL,NULL);

    g_main_loop_run(loop);
}

I compiled it and ran it, but the function dbus_name_owner_changed never ran. Why can't I connect the signal successfully?

I know I missed dbus_g_object_register_marshaller before add signal and connect the signal. But, even if I add the dbus_g_object_register_marshaller on my code, it still fails. Why?

Flexo
  • 87,323
  • 22
  • 191
  • 272
wang
  • 53
  • 5

1 Answers1

0

I have solve it .Because the D_PATH should be the "/org/freedesktop/DBus" .

wang
  • 53
  • 5