| 1 |
/* |
| 2 |
* vim:ts=4:sw=4:expandtab |
| 3 |
* |
| 4 |
* i3bar - an xcb-based status- and ws-bar for i3 |
| 5 |
* © 2010 Axel Wagner and contributors (see also: LICENSE) |
| 6 |
* |
| 7 |
* ipc.c: Communicating with i3 |
| 8 |
* |
| 9 |
*/ |
| 10 |
#include "common.h" |
| 11 |
|
| 12 |
#include <errno.h> |
| 13 |
#include <ev.h> |
| 14 |
#include <i3/ipc.h> |
| 15 |
#include <stdint.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#include <string.h> |
| 18 |
#include <unistd.h> |
| 19 |
#ifdef I3_ASAN_ENABLED |
| 20 |
#include <sanitizer/lsan_interface.h> |
| 21 |
#endif |
| 22 |
|
| 23 |
ev_io *i3_connection; |
| 24 |
|
| 25 |
const char *sock_path; |
| 26 |
|
| 27 |
typedef void (*handler_t)(const unsigned char *, size_t); |
| 28 |
|
| 29 |
/* |
| 30 |
* Returns true when i3bar is configured to read workspace information from i3 |
| 31 |
* via JSON over the i3 IPC interface, as opposed to reading workspace |
| 32 |
* information from the workspace_command via JSON over stdout. |
| 33 |
* |
| 34 |
*/ |
| 35 |
static bool i3_provides_workspaces(void) { |
| 36 |
return !config.disable_ws && config.workspace_command == NULL; |
| 37 |
} |
| 38 |
|
| 39 |
/* |
| 40 |
* Called, when we get a reply to a command from i3. |
| 41 |
* Since i3 does not give us much feedback on commands, we do not much |
| 42 |
* |
| 43 |
*/ |
| 44 |
static void got_command_reply(const unsigned char *reply, size_t size) { |
| 45 |
/* TODO: Error handling for command replies */ |
| 46 |
} |
| 47 |
|
| 48 |
/* |
| 49 |
* Called, when we get a reply with workspaces data |
| 50 |
* |
| 51 |
*/ |
| 52 |
static void got_workspace_reply(const unsigned char *reply, size_t size) { |
| 53 |
DLOG("Got workspace data!\n"); |
| 54 |
parse_workspaces_json(reply, size); |
| 55 |
draw_bars(false); |
| 56 |
} |
| 57 |
|
| 58 |
/* |
| 59 |
* Called, when we get a reply for a subscription. |
| 60 |
* Since i3 does not give us much feedback on commands, we do not much |
| 61 |
* |
| 62 |
*/ |
| 63 |
static void got_subscribe_reply(const unsigned char *reply, size_t size) { |
| 64 |
DLOG("Got subscribe reply: %s\n", reply); |
| 65 |
/* TODO: Error handling for subscribe commands */ |
| 66 |
} |
| 67 |
|
| 68 |
/* |
| 69 |
* Called, when we get a reply with outputs data |
| 70 |
* |
| 71 |
*/ |
| 72 |
static void got_output_reply(const unsigned char *reply, size_t size) { |
| 73 |
DLOG("Clearing old output configuration...\n"); |
| 74 |
free_outputs(); |
| 75 |
|
| 76 |
DLOG("Parsing outputs JSON...\n"); |
| 77 |
parse_outputs_json(reply, size); |
| 78 |
DLOG("Reconfiguring windows...\n"); |
| 79 |
reconfig_windows(false); |
| 80 |
|
| 81 |
i3_output *o_walk; |
| 82 |
SLIST_FOREACH (o_walk, outputs, slist) { |
| 83 |
kick_tray_clients(o_walk); |
| 84 |
} |
| 85 |
|
| 86 |
if (i3_provides_workspaces()) { |
| 87 |
i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL); |
| 88 |
} else if (config.workspace_command) { |
| 89 |
/* Communication with the workspace child is one-way. Since we called |
| 90 |
* free_outputs() and free_workspaces() we have lost our workspace |
| 91 |
* information which will result in no workspace buttons. A |
| 92 |
* well-behaving client should be subscribed to output events as well |
| 93 |
* and re-send the output information to i3bar. Even in that case |
| 94 |
* though there is a race condition where the child can send the new |
| 95 |
* workspace information after the output change before i3bar receives |
| 96 |
* the output event from i3. For this reason, we re-parse the latest |
| 97 |
* received JSON. */ |
| 98 |
repeat_last_ws_json(); |
| 99 |
} |
| 100 |
|
| 101 |
draw_bars(false); |
| 102 |
} |
| 103 |
|
| 104 |
/* |
| 105 |
* Called when we get the configuration for our bar instance |
| 106 |
* |
| 107 |
*/ |
| 108 |
static void got_bar_config(const unsigned char *reply, size_t size) { |
| 109 |
if (!config.bar_id) { |
| 110 |
DLOG("Received bar list \"%s\"\n", reply); |
| 111 |
parse_get_first_i3bar_config(reply, size); |
| 112 |
|
| 113 |
if (!config.bar_id) { |
| 114 |
ELOG("No bar configuration found, please configure a bar block in your i3 config file.\n"); |
| 115 |
exit(EXIT_FAILURE); |
| 116 |
} |
| 117 |
|
| 118 |
LOG("Using first bar config: %s. Use --bar_id to manually select a different bar configuration.\n", config.bar_id); |
| 119 |
i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_BAR_CONFIG, config.bar_id); |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
DLOG("Received bar config \"%s\"\n", reply); |
| 124 |
/* We initiate the main function by requesting infos about the outputs and |
| 125 |
* workspaces. Everything else (creating the bars, showing the right workspace- |
| 126 |
* buttons and more) is taken care of by the event-drivenness of the code */ |
| 127 |
i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL); |
| 128 |
|
| 129 |
free_colors(&(config.colors)); |
| 130 |
parse_config_json(reply, size); |
| 131 |
|
| 132 |
/* Now we can actually use 'config', so let's subscribe to the appropriate |
| 133 |
* events and request the workspaces if necessary. */ |
| 134 |
subscribe_events(); |
| 135 |
if (i3_provides_workspaces()) { |
| 136 |
i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL); |
| 137 |
} |
| 138 |
|
| 139 |
/* Initialize the rest of XCB */ |
| 140 |
init_xcb_late(config.fontname); |
| 141 |
|
| 142 |
/* Resolve color strings to colorpixels and save them, then free the strings. */ |
| 143 |
init_colors(&(config.colors)); |
| 144 |
|
| 145 |
start_child(config.command); |
| 146 |
start_ws_child(config.workspace_command); |
| 147 |
} |
| 148 |
|
| 149 |
/* Data structure to easily call the reply handlers later */ |
| 150 |
handler_t reply_handlers[] = { |
| 151 |
&got_command_reply, /* I3_IPC_REPLY_TYPE_COMMAND */ |
| 152 |
&got_workspace_reply, /* I3_IPC_REPLY_TYPE_WORKSPACES */ |
| 153 |
&got_subscribe_reply, /* I3_IPC_REPLY_TYPE_SUBSCRIBE */ |
| 154 |
&got_output_reply, /* I3_IPC_REPLY_TYPE_OUTPUTS */ |
| 155 |
NULL, /* I3_IPC_REPLY_TYPE_TREE */ |
| 156 |
NULL, /* I3_IPC_REPLY_TYPE_MARKS */ |
| 157 |
&got_bar_config, /* I3_IPC_REPLY_TYPE_BAR_CONFIG */ |
| 158 |
NULL, /* I3_IPC_REPLY_TYPE_VERSION */ |
| 159 |
NULL, /* I3_IPC_REPLY_TYPE_BINDING_MODES */ |
| 160 |
NULL, /* I3_IPC_REPLY_TYPE_CONFIG */ |
| 161 |
NULL, /* I3_IPC_REPLY_TYPE_TICK */ |
| 162 |
NULL, /* I3_IPC_REPLY_TYPE_SYNC */ |
| 163 |
}; |
| 164 |
|
| 165 |
/* |
| 166 |
* Called, when a workspace event arrives (i.e. the user changed the workspace) |
| 167 |
* |
| 168 |
*/ |
| 169 |
static void got_workspace_event(const unsigned char *event, size_t size) { |
| 170 |
DLOG("Got workspace event!\n"); |
| 171 |
i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL); |
| 172 |
} |
| 173 |
|
| 174 |
/* |
| 175 |
* Called, when an output event arrives (i.e. the screen configuration changed) |
| 176 |
* |
| 177 |
*/ |
| 178 |
static void got_output_event(const unsigned char *event, size_t size) { |
| 179 |
DLOG("Got output event!\n"); |
| 180 |
i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL); |
| 181 |
} |
| 182 |
|
| 183 |
/* |
| 184 |
* Called, when a mode event arrives (i3 changed binding mode). |
| 185 |
* |
| 186 |
*/ |
| 187 |
static void got_mode_event(const unsigned char *event, size_t size) { |
| 188 |
DLOG("Got mode event!\n"); |
| 189 |
parse_mode_json(event, size); |
| 190 |
draw_bars(false); |
| 191 |
} |
| 192 |
|
| 193 |
static bool strings_differ(char *a, char *b) { |
| 194 |
const bool a_null = (a == NULL); |
| 195 |
const bool b_null = (b == NULL); |
| 196 |
if (a_null != b_null) { |
| 197 |
return true; |
| 198 |
} |
| 199 |
if (a_null && b_null) { |
| 200 |
return false; |
| 201 |
} |
| 202 |
return strcmp(a, b) != 0; |
| 203 |
} |
| 204 |
|
| 205 |
/* |
| 206 |
* Called, when a barconfig_update event arrives (i.e. i3 changed the bar hidden_state or mode) |
| 207 |
* |
| 208 |
*/ |
| 209 |
static void got_bar_config_update(const unsigned char *event, size_t size) { |
| 210 |
/* check whether this affect this bar instance by checking the bar_id */ |
| 211 |
char *expected_id; |
| 212 |
sasprintf(&expected_id, "\"id\":\"%s\"", config.bar_id); |
| 213 |
char *found_id = strstr((const char *)event, expected_id); |
| 214 |
FREE(expected_id); |
| 215 |
if (found_id == NULL) { |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
/* reconfigure the bar based on the current outputs */ |
| 220 |
i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL); |
| 221 |
|
| 222 |
free_colors(&(config.colors)); |
| 223 |
|
| 224 |
/* update the configuration with the received settings */ |
| 225 |
DLOG("Received bar config update \"%s\"\n", event); |
| 226 |
|
| 227 |
char *old_command = config.command; |
| 228 |
char *old_workspace_command = config.workspace_command; |
| 229 |
config.command = NULL; |
| 230 |
config.workspace_command = NULL; |
| 231 |
bar_display_mode_t old_mode = config.hide_on_modifier; |
| 232 |
|
| 233 |
parse_config_json(event, size); |
| 234 |
if (old_mode != config.hide_on_modifier) { |
| 235 |
reconfig_windows(true); |
| 236 |
} |
| 237 |
|
| 238 |
/* update fonts and colors */ |
| 239 |
init_xcb_late(config.fontname); |
| 240 |
init_colors(&(config.colors)); |
| 241 |
|
| 242 |
/* restart status command process */ |
| 243 |
if (!status_child_is_alive() || strings_differ(old_command, config.command)) { |
| 244 |
kill_child(); |
| 245 |
clear_statusline(&statusline_head, true); |
| 246 |
start_child(config.command); |
| 247 |
} |
| 248 |
free(old_command); |
| 249 |
|
| 250 |
/* restart workspace command process */ |
| 251 |
if (!ws_child_is_alive() || strings_differ(old_workspace_command, config.workspace_command)) { |
| 252 |
free_workspaces(); |
| 253 |
kill_ws_child(); |
| 254 |
start_ws_child(config.workspace_command); |
| 255 |
} |
| 256 |
free(old_workspace_command); |
| 257 |
|
| 258 |
draw_bars(false); |
| 259 |
} |
| 260 |
|
| 261 |
/* Data structure to easily call the event handlers later */ |
| 262 |
handler_t event_handlers[] = { |
| 263 |
&got_workspace_event, |
| 264 |
&got_output_event, |
| 265 |
&got_mode_event, |
| 266 |
NULL, |
| 267 |
&got_bar_config_update, |
| 268 |
}; |
| 269 |
|
| 270 |
/* |
| 271 |
* Called, when we get a message from i3 |
| 272 |
* |
| 273 |
*/ |
| 274 |
static void got_data(struct ev_loop *loop, ev_io *watcher, int events) { |
| 275 |
DLOG("Got data!\n"); |
| 276 |
int fd = watcher->fd; |
| 277 |
|
| 278 |
/* First we only read the header, because we know its length */ |
| 279 |
uint32_t header_len = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) * 2; |
| 280 |
char *header = smalloc(header_len); |
| 281 |
|
| 282 |
/* We first parse the fixed-length IPC header, to know, how much data |
| 283 |
* we have to expect */ |
| 284 |
uint32_t rec = 0; |
| 285 |
while (rec < header_len) { |
| 286 |
const int n = read(fd, header + rec, header_len - rec); |
| 287 |
if (n == -1) { |
| 288 |
ELOG("read() failed: %s\n", strerror(errno)); |
| 289 |
exit(EXIT_FAILURE); |
| 290 |
} |
| 291 |
if (n == 0) { |
| 292 |
/* EOF received. Since i3 will restart i3bar instances as appropriate, |
| 293 |
* we exit here. */ |
| 294 |
DLOG("EOF received, exiting...\n"); |
| 295 |
clean_xcb(); |
| 296 |
free(header); |
| 297 |
#ifdef I3_ASAN_ENABLED |
| 298 |
__lsan_do_leak_check(); |
| 299 |
#endif |
| 300 |
exit(EXIT_SUCCESS); |
| 301 |
} |
| 302 |
rec += n; |
| 303 |
} |
| 304 |
|
| 305 |
if (strncmp(header, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC))) { |
| 306 |
ELOG("Wrong magic code: %.*s\n Expected: %s\n", |
| 307 |
(int)strlen(I3_IPC_MAGIC), |
| 308 |
header, |
| 309 |
I3_IPC_MAGIC); |
| 310 |
exit(EXIT_FAILURE); |
| 311 |
} |
| 312 |
|
| 313 |
char *walk = header + strlen(I3_IPC_MAGIC); |
| 314 |
uint32_t size; |
| 315 |
memcpy(&size, (uint32_t *)walk, sizeof(uint32_t)); |
| 316 |
walk += sizeof(uint32_t); |
| 317 |
uint32_t type; |
| 318 |
memcpy(&type, (uint32_t *)walk, sizeof(uint32_t)); |
| 319 |
|
| 320 |
/* Now that we know, what to expect, we can start read()ing the rest |
| 321 |
* of the message */ |
| 322 |
unsigned char *buffer = smalloc(size + 1); |
| 323 |
rec = 0; |
| 324 |
|
| 325 |
while (rec < size) { |
| 326 |
int n = read(fd, buffer + rec, size - rec); |
| 327 |
if (n == -1) { |
| 328 |
ELOG("read() failed: %s\n", strerror(errno)); |
| 329 |
exit(EXIT_FAILURE); |
| 330 |
} |
| 331 |
if (n == 0) { |
| 332 |
ELOG("Nothing to read!\n"); |
| 333 |
exit(EXIT_FAILURE); |
| 334 |
} |
| 335 |
rec += n; |
| 336 |
} |
| 337 |
buffer[size] = '\0'; |
| 338 |
|
| 339 |
/* And call the callback (indexed by the type) */ |
| 340 |
if (type & (1UL << 31)) { |
| 341 |
type ^= 1UL << 31; |
| 342 |
event_handlers[type](buffer, size); |
| 343 |
} else { |
| 344 |
if (reply_handlers[type]) { |
| 345 |
reply_handlers[type](buffer, size); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
FREE(header); |
| 350 |
FREE(buffer); |
| 351 |
} |
| 352 |
|
| 353 |
/* |
| 354 |
* Sends a message to i3. |
| 355 |
* type must be a valid I3_IPC_MESSAGE_TYPE (see i3/ipc.h for further information) |
| 356 |
* |
| 357 |
*/ |
| 358 |
int i3_send_msg(uint32_t type, const char *payload) { |
| 359 |
uint32_t len = 0; |
| 360 |
if (payload != NULL) { |
| 361 |
len = strlen(payload); |
| 362 |
} |
| 363 |
|
| 364 |
/* We are a wellbehaved client and send a proper header first */ |
| 365 |
uint32_t to_write = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) * 2 + len; |
| 366 |
/* TODO: I'm not entirely sure if this buffer really has to contain more |
| 367 |
* than the pure header (why not just write() the payload from *payload?), |
| 368 |
* but we leave it for now */ |
| 369 |
char *buffer = smalloc(to_write); |
| 370 |
char *walk = buffer; |
| 371 |
|
| 372 |
memcpy(buffer, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)); |
| 373 |
walk += strlen(I3_IPC_MAGIC); |
| 374 |
memcpy(walk, &len, sizeof(uint32_t)); |
| 375 |
walk += sizeof(uint32_t); |
| 376 |
memcpy(walk, &type, sizeof(uint32_t)); |
| 377 |
walk += sizeof(uint32_t); |
| 378 |
|
| 379 |
if (payload != NULL) { |
| 380 |
memcpy(walk, payload, len); |
| 381 |
} |
| 382 |
|
| 383 |
swrite(i3_connection->fd, buffer, to_write); |
| 384 |
|
| 385 |
FREE(buffer); |
| 386 |
|
| 387 |
return 1; |
| 388 |
} |
| 389 |
|
| 390 |
/* |
| 391 |
* Initiate a connection to i3. |
| 392 |
* socket_path must be a valid path to the ipc_socket of i3 |
| 393 |
* |
| 394 |
*/ |
| 395 |
void init_connection(const char *socket_path) { |
| 396 |
sock_path = socket_path; |
| 397 |
int sockfd = ipc_connect(socket_path); |
| 398 |
i3_connection = smalloc(sizeof(ev_io)); |
| 399 |
ev_io_init(i3_connection, &got_data, sockfd, EV_READ); |
| 400 |
ev_io_start(main_loop, i3_connection); |
| 401 |
} |
| 402 |
|
| 403 |
/* |
| 404 |
* Destroy the connection to i3. |
| 405 |
*/ |
| 406 |
void destroy_connection(void) { |
| 407 |
close(i3_connection->fd); |
| 408 |
ev_io_stop(main_loop, i3_connection); |
| 409 |
} |
| 410 |
|
| 411 |
/* |
| 412 |
* Subscribe to all the i3-events, we need |
| 413 |
* |
| 414 |
*/ |
| 415 |
void subscribe_events(void) { |
| 416 |
if (i3_provides_workspaces()) { |
| 417 |
i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"workspace\", \"output\", \"mode\", \"barconfig_update\" ]"); |
| 418 |
} else { |
| 419 |
i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"output\", \"mode\", \"barconfig_update\" ]"); |
| 420 |
} |
| 421 |
} |