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