## Exporting objects

With dbus-glib, exporting an object over D-Bus works by generating a bunch of glue code from your introspection XML with dbus-binding-tool. The glue code gets included in your source, and you need to call

    1
    2
    dbus_g_object_type_install_info (TYPE_MYOBJECT,
                                     &dbus_glib_myobject_object_info);

in your `class_init()` function to tell dbus-glib about your type. To actually export an instance, you call

    1
    2
    3
    dbus_g_connection_register_g_object (system_bus_connection,
                                         my_object_path,
                                         G_OBJECT (my_object));

The GDBus way of exporting an object works by embedding the introspection XML in the source, creating introspection data structures from it with `g_dbus_node_info_new_for_xml()`, and passing that along when you register the object:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    static const gchar introspection_xml[] =
      "<node>"
      "  <interface name='org.gtk.GDBus.TestPeerInterface'>"
      "    <method name='HelloWorld'>"
      "      <arg type='s' name='greeting' direction='in'/>"
      "      <arg type='s' name='response' direction='out'/>"
      "    </method>"
      "  </interface>"
      "</node>";

    /* parse introspection data */
    introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);

    /
    id = g_dbus_connection_register_object (connection,
                                            "/org/gtk/GDBus/TestObject",
                                            "org.gtk.GDBus.TestPeerInterface",
                                            introspection_data->interfaces[0],
                                            &interface_vtable,
                                            NULL,  /* user_data */
                                            NULL,  /* user_data_free_func */
                                            NULL); /* GError** */

The actual implementation of the exported object is done by specifying a GDBusInterfaceVTable that has `method_call()`, `get_property()` and `set_property()` methods. There is no direct support beyond that for exporting GObjects, so there is quite a bit of manual work involved, as you can see in the following example.

Since the VTable methods don't have any direct GObject support, we pass the exported object as `user_data`. Also note that we have to handle the emission of the PropertiesChanged signal ourselves, by connecting to ::notify.

Example 20. Exporting a GObject

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    #include <gio/gio.h>
    #include <stdlib.h>

    /* ---------------------------------------------------------------------------------------------------- */

    /* The object we want to export */
    typedef struct _MyObjectClass MyObjectClass;
    typedef struct _MyObject MyObject;

    struct _MyObjectClass
    {
      GObjectClass parent_class;
    };

    struct _MyObject
    {
      GObject parent_instance;

      gint count;
      gchar *name;
    };

    enum
    {
      PROP_0,
      PROP_COUNT,
      PROP_NAME
    };

    G_DEFINE_TYPE (MyObject, my_object, G_TYPE_OBJECT);

    static void
    my_object_finalize (GObject *object)
    {
      MyObject *myobj = (MyObject*)object;

      g_free (myobj->name);

      G_OBJECT_CLASS (my_object_parent_class)->finalize (object);
    }

    static void
    my_object_init (MyObject *object)
    {
      object->count = 0;
      object->name = NULL;
    }

    static void
    my_object_get_property (GObject    *object,
                            guint       prop_id,
                            GValue     *value,
                            GParamSpec *pspec)
    {
      MyObject *myobj = (MyObject*)object;

      switch (prop_id)
        {
        case PROP_COUNT:
          g_value_set_int (value, myobj->count);
          break;

        case PROP_NAME:
          g_value_set_string (value, myobj->name);
          break;

        default:
          G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        }
    }

    static void
    my_object_set_property (GObject      *object,
                            guint         prop_id,
                            const GValue *value,
                            GParamSpec   *pspec)
    {
      MyObject *myobj = (MyObject*)object;

      switch (prop_id)
        {
        case PROP_COUNT:
          myobj->count = g_value_get_int (value);
          break;

        case PROP_NAME:
          g_free (myobj->name);
          myobj->name = g_value_dup_string (value);
          break;

        default:
          G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        }
    }

    static void
    my_object_class_init (MyObjectClass *class)
    {
      GObjectClass *gobject_class = G_OBJECT_CLASS (class);

      gobject_class->finalize = my_object_finalize;
      gobject_class->set_property = my_object_set_property;
      gobject_class->get_property = my_object_get_property;

      g_object_class_install_property (gobject_class,
                                       PROP_COUNT,
                                       g_param_spec_int ("count",
                                                         "Count",
                                                         "Count",
                                                         0, 99999, 0,
                                                         G_PARAM_READWRITE));

      g_object_class_install_property (gobject_class,
                                       PROP_NAME,
                                       g_param_spec_string ("name",
                                                            "Name",
                                                            "Name",
                                                            NULL,
                                                            G_PARAM_READWRITE));
    }

    /* A method that we want to export */
    void
    my_object_change_count (MyObject *myobj,
                            gint      change)
    {
      myobj->count = 2 * myobj->count + change;

      g_object_notify (G_OBJECT (myobj), "count");
    }

    /* ---------------------------------------------------------------------------------------------------- */

    static GDBusNodeInfo *introspection_data = NULL;

    /* Introspection data for the service we are exporting */
    static const gchar introspection_xml[] =
      "<node>"
      "  <interface name='org.myorg.MyObject'>"
      "    <method name='ChangeCount'>"
      "      <arg type='i' name='change' direction='in'/>"
      "    </method>"
      "    <property type='i' name='Count' access='read'/>"
      "    <property type='s' name='Name' access='readwrite'/>"
      "  </interface>"
      "</node>";

    static void
    handle_method_call (GDBusConnection       *connection,
                        const gchar           *sender,
                        const gchar           *object_path,
                        const gchar           *interface_name,
                        const gchar           *method_name,
                        GVariant              *parameters,
                        GDBusMethodInvocation *invocation,
                        gpointer               user_data)
    {
      MyObject *myobj = user_data;

      if (g_strcmp0 (method_name, "ChangeCount") == 0)
        {
          gint change;
          g_variant_get (parameters, "(i)", &change);

          my_object_change_count (myobj, change);

          g_dbus_method_invocation_return_value (invocation, NULL);
        }
    }

    static GVariant *
    handle_get_property (GDBusConnection  *connection,
                         const gchar      *sender,
                         const gchar      *object_path,
                         const gchar      *interface_name,
                         const gchar      *property_name,
                         GError          **error,
                         gpointer          user_data)
    {
      GVariant *ret;
      MyObject *myobj = user_data;

      ret = NULL;
      if (g_strcmp0 (property_name, "Count") == 0)
        {
          ret = g_variant_new_int32 (myobj->count);
        }
      else if (g_strcmp0 (property_name, "Name") == 0)
        {
          ret = g_variant_new_string (myobj->name ? myobj->name : "");
        }

      return ret;
    }

    static gboolean
    handle_set_property (GDBusConnection  *connection,
                         const gchar      *sender,
                         const gchar      *object_path,
                         const gchar      *interface_name,
                         const gchar      *property_name,
                         GVariant         *value,
                         GError          **error,
                         gpointer          user_data)
    {
      MyObject *myobj = user_data;

      if (g_strcmp0 (property_name, "Count") == 0)
        {
          g_object_set (myobj, "count", g_variant_get_int32 (value), NULL);
        }
      else if (g_strcmp0 (property_name, "Name") == 0)
        {
          g_object_set (myobj, "name", g_variant_get_string (value, NULL), NULL);
        }

      return TRUE;
    }

    /* for now */
    static const GDBusInterfaceVTable interface_vtable =
    {
      handle_method_call,
      handle_get_property,
      handle_set_property
    };

    static void
    send_property_change (GObject         *obj,
                          GParamSpec      *pspec,
                          GDBusConnection *connection)
    {
      GVariantBuilder *builder;
      GVariantBuilder *invalidated_builder;
      MyObject *myobj = (MyObject *)obj;

      builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
      invalidated_builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));

      if (g_strcmp0 (pspec->name, "count") == 0)
        g_variant_builder_add (builder,
                               "{sv}",
                               "Count", g_variant_new_int32 (myobj->count));
      else if (g_strcmp0 (pspec->name, "name") == 0)
        g_variant_builder_add (builder,
                               "{sv}",
                               "Name", g_variant_new_string (myobj->name ? myobj->name : ""));

      g_dbus_connection_emit_signal (connection,
                                     NULL,
                                     "/org/myorg/MyObject",
                                     "org.freedesktop.DBus.Properties",
                                     "PropertiesChanged",
                                     g_variant_new ("(sa{sv}as)",
                                                    "org.myorg.MyObject",
                                                    builder,
                                                    invalidated_builder),
                                     NULL);
    }

    static void
    on_bus_acquired (GDBusConnection *connection,
                     const gchar     *name,
                     gpointer         user_data)
    {
      MyObject *myobj = user_data;
      guint registration_id;

      g_signal_connect (myobj, "notify",
                        G_CALLBACK (send_property_change), connection);
      registration_id = g_dbus_connection_register_object (connection,
                                                           "/org/myorg/MyObject",
                                                           introspection_data->interfaces[0],
                                                           &interface_vtable,
                                                           myobj,
                                                           NULL,  /* user_data_free_func */
                                                           NULL); /* GError** */
      g_assert (registration_id > 0);
    }

    static void
    on_name_acquired (GDBusConnection *connection,
                      const gchar     *name,
                      gpointer         user_data)
    {
    }

    static void
    on_name_lost (GDBusConnection *connection,
                  const gchar     *name,
                  gpointer         user_data)
    {
      exit (1);
    }

    int
    main (int argc, char *argv[])
    {
      guint owner_id;
      GMainLoop *loop;
      MyObject *myobj;

      g_type_init ();

      /* We are lazy here - we don't want to manually provide
       * the introspection data structures - so we just build
       * them from XML.
       */
      introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
      g_assert (introspection_data != NULL);

      myobj = g_object_new (my_object_get_type (), NULL);

      owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
                                 "org.myorg.MyObject",
                                 G_BUS_NAME_OWNER_FLAGS_NONE,
                                 on_bus_acquired,
                                 on_name_acquired,
                                 on_name_lost,
                                 myobj,
                                 NULL);

      loop = g_main_loop_new (NULL, FALSE);
      g_main_loop_run (loop);

      g_bus_unown_name (owner_id);

      g_dbus_node_info_unref (introspection_data);

      g_object_unref (myobj);

      return 0;
    }