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