summaryrefslogtreecommitdiff
path: root/gui_common.c
blob: 26c3d61b73feacd5b77d45e86767944ef5680564 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "para.h"
#include "fd.h"

extern const char *status_item_list[NUM_STAT_ITEMS];

int para_open_audiod_pipe(char *cmd)
{
	int fds[3] = {0, 1, 0};
	pid_t pid;
	int ret = para_exec_cmdline_pid(&pid, cmd, fds);
	if (ret < 0)
		return ret;
	ret = mark_fd_nonblock(fds[1]);
	if (ret > 0)
		return fds[1];
	close(fds[1]);
	return ret;
}

int read_audiod_pipe(int fd, void (*line_handler)(char *) )
{
	static char buf[4096];
	const ssize_t bufsize = sizeof(buf) - 1;
	static ssize_t loaded;
	ssize_t ret;

	if (loaded >= bufsize)
		loaded = 0;
	ret = read(fd, buf + loaded, bufsize - loaded);
	if (ret > 0) {
		loaded += ret;
		buf[loaded] = '\0';
		loaded = for_each_line(buf, loaded, line_handler);
	}
	return ret;
}