i3bar: Handle the first line with another callback

This commit is contained in:
Quentin Glidic 2012-09-03 10:11:01 +02:00 committed by Michael Stapelberg
parent f691927aa7
commit 310ae2d0b5
1 changed files with 33 additions and 22 deletions

View File

@ -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,8 +224,25 @@ 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) {
if (!plaintext) {
read_json_input(buffer, rec);
} else {
read_flat_input((char*)buffer, rec);
}
free(buffer);
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;
@ -238,19 +254,14 @@ void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
* 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);
} else {
read_flat_input((char*)buffer, rec);
} else {
read_json_input(buffer + consumed, rec - consumed);
}
free(buffer);
draw_bars();
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);
}
/*
@ -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 */