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);
43 /* statusline pointed to memory within statusline_buffer */
47 if (child_sig != NULL) {
48 ev_child_stop(main_loop, child_sig);
54 * Callbalk for stdin. We read a line from stdin and store the result
58 void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
62 int buffer_len = STDIN_CHUNK_SIZE;
63 char *buffer = malloc(buffer_len);
66 n = read(fd, buffer + rec, buffer_len - rec);
68 if (errno == EAGAIN) {
69 /* remove trailing newline and finish up */
73 ELOG("read() failed!: %s\n", strerror(errno));
78 /* remove trailing newline and finish up */
82 /* end of file, kill the watcher */
83 ELOG("stdin: received EOF\n");
90 if (rec == buffer_len) {
91 buffer_len += STDIN_CHUNK_SIZE;
92 buffer = realloc(buffer, buffer_len);
95 if (*buffer == '\0') {
99 FREE(statusline_buffer);
100 statusline = statusline_buffer = buffer;
101 for (n = 0; buffer[n] != '\0'; ++n) {
102 if (buffer[n] == '\n')
103 statusline = &buffer[n + 1];
105 DLOG("%s\n", statusline);
110 * We received a sigchild, meaning, that the child-process terminated.
111 * We simply free the respective data-structures and don't care for input
115 void child_sig_cb(struct ev_loop *loop, ev_child *watcher, int revents) {
116 ELOG("Child (pid: %d) unexpectedly exited with status %d\n",
123 * Start a child-process with the specified command and reroute stdin.
124 * We actually start a $SHELL to execute the command so we don't have to care
125 * about arguments and such
128 void start_child(char *command) {
130 if (command != NULL) {
136 ELOG("Couldn't fork(): %s\n", strerror(errno));
139 /* Child-process. Reroute stdout and start shell */
142 dup2(fd[1], STDOUT_FILENO);
144 static const char *shell = NULL;
146 if ((shell = getenv("SHELL")) == NULL)
149 execl(shell, shell, "-c", command, (char*) NULL);
152 /* Parent-process. Rerout stdin */
155 dup2(fd[0], STDIN_FILENO);
157 /* If hide-on-modifier is set, we start of by sending the
158 * child a SIGSTOP, because the bars aren't mapped at start */
159 if (config.hide_on_modifier) {
167 /* We set O_NONBLOCK because blocking is evil in event-driven software */
168 fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
170 stdin_io = malloc(sizeof(ev_io));
171 ev_io_init(stdin_io, &stdin_io_cb, STDIN_FILENO, EV_READ);
172 ev_io_start(main_loop, stdin_io);
174 /* We must cleanup, if the child unexpectedly terminates */
175 child_sig = malloc(sizeof(ev_child));
176 ev_child_init(child_sig, &child_sig_cb, child_pid, 0);
177 ev_child_start(main_loop, child_sig);
182 * kill()s the child-process (if existent) and closes and
183 * free()s the stdin- and sigchild-watchers
187 if (child_pid != 0) {
188 kill(child_pid, SIGCONT);
189 kill(child_pid, SIGTERM);
191 waitpid(child_pid, &status, 0);
198 * Sends a SIGSTOP to the child-process (if existent)
202 if (child_pid != 0) {
203 kill(child_pid, SIGSTOP);
208 * Sends a SIGCONT to the child-process (if existent)
212 if (child_pid != 0) {
213 kill(child_pid, SIGCONT);