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
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");
66 reconfig_windows(false);
69 SLIST_FOREACH(o_walk, outputs, slist) {
70 kick_tray_clients(o_walk);
77 * Called when we get the configuration for our bar instance
80 void got_bar_config(char *reply) {
81 DLOG("Received bar config \"%s\"\n", reply);
82 /* We initiate the main function by requesting infos about the outputs and
83 * workspaces. Everything else (creating the bars, showing the right workspace-
84 * buttons and more) is taken care of by the event-drivenness of the code */
85 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
87 free_colors(&(config.colors));
88 parse_config_json(reply);
90 /* Now we can actually use 'config', so let's subscribe to the appropriate
91 * events and request the workspaces if necessary. */
93 if (!config.disable_ws)
94 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
96 /* Initialize the rest of XCB */
97 init_xcb_late(config.fontname);
99 /* Resolve color strings to colorpixels and save them, then free the strings. */
100 init_colors(&(config.colors));
102 start_child(config.command);
103 FREE(config.command);
106 /* Data structure to easily call the reply handlers later */
107 handler_t reply_handlers[] = {
109 &got_workspace_reply,
110 &got_subscribe_reply,
118 * Called, when a workspace event arrives (i.e. the user changed the workspace)
121 void got_workspace_event(char *event) {
122 DLOG("Got workspace event!\n");
123 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
127 * Called, when an output event arrives (i.e. the screen configuration changed)
130 void got_output_event(char *event) {
131 DLOG("Got output event!\n");
132 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
133 if (!config.disable_ws) {
134 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
139 * Called, when a mode event arrives (i3 changed binding mode).
142 void got_mode_event(char *event) {
143 DLOG("Got mode event!\n");
144 parse_mode_json(event);
149 * Called, when a barconfig_update event arrives (i.e. i3 changed the bar hidden_state or mode)
152 void got_bar_config_update(char *event) {
153 /* check whether this affect this bar instance by checking the bar_id */
155 sasprintf(&expected_id, "\"id\":\"%s\"", config.bar_id);
156 char *found_id = strstr(event, expected_id);
158 if (found_id == NULL)
161 /* reconfigure the bar based on the current outputs */
162 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
164 free_colors(&(config.colors));
166 /* update the configuration with the received settings */
167 DLOG("Received bar config update \"%s\"\n", event);
168 bar_display_mode_t old_mode = config.hide_on_modifier;
169 parse_config_json(event);
170 if (old_mode != config.hide_on_modifier) {
171 reconfig_windows(true);
174 /* update fonts and colors */
175 init_xcb_late(config.fontname);
176 init_colors(&(config.colors));
181 /* Data structure to easily call the event handlers later */
182 handler_t event_handlers[] = {
183 &got_workspace_event,
187 &got_bar_config_update,
191 * Called, when we get a message from i3
194 void got_data(struct ev_loop *loop, ev_io *watcher, int events) {
196 int fd = watcher->fd;
198 /* First we only read the header, because we know its length */
199 uint32_t header_len = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) * 2;
200 char *header = smalloc(header_len);
202 /* We first parse the fixed-length IPC header, to know, how much data
203 * we have to expect */
205 while (rec < header_len) {
206 int n = read(fd, header + rec, header_len - rec);
208 ELOG("read() failed: %s\n", strerror(errno));
212 /* EOF received. Since i3 will restart i3bar instances as appropriate,
214 DLOG("EOF received, exiting...\n");
221 if (strncmp(header, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC))) {
222 ELOG("Wrong magic code: %.*s\n Expected: %s\n",
223 (int)strlen(I3_IPC_MAGIC),
229 char *walk = header + strlen(I3_IPC_MAGIC);
231 memcpy(&size, (uint32_t *)walk, sizeof(uint32_t));
232 walk += sizeof(uint32_t);
234 memcpy(&type, (uint32_t *)walk, sizeof(uint32_t));
236 /* Now that we know, what to expect, we can start read()ing the rest
238 char *buffer = smalloc(size + 1);
242 int n = read(fd, buffer + rec, size - rec);
244 ELOG("read() failed: %s\n", strerror(errno));
248 ELOG("Nothing to read!\n");
255 /* And call the callback (indexed by the type) */
256 if (type & (1 << 31)) {
258 event_handlers[type](buffer);
260 if (reply_handlers[type])
261 reply_handlers[type](buffer);
269 * Sends a message to i3.
270 * type must be a valid I3_IPC_MESSAGE_TYPE (see i3/ipc.h for further information)
273 int i3_send_msg(uint32_t type, const char *payload) {
275 if (payload != NULL) {
276 len = strlen(payload);
279 /* We are a wellbehaved client and send a proper header first */
280 uint32_t to_write = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) * 2 + len;
281 /* TODO: I'm not entirely sure if this buffer really has to contain more
282 * than the pure header (why not just write() the payload from *payload?),
283 * but we leave it for now */
284 char *buffer = smalloc(to_write);
287 strncpy(buffer, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC));
288 walk += strlen(I3_IPC_MAGIC);
289 memcpy(walk, &len, sizeof(uint32_t));
290 walk += sizeof(uint32_t);
291 memcpy(walk, &type, sizeof(uint32_t));
292 walk += sizeof(uint32_t);
295 strncpy(walk, payload, len);
297 swrite(i3_connection->fd, buffer, to_write);
305 * Initiate a connection to i3.
306 * socket_path must be a valid path to the ipc_socket of i3
309 int init_connection(const char *socket_path) {
310 sock_path = socket_path;
311 int sockfd = ipc_connect(socket_path);
312 i3_connection = smalloc(sizeof(ev_io));
313 ev_io_init(i3_connection, &got_data, sockfd, EV_READ);
314 ev_io_start(main_loop, i3_connection);
319 * Destroy the connection to i3.
321 void destroy_connection(void) {
322 close(i3_connection->fd);
323 ev_io_stop(main_loop, i3_connection);
327 * Subscribe to all the i3-events, we need
330 void subscribe_events(void) {
331 if (config.disable_ws) {
332 i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"output\", \"mode\", \"barconfig_update\" ]");
334 i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"workspace\", \"output\", \"mode\", \"barconfig_update\" ]");