GDBUS-CODEGEN(1)
πΈ NAME πΈ
gdbus-codegen β the D-Bus code and documentation generator, aka the lil tool that writes your boilerplate so you don't have to π
β¨ SYNOPSIS β¨
gdbus-codegen
[--help]
[--interface-prefix org.project.Prefix]
[--header | --body | --interface-info-header | --interface-info-body | --generate-c-code OUTFILES]
[--c-namespace YourProject]
[--c-generate-object-manager]
[--c-generate-autocleanup none|objects|all]
[--output-directory OUTDIR | --output OUTFILE]
[--generate-docbook OUTFILES]
[--generate-md OUTFILES]
[--generate-rst OUTFILES]
[--pragma-once]
[--xml-files FILE]
[--symbol-decorator DECORATOR [--symbol-decorator-header HEADER] [--symbol-decorator-define DEFINE]]
[--annotate ELEMENT KEY VALUE]β¦
[--glib-min-required VERSION]
[--glib-max-allowed VERSION]
[--extension-path EXTENSION_PATH]
FILEβ¦
π DESCRIPTION π
okay so gdbus-codegen is the bestie that generates code and/or documentation for one or more D-Bus interfaces. no cap.
Here's the vibe: gdbus-codegen reads D-Bus Introspection XML (spec lives at https://dbus.freedesktop.org/doc/dbus-specification.html#introspection-format) from the files you pass as extra arguments on the command line, and then it β¨generates output filesβ¨. Right now it can generate:
- C source code β via
--body - C header β via
--header - DocBook XML β via
--generate-docbook
OR, if you want something more restricted, you can generate C source code and headers that just contain the interface information (as GDBusInterfaceInfo structures) using --interface-info-body and --interface-info-header. lowkey perfect when you only need the metadata, not the whole personality.
π¦ GENERATING C CODE π¦
When you're generating C code, a GInterface derived type gets generated for each D-Bus interface. And it gets better β for every generated type, FooBar, two concrete instantiatable types are ALSO generated: FooBarProxy and FooBarSkeleton, both implementing said interface.
FooBarProxyis derived fromGDBusProxyand is intended for use on the client side πFooBarSkeletonis derived from theGDBusInterfaceSkeletontype, which makes it easy to export on aGDBusConnectionβ either directly or via aGDBusObjectManagerServerinstance π
For C code generation you can use whichever of these fits:
--bodyβ generates source code--headerβ generates headers--interface-info-bodyβ generates interface information source code--interface-info-headerβ generates interface information headers
These options must be used along with --output, which is what specifies the file to output to. fr fr.
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, because multiple files are being generated. Instead, pass --output-directory to specify the directory to put the output files in. By default the current directory will be used.
π naming, the way that it works π
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.Coyotethe name used isComAcmeCoyote. - For the D-Bus interface
org.project.Bar.Frobnicatorwith--interface-prefixset toorg.project., the name used isBarFrobnicator.
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 the 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). iconic behavior honestly.
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.iSCSITargetwith the valueiSCSI_Target, the CamelCase form isiSCSITargetwhile the lower-case form isiscsi_target. - If the annotation is used on the method
EjectTheiPodwith the valueEject_The_iPod, the lower-case form iseject_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. (See the DocBook documentation at https://tdg.docbook.org/tdg/4.5/refentry.html.) it's giving very formal, very scholarly π
π GENERATING MARKDOWN DOCUMENTATION π
Each generated Markdown file (see the --generate-md option for details) is a plain text Markdown document describing the D-Bus interface. simple. clean. slay. β¨
π§ GENERATING RESTRUCTUREDTEXT DOCUMENTATION π§
Each generated reStructuredText file (see the --generate-rst option for details) is a plain text reStructuredText document (spec at https://docutils.sourceforge.io/rst.html) describing the D-Bus interface.
π OPTIONS π
The following options are supported, bestie. read them all, i'm not summarizing, we don't do that here π«Ά
-h,--helpβ Show help and exit. the classic.--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 type name for the C binding and the DocBooksortasattribute (docs athttps://tdg.docbook.org/tdg/4.5/primary.html).--generate-docbook OUTFILESβ Generate DocBook Documentation for each D-Bus interface and put it inOUTFILES-NAME.xmlwhereNAMEis a placeholder for the interface name, e.g.net.Corp.FooBarand so on.
Pass --output-directory to specify the directory to put the output files in. By default the current directory will be used.
--generate-md OUTFILESβ Generate Markdown Documentation for each D-Bus interface and put it inOUTFILES-NAME.mdwhereNAMEis a placeholder for the interface name, e.g.net.Corp.FooBarand so on.
Pass --output-directory to specify the directory to put the output files in. By default the current directory will be used.
--generate-rst OUTFILESβ Generate reStructuredText Documentation for each D-Bus interface and put it inOUTFILES-NAME.rstwhereNAMEis a placeholder for the interface name, e.g.net.Corp.FooBarand 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 inOUTFILES.candOUTFILES.hincluding any sub-directories. If you want the files to be output in a different location use--output-directoryasOUTFILES.hincluding sub-directories will be referenced fromOUTFILES.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 (seehttps://en.wikipedia.org/wiki/Camel_case) or Ugly_Case (see above).--pragma-onceβ If this option is passed, the#pragma oncepreprocessor directive (seehttps://en.wikipedia.org/wiki/Pragma_once) is used instead of include guards.--c-generate-object-managerβ If this option is passed, suitableGDBusObject,GDBusObjectProxy,GDBusObjectSkeletonandGDBusObjectManagerClientsubclasses are generated. main character energy for your object manager π--c-generate-autocleanup none|objects|allβ This option influences what types autocleanup functions are generated for.nonemeans to not generate any autocleanup functions.objectsmeans to generate them for object types, andallmeans to generate them for object types and interfaces. The default isobjectsdue to a corner case in backwards compatibility with a few projects, but you should likely switch your project to useall. 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 theGDBusInterfaceInfostructures 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 theGDBusInterfaceInfostructures 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.
--symbol-decorator DECORATORβ If aDECORATORis passed in with this option, all the generated function prototypes in the generated header will be marked withDECORATOR. This can be used, for instance, to export symbols from code generated withgdbus-codegen.
This option was added in GLib 2.66.
--symbol-decorator-header HEADERβ If aHEADERis passed in with this option, the generated header will put a#include HEADERbefore the rest of the items, except for the inclusion guards or#pragma once(if--pragma-onceis used). This is used if using another header file is needed for the decorator passed in via--symbol-decoratorto be defined.
This option was added in GLib 2.66.
This option can only be used if --symbol-decorator is used.
--symbol-decorator-define DEFINEβ bestie, if you hand aDEFINEover with this one, the generated source is gonna slap a#define DEFINEat the top, before all the other items. Why tho? Because sometimes a particular macro is needed so that the decorator you passed via--symbol-decoratoractually resolves to the correct definition while the generated source is getting compiled. π
This option was added in GLib 2.66.
Heads up: you can only use this one if --symbol-decorator is also in play.
--output OUTFILEβ this is the full path where your header (--header,--interface-info-header) or your source code (--body,--interface-info-body) gets written, using the path and filename you gave with--output. The full path could look something like$($OUTFILE).{c,h}. β¨
Nope nope nope: using --generate-c-code, --generate-docbook or --output-directory alongside --output is not allowed, because --output exists to generate exactly one file.
Since GLib 2.80, if OUTFILE is the literal string -, the header or source code gets written to standard output. π«Ά
For --body and --interface-info-body, the generated code will not automatically #include a matching header file when it's writing to standard output, because there's just no obvious name for that header file. Which means you might have to do the cc -include foo.h thing, or generate a filename like foo-impl.h and #include it into a wrapper .c file. π
For --header and --interface-info-header, there's no obvious name for a traditional multiple-inclusion guard when writing to standard output, so the move here is to use the --pragma-once option. Highly recommend.
And in that rare, cursed situation where your intended output filename starts with -? Prefix it with ./. Periodt.
--annotate ELEMENT KEY VALUEβ used to inject D-Bus annotations into the XML files you give it. It works with interfaces, methods, signals, properties and arguments, like this:
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. Go wild. π
--glib-min-required VERSIONβ sets the minimum version of GLib that the code generated bygdbus-codegenis allowed to depend on. Why does this exist? So that futuregdbus-codegenversions can make backwards-incompatible changes to their output or behaviour, and you can opt in by bumping the value you pass here. If you don't pass this option, the output fromgdbus-codegenis guaranteed to be compatible with all versions of GLib from 2.30 upwards, since that's whengdbus-codegenfirst dropped. π
Real talk though: some version parameters introduce incompatible changes. Every single caller of the generated code might need updating, and if the generated code is part of a library's API or ABI, then increasing the version parameter can literally cause an API or ABI break. Be careful out there. π
The version number must be of the form MAJOR.MINOR.MICRO, where all parts are integers. MINOR and MICRO are optional. And it may not be smaller than 2.30.
If the version number is 2.64 or greater, the generated code will have these features:
- If a method has
h(file descriptor) parameter(s), aGUnixFDListparameter will exist in the generated code for it (whereas previously the annotationorg.gtk.GDBus.C.UnixFDwas required), and - Method call functions will have two additional arguments so you can specify
GDBusCallFlagsand a timeout value, exactly as is possible when usingg_dbus_proxy_call(). Slay.
--glib-max-allowed VERSIONβ sets the maximum version of GLib that the code generated bygdbus-codegenis allowed to depend on. This is here so you can make sure the codegdbus-codegenspits out 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 the one passed to --glib-min-required. It defaults to the version of GLib which provides this gdbus-codegen.
--extension-path EXTENSION_PATHβ used to load an extension into the codegen.EXTENSION_PATHis a path to a Python file that gets loaded as a module. The extension needs to define at least the functiondef init(args, options)whereargsis aargparse.Namespaceandoptionsis adictcontaining the keyversion. β¨
Every other API the extension can use is internal and therefore unstable, but effort is made to bump the version field when those internals change. If you wanna use this mechanism, please get in touch by filing an issue at https://gitlab.gnome.org/GNOME/glib/-/issues. with your use case. (iykyk)
π 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 say that the element is deprecated if its value is true. Heads up though: this annotation is defined in the D-Bus specification at https://dbus.freedesktop.org/doc/dbus-specification.html#introspection-format and it can only assume the values true and false. So specifically, you cannot specify the version that the element was deprecated in, nor any helpful deprecation message. That kind of info should go into the element documentation instead. π₯²
When generating C code, this annotation is used to add G_GNUC_DEPRECATED to the generated functions for the element.
When generating DocBook XML, a deprecation warning will appear alongside the documentation for the element.
org.gtk.GDBus.Since
Can be used on any <interface>, <method>, <signal> and <property> element to specify the version the element appeared in (any free-form string, but it gets compared using a version-aware sort function). π¦
When generating C code, this field is used to ensure function pointer order for preserving ABI/API β check out '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 (https://en.wikipedia.org/wiki/Camel_case) 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 the parser used by gdbus-codegen parses XML comments in a way similar to gtk-doc (https://gitlab.gnome.org/GNOME/gtk-doc/):
<!--
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.
@since: 2.30
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>
Note that @since can be used in any inline documentation bit (e.g. for interfaces, methods, signals and properties) to set the org.gtk.GDBus.Since annotation. And for the org.gtk.GDBus.DocString annotation (and inline comments), note that substrings of the form #net.Corp.Bar, net.Corp.Bar.FooMethod(), #net.Corp.Bar::BarSignal and #net.Corp.InlineDocs:BazProperty are all expanded to links to the respective interface, method, signal and property. Additionally, substrings starting with @ and % characters are rendered as parameter (https://tdg.docbook.org/tdg/4.5/parameter.html) and constant (https://tdg.docbook.org/tdg/4.5/constant.html) respectively. β¨
If both XML comments and org.gtk.GDBus.DocString or org.gtk.GDBus.DocString.Short annotations are present, the latter wins. No cap.
π EXAMPLE π
Okay bestie, 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>
If gdbus-codegen is used on this file like this:
gdbus-codegen --generate-c-code myapp-generated \
--c-namespace MyApp \
--interface-prefix net.corp.MyApp. \
net.Corp.MyApp.Frobber.xml
and boom π₯ bestie, you now have two files called myapp-generated.[ch] just sitting there, existing, thriving. Inside them:
- an abstract
GTypeInterface-derived type calledMyAppFrobber - two instantiatable types with that exact same name, just suffixed with
ProxyandSkeletonπ¦
Roughly speaking, the generated file contains these facilities:
/* 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);
Okay so the vibe here is extremely consistent, and honestly? iconic. For every D-Bus method, you get three C functions for calling it, 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 get generated (one setter, one getter β a whole duo π ) plus one GObject property.
Here's the rundown of what shows up and where it actually applies:
- Types β Client side: use
MyAppFrobberProxy. Server side: any type implementing theMyAppFrobberinterface. - Methods β Client side: use
m_a_f_hello_world()to call. Server side: receive via thehandle_hello_world()signal handler, then complete the call withm_a_f_complete_hello_world(). - Signals β Client side: connect to the
::notificationsignal. Server side: usem_a_f_emit_notification()to emit signal. - Properties (Reading) β Client side: use
m_a_f_get_verbose()or the:verboseproperty. Server side: implement theget_property()vfunc of GObject. - Properties (writing) β Client side: use
m_a_f_set_verbose()or the:verboseproperty. Server side: implement theset_property()vfunc of GObject.
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);
Instead of using the generic GDBusProxy facilities, you can use the generated methods β like 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::Notification 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 GObject::notify signal to listen to property changes. π
Heads up bestie: all property access goes through theGDBusProxyproperty cache, so no I/O is ever done when reading properties. Also note that setting a property will cause theorg.freedesktop.DBus.Properties.Setmethod (documentation athttps://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-properties) 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(). β¨
okay so instead of subclassing, bestie, it's often way easier to just use the generated MyAppFrobberSkeleton subclass that codegen handed you. π
To deal with incoming method calls, you hit g_signal_connect() with the ::handle-* signals β and instead of overriding the get_property() and set_property() vfuncs from GObject, you just use g_object_get() and g_object_set(), or the generated property getters and setters (the generated class has an internal property bag implementation, lowkey iconic). β¨
For example:
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 */
}
now for the β¨atomic changesetsβ¨ tea, because multiple properties can change at the same time and we need that to be clean: GObject::notify signals get queued up when they're received. π«Ά That queue gets drained in an idle handler β which is called from the thread-default main loop of the thread where the skeleton object was constructed β and that draining will cause emissions of the org.freedesktop.DBus.Properties::PropertiesChanged (documentation <https://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-properties>) signal with all the properties that have changed. π₯ Wanna empty that queue immediately? Use g_dbus_interface_skeleton_flush() or g_dbus_object_skeleton_flush(). And if you're on a different thread, use g_object_freeze_notify() and g_object_thaw_notify() for atomic changesets. periodt. π
π¦ C TYPE MAPPING π¦
okay so the mapping rules, bestie, memorize these:
- Scalar types (type strings
b,y,n,q,i,u,x,tandd), strings (type stringss,ay,oandg) and arrays of strings (type stringsas,aoandaay) are mapped to the natural types β likegboolean,gdouble,gint,gchar*,gchar**and so on. π - Everything else? Mapped to the
GVarianttype. no cap.
And you can turn this automatic mapping off with the annotation org.gtk.GDBus.C.ForceGVariant β if you use it, a GVariant is always exchanged instead of the corresponding native C type. πΈ This annotation is genuinely convenient when you're using bytestrings (type string ay) for data that could have embedded nul bytes. π
π STABILITY GUARANTEES π
here's the promises, and bestie they are promises:
- 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 their C ABI either. The ABI of the generated instance and class structures will be preserved as well. slay.
- The ABI of the generated GType instances will be preserved only if the
org.gtk.GDBus.Sinceannotation is used judiciously β this is because the VTable for the GInterface relies on function pointers for signal handlers. π Specifically: if a D-Bus method, property or signal is added to a D-Bus interface, then the ABI of the generated GInterface type is preserved if, and only if, each added method, property signal is annotated with theorg.gtk.GDBus.Sinceannotation using a greater version number than previous versions. π§
The generated C code currently happens to be annotated with gtk-doc <https://gitlab.gnome.org/GNOME/gtk-doc/> and GObject Introspection <https://gi.readthedocs.io/en/latest/> comments and annotations. β But the layout and contents might change in the future, so no guarantees about e.g. SECTION usage etc. are given. π
And while the generated DocBook for D-Bus interfaces isn't expected to change, no guarantees are given at this point either. π
β οΈ okay this one is important and I need you to listen: it is important to note that the generated code should not be checked into version control systems, nor should it be included in distributed source archives. π do not do it. I'm so serious.
π BUGS π
Found something broken? π Please send bug reports to either the distribution bug tracker or the upstream bug tracker <https://gitlab.gnome.org/GNOME/glib/issues/new>. π«Ά
π SEE ALSO π
gdbus(1)<man:gdbus(1)> β go read it bestie, it's giving essential π
and that's the whole page, babes π
β this one's from the gdbus-codegen manual, and the lil footer down at the bottom just reads gdbus-codegen(), with the version slot left empty and no date stamped in. iconic minimalism, honestly. go generate some code and eat. πβ¨π¦
π¦ where this page comes from
- arch rolling: glib2-devel 2.90.1-1 Β· LGPL-2.1-or-later
- ubuntu stonking: libgio-2.0-dev-bin 2.90.0-1 Β· LGPL-2.1-or-later
- π the page's license: LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms