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