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