diff options
Diffstat (limited to 'i3bar/src/ipc.c')
-rw-r--r-- | i3bar/src/ipc.c | 88 |
1 files changed, 61 insertions, 27 deletions
diff --git a/i3bar/src/ipc.c b/i3bar/src/ipc.c index 3ab4738c..95130209 100644 --- a/i3bar/src/ipc.c +++ b/i3bar/src/ipc.c @@ -24,14 +24,24 @@ ev_io *i3_connection; const char *sock_path; -typedef void (*handler_t)(char *); +typedef void (*handler_t)(const unsigned char *, size_t); + +/* + * Returns true when i3bar is configured to read workspace information from i3 + * via JSON over the i3 IPC interface, as opposed to reading workspace + * information from the workspace_command via JSON over stdout. + * + */ +static bool i3_provides_workspaces(void) { + return !config.disable_ws && config.workspace_command == NULL; +} /* * Called, when we get a reply to a command from i3. * Since i3 does not give us much feedback on commands, we do not much * */ -static void got_command_reply(char *reply) { +static void got_command_reply(const unsigned char *reply, size_t size) { /* TODO: Error handling for command replies */ } @@ -39,9 +49,9 @@ static void got_command_reply(char *reply) { * Called, when we get a reply with workspaces data * */ -static void got_workspace_reply(char *reply) { +static void got_workspace_reply(const unsigned char *reply, size_t size) { DLOG("Got workspace data!\n"); - parse_workspaces_json(reply); + parse_workspaces_json(reply, size); draw_bars(false); } @@ -50,7 +60,7 @@ static void got_workspace_reply(char *reply) { * Since i3 does not give us much feedback on commands, we do not much * */ -static void got_subscribe_reply(char *reply) { +static void got_subscribe_reply(const unsigned char *reply, size_t size) { DLOG("Got subscribe reply: %s\n", reply); /* TODO: Error handling for subscribe commands */ } @@ -59,12 +69,12 @@ static void got_subscribe_reply(char *reply) { * Called, when we get a reply with outputs data * */ -static void got_output_reply(char *reply) { +static void got_output_reply(const unsigned char *reply, size_t size) { DLOG("Clearing old output configuration...\n"); free_outputs(); DLOG("Parsing outputs JSON...\n"); - parse_outputs_json(reply); + parse_outputs_json(reply, size); DLOG("Reconfiguring windows...\n"); reconfig_windows(false); @@ -73,8 +83,19 @@ static void got_output_reply(char *reply) { kick_tray_clients(o_walk); } - if (!config.disable_ws) { + if (i3_provides_workspaces()) { i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL); + } else if (config.workspace_command) { + /* Communication with the workspace child is one-way. Since we called + * free_outputs() and free_workspaces() we have lost our workspace + * information which will result in no workspace buttons. A + * well-behaving client should be subscribed to output events as well + * and re-send the output information to i3bar. Even in that case + * though there is a race condition where the child can send the new + * workspace information after the output change before i3bar receives + * the output event from i3. For this reason, we re-parse the latest + * received JSON. */ + repeat_last_ws_json(); } draw_bars(false); @@ -84,10 +105,10 @@ static void got_output_reply(char *reply) { * Called when we get the configuration for our bar instance * */ -static void got_bar_config(char *reply) { +static void got_bar_config(const unsigned char *reply, size_t size) { if (!config.bar_id) { DLOG("Received bar list \"%s\"\n", reply); - parse_get_first_i3bar_config(reply); + parse_get_first_i3bar_config(reply, size); if (!config.bar_id) { ELOG("No bar configuration found, please configure a bar block in your i3 config file.\n"); @@ -106,13 +127,14 @@ static void got_bar_config(char *reply) { i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL); free_colors(&(config.colors)); - parse_config_json(reply); + parse_config_json(reply, size); /* Now we can actually use 'config', so let's subscribe to the appropriate * events and request the workspaces if necessary. */ subscribe_events(); - if (!config.disable_ws) + if (i3_provides_workspaces()) { i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL); + } /* Initialize the rest of XCB */ init_xcb_late(config.fontname); @@ -121,6 +143,7 @@ static void got_bar_config(char *reply) { init_colors(&(config.colors)); start_child(config.command); + start_ws_child(config.workspace_command); } /* Data structure to easily call the reply handlers later */ @@ -143,7 +166,7 @@ handler_t reply_handlers[] = { * Called, when a workspace event arrives (i.e. the user changed the workspace) * */ -static void got_workspace_event(char *event) { +static void got_workspace_event(const unsigned char *event, size_t size) { DLOG("Got workspace event!\n"); i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL); } @@ -152,7 +175,7 @@ static void got_workspace_event(char *event) { * Called, when an output event arrives (i.e. the screen configuration changed) * */ -static void got_output_event(char *event) { +static void got_output_event(const unsigned char *event, size_t size) { DLOG("Got output event!\n"); i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL); } @@ -161,9 +184,9 @@ static void got_output_event(char *event) { * Called, when a mode event arrives (i3 changed binding mode). * */ -static void got_mode_event(char *event) { +static void got_mode_event(const unsigned char *event, size_t size) { DLOG("Got mode event!\n"); - parse_mode_json(event); + parse_mode_json(event, size); draw_bars(false); } @@ -183,11 +206,11 @@ static bool strings_differ(char *a, char *b) { * Called, when a barconfig_update event arrives (i.e. i3 changed the bar hidden_state or mode) * */ -static void got_bar_config_update(char *event) { +static void got_bar_config_update(const unsigned char *event, size_t size) { /* check whether this affect this bar instance by checking the bar_id */ char *expected_id; sasprintf(&expected_id, "\"id\":\"%s\"", config.bar_id); - char *found_id = strstr(event, expected_id); + char *found_id = strstr((const char *)event, expected_id); FREE(expected_id); if (found_id == NULL) return; @@ -201,10 +224,12 @@ static void got_bar_config_update(char *event) { DLOG("Received bar config update \"%s\"\n", event); char *old_command = config.command; + char *old_workspace_command = config.workspace_command; config.command = NULL; + config.workspace_command = NULL; bar_display_mode_t old_mode = config.hide_on_modifier; - parse_config_json(event); + parse_config_json(event, size); if (old_mode != config.hide_on_modifier) { reconfig_windows(true); } @@ -214,13 +239,21 @@ static void got_bar_config_update(char *event) { init_colors(&(config.colors)); /* restart status command process */ - if (strings_differ(old_command, config.command)) { + if (!status_child_is_alive() || strings_differ(old_command, config.command)) { kill_child(); clear_statusline(&statusline_head, true); start_child(config.command); } free(old_command); + /* restart workspace command process */ + if (!ws_child_is_alive() || strings_differ(old_workspace_command, config.workspace_command)) { + free_workspaces(); + kill_ws_child(); + start_ws_child(config.workspace_command); + } + free(old_workspace_command); + draw_bars(false); } @@ -284,7 +317,7 @@ static void got_data(struct ev_loop *loop, ev_io *watcher, int events) { /* Now that we know, what to expect, we can start read()ing the rest * of the message */ - char *buffer = smalloc(size + 1); + unsigned char *buffer = smalloc(size + 1); rec = 0; while (rec < size) { @@ -304,10 +337,11 @@ static void got_data(struct ev_loop *loop, ev_io *watcher, int events) { /* And call the callback (indexed by the type) */ if (type & (1UL << 31)) { type ^= 1UL << 31; - event_handlers[type](buffer); + event_handlers[type](buffer, size); } else { - if (reply_handlers[type]) - reply_handlers[type](buffer); + if (reply_handlers[type]) { + reply_handlers[type](buffer, size); + } } FREE(header); @@ -377,9 +411,9 @@ void destroy_connection(void) { * */ void subscribe_events(void) { - if (config.disable_ws) { - i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"output\", \"mode\", \"barconfig_update\" ]"); - } else { + if (i3_provides_workspaces()) { i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"workspace\", \"output\", \"mode\", \"barconfig_update\" ]"); + } else { + i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"output\", \"mode\", \"barconfig_update\" ]"); } } |