2 * vim:ts=4:sw=4:expandtab
4 * i3bar - an xcb-based status- and ws-bar for i3
5 * © 2010 Axel Wagner and contributors (see also: LICENSE)
7 * ipc.c: Communicating with i3
18 #include <sys/socket.h>
22 #ifdef I3_ASAN_ENABLED
23 #include <sanitizer/lsan_interface.h>
28 const char *sock_path;
30 typedef void (*handler_t)(char *);
33 * Called, when we get a reply to a command from i3.
34 * Since i3 does not give us much feedback on commands, we do not much
37 void got_command_reply(char *reply) {
38 /* TODO: Error handling for command replies */
42 * Called, when we get a reply with workspaces data
45 void got_workspace_reply(char *reply) {
46 DLOG("Got workspace data!\n");
47 parse_workspaces_json(reply);
52 * Called, when we get a reply for a subscription.
53 * Since i3 does not give us much feedback on commands, we do not much
56 void got_subscribe_reply(char *reply) {
57 DLOG("Got subscribe reply: %s\n", reply);
58 /* TODO: Error handling for subscribe commands */
62 * Called, when we get a reply with outputs data
65 void got_output_reply(char *reply) {
66 DLOG("Clearing old output configuration...\n");
68 SLIST_FOREACH(o_walk, outputs, slist) {
69 destroy_window(o_walk);
71 FREE_SLIST(outputs, i3_output);
73 DLOG("Parsing outputs JSON...\n");
74 parse_outputs_json(reply);
75 DLOG("Reconfiguring windows...\n");
76 reconfig_windows(false);
78 SLIST_FOREACH(o_walk, outputs, slist) {
79 kick_tray_clients(o_walk);
82 if (!config.disable_ws) {
83 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
90 * Called when we get the configuration for our bar instance
93 void got_bar_config(char *reply) {
94 DLOG("Received bar config \"%s\"\n", reply);
95 /* We initiate the main function by requesting infos about the outputs and
96 * workspaces. Everything else (creating the bars, showing the right workspace-
97 * buttons and more) is taken care of by the event-drivenness of the code */
98 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
100 free_colors(&(config.colors));
101 parse_config_json(reply);
103 /* Now we can actually use 'config', so let's subscribe to the appropriate
104 * events and request the workspaces if necessary. */
106 if (!config.disable_ws)
107 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
109 /* Initialize the rest of XCB */
110 init_xcb_late(config.fontname);
112 /* Resolve color strings to colorpixels and save them, then free the strings. */
113 init_colors(&(config.colors));
115 start_child(config.command);
116 FREE(config.command);
119 /* Data structure to easily call the reply handlers later */
120 handler_t reply_handlers[] = {
122 &got_workspace_reply,
123 &got_subscribe_reply,
131 * Called, when a workspace event arrives (i.e. the user changed the workspace)
134 void got_workspace_event(char *event) {
135 DLOG("Got workspace event!\n");
136 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
140 * Called, when an output event arrives (i.e. the screen configuration changed)
143 void got_output_event(char *event) {
144 DLOG("Got output event!\n");
145 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
146 if (!config.disable_ws) {
147 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
152 * Called, when a mode event arrives (i3 changed binding mode).
155 void got_mode_event(char *event) {
156 DLOG("Got mode event!\n");
157 parse_mode_json(event);
162 * Called, when a barconfig_update event arrives (i.e. i3 changed the bar hidden_state or mode)
165 void got_bar_config_update(char *event) {
166 /* check whether this affect this bar instance by checking the bar_id */
168 sasprintf(&expected_id, "\"id\":\"%s\"", config.bar_id);
169 char *found_id = strstr(event, expected_id);
171 if (found_id == NULL)
174 /* reconfigure the bar based on the current outputs */
175 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
177 free_colors(&(config.colors));
179 /* update the configuration with the received settings */
180 DLOG("Received bar config update \"%s\"\n", event);
181 bar_display_mode_t old_mode = config.hide_on_modifier;
182 parse_config_json(event);
183 if (old_mode != config.hide_on_modifier) {
184 reconfig_windows(true);
187 /* update fonts and colors */
188 init_xcb_late(config.fontname);
189 init_colors(&(config.colors));
191 /* restart status command process */
193 start_child(config.command);
194 FREE(config.command);
199 /* Data structure to easily call the event handlers later */
200 handler_t event_handlers[] = {
201 &got_workspace_event,
205 &got_bar_config_update,
209 * Called, when we get a message from i3
212 void got_data(struct ev_loop *loop, ev_io *watcher, int events) {
214 int fd = watcher->fd;
216 /* First we only read the header, because we know its length */
217 uint32_t header_len = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) * 2;
218 char *header = smalloc(header_len);
220 /* We first parse the fixed-length IPC header, to know, how much data
221 * we have to expect */
223 while (rec < header_len) {
224 int n = read(fd, header + rec, header_len - rec);
226 ELOG("read() failed: %s\n", strerror(errno));
230 /* EOF received. Since i3 will restart i3bar instances as appropriate,
232 DLOG("EOF received, exiting...\n");
233 #ifdef I3_ASAN_ENABLED
234 __lsan_do_leak_check();
242 if (strncmp(header, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC))) {
243 ELOG("Wrong magic code: %.*s\n Expected: %s\n",
244 (int)strlen(I3_IPC_MAGIC),
250 char *walk = header + strlen(I3_IPC_MAGIC);
252 memcpy(&size, (uint32_t *)walk, sizeof(uint32_t));
253 walk += sizeof(uint32_t);
255 memcpy(&type, (uint32_t *)walk, sizeof(uint32_t));
257 /* Now that we know, what to expect, we can start read()ing the rest
259 char *buffer = smalloc(size + 1);
263 int n = read(fd, buffer + rec, size - rec);
265 ELOG("read() failed: %s\n", strerror(errno));
269 ELOG("Nothing to read!\n");
276 /* And call the callback (indexed by the type) */
277 if (type & (1 << 31)) {
279 event_handlers[type](buffer);
281 if (reply_handlers[type])
282 reply_handlers[type](buffer);
290 * Sends a message to i3.
291 * type must be a valid I3_IPC_MESSAGE_TYPE (see i3/ipc.h for further information)
294 int i3_send_msg(uint32_t type, const char *payload) {
296 if (payload != NULL) {
297 len = strlen(payload);
300 /* We are a wellbehaved client and send a proper header first */
301 uint32_t to_write = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) * 2 + len;
302 /* TODO: I'm not entirely sure if this buffer really has to contain more
303 * than the pure header (why not just write() the payload from *payload?),
304 * but we leave it for now */
305 char *buffer = smalloc(to_write);
308 strncpy(buffer, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC));
309 walk += strlen(I3_IPC_MAGIC);
310 memcpy(walk, &len, sizeof(uint32_t));
311 walk += sizeof(uint32_t);
312 memcpy(walk, &type, sizeof(uint32_t));
313 walk += sizeof(uint32_t);
316 strncpy(walk, payload, len);
318 swrite(i3_connection->fd, buffer, to_write);
326 * Initiate a connection to i3.
327 * socket_path must be a valid path to the ipc_socket of i3
330 int init_connection(const char *socket_path) {
331 sock_path = socket_path;
332 int sockfd = ipc_connect(socket_path);
333 i3_connection = smalloc(sizeof(ev_io));
334 ev_io_init(i3_connection, &got_data, sockfd, EV_READ);
335 ev_io_start(main_loop, i3_connection);
340 * Destroy the connection to i3.
342 void destroy_connection(void) {
343 close(i3_connection->fd);
344 ev_io_stop(main_loop, i3_connection);
348 * Subscribe to all the i3-events, we need
351 void subscribe_events(void) {
352 if (config.disable_ws) {
353 i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"output\", \"mode\", \"barconfig_update\" ]");
355 i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"workspace\", \"output\", \"mode\", \"barconfig_update\" ]");