]> git.sur5r.net Git - i3/i3/blob - i3bar/src/ipc.c
Bugfix: Delay event-subscription until reconnect (thx aniou)
[i3/i3] / i3bar / src / ipc.c
1 /*
2  * i3bar - an xcb-based status- and ws-bar for i3
3  *
4  * © 2010-2011 Axel Wagner and contributors
5  *
6  * See file LICNSE for license information
7  *
8  * src/ipc.c: Communicating with i3
9  *
10  */
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <stdint.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 ev_timer   *reconn = NULL;
25
26 const char *sock_path;
27
28 typedef void(*handler_t)(char*);
29
30 /*
31  * Retry to connect.
32  *
33  */
34 void retry_connection(struct ev_loop *loop, ev_timer *w, int events) {
35     static int retries = 8;
36     if (init_connection(sock_path) == 0) {
37         if (retries == 0) {
38             ELOG("Retried 8 times - connection failed!\n");
39             exit(EXIT_FAILURE);
40         }
41         retries--;
42         return;
43     }
44     retries = 8;
45     ev_timer_stop(loop, w);
46     subscribe_events();
47
48     /* We get the current outputs and workspaces, to
49      * reconfigure all bars with the current configuration */
50     i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
51     if (!config.disable_ws) {
52         i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
53     }
54 }
55
56 /*
57  * Schedule a reconnect
58  *
59  */
60 void reconnect() {
61     if (reconn == NULL) {
62         if ((reconn = malloc(sizeof(ev_timer))) == NULL) {
63             ELOG("malloc() failed: %s\n", strerror(errno));
64             exit(EXIT_FAILURE);
65         }
66     } else {
67         ev_timer_stop(main_loop, reconn);
68     }
69     ev_timer_init(reconn, retry_connection, 0.25, 0.25);
70     ev_timer_start(main_loop, reconn);
71 }
72
73 /*
74  * Called, when we get a reply to a command from i3.
75  * Since i3 does not give us much feedback on commands, we do not much
76  *
77  */
78 void got_command_reply(char *reply) {
79     /* TODO: Error handling for command-replies */
80 }
81
82 /*
83  * Called, when we get a reply with workspaces-data
84  *
85  */
86 void got_workspace_reply(char *reply) {
87     DLOG("Got Workspace-Data!\n");
88     parse_workspaces_json(reply);
89     draw_bars();
90 }
91
92 /*
93  * Called, when we get a reply for a subscription.
94  * Since i3 does not give us much feedback on commands, we do not much
95  *
96  */
97 void got_subscribe_reply(char *reply) {
98     DLOG("Got Subscribe Reply: %s\n", reply);
99     /* TODO: Error handling for subscribe-commands */
100 }
101
102 /*
103  * Called, when we get a reply with outputs-data
104  *
105  */
106 void got_output_reply(char *reply) {
107     DLOG("Parsing Outputs-JSON...\n");
108     parse_outputs_json(reply);
109     DLOG("Reconfiguring Windows...\n");
110     realloc_sl_buffer();
111     reconfig_windows();
112 }
113
114 /* Data-structure to easily call the reply-handlers later */
115 handler_t reply_handlers[] = {
116     &got_command_reply,
117     &got_workspace_reply,
118     &got_subscribe_reply,
119     &got_output_reply,
120 };
121
122 /*
123  * Called, when a workspace-event arrives (i.e. the user changed the workspace)
124  *
125  */
126 void got_workspace_event(char *event) {
127     DLOG("Got Workspace Event!\n");
128     i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
129 }
130
131 /*
132  * Called, when an output-event arrives (i.e. the screen-configuration changed)
133  *
134  */
135 void got_output_event(char *event) {
136     DLOG("Got Output Event!\n");
137     i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
138     if (!config.disable_ws) {
139         i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
140     }
141 }
142
143 /* Data-structure to easily call the reply-handlers later */
144 handler_t event_handlers[] = {
145     &got_workspace_event,
146     &got_output_event
147 };
148
149 /*
150  * Called, when we get a message from i3
151  *
152  */
153 void got_data(struct ev_loop *loop, ev_io *watcher, int events) {
154     DLOG("Got data!\n");
155     int fd = watcher->fd;
156
157     /* First we only read the header, because we know its length */
158     uint32_t header_len = strlen(I3_IPC_MAGIC) + sizeof(uint32_t)*2;
159     char *header = malloc(header_len);
160     if (header == NULL) {
161         ELOG("Could not allocate memory: %s\n", strerror(errno));
162         exit(EXIT_FAILURE);
163     }
164
165     /* We first parse the fixed-length IPC-header, to know, how much data
166      * we have to expect */
167     uint32_t rec = 0;
168     while (rec < header_len) {
169         int n = read(fd, header + rec, header_len - rec);
170         if (n == -1) {
171             ELOG("read() failed: %s\n", strerror(errno));
172             exit(EXIT_FAILURE);
173         }
174         if (n == 0) {
175             /* EOF received. We try to recover a few times, because most likely
176              * i3 just restarted */
177             ELOG("EOF received, try to recover...\n");
178             destroy_connection();
179             reconnect();
180             return;
181         }
182         rec += n;
183     }
184
185     if (strncmp(header, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC))) {
186         ELOG("Wrong magic code: %.*s\n Expected: %s\n",
187              (int) strlen(I3_IPC_MAGIC),
188              header,
189              I3_IPC_MAGIC);
190         exit(EXIT_FAILURE);
191     }
192
193     char *walk = header + strlen(I3_IPC_MAGIC);
194     uint32_t size;
195     memcpy(&size, (uint32_t*)walk, sizeof(uint32_t));
196     walk += sizeof(uint32_t);
197     uint32_t type;
198     memcpy(&type, (uint32_t*)walk, sizeof(uint32_t));
199
200     /* Now that we know, what to expect, we can start read()ing the rest
201      * of the message */
202     char *buffer = malloc(size + 1);
203     if (buffer == NULL) {
204         /* EOF received. We try to recover a few times, because most likely
205          * i3 just restarted */
206         ELOG("EOF received, try to recover...\n");
207         destroy_connection();
208         reconnect();
209         return;
210     }
211     rec = 0;
212
213     while (rec < size) {
214         int n = read(fd, buffer + rec, size - rec);
215         if (n == -1) {
216             ELOG("read() failed: %s\n", strerror(errno));
217             exit(EXIT_FAILURE);
218         }
219         if (n == 0) {
220             ELOG("Nothing to read!\n");
221             exit(EXIT_FAILURE);
222         }
223         rec += n;
224     }
225     buffer[size] = '\0';
226
227     /* And call the callback (indexed by the type) */
228     if (type & (1 << 31)) {
229         type ^= 1 << 31;
230         event_handlers[type](buffer);
231     } else {
232         reply_handlers[type](buffer);
233     }
234
235     FREE(header);
236     FREE(buffer);
237 }
238
239 /*
240  * Sends a Message to i3.
241  * type must be a valid I3_IPC_MESSAGE_TYPE (see i3/ipc.h for further information)
242  *
243  */
244 int i3_send_msg(uint32_t type, const char *payload) {
245     uint32_t len = 0;
246     if (payload != NULL) {
247         len = strlen(payload);
248     }
249
250     /* We are a wellbehaved client and send a proper header first */
251     uint32_t to_write = strlen (I3_IPC_MAGIC) + sizeof(uint32_t)*2 + len;
252     /* TODO: I'm not entirely sure if this buffer really has to contain more
253      * than the pure header (why not just write() the payload from *payload?),
254      * but we leave it for now */
255     char *buffer = malloc(to_write);
256     if (buffer == NULL) {
257         ELOG("Could not allocate memory: %s\n", strerror(errno));
258         exit(EXIT_FAILURE);
259     }
260
261     char *walk = buffer;
262
263     strncpy(buffer, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC));
264     walk += strlen(I3_IPC_MAGIC);
265     memcpy(walk, &len, sizeof(uint32_t));
266     walk += sizeof(uint32_t);
267     memcpy(walk, &type, sizeof(uint32_t));
268     walk += sizeof(uint32_t);
269
270     if (payload != NULL)
271         strncpy(walk, payload, len);
272
273     uint32_t written = 0;
274
275     while (to_write > 0) {
276         int n = write(i3_connection->fd, buffer + written, to_write);
277         if (n == -1) {
278             ELOG("write() failed: %s\n", strerror(errno));
279             exit(EXIT_FAILURE);
280         }
281
282         to_write -= n;
283         written += n;
284     }
285
286     FREE(buffer);
287
288     return 1;
289 }
290
291 /*
292  * Initiate a connection to i3.
293  * socket-path must be a valid path to the ipc_socket of i3
294  *
295  */
296 int init_connection(const char *socket_path) {
297     sock_path = socket_path;
298     int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
299     if (sockfd == -1) {
300         ELOG("Could not create Socket: %s\n", strerror(errno));
301         exit(EXIT_FAILURE);
302     }
303
304     struct sockaddr_un addr;
305     memset(&addr, 0, sizeof(struct sockaddr_un));
306     addr.sun_family = AF_LOCAL;
307     strcpy(addr.sun_path, sock_path);
308     if (connect(sockfd, (const struct sockaddr*) &addr, sizeof(struct sockaddr_un)) < 0) {
309         ELOG("Could not connect to i3! %s: %s\n", sock_path, strerror(errno));
310         reconnect();
311         return 0;
312     }
313
314     i3_connection = malloc(sizeof(ev_io));
315     if (i3_connection == NULL) {
316         ELOG("malloc() failed: %s\n", strerror(errno));
317         exit(EXIT_FAILURE);
318     }
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() {
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() {
337     if (config.disable_ws) {
338         i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"output\" ]");
339     } else {
340         i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"workspace\", \"output\" ]");
341     }
342 }