RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
D-Bus transport
Import dbus/mod.zig from a consumer under src/. This is an opt-in module:
importing it does not open a connection or claim a name. Consumers such as
notifications choose their own startup policy. No new C library is
required; the module uses libc and the existing Wayland event loop.
Connection.openSession(allocator, loop, environ)reads the explicitDBUS_SESSION_BUS_ADDRESSviastd.process.Environ.openSystemreadsDBUS_SYSTEM_BUS_ADDRESS, defaulting tounix:path=/var/run/dbus/system_bus_socket. Tests override it with a private bus address.openinstead takes an address string. Unix path and Linux abstract alternatives are tried in order. Connection and EXTERNAL authentication share a two-second deadline per alternative; a supplied GUID must match. The fd stays nonblocking throughout; startup waits usepoll, while subsequent traffic useswl.EventLoop.addFd.- Call
hello(owner, callback)once. On a successful callback,connection.unique_nameis available and ordinary calls are permitted.requestName(name, flags, owner, callback)returns the bus'suresult unchanged; the consumer chooses ownership/contending-name policy. - Construct arguments in a
wire.Writer{ .allocator = allocator }, then pass its address and the signature tocall. Calls copy their encoded arguments into the write queue, so the writer can immediately be freed. The callback gets either a borrowedwire.Message(including D-Bus ERROR messages) or a localFailuresuch as timeout/disconnect. Timeout is in milliseconds; the timer wakes for the earliest pending deadline and disarms when idle. At most 256 calls may be pending. registerinstalls a handler for path/interface/member. Its strings and owner must outlive their registration. Readmessage.bodythrough a local reader copy after checkingmessage.headers.signature, then usereplyorreplyError. Incoming bodies are already structurally validated.unregisterMethodsByOwner(owner)removes method handlers for temporary request objects before their owner or path storage is freed.signalemits a path/interface/member message. No-reply calls suppress both success and error replies. Unknown methods receiveUnknownMethod; handler errors becomeInvalidArgs. A handler that replies must return success to avoid a second error reply. Returning successfully without replying sends nothing; this is the supported deferred-reply path. CalldeferReply(message)inside the handler to own aConnection.Deferredcontaining the serial and a copied sender. It does not retain the body or any other borrowed slices. Return success after deferring. Later,replyDeferred(&deferred, signature, &body)orreplyErrorDeferred(&deferred, name, description)sends and releases the handle. Both suppress output for no-reply calls. On a send failure, the handle remains owned so it can be retried or released explicitly. Treat handles as move-only and use only their originating connection.releaseDeferred(&deferred)abandons without sending and is idempotent; replying through an already consumed handle fails withInvalidRequest.registerSignalinstalls a signal handler matching on optionalsender,path,interface, andmember. Multiple matching handlers can be installed.unregisterSignalandunregisterSignalsByOwnerremove handlers.addMatch(rule, owner, callback)andremoveMatch(rule, owner, callback)issue bus match rules (callbackcan be null for fire-and-forget).subscribe(handler, owner, callback)andunsubscribe(handler, owner, callback)format the signal match rule and register/unregister the handler and match rule in one step. Incoming signals are dispatched to all matching handlers.closeremoves sources, closes the fd and completes all pending calls exactly once withDisconnected; it is idempotent.destroyadditionally frees the object. Both must run before the Wayland event loop is destroyed. Callbacks may close the connection or enqueue new messages. They may not destroy it or recursively dispatch the loop. All message slices expire when the callback returns; copy any values needed later. Closing invalidates every deferred reply (Disconnectedon a later send). Consumers must abandon every outstanding deferred reply on close: cancel their delayed work and callreleaseDeferredbeforedestroy. Handles are caller-owned, so close does not free their copied sender storage for them.flushattempts one nonblocking write without dispatching callbacks. This allows best-effort shutdown messages before close, but does not guarantee delivery if the socket is backpressured or only accepts a partial write. An optionalon_disconnecthook runs exactly once after pending calls are completed, while the connection remains alive. It may cancel consumer work and release deferred handles, but must not destroy the connection or dispatch.
Writers expose byte, int(T, value), uint32, boolean, double, string,
objectPath, signature, alignTo, variant, beginArray and endArray.
After variant(sig) write one value; pair each beginArray(element_alignment)
with endArray(token). Begin structs/dict entries with alignTo(8). Body
writers start at offset zero, equivalent to the message's 8-aligned body
origin. Readers returned by array retain absolute offsets and bound their
view at the array end. done checks that the complete value was consumed.
Outgoing messages are also decoded/validated before entering the queue.
The codec enforces the specification's 128 MiB message and 64 MiB array
limits, zero padding/terminators, UTF-8, signature grammar/depth, header types,
required fields, names, and exact body consumption. The connection separately
caps queued output at 8 MiB. Malformed input closes the connection and logs
an error; it is never passed to a handler. openSystemWithFds opts into Unix
descriptor negotiation and reception (up to 16 descriptors per message).
Received descriptors are borrowed during dispatch and closed afterward; duplicate
one if it must outlive the callback. Sending descriptors remains unsupported.
There is no automatic introspection, reconnect, or bus activation fallback.
Authentication supports EXTERNAL only.
See TESTING.md for zig build test-dbus, and
hello.md for the real wire fixture's provenance.