]> git.sur5r.net Git - i3/i3/blob - i3bar/src/ipc.c
Fixes for undefined behaviour on signed shift (#3453)
[i3/i3] / i3bar / src / ipc.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3bar - an xcb-based status- and ws-bar for i3
5  * © 2010 Axel Wagner and contributors (see also: LICENSE)
6  *
7  * ipc.c: Communicating with i3
8  *
9  */
10 #include "common.h"
11
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <unistd.h>
15 #include <stdint.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <sys/socket.h>
19 #include <sys/un.h>
20 #include <i3/ipc.h>
21 #include <ev.h>
22 #ifdef I3_ASAN_ENABLED
23 #include <sanitizer/lsan_interface.h>
24 #endif
25
26 ev_io *i3_connection;
27
28 const char *sock_path;
29
30 typedef void (*handler_t)(char *);
31
32 /*
33  * Called, when we get a reply to a command from i3.
34  * Since i3 does not give us much feedback on commands, we do not much
35  *
36  */
37 void got_command_reply(char *reply) {
38     /* TODO: Error handling for command replies */
39 }
40
41 /*
42  * Called, when we get a reply with workspaces data
43  *
44  */
45 void got_workspace_reply(char *reply) {
46     DLOG("Got workspace data!\n");
47     parse_workspaces_json(reply);
48     draw_bars(false);
49 }
50
51 /*
52  * Called, when we get a reply for a subscription.
53  * Since i3 does not give us much feedback on commands, we do not much
54  *
55  */
56 void got_subscribe_reply(char *reply) {
57     DLOG("Got subscribe reply: %s\n", reply);
58     /* TODO: Error handling for subscribe commands */
59 }
60
61 /*
62  * Called, when we get a reply with outputs data
63  *
64  */
65 void got_output_reply(char *reply) {
66     DLOG("Clearing old output configuration...\n");
67     free_outputs();
68
69     DLOG("Parsing outputs JSON...\n");
70     parse_outputs_json(reply);
71     DLOG("Reconfiguring windows...\n");
72     reconfig_windows(false);
73
74     i3_output *o_walk;
75     SLIST_FOREACH(o_walk, outputs, slist) {
76         kick_tray_clients(o_walk);
77     }
78
79     if (!config.disable_ws) {
80         i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
81     }
82
83     draw_bars(false);
84 }
85
86 /*
87  * Called when we get the configuration for our bar instance
88  *
89  */
90 void got_bar_config(char *reply) {
91     DLOG("Received bar config \"%s\"\n", reply);
92     /* We initiate the main function by requesting infos about the outputs and
93      * workspaces. Everything else (creating the bars, showing the right workspace-
94      * buttons and more) is taken care of by the event-drivenness of the code */
95     i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
96
97     free_colors(&(config.colors));
98     parse_config_json(reply);
99
100     /* Now we can actually use 'config', so let's subscribe to the appropriate
101      * events and request the workspaces if necessary. */
102     subscribe_events();
103     if (!config.disable_ws)
104         i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
105
106     /* Initialize the rest of XCB */
107     init_xcb_late(config.fontname);
108
109     /* Resolve color strings to colorpixels and save them, then free the strings. */
110     init_colors(&(config.colors));
111
112     start_child(config.command);
113 }
114
115 /* Data structure to easily call the reply handlers later */
116 handler_t reply_handlers[] = {
117     &got_command_reply,   /* I3_IPC_REPLY_TYPE_COMMAND */
118     &got_workspace_reply, /* I3_IPC_REPLY_TYPE_WORKSPACES */
119     &got_subscribe_reply, /* I3_IPC_REPLY_TYPE_SUBSCRIBE */
120     &got_output_reply,    /* I3_IPC_REPLY_TYPE_OUTPUTS */
121     NULL,                 /* I3_IPC_REPLY_TYPE_TREE */
122     NULL,                 /* I3_IPC_REPLY_TYPE_MARKS */
123     &got_bar_config,      /* I3_IPC_REPLY_TYPE_BAR_CONFIG */
124     NULL,                 /* I3_IPC_REPLY_TYPE_VERSION */
125     NULL,                 /* I3_IPC_REPLY_TYPE_BINDING_MODES */
126     NULL,                 /* I3_IPC_REPLY_TYPE_CONFIG */
127     NULL,                 /* I3_IPC_REPLY_TYPE_TICK */
128     NULL,                 /* I3_IPC_REPLY_TYPE_SYNC */
129 };
130
131 /*
132  * Called, when a workspace event arrives (i.e. the user changed the workspace)
133  *
134  */
135 void got_workspace_event(char *event) {
136     DLOG("Got workspace event!\n");
137     i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
138 }
139
140 /*
141  * Called, when an output event arrives (i.e. the screen configuration changed)
142  *
143  */
144 void got_output_event(char *event) {
145     DLOG("Got output event!\n");
146     i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
147     if (!config.disable_ws) {
148         i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
149     }
150 }
151
152 /*
153  * Called, when a mode event arrives (i3 changed binding mode).
154  *
155  */
156 void got_mode_event(char *event) {
157     DLOG("Got mode event!\n");
158     parse_mode_json(event);
159     draw_bars(false);
160 }
161
162 /*
163  * Called, when a barconfig_update event arrives (i.e. i3 changed the bar hidden_state or mode)
164  *
165  */
166 void got_bar_config_update(char *event) {
167     /* check whether this affect this bar instance by checking the bar_id */
168     char *expected_id;
169     sasprintf(&expected_id, "\"id\":\"%s\"", config.bar_id);
170     char *found_id = strstr(event, expected_id);
171     FREE(expected_id);
172     if (found_id == NULL)
173         return;
174
175     /* reconfigure the bar based on the current outputs */
176     i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
177
178     free_colors(&(config.colors));
179
180     /* update the configuration with the received settings */
181     DLOG("Received bar config update \"%s\"\n", event);
182     char *old_command = config.command ? sstrdup(config.command) : NULL;
183     bar_display_mode_t old_mode = config.hide_on_modifier;
184     parse_config_json(event);
185     if (old_mode != config.hide_on_modifier) {
186         reconfig_windows(true);
187     }
188
189     /* update fonts and colors */
190     init_xcb_late(config.fontname);
191     init_colors(&(config.colors));
192
193     /* restart status command process */
194     if (old_command && strcmp(old_command, config.command) != 0) {
195         kill_child();
196         start_child(config.command);
197     }
198     free(old_command);
199
200     draw_bars(false);
201 }
202
203 /* Data structure to easily call the event handlers later */
204 handler_t event_handlers[] = {
205     &got_workspace_event,
206     &got_output_event,
207     &got_mode_event,
208     NULL,
209     &got_bar_config_update,
210 };
211
212 /*
213  * Called, when we get a message from i3
214  *
215  */
216 void got_data(struct ev_loop *loop, ev_io *watcher, int events) {
217     DLOG("Got data!\n");
218     int fd = watcher->fd;
219
220     /* First we only read the header, because we know its length */
221     uint32_t header_len = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) * 2;
222     char *header = smalloc(header_len);
223
224     /* We first parse the fixed-length IPC header, to know, how much data
225      * we have to expect */
226     uint32_t rec = 0;
227     while (rec < header_len) {
228         int n = read(fd, header + rec, header_len - rec);
229         if (n == -1) {
230             ELOG("read() failed: %s\n", strerror(errno));
231             exit(EXIT_FAILURE);
232         }
233         if (n == 0) {
234             /* EOF received. Since i3 will restart i3bar instances as appropriate,
235              * we exit here. */
236             DLOG("EOF received, exiting...\n");
237 #ifdef I3_ASAN_ENABLED
238             __lsan_do_leak_check();
239 #endif
240             clean_xcb();
241             exit(EXIT_SUCCESS);
242         }
243         rec += n;
244     }
245
246     if (strncmp(header, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC))) {
247         ELOG("Wrong magic code: %.*s\n Expected: %s\n",
248              (int)strlen(I3_IPC_MAGIC),
249              header,
250              I3_IPC_MAGIC);
251         exit(EXIT_FAILURE);
252     }
253
254     char *walk = header + strlen(I3_IPC_MAGIC);
255     uint32_t size;
256     memcpy(&size, (uint32_t *)walk, sizeof(uint32_t));
257     walk += sizeof(uint32_t);
258     uint32_t type;
259     memcpy(&type, (uint32_t *)walk, sizeof(uint32_t));
260
261     /* Now that we know, what to expect, we can start read()ing the rest
262      * of the message */
263     char *buffer = smalloc(size + 1);
264     rec = 0;
265
266     while (rec < size) {
267         int n = read(fd, buffer + rec, size - rec);
268         if (n == -1) {
269             ELOG("read() failed: %s\n", strerror(errno));
270             exit(EXIT_FAILURE);
271         }
272         if (n == 0) {
273             ELOG("Nothing to read!\n");
274             exit(EXIT_FAILURE);
275         }
276         rec += n;
277     }
278     buffer[size] = '\0';
279
280     /* And call the callback (indexed by the type) */
281     if (type & (1UL << 31)) {
282         type ^= 1UL << 31;
283         event_handlers[type](buffer);
284     } else {
285         if (reply_handlers[type])
286             reply_handlers[type](buffer);
287     }
288
289     FREE(header);
290     FREE(buffer);
291 }
292
293 /*
294  * Sends a message to i3.
295  * type must be a valid I3_IPC_MESSAGE_TYPE (see i3/ipc.h for further information)
296  *
297  */
298 int i3_send_msg(uint32_t type, const char *payload) {
299     uint32_t len = 0;
300     if (payload != NULL) {
301         len = strlen(payload);
302     }
303
304     /* We are a wellbehaved client and send a proper header first */
305     uint32_t to_write = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) * 2 + len;
306     /* TODO: I'm not entirely sure if this buffer really has to contain more
307      * than the pure header (why not just write() the payload from *payload?),
308      * but we leave it for now */
309     char *buffer = smalloc(to_write);
310     char *walk = buffer;
311
312     memcpy(buffer, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC));
313     walk += strlen(I3_IPC_MAGIC);
314     memcpy(walk, &len, sizeof(uint32_t));
315     walk += sizeof(uint32_t);
316     memcpy(walk, &type, sizeof(uint32_t));
317     walk += sizeof(uint32_t);
318
319     if (payload != NULL)
320         strncpy(walk, payload, len);
321
322     swrite(i3_connection->fd, buffer, to_write);
323
324     FREE(buffer);
325
326     return 1;
327 }
328
329 /*
330  * Initiate a connection to i3.
331  * socket_path must be a valid path to the ipc_socket of i3
332  *
333  */
334 int init_connection(const char *socket_path) {
335     sock_path = socket_path;
336     int sockfd = ipc_connect(socket_path);
337     i3_connection = smalloc(sizeof(ev_io));
338     ev_io_init(i3_connection, &got_data, sockfd, EV_READ);
339     ev_io_start(main_loop, i3_connection);
340     return 1;
341 }
342
343 /*
344  * Destroy the connection to i3.
345  */
346 void destroy_connection(void) {
347     close(i3_connection->fd);
348     ev_io_stop(main_loop, i3_connection);
349 }
350
351 /*
352  * Subscribe to all the i3-events, we need
353  *
354  */
355 void subscribe_events(void) {
356     if (config.disable_ws) {
357         i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"output\", \"mode\", \"barconfig_update\" ]");
358     } else {
359         i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"workspace\", \"output\", \"mode\", \"barconfig_update\" ]");
360     }
361 }