-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipc.h
320 lines (287 loc) · 10.2 KB
/
ipc.h
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#ifndef IPC_H_
#define IPC_H_
#include <stdint.h>
#include <sys/epoll.h>
#include <yajl/yajl_gen.h>
#include "IPCClient.h"
// clang-format off
#define IPC_MAGIC "DWM-IPC"
#define IPC_MAGIC_ARR { 'D', 'W', 'M', '-', 'I', 'P', 'C'}
#define IPC_MAGIC_LEN 7 // Not including null char
#define IPCCOMMAND(FUNC, ARGC, TYPES) \
{ #FUNC, {FUNC }, ARGC, (ArgType[ARGC])TYPES }
// clang-format on
typedef enum IPCMessageType {
IPC_TYPE_RUN_COMMAND = 0,
IPC_TYPE_GET_MONITORS = 1,
IPC_TYPE_GET_TAGS = 2,
IPC_TYPE_GET_LAYOUTS = 3,
IPC_TYPE_GET_DWM_CLIENT = 4,
IPC_TYPE_SUBSCRIBE = 5,
IPC_TYPE_EVENT = 6
} IPCMessageType;
typedef enum IPCEvent {
IPC_EVENT_TAG_CHANGE = 1 << 0,
IPC_EVENT_CLIENT_FOCUS_CHANGE = 1 << 1,
IPC_EVENT_LAYOUT_CHANGE = 1 << 2,
IPC_EVENT_MONITOR_FOCUS_CHANGE = 1 << 3,
IPC_EVENT_FOCUSED_TITLE_CHANGE = 1 << 4,
IPC_EVENT_FOCUSED_STATE_CHANGE = 1 << 5
} IPCEvent;
typedef enum IPCSubscriptionAction {
IPC_ACTION_UNSUBSCRIBE = 0,
IPC_ACTION_SUBSCRIBE = 1
} IPCSubscriptionAction;
/**
* Every IPC packet starts with this structure
*/
typedef struct dwm_ipc_header {
uint8_t magic[IPC_MAGIC_LEN];
uint32_t size;
uint8_t type;
} __attribute((packed)) dwm_ipc_header_t;
typedef enum ArgType {
ARG_TYPE_NONE = 0,
ARG_TYPE_UINT = 1,
ARG_TYPE_SINT = 2,
ARG_TYPE_FLOAT = 3,
ARG_TYPE_PTR = 4,
ARG_TYPE_STR = 5
} ArgType;
/**
* An IPCCommand function can have either of these function signatures
*/
typedef union ArgFunction {
void (*single_param)(const Arg *);
void (*array_param)(const Arg *, int);
} ArgFunction;
typedef struct IPCCommand {
char *name;
ArgFunction func;
unsigned int argc;
ArgType *arg_types;
} IPCCommand;
typedef struct IPCParsedCommand {
char *name;
Arg *args;
ArgType *arg_types;
unsigned int argc;
} IPCParsedCommand;
/**
* Initialize the IPC socket and the IPC module
*
* @param socket_path Path to create the socket at
* @param epoll_fd File descriptor for epoll
* @param commands Address of IPCCommands array defined in config.h
* @param commands_len Length of commands[] array
*
* @return int The file descriptor of the socket if it was successfully created,
* -1 otherwise
*/
int ipc_init(const char *socket_path, const int p_epoll_fd,
IPCCommand commands[], const int commands_len);
/**
* Uninitialize the socket and module. Free allocated memory and restore static
* variables to their state before ipc_init
*/
void ipc_cleanup();
/**
* Get the file descriptor of the IPC socket
*
* @return int File descriptor of IPC socket, -1 if socket not created.
*/
int ipc_get_sock_fd();
/**
* Get address to IPCClient with specified file descriptor
*
* @param fd File descriptor of IPC Client
*
* @return Address to IPCClient with specified file descriptor, -1 otherwise
*/
IPCClient *ipc_get_client(int fd);
/**
* Check if an IPC client exists with the specified file descriptor
*
* @param fd File descriptor
*
* @return int 1 if client exists, 0 otherwise
*/
int ipc_is_client_registered(int fd);
/**
* Disconnect an IPCClient from the socket and remove the client from the list
* of known connected clients
*
* @param c Address of IPCClient
*
* @return 0 if the client's file descriptor was closed successfully, the
* result of executing close() on the file descriptor otherwise.
*/
int ipc_drop_client(IPCClient *c);
/**
* Accept an IPC Client requesting to connect to the socket and add it to the
* list of clients
*
* @return File descriptor of new client, -1 on error
*/
int ipc_accept_client();
/**
* Read an incoming message from an accepted IPC client
*
* @param c Address of IPCClient
* @param msg_type Address to IPCMessageType variable which will be assigned
* the message type of the received message
* @param msg_size Address to uint32_t variable which will be assigned the size
* of the received message
* @param msg Address to char* variable which will be assigned the address of
* the received message. This must be freed using free().
*
* @return 0 on success, -1 on error reading message, -2 if reading the message
* resulted in EAGAIN, EINTR, or EWOULDBLOCK.
*/
int ipc_read_client(IPCClient *c, IPCMessageType *msg_type, uint32_t *msg_size,
char **msg);
/**
* Write any pending buffer of the client to the client's socket
*
* @param c Client whose buffer to write
*
* @return Number of bytes written >= 0, -1 otherwise. errno will still be set
* from the write operation.
*/
ssize_t ipc_write_client(IPCClient *c);
/**
* Prepare a message in the specified client's buffer.
*
* @param c Client to prepare message for
* @param msg_type Type of message to prepare
* @param msg_size Size of the message in bytes. Should not exceed
* MAX_MESSAGE_SIZE
* @param msg Message to prepare (not including header). This pointer can be
* freed after the function invocation.
*/
void ipc_prepare_send_message(IPCClient *c, const IPCMessageType msg_type,
const uint32_t msg_size, const char *msg);
/**
* Prepare an error message in the specified client's buffer
*
* @param c Client to prepare message for
* @param msg_type Type of message
* @param format Format string following vsprintf
* @param ... Arguments for format string
*/
void ipc_prepare_reply_failure(IPCClient *c, IPCMessageType msg_type,
const char *format, ...);
/**
* Prepare a success message in the specified client's buffer
*
* @param c Client to prepare message for
* @param msg_type Type of message
*/
void ipc_prepare_reply_success(IPCClient *c, IPCMessageType msg_type);
/**
* Send a tag_change_event to all subscribers. Should be called only when there
* has been a tag state change.
*
* @param mon_num The index of the monitor (Monitor.num property)
* @param old_state The old tag state
* @param new_state The new (now current) tag state
*/
void ipc_tag_change_event(const int mon_num, TagState old_state,
TagState new_state);
/**
* Send a client_focus_change_event to all subscribers. Should be called only
* when the client focus changes.
*
* @param mon_num The index of the monitor (Monitor.num property)
* @param old_client The old DWM client selection (Monitor.oldsel)
* @param new_client The new (now current) DWM client selection
*/
void ipc_client_focus_change_event(const int mon_num, Client *old_client,
Client *new_client);
/**
* Send a layout_change_event to all subscribers. Should be called only
* when there has been a layout change.
*
* @param mon_num The index of the monitor (Monitor.num property)
* @param old_symbol The old layout symbol
* @param old_layout Address to the old Layout
* @param new_symbol The new (now current) layout symbol
* @param new_layout Address to the new Layout
*/
void ipc_layout_change_event(const int mon_num, const char *old_symbol,
const Layout *old_layout, const char *new_symbol,
const Layout *new_layout);
/**
* Send a monitor_focus_change_event to all subscribers. Should be called only
* when the monitor focus changes.
*
* @param last_mon_num The index of the previously selected monitor
* @param new_mon_num The index of the newly selected monitor
*/
void ipc_monitor_focus_change_event(const int last_mon_num,
const int new_mon_num);
/**
* Send a focused_title_change_event to all subscribers. Should only be called
* if a selected client has a title change.
*
* @param mon_num Index of the client's monitor
* @param client_id Window XID of client
* @param old_name Old name of the client window
* @param new_name New name of the client window
*/
void ipc_focused_title_change_event(const int mon_num, const Window client_id,
const char *old_name, const char *new_name);
/**
* Send a focused_state_change_event to all subscribers. Should only be called
* if a selected client has a state change.
*
* @param mon_num Index of the client's monitor
* @param client_id Window XID of client
* @param old_state Old state of the client
* @param new_state New state of the client
*/
void ipc_focused_state_change_event(const int mon_num, const Window client_id,
const ClientState *old_state,
const ClientState *new_state);
/**
* Check to see if an event has occured and call the *_change_event functions
* accordingly
*
* @param mons Address of Monitor pointing to start of linked list
* @param lastselmon Address of pointer to previously selected monitor
* @param selmon Address of selected Monitor
*/
void ipc_send_events(Monitor *mons, Monitor **lastselmon, Monitor *selmon);
/**
* Handle an epoll event caused by a registered IPC client. Read, process, and
* handle any received messages from clients. Write pending buffer to client if
* the client is ready to receive messages. Drop clients that have sent an
* EPOLLHUP.
*
* @param ev Associated epoll event returned by epoll_wait
* @param mons Address of Monitor pointing to start of linked list
* @param selmon Address of selected Monitor
* @param lastselmon Address of pointer to previously selected monitor
* @param tags Array of tag names
* @param tags_len Length of tags array
* @param layouts Array of available layouts
* @param layouts_len Length of layouts array
*
* @return 0 if event was successfully handled, -1 on any error receiving
* or handling incoming messages or unhandled epoll event.
*/
int ipc_handle_client_epoll_event(struct epoll_event *ev, Monitor *mons,
Monitor **lastselmon, Monitor *selmon,
const char *tags[], const int tags_len,
const Layout *layouts, const int layouts_len);
/**
* Handle an epoll event caused by the IPC socket. This function only handles an
* EPOLLIN event indicating a new client requesting to connect to the socket.
*
* @param ev Associated epoll event returned by epoll_wait
*
* @return 0, if the event was successfully handled, -1 if not an EPOLLIN event
* or if a new IPC client connection request could not be accepted.
*/
int ipc_handle_socket_epoll_event(struct epoll_event *ev);
#endif /* IPC_H_ */