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 * child.c: Getting Input for the statusline
12 #include <sys/types.h>
21 #include <yajl/yajl_common.h>
22 #include <yajl/yajl_parse.h>
23 #include <yajl/yajl_version.h>
27 /* Global variables for child_*() */
28 i3bar_child child = { 0 };
30 /* stdin- and sigchild-watchers */
34 /* JSON parser for stdin */
35 yajl_callbacks callbacks;
38 typedef struct parser_ctx {
39 /* True if one of the parsed blocks was urgent */
42 /* A copy of the last JSON map key. */
45 /* The current block. Will be filled, then copied and put into the list of
47 struct status_block block;
50 parser_ctx parser_context;
52 /* The buffer statusline points to */
53 struct statusline_head statusline_head = TAILQ_HEAD_INITIALIZER(statusline_head);
54 char *statusline_buffer = NULL;
57 * Stop and free() the stdin- and sigchild-watchers
61 if (stdin_io != NULL) {
62 ev_io_stop(main_loop, stdin_io);
64 FREE(statusline_buffer);
65 /* statusline pointed to memory within statusline_buffer */
69 if (child_sig != NULL) {
70 ev_child_stop(main_loop, child_sig);
74 memset(&child, 0, sizeof(i3bar_child));
78 * The start of a new array is the start of a new status line, so we clear all
82 static int stdin_start_array(void *context) {
83 struct status_block *first;
84 while (!TAILQ_EMPTY(&statusline_head)) {
85 first = TAILQ_FIRST(&statusline_head);
86 I3STRING_FREE(first->full_text);
88 TAILQ_REMOVE(&statusline_head, first, blocks);
95 * The start of a map is the start of a single block of the status line.
98 static int stdin_start_map(void *context) {
99 parser_ctx *ctx = context;
100 memset(&(ctx->block), '\0', sizeof(struct status_block));
102 /* Default width of the separator block. */
103 ctx->block.sep_block_width = 9;
109 static int stdin_map_key(void *context, const unsigned char *key, size_t len) {
111 static int stdin_map_key(void *context, const unsigned char *key, unsigned int len) {
113 parser_ctx *ctx = context;
114 FREE(ctx->last_map_key);
115 sasprintf(&(ctx->last_map_key), "%.*s", len, key);
119 static int stdin_boolean(void *context, int val) {
120 parser_ctx *ctx = context;
121 if (strcasecmp(ctx->last_map_key, "urgent") == 0) {
122 ctx->block.urgent = val;
124 if (strcasecmp(ctx->last_map_key, "separator") == 0) {
125 ctx->block.no_separator = !val;
131 static int stdin_string(void *context, const unsigned char *val, size_t len) {
133 static int stdin_string(void *context, const unsigned char *val, unsigned int len) {
135 parser_ctx *ctx = context;
136 if (strcasecmp(ctx->last_map_key, "full_text") == 0) {
137 ctx->block.full_text = i3string_from_utf8_with_length((const char *)val, len);
139 if (strcasecmp(ctx->last_map_key, "color") == 0) {
140 sasprintf(&(ctx->block.color), "%.*s", len, val);
142 if (strcasecmp(ctx->last_map_key, "align") == 0) {
143 if (len == strlen("left") && !strncmp((const char*)val, "left", strlen("left"))) {
144 ctx->block.align = ALIGN_LEFT;
145 } else if (len == strlen("right") && !strncmp((const char*)val, "right", strlen("right"))) {
146 ctx->block.align = ALIGN_RIGHT;
148 ctx->block.align = ALIGN_CENTER;
155 static int stdin_integer(void *context, long long val) {
157 static int stdin_integer(void *context, long val) {
159 parser_ctx *ctx = context;
160 if (strcasecmp(ctx->last_map_key, "min_width") == 0) {
161 ctx->block.min_width = (uint32_t)val;
163 if (strcasecmp(ctx->last_map_key, "separator_block_width") == 0) {
164 ctx->block.sep_block_width = (uint32_t)val;
169 static int stdin_end_map(void *context) {
170 parser_ctx *ctx = context;
171 struct status_block *new_block = smalloc(sizeof(struct status_block));
172 memcpy(new_block, &(ctx->block), sizeof(struct status_block));
173 /* Ensure we have a full_text set, so that when it is missing (or null),
174 * i3bar doesn’t crash and the user gets an annoying message. */
175 if (!new_block->full_text)
176 new_block->full_text = i3string_from_utf8("SPEC VIOLATION (null)");
177 if (new_block->urgent)
178 ctx->has_urgent = true;
179 TAILQ_INSERT_TAIL(&statusline_head, new_block, blocks);
183 static int stdin_end_array(void *context) {
184 DLOG("dumping statusline:\n");
185 struct status_block *current;
186 TAILQ_FOREACH(current, &statusline_head, blocks) {
187 DLOG("full_text = %s\n", i3string_as_utf8(current->full_text));
188 DLOG("color = %s\n", current->color);
190 DLOG("end of dump\n");
195 * Helper function to read stdin
198 static unsigned char *get_buffer(ev_io *watcher, int *ret_buffer_len) {
199 int fd = watcher->fd;
202 int buffer_len = STDIN_CHUNK_SIZE;
203 unsigned char *buffer = smalloc(buffer_len+1);
206 n = read(fd, buffer + rec, buffer_len - rec);
208 if (errno == EAGAIN) {
212 ELOG("read() failed!: %s\n", strerror(errno));
216 /* end of file, kill the watcher */
217 ELOG("stdin: received EOF\n");
220 *ret_buffer_len = -1;
225 if (rec == buffer_len) {
226 buffer_len += STDIN_CHUNK_SIZE;
227 buffer = srealloc(buffer, buffer_len);
230 if (*buffer == '\0') {
234 *ret_buffer_len = rec;
238 static void read_flat_input(char *buffer, int length) {
239 struct status_block *first = TAILQ_FIRST(&statusline_head);
240 /* Clear the old buffer if any. */
241 I3STRING_FREE(first->full_text);
242 /* Remove the trailing newline and terminate the string at the same
244 if (buffer[length-1] == '\n' || buffer[length-1] == '\r')
245 buffer[length-1] = '\0';
246 else buffer[length] = '\0';
247 first->full_text = i3string_from_utf8(buffer);
250 static bool read_json_input(unsigned char *input, int length) {
251 yajl_status status = yajl_parse(parser, input, length);
252 bool has_urgent = false;
254 if (status != yajl_status_ok) {
256 if (status != yajl_status_ok && status != yajl_status_insufficient_data) {
258 fprintf(stderr, "[i3bar] Could not parse JSON input (code %d): %.*s\n",
259 status, length, input);
260 } else if (parser_context.has_urgent) {
267 * Callbalk for stdin. We read a line from stdin and store the result
271 void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
273 unsigned char *buffer = get_buffer(watcher, &rec);
276 bool has_urgent = false;
277 if (child.version > 0) {
278 has_urgent = read_json_input(buffer, rec);
280 read_flat_input((char*)buffer, rec);
283 draw_bars(has_urgent);
287 * Callbalk for stdin first line. We read the first line to detect
288 * whether this is JSON or plain text
291 void stdin_io_first_line_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
293 unsigned char *buffer = get_buffer(watcher, &rec);
296 DLOG("Detecting input type based on buffer *%.*s*\n", rec, buffer);
297 /* Detect whether this is JSON or plain text. */
298 unsigned int consumed = 0;
299 /* At the moment, we don’t care for the version. This might change
300 * in the future, but for now, we just discard it. */
301 parse_json_header(&child, buffer, rec, &consumed);
302 if (child.version > 0) {
303 /* If hide-on-modifier is set, we start of by sending the
304 * child a SIGSTOP, because the bars aren't mapped at start */
305 if (config.hide_on_modifier) {
308 read_json_input(buffer + consumed, rec - consumed);
310 /* In case of plaintext, we just add a single block and change its
311 * full_text pointer later. */
312 struct status_block *new_block = scalloc(sizeof(struct status_block));
313 TAILQ_INSERT_TAIL(&statusline_head, new_block, blocks);
314 read_flat_input((char*)buffer, rec);
317 ev_io_stop(main_loop, stdin_io);
318 ev_io_init(stdin_io, &stdin_io_cb, STDIN_FILENO, EV_READ);
319 ev_io_start(main_loop, stdin_io);
323 * We received a sigchild, meaning, that the child-process terminated.
324 * We simply free the respective data-structures and don't care for input
328 void child_sig_cb(struct ev_loop *loop, ev_child *watcher, int revents) {
329 ELOG("Child (pid: %d) unexpectedly exited with status %d\n",
336 * Start a child-process with the specified command and reroute stdin.
337 * We actually start a $SHELL to execute the command so we don't have to care
338 * about arguments and such
341 void start_child(char *command) {
342 /* Allocate a yajl parser which will be used to parse stdin. */
343 memset(&callbacks, '\0', sizeof(yajl_callbacks));
344 callbacks.yajl_map_key = stdin_map_key;
345 callbacks.yajl_boolean = stdin_boolean;
346 callbacks.yajl_string = stdin_string;
347 callbacks.yajl_integer = stdin_integer;
348 callbacks.yajl_start_array = stdin_start_array;
349 callbacks.yajl_end_array = stdin_end_array;
350 callbacks.yajl_start_map = stdin_start_map;
351 callbacks.yajl_end_map = stdin_end_map;
353 yajl_parser_config parse_conf = { 0, 0 };
355 parser = yajl_alloc(&callbacks, &parse_conf, NULL, (void*)&parser_context);
357 parser = yajl_alloc(&callbacks, NULL, &parser_context);
360 if (command != NULL) {
363 err(EXIT_FAILURE, "pipe(fd)");
368 ELOG("Couldn't fork(): %s\n", strerror(errno));
371 /* Child-process. Reroute stdout and start shell */
374 dup2(fd[1], STDOUT_FILENO);
376 static const char *shell = NULL;
378 if ((shell = getenv("SHELL")) == NULL)
381 execl(shell, shell, "-c", command, (char*) NULL);
384 /* Parent-process. Rerout stdin */
387 dup2(fd[0], STDIN_FILENO);
393 /* We set O_NONBLOCK because blocking is evil in event-driven software */
394 fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
396 stdin_io = smalloc(sizeof(ev_io));
397 ev_io_init(stdin_io, &stdin_io_first_line_cb, STDIN_FILENO, EV_READ);
398 ev_io_start(main_loop, stdin_io);
400 /* We must cleanup, if the child unexpectedly terminates */
401 child_sig = smalloc(sizeof(ev_child));
402 ev_child_init(child_sig, &child_sig_cb, child.pid, 0);
403 ev_child_start(main_loop, child_sig);
405 atexit(kill_child_at_exit);
409 * kill()s the child-process (if any). Called when exit()ing.
412 void kill_child_at_exit(void) {
414 if (child.cont_signal > 0 && child.stopped)
415 kill(child.pid, child.cont_signal);
416 kill(child.pid, SIGTERM);
421 * kill()s the child-process (if existent) and closes and
422 * free()s the stdin- and sigchild-watchers
425 void kill_child(void) {
427 if (child.cont_signal > 0 && child.stopped)
428 kill(child.pid, child.cont_signal);
429 kill(child.pid, SIGTERM);
431 waitpid(child.pid, &status, 0);
437 * Sends a SIGSTOP to the child-process (if existent)
440 void stop_child(void) {
441 if (child.stop_signal > 0 && !child.stopped) {
442 child.stopped = true;
443 kill(child.pid, child.stop_signal);
448 * Sends a SIGCONT to the child-process (if existent)
451 void cont_child(void) {
452 if (child.cont_signal > 0 && child.stopped) {
453 child.stopped = false;
454 kill(child.pid, child.cont_signal);