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
|
#ifndef IPC_H
#define IPC_H
// NOTE: control socket. lets other programs (waybar, scripts, keybinds, another terminal) drive
// the running player and follow what it plays.
//
// transport: a unix stream socket, $XDG_RUNTIME_DIR/mp3_player.sock (MP3_PLAYER_SOCKET overrides).
// the runtime dir is 0700 and peers are checked with SO_PEERCRED, so only our own user gets in.
// served by the main thread inside the poll it already sleeps in: no thread, no timer, nothing
// runs while nobody talks to it.
//
// protocol: text lines in, json lines out.
// -> one command per line, the same words the test scripts use: toggle, seek +10, vol -5 ...
// <- exactly one json line answers every command: {"ok":true} / {"ok":false,"error":"..."}
// -> "status" <- {"event":"status", ...} (the reply)
// -> "subscribe" <- a status line now, then another one whenever anything in it changes
// (track, state, whole second of position, volume, queue, download progress).
// the connection still takes commands.
// a line never contains a raw newline (json escapes it), so clients just read lines.
//
// clients in this binary (no window, no audio, no catalog, they only talk to the socket):
// mp3_player ctl <command ...> send one command, print the reply. exit 0 ok, 1 refused, 2 not running
// mp3_player waybar [options] stream waybar custom module json, survives player restarts
#define IPC_MAX_CLIENTS 16
#define IPC_LINE_MAX 1024
#define IPC_PATH_MAX 108 // sun_path
typedef struct IpcClient IpcClient;
struct IpcClient {
int fd; // -1 = free slot
b32 subscribed;
u32 in_size;
char in[IPC_LINE_MAX];
};
typedef struct IpcServer IpcServer;
struct IpcServer {
int listen_fd; // -1 = not serving
int epoll_fd; // listen + every client, the main loop polls just this one fd
char path[IPC_PATH_MAX];
IpcClient clients[IPC_MAX_CLIENTS];
u32 client_count;
u32 subscribers;
u64 commands; // counters for the profiler
u64 events;
};
// NOTE: called once per complete line. reply with ipc_reply (exactly once).
typedef void IpcHandler(void *user, IpcServer *server, u32 client, char *line);
internal b32 ipc_socket_path(char *out, u32 size);
internal b32 ipc_server_open(IpcServer *server); // false: another instance serves, or no socket
internal void ipc_server_close(IpcServer *server);
internal void ipc_server_poll(IpcServer *server, IpcHandler *handler, void *user); // when epoll_fd is readable
internal void ipc_reply(IpcServer *server, u32 client, const char *text, u32 size); // one line, '\n' added
internal void ipc_subscribe(IpcServer *server, u32 client); // status events from now on
internal void ipc_broadcast(IpcServer *server, const char *text, u32 size); // to subscribers
//- json writing (flat objects is all the protocol needs)
typedef struct JsonWriter JsonWriter;
struct JsonWriter {
char *data;
u32 size;
u32 capacity;
b32 any;
};
internal void json_begin(JsonWriter *json, char *buffer, u32 capacity);
internal void json_end(JsonWriter *json);
internal void json_string(JsonWriter *json, const char *key, String8 value);
internal void json_u64(JsonWriter *json, const char *key, u64 value);
internal void json_f64(JsonWriter *json, const char *key, f64 value);
internal void json_bool(JsonWriter *json, const char *key, b32 value);
//- clients
internal int ipc_connect(void); // -1 when nobody serves
internal int ipc_ctl_main(int argc, char **argv);
internal int ipc_waybar_main(int argc, char **argv);
#endif // IPC_H
|