feat: improve session switching, fractional DPI/GTK theming, XDG launcher, CPU/RAM status, and power policy

This commit is contained in:
Luxferre committed 2026-11-26 14:46:03 +02:00
1 parent 72b05b650e
commit 07d9134d2d
28 files changed
+2670 -302

No files matched your search

+126 -1
View File
@@ -23,6 +23,14 @@
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
#include <sys/sysctl.h>
#if defined(__OpenBSD__)
#include <uvm/uvmexp.h>
#elif defined(__NetBSD__)
#include <uvm/uvm_extern.h>
#endif
#endif
#ifndef ANODE_PREFIX
#define ANODE_PREFIX "/usr/local"
@@ -34,6 +42,21 @@ static uint64_t plat_now_ms(void) {
return (uint64_t)ts.tv_sec * 1000ULL + (uint64_t)(ts.tv_nsec / 1000000L);
}
static int plat_read_buf(const char *path, char *buf, size_t sz) {
if (!path || !buf || sz < 2) return -1;
int fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd < 0) return -1;
size_t off = 0;
while (off + 1 < sz) {
ssize_t n = read(fd, buf + off, sz - 1 - off);
if (n <= 0) break;
off += (size_t)n;
}
close(fd);
buf[off] = '\0';
return off > 0 ? 0 : -1;
}
static int plat_read_line(const char *path, char *buf, size_t sz) {
if (!path || !buf || sz == 0) return -1;
FILE *f = fopen(path, "r");
@@ -525,7 +548,7 @@ bool anode_backlight_get(int *cur_out, int *max_out, char *backend_out, size_t b
if (plat_read_line(p, b, sizeof(b)) == 0 && atoi(b) > 0) {
mx = atoi(b);
snprintf(p, sizeof(p), "/sys/class/backlight/%s/brightness", de->d_name);
if (plat_read_line(p, b, sizeof(b)) == 0) {
if (access(p, W_OK) == 0 && plat_read_line(p, b, sizeof(b)) == 0) {
cur = atoi(b);
if (cur_out) *cur_out = cur;
if (max_out) *max_out = mx;
@@ -1037,3 +1060,105 @@ bool anode_find_wifi_iface(char *out, size_t sz) {
}
return false;
}
bool anode_read_cpu_sample(anode_cpu_sample_t *out) {
if (!out) return false;
#if defined(__linux__) || defined(__linux)
char buf[512];
if (plat_read_buf("/proc/stat", buf, sizeof(buf)) == 0) {
return anode_parse_proc_stat_cpu(buf, out);
}
return false;
#elif defined(__FreeBSD__)
long cp[5];
size_t len = sizeof(cp);
if (sysctlbyname("kern.cp_time", cp, &len, NULL, 0) == 0 && len >= sizeof(cp)) {
unsigned long long tot = 0;
for (int i = 0; i < 5; i++) tot += (unsigned long long)cp[i];
out->total = tot;
out->idle = (unsigned long long)cp[4];
return tot > 0;
}
return false;
#elif defined(__OpenBSD__)
int mib[2] = {CTL_KERN, KERN_CPTIME};
long cp[5];
size_t len = sizeof(cp);
if (sysctl(mib, 2, cp, &len, NULL, 0) == 0 && len >= sizeof(cp)) {
unsigned long long tot = 0;
for (int i = 0; i < 5; i++) tot += (unsigned long long)cp[i];
out->total = tot;
out->idle = (unsigned long long)cp[4];
return tot > 0;
}
return false;
#elif defined(__NetBSD__)
uint64_t cp[5];
size_t len = sizeof(cp);
if (sysctlbyname("kern.cp_time", cp, &len, NULL, 0) == 0 && len >= sizeof(cp)) {
unsigned long long tot = 0;
for (int i = 0; i < 5; i++) tot += (unsigned long long)cp[i];
out->total = tot;
out->idle = (unsigned long long)cp[4];
return tot > 0;
}
return false;
#else
return false;
#endif
}
bool anode_read_mem_usage_pct(int *pct_out) {
if (!pct_out) return false;
#if defined(__linux__) || defined(__linux)
char buf[4096];
if (plat_read_buf("/proc/meminfo", buf, sizeof(buf)) == 0) {
return anode_parse_proc_meminfo_pct(buf, pct_out);
}
return false;
#elif defined(__FreeBSD__)
unsigned long phys = 0;
unsigned int pg = 0, vfree = 0, vinact = 0;
size_t s_phys = sizeof(phys), s_u = sizeof(pg);
if (sysctlbyname("hw.physmem", &phys, &s_phys, NULL, 0) != 0 || phys == 0) return false;
if (sysctlbyname("vm.stats.vm.v_page_size", &pg, &s_u, NULL, 0) != 0 || pg == 0) return false;
s_u = sizeof(vfree);
sysctlbyname("vm.stats.vm.v_free_count", &vfree, &s_u, NULL, 0);
s_u = sizeof(vinact);
sysctlbyname("vm.stats.vm.v_inactive_count", &vinact, &s_u, NULL, 0);
unsigned long long avail = (unsigned long long)(vfree + vinact) * (unsigned long long)pg;
if (avail > phys) avail = phys;
int pct = (int)(((phys - avail) * 100ULL + phys / 2ULL) / phys);
if (pct < 0) pct = 0;
if (pct > 100) pct = 100;
*pct_out = pct;
return true;
#elif defined(__OpenBSD__)
int mib[2] = {CTL_VM, VM_UVMEXP};
struct uvmexp uvm;
size_t len = sizeof(uvm);
if (sysctl(mib, 2, &uvm, &len, NULL, 0) != 0 || uvm.npages <= 0) return false;
unsigned long long tot = (unsigned long long)uvm.npages;
unsigned long long avail = (unsigned long long)(uvm.free + uvm.inactive);
if (avail > tot) avail = tot;
int pct = (int)(((tot - avail) * 100ULL + tot / 2ULL) / tot);
if (pct < 0) pct = 0;
if (pct > 100) pct = 100;
*pct_out = pct;
return true;
#elif defined(__NetBSD__)
struct uvmexp_sysctl uvm;
size_t len = sizeof(uvm);
if (sysctlbyname("vm.uvmexp2", &uvm, &len, NULL, 0) != 0 || uvm.npages <= 0) return false;
unsigned long long tot = (unsigned long long)uvm.npages;
unsigned long long avail = (unsigned long long)(uvm.free + uvm.inactive);
if (avail > tot) avail = tot;
int pct = (int)(((tot - avail) * 100ULL + tot / 2ULL) / tot);
if (pct < 0) pct = 0;
if (pct > 100) pct = 100;
*pct_out = pct;
return true;
#else
return false;
#endif
}
+6
View File
@@ -7,6 +7,8 @@
#include <stdbool.h>
#include <stddef.h>
#include "anode_sys_parse.h"
#ifdef __cplusplus
extern "C" {
#endif
@@ -77,6 +79,10 @@ int anode_power_action(const char *action); /* suspend, hibernate, reboot, power
int anode_list_net_ifaces(anode_net_iface_t *out, int max_ifaces);
bool anode_find_wifi_iface(char *out, size_t sz);
/* System telemetry (CPU & RAM usage) */
bool anode_read_cpu_sample(anode_cpu_sample_t *out);
bool anode_read_mem_usage_pct(int *pct_out);
#ifdef __cplusplus
}
#endif
+7 -6
View File
@@ -32,9 +32,9 @@ typedef struct {
static inline anode_power_cfg_t anode_power_cfg_default(void) {
anode_power_cfg_t c;
c.dim_ms = 60000U;
c.lock_ms = 180000U;
c.suspend_ms = 600000U;
c.dim_ms = 300000U; /* 5 min */
c.lock_ms = 600000U; /* 10 min */
c.suspend_ms = 1800000U; /* 30 min (battery only) */
c.crit_batt_pct = 5;
c.suspend_on_ac = false;
return c;
@@ -67,9 +67,10 @@ static inline void anode_power_policy_step(const anode_power_cfg_t *cfg,
}
}
/* User activity resumes from dimmed/suspended */
if (idle_ms < cfg->dim_ms) {
if (*state == ANODE_PWR_DIMMED || *state == ANODE_PWR_SUSPENDED) {
/* User activity resumes from dimmed/locked/suspended */
uint32_t resume_thresh = cfg->dim_ms > 0 ? cfg->dim_ms : (cfg->lock_ms > 0 ? cfg->lock_ms : cfg->suspend_ms);
if (resume_thresh == 0 || idle_ms < resume_thresh) {
if (*state == ANODE_PWR_DIMMED || *state == ANODE_PWR_LOCKED || *state == ANODE_PWR_SUSPENDED) {
a.restore_brightness = true;
}
*state = screen_locked ? ANODE_PWR_LOCKED : ANODE_PWR_ACTIVE;
+6 -3
View File
@@ -131,12 +131,15 @@ static inline void anode_read_xres_scale_from_db(XrmDatabase db, Display *dpy,
dpi = anode_clamp_dpi(v);
have_dpi = 1;
}
if (XrmGetResource(db, "anode.scale", "Anode.Scale", &type, &val) && val.addr &&
anode_parse_positive_double(val.addr, &v)) {
if ((XrmGetResource(db, "anode.scale", "Anode.Scale", &type, &val) && val.addr &&
anode_parse_positive_double(val.addr, &v)) ||
(XrmGetResource(db, "theme.scale", "Theme.Scale", &type, &val) && val.addr &&
anode_parse_positive_double(val.addr, &v))) {
scale = anode_clamp_scale(v);
have_scale = 1;
}
if (XrmGetResource(db, "anode.touchMode", "Anode.TouchMode", &type, &val) && val.addr) {
if ((XrmGetResource(db, "anode.touchMode", "Anode.TouchMode", &type, &val) && val.addr) ||
(XrmGetResource(db, "theme.touchMode", "Theme.TouchMode", &type, &val) && val.addr)) {
touch = anode_parse_bool_str(val.addr, 0);
}
}
+126
View File
@@ -0,0 +1,126 @@
/*
* ANODE Cross-Unix CPU & Memory Parsers
*/
#ifndef ANODE_SYS_PARSE_H
#define ANODE_SYS_PARSE_H
#include <ctype.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
unsigned long long total;
unsigned long long idle;
} anode_cpu_sample_t;
static inline bool anode_parse_proc_stat_cpu(const char *text, anode_cpu_sample_t *out) {
if (!text || !out) return false;
out->total = 0;
out->idle = 0;
const char *p = text;
while (*p) {
const char *nl = strchr(p, '\n');
size_t len = nl ? (size_t)(nl - p) : strlen(p);
if (len >= 4 && strncmp(p, "cpu", 3) == 0 && isspace((unsigned char)p[3])) {
const char *cur = p + 3;
const char *end = p + len;
unsigned long long vals[10];
int count = 0;
while (cur < end && count < 10) {
while (cur < end && isspace((unsigned char)*cur)) cur++;
if (cur >= end || !isdigit((unsigned char)*cur)) break;
char *next = NULL;
unsigned long long v = strtoull(cur, &next, 10);
if (next == cur || next > end) break;
vals[count++] = v;
cur = next;
}
if (count < 4) return false;
unsigned long long tot = 0;
for (int i = 0; i < count; i++) tot += vals[i];
unsigned long long idl = vals[3] + (count > 4 ? vals[4] : 0ULL);
out->total = tot;
out->idle = idl;
return tot > 0;
}
if (!nl) break;
p = nl + 1;
}
return false;
}
static inline int anode_cpu_usage_pct(const anode_cpu_sample_t *prev,
const anode_cpu_sample_t *cur) {
if (!prev || !cur || cur->total <= prev->total) return 0;
unsigned long long dt = cur->total - prev->total;
unsigned long long di = (cur->idle >= prev->idle) ? (cur->idle - prev->idle) : 0ULL;
if (di > dt) di = dt;
unsigned long long used = dt - di;
int pct = (int)((used * 100ULL + dt / 2ULL) / dt);
if (pct < 0) pct = 0;
if (pct > 100) pct = 100;
return pct;
}
static inline bool anode_parse_proc_meminfo_pct(const char *text, int *pct_out) {
if (!text || !pct_out) return false;
unsigned long long mem_total = 0;
unsigned long long mem_avail = 0;
unsigned long long mem_free = 0;
unsigned long long buffers = 0;
unsigned long long cached = 0;
unsigned long long srecl = 0;
bool has_total = false;
bool has_avail = false;
const char *p = text;
while (*p) {
const char *nl = strchr(p, '\n');
size_t len = nl ? (size_t)(nl - p) : strlen(p);
const char *colon = memchr(p, ':', len);
if (colon) {
size_t klen = (size_t)(colon - p);
const char *vp = colon + 1;
const char *end = p + len;
while (vp < end && isspace((unsigned char)*vp)) vp++;
if (vp < end && isdigit((unsigned char)*vp)) {
char *next = NULL;
unsigned long long val = strtoull(vp, &next, 10);
if (next > vp) {
if (klen == 8 && strncmp(p, "MemTotal", 8) == 0) {
mem_total = val;
has_total = true;
} else if (klen == 12 && strncmp(p, "MemAvailable", 12) == 0) {
mem_avail = val;
has_avail = true;
} else if (klen == 7 && strncmp(p, "MemFree", 7) == 0) {
mem_free = val;
} else if (klen == 7 && strncmp(p, "Buffers", 7) == 0) {
buffers = val;
} else if (klen == 6 && strncmp(p, "Cached", 6) == 0) {
cached = val;
} else if (klen == 12 && strncmp(p, "SReclaimable", 12) == 0) {
srecl = val;
}
}
}
}
if (!nl) break;
p = nl + 1;
}
if (!has_total || mem_total == 0) return false;
unsigned long long avail = has_avail ? mem_avail : (mem_free + buffers + cached + srecl);
if (avail > mem_total) avail = mem_total;
unsigned long long used = mem_total - avail;
int pct = (int)((used * 100ULL + mem_total / 2ULL) / mem_total);
if (pct < 0) pct = 0;
if (pct > 100) pct = 100;
*pct_out = pct;
return true;
}
#endif /* ANODE_SYS_PARSE_H */