2 * vim:ts=4:sw=4:expandtab
4 * i3bar - an xcb-based status- and ws-bar for i3
5 * © 2010-2012 Axel Wagner and contributors (see also: LICENSE)
7 * ipc.c: Communicating with i3
16 #include <sys/socket.h>
25 const char *sock_path;
27 typedef void (*handler_t)(char *);
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
34 void got_command_reply(char *reply) {
35 /* TODO: Error handling for command replies */
39 * Called, when we get a reply with workspaces data
42 void got_workspace_reply(char *reply) {
43 DLOG("Got workspace data!\n");
44 parse_workspaces_json(reply);
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
53 void got_subscribe_reply(char *reply) {
54 DLOG("Got subscribe reply: %s\n", reply);
55 /* TODO: Error handling for subscribe commands */
59 * Called, when we get a reply with outputs data
62 void got_output_reply(char *reply) {
63 DLOG("Parsing outputs JSON...\n");
64 parse_outputs_json(reply);
65 DLOG("Reconfiguring windows...\n");
67 reconfig_windows(false);
70 SLIST_FOREACH(o_walk, outputs, slist) {
71 kick_tray_clients(o_walk);
78 * Called when we get the configuration for our bar instance
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);
88 free_colors(&(config.colors));
89 parse_config_json(reply);
91 /* Now we can actually use 'config', so let's subscribe to the appropriate
92 * events and request the workspaces if necessary. */
94 if (!config.disable_ws)
95 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
97 /* Initialize the rest of XCB */
98 init_xcb_late(config.fontname);
100 /* Resolve color strings to colorpixels and save them, then free the strings. */
101 init_colors(&(config.colors));
103 start_child(config.command);
104 FREE(config.command);
107 /* Data structure to easily call the reply handlers later */
108 handler_t reply_handlers[] = {
110 &got_workspace_reply,
111 &got_subscribe_reply,
119 * Called, when a workspace event arrives (i.e. the user changed the workspace)
122 void got_workspace_event(char *event) {
123 DLOG("Got workspace event!\n");
124 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
128 * Called, when an output event arrives (i.e. the screen configuration changed)
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);
140 * Called, when a mode event arrives (i3 changed binding mode).
143 void got_mode_event(char *event) {
144 DLOG("Got mode event!\n");
145 parse_mode_json(event);
150 * Called, when a barconfig_update event arrives (i.e. i3 changed the bar hidden_state or mode)
153 void got_bar_config_update(char *event) {
154 /* check whether this affect this bar instance by checking the bar_id */
156 sasprintf(&expected_id, "\"id\":\"%s\"", config.bar_id);
157 char *found_id = strstr(event, expected_id);
159 if (found_id == NULL)
162 /* reconfigure the bar based on the current outputs */
163 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
165 free_colors(&(config.colors));
167 /* update the configuration with the received settings */
168 DLOG("Received bar config update \"%s\"\n", event);
169 bar_display_mode_t old_mode = config.hide_on_modifier;
170 parse_config_json(event);
171 if (old_mode != config.hide_on_modifier) {
172 reconfig_windows(true);
175 /* update fonts and colors */
176 init_xcb_late(config.fontname);
177 init_colors(&(config.colors));
183 /* Data structure to easily call the event handlers later */
184 handler_t event_handlers[] = {
185 &got_workspace_event,
189 &got_bar_config_update,
193 * Called, when we get a message from i3
196 void got_data(struct ev_loop *loop, ev_io *watcher, int events) {
198 int fd = watcher->fd;
200 /* First we only read the header, because we know its length */
201 uint32_t header_len = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) * 2;
202 char *header = smalloc(header_len);
204 /* We first parse the fixed-length IPC header, to know, how much data
205 * we have to expect */
207 while (rec < header_len) {
208 int n = read(fd, header + rec, header_len - rec);
210 ELOG("read() failed: %s\n", strerror(errno));
214 /* EOF received. Since i3 will restart i3bar instances as appropriate,
216 DLOG("EOF received, exiting...\n");
223 if (strncmp(header, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC))) {
224 ELOG("Wrong magic code: %.*s\n Expected: %s\n",
225 (int)strlen(I3_IPC_MAGIC),
231 char *walk = header + strlen(I3_IPC_MAGIC);
233 memcpy(&size, (uint32_t *)walk, sizeof(uint32_t));
234 walk += sizeof(uint32_t);
236 memcpy(&type, (uint32_t *)walk, sizeof(uint32_t));
238 /* Now that we know, what to expect, we can start read()ing the rest
240 char *buffer = smalloc(size + 1);
244 int n = read(fd, buffer + rec, size - rec);
246 ELOG("read() failed: %s\n", strerror(errno));
250 ELOG("Nothing to read!\n");
257 /* And call the callback (indexed by the type) */
258 if (type & (1 << 31)) {
260 event_handlers[type](buffer);
262 if (reply_handlers[type])
263 reply_handlers[type](buffer);
271 * Sends a message to i3.
272 * type must be a valid I3_IPC_MESSAGE_TYPE (see i3/ipc.h for further information)
275 int i3_send_msg(uint32_t type, const char *payload) {
277 if (payload != NULL) {
278 len = strlen(payload);
281 /* We are a wellbehaved client and send a proper header first */
282 uint32_t to_write = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) * 2 + len;
283 /* TODO: I'm not entirely sure if this buffer really has to contain more
284 * than the pure header (why not just write() the payload from *payload?),
285 * but we leave it for now */
286 char *buffer = smalloc(to_write);
289 strncpy(buffer, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC));
290 walk += strlen(I3_IPC_MAGIC);
291 memcpy(walk, &len, sizeof(uint32_t));
292 walk += sizeof(uint32_t);
293 memcpy(walk, &type, sizeof(uint32_t));
294 walk += sizeof(uint32_t);
297 strncpy(walk, payload, len);
299 uint32_t written = 0;
301 while (to_write > 0) {
302 int n = write(i3_connection->fd, buffer + written, to_write);
304 ELOG("write() failed: %s\n", strerror(errno));
318 * Initiate a connection to i3.
319 * socket_path must be a valid path to the ipc_socket of i3
322 int init_connection(const char *socket_path) {
323 sock_path = socket_path;
324 int sockfd = ipc_connect(socket_path);
325 i3_connection = smalloc(sizeof(ev_io));
326 ev_io_init(i3_connection, &got_data, sockfd, EV_READ);
327 ev_io_start(main_loop, i3_connection);
332 * Destroy the connection to i3.
334 void destroy_connection(void) {
335 close(i3_connection->fd);
336 ev_io_stop(main_loop, i3_connection);
340 * Subscribe to all the i3-events, we need
343 void subscribe_events(void) {
344 if (config.disable_ws) {
345 i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"output\", \"mode\", \"barconfig_update\" ]");
347 i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"workspace\", \"output\", \"mode\", \"barconfig_update\" ]");