2 * vim:ts=4:sw=4:expandtab
4 * i3bar - an xcb-based status- and ws-bar for i3
6 * © 2010-2011 Axel Wagner and contributors
8 * See file LICNSE for license information
10 * src/child.c: Getting Input for the statusline
15 #include <sys/types.h>
26 /* Global variables for child_*() */
29 /* stdin- and sigchild-watchers */
33 /* The buffer statusline points to */
34 char *statusline_buffer = NULL;
37 * Stop and free() the stdin- and sigchild-watchers
41 if (stdin_io != NULL) {
42 ev_io_stop(main_loop, stdin_io);
44 FREE(statusline_buffer);
45 /* statusline pointed to memory within statusline_buffer */
49 if (child_sig != NULL) {
50 ev_child_stop(main_loop, child_sig);
56 * Callbalk for stdin. We read a line from stdin and store the result
60 void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
64 int buffer_len = STDIN_CHUNK_SIZE;
65 char *buffer = malloc(buffer_len);
68 n = read(fd, buffer + rec, buffer_len - rec);
70 if (errno == EAGAIN) {
71 /* remove trailing newline and finish up */
75 ELOG("read() failed!: %s\n", strerror(errno));
80 /* remove trailing newline and finish up */
84 /* end of file, kill the watcher */
85 ELOG("stdin: received EOF\n");
92 if (rec == buffer_len) {
93 buffer_len += STDIN_CHUNK_SIZE;
94 buffer = realloc(buffer, buffer_len);
97 if (*buffer == '\0') {
101 FREE(statusline_buffer);
102 statusline = statusline_buffer = buffer;
103 for (n = 0; buffer[n] != '\0'; ++n) {
104 if (buffer[n] == '\n')
105 statusline = &buffer[n + 1];
107 DLOG("%s\n", statusline);
112 * We received a sigchild, meaning, that the child-process terminated.
113 * We simply free the respective data-structures and don't care for input
117 void child_sig_cb(struct ev_loop *loop, ev_child *watcher, int revents) {
118 ELOG("Child (pid: %d) unexpectedly exited with status %d\n",
125 * Start a child-process with the specified command and reroute stdin.
126 * We actually start a $SHELL to execute the command so we don't have to care
127 * about arguments and such
130 void start_child(char *command) {
132 if (command != NULL) {
138 ELOG("Couldn't fork(): %s\n", strerror(errno));
141 /* Child-process. Reroute stdout and start shell */
144 dup2(fd[1], STDOUT_FILENO);
146 static const char *shell = NULL;
148 if ((shell = getenv("SHELL")) == NULL)
151 execl(shell, shell, "-c", command, (char*) NULL);
154 /* Parent-process. Rerout stdin */
157 dup2(fd[0], STDIN_FILENO);
159 /* If hide-on-modifier is set, we start of by sending the
160 * child a SIGSTOP, because the bars aren't mapped at start */
161 if (config.hide_on_modifier) {
169 /* We set O_NONBLOCK because blocking is evil in event-driven software */
170 fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
172 stdin_io = malloc(sizeof(ev_io));
173 ev_io_init(stdin_io, &stdin_io_cb, STDIN_FILENO, EV_READ);
174 ev_io_start(main_loop, stdin_io);
176 /* We must cleanup, if the child unexpectedly terminates */
177 child_sig = malloc(sizeof(ev_child));
178 ev_child_init(child_sig, &child_sig_cb, child_pid, 0);
179 ev_child_start(main_loop, child_sig);
184 * kill()s the child-process (if existent) and closes and
185 * free()s the stdin- and sigchild-watchers
189 if (child_pid != 0) {
190 kill(child_pid, SIGCONT);
191 kill(child_pid, SIGTERM);
193 waitpid(child_pid, &status, 0);
200 * Sends a SIGSTOP to the child-process (if existent)
204 if (child_pid != 0) {
205 kill(child_pid, SIGSTOP);
210 * Sends a SIGCONT to the child-process (if existent)
214 if (child_pid != 0) {
215 kill(child_pid, SIGCONT);