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