0xCatPKG/dbuz
D-Bus client library written in pure zig
This library is more like a pet project and practice for me to learn Zig. It provides a simple interface to interact with D-Bus offloading multiple tasks to the zig's comptime.
$ zig fetch --save git+https://github.com/0xCatPKG/dbuz
Then in your build.zig file, do something like
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const dbuz = b.dependency("dbuz", .{
.target = target,
.optimize = optimize,
});
// ...
exe_mod.addImport("dbuz", dbuz.module("dbuz"));
// ...
}
const std = @import("std");
const dbuz = @import("dbuz");
pub fn main() !void {
const allocator = std.heap.page_allocator;
/// Create connection to D-Bus session bus
const connection = try dbuz.connect(allocator, .{});
defer connection.deinit();
const poll_cond, const poll_thread =
try dbuz.spawnPollingThread(connection, allocator);
defer allocator.destroy(poll_cond);
defer poll_thread.join();
defer poll_cond.* = false;
std.posix.nanosleep(0, std.time.ns_per_ms * 25); // Sleep for some time to avoid busy loop
std.debug.print('My unique name is: {?s}\n', .{connection.unique_name});
}
Information about handling method calls and signals in dbuz may be found in Interfaces and MatchGroups
dbuz serializes native types using compile time reflection. Following mapping exists:
DBus signature symbol | Native type | Notes |
---|---|---|
y |
u/i1..8 | Any int with less or equal to 8 bits is serialized as a byte |
b |
bool | Boolean type |
n |
i9..16 | Any int with less or equal to 16 bits is serialized as a short |
q |
u9..16 | Any int with less or equal to 16 bits is serialized as a short |
i |
i17..32 | Any int with less or equal to 32 bits is serialized as an int |
u |
u17..32 | Any int with less or equal to 32 bits is serialized as an unsigned int |
x |
i33..64 | Any int with less or equal to 64 bits is serialized as a long |
t |
u33..64 | Any int with less or equal to 64 bits is serialized as an unsigned long |
d |
f1..64 | All floats must be less than or equal to 64 bits |
h |
std.fs.File, std.fs.Dir | Out of band data, uses .handle as file descriptor |
s |
DBusString | String type |
o |
DBusObjectPath | Object path type |
g |
DBusSignature | Signature type |
a |
[]T | All pointers passed to de/serialization must be slices |
() |
struct{...} | All structs except tuples are serialized recursively according to their field native types, tuples are serialized as sequence of values |
{} |
struct{...} | All structs are serialized as dicts, if they have following declarations: put, getOrPut, getOrPutAdapted, get, iterator, KV |
v |
union(enum) {...} | All union types are serialized as a variants. Duplicate types are unchecked illegal behavior |