From 310ae2d0b5df345b06bc8014721691bc0f521e2f Mon Sep 17 00:00:00 2001 From: Quentin Glidic Date: Mon, 3 Sep 2012 10:11:01 +0200 Subject: [PATCH] i3bar: Handle the first line with another callback --- i3bar/src/child.c | 55 ++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/i3bar/src/child.c b/i3bar/src/child.c index 9236de3f..4c91da3d 100644 --- a/i3bar/src/child.c +++ b/i3bar/src/child.c @@ -32,7 +32,6 @@ ev_io *stdin_io; ev_child *child_sig; /* JSON parser for stdin */ -bool first_line = true; bool plaintext = false; yajl_callbacks callbacks; yajl_handle parser; @@ -225,27 +224,8 @@ void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) { unsigned char *buffer = get_buffer(watcher, &rec); if (buffer == NULL) return; - unsigned char *json_input = buffer; - if (first_line) { - DLOG("Detecting input type based on buffer *%.*s*\n", rec, buffer); - /* Detect whether this is JSON or plain text. */ - unsigned int consumed = 0; - /* At the moment, we don’t care for the version. This might change - * in the future, but for now, we just discard it. */ - plaintext = (determine_json_version(buffer, rec, &consumed) == -1); - if (plaintext) { - /* In case of plaintext, we just add a single block and change its - * full_text pointer later. */ - struct status_block *new_block = scalloc(sizeof(struct status_block)); - TAILQ_INSERT_TAIL(&statusline_head, new_block, blocks); - } else { - json_input += consumed; - rec -= consumed; - } - first_line = false; - } if (!plaintext) { - read_json_input(json_input, rec); + read_json_input(buffer, rec); } else { read_flat_input((char*)buffer, rec); } @@ -253,6 +233,37 @@ void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) { draw_bars(); } +/* + * Callbalk for stdin first line. We read the first line to detect + * whether this is JSON or plain text + * + */ +void stdin_io_first_line_cb(struct ev_loop *loop, ev_io *watcher, int revents) { + int rec; + unsigned char *buffer = get_buffer(watcher, &rec); + if (buffer == NULL) + return; + DLOG("Detecting input type based on buffer *%.*s*\n", rec, buffer); + /* Detect whether this is JSON or plain text. */ + unsigned int consumed = 0; + /* At the moment, we don’t care for the version. This might change + * in the future, but for now, we just discard it. */ + plaintext = (determine_json_version(buffer, rec, &consumed) == -1); + if (plaintext) { + /* In case of plaintext, we just add a single block and change its + * full_text pointer later. */ + struct status_block *new_block = scalloc(sizeof(struct status_block)); + TAILQ_INSERT_TAIL(&statusline_head, new_block, blocks); + read_flat_input((char*)buffer, rec); + } else { + read_json_input(buffer + consumed, rec - consumed); + } + free(buffer); + ev_io_stop(main_loop, stdin_io); + ev_io_init(stdin_io, &stdin_io_cb, STDIN_FILENO, EV_READ); + ev_io_start(main_loop, stdin_io); +} + /* * We received a sigchild, meaning, that the child-process terminated. * We simply free the respective data-structures and don't care for input @@ -333,7 +344,7 @@ void start_child(char *command) { fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); stdin_io = smalloc(sizeof(ev_io)); - ev_io_init(stdin_io, &stdin_io_cb, STDIN_FILENO, EV_READ); + ev_io_init(stdin_io, &stdin_io_first_line_cb, STDIN_FILENO, EV_READ); ev_io_start(main_loop, stdin_io); /* We must cleanup, if the child unexpectedly terminates */ -- 2.39.5