Implement 0010 RPM/L2/WCD/UART deep-standby, Adreno 304 ICC/SMMU fixes, and C11 autostart daemons

- patches/kernel/0010-msm8909-rpm-lpm-bimc-wcd-ldo-uart.patch and device DTS/DTB files (nokia-{argon,beatles,beatles-cover,coconut,sparkler,leo}): vMPM APSS RPM sleep, L2 SAW2 collapse, WCD-digital runtime PM, PM8909 LDO seeding, ttyMSM0 disable, dynamic msm_gpu gfx-mem BIMC interconnect scaling (mas_oxili), and qcom_iommu context restore + atomic TLB flush on resume.
- Replace Python/shell autostart loops with compiled C11 ARM ELF daemons (bananarchy-dsds, bananarchy-modemmonitor, bananarchy-soundmonitor, bananarchy-notifmonitor, bananarchy-run-aligned, bananarchy-session-loop) plus scripts/test-dsds-daemon.py.
- Update scripts/check-rebuild.sh, CHANGELOG.md, BATTERY-OPTIMIZATION-ANALYSIS.md, MANUAL.md, README.md, and AGENTS.md with verified a1114 (carbonara) image/archive pins and guards.
This commit is contained in:
Luxferre committed 2026-11-13 18:23:14 +02:00
1 parent 96c66bf0c6
commit 2ae4ef34a2
86 files changed
+99754 -94563

No files matched your search

File diff suppressed because it is too large. Load diff
@@ -0,0 +1,908 @@
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
/* Compiled ModemManager D-Bus monitor & DSDS supervisor replacing sxmo_modemmonitor.sh. */
#include <dirent.h>
#include <limits.h>
#include <poll.h>
#include <regex.h>
#include "common.h"
static volatile sig_atomic_t stop_requested = 0;
static volatile pid_t mm_pid = -1;
static volatile pid_t mms_pid = -1;
static volatile pid_t vvm_pid = -1;
static volatile pid_t dsds_pid = -1;
static int sig_pipe[2] = { -1, -1 };
static void handle_stop(int sig)
{
(void)sig;
stop_requested = 1;
if(sig_pipe[1] >= 0) {
char b = 'q';
ssize_t r = write(sig_pipe[1], &b, 1);
(void)r;
}
}
static void handle_sigchld(int sig)
{
(void)sig;
if(sig_pipe[1] >= 0) {
char b = 'c';
ssize_t r = write(sig_pipe[1], &b, 1);
(void)r;
}
}
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 void resolve_notifdir(char *out, size_t len)
{
const char *env_dir = getenv("SXMO_NOTIFDIR");
const char *xdg_data = getenv("XDG_DATA_HOME");
const char *home = getenv("HOME");
if(env_dir && *env_dir && strcmp(env_dir, "/") != 0) {
safe_copy(out, len, env_dir);
return;
}
if(xdg_data && *xdg_data) {
snprintf(out, len, "%s/sxmo/notifications", xdg_data);
return;
}
snprintf(out, len, "%s/.local/share/sxmo/notifications",
(home && *home) ? home : "/home/user");
}
static void cleanup_session_files(void)
{
const char *xdg_rt = getenv("XDG_RUNTIME_DIR");
const char *rt = (xdg_rt && *xdg_rt) ? xdg_rt : "/run/user/10000";
char dir_path[PATH_MAX];
char notif_dir[PATH_MAX];
DIR *dir;
struct dirent *de;
snprintf(dir_path, sizeof(dir_path), "%s/sxmo_calls", rt);
dir = opendir(dir_path);
if(dir) {
while((de = readdir(dir)) != NULL) {
if(strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
continue;
unlinkat(dirfd(dir), de->d_name, 0);
}
closedir(dir);
}
snprintf(dir_path, sizeof(dir_path), "%s/sxmo.ring.pid", rt);
unlink(dir_path);
resolve_notifdir(notif_dir, sizeof(notif_dir));
dir = opendir(notif_dir);
if(dir) {
while((de = readdir(dir)) != NULL) {
if(strncmp(de->d_name, "incomingcall", 12) != 0)
continue;
unlinkat(dirfd(dir), de->d_name, 0);
}
closedir(dir);
}
}
static const char *statenum_to_name(const char *s)
{
while(*s == ' ' || *s == '\t')
s++;
if(strcmp(s, "int32 -1") == 0) return "failed";
if(strcmp(s, "int32 0") == 0) return "unknown";
if(strcmp(s, "int32 1") == 0) return "initializing";
if(strcmp(s, "int32 2") == 0) return "locked";
if(strcmp(s, "int32 3") == 0) return "disabled";
if(strcmp(s, "int32 4") == 0) return "disabling";
if(strcmp(s, "int32 5") == 0) return "enabling";
if(strcmp(s, "int32 6") == 0) return "enabled";
if(strcmp(s, "int32 7") == 0) return "searching";
if(strcmp(s, "int32 8") == 0) return "registered";
if(strcmp(s, "int32 9") == 0) return "disconnecting";
if(strcmp(s, "int32 10") == 0) return "connecting";
if(strcmp(s, "int32 11") == 0) return "connected";
return "ERROR";
}
static void get_mm_state(char *out, size_t len)
{
char raw[4096];
char *line;
char *save = NULL;
char *const argv[] = {
(char *)"mmcli", (char *)"-m", (char *)"any", (char *)"-K", 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)) {
if(strncmp(line, "modem.generic.state ", 20) == 0) {
char *colon = strchr(line, ':');
if(colon) {
colon++;
while(*colon == ' ' || *colon == '\t')
colon++;
safe_copy(out, len, colon);
}
break;
}
}
}
static int grep_regex(const char *text, const char *pattern)
{
regex_t re;
int rc;
if(regcomp(&re, pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE) != 0)
return 0;
rc = regexec(&re, text, 0, NULL, 0);
regfree(&re);
return rc == 0;
}
static void spawn_uim_cmd(const char *subcmd, char *out, size_t out_len)
{
if(geteuid() == 0) {
char *const argv[] = {
(char *)"/usr/libexec/msm-modem-uim-selection", (char *)subcmd, NULL
};
if(out)
run_capture(argv, out, out_len);
else
run_sync_quiet(argv);
} else {
char *const argv[] = {
(char *)"doas", (char *)"-n",
(char *)"/usr/libexec/msm-modem-uim-selection", (char *)subcmd, NULL
};
if(out)
run_capture(argv, out, out_len);
else
run_sync_quiet(argv);
}
}
static void spawn_boot_probe(void)
{
pid_t mid = fork();
if(mid == 0) {
pid_t leaf;
setsid();
leaf = fork();
if(leaf == 0) {
char newstate[64] = "";
char uim_st[2048] = "";
int tries;
for(tries = 0; tries < 15; tries++) {
get_mm_state(newstate, sizeof(newstate));
if(newstate[0] != '\0')
break;
sleep(2);
}
spawn_uim_cmd("status", uim_st, sizeof(uim_st));
if(grep_regex(uim_st,
"SLOT[12]_PIN1_STATE=(enabled-not-verified|blocked)|"
"SLOT[12]_APP_STATE=(pin1|puk1)")) {
safe_copy(newstate, sizeof(newstate), "locked");
} else if(strcmp(newstate, "locked") == 0 &&
grep_regex(uim_st, "^MODEM_AVAILABLE=1$")) {
get_mm_state(newstate, sizeof(newstate));
if(strcmp(newstate, "locked") == 0)
safe_copy(newstate, sizeof(newstate), "disabled");
} else if(grep_regex(uim_st, "SLOT[12]_STATE=present") &&
grep_regex(uim_st, "^PRIMARY_GW_SLOT=0$")) {
spawn_uim_cmd("boot", NULL, 0);
get_mm_state(newstate, sizeof(newstate));
} else if(grep_regex(uim_st, "^SLOT1_STATE=present$") &&
grep_regex(uim_st, "^SLOT2_STATE=present$") &&
grep_regex(uim_st, "^SECONDARY_GW_SLOT=0$")) {
spawn_uim_cmd("boot", NULL, 0);
}
if(newstate[0] != '\0') {
char *const hook_argv[] = {
(char *)"sxmo_hook_modem.sh", (char *)"boot",
newstate, (char *)"0", NULL
};
char *const sb_argv[] = {
(char *)"sxmo_hook_statusbar.sh", (char *)"modem", NULL
};
run_sync_quiet(hook_argv);
run_sync_quiet(sb_argv);
}
_exit(0);
}
_exit(0);
}
if(mid > 0) {
int st;
while(waitpid(mid, &st, 0) < 0 && errno == EINTR) {
}
}
}
static void dispatch_modem_state_async(const char *oldstate,
const char *newstate,
const char *reason)
{
pid_t mid = fork();
if(mid == 0) {
pid_t leaf;
setsid();
leaf = fork();
if(leaf == 0) {
char old_copy[64];
char new_copy[64];
char reason_copy[64];
const char *p = reason;
if(strncmp(p, "uint32 ", 7) == 0)
p += 7;
safe_copy(old_copy, sizeof(old_copy), oldstate);
safe_copy(new_copy, sizeof(new_copy), newstate);
safe_copy(reason_copy, sizeof(reason_copy), p);
{
char *const hook_argv[] = {
(char *)"sxmo_hook_modem.sh", old_copy, new_copy, reason_copy, NULL
};
char *const sb_argv[] = {
(char *)"sxmo_hook_statusbar.sh", (char *)"modem", NULL
};
run_sync_quiet(hook_argv);
run_sync_quiet(sb_argv);
}
_exit(0);
}
_exit(0);
}
if(mid > 0) {
int st;
while(waitpid(mid, &st, 0) < 0 && errno == EINTR) {
}
}
}
static int start_dbus_pipe(char *const argv[], volatile pid_t *out_pid)
{
int pfd[2];
pid_t pid;
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);
}
execvp(argv[0], argv);
_exit(127);
}
close(pfd[1]);
if(pid < 0) {
close(pfd[0]);
return -1;
}
*out_pid = pid;
return pfd[0];
}
struct mm_parser {
int state_stage;
char old_line[128];
char new_line[128];
char cur_modem[128];
};
static void mm_process_line(struct mm_parser *mp, const char *line)
{
if(mp->state_stage == 1) {
safe_copy(mp->old_line, sizeof(mp->old_line), line);
mp->state_stage = 2;
return;
}
if(mp->state_stage == 2) {
safe_copy(mp->new_line, sizeof(mp->new_line), line);
mp->state_stage = 3;
return;
}
if(mp->state_stage == 3) {
mp->state_stage = 0;
dispatch_modem_state_async(statenum_to_name(mp->old_line),
statenum_to_name(mp->new_line), line);
return;
}
if(strncmp(line, "signal", 6) == 0 &&
strstr(line, "interface=org.freedesktop.ModemManager1.Modem.Voice") &&
strstr(line, "member=CallAdded")) {
char *const argv[] = {
(char *)"sxmo_modem.sh", (char *)"checkforincomingcalls", NULL
};
spawn_detached(argv);
} else if(strncmp(line, "signal", 6) == 0 &&
strstr(line, "interface=org.freedesktop.ModemManager1.Modem.Messaging") &&
strstr(line, "member=Added")) {
char *const argv[] = {
(char *)"sxmo_modem.sh", (char *)"checkfornewtexts", NULL
};
spawn_detached(argv);
} else if(strncmp(line, "signal", 6) == 0 &&
strstr(line, "path=/org/freedesktop/ModemManager1/SMS/") &&
strstr(line, "member=PropertiesChanged")) {
char *const argv[] = {
(char *)"sxmo_modem.sh", (char *)"checkfornewtexts", NULL
};
spawn_detached(argv);
} else if(strncmp(line, "signal", 6) == 0 &&
strstr(line, "path=/org/freedesktop/ModemManager1/Call/") &&
strstr(line, "member=PropertiesChanged")) {
char *const argv[] = {
(char *)"sxmo_modem.sh", (char *)"checkforfinishedcalls", NULL
};
spawn_detached(argv);
} else if(strncmp(line, "signal", 6) == 0 &&
strstr(line, "interface=org.freedesktop.ModemManager1.Modem;") &&
strstr(line, "member=StateChanged")) {
mp->state_stage = 1;
} else if(strncmp(line, "signal", 6) == 0 &&
strstr(line, "path=/org/freedesktop/ModemManager1/Modem/") &&
strstr(line, "member=PropertiesChanged")) {
const char *p = strstr(line, "path=");
if(p) {
const char *semi;
p += 5;
semi = strchr(p, ';');
if(semi && (size_t)(semi - p) < sizeof(mp->cur_modem)) {
memcpy(mp->cur_modem, p, (size_t)(semi - p));
mp->cur_modem[semi - p] = '\0';
}
}
} else if(strstr(line, "string \"NetworkNotification\"") &&
mp->cur_modem[0] != '\0') {
char *const argv[] = {
(char *)"sxmo_ussd.sh", mp->cur_modem,
(char *)"NetworkNotification", (char *)"", NULL
};
spawn_detached(argv);
} else if(strstr(line, "string \"NetworkRequest\"") &&
mp->cur_modem[0] != '\0') {
char *const argv[] = {
(char *)"sxmo_ussd.sh", mp->cur_modem,
(char *)"NetworkRequest", (char *)"", NULL
};
spawn_detached(argv);
}
}
struct aux_parser {
char mms_path[256];
int vvm_start;
char vvm_date[128];
char vvm_sender[128];
char vvm_id[128];
char vvm_attach[256];
};
static void extract_quoted(const char *line, char *out, size_t len)
{
const char *q1 = strchr(line, '"');
const char *q2 = q1 ? strchr(q1 + 1, '"') : NULL;
if(!q1 || !q2 || q2 <= q1 + 1) {
out[0] = '\0';
return;
}
{
size_t n = (size_t)(q2 - q1 - 1);
if(n >= len)
n = len - 1;
memcpy(out, q1 + 1, n);
out[n] = '\0';
}
}
static void mms_process_line(struct aux_parser *ap, const char *line)
{
const char *p = line;
while(*p == ' ' || *p == '\t')
p++;
if(strncmp(p, "object path", 11) == 0) {
extract_quoted(p, ap->mms_path, sizeof(ap->mms_path));
} else if(strstr(p, "string \"received\"") && ap->mms_path[0] != '\0') {
char *const argv[] = {
(char *)"sxmo_mms.sh", (char *)"processmms", ap->mms_path, NULL
};
spawn_detached(argv);
}
}
static void vvm_process_line(struct aux_parser *ap, const char *line)
{
static int vvm_field = 0;
const char *p = line;
while(*p == ' ' || *p == '\t')
p++;
if(vvm_field == 1) {
extract_quoted(p, ap->vvm_date, sizeof(ap->vvm_date));
vvm_field = 0;
return;
}
if(vvm_field == 2) {
extract_quoted(p, ap->vvm_sender, sizeof(ap->vvm_sender));
vvm_field = 0;
return;
}
if(vvm_field == 3) {
extract_quoted(p, ap->vvm_attach, sizeof(ap->vvm_attach));
vvm_field = 0;
return;
}
if(strncmp(p, "object path", 11) == 0) {
char obj[256];
const char *slash;
extract_quoted(p, obj, sizeof(obj));
slash = strrchr(obj, '/');
safe_copy(ap->vvm_id, sizeof(ap->vvm_id), slash ? slash + 1 : obj);
ap->vvm_start = 1;
} else if(p[0] == ']') {
ap->vvm_start = 0;
{
char *const argv[] = {
(char *)"sxmo_vvm.sh", (char *)"processvvm",
ap->vvm_date, ap->vvm_sender, ap->vvm_id, ap->vvm_attach, NULL
};
spawn_detached(argv);
}
} else if(ap->vvm_start) {
if(strncmp(p, "string \"Date\"", 13) == 0)
vvm_field = 1;
else if(strncmp(p, "string \"Sender\"", 15) == 0)
vvm_field = 2;
else if(strncmp(p, "string \"Attachments\"", 20) == 0)
vvm_field = 3;
}
}
typedef void (*line_cb_fn)(void *ctx, const char *line);
static int drain_pipe_lines(int fd, char *buf, size_t buf_cap, size_t *len,
line_cb_fn cb, void *ctx)
{
for(;;) {
ssize_t n;
char *nl;
if(*len >= buf_cap - 1)
*len = 0;
n = read(fd, buf + *len, buf_cap - 1 - *len);
if(n > 0) {
*len += (size_t)n;
buf[*len] = '\0';
while((nl = strchr(buf, '\n')) != NULL) {
size_t rest;
*nl = '\0';
cb(ctx, buf);
rest = (size_t)(buf + *len - (nl + 1));
memmove(buf, nl + 1, rest);
*len = rest;
buf[*len] = '\0';
}
continue;
}
if(n == 0 || (errno != EAGAIN && errno != EWOULDBLOCK))
return -1;
break;
}
return 0;
}
static int dsds_lock_held(void)
{
int fd = open("/tmp/dsds_daemon.lock", O_RDONLY | O_CLOEXEC);
int held = 0;
if(fd < 0)
return 0;
if(flock(fd, LOCK_EX | LOCK_NB) < 0) {
if(errno == EWOULDBLOCK || errno == EAGAIN)
held = 1;
} else {
flock(fd, LOCK_UN);
}
close(fd);
return held;
}
static void maybe_start_dsds(long long *due_ms)
{
char active[16] = "";
if(access("/tmp/sxmo_dsds_active", F_OK) != 0)
spawn_uim_cmd("status", NULL, 0);
if(read_first_line("/tmp/sxmo_dsds_active", active, sizeof(active)) != 0 ||
strcmp(active, "1") != 0) {
*due_ms = -1;
return;
}
if(dsds_pid > 0) {
*due_ms = -1;
return;
}
if(dsds_lock_held()) {
*due_ms = now_ms() + 30000;
return;
}
{
pid_t pid = fork();
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);
}
if(geteuid() == 0) {
execl("/usr/bin/bananarchy-dsds", "bananarchy-dsds", (char *)NULL);
} else {
execlp("doas", "doas", "-n", "/usr/bin/bananarchy-dsds", (char *)NULL);
}
_exit(127);
}
if(pid > 0) {
dsds_pid = pid;
*due_ms = -1;
} else {
*due_ms = now_ms() + 30000;
}
}
}
int main(void)
{
struct sigaction sa;
const char *home = getenv("HOME");
char mms_flag[PATH_MAX];
char vvm_flag[PATH_MAX];
int mm_fd;
int mms_fd = -1;
int vvm_fd = -1;
int tmp_ifd;
int wd = -1;
char mm_buf[4096];
char mms_buf[2048];
char vvm_buf[2048];
size_t mm_len = 0;
size_t mms_len = 0;
size_t vvm_len = 0;
struct mm_parser mp;
struct aux_parser ap;
long long dsds_due_ms;
memset(&mp, 0, sizeof(mp));
memset(&ap, 0, sizeof(ap));
if(pipe2(sig_pipe, O_NONBLOCK | O_CLOEXEC) < 0)
return 1;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = handle_stop;
sigemptyset(&sa.sa_mask);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sa.sa_handler = handle_sigchld;
sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
sigaction(SIGCHLD, &sa, NULL);
signal(SIGPIPE, SIG_IGN);
cleanup_session_files();
spawn_boot_probe();
{
char *const mm_argv[] = {
(char *)"dbus-monitor", (char *)"--system",
(char *)"interface='org.freedesktop.ModemManager1.Modem.Voice',type='signal',member='CallAdded'",
(char *)"interface='org.freedesktop.ModemManager1.Modem.Messaging',type='signal',member='Added'",
(char *)"interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',arg0='org.freedesktop.ModemManager1.Sms'",
(char *)"interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',arg0='org.freedesktop.ModemManager1.Call'",
(char *)"interface='org.freedesktop.ModemManager1.Modem',type='signal',member='StateChanged'",
(char *)"interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',arg0='org.freedesktop.ModemManager1.Modem.Modem3gpp.Ussd'",
NULL
};
mm_fd = start_dbus_pipe(mm_argv, &mm_pid);
}
snprintf(mms_flag, sizeof(mms_flag), "%s/.mms/modemmanager/mms",
(home && *home) ? home : "/home/user");
if(access(mms_flag, F_OK) == 0) {
char *const mms_argv[] = {
(char *)"dbus-monitor",
(char *)"interface='org.ofono.mms.Service',type='signal',member='MessageAdded'",
NULL
};
mms_fd = start_dbus_pipe(mms_argv, &mms_pid);
}
snprintf(vvm_flag, sizeof(vvm_flag), "%s/.vvm/modemmanager/vvm",
(home && *home) ? home : "/home/user");
if(access(vvm_flag, F_OK) == 0) {
char *const vvm_argv[] = {
(char *)"dbus-monitor",
(char *)"interface='org.kop316.vvm.Service',type='signal',member='MessageAdded'",
NULL
};
vvm_fd = start_dbus_pipe(vvm_argv, &vvm_pid);
}
tmp_ifd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
if(tmp_ifd >= 0) {
wd = inotify_add_watch(tmp_ifd, "/tmp",
IN_CLOSE_WRITE | IN_MOVED_TO | IN_CREATE | IN_DELETE);
(void)wd;
}
dsds_due_ms = now_ms() + 4000;
while(!stop_requested) {
struct pollfd fds[5];
int nfds = 0;
int idx_sig = -1, idx_mm = -1, idx_mms = -1, idx_vvm = -1, idx_tmp = -1;
int timeout_ms = -1;
long long now = now_ms();
idx_sig = nfds;
fds[nfds].fd = sig_pipe[0];
fds[nfds].events = POLLIN;
fds[nfds].revents = 0;
nfds++;
if(mm_fd >= 0) {
idx_mm = nfds;
fds[nfds].fd = mm_fd;
fds[nfds].events = POLLIN;
fds[nfds].revents = 0;
nfds++;
}
if(mms_fd >= 0) {
idx_mms = nfds;
fds[nfds].fd = mms_fd;
fds[nfds].events = POLLIN;
fds[nfds].revents = 0;
nfds++;
}
if(vvm_fd >= 0) {
idx_vvm = nfds;
fds[nfds].fd = vvm_fd;
fds[nfds].events = POLLIN;
fds[nfds].revents = 0;
nfds++;
}
if(tmp_ifd >= 0) {
idx_tmp = nfds;
fds[nfds].fd = tmp_ifd;
fds[nfds].events = POLLIN;
fds[nfds].revents = 0;
nfds++;
}
if(dsds_due_ms >= 0) {
long long rem = dsds_due_ms - now;
timeout_ms = (rem > 0) ? (int)rem : 0;
}
if(poll(fds, (nfds_t)nfds, timeout_ms) < 0) {
if(errno == EINTR)
continue;
break;
}
if(stop_requested)
break;
if(idx_sig >= 0 && (fds[idx_sig].revents & POLLIN)) {
char sbuf[64];
int st;
while(read(sig_pipe[0], sbuf, sizeof(sbuf)) > 0) {
}
if(dsds_pid > 0 && waitpid(dsds_pid, &st, WNOHANG) > 0) {
char active[16] = "";
dsds_pid = -1;
if(read_first_line("/tmp/sxmo_dsds_active", active, sizeof(active)) == 0 &&
strcmp(active, "1") == 0) {
dsds_due_ms = now_ms() + 30000;
}
}
if(mm_pid > 0 && waitpid(mm_pid, &st, WNOHANG) > 0)
mm_pid = -1;
if(mms_pid > 0 && waitpid(mms_pid, &st, WNOHANG) > 0)
mms_pid = -1;
if(vvm_pid > 0 && waitpid(vvm_pid, &st, WNOHANG) > 0)
vvm_pid = -1;
}
if(idx_mm >= 0 && (fds[idx_mm].revents & (POLLIN | POLLERR | POLLHUP))) {
if(drain_pipe_lines(mm_fd, mm_buf, sizeof(mm_buf), &mm_len,
(line_cb_fn)mm_process_line, &mp) < 0) {
close(mm_fd);
mm_fd = -1;
break;
}
}
if(idx_mms >= 0 && (fds[idx_mms].revents & (POLLIN | POLLERR | POLLHUP))) {
if(drain_pipe_lines(mms_fd, mms_buf, sizeof(mms_buf), &mms_len,
(line_cb_fn)mms_process_line, &ap) < 0) {
close(mms_fd);
mms_fd = -1;
}
}
if(idx_vvm >= 0 && (fds[idx_vvm].revents & (POLLIN | POLLERR | POLLHUP))) {
if(drain_pipe_lines(vvm_fd, vvm_buf, sizeof(vvm_buf), &vvm_len,
(line_cb_fn)vvm_process_line, &ap) < 0) {
close(vvm_fd);
vvm_fd = -1;
}
}
if(idx_tmp >= 0 && (fds[idx_tmp].revents & POLLIN)) {
char ibuf[4096];
ssize_t n;
while((n = read(tmp_ifd, ibuf, sizeof(ibuf))) > 0) {
ssize_t off = 0;
while(off < n) {
const struct inotify_event *ev =
(const struct inotify_event *)(ibuf + off);
if(ev->len > 0 && strcmp(ev->name, "sxmo_dsds_active") == 0) {
char active[16] = "";
if(read_first_line("/tmp/sxmo_dsds_active", active, sizeof(active)) == 0 &&
strcmp(active, "1") == 0) {
if(dsds_pid <= 0)
dsds_due_ms = now_ms() + 200;
} else {
dsds_due_ms = -1;
}
}
off += (ssize_t)(sizeof(*ev) + ev->len);
}
}
}
if(dsds_due_ms >= 0 && now_ms() >= dsds_due_ms)
maybe_start_dsds(&dsds_due_ms);
}
if(mm_pid > 0) {
int st;
kill(mm_pid, SIGTERM);
waitpid(mm_pid, &st, 0);
}
if(mms_pid > 0) {
int st;
kill(mms_pid, SIGTERM);
waitpid(mms_pid, &st, 0);
}
if(vvm_pid > 0) {
int st;
kill(vvm_pid, SIGTERM);
waitpid(vvm_pid, &st, 0);
}
if(dsds_pid > 0) {
int st;
kill(dsds_pid, SIGTERM);
waitpid(dsds_pid, &st, 0);
}
if(mm_fd >= 0) close(mm_fd);
if(mms_fd >= 0) close(mms_fd);
if(vvm_fd >= 0) close(vvm_fd);
if(tmp_ifd >= 0) close(tmp_ifd);
close(sig_pipe[0]);
close(sig_pipe[1]);
return 0;
}
@@ -0,0 +1,346 @@
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
/* Compiled notification monitor replacing sxmo_notifs.sh monitor. */
#include <dirent.h>
#include <limits.h>
#include <poll.h>
#include "common.h"
static volatile sig_atomic_t stop_requested = 0;
static void handle_signal(int sig)
{
(void)sig;
stop_requested = 1;
}
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 mkdir_p(const char *path)
{
char buf[PATH_MAX];
char *p;
safe_copy(buf, sizeof(buf), path);
for(p = buf + 1; *p; p++) {
if(*p == '/') {
*p = '\0';
mkdir(buf, 0755);
*p = '/';
}
}
mkdir(buf, 0755);
}
static void resolve_notifdir(char *out, size_t len)
{
const char *env_dir = getenv("SXMO_NOTIFDIR");
const char *xdg_data = getenv("XDG_DATA_HOME");
const char *home = getenv("HOME");
if(env_dir && *env_dir && strcmp(env_dir, "/") != 0) {
safe_copy(out, len, env_dir);
return;
}
if(xdg_data && *xdg_data) {
snprintf(out, len, "%s/sxmo/notifications", xdg_data);
return;
}
snprintf(out, len, "%s/.local/share/sxmo/notifications",
(home && *home) ? home : "/home/user");
}
static void strip_eol(char *s)
{
size_t n = strlen(s);
while(n > 0 && (s[n - 1] == '\n' || s[n - 1] == '\r'))
s[--n] = '\0';
}
static void clear_notif_group(const char *notifdir, const char *group)
{
DIR *dir;
struct dirent *de;
if(!group || !*group)
return;
dir = opendir(notifdir);
if(!dir)
return;
while((de = readdir(dir)) != NULL) {
char path[PATH_MAX];
char line[512];
struct stat st;
if(de->d_name[0] == '.')
continue;
snprintf(path, sizeof(path), "%s/%s", notifdir, de->d_name);
if(stat(path, &st) < 0 || !S_ISREG(st.st_mode))
continue;
if(read_first_line(path, line, sizeof(line)) == 0 &&
strcmp(line, group) == 0) {
unlink(path);
}
}
closedir(dir);
}
static void notifications_hook(const char *notifdir)
{
DIR *dir = opendir(notifdir);
struct dirent *de;
int count = 0;
char count_str[32];
if(dir) {
while((de = readdir(dir)) != NULL) {
char path[PATH_MAX];
struct stat st;
if(de->d_name[0] == '.')
continue;
snprintf(path, sizeof(path), "%s/%s", notifdir, de->d_name);
if(stat(path, &st) == 0 && S_ISREG(st.st_mode))
count++;
}
closedir(dir);
}
snprintf(count_str, sizeof(count_str), "%d", count);
{
char *const argv[] = {
(char *)"sxmo_hook_notifications.sh", count_str, NULL
};
spawn_detached(argv);
}
}
static void run_dunstify_worker(const char *notifdir, const char *group,
const char *action, const char *msg)
{
pid_t mid = fork();
if(mid == 0) {
pid_t leaf;
setsid();
leaf = fork();
if(leaf == 0) {
int pipefd[2];
pid_t dpid;
char out[128] = "";
ssize_t total = 0;
if(pipe2(pipefd, O_CLOEXEC) < 0)
_exit(1);
dpid = fork();
if(dpid == 0) {
int nullfd = open("/dev/null", O_RDWR | O_CLOEXEC);
close(pipefd[0]);
dup2(pipefd[1], STDOUT_FILENO);
if(pipefd[1] > STDERR_FILENO)
close(pipefd[1]);
if(nullfd >= 0) {
dup2(nullfd, STDIN_FILENO);
dup2(nullfd, STDERR_FILENO);
if(nullfd > STDERR_FILENO)
close(nullfd);
}
execlp("dunstify", "dunstify", "--action=default,Open", msg, (char *)NULL);
_exit(127);
}
close(pipefd[1]);
if(dpid > 0) {
ssize_t n;
int st;
while(total < (ssize_t)sizeof(out) - 1 &&
(n = read(pipefd[0], out + total, sizeof(out) - 1 - (size_t)total)) > 0) {
total += n;
}
out[total] = '\0';
while(waitpid(dpid, &st, 0) < 0 && errno == EINTR) {
}
}
close(pipefd[0]);
strip_eol(out);
if(strcmp(out, "default") == 0) {
clear_notif_group(notifdir, group);
{
char *const sb_argv[] = {
(char *)"sxmo_hook_statusbar.sh", (char *)"notifications", NULL
};
spawn_detached(sb_argv);
}
if(action[0] != '\0' && strcmp(action, "none") != 0) {
char *const act_argv[] = {
(char *)"/bin/sh", (char *)"-c", (char *)action, NULL
};
spawn_detached(act_argv);
}
}
_exit(0);
}
_exit(0);
}
if(mid > 0) {
int st;
while(waitpid(mid, &st, 0) < 0 && errno == EINTR) {
}
}
}
static void handle_new_notif_file(const char *notifdir, const char *path,
int is_boot)
{
const char *base = strrchr(path, '/');
struct stat st;
FILE *f;
char group[512] = "";
char action[1024] = "";
char msg[256] = "";
int nl_count = 0;
int ch;
size_t msg_len = 0;
base = base ? base + 1 : path;
if(base[0] == '.')
return;
if(stat(path, &st) < 0 || !S_ISREG(st.st_mode))
return;
f = fopen(path, "r");
if(!f)
return;
while((ch = fgetc(f)) != EOF) {
if(ch == '\n')
nl_count++;
}
if(nl_count < 3) {
fclose(f);
unlink(path);
return;
}
rewind(f);
if(!fgets(group, sizeof(group), f))
group[0] = '\0';
if(!fgets(action, sizeof(action), f))
action[0] = '\0';
while(msg_len < 70 && (ch = fgetc(f)) != EOF) {
if(ch == '\r')
continue;
if(ch == '\n') {
if(msg_len > 0)
break;
continue;
}
msg[msg_len++] = (char)ch;
}
msg[msg_len] = '\0';
fclose(f);
strip_eol(group);
strip_eol(action);
if(!is_boot) {
char path_copy[PATH_MAX];
safe_copy(path_copy, sizeof(path_copy), path);
char *const hook_argv[] = {
(char *)"sxmo_hook_notification.sh", path_copy, NULL
};
spawn_detached(hook_argv);
}
run_dunstify_worker(notifdir, group, action, msg);
}
int main(void)
{
char notifdir[PATH_MAX];
int ifd;
DIR *dir;
struct dirent *de;
struct sigaction sa;
struct pollfd pfd;
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);
resolve_notifdir(notifdir, sizeof(notifdir));
mkdir_p(notifdir);
dir = opendir(notifdir);
if(dir) {
while((de = readdir(dir)) != NULL) {
char path[PATH_MAX];
if(de->d_name[0] == '.')
continue;
snprintf(path, sizeof(path), "%s/%s", notifdir, de->d_name);
handle_new_notif_file(notifdir, path, 1);
}
closedir(dir);
}
notifications_hook(notifdir);
ifd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
if(ifd < 0)
return 1;
if(inotify_add_watch(ifd, notifdir,
IN_CLOSE_WRITE | IN_MOVED_TO | IN_DELETE) < 0) {
close(ifd);
return 1;
}
pfd.fd = ifd;
pfd.events = POLLIN;
while(!stop_requested) {
char buf[4096];
ssize_t n;
int off = 0;
int rc = poll(&pfd, 1, -1);
if(rc < 0) {
if(errno == EINTR)
continue;
break;
}
n = read(ifd, buf, sizeof(buf));
if(n <= 0)
continue;
while(off < n) {
const struct inotify_event *ev =
(const struct inotify_event *)(buf + off);
if(ev->len > 0 && ev->name[0] != '.' &&
(ev->mask & (IN_CLOSE_WRITE | IN_MOVED_TO))) {
char path[PATH_MAX];
snprintf(path, sizeof(path), "%s/%s", notifdir, ev->name);
handle_new_notif_file(notifdir, path, 0);
}
off += (int)(sizeof(*ev) + ev->len);
}
notifications_hook(notifdir);
}
close(ifd);
return 0;
}
@@ -0,0 +1,110 @@
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
/* Wall-clock aligned periodic runner replacing sxmo_run_aligned.sh + sxmo_aligned_sleep. */
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
static volatile sig_atomic_t stop_requested = 0;
static volatile pid_t active_child = -1;
static void handle_stop(int sig)
{
pid_t child = active_child;
(void)sig;
stop_requested = 1;
if(child > 0)
kill(child, SIGTERM);
}
static void aligned_sleep(long interval)
{
struct timespec now;
struct timespec req;
long long rem_ns;
long long rem_s;
if(clock_gettime(CLOCK_REALTIME, &now) < 0) {
req.tv_sec = (time_t)interval;
req.tv_nsec = 0;
} else {
rem_ns = 1000000000LL - (long long)now.tv_nsec;
rem_s = (interval - 1LL) - ((long long)now.tv_sec % interval);
if(rem_s < 0)
rem_s += interval;
if(rem_ns >= 1000000000LL) {
rem_ns -= 1000000000LL;
rem_s += 1LL;
}
req.tv_sec = (time_t)rem_s;
req.tv_nsec = (long)rem_ns;
}
while(!stop_requested) {
if(nanosleep(&req, &req) == 0)
break;
if(errno != EINTR)
break;
}
}
int main(int argc, char **argv)
{
struct sigaction sa;
char *endptr = NULL;
long interval;
if(argc < 3) {
fprintf(stderr, "Usage: %s <interval_sec> <cmd> [args...]\n", argv[0]);
return 1;
}
interval = strtol(argv[1], &endptr, 10);
if(!endptr || *endptr != '\0' || interval <= 0) {
fprintf(stderr, "%s: invalid interval '%s'\n", argv[0], argv[1]);
return 1;
}
memset(&sa, 0, sizeof(sa));
sa.sa_handler = handle_stop;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
while(!stop_requested) {
pid_t pid = fork();
if(pid == 0) {
sigset_t empty;
sigemptyset(&empty);
sigprocmask(SIG_SETMASK, &empty, NULL);
sa.sa_handler = SIG_DFL;
sa.sa_flags = 0;
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
execvp(argv[2], &argv[2]);
_exit(127);
}
if(pid > 0) {
int status;
active_child = pid;
while(waitpid(pid, &status, 0) < 0) {
if(errno != EINTR)
break;
}
active_child = -1;
}
if(stop_requested)
break;
aligned_sleep(interval);
}
return 0;
}
@@ -0,0 +1,281 @@
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
/* Compiled X11 session supervisor loop replacing bananarchy-x11-session.sh tail. */
#include <ctype.h>
#include <dirent.h>
#include <limits.h>
#include <time.h>
#include "common.h"
static volatile sig_atomic_t stop_requested = 0;
static volatile pid_t dwm_pid = -1;
static void handle_signal(int sig)
{
pid_t child = dwm_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 int 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)
return -1;
while(waitpid(pid, &st, 0) < 0) {
if(errno != EINTR)
return -1;
}
return WIFEXITED(st) ? WEXITSTATUS(st) : -1;
}
static int proc_is_running(const char *target)
{
DIR *dir = opendir("/proc");
struct dirent *de;
size_t tlen = strlen(target);
if(!dir)
return 0;
while((de = readdir(dir)) != NULL) {
char comm_path[300];
char buf[64];
int n;
FILE *f;
if(!isdigit((unsigned char)de->d_name[0]))
continue;
snprintf(comm_path, sizeof(comm_path), "/proc/%s/comm", de->d_name);
f = fopen(comm_path, "r");
if(!f)
continue;
if(!fgets(buf, sizeof(buf), f))
buf[0] = '\0';
fclose(f);
n = (int)strlen(buf);
while(n > 0 && (buf[n - 1] == '\n' || buf[n - 1] == '\r'))
buf[--n] = '\0';
if((size_t)n == tlen && strcmp(buf, target) == 0) {
closedir(dir);
return 1;
}
}
closedir(dir);
return 0;
}
static int session_pid_matches(const char *pid_file)
{
char buf[64];
long val;
if(read_first_line(pid_file, buf, sizeof(buf)) < 0 || buf[0] == '\0')
return 0;
val = strtol(buf, NULL, 10);
return val == (long)getpid();
}
static int screen_is_dark(void)
{
char buf[32];
if(read_first_line("/sys/class/backlight/backlight/brightness",
buf, sizeof(buf)) == 0 &&
strcmp(buf, "0") == 0) {
return 1;
}
return 0;
}
static void resolve_wallpaper(char *wp_file, size_t wp_len,
char *wp_flag, size_t flag_len)
{
const char *xdg_cfg = getenv("XDG_CONFIG_HOME");
const char *home = getenv("HOME");
char cfg_base[PATH_MAX];
char path_buf[PATH_MAX + 64];
char line[PATH_MAX];
safe_copy(wp_file, wp_len, "/usr/share/wallpapers/bananarchy.png");
safe_copy(wp_flag, flag_len, "--bg-fill");
if(xdg_cfg && *xdg_cfg) {
safe_copy(cfg_base, sizeof(cfg_base), xdg_cfg);
} else {
snprintf(cfg_base, sizeof(cfg_base), "%s/.config",
(home && *home) ? home : "/home/user");
}
snprintf(path_buf, sizeof(path_buf), "%s/sxmo/wallpaper", cfg_base);
if(read_first_line(path_buf, line, sizeof(line)) == 0 && line[0] != '\0') {
if(access(line, F_OK) == 0)
safe_copy(wp_file, wp_len, line);
}
snprintf(path_buf, sizeof(path_buf), "%s/sxmo/wallpaper_mode", cfg_base);
if(read_first_line(path_buf, line, sizeof(line)) == 0) {
if(strcmp(line, "scale") == 0 || strcmp(line, "--bg-scale") == 0)
safe_copy(wp_flag, flag_len, "--bg-scale");
else if(strcmp(line, "max") == 0 || strcmp(line, "--bg-max") == 0)
safe_copy(wp_flag, flag_len, "--bg-max");
else if(strcmp(line, "center") == 0 || strcmp(line, "--bg-center") == 0)
safe_copy(wp_flag, flag_len, "--bg-center");
else if(strcmp(line, "tile") == 0 || strcmp(line, "--bg-tile") == 0)
safe_copy(wp_flag, flag_len, "--bg-tile");
}
}
int main(int argc, char **argv)
{
const char *xdg_rt = getenv("XDG_RUNTIME_DIR");
const char *home = getenv("HOME");
char default_pid_file[PATH_MAX];
const char *pid_file;
struct sigaction sa;
int first_iter = 1;
if(argc >= 2 && argv[1][0] != '\0') {
pid_file = argv[1];
} else {
snprintf(default_pid_file, sizeof(default_pid_file),
"%s/bananarchy-x11-session.pid",
(xdg_rt && *xdg_rt) ? xdg_rt : "/run/user/10000");
pid_file = default_pid_file;
}
memset(&sa, 0, sizeof(sa));
sa.sa_handler = handle_signal;
sigemptyset(&sa.sa_mask);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
while(!stop_requested && session_pid_matches(pid_file)) {
char wp_file[PATH_MAX];
char wp_flag[32];
int dark;
pid_t child;
if(proc_is_running("dwm"))
return 0;
dark = screen_is_dark();
if(!dark) {
char xres[PATH_MAX];
snprintf(xres, sizeof(xres), "%s/.Xresources",
(home && *home) ? home : "/home/user");
char *const xrdb_argv[] = {
(char *)"xrdb", (char *)"-merge", xres, NULL
};
char *const dunst_argv[] = {
(char *)"/usr/bin/bananarchy-sync-dunst-theme",
(char *)"--no-restart", NULL
};
run_sync_quiet(xrdb_argv);
run_sync_quiet(dunst_argv);
}
resolve_wallpaper(wp_file, sizeof(wp_file), wp_flag, sizeof(wp_flag));
if(!dark) {
char *const feh_argv[] = {
(char *)"feh", (char *)"--no-fehbg", wp_flag, wp_file, NULL
};
spawn_detached(feh_argv);
}
if(!first_iter && !proc_is_running("fcitx5")) {
char *const super_argv[] = {
(char *)"superctl", (char *)"start", (char *)"fcitx5", NULL
};
if(run_sync_quiet(super_argv) != 0) {
char *const fcitx_argv[] = {
(char *)"/usr/bin/bananarchy-fcitx5.sh", NULL
};
spawn_detached(fcitx_argv);
}
}
first_iter = 0;
child = fork();
if(child == 0) {
int fd;
if(access("/tmp/bananarchy_ext_logging", F_OK) == 0) {
fd = open("/tmp/dwm.log", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
} else {
fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
}
if(fd >= 0) {
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
if(fd > STDERR_FILENO)
close(fd);
}
execlp("dwm", "dwm", (char *)NULL);
_exit(127);
}
if(child > 0) {
int st;
pid_t w;
dwm_pid = child;
for(;;) {
w = waitpid(-1, &st, 0);
if(w == child)
break;
if(w < 0 && errno != EINTR)
break;
}
dwm_pid = -1;
}
if(stop_requested || !session_pid_matches(pid_file) || proc_is_running("dwm"))
return 0;
{
struct timespec req = { 0, 200000000L };
nanosleep(&req, NULL);
}
}
return 0;
}
@@ -0,0 +1,575 @@
#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 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 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 handle_jack_insert(int jack_fd)
{
char cur_out[64] = "";
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);
write_state_file("/tmp/sxmo_jack_plugged", "1");
if(query_ev_sw(jack_fd, SW_MICROPHONE_INSERT, "name='Mic Jack'")) {
write_state_file("/tmp/sxmo_audio_in_device", "Headset");
sxmo_audio_cmd("setinput", "Headset");
} else {
write_state_file("/tmp/sxmo_audio_in_device", "Mic1");
}
sxmo_audio_cmd("set", "Headphones");
}
static void handle_jack_remove(void)
{
char incall[16] = "";
const char *target = "Speaker";
write_state_file("/tmp/sxmo_jack_plugged", "0");
write_state_file("/tmp/sxmo_audio_in_device", "Mic1");
sxmo_audio_cmd("setinput", "Mic1");
if(read_first_line("/tmp/sxmo_in_call", incall, sizeof(incall)) == 0 &&
strcmp(incall, "1") == 0) {
target = "Earpiece";
}
sxmo_audio_cmd("set", target);
}
static void handle_jack_events(int *jack_fd_ptr)
{
int fd = *jack_fd_ptr;
struct input_event ev;
ssize_t n;
while((n = read(fd, &ev, sizeof(ev))) == (ssize_t)sizeof(ev)) {
if(ev.type != EV_SW)
continue;
if(ev.code == SW_HEADPHONE_INSERT && ev.value == 1) {
struct timespec ts = { 0, 100000000L };
nanosleep(&ts, NULL);
handle_jack_insert(fd);
} else if(ev.code == SW_HEADPHONE_INSERT && ev.value == 0) {
handle_jack_remove();
} else if(ev.code == SW_MICROPHONE_INSERT && ev.value == 1) {
sxmo_audio_cmd("setinput", "Headset");
} else if(ev.code == SW_MICROPHONE_INSERT && ev.value == 0) {
sxmo_audio_cmd("setinput", "Mic1");
}
}
if(n < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
close(fd);
*jack_fd_ptr = -1;
}
}
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 dispatch_pactl_line(const char *line, char *prev_bt, size_t prev_bt_len)
{
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");
}
{
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(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, "name='Headphone Jack'")) {
handle_jack_insert(jack_fd);
} else {
char bt_sink[128] = "";
write_state_file("/tmp/sxmo_jack_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);
}
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;
if(query_ev_sw(jack_fd, SW_HEADPHONE_INSERT, "name='Headphone Jack'"))
handle_jack_insert(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));
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));
jack_retry_at = (jack_fd < 0) ? (now + 5000) : -1;
}
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;
}
+45
View File
@@ -32,7 +32,10 @@
#define EV_SW 0x05
#define KEY_MAX 0x2ff
#define SW_LID 0x00
#define SW_HEADPHONE_INSERT 0x02
#define SW_MICROPHONE_INSERT 0x04
#define SW_KEYPAD_SLIDE 0x0a
#define SW_MAX 0x0f
#define KEY_VOLUMEDOWN 114
#define KEY_VOLUMEUP 115
#ifndef KEY_POWER
@@ -46,6 +49,8 @@
((unsigned int)(nr) << 0) | ((unsigned int)(size) << 16))
#define BA_EVIOCGBIT(ev, len) \
BA_IOC(2U, 'E', 0x20U + (unsigned int)(ev), (unsigned int)(len))
#define BA_EVIOCGSW(len) \
BA_IOC(2U, 'E', 0x1bU, (unsigned int)(len))
/* Match the kernel evdev record layout (__kernel_ulong_t sec/usec), not musl
* time64 struct timeval: on 32-bit armv7 musl, struct timeval is 16 bytes
@@ -304,4 +309,44 @@ static BA_UNUSED long long now_ms(void)
return (long long)ts.tv_sec * 1000LL + ts.tv_nsec / 1000000LL;
}
static BA_UNUSED int read_first_line(const char *path, char *buf, size_t len)
{
FILE *f;
size_t n;
if(!buf || len == 0)
return -1;
buf[0] = '\0';
f = fopen(path, "r");
if(!f)
return -1;
if(!fgets(buf, (int)len, f)) {
fclose(f);
buf[0] = '\0';
return -1;
}
fclose(f);
n = strlen(buf);
while(n > 0 && (buf[n - 1] == '\n' || buf[n - 1] == '\r'))
buf[--n] = '\0';
return 0;
}
static BA_UNUSED pid_t spawn_async_cmd(char *const argv[])
{
pid_t pid = fork();
int nullfd;
if(pid != 0)
return pid;
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);
}
#endif