2 * vim:ts=4:sw=4:expandtab
4 * i3bar - an xcb-based status- and ws-bar for i3
5 * © 2010-2011 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>
26 /* Global variables for child_*() */
29 /* stdin- and sigchild-watchers */
33 /* JSON parser for stdin */
34 bool first_line = true;
35 bool plaintext = false;
36 yajl_callbacks callbacks;
39 typedef struct parser_ctx {
40 /* A copy of the last JSON map key. */
43 /* The current block. Will be filled, then copied and put into the list of
45 struct status_block block;
48 parser_ctx parser_context;
50 /* The buffer statusline points to */
51 struct statusline_head statusline_head = TAILQ_HEAD_INITIALIZER(statusline_head);
52 char *statusline_buffer = NULL;
55 * Stop and free() the stdin- and sigchild-watchers
59 if (stdin_io != NULL) {
60 ev_io_stop(main_loop, stdin_io);
62 FREE(statusline_buffer);
63 /* statusline pointed to memory within statusline_buffer */
67 if (child_sig != NULL) {
68 ev_child_stop(main_loop, child_sig);
74 * The start of a new array is the start of a new status line, so we clear all
78 static int stdin_start_array(void *context) {
79 struct status_block *first;
80 while (!TAILQ_EMPTY(&statusline_head)) {
81 first = TAILQ_FIRST(&statusline_head);
82 FREE(first->full_text);
84 TAILQ_REMOVE(&statusline_head, first, blocks);
91 * The start of a map is the start of a single block of the status line.
94 static int stdin_start_map(void *context) {
95 parser_ctx *ctx = context;
96 memset(&(ctx->block), '\0', sizeof(struct status_block));
100 static int stdin_map_key(void *context, const unsigned char *key, size_t len) {
101 parser_ctx *ctx = context;
102 FREE(ctx->last_map_key);
103 sasprintf(&(ctx->last_map_key), "%.*s", len, key);
107 static int stdin_string(void *context, const unsigned char *val, size_t len) {
108 parser_ctx *ctx = context;
109 if (strcasecmp(ctx->last_map_key, "full_text") == 0) {
110 sasprintf(&(ctx->block.full_text), "%.*s", len, val);
112 if (strcasecmp(ctx->last_map_key, "color") == 0) {
113 sasprintf(&(ctx->block.color), "%.*s", len, val);
118 static int stdin_end_map(void *context) {
119 parser_ctx *ctx = context;
120 struct status_block *new_block = smalloc(sizeof(struct status_block));
121 memcpy(new_block, &(ctx->block), sizeof(struct status_block));
122 TAILQ_INSERT_TAIL(&statusline_head, new_block, blocks);
126 static int stdin_end_array(void *context) {
127 DLOG("dumping statusline:\n");
128 struct status_block *current;
129 TAILQ_FOREACH(current, &statusline_head, blocks) {
130 DLOG("full_text = %s\n", current->full_text);
131 DLOG("color = %s\n", current->color);
133 DLOG("end of dump\n");
138 * Callbalk for stdin. We read a line from stdin and store the result
142 void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
143 int fd = watcher->fd;
146 int buffer_len = STDIN_CHUNK_SIZE;
147 unsigned char *buffer = smalloc(buffer_len);
150 n = read(fd, buffer + rec, buffer_len - rec);
152 if (errno == EAGAIN) {
156 ELOG("read() failed!: %s\n", strerror(errno));
160 /* end of file, kill the watcher */
161 ELOG("stdin: received EOF\n");
168 if (rec == buffer_len) {
169 buffer_len += STDIN_CHUNK_SIZE;
170 buffer = srealloc(buffer, buffer_len);
173 if (*buffer == '\0') {
178 unsigned char *json_input = buffer;
180 DLOG("Detecting input type based on buffer *%.*s*\n", rec, buffer);
181 /* Detect whether this is JSON or plain text. */
182 plaintext = (strncasecmp((char*)buffer, "{\"version\":", strlen("{\"version\":")) != 0);
184 /* In case of plaintext, we just add a single block and change its
185 * full_text pointer later. */
186 struct status_block *new_block = scalloc(sizeof(struct status_block));
187 TAILQ_INSERT_TAIL(&statusline_head, new_block, blocks);
189 /* At the moment, we don’t care for the version. This might change
190 * in the future, but for now, we just discard it. */
191 while (*json_input != '\n' && *json_input != '\0') {
199 yajl_status status = yajl_parse(parser, json_input, rec);
200 if (status != yajl_status_ok) {
201 fprintf(stderr, "[i3bar] Could not parse JSON input: %.*s\n",
206 struct status_block *first = TAILQ_FIRST(&statusline_head);
207 /* Remove the trailing newline and terminate the string at the same
209 buffer[rec-1] = '\0';
210 first->full_text = (char*)buffer;
216 * We received a sigchild, meaning, that the child-process terminated.
217 * We simply free the respective data-structures and don't care for input
221 void child_sig_cb(struct ev_loop *loop, ev_child *watcher, int revents) {
222 ELOG("Child (pid: %d) unexpectedly exited with status %d\n",
229 * Start a child-process with the specified command and reroute stdin.
230 * We actually start a $SHELL to execute the command so we don't have to care
231 * about arguments and such
234 void start_child(char *command) {
235 /* Allocate a yajl parser which will be used to parse stdin. */
236 memset(&callbacks, '\0', sizeof(yajl_callbacks));
237 callbacks.yajl_map_key = stdin_map_key;
238 callbacks.yajl_string = stdin_string;
239 callbacks.yajl_start_array = stdin_start_array;
240 callbacks.yajl_end_array = stdin_end_array;
241 callbacks.yajl_start_map = stdin_start_map;
242 callbacks.yajl_end_map = stdin_end_map;
243 parser = yajl_alloc(&callbacks, NULL, &parser_context);
246 if (command != NULL) {
249 err(EXIT_FAILURE, "pipe(fd)");
254 ELOG("Couldn't fork(): %s\n", strerror(errno));
257 /* Child-process. Reroute stdout and start shell */
260 dup2(fd[1], STDOUT_FILENO);
262 static const char *shell = NULL;
264 if ((shell = getenv("SHELL")) == NULL)
267 execl(shell, shell, "-c", command, (char*) NULL);
270 /* Parent-process. Rerout stdin */
273 dup2(fd[0], STDIN_FILENO);
275 /* If hide-on-modifier is set, we start of by sending the
276 * child a SIGSTOP, because the bars aren't mapped at start */
277 if (config.hide_on_modifier) {
285 /* We set O_NONBLOCK because blocking is evil in event-driven software */
286 fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
288 stdin_io = smalloc(sizeof(ev_io));
289 ev_io_init(stdin_io, &stdin_io_cb, STDIN_FILENO, EV_READ);
290 ev_io_start(main_loop, stdin_io);
292 /* We must cleanup, if the child unexpectedly terminates */
293 child_sig = smalloc(sizeof(ev_child));
294 ev_child_init(child_sig, &child_sig_cb, child_pid, 0);
295 ev_child_start(main_loop, child_sig);
300 * kill()s the child-process (if existent) and closes and
301 * free()s the stdin- and sigchild-watchers
305 if (child_pid != 0) {
306 kill(child_pid, SIGCONT);
307 kill(child_pid, SIGTERM);
309 waitpid(child_pid, &status, 0);
316 * Sends a SIGSTOP to the child-process (if existent)
320 if (child_pid != 0) {
321 kill(child_pid, SIGSTOP);
326 * Sends a SIGCONT to the child-process (if existent)
330 if (child_pid != 0) {
331 kill(child_pid, SIGCONT);