]> git.sur5r.net Git - i3/i3/blobdiff - i3bar/src/child.c
Update changelog and copyright, bump version and more
[i3/i3] / i3bar / src / child.c
index 2b0fb780a77b1a27bd857188ea7846439c659b32..3f59d060f51979d15fd367a9444d8d3eb7f451a2 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * i3bar - an xcb-based status- and ws-bar for i3
  *
- * © 2010 Axel Wagner and contributors
+ * © 2010-2011 Axel Wagner and contributors
  *
  * See file LICNSE for license information
  *
@@ -11,6 +11,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <sys/types.h>
+#include <sys/wait.h>
 #include <signal.h>
 #include <stdio.h>
 #include <fcntl.h>
 
 #include "common.h"
 
+/* Global variables for child_*() */
+pid_t child_pid;
+
 /* stdin- and sigchild-watchers */
 ev_io    *stdin_io;
 ev_child *child_sig;
 
+/* The buffer statusline points to */
+char *statusline_buffer = NULL;
+
 /*
  * Stop and free() the stdin- and sigchild-watchers
  *
  */
 void cleanup() {
-    ev_io_stop(main_loop, stdin_io);
-    ev_child_stop(main_loop, child_sig);
-    FREE(stdin_io);
-    FREE(child_sig);
-    FREE(statusline);
-}
+    if (stdin_io != NULL) {
+        ev_io_stop(main_loop, stdin_io);
+        FREE(stdin_io);
+        FREE(statusline_buffer);
+    }
 
-/*
- * Since we don't use colors and stuff, we strip the dzen-formatstrings
- *
- */
-void strip_dzen_formats(char *buffer) {
-    char *src = buffer;
-    char *dest = buffer;
-    while (*src != '\0') {
-        /* ^ starts a format-string, ) ends it */
-        if (*src == '^') {
-            /* We replace the seperators from i3status by pipe-symbols */
-            if (!strncmp(src, "^ro", strlen("^ro"))) {
-                *(dest++) = ' ';
-                *(dest++) = '|';
-                *(dest++) = ' ';
-            }
-            while (*src != ')') {
-                src++;
-            }
-            src++;
-        } else {
-            *dest = *src;
-            src++;
-            dest++;
-        }
+    if (child_sig != NULL) {
+        ev_child_stop(main_loop, child_sig);
+        FREE(child_sig);
     }
-    /* The last character is \n, which xcb cannot display */
-    *(--dest) = '\0';
 }
 
 /*
- * Callbalk for stdin. We read a line from stdin, strip dzen-formats and store
- * the result in statusline
+ * Callbalk for stdin. We read a line from stdin and store the result
+ * in statusline
  *
  */
 void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
@@ -77,38 +59,47 @@ void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
     int rec = 0;
     int buffer_len = STDIN_CHUNK_SIZE;
     char *buffer = malloc(buffer_len);
-    memset(buffer, '\0', buffer_len);
+    buffer[0] = '\0';
     while(1) {
         n = read(fd, buffer + rec, buffer_len - rec);
         if (n == -1) {
             if (errno == EAGAIN) {
+                /* remove trailing newline and finish up */
+                buffer[rec-1] = '\0';
                 break;
             }
-            printf("ERROR: read() failed!");
+            ELOG("read() failed!: %s\n", strerror(errno));
             exit(EXIT_FAILURE);
         }
         if (n == 0) {
-            if (rec == buffer_len) {
-                char *tmp = buffer;
-                buffer = malloc(buffer_len + STDIN_CHUNK_SIZE);
-                memset(buffer, '\0', buffer_len);
-                strncpy(buffer, tmp, buffer_len);
-                buffer_len += STDIN_CHUNK_SIZE;
-                FREE(tmp);
-            } else {
-                break;
+            if (rec != 0) {
+                /* remove trailing newline and finish up */
+                buffer[rec-1] = '\0';
             }
+
+            /* end of file, kill the watcher */
+            DLOG("stdin: EOF\n");
+            cleanup();
+            break;
         }
         rec += n;
+
+        if (rec == buffer_len) {
+            buffer_len += STDIN_CHUNK_SIZE;
+            buffer = realloc(buffer, buffer_len);
+           }
     }
-    if (strlen(buffer) == 0) {
+    if (*buffer == '\0') {
         FREE(buffer);
         return;
     }
-    strip_dzen_formats(buffer);
-    FREE(statusline);
-    statusline = buffer;
-    printf("%s\n", buffer);
+    FREE(statusline_buffer);
+    statusline = statusline_buffer = buffer;
+    for (n = 0; buffer[n] != '\0'; ++n) {
+        if (buffer[n] == '\n')
+            statusline = &buffer[n + 1];
+    }
+    DLOG("%s\n", statusline);
     draw_bars();
 }
 
@@ -119,7 +110,7 @@ void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
  *
  */
 void child_sig_cb(struct ev_loop *loop, ev_child *watcher, int revents) {
-    printf("Child (pid: %d) unexpectedly exited with status %d\n",
+    DLOG("Child (pid: %d) unexpectedly exited with status %d\n",
            child_pid,
            watcher->rstatus);
     cleanup();
@@ -139,9 +130,10 @@ void start_child(char *command) {
         child_pid = fork();
         switch (child_pid) {
             case -1:
-                printf("ERROR: Couldn't fork()");
+                ELOG("Couldn't fork(): %s\n", strerror(errno));
                 exit(EXIT_FAILURE);
             case 0:
+                /* Child-process. Reroute stdout and start shell */
                 close(fd[0]);
 
                 dup2(fd[1], STDOUT_FILENO);
@@ -154,14 +146,22 @@ void start_child(char *command) {
                 execl(shell, shell, "-c", command, (char*) NULL);
                 return;
             default:
+                /* Parent-process. Rerout stdin */
                 close(fd[1]);
 
                 dup2(fd[0], STDIN_FILENO);
 
+                /* If hide-on-modifier is set, we start of by sending the
+                 * child a SIGSTOP, because the bars aren't mapped at start */
+                if (config.hide_on_modifier) {
+                    stop_child();
+                }
+
                 break;
         }
     }
 
+    /* We set O_NONBLOCK because blocking is evil in event-driven software */
     fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
 
     stdin_io = malloc(sizeof(ev_io));
@@ -169,20 +169,44 @@ void start_child(char *command) {
     ev_io_start(main_loop, stdin_io);
 
     /* We must cleanup, if the child unexpectedly terminates */
-    child_sig = malloc(sizeof(ev_io));
+    child_sig = malloc(sizeof(ev_child));
     ev_child_init(child_sig, &child_sig_cb, child_pid, 0);
     ev_child_start(main_loop, child_sig);
 
 }
 
 /*
- * kill()s the child-prozess (if existend) and closes and
+ * kill()s the child-process (if existent) and closes and
  * free()s the stdin- and sigchild-watchers
  *
  */
 void kill_child() {
     if (child_pid != 0) {
-        kill(child_pid, SIGQUIT);
+        kill(child_pid, SIGCONT);
+        kill(child_pid, SIGTERM);
+        int status;
+        waitpid(child_pid, &status, 0);
+        child_pid = 0;
+        cleanup();
+    }
+}
+
+/*
+ * Sends a SIGSTOP to the child-process (if existent)
+ *
+ */
+void stop_child() {
+    if (child_pid != 0) {
+        kill(child_pid, SIGSTOP);
+    }
+}
+
+/*
+ * Sends a SIGCONT to the child-process (if existent)
+ *
+ */
+void cont_child() {
+    if (child_pid != 0) {
+        kill(child_pid, SIGCONT);
     }
-    cleanup();
 }