Retain PM8909 MBHC digital debounce clock (DIG_CLK_CTL_D_MBHC_CLK_EN) and master bias (CDC_A_MASTER_BIAS_CTL = 0x30) across s2 standby gating while keeping the 100 uA button ISRC and button IRQs disabled when unplugged, check CDC_A_MBHC_RESULT_2 on boot/set_jack/switch IRQs with 3/4-pole FSM classification, fix iface=CARD jack queries, event coalescing, and stale unplugged Headphones boot fallback in bananarchy-soundmonitor and sxmo audio scripts, harden host tests against CPU contention, and rebuild all a1125 (ampere) target images.
694 lines
18 KiB
C
694 lines
18 KiB
C
#ifndef _GNU_SOURCE
|
|
#define _GNU_SOURCE
|
|
#endif
|
|
/* Compiled audio jack + PipeWire monitor replacing sxmo_soundmonitor.sh + evtest. */
|
|
#include <ctype.h>
|
|
#include <limits.h>
|
|
#include <poll.h>
|
|
|
|
#include "common.h"
|
|
|
|
static volatile sig_atomic_t stop_requested = 0;
|
|
static volatile pid_t pactl_pid = -1;
|
|
static int internal_synced = 0;
|
|
static int last_hp_plugged = -1;
|
|
static int last_mic_plugged = -1;
|
|
|
|
static void handle_signal(int sig)
|
|
{
|
|
pid_t child = pactl_pid;
|
|
(void)sig;
|
|
stop_requested = 1;
|
|
if(child > 0)
|
|
kill(child, SIGTERM);
|
|
}
|
|
|
|
static void spawn_detached(char *const argv[])
|
|
{
|
|
pid_t mid = fork();
|
|
if(mid == 0) {
|
|
pid_t leaf;
|
|
setsid();
|
|
leaf = fork();
|
|
if(leaf == 0) {
|
|
int nullfd = open("/dev/null", O_RDWR | O_CLOEXEC);
|
|
if(nullfd >= 0) {
|
|
dup2(nullfd, STDIN_FILENO);
|
|
dup2(nullfd, STDOUT_FILENO);
|
|
dup2(nullfd, STDERR_FILENO);
|
|
if(nullfd > STDERR_FILENO)
|
|
close(nullfd);
|
|
}
|
|
execvp(argv[0], argv);
|
|
_exit(127);
|
|
}
|
|
_exit(0);
|
|
}
|
|
if(mid > 0) {
|
|
int st;
|
|
while(waitpid(mid, &st, 0) < 0 && errno == EINTR) {
|
|
}
|
|
}
|
|
}
|
|
|
|
static void run_sync_quiet(char *const argv[])
|
|
{
|
|
pid_t pid = fork();
|
|
int st = 0;
|
|
if(pid == 0) {
|
|
int nullfd = open("/dev/null", O_RDWR | O_CLOEXEC);
|
|
if(nullfd >= 0) {
|
|
dup2(nullfd, STDIN_FILENO);
|
|
dup2(nullfd, STDOUT_FILENO);
|
|
dup2(nullfd, STDERR_FILENO);
|
|
if(nullfd > STDERR_FILENO)
|
|
close(nullfd);
|
|
}
|
|
execvp(argv[0], argv);
|
|
_exit(127);
|
|
}
|
|
if(pid > 0) {
|
|
while(waitpid(pid, &st, 0) < 0 && errno == EINTR) {
|
|
}
|
|
}
|
|
}
|
|
|
|
static int run_capture(char *const argv[], char *out, size_t out_len)
|
|
{
|
|
int pfd[2];
|
|
pid_t pid;
|
|
size_t total = 0;
|
|
int st = 0;
|
|
if(!out || out_len == 0)
|
|
return -1;
|
|
out[0] = '\0';
|
|
if(pipe2(pfd, O_CLOEXEC) < 0)
|
|
return -1;
|
|
pid = fork();
|
|
if(pid == 0) {
|
|
int nullfd = open("/dev/null", O_RDWR | O_CLOEXEC);
|
|
close(pfd[0]);
|
|
dup2(pfd[1], STDOUT_FILENO);
|
|
if(pfd[1] > STDERR_FILENO)
|
|
close(pfd[1]);
|
|
if(nullfd >= 0) {
|
|
dup2(nullfd, STDIN_FILENO);
|
|
dup2(nullfd, STDERR_FILENO);
|
|
if(nullfd > STDERR_FILENO)
|
|
close(nullfd);
|
|
}
|
|
execvp(argv[0], argv);
|
|
_exit(127);
|
|
}
|
|
close(pfd[1]);
|
|
if(pid < 0) {
|
|
close(pfd[0]);
|
|
return -1;
|
|
}
|
|
while(total < out_len - 1) {
|
|
ssize_t n = read(pfd[0], out + total, out_len - 1 - total);
|
|
if(n > 0) {
|
|
total += (size_t)n;
|
|
continue;
|
|
}
|
|
if(n < 0 && errno == EINTR)
|
|
continue;
|
|
break;
|
|
}
|
|
out[total] = '\0';
|
|
close(pfd[0]);
|
|
while(waitpid(pid, &st, 0) < 0 && errno == EINTR) {
|
|
}
|
|
return WIFEXITED(st) ? WEXITSTATUS(st) : -1;
|
|
}
|
|
|
|
static int str_contains_ci(const char *haystack, const char *needle)
|
|
{
|
|
size_t nlen = strlen(needle);
|
|
size_t i;
|
|
if(nlen == 0)
|
|
return 1;
|
|
for(i = 0; haystack[i]; i++) {
|
|
size_t j = 0;
|
|
while(j < nlen && haystack[i + j] &&
|
|
tolower((unsigned char)haystack[i + j]) ==
|
|
tolower((unsigned char)needle[j])) {
|
|
j++;
|
|
}
|
|
if(j == nlen)
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int open_jack_dev(char *dev_path, size_t dev_len)
|
|
{
|
|
glob_t gl;
|
|
size_t i;
|
|
int fd = -1;
|
|
dev_path[0] = '\0';
|
|
if(glob("/sys/class/input/event*/device/name", 0, NULL, &gl) != 0)
|
|
return -1;
|
|
for(i = 0; i < gl.gl_pathc; i++) {
|
|
char name[128];
|
|
const char *ev;
|
|
char *slash;
|
|
char ev_name[32];
|
|
if(read_first_line(gl.gl_pathv[i], name, sizeof(name)) < 0)
|
|
continue;
|
|
if(!str_contains_ci(name, "jack") && !str_contains_ci(name, "headset"))
|
|
continue;
|
|
ev = strstr(gl.gl_pathv[i], "event");
|
|
if(!ev)
|
|
continue;
|
|
slash = strchr(ev, '/');
|
|
if(!slash || (size_t)(slash - ev) >= sizeof(ev_name))
|
|
continue;
|
|
memcpy(ev_name, ev, (size_t)(slash - ev));
|
|
ev_name[slash - ev] = '\0';
|
|
snprintf(dev_path, dev_len, "/dev/input/%s", ev_name);
|
|
fd = open(dev_path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
|
|
if(fd >= 0)
|
|
break;
|
|
}
|
|
globfree(&gl);
|
|
return fd;
|
|
}
|
|
|
|
static int query_ev_sw(int fd, unsigned int code, const char *amixer_name)
|
|
{
|
|
if(fd >= 0) {
|
|
unsigned char sw[(SW_MAX + 7) / 8 + 1];
|
|
memset(sw, 0, sizeof(sw));
|
|
if(ioctl(fd, BA_EVIOCGSW(sizeof(sw)), sw) >= 0)
|
|
return (sw[code / 8] & (1u << (code % 8))) != 0;
|
|
}
|
|
{
|
|
char out[256];
|
|
char *const argv[] = {
|
|
(char *)"amixer", (char *)"-c", (char *)"0",
|
|
(char *)"cget", (char *)amixer_name, NULL
|
|
};
|
|
if(run_capture(argv, out, sizeof(out)) == 0 &&
|
|
strstr(out, ": values=on") != NULL) {
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static void find_bt_sink(char *out, size_t out_len)
|
|
{
|
|
char raw[2048];
|
|
char *line;
|
|
char *save = NULL;
|
|
char *const argv[] = {
|
|
(char *)"pactl", (char *)"list", (char *)"short", (char *)"sinks", NULL
|
|
};
|
|
out[0] = '\0';
|
|
if(run_capture(argv, raw, sizeof(raw)) != 0)
|
|
return;
|
|
for(line = strtok_r(raw, "\n", &save); line; line = strtok_r(NULL, "\n", &save)) {
|
|
char *c1 = strtok(line, " \t");
|
|
char *c2 = c1 ? strtok(NULL, " \t") : NULL;
|
|
if(c2 && strncmp(c2, "bluez_output.", 13) == 0) {
|
|
safe_copy(out, out_len, c2);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
static int has_internal_sink(void)
|
|
{
|
|
char raw[2048];
|
|
char *line;
|
|
char *save = NULL;
|
|
char *const argv[] = {
|
|
(char *)"pactl", (char *)"list", (char *)"short", (char *)"sinks", NULL
|
|
};
|
|
if(run_capture(argv, raw, sizeof(raw)) != 0)
|
|
return 0;
|
|
for(line = strtok_r(raw, "\n", &save); line; line = strtok_r(NULL, "\n", &save)) {
|
|
char *c1 = strtok(line, " \t");
|
|
char *c2 = c1 ? strtok(NULL, " \t") : NULL;
|
|
if(c2 &&
|
|
(strncmp(c2, "alsa_output.platform-7702000.", 29) == 0 ||
|
|
strncmp(c2, "alsa_output.platform-sound.", 27) == 0)) {
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static void sxmo_audio_cmd(const char *arg1, const char *arg2)
|
|
{
|
|
if(arg2) {
|
|
char *const argv[] = {
|
|
(char *)"sxmo_audio.sh", (char *)"device",
|
|
(char *)arg1, (char *)arg2, NULL
|
|
};
|
|
run_sync_quiet(argv);
|
|
} else {
|
|
char *const argv[] = {
|
|
(char *)"sxmo_audio.sh", (char *)"device", (char *)arg1, NULL
|
|
};
|
|
run_sync_quiet(argv);
|
|
}
|
|
}
|
|
|
|
static void write_state_file(const char *path, const char *val)
|
|
{
|
|
FILE *f = fopen(path, "w");
|
|
if(!f)
|
|
return;
|
|
fputs(val, f);
|
|
fputc('\n', f);
|
|
fclose(f);
|
|
}
|
|
|
|
static void sanitize_unplugged_boot_state(void)
|
|
{
|
|
char buf[64] = "";
|
|
write_state_file("/tmp/sxmo_jack_plugged", "0");
|
|
if(read_first_line("/tmp/sxmo_audio_out_device", buf, sizeof(buf)) == 0 &&
|
|
strcmp(buf, "Headphones") == 0) {
|
|
unlink("/tmp/sxmo_audio_out_device");
|
|
}
|
|
buf[0] = '\0';
|
|
if(read_first_line("/tmp/sxmo_audio_in_device", buf, sizeof(buf)) == 0 &&
|
|
strcmp(buf, "Headset") == 0) {
|
|
unlink("/tmp/sxmo_audio_in_device");
|
|
}
|
|
}
|
|
|
|
static void handle_jack_insert(int jack_fd)
|
|
{
|
|
char cur_out[64] = "";
|
|
int mic_plugged;
|
|
char *const get_argv[] = {
|
|
(char *)"sxmo_audio.sh", (char *)"device", (char *)"get", NULL
|
|
};
|
|
if(run_capture(get_argv, cur_out, sizeof(cur_out)) != 0 || cur_out[0] == '\0') {
|
|
safe_copy(cur_out, sizeof(cur_out), "Speaker");
|
|
} else {
|
|
cur_out[strcspn(cur_out, "\r\n")] = '\0';
|
|
}
|
|
if(strcmp(cur_out, "Headphones") != 0)
|
|
write_state_file("/tmp/sxmo_prev_out_device", cur_out);
|
|
mic_plugged = query_ev_sw(jack_fd, SW_MICROPHONE_INSERT,
|
|
"iface=CARD,name='Mic Jack'");
|
|
write_state_file("/tmp/sxmo_jack_plugged", "1");
|
|
write_state_file("/tmp/sxmo_audio_in_device",
|
|
mic_plugged ? "Headset" : "Mic1");
|
|
sxmo_audio_cmd("set", "Headphones");
|
|
last_hp_plugged = 1;
|
|
last_mic_plugged = mic_plugged;
|
|
}
|
|
|
|
static void handle_jack_remove(void)
|
|
{
|
|
char incall[16] = "";
|
|
char bt_sink[128] = "";
|
|
const char *target = "Speaker";
|
|
write_state_file("/tmp/sxmo_jack_plugged", "0");
|
|
write_state_file("/tmp/sxmo_audio_in_device", "Mic1");
|
|
find_bt_sink(bt_sink, sizeof(bt_sink));
|
|
if(bt_sink[0] != '\0') {
|
|
target = "Bluetooth";
|
|
} else if(read_first_line("/tmp/sxmo_in_call", incall, sizeof(incall)) == 0 &&
|
|
strcmp(incall, "1") == 0) {
|
|
target = "Earpiece";
|
|
}
|
|
sxmo_audio_cmd("set", target);
|
|
last_hp_plugged = 0;
|
|
last_mic_plugged = 0;
|
|
}
|
|
|
|
static int drain_jack_events(int fd, int *saw_sw)
|
|
{
|
|
struct input_event ev;
|
|
ssize_t n;
|
|
while((n = read(fd, &ev, sizeof(ev))) == (ssize_t)sizeof(ev)) {
|
|
if(ev.type == EV_SW &&
|
|
(ev.code == SW_HEADPHONE_INSERT || ev.code == SW_MICROPHONE_INSERT)) {
|
|
*saw_sw = 1;
|
|
}
|
|
}
|
|
if(n < 0 && errno != EAGAIN && errno != EWOULDBLOCK)
|
|
return -1;
|
|
return 0;
|
|
}
|
|
|
|
static void handle_jack_events(int *jack_fd_ptr)
|
|
{
|
|
int fd = *jack_fd_ptr;
|
|
int saw_sw = 0;
|
|
int hp;
|
|
int mic;
|
|
|
|
if(drain_jack_events(fd, &saw_sw) < 0) {
|
|
close(fd);
|
|
*jack_fd_ptr = -1;
|
|
last_hp_plugged = -1;
|
|
last_mic_plugged = -1;
|
|
return;
|
|
}
|
|
if(!saw_sw)
|
|
return;
|
|
|
|
/* Coalesce 4-pole insert/remove SW_HEADPHONE + SW_MICROPHONE bounce pairs. */
|
|
{
|
|
struct timespec ts = { 0, 50000000L };
|
|
nanosleep(&ts, NULL);
|
|
}
|
|
if(drain_jack_events(fd, &saw_sw) < 0) {
|
|
close(fd);
|
|
*jack_fd_ptr = -1;
|
|
last_hp_plugged = -1;
|
|
last_mic_plugged = -1;
|
|
return;
|
|
}
|
|
|
|
hp = query_ev_sw(fd, SW_HEADPHONE_INSERT, "iface=CARD,name='Headphone Jack'");
|
|
mic = query_ev_sw(fd, SW_MICROPHONE_INSERT, "iface=CARD,name='Mic Jack'");
|
|
if(hp && last_hp_plugged != 1) {
|
|
handle_jack_insert(fd);
|
|
} else if(!hp && last_hp_plugged != 0) {
|
|
handle_jack_remove();
|
|
} else if(hp && mic != last_mic_plugged) {
|
|
last_mic_plugged = mic;
|
|
write_state_file("/tmp/sxmo_audio_in_device", mic ? "Headset" : "Mic1");
|
|
sxmo_audio_cmd("setinput", mic ? "Headset" : "Mic1");
|
|
}
|
|
}
|
|
|
|
static int start_pactl_subscribe(char *prev_bt, size_t prev_bt_len)
|
|
{
|
|
int pfd[2];
|
|
pid_t pid;
|
|
char cur_out[64] = "";
|
|
|
|
find_bt_sink(prev_bt, prev_bt_len);
|
|
if(prev_bt[0] != '\0') {
|
|
read_first_line("/tmp/sxmo_audio_out_device", cur_out, sizeof(cur_out));
|
|
if(strcmp(cur_out, "Bluetooth") != 0)
|
|
sxmo_audio_cmd("set", "Bluetooth");
|
|
else
|
|
sxmo_audio_cmd("apply", NULL);
|
|
}
|
|
|
|
if(pipe2(pfd, O_NONBLOCK | O_CLOEXEC) < 0)
|
|
return -1;
|
|
pid = fork();
|
|
if(pid == 0) {
|
|
int nullfd = open("/dev/null", O_RDWR | O_CLOEXEC);
|
|
close(pfd[0]);
|
|
dup2(pfd[1], STDOUT_FILENO);
|
|
if(pfd[1] > STDERR_FILENO)
|
|
close(pfd[1]);
|
|
if(nullfd >= 0) {
|
|
dup2(nullfd, STDIN_FILENO);
|
|
dup2(nullfd, STDERR_FILENO);
|
|
if(nullfd > STDERR_FILENO)
|
|
close(nullfd);
|
|
}
|
|
execlp("pactl", "pactl", "subscribe", (char *)NULL);
|
|
_exit(127);
|
|
}
|
|
close(pfd[1]);
|
|
if(pid < 0) {
|
|
close(pfd[0]);
|
|
return -1;
|
|
}
|
|
pactl_pid = pid;
|
|
return pfd[0];
|
|
}
|
|
|
|
static void sync_reopened_jack(int jack_fd)
|
|
{
|
|
if(query_ev_sw(jack_fd, SW_HEADPHONE_INSERT,
|
|
"iface=CARD,name='Headphone Jack'")) {
|
|
if(last_hp_plugged != 1)
|
|
handle_jack_insert(jack_fd);
|
|
} else if(last_hp_plugged == 1) {
|
|
handle_jack_remove();
|
|
} else {
|
|
sanitize_unplugged_boot_state();
|
|
last_hp_plugged = 0;
|
|
last_mic_plugged = 0;
|
|
}
|
|
}
|
|
|
|
static void dispatch_pactl_line(const char *line, char *prev_bt, size_t prev_bt_len,
|
|
int jack_fd)
|
|
{
|
|
if((strstr(line, "Event 'new' on sink") != NULL) ||
|
|
(strstr(line, "Event 'new' on card") != NULL) ||
|
|
(strstr(line, "Event 'change' on card") != NULL)) {
|
|
char bt_sink[128] = "";
|
|
find_bt_sink(bt_sink, sizeof(bt_sink));
|
|
if(bt_sink[0] != '\0' && strcmp(bt_sink, prev_bt) != 0) {
|
|
safe_copy(prev_bt, prev_bt_len, bt_sink);
|
|
sxmo_audio_cmd("set", "Bluetooth");
|
|
}
|
|
if(!internal_synced && has_internal_sink()) {
|
|
internal_synced = 1;
|
|
if(bt_sink[0] == '\0') {
|
|
if(query_ev_sw(jack_fd, SW_HEADPHONE_INSERT,
|
|
"iface=CARD,name='Headphone Jack'")) {
|
|
handle_jack_insert(jack_fd);
|
|
} else {
|
|
sanitize_unplugged_boot_state();
|
|
last_hp_plugged = 0;
|
|
last_mic_plugged = 0;
|
|
sxmo_audio_cmd("apply", NULL);
|
|
}
|
|
}
|
|
}
|
|
{
|
|
char *const sb_argv[] = {
|
|
(char *)"sxmo_hook_statusbar.sh", (char *)"volume", NULL
|
|
};
|
|
spawn_detached(sb_argv);
|
|
}
|
|
} else if((strstr(line, "Event 'remove' on sink") != NULL) ||
|
|
(strstr(line, "Event 'remove' on card") != NULL)) {
|
|
char bt_sink[128] = "";
|
|
find_bt_sink(bt_sink, sizeof(bt_sink));
|
|
if(internal_synced && !has_internal_sink())
|
|
internal_synced = 0;
|
|
if(bt_sink[0] == '\0' && prev_bt[0] != '\0') {
|
|
char cur_out[64] = "";
|
|
prev_bt[0] = '\0';
|
|
read_first_line("/tmp/sxmo_audio_out_device", cur_out, sizeof(cur_out));
|
|
if(strcmp(cur_out, "Bluetooth") == 0) {
|
|
char jbuf[16] = "";
|
|
if(read_first_line("/tmp/sxmo_jack_plugged", jbuf, sizeof(jbuf)) == 0 &&
|
|
strcmp(jbuf, "1") == 0) {
|
|
sxmo_audio_cmd("set", "Headphones");
|
|
} else if(access("/tmp/sxmo_in_call", R_OK) == 0) {
|
|
sxmo_audio_cmd("set", "Earpiece");
|
|
} else {
|
|
sxmo_audio_cmd("set", "Speaker");
|
|
}
|
|
}
|
|
}
|
|
{
|
|
char *const sb_argv[] = {
|
|
(char *)"sxmo_hook_statusbar.sh", (char *)"volume", NULL
|
|
};
|
|
spawn_detached(sb_argv);
|
|
}
|
|
} else if((strstr(line, "Event 'change' on sink") != NULL) ||
|
|
(strstr(line, "Event 'change' on source") != NULL)) {
|
|
if(access("/tmp/sxmo_volset.lock", F_OK) != 0) {
|
|
char *const sb_argv[] = {
|
|
(char *)"sxmo_hook_statusbar.sh", (char *)"volume", NULL
|
|
};
|
|
spawn_detached(sb_argv);
|
|
}
|
|
}
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
struct sigaction sa;
|
|
int inotify_fd;
|
|
int jack_fd;
|
|
char jack_path[64] = "";
|
|
int pactl_fd;
|
|
char prev_bt[128] = "";
|
|
char pactl_buf[4096];
|
|
size_t pactl_len = 0;
|
|
long long pactl_retry_at = -1;
|
|
long long jack_retry_at = -1;
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
sa.sa_handler = handle_signal;
|
|
sigemptyset(&sa.sa_mask);
|
|
sigaction(SIGTERM, &sa, NULL);
|
|
sigaction(SIGINT, &sa, NULL);
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
inotify_fd = open_input_inotify();
|
|
jack_fd = open_jack_dev(jack_path, sizeof(jack_path));
|
|
if(query_ev_sw(jack_fd, SW_HEADPHONE_INSERT,
|
|
"iface=CARD,name='Headphone Jack'")) {
|
|
handle_jack_insert(jack_fd);
|
|
} else {
|
|
char bt_sink[128] = "";
|
|
sanitize_unplugged_boot_state();
|
|
last_hp_plugged = 0;
|
|
last_mic_plugged = 0;
|
|
find_bt_sink(bt_sink, sizeof(bt_sink));
|
|
if(bt_sink[0] != '\0')
|
|
sxmo_audio_cmd("set", "Bluetooth");
|
|
else
|
|
sxmo_audio_cmd("apply", NULL);
|
|
}
|
|
internal_synced = has_internal_sink();
|
|
if(jack_fd < 0)
|
|
jack_retry_at = now_ms() + 5000;
|
|
|
|
pactl_fd = start_pactl_subscribe(prev_bt, sizeof(prev_bt));
|
|
if(pactl_fd < 0)
|
|
pactl_retry_at = now_ms() + 10000;
|
|
|
|
while(!stop_requested) {
|
|
struct pollfd fds[3];
|
|
int nfds = 0;
|
|
int idx_inot = -1;
|
|
int idx_jack = -1;
|
|
int idx_pactl = -1;
|
|
int timeout_ms = -1;
|
|
long long now = now_ms();
|
|
|
|
if(inotify_fd >= 0) {
|
|
idx_inot = nfds;
|
|
fds[nfds].fd = inotify_fd;
|
|
fds[nfds].events = POLLIN;
|
|
fds[nfds].revents = 0;
|
|
nfds++;
|
|
}
|
|
if(jack_fd >= 0) {
|
|
idx_jack = nfds;
|
|
fds[nfds].fd = jack_fd;
|
|
fds[nfds].events = POLLIN;
|
|
fds[nfds].revents = 0;
|
|
nfds++;
|
|
}
|
|
if(pactl_fd >= 0) {
|
|
idx_pactl = nfds;
|
|
fds[nfds].fd = pactl_fd;
|
|
fds[nfds].events = POLLIN;
|
|
fds[nfds].revents = 0;
|
|
nfds++;
|
|
}
|
|
|
|
if(pactl_retry_at > 0) {
|
|
int rem = (pactl_retry_at > now) ? (int)(pactl_retry_at - now) : 0;
|
|
if(timeout_ms < 0 || rem < timeout_ms)
|
|
timeout_ms = rem;
|
|
}
|
|
if(jack_retry_at > 0 && jack_fd < 0) {
|
|
int rem = (jack_retry_at > now) ? (int)(jack_retry_at - now) : 0;
|
|
if(timeout_ms < 0 || rem < timeout_ms)
|
|
timeout_ms = rem;
|
|
}
|
|
|
|
if(nfds == 0) {
|
|
struct timespec ts = { 1, 0 };
|
|
nanosleep(&ts, NULL);
|
|
} else if(poll(fds, (nfds_t)nfds, timeout_ms) < 0) {
|
|
if(errno == EINTR)
|
|
continue;
|
|
break;
|
|
}
|
|
|
|
if(stop_requested)
|
|
break;
|
|
|
|
if(idx_inot >= 0 && (fds[idx_inot].revents & POLLIN) &&
|
|
drain_inotify(inotify_fd) && jack_fd < 0) {
|
|
jack_fd = open_jack_dev(jack_path, sizeof(jack_path));
|
|
if(jack_fd >= 0) {
|
|
jack_retry_at = -1;
|
|
sync_reopened_jack(jack_fd);
|
|
}
|
|
}
|
|
|
|
if(idx_jack >= 0 && (fds[idx_jack].revents & (POLLIN | POLLERR | POLLHUP))) {
|
|
handle_jack_events(&jack_fd);
|
|
if(jack_fd < 0)
|
|
jack_retry_at = now_ms() + 5000;
|
|
}
|
|
|
|
if(idx_pactl >= 0 && (fds[idx_pactl].revents & (POLLIN | POLLERR | POLLHUP))) {
|
|
int eof = 0;
|
|
for(;;) {
|
|
ssize_t n;
|
|
if(pactl_len >= sizeof(pactl_buf) - 1)
|
|
pactl_len = 0;
|
|
n = read(pactl_fd, pactl_buf + pactl_len,
|
|
sizeof(pactl_buf) - 1 - pactl_len);
|
|
if(n > 0) {
|
|
char *nl;
|
|
pactl_len += (size_t)n;
|
|
pactl_buf[pactl_len] = '\0';
|
|
while((nl = strchr(pactl_buf, '\n')) != NULL) {
|
|
size_t rest;
|
|
*nl = '\0';
|
|
dispatch_pactl_line(pactl_buf, prev_bt, sizeof(prev_bt), jack_fd);
|
|
rest = (size_t)(pactl_buf + pactl_len - (nl + 1));
|
|
memmove(pactl_buf, nl + 1, rest);
|
|
pactl_len = rest;
|
|
pactl_buf[pactl_len] = '\0';
|
|
}
|
|
continue;
|
|
}
|
|
if(n == 0 || (errno != EAGAIN && errno != EWOULDBLOCK))
|
|
eof = 1;
|
|
break;
|
|
}
|
|
if(eof) {
|
|
close(pactl_fd);
|
|
pactl_fd = -1;
|
|
if(pactl_pid > 0) {
|
|
int st;
|
|
waitpid(pactl_pid, &st, WNOHANG);
|
|
pactl_pid = -1;
|
|
}
|
|
pactl_retry_at = now_ms() + 10000;
|
|
}
|
|
}
|
|
|
|
now = now_ms();
|
|
if(jack_fd < 0 && jack_retry_at > 0 && now >= jack_retry_at) {
|
|
jack_fd = open_jack_dev(jack_path, sizeof(jack_path));
|
|
if(jack_fd >= 0) {
|
|
jack_retry_at = -1;
|
|
sync_reopened_jack(jack_fd);
|
|
} else {
|
|
jack_retry_at = now + 5000;
|
|
}
|
|
}
|
|
if(pactl_fd < 0 && pactl_retry_at > 0 && now >= pactl_retry_at) {
|
|
pactl_fd = start_pactl_subscribe(prev_bt, sizeof(prev_bt));
|
|
pactl_retry_at = (pactl_fd < 0) ? (now + 10000) : -1;
|
|
}
|
|
}
|
|
|
|
if(pactl_pid > 0) {
|
|
int st;
|
|
kill(pactl_pid, SIGTERM);
|
|
waitpid(pactl_pid, &st, 0);
|
|
}
|
|
if(pactl_fd >= 0)
|
|
close(pactl_fd);
|
|
if(jack_fd >= 0)
|
|
close(jack_fd);
|
|
if(inotify_fd >= 0)
|
|
close(inotify_fd);
|
|
return 0;
|
|
}
|