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