]> git.sur5r.net Git - i3/i3/blobdiff - i3bar/src/child.c
i3bar: add modelines to all files
[i3/i3] / i3bar / src / child.c
index 7cd364e0391d3b407db084dfb2957b0cea81027e..ff1bc8bb63922c94e14378767c92024969a78fc6 100644 (file)
@@ -1,7 +1,9 @@
 /*
+ * vim:ts=4:sw=4:expandtab
+ *
  * 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 +13,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <sys/types.h>
+#include <sys/wait.h>
 #include <signal.h>
 #include <stdio.h>
 #include <fcntl.h>
@@ -27,16 +30,26 @@ pid_t child_pid;
 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);
+       /* statusline pointed to memory within statusline_buffer */
+       statusline = NULL;
+    }
+
+    if (child_sig != NULL) {
+        ev_child_stop(main_loop, child_sig);
+        FREE(child_sig);
+    }
 }
 
 /*
@@ -50,7 +63,7 @@ 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) {
@@ -59,28 +72,39 @@ void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
                 buffer[rec-1] = '\0';
                 break;
             }
-            ELOG("read() failed!\n");
+            ELOG("read() failed!: %s\n", strerror(errno));
             exit(EXIT_FAILURE);
         }
         if (n == 0) {
-            if (rec == buffer_len) {
-                buffer_len += STDIN_CHUNK_SIZE;
-                buffer = realloc(buffer, buffer_len);
-            } else {
+            if (rec != 0) {
                 /* remove trailing newline and finish up */
                 buffer[rec-1] = '\0';
-                break;
             }
+
+            /* end of file, kill the watcher */
+            ELOG("stdin: received EOF\n");
+            cleanup();
+            draw_bars();
+            return;
         }
         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;
     }
-    FREE(statusline);
-    statusline = buffer;
-    DLOG("%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();
 }
 
@@ -91,7 +115,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) {
-    DLOG("Child (pid: %d) unexpectedly exited with status %d\n",
+    ELOG("Child (pid: %d) unexpectedly exited with status %d\n",
            child_pid,
            watcher->rstatus);
     cleanup();
@@ -111,7 +135,7 @@ void start_child(char *command) {
         child_pid = fork();
         switch (child_pid) {
             case -1:
-                ELOG("Couldn't fork()\n");
+                ELOG("Couldn't fork(): %s\n", strerror(errno));
                 exit(EXIT_FAILURE);
             case 0:
                 /* Child-process. Reroute stdout and start shell */
@@ -132,6 +156,12 @@ void start_child(char *command) {
 
                 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;
         }
     }
@@ -157,9 +187,13 @@ void start_child(char *command) {
  */
 void kill_child() {
     if (child_pid != 0) {
+        kill(child_pid, SIGCONT);
         kill(child_pid, SIGTERM);
+        int status;
+        waitpid(child_pid, &status, 0);
+        child_pid = 0;
+        cleanup();
     }
-    cleanup();
 }
 
 /*