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