| 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 |
* child.c: Getting input for the statusline |
| 8 |
* |
| 9 |
*/ |
| 10 |
#include "common.h" |
| 11 |
#include "queue.h" |
| 12 |
#include "yajl_utils.h" |
| 13 |
|
| 14 |
#include <ctype.h> /* isspace */ |
| 15 |
#include <err.h> |
| 16 |
#include <errno.h> |
| 17 |
#include <ev.h> |
| 18 |
#include <fcntl.h> |
| 19 |
#include <paths.h> |
| 20 |
#include <signal.h> |
| 21 |
#include <stdarg.h> |
| 22 |
#include <stdio.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <string.h> |
| 25 |
#include <strings.h> |
| 26 |
#include <sys/wait.h> |
| 27 |
#include <unistd.h> |
| 28 |
|
| 29 |
#include <yajl/yajl_gen.h> |
| 30 |
#include <yajl/yajl_parse.h> |
| 31 |
|
| 32 |
/* Global variables for child_*() */ |
| 33 |
i3bar_child status_child = {0}; |
| 34 |
i3bar_child ws_child = {0}; |
| 35 |
|
| 36 |
#define DLOG_CHILD(c) \ |
| 37 |
do { \ |
| 38 |
if ((c).pid == 0) { \ |
| 39 |
DLOG("%s: child pid = 0\n", __func__); \ |
| 40 |
} else if ((c).pid == status_child.pid) { \ |
| 41 |
DLOG("%s: status_command: pid=%ld stopped=%d stop_signal=%d cont_signal=%d click_events=%d click_events_init=%d\n", \ |
| 42 |
__func__, (long)(c).pid, (c).stopped, (c).stop_signal, (c).cont_signal, (c).click_events, (c).click_events_init); \ |
| 43 |
} else if ((c).pid == ws_child.pid) { \ |
| 44 |
DLOG("%s: workspace_command: pid=%ld stopped=%d stop_signal=%d cont_signal=%d click_events=%d click_events_init=%d\n", \ |
| 45 |
__func__, (long)(c).pid, (c).stopped, (c).stop_signal, (c).cont_signal, (c).click_events, (c).click_events_init); \ |
| 46 |
} else { \ |
| 47 |
ELOG("%s: unknown child, this should never happen " \ |
| 48 |
"pid=%ld stopped=%d stop_signal=%d cont_signal=%d click_events=%d click_events_init=%d\n", \ |
| 49 |
__func__, (long)(c).pid, (c).stopped, (c).stop_signal, (c).cont_signal, (c).click_events, (c).click_events_init); \ |
| 50 |
} \ |
| 51 |
} while (0) |
| 52 |
#define DLOG_CHILDREN \ |
| 53 |
do { \ |
| 54 |
DLOG_CHILD(status_child); \ |
| 55 |
DLOG_CHILD(ws_child); \ |
| 56 |
} while (0) |
| 57 |
|
| 58 |
/* JSON parser for stdin */ |
| 59 |
yajl_handle parser; |
| 60 |
|
| 61 |
/* JSON generator for stdout */ |
| 62 |
yajl_gen gen; |
| 63 |
|
| 64 |
typedef struct parser_ctx { |
| 65 |
/* True if one of the parsed blocks was urgent */ |
| 66 |
bool has_urgent; |
| 67 |
|
| 68 |
/* A copy of the last JSON map key. */ |
| 69 |
char *last_map_key; |
| 70 |
|
| 71 |
/* The current block. Will be filled, then copied and put into the list of |
| 72 |
* blocks. */ |
| 73 |
struct status_block block; |
| 74 |
} parser_ctx; |
| 75 |
|
| 76 |
parser_ctx parser_context; |
| 77 |
|
| 78 |
struct statusline_head statusline_head = TAILQ_HEAD_INITIALIZER(statusline_head); |
| 79 |
/* Used temporarily while reading a statusline */ |
| 80 |
struct statusline_head statusline_buffer = TAILQ_HEAD_INITIALIZER(statusline_buffer); |
| 81 |
|
| 82 |
int child_stdin; |
| 83 |
|
| 84 |
/* |
| 85 |
* Remove all blocks from the given statusline. |
| 86 |
* If free_resources is set, the fields of each status block will be free'd. |
| 87 |
*/ |
| 88 |
void clear_statusline(struct statusline_head *head, bool free_resources) { |
| 89 |
struct status_block *first; |
| 90 |
while (!TAILQ_EMPTY(head)) { |
| 91 |
first = TAILQ_FIRST(head); |
| 92 |
if (free_resources) { |
| 93 |
I3STRING_FREE(first->full_text); |
| 94 |
I3STRING_FREE(first->short_text); |
| 95 |
FREE(first->color); |
| 96 |
FREE(first->name); |
| 97 |
FREE(first->instance); |
| 98 |
FREE(first->min_width_str); |
| 99 |
FREE(first->background); |
| 100 |
FREE(first->border); |
| 101 |
} |
| 102 |
|
| 103 |
TAILQ_REMOVE(head, first, blocks); |
| 104 |
free(first); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
static void copy_statusline(struct statusline_head *from, struct statusline_head *to) { |
| 109 |
struct status_block *current; |
| 110 |
TAILQ_FOREACH (current, from, blocks) { |
| 111 |
struct status_block *new_block = smalloc(sizeof(struct status_block)); |
| 112 |
memcpy(new_block, current, sizeof(struct status_block)); |
| 113 |
TAILQ_INSERT_TAIL(to, new_block, blocks); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/* |
| 118 |
* Replaces the statusline in memory with an error message. Pass a format |
| 119 |
* string and format parameters as you would in `printf'. The next time |
| 120 |
* `draw_bars' is called, the error message text will be drawn on the bar in |
| 121 |
* the space allocated for the statusline. |
| 122 |
*/ |
| 123 |
__attribute__((format(printf, 1, 2))) static void set_statusline_error(const char *format, ...) { |
| 124 |
clear_statusline(&statusline_head, true); |
| 125 |
|
| 126 |
char *message; |
| 127 |
va_list args; |
| 128 |
va_start(args, format); |
| 129 |
if (vasprintf(&message, format, args) == -1) { |
| 130 |
goto finish; |
| 131 |
} |
| 132 |
|
| 133 |
struct status_block *err_block = scalloc(1, sizeof(struct status_block)); |
| 134 |
err_block->full_text = i3string_from_utf8("Error: "); |
| 135 |
err_block->name = sstrdup("error"); |
| 136 |
err_block->color = sstrdup("#ff0000"); |
| 137 |
err_block->no_separator = true; |
| 138 |
|
| 139 |
struct status_block *message_block = scalloc(1, sizeof(struct status_block)); |
| 140 |
message_block->full_text = i3string_from_utf8(message); |
| 141 |
message_block->name = sstrdup("error_message"); |
| 142 |
message_block->color = sstrdup("#ff0000"); |
| 143 |
message_block->no_separator = true; |
| 144 |
|
| 145 |
TAILQ_INSERT_HEAD(&statusline_head, err_block, blocks); |
| 146 |
TAILQ_INSERT_TAIL(&statusline_head, message_block, blocks); |
| 147 |
|
| 148 |
finish: |
| 149 |
free(message); |
| 150 |
va_end(args); |
| 151 |
} |
| 152 |
|
| 153 |
/* |
| 154 |
* Stop and free() the stdin- and SIGCHLD-watchers |
| 155 |
* |
| 156 |
*/ |
| 157 |
static void cleanup(i3bar_child *c) { |
| 158 |
DLOG_CHILD(*c); |
| 159 |
|
| 160 |
if (c->stdin_io != NULL) { |
| 161 |
ev_io_stop(main_loop, c->stdin_io); |
| 162 |
FREE(c->stdin_io); |
| 163 |
|
| 164 |
if (c->pid == status_child.pid) { |
| 165 |
close(child_stdin); |
| 166 |
child_stdin = 0; |
| 167 |
} |
| 168 |
close(c->stdin_fd); |
| 169 |
} |
| 170 |
|
| 171 |
if (c->child_sig != NULL) { |
| 172 |
ev_child_stop(main_loop, c->child_sig); |
| 173 |
FREE(c->child_sig); |
| 174 |
} |
| 175 |
|
| 176 |
FREE(c->pending_line); |
| 177 |
memset(c, 0, sizeof(i3bar_child)); |
| 178 |
} |
| 179 |
|
| 180 |
/* |
| 181 |
* The start of a new array is the start of a new status line, so we clear all |
| 182 |
* previous entries from the buffer. |
| 183 |
*/ |
| 184 |
static int stdin_start_array(void *context) { |
| 185 |
// the blocks are still used by statusline_head, so we won't free the |
| 186 |
// resources here. |
| 187 |
clear_statusline(&statusline_buffer, false); |
| 188 |
return 1; |
| 189 |
} |
| 190 |
|
| 191 |
/* |
| 192 |
* The start of a map is the start of a single block of the status line. |
| 193 |
* |
| 194 |
*/ |
| 195 |
static int stdin_start_map(void *context) { |
| 196 |
parser_ctx *ctx = context; |
| 197 |
memset(&(ctx->block), '\0', sizeof(struct status_block)); |
| 198 |
|
| 199 |
/* Default width of the separator block. */ |
| 200 |
if (config.separator_symbol == NULL) { |
| 201 |
ctx->block.sep_block_width = logical_px(9); |
| 202 |
} else { |
| 203 |
ctx->block.sep_block_width = logical_px(8) + separator_symbol_width; |
| 204 |
} |
| 205 |
|
| 206 |
/* By default we draw all four borders if a border is set. */ |
| 207 |
ctx->block.border_top = 1; |
| 208 |
ctx->block.border_right = 1; |
| 209 |
ctx->block.border_bottom = 1; |
| 210 |
ctx->block.border_left = 1; |
| 211 |
|
| 212 |
return 1; |
| 213 |
} |
| 214 |
|
| 215 |
static int stdin_map_key(void *context, const unsigned char *key, size_t len) { |
| 216 |
parser_ctx *ctx = context; |
| 217 |
FREE(ctx->last_map_key); |
| 218 |
sasprintf(&(ctx->last_map_key), "%.*s", (int)len, key); |
| 219 |
return 1; |
| 220 |
} |
| 221 |
|
| 222 |
static int stdin_boolean(void *context, int val) { |
| 223 |
parser_ctx *ctx = context; |
| 224 |
|
| 225 |
if (!ctx->last_map_key) { |
| 226 |
return 0; |
| 227 |
} |
| 228 |
|
| 229 |
if (strcasecmp(ctx->last_map_key, "urgent") == 0) { |
| 230 |
ctx->block.urgent = val; |
| 231 |
return 1; |
| 232 |
} |
| 233 |
if (strcasecmp(ctx->last_map_key, "separator") == 0) { |
| 234 |
ctx->block.no_separator = !val; |
| 235 |
return 1; |
| 236 |
} |
| 237 |
|
| 238 |
return 1; |
| 239 |
} |
| 240 |
|
| 241 |
static int stdin_string(void *context, const unsigned char *val, size_t len) { |
| 242 |
parser_ctx *ctx = context; |
| 243 |
|
| 244 |
if (!ctx->last_map_key) { |
| 245 |
return 0; |
| 246 |
} |
| 247 |
|
| 248 |
if (strcasecmp(ctx->last_map_key, "full_text") == 0) { |
| 249 |
ctx->block.full_text = i3string_from_markup_with_length((const char *)val, len); |
| 250 |
return 1; |
| 251 |
} |
| 252 |
if (strcasecmp(ctx->last_map_key, "short_text") == 0) { |
| 253 |
ctx->block.short_text = i3string_from_markup_with_length((const char *)val, len); |
| 254 |
return 1; |
| 255 |
} |
| 256 |
if (strcasecmp(ctx->last_map_key, "color") == 0) { |
| 257 |
sasprintf(&(ctx->block.color), "%.*s", (int)len, val); |
| 258 |
return 1; |
| 259 |
} |
| 260 |
if (strcasecmp(ctx->last_map_key, "background") == 0) { |
| 261 |
sasprintf(&(ctx->block.background), "%.*s", (int)len, val); |
| 262 |
return 1; |
| 263 |
} |
| 264 |
if (strcasecmp(ctx->last_map_key, "border") == 0) { |
| 265 |
sasprintf(&(ctx->block.border), "%.*s", (int)len, val); |
| 266 |
return 1; |
| 267 |
} |
| 268 |
if (strcasecmp(ctx->last_map_key, "markup") == 0) { |
| 269 |
ctx->block.pango_markup = (len == strlen("pango") && !strncasecmp((const char *)val, "pango", strlen("pango"))); |
| 270 |
return 1; |
| 271 |
} |
| 272 |
if (strcasecmp(ctx->last_map_key, "align") == 0) { |
| 273 |
if (len == strlen("center") && !strncmp((const char *)val, "center", strlen("center"))) { |
| 274 |
ctx->block.align = ALIGN_CENTER; |
| 275 |
} else if (len == strlen("right") && !strncmp((const char *)val, "right", strlen("right"))) { |
| 276 |
ctx->block.align = ALIGN_RIGHT; |
| 277 |
} else { |
| 278 |
ctx->block.align = ALIGN_LEFT; |
| 279 |
} |
| 280 |
return 1; |
| 281 |
} |
| 282 |
if (strcasecmp(ctx->last_map_key, "min_width") == 0) { |
| 283 |
sasprintf(&(ctx->block.min_width_str), "%.*s", (int)len, val); |
| 284 |
return 1; |
| 285 |
} |
| 286 |
if (strcasecmp(ctx->last_map_key, "name") == 0) { |
| 287 |
sasprintf(&(ctx->block.name), "%.*s", (int)len, val); |
| 288 |
return 1; |
| 289 |
} |
| 290 |
if (strcasecmp(ctx->last_map_key, "instance") == 0) { |
| 291 |
sasprintf(&(ctx->block.instance), "%.*s", (int)len, val); |
| 292 |
return 1; |
| 293 |
} |
| 294 |
|
| 295 |
return 1; |
| 296 |
} |
| 297 |
|
| 298 |
static int stdin_integer(void *context, long long val) { |
| 299 |
parser_ctx *ctx = context; |
| 300 |
|
| 301 |
if (!ctx->last_map_key) { |
| 302 |
return 0; |
| 303 |
} |
| 304 |
|
| 305 |
if (strcasecmp(ctx->last_map_key, "min_width") == 0) { |
| 306 |
ctx->block.min_width = (uint32_t)val; |
| 307 |
return 1; |
| 308 |
} |
| 309 |
if (strcasecmp(ctx->last_map_key, "separator_block_width") == 0) { |
| 310 |
ctx->block.sep_block_width = (uint32_t)val; |
| 311 |
return 1; |
| 312 |
} |
| 313 |
if (strcasecmp(ctx->last_map_key, "border_top") == 0) { |
| 314 |
ctx->block.border_top = (uint32_t)val; |
| 315 |
return 1; |
| 316 |
} |
| 317 |
if (strcasecmp(ctx->last_map_key, "border_right") == 0) { |
| 318 |
ctx->block.border_right = (uint32_t)val; |
| 319 |
return 1; |
| 320 |
} |
| 321 |
if (strcasecmp(ctx->last_map_key, "border_bottom") == 0) { |
| 322 |
ctx->block.border_bottom = (uint32_t)val; |
| 323 |
return 1; |
| 324 |
} |
| 325 |
if (strcasecmp(ctx->last_map_key, "border_left") == 0) { |
| 326 |
ctx->block.border_left = (uint32_t)val; |
| 327 |
return 1; |
| 328 |
} |
| 329 |
|
| 330 |
return 1; |
| 331 |
} |
| 332 |
|
| 333 |
/* |
| 334 |
* When a map is finished, we have an entire status block. |
| 335 |
* Move it from the parser's context to the statusline buffer. |
| 336 |
*/ |
| 337 |
static int stdin_end_map(void *context) { |
| 338 |
parser_ctx *ctx = context; |
| 339 |
struct status_block *new_block = smalloc(sizeof(struct status_block)); |
| 340 |
memcpy(new_block, &(ctx->block), sizeof(struct status_block)); |
| 341 |
/* Ensure we have a full_text set, so that when it is missing (or null), |
| 342 |
* i3bar doesn’t crash and the user gets an annoying message. */ |
| 343 |
if (!new_block->full_text) { |
| 344 |
new_block->full_text = i3string_from_utf8("SPEC VIOLATION: full_text is NULL!"); |
| 345 |
} |
| 346 |
if (new_block->urgent) { |
| 347 |
ctx->has_urgent = true; |
| 348 |
} |
| 349 |
|
| 350 |
if (new_block->min_width_str) { |
| 351 |
i3String *text = i3string_from_utf8(new_block->min_width_str); |
| 352 |
i3string_set_markup(text, new_block->pango_markup); |
| 353 |
new_block->min_width = (uint32_t)predict_text_width(text); |
| 354 |
i3string_free(text); |
| 355 |
} |
| 356 |
|
| 357 |
i3string_set_markup(new_block->full_text, new_block->pango_markup); |
| 358 |
|
| 359 |
new_block->use_short = false; |
| 360 |
if (new_block->short_text != NULL) { |
| 361 |
i3string_set_markup(new_block->short_text, new_block->pango_markup); |
| 362 |
} |
| 363 |
|
| 364 |
TAILQ_INSERT_TAIL(&statusline_buffer, new_block, blocks); |
| 365 |
|
| 366 |
return 1; |
| 367 |
} |
| 368 |
|
| 369 |
/* |
| 370 |
* When an array is finished, we have an entire statusline. |
| 371 |
* Copy it from the buffer to the actual statusline. |
| 372 |
*/ |
| 373 |
static int stdin_end_array(void *context) { |
| 374 |
DLOG("copying statusline_buffer to statusline_head\n"); |
| 375 |
clear_statusline(&statusline_head, true); |
| 376 |
copy_statusline(&statusline_buffer, &statusline_head); |
| 377 |
|
| 378 |
DLOG("dumping statusline:\n"); |
| 379 |
struct status_block *current; |
| 380 |
TAILQ_FOREACH (current, &statusline_head, blocks) { |
| 381 |
DLOG("full_text = %s\n", i3string_as_utf8(current->full_text)); |
| 382 |
DLOG("short_text = %s\n", (current->short_text == NULL ? NULL : i3string_as_utf8(current->short_text))); |
| 383 |
DLOG("color = %s\n", current->color); |
| 384 |
} |
| 385 |
DLOG("end of dump\n"); |
| 386 |
return 1; |
| 387 |
} |
| 388 |
|
| 389 |
/* |
| 390 |
* Helper function to read stdin |
| 391 |
* |
| 392 |
* Returns NULL on EOF. |
| 393 |
* |
| 394 |
*/ |
| 395 |
static unsigned char *get_buffer(int fd, int *ret_buffer_len) { |
| 396 |
int rec = 0; |
| 397 |
int buffer_len = STDIN_CHUNK_SIZE; |
| 398 |
unsigned char *buffer = smalloc(buffer_len + 1); |
| 399 |
buffer[0] = '\0'; |
| 400 |
while (1) { |
| 401 |
const ssize_t n = read(fd, buffer + rec, buffer_len - rec); |
| 402 |
if (n == -1) { |
| 403 |
if (errno == EAGAIN) { |
| 404 |
/* finish up */ |
| 405 |
break; |
| 406 |
} |
| 407 |
ELOG("read() failed!: %s\n", strerror(errno)); |
| 408 |
FREE(buffer); |
| 409 |
exit(EXIT_FAILURE); |
| 410 |
} |
| 411 |
if (n == 0) { |
| 412 |
ELOG("stdin: received EOF\n"); |
| 413 |
FREE(buffer); |
| 414 |
*ret_buffer_len = -1; |
| 415 |
return NULL; |
| 416 |
} |
| 417 |
rec += n; |
| 418 |
|
| 419 |
if (rec == buffer_len) { |
| 420 |
buffer_len += STDIN_CHUNK_SIZE; |
| 421 |
buffer = srealloc(buffer, buffer_len + 1); |
| 422 |
} |
| 423 |
} |
| 424 |
buffer[rec] = '\0'; |
| 425 |
if (buffer[0] == '\0') { |
| 426 |
FREE(buffer); |
| 427 |
rec = -1; |
| 428 |
} |
| 429 |
*ret_buffer_len = rec; |
| 430 |
return buffer; |
| 431 |
} |
| 432 |
|
| 433 |
static void read_flat_input(char *buffer, int length) { |
| 434 |
struct status_block *first = TAILQ_FIRST(&statusline_head); |
| 435 |
/* Clear the old buffer if any. */ |
| 436 |
I3STRING_FREE(first->full_text); |
| 437 |
/* Remove the trailing newline and terminate the string at the same |
| 438 |
* time. */ |
| 439 |
if (buffer[length - 1] == '\n' || buffer[length - 1] == '\r') { |
| 440 |
buffer[length - 1] = '\0'; |
| 441 |
} else { |
| 442 |
buffer[length] = '\0'; |
| 443 |
} |
| 444 |
|
| 445 |
first->full_text = i3string_from_utf8(buffer); |
| 446 |
} |
| 447 |
|
| 448 |
static bool read_json_input(unsigned char *input, int length) { |
| 449 |
yajl_status status = yajl_parse(parser, input, length); |
| 450 |
bool has_urgent = false; |
| 451 |
if (status != yajl_status_ok) { |
| 452 |
char *message = (char *)yajl_get_error(parser, 0, input, length); |
| 453 |
|
| 454 |
/* strip the newline yajl adds to the error message */ |
| 455 |
if (message[strlen(message) - 1] == '\n') { |
| 456 |
message[strlen(message) - 1] = '\0'; |
| 457 |
} |
| 458 |
|
| 459 |
fprintf(stderr, "[i3bar] Could not parse JSON input (code = %d, message = %s): %.*s\n", |
| 460 |
status, message, length, input); |
| 461 |
|
| 462 |
set_statusline_error("Could not parse JSON (%s)", message); |
| 463 |
yajl_free_error(parser, (unsigned char *)message); |
| 464 |
draw_bars(false); |
| 465 |
} else if (parser_context.has_urgent) { |
| 466 |
has_urgent = true; |
| 467 |
} |
| 468 |
return has_urgent; |
| 469 |
} |
| 470 |
|
| 471 |
/* |
| 472 |
* Callbalk for stdin. We read a line from stdin and store the result |
| 473 |
* in statusline |
| 474 |
* |
| 475 |
*/ |
| 476 |
static void stdin_io_cb(int fd) { |
| 477 |
int rec; |
| 478 |
unsigned char *buffer = get_buffer(fd, &rec); |
| 479 |
if (buffer == NULL) { |
| 480 |
return; |
| 481 |
} |
| 482 |
bool has_urgent = false; |
| 483 |
if (status_child.version > 0) { |
| 484 |
has_urgent = read_json_input(buffer, rec); |
| 485 |
} else { |
| 486 |
read_flat_input((char *)buffer, rec); |
| 487 |
} |
| 488 |
free(buffer); |
| 489 |
draw_bars(has_urgent); |
| 490 |
} |
| 491 |
|
| 492 |
/* |
| 493 |
* Callbalk for stdin first line. We read the first line to detect |
| 494 |
* whether this is JSON or plain text |
| 495 |
* |
| 496 |
*/ |
| 497 |
static void stdin_io_first_line_cb(int fd) { |
| 498 |
int rec; |
| 499 |
unsigned char *buffer = get_buffer(fd, &rec); |
| 500 |
if (buffer == NULL) { |
| 501 |
return; |
| 502 |
} |
| 503 |
DLOG("Detecting input type based on buffer *%.*s*\n", rec, buffer); |
| 504 |
/* Detect whether this is JSON or plain text. */ |
| 505 |
unsigned int consumed = 0; |
| 506 |
/* At the moment, we don’t care for the version. This might change |
| 507 |
* in the future, but for now, we just discard it. */ |
| 508 |
parse_json_header(&status_child, buffer, rec, &consumed); |
| 509 |
if (status_child.version > 0) { |
| 510 |
/* If hide-on-modifier is set, we start of by sending the status_child |
| 511 |
* a SIGSTOP, because the bars aren't mapped at start */ |
| 512 |
if (config.hide_on_modifier) { |
| 513 |
stop_children(); |
| 514 |
} |
| 515 |
draw_bars(read_json_input(buffer + consumed, rec - consumed)); |
| 516 |
} else { |
| 517 |
/* In case of plaintext, we just add a single block and change its |
| 518 |
* full_text pointer later. */ |
| 519 |
struct status_block *new_block = scalloc(1, sizeof(struct status_block)); |
| 520 |
TAILQ_INSERT_TAIL(&statusline_head, new_block, blocks); |
| 521 |
read_flat_input((char *)buffer, rec); |
| 522 |
} |
| 523 |
free(buffer); |
| 524 |
} |
| 525 |
|
| 526 |
static bool isempty(char *s) { |
| 527 |
while (*s != '\0') { |
| 528 |
if (!isspace((unsigned char)*s)) { |
| 529 |
return false; |
| 530 |
} |
| 531 |
s++; |
| 532 |
} |
| 533 |
return true; |
| 534 |
} |
| 535 |
|
| 536 |
static char *append_string(const char *previous, const char *str) { |
| 537 |
if (previous != NULL) { |
| 538 |
char *result; |
| 539 |
sasprintf(&result, "%s%s", previous, str); |
| 540 |
return result; |
| 541 |
} |
| 542 |
return sstrdup(str); |
| 543 |
} |
| 544 |
|
| 545 |
static char *ws_last_json; |
| 546 |
|
| 547 |
static void ws_stdin_io_cb(int fd) { |
| 548 |
int rec; |
| 549 |
unsigned char *buffer = get_buffer(fd, &rec); |
| 550 |
if (buffer == NULL) { |
| 551 |
return; |
| 552 |
} |
| 553 |
|
| 554 |
gchar **strings = g_strsplit((const char *)buffer, "\n", 0); |
| 555 |
for (int idx = 0; strings[idx] != NULL; idx++) { |
| 556 |
if (ws_child.pending_line == NULL && isempty(strings[idx])) { |
| 557 |
/* In the normal case where the buffer ends with '\n', the last |
| 558 |
* string should be empty */ |
| 559 |
continue; |
| 560 |
} |
| 561 |
|
| 562 |
if (strings[idx + 1] == NULL) { |
| 563 |
/* This is the last string but it is not empty, meaning that we have |
| 564 |
* read data that is incomplete, save it for later. */ |
| 565 |
char *new = append_string(ws_child.pending_line, strings[idx]); |
| 566 |
free(ws_child.pending_line); |
| 567 |
ws_child.pending_line = new; |
| 568 |
continue; |
| 569 |
} |
| 570 |
|
| 571 |
free(ws_last_json); |
| 572 |
ws_last_json = append_string(ws_child.pending_line, strings[idx]); |
| 573 |
FREE(ws_child.pending_line); |
| 574 |
|
| 575 |
parse_workspaces_json((const unsigned char *)ws_last_json, strlen(ws_last_json)); |
| 576 |
} |
| 577 |
|
| 578 |
g_strfreev(strings); |
| 579 |
free(buffer); |
| 580 |
|
| 581 |
draw_bars(false); |
| 582 |
} |
| 583 |
|
| 584 |
static void common_stdin_cb(struct ev_loop *loop, ev_io *watcher, int revents) { |
| 585 |
if (watcher == status_child.stdin_io) { |
| 586 |
if (status_child.version == (uint32_t)-1) { |
| 587 |
stdin_io_first_line_cb(watcher->fd); |
| 588 |
} else { |
| 589 |
stdin_io_cb(watcher->fd); |
| 590 |
} |
| 591 |
} else if (watcher == ws_child.stdin_io) { |
| 592 |
ws_stdin_io_cb(watcher->fd); |
| 593 |
} else { |
| 594 |
ELOG("Got callback for unknown watcher fd=%d\n", watcher->fd); |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
/* |
| 599 |
* When workspace_command is enabled this function is used to re-parse the |
| 600 |
* latest received JSON from the client. |
| 601 |
*/ |
| 602 |
void repeat_last_ws_json(void) { |
| 603 |
if (ws_last_json) { |
| 604 |
DLOG("Repeating last workspace JSON\n"); |
| 605 |
parse_workspaces_json((const unsigned char *)ws_last_json, strlen(ws_last_json)); |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
/* |
| 610 |
* Wrapper around set_workspace_button_error to mimic the call of |
| 611 |
* set_statusline_error. |
| 612 |
*/ |
| 613 |
__attribute__((format(printf, 1, 2))) static void set_workspace_button_error_f(const char *format, ...) { |
| 614 |
char *message; |
| 615 |
va_list args; |
| 616 |
va_start(args, format); |
| 617 |
if (vasprintf(&message, format, args) == -1) { |
| 618 |
goto finish; |
| 619 |
} |
| 620 |
|
| 621 |
set_workspace_button_error(message); |
| 622 |
|
| 623 |
finish: |
| 624 |
free(message); |
| 625 |
va_end(args); |
| 626 |
} |
| 627 |
|
| 628 |
/* |
| 629 |
* Replaces the workspace buttons with an error message. |
| 630 |
*/ |
| 631 |
void set_workspace_button_error(const char *message) { |
| 632 |
free_workspaces(); |
| 633 |
|
| 634 |
char *name = NULL; |
| 635 |
sasprintf(&name, "Error: %s", message); |
| 636 |
|
| 637 |
i3_output *output; |
| 638 |
SLIST_FOREACH (output, outputs, slist) { |
| 639 |
i3_ws *fake_ws = scalloc(1, sizeof(i3_ws)); |
| 640 |
/* Don't set the canonical_name field to make this workspace unfocusable. */ |
| 641 |
fake_ws->name = i3string_from_utf8(name); |
| 642 |
fake_ws->name_width = predict_text_width(fake_ws->name); |
| 643 |
fake_ws->num = -1; |
| 644 |
fake_ws->urgent = fake_ws->visible = true; |
| 645 |
fake_ws->output = output; |
| 646 |
|
| 647 |
TAILQ_INSERT_TAIL(output->workspaces, fake_ws, tailq); |
| 648 |
} |
| 649 |
|
| 650 |
free(name); |
| 651 |
} |
| 652 |
|
| 653 |
/* |
| 654 |
* We received a SIGCHLD, meaning, that the child process terminated. |
| 655 |
* We simply free the respective data structures and don't care for input |
| 656 |
* anymore |
| 657 |
* |
| 658 |
*/ |
| 659 |
static void child_sig_cb(struct ev_loop *loop, ev_child *watcher, int revents) { |
| 660 |
const int exit_status = WEXITSTATUS(watcher->rstatus); |
| 661 |
|
| 662 |
ELOG("Child (pid: %d) unexpectedly exited with status %d\n", |
| 663 |
watcher->pid, |
| 664 |
exit_status); |
| 665 |
|
| 666 |
__attribute__((format(printf, 1, 2))) void (*error_function_pointer)(const char *, ...) = NULL; |
| 667 |
const char *command_type = ""; |
| 668 |
i3bar_child *c = NULL; |
| 669 |
if (watcher->pid == status_child.pid) { |
| 670 |
command_type = "status_command"; |
| 671 |
error_function_pointer = set_statusline_error; |
| 672 |
c = &status_child; |
| 673 |
} else if (watcher->pid == ws_child.pid) { |
| 674 |
command_type = "workspace_command"; |
| 675 |
error_function_pointer = set_workspace_button_error_f; |
| 676 |
c = &ws_child; |
| 677 |
} else { |
| 678 |
ELOG("Unknown child pid, this should never happen\n"); |
| 679 |
return; |
| 680 |
} |
| 681 |
DLOG_CHILD(*c); |
| 682 |
|
| 683 |
/* this error is most likely caused by a user giving a nonexecutable or |
| 684 |
* nonexistent file, so we will handle those cases separately. */ |
| 685 |
if (exit_status == 126) { |
| 686 |
error_function_pointer("%s is not executable (exit %d)", command_type, exit_status); |
| 687 |
} else if (exit_status == 127) { |
| 688 |
error_function_pointer("%s not found or is missing a library dependency (exit %d)", command_type, exit_status); |
| 689 |
} else { |
| 690 |
error_function_pointer("%s process exited unexpectedly (exit %d)", command_type, exit_status); |
| 691 |
} |
| 692 |
|
| 693 |
cleanup(c); |
| 694 |
draw_bars(false); |
| 695 |
} |
| 696 |
|
| 697 |
static void child_write_output(void) { |
| 698 |
if (status_child.click_events) { |
| 699 |
const unsigned char *output; |
| 700 |
size_t size; |
| 701 |
ssize_t n; |
| 702 |
|
| 703 |
yajl_gen_get_buf(gen, &output, &size); |
| 704 |
|
| 705 |
n = writeall(child_stdin, output, size); |
| 706 |
if (n != -1) { |
| 707 |
n = writeall(child_stdin, "\n", 1); |
| 708 |
} |
| 709 |
|
| 710 |
yajl_gen_clear(gen); |
| 711 |
|
| 712 |
if (n == -1) { |
| 713 |
status_child.click_events = false; |
| 714 |
kill_child(); |
| 715 |
set_statusline_error("child_write_output failed"); |
| 716 |
draw_bars(false); |
| 717 |
} |
| 718 |
} |
| 719 |
} |
| 720 |
|
| 721 |
static pid_t sfork(void) { |
| 722 |
const pid_t pid = fork(); |
| 723 |
if (pid == -1) { |
| 724 |
ELOG("Couldn't fork(): %s\n", strerror(errno)); |
| 725 |
exit(EXIT_FAILURE); |
| 726 |
} |
| 727 |
return pid; |
| 728 |
} |
| 729 |
|
| 730 |
static void spipe(int pipedes[2]) { |
| 731 |
if (pipe(pipedes) == -1) { |
| 732 |
err(EXIT_FAILURE, "pipe(pipe_in)"); |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
static void exec_shell(char *command) { |
| 737 |
execl(_PATH_BSHELL, _PATH_BSHELL, "-c", command, (char *)NULL); |
| 738 |
err(EXIT_FAILURE, "execl return"); /* only reached on error */ |
| 739 |
} |
| 740 |
|
| 741 |
static void setup_child_cb(i3bar_child *child) { |
| 742 |
/* We set O_NONBLOCK because blocking is evil in event-driven software */ |
| 743 |
fcntl(child->stdin_fd, F_SETFL, O_NONBLOCK); |
| 744 |
|
| 745 |
child->stdin_io = smalloc(sizeof(ev_io)); |
| 746 |
ev_io_init(child->stdin_io, &common_stdin_cb, child->stdin_fd, EV_READ); |
| 747 |
ev_io_start(main_loop, child->stdin_io); |
| 748 |
|
| 749 |
/* We must cleanup, if the child unexpectedly terminates */ |
| 750 |
child->child_sig = smalloc(sizeof(ev_child)); |
| 751 |
ev_child_init(child->child_sig, &child_sig_cb, child->pid, 0); |
| 752 |
ev_child_start(main_loop, child->child_sig); |
| 753 |
|
| 754 |
DLOG_CHILD(*child); |
| 755 |
} |
| 756 |
|
| 757 |
/* |
| 758 |
* Start a child process with the specified command and reroute stdin. |
| 759 |
* We actually start a shell to execute the command so we don't have to care |
| 760 |
* about arguments and such. |
| 761 |
* |
| 762 |
* If `command' is NULL, such as in the case when no `status_command' is given |
| 763 |
* in the bar config, no child will be started. |
| 764 |
* |
| 765 |
*/ |
| 766 |
void start_child(char *command) { |
| 767 |
if (command == NULL) { |
| 768 |
return; |
| 769 |
} |
| 770 |
|
| 771 |
/* Allocate a yajl parser which will be used to parse stdin. */ |
| 772 |
static yajl_callbacks callbacks = { |
| 773 |
.yajl_boolean = stdin_boolean, |
| 774 |
.yajl_integer = stdin_integer, |
| 775 |
.yajl_string = stdin_string, |
| 776 |
.yajl_start_map = stdin_start_map, |
| 777 |
.yajl_map_key = stdin_map_key, |
| 778 |
.yajl_end_map = stdin_end_map, |
| 779 |
.yajl_start_array = stdin_start_array, |
| 780 |
.yajl_end_array = stdin_end_array, |
| 781 |
}; |
| 782 |
parser = yajl_alloc(&callbacks, NULL, &parser_context); |
| 783 |
gen = yajl_gen_alloc(NULL); |
| 784 |
|
| 785 |
int pipe_in[2]; /* pipe we read from */ |
| 786 |
int pipe_out[2]; /* pipe we write to */ |
| 787 |
spipe(pipe_in); |
| 788 |
spipe(pipe_out); |
| 789 |
|
| 790 |
status_child.pid = sfork(); |
| 791 |
if (status_child.pid == 0) { |
| 792 |
/* Child-process. Reroute streams and start shell */ |
| 793 |
close(pipe_in[0]); |
| 794 |
close(pipe_out[1]); |
| 795 |
|
| 796 |
dup2(pipe_in[1], STDOUT_FILENO); |
| 797 |
dup2(pipe_out[0], STDIN_FILENO); |
| 798 |
|
| 799 |
setpgid(status_child.pid, 0); |
| 800 |
exec_shell(command); |
| 801 |
return; |
| 802 |
} |
| 803 |
/* Parent-process. Reroute streams */ |
| 804 |
close(pipe_in[1]); |
| 805 |
close(pipe_out[0]); |
| 806 |
|
| 807 |
status_child.stdin_fd = pipe_in[0]; |
| 808 |
child_stdin = pipe_out[1]; |
| 809 |
status_child.version = -1; |
| 810 |
|
| 811 |
setup_child_cb(&status_child); |
| 812 |
} |
| 813 |
|
| 814 |
/* |
| 815 |
* Same as start_child but starts the configured client that manages workspace |
| 816 |
* buttons. |
| 817 |
* |
| 818 |
*/ |
| 819 |
void start_ws_child(char *command) { |
| 820 |
if (command == NULL) { |
| 821 |
return; |
| 822 |
} |
| 823 |
|
| 824 |
ws_child.stop_signal = SIGSTOP; |
| 825 |
ws_child.cont_signal = SIGCONT; |
| 826 |
|
| 827 |
int pipe_in[2]; /* pipe we read from */ |
| 828 |
spipe(pipe_in); |
| 829 |
|
| 830 |
ws_child.pid = sfork(); |
| 831 |
if (ws_child.pid == 0) { |
| 832 |
/* Child-process. Reroute streams and start shell */ |
| 833 |
close(pipe_in[0]); |
| 834 |
dup2(pipe_in[1], STDOUT_FILENO); |
| 835 |
|
| 836 |
setpgid(ws_child.pid, 0); |
| 837 |
exec_shell(command); |
| 838 |
} |
| 839 |
/* Parent-process. Reroute streams */ |
| 840 |
close(pipe_in[1]); |
| 841 |
ws_child.stdin_fd = pipe_in[0]; |
| 842 |
|
| 843 |
setup_child_cb(&ws_child); |
| 844 |
} |
| 845 |
|
| 846 |
static void child_click_events_initialize(void) { |
| 847 |
DLOG_CHILD(status_child); |
| 848 |
|
| 849 |
if (!status_child.click_events_init) { |
| 850 |
yajl_gen_array_open(gen); |
| 851 |
child_write_output(); |
| 852 |
status_child.click_events_init = true; |
| 853 |
} |
| 854 |
} |
| 855 |
|
| 856 |
/* |
| 857 |
* Generates a click event, if enabled. |
| 858 |
* |
| 859 |
*/ |
| 860 |
void send_block_clicked(int button, const char *name, const char *instance, int x, int y, int x_rel, int y_rel, int out_x, int out_y, int width, int height, int mods) { |
| 861 |
if (!status_child.click_events) { |
| 862 |
return; |
| 863 |
} |
| 864 |
|
| 865 |
child_click_events_initialize(); |
| 866 |
|
| 867 |
yajl_gen_map_open(gen); |
| 868 |
|
| 869 |
if (name) { |
| 870 |
ystr("name"); |
| 871 |
ystr(name); |
| 872 |
} |
| 873 |
|
| 874 |
if (instance) { |
| 875 |
ystr("instance"); |
| 876 |
ystr(instance); |
| 877 |
} |
| 878 |
|
| 879 |
ystr("button"); |
| 880 |
yajl_gen_integer(gen, button); |
| 881 |
|
| 882 |
ystr("modifiers"); |
| 883 |
yajl_gen_array_open(gen); |
| 884 |
if (mods & XCB_MOD_MASK_SHIFT) { |
| 885 |
ystr("Shift"); |
| 886 |
} |
| 887 |
if (mods & XCB_MOD_MASK_CONTROL) { |
| 888 |
ystr("Control"); |
| 889 |
} |
| 890 |
if (mods & XCB_MOD_MASK_1) { |
| 891 |
ystr("Mod1"); |
| 892 |
} |
| 893 |
if (mods & XCB_MOD_MASK_2) { |
| 894 |
ystr("Mod2"); |
| 895 |
} |
| 896 |
if (mods & XCB_MOD_MASK_3) { |
| 897 |
ystr("Mod3"); |
| 898 |
} |
| 899 |
if (mods & XCB_MOD_MASK_4) { |
| 900 |
ystr("Mod4"); |
| 901 |
} |
| 902 |
if (mods & XCB_MOD_MASK_5) { |
| 903 |
ystr("Mod5"); |
| 904 |
} |
| 905 |
yajl_gen_array_close(gen); |
| 906 |
|
| 907 |
ystr("x"); |
| 908 |
yajl_gen_integer(gen, x); |
| 909 |
|
| 910 |
ystr("y"); |
| 911 |
yajl_gen_integer(gen, y); |
| 912 |
|
| 913 |
ystr("relative_x"); |
| 914 |
yajl_gen_integer(gen, x_rel); |
| 915 |
|
| 916 |
ystr("relative_y"); |
| 917 |
yajl_gen_integer(gen, y_rel); |
| 918 |
|
| 919 |
ystr("output_x"); |
| 920 |
yajl_gen_integer(gen, out_x); |
| 921 |
|
| 922 |
ystr("output_y"); |
| 923 |
yajl_gen_integer(gen, out_y); |
| 924 |
|
| 925 |
ystr("width"); |
| 926 |
yajl_gen_integer(gen, width); |
| 927 |
|
| 928 |
ystr("height"); |
| 929 |
yajl_gen_integer(gen, height); |
| 930 |
|
| 931 |
yajl_gen_map_close(gen); |
| 932 |
child_write_output(); |
| 933 |
} |
| 934 |
|
| 935 |
static bool is_alive(i3bar_child *c) { |
| 936 |
return c->pid > 0; |
| 937 |
} |
| 938 |
|
| 939 |
/* |
| 940 |
* Returns true if the status child process is alive. |
| 941 |
* |
| 942 |
*/ |
| 943 |
bool status_child_is_alive(void) { |
| 944 |
return is_alive(&status_child); |
| 945 |
} |
| 946 |
|
| 947 |
/* |
| 948 |
* Returns true if the workspace child process is alive. |
| 949 |
* |
| 950 |
*/ |
| 951 |
bool ws_child_is_alive(void) { |
| 952 |
return is_alive(&ws_child); |
| 953 |
} |
| 954 |
|
| 955 |
/* |
| 956 |
* kill()s the child process (if any). Called when exit()ing. |
| 957 |
* |
| 958 |
*/ |
| 959 |
void kill_children_at_exit(void) { |
| 960 |
DLOG_CHILDREN; |
| 961 |
cont_children(); |
| 962 |
|
| 963 |
if (is_alive(&status_child)) { |
| 964 |
killpg(status_child.pid, SIGTERM); |
| 965 |
} |
| 966 |
if (is_alive(&ws_child)) { |
| 967 |
killpg(ws_child.pid, SIGTERM); |
| 968 |
} |
| 969 |
} |
| 970 |
|
| 971 |
static void cont_child(i3bar_child *c) { |
| 972 |
if (is_alive(c) && c->cont_signal > 0 && c->stopped) { |
| 973 |
c->stopped = false; |
| 974 |
killpg(c->pid, c->cont_signal); |
| 975 |
} |
| 976 |
} |
| 977 |
|
| 978 |
static void kill_and_wait(i3bar_child *c) { |
| 979 |
DLOG_CHILD(*c); |
| 980 |
if (!is_alive(c)) { |
| 981 |
return; |
| 982 |
} |
| 983 |
|
| 984 |
cont_child(c); |
| 985 |
killpg(c->pid, SIGTERM); |
| 986 |
int status; |
| 987 |
waitpid(c->pid, &status, 0); |
| 988 |
cleanup(c); |
| 989 |
} |
| 990 |
|
| 991 |
/* |
| 992 |
* kill()s the child process (if any) and closes and free()s the stdin- and |
| 993 |
* SIGCHLD-watchers |
| 994 |
* |
| 995 |
*/ |
| 996 |
void kill_child(void) { |
| 997 |
kill_and_wait(&status_child); |
| 998 |
} |
| 999 |
|
| 1000 |
/* |
| 1001 |
* kill()s the workspace child process (if any) and closes and free()s the |
| 1002 |
* stdin- and SIGCHLD-watchers. |
| 1003 |
* Similar to kill_child. |
| 1004 |
* |
| 1005 |
*/ |
| 1006 |
void kill_ws_child(void) { |
| 1007 |
kill_and_wait(&ws_child); |
| 1008 |
} |
| 1009 |
|
| 1010 |
static void stop_child(i3bar_child *c) { |
| 1011 |
if (c->stop_signal > 0 && !c->stopped) { |
| 1012 |
c->stopped = true; |
| 1013 |
killpg(c->pid, c->stop_signal); |
| 1014 |
} |
| 1015 |
} |
| 1016 |
|
| 1017 |
/* |
| 1018 |
* Sends a SIGSTOP to the child process (if existent) |
| 1019 |
* |
| 1020 |
*/ |
| 1021 |
void stop_children(void) { |
| 1022 |
DLOG_CHILDREN; |
| 1023 |
stop_child(&status_child); |
| 1024 |
stop_child(&ws_child); |
| 1025 |
} |
| 1026 |
|
| 1027 |
/* |
| 1028 |
* Sends a SIGCONT to the child process (if existent) |
| 1029 |
* |
| 1030 |
*/ |
| 1031 |
void cont_children(void) { |
| 1032 |
DLOG_CHILDREN; |
| 1033 |
|
| 1034 |
cont_child(&status_child); |
| 1035 |
cont_child(&ws_child); |
| 1036 |
} |
| 1037 |
|
| 1038 |
/* |
| 1039 |
* Whether or not the child want click events |
| 1040 |
* |
| 1041 |
*/ |
| 1042 |
bool child_want_click_events(void) { |
| 1043 |
return status_child.click_events; |
| 1044 |
} |