GDBUS-CODEGEN(1) | User Commands | GDBUS-CODEGEN(1) |
NAME
gdbus-codegen - D-Bus code and documentation generatorSYNOPSIS
gdbus-codegen [-h, --help] [--interface-prefix org.project.Prefix] [--generate-c-code OUTFILES] [--c-namespace YourProject] [--c-generate-object-manager] [--generate-docbook OUTFILES] [--xml-files FILE] [--annotate ELEMENT KEY VALUE]... FILE [FILE...]
DESCRIPTION
gdbus-codegen is used to generate code and/or documentation for one or more D-Bus interfaces. The tool reads D-Bus Introspection XML[1] files and generates output files. The tool currently supports generating C code (via --generate-c-code) and Docbook XML (via --generate-docbook).GENERATING C CODE
When generating C code, a #GInterface -derived type is generated for each D-Bus interface. Additionally, for every generated type, FooBar, two concrete instantiable types, FooBarProxy and FooBarSkeleton, implementing said interface are also generated. The former is derived from #GDBusProxy and intended for use on the client side while the latter is derived from the #GDBusInterfaceSkeleton type making it easy to export on a #GDBusConnection either directly or via a #GDBusObjectManagerServer instance.GENERATING DOCBOOK DOCUMENTATION
Each generated Docbook XML file (see the --generate-docbook option for details) is a RefEntry[2] article describing the D-Bus interface.OPTIONS
The following options are supported:
Show help and exit.
The D-Bus introspection XML file.
A prefix to strip from all D-Bus interface names when calculating the typename for the C binding and the Docbook sortas attribute[3].
Generate Docbook Documentation for each D-Bus interface and put it in OUTFILES-NAME.xml where NAME is a place-holder for the interface name, e.g. net.Corp.FooBar and so on.
Generate C code for all D-Bus interfaces and put it in OUTFILES.c and OUTFILES.h.
The namespace to use for generated C code. This is expected to be in CamelCase[4] or Ugly_Case (see above).
If this option is passed, suitable #GDBusObject, #GDBusObjectProxy, #GDBusObjectSkeleton and #GDBusObjectManagerClient subclasses are generated.
Used to inject D-Bus annotations into the given XML files. It can be used with interfaces, methods, signals, properties and arguments in the following way:
gdbus-codegen --c-namespace MyApp \
--generate-c-code myapp-generated \
--annotate "org.project.InterfaceName" \
org.gtk.GDBus.C.Name MyFrobnicator \
--annotate "org.project.InterfaceName:Property" \
bar bat \
--annotate "org.project.InterfaceName.Method()" \
org.freedesktop.DBus.Deprecated true \
--annotate "org.project.InterfaceName.Method()[arg_name]" \
snake hiss \
--annotate "org.project.InterfaceName::Signal" \
cat meow \
--annotate "org.project.InterfaceName::Signal[arg_name]" \
dog wuff \
myapp-dbus-interfaces.xml
Any UTF-8 string can be used for KEY and VALUE.
gdbus-codegen --c-namespace MyApp \
--generate-c-code myapp-generated \
--annotate "org.project.InterfaceName" \
org.gtk.GDBus.C.Name MyFrobnicator \
--annotate "org.project.InterfaceName:Property" \
bar bat \
--annotate "org.project.InterfaceName.Method()" \
org.freedesktop.DBus.Deprecated true \
--annotate "org.project.InterfaceName.Method()[arg_name]" \
snake hiss \
--annotate "org.project.InterfaceName::Signal" \
cat meow \
--annotate "org.project.InterfaceName::Signal[arg_name]" \
dog wuff \
myapp-dbus-interfaces.xml
SUPPORTED D-BUS ANNOTATIONS
The following D-Bus annotations are supported by gdbus-codegen:
Can be used on any <interface>, <method>, <signal> and <property> element to specify that the element is deprecated if its value is true. Note that this annotation is defined in the D-Bus specification[1] and can only assume the values true and false. In particular, you cannot specify the version that the element was deprecated in nor any helpful deprecation message. Such information should be added to the element documentation instead.
When generating C code, this annotation is used to add #G_GNUC_DEPRECATED to generated functions for the element.
When generating Docbook XML, a deprecation warning will appear along the documentation for the element.
Can be used on any <interface>, <method>, <signal> and <property> element to specify the version (any free-form string but compared using a version-aware sort function) the element appeared in.
When generating C code, this field is used to ensure function pointer order for preserving ABI/API, see the section called “STABILITY GUARANTEES”.
When generating Docbook XML, the value of this tag appears in the documentation.
A string with Docbook content for documentation. This annotation can be used on <interface>, <method>, <signal>, <property> and <arg> elements.
A string with Docbook content for short/brief documentation. This annotation can only be used on <interface> elements.
Can be used on any <interface>, <method>, <signal> and <property> element to specify the name to use when generating C code. The value is expected to be in CamelCase[4] or Ugly_Case (see above).
If set to a non-empty string, a #GVariant instance will be used instead of the natural C type. This annotation can be used on any <arg> and <property> element.
If set to a non-empty string, the generated code will include parameters to exchange file descriptors using the #GUnixFDList type. This annotation can be used on <method> elements.
EXAMPLE
Consider the following D-Bus Introspection XML.<node>
<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>
</node>
gdbus-codegen --generate-c-code myapp-generated \
--c-namespace MyApp \
--interface-prefix net.corp.MyApp. \
net.Corp.MyApp.Frobber.xml
/* GType macros for the three generated types */
#define MY_APP_TYPE_FROBBER (my_app_frobber_get_type ())
#define MY_APP_TYPE_FROBBER_SKELETON (my_app_frobber_skeleton_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 skeleton object, ready to be exported */
MyAppFrobber *my_app_frobber_skeleton_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);
Client | Server | |
Types | Use MyAppFrobberProxy | Any type implementing the MyAppFrobber interface |
Methods | Use m_a_f_hello_world() to call. | Receive via the handle_hello_world() signal handler. Complete the call with m_a_f_complete_hello_world() |
Signals | Connect to the ::notification GObject signal. | Use m_a_f_emit_notification() to emit signal. |
Properties (Reading) | Use m_a_f_get_verbose() or :verbose. | Implement #GObject's get_property() vfunc. |
Properties (writing) | Use m_a_f_set_verbose() or :verbose. | Implement #GObject's set_property() vfunc. |
Client-side usage
You can use the generated proxy type with the generated constructors: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);
Server-side usage
The generated MyAppFrobber interface is designed so it is easy to implement it in a #GObject subclass. For example, to handle HelloWorld() method invocations, set the vfunc for handle_hello_hello_world() in the MyAppFrobberIface structure. Similary, to handle the net.Corp.MyApp.Frobber:Verbose property override the :verbose #GObject property from the subclass. To emit a signal, use e.g. my_app_emit_signal() or g_signal_emit_by_name().static gboolean
on_handle_hello_world (MyAppFrobber *interface,
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 (interface, invocation, response);
g_free (response);
}
else
{
g_dbus_method_invocation_return_error (invocation,
MY_APP_ERROR,
MY_APP_ERROR_NO_WHINING,
"Hey, %s, there will be no whining!",
g_dbus_method_invocation_get_sender (invocation));
}
return TRUE;
}
[...]
interface = my_app_frobber_skeleton_new ();
my_app_frobber_set_verbose (interface, TRUE);
g_signal_connect (interface,
"handle-hello-world",
G_CALLBACK (on_handle_hello_world),
some_user_data);
[...]
error = NULL;
if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (interface),
connection,
"/path/of/dbus_object",
&error))
{
/* handle error */
}
C TYPE MAPPING
Scalar types (type-strings and and and gchar*, gchar** and so on. Everything else is mapped to the #GVariant type.STABILITY GUARANTEES
The generated C functions are guaranteed to not change their ABI that is, if a method, signal or property does not change its signature in the introspection XML, the generated C functions will not change its C ABI either. The ABI of the generated instance and class structures will be preserved as well.BUGS
Please send bug reports to either the distribution bug tracker or the upstream bug tracker at https://bugzilla.gnome.org/enter_bug.cgi?product=glib.SEE ALSO
gdbus(1)NOTES
- 1.
- D-Bus Introspection XML
http://dbus.freedesktop.org/doc/dbus-specification.html#introspection-format
- 2.
- RefEntry
http://www.docbook.org/tdg/en/html/refentry.html
- 3.
- sortas attribute
http://www.docbook.org/tdg/en/html/primary.html
- 4.
- CamelCase
http://en.wikipedia.org/wiki/CamelCase
- 5.
- gtk-doc
http://www.gtk.org/gtk-doc/
- 6.
- parameter
http://www.docbook.org/tdg/en/html/parameter.html
- 7.
- constant
http://www.docbook.org/tdg/en/html/constant.html
- 8.
- org.freedesktop.DBus.Properties.Set
http://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-properties
- 9.
- GObject Introspection
https://wiki.gnome.org/Projects/GObjectIntrospection
GIO |