13 /* stdin- and sigchild-watchers */
18 * Stop and free() the stdin- and sigchild-watchers
22 ev_io_stop(main_loop, stdin_io);
23 ev_child_stop(main_loop, child_sig);
30 * Since we don't use colors and stuff, we strip the dzen-formatstrings
33 void strip_dzen_formats(char *buffer) {
36 while (*src != '\0') {
37 /* ^ starts a format-string, ) ends it */
39 /* We replace the seperators from i3status by pipe-symbols */
40 if (!strncmp(src, "^ro", strlen("^ro"))) {
55 /* The last character is \n, which xcb cannot display */
60 * Callbalk for stdin. We read a line from stdin, strip dzen-formats and store
61 * the result in statusline
64 void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
68 int buffer_len = STDIN_CHUNK_SIZE;
69 char *buffer = malloc(buffer_len);
70 memset(buffer, '\0', buffer_len);
72 n = read(fd, buffer + rec, buffer_len - rec);
74 if (errno == EAGAIN) {
77 printf("ERROR: read() failed!");
81 if (rec == buffer_len) {
83 buffer = malloc(buffer_len + STDIN_CHUNK_SIZE);
84 memset(buffer, '\0', buffer_len);
85 strncpy(buffer, tmp, buffer_len);
86 buffer_len += STDIN_CHUNK_SIZE;
94 if (strlen(buffer) == 0) {
98 strip_dzen_formats(buffer);
101 printf("%s\n", buffer);
106 * We received a sigchild, meaning, that the child-process terminated.
107 * We simply free the respective data-structures and don't care for input
111 void child_sig_cb(struct ev_loop *loop, ev_child *watcher, int revents) {
112 printf("Child (pid: %d) unexpectedly exited with status %d\n",
119 * Start a child-process with the specified command and reroute stdin.
120 * We actually start a $SHELL to execute the command so we don't have to care
121 * about arguments and such
124 void start_child(char *command) {
126 if (command != NULL) {
132 printf("ERROR: Couldn't fork()");
137 dup2(fd[1], STDOUT_FILENO);
139 static const char *shell = NULL;
141 if ((shell = getenv("SHELL")) == NULL)
144 execl(shell, shell, "-c", command, (char*) NULL);
149 dup2(fd[0], STDIN_FILENO);
155 fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
157 stdin_io = malloc(sizeof(ev_io));
158 ev_io_init(stdin_io, &stdin_io_cb, STDIN_FILENO, EV_READ);
159 ev_io_start(main_loop, stdin_io);
161 /* We must cleanup, if the child unexpectedly terminates */
162 child_sig = malloc(sizeof(ev_io));
163 ev_child_init(child_sig, &child_sig_cb, child_pid, 0);
164 ev_child_start(main_loop, child_sig);
169 * kill()s the child-prozess (if existend) and closes and
170 * free()s the stdin- and sigchild-watchers
174 if (child_pid != 0) {
175 kill(child_pid, SIGQUIT);