#Exporting Services and Objects

Writing a D-Bus daemon in C usually involves registering XML schemas, building hash tables of method pointers, and manually unpacking container iterators.

In Goose, your native Zig struct is the service. You define public methods, declare properties, and register the struct with your connection. Goose inspects your types at compile time and handles routing automatically.


#Defining a Service Struct

Here is a minimal service struct:

zig
const std = @import("std");
const goose = @import("goose");
const GStr = goose.core.value.GStr;

pub const PlayerService = struct {
    conn: *goose.Connection,

    // Properties exposed through org.freedesktop.DBus.Properties
    Volume: goose.Property(u32, .ReadWrite) = .init(50),
    Status: goose.Property(GStr, .Read) = .init(GStr.new("Playing")),

    // Signals emitted to listening clients
    TrackChanged: goose.Signal(GStr) = .init("TrackChanged"),

    // Required: initializer called when the object is registered
    pub fn init(conn: *goose.Connection, _: void) @This() {
        return .{ .conn = conn };
    }

    // Public methods become D-Bus methods
    pub fn Play(self: *@This()) !void {
        self.Status.value = GStr.new("Playing");
    }

    pub fn Pause(self: *@This()) !void {
        self.Status.value = GStr.new("Paused");
    }

    pub fn NextTrack(self: *@This(), track_title: GStr) !void {
        try self.TrackChanged.trigger(self.conn, track_title);
    }
};

#Context Parameter Injection

One of the best features in Goose is Context Parameter Injection.

Often, your method needs access to runtime information about the caller—like the sender's bus address, message flags, an active connection handle, or a passed file descriptor. But you don't want those internal parameters appearing on your public D-Bus interface.

When Goose compiles your method dispatchers, it inspects your parameter types. If it encounters a context type, it injects the value at runtime and omits it completely from the public D-Bus wire signature and introspection XML:

Parameter Routing

Context Parameter Routing

How Goose transparently merges wire arguments with runtime metadata at call time.

Incoming D-Bus Wire Message
Method: "TransferFile" Sig: 'sh'
arg0: "report.pdf" ('s') arg1: FD Index 0 ('h')
▼ Unpacked by Goose Dispatcher
Injected Runtime Context
conn: *goose.Connection Active socket connection
msg: *const goose.core.Message Caller bus address & serial
Decoded Wire Arguments
filename: GStr Decoded string ('s')
rfd: ResolvedFd Live open OS handle ('h')
▼ Invokes your native Zig method
Your Method Signature
pub fn TransferFile(
    self: *@This(),
    conn: *goose.Connection,         // Injected by Goose
    msg: *const goose.core.Message,  // Injected by Goose
    filename: GStr,                  // Decoded wire arg 's'
    rfd: goose.ResolvedFd,           // Resolved wire arg 'h'
) !u32

#Supported Injected Parameters

Parameter Type What Goose Injects On Public Wire Sig?
*goose.Connection Active connection handling the request No
*const goose.core.Message or goose.core.Message Raw message envelope (access sender with msg.getSender()) No
goose.ResolvedFd Live open OS file descriptor (std.posix.fd_t) Yes (Type 'h')
[]goose.ResolvedFd Slice of live open file descriptors Yes (Type 'ah')

#Example Using Injected Parameters

zig
pub fn TransferFile(
    self: *@This(),
    conn: *goose.Connection,         // Injected: active connection
    msg: *const goose.core.Message,  // Injected: caller metadata
    filename: GStr,                  // Wire arg: 's'
    rfd: goose.ResolvedFd,           // Wire arg: 'h'
) !u32 {
    _ = self;

    // Check who called us
    const sender = msg.getSender() orelse "unknown";
    std.debug.print("File '{s}' sent by caller {s}\n", .{ filename.s, sender });

    // Read directly from the passed file descriptor
    var buf: [256]u8 = undefined;
    const bytes = try std.posix.read(rfd.handle, &buf);

    // Emit a signal back using the injected connection
    try self.FileReceivedSignal.trigger(conn, @intCast(bytes));

    return @intCast(bytes);
}

#Properties and Signals

#Properties

Properties are wrapped with goose.Property(T, access):

Property Struct Definition (Live Extract) ~/goose/src/dbus_types.zig
zig
/// Property definition helper.
pub fn Property(comptime T: type, comptime access_mode: Access) type {
    return struct {
        value: T,
        pub const __is_goose_property = true;
        pub const DataType = T;
        pub const AccessMode = access_mode;

        pub fn init(value: T) @This() {
            return .{ .value = value };
        }
    };
}

Supported access permissions:

  • .Read: Read-only.
  • .Write: Write-only.
  • .ReadWrite: Fully readable and writable.

#Automatic `PropertiesChanged` Signals

When an external client calls org.freedesktop.DBus.Properties.Set to update a writable property, Goose automatically emits an org.freedesktop.DBus.Properties.PropertiesChanged signal to notify all listening clients on the bus.

#Signals

Signals are declared using goose.Signal(T):

zig
TrackChanged: goose.Signal(GStr) = .init("TrackChanged"),

To emit the signal to all subscribers on the bus:

zig
try self.TrackChanged.trigger(self.conn, GStr.new("Next Song"));

#Error Handling

You don't need to manually create error reply packets. Simply return a Zig error union (!T) from your method:

zig
pub fn SetVolume(self: *@This(), volume: u32) !void {
    if (volume > 100) {
        return error.VolumeTooHigh;
    }
    self.Volume.value = volume;
}

If your method returns an error, Goose converts it into a D-Bus error reply:

  • Error Name: org.freedesktop.DBus.Error.Failed
  • Error Description: The Zig error name (e.g. "VolumeTooHigh").

#Registering and Serving

Registering the service connects your struct to a bus name and path:

zig
pub fn main(init: std.process.Init) !void {
    const allocator = init.gpa;
    const io = init.io;

    var conn = try goose.Connection.init(allocator, .Session, io, init.environ_map);
    defer conn.close();

    // Register our object
    try conn.registerObject(
        PlayerService,
        "org.mpris.MediaPlayer2.Goose",
        "/org/mpris/MediaPlayer2",
        {},
    );

    std.debug.print("Player service registered. Waiting for requests...\n", .{});

    // Enter request loop
    try conn.serve();
}

#Testing with Linux CLI Tools

Once your service is running, you can test it directly from your terminal using busctl:

#Introspect Your Service

bash
busctl --user introspect org.mpris.MediaPlayer2.Goose /org/mpris/MediaPlayer2

#Call a Method

bash
busctl --user call org.mpris.MediaPlayer2.Goose /org/mpris/MediaPlayer2 \
  org.mpris.MediaPlayer2.Goose Play

#Read a Property

bash
busctl --user get-property org.mpris.MediaPlayer2.Goose /org/mpris/MediaPlayer2 \
  org.mpris.MediaPlayer2.Goose Volume