#Introspection and Code Generation
D-Bus has a neat built-in discovery protocol called Introspection. Any object on the bus can expose the org.freedesktop.DBus.Introspectable interface, which returns an XML document listing all methods, arguments, signatures, properties, and signals supported by that object.
Goose supports introspection in both directions:
- Server Side: When you register any native Zig struct, Goose automatically implements
org.freedesktop.DBus.Introspectableand serves valid XML without any extra work from you. - Client Side: Goose includes a code generator tool (
goose-generate) that takes any D-Bus introspection XML and emits strongly typed Zig client code.
#Automatic Introspection on Exported Structs
When you register a service struct with Goose, you do not need to create or maintain XML files:
const EchoService = struct {
conn: *goose.Connection,
Counter: goose.Property(u32, .ReadWrite) = goose.property(u32, .ReadWrite, 1),
pub fn Echo(self: *@This(), text: goose.core.value.GStr) !goose.core.value.GStr {
_ = self;
return text;
}
};
When an external client calls Introspect on this object path, Goose synthesizes this schema on the fly:
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
<interface name="com.example.EchoService">
<method name="Echo">
<arg name="arg0" type="s" direction="in"/>
<arg type="s" direction="out"/>
</method>
<property name="Counter" type="u" access="readwrite"/>
</interface>
<interface name="org.freedesktop.DBus.Properties"/>
<interface name="org.freedesktop.DBus.Introspectable"/>
</node>
#The CLI Developer Tools
Goose provides two command-line tools in src/tools/:
#Installing Tools in `build.zig`
To build and install the tools to your zig-out/bin, add the artifact installations in your build.zig:
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const goose_dep = b.dependency("goose", .{
.target = target,
.optimize = optimize,
});
b.installArtifact(goose_dep.artifact("goose-introspection"));
b.installArtifact(goose_dep.artifact("goose-generate"));
}
Now run zig build to generate the binaries.
#Tool 1: `goose-introspection`
goose-introspection queries a target service on the bus and prints its XML schema:
./zig-out/bin/goose-introspection <destination> <path> <bus_type>
#Examples
#Query Desktop Notifications
./zig-out/bin/goose-introspection org.freedesktop.Notifications /org/freedesktop/Notifications session
#Query Systemd Hostname Service
./zig-out/bin/goose-introspection org.freedesktop.hostname1 /org/freedesktop/hostname1 system
#Tool 2: `goose-generate`
goose-generate takes an object path and emits a complete, strongly typed Zig proxy struct to stdout:
./zig-out/bin/goose-generate <destination> <path> <bus_type> > src/notifications_proxy.zig
#Type Translation Table
The code generator maps D-Bus schema types into native Zig and Goose types:
| D-Bus Signature | Generated Parameter Type | Generated Return Type |
|---|---|---|
s |
GStr |
GStr |
o |
GPath |
GPath |
g |
GSig |
GSig |
b |
bool |
bool |
y |
u8 |
u8 |
q / u / t |
u16 / u32 / u64 |
u16 / u32 / u64 |
n / i / x |
i16 / i32 / i64 |
i16 / i32 / i64 |
d |
f64 |
f64 |
h |
GUFd |
GUFd |
v |
GVariant |
GVariant |
as |
[]const GStr |
[]GStr |
#What Generated Code Looks Like
Here is an example of what goose-generate outputs:
const std = @import("std");
const goose = @import("goose");
const proxy = goose.proxy;
const GStr = goose.core.value.GStr;
pub const NotificationsProxy = struct {
inner: proxy.Proxy,
pub fn init(conn: *goose.Connection) NotificationsProxy {
return .{
.inner = proxy.Proxy.init(
conn,
"org.freedesktop.Notifications",
"/org/freedesktop/Notifications",
"org.freedesktop.Notifications",
),
};
}
pub fn GetCapabilities(self: NotificationsProxy) ![]GStr {
var res = try self.inner.call("GetCapabilities", .{});
defer res.deinit();
return res.expectAlloc([]GStr);
}
pub fn CloseNotification(self: NotificationsProxy, id: u32) !void {
var res = try self.inner.call("CloseNotification", .{id});
defer res.deinit();
return res.expect(void);
}
};
Using the generated proxy in your application is clean and type-safe:
var client = NotificationsProxy.init(&conn);
const capabilities = try client.GetCapabilities();
defer allocator.free(capabilities);
for (capabilities) |cap| {
std.debug.print("Capability: {s}\n", .{cap.s});
}
#Automating Code Generation in `build.zig`
Instead of checking generated .zig files into git, you can run goose-generate during the build step:
const generate_cmd = b.addRunArtifact(goose_dep.artifact("goose-generate"));
generate_cmd.addArgs(&.{
"org.freedesktop.Notifications",
"/org/freedesktop/Notifications",
"session",
});
const generated_file = generate_cmd.captureStdOut();
// Import the generated proxy as an anonymous module
my_app.root_module.addAnonymousImport("notifications_proxy", .{
.root_source_file = generated_file,
});