1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
#ifndef IPC_DBUS_H
#define IPC_DBUS_H
// NOTE: just enough of the d-bus wire protocol to be a service on the session bus (for mpris):
// connect + EXTERNAL auth, marshal / unmarshal messages, method returns, errors and signals.
// no libdbus / sd-bus: the protocol is a few pages, and this way the bus socket is one more fd in
// the main loop's poll, read with MSG_DONTWAIT, nothing in between.
//
// little endian messages only (every peer on this machine is), no unix fd passing, no
// introspection of others. spec: https://dbus.freedesktop.org/doc/dbus-specification.html
typedef enum DBusType {
DBUS_METHOD_CALL = 1,
DBUS_METHOD_RETURN = 2,
DBUS_ERROR = 3,
DBUS_SIGNAL = 4,
} DBusType;
#define DBUS_FLAG_NO_REPLY_EXPECTED 0x1
#define DBUS_MESSAGE_MAX (64 * 1024) // ours are a few hundred bytes, calls to us too
//- writing
typedef struct DBusWriter DBusWriter;
struct DBusWriter {
u8 *data;
u32 size;
u32 capacity;
b32 overflow;
};
internal void dbus_w_init(DBusWriter *w, u8 *buffer, u32 capacity);
internal void dbus_w_align(DBusWriter *w, u32 alignment);
internal void dbus_w_u8(DBusWriter *w, u8 value);
internal void dbus_w_u32(DBusWriter *w, u32 value);
internal void dbus_w_i32(DBusWriter *w, s32 value);
internal void dbus_w_i64(DBusWriter *w, s64 value);
internal void dbus_w_f64(DBusWriter *w, f64 value);
internal void dbus_w_bool(DBusWriter *w, b32 value);
internal void dbus_w_string(DBusWriter *w, String8 value); // also object paths
internal void dbus_w_signature(DBusWriter *w, const char *signature);
internal u32 dbus_w_array_begin(DBusWriter *w, u32 element_alignment); // returns a mark for _end
internal void dbus_w_array_end(DBusWriter *w, u32 mark);
#define dbus_w_cstring(w, text) dbus_w_string((w), str8((char *)(text)))
//- reading
typedef struct DBusReader DBusReader;
struct DBusReader {
u8 *data;
u32 size;
u32 at; // offsets are relative to the start of the body, which is 8 aligned in the message
b32 bad;
};
internal u32 dbus_r_u32(DBusReader *r);
internal s64 dbus_r_i64(DBusReader *r);
internal f64 dbus_r_f64(DBusReader *r);
internal String8 dbus_r_string(DBusReader *r); // s and o
internal String8 dbus_r_signature(DBusReader *r); // g
typedef struct DBusMessage DBusMessage;
struct DBusMessage {
u8 type;
u8 flags;
u32 serial;
u32 reply_serial;
String8 path;
String8 interface;
String8 member;
String8 error_name;
String8 destination;
String8 sender;
String8 signature;
u8 *body;
u32 body_size;
};
internal DBusReader dbus_body(DBusMessage *message);
//- connection
typedef struct DBusConnection DBusConnection;
struct DBusConnection {
int fd; // -1 = not connected
u32 serial;
char unique_name[64];
u32 in_size;
u8 in[DBUS_MESSAGE_MAX * 2];
u8 out[DBUS_MESSAGE_MAX];
};
typedef void DBusHandler(void *user, DBusConnection *bus, DBusMessage *message);
internal b32 dbus_connect_session(DBusConnection *bus); // connect, authenticate, Hello
internal void dbus_close(DBusConnection *bus);
// NOTE: one message. body_signature "" = no body. returns the serial (0 = failed).
internal u32 dbus_send(DBusConnection *bus, u8 type, u8 flags, const char *path, const char *interface, const char *member,
const char *destination, u32 reply_serial, const char *error_name,
const char *body_signature, u8 *body, u32 body_size);
internal void dbus_reply(DBusConnection *bus, DBusMessage *call, const char *signature, u8 *body, u32 body_size);
internal void dbus_reply_error(DBusConnection *bus, DBusMessage *call, const char *error_name, const char *text);
internal b32 dbus_call(DBusConnection *bus, const char *destination, const char *path, const char *interface, const char *member,
const char *signature, u8 *body, u32 body_size, DBusMessage *reply, u64 timeout_ns); // blocking, startup only
internal b32 dbus_poll(DBusConnection *bus, DBusHandler *handler, void *user); // false: the bus went away
#endif // IPC_DBUS_H
|