Fix SSD2092 wake, fuel-gauge jump, XLibre rotation, media keys, and popup rendering

This commit is contained in:
Luxferre committed 2026-11-24 11:05:18 +02:00
1 parent 7f3b3c2c0c
commit dbc0275979
43 files changed
+3388 -115

No files matched your search

+218 -6
View File
@@ -27,6 +27,9 @@
#define SOCK_PATH "/run/geminux/powerd.sock"
#define PROFILE_FILE "/run/geminux/power-profile"
#define FG_STATE_DIR "/var/lib/geminux"
#define FG_STATE_FILE "/var/lib/geminux/fuel-gauge.state"
#define FG_SYSFS_DIR "/sys/class/power_supply/battery/device"
#define MAX_EVDEVS 16
typedef struct {
@@ -42,11 +45,16 @@ typedef struct {
geminux_idle_stage_t idle_stage;
geminux_power_policy_t policy;
bool charge_thresh_set;
bool fg_restored;
bool fg_file_synced;
uint32_t saved_fg_r_uohm;
uint32_t saved_fg_full_uah;
bool policy_applied;
geminux_profile_t applied_profile;
bool applied_ac;
bool applied_a72_ready;
bool applied_cpufreq_present;
bool applied_interactive_boost;
pid_t consys_restore_pid;
bool consys_suspended;
bool bt_was_loaded;
@@ -89,6 +97,91 @@ static void ensure_run_dir(void) {
if (gr) chown("/run/geminux", 0, gr->gr_gid);
}
static int read_sysfs_u32(const char *path, uint32_t *out) {
char buf[48] = {0};
if (read_sysfs_line(path, buf, sizeof(buf)) != 0 || !buf[0]) return -1;
char *end = NULL;
errno = 0;
unsigned long long v = strtoull(buf, &end, 10);
if (errno != 0 || end == buf || *end != '\0' || v > 0xffffffffULL) return -1;
*out = (uint32_t)v;
return 0;
}
static void restore_fuel_gauge_state(powerd_ctx_t *ctx) {
const char *r_path = FG_SYSFS_DIR "/learned_resistance_uohm";
const char *f_path = FG_SYSFS_DIR "/learned_full_uah";
if (access(r_path, F_OK) != 0 || access(f_path, F_OK) != 0) return;
FILE *f = fopen(FG_STATE_FILE, "r");
if (f) {
char buf[256] = {0};
size_t n = fread(buf, 1, sizeof(buf) - 1, f);
fclose(f);
buf[n] = '\0';
uint32_t r_val = 0, f_val = 0;
if (geminux_fg_state_parse(buf, &r_val, &f_val)) {
char r_str[32], f_str[32];
snprintf(r_str, sizeof(r_str), "%u", r_val);
snprintf(f_str, sizeof(f_str), "%u", f_val);
write_sysfs_str(r_path, r_str);
write_sysfs_str(f_path, f_str);
ctx->fg_file_synced = true;
}
}
uint32_t cur_r = 0, cur_f = 0;
if (read_sysfs_u32(r_path, &cur_r) == 0 && read_sysfs_u32(f_path, &cur_f) == 0) {
ctx->saved_fg_r_uohm = cur_r;
ctx->saved_fg_full_uah = cur_f;
}
ctx->fg_restored = true;
}
static void save_fuel_gauge_state(powerd_ctx_t *ctx) {
uint32_t r_val = 0, f_val = 0;
if (read_sysfs_u32(FG_SYSFS_DIR "/learned_resistance_uohm", &r_val) != 0 ||
read_sysfs_u32(FG_SYSFS_DIR "/learned_full_uah", &f_val) != 0) {
return;
}
char content[128];
if (geminux_fg_state_format(r_val, f_val, content, sizeof(content)) != 0) return;
if (ctx->fg_file_synced &&
r_val == ctx->saved_fg_r_uohm &&
f_val == ctx->saved_fg_full_uah) {
return;
}
mkdir("/var/lib", 0755);
mkdir(FG_STATE_DIR, 0755);
char tmp_path[128];
snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", FG_STATE_FILE);
int fd = open(tmp_path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
if (fd < 0) return;
size_t len = strlen(content), off = 0;
bool ok = true;
while (off < len) {
ssize_t w = write(fd, content + off, len - off);
if (w <= 0) {
if (w < 0 && errno == EINTR) continue;
ok = false;
break;
}
off += (size_t)w;
}
if (ok && fsync(fd) != 0) ok = false;
if (close(fd) != 0) ok = false;
if (ok && rename(tmp_path, FG_STATE_FILE) == 0) {
ctx->saved_fg_r_uohm = r_val;
ctx->saved_fg_full_uah = f_val;
ctx->fg_file_synced = true;
} else {
unlink(tmp_path);
}
}
static void refresh_ac_and_hdmi(powerd_ctx_t *ctx) {
if (!ctx->charge_thresh_set) {
if (write_sysfs_str("/sys/class/power_supply/battery/charge_control_end_threshold", "100") == 0) {
@@ -156,6 +249,9 @@ static void toggle_backlight(powerd_ctx_t *ctx, bool on) {
save_active_brightness(ctx, p);
write_sysfs_str(p, "0");
} else {
char pwr[320];
snprintf(pwr, sizeof(pwr), "/sys/class/backlight/%s/bl_power", de->d_name);
write_sysfs_str(pwr, "0");
char restore[32];
int val = ctx->saved_brightness > 0 ? ctx->saved_brightness : 128;
snprintf(restore, sizeof(restore), "%d", val);
@@ -217,11 +313,13 @@ static void evaluate_idle(powerd_ctx_t *ctx) {
if (act == IDLE_ACT_DIM) {
dim_backlight(ctx);
ctx->idle_stage = IDLE_STAGE_DIMMED;
apply_cpu_sysfs(ctx, false);
arm_timer_ms(ctx->tmr_fd, next_wait_ms);
} else if (act == IDLE_ACT_LOCK_BLANK) {
lock_x11_session();
toggle_backlight(ctx, false);
ctx->idle_stage = IDLE_STAGE_LOCKED_BLANK;
apply_cpu_sysfs(ctx, false);
arm_timer_ms(ctx->tmr_fd, 0);
} else if (act == IDLE_ACT_SUSPEND) {
arm_timer_ms(ctx->tmr_fd, 0);
@@ -236,6 +334,7 @@ static void note_user_activity(powerd_ctx_t *ctx) {
if (ctx->idle_stage != IDLE_STAGE_ACTIVE) {
ctx->idle_stage = IDLE_STAGE_ACTIVE;
toggle_backlight(ctx, true);
apply_cpu_sysfs(ctx, false);
evaluate_idle(ctx);
}
}
@@ -243,11 +342,15 @@ static void note_user_activity(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);
bool interactive_boost = (ctx->policy.profile == POWER_PROFILE_BALANCED &&
ctx->idle_stage == IDLE_STAGE_ACTIVE &&
!ctx->policy.lid_closed);
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) {
ctx->applied_cpufreq_present == cpufreq_present &&
ctx->applied_interactive_boost == interactive_boost) {
return;
}
@@ -266,17 +369,35 @@ static void apply_cpu_sysfs(powerd_ctx_t *ctx, bool force) {
struct dirent *de;
while ((de = readdir(d)) != NULL) {
if (strncmp(de->d_name, "policy", 6) != 0) continue;
char p[256], freq_buf[32];
char p[256], freq_buf[32], min_buf[32] = "221000", max_hw_buf[32] = {0};
int first_cpu = atoi(de->d_name + 6);
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpufreq/%s/cpuinfo_min_freq", de->d_name);
(void)read_sysfs_line(p, min_buf, sizeof(min_buf));
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpufreq/%s/cpuinfo_max_freq", de->d_name);
unsigned long hw_max =
(read_sysfs_line(p, max_hw_buf, sizeof(max_hw_buf)) == 0) ? strtoul(max_hw_buf, NULL, 10) : 0UL;
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpufreq/%s/scaling_governor", de->d_name);
write_sysfs_str(p, par.governor);
int first_cpu = atoi(de->d_name + 6);
unsigned long cap = (first_cpu >= 8) ? par.a72_max_khz :
(first_cpu >= 4) ? par.a53_max_khz : par.a53_ll_max_khz;
if (hw_max > 0 && (cap == 0 || cap > hw_max)) {
cap = hw_max;
}
/* Reset floor before adjusting ceiling so scaling_min_freq never blocks scaling_max_freq. */
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpufreq/%s/scaling_min_freq", de->d_name);
write_sysfs_str(p, min_buf);
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);
write_sysfs_str(p, freq_buf);
if (interactive_boost && first_cpu < 8) {
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpufreq/%s/scaling_min_freq", de->d_name);
write_sysfs_str(p, freq_buf);
}
}
}
closedir(d);
@@ -292,6 +413,7 @@ static void apply_cpu_sysfs(powerd_ctx_t *ctx, bool force) {
ctx->applied_ac = ctx->policy.on_ac_power;
ctx->applied_a72_ready = a72_ready;
ctx->applied_cpufreq_present = cpufreq_present;
ctx->applied_interactive_boost = interactive_boost;
}
static void apply_cpu_policy(powerd_ctx_t *ctx, bool force) {
@@ -542,15 +664,93 @@ static void refresh_lid_state(powerd_ctx_t *ctx) {
}
}
/* gpio-keys SW_LID applies a 64 ms debounce timer on wakeup; wait up to
* timeout_ms using poll() so a lid-open wake sees SW_LID == 0 before we
* decide whether to light the backlight. */
static void wait_post_resume_lid_events(powerd_ctx_t *ctx, int timeout_ms) {
refresh_lid_state(ctx);
if (!ctx->policy.lid_closed || ctx->num_ev <= 0) return;
uint64_t deadline = monotonic_ms() + (uint64_t)timeout_ms;
struct pollfd pfds[MAX_EVDEVS];
while (ctx->policy.lid_closed) {
uint64_t now = monotonic_ms();
if (now >= deadline) break;
int slice = (int)(deadline - now);
if (slice > 20) slice = 20;
for (int i = 0; i < ctx->num_ev; i++) {
pfds[i].fd = ctx->ev_fds[i];
pfds[i].events = POLLIN;
pfds[i].revents = 0;
}
int pr = poll(pfds, (nfds_t)ctx->num_ev, slice);
if (pr < 0) {
if (errno == EINTR) continue;
break;
}
for (int i = 0; i < ctx->num_ev; i++) {
if (!(pfds[i].revents & POLLIN)) continue;
struct input_event iev;
while (read(ctx->ev_fds[i], &iev, sizeof(iev)) == (ssize_t)sizeof(iev)) {
if (iev.type == EV_SW && iev.code == SW_LID) {
ctx->policy.lid_closed = (iev.value != 0);
} else if (iev.type == EV_KEY || iev.type == EV_REL || iev.type == EV_ABS) {
ctx->last_input_ms = monotonic_ms();
}
}
}
refresh_lid_state(ctx);
}
}
static void prepare_cpus_for_suspend(void) {
DIR *d = opendir("/sys/devices/system/cpu/cpufreq");
if (d) {
struct dirent *de;
while ((de = readdir(d)) != NULL) {
if (strncmp(de->d_name, "policy", 6) != 0) continue;
char p[256], min_buf[32] = "221000";
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpufreq/%s/cpuinfo_min_freq", de->d_name);
(void)read_sysfs_line(p, min_buf, sizeof(min_buf));
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpufreq/%s/scaling_min_freq", de->d_name);
write_sysfs_str(p, min_buf);
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpufreq/%s/scaling_governor", de->d_name);
write_sysfs_str(p, "powersave");
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpufreq/%s/scaling_max_freq", de->d_name);
write_sysfs_str(p, min_buf);
}
closedir(d);
}
for (int cpu = 9; cpu >= 1; cpu--) {
char p[128];
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpu%d/online", cpu);
write_sysfs_str(p, "0");
}
}
static void restore_cpus_after_resume(powerd_ctx_t *ctx) {
for (int cpu = 1; cpu <= 7; cpu++) {
char p[128];
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpu%d/online", cpu);
write_sysfs_str(p, "1");
}
ctx->policy_applied = false;
}
static void trigger_suspend(powerd_ctx_t *ctx) {
arm_timer_ms(ctx->tmr_fd, 0);
lock_x11_session();
toggle_backlight(ctx, false);
save_fuel_gauge_state(ctx);
prepare_consys_for_suspend(ctx);
prepare_cpus_for_suspend();
write_sysfs_str("/sys/power/mem_sleep", "deep");
bool suspended = (write_sysfs_str("/sys/power/state", "mem") == 0 ||
write_sysfs_str("/sys/power/state", "freeze") == 0);
restore_cpus_after_resume(ctx);
restore_consys_after_resume(ctx);
refresh_lid_state(ctx);
wait_post_resume_lid_events(ctx, 90);
refresh_ac_and_hdmi(ctx);
if (suspended && !ctx->policy.lid_closed) {
ctx->idle_stage = IDLE_STAGE_ACTIVE;
@@ -707,6 +907,7 @@ int main(void) {
ctx.last_input_ms = monotonic_ms();
ctx.idle_stage = IDLE_STAGE_ACTIVE;
restore_fuel_gauge_state(&ctx);
refresh_ac_and_hdmi(&ctx);
scan_input_devices(&ctx);
apply_cpu_policy(&ctx, true);
@@ -747,17 +948,27 @@ int main(void) {
char ubuf[2048];
ssize_t ur;
bool need_ev_rescan = false;
bool need_ps = false;
bool need_ps_or_drm = false;
while ((ur = recv(ctx.uev_fd, ubuf, sizeof(ubuf), 0)) > 0) {
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")) {
if (geminux_uevent_has_subsystem(ubuf, (size_t)ur, "power_supply")) {
need_ps = true;
need_ps_or_drm = true;
} else if (geminux_uevent_has_subsystem(ubuf, (size_t)ur, "drm")) {
need_ps_or_drm = true;
}
}
if (need_ev_rescan) scan_input_devices(&ctx);
if (need_ps) {
if (!ctx.fg_restored) {
restore_fuel_gauge_state(&ctx);
} else {
save_fuel_gauge_state(&ctx);
}
}
if (need_ps_or_drm) {
bool prev_ac = ctx.policy.on_ac_power;
bool prev_hdmi = ctx.policy.hdmi_connected;
@@ -810,6 +1021,7 @@ int main(void) {
}
}
save_fuel_gauge_state(&ctx);
unlink(SOCK_PATH);
close(ctx.epfd);
return 0;