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