Add gdbus-codegen(1) command
authorDavid Zeuthen <davidz@redhat.com>
Fri, 8 Apr 2011 20:57:31 +0000 (16:57 -0400)
committerDavid Zeuthen <davidz@redhat.com>
Fri, 8 Apr 2011 21:11:38 +0000 (17:11 -0400)
It doesn't work yet without the user manually adding ffi bits. That's
the topic of the next commit.

Signed-off-by: David Zeuthen <davidz@redhat.com>
15 files changed:
configure.ac
docs/reference/gio/Makefile.am
docs/reference/gio/gdbus-codegen.xml [new file with mode: 0644]
docs/reference/gio/gio-docs.xml
gio/Makefile.am
gio/gdbus-codegen/Makefile.am [new file with mode: 0644]
gio/gdbus-codegen/__init__.py [new file with mode: 0644]
gio/gdbus-codegen/codegen.py [new file with mode: 0644]
gio/gdbus-codegen/codegen_docbook.py [new file with mode: 0644]
gio/gdbus-codegen/codegen_main.py [new file with mode: 0644]
gio/gdbus-codegen/config.py.in [new file with mode: 0644]
gio/gdbus-codegen/dbustypes.py [new file with mode: 0644]
gio/gdbus-codegen/gdbus-codegen.in [new file with mode: 0755]
gio/gdbus-codegen/parser.py [new file with mode: 0644]
gio/gdbus-codegen/utils.py [new file with mode: 0644]

index 1fafc76..683c770 100644 (file)
@@ -3770,6 +3770,8 @@ gobject/tests/Makefile
 gthread/Makefile
 gthread/tests/Makefile
 gio/Makefile
+gio/gdbus-codegen/Makefile
+gio/gdbus-codegen/config.py
 gio/xdgmime/Makefile
 gio/inotify/Makefile
 gio/libasyncns/Makefile
index a690ece..6c522ee 100644 (file)
@@ -149,6 +149,7 @@ content_files =                     \
        glib-compile-schemas.xml\
        gsettings.xml           \
        gdbus.xml               \
+       gdbus-codegen.xml       \
        $(NULL)
 
 expand_content_files =         \
@@ -170,8 +171,9 @@ EXTRA_DIST +=                       \
 man_MANS =                     \
        gio-querymodules.1      \
        glib-compile-schemas.1  \
-       gsettings.1     \
-       gdbus.1
+       gsettings.1             \
+       gdbus.1                 \
+       gdbus-codegen.1
 
 if ENABLE_MAN
 
diff --git a/docs/reference/gio/gdbus-codegen.xml b/docs/reference/gio/gdbus-codegen.xml
new file mode 100644 (file)
index 0000000..77083f8
--- /dev/null
@@ -0,0 +1,674 @@
+<refentry id="gdbus-codegen" lang="en">
+
+<refmeta>
+  <refentrytitle>gdbus-codegen</refentrytitle>
+  <manvolnum>1</manvolnum>
+  <refmiscinfo class="manual">User Commands</refmiscinfo>
+</refmeta>
+
+<refnamediv>
+  <refname>gdbus-codegen</refname>
+  <refpurpose>D-Bus Code Generator</refpurpose>
+</refnamediv>
+
+<refsynopsisdiv>
+  <cmdsynopsis>
+    <command>gdbus-codegen</command>
+    <arg><option>--interface-prefix</option> <replaceable>org.project.Prefix</replaceable></arg>
+    <arg><option>--c-namespace</option> <replaceable>YourProject</replaceable></arg>
+    <arg><option>--generate-c-code</option> <replaceable>OUTFILES</replaceable></arg>
+    <arg><option>--generate-docbook</option> <replaceable>OUTFILES</replaceable></arg>
+    <group choice="plain" rep="repeat">
+      <arg>
+        <option>--annotate</option>
+        <replaceable>element</replaceable>
+        <option>--key</option>
+        <replaceable>key</replaceable>
+        <option>--value</option>
+        <replaceable>key</replaceable>
+      </arg>
+    </group>
+    <arg choice="plain">FILE</arg>
+    <arg>
+      <arg choice="plain" rep="repeat">FILE</arg>
+    </arg>
+  </cmdsynopsis>
+</refsynopsisdiv>
+
+<refsect1>
+  <title>Description</title>
+  <para>
+    <command>gdbus-codegen</command> is used to generate code and/or
+    documentation for one or more D-Bus interfaces. The tool reads
+    D-Bus Introspection XML files and generates output files. The tool
+    currently supports generating C code (via
+    <option>--generate-c-code</option>) and Docbook XML (via
+    <option>--generate-docbook</option>).
+  </para>
+  <para>
+    When generating C code, an abstract
+    <type>GInterface</type>-derived type is generated for each D-Bus
+    interface. Additionally, for every generated type,
+    <type>FooBar</type>, two concrete instantiable types,
+    <type>FooBarProxy</type> and <type>FooBarStub</type>, implementing
+    said interface are also generated. The former is derived from
+    <type>GDBusProxy</type> and intended for use on the client side
+    while the latter is derived from the
+    <type>GDBusInterfaceStub</type> type making it easy to export on a
+    <type>GDBusConnection</type> either directly or via a
+    <type>GDBusObjectManagerServer</type>.
+  </para>
+</refsect1>
+
+<refsect1>
+  <title>Options</title>
+  <para>
+    The following options are supported:
+  </para>
+  <variablelist>
+
+    <varlistentry>
+      <term><option>--interface-prefix</option> <replaceable>org.project.Prefix.</replaceable></term>
+      <listitem>
+        <para>
+          A prefix to strip from all D-Bus interface names when
+          calculating the typename for the C binding and the Docbook
+          <ulink
+          url="http://www.docbook.org/tdg/en/html/primary.html">sortas
+          attribute</ulink>.
+        </para>
+      </listitem>
+    </varlistentry>
+
+    <varlistentry>
+      <term><option>--generate-docbook</option> <replaceable>OUTFILES</replaceable></term>
+      <listitem>
+        <para>
+          Generate Docbook Documentation for each D-Bus interface and
+          put it in
+          <filename>OUTFILES-org.Project.IfaceName.xml</filename> (where
+          <literal>org.Project.IfaceName</literal> is a place-holder for
+          the interface name).
+        </para>
+      </listitem>
+    </varlistentry>
+
+    <varlistentry>
+      <term><option>--generate-c-code</option> <replaceable>OUTFILES</replaceable></term>
+      <listitem>
+        <para>
+          Generate C code for all D-Bus interfaces and put it in
+          <filename>OUTFILES.c</filename> and
+          <filename>OUTFILES.h</filename>.
+        </para>
+      </listitem>
+    </varlistentry>
+
+    <varlistentry>
+      <term><option>--c-namespace</option> <replaceable>YourProject</replaceable></term>
+      <listitem>
+        <para>
+          The namespace to use for generated C code. This must be
+          provided in CamelCase format.
+        </para>
+      </listitem>
+    </varlistentry>
+
+    <varlistentry>
+      <term><option>--annotate</option></term>
+      <listitem>
+        <para>
+          Used together with <option>--key</option> and
+          <option>--value</option> to annotate the given XML files. It
+          can be used with interfaces, methods, signals, properties
+          and arguments in the following way:
+        </para>
+<informalexample><programlisting><![CDATA[
+gdbus-codegen --c-namespace MyApp                           \
+  --generate-c-code myapp-generated                         \
+  --annotate "org.project.InterfaceName"                    \
+    --key org.gtk.GDBus.Name --value MyFrobnicator          \
+  --annotate "org.project.InterfaceName:Property"           \
+    --key bar --value bat                                   \
+  --annotate "org.project.InterfaceName.Method()"           \
+    --key org.freedesktop.DBus.Deprecated --value true      \
+  --annotate "org.project.InterfaceName.Method()[arg_name]" \
+    --key snake --value hiss                                \
+  --annotate "org.project.InterfaceName::Signal"            \
+    --key cat --value meow                                  \
+  --annotate "org.project.InterfaceName::Signal[arg_name]"  \
+    --key dog --value wuff                                  \
+  myapp-dbus-interfaces.xml
+]]></programlisting></informalexample>
+      </listitem>
+    </varlistentry>
+  </variablelist>
+</refsect1>
+
+<refsect1>
+  <title>Supported D-Bus Annotations</title>
+  <para>
+    The following D-Bus annotations are supported by
+    <command>gdbus-codegen</command>:
+  </para>
+
+  <variablelist>
+
+    <varlistentry>
+      <term><literal>org.gtk.GDBus.Name</literal></term>
+      <listitem>
+        <para>
+          Can be used on any <literal>&lt;interface&gt;</literal>,
+          <literal>&lt;method&gt;</literal>,
+          <literal>&lt;signal&gt;</literal> and
+          <literal>&lt;property&gt;</literal> element to specify the
+          name to use.
+        </para>
+        <para>
+          For interfaces where this annotation is not specified, the
+          name used is the D-Bus interface name stripped with the
+          prefix given with <option>--interface-prefix</option> and with
+          the dots removed and initial characters capitalized. For
+          example the D-Bus interface
+          <literal>com.acme.Coyote</literal> the name used is
+          <type>ComAcmeCoyote</type>. For the D-Bus interface
+          <literal>org.project.Bar.Frobnicator</literal> with
+          <option>--interface-prefix</option>
+          <literal>org.project.</literal>, the name used is
+          <type>BarFrobnicator</type>.
+        </para>
+        <para>
+          For methods, signals and properties the name used is
+          calculated by transforming
+          <literal>NameGivenThisWay</literal> into
+          <literal>name_given_this_way</literal>, e.g. roughly
+          converting from camel-case to lower-case with underscores
+          using certain heuristics.
+        </para>
+      </listitem>
+    </varlistentry>
+
+    <varlistentry>
+      <term><literal>org.gtk.GDBus.C.ForceGVariant</literal></term>
+      <listitem>
+        <para>
+          If set to a non-empty string, a <type>GVariant</type> will
+          be used instead of the natural C type. This annotation can
+          be used on any <literal>&lt;arg&gt;</literal> and
+          <literal>&lt;property&gt;</literal> element.
+        </para>
+      </listitem>
+    </varlistentry>
+
+    <varlistentry>
+      <term><literal>org.gtk.GDBus.DocString</literal></term>
+      <listitem>
+        <para>
+          A string with Docbook content for documentation. This annotation can
+          be used on <literal>&lt;interface&gt;</literal>,
+          <literal>&lt;method&gt;</literal>,
+          <literal>&lt;signal&gt;</literal>,
+          <literal>&lt;property&gt;</literal> and
+          <literal>&lt;arg&gt;</literal> elements.
+        </para>
+      </listitem>
+    </varlistentry>
+
+    <varlistentry>
+      <term><literal>org.gtk.GDBus.DocString.Short</literal></term>
+      <listitem>
+        <para>
+          A string with Docbook content for short/brief
+          documentation. This annotation can only be used on
+          <literal>&lt;interface&gt;</literal> elements.
+        </para>
+      </listitem>
+    </varlistentry>
+
+  </variablelist>
+
+  <para>
+    As an easier alternative to using the
+    <literal>org.gtk.GDBus.DocString</literal> annotation, note that
+    XML parser used by <command>gdbus-codegen</command> parses XML
+    comments in a way similar to <ulink
+    url="http://www.gtk.org/gtk-doc/">gtk-doc</ulink>:
+<informalexample><programlisting><![CDATA[
+<!--
+  net.Corp.Bar:
+  @short_description: A short description
+
+  A <emphasis>longer</emphasis> description.
+
+  This is a new paragraph.
+-->
+<interface name="net.corp.Bar">
+  <!--
+    FooMethod:
+    @greeting: The docs for greeting parameter.
+    @response: The docs for response parameter.
+
+    The docs for the actual method.
+  -->
+  <method name="FooMethod">
+    <arg name="greeting" direction="in" type="s"/>
+    <arg name="response" direction="out" type="s"/>
+  </method>
+
+  <!--
+    BarSignal:
+    @blah: The docs for blah parameter.
+    @boo: The docs for boo parameter.
+
+    The docs for the actual signal.
+  -->
+  <signal name="BarSignal">
+    <arg name="blah" type="s"/>
+    <arg name="boo" type="s"/>
+  </signal>
+
+  <!-- BazProperty: The docs for the property. -->
+  <property name="BazProperty" type="s" access="read"/>
+</interface>
+]]></programlisting></informalexample>
+  </para>
+  <para>
+    For the <literal>org.gtk.GDBus.DocString</literal> annotation (and
+    inline comments), note that substrings of the form
+    <literal>#net.Corp.Bar</literal>,
+    <literal>net.Corp.Bar.FooMethod()</literal>,
+    <literal>#net.Corp.Bar::BarSignal</literal> and
+    <literal>#net.Corp.InlineDocs:BazProperty</literal> are all expanded
+    to links to the respective interface, method, signal and property.
+  </para>
+</refsect1>
+
+<refsect1>
+  <title>Example</title>
+  <para>
+    Consider the following D-Bus Introspection XML.
+  </para>
+  <informalexample><programlisting><![CDATA[
+<interface name="net.Corp.MyApp.Frobber">
+  <method name="HelloWorld">
+    <arg name="greeting" direction="in" type="s"/>
+    <arg name="response" direction="out" type="s"/>
+  </method>
+
+  <signal name="Notification">
+    <arg name="icon_blob" type="ay"/>
+    <arg name="height" type="i"/>
+    <arg name="messages" type="as"/>
+  </signal>
+
+  <property name="Verbose" type="b" access="readwrite"/>
+</interface>
+]]></programlisting>
+  </informalexample>
+  <para>
+    If <command>gdbus-codegen</command> is used on this file like this:
+  </para>
+<informalexample><programlisting><![CDATA[
+gdbus-codegen --generate-c-code myapp-generated       \
+              --c-namespace MyApp                     \
+              --interface-prefix net.corp.MyApp.      \
+              net.Corp.MyApp.Frobber.xml
+]]></programlisting></informalexample>
+  <para>
+    two files called
+    <filename>myapp-generated.[ch]</filename> are
+    generated. The files provide an abstract
+    <type>GInterface</type>-derived type called
+    <type>MyAppFrobber</type> as well as two instantiable types with
+    the same name but suffixed with <type>Proxy</type> and
+    <type>Stub</type>. The generated file, roughly, contains the
+    following facilities:
+  </para>
+<informalexample><programlisting><![CDATA[
+/* GType macros for the three generated types */
+#define MY_APP_TYPE_FROBBER (my_app_frobber_get_type ())
+#define MY_APP_TYPE_FROBBER_STUB (my_app_frobber_stub_get_type ())
+#define MY_APP_TYPE_FROBBER_PROXY (my_app_frobber_proxy_get_type ())
+
+typedef struct _MyAppFrobber MyAppFrobber; /* Dummy typedef */
+
+typedef struct
+{
+  GTypeInterface parent_iface;
+
+  /* Signal handler for the ::notification signal */
+  void (*notification) (MyAppFrobber *proxy,
+                        GVariant *icon_blob,
+                        gint height,
+                        const gchar* const *messages);
+
+  /* Signal handler for the ::handle-hello-world signal */
+  gboolean (*handle_hello_world) (MyAppFrobber *proxy,
+                                  GDBusMethodInvocation *invocation,
+                                  const gchar *greeting);
+} MyAppFrobberIface;
+
+/* Asynchronously calls HelloWorld() */
+void
+my_app_frobber_call_hello_world (MyAppFrobber *proxy,
+                                 const gchar *greeting,
+                                 GCancellable *cancellable,
+                                 GAsyncReadyCallback callback,
+                                 gpointer user_data);
+gboolean
+my_app_frobber_call_hello_world_finish (MyAppFrobber *proxy,
+                                        gchar **out_response,
+                                        GAsyncResult *res,
+                                        GError **error);
+
+/* Synchronously calls HelloWorld(). Blocks calling thread. */
+gboolean
+my_app_frobber_call_hello_world_sync (MyAppFrobber *proxy,
+                                      const gchar *greeting,
+                                      gchar **out_response,
+                                      GCancellable *cancellable,
+                                      GError **error);
+
+/* Completes handling the HelloWorld() method call */
+void
+my_app_frobber_complete_hello_world (MyAppFrobber *object,
+                                     GDBusMethodInvocation *invocation,
+                                     const gchar *response);
+
+/* Emits the ::notification signal / Notification() D-Bus signal */
+void
+my_app_frobber_emit_notification (MyAppFrobber *object,
+                                  GVariant *icon_blob,
+                                  gint height,
+                                  const gchar* const *messages);
+
+/* Gets the :verbose GObject property / Verbose D-Bus property.
+ * Does no blocking I/O.
+ */
+gboolean my_app_frobber_get_verbose (MyAppFrobber *object);
+
+/* Sets the :verbose GObject property / Verbose D-Bus property.
+ * Does no blocking I/O.
+ */
+void my_app_frobber_set_verbose (MyAppFrobber *object,
+                                 gboolean      value);
+
+/* Gets the interface info */
+GDBusInterfaceInfo *my_app_frobber_interface_info (void);
+
+/* Creates a new stub object, ready to be exported */
+MyAppFrobber *my_app_frobber_stub_new (void);
+
+/* Client-side proxy constructors.
+ *
+ * Additionally, _new_for_bus(), _new_for_bus_finish() and
+ * _new_for_bus_sync() proxy constructors are also generated.
+ */
+void
+my_app_frobber_proxy_new        (GDBusConnection     *connection,
+                                 GDBusProxyFlags      flags,
+                                 const gchar         *name,
+                                 const gchar         *object_path,
+                                 GCancellable        *cancellable,
+                                 GAsyncReadyCallback  callback,
+                                 gpointer             user_data);
+MyAppFrobber *
+my_app_frobber_proxy_new_finish (GAsyncResult        *res,
+                                 GError             **error);
+MyAppFrobber *
+my_app_frobber_proxy_new_sync   (GDBusConnection     *connection,
+                                 GDBusProxyFlags      flags,
+                                 const gchar         *name,
+                                 const gchar         *object_path,
+                                 GCancellable        *cancellable,
+                                 GError             **error);
+]]></programlisting></informalexample>
+  <para>
+    Thus, for every D-Bus method, there will be three C functions for
+    calling the method, one <type>GObject</type> signal for handling
+    an incoming call and one C function for completing an incoming
+    call. For every D-Bus signal, there's one <type>GObject</type>
+    signal and one C function for emitting it. For every D-Bus
+    property, two C functions are generated (one setter, one getter)
+    and one <type>GObject</type> property. The following table
+    summarizes the generated facilities and where they are applicable:
+  </para>
+  <informaltable>
+    <tgroup cols="3">
+      <thead>
+        <row>
+          <entry></entry>
+          <entry>Client</entry>
+          <entry>Server</entry>
+        </row>
+      </thead>
+      <tbody>
+        <row>
+          <entry>Types</entry>
+          <entry>Use <type>MyAppFrobberProxy</type></entry>
+          <entry>Any type implementing the <type>MyAppFrobber</type> interface</entry>
+        </row>
+        <row>
+          <entry>Methods</entry>
+          <entry>Use <function>m_a_f_hello_world()</function> to call.</entry>
+          <entry>Receive via the <function>handle_hello_world()</function> signal handler. Complete the call with <function>m_a_f_complete_hello_world()</function></entry>
+        </row>
+        <row>
+          <entry>Signals</entry>
+          <entry>Connect to the <function>::notification</function> GObject signal.</entry>
+          <entry>Use <function>m_a_f_emit_notification()</function> to emit signal.</entry>
+        </row>
+        <row>
+          <entry>Properties (Reading)</entry>
+          <entry>Use <function>m_a_f_get_verbose()</function> or <parameter>:verbose</parameter>.</entry>
+          <entry>Implement <type>GObject</type>'s <function>get_property()</function> vfunc.</entry>
+        </row>
+        <row>
+          <entry>Properties (writing)</entry>
+          <entry>Use <function>m_a_f_set_verbose()</function> or <parameter>:verbose</parameter>.</entry>
+          <entry>Implement <type>GObject</type>'s <function>set_property()</function> vfunc.</entry>
+        </row>
+      </tbody>
+    </tgroup>
+  </informaltable>
+
+  <refsect2>
+    <title>Client-side usage</title>
+    <para>
+      You can use the generated proxy type with the generated
+      constructors:
+    </para>
+    <informalexample><programlisting><![CDATA[
+    MyAppFrobber *proxy;
+    GError *error;
+
+    error = NULL;
+    proxy = my_app_frobber_proxy_new_for_bus_sync (
+                G_BUS_TYPE_SESSION,
+                G_DBUS_PROXY_FLAGS_NONE,
+                "net.Corp.MyApp",              /* bus name */
+                "/net/Corp/MyApp/SomeFrobber", /* object */
+                NULL,                          /* GCancellable* */
+                &error);
+    /* do stuff with proxy */
+    g_object_unref (proxy);
+]]></programlisting></informalexample>
+    <para>
+      Instead of using the generic <type>GDBusProxy</type> facilities,
+      one can use the generated methods such as
+      <function>my_app_frobber_call_hello_world()</function> to invoke
+      the <function>net.Corp.MyApp.Frobber.HelloWorld()</function>
+      D-Bus method, connect to the the
+      <function>::notification</function> GObject signal to receive
+      the <function>net.Corp.MyApp.Frobber::Notication</function> D-Bus
+      signal and get/set the
+      <parameter>net.Corp.MyApp.Frobber:Verbose</parameter> D-Bus
+      Property using either the GObject property
+      <parameter>:verbose</parameter> or the
+      <function>my_app_get_verbose()</function> and
+      <function>my_app_set_verbose()</function> methods. Use the
+      standard <function>GObject::notify</function> signal to listen
+      to property changes.
+    </para>
+    <para>
+      Note that all property access is via <type>GDBusProxy</type>'s
+      property cache so no IO is ever done when reading properties.
+      Also note that setting a property will cause
+      <function>org.freedesktop.DBus.Properties.Set()</function> to be
+      called on the remote object. This call, however, is asynchronous
+      so setting a property won't block. Further, the change is
+      delayed and no error checking is possible.
+    </para>
+  </refsect2>
+
+  <refsect2>
+    <title>Server-side usage</title>
+    <para>
+      The generated <type>MyAppFrobber</type> interface is designed so
+      it is easy to implement it in a <type>GObject</type>
+      subclass. For example, to handle
+      <function>HelloWorld()</function> method invocations, set the
+      vfunc for <function>handle_hello_hello_world()</function> in the
+      <type>MyAppFrobberIface</type> structure. Similary, to handle
+      the <parameter>net.Corp.MyApp.Frobber:Verbose</parameter>
+      property override the <parameter>:verbose</parameter> GObject
+      property from the subclass. To emit a signal, use
+      e.g. <function>my_app_emit_signal()</function> or
+      <function>g_signal_emit_by_name()</function>.
+    </para>
+    <para>
+      Instead of subclassing, it is often easier to use the generated
+      <type>MyAppFrobberStub</type> subclass. To handle incoming
+      method calls, use <function>g_signal_connect()</function> with
+      the <function>::handle-*</function> signals and instead of
+      overriding <type>GObject</type>'s
+      <function>get_property()</function> and
+      <function>set_property()</function> vfuncs, use
+      <function>g_object_get()</function> and
+      <function>g_object_set()</function> or the generated property
+      getters and setters (the generated class has an internal
+      property bag implementation).
+    </para>
+    <informalexample><programlisting><![CDATA[
+static gboolean
+on_handle_hello_world (MyAppFrobber           *object,
+                       GDBusMethodInvocation  *invocation,
+                       const gchar            *greeting,
+                       gpointer                user_data)
+{
+  if (g_strcmp0 (greeting, "Boo") != 0)
+    {
+      gchar *response;
+      response = g_strdup_printf ("Word! You said `%s'.", greeting);
+      my_app_complete_hello_world (object, invocation, response);
+      g_free (response);
+    }
+  else
+    {
+      g_dbus_method_invocation_return_error (MY_APP_ERROR,
+                 MY_APP_ERROR_NO_WHINING,
+                 "Hey, %s, there will be no whining!",
+                 g_dbus_method_invocation_get_sender (invocation));
+    }
+  return TRUE;
+}
+
+  [...]
+
+  object = my_app_frobber_stub_new ();
+  my_app_frobber_set_verbose (object, TRUE);
+
+  g_signal_connect (object,
+                    "handle-hello-world",
+                    G_CALLBACK (on_handle_hello-world),
+                    some_user_data);
+
+  [...]
+
+  error = NULL;
+  id = g_dbus_interface_register_object (G_DBUS_INTERFACE (object),
+                                         connection,
+                                         "/path/of/object",
+                                         &error);
+]]></programlisting></informalexample>
+    <para>
+      To facility atomic changesets (multiple properties changing at
+      the same time), <function>GObject::notify</function> signals are
+      queued up when received. The queue is drained in an idle handler
+      and will cause emissions of the
+      <function>org.freedesktop.DBus.Properties::PropertiesChanged</function>
+      signal with all the properties that has changed. Use
+      <function>g_dbus_interface_stub_flush()</function> to empty the
+      queue immediately.
+    </para>
+  </refsect2>
+</refsect1>
+
+<refsect1>
+  <title>C Type Mapping</title>
+  <para>
+    Scalar types, strings (including object paths (type-string
+    <literal>o</literal>), signatures (type-string
+    <literal>g</literal>) and bytestrings (type-string
+    <literal>ay</literal>)) and arrays of string (type-string
+    <literal>as</literal>) and arrays of bytestrings (type-string
+    <literal>aay</literal>) are mapped to the natural types,
+    e.g. <type>gboolean</type>, <type>gdouble</type>,
+    <type>gint</type>, <type>gchar*</type>, <type>gchar **</type> and
+    so on. Everything else is mapped to the <type>GVariant</type>
+    type.
+  </para>
+  <para>
+    This automatic mapping can be turned off by using the annotation
+    <literal>org.gtk.GDBus.C.ForceGVariant</literal> - if used then a
+    <type>GVariant</type> is always exchanged instead of the
+    corresponding native C type. This annotation may be convenient to
+    use when using the type-string <literal>ay</literal> for data with
+    embedded NUL bytes.
+  </para>
+</refsect1>
+
+<refsect1>
+  <title>Stability Guarantees</title>
+  <para>
+    No guarantees about the API and ABI of the code generated by
+    <command>gdbus-codegen</command> are given. This means that code
+    generated by future versions of this program may have a different
+    API or ABI even if the underlying D-Bus interface hasn't
+    changed. As such, always include the generated code in
+    distribution tarballs and never expose the code in any stable
+    interfaces.
+  </para>
+  <para>
+    Future versions of <command>gdbus-codegen</command> will provide
+    ABI and API guarantees on the generated code.
+  </para>
+</refsect1>
+
+<refsect1>
+  <title>Author</title>
+  <para>
+    Written by David Zeuthen <email>zeuthen@gmail.com</email> with
+    a lot of help from many others.
+  </para>
+</refsect1>
+
+<refsect1>
+  <title>Bugs</title>
+  <para>
+    Please send bug reports to either the distribution bug tracker
+    or the upstream bug tracker at
+    <ulink url="https://bugzilla.gnome.org/enter_bug.cgi?product=glib"/>.
+  </para>
+</refsect1>
+
+<refsect1>
+  <title>See also</title>
+  <para>
+    <citerefentry>
+      <refentrytitle>gdbus</refentrytitle><manvolnum>1</manvolnum>
+    </citerefentry>
+  </para>
+</refsect1>
+
+</refentry>
index 2534e52..4b90518 100644 (file)
         <xi:include href="gsettings.xml"/>
         <xi:include href="glib-compile-schemas.xml"/>
         <xi:include href="gdbus.xml"/>
+        <xi:include href="gdbus-codegen.xml"/>
     </chapter>
   </part>
 
index 318c22c..afcbc9e 100644 (file)
@@ -2,7 +2,7 @@ include $(top_srcdir)/Makefile.decl
 
 NULL =
 
-SUBDIRS=
+SUBDIRS = gdbus-codegen
 
 if OS_UNIX
 SUBDIRS += libasyncns xdgmime
diff --git a/gio/gdbus-codegen/Makefile.am b/gio/gdbus-codegen/Makefile.am
new file mode 100644 (file)
index 0000000..98bc4f7
--- /dev/null
@@ -0,0 +1,27 @@
+
+NULL =
+bin_SCRIPTS =
+EXTRA_DIST =
+
+codegendir = $(libdir)/gdbus-codegen
+codegen_PYTHON =                                               \
+       __init__.py                                             \
+       codegen.py                                              \
+       codegen_main.py                                         \
+       codegen_docbook.py                                      \
+       config.py                                               \
+       dbustypes.py                                            \
+       parser.py                                               \
+       utils.py                                                \
+       $(NULL)
+
+bin_SCRIPTS += gdbus-codegen
+
+EXTRA_DIST += gdbus-codegen.in
+
+gdbus-codegen: gdbus-codegen.in Makefile
+       $(AM_V_GEN) sed -e s,@libdir\@,$(libdir), -e s,@PYTHON\@,$(PYTHON), $< > $@.tmp && mv $@.tmp $@
+       @chmod a+x $@
+
+clean-local:
+       rm -f *~
diff --git a/gio/gdbus-codegen/__init__.py b/gio/gdbus-codegen/__init__.py
new file mode 100644 (file)
index 0000000..8b13789
--- /dev/null
@@ -0,0 +1 @@
+
diff --git a/gio/gdbus-codegen/codegen.py b/gio/gdbus-codegen/codegen.py
new file mode 100644 (file)
index 0000000..974e6e9
--- /dev/null
@@ -0,0 +1,2104 @@
+# -*- Mode: Python -*-
+
+import sys
+import argparse
+
+import config
+import utils
+import dbustypes
+
+# ----------------------------------------------------------------------------------------------------
+
+class CodeGenerator:
+    def __init__(self, ifaces, namespace, interface_prefix, h, c):
+        self.ifaces = ifaces
+        self.h = h
+        self.c = c
+        self.namespace = namespace
+        if len(namespace) > 1:
+            self.ns_upper = utils.camel_case_to_uscore(namespace).upper() + '_'
+            self.ns_lower = utils.camel_case_to_uscore(namespace).lower() + '_'
+        else:
+            self.ns_upper = ''
+            self.ns_lower = ''
+        self.interface_prefix = interface_prefix
+        self.header_guard = self.h.name.upper().replace('.', '_').replace('-', '_')
+
+    # ----------------------------------------------------------------------------------------------------
+
+    def generate_intro(self):
+        self.c.write('/*\n'
+                     ' * Generated by gdbus-codegen.py %s. DO NOT EDIT.\n'
+                     ' */\n'
+                     '\n'
+                     %(config.VERSION))
+        self.c.write('#ifdef HAVE_CONFIG_H\n'
+                     '#  include "config.h"\n'
+                     '#endif\n'
+                     '\n'
+                     '#include "%s"\n'
+                     '\n'%(self.h.name))
+
+        self.c.write('typedef struct\n'
+                     '{\n'
+                     '  GDBusArgInfo parent_struct;\n'
+                     '  gboolean use_gvariant;\n'
+                     '} _ExtendedGDBusArgInfo;\n'
+                     '\n')
+
+        self.c.write('typedef struct\n'
+                     '{\n'
+                     '  GDBusMethodInfo parent_struct;\n'
+                     '  const gchar *signal_name;\n'
+                     '} _ExtendedGDBusMethodInfo;\n'
+                     '\n')
+
+        self.c.write('typedef struct\n'
+                     '{\n'
+                     '  GDBusSignalInfo parent_struct;\n'
+                     '  const gchar *signal_name;\n'
+                     '} _ExtendedGDBusSignalInfo;\n'
+                     '\n')
+
+        self.c.write('typedef struct\n'
+                     '{\n'
+                     '  GDBusPropertyInfo parent_struct;\n'
+                     '  const gchar *hyphen_name;\n'
+                     '  gboolean use_gvariant;\n'
+                     '} _ExtendedGDBusPropertyInfo;\n'
+                     '\n')
+
+        self.c.write('typedef struct\n'
+                     '{\n'
+                     '  const _ExtendedGDBusPropertyInfo *info;\n'
+                     '  GParamSpec *pspec;\n'
+                     '  GValue value;\n'
+                     '} ChangedProperty;\n'
+                     '\n'
+                     'static void\n'
+                     '_changed_property_free (ChangedProperty *data)\n'
+                     '{\n'
+                     '  g_value_unset (&data->value);\n'
+                     '  g_free (data);\n'
+                     '}\n'
+                     '\n')
+
+        self.c.write('static gboolean\n'
+                     '_g_strv_equal0 (gchar **a, gchar **b)\n'
+                     '{\n'
+                     '  gboolean ret = FALSE;\n'
+                     '  guint n;\n'
+                     '  if (a == NULL && b == NULL)\n'
+                     '    {\n'
+                     '      ret = TRUE;\n'
+                     '      goto out;\n'
+                     '    }\n'
+                     '  if (a == NULL || b == NULL)\n'
+                     '    goto out;\n'
+                     '  if (g_strv_length (a) != g_strv_length (b))\n'
+                     '    goto out;\n'
+                     '  for (n = 0; a[n] != NULL; n++)\n'
+                     '    if (g_strcmp0 (a[n], b[n]) != 0)\n'
+                     '      goto out;\n'
+                     '  ret = TRUE;\n'
+                     'out:\n'
+                     '  return ret;\n'
+                     '}\n'
+                     '\n')
+
+        self.c.write('static gboolean\n'
+                     '_g_variant_equal0 (GVariant *a, GVariant *b)\n'
+                     '{\n'
+                     '  gboolean ret = FALSE;\n'
+                     '  if (a == NULL && b == NULL)\n'
+                     '    goto out;\n'
+                     '  if (a == NULL || b == NULL)\n'
+                     '    goto out;\n'
+                     '  ret = g_variant_equal (a, b);\n'
+                     'out:\n'
+                     '  return ret;\n'
+                     '}\n'
+                     '\n')
+
+        # simplified - only supports the types we use
+        self.c.write('static gboolean\n'
+                     '_g_value_equal (const GValue *a, const GValue *b)\n'
+                     '{\n'
+                     '  gboolean ret = FALSE;\n'
+                     '  g_assert (G_VALUE_TYPE (a) == G_VALUE_TYPE (b));\n'
+                     '  switch (G_VALUE_TYPE (a))\n'
+                     '    {\n'
+                     '      case G_TYPE_BOOLEAN:\n'
+                     '        ret = (g_value_get_boolean (a) == g_value_get_boolean (b));\n'
+                     '        break;\n'
+                     '      case G_TYPE_UCHAR:\n'
+                     '        ret = (g_value_get_uchar (a) == g_value_get_uchar (b));\n'
+                     '        break;\n'
+                     '      case G_TYPE_INT:\n'
+                     '        ret = (g_value_get_int (a) == g_value_get_int (b));\n'
+                     '        break;\n'
+                     '      case G_TYPE_UINT:\n'
+                     '        ret = (g_value_get_uint (a) == g_value_get_uint (b));\n'
+                     '        break;\n'
+                     '      case G_TYPE_INT64:\n'
+                     '        ret = (g_value_get_int64 (a) == g_value_get_int64 (b));\n'
+                     '        break;\n'
+                     '      case G_TYPE_UINT64:\n'
+                     '        ret = (g_value_get_uint64 (a) == g_value_get_uint64 (b));\n'
+                     '        break;\n'
+                     '      case G_TYPE_DOUBLE:\n'
+                     '        ret = (g_value_get_double (a) == g_value_get_double (b));\n'
+                     '        break;\n'
+                     '      case G_TYPE_STRING:\n'
+                     '        ret = (g_strcmp0 (g_value_get_string (a), g_value_get_string (b)) == 0);\n'
+                     '        break;\n'
+                     '      case G_TYPE_VARIANT:\n'
+                     '        ret = _g_variant_equal0 (g_value_get_variant (a), g_value_get_variant (b));\n'
+                     '        break;\n'
+                     '      default:\n'
+                     '        if (G_VALUE_TYPE (a) == G_TYPE_STRV)\n'
+                     '          ret = _g_strv_equal0 (g_value_get_boxed (a), g_value_get_boxed (b));\n'
+                     '        else\n'
+                     '          g_critical ("_g_value_equal() does not handle type %s", g_type_name (G_VALUE_TYPE (a)));\n'
+                     '        break;\n'
+                     '    }\n'
+                     '  return ret;\n'
+                     '}\n'
+                     '\n')
+
+        self.h.write('/*\n'
+                     ' * Generated by gdbus-codegen.py %s. DO NOT EDIT.\n'
+                     ' */\n'
+                     '\n'
+                     '#ifndef __%s__\n'
+                     '#define __%s__\n'
+                     '\n'%(config.VERSION, self.header_guard, self.header_guard))
+        self.h.write('#include <gio/gio.h>\n'
+                     '\n'
+                     'G_BEGIN_DECLS\n'
+                     '\n')
+
+    # ----------------------------------------------------------------------------------------------------
+
+    def declare_types(self):
+        for i in self.ifaces:
+            self.h.write('\n')
+            self.h.write('/* ------------------------------------------------------------------------ */\n')
+            self.h.write('/* Declarations for %s */\n'%i.name)
+            self.h.write('\n')
+
+            # First the GInterface
+            self.h.write('#define %sTYPE_%s (%s_get_gtype ())\n'%(i.ns_upper, i.name_upper, i.name_lower))
+            self.h.write('#define %s%s(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), %sTYPE_%s, %s))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper, i.camel_name))
+            self.h.write('#define %sIS_%s(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), %sTYPE_%s))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper))
+            self.h.write('#define %s%s_GET_IFACE(o) (G_TYPE_INSTANCE_GET_INTERFACE ((o), %sTYPE_%s, %s))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper, i.camel_name))
+            self.h.write('\n')
+            self.h.write('struct _%s;\n'%(i.camel_name))
+            self.h.write('typedef struct _%s %s;\n'%(i.camel_name, i.camel_name))
+            self.h.write('typedef struct _%sIface %sIface;\n'%(i.camel_name, i.camel_name))
+            self.h.write('\n')
+            self.h.write('struct _%sIface\n'%(i.camel_name))
+            self.h.write('{\n')
+            self.h.write('  GTypeInterface parent_iface;\n')
+            if len(i.methods) > 0:
+                self.h.write('\n')
+                self.h.write('  /* GObject signal class handlers for incoming D-Bus method calls: */\n')
+                for m in i.methods:
+                    self.h.write('  gboolean (*handle_%s) (\n'
+                                 '    %s *object,\n'
+                                 '    GDBusMethodInvocation *invocation'%(m.name_lower, i.camel_name))
+                    for a in m.in_args:
+                        self.h.write(',\n    %s%s'%(a.ctype_in, a.name))
+                    self.h.write(');\n')
+                    self.h.write('\n')
+            if len(i.signals) > 0:
+                self.h.write('\n')
+                self.h.write('  /* GObject signal class handlers for received D-Bus signals: */\n')
+                for s in i.signals:
+                    self.h.write('  void (*%s) (\n'
+                                 '    %s *object'%(s.name_lower, i.camel_name))
+                    for a in s.args:
+                        self.h.write(',\n    %s%s'%(a.ctype_in, a.name))
+                    self.h.write(');\n')
+                    self.h.write('\n')
+            self.h.write('};\n')
+            self.h.write('\n')
+            self.h.write('GType %s_get_gtype (void) G_GNUC_CONST;\n'%(i.name_lower))
+            self.h.write('\n')
+            self.h.write('GDBusInterfaceInfo *%s_interface_info (void);\n'%(i.name_lower))
+            if len(i.properties) > 0:
+                self.h.write('guint %s_override_properties (GObjectClass *klass, guint property_id_begin);\n'%(i.name_lower))
+            self.h.write('\n')
+
+            # Then method call completion functions
+            if len(i.methods) > 0:
+                self.h.write('\n')
+                self.h.write('/* D-Bus method call completion functions: */\n')
+                for m in i.methods:
+                    self.h.write('void %s_complete_%s (\n'
+                                 '    %s *object,\n'
+                                 '    GDBusMethodInvocation *invocation'%(i.name_lower, m.name_lower, i.camel_name))
+                    for a in m.out_args:
+                        self.h.write(',\n    %s%s'%(a.ctype_in, a.name))
+                    self.h.write(');\n')
+                    self.h.write('\n')
+                self.h.write('\n')
+
+            # Then signal emission functions
+            if len(i.signals) > 0:
+                self.h.write('\n')
+                self.h.write('/* D-Bus signal emissions functions: */\n')
+                for s in i.signals:
+                    self.h.write('void %s_emit_%s (\n'
+                                 '    %s *object'%(i.name_lower, s.name_lower, i.camel_name))
+                    for a in s.args:
+                        self.h.write(',\n    %s%s'%(a.ctype_in, a.name))
+                    self.h.write(');\n')
+                    self.h.write('\n')
+                self.h.write('\n')
+
+            # Then method call declarations
+            if len(i.methods) > 0:
+                self.h.write('\n')
+                self.h.write('/* D-Bus method calls: */\n')
+                for m in i.methods:
+                    # async begin
+                    self.h.write('void %s_call_%s (\n'
+                                 '    %s *proxy'%(i.name_lower, m.name_lower, i.camel_name))
+                    for a in m.in_args:
+                        self.h.write(',\n    %s%s'%(a.ctype_in, a.name))
+                    self.h.write(',\n'
+                                 '    GCancellable *cancellable,\n'
+                                 '    GAsyncReadyCallback callback,\n'
+                                 '    gpointer user_data);\n')
+                    self.h.write('\n')
+                    # async finish
+                    self.h.write('gboolean %s_call_%s_finish (\n'
+                                 '    %s *proxy'%(i.name_lower, m.name_lower, i.camel_name))
+                    for a in m.out_args:
+                        self.h.write(',\n    %sout_%s'%(a.ctype_out, a.name))
+                    self.h.write(',\n'
+                                 '    GAsyncResult *res,\n'
+                                 '    GError **error);\n')
+                    self.h.write('\n')
+                    # sync
+                    self.h.write('gboolean %s_call_%s_sync (\n'
+                                 '    %s *proxy'%(i.name_lower, m.name_lower, i.camel_name))
+                    for a in m.in_args:
+                        self.h.write(',\n    %s%s'%(a.ctype_in, a.name))
+                    for a in m.out_args:
+                        self.h.write(',\n    %sout_%s'%(a.ctype_out, a.name))
+                    self.h.write(',\n'
+                                 '    GCancellable *cancellable,\n'
+                                 '    GError **error);\n')
+                    self.h.write('\n')
+                self.h.write('\n')
+
+            # Then the property accessor declarations
+            if len(i.properties) > 0:
+                self.h.write('\n')
+                self.h.write('/* D-Bus property accessors: */\n')
+                for p in i.properties:
+                    # getter
+                    self.h.write('%s%s_get_%s (%s *object);\n'%(p.arg.ctype_in, i.name_lower, p.name_lower, i.camel_name))
+                    # setter
+                    self.h.write('void %s_set_%s (%s *object, %svalue);\n'%(i.name_lower, p.name_lower, i.camel_name, p.arg.ctype_in, ))
+                    self.h.write('\n')
+
+            # Then the proxy
+            self.h.write('\n')
+            self.h.write('/* ---- */\n')
+            self.h.write('\n')
+            self.h.write('#define %sTYPE_%s_PROXY (%s_proxy_get_gtype ())\n'%(i.ns_upper, i.name_upper, i.name_lower))
+            self.h.write('#define %s%s_PROXY(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), %sTYPE_%s_PROXY, %sProxy))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper, i.camel_name))
+            self.h.write('#define %s%s_PROXY_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), %sTYPE_%s_PROXY, %sProxyClass))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper, i.camel_name))
+            self.h.write('#define %s%s_PROXY_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), %sTYPE_%s_PROXY, %sProxyClass))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper, i.camel_name))
+            self.h.write('#define %sIS_%s_PROXY(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), %sTYPE_%s_PROXY))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper))
+            self.h.write('#define %sIS_%s_PROXY_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), %sTYPE_%s_PROXY))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper))
+            self.h.write('\n')
+            self.h.write('typedef struct _%sProxy %sProxy;\n'%(i.camel_name, i.camel_name))
+            self.h.write('typedef struct _%sProxyClass %sProxyClass;\n'%(i.camel_name, i.camel_name))
+            self.h.write('typedef struct _%sProxyPrivate %sProxyPrivate;\n'%(i.camel_name, i.camel_name))
+            self.h.write('\n')
+            self.h.write('struct _%sProxy\n'%(i.camel_name))
+            self.h.write('{\n')
+            self.h.write('  GDBusProxy parent_instance;\n')
+            self.h.write('  %sProxyPrivate *priv;\n'%(i.camel_name))
+            self.h.write('};\n')
+            self.h.write('\n')
+            self.h.write('struct _%sProxyClass\n'%(i.camel_name))
+            self.h.write('{\n')
+            self.h.write('  GDBusProxyClass parent_class;\n')
+            self.h.write('};\n')
+            self.h.write('\n')
+            self.h.write('GType %s_proxy_get_gtype (void) G_GNUC_CONST;\n'%(i.name_lower))
+
+            self.h.write('\n')
+            self.h.write('void %s_proxy_new (\n'
+                         '    GDBusConnection     *connection,\n'
+                         '    GDBusProxyFlags      flags,\n'
+                         '    const gchar         *name,\n'
+                         '    const gchar         *object_path,\n'
+                         '    GCancellable        *cancellable,\n'
+                         '    GAsyncReadyCallback  callback,\n'
+                         '    gpointer             user_data);\n'
+                         %(i.name_lower))
+            self.h.write('%s *%s_proxy_new_finish (\n'
+                         '    GAsyncResult        *res,\n'
+                         '    GError             **error);\n'
+                         %(i.camel_name, i.name_lower))
+            self.h.write('%s *%s_proxy_new_sync (\n'
+                         '    GDBusConnection     *connection,\n'
+                         '    GDBusProxyFlags      flags,\n'
+                         '    const gchar         *name,\n'
+                         '    const gchar         *object_path,\n'
+                         '    GCancellable        *cancellable,\n'
+                         '    GError             **error);\n'
+                         %(i.camel_name, i.name_lower))
+            self.h.write('\n')
+            self.h.write('void %s_proxy_new_for_bus (\n'
+                         '    GBusType             bus_type,\n'
+                         '    GDBusProxyFlags      flags,\n'
+                         '    const gchar         *name,\n'
+                         '    const gchar         *object_path,\n'
+                         '    GCancellable        *cancellable,\n'
+                         '    GAsyncReadyCallback  callback,\n'
+                         '    gpointer             user_data);\n'
+                         %(i.name_lower))
+            self.h.write('%s *%s_proxy_new_for_bus_finish (\n'
+                         '    GAsyncResult        *res,\n'
+                         '    GError             **error);\n'
+                         %(i.camel_name, i.name_lower))
+            self.h.write('%s *%s_proxy_new_for_bus_sync (\n'
+                         '    GBusType             bus_type,\n'
+                         '    GDBusProxyFlags      flags,\n'
+                         '    const gchar         *name,\n'
+                         '    const gchar         *object_path,\n'
+                         '    GCancellable        *cancellable,\n'
+                         '    GError             **error);\n'
+                         %(i.camel_name, i.name_lower))
+            self.h.write('\n')
+
+            # Interface proxy accessors
+            self.h.write('#define %sGET_%s(object) (g_dbus_object_lookup_with_typecheck (G_DBUS_OBJECT (object), "%s", %sTYPE_%s))\n'%(i.ns_upper, i.name_upper, i.name, i.ns_upper, i.name_upper))
+            self.h.write('#define %sPEEK_%s(object) (g_dbus_object_peek_with_typecheck (G_DBUS_OBJECT (object), "%s", %sTYPE_%s))\n'%(i.ns_upper, i.name_upper, i.name, i.ns_upper, i.name_upper))
+            self.h.write('\n')
+
+            # Then the stub
+            self.h.write('\n')
+            self.h.write('/* ---- */\n')
+            self.h.write('\n')
+            self.h.write('#define %sTYPE_%s_STUB (%s_stub_get_gtype ())\n'%(i.ns_upper, i.name_upper, i.name_lower))
+            self.h.write('#define %s%s_STUB(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), %sTYPE_%s_STUB, %sStub))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper, i.camel_name))
+            self.h.write('#define %s%s_STUB_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), %sTYPE_%s_STUB, %sStubClass))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper, i.camel_name))
+            self.h.write('#define %s%s_STUB_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), %sTYPE_%s_STUB, %sStubClass))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper, i.camel_name))
+            self.h.write('#define %sIS_%s_STUB(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), %sTYPE_%s_STUB))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper))
+            self.h.write('#define %sIS_%s_STUB_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), %sTYPE_%s_STUB))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper))
+            self.h.write('\n')
+            self.h.write('typedef struct _%sStub %sStub;\n'%(i.camel_name, i.camel_name))
+            self.h.write('typedef struct _%sStubClass %sStubClass;\n'%(i.camel_name, i.camel_name))
+            self.h.write('typedef struct _%sStubPrivate %sStubPrivate;\n'%(i.camel_name, i.camel_name))
+            self.h.write('\n')
+            self.h.write('struct _%sStub\n'%(i.camel_name))
+            self.h.write('{\n')
+            self.h.write('  GDBusInterfaceStub parent_instance;\n')
+            self.h.write('  %sStubPrivate *priv;\n'%(i.camel_name))
+            self.h.write('};\n')
+            self.h.write('\n')
+            self.h.write('struct _%sStubClass\n'%(i.camel_name))
+            self.h.write('{\n')
+            self.h.write('  GDBusInterfaceStubClass parent_class;\n')
+            self.h.write('};\n')
+            self.h.write('\n')
+            self.h.write('GType %s_stub_get_gtype (void) G_GNUC_CONST;\n'%(i.name_lower))
+            self.h.write('\n')
+            self.h.write('%s *%s_stub_new (void);\n'%(i.camel_name, i.name_lower))
+
+            self.h.write('\n')
+
+        # Finally, the proxy manager
+        self.h.write('\n')
+        self.h.write('/* ---- */\n')
+        self.h.write('\n')
+        self.h.write('#define %sTYPE_OBJECT_MANAGER_CLIENT (%sobject_manager_client_get_gtype ())\n'%(self.ns_upper, self.ns_lower))
+        self.h.write('#define %sOBJECT_MANAGER_CLIENT(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), %sTYPE_OBJECT_MANAGER_CLIENT, %sObjectManagerClient))\n'%(self.ns_upper, self.ns_upper, self.namespace))
+        self.h.write('#define %sOBJECT_MANAGER_CLIENT_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), %sTYPE_OBJECT_MANAGER_CLIENT, %sObjectManagerClientClass))\n'%(self.ns_upper, self.ns_upper, self.namespace))
+        self.h.write('#define %sOBJECT_MANAGER_CLIENT_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), %sTYPE_OBJECT_MANAGER_CLIENT, %sObjectManagerClientClass))\n'%(self.ns_upper, self.ns_upper, self.namespace))
+        self.h.write('#define %sIS_OBJECT_MANAGER_CLIENT(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), %sTYPE_OBJECT_MANAGER_CLIENT))\n'%(self.ns_upper, self.ns_upper))
+        self.h.write('#define %sIS_OBJECT_MANAGER_CLIENT_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), %sTYPE_OBJECT_MANAGER_CLIENT))\n'%(self.ns_upper, self.ns_upper))
+        self.h.write('\n')
+        self.h.write('typedef struct _%sObjectManagerClient %sObjectManagerClient;\n'%(self.namespace, self.namespace))
+        self.h.write('typedef struct _%sObjectManagerClientClass %sObjectManagerClientClass;\n'%(self.namespace, self.namespace))
+        self.h.write('typedef struct _%sObjectManagerClientPrivate %sObjectManagerClientPrivate;\n'%(self.namespace, self.namespace))
+        self.h.write('\n')
+        self.h.write('struct _%sObjectManagerClient\n'%(self.namespace))
+        self.h.write('{\n')
+        self.h.write('  GDBusObjectManagerClient parent_instance;\n')
+        self.h.write('  %sObjectManagerClientPrivate *priv;\n'%(self.namespace))
+        self.h.write('};\n')
+        self.h.write('\n')
+        self.h.write('struct _%sObjectManagerClientClass\n'%(self.namespace))
+        self.h.write('{\n')
+        self.h.write('  GDBusObjectManagerClientClass parent_class;\n')
+        self.h.write('};\n')
+        self.h.write('\n')
+        self.h.write('GType %sobject_manager_client_get_gtype (void) G_GNUC_CONST;\n'%(self.ns_lower))
+        self.h.write('\n')
+        self.h.write('GDBusProxyTypeFunc %sobject_manager_client_get_proxy_type_func (void);\n'%(self.ns_lower))
+        self.h.write('\n')
+        self.h.write('void %sobject_manager_client_new (\n'
+                     '    GDBusConnection        *connection,\n'
+                     '    GDBusObjectManagerClientFlags  flags,\n'
+                     '    const gchar            *name,\n'
+                     '    const gchar            *object_path,\n'
+                     '    GCancellable           *cancellable,\n'
+                     '    GAsyncReadyCallback     callback,\n'
+                     '    gpointer                user_data);\n'
+                     %(self.ns_lower))
+        self.h.write('GDBusObjectManager *%sobject_manager_client_new_finish (\n'
+                     '    GAsyncResult        *res,\n'
+                     '    GError             **error);\n'
+                     %(self.ns_lower))
+        self.h.write('GDBusObjectManager *%sobject_manager_client_new_sync (\n'
+                     '    GDBusConnection        *connection,\n'
+                     '    GDBusObjectManagerClientFlags  flags,\n'
+                     '    const gchar            *name,\n'
+                     '    const gchar            *object_path,\n'
+                     '    GCancellable           *cancellable,\n'
+                     '    GError                **error);\n'
+                     %(self.ns_lower))
+        self.h.write('\n')
+        self.h.write('void %sobject_manager_client_new_for_bus (\n'
+                     '    GBusType                bus_type,\n'
+                     '    GDBusObjectManagerClientFlags  flags,\n'
+                     '    const gchar            *name,\n'
+                     '    const gchar            *object_path,\n'
+                     '    GCancellable           *cancellable,\n'
+                     '    GAsyncReadyCallback     callback,\n'
+                     '    gpointer                user_data);\n'
+                     %(self.ns_lower))
+        self.h.write('GDBusObjectManager *%sobject_manager_client_new_for_bus_finish (\n'
+                     '    GAsyncResult        *res,\n'
+                     '    GError             **error);\n'
+                     %(self.ns_lower))
+        self.h.write('GDBusObjectManager *%sobject_manager_client_new_for_bus_sync (\n'
+                     '    GBusType                bus_type,\n'
+                     '    GDBusObjectManagerClientFlags  flags,\n'
+                     '    const gchar            *name,\n'
+                     '    const gchar            *object_path,\n'
+                     '    GCancellable           *cancellable,\n'
+                     '    GError                **error);\n'
+                     %(self.ns_lower))
+        self.h.write('\n')
+
+    # ----------------------------------------------------------------------------------------------------
+
+    def generate_outro(self):
+        self.h.write('\n'
+                     'G_END_DECLS\n'
+                     '\n'
+                     '#endif /* __%s__ */\n'%(self.header_guard))
+
+    # ----------------------------------------------------------------------------------------------------
+
+    def generate_annotations(self, prefix, annotations):
+        if annotations == None:
+            return
+
+        n = 0
+        for a in annotations:
+            #self.generate_annotations('%s_%d'%(prefix, n), a.get_annotations())
+
+            # skip internal annotations
+            if a.key.startswith('org.gtk.GDBus'):
+                continue
+
+            self.c.write('static const GDBusAnnotationInfo %s_%d =\n'
+                         '{\n'
+                         '  -1,\n'
+                         '  "%s",\n'
+                         '  "%s",\n'%(prefix, n, a.key, a.value))
+            if len(a.annotations) == 0:
+                self.c.write('  NULL\n')
+            else:
+                self.c.write('  (GDBusAnnotationInfo **) &%s_%d_pointers\n'%(prefix, n))
+            self.c.write('};\n'
+                         '\n')
+            n += 1
+
+        if n > 0:
+            self.c.write('static const GDBusAnnotationInfo * const %s_pointers[] =\n'
+                             '{\n'%(prefix))
+            m = 0;
+            for a in annotations:
+                if a.key.startswith('org.gtk.GDBus'):
+                    continue
+                self.c.write('  &%s_%d,\n'%(prefix, m))
+                m += 1
+            self.c.write('  NULL\n'
+                         '};\n'
+                         '\n')
+        return n
+
+    def generate_args(self, prefix, args):
+        for a in args:
+            num_anno = self.generate_annotations('%s_arg_%s_annotation_info'%(prefix, a.name), a.annotations)
+
+            self.c.write('static const _ExtendedGDBusArgInfo %s_%s =\n'
+                         '{\n'
+                         '  {\n'
+                         '    -1,\n'
+                         '    "%s",\n'
+                         '    "%s",\n'%(prefix, a.name, a.name, a.signature))
+            if num_anno == 0:
+                self.c.write('    NULL\n')
+            else:
+                self.c.write('    (GDBusAnnotationInfo **) &%s_arg_%s_annotation_info_pointers\n'%(prefix, a.name))
+            self.c.write('  },\n')
+            if not utils.lookup_annotation(a.annotations, 'org.gtk.GDBus.C.ForceGVariant'):
+                self.c.write('  FALSE\n')
+            else:
+                self.c.write('  TRUE\n')
+            self.c.write('};\n'
+                         '\n')
+
+        if len(args) > 0:
+            self.c.write('static const _ExtendedGDBusArgInfo * const %s_pointers[] =\n'
+                             '{\n'%(prefix))
+            for a in args:
+                self.c.write('  &%s_%s,\n'%(prefix, a.name))
+            self.c.write('  NULL\n'
+                         '};\n'
+                         '\n')
+
+    def generate_introspection_for_interface(self, i):
+            self.c.write('/* ---- Introspection data for %s ---- */\n'
+                         '\n'%(i.name))
+
+            if len(i.methods) > 0:
+                for m in i.methods:
+                    self.generate_args('_%s_method_info_%s_IN_ARG'%(i.name_lower, m.name_lower), m.in_args)
+                    self.generate_args('_%s_method_info_%s_OUT_ARG'%(i.name_lower, m.name_lower), m.out_args)
+
+                    num_anno = self.generate_annotations('_%s_method_%s_annotation_info'%(i.name_lower, m.name_lower), m.annotations)
+
+                    self.c.write('static const _ExtendedGDBusMethodInfo _%s_method_info_%s =\n'
+                                 '{\n'
+                                 '  {\n'
+                                 '    -1,\n'
+                                 '    "%s",\n'%(i.name_lower, m.name_lower, m.name))
+                    if len(m.in_args) == 0:
+                        self.c.write('    NULL,\n')
+                    else:
+                        self.c.write('    (GDBusArgInfo **) &_%s_method_info_%s_IN_ARG_pointers,\n'%(i.name_lower, m.name_lower))
+                    if len(m.out_args) == 0:
+                        self.c.write('    NULL,\n')
+                    else:
+                        self.c.write('    (GDBusArgInfo **) &_%s_method_info_%s_OUT_ARG_pointers,\n'%(i.name_lower, m.name_lower))
+                    if num_anno == 0:
+                        self.c.write('    NULL\n')
+                    else:
+                        self.c.write('    (GDBusAnnotationInfo **) &_%s_method_%s_annotation_info_pointers\n'%(i.name_lower, m.name_lower))
+                    self.c.write('  },\n'
+                                 '  "handle-%s"\n'
+                                 %(m.name_hyphen))
+                    self.c.write('};\n'
+                                 '\n')
+
+                self.c.write('static const _ExtendedGDBusMethodInfo * const _%s_method_info_pointers[] =\n'
+                             '{\n'%(i.name_lower))
+                for m in i.methods:
+                    self.c.write('  &_%s_method_info_%s,\n'%(i.name_lower, m.name_lower))
+                self.c.write('  NULL\n'
+                             '};\n'
+                             '\n')
+
+            # ---
+
+            if len(i.signals) > 0:
+                for s in i.signals:
+                    self.generate_args('_%s_signal_info_%s_ARG'%(i.name_lower, s.name_lower), s.args)
+
+                    num_anno = self.generate_annotations('_%s_signal_%s_annotation_info'%(i.name_lower, s.name_lower), s.annotations)
+                    self.c.write('static const _ExtendedGDBusSignalInfo _%s_signal_info_%s =\n'
+                                 '{\n'
+                                 '  {\n'
+                                 '    -1,\n'
+                                 '    "%s",\n'%(i.name_lower, s.name_lower, s.name))
+                    if len(s.args) == 0:
+                        self.c.write('    NULL,\n')
+                    else:
+                        self.c.write('    (GDBusArgInfo **) &_%s_signal_info_%s_ARG_pointers,\n'%(i.name_lower, s.name_lower))
+                    if num_anno == 0:
+                        self.c.write('    NULL\n')
+                    else:
+                        self.c.write('    (GDBusAnnotationInfo **) &_%s_signal_%s_annotation_info_pointers\n'%(i.name_lower, s.name_lower))
+                    self.c.write('  },\n'
+                                 '  "%s"\n'
+                                 %(s.name_hyphen))
+                    self.c.write('};\n'
+                                 '\n')
+
+                self.c.write('static const _ExtendedGDBusSignalInfo * const _%s_signal_info_pointers[] =\n'
+                             '{\n'%(i.name_lower))
+                for s in i.signals:
+                    self.c.write('  &_%s_signal_info_%s,\n'%(i.name_lower, s.name_lower))
+                self.c.write('  NULL\n'
+                             '};\n'
+                             '\n')
+
+            # ---
+
+            if len(i.properties) > 0:
+                for p in i.properties:
+                    if p.readable and p.writable:
+                        access = 'G_DBUS_PROPERTY_INFO_FLAGS_READABLE | G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE'
+                    elif p.readable:
+                        access = 'G_DBUS_PROPERTY_INFO_FLAGS_READABLE'
+                    elif p.writable:
+                        access = 'G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE'
+                    else:
+                        access = 'G_DBUS_PROPERTY_INFO_FLAGS_NONE'
+                    num_anno = self.generate_annotations('_%s_property_%s_annotation_info'%(i.name_lower, p.name_lower), p.annotations)
+                    self.c.write('static const _ExtendedGDBusPropertyInfo _%s_property_info_%s =\n'
+                                 '{\n'
+                                 '  {\n'
+                                 '    -1,\n'
+                                 '    "%s",\n'
+                                 '    "%s",\n'
+                                 '    %s,\n'%(i.name_lower, p.name_lower, p.name, p.arg.signature, access))
+                    if num_anno == 0:
+                        self.c.write('    NULL\n')
+                    else:
+                        self.c.write('    (GDBusAnnotationInfo **) &_%s_property_%s_annotation_info_pointers\n'%(i.name_lower, p.name_lower))
+                    self.c.write('  },\n'
+                                 '  "%s",\n'
+                                 %(p.name_hyphen))
+                    if not utils.lookup_annotation(p.annotations, 'org.gtk.GDBus.C.ForceGVariant'):
+                        self.c.write('  FALSE\n')
+                    else:
+                        self.c.write('  TRUE\n')
+                    self.c.write('};\n'
+                                 '\n')
+
+                self.c.write('static const _ExtendedGDBusPropertyInfo * const _%s_property_info_pointers[] =\n'
+                             '{\n'%(i.name_lower))
+                for p in i.properties:
+                    self.c.write('  &_%s_property_info_%s,\n'%(i.name_lower, p.name_lower))
+                self.c.write('  NULL\n'
+                             '};\n'
+                             '\n')
+
+            num_anno = self.generate_annotations('_%s_annotation_info'%(i.name_lower), i.annotations)
+            self.c.write('static const GDBusInterfaceInfo _%s_interface_info =\n'
+                         '{\n'
+                         '  -1,\n'
+                         '  "%s",\n'%(i.name_lower, i.name))
+            if len(i.methods) == 0:
+                self.c.write('  NULL,\n')
+            else:
+                self.c.write('  (GDBusMethodInfo **) &_%s_method_info_pointers,\n'%(i.name_lower))
+            if len(i.signals) == 0:
+                self.c.write('  NULL,\n')
+            else:
+                self.c.write('  (GDBusSignalInfo **) &_%s_signal_info_pointers,\n'%(i.name_lower))
+            if len(i.properties) == 0:
+                self.c.write('  NULL,\n')
+            else:
+                self.c.write('  (GDBusPropertyInfo **) &_%s_property_info_pointers,\n'%(i.name_lower))
+            if num_anno == 0:
+                self.c.write('  NULL\n')
+            else:
+                self.c.write('  (GDBusAnnotationInfo **) &_%s_annotation_info_pointers\n'%(i.name_lower))
+            self.c.write('};\n'
+                         '\n')
+            self.c.write('\n')
+            self.c.write('GDBusInterfaceInfo *\n'
+                         '%s_interface_info (void)\n'
+                         '{\n'
+                         '  return (GDBusInterfaceInfo *) &_%s_interface_info;\n'
+                         '}\n'
+                         '\n'%(i.name_lower, i.name_lower))
+
+            if len(i.properties) > 0:
+                self.c.write('guint\n'
+                             '%s_override_properties (GObjectClass *klass, guint property_id_begin)\n'
+                             '{\n'%(i.name_lower))
+                for p in i.properties:
+                    self.c.write ('  g_object_class_override_property (klass, property_id_begin++, "%s");\n'%(p.name_hyphen))
+                self.c.write('  return property_id_begin - 1;\n'
+                             '}\n'
+                             '\n')
+            self.c.write('\n')
+
+    # ----------------------------------------------------------------------------------------------------
+
+    def generate_interface(self, i):
+        self.c.write('\n')
+
+        self.c.write('static void\n'
+                     '%s_default_init (%sIface *iface)\n'
+                     '{\n'%(i.name_lower, i.camel_name));
+
+        if len(i.methods) > 0:
+            self.c.write('  /* GObject signals for incoming D-Bus method calls: */\n')
+            for m in i.methods:
+                self.c.write('  g_signal_new ("handle-%s",\n'
+                             '    G_TYPE_FROM_INTERFACE (iface),\n'
+                             '    G_SIGNAL_RUN_LAST,\n'
+                             '    G_STRUCT_OFFSET (%sIface, handle_%s),\n'
+                             '    g_signal_accumulator_true_handled,\n'
+                             '    NULL,\n' # accu_data
+                             '    _cclosure_marshal_generic,\n'
+                             '    G_TYPE_BOOLEAN,\n'
+                             '    %d,\n'
+                             '    G_TYPE_DBUS_METHOD_INVOCATION'
+                             %(m.name_hyphen, i.camel_name, m.name_lower, len(m.in_args) + 1))
+                for a in m.in_args:
+                    self.c.write (', %s'%(a.gtype))
+                self.c.write(');\n')
+                self.c.write('\n')
+
+        if len(i.signals) > 0:
+            self.c.write('  /* GObject signals for received D-Bus signals: */\n')
+            for s in i.signals:
+                self.c.write('  g_signal_new ("%s",\n'
+                             '    G_TYPE_FROM_INTERFACE (iface),\n'
+                             '    G_SIGNAL_RUN_LAST,\n'
+                             '    G_STRUCT_OFFSET (%sIface, %s),\n'
+                             '    NULL,\n' # accumulator
+                             '    NULL,\n' # accu_data
+                             '    _cclosure_marshal_generic,\n'
+                             '    G_TYPE_NONE,\n'
+                             '    %d'
+                             %(s.name_hyphen, i.camel_name, s.name_lower, len(s.args)))
+                for a in s.args:
+                    self.c.write (', %s'%(a.gtype))
+                self.c.write(');\n')
+                self.c.write('\n')
+
+        if len(i.properties) > 0:
+            self.c.write('  /* GObject properties for D-Bus properties: */\n')
+            for p in i.properties:
+                self.c.write('  g_object_interface_install_property (iface,\n')
+                if p.arg.gtype == 'G_TYPE_VARIANT':
+                    s = 'g_param_spec_variant ("%s", NULL, NULL, G_VARIANT_TYPE ("%s"), NULL'%(p.name_hyphen, p.arg.signature)
+                elif p.arg.signature == 'b':
+                    s = 'g_param_spec_boolean ("%s", NULL, NULL, FALSE'%(p.name_hyphen)
+                elif p.arg.signature == 'y':
+                    s = 'g_param_spec_uchar ("%s", NULL, NULL, 0, 255, 0'%(p.name_hyphen)
+                elif p.arg.signature == 'n':
+                    s = 'g_param_spec_int ("%s", NULL, NULL, G_MININT16, G_MAXINT16, 0'%(p.name_hyphen)
+                elif p.arg.signature == 'q':
+                    s = 'g_param_spec_uint ("%s", NULL, NULL, 0, G_MAXUINT16, 0'%(p.name_hyphen)
+                elif p.arg.signature == 'i':
+                    s = 'g_param_spec_int ("%s", NULL, NULL, G_MININT32, G_MAXINT32, 0'%(p.name_hyphen)
+                elif p.arg.signature == 'u':
+                    s = 'g_param_spec_uint ("%s", NULL, NULL, 0, G_MAXUINT32, 0'%(p.name_hyphen)
+                elif p.arg.signature == 'x':
+                    s = 'g_param_spec_int64 ("%s", NULL, NULL, G_MININT64, G_MAXINT64, 0'%(p.name_hyphen)
+                elif p.arg.signature == 't':
+                    s = 'g_param_spec_uint64 ("%s", NULL, NULL, 0, G_MAXUINT64, 0'%(p.name_hyphen)
+                elif p.arg.signature == 'd':
+                    s = 'g_param_spec_double ("%s", NULL, NULL, -G_MAXDOUBLE, G_MAXDOUBLE, 0.0'%(p.name_hyphen)
+                elif p.arg.signature == 's':
+                    s = 'g_param_spec_string ("%s", NULL, NULL, NULL'%(p.name_hyphen)
+                elif p.arg.signature == 'o':
+                    s = 'g_param_spec_string ("%s", NULL, NULL, NULL'%(p.name_hyphen)
+                elif p.arg.signature == 'g':
+                    s = 'g_param_spec_string ("%s", NULL, NULL, NULL'%(p.name_hyphen)
+                elif p.arg.signature == 'ay':
+                    s = 'g_param_spec_string ("%s", NULL, NULL, NULL'%(p.name_hyphen)
+                elif p.arg.signature == 'as':
+                    s = 'g_param_spec_boxed ("%s", NULL, NULL, G_TYPE_STRV'%(p.name_hyphen)
+                elif p.arg.signature == 'aay':
+                    s = 'g_param_spec_boxed ("%s", NULL, NULL, G_TYPE_STRV'%(p.name_hyphen)
+                else:
+                    raise RuntimeError('Unsupported gtype %s for GParamSpec'%(p.arg.gtype))
+                self.c.write('    %s, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));'%s);
+                self.c.write('\n')
+
+        self.c.write('}\n'
+                     '\n')
+
+        self.c.write('typedef %sIface %sInterface;\n'%(i.camel_name, i.camel_name))
+        self.c.write('#define %s_get_type %s_get_gtype\n'%(i.name_lower, i.name_lower))
+        self.c.write('G_DEFINE_INTERFACE (%s, %s, G_TYPE_OBJECT);\n'%(i.camel_name, i.name_lower))
+        self.c.write('#undef %s_get_type\n'%(i.name_lower))
+        self.c.write('\n')
+
+    # ----------------------------------------------------------------------------------------------------
+
+    def generate_property_accessors(self, i):
+        for p in i.properties:
+            # getter
+            self.c.write('%s\n'
+                         '%s_get_%s (%s *object)\n'
+                         '{\n'
+                         '  %svalue;\n'%(p.arg.ctype_in, i.name_lower, p.name_lower, i.camel_name, p.arg.ctype_in_g))
+            self.c.write('  g_object_get (G_OBJECT (object), "%s", &value, NULL);\n'%(p.name_hyphen))
+            if p.arg.free_func:
+                self.c.write('  if (value != NULL)\n'
+                             '    g_object_set_data_full (G_OBJECT (object), "-x-memoizing-%s", (gpointer) value, (GDestroyNotify) %s);\n'
+                             '  else\n'
+                             '    g_object_set_data_full (G_OBJECT (object), "-x-memoizing-%s", (gpointer) value, NULL);\n'
+                             %(p.name_hyphen, p.arg.free_func, p.name_hyphen))
+            self.c.write('  return value;\n')
+            self.c.write('}\n')
+            self.c.write('\n')
+            # setter
+            self.c.write('void\n'
+                         '%s_set_%s (%s *object, %svalue)\n'
+                         '{\n'%(i.name_lower, p.name_lower, i.camel_name, p.arg.ctype_in, ))
+            self.c.write('  g_object_set (G_OBJECT (object), "%s", value, NULL);\n'%(p.name_hyphen))
+            self.c.write('}\n')
+            self.c.write('\n')
+
+    # ---------------------------------------------------------------------------------------------------
+
+    def generate_signal_emitters(self, i):
+        for s in i.signals:
+            self.c.write('void\n'
+                         '%s_emit_%s (\n'
+                         '    %s *object'%(i.name_lower, s.name_lower, i.camel_name))
+            for a in s.args:
+                self.c.write(',\n    %s%s'%(a.ctype_in, a.name))
+            self.c.write(')\n'
+                         '{\n'
+                         '  g_signal_emit_by_name (object, "%s"'%(s.name_hyphen))
+            for a in s.args:
+                self.c.write(', %s'%a.name)
+            self.c.write(');\n')
+            self.c.write('}\n'
+                         '\n')
+
+    # ---------------------------------------------------------------------------------------------------
+
+    def generate_method_calls(self, i):
+        for m in i.methods:
+            # async begin
+            self.c.write('void\n'
+                         '%s_call_%s (\n'
+                         '    %s *proxy'%(i.name_lower, m.name_lower, i.camel_name))
+            for a in m.in_args:
+                self.c.write(',\n    %s%s'%(a.ctype_in, a.name))
+            self.c.write(',\n'
+                         '    GCancellable *cancellable,\n'
+                         '    GAsyncReadyCallback callback,\n'
+                         '    gpointer user_data)\n'
+                         '{\n')
+            self.c.write('  g_dbus_proxy_call (G_DBUS_PROXY (proxy),\n'
+                         '    "%s",\n'
+                         '    g_variant_new ("('%(m.name))
+            for a in m.in_args:
+                self.c.write('%s'%(a.format_in))
+            self.c.write(')"')
+            for a in m.in_args:
+                self.c.write(',\n                   %s'%(a.name))
+            self.c.write('),\n'
+                         '    G_DBUS_CALL_FLAGS_NONE,\n'
+                         '    -1,\n'
+                         '    cancellable,\n'
+                         '    callback,\n'
+                         '    user_data);\n')
+            self.c.write('}\n'
+                         '\n')
+            # async finish
+            self.c.write('gboolean\n'
+                         '%s_call_%s_finish (\n'
+                         '    %s *proxy'%(i.name_lower, m.name_lower, i.camel_name))
+            for a in m.out_args:
+                self.c.write(',\n    %sout_%s'%(a.ctype_out, a.name))
+            self.c.write(',\n'
+                         '    GAsyncResult *res,\n'
+                         '    GError **error)\n'
+                         '{\n'
+                         '  GVariant *_ret;\n'
+                         '  _ret = g_dbus_proxy_call_finish (G_DBUS_PROXY (proxy), res, error);\n'
+                         '  if (_ret == NULL)\n'
+                         '    goto _out;\n')
+            self.c.write('  g_variant_get (_ret,\n'
+                         '                 \"(')
+            for a in m.out_args:
+                self.c.write('%s'%(a.format_out))
+            self.c.write(')"')
+            for a in m.out_args:
+                self.c.write(',\n                 out_%s'%(a.name))
+            self.c.write(');\n'
+                         '  g_variant_unref (_ret);\n')
+            self.c.write('_out:\n'
+                         '  return _ret != NULL;\n'
+                         '}\n'
+                         '\n')
+
+
+            # sync
+            self.c.write('gboolean\n'
+                         '%s_call_%s_sync (\n'
+                         '    %s *proxy'%(i.name_lower, m.name_lower, i.camel_name))
+            for a in m.in_args:
+                self.c.write(',\n    %s%s'%(a.ctype_in, a.name))
+            for a in m.out_args:
+                self.c.write(',\n    %sout_%s'%(a.ctype_out, a.name))
+            self.c.write(',\n'
+                         '    GCancellable *cancellable,\n'
+                         '    GError **error)\n'
+                         '{\n'
+                         '  GVariant *_ret;\n')
+            self.c.write('  _ret = g_dbus_proxy_call_sync (G_DBUS_PROXY (proxy),\n'
+                         '    "%s",\n'
+                         '    g_variant_new ("('%(m.name))
+            for a in m.in_args:
+                self.c.write('%s'%(a.format_in))
+            self.c.write(')"')
+            for a in m.in_args:
+                self.c.write(',\n                   %s'%(a.name))
+            self.c.write('),\n'
+                         '    G_DBUS_CALL_FLAGS_NONE,\n'
+                         '    -1,\n'
+                         '    cancellable,\n'
+                         '    error);\n'
+                         '  if (_ret == NULL)\n'
+                         '    goto _out;\n')
+            self.c.write('  g_variant_get (_ret,\n'
+                         '                 \"(')
+            for a in m.out_args:
+                self.c.write('%s'%(a.format_out))
+            self.c.write(')"')
+            for a in m.out_args:
+                self.c.write(',\n                 out_%s'%(a.name))
+            self.c.write(');\n'
+                         '  g_variant_unref (_ret);\n')
+            self.c.write('_out:\n'
+                         '  return _ret != NULL;\n'
+                         '}\n'
+                         '\n')
+
+    # ---------------------------------------------------------------------------------------------------
+
+    def generate_method_completers(self, i):
+        for m in i.methods:
+            self.c.write('void\n'
+                         '%s_complete_%s (\n'
+                         '    %s *object,\n'
+                         '    GDBusMethodInvocation *invocation'%(i.name_lower, m.name_lower, i.camel_name))
+            for a in m.out_args:
+                self.c.write(',\n    %s%s'%(a.ctype_in, a.name))
+            self.c.write(')\n'
+                         '{\n')
+
+            self.c.write('  g_dbus_method_invocation_return_value (invocation,\n'
+                         '    g_variant_new ("(')
+            for a in m.out_args:
+                self.c.write('%s'%(a.format_in))
+            self.c.write(')"')
+            for a in m.out_args:
+                self.c.write(',\n                   %s'%(a.name))
+            self.c.write('));\n'
+                         '}\n'
+                         '\n')
+
+    # ---------------------------------------------------------------------------------------------------
+
+    def generate_proxy(self, i):
+        # class boilerplate
+        self.c.write('/* ------------------------------------------------------------------------ */\n'
+                     '\n')
+        self.c.write('static void\n'
+                     '%s_proxy_iface_init (%sIface *iface)\n'
+                     '{\n'
+                     '}\n'
+                     '\n'%(i.name_lower, i.camel_name))
+        self.c.write('#define %s_proxy_get_type %s_proxy_get_gtype\n'%(i.name_lower, i.name_lower))
+        self.c.write('G_DEFINE_TYPE_WITH_CODE (%sProxy, %s_proxy, G_TYPE_DBUS_PROXY,\n'%(i.camel_name, i.name_lower))
+        self.c.write('                         G_IMPLEMENT_INTERFACE (%sTYPE_%s, %s_proxy_iface_init));\n'%(i.ns_upper, i.name_upper, i.name_lower))
+        self.c.write('#undef %s_proxy_get_type\n'
+                     '\n'%(i.name_lower))
+
+        # property accessors
+        #
+        # Note that we are guaranteed that prop_id starts at 1 and is
+        # laid out in the same order as introspection data pointers
+        #
+        self.c.write('static void\n'
+                     '%s_proxy_get_property (GObject      *object,\n'
+                     '  guint         prop_id,\n'
+                     '  GValue       *value,\n'
+                     '  GParamSpec   *pspec)\n'
+                     '{\n'%(i.name_lower))
+        if len(i.properties) > 0:
+            self.c.write('  const _ExtendedGDBusPropertyInfo *info;\n'
+                         '  GVariant *variant;\n'
+                         '  g_assert (prop_id - 1 >= 0 && prop_id - 1 < %d);\n'
+                         '  info = _%s_property_info_pointers[prop_id - 1];\n'
+                         '  variant = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (object), info->parent_struct.name);\n'
+                         '  if (info->use_gvariant)\n'
+                         '    g_value_set_variant (value, variant);\n'
+                         '  else\n'
+                         '    g_dbus_gvariant_to_gvalue (variant, value);\n'
+                         '  if (variant != NULL)\n'
+                         '    g_variant_unref (variant);\n'
+                         %(len(i.properties), i.name_lower))
+        self.c.write('}\n'
+                     '\n')
+        if len(i.properties) > 0:
+            self.c.write('static void\n'
+                         '%s_proxy_set_property_cb (GDBusProxy *proxy,\n'
+                         '  GAsyncResult *res,\n'
+                         '  gpointer      user_data)\n'
+                         '{\n'%(i.name_lower))
+            self.c.write('  const _ExtendedGDBusPropertyInfo *info = user_data;\n'
+                         '  GError *error;\n'
+                         '  error = NULL;\n'
+                         '  if (!g_dbus_proxy_call_finish (proxy, res, &error))\n'
+                         '    {\n'
+                         '      g_warning ("Error setting property `%%s\' on interface %s: %%s (%%s, %%d)",\n'
+                         '                 info->parent_struct.name, \n'
+                         '                 error->message, g_quark_to_string (error->domain), error->code);\n'
+                         '      g_error_free (error);\n'
+                         '    }\n'
+                         %(i.name))
+            self.c.write('}\n'
+                         '\n')
+        self.c.write('static void\n'
+                     '%s_proxy_set_property (GObject      *object,\n'
+                     '  guint         prop_id,\n'
+                     '  const GValue *value,\n'
+                     '  GParamSpec   *pspec)\n'
+                     '{\n'%(i.name_lower))
+        if len(i.properties) > 0:
+            self.c.write('  const _ExtendedGDBusPropertyInfo *info;\n'
+                         '  GVariant *variant;\n'
+                         '  g_assert (prop_id - 1 >= 0 && prop_id - 1 < %d);\n'
+                         '  info = _%s_property_info_pointers[prop_id - 1];\n'
+                         '  variant = g_dbus_gvalue_to_gvariant (value, G_VARIANT_TYPE (info->parent_struct.signature));\n'
+                         '  g_dbus_proxy_call (G_DBUS_PROXY (object),\n'
+                         '    "org.freedesktop.DBus.Properties.Set",\n'
+                         '    g_variant_new ("(ssv)", "%s", info->parent_struct.name, variant),\n'
+                         '    G_DBUS_CALL_FLAGS_NONE,\n'
+                         '    -1,\n'
+                         '    NULL, (GAsyncReadyCallback) %s_proxy_set_property_cb, (gpointer) info);\n'
+                         '  g_variant_unref (variant);\n'
+                         %(len(i.properties), i.name_lower, i.name, i.name_lower))
+        self.c.write('}\n'
+                     '\n')
+
+        # signal received
+        self.c.write('static void\n'
+                     '%s_proxy_g_signal (GDBusProxy *proxy,\n'
+                     '  const gchar *sender_name,\n'
+                     '  const gchar *signal_name,\n'
+                     '  GVariant *parameters)\n'
+                     '{\n'%(i.name_lower))
+        self.c.write('  _ExtendedGDBusSignalInfo *info;\n'
+                     '  GVariantIter iter;\n'
+                     '  GVariant *child;\n'
+                     '  GValue *paramv;\n'
+                     '  guint num_params;\n'
+                     '  guint n;\n'
+                     '  guint signal_id;\n');
+        # Note: info could be NULL if we are talking to a newer version of the interface
+        self.c.write('  info = (_ExtendedGDBusSignalInfo *) g_dbus_interface_info_lookup_signal ((GDBusInterfaceInfo *) &_%s_interface_info, signal_name);\n'
+                     '  if (info == NULL)\n'
+                     '    return;\n'
+                     %(i.name_lower))
+        self.c.write ('  num_params = g_variant_n_children (parameters);\n'
+                      '  paramv = g_new0 (GValue, num_params + 1);\n'
+                      '  g_value_init (&paramv[0], %sTYPE_%s);\n'
+                      '  g_value_set_object (&paramv[0], proxy);\n'
+                      %(i.ns_upper, i.name_upper))
+        self.c.write('  g_variant_iter_init (&iter, parameters);\n'
+                     '  n = 1;\n'
+                     '  while ((child = g_variant_iter_next_value (&iter)) != NULL)\n'
+                     '    {\n'
+                     '      _ExtendedGDBusArgInfo *arg_info = (_ExtendedGDBusArgInfo *) info->parent_struct.args[n - 1];\n'
+                     '      if (arg_info->use_gvariant)\n'
+                     '        {\n'
+                     '          g_value_init (&paramv[n], G_TYPE_VARIANT);\n'
+                     '          g_value_set_variant (&paramv[n], child);\n'
+                     '          n++;\n'
+                     '        }\n'
+                     '      else\n'
+                     '        g_dbus_gvariant_to_gvalue (child, &paramv[n++]);\n'
+                     '      g_variant_unref (child);\n'
+                     '    }\n'
+                     )
+        self.c.write('  signal_id = g_signal_lookup (info->signal_name, %sTYPE_%s);\n'
+                     %(i.ns_upper, i.name_upper))
+        self.c.write('  g_signal_emitv (paramv, signal_id, 0, NULL);\n')
+        self.c.write('  for (n = 0; n < num_params + 1; n++)\n'
+                     '    g_value_unset (&paramv[n]);\n'
+                     '  g_free (paramv);\n')
+        self.c.write('}\n'
+                     '\n')
+
+        # property changed
+        self.c.write('static void\n'
+                     '%s_proxy_g_properties_changed (GDBusProxy *proxy,\n'
+                     '  GVariant *changed_properties,\n'
+                     '  const gchar *const *invalidated_properties)\n'
+                     '{\n'%(i.name_lower))
+        # Note: info could be NULL if we are talking to a newer version of the interface
+        self.c.write('  guint n;\n'
+                     '  const gchar *key;\n'
+                     '  GVariantIter *iter;\n'
+                     '  _ExtendedGDBusPropertyInfo *info;\n'
+                     '  g_variant_get (changed_properties, "a{sv}", &iter);\n'
+                     '  while (g_variant_iter_next (iter, "{&sv}", &key, NULL))\n'
+                     '    {\n'
+                     '      info = (_ExtendedGDBusPropertyInfo *) g_dbus_interface_info_lookup_property ((GDBusInterfaceInfo *) &_%s_interface_info, key);\n'
+                     '      if (info != NULL)\n'
+                     '        g_object_notify (G_OBJECT (proxy), info->hyphen_name);\n'
+                     '    }\n'
+                     '  g_variant_iter_free (iter);\n'
+                     '  for (n = 0; invalidated_properties[n] != NULL; n++)\n'
+                     '    {\n'
+                     '      info = (_ExtendedGDBusPropertyInfo *) g_dbus_interface_info_lookup_property ((GDBusInterfaceInfo *) &_%s_interface_info, invalidated_properties[n]);\n'
+                     '      if (info != NULL)\n'
+                     '        g_object_notify (G_OBJECT (proxy), info->hyphen_name);\n'
+                     '    }\n'
+                     '}\n'
+                     '\n'
+                     %(i.name_lower, i.name_lower))
+
+        # class boilerplate
+        self.c.write('static void\n'
+                     '%s_proxy_init (%sProxy *proxy)\n'
+                     '{\n'
+                     '  g_dbus_proxy_set_interface_info (G_DBUS_PROXY (proxy), %s_interface_info ());\n'
+                     '}\n'
+                     '\n'%(i.name_lower, i.camel_name, i.name_lower))
+        self.c.write('static void\n'
+                     '%s_proxy_class_init (%sProxyClass *klass)\n'
+                     '{\n'
+                     '  GObjectClass *gobject_class;\n'
+                     '  GDBusProxyClass *proxy_class;\n'
+                     '\n'
+                     '  gobject_class = G_OBJECT_CLASS (klass);\n'
+                     '  gobject_class->get_property = %s_proxy_get_property;\n'
+                     '  gobject_class->set_property = %s_proxy_set_property;\n'
+                     '\n'
+                     '  proxy_class = G_DBUS_PROXY_CLASS (klass);\n'
+                     '  proxy_class->g_signal = %s_proxy_g_signal;\n'
+                     '  proxy_class->g_properties_changed = %s_proxy_g_properties_changed;\n'
+                     '\n'%(i.name_lower, i.camel_name, i.name_lower, i.name_lower, i.name_lower, i.name_lower))
+        if len(i.properties) > 0:
+            self.c.write('\n'
+                         '  %s_override_properties (gobject_class, 1);\n'%(i.name_lower))
+        self.c.write('}\n')
+
+        # constructors
+        self.c.write('void\n'
+                     '%s_proxy_new (\n'
+                     '    GDBusConnection     *connection,\n'
+                     '    GDBusProxyFlags      flags,\n'
+                     '    const gchar         *name,\n'
+                     '    const gchar         *object_path,\n'
+                     '    GCancellable        *cancellable,\n'
+                     '    GAsyncReadyCallback  callback,\n'
+                     '    gpointer             user_data)\n'
+                     '{\n'
+                     '  g_async_initable_new_async (%sTYPE_%s_PROXY, G_PRIORITY_DEFAULT, cancellable, callback, user_data, "g-flags", flags, "g-name", name, "g-connection", connection, "g-object-path", object_path, "g-interface-name", "%s", NULL);\n'
+                     '}\n'
+                     '\n'
+                     %(i.name_lower, i.ns_upper, i.name_upper, i.name))
+        self.c.write('%s *\n'
+                     '%s_proxy_new_finish (\n'
+                     '    GAsyncResult        *res,\n'
+                     '    GError             **error)\n'
+                     '{\n'
+                     '  GObject *ret;\n'
+                     '  GObject *source_object;\n'
+                     '  source_object = g_async_result_get_source_object (res);\n'
+                     '  ret = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object), res, error);\n'
+                     '  g_object_unref (source_object);\n'
+                     '  if (ret != NULL)\n'
+                     '    return %s%s (ret);\n'
+                     '  else\n'
+                     '    return NULL;\n'
+                     '}\n'
+                     '\n'
+                     %(i.camel_name, i.name_lower, i.ns_upper, i.name_upper))
+        self.c.write('%s *\n'
+                     '%s_proxy_new_sync (\n'
+                     '    GDBusConnection     *connection,\n'
+                     '    GDBusProxyFlags      flags,\n'
+                     '    const gchar         *name,\n'
+                     '    const gchar         *object_path,\n'
+                     '    GCancellable        *cancellable,\n'
+                     '    GError             **error)\n'
+                     '{\n'
+                     '  GInitable *ret;\n'
+                     '  ret = g_initable_new (%sTYPE_%s_PROXY, cancellable, error, "g-flags", flags, "g-name", name, "g-connection", connection, "g-object-path", object_path, "g-interface-name", "%s", NULL);\n'
+                     '  if (ret != NULL)\n'
+                     '    return %s%s (ret);\n'
+                     '  else\n'
+                     '    return NULL;\n'
+                     '}\n'
+                     '\n'
+                     %(i.camel_name, i.name_lower, i.ns_upper, i.name_upper, i.name, i.ns_upper, i.name_upper))
+        self.c.write('\n')
+        self.c.write('void\n'
+                     '%s_proxy_new_for_bus (\n'
+                     '    GBusType             bus_type,\n'
+                     '    GDBusProxyFlags      flags,\n'
+                     '    const gchar         *name,\n'
+                     '    const gchar         *object_path,\n'
+                     '    GCancellable        *cancellable,\n'
+                     '    GAsyncReadyCallback  callback,\n'
+                     '    gpointer             user_data)\n'
+                     '{\n'
+                     '  g_async_initable_new_async (%sTYPE_%s_PROXY, G_PRIORITY_DEFAULT, cancellable, callback, user_data, "g-flags", flags, "g-name", name, "g-bus-type", bus_type, "g-object-path", object_path, "g-interface-name", "%s", NULL);\n'
+                     '}\n'
+                     '\n'
+                     %(i.name_lower, i.ns_upper, i.name_upper, i.name))
+        self.c.write('%s *\n'
+                     '%s_proxy_new_for_bus_finish (\n'
+                     '    GAsyncResult        *res,\n'
+                     '    GError             **error)\n'
+                     '{\n'
+                     '  GObject *ret;\n'
+                     '  GObject *source_object;\n'
+                     '  source_object = g_async_result_get_source_object (res);\n'
+                     '  ret = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object), res, error);\n'
+                     '  g_object_unref (source_object);\n'
+                     '  if (ret != NULL)\n'
+                     '    return %s%s (ret);\n'
+                     '  else\n'
+                     '    return NULL;\n'
+                     '}\n'
+                     '\n'
+                     %(i.camel_name, i.name_lower, i.ns_upper, i.name_upper))
+        self.c.write('%s *\n'
+                     '%s_proxy_new_for_bus_sync (\n'
+                     '    GBusType             bus_type,\n'
+                     '    GDBusProxyFlags      flags,\n'
+                     '    const gchar         *name,\n'
+                     '    const gchar         *object_path,\n'
+                     '    GCancellable        *cancellable,\n'
+                     '    GError             **error)\n'
+                     '{\n'
+                     '  GInitable *ret;\n'
+                     '  ret = g_initable_new (%sTYPE_%s_PROXY, cancellable, error, "g-flags", flags, "g-name", name, "g-bus-type", bus_type, "g-object-path", object_path, "g-interface-name", "%s", NULL);\n'
+                     '  if (ret != NULL)\n'
+                     '    return %s%s (ret);\n'
+                     '  else\n'
+                     '    return NULL;\n'
+                     '}\n'
+                     '\n'
+                     %(i.camel_name, i.name_lower, i.ns_upper, i.name_upper, i.name, i.ns_upper, i.name_upper))
+        self.c.write('\n')
+
+    # ---------------------------------------------------------------------------------------------------
+
+    def generate_stub(self, i):
+        # class boilerplate
+        self.c.write('/* ------------------------------------------------------------------------ */\n'
+                     '\n')
+
+        self.c.write('struct _%sStubPrivate\n'
+                     '{\n'
+                     '  GValueArray *properties;\n'
+                     '  GList *changed_properties;\n'
+                     '  GSource *changed_properties_idle_source;\n'
+                     '  GMainContext *context;\n'
+                     '};\n'
+                     '\n'%i.camel_name)
+
+        self.c.write('static void\n'
+                     '_%s_stub_handle_method_call (\n'
+                     '  GDBusConnection *connection,\n'
+                     '  const gchar *sender,\n'
+                     '  const gchar *object_path,\n'
+                     '  const gchar *interface_name,\n'
+                     '  const gchar *method_name,\n'
+                     '  GVariant *parameters,\n'
+                     '  GDBusMethodInvocation *invocation,\n'
+                     '  gpointer user_data)\n'
+                     '{\n'
+                     '  %sStub *stub = %s%s_STUB (user_data);\n'
+                     '  _ExtendedGDBusMethodInfo *info;\n'
+                     '  GVariantIter iter;\n'
+                     '  GVariant *child;\n'
+                     '  GValue *paramv;\n'
+                     '  guint num_params;\n'
+                     '  guint n;\n'
+                     '  guint signal_id;\n'
+                     '  GValue return_value = {0};\n'
+                     %(i.name_lower, i.camel_name, i.ns_upper, i.name_upper))
+        self.c.write('  info = (_ExtendedGDBusMethodInfo *) g_dbus_method_invocation_get_method_info (invocation);\n'
+                     '  g_assert (info != NULL);\n'
+                     %())
+        self.c.write ('  num_params = g_variant_n_children (parameters);\n'
+                      '  paramv = g_new0 (GValue, num_params + 2);\n'
+                      '  g_value_init (&paramv[0], %sTYPE_%s);\n'
+                      '  g_value_set_object (&paramv[0], stub);\n'
+                      '  g_value_init (&paramv[1], G_TYPE_DBUS_METHOD_INVOCATION);\n'
+                      '  g_value_set_object (&paramv[1], invocation);\n'
+                      %(i.ns_upper, i.name_upper))
+        self.c.write('  g_variant_iter_init (&iter, parameters);\n'
+                     '  n = 2;\n'
+                     '  while ((child = g_variant_iter_next_value (&iter)) != NULL)\n'
+                     '    {\n'
+                     '      _ExtendedGDBusArgInfo *arg_info = (_ExtendedGDBusArgInfo *) info->parent_struct.in_args[n - 2];\n'
+                     '      if (arg_info->use_gvariant)\n'
+                     '        {\n'
+                     '          g_value_init (&paramv[n], G_TYPE_VARIANT);\n'
+                     '          g_value_set_variant (&paramv[n], child);\n'
+                     '          n++;\n'
+                     '        }\n'
+                     '      else\n'
+                     '        g_dbus_gvariant_to_gvalue (child, &paramv[n++]);\n'
+                     '      g_variant_unref (child);\n'
+                     '    }\n'
+                     )
+        self.c.write('  signal_id = g_signal_lookup (info->signal_name, %sTYPE_%s);\n'
+                     %(i.ns_upper, i.name_upper))
+        self.c.write('  g_value_init (&return_value, G_TYPE_BOOLEAN);\n'
+                     '  g_signal_emitv (paramv, signal_id, 0, &return_value);\n'
+                     '  if (!g_value_get_boolean (&return_value))\n'
+                     '    g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD, "Method %s is not implemented on interface %s", method_name, interface_name);\n'
+                     '  g_value_unset (&return_value);\n'
+                     )
+        self.c.write('  for (n = 0; n < num_params + 2; n++)\n'
+                     '    g_value_unset (&paramv[n]);\n'
+                     '  g_free (paramv);\n')
+        self.c.write('}\n'
+                     '\n')
+
+        self.c.write('static GVariant *\n'
+                     '_%s_stub_handle_get_property (\n'
+                     '  GDBusConnection *connection,\n'
+                     '  const gchar *sender,\n'
+                     '  const gchar *object_path,\n'
+                     '  const gchar *interface_name,\n'
+                     '  const gchar *property_name,\n'
+                     '  GError **error,\n'
+                     '  gpointer user_data)\n'
+                     '{\n'
+                     '  %sStub *stub = %s%s_STUB (user_data);\n'
+                     '  GValue value = {0};\n'
+                     '  GParamSpec *pspec;\n'
+                     '  _ExtendedGDBusPropertyInfo *info;\n'
+                     '  GVariant *ret;\n'
+                     %(i.name_lower, i.camel_name, i.ns_upper, i.name_upper))
+        self.c.write('  ret = NULL;\n'
+                     '  info = (_ExtendedGDBusPropertyInfo *) g_dbus_interface_info_lookup_property ((GDBusInterfaceInfo *) &_%s_interface_info, property_name);\n'
+                     '  g_assert (info != NULL);\n'
+                     '  pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (stub), info->hyphen_name);\n'
+                     '  if (pspec == NULL)\n'
+                     '    {\n'
+                     '      g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "No property with name %%s", property_name);\n'
+                     '    }\n'
+                     '  else\n'
+                     '    {\n'
+                     '      g_value_init (&value, pspec->value_type);\n'
+                     '      g_object_get_property (G_OBJECT (stub), info->hyphen_name, &value);\n'
+                     '      ret = g_dbus_gvalue_to_gvariant (&value, G_VARIANT_TYPE (info->parent_struct.signature));\n'
+                     '      g_value_unset (&value);\n'
+                     '    }\n'
+                     '  return ret;\n'
+                     '}\n'
+                     '\n'
+                     %(i.name_lower))
+
+        self.c.write('static gboolean\n'
+                     '_%s_stub_handle_set_property (\n'
+                     '  GDBusConnection *connection,\n'
+                     '  const gchar *sender,\n'
+                     '  const gchar *object_path,\n'
+                     '  const gchar *interface_name,\n'
+                     '  const gchar *property_name,\n'
+                     '  GVariant *variant,\n'
+                     '  GError **error,\n'
+                     '  gpointer user_data)\n'
+                     '{\n'
+                     '  %sStub *stub = %s%s_STUB (user_data);\n'
+                     '  GValue value = {0};\n'
+                     '  GParamSpec *pspec;\n'
+                     '  _ExtendedGDBusPropertyInfo *info;\n'
+                     '  gboolean ret;\n'
+                     %(i.name_lower, i.camel_name, i.ns_upper, i.name_upper))
+        self.c.write('  ret = FALSE;\n'
+                     '  info = (_ExtendedGDBusPropertyInfo *) g_dbus_interface_info_lookup_property ((GDBusInterfaceInfo *) &_%s_interface_info, property_name);\n'
+                     '  g_assert (info != NULL);\n'
+                     '  pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (stub), info->hyphen_name);\n'
+                     '  if (pspec == NULL)\n'
+                     '    {\n'
+                     '      g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "No property with name %%s", property_name);\n'
+                     '    }\n'
+                     '  else\n'
+                     '    {\n'
+                     '      if (info->use_gvariant)\n'
+                     '        g_value_set_variant (&value, variant);\n'
+                     '      else\n'
+                     '        g_dbus_gvariant_to_gvalue (variant, &value);\n'
+                     '      g_object_set_property (G_OBJECT (stub), info->hyphen_name, &value);\n'
+                     '      g_value_unset (&value);\n'
+                     '      ret = TRUE;\n'
+                     '    }\n'
+                     '  return ret;\n'
+                     '}\n'
+                     '\n'
+                     %(i.name_lower))
+
+
+        self.c.write('static const GDBusInterfaceVTable _%s_stub_vtable =\n'
+                     '{\n'
+                     '  _%s_stub_handle_method_call,\n'
+                     '  _%s_stub_handle_get_property,\n'
+                     '  _%s_stub_handle_set_property\n'
+                     '};\n'
+                     '\n'%(i.name_lower, i.name_lower, i.name_lower, i.name_lower))
+
+        self.c.write('static GDBusInterfaceInfo *\n'
+                     '%s_stub_dbus_interface_get_info (GDBusInterfaceStub *stub)\n'
+                     '{\n'
+                     '  return %s_interface_info ();\n'
+                     %(i.name_lower, i.name_lower))
+        self.c.write('}\n'
+                     '\n')
+
+        self.c.write('static GDBusInterfaceVTable *\n'
+                     '%s_stub_dbus_interface_get_vtable (GDBusInterfaceStub *stub)\n'
+                     '{\n'
+                     '  return (GDBusInterfaceVTable *) &_%s_stub_vtable;\n'
+                     %(i.name_lower, i.name_lower))
+        self.c.write('}\n'
+                     '\n')
+
+        self.c.write('static GVariant *\n'
+                     '%s_stub_dbus_interface_get_properties (GDBusInterfaceStub *_stub)\n'
+                     '{\n'
+                     '  %sStub *stub = %s%s_STUB (_stub);\n'
+                     %(i.name_lower, i.camel_name, i.ns_upper, i.name_upper))
+        self.c.write('\n'
+                     '  GVariantBuilder builder;\n'
+                     '  guint n;\n'
+                     '  g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));\n'
+                     '  if (_%s_interface_info.properties == NULL)\n'
+                     '    goto out;\n'
+                     '  for (n = 0; _%s_interface_info.properties[n] != NULL; n++)\n'
+                     '    {\n'
+                     '      GDBusPropertyInfo *info = _%s_interface_info.properties[n];\n'
+                     '      if (info->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE)\n'
+                     '        {\n'
+                     '          GVariant *value;\n'
+                     '          value = _%s_stub_handle_get_property (g_dbus_interface_stub_get_connection (G_DBUS_INTERFACE_STUB (stub)), NULL, g_dbus_interface_stub_get_object_path (G_DBUS_INTERFACE_STUB (stub)), "%s", info->name, NULL, stub);\n'
+                     '          if (value != NULL)\n'
+                     '            {\n'
+                     '              if (g_variant_is_floating (value))\n'
+                     '                g_variant_ref_sink (value);\n'
+                     '              g_variant_builder_add (&builder, "{sv}", info->name, value);\n'
+                     '              g_variant_unref (value);\n'
+                     '            }\n'
+                     '        }\n'
+                     '    }\n'
+                     'out:\n'
+                     '  return g_variant_builder_end (&builder);\n'
+                     '}\n'
+                     '\n'
+                     %(i.name_lower, i.name_lower, i.name_lower, i.name_lower, i.name))
+
+        if len(i.properties) > 0:
+            self.c.write('static gboolean _%s_emit_changed (gpointer user_data);\n'
+                         '\n'
+                         %(i.name_lower))
+
+        self.c.write('static void\n'
+                     '%s_stub_dbus_interface_flush (GDBusInterfaceStub *_stub)\n'
+                     '{\n'
+                     %(i.name_lower))
+        if len(i.properties) > 0:
+            self.c.write('  %sStub *stub = %s%s_STUB (_stub);\n'
+                         '  if (stub->priv->changed_properties_idle_source != NULL)\n'
+                         '    {\n'
+                         '      g_source_destroy (stub->priv->changed_properties_idle_source);\n'
+                         '      stub->priv->changed_properties_idle_source = NULL;\n'
+                         '      _%s_emit_changed (stub);\n'
+                         '    }\n'
+                         %(i.camel_name, i.ns_upper, i.name_upper, i.name_lower))
+        self.c.write('}\n'
+                     '\n')
+
+        for s in i.signals:
+            self.c.write('static void\n'
+                         '_%s_on_signal_%s (\n'
+                         '    %s *object'%(i.name_lower, s.name_lower, i.camel_name))
+            for a in s.args:
+                self.c.write(',\n    %s%s'%(a.ctype_in, a.name))
+            self.c.write(')\n'
+                         '{\n'
+                         '  %sStub *stub = %s%s_STUB (object);\n'
+                         '  GDBusConnection *connection = g_dbus_interface_stub_get_connection (G_DBUS_INTERFACE_STUB (stub));\n'
+                         %(i.camel_name, i.ns_upper, i.name_upper))
+            self.c.write('  if (connection == NULL)\n'
+                         '    return;\n'
+                         '  g_dbus_connection_emit_signal (connection,\n'
+                         '    NULL, g_dbus_interface_stub_get_object_path (G_DBUS_INTERFACE_STUB (stub)), "%s", "%s",\n'
+                         '    g_variant_new ("('
+                         %(i.name, s.name))
+            for a in s.args:
+                self.c.write('%s'%(a.format_in))
+            self.c.write(')"')
+            for a in s.args:
+                self.c.write(',\n                   %s'%(a.name))
+            self.c.write('), NULL);\n')
+            self.c.write('}\n'
+                         '\n')
+
+        self.c.write('static void\n'
+                     '%s_stub_iface_init (%sIface *iface)\n'
+                     '{\n'
+                     %(i.name_lower, i.camel_name))
+        for s in i.signals:
+            self.c.write('  iface->%s = _%s_on_signal_%s;\n'
+                         %(s.name_lower, i.name_lower, s.name_lower))
+        self.c.write('}\n'
+                     '\n')
+        self.c.write('#define %s_stub_get_type %s_stub_get_gtype\n'%(i.name_lower, i.name_lower))
+        self.c.write('G_DEFINE_TYPE_WITH_CODE (%sStub, %s_stub, G_TYPE_DBUS_INTERFACE_STUB,\n'%(i.camel_name, i.name_lower))
+        self.c.write('                         G_IMPLEMENT_INTERFACE (%sTYPE_%s, %s_stub_iface_init));\n'%(i.ns_upper, i.name_upper, i.name_lower))
+        self.c.write('#undef %s_stub_get_type\n'
+                     '\n'%(i.name_lower))
+
+        # finalize
+        self.c.write('static void\n'
+                     '%s_stub_finalize (GObject *object)\n'
+                     '{\n'%(i.name_lower))
+        self.c.write('  %sStub *stub = %s%s_STUB (object);\n'%(i.camel_name, i.ns_upper, i.name_upper))
+        if len(i.properties) > 0:
+            self.c.write('  g_value_array_free (stub->priv->properties);\n')
+        self.c.write('  g_list_foreach (stub->priv->changed_properties, (GFunc) _changed_property_free, NULL);\n')
+        self.c.write('  g_list_free (stub->priv->changed_properties);\n')
+        self.c.write('  if (stub->priv->changed_properties_idle_source != NULL)\n')
+        self.c.write('    g_source_destroy (stub->priv->changed_properties_idle_source);\n')
+        self.c.write('  if (stub->priv->context != NULL)\n')
+        self.c.write('    g_main_context_unref (stub->priv->context);\n')
+        self.c.write('  G_OBJECT_CLASS (%s_stub_parent_class)->finalize (object);\n'
+                     '}\n'
+                     '\n'%(i.name_lower))
+
+        # property accessors (TODO: generate PropertiesChanged signals in setter)
+        self.c.write('static void\n'
+                     '%s_stub_get_property (GObject      *object,\n'
+                     '  guint         prop_id,\n'
+                     '  GValue       *value,\n'
+                     '  GParamSpec   *pspec)\n'
+                     '{\n'%(i.name_lower))
+        self.c.write('  %sStub *stub = %s%s_STUB (object);\n'
+                     '  g_assert (prop_id - 1 >= 0 && prop_id - 1 < %d);\n'
+                     '  g_value_copy (&stub->priv->properties->values[prop_id - 1], value);\n'
+                     %(i.camel_name, i.ns_upper, i.name_upper, len(i.properties)))
+        self.c.write('}\n'
+                     '\n')
+        if len(i.properties) > 0:
+            self.c.write('static gboolean\n'
+                         '_%s_emit_changed (gpointer user_data)\n'
+                         '{\n'
+                         '  %sStub *stub = %s%s_STUB (user_data);\n'
+                         %(i.name_lower, i.camel_name, i.ns_upper, i.name_upper))
+            self.c.write('  GList *l;\n'
+                         '  GVariantBuilder builder;\n'
+                         '  GVariantBuilder invalidated_builder;\n'
+                         '  g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));\n'
+                         '  g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));\n'
+                         '  for (l = stub->priv->changed_properties; l != NULL; l = l->next)\n'
+                         '    {\n'
+                         '      ChangedProperty *cp = l->data;\n'
+                         '      GVariant *variant;\n'
+                         '      variant = g_dbus_gvalue_to_gvariant (&cp->value, G_VARIANT_TYPE (cp->info->parent_struct.signature));\n'
+                         '      g_variant_builder_add (&builder, "{sv}", cp->info->parent_struct.name, variant);\n'
+                         '      g_variant_unref (variant);\n'
+                         '    }\n'
+                         '  g_dbus_connection_emit_signal (g_dbus_interface_stub_get_connection (G_DBUS_INTERFACE_STUB (stub)),\n'
+                         '                                 NULL, g_dbus_interface_stub_get_object_path (G_DBUS_INTERFACE_STUB (stub)),\n'
+                         '                                 "org.freedesktop.DBus.Properties",\n'
+                         '                                 "PropertiesChanged",\n'
+                         '                                 g_variant_new ("(sa{sv}as)",\n'
+                         '                                                "%s",\n'
+                         '                                                &builder, &invalidated_builder),\n'
+                         '                                 NULL);\n'
+                         %(i.name))
+            self.c.write('  g_list_foreach (stub->priv->changed_properties, (GFunc) _changed_property_free, NULL);\n')
+            self.c.write('  g_list_free (stub->priv->changed_properties);\n')
+            self.c.write('  stub->priv->changed_properties = NULL;\n')
+            self.c.write('  stub->priv->changed_properties_idle_source = NULL;\n')
+            self.c.write('  return FALSE;\n'
+                         '}\n'
+                         '\n')
+            # if property is already scheduled then re-use entry
+            self.c.write('static void\n'
+                         '_%s_schedule_emit_changed (%sStub *stub, const _ExtendedGDBusPropertyInfo *info, GParamSpec *pspec, const GValue *value)\n'
+                         '{\n'
+                         '  ChangedProperty *cp;\n'
+                         '  GList *l;\n'
+                         '  cp = NULL;\n'
+                         '  for (l = stub->priv->changed_properties; l != NULL; l = l->next)\n'
+                         '    {\n'
+                         '      ChangedProperty *i_cp = l->data;\n'
+                         '      if (i_cp->info == info)\n'
+                         '        {\n'
+                         '          cp = i_cp;\n'
+                         '          g_value_unset (&cp->value);\n'
+                         '          break;\n'
+                         '        }\n'
+                         '    }\n'
+                         '  if (cp == NULL)\n'
+                         '    {\n'
+                         '      cp = g_new0 (ChangedProperty, 1);\n'
+                         '      cp->pspec = pspec;\n'
+                         '      cp->info = info;\n'
+                         '      stub->priv->changed_properties = g_list_prepend (stub->priv->changed_properties, cp);\n'
+                         '    }\n'
+                         '  g_value_init (&cp->value, G_VALUE_TYPE (value));\n'
+                         '  g_value_copy (value, &cp->value);\n'
+                         '  if (stub->priv->changed_properties_idle_source == NULL)\n'
+                         '    {\n'
+                         '      stub->priv->changed_properties_idle_source = g_idle_source_new ();\n'
+                         '      g_source_set_priority (stub->priv->changed_properties_idle_source, G_PRIORITY_DEFAULT);\n'
+                         '      g_source_set_callback (stub->priv->changed_properties_idle_source, _%s_emit_changed, g_object_ref (stub), (GDestroyNotify) g_object_unref);\n'
+                         '      g_source_attach (stub->priv->changed_properties_idle_source, stub->priv->context);\n'
+                         '      g_source_unref (stub->priv->changed_properties_idle_source);\n'
+                         '    }\n'
+                         '}\n'
+                         '\n'
+                         %(i.name_lower, i.camel_name, i.name_lower))
+        self.c.write('static void\n'
+                     '%s_stub_set_property (GObject      *object,\n'
+                     '  guint         prop_id,\n'
+                     '  const GValue *value,\n'
+                     '  GParamSpec   *pspec)\n'
+                     '{\n'%(i.name_lower))
+        if len(i.properties) > 0:
+            self.c.write('  %sStub *stub = %s%s_STUB (object);\n'
+                         '  g_assert (prop_id - 1 >= 0 && prop_id - 1 < %d);\n'
+                         '  if (!_g_value_equal (value, &stub->priv->properties->values[prop_id - 1]))\n'
+                         '    {\n'
+                         '      g_value_copy (value, &stub->priv->properties->values[prop_id - 1]);\n'
+                         '      g_object_notify_by_pspec (object, pspec);\n'
+                         '      if (g_dbus_interface_stub_get_connection (G_DBUS_INTERFACE_STUB (stub)) != NULL)\n'
+                         '        _%s_schedule_emit_changed (stub, _%s_property_info_pointers[prop_id - 1], pspec, value);\n'
+                         '    }\n'
+                         %(i.camel_name, i.ns_upper, i.name_upper, len(i.properties), i.name_lower, i.name_lower))
+        self.c.write('}\n'
+                     '\n')
+
+        self.c.write('static void\n'
+                     '%s_stub_init (%sStub *stub)\n'
+                     '{\n'
+                     '  stub->priv = G_TYPE_INSTANCE_GET_PRIVATE (stub, %sTYPE_%s_STUB, %sStubPrivate);\n'
+                     %(i.name_lower, i.camel_name, i.ns_upper, i.name_upper, i.camel_name))
+        self.c.write('  stub->priv->context = g_main_context_get_thread_default ();\n')
+        self.c.write('  if (stub->priv->context != NULL)\n')
+        self.c.write('    g_main_context_ref (stub->priv->context);\n')
+        if len(i.properties) > 0:
+            self.c.write('  stub->priv->properties = g_value_array_new (%d);\n'%(len(i.properties)))
+            n = 0
+            for p in i.properties:
+                self.c.write('  g_value_array_append (stub->priv->properties, NULL);\n')
+                self.c.write('  g_value_init (&stub->priv->properties->values[%d], %s);\n'%(n, p.arg.gtype))
+                n += 1
+        self.c.write('}\n'
+                     '\n')
+        self.c.write('static void\n'
+                     '%s_stub_class_init (%sStubClass *klass)\n'
+                     '{\n'
+                     '  GObjectClass *gobject_class;\n'
+                     '  GDBusInterfaceStubClass *stub_class;\n'
+                     '\n'
+                     '  g_type_class_add_private (klass, sizeof (%sStubPrivate));\n'
+                     '\n'
+                     '  gobject_class = G_OBJECT_CLASS (klass);\n'
+                     '  gobject_class->finalize = %s_stub_finalize;\n'
+                     '  gobject_class->get_property = %s_stub_get_property;\n'
+                     '  gobject_class->set_property = %s_stub_set_property;\n'
+                     '\n'%(i.name_lower, i.camel_name, i.camel_name, i.name_lower, i.name_lower, i.name_lower))
+        if len(i.properties) > 0:
+            self.c.write('\n'
+                         '  %s_override_properties (gobject_class, 1);\n'%(i.name_lower))
+        self.c.write('\n'
+                     '  stub_class = G_DBUS_INTERFACE_STUB_CLASS (klass);\n');
+        self.c.write('  stub_class->get_info = %s_stub_dbus_interface_get_info;\n'%(i.name_lower))
+        self.c.write('  stub_class->get_properties = %s_stub_dbus_interface_get_properties;\n'%(i.name_lower))
+        self.c.write('  stub_class->flush = %s_stub_dbus_interface_flush;\n'%(i.name_lower))
+        self.c.write('  stub_class->get_vtable = %s_stub_dbus_interface_get_vtable;\n'%(i.name_lower))
+        self.c.write('}\n'
+                     '\n')
+
+        # constructors
+        self.c.write('%s *\n'
+                     '%s_stub_new (void)\n'
+                     '{\n'
+                     '  return %s%s (g_object_new (%sTYPE_%s_STUB, NULL));\n'
+                     '}\n'
+                     '\n'%(i.camel_name, i.name_lower, i.ns_upper, i.name_upper, i.ns_upper, i.name_upper))
+
+    # ---------------------------------------------------------------------------------------------------
+
+    # From https://bugzilla.gnome.org/show_bug.cgi?id=567087
+    #
+    # See https://bugzilla.gnome.org/show_bug.cgi?id=401080 for the request
+    # to include this in libgobject
+    #
+    def generate_generic_marshaller(self):
+        self.c.write('#include <ffi.h>\n'
+                     ''
+                     'static ffi_type *\n'
+                     'value_to_ffi_type (const GValue *gvalue, gpointer *value)\n'
+                     '{\n'
+                     '  ffi_type *rettype = NULL;\n'
+                     '  GType type = g_type_fundamental (G_VALUE_TYPE (gvalue));\n'
+                     '  g_assert (type != G_TYPE_INVALID);\n'
+                     '\n'
+                     '  switch (type)\n'
+                     '    {\n'
+                     '    case G_TYPE_BOOLEAN:\n'
+                     '    case G_TYPE_CHAR:\n'
+                     '    case G_TYPE_INT:\n'
+                     '      rettype = &ffi_type_sint;\n'
+                     '      *value = (gpointer)&(gvalue->data[0].v_int);\n'
+                     '      break;\n'
+                     '    case G_TYPE_UCHAR:\n'
+                     '    case G_TYPE_UINT:\n'
+                     '      rettype = &ffi_type_uint;\n'
+                     '      *value = (gpointer)&(gvalue->data[0].v_uint);\n'
+                     '      break;\n'
+                     '    case G_TYPE_STRING:\n'
+                     '    case G_TYPE_OBJECT:\n'
+                     '    case G_TYPE_BOXED:\n'
+                     '    case G_TYPE_POINTER:\n'
+                     '    case G_TYPE_INTERFACE:\n'
+                     '    case G_TYPE_VARIANT:\n'
+                     '      rettype = &ffi_type_pointer;\n'
+                     '      *value = (gpointer)&(gvalue->data[0].v_pointer);\n'
+                     '      break;\n'
+                     '    case G_TYPE_FLOAT:\n'
+                     '      rettype = &ffi_type_float;\n'
+                     '      *value = (gpointer)&(gvalue->data[0].v_float);\n'
+                     '      break;\n'
+                     '    case G_TYPE_DOUBLE:\n'
+                     '      rettype = &ffi_type_double;\n'
+                     '      *value = (gpointer)&(gvalue->data[0].v_double);\n'
+                     '      break;\n'
+                     '    case G_TYPE_LONG:\n'
+                     '      rettype = &ffi_type_slong;\n'
+                     '      *value = (gpointer)&(gvalue->data[0].v_long);\n'
+                     '      break;\n'
+                     '    case G_TYPE_ULONG:\n'
+                     '      rettype = &ffi_type_ulong;\n'
+                     '      *value = (gpointer)&(gvalue->data[0].v_ulong);\n'
+                     '      break;\n'
+                     '    case G_TYPE_INT64:\n'
+                     '      rettype = &ffi_type_sint64;\n'
+                     '      *value = (gpointer)&(gvalue->data[0].v_int64);\n'
+                     '      break;\n'
+                     '    case G_TYPE_UINT64:\n'
+                     '      rettype = &ffi_type_uint64;\n'
+                     '      *value = (gpointer)&(gvalue->data[0].v_uint64);\n'
+                     '      break;\n'
+                     '    default:\n'
+                     '      rettype = &ffi_type_pointer;\n'
+                     '      *value = NULL;\n'
+                     '      g_warning ("value_to_ffi_type: Unsupported fundamental type: %s", g_type_name (type));\n'
+                     '      break;\n'
+                     '    }\n'
+                     '  return rettype;\n'
+                     '}\n'
+                     '\n'
+                     'static void\n'
+                     'value_from_ffi_type (GValue *gvalue, gpointer *value)\n'
+                     '{\n'
+                     '  switch (g_type_fundamental (G_VALUE_TYPE (gvalue)))\n'
+                     '    {\n'
+                     '    case G_TYPE_INT:\n'
+                     '      g_value_set_int (gvalue, *(gint*)value);\n'
+                     '      break;\n'
+                     '    case G_TYPE_FLOAT:\n'
+                     '      g_value_set_float (gvalue, *(gfloat*)value);\n'
+                     '      break;\n'
+                     '    case G_TYPE_DOUBLE:\n'
+                     '      g_value_set_double (gvalue, *(gdouble*)value);\n'
+                     '      break;\n'
+                     '    case G_TYPE_BOOLEAN:\n'
+                     '      g_value_set_boolean (gvalue, *(gboolean*)value);\n'
+                     '      break;\n'
+                     '    case G_TYPE_STRING:\n'
+                     '      g_value_set_string (gvalue, *(gchar**)value);\n'
+                     '      break;\n'
+                     '    case G_TYPE_CHAR:\n'
+                     '      g_value_set_char (gvalue, *(gchar*)value);\n'
+                     '      break;\n'
+                     '    case G_TYPE_UCHAR:\n'
+                     '      g_value_set_uchar (gvalue, *(guchar*)value);\n'
+                     '      break;\n'
+                     '    case G_TYPE_UINT:\n'
+                     '      g_value_set_uint (gvalue, *(guint*)value);\n'
+                     '      break;\n'
+                     '    case G_TYPE_POINTER:\n'
+                     '      g_value_set_pointer (gvalue, *(gpointer*)value);\n'
+                     '      break;\n'
+                     '    case G_TYPE_LONG:\n'
+                     '      g_value_set_long (gvalue, *(glong*)value);\n'
+                     '      break;\n'
+                     '    case G_TYPE_ULONG:\n'
+                     '      g_value_set_ulong (gvalue, *(gulong*)value);\n'
+                     '      break;\n'
+                     '    case G_TYPE_INT64:\n'
+                     '      g_value_set_int64 (gvalue, *(gint64*)value);\n'
+                     '      break;\n'
+                     '    case G_TYPE_UINT64:\n'
+                     '      g_value_set_uint64 (gvalue, *(guint64*)value);\n'
+                     '      break;\n'
+                     '    case G_TYPE_BOXED:\n'
+                     '      g_value_set_boxed (gvalue, *(gpointer*)value);\n'
+                     '      break;\n'
+                     '    default:\n'
+                     '      g_warning ("value_from_ffi_type: Unsupported fundamental type: %s",\n'
+                     '                g_type_name (g_type_fundamental (G_VALUE_TYPE (gvalue))));\n'
+                     '    }\n'
+                     '}\n'
+                     '\n'
+                     'static void\n'
+                     '_cclosure_marshal_generic (GClosure *closure,\n'
+                     '                             GValue *return_gvalue,\n'
+                     '                             guint n_param_values,\n'
+                     '                             const GValue *param_values,\n'
+                     '                             gpointer invocation_hint,\n'
+                     '                             gpointer marshal_data)\n'
+                     '{\n'
+                     '  ffi_type *rtype;\n'
+                     '  void *rvalue;\n'
+                     '  int n_args;\n'
+                     '  ffi_type **atypes;\n'
+                     '  void **args;\n'
+                     '  int i;\n'
+                     '  ffi_cif cif;\n'
+                     '  GCClosure *cc = (GCClosure*) closure;\n'
+                     '\n'
+                     '  if (return_gvalue && G_VALUE_TYPE (return_gvalue)) \n'
+                     '    {\n'
+                     '      rtype = value_to_ffi_type (return_gvalue, &rvalue);\n'
+                     '    }\n'
+                     '  else \n'
+                     '    {\n'
+                     '      rtype = &ffi_type_void;\n'
+                     '    }\n'
+                     '\n'
+                     '  rvalue = g_alloca (MAX (rtype->size, sizeof (ffi_arg)));\n'
+                     '  \n'
+                     '  n_args = n_param_values + 1;\n'
+                     '  atypes = g_alloca (sizeof (ffi_type *) * n_args);\n'
+                     '  args =  g_alloca (sizeof (gpointer) * n_args);\n'
+                     '\n'
+                     '  if (G_CCLOSURE_SWAP_DATA (closure))\n'
+                     '    {\n'
+                     '      atypes[n_args-1] = value_to_ffi_type (param_values + 0,  \n'
+                     '                                            &args[n_args-1]);\n'
+                     '      atypes[0] = &ffi_type_pointer;\n'
+                     '      args[0] = &closure->data;\n'
+                     '    }\n'
+                     '  else\n'
+                     '    {\n'
+                     '      atypes[0] = value_to_ffi_type (param_values + 0, &args[0]);\n'
+                     '      atypes[n_args-1] = &ffi_type_pointer;\n'
+                     '      args[n_args-1] = &closure->data;\n'
+                     '    }\n'
+                     '\n'
+                     '  for (i = 1; i < n_args - 1; i++)\n'
+                     '    atypes[i] = value_to_ffi_type (param_values + i, &args[i]);\n'
+                     '\n'
+                     '  if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_args, rtype, atypes) != FFI_OK)\n'
+                     '    return;\n'
+                     '\n'
+                     '  ffi_call (&cif, marshal_data ? marshal_data : cc->callback, rvalue, args);\n'
+                     '\n'
+                     '  if (return_gvalue && G_VALUE_TYPE (return_gvalue))\n'
+                     '    value_from_ffi_type (return_gvalue, rvalue);\n'
+                     '}\n'
+                     '\n')
+
+    # ---------------------------------------------------------------------------------------------------
+
+    def generate_object_manager_client(self):
+        self.c.write('/* ------------------------------------------------------------------------\n'
+                     ' * Code for proxy manager\n'
+                     ' * ------------------------------------------------------------------------\n'
+                     ' */\n'
+                     '\n')
+
+        # class boilerplate
+        self.c.write('/* ------------------------------------------------------------------------ */\n'
+                     '\n')
+        self.c.write('#define %sobject_manager_client_get_type %sobject_manager_client_get_gtype\n'%(self.ns_lower, self.ns_lower))
+        self.c.write('G_DEFINE_TYPE (%sObjectManagerClient, %sobject_manager_client, G_TYPE_DBUS_OBJECT_MANAGER_CLIENT);\n'%(self.namespace, self.ns_lower))
+        self.c.write('#undef %sobject_manager_client_get_type\n'
+                     '\n'%(self.ns_lower))
+
+        # class boilerplate
+        self.c.write('static void\n'
+                     '%sobject_manager_client_init (%sObjectManagerClient *manager)\n'
+                     '{\n'
+                     '}\n'
+                     '\n'%(self.ns_lower, self.namespace))
+        self.c.write('static void\n'
+                     '%sobject_manager_client_class_init (%sObjectManagerClientClass *klass)\n'
+                     '{\n'
+                     '}\n'
+                     '\n'%(self.ns_lower, self.namespace))
+
+        self.c.write('static GType\n'
+                     '_%sobject_manager_client_get_proxy_type_func (GDBusObjectManagerClient *manager, const gchar *object_path, const gchar *interface_name, gpointer user_data)\n'
+                     '{\n'
+                     %(self.ns_lower))
+        self.c.write('  static gsize once_init_value = 0;\n'
+                     '  static GHashTable *lookup_hash;\n'
+                     '  GType ret;\n'
+                     '\n'
+                     '  if (g_once_init_enter (&once_init_value))\n'
+                     '    {\n'
+                     '      lookup_hash = g_hash_table_new (g_str_hash, g_str_equal);\n')
+        for i in self.ifaces:
+            self.c.write('      g_hash_table_insert (lookup_hash, "%s", GSIZE_TO_POINTER (%sTYPE_%s_PROXY));\n'
+                         %(i.name, i.ns_upper, i.name_upper))
+        self.c.write('      g_once_init_leave (&once_init_value, 1);\n'
+                     '    }\n')
+        self.c.write('  ret = (GType) GPOINTER_TO_SIZE (g_hash_table_lookup (lookup_hash, interface_name));\n'
+                     '  if (ret == (GType) 0)\n'
+                     '    ret = G_TYPE_DBUS_PROXY;\n')
+        self.c.write('  return ret;\n'
+                     '}\n'
+                     '\n')
+
+        self.c.write('GDBusProxyTypeFunc\n'
+                     '%sobject_manager_client_get_proxy_type_func (void)\n'
+                     '{\n'
+                     '  return _%sobject_manager_client_get_proxy_type_func;\n'
+                     '}\n'
+                     '\n'
+                     %(self.ns_lower, self.ns_lower))
+
+        # constructors
+        self.c.write('void\n'
+                     '%sobject_manager_client_new (\n'
+                     '    GDBusConnection        *connection,\n'
+                     '    GDBusObjectManagerClientFlags  flags,\n'
+                     '    const gchar            *name,\n'
+                     '    const gchar            *object_path,\n'
+                     '    GCancellable           *cancellable,\n'
+                     '    GAsyncReadyCallback     callback,\n'
+                     '    gpointer                user_data)\n'
+                     '{\n'
+                     '  g_async_initable_new_async (%sTYPE_OBJECT_MANAGER_CLIENT, G_PRIORITY_DEFAULT, cancellable, callback, user_data, "flags", flags, "name", name, "connection", connection, "object-path", object_path, "get-proxy-type-func", _%sobject_manager_client_get_proxy_type_func, NULL);\n'
+                     '}\n'
+                     '\n'
+                     %(self.ns_lower, self.ns_upper, self.ns_lower))
+        self.c.write('GDBusObjectManager *\n'
+                     '%sobject_manager_client_new_finish (\n'
+                     '    GAsyncResult        *res,\n'
+                     '    GError             **error)\n'
+                     '{\n'
+                     '  GObject *ret;\n'
+                     '  GObject *source_object;\n'
+                     '  source_object = g_async_result_get_source_object (res);\n'
+                     '  ret = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object), res, error);\n'
+                     '  g_object_unref (source_object);\n'
+                     '  if (ret != NULL)\n'
+                     '    return G_DBUS_OBJECT_MANAGER (ret);\n'
+                     '  else\n'
+                     '    return NULL;\n'
+                     '}\n'
+                     '\n'
+                     %(self.ns_lower))
+        self.c.write('GDBusObjectManager *\n'
+                     '%sobject_manager_client_new_sync (\n'
+                     '    GDBusConnection        *connection,\n'
+                     '    GDBusObjectManagerClientFlags  flags,\n'
+                     '    const gchar            *name,\n'
+                     '    const gchar            *object_path,\n'
+                     '    GCancellable           *cancellable,\n'
+                     '    GError                **error)\n'
+                     '{\n'
+                     '  GInitable *ret;\n'
+                     '  ret = g_initable_new (%sTYPE_OBJECT_MANAGER_CLIENT, cancellable, error, "flags", flags, "name", name, "connection", connection, "object-path", object_path, "get-proxy-type-func", _%sobject_manager_client_get_proxy_type_func, NULL);\n'
+                     '  if (ret != NULL)\n'
+                     '    return G_DBUS_OBJECT_MANAGER (ret);\n'
+                     '  else\n'
+                     '    return NULL;\n'
+                     '}\n'
+                     '\n'
+                     %(self.ns_lower, self.ns_upper, self.ns_lower))
+        self.c.write('\n')
+        self.c.write('void\n'
+                     '%sobject_manager_client_new_for_bus (\n'
+                     '    GBusType                bus_type,\n'
+                     '    GDBusObjectManagerClientFlags  flags,\n'
+                     '    const gchar            *name,\n'
+                     '    const gchar            *object_path,\n'
+                     '    GCancellable           *cancellable,\n'
+                     '    GAsyncReadyCallback     callback,\n'
+                     '    gpointer                user_data)\n'
+                     '{\n'
+                     '  g_async_initable_new_async (%sTYPE_OBJECT_MANAGER_CLIENT, G_PRIORITY_DEFAULT, cancellable, callback, user_data, "flags", flags, "name", name, "bus-type", bus_type, "object-path", object_path, "get-proxy-type-func", _%sobject_manager_client_get_proxy_type_func, NULL);\n'
+                     '}\n'
+                     '\n'
+                     %(self.ns_lower, self.ns_upper, self.ns_lower))
+        self.c.write('GDBusObjectManager *\n'
+                     '%sobject_manager_client_new_for_bus_finish (\n'
+                     '    GAsyncResult        *res,\n'
+                     '    GError             **error)\n'
+                     '{\n'
+                     '  GObject *ret;\n'
+                     '  GObject *source_object;\n'
+                     '  source_object = g_async_result_get_source_object (res);\n'
+                     '  ret = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object), res, error);\n'
+                     '  g_object_unref (source_object);\n'
+                     '  if (ret != NULL)\n'
+                     '    return G_DBUS_OBJECT_MANAGER (ret);\n'
+                     '  else\n'
+                     '    return NULL;\n'
+                     '}\n'
+                     '\n'
+                     %(self.ns_lower))
+        self.c.write('GDBusObjectManager *\n'
+                     '%sobject_manager_client_new_for_bus_sync (\n'
+                     '    GBusType                bus_type,\n'
+                     '    GDBusObjectManagerClientFlags  flags,\n'
+                     '    const gchar            *name,\n'
+                     '    const gchar            *object_path,\n'
+                     '    GCancellable           *cancellable,\n'
+                     '    GError                **error)\n'
+                     '{\n'
+                     '  GInitable *ret;\n'
+                     '  ret = g_initable_new (%sTYPE_OBJECT_MANAGER_CLIENT, cancellable, error, "flags", flags, "name", name, "bus-type", bus_type, "object-path", object_path, "get-proxy-type-func", _%sobject_manager_client_get_proxy_type_func, NULL);\n'
+                     '  if (ret != NULL)\n'
+                     '    return G_DBUS_OBJECT_MANAGER (ret);\n'
+                     '  else\n'
+                     '    return NULL;\n'
+                     '}\n'
+                     '\n'
+                     %(self.ns_lower, self.ns_upper, self.ns_lower))
+        self.c.write('\n')
+
+    # ---------------------------------------------------------------------------------------------------
+
+    def generate(self):
+        self.generate_intro()
+        self.generate_generic_marshaller()
+        self.declare_types()
+        for i in self.ifaces:
+            self.c.write('/* ------------------------------------------------------------------------\n'
+                         ' * Code for interface %s\n'
+                         ' * ------------------------------------------------------------------------\n'
+                         ' */\n'
+                         '\n'%(i.name))
+            self.generate_introspection_for_interface(i)
+            self.generate_interface(i)
+            self.generate_property_accessors(i)
+            self.generate_signal_emitters(i)
+            self.generate_method_calls(i)
+            self.generate_method_completers(i)
+            self.generate_proxy(i)
+            self.generate_stub(i)
+        self.generate_object_manager_client()
+        self.generate_outro()
diff --git a/gio/gdbus-codegen/codegen_docbook.py b/gio/gdbus-codegen/codegen_docbook.py
new file mode 100644 (file)
index 0000000..889c965
--- /dev/null
@@ -0,0 +1,283 @@
+# -*- Mode: Python -*-
+
+import sys
+import argparse
+
+import config
+import utils
+import dbustypes
+import parser
+
+# ----------------------------------------------------------------------------------------------------
+
+class DocbookCodeGenerator:
+    def __init__(self, ifaces, docbook):
+        self.ifaces = ifaces
+        self.docbook = docbook
+
+    def print_method_prototype(self, i, m, in_synopsis):
+        max_method_len = 0
+        if in_synopsis:
+            for _m in i.methods:
+                max_method_len = max(len(_m.name), max_method_len)
+        else:
+            max_method_len = max(len(m.name), max_method_len)
+
+        max_signature_len = 0
+        if in_synopsis:
+            for _m in i.methods:
+                for a in _m.in_args:
+                    max_signature_len = max(len(a.signature), max_signature_len)
+                for a in _m.out_args:
+                    max_signature_len = max(len(a.signature), max_signature_len)
+        else:
+            for a in m.in_args:
+                max_signature_len = max(len(a.signature), max_signature_len)
+            for a in m.out_args:
+                max_signature_len = max(len(a.signature), max_signature_len)
+
+        if in_synopsis:
+            self.out.write('<link linkend="gdbus-method-%s.%s">%s</link>%*s ('
+                           %(utils.dots_to_hyphens(i.name), m.name, m.name, max_method_len - len(m.name), ''))
+        else:
+            self.out.write('%s%*s ('
+                           %(m.name, max_method_len - len(m.name), ''))
+        count = 0
+        for a in m.in_args:
+            if (count > 0):
+                self.out.write(',\n%*s'%(max_method_len + 2, ''))
+            self.out.write('IN  %s%*s %s'%(a.signature, max_signature_len - len(a.signature), '', a.name))
+            count = count + 1
+        for a in m.out_args:
+            if (count > 0):
+                self.out.write(',\n%*s'%(max_method_len + 2, ''))
+            self.out.write('OUT %s%*s %s'%(a.signature, max_signature_len - len(a.signature), '', a.name))
+            count = count + 1
+        self.out.write(');\n')
+
+    def print_signal_prototype(self, i, s, in_synopsis):
+        max_signal_len = 0
+        if in_synopsis:
+            for _s in i.signals:
+                max_signal_len = max(len(_s.name), max_signal_len)
+        else:
+            max_signal_len = max(len(s.name), max_signal_len)
+
+        max_signature_len = 0
+        if in_synopsis:
+            for _s in i.signals:
+                for a in _s.args:
+                    max_signature_len = max(len(a.signature), max_signature_len)
+        else:
+            for a in s.args:
+                max_signature_len = max(len(a.signature), max_signature_len)
+
+        if in_synopsis:
+            self.out.write('<link linkend="gdbus-signal-%s.%s">%s</link>%*s ('
+                           %(utils.dots_to_hyphens(i.name), s.name, s.name, max_signal_len - len(s.name), ''))
+        else:
+            self.out.write('%s%*s ('
+                           %(s.name, max_signal_len - len(s.name), ''))
+        count = 0
+        for a in s.args:
+            if (count > 0):
+                self.out.write(',\n%*s'%(max_signal_len + 2, ''))
+            self.out.write('%s%*s %s'%(a.signature, max_signature_len - len(a.signature), '', a.name))
+            count = count + 1
+        self.out.write(');\n')
+
+    def print_property_prototype(self, i, p, in_synopsis):
+        max_property_len = 0
+        if in_synopsis:
+            for _p in i.properties:
+                max_property_len = max(len(_p.name), max_property_len)
+        else:
+            max_property_len = max(len(p.name), max_property_len)
+
+        max_signature_len = 0
+        if in_synopsis:
+            for _p in i.properties:
+                max_signature_len = max(len(_p.signature), max_signature_len)
+        else:
+            max_signature_len = max(len(p.signature), max_signature_len)
+
+        if in_synopsis:
+            self.out.write('<link linkend="gdbus-property-%s.%s">%s</link>%*s'
+                           %(utils.dots_to_hyphens(i.name), p.name, p.name, max_property_len - len(p.name), ''))
+        else:
+            self.out.write('%s%*s'
+                           %(p.name, max_property_len - len(p.name), ''))
+        if p.readable and p.writable:
+            access = 'readwrite'
+        elif p.readable:
+            access = 'readable '
+        else:
+            access = 'writable '
+        self.out.write('  %s  %s\n'%(access, p.signature))
+
+
+    def print_synopsis_methods(self, i):
+        self.out.write('  <refsynopsisdiv role="synopsis">\n'%())
+        self.out.write('    <title role="synopsis.title">Methods</title>\n'%())
+        self.out.write('    <synopsis>\n'%())
+        for m in i.methods:
+            self.print_method_prototype(i, m, in_synopsis=True)
+        self.out.write('</synopsis>\n'%())
+        self.out.write('  </refsynopsisdiv>\n'%())
+
+    def print_synopsis_signals(self, i):
+        self.out.write('  <refsect1 role="signal_proto">\n'%())
+        self.out.write('    <title role="signal_proto.title">Signals</title>\n'%())
+        self.out.write('    <synopsis>\n'%())
+        for s in i.signals:
+            self.print_signal_prototype(i, s, in_synopsis=True)
+        self.out.write('</synopsis>\n'%())
+        self.out.write('  </refsect1>\n'%())
+
+    def print_synopsis_properties(self, i):
+        self.out.write('  <refsect1 role="properties">\n'%())
+        self.out.write('    <title role="properties.title">Properties</title>\n'%())
+        self.out.write('    <synopsis>\n'%())
+        for p in i.properties:
+            self.print_property_prototype(i, p, in_synopsis=True)
+        self.out.write('</synopsis>\n'%())
+        self.out.write('  </refsect1>\n'%())
+
+    def print_method(self, i, m):
+        self.out.write('<refsect2 role="method" id="gdbus-method-%s.%s">\n'%(utils.dots_to_hyphens(i.name), m.name))
+        self.out.write('  <title>The %s() method</title>\n'%(m.name))
+        self.out.write('  <indexterm zone="gdbus-method-%s.%s"><primary sortas="%s.%s">%s.%s()</primary></indexterm>\n'%(utils.dots_to_hyphens(i.name), m.name, i.name_without_prefix, m.name, i.name, m.name))
+        self.out.write('<programlisting>\n')
+        self.print_method_prototype(i, m, in_synopsis=False)
+        self.out.write('</programlisting>\n')
+        self.out.write('<para>%s</para>\n'%(self.expand(m.doc_string)))
+        self.out.write('<variablelist role="params">\n')
+        for a in m.in_args:
+            self.out.write('<varlistentry>\n'%())
+            self.out.write('  <term><literal>IN %s <parameter>%s</parameter></literal>:</term>\n'%(a.signature, a.name))
+            self.out.write('  <listitem><para>%s</para></listitem>\n'%(self.expand(a.doc_string)))
+            self.out.write('</varlistentry>\n'%())
+        for a in m.out_args:
+            self.out.write('<varlistentry>\n'%())
+            self.out.write('  <term><literal>OUT %s <parameter>%s</parameter></literal>:</term>\n'%(a.signature, a.name))
+            self.out.write('  <listitem><para>%s</para></listitem>\n'%(self.expand(a.doc_string)))
+            self.out.write('</varlistentry>\n'%())
+        self.out.write('</variablelist>\n')
+        self.out.write('</refsect2>\n')
+
+    def print_signal(self, i, s):
+        self.out.write('<refsect2 role="signal" id="gdbus-signal-%s.%s">\n'%(utils.dots_to_hyphens(i.name), s.name))
+        self.out.write('  <title>The "%s" signal</title>\n'%(s.name))
+        self.out.write('  <indexterm zone="gdbus-signal-%s.%s"><primary sortas="%s::%s">%s::%s</primary></indexterm>\n'%(utils.dots_to_hyphens(i.name), s.name, i.name_without_prefix, s.name, i.name, s.name))
+        self.out.write('<programlisting>\n')
+        self.print_signal_prototype(i, s, in_synopsis=False)
+        self.out.write('</programlisting>\n')
+        self.out.write('<para>%s</para>\n'%(self.expand(s.doc_string)))
+        self.out.write('<variablelist role="params">\n')
+        for a in s.args:
+            self.out.write('<varlistentry>\n'%())
+            self.out.write('  <term><literal>%s <parameter>%s</parameter></literal>:</term>\n'%(a.signature, a.name))
+            self.out.write('  <listitem><para>%s</para></listitem>\n'%(self.expand(a.doc_string)))
+            self.out.write('</varlistentry>\n'%())
+        self.out.write('</variablelist>\n')
+        self.out.write('</refsect2>\n')
+
+    def print_property(self, i, p):
+        self.out.write('<refsect2 role="property" id="gdbus-property-%s.%s">\n'%(utils.dots_to_hyphens(i.name), p.name))
+        self.out.write('  <title>The "%s" property</title>\n'%(p.name))
+        self.out.write('  <indexterm zone="gdbus-property-%s.%s"><primary sortas="%s:%s">%s:%s</primary></indexterm>\n'%(utils.dots_to_hyphens(i.name), p.name, i.name_without_prefix, p.name, i.name, p.name))
+        self.out.write('<programlisting>\n')
+        self.print_property_prototype(i, p, in_synopsis=False)
+        self.out.write('</programlisting>\n')
+        self.out.write('<para>%s</para>\n'%(self.expand(p.doc_string)))
+        self.out.write('</refsect2>\n')
+
+    def expand(self, s):
+        for key in self.expand_member_dict_keys:
+            s = s.replace(key, self.expand_member_dict[key])
+        for key in self.expand_iface_dict_keys:
+            s = s.replace(key, self.expand_iface_dict[key])
+        return s
+
+    def generate_expand_dicts(self):
+        self.expand_member_dict = {}
+        self.expand_iface_dict = {}
+        for i in self.ifaces:
+            key = '#%s'%(i.name)
+            value = '<link linkend="gdbus-interface-%s.top_of_page">%s</link>'%(utils.dots_to_hyphens(i.name), i.name)
+            self.expand_iface_dict[key] = value
+            for m in i.methods:
+                key = '%s.%s()'%(i.name, m.name)
+                value = '<link linkend="gdbus-method-%s.%s">%s()</link>'%(utils.dots_to_hyphens(i.name), m.name, m.name)
+                self.expand_member_dict[key] = value
+            for s in i.signals:
+                key = '#%s::%s'%(i.name, s.name)
+                value = '<link linkend="gdbus-signal-%s.%s">"%s"</link>'%(utils.dots_to_hyphens(i.name), s.name, s.name)
+                self.expand_member_dict[key] = value
+            for p in i.properties:
+                key = '#%s:%s'%(i.name, p.name)
+                value = '<link linkend="gdbus-property-%s.%s">"%s"</link>'%(utils.dots_to_hyphens(i.name), p.name, p.name)
+                self.expand_member_dict[key] = value
+        # Make sure to expand the keys in reverse order so e.g. #org.foo.Iface:MediaCompat
+        # is evaluated before #org.foo.Iface:Media ...
+        self.expand_member_dict_keys = self.expand_member_dict.keys()
+        self.expand_member_dict_keys.sort(reverse=True)
+        self.expand_iface_dict_keys = self.expand_iface_dict.keys()
+        self.expand_iface_dict_keys.sort(reverse=True)
+
+    def generate(self):
+        self.generate_expand_dicts()
+        for i in self.ifaces:
+            self.out = file('%s-%s.xml'%(self.docbook, i.name), 'w')
+            self.out.write(''%())
+            self.out.write('<?xml version="1.0" encoding="utf-8"?>\n'%())
+            self.out.write('<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"\n'%())
+            self.out.write('               "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" [\n'%())
+            self.out.write(']>\n'%())
+            self.out.write('<refentry id="gdbus-%s">\n'%(i.name))
+            self.out.write('  <refmeta>'%())
+            self.out.write('    <refentrytitle role="top_of_page" id="gdbus-interface-%s.top_of_page">%s</refentrytitle>\n'%(utils.dots_to_hyphens(i.name), i.name))
+            self.out.write('  <indexterm zone="gdbus-interface-%s.top_of_page"><primary sortas="%s">%s</primary></indexterm>\n'%(utils.dots_to_hyphens(i.name), i.name_without_prefix, i.name))
+            self.out.write('  </refmeta>'%())
+
+            self.out.write('  <refnamediv>'%())
+            self.out.write('    <refname>%s</refname>'%(i.name))
+            self.out.write('    <refpurpose>%s</refpurpose>'%(i.doc_string_brief))
+            self.out.write('  </refnamediv>'%())
+
+            if len(i.methods) > 0:
+                self.print_synopsis_methods(i)
+            if len(i.signals) > 0:
+                self.print_synopsis_signals(i)
+            if len(i.properties) > 0:
+                self.print_synopsis_properties(i)
+
+            self.out.write('<refsect1 role="desc" id="gdbus-interface-%s">\n'%(utils.dots_to_hyphens(i.name)))
+            self.out.write('  <title role="desc.title">Description</title>\n'%())
+            self.out.write('  <para>%s</para>\n'%(self.expand(i.doc_string)))
+            self.out.write('</refsect1>\n'%())
+
+            if len(i.methods) > 0:
+                self.out.write('<refsect1 role="details" id="gdbus-methods-%s">\n'%(i.name))
+                self.out.write('  <title role="details.title">Method Details</title>\n'%())
+                for m in i.methods:
+                    self.print_method(i, m)
+                self.out.write('</refsect1>\n'%())
+
+            if len(i.signals) > 0:
+                self.out.write('<refsect1 role="details" id="gdbus-signals-%s">\n'%(i.name))
+                self.out.write('  <title role="details.title">Signal Details</title>\n'%())
+                for s in i.signals:
+                    self.print_signal(i, s)
+                self.out.write('</refsect1>\n'%())
+
+            if len(i.properties) > 0:
+                self.out.write('<refsect1 role="details" id="gdbus-properties-%s">\n'%(i.name))
+                self.out.write('  <title role="details.title">Property Details</title>\n'%())
+                for s in i.properties:
+                    self.print_property(i, s)
+                self.out.write('</refsect1>\n'%())
+
+            self.out.write('</refentry>\n')
+            self.out.write('\n')
+
diff --git a/gio/gdbus-codegen/codegen_main.py b/gio/gdbus-codegen/codegen_main.py
new file mode 100644 (file)
index 0000000..9ab2d1f
--- /dev/null
@@ -0,0 +1,171 @@
+# -*- Mode: Python -*-
+
+import sys
+import argparse
+
+import config
+import utils
+import dbustypes
+import parser
+import codegen
+import codegen_docbook
+
+def find_arg(arg_list, arg_name):
+    for a in arg_list:
+        if a.name == arg_name:
+            return a
+    return None
+
+def find_method(iface, method):
+    for m in iface.methods:
+        if m.name == method:
+            return m
+    return None
+
+def find_signal(iface, signal):
+    for m in iface.signals:
+        if m.name == signal:
+            return m
+    return None
+
+def find_prop(iface, prop):
+    for m in iface.properties:
+        if m.name == prop:
+            return m
+    return None
+
+def apply_annotation(iface_list, iface, method, signal, prop, arg, key, value):
+    for i in iface_list:
+        if i.name == iface:
+            iface_obj = i
+            break
+
+    if iface_obj == None:
+        raise RuntimeError('No interface %s'%iface)
+
+    target_obj = None
+
+    if method:
+        method_obj = find_method(iface_obj, method)
+        if method_obj == None:
+            raise RuntimeError('No method %s on interface %s'%(method, iface))
+        if arg:
+            arg_obj = find_arg(method_obj.in_args, arg)
+            if (arg_obj == None):
+                arg_obj = find_arg(method_obj.out_args, arg)
+                if (arg_obj == None):
+                    raise RuntimeError('No arg %s on method %s on interface %s'%(arg, method, iface))
+            target_obj = arg_obj
+        else:
+            target_obj = method_obj
+    elif signal:
+        signal_obj = find_signal(iface_obj, signal)
+        if signal_obj == None:
+            raise RuntimeError('No signal %s on interface %s'%(signal, iface))
+        if arg:
+            arg_obj = find_arg(signal_obj.args, arg)
+            if (arg_obj == None):
+                raise RuntimeError('No arg %s on signal %s on interface %s'%(arg, signal, iface))
+            target_obj = arg_obj
+        else:
+            target_obj = signal_obj
+    elif prop:
+        prop_obj = find_prop(iface_obj, prop)
+        if prop_obj == None:
+            raise RuntimeError('No property %s on interface %s'%(prop, iface))
+        target_obj = prop_obj
+    else:
+        target_obj = iface_obj
+    target_obj.annotations.insert(0, dbustypes.Annotation(key, value))
+
+
+def apply_annotations(iface_list, annotation_list):
+    # apply annotations given on the command line
+    for (what, key, value) in annotation_list:
+        pos = what.find('::')
+        if pos != -1:
+            # signal
+            iface = what[0:pos];
+            signal = what[pos + 2:]
+            pos = signal.find('[')
+            if pos != -1:
+                arg = signal[pos + 1:]
+                signal = signal[0:pos]
+                pos = arg.find(']')
+                arg = arg[0:pos]
+                apply_annotation(iface_list, iface, None, signal, None, arg, key, value)
+            else:
+                apply_annotation(iface_list, iface, None, signal, None, None, key, value)
+        else:
+            pos = what.find(':')
+            if pos != -1:
+                # property
+                iface = what[0:pos];
+                prop = what[pos + 1:]
+                apply_annotation(iface_list, iface, None, None, prop, None, key, value)
+            else:
+                pos = what.find('()')
+                if pos != -1:
+                    # method
+                    combined = what[0:pos]
+                    pos = combined.rfind('.')
+                    iface = combined[0:pos]
+                    method = combined[pos + 1:]
+                    pos = what.find('[')
+                    if pos != -1:
+                        arg = what[pos + 1:]
+                        pos = arg.find(']')
+                        arg = arg[0:pos]
+                        apply_annotation(iface_list, iface, method, None, None, arg, key, value)
+                    else:
+                        apply_annotation(iface_list, iface, method, None, None, None, key, value)
+                else:
+                    # must be an interface
+                    iface = what
+                    apply_annotation(iface_list, iface, None, None, None, None, key, value)
+
+def codegen_main():
+    arg_parser = argparse.ArgumentParser(description='GDBus Code Generator')
+    arg_parser.add_argument('xml_files', metavar='FILE', type=file, nargs='+',
+                            help='D-Bus introspection XML file')
+    arg_parser.add_argument('--interface-prefix', nargs='?', metavar='PREFIX', default='',
+                            help='String to strip from D-Bus interface names for code and docs')
+    arg_parser.add_argument('--c-namespace', nargs='?', metavar='NAMESPACE', default='',
+                            help='The namespace to use for generated C code')
+    arg_parser.add_argument('--generate-c-code', nargs='?', metavar='OUTFILES',
+                            help='Generate C code in OUTFILES.[ch]')
+    arg_parser.add_argument('--generate-docbook', nargs='?', metavar='OUTFILES',
+                            help='Generate Docbook in OUTFILES-org.Project.IFace.xml')
+    arg_parser.add_argument('--annotate', nargs=3, action='append', metavar=('WHAT', 'KEY', 'VALUE'),
+                            help='Add annotation (may be used several times)')
+    args = arg_parser.parse_args();
+
+    all_ifaces = []
+    for f in args.xml_files:
+        xml_data = f.read()
+        f.close()
+        parsed_ifaces = parser.parse_dbus_xml(xml_data)
+        all_ifaces.extend(parsed_ifaces)
+
+    if args.annotate != None:
+        apply_annotations(all_ifaces, args.annotate)
+
+    for i in parsed_ifaces:
+        i.post_process(args.interface_prefix, args.c_namespace)
+
+    c_code = args.generate_c_code
+    if c_code:
+        h = file(c_code + '.h', 'w')
+        c = file(c_code + '.c', 'w')
+        gen = codegen.CodeGenerator(all_ifaces, args.c_namespace, args.interface_prefix, h, c);
+        ret = gen.generate()
+
+    docbook = args.generate_docbook
+    if docbook:
+        gen = codegen_docbook.DocbookCodeGenerator(all_ifaces, docbook);
+        ret = gen.generate()
+
+    sys.exit(0)
+
+if __name__ == "__main__":
+    codegen_main()
diff --git a/gio/gdbus-codegen/config.py.in b/gio/gdbus-codegen/config.py.in
new file mode 100644 (file)
index 0000000..ad42a2f
--- /dev/null
@@ -0,0 +1,6 @@
+# -*- Mode: Python -*-
+
+DATADIR = "@datarootdir@"
+DATADIR = DATADIR.replace(
+    "${prefix}", "@prefix@")
+VERSION = "@VERSION@"
diff --git a/gio/gdbus-codegen/dbustypes.py b/gio/gdbus-codegen/dbustypes.py
new file mode 100644 (file)
index 0000000..a0cecbb
--- /dev/null
@@ -0,0 +1,288 @@
+# -*- Mode: Python -*-
+
+import utils
+
+class Annotation:
+    def __init__(self, key, value):
+        self.key = key
+        self.value = value
+        self.annotations = []
+
+class Arg:
+    def __init__(self, name, signature):
+        self.name = name
+        self.signature = signature
+        self.annotations = []
+        self.doc_string = ''
+
+    def post_process(self, interface_prefix, c_namespace, arg_number):
+        if len(self.doc_string) == 0:
+            self.doc_string = utils.lookup_docs(self.annotations)
+
+        if self.name == None:
+            self.name = 'unnamed_arg%d'%arg_number
+        # default to GVariant
+        self.ctype_in_g  = 'GVariant *'
+        self.ctype_in  = 'GVariant *'
+        self.ctype_out = 'GVariant **'
+        self.gtype = 'G_TYPE_VARIANT'
+        self.free_func = 'g_variant_unref'
+        self.format_in = '@' + self.signature
+        self.format_out = '@' + self.signature
+        if not utils.lookup_annotation(self.annotations, 'org.gtk.GDBus.C.ForceGVariant'):
+            if self.signature == 'b':
+                self.ctype_in_g  = 'gboolean '
+                self.ctype_in  = 'gboolean '
+                self.ctype_out = 'gboolean *'
+                self.gtype = 'G_TYPE_BOOLEAN'
+                self.free_func = None
+                self.format_in = 'b'
+                self.format_out = 'b'
+            elif self.signature == 'y':
+                self.ctype_in_g  = 'guchar '
+                self.ctype_in  = 'guchar '
+                self.ctype_out = 'guchar *'
+                self.gtype = 'G_TYPE_UCHAR'
+                self.free_func = None
+                self.format_in = 'y'
+                self.format_out = 'y'
+            elif self.signature == 'n':
+                self.ctype_in_g  = 'gint '
+                self.ctype_in  = 'gint16 '
+                self.ctype_out = 'gint16 *'
+                self.gtype = 'G_TYPE_INT'
+                self.free_func = None
+                self.format_in = 'n'
+                self.format_out = 'n'
+            elif self.signature == 'q':
+                self.ctype_in_g  = 'guint '
+                self.ctype_in  = 'guint16 '
+                self.ctype_out = 'guint16 *'
+                self.gtype = 'G_TYPE_UINT'
+                self.free_func = None
+                self.format_in = 'q'
+                self.format_out = 'q'
+            elif self.signature == 'i':
+                self.ctype_in_g  = 'gint '
+                self.ctype_in  = 'gint '
+                self.ctype_out = 'gint *'
+                self.gtype = 'G_TYPE_INT'
+                self.free_func = None
+                self.format_in = 'i'
+                self.format_out = 'i'
+            elif self.signature == 'u':
+                self.ctype_in_g  = 'guint '
+                self.ctype_in  = 'guint '
+                self.ctype_out = 'guint *'
+                self.gtype = 'G_TYPE_UINT'
+                self.free_func = None
+                self.format_in = 'u'
+                self.format_out = 'u'
+            elif self.signature == 'x':
+                self.ctype_in_g  = 'gint64 '
+                self.ctype_in  = 'gint64 '
+                self.ctype_out = 'gint64 *'
+                self.gtype = 'G_TYPE_INT64'
+                self.free_func = None
+                self.format_in = 'x'
+                self.format_out = 'x'
+            elif self.signature == 't':
+                self.ctype_in_g  = 'guint64 '
+                self.ctype_in  = 'guint64 '
+                self.ctype_out = 'guint64 *'
+                self.gtype = 'G_TYPE_UINT64'
+                self.free_func = None
+                self.format_in = 't'
+                self.format_out = 't'
+            elif self.signature == 'd':
+                self.ctype_in_g  = 'gdouble '
+                self.ctype_in  = 'gdouble '
+                self.ctype_out = 'gdouble *'
+                self.gtype = 'G_TYPE_DOUBLE'
+                self.free_func = None
+                self.format_in = 'd'
+                self.format_out = 'd'
+            elif self.signature == 's':
+                self.ctype_in_g  = 'const gchar *'
+                self.ctype_in  = 'const gchar *'
+                self.ctype_out = 'gchar **'
+                self.gtype = 'G_TYPE_STRING'
+                self.free_func = 'g_free'
+                self.format_in = 's'
+                self.format_out = 's'
+            elif self.signature == 'o':
+                self.ctype_in_g  = 'const gchar *'
+                self.ctype_in  = 'const gchar *'
+                self.ctype_out = 'gchar **'
+                self.gtype = 'G_TYPE_STRING'
+                self.free_func = 'g_free'
+                self.format_in = 'o'
+                self.format_out = 'o'
+            elif self.signature == 'g':
+                self.ctype_in_g  = 'const gchar *'
+                self.ctype_in  = 'const gchar *'
+                self.ctype_out = 'gchar **'
+                self.gtype = 'G_TYPE_STRING'
+                self.free_func = 'g_free'
+                self.format_in = 'g'
+                self.format_out = 'g'
+            elif self.signature == 'ay':
+                self.ctype_in_g  = 'const gchar *'
+                self.ctype_in  = 'const gchar *'
+                self.ctype_out = 'gchar **'
+                self.gtype = 'G_TYPE_STRING'
+                self.free_func = 'g_free'
+                self.format_in = '^ay'
+                self.format_out = '^ay'
+            elif self.signature == 'as':
+                self.ctype_in_g  = 'const gchar *const *'
+                self.ctype_in  = 'const gchar *const *'
+                self.ctype_out = 'gchar ***'
+                self.gtype = 'G_TYPE_STRV'
+                self.free_func = 'g_strfreev'
+                self.format_in = '^as'
+                self.format_out = '^as'
+            elif self.signature == 'aay':
+                self.ctype_in_g  = 'const gchar *const *'
+                self.ctype_in  = 'const gchar *const *'
+                self.ctype_out = 'gchar ***'
+                self.gtype = 'G_TYPE_STRV'
+                self.free_func = 'g_strfreev'
+                self.format_in = '^aay'
+                self.format_out = '^aay'
+
+class Method:
+    def __init__(self, name):
+        self.name = name
+        self.in_args = []
+        self.out_args = []
+        self.annotations = []
+        self.doc_string = ''
+
+    def post_process(self, interface_prefix, c_namespace):
+        if len(self.doc_string) == 0:
+            self.doc_string = utils.lookup_docs(self.annotations)
+
+        name = self.name
+        overridden_name = utils.lookup_annotation(self.annotations, 'org.gtk.GDBus.Name')
+        if overridden_name:
+            name = overridden_name
+
+        self.name_lower = utils.camel_case_to_uscore(name).lower().replace('-', '_')
+        self.name_hyphen = self.name_lower.replace('_', '-')
+
+        arg_count = 0
+        for a in self.in_args:
+            a.post_process(interface_prefix, c_namespace, arg_count)
+            arg_count += 1
+
+        for a in self.out_args:
+            a.post_process(interface_prefix, c_namespace, arg_count)
+            arg_count += 1
+
+class Signal:
+    def __init__(self, name):
+        self.name = name
+        self.args = []
+        self.annotations = []
+        self.doc_string = ''
+
+    def post_process(self, interface_prefix, c_namespace):
+        if len(self.doc_string) == 0:
+            self.doc_string = utils.lookup_docs(self.annotations)
+
+        name = self.name
+        overridden_name = utils.lookup_annotation(self.annotations, 'org.gtk.GDBus.Name')
+        if overridden_name:
+            name = overridden_name
+
+        self.name_lower = utils.camel_case_to_uscore(name).lower().replace('-', '_')
+        self.name_hyphen = self.name_lower.replace('_', '-')
+
+        arg_count = 0
+        for a in self.args:
+            a.post_process(interface_prefix, c_namespace, arg_count)
+            arg_count += 1
+
+class Property:
+    def __init__(self, name, signature, access):
+        self.name = name
+        self.signature = signature
+        self.access = access
+        self.annotations = []
+        self.arg = Arg('value', self.signature)
+        self.arg.annotations = self.annotations
+        self.readable = False
+        self.writable = False
+        if self.access == 'readwrite':
+            self.readable = True
+            self.writable = True
+        elif self.access == 'read':
+            self.readable = True
+        elif self.access == 'write':
+            self.writable = True
+        else:
+            raise RuntimeError('Invalid access type %s'%self.access)
+        self.doc_string = ''
+
+    def post_process(self, interface_prefix, c_namespace):
+        if len(self.doc_string) == 0:
+            self.doc_string = utils.lookup_docs(self.annotations)
+
+        name = self.name
+        overridden_name = utils.lookup_annotation(self.annotations, 'org.gtk.GDBus.Name')
+        if overridden_name:
+            name = overridden_name
+
+        self.name_lower = utils.camel_case_to_uscore(name).lower().replace('-', '_')
+        self.name_hyphen = self.name_lower.replace('_', '-')
+
+        # recalculate arg
+        self.arg.annotations = self.annotations
+        self.arg.post_process(interface_prefix, c_namespace, 0)
+
+class Interface:
+    def __init__(self, name):
+        self.name = name
+        self.methods = []
+        self.signals = []
+        self.properties = []
+        self.annotations = []
+        self.doc_string = ''
+        self.doc_string_brief = ''
+
+    def post_process(self, interface_prefix, c_namespace):
+        if len(self.doc_string) == 0:
+            self.doc_string = utils.lookup_docs(self.annotations)
+        if len(self.doc_string_brief) == 0:
+            self.doc_string_brief = utils.lookup_brief_docs(self.annotations)
+
+        overridden_name = utils.lookup_annotation(self.annotations, 'org.gtk.GDBus.Name')
+        if overridden_name:
+            name = overridden_name
+        else:
+            name = self.name
+            if name.startswith(interface_prefix):
+                name = name[len(interface_prefix):]
+        self.name_without_prefix = name
+        name = utils.strip_dots(name)
+        name_with_ns = utils.strip_dots(c_namespace + '.' + name)
+
+
+        self.camel_name = name_with_ns
+        if len(c_namespace) > 0:
+            self.ns_upper = utils.camel_case_to_uscore(c_namespace).upper() + '_'
+            self.name_lower = utils.camel_case_to_uscore(c_namespace) + '_' + utils.camel_case_to_uscore(name)
+        else:
+            self.ns_upper = ''
+            self.name_lower = utils.camel_case_to_uscore(name_with_ns)
+        self.name_upper = utils.camel_case_to_uscore(name).upper()
+
+        for m in self.methods:
+            m.post_process(interface_prefix, c_namespace)
+
+        for s in self.signals:
+            s.post_process(interface_prefix, c_namespace)
+
+        for p in self.properties:
+            p.post_process(interface_prefix, c_namespace)
diff --git a/gio/gdbus-codegen/gdbus-codegen.in b/gio/gdbus-codegen/gdbus-codegen.in
new file mode 100755 (executable)
index 0000000..12136d7
--- /dev/null
@@ -0,0 +1,11 @@
+#!/usr/bin/python
+
+import os
+import sys
+
+path = os.path.join('@libdir@', 'gdbus-codegen')
+sys.path.insert(0, path)
+
+from codegen_main import codegen_main
+
+sys.exit(codegen_main())
diff --git a/gio/gdbus-codegen/parser.py b/gio/gdbus-codegen/parser.py
new file mode 100644 (file)
index 0000000..e5d93bc
--- /dev/null
@@ -0,0 +1,255 @@
+# -*- Mode: Python -*-
+
+import dbustypes
+
+import sys
+import xml.parsers.expat
+
+class DBusXMLParser:
+    STATE_TOP = 'top'
+    STATE_NODE = 'node'
+    STATE_INTERFACE = 'interface'
+    STATE_METHOD = 'method'
+    STATE_SIGNAL = 'signal'
+    STATE_PROPERTY = 'property'
+    STATE_ARG = 'arg'
+    STATE_ANNOTATION = 'annotation'
+
+    def __init__(self, xml_data):
+        self._parser = xml.parsers.expat.ParserCreate()
+        self._parser.CommentHandler = self.handle_comment
+        self._parser.CharacterDataHandler = self.handle_char_data
+        self._parser.StartElementHandler = self.handle_start_element
+        self._parser.EndElementHandler = self.handle_end_element
+
+        self.parsed_interfaces = []
+        self._cur_object = None
+
+        self.state = DBusXMLParser.STATE_TOP
+        self.state_stack = []
+        self._cur_object = None
+        self._cur_object_stack = []
+
+        self.doc_comment_last_symbol = ''
+
+        self._parser.Parse(xml_data)
+
+    COMMENT_STATE_BEGIN = 'begin'
+    COMMENT_STATE_PARAMS = 'params'
+    COMMENT_STATE_BODY = 'body'
+    COMMENT_STATE_SKIP = 'skip'
+    def handle_comment(self, data):
+        comment_state = DBusXMLParser.COMMENT_STATE_BEGIN;
+        lines = data.split('\n')
+        symbol = ''
+        body = ''
+        in_para = False
+        params = {}
+        for line in lines:
+            orig_line = line
+            line = line.lstrip()
+            if comment_state == DBusXMLParser.COMMENT_STATE_BEGIN:
+                if len(line) > 0:
+                    colon_index = line.find(': ')
+                    if colon_index == -1:
+                        if line.endswith(':'):
+                            symbol = line[0:len(line)-1]
+                            comment_state = DBusXMLParser.COMMENT_STATE_PARAMS
+                        else:
+                            comment_state = DBusXMLParser.COMMENT_STATE_SKIP
+                    else:
+                        symbol = line[0:colon_index]
+                        rest_of_line = line[colon_index+2:].strip()
+                        if len(rest_of_line) > 0:
+                            body += '<para>' + rest_of_line + '</para>'
+                        comment_state = DBusXMLParser.COMMENT_STATE_PARAMS
+            elif comment_state == DBusXMLParser.COMMENT_STATE_PARAMS:
+                if line.startswith('@'):
+                    colon_index = line.find(': ')
+                    if colon_index == -1:
+                        comment_state = DBusXMLParser.COMMENT_STATE_BODY
+                        if not in_para:
+                            body += '<para>'
+                            in_para = True
+                        body += orig_line + '\n'
+                    else:
+                        param = line[1:colon_index]
+                        docs = line[colon_index + 2:]
+                        params[param] = docs
+                else:
+                    comment_state = DBusXMLParser.COMMENT_STATE_BODY
+                    if len(line) > 0:
+                        if not in_para:
+                            body += '<para>'
+                            in_para = True
+                        body += orig_line + '\n'
+            elif comment_state == DBusXMLParser.COMMENT_STATE_BODY:
+                if len(line) > 0:
+                    if not in_para:
+                        body += '<para>'
+                        in_para = True
+                    body += orig_line + '\n'
+                else:
+                    if in_para:
+                        body += '</para>'
+                        in_para = False
+        if in_para:
+            body += '</para>'
+
+        if symbol != '':
+            self.doc_comment_last_symbol = symbol
+            self.doc_comment_params = params
+            self.doc_comment_body = body
+
+    def handle_char_data(self, data):
+        #print 'char_data=%s'%data
+        pass
+
+    def handle_start_element(self, name, attrs):
+        old_state = self.state
+        old_cur_object = self._cur_object
+        if self.state == DBusXMLParser.STATE_TOP:
+            if name == DBusXMLParser.STATE_NODE:
+                self.state = DBusXMLParser.STATE_NODE
+            else:
+                raise RuntimeError('Cannot go from state "%s" to element with name "%s"'%(self.state, name))
+        elif self.state == DBusXMLParser.STATE_NODE:
+            if name == DBusXMLParser.STATE_INTERFACE:
+                self.state = DBusXMLParser.STATE_INTERFACE
+                iface = dbustypes.Interface(attrs['name'])
+                self._cur_object = iface
+                self.parsed_interfaces.append(iface)
+            elif name == DBusXMLParser.STATE_ANNOTATION:
+                self.state = DBusXMLParser.STATE_ANNOTATION
+                anno = dbustypes.Annotation(attrs['name'], attrs['value'])
+                self._cur_object.annotations.append(anno)
+                self._cur_object = anno
+            else:
+                raise RuntimeError('Cannot go from state "%s" to element with name "%s"'%(self.state, name))
+
+            # assign docs, if any
+            if self.doc_comment_last_symbol == attrs['name']:
+                self._cur_object.doc_string = self.doc_comment_body
+                if self.doc_comment_params.has_key('short_description'):
+                    short_description = self.doc_comment_params['short_description']
+                    self._cur_object.doc_string_brief = short_description
+
+        elif self.state == DBusXMLParser.STATE_INTERFACE:
+            if name == DBusXMLParser.STATE_METHOD:
+                self.state = DBusXMLParser.STATE_METHOD
+                method = dbustypes.Method(attrs['name'])
+                self._cur_object.methods.append(method)
+                self._cur_object = method
+            elif name == DBusXMLParser.STATE_SIGNAL:
+                self.state = DBusXMLParser.STATE_SIGNAL
+                signal = dbustypes.Signal(attrs['name'])
+                self._cur_object.signals.append(signal)
+                self._cur_object = signal
+            elif name == DBusXMLParser.STATE_PROPERTY:
+                self.state = DBusXMLParser.STATE_PROPERTY
+                prop = dbustypes.Property(attrs['name'], attrs['type'], attrs['access'])
+                self._cur_object.properties.append(prop)
+                self._cur_object = prop
+            elif name == DBusXMLParser.STATE_ANNOTATION:
+                self.state = DBusXMLParser.STATE_ANNOTATION
+                anno = dbustypes.Annotation(attrs['name'], attrs['value'])
+                self._cur_object.annotations.append(anno)
+                self._cur_object = anno
+            else:
+                raise RuntimeError('Cannot go from state "%s" to element with name "%s"'%(self.state, name))
+
+            # assign docs, if any
+            if attrs.has_key('name') and self.doc_comment_last_symbol == attrs['name']:
+                self._cur_object.doc_string = self.doc_comment_body
+
+        elif self.state == DBusXMLParser.STATE_METHOD:
+            if name == DBusXMLParser.STATE_ARG:
+                self.state = DBusXMLParser.STATE_ARG
+                arg_name = None
+                if attrs.has_key('name'):
+                    arg_name = attrs['name']
+                arg = dbustypes.Arg(arg_name, attrs['type'])
+                direction = attrs['direction']
+                if direction == 'in':
+                    self._cur_object.in_args.append(arg)
+                elif direction == 'out':
+                    self._cur_object.out_args.append(arg)
+                else:
+                    raise RuntimeError('Invalid direction "%s"'%(direction))
+                self._cur_object = arg
+            elif name == DBusXMLParser.STATE_ANNOTATION:
+                self.state = DBusXMLParser.STATE_ANNOTATION
+                anno = dbustypes.Annotation(attrs['name'], attrs['value'])
+                self._cur_object.annotations.append(anno)
+                self._cur_object = anno
+            else:
+                raise RuntimeError('Cannot go from state "%s" to element with name "%s"'%(self.state, name))
+
+            # assign docs, if any
+            if self.doc_comment_last_symbol == old_cur_object.name:
+                if attrs.has_key('name') and self.doc_comment_params.has_key(attrs['name']):
+                    doc_string = self.doc_comment_params[attrs['name']]
+                    if doc_string != None:
+                        self._cur_object.doc_string = doc_string
+
+        elif self.state == DBusXMLParser.STATE_SIGNAL:
+            if name == DBusXMLParser.STATE_ARG:
+                self.state = DBusXMLParser.STATE_ARG
+                arg_name = None
+                if attrs.has_key('name'):
+                    arg_name = attrs['name']
+                arg = dbustypes.Arg(arg_name, attrs['type'])
+                self._cur_object.args.append(arg)
+                self._cur_object = arg
+            elif name == DBusXMLParser.STATE_ANNOTATION:
+                self.state = DBusXMLParser.STATE_ANNOTATION
+                anno = dbustypes.Annotation(attrs['name'], attrs['value'])
+                self._cur_object.annotations.append(anno)
+                self._cur_object = anno
+            else:
+                raise RuntimeError('Cannot go from state "%s" to element with name "%s"'%(self.state, name))
+
+            # assign docs, if any
+            if self.doc_comment_last_symbol == old_cur_object.name:
+                if attrs.has_key('name') and self.doc_comment_params.has_key(attrs['name']):
+                    doc_string = self.doc_comment_params[attrs['name']]
+                    if doc_string != None:
+                        self._cur_object.doc_string = doc_string
+
+        elif self.state == DBusXMLParser.STATE_PROPERTY:
+            if name == DBusXMLParser.STATE_ANNOTATION:
+                self.state = DBusXMLParser.STATE_ANNOTATION
+                anno = dbustypes.Annotation(attrs['name'], attrs['value'])
+                self._cur_object.annotations.append(anno)
+                self._cur_object = anno
+            else:
+                raise RuntimeError('Cannot go from state "%s" to element with name "%s"'%(self.state, name))
+        elif self.state == DBusXMLParser.STATE_ARG:
+            if name == DBusXMLParser.STATE_ANNOTATION:
+                self.state = DBusXMLParser.STATE_ANNOTATION
+                anno = dbustypes.Annotation(attrs['name'], attrs['value'])
+                self._cur_object.annotations.append(anno)
+                self._cur_object = anno
+            else:
+                raise RuntimeError('Cannot go from state "%s" to element with name "%s"'%(self.state, name))
+        elif self.state == DBusXMLParser.STATE_ANNOTATION:
+            if name == DBusXMLParser.STATE_ANNOTATION:
+                self.state = DBusXMLParser.STATE_ANNOTATION
+                anno = dbustypes.Annotation(attrs['name'], attrs['value'])
+                self._cur_object.annotations.append(anno)
+                self._cur_object = anno
+            else:
+                raise RuntimeError('Cannot go from state "%s" to element with name "%s"'%(self.state, name))
+        else:
+            raise RuntimeError('Unhandled state "%s" while entering element with name "%s"'%(self.state, name))
+
+        self.state_stack.append(old_state)
+        self._cur_object_stack.append(old_cur_object)
+
+    def handle_end_element(self, name):
+        self.state = self.state_stack.pop()
+        self._cur_object = self._cur_object_stack.pop()
+
+def parse_dbus_xml(xml_data):
+    parser = DBusXMLParser(xml_data)
+    return parser.parsed_interfaces
diff --git a/gio/gdbus-codegen/utils.py b/gio/gdbus-codegen/utils.py
new file mode 100644 (file)
index 0000000..4966077
--- /dev/null
@@ -0,0 +1,56 @@
+# -*- Mode: Python -*-
+
+def strip_dots(s):
+    ret = ''
+    force_upper = False
+    for c in s:
+        if c == '.':
+            force_upper = True
+        else:
+            if force_upper:
+                ret += c.upper()
+                force_upper = False
+            else:
+                ret += c
+    return ret
+
+def dots_to_hyphens(s):
+    return s.replace('.', '-')
+
+def camel_case_to_uscore(s):
+    ret = ''
+    insert_uscore = False
+    prev_was_lower = False
+    for c in s:
+        if c.isupper():
+            if prev_was_lower:
+                insert_uscore = True
+            prev_was_lower = False
+        else:
+            prev_was_lower = True
+        if insert_uscore:
+            ret += '_'
+        ret += c.lower()
+        insert_uscore = False
+    return ret
+
+def lookup_annotation(annotations, key):
+    if annotations:
+        for a in annotations:
+            if a.key == key:
+                return a.value
+    return None
+
+def lookup_docs(annotations):
+    s = lookup_annotation(annotations, 'org.gtk.GDBus.DocString')
+    if s == None:
+        return ''
+    else:
+        return s
+
+def lookup_brief_docs(annotations):
+    s = lookup_annotation(annotations, 'org.gtk.GDBus.DocString.Short')
+    if s == None:
+        return ''
+    else:
+        return s