gui: mark all fds used for select() as non-blocking.
[paraslash.git] / gui_common.c
1 #include "para.h"
2 #include "fd.h"
3
4 extern const char *status_item_list[NUM_STAT_ITEMS];
5
6
7 int para_open_audiod_pipe(char *cmd)
8 {
9 int fds[3] = {0, 1, 0};
10 pid_t pid;
11 int ret = para_exec_cmdline_pid(&pid, cmd, fds);
12 if (ret < 0)
13 return ret;
14 ret = mark_fd_nonblock(fds[1]);
15 if (ret > 0)
16 return fds[1];
17 close(fds[1]);
18 return ret;
19 }
20
21 int read_audiod_pipe(int fd, void (*line_handler)(char *) )
22 {
23 static char buf[STRINGSIZE];
24 const ssize_t bufsize = sizeof(buf) - 1;
25 static ssize_t loaded;
26 ssize_t ret;
27
28 if (loaded >= bufsize)
29 loaded = 0;
30 ret = read(fd, buf + loaded, bufsize - loaded);
31 if (ret > 0) {
32 loaded += ret;
33 buf[loaded] = '\0';
34 loaded = for_each_line(buf, loaded, line_handler);
35 }
36 return ret;
37 }