2 * i3bar - an xcb-based status- and ws-bar for i3
4 * © 2010-2011 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>
24 /* Global variables for child_*() */
27 /* stdin- and sigchild-watchers */
31 /* The buffer statusline points to */
32 char *statusline_buffer = NULL;
35 * Stop and free() the stdin- and sigchild-watchers
39 if (stdin_io != NULL) {
40 ev_io_stop(main_loop, stdin_io);
42 FREE(statusline_buffer);
45 if (child_sig != NULL) {
46 ev_child_stop(main_loop, child_sig);
52 * Callbalk for stdin. We read a line from stdin and store the result
56 void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
60 int buffer_len = STDIN_CHUNK_SIZE;
61 char *buffer = malloc(buffer_len);
64 n = read(fd, buffer + rec, buffer_len - rec);
66 if (errno == EAGAIN) {
67 /* remove trailing newline and finish up */
71 ELOG("read() failed!: %s\n", strerror(errno));
76 /* remove trailing newline and finish up */
80 /* end of file, kill the watcher */
87 if (rec == buffer_len) {
88 buffer_len += STDIN_CHUNK_SIZE;
89 buffer = realloc(buffer, buffer_len);
92 if (*buffer == '\0') {
96 FREE(statusline_buffer);
97 statusline = statusline_buffer = buffer;
98 for (n = 0; buffer[n] != '\0'; ++n) {
99 if (buffer[n] == '\n')
100 statusline = &buffer[n + 1];
102 DLOG("%s\n", statusline);
107 * We received a sigchild, meaning, that the child-process terminated.
108 * We simply free the respective data-structures and don't care for input
112 void child_sig_cb(struct ev_loop *loop, ev_child *watcher, int revents) {
113 DLOG("Child (pid: %d) unexpectedly exited with status %d\n",
120 * Start a child-process with the specified command and reroute stdin.
121 * We actually start a $SHELL to execute the command so we don't have to care
122 * about arguments and such
125 void start_child(char *command) {
127 if (command != NULL) {
133 ELOG("Couldn't fork(): %s\n", strerror(errno));
136 /* Child-process. Reroute stdout and start shell */
139 dup2(fd[1], STDOUT_FILENO);
141 static const char *shell = NULL;
143 if ((shell = getenv("SHELL")) == NULL)
146 execl(shell, shell, "-c", command, (char*) NULL);
149 /* Parent-process. Rerout stdin */
152 dup2(fd[0], STDIN_FILENO);
154 /* If hide-on-modifier is set, we start of by sending the
155 * child a SIGSTOP, because the bars aren't mapped at start */
156 if (config.hide_on_modifier) {
164 /* We set O_NONBLOCK because blocking is evil in event-driven software */
165 fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
167 stdin_io = malloc(sizeof(ev_io));
168 ev_io_init(stdin_io, &stdin_io_cb, STDIN_FILENO, EV_READ);
169 ev_io_start(main_loop, stdin_io);
171 /* We must cleanup, if the child unexpectedly terminates */
172 child_sig = malloc(sizeof(ev_child));
173 ev_child_init(child_sig, &child_sig_cb, child_pid, 0);
174 ev_child_start(main_loop, child_sig);
179 * kill()s the child-process (if existent) and closes and
180 * free()s the stdin- and sigchild-watchers
184 if (child_pid != 0) {
185 kill(child_pid, SIGCONT);
186 kill(child_pid, SIGTERM);
188 waitpid(child_pid, &status, 0);
195 * Sends a SIGSTOP to the child-process (if existent)
199 if (child_pid != 0) {
200 kill(child_pid, SIGSTOP);
205 * Sends a SIGCONT to the child-process (if existent)
209 if (child_pid != 0) {
210 kill(child_pid, SIGCONT);