]> git.sur5r.net Git - i3/i3/blob - i3bar/src/ipc.c
Merge branch 'fix-move-ws'
[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();
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     parse_config_json(reply);
88
89     /* Now we can actually use 'config', so let's subscribe to the appropriate
90      * events and request the workspaces if necessary. */
91     subscribe_events();
92     if (!config.disable_ws)
93         i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
94
95     /* Initialize the rest of XCB */
96     init_xcb_late(config.fontname);
97
98     /* Resolve color strings to colorpixels and save them, then free the strings. */
99     init_colors(&(config.colors));
100     free_colors(&(config.colors));
101
102     /* The name of this function is actually misleading. Even if no command is
103      * specified, this function initiates the watchers to listen on stdin and
104      * react accordingly */
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 /* Data-structure to easily call the reply-handlers later */
153 handler_t event_handlers[] = {
154     &got_workspace_event,
155     &got_output_event,
156     &got_mode_event
157 };
158
159 /*
160  * Called, when we get a message from i3
161  *
162  */
163 void got_data(struct ev_loop *loop, ev_io *watcher, int events) {
164     DLOG("Got data!\n");
165     int fd = watcher->fd;
166
167     /* First we only read the header, because we know its length */
168     uint32_t header_len = strlen(I3_IPC_MAGIC) + sizeof(uint32_t)*2;
169     char *header = smalloc(header_len);
170
171     /* We first parse the fixed-length IPC-header, to know, how much data
172      * we have to expect */
173     uint32_t rec = 0;
174     while (rec < header_len) {
175         int n = read(fd, header + rec, header_len - rec);
176         if (n == -1) {
177             ELOG("read() failed: %s\n", strerror(errno));
178             exit(EXIT_FAILURE);
179         }
180         if (n == 0) {
181             /* EOF received. Since i3 will restart i3bar instances as appropriate,
182              * we exit here. */
183             DLOG("EOF received, exiting...\n");
184             clean_xcb();
185             exit(EXIT_SUCCESS);
186         }
187         rec += n;
188     }
189
190     if (strncmp(header, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC))) {
191         ELOG("Wrong magic code: %.*s\n Expected: %s\n",
192              (int) strlen(I3_IPC_MAGIC),
193              header,
194              I3_IPC_MAGIC);
195         exit(EXIT_FAILURE);
196     }
197
198     char *walk = header + strlen(I3_IPC_MAGIC);
199     uint32_t size;
200     memcpy(&size, (uint32_t*)walk, sizeof(uint32_t));
201     walk += sizeof(uint32_t);
202     uint32_t type;
203     memcpy(&type, (uint32_t*)walk, sizeof(uint32_t));
204
205     /* Now that we know, what to expect, we can start read()ing the rest
206      * of the message */
207     char *buffer = smalloc(size + 1);
208     rec = 0;
209
210     while (rec < size) {
211         int n = read(fd, buffer + rec, size - rec);
212         if (n == -1) {
213             ELOG("read() failed: %s\n", strerror(errno));
214             exit(EXIT_FAILURE);
215         }
216         if (n == 0) {
217             ELOG("Nothing to read!\n");
218             exit(EXIT_FAILURE);
219         }
220         rec += n;
221     }
222     buffer[size] = '\0';
223
224     /* And call the callback (indexed by the type) */
225     if (type & (1 << 31)) {
226         type ^= 1 << 31;
227         event_handlers[type](buffer);
228     } else {
229         if (reply_handlers[type])
230             reply_handlers[type](buffer);
231     }
232
233     FREE(header);
234     FREE(buffer);
235 }
236
237 /*
238  * Sends a Message to i3.
239  * type must be a valid I3_IPC_MESSAGE_TYPE (see i3/ipc.h for further information)
240  *
241  */
242 int i3_send_msg(uint32_t type, const char *payload) {
243     uint32_t len = 0;
244     if (payload != NULL) {
245         len = strlen(payload);
246     }
247
248     /* We are a wellbehaved client and send a proper header first */
249     uint32_t to_write = strlen (I3_IPC_MAGIC) + sizeof(uint32_t)*2 + len;
250     /* TODO: I'm not entirely sure if this buffer really has to contain more
251      * than the pure header (why not just write() the payload from *payload?),
252      * but we leave it for now */
253     char *buffer = smalloc(to_write);
254     char *walk = buffer;
255
256     strncpy(buffer, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC));
257     walk += strlen(I3_IPC_MAGIC);
258     memcpy(walk, &len, sizeof(uint32_t));
259     walk += sizeof(uint32_t);
260     memcpy(walk, &type, sizeof(uint32_t));
261     walk += sizeof(uint32_t);
262
263     if (payload != NULL)
264         strncpy(walk, payload, len);
265
266     uint32_t written = 0;
267
268     while (to_write > 0) {
269         int n = write(i3_connection->fd, buffer + written, to_write);
270         if (n == -1) {
271             ELOG("write() failed: %s\n", strerror(errno));
272             exit(EXIT_FAILURE);
273         }
274
275         to_write -= n;
276         written += n;
277     }
278
279     FREE(buffer);
280
281     return 1;
282 }
283
284 /*
285  * Initiate a connection to i3.
286  * socket-path must be a valid path to the ipc_socket of i3
287  *
288  */
289 int init_connection(const char *socket_path) {
290     sock_path = socket_path;
291     int sockfd = ipc_connect(socket_path);
292     i3_connection = smalloc(sizeof(ev_io));
293     ev_io_init(i3_connection, &got_data, sockfd, EV_READ);
294     ev_io_start(main_loop, i3_connection);
295     return 1;
296 }
297
298 /*
299  * Destroy the connection to i3.
300  */
301 void destroy_connection(void) {
302     close(i3_connection->fd);
303     ev_io_stop(main_loop, i3_connection);
304 }
305
306 /*
307  * Subscribe to all the i3-events, we need
308  *
309  */
310 void subscribe_events(void) {
311     if (config.disable_ws) {
312         i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"output\", \"mode\" ]");
313     } else {
314         i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"workspace\", \"output\", \"mode\" ]");
315     }
316 }