Add screensaver auto-lock, lynx/gsocket/apexrc defaults, htoprc, and Bantam updates
This commit is contained in:
1 parent
b7dca4f15f
commit
c12ed68d19
36 files changed
+1421
-75
No files matched your search
@@ -116,7 +116,7 @@ Bantam reads `.bantam.cfg` from the current working directory.
|
||||
|
||||
```ini
|
||||
endpoint=https://api.kilo.ai/api/openrouter
|
||||
model=openrouter/free
|
||||
model=kilo-auto/free
|
||||
temperature=0.7
|
||||
api_key=your_api_key_here
|
||||
stream=true
|
||||
@@ -151,7 +151,7 @@ Bantam reads `.bantam.cfg` from the current working directory.
|
||||
tokens, and context-window utilization):
|
||||
|
||||
```text
|
||||
[openrouter/free: 1420 prompt (1000 cached, 420 uncached) + 85 completion | context: 1420/262144 (0.5%)]
|
||||
[kilo-auto/free: 1420 prompt (1000 cached, 420 uncached) + 85 completion | context: 1420/262144 (0.5%)]
|
||||
```
|
||||
|
||||
Sessions are saved under `~/.bantam/sessions/` and can be managed with
|
||||
@@ -231,7 +231,7 @@ override the built-in defaults.)
|
||||
|
||||
- `endpoint` (base OpenAI-compatible API URL, default
|
||||
`https://api.kilo.ai/api/openrouter`; falls back to `BANTAM_ENDPOINT`).
|
||||
- `model` (model name, default `openrouter/free`; falls back to
|
||||
- `model` (model name, default `kilo-auto/free`; falls back to
|
||||
`BANTAM_MODEL`).
|
||||
- `temperature` (model temperature, default `0.7`; falls back to
|
||||
`BANTAM_TEMP` or `BANTAM_TEMPERATURE`).
|
||||
|
||||
@@ -16,7 +16,7 @@ static void cfg_raw_set(Cfg &c, std::string_view k, std::string_view v) {
|
||||
static void cfg_init(Cfg &c) {
|
||||
c = Cfg{};
|
||||
c.endpoint = "https://api.kilo.ai/api/openrouter";
|
||||
c.model = "openrouter/free";
|
||||
c.model = "kilo-auto/free";
|
||||
c.api_key = "-";
|
||||
c.temperature = 0.7;
|
||||
c.timeout = 300;
|
||||
|
||||
@@ -61,6 +61,50 @@ geminux_profile_params_t geminux_policy_params(geminux_profile_t prof, bool on_a
|
||||
return out;
|
||||
}
|
||||
|
||||
geminux_idle_action_t geminux_policy_eval_idle(
|
||||
const geminux_power_policy_t *p,
|
||||
geminux_idle_stage_t stage,
|
||||
uint64_t elapsed_ms,
|
||||
uint64_t *next_wait_ms) {
|
||||
if (next_wait_ms) *next_wait_ms = 0;
|
||||
if (!p || p->lid_closed || p->hdmi_connected || p->inhibit_count > 0) {
|
||||
return IDLE_ACT_NONE;
|
||||
}
|
||||
|
||||
geminux_profile_params_t par = geminux_policy_params(p->profile, p->on_ac_power);
|
||||
uint64_t dim_ms = (par.dim_timeout_s > 0) ? (uint64_t)par.dim_timeout_s * 1000ULL : 0;
|
||||
uint64_t susp_ms =
|
||||
(!p->on_ac_power && par.suspend_timeout_s > 0) ? (uint64_t)par.suspend_timeout_s * 1000ULL : 0;
|
||||
|
||||
if (susp_ms > 0) {
|
||||
if (elapsed_ms >= susp_ms) {
|
||||
if (next_wait_ms) *next_wait_ms = 0;
|
||||
return IDLE_ACT_SUSPEND;
|
||||
}
|
||||
if (dim_ms > 0 && dim_ms < susp_ms) {
|
||||
if (elapsed_ms >= dim_ms) {
|
||||
if (next_wait_ms) *next_wait_ms = susp_ms - elapsed_ms;
|
||||
return (stage == IDLE_STAGE_ACTIVE) ? IDLE_ACT_DIM : IDLE_ACT_NONE;
|
||||
}
|
||||
if (next_wait_ms) *next_wait_ms = dim_ms - elapsed_ms;
|
||||
return IDLE_ACT_NONE;
|
||||
}
|
||||
if (next_wait_ms) *next_wait_ms = susp_ms - elapsed_ms;
|
||||
return IDLE_ACT_NONE;
|
||||
}
|
||||
|
||||
if (dim_ms > 0) {
|
||||
if (elapsed_ms >= dim_ms) {
|
||||
if (next_wait_ms) *next_wait_ms = 0;
|
||||
return (stage != IDLE_STAGE_LOCKED_BLANK) ? IDLE_ACT_LOCK_BLANK : IDLE_ACT_NONE;
|
||||
}
|
||||
if (next_wait_ms) *next_wait_ms = dim_ms - elapsed_ms;
|
||||
return IDLE_ACT_NONE;
|
||||
}
|
||||
|
||||
return IDLE_ACT_NONE;
|
||||
}
|
||||
|
||||
bool geminux_policy_add_inhibit(geminux_power_policy_t *p, const char *tag) {
|
||||
if (!p || !tag || !tag[0]) return false;
|
||||
for (int i = 0; i < p->inhibit_count; i++) {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define GEMINUX_POWER_POLICY_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define GEMINUX_MAX_INHIBITORS 16
|
||||
|
||||
@@ -11,6 +12,19 @@ typedef enum {
|
||||
POWER_PROFILE_PERFORMANCE = 2,
|
||||
} geminux_profile_t;
|
||||
|
||||
typedef enum {
|
||||
IDLE_STAGE_ACTIVE = 0,
|
||||
IDLE_STAGE_DIMMED = 1,
|
||||
IDLE_STAGE_LOCKED_BLANK = 2,
|
||||
} geminux_idle_stage_t;
|
||||
|
||||
typedef enum {
|
||||
IDLE_ACT_NONE = 0,
|
||||
IDLE_ACT_DIM = 1,
|
||||
IDLE_ACT_LOCK_BLANK = 2,
|
||||
IDLE_ACT_SUSPEND = 3,
|
||||
} geminux_idle_action_t;
|
||||
|
||||
typedef struct {
|
||||
geminux_profile_t profile;
|
||||
bool lid_closed;
|
||||
@@ -35,6 +49,11 @@ void geminux_policy_init(geminux_power_policy_t *p);
|
||||
bool geminux_policy_set_profile_name(geminux_power_policy_t *p, const char *name);
|
||||
const char *geminux_policy_profile_name(geminux_profile_t prof);
|
||||
geminux_profile_params_t geminux_policy_params(geminux_profile_t prof, bool on_ac);
|
||||
geminux_idle_action_t geminux_policy_eval_idle(
|
||||
const geminux_power_policy_t *p,
|
||||
geminux_idle_stage_t stage,
|
||||
uint64_t elapsed_ms,
|
||||
uint64_t *next_wait_ms);
|
||||
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);
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <poll.h>
|
||||
#include <pwd.h>
|
||||
#include <signal.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -21,6 +22,7 @@
|
||||
#include <sys/timerfd.h>
|
||||
#include <sys/un.h>
|
||||
#include <sys/wait.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define SOCK_PATH "/run/geminux/powerd.sock"
|
||||
@@ -36,9 +38,17 @@ typedef struct {
|
||||
int ev_fds[MAX_EVDEVS];
|
||||
int num_ev;
|
||||
int saved_brightness;
|
||||
uint64_t last_input_ms;
|
||||
geminux_idle_stage_t idle_stage;
|
||||
geminux_power_policy_t policy;
|
||||
} powerd_ctx_t;
|
||||
|
||||
static uint64_t monotonic_ms(void) {
|
||||
struct timespec ts;
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) return 0;
|
||||
return (uint64_t)ts.tv_sec * 1000ULL + (uint64_t)(ts.tv_nsec / 1000000L);
|
||||
}
|
||||
|
||||
static int write_sysfs_str(const char *path, const char *val) {
|
||||
int fd = open(path, O_WRONLY | O_CLOEXEC);
|
||||
if (fd < 0) return -1;
|
||||
@@ -105,6 +115,113 @@ 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 save_active_brightness(powerd_ctx_t *ctx, const char *bri_path) {
|
||||
if (ctx->idle_stage != IDLE_STAGE_ACTIVE) return;
|
||||
char cur[32] = {0};
|
||||
if (read_sysfs_line(bri_path, cur, sizeof(cur)) == 0 && atoi(cur) > 0) {
|
||||
ctx->saved_brightness = atoi(cur);
|
||||
}
|
||||
}
|
||||
|
||||
static void toggle_backlight(powerd_ctx_t *ctx, bool on) {
|
||||
DIR *d = opendir("/sys/class/backlight");
|
||||
if (!d) return;
|
||||
struct dirent *de;
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
if (de->d_name[0] == '.') continue;
|
||||
char p[320];
|
||||
snprintf(p, sizeof(p), "/sys/class/backlight/%s/brightness", de->d_name);
|
||||
if (!on) {
|
||||
save_active_brightness(ctx, p);
|
||||
write_sysfs_str(p, "0");
|
||||
} else {
|
||||
char restore[32];
|
||||
int val = ctx->saved_brightness > 0 ? ctx->saved_brightness : 128;
|
||||
snprintf(restore, sizeof(restore), "%d", val);
|
||||
write_sysfs_str(p, restore);
|
||||
}
|
||||
break;
|
||||
}
|
||||
closedir(d);
|
||||
}
|
||||
|
||||
static void dim_backlight(powerd_ctx_t *ctx) {
|
||||
DIR *d = opendir("/sys/class/backlight");
|
||||
if (!d) return;
|
||||
struct dirent *de;
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
if (de->d_name[0] == '.') continue;
|
||||
char p[320], max_buf[32] = {0};
|
||||
snprintf(p, sizeof(p), "/sys/class/backlight/%s/brightness", de->d_name);
|
||||
save_active_brightness(ctx, p);
|
||||
int max_bri = 255;
|
||||
snprintf(p, sizeof(p), "/sys/class/backlight/%s/max_brightness", de->d_name);
|
||||
if (read_sysfs_line(p, max_buf, sizeof(max_buf)) == 0 && atoi(max_buf) > 0) {
|
||||
max_bri = atoi(max_buf);
|
||||
}
|
||||
int cur_base = ctx->saved_brightness > 0 ? ctx->saved_brightness : 128;
|
||||
int target = cur_base / 5;
|
||||
int min_floor = max_bri / 20;
|
||||
if (target < min_floor) target = min_floor;
|
||||
if (target >= cur_base) target = cur_base / 2;
|
||||
if (target < 4) target = 4;
|
||||
if (target > cur_base) target = cur_base;
|
||||
char dim_str[32];
|
||||
snprintf(dim_str, sizeof(dim_str), "%d", target);
|
||||
snprintf(p, sizeof(p), "/sys/class/backlight/%s/brightness", de->d_name);
|
||||
write_sysfs_str(p, dim_str);
|
||||
break;
|
||||
}
|
||||
closedir(d);
|
||||
}
|
||||
|
||||
static void arm_timer_ms(int tmr_fd, uint64_t wait_ms) {
|
||||
struct itimerspec its;
|
||||
memset(&its, 0, sizeof(its));
|
||||
if (wait_ms > 0) {
|
||||
its.it_value.tv_sec = (time_t)(wait_ms / 1000ULL);
|
||||
its.it_value.tv_nsec = (long)((wait_ms % 1000ULL) * 1000000ULL);
|
||||
if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0) {
|
||||
its.it_value.tv_nsec = 1000000L;
|
||||
}
|
||||
}
|
||||
timerfd_settime(tmr_fd, 0, &its, NULL);
|
||||
}
|
||||
|
||||
static void evaluate_idle(powerd_ctx_t *ctx) {
|
||||
uint64_t now = monotonic_ms();
|
||||
uint64_t elapsed = (now >= ctx->last_input_ms) ? (now - ctx->last_input_ms) : 0;
|
||||
uint64_t next_wait_ms = 0;
|
||||
geminux_idle_action_t act = geminux_policy_eval_idle(&ctx->policy, ctx->idle_stage, elapsed, &next_wait_ms);
|
||||
if (act == IDLE_ACT_DIM) {
|
||||
dim_backlight(ctx);
|
||||
ctx->idle_stage = IDLE_STAGE_DIMMED;
|
||||
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;
|
||||
arm_timer_ms(ctx->tmr_fd, 0);
|
||||
} else if (act == IDLE_ACT_SUSPEND) {
|
||||
arm_timer_ms(ctx->tmr_fd, 0);
|
||||
trigger_suspend(ctx);
|
||||
} else {
|
||||
arm_timer_ms(ctx->tmr_fd, next_wait_ms);
|
||||
}
|
||||
}
|
||||
|
||||
static void note_user_activity(powerd_ctx_t *ctx) {
|
||||
ctx->last_input_ms = monotonic_ms();
|
||||
if (ctx->idle_stage != IDLE_STAGE_ACTIVE) {
|
||||
ctx->idle_stage = IDLE_STAGE_ACTIVE;
|
||||
toggle_backlight(ctx, true);
|
||||
evaluate_idle(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
static void apply_cpu_policy(powerd_ctx_t *ctx) {
|
||||
geminux_profile_params_t par = geminux_policy_params(ctx->policy.profile, ctx->policy.on_ac_power);
|
||||
|
||||
@@ -139,37 +256,7 @@ static void apply_cpu_policy(powerd_ctx_t *ctx) {
|
||||
|
||||
ensure_run_dir();
|
||||
write_sysfs_str(PROFILE_FILE, geminux_policy_profile_name(ctx->policy.profile));
|
||||
|
||||
struct itimerspec its;
|
||||
memset(&its, 0, sizeof(its));
|
||||
if (par.suspend_timeout_s > 0 && !ctx->policy.hdmi_connected && ctx->policy.inhibit_count == 0) {
|
||||
its.it_value.tv_sec = par.suspend_timeout_s;
|
||||
}
|
||||
timerfd_settime(ctx->tmr_fd, 0, &its, NULL);
|
||||
}
|
||||
|
||||
static void toggle_backlight(powerd_ctx_t *ctx, bool on) {
|
||||
DIR *d = opendir("/sys/class/backlight");
|
||||
if (!d) return;
|
||||
struct dirent *de;
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
if (de->d_name[0] == '.') continue;
|
||||
char p[320], cur[32] = {0};
|
||||
snprintf(p, sizeof(p), "/sys/class/backlight/%s/brightness", de->d_name);
|
||||
if (!on) {
|
||||
if (read_sysfs_line(p, cur, sizeof(cur)) == 0 && atoi(cur) > 0) {
|
||||
ctx->saved_brightness = atoi(cur);
|
||||
}
|
||||
write_sysfs_str(p, "0");
|
||||
} else {
|
||||
char restore[32];
|
||||
int val = ctx->saved_brightness > 0 ? ctx->saved_brightness : 128;
|
||||
snprintf(restore, sizeof(restore), "%d", val);
|
||||
write_sysfs_str(p, restore);
|
||||
}
|
||||
break;
|
||||
}
|
||||
closedir(d);
|
||||
evaluate_idle(ctx);
|
||||
}
|
||||
|
||||
static bool is_lock_running(void) {
|
||||
@@ -254,11 +341,16 @@ static void trigger_suspend(powerd_ctx_t *ctx) {
|
||||
if (write_sysfs_str("/sys/power/state", "mem") == 0 ||
|
||||
write_sysfs_str("/sys/power/state", "freeze") == 0) {
|
||||
/* Woken back up from suspend */
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -277,7 +369,7 @@ static void scan_input_devices(powerd_ctx_t *ctx) {
|
||||
if (fd < 0) continue;
|
||||
unsigned long evbits = 0;
|
||||
if (ioctl(fd, EVIOCGBIT(0, sizeof(evbits)), &evbits) >= 0 &&
|
||||
((evbits & (1UL << EV_SW)) || (evbits & (1UL << EV_KEY)))) {
|
||||
(evbits & ((1UL << EV_SW) | (1UL << EV_KEY) | (1UL << EV_REL) | (1UL << EV_ABS)))) {
|
||||
if (evbits & (1UL << EV_SW)) {
|
||||
unsigned long swbits = 0, swstate = 0;
|
||||
if (ioctl(fd, EVIOCGBIT(EV_SW, sizeof(swbits)), &swbits) >= 0 &&
|
||||
@@ -401,6 +493,8 @@ int main(void) {
|
||||
}
|
||||
}
|
||||
|
||||
ctx.last_input_ms = monotonic_ms();
|
||||
ctx.idle_stage = IDLE_STAGE_ACTIVE;
|
||||
refresh_ac_and_hdmi(&ctx);
|
||||
scan_input_devices(&ctx);
|
||||
apply_cpu_policy(&ctx);
|
||||
@@ -435,9 +529,7 @@ int main(void) {
|
||||
uint64_t exp = 0;
|
||||
while (read(ctx.tmr_fd, &exp, sizeof(exp)) > 0) {}
|
||||
refresh_ac_and_hdmi(&ctx);
|
||||
if (!ctx.policy.on_ac_power && !ctx.policy.hdmi_connected && ctx.policy.inhibit_count == 0) {
|
||||
trigger_suspend(&ctx);
|
||||
}
|
||||
evaluate_idle(&ctx);
|
||||
} else if (fd == ctx.uev_fd) {
|
||||
char ubuf[2048];
|
||||
ssize_t ur;
|
||||
@@ -448,29 +540,50 @@ int main(void) {
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
apply_cpu_policy(&ctx);
|
||||
} else {
|
||||
struct input_event iev;
|
||||
bool saw_activity = false;
|
||||
bool power_pressed = false;
|
||||
while (read(fd, &iev, sizeof(iev)) == (ssize_t)sizeof(iev)) {
|
||||
if (iev.type == EV_SW && iev.code == SW_LID) {
|
||||
ctx.policy.lid_closed = (iev.value != 0);
|
||||
if (ctx.policy.lid_closed) {
|
||||
lock_x11_session();
|
||||
evaluate_idle(&ctx);
|
||||
if (geminux_should_suspend_on_lid(&ctx.policy)) {
|
||||
trigger_suspend(&ctx);
|
||||
} else {
|
||||
toggle_backlight(&ctx, false);
|
||||
}
|
||||
} else {
|
||||
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 (iev.type == EV_KEY && iev.code == KEY_POWER && iev.value == 1) {
|
||||
trigger_suspend(&ctx);
|
||||
if (ctx.idle_stage == IDLE_STAGE_LOCKED_BLANK) {
|
||||
note_user_activity(&ctx);
|
||||
} else {
|
||||
power_pressed = true;
|
||||
}
|
||||
} else if (iev.type == EV_KEY || iev.type == EV_REL || iev.type == EV_ABS) {
|
||||
saw_activity = true;
|
||||
}
|
||||
}
|
||||
if (power_pressed) {
|
||||
trigger_suspend(&ctx);
|
||||
} else if (saw_activity && !ctx.policy.lid_closed) {
|
||||
note_user_activity(&ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user