gdbus-codegen
gdbus-codegen — D-Bus code and documentation generator
|
|
Synopsis
gdbus-codegen [-h, --help] [--interface-prefix org.project.Prefix] [--generate-c-code OUTFILES] [--c-namespace YourProject] [--c-generate-object-manager] [--c-generate-autocleanup none|objects|all] [--output-directory OUTDIR] [--generate-docbook OUTFILES] [--pragma-once] [--xml-files FILE] [--header] [--body] [--interface-info-header] [--interface-info-body] [--output OUTFILE] [
--annotate
ELEMENT
KEY
VALUE
]... [--glib-min-required VERSION] [--glib-max-allowed VERSION] FILE [
FILE...
]
Description
gdbus-codegen is used to generate code and/or
documentation for one or more D-Bus interfaces.
gdbus-codegen reads
D-Bus
Introspection XML from files passed as additional
arguments on the command line and generates output files.
It currently supports generating C source code (via
--body) or header (via --header)
and Docbook XML (via --generate-docbook). Alternatively,
more restricted C source code and headers can be generated, which just
contain the interface information (as GDBusInterfaceInfo
structures) using --interface-info-body and
--interface-info-header.
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.
For C code generation either --body that
generates source code, --header that
generates headers, --interface-info-body that generates
interface information source code, or
--interface-info-header that generates interface information
headers, can be used. These options must be used along with
--output, which is used to specify the file to output to.
Both files can be generated at the same time by using
--generate-c-code, but this option is deprecated.
In this case --output cannot be used due to the
generation of multiple files. Instead pass
--output-directory to specify the directory to put
the output files in. By default the current directory will be used.
The name of each generated C type is derived from the D-Bus
interface name stripped with the prefix given with
--interface-prefix and with the dots removed and
initial characters capitalized. For example, for the D-Bus
interface com.acme.Coyote the name used is
ComAcmeCoyote. For the D-Bus interface
org.project.Bar.Frobnicator with
--interface-prefix
org.project., the name used is
BarFrobnicator.
For methods, signals and properties, if not specified, the name
defaults to the name of the method, signal or property.
Two forms of the name are used - the CamelCase form and the
lower-case form. The CamelCase form is used for the GType and
struct name, while lower-case form is used in function names. The
lower-case form is calculated by converting from CamelCase to
lower-case and inserting underscores at word boundaries (using
certain heuristics).
If the value given by the org.gtk.GDBus.C.Name
annotation or the --c-namespace option contains
an underscore (sometimes called Ugly_Case),
then the camel-case name is derived by removing all underscores,
and the lower-case name is derived by lower-casing the
string. This is useful in some situations where abbreviations are
used. For example, if the annotation is used on the interface
net.MyCorp.MyApp.iSCSITarget with the value
iSCSI_Target the CamelCase form is
iSCSITarget while the lower-case form is
iscsi_target. If the annotation is used on the
method EjectTheiPod with the value
Eject_The_iPod, the lower-case form is
eject_the_ipod.
Generating Docbook documentation
Each generated Docbook XML file (see the
--generate-docbook option for details) is a RefEntry
article describing the D-Bus interface.
Options
The following options are supported:
-h, --help
|
Show help and exit.
|
--xml-files FILE
|
This option is deprecated; use positional arguments instead.
The D-Bus introspection XML file.
|
--interface-prefix org.project.Prefix.
|
A prefix to strip from all D-Bus interface names when
calculating the typename for the C binding and the Docbook
sortas
attribute.
|
--generate-docbook OUTFILES
|
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.
Pass --output-directory to specify the directory
to put the output files in. By default the current directory
will be used.
|
--generate-c-code OUTFILES
|
Generate C code for all D-Bus interfaces and put it in
OUTFILES.c and
OUTFILES.h including any sub-directories. If you want the files to
be output in a different location use --output-directory as OUTFILES.h
including sub-directories will be referenced from OUTFILES.c.
The full paths would then be $(OUTDIR)/$(dirname $OUTFILES)/$(basename $OUTFILES).{c,h}.
|
--c-namespace YourProject
|
The namespace to use for generated C code. This is expected
to be in CamelCase
or Ugly_Case (see above).
|
--pragma-once
|
If this option is passed, the
#pragma once
preprocessor directive is used instead of include guards.
|
--c-generate-object-manager
|
If this option is passed, suitable GDBusObject,
GDBusObjectProxy, GDBusObjectSkeleton and
GDBusObjectManagerClient subclasses are generated.
|
--c-generate-autocleanup none|objects|all
|
This option influences what types autocleanup functions are
generated for. 'none' means to not generate any autocleanup functions.
'objects' means to generate them for object types, and 'all' means to
generate them for object types and interfaces. The default is 'objects'
due to a corner case in backwards compatibility with a few projects,
but you should likely switch your project to use 'all'.
This option was added in GLib 2.50.
|
--output-directory OUTDIR
|
Directory to output generated source to. Equivalent to changing directory before generation.
This option cannot be used with --body,
--header, --interface-info-body or
--interface-info-header; and
--output must be used.
|
--header
|
If this option is passed, it will generate the header code and write it to the disk by
using the path and file name provided by --output.
Using --generate-c-code, --generate-docbook or
--output-directory are not allowed to be used along with
--header and --body options, because these options
are used to generate only one file.
|
--body
|
If this option is passed, it will generate the source code and write it to the disk by
using the path and file name provided by --output.
Using --generate-c-code, --generate-docbook or
--output-directory are not allowed to be used along with
--header and --body options, because these options
are used to generate only one file.
|
--interface-info-header
|
If this option is passed, it will generate the header code for the
GDBusInterfaceInfo structures only and will write it to
the disk by using the path and file name provided by
--output.
Using --generate-c-code, --generate-docbook or
--output-directory are not allowed to be used along with
the --interface-info-header and
--interface-info-body options, because these options
are used to generate only one file.
|
--interface-info-body
|
If this option is passed, it will generate the source code for the
GDBusInterfaceInfo structures only and will write it to
the disk by using the path and file name provided by
--output.
Using --generate-c-code, --generate-docbook or
--output-directory are not allowed to be used along with
the --interface-info-header and
--interface-info-body options, because these options
are used to generate only one file.
|
--output OUTFILE
|
The full path where the header (--header,
--interface-info-header) or the source code
(--body, --interface-info-body) will
be written, using the path and filename provided by
--output. The full path could be something like
$($OUTFILE).{c,h}.
Using --generate-c-code, --generate-docbook or
--output-directory is not allowed along with
--output, because the latter is used to generate only one file.
|
--annotate ELEMENT KEY VALUE
|
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:
Any UTF-8 string can be used for KEY and VALUE.
|
--glib-min-required VERSION
|
Specifies the minimum version of GLib which the code generated by
gdbus-codegen can depend on. This may be used to
make backwards-incompatible changes in the output or behaviour of
gdbus-codegen in future, which users may opt in to
by increasing the value they pass for --glib-min-required.
If this option is not passed, the output from gdbus-codegen
is guaranteed to be compatible with all versions of GLib from 2.30
upwards, as that is when gdbus-codegen was first
released.
Note that some version parameters introduce incompatible changes: all callers
of the generated code might need to be updated, and if the generated code is part of
a library's API or ABI, then increasing the version parameter can result in an API
or ABI break.
The version number must be of the form
MAJOR.MINOR.MICRO,
where all parts are integers. MINOR and
MICRO are optional. The version number may not be smaller
than 2.30.
If the version number is 2.64 or greater, the generated code will
have the following features: (1) If a method has h (file
descriptor) parameter(s), a GUnixFDList parameter will exist in the
generated code for it (whereas previously the annotation
org.gtk.GDBus.C.UnixFD was required), and (2) Method call functions will
have two additional arguments to allow the user to specify GDBusCallFlags
and a timeout value, as is possible when using g_dbus_proxy_call().
|
--glib-max-allowed VERSION
|
Specifies the maximum version of GLib which the code generated by
gdbus-codegen can depend on. This may be used to
ensure that code generated by gdbus-codegen is
compilable with specific older versions of GLib that your software has
to support.
The version number must be of the form
MAJOR.MINOR.MICRO,
where all parts are integers. MINOR and
MICRO are optional. The version number must
be greater than or equal to that passed to --glib-min-required.
It defaults to the version of GLib which provides this gdbus-codegen.
|
Supported D-Bus Annotations
The following D-Bus annotations are supported by
gdbus-codegen:
org.freedesktop.DBus.Deprecated
|
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 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.
|
org.gtk.GDBus.Since
|
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.
|
org.gtk.GDBus.DocString
|
A string with Docbook content for documentation. This annotation can
be used on <interface>,
<method>,
<signal>,
<property> and
<arg> elements.
|
org.gtk.GDBus.DocString.Short
|
A string with Docbook content for short/brief
documentation. This annotation can only be used on
<interface> elements.
|
org.gtk.GDBus.C.Name
|
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
or Ugly_Case (see above).
|
org.gtk.GDBus.C.ForceGVariant
|
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.
|
org.gtk.GDBus.C.UnixFD
|
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.
|
As an easier alternative to using the
org.gtk.GDBus.DocString annotation, note that
parser used by gdbus-codegen parses XML
comments in a way similar to gtk-doc:
Note that <parameter>since</parameter> can be used in any inline
documentation bit (e.g. for interfaces, methods, signals and
properties) to set the org.gtk.GDBus.Since
annotation. For the org.gtk.GDBus.DocString
annotation (and inline comments), note that substrings of the form
<link linkend="net.Corp.Bar"><type>net.Corp.Bar</type></link>,
net.Corp.Bar.FooMethod(),
<link linkend="net.Corp.Bar-BarSignal"><type>“BarSignal”</type></link> and
<link linkend="net.Corp.InlineDocs--BazProperty"><type>“BazProperty”</type></link> are all
expanded to links to the respective interface, method, signal and
property.
Additionally, substrings starting with @ and % characters are rendered as
parameter and
constant respectively.
If both XML comments and
org.gtk.GDBus.DocString or
org.gtk.GDBus.DocString.Short annotations are
present, the latter wins.
Example
Consider the following D-Bus Introspection XML.
If gdbus-codegen is used on this file like this:
two files called
myapp-generated.[ch] are
generated. The files provide an abstract
GTypeInterface-derived type called
MyAppFrobber as well as two instantiable types with
the same name but suffixed with Proxy and
Skeleton. The generated file, roughly, contains the
following facilities:
Thus, for every D-Bus method, there will be three C functions for
calling the method, one GObject signal for handling an incoming
call and one C function for completing an incoming call. For every
D-Bus signal, there's one GObject signal and one C function for
emitting it. For every D-Bus property, two C functions are
generated (one setter, one getter) and one GObject property. The
following table summarizes the generated facilities and where they
are applicable:
Client-side usage
You can use the generated proxy type with the generated
constructors:
Instead of using the generic GDBusProxy facilities, one can use
the generated methods such as
my_app_frobber_call_hello_world() to invoke
the net.Corp.MyApp.Frobber.HelloWorld()
D-Bus method, connect to the
::notification GObject signal to receive
the net.Corp.MyApp.Frobber::Notication
D-Bus signal and get/set the
net.Corp.MyApp.Frobber:Verbose D-Bus
Property using either the GObject property
:verbose or the
my_app_get_verbose() and
my_app_set_verbose() methods. Use the
standard “notify” signal to listen to property changes.
Note that all property access is via GDBusProxy's
property cache so no I/O is ever done when reading properties.
Also note that setting a property will cause the
org.freedesktop.DBus.Properties.Set method 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.
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. Similarly, 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().
Instead of subclassing, it is often easier to use the generated
MyAppFrobberSkeleton subclass. To handle incoming
method calls, use g_signal_connect() with
the ::handle-* signals and instead of
overriding GObject's
get_property() and
set_property() vfuncs, use
g_object_get() and
g_object_set() or the generated property
getters and setters (the generated class has an internal
property bag implementation).
To facilitate atomic changesets (multiple properties changing at
the same time), “notify” signals are queued up when
received. The queue is drained in an idle handler (which is called from the
thread-default main loop
of the thread where the skeleton object was
constructed) and will cause emissions of the org.freedesktop.DBus.Properties::PropertiesChanged
signal with all the properties that have changed. Use
g_dbus_interface_skeleton_flush() or
g_dbus_object_skeleton_flush() to empty the queue
immediately. Use g_object_freeze_notify() and
g_object_thaw_notify() for atomic changesets if on a different
thread.
C Type Mapping
Scalar types
(type-strings
'b',
'y',
'n',
'q',
'i',
'u',
'x',
't' and
'd')
),
strings (type-strings
's',
'ay',
'o' and
'g') and
arrays of string (type-strings
'as',
'ao' and
'aay')
are mapped to the natural types,
e.g. gboolean, gdouble, gint, gchar*,
gchar** and
so on. Everything else is mapped to the GVariant
type.
This automatic mapping can be turned off by using the annotation
org.gtk.GDBus.C.ForceGVariant - if used then a
GVariant is always exchanged instead of the
corresponding native C type. This annotation may be convenient to
use when using
bytestrings (type-string 'ay')
for data that could have embedded NUL bytes.
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.
The ABI of the generated GTypes will be preserved only if
the org.gtk.GDBus.Since annotation is used
judiciously — this is because the VTable for the GInterface
relies on functions pointers for signal handlers. Specifically, if
a D-Bus method, property or signal or is added to a D-Bus
interface, then ABI of the generated GInterface type is preserved
if, and only if, each added method, property signal is annotated
with they org.gtk.GDBus.Since annotation using
a greater version number than previous versions.
The generated C code currently happens to be annotated with gtk-doc / GObject
Introspection comments / annotations. The layout and
contents might change in the future so no guarantees about
e.g. SECTION usage etc. is given.
While the generated Docbook for D-Bus interfaces isn't expected to
change, no guarantees are given at this point.
It is important to note that the generated code should not be
checked into revision control systems, nor it should be included
in distributed source archives.