Fix boot sequence errors, enable instant Chrony online sync, and add timezone UI

- Boot sequence cleanups:
  - Scope ModemManager's musl shared-plugin LD_PRELOAD to the child daemon via
    supervise_daemon_args="--env LD_PRELOAD=..." so OpenRC helpers and plugins
    start cleanly with zero "symbol not found" errors.
  - Link XLibre fbdev_drv.so against libfbdevhw.so and libshadow.so, pin
    libxfont2 in rootfs-packages.txt, populate /usr/share/fonts/*/fonts.dir,
    provide XKeysymDB entries, create ~/.cache before dmenu_run, disable TearFree
    for ShadowFB modesetting, and avoid D-Bus autolaunch collision with
    ModemManager.
  - Enable CONFIG_SYN_COOKIES=y and disable IPMI_HANDLER in
    geminux-mt6797x.config, add /etc/sysctl.d/00-alpine.conf, drop x86
    kernel.nmi_watchdog from 99-geminux-power.conf, and disable module
    load/unload in zram-init for built-in zram.
  - Add MT6797 ALSA UCM2 DL2/DL3/UL2/UL3/UL_MONO_1 backend DAI routes in
    HiFi.conf and apply 0006-mt6797-boot-cleanups.patch (stk3310 ID 0x11,
    mt6797-fm firmware search path, mt6797_mac_wq, and pre-auth TX abort).
- Chrony immediate online time sync:
  - Enable FAST_STARTUP, configure makestep 1.0 -1 with sourcedir
    /etc/chrony/sources.d, and trigger burst/waitsync/makestep/hwclock on
    NetworkManager interface up via 30-geminux-chrony.
- Date, Time & Timezone UI:
  - Add geminux-datetime libgemui X11 utility with zoneinfo browser, live
    search, region filters, and NTP sync trigger.
  - Add doas-authorized geminux-tzctl helper with zoneinfo validation and
    update geminux-statusd to reload /etc/timezone cleanly under musl.
This commit is contained in:
Luxferre committed 2026-11-22 23:34:44 +02:00
1 parent 6aea73cbeb
commit 8843558f3b
65 files changed
+1060 -37

No files matched your search

+4
View File
@@ -17,10 +17,14 @@ APPS = \
geminux-camera \
geminux-control \
geminux-themes \
geminux-datetime \
geminux-lock
all: $(APPS)
geminux-datetime: geminux-datetime.c ../geminux-daemons/geminux-tz-valid.h ../libgemui/gemui.h
$(CC) $(CFLAGS) -o $@ $< $(LIBS)
geminux-camera: geminux-camera.c ../libgemui/gemui.h
$(CC) $(CFLAGS) -o $@ $< $(LIBS) -ljpeg
+14 -8
View File
@@ -10,11 +10,12 @@
#define HIT_VOL 2
#define HIT_PROF 3
#define HIT_LED 4
#define HIT_LOCK 5
#define HIT_SUSPEND 6
#define HIT_REBOOT 7
#define HIT_POWER 8
#define HIT_MIXER 9
#define HIT_LOCK 5
#define HIT_SUSPEND 6
#define HIT_REBOOT 7
#define HIT_POWER 8
#define HIT_MIXER 9
#define HIT_DATETIME 10
typedef struct {
gemui_app_t *app;
@@ -94,7 +95,7 @@ static void on_draw(gemui_app_t *app, cairo_t *cr, int w, int h, void *ud) {
ctrl_state_t *st = ud;
const gemui_theme_t *t = gemui_theme(app);
gemui_draw_header(app, cr, "\xef\x80\x93", "Geminux Control Center", "Power, Audio, Display & LEDs");
gemui_draw_header(app, cr, "\xef\x80\x93", "Geminux Control Center", "Power, Audio, Display, Time & LEDs");
int pad = 24;
int cw = (w - pad * 2);
@@ -105,12 +106,14 @@ static void on_draw(gemui_app_t *app, cairo_t *cr, int w, int h, void *ud) {
snprintf(btxt, sizeof(btxt), "%d%%", (int)(bnorm * 100.0 + 0.5));
gemui_draw_slider(app, cr, st->bri_rect, "Display Backlight", bnorm, btxt, HIT_BRI, 0, false);
st->vol_rect = (gemui_rect_t){pad, 210, cw - 240, 104};
st->vol_rect = (gemui_rect_t){pad, 210, cw - 468, 104};
char vtxt[32];
snprintf(vtxt, sizeof(vtxt), "%d%%", st->vol_pct);
gemui_draw_slider(app, cr, st->vol_rect, "Master Audio Volume", st->vol_pct / 100.0, vtxt, HIT_VOL, 0, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + cw - 228, 210, 228, 104},
gemui_draw_button(app, cr, (gemui_rect_t){pad + cw - 456, 210, 222, 104},
"Audio Mixer", HIT_MIXER, 0, true, false, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + cw - 222, 210, 222, 104},
"Time & Zone", HIT_DATETIME, 0, false, false, false);
gemui_draw_text(cr, t, (gemui_rect_t){pad, 330, cw, 40},
"MT6797X Power Profile (A72 Big Cluster + Schedutil)",
@@ -181,6 +184,9 @@ static void on_ptr(gemui_app_t *app, int type, int x, int y, unsigned int btn, v
} else if (hit->id == HIT_MIXER) {
const char *cmd[] = {"geminux-audio", NULL};
gemui_spawn_detached(cmd);
} else if (hit->id == HIT_DATETIME) {
const char *cmd[] = {"geminux-datetime", NULL};
gemui_spawn_detached(cmd);
} else if (hit->id == HIT_PROF) {
static const char *pnames[3] = {"eco", "balanced", "performance"};
st->profile = hit->val;
+564
View File
@@ -0,0 +1,564 @@
#define _POSIX_C_SOURCE 200809L
#include "gemui.h"
#include "../geminux-daemons/geminux-tz-valid.h"
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#define MAX_ZONES 1024
#define HIT_SEARCH 1
#define HIT_FILTER 2
#define HIT_ZONE 3
#define HIT_APPLY 4
#define HIT_SYNC 5
typedef struct {
char name[96];
} tz_entry_t;
typedef struct {
gemui_app_t *app;
tz_entry_t zones[MAX_ZONES];
int num_zones;
int filt_idx[MAX_ZONES];
int num_filt;
int sel_filt;
int scroll_off;
int region_filter; /* 0=All, 1=Europe, 2=America, 3=Asia, 4=Pacific/Aus, 5=UTC/Etc */
char query[64];
char active_zone[96];
char ntp_status[128];
int ntp_tick;
bool sync_running;
pid_t sync_pid;
int sync_fd;
} dt_state_t;
static int cmp_zone(const void *a, const void *b) {
const tz_entry_t *za = a;
const tz_entry_t *zb = b;
return strcmp(za->name, zb->name);
}
static bool str_icontains(const char *hay, const char *needle) {
if (!needle || !needle[0]) return true;
size_t nlen = strlen(needle);
for (const char *p = hay; *p; p++) {
size_t i = 0;
while (i < nlen && p[i] &&
tolower((unsigned char)p[i]) == tolower((unsigned char)needle[i])) {
i++;
}
if (i == nlen) return true;
}
return false;
}
static void add_zone(dt_state_t *st, const char *name) {
if (st->num_zones >= MAX_ZONES || !geminux_tz_file_valid("/usr/share/zoneinfo", name)) {
return;
}
for (int i = 0; i < st->num_zones; i++) {
if (strcmp(st->zones[i].name, name) == 0) return;
}
snprintf(st->zones[st->num_zones++].name, 96, "%s", name);
}
static void scan_zone_dir(dt_state_t *st, const char *prefix, int depth) {
if (depth > 3 || st->num_zones >= MAX_ZONES) return;
char dirpath[512];
snprintf(dirpath, sizeof(dirpath), "/usr/share/zoneinfo/%s", prefix);
DIR *d = opendir(dirpath);
if (!d) return;
struct dirent *de;
while ((de = readdir(d)) != NULL && st->num_zones < MAX_ZONES) {
if (de->d_name[0] == '.') continue;
char rel[128];
int n = snprintf(rel, sizeof(rel), "%s/%s", prefix, de->d_name);
if (n < 0 || (size_t)n >= sizeof(rel)) continue;
char full[512];
snprintf(full, sizeof(full), "/usr/share/zoneinfo/%s", rel);
struct stat sb;
if (stat(full, &sb) != 0) continue;
if (S_ISDIR(sb.st_mode)) {
scan_zone_dir(st, rel, depth + 1);
} else if (S_ISREG(sb.st_mode)) {
add_zone(st, rel);
}
}
closedir(d);
}
static void read_active_zone(dt_state_t *st) {
char buf[128] = {0};
if (gemui_read_file_line("/etc/timezone", buf, sizeof(buf)) == 0 &&
geminux_tz_name_valid(buf)) {
snprintf(st->active_zone, sizeof(st->active_zone), "%s", buf);
return;
}
char linkbuf[512];
ssize_t n = readlink("/etc/localtime", linkbuf, sizeof(linkbuf) - 1);
if (n > 0) {
linkbuf[n] = '\0';
const char *pfx = "/usr/share/zoneinfo/";
const char *p = strstr(linkbuf, pfx);
if (p) {
p += strlen(pfx);
if (geminux_tz_name_valid(p)) {
snprintf(st->active_zone, sizeof(st->active_zone), "%s", p);
return;
}
}
}
snprintf(st->active_zone, sizeof(st->active_zone), "UTC");
}
static void load_zones(dt_state_t *st) {
static const char *const regions[] = {
"Africa", "America", "Antarctica", "Arctic", "Asia", "Atlantic",
"Australia", "Brazil", "Canada", "Chile", "Etc", "Europe",
"Indian", "Mexico", "Pacific", "US"
};
st->num_zones = 0;
add_zone(st, "UTC");
add_zone(st, "GMT");
for (size_t i = 0; i < sizeof(regions) / sizeof(regions[0]); i++) {
scan_zone_dir(st, regions[i], 1);
}
if (st->num_zones > 1) {
qsort(st->zones, (size_t)st->num_zones, sizeof(st->zones[0]), cmp_zone);
}
read_active_zone(st);
}
static bool match_region(const char *z, int reg) {
if (reg == 0) return true;
if (reg == 1) return strncmp(z, "Europe/", 7) == 0;
if (reg == 2) return strncmp(z, "America/", 8) == 0 ||
strncmp(z, "US/", 3) == 0 ||
strncmp(z, "Canada/", 7) == 0 ||
strncmp(z, "Brazil/", 7) == 0 ||
strncmp(z, "Mexico/", 7) == 0 ||
strncmp(z, "Chile/", 6) == 0;
if (reg == 3) return strncmp(z, "Asia/", 5) == 0 || strncmp(z, "Indian/", 7) == 0;
if (reg == 4) return strncmp(z, "Pacific/", 8) == 0 || strncmp(z, "Australia/", 10) == 0;
return strcmp(z, "UTC") == 0 || strcmp(z, "GMT") == 0 || strncmp(z, "Etc/", 4) == 0;
}
static int dt_visible_rows(const dt_state_t *st) {
int h = st->app ? gemui_height(st->app) : 920;
int list_h = h - 242;
int vis = (list_h - 16) / 62;
return vis > 0 ? vis : 1;
}
static void clamp_scroll(dt_state_t *st) {
int vis = dt_visible_rows(st);
int max_off = st->num_filt > vis ? st->num_filt - vis : 0;
if (st->scroll_off < 0) st->scroll_off = 0;
if (st->scroll_off > max_off) st->scroll_off = max_off;
if (st->sel_filt >= 0 && st->sel_filt < st->num_filt) {
if (st->sel_filt < st->scroll_off) st->scroll_off = st->sel_filt;
else if (st->sel_filt >= st->scroll_off + vis) st->scroll_off = st->sel_filt - vis + 1;
}
}
static void rebuild_filter(dt_state_t *st, const char *prefer_zone) {
char keep[96] = {0};
if (prefer_zone && prefer_zone[0]) {
snprintf(keep, sizeof(keep), "%s", prefer_zone);
} else if (st->sel_filt >= 0 && st->sel_filt < st->num_filt) {
snprintf(keep, sizeof(keep), "%s", st->zones[st->filt_idx[st->sel_filt]].name);
}
st->num_filt = 0;
st->sel_filt = -1;
for (int i = 0; i < st->num_zones; i++) {
const char *z = st->zones[i].name;
if (!match_region(z, st->region_filter)) continue;
if (!str_icontains(z, st->query)) continue;
if (keep[0] && strcmp(z, keep) == 0) st->sel_filt = st->num_filt;
st->filt_idx[st->num_filt++] = i;
}
if (st->sel_filt < 0 && st->num_filt > 0) st->sel_filt = 0;
clamp_scroll(st);
}
static void refresh_ntp(dt_state_t *st) {
char out[512] = {0};
const char *cmd[] = {"chronyc", "-c", "tracking", NULL};
if (gemui_run_cmd_timeout(cmd, out, sizeof(out), 300) != 0 || !out[0]) {
snprintf(st->ntp_status, sizeof(st->ntp_status), "chronyd not running / offline");
return;
}
char *nl = strpbrk(out, "\r\n");
if (nl) *nl = '\0';
char *fields[6] = {0};
int nf = 0;
char *save = NULL;
for (char *tok = strtok_r(out, ",", &save); tok && nf < 6; tok = strtok_r(NULL, ",", &save)) {
fields[nf++] = tok;
}
int stratum = (nf >= 3) ? atoi(fields[2]) : 0;
const char *refip = (nf >= 2) ? fields[1] : "";
if (stratum > 0 && stratum < 16 && refip[0] && strcmp(refip, "0.0.0.0") != 0) {
snprintf(st->ntp_status, sizeof(st->ntp_status), "Synced (Stratum %d · %s)", stratum, refip);
} else {
snprintf(st->ntp_status, sizeof(st->ntp_status), "Unsynced (waiting for network/NTP)");
}
}
static void format_time_for_zone(const char *zone, const char *restore_zone,
time_t now, char *out, size_t out_sz) {
struct tm tm;
if (zone && zone[0]) {
setenv("TZ", zone, 1);
tzset();
localtime_r(&now, &tm);
} else {
gmtime_r(&now, &tm);
}
strftime(out, out_sz, "%Y-%m-%d %H:%M:%S (%Z / UTC%z)", &tm);
if (restore_zone && restore_zone[0]) {
setenv("TZ", restore_zone, 1);
tzset();
}
}
static const char *selected_zone(const dt_state_t *st) {
if (st->sel_filt >= 0 && st->sel_filt < st->num_filt) {
return st->zones[st->filt_idx[st->sel_filt]].name;
}
return st->active_zone[0] ? st->active_zone : "UTC";
}
static void apply_selected(dt_state_t *st) {
const char *zone = selected_zone(st);
const char *cmd[] = {"doas", "/usr/sbin/geminux-tzctl", "set", zone, NULL};
int rc = gemui_run_cmd_timeout(cmd, NULL, 0, 2000);
if (rc == 0) {
snprintf(st->active_zone, sizeof(st->active_zone), "%s", zone);
setenv("TZ", st->active_zone, 1);
tzset();
gemui_show_toast(st->app, "Timezone updated", 2000);
} else {
gemui_show_toast(st->app, "Failed to update timezone", 2500);
}
gemui_request_redraw(st->app);
}
static void on_sync_fd(gemui_app_t *app, int fd, void *ud) {
dt_state_t *st = ud;
char buf[256];
bool eof = false;
while (1) {
ssize_t n = read(fd, buf, sizeof(buf));
if (n == 0) {
eof = true;
break;
}
if (n < 0) {
if (errno == EINTR) continue;
if (errno == EAGAIN || errno == EWOULDBLOCK) break;
eof = true;
break;
}
}
if (!eof) return;
gemui_del_fd(app, fd);
close(fd);
st->sync_fd = -1;
int status = 0;
if (st->sync_pid > 0) {
while (waitpid(st->sync_pid, &status, 0) < 0 && errno == EINTR) {}
}
st->sync_pid = 0;
st->sync_running = false;
int rc = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
refresh_ntp(st);
gemui_show_toast(app, rc == 0 ? "NTP clock synchronized" : "NTP sync failed (offline or unreachable)", 2600);
gemui_request_redraw(app);
}
static void start_ntp_sync(dt_state_t *st) {
if (st->sync_running) {
gemui_show_toast(st->app, "NTP sync already in progress...", 1400);
return;
}
int pfd[2];
if (pipe(pfd) != 0) {
gemui_show_toast(st->app, "Failed to start NTP sync", 2000);
return;
}
pid_t pid = fork();
if (pid < 0) {
close(pfd[0]);
close(pfd[1]);
gemui_show_toast(st->app, "Failed to start NTP sync", 2000);
return;
}
if (pid == 0) {
close(pfd[0]);
int devnull = open("/dev/null", O_RDONLY);
if (devnull >= 0) {
dup2(devnull, STDIN_FILENO);
close(devnull);
}
dup2(pfd[1], STDOUT_FILENO);
dup2(pfd[1], STDERR_FILENO);
close(pfd[1]);
const char *argv[] = {"doas", "/usr/sbin/geminux-tzctl", "sync", NULL};
execvp(argv[0], (char *const *)argv);
_exit(127);
}
close(pfd[1]);
fcntl(pfd[0], F_SETFD, fcntl(pfd[0], F_GETFD) | FD_CLOEXEC);
fcntl(pfd[0], F_SETFL, fcntl(pfd[0], F_GETFL) | O_NONBLOCK);
st->sync_pid = pid;
st->sync_fd = pfd[0];
st->sync_running = true;
gemui_add_fd(st->app, st->sync_fd, on_sync_fd, st);
gemui_show_toast(st->app, "Syncing clock via NTP...", 1600);
gemui_request_redraw(st->app);
}
static void on_tick(gemui_app_t *app, void *ud) {
dt_state_t *st = ud;
st->ntp_tick++;
if ((st->ntp_tick % 5) == 0 && !st->sync_running) refresh_ntp(st);
gemui_request_redraw(app);
}
static void on_draw(gemui_app_t *app, cairo_t *cr, int w, int h, void *ud) {
dt_state_t *st = ud;
const gemui_theme_t *t = gemui_theme(app);
gemui_draw_header(app, cr, "\xf3\xb0\xa5\x94", "Date, Time & Timezone", st->active_zone);
int pad = 20;
int top = 88;
int left_w = (w - pad * 3) / 2;
int right_x = pad * 2 + left_w;
int right_w = w - right_x - pad;
gemui_draw_input(app, cr, (gemui_rect_t){pad, top, left_w, 64},
"Search zone (e.g. Berlin, New_York, Tokyo)...", st->query,
false, true, HIT_SEARCH, 0);
static const char *reg_labels[6] = {"All", "Europe", "America", "Asia", "Pacific", "UTC"};
int fw = (left_w - 5 * 8) / 6;
for (int i = 0; i < 6; i++) {
gemui_draw_button(app, cr, (gemui_rect_t){pad + i * (fw + 8), top + 74, fw, 52},
reg_labels[i], HIT_FILTER, i, false, st->region_filter == i, false);
}
gemui_rect_t list = {pad, top + 136, left_w, h - (top + 136) - pad};
gemui_fill_round(cr, list, 12, t->bg_bar);
gemui_stroke_round(cr, list, 12, 1.5, t->border);
int rh = 62;
int cap = dt_visible_rows(st);
for (int i = st->scroll_off; i < st->num_filt && (i - st->scroll_off) < cap; i++) {
const char *zname = st->zones[st->filt_idx[i]].name;
bool sel = (i == st->sel_filt);
bool active = (strcmp(zname, st->active_zone) == 0);
gemui_rect_t rr = {list.x + 10, list.y + 8 + (i - st->scroll_off) * rh, list.w - 20, rh - 6};
gemui_fill_round(cr, rr, 10, sel ? t->sel_bg : (i % 2 ? t->bg_alt_odd : t->bg_alt_even));
gemui_draw_text(cr, t, (gemui_rect_t){rr.x + 14, rr.y, rr.w - 130, rr.h},
zname, -1, sel || active, GEMUI_ALIGN_LEFT,
sel ? t->sel_fg : t->fg_bright);
if (active) {
gemui_draw_text(cr, t, (gemui_rect_t){rr.x + rr.w - 110, rr.y, 96, rr.h},
"[Active]", -2, true, GEMUI_ALIGN_RIGHT,
sel ? t->sel_fg : t->success);
}
gemui_hit_add(app, rr, HIT_ZONE, i);
}
if (st->num_filt == 0) {
gemui_draw_text(cr, t, list, "No matching timezones", 0, false, GEMUI_ALIGN_CENTER, t->fg_muted);
}
/* Right pane: live clock, preview & NTP status */
gemui_rect_t card = {right_x, top, right_w, h - top - 196};
gemui_fill_round(cr, card, 14, t->bg_bar);
gemui_stroke_round(cr, card, 14, 1.5, t->border);
time_t now = time(NULL);
char local_str[128], utc_str[128], prev_str[128];
const char *sel_name = selected_zone(st);
format_time_for_zone(st->active_zone, st->active_zone, now, local_str, sizeof(local_str));
format_time_for_zone(NULL, st->active_zone, now, utc_str, sizeof(utc_str));
format_time_for_zone(sel_name, st->active_zone, now, prev_str, sizeof(prev_str));
int cx = card.x + 24;
int cw = card.w - 48;
int cy = card.y + 20;
gemui_draw_text(cr, t, (gemui_rect_t){cx, cy, cw, 34},
"Current System Local Time", -2, true, GEMUI_ALIGN_LEFT, t->fg_muted);
gemui_draw_text(cr, t, (gemui_rect_t){cx, cy + 32, cw, 48},
local_str, 2, true, GEMUI_ALIGN_LEFT, t->fg_bright);
cy += 98;
gemui_draw_text(cr, t, (gemui_rect_t){cx, cy, cw, 34},
"Universal Time (UTC)", -2, true, GEMUI_ALIGN_LEFT, t->fg_muted);
gemui_draw_text(cr, t, (gemui_rect_t){cx, cy + 32, cw, 42},
utc_str, 0, false, GEMUI_ALIGN_LEFT, t->fg_main);
cy += 92;
char prev_hdr[128];
snprintf(prev_hdr, sizeof(prev_hdr), "Selected Zone Preview: %s", sel_name);
gemui_draw_text(cr, t, (gemui_rect_t){cx, cy, cw, 34},
prev_hdr, -2, true, GEMUI_ALIGN_LEFT, t->info);
gemui_draw_text(cr, t, (gemui_rect_t){cx, cy + 32, cw, 46},
prev_str, 1, true, GEMUI_ALIGN_LEFT, t->accent);
cy += 96;
gemui_draw_text(cr, t, (gemui_rect_t){cx, cy, cw, 34},
"Chrony NTP Synchronization", -2, true, GEMUI_ALIGN_LEFT, t->fg_muted);
gemui_draw_text(cr, t, (gemui_rect_t){cx, cy + 32, cw, 42},
st->ntp_status, -1, true, GEMUI_ALIGN_LEFT,
strncmp(st->ntp_status, "Synced", 6) == 0 ? t->success : t->warning);
gemui_draw_button(app, cr, (gemui_rect_t){right_x, h - 180, right_w, 74},
"Apply Selected Timezone", HIT_APPLY, 0, true, false, false);
gemui_draw_button(app, cr, (gemui_rect_t){right_x, h - 94, right_w, 74},
st->sync_running ? "Syncing NTP..." : "Sync Clock Now (NTP)",
HIT_SYNC, 0, false, st->sync_running, false);
}
static void on_key(gemui_app_t *app, KeySym ks, unsigned int state, const char *utf8, void *ud) {
dt_state_t *st = ud;
if (ks == XK_Escape) {
gemui_quit(app, 0);
} else if ((state & ControlMask) && (ks == XK_s || ks == XK_S)) {
start_ntp_sync(st);
} else if ((state & ControlMask) && (ks == XK_u || ks == XK_U)) {
if (st->query[0]) {
st->query[0] = '\0';
rebuild_filter(st, NULL);
gemui_request_redraw(app);
}
} else if (ks == XK_Up && st->sel_filt > 0) {
st->sel_filt--;
clamp_scroll(st);
gemui_request_redraw(app);
} else if (ks == XK_Down && st->sel_filt + 1 < st->num_filt) {
st->sel_filt++;
clamp_scroll(st);
gemui_request_redraw(app);
} else if (ks == XK_Page_Up) {
int vis = dt_visible_rows(st);
st->sel_filt -= vis;
if (st->sel_filt < 0) st->sel_filt = 0;
clamp_scroll(st);
gemui_request_redraw(app);
} else if (ks == XK_Page_Down && st->num_filt > 0) {
int vis = dt_visible_rows(st);
st->sel_filt += vis;
if (st->sel_filt >= st->num_filt) st->sel_filt = st->num_filt - 1;
clamp_scroll(st);
gemui_request_redraw(app);
} else if (ks == XK_Return || ks == XK_KP_Enter) {
apply_selected(st);
} else if (ks == XK_BackSpace) {
if (st->query[0]) {
gemui_utf8_pop_char(st->query);
rebuild_filter(st, NULL);
gemui_request_redraw(app);
}
} else if (ks == XK_Delete) {
if (st->query[0]) {
st->query[0] = '\0';
rebuild_filter(st, NULL);
gemui_request_redraw(app);
}
} else if (!(state & ControlMask) && utf8 && utf8[0]) {
size_t cur = strlen(st->query), add = strlen(utf8);
if (cur + add < sizeof(st->query)) {
memcpy(st->query + cur, utf8, add + 1);
rebuild_filter(st, NULL);
gemui_request_redraw(app);
}
}
}
static void on_ptr(gemui_app_t *app, int type, int x, int y, unsigned int btn, void *ud) {
(void)btn;
dt_state_t *st = ud;
if (type == GEMUI_PTR_SCROLL_UP) {
if (st->scroll_off > 0) {
st->scroll_off--;
gemui_request_redraw(app);
}
return;
}
if (type == GEMUI_PTR_SCROLL_DOWN) {
int vis = dt_visible_rows(st);
if (st->scroll_off + vis < st->num_filt) {
st->scroll_off++;
gemui_request_redraw(app);
}
return;
}
if (type != GEMUI_PTR_DOWN) return;
const gemui_hit_t *hit = gemui_hit_test(app, x, y);
if (!hit) return;
if (hit->id == HIT_FILTER) {
st->region_filter = hit->val;
rebuild_filter(st, NULL);
gemui_request_redraw(app);
} else if (hit->id == HIT_ZONE) {
st->sel_filt = hit->val;
gemui_request_redraw(app);
} else if (hit->id == HIT_APPLY) {
apply_selected(st);
} else if (hit->id == HIT_SYNC) {
start_ntp_sync(st);
}
}
int main(void) {
static dt_state_t st;
memset(&st, 0, sizeof(st));
st.sync_fd = -1;
load_zones(&st);
if (st.active_zone[0]) {
setenv("TZ", st.active_zone, 1);
tzset();
}
rebuild_filter(&st, st.active_zone);
refresh_ntp(&st);
st.app = gemui_create("geminux-datetime", "Geminux Date & Timezone", 1900, 920, false);
if (!st.app) return 1;
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_tick, &st);
int rc = gemui_run(st.app);
if (st.sync_pid > 0) {
kill(st.sync_pid, SIGTERM);
waitpid(st.sync_pid, NULL, 0);
}
if (st.sync_fd >= 0) close(st.sync_fd);
gemui_destroy(st.app);
return rc;
}