2 * i3bar - an xcb-based status- and ws-bar for i3
4 * © 2010 Axel Wagner and contributors
6 * See file LICNSE for license information
8 * src/child.c: Getting Input for the statusline
13 #include <sys/types.h>
23 /* Global variables for child_*() */
26 /* stdin- and sigchild-watchers */
30 /* The buffer statusline points to */
31 char *statusline_buffer = NULL;
34 * Stop and free() the stdin- and sigchild-watchers
38 ev_io_stop(main_loop, stdin_io);
39 ev_child_stop(main_loop, child_sig);
42 FREE(statusline_buffer);
46 * Callbalk for stdin. We read a line from stdin and store the result
50 void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
54 int buffer_len = STDIN_CHUNK_SIZE;
55 char *buffer = malloc(buffer_len);
58 n = read(fd, buffer + rec, buffer_len - rec);
60 if (errno == EAGAIN) {
61 /* remove trailing newline and finish up */
65 ELOG("read() failed!: %s\n", strerror(errno));
69 if (rec == buffer_len) {
70 buffer_len += STDIN_CHUNK_SIZE;
71 buffer = realloc(buffer, buffer_len);
74 /* remove trailing newline and finish up */
82 if (*buffer == '\0') {
86 FREE(statusline_buffer);
87 statusline = statusline_buffer = buffer;
88 for (n = 0; buffer[n] != '\0'; ++n) {
89 if (buffer[n] == '\n')
90 statusline = &buffer[n + 1];
92 DLOG("%s\n", statusline);
97 * We received a sigchild, meaning, that the child-process terminated.
98 * We simply free the respective data-structures and don't care for input
102 void child_sig_cb(struct ev_loop *loop, ev_child *watcher, int revents) {
103 DLOG("Child (pid: %d) unexpectedly exited with status %d\n",
110 * Start a child-process with the specified command and reroute stdin.
111 * We actually start a $SHELL to execute the command so we don't have to care
112 * about arguments and such
115 void start_child(char *command) {
117 if (command != NULL) {
123 ELOG("Couldn't fork(): %s\n", strerror(errno));
126 /* Child-process. Reroute stdout and start shell */
129 dup2(fd[1], STDOUT_FILENO);
131 static const char *shell = NULL;
133 if ((shell = getenv("SHELL")) == NULL)
136 execl(shell, shell, "-c", command, (char*) NULL);
139 /* Parent-process. Rerout stdin */
142 dup2(fd[0], STDIN_FILENO);
144 /* If hide-on-modifier is set, we start of by sending the
145 * child a SIGSTOP, because the bars aren't mapped at start */
152 /* We set O_NONBLOCK because blocking is evil in event-driven software */
153 fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
155 stdin_io = malloc(sizeof(ev_io));
156 ev_io_init(stdin_io, &stdin_io_cb, STDIN_FILENO, EV_READ);
157 ev_io_start(main_loop, stdin_io);
159 /* We must cleanup, if the child unexpectedly terminates */
160 child_sig = malloc(sizeof(ev_child));
161 ev_child_init(child_sig, &child_sig_cb, child_pid, 0);
162 ev_child_start(main_loop, child_sig);
167 * kill()s the child-process (if existent) and closes and
168 * free()s the stdin- and sigchild-watchers
172 if (child_pid != 0) {
173 kill(child_pid, SIGCONT);
174 kill(child_pid, SIGTERM);
180 * Sends a SIGSTOP to the child-process (if existent)
184 if (child_pid != 0) {
185 kill(child_pid, SIGSTOP);
190 * Sends a SIGCONT to the child-process (if existent)
194 if (child_pid != 0) {
195 kill(child_pid, SIGCONT);