Fix battery drain sources across suspend, CPUfreq, radios, and daemons

This commit is contained in:
Luxferre committed 2026-11-23 20:00:17 +02:00
1 parent 4b36aa0726
commit 7f3b3c2c0c
24 files changed
+487 -64

No files matched your search

+23 -2
View File
@@ -20,6 +20,9 @@ typedef struct {
char password[128];
char bat_text[96];
char err_msg[96];
int last_min;
int last_yday;
int timer_ms;
bool grabbed;
bool checking;
} lock_state_t;
@@ -99,9 +102,22 @@ static void on_timer(gemui_app_t *app, void *ud) {
lock_state_t *st = ud;
if (!st->grabbed) {
st->grabbed = gemui_grab_input(app);
if (st->grabbed && st->timer_ms != 15000) {
st->timer_ms = 15000;
gemui_set_timer_ms(app, 15000, on_timer, st);
}
}
char prev_bat[96];
snprintf(prev_bat, sizeof(prev_bat), "%s", st->bat_text);
refresh_battery(st);
gemui_request_redraw(app);
time_t now = time(NULL);
struct tm tm;
localtime_r(&now, &tm);
if (tm.tm_min != st->last_min || tm.tm_yday != st->last_yday ||
strcmp(prev_bat, st->bat_text) != 0) {
gemui_request_redraw(app);
}
}
static void on_draw(gemui_app_t *app, cairo_t *cr, int w, int h, void *ud) {
@@ -113,6 +129,8 @@ static void on_draw(gemui_app_t *app, cairo_t *cr, int w, int h, void *ud) {
time_t now = time(NULL);
struct tm tm;
localtime_r(&now, &tm);
st->last_min = tm.tm_min;
st->last_yday = tm.tm_yday;
char clock_str[32], date_str[64];
strftime(clock_str, sizeof(clock_str), "%H:%M", &tm);
strftime(date_str, sizeof(date_str), "%A, %d %B %Y", &tm);
@@ -190,6 +208,8 @@ static void on_ptr(gemui_app_t *app, int type, int x, int y, unsigned int btn, v
int main(void) {
lock_state_t st;
memset(&st, 0, sizeof(st));
st.last_min = -1;
st.last_yday = -1;
struct passwd *pw = getpwuid(getuid());
snprintf(st.user, sizeof(st.user), "%s", (pw && pw->pw_name) ? pw->pw_name : "user");
refresh_battery(&st);
@@ -211,7 +231,8 @@ int main(void) {
gemui_handlers_t h = { .on_draw = on_draw, .on_key = on_key, .on_ptr = on_ptr };
gemui_set_handlers(st.app, &h, &st);
gemui_set_timer_ms(st.app, 1000, on_timer, &st);
st.timer_ms = st.grabbed ? 15000 : 250;
gemui_set_timer_ms(st.app, st.timer_ms, on_timer, &st);
int rc = gemui_run(st.app);
gemui_ungrab_input(st.app);
gemui_destroy(st.app);
+2 -2
View File
@@ -35,8 +35,8 @@ geminux-powerd: geminux-powerd.c geminux-power-policy.c geminux-power-policy.h
geminux-power-ctl: geminux-power-ctl.c
$(CC) $(CFLAGS) -o $@ $<
geminux-statusd: geminux-statusd.c
$(CC) $(CFLAGS) $(X11_CFLAGS) -o $@ $< $(X11_LIBS)
geminux-statusd: geminux-statusd.c geminux-power-policy.h geminux-tz-valid.h
$(CC) $(CFLAGS) $(X11_CFLAGS) -o $@ geminux-statusd.c $(X11_LIBS)
geminux-touchd: geminux-touchd.c geminux-touch-gesture.c geminux-touch-gesture.h
$(CC) $(CFLAGS) $(XTOUCH_CFLAGS) -o $@ geminux-touchd.c geminux-touch-gesture.c $(XTOUCH_LIBS)
+10 -2
View File
@@ -237,8 +237,16 @@ int main(void) {
}
} else if (fd == ctx.uev_fd) {
char ubuf[1024];
while (recv(ctx.uev_fd, ubuf, sizeof(ubuf), 0) > 0) {}
open_ccci_devs(&ctx);
ssize_t ur;
bool need_ccci = false;
while ((ur = recv(ctx.uev_fd, ubuf, sizeof(ubuf), 0)) > 0) {
if (memmem(ubuf, (size_t)ur, "ccci", 4) != NULL) {
need_ccci = true;
}
}
if (need_ccci && (ctx.ctl_fd < 0 || ctx.fs_fd < 0 || ctx.rpc_fd < 0)) {
open_ccci_devs(&ctx);
}
} else if (fd == ctx.ctl_fd) {
handle_ctl_read(&ctx);
} else if (fd == ctx.fs_fd || fd == ctx.rpc_fd) {
+28 -2
View File
@@ -62,14 +62,38 @@ int main(int argc, char **argv) {
}
char gov0[64] = "unknown";
char max0[32] = "unknown", max4[32] = "unknown";
read_str("/sys/devices/system/cpu/cpufreq/policy0/scaling_governor", gov0, sizeof(gov0));
read_str("/sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq", max0, sizeof(max0));
read_str("/sys/devices/system/cpu/cpufreq/policy4/scaling_max_freq", max4, sizeof(max4));
const char *bt_state = "absent";
d = opendir("/sys/class/rfkill");
if (d) {
struct dirent *de;
while ((de = readdir(d)) != NULL) {
if (de->d_name[0] == '.') continue;
char p[320], typ[32] = {0}, soft[16] = {0};
snprintf(p, sizeof(p), "/sys/class/rfkill/%s/type", de->d_name);
if (read_str(p, typ, sizeof(typ)) == 0 && strcmp(typ, "bluetooth") == 0) {
snprintf(p, sizeof(p), "/sys/class/rfkill/%s/soft", de->d_name);
if (read_str(p, soft, sizeof(soft)) == 0) {
bt_state = (atoi(soft) != 0) ? "blocked" : "unblocked";
}
break;
}
}
closedir(d);
}
if (json) {
printf("{\"profile\":\"%s\",\"battery_pct\":\"%s\",\"battery_status\":\"%s\","
"\"voltage_uv\":\"%s\",\"current_ua\":\"%s\",\"online_cpus\":%d,"
"\"a72_online\":%d,\"governor\":\"%s\",\"zram_algo\":\"%s\",\"mem_sleep\":\"%s\"}\n",
"\"a72_online\":%d,\"governor\":\"%s\",\"policy0_max_khz\":\"%s\","
"\"policy4_max_khz\":\"%s\",\"bt_rfkill_soft\":\"%s\","
"\"zram_algo\":\"%s\",\"mem_sleep\":\"%s\"}\n",
profile, bat_cap, bat_status, bat_uv, bat_ua, online_cpus, a72_online,
gov0, zram_algo, mem_sleep);
gov0, max0, max4, bt_state, zram_algo, mem_sleep);
return 0;
}
@@ -79,6 +103,8 @@ int main(int argc, char **argv) {
printf("Battery Status : %s%% (%s, V=%s uV, I=%s uA)\n", bat_cap, bat_status, bat_uv, bat_ua);
printf("CPUs Online : %d total (%d A72 big cores online)\n", online_cpus, a72_online);
printf("CPUfreq Governor : %s\n", gov0);
printf("CPUfreq Caps : policy0=%s kHz, policy4=%s kHz\n", max0, max4);
printf("Bluetooth RFKill : %s\n", bt_state);
printf("ZRAM Compression : %s\n", zram_algo);
printf("Suspend Modes : %s\n", mem_sleep);
return 0;
+5 -2
View File
@@ -41,19 +41,22 @@ geminux_profile_params_t geminux_policy_params(geminux_profile_t prof, bool on_a
out.governor = "schedutil";
if (prof == POWER_PROFILE_ECO) {
out.a72_online = false;
out.a53_max_khz = 1391000UL;
out.a53_ll_max_khz = 624000UL;
out.a53_max_khz = 832000UL;
out.a72_max_khz = 0UL;
out.dim_timeout_s = on_ac ? 180 : 60;
out.suspend_timeout_s = on_ac ? 0 : 180;
} else if (prof == POWER_PROFILE_PERFORMANCE) {
out.a72_online = true;
out.a53_ll_max_khz = 1547000UL;
out.a53_max_khz = 1846000UL;
out.a72_max_khz = 2600000UL;
out.dim_timeout_s = on_ac ? 600 : 180;
out.suspend_timeout_s = 0;
} else {
out.a72_online = true;
out.a53_max_khz = 1547000UL;
out.a53_ll_max_khz = on_ac ? 897000UL : 806000UL;
out.a53_max_khz = on_ac ? 1209000UL : 1092000UL;
out.a72_max_khz = 2002000UL;
out.dim_timeout_s = on_ac ? 300 : 120;
out.suspend_timeout_s = on_ac ? 0 : 300;
@@ -2,7 +2,9 @@
#define GEMINUX_POWER_POLICY_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#define GEMINUX_MAX_INHIBITORS 16
@@ -39,6 +41,7 @@ typedef struct {
typedef struct {
const char *governor;
bool a72_online;
unsigned long a53_ll_max_khz;
unsigned long a53_max_khz;
unsigned long a72_max_khz;
int dim_timeout_s;
@@ -58,4 +61,25 @@ bool geminux_policy_add_inhibit(geminux_power_policy_t *p, const char *tag);
bool geminux_policy_del_inhibit(geminux_power_policy_t *p, const char *tag);
bool geminux_should_suspend_on_lid(const geminux_power_policy_t *p);
static inline bool geminux_uevent_has_subsystem(const char *buf, size_t len, const char *subsys) {
if (!buf || len == 0 || !subsys || !subsys[0]) return false;
const size_t slen = strlen(subsys);
static const char pfx[] = "SUBSYSTEM=";
const size_t plen = sizeof(pfx) - 1;
size_t i = 0;
while (i < len) {
while (i < len && (buf[i] == '\0' || buf[i] == '\n' || buf[i] == '\r')) i++;
if (i >= len) break;
size_t start = i;
while (i < len && buf[i] != '\0' && buf[i] != '\n' && buf[i] != '\r') i++;
size_t tok_len = i - start;
if (tok_len == plen + slen &&
memcmp(buf + start, pfx, plen) == 0 &&
memcmp(buf + start + plen, subsys, slen) == 0) {
return true;
}
}
return false;
}
#endif /* GEMINUX_POWER_POLICY_H */
+251 -29
View File
@@ -41,6 +41,18 @@ typedef struct {
uint64_t last_input_ms;
geminux_idle_stage_t idle_stage;
geminux_power_policy_t policy;
bool charge_thresh_set;
bool policy_applied;
geminux_profile_t applied_profile;
bool applied_ac;
bool applied_a72_ready;
bool applied_cpufreq_present;
pid_t consys_restore_pid;
bool consys_suspended;
bool bt_was_loaded;
bool bt_was_blocked;
bool gnss_was_loaded;
bool radio_was_loaded;
} powerd_ctx_t;
static uint64_t monotonic_ms(void) {
@@ -78,7 +90,11 @@ static void ensure_run_dir(void) {
}
static void refresh_ac_and_hdmi(powerd_ctx_t *ctx) {
write_sysfs_str("/sys/class/power_supply/battery/charge_control_end_threshold", "100");
if (!ctx->charge_thresh_set) {
if (write_sysfs_str("/sys/class/power_supply/battery/charge_control_end_threshold", "100") == 0) {
ctx->charge_thresh_set = true;
}
}
ctx->policy.on_ac_power = false;
DIR *d = opendir("/sys/class/power_supply");
if (d) {
@@ -117,6 +133,8 @@ static void refresh_ac_and_hdmi(powerd_ctx_t *ctx) {
static void lock_x11_session(void);
static void trigger_suspend(powerd_ctx_t *ctx);
static void apply_cpu_sysfs(powerd_ctx_t *ctx, bool force);
static void apply_cpu_policy(powerd_ctx_t *ctx, bool force);
static void save_active_brightness(powerd_ctx_t *ctx, const char *bri_path) {
if (ctx->idle_stage != IDLE_STAGE_ACTIVE) return;
@@ -222,10 +240,19 @@ static void note_user_activity(powerd_ctx_t *ctx) {
}
}
static void apply_cpu_policy(powerd_ctx_t *ctx) {
static void apply_cpu_sysfs(powerd_ctx_t *ctx, bool force) {
bool a72_ready = (access("/sys/module/mt6797_a72", F_OK) == 0);
bool cpufreq_present = (access("/sys/devices/system/cpu/cpufreq/policy0", F_OK) == 0);
if (!force && ctx->policy_applied &&
ctx->applied_profile == ctx->policy.profile &&
ctx->applied_ac == ctx->policy.on_ac_power &&
ctx->applied_a72_ready == a72_ready &&
ctx->applied_cpufreq_present == cpufreq_present) {
return;
}
geminux_profile_params_t par = geminux_policy_params(ctx->policy.profile, ctx->policy.on_ac_power);
bool a72_ready = (access("/sys/module/mt6797_a72", F_OK) == 0);
if (!par.a72_online || a72_ready) {
for (int cpu = 8; cpu <= 9; cpu++) {
char p[128];
@@ -244,7 +271,8 @@ static void apply_cpu_policy(powerd_ctx_t *ctx) {
write_sysfs_str(p, par.governor);
int first_cpu = atoi(de->d_name + 6);
unsigned long cap = (first_cpu >= 8) ? par.a72_max_khz : par.a53_max_khz;
unsigned long cap = (first_cpu >= 8) ? par.a72_max_khz :
(first_cpu >= 4) ? par.a53_max_khz : par.a53_ll_max_khz;
if (cap > 0) {
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpufreq/%s/scaling_max_freq", de->d_name);
snprintf(freq_buf, sizeof(freq_buf), "%lu", cap);
@@ -254,8 +282,20 @@ static void apply_cpu_policy(powerd_ctx_t *ctx) {
closedir(d);
}
ensure_run_dir();
write_sysfs_str(PROFILE_FILE, geminux_policy_profile_name(ctx->policy.profile));
if (force || !ctx->policy_applied || ctx->applied_profile != ctx->policy.profile) {
ensure_run_dir();
write_sysfs_str(PROFILE_FILE, geminux_policy_profile_name(ctx->policy.profile));
}
ctx->policy_applied = true;
ctx->applied_profile = ctx->policy.profile;
ctx->applied_ac = ctx->policy.on_ac_power;
ctx->applied_a72_ready = a72_ready;
ctx->applied_cpufreq_present = cpufreq_present;
}
static void apply_cpu_policy(powerd_ctx_t *ctx, bool force) {
apply_cpu_sysfs(ctx, force);
evaluate_idle(ctx);
}
@@ -334,23 +374,194 @@ static void lock_x11_session(void) {
close(pfd[0]);
}
static int run_cmd_sync(const char *const argv[]) {
pid_t pid = fork();
if (pid < 0) return -1;
if (pid == 0) {
int devnull = open("/dev/null", O_RDWR);
if (devnull >= 0) {
if (devnull != STDIN_FILENO) dup2(devnull, STDIN_FILENO);
if (devnull != STDOUT_FILENO) dup2(devnull, STDOUT_FILENO);
if (devnull != STDERR_FILENO) dup2(devnull, STDERR_FILENO);
if (devnull > 2) close(devnull);
}
execv(argv[0], (char *const *)argv);
_exit(127);
}
int st = 0;
while (waitpid(pid, &st, 0) < 0) {
if (errno != EINTR) return -1;
}
return (WIFEXITED(st) && WEXITSTATUS(st) == 0) ? 0 : -1;
}
static void reap_consys_restore(powerd_ctx_t *ctx, bool wait_block) {
if (ctx->consys_restore_pid <= 0) return;
int opts = wait_block ? 0 : WNOHANG;
pid_t r;
do {
r = waitpid(ctx->consys_restore_pid, NULL, opts);
} while (r < 0 && errno == EINTR);
if (r == ctx->consys_restore_pid || (r < 0 && errno == ECHILD)) {
ctx->consys_restore_pid = -1;
}
}
static bool is_bt_rfkill_soft_blocked(void) {
DIR *d = opendir("/sys/class/rfkill");
if (!d) return true;
bool found = false;
bool blocked = true;
struct dirent *de;
while ((de = readdir(d)) != NULL) {
if (de->d_name[0] == '.') continue;
char p[320], typ[32] = {0}, soft[16] = {0};
snprintf(p, sizeof(p), "/sys/class/rfkill/%s/type", de->d_name);
if (read_sysfs_line(p, typ, sizeof(typ)) == 0 && strcmp(typ, "bluetooth") == 0) {
found = true;
snprintf(p, sizeof(p), "/sys/class/rfkill/%s/soft", de->d_name);
if (read_sysfs_line(p, soft, sizeof(soft)) == 0 && atoi(soft) == 0) {
blocked = false;
}
}
}
closedir(d);
return found ? blocked : true;
}
static void set_bt_rfkill_soft_blocked(bool blocked) {
DIR *d = opendir("/sys/class/rfkill");
if (d) {
struct dirent *de;
while ((de = readdir(d)) != NULL) {
if (de->d_name[0] == '.') continue;
char p[320], typ[32] = {0};
snprintf(p, sizeof(p), "/sys/class/rfkill/%s/type", de->d_name);
if (read_sysfs_line(p, typ, sizeof(typ)) == 0 && strcmp(typ, "bluetooth") == 0) {
snprintf(p, sizeof(p), "/sys/class/rfkill/%s/soft", de->d_name);
write_sysfs_str(p, blocked ? "1" : "0");
}
}
closedir(d);
}
if (blocked) {
const char *argv[] = {"/usr/sbin/rfkill", "block", "bluetooth", NULL};
run_cmd_sync(argv);
}
}
/* mt6797_consys_prepare() refuses suspend with -EBUSY while CONN is powered;
* unloading reverse dependents lets mt6797_consys_remove() power off CONN/VCN rails. */
static void prepare_consys_for_suspend(powerd_ctx_t *ctx) {
reap_consys_restore(ctx, true);
if (access("/sys/module/mt6797_consys", F_OK) != 0) {
ctx->consys_suspended = false;
return;
}
ctx->consys_suspended = true;
ctx->bt_was_loaded = (access("/sys/module/btmt6797", F_OK) == 0);
ctx->bt_was_blocked = ctx->bt_was_loaded ? is_bt_rfkill_soft_blocked() : true;
ctx->gnss_was_loaded = (access("/sys/module/gnss_mt6797", F_OK) == 0);
ctx->radio_was_loaded = (access("/sys/module/radio_mt6797", F_OK) == 0);
static const char *const mods[] = {
"btmt6797",
"radio_mt6797",
"gnss_mt6797",
"mac",
"mt6797_consys",
};
for (size_t i = 0; i < sizeof(mods) / sizeof(mods[0]); i++) {
char mpath[128];
snprintf(mpath, sizeof(mpath), "/sys/module/%s", mods[i]);
if (access(mpath, F_OK) == 0) {
const char *argv[] = {"/sbin/rmmod", mods[i], NULL};
run_cmd_sync(argv);
}
}
}
static void restore_consys_after_resume(powerd_ctx_t *ctx) {
if (!ctx->consys_suspended) return;
ctx->consys_suspended = false;
reap_consys_restore(ctx, true);
bool bt_loaded = ctx->bt_was_loaded;
bool bt_blocked = ctx->bt_was_blocked;
bool gnss_loaded = ctx->gnss_was_loaded;
bool radio_loaded = ctx->radio_was_loaded;
pid_t pid = fork();
if (pid < 0) return;
if (pid == 0) {
int devnull = open("/dev/null", O_RDWR);
if (devnull >= 0) {
if (devnull != STDIN_FILENO) dup2(devnull, STDIN_FILENO);
if (devnull != STDOUT_FILENO) dup2(devnull, STDOUT_FILENO);
if (devnull != STDERR_FILENO) dup2(devnull, STDERR_FILENO);
if (devnull > 2) close(devnull);
}
const char *wlan_core[] = {"/sbin/modprobe", "mt6797-wlan-core", NULL};
const char *consys[] = {"/sbin/modprobe", "mt6797-consys", NULL};
const char *mac[] = {"/sbin/modprobe", "mac", NULL};
run_cmd_sync(wlan_core);
run_cmd_sync(consys);
run_cmd_sync(mac);
if (bt_loaded) {
const char *bt[] = {"/sbin/modprobe", "btmt6797", NULL};
run_cmd_sync(bt);
if (bt_blocked) {
set_bt_rfkill_soft_blocked(true);
}
}
if (gnss_loaded) {
const char *gnss[] = {"/sbin/modprobe", "gnss-mt6797", NULL};
run_cmd_sync(gnss);
}
if (radio_loaded) {
const char *radio[] = {"/sbin/modprobe", "radio-mt6797", NULL};
run_cmd_sync(radio);
}
_exit(0);
}
ctx->consys_restore_pid = pid;
}
static void refresh_lid_state(powerd_ctx_t *ctx) {
for (int i = 0; i < ctx->num_ev; i++) {
unsigned long evbits = 0;
if (ioctl(ctx->ev_fds[i], EVIOCGBIT(0, sizeof(evbits)), &evbits) >= 0 &&
(evbits & (1UL << EV_SW))) {
unsigned long swbits = 0, swstate = 0;
if (ioctl(ctx->ev_fds[i], EVIOCGBIT(EV_SW, sizeof(swbits)), &swbits) >= 0 &&
(swbits & (1UL << SW_LID)) &&
ioctl(ctx->ev_fds[i], EVIOCGSW(sizeof(swstate)), &swstate) >= 0) {
ctx->policy.lid_closed = !!(swstate & (1UL << SW_LID));
}
}
}
}
static void trigger_suspend(powerd_ctx_t *ctx) {
lock_x11_session();
toggle_backlight(ctx, false);
prepare_consys_for_suspend(ctx);
write_sysfs_str("/sys/power/mem_sleep", "deep");
if (write_sysfs_str("/sys/power/state", "mem") == 0 ||
write_sysfs_str("/sys/power/state", "freeze") == 0) {
/* Woken back up from suspend */
bool suspended = (write_sysfs_str("/sys/power/state", "mem") == 0 ||
write_sysfs_str("/sys/power/state", "freeze") == 0);
restore_consys_after_resume(ctx);
refresh_lid_state(ctx);
refresh_ac_and_hdmi(ctx);
if (suspended && !ctx->policy.lid_closed) {
ctx->idle_stage = IDLE_STAGE_ACTIVE;
ctx->last_input_ms = monotonic_ms();
toggle_backlight(ctx, true);
refresh_ac_and_hdmi(ctx);
apply_cpu_policy(ctx);
} else if (!ctx->policy.lid_closed) {
ctx->idle_stage = IDLE_STAGE_ACTIVE;
ctx->last_input_ms = monotonic_ms();
toggle_backlight(ctx, true);
evaluate_idle(ctx);
apply_cpu_policy(ctx, true);
} else {
toggle_backlight(ctx, false);
ctx->idle_stage = IDLE_STAGE_LOCKED_BLANK;
arm_timer_ms(ctx->tmr_fd, 0);
apply_cpu_sysfs(ctx, true);
}
}
@@ -396,7 +607,7 @@ static void handle_client_cmd(powerd_ctx_t *ctx, int cfd) {
if (strncmp(buf, "profile ", 8) == 0) {
if (geminux_policy_set_profile_name(&ctx->policy, buf + 8)) {
apply_cpu_policy(ctx);
apply_cpu_policy(ctx, false);
dprintf(cfd, "OK profile=%s\n", geminux_policy_profile_name(ctx->policy.profile));
} else {
dprintf(cfd, "ERR unknown profile\n");
@@ -406,11 +617,11 @@ static void handle_client_cmd(powerd_ctx_t *ctx, int cfd) {
trigger_suspend(ctx);
} else if (strncmp(buf, "inhibit add ", 12) == 0) {
geminux_policy_add_inhibit(&ctx->policy, buf + 12);
apply_cpu_policy(ctx);
apply_cpu_policy(ctx, false);
dprintf(cfd, "OK inhibits=%d\n", ctx->policy.inhibit_count);
} else if (strncmp(buf, "inhibit del ", 12) == 0) {
geminux_policy_del_inhibit(&ctx->policy, buf + 12);
apply_cpu_policy(ctx);
apply_cpu_policy(ctx, false);
dprintf(cfd, "OK inhibits=%d\n", ctx->policy.inhibit_count);
} else if (strcmp(buf, "lid-suspend on") == 0) {
ctx->policy.lid_suspend_enabled = true;
@@ -469,6 +680,7 @@ int main(void) {
signal(SIGPIPE, SIG_IGN);
powerd_ctx_t ctx;
memset(&ctx, 0, sizeof(ctx));
ctx.consys_restore_pid = -1;
ctx.saved_brightness = 128;
geminux_policy_init(&ctx.policy);
@@ -497,12 +709,13 @@ int main(void) {
ctx.idle_stage = IDLE_STAGE_ACTIVE;
refresh_ac_and_hdmi(&ctx);
scan_input_devices(&ctx);
apply_cpu_policy(&ctx);
apply_cpu_policy(&ctx, true);
struct epoll_event events[16];
bool running = true;
while (running) {
int n = epoll_wait(ctx.epfd, events, 16, -1);
reap_consys_restore(&ctx, false);
if (n < 0) {
if (errno == EINTR) continue;
break;
@@ -514,7 +727,7 @@ int main(void) {
while (read(ctx.sig_fd, &si, sizeof(si)) > 0) {
if (si.ssi_signo == SIGHUP) {
refresh_ac_and_hdmi(&ctx);
apply_cpu_policy(&ctx);
apply_cpu_policy(&ctx, true);
} else {
running = false;
}
@@ -534,19 +747,28 @@ int main(void) {
char ubuf[2048];
ssize_t ur;
bool need_ev_rescan = false;
bool need_ps_or_drm = false;
while ((ur = recv(ctx.uev_fd, ubuf, sizeof(ubuf), 0)) > 0) {
if (memmem(ubuf, (size_t)ur, "SUBSYSTEM=input", 15)) {
if (geminux_uevent_has_subsystem(ubuf, (size_t)ur, "input")) {
need_ev_rescan = true;
}
if (geminux_uevent_has_subsystem(ubuf, (size_t)ur, "power_supply") ||
geminux_uevent_has_subsystem(ubuf, (size_t)ur, "drm")) {
need_ps_or_drm = true;
}
}
if (need_ev_rescan) scan_input_devices(&ctx);
bool prev_ac = ctx.policy.on_ac_power;
bool prev_hdmi = ctx.policy.hdmi_connected;
refresh_ac_and_hdmi(&ctx);
if (prev_ac != ctx.policy.on_ac_power || prev_hdmi != ctx.policy.hdmi_connected) {
note_user_activity(&ctx);
if (need_ps_or_drm) {
bool prev_ac = ctx.policy.on_ac_power;
bool prev_hdmi = ctx.policy.hdmi_connected;
refresh_ac_and_hdmi(&ctx);
if (prev_ac != ctx.policy.on_ac_power || prev_hdmi != ctx.policy.hdmi_connected) {
if (!ctx.policy.lid_closed) {
note_user_activity(&ctx);
}
apply_cpu_policy(&ctx, false);
}
}
apply_cpu_policy(&ctx);
} else {
struct input_event iev;
bool saw_activity = false;
@@ -567,7 +789,7 @@ int main(void) {
ctx.last_input_ms = monotonic_ms();
toggle_backlight(&ctx, true);
refresh_ac_and_hdmi(&ctx);
apply_cpu_policy(&ctx);
apply_cpu_policy(&ctx, true);
}
} else if (iev.type == EV_KEY && iev.code == KEY_POWER && iev.value == 1) {
if (ctx.idle_stage == IDLE_STAGE_LOCKED_BLANK) {
+16 -2
View File
@@ -16,6 +16,7 @@
#include <time.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include "geminux-power-policy.h"
#include "geminux-tz-valid.h"
static int read_line(const char *path, char *buf, size_t sz) {
@@ -104,10 +105,14 @@ static void update_status(Display *dpy) {
char clk[32];
strftime(clk, sizeof(clk), "%a %d %H:%M", &tm);
static char last_bar[256] = {0};
char bar[256];
snprintf(bar, sizeof(bar), " %s | %s | %s | %s ", net, mdm, bat, clk);
XStoreName(dpy, DefaultRootWindow(dpy), bar);
XFlush(dpy);
if (strcmp(bar, last_bar) != 0) {
snprintf(last_bar, sizeof(last_bar), "%s", bar);
XStoreName(dpy, DefaultRootWindow(dpy), bar);
XFlush(dpy);
}
}
static void arm_minute_timer(int tfd) {
@@ -189,6 +194,15 @@ int main(void) {
running = false;
}
}
} else if (fd == uev_fd) {
char drain[2048];
ssize_t nr;
while ((nr = recv(uev_fd, drain, sizeof(drain), 0)) > 0) {
if (geminux_uevent_has_subsystem(drain, (size_t)nr, "power_supply") ||
geminux_uevent_has_subsystem(drain, (size_t)nr, "net")) {
dirty = true;
}
}
} else {
char drain[1024];
while (read(fd, drain, sizeof(drain)) > 0) {}