forked from lynks--/lifebar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipc.c
296 lines (258 loc) · 8.11 KB
/
ipc.c
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
#include "lifebar.h"
int is_output_key_label(char *c) {
if(strcmp(c, "name") == 0) return 1;
if(strcmp(c, "active") == 0) return 1;
if(strcmp(c, "x") == 0) return 1;
if(strcmp(c, "y") == 0) return 1;
if(strcmp(c, "width") == 0) return 1;
if(strcmp(c, "height") == 0) return 1;
return 0;
}
void handle_output_value_label(struct i3_output *o, char *key, char *value) {
if(strlen(key) > 0) {
if(strcmp(key, "name") == 0) strcpy(o->name, value);
else if(strcmp(key, "active") == 0) strcpy(o->active, value);
else if(strcmp(key, "x") == 0) o->x = atoi(value);
else if(strcmp(key, "y") == 0) o->y = atoi(value);
else if(strcmp(key, "width") == 0) o->width = atoi(value);
else if(strcmp(key, "height") == 0) o->height = atoi(value);
}
}
int is_workspace_key_label(char *c) {
if(strcmp(c, "name") == 0) return 1;
if(strcmp(c, "visible") == 0) return 1;
if(strcmp(c, "focused") == 0) return 1;
if(strcmp(c, "urgent") == 0) return 1;
if(strcmp(c, "output") == 0) return 1;
return 0;
}
void handle_workspace_value_label(struct i3_workspace *o,
char *key, char *value) {
if(strlen(key) > 0) {
if(strcmp(key, "name") == 0) strcpy(o->name, value);
else if(strcmp(key, "visible") == 0) strcpy(o->visible, value);
else if(strcmp(key, "focused") == 0) strcpy(o->focused, value);
else if(strcmp(key, "urgent") == 0) strcpy(o->urgent, value);
else if(strcmp(key, "output") == 0) strcpy(o->output, value);
}
}
void debug_i3_output(struct i3_output *head) {
while(head != NULL) {
printf("=========\nname=%s\nactive=%s\nx=%d\ny=%d\nwidth=%d\nheight=\
%d\n",
head->name, head->active, head->x, head->y, head->width,
head->height);
head = head->next;
}
}
void debug_i3_workspace(struct i3_workspace *head) {
while(head != NULL) {
printf("=========\nname=%s\nvisible=%s\noutput=%s\n", head->name,
head->visible, head->output);
head = head->next;
}
}
void i3_ipc_send(char **ret, int i3_sock, int type, char *payload) {
//we pack the buffer and send it
int i;
int plen = strlen(payload);
char buf[plen + I3_HEADER_LENGTH];
strcpy(buf, "i3-ipc");
*((int *)(buf + 6)) = plen;
*((int *)(buf + 10)) = type;
strcpy(buf + I3_HEADER_LENGTH, payload);
write(i3_sock, buf, plen + 14);
//first read the header
char *recv_buf = malloc(I3_HEADER_LENGTH + 1);
uint32_t recv = 0;
while(recv < I3_HEADER_LENGTH) {
ssize_t r = read(i3_sock, recv_buf + recv, I3_HEADER_LENGTH - recv);
if(r == -1) {
perror("recv");
exit(1);
}
if(r == 0) {
fprintf(stderr, "%seof from i3, quitting...\n", BAD_MSG);
exit(1);
}
recv += r;
}
*((char *)(recv_buf + recv)) = '\0';
//make sure this message is well formatted
if(strncmp("i3-ipc", recv_buf, 6) != 0) {
fprintf(stderr, "%sbad ipc message from i3:%s\n", BAD_MSG, recv_buf);
exit(1);
}
//then we check the message length
uint32_t msg_len = *((uint32_t *)(recv_buf + 6));
recv_buf = realloc(recv_buf, recv + 1 + msg_len);
uint32_t total = I3_HEADER_LENGTH + msg_len;
while(recv < total) {
ssize_t r = read(i3_sock, recv_buf + recv, total - recv);
if(r == -1) {
perror("recv");
exit(1);
}
if(r == 0) {
fprintf(stderr, "%seof from i3, quitting...\n", BAD_MSG);
exit(1);
}
recv += r;
}
*((char *)(recv_buf + recv)) = '\0';
*ret = recv_buf + 14; //skip over the header
}
void free_ipc_result(char *c) {
//this function exists because we have to free the actual malloced
//result, which included the 14 byte header we skipped over
free(c - 14);
}
void free_workspaces_list(struct i3_workspace *head) {
struct i3_workspace *next;
while(head != NULL) {
next = head->next;
free(head);
head = next;
}
}
struct i3_output *get_i3_outputs(int i3_sock) {
//query i3 for the current outputs and build a linked list
//note: this function returns a list in reverse order to the json array
struct i3_output *head = NULL;
char *output_json;
i3_ipc_send(&output_json, i3_sock, GET_OUTPUTS, "");
char key[64];
char label[64];
char *labelptr = label;
int inside = 0;
int i;
for(i = 0; i < strlen(output_json); i++) {
char c = *(output_json + i);
char d = *(output_json + i + 1);
if(c == '\"') {
if(inside == 0) inside = 1;
else {
inside = 0;
*labelptr = '\0';
if(strcmp(label, "name") == 0) {
//we are entering a new output block
//we create a new struct pointing to the current head
struct i3_output *o = malloc(sizeof *o);
o->next = head;
head = o;
}
if(is_output_key_label(label)) strcpy(key, label);
else {
handle_output_value_label(head, key, label);
key[0] = '\0';
}
labelptr = label; //reset the label pointer
}
}
else if(c == ':' && d != '\"' && d != '{')
inside = 2; //inside a non-"-delimited value
else if((c == ',' || c == '}') && inside == 2) {
inside = 0;
*labelptr = '\0';
handle_output_value_label(head, key, label);
key[0] = '\0';
labelptr = label;
}
else {
if(inside != 0) {
*labelptr = c;
labelptr++;
}
}
}
free_ipc_result(output_json);
return head;
}
struct i3_workspace *get_i3_workspaces(int i3_sock) {
struct i3_workspace *head = NULL;
struct i3_workspace *tail = NULL;
char *workspace_json;
i3_ipc_send(&workspace_json, i3_sock, GET_WORKSPACES, "");
//protection from broken ipc code
if(*workspace_json != '[') return NULL;
char key[64];
char label[64];
char *labelptr = label;
int inside = 0;
int i;
for(i = 0; i < strlen(workspace_json); i++) {
char c = *(workspace_json + i);
char d = *(workspace_json + i + 1);
if(c == '\"') {
if(inside == 0) inside = 1;
else {
inside = 0;
*labelptr = '\0';
if(strcmp(label, "name") == 0) {
//we are entering a new workspace block
//we create a new struct pointing to the current head
struct i3_workspace *o = malloc(sizeof *o);
if(tail != NULL) tail->next = o;
else head = o;
o->next = NULL;
tail = o;
}
if(is_workspace_key_label(label)) strcpy(key, label);
else {
handle_workspace_value_label(tail, key, label);
key[0] = '\0';
}
labelptr = label; //reset the label pointer
}
}
else if(c == ':' && d != '\"' && d != '{')
inside = 2; //inside a non-"-delimited value
else if((c == ',' || c == '}') && inside == 2) {
inside = 0;
*labelptr = '\0';
handle_workspace_value_label(tail, key, label);
key[0] = '\0';
labelptr = label;
}
else {
if(inside != 0) {
*labelptr = c;
labelptr++;
}
}
}
free_ipc_result(workspace_json);
return head;
}
void get_i3_sockpath(char **ret) {
int pid, err;
int pipefd[2];
FILE *output;
char buf[512];
//set up the pipe
if(pipe(pipefd) < 0) {
perror("pipe");
exit(1);
}
if((pid = fork()) == 0) {
//we are in the child
//duplicate writing end of pipe to child stdout
dup2(pipefd[1], STDOUT_FILENO);
//close both of my ends of the pipe?
close(pipefd[0]);
close(pipefd[1]);
execl("/usr/bin/i3", "i3", "--get-socketpath", NULL);
perror("execl");
}
//we are in the parent
//close writing end of pipe
close(pipefd[1]);
//open the reading end of the pipe
output = fdopen(pipefd[0], "r");
fgets(buf, sizeof buf, output);
int len = strlen(buf);
//remove the trailing newline
buf[len - 1] = '\0';
*ret = malloc(len);
strcpy(*ret, buf);
}