Files
geminux/src/geminux-apps/geminux-net.c
T

1731 lines
63 KiB
C
Raw Normal View History

#define _POSIX_C_SOURCE 200809L
#include "gemui.h"
#include "geminux-wifi-list.h"
#include "geminux-bt-list.h"
#include "../geminux-daemons/geminux-dns-valid.h"
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#define HIT_TAB 1
#define HIT_TOGGLE 2
#define HIT_ACTION 3
#define HIT_ROW 4
#define HIT_PSK 5
#define HIT_CONNECT 6
#define HIT_SHOW_PSK 7
#define HIT_BT_CONN 8
#define HIT_BT_DISC 9
#define HIT_BT_REM 10
#define HIT_AUDIO 11
#define HIT_WIFI_DISC 12
#define HIT_DNS_CUSTOM 13
#define HIT_DNS_APPLY 14
#define HIT_CELL_INPUT 15
#define HIT_CELL_ADD 16
#define HIT_CELL_USE 17
#define HIT_CELL_DEL 18
#define HIT_CELL_DATA 19
enum {
WORK_NONE = 0,
WORK_WIFI_CONN,
WORK_WIFI_SCAN,
WORK_WIFI_DISC,
WORK_DNS_APPLY,
WORK_DNS_TEST,
WORK_ESIM_LIST,
WORK_ESIM_DL,
WORK_ESIM_USE,
WORK_ESIM_DEL,
WORK_CELL_DATA,
WORK_CELL_CFG,
WORK_BT_PWR,
WORK_BT_SCAN,
WORK_BT_CONN,
WORK_BT_DISC,
WORK_BT_REM,
};
typedef struct {
char col1[128];
char col2[96];
char col3[48];
} net_row_t;
typedef struct {
char idx_tag[8];
bool enabled;
char masked_iccid[32];
char label[128];
} esim_ui_row_t;
typedef struct {
gemui_app_t *app;
int tab; /* 0=Wi-Fi, 1=DNS, 2=4G/eSIM, 3=Bluetooth, 4=USB/HDMI */
bool wifi_on;
bool wifi_connected;
bool cellular_on;
bool cell_data_up;
bool cell_ims_on;
bool airplane_mode;
bool cell_physical_sim;
bool esim_loaded;
bool bt_on;
bool bt_svc_up;
bool hdmi_on;
int dns_mode;
char ims_state[32];
net_row_t rows[32];
esim_ui_row_t esim_profiles[16];
int esim_count;
geminux_wifi_group_t wifi_groups[GEMINUX_MAX_WIFI_GROUPS];
geminux_bt_dev_t bt_devs[GEMINUX_MAX_BT_DEVS];
int num_rows;
int sel_row;
int scroll_off;
char status_text[256];
char psk[128];
char active_dns[128];
char custom_dns[192];
char cell_slot[16];
char cell_apn[64];
char cell_ip[48];
char cell_input[256];
char cell_summary[128];
bool focus_psk;
bool focus_dns;
bool focus_cell;
bool show_psk;
pid_t worker_pid;
int worker_fd;
int worker_kind;
int worker_tick;
uint64_t worker_deadline_ms;
char worker_label[96];
char worker_out[2048];
size_t worker_len;
} net_state_t;
static uint64_t net_now_ms(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000ULL + (uint64_t)(ts.tv_nsec / 1000000L);
}
static int net_visible_rows(const net_state_t *st) {
int h = st->app ? gemui_height(st->app) : 940;
int foot_h = (st->tab <= 3) ? 94 : 0;
int box_h = h - (92 + 82 + 88) - 20 - foot_h;
int vis = (box_h - 8) / 72;
return vis > 0 ? vis : 1;
}
static void net_clamp_scroll(net_state_t *st) {
int vis = net_visible_rows(st);
int max_off = st->num_rows > vis ? st->num_rows - 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_row >= 0 && st->sel_row < st->num_rows) {
if (st->sel_row < st->scroll_off) st->scroll_off = st->sel_row;
else if (st->sel_row >= st->scroll_off + vis) st->scroll_off = st->sel_row - vis + 1;
}
}
static void strip_eol(char *s) {
size_t n = strlen(s);
while (n > 0 && (s[n - 1] == '\n' || s[n - 1] == '\r')) s[--n] = '\0';
}
static void refresh_wifi(net_state_t *st, const char *prev_ssid) {
static char out[16384];
out[0] = '\0';
const char *wargv[] = {"nmcli", "-t", "-f", "WIFI", "general", NULL};
if (gemui_run_cmd_timeout(wargv, out, sizeof(out), 900) == 0) {
st->wifi_on = strstr(out, "enabled") != NULL;
}
/* Use cached BSS table so periodic UI redraws never vanish on slow HW rescans. */
out[0] = '\0';
const char *scan[] = {"nmcli", "-t", "-f", "IN-USE,BSSID,SSID,CHAN,SIGNAL,SECURITY",
"dev", "wifi", "list", "--rescan", "no", NULL};
int ng = 0;
if (gemui_run_cmd_timeout(scan, out, sizeof(out), 2000) == 0) {
ng = geminux_parse_wifi_scan(out, st->wifi_groups, GEMINUX_MAX_WIFI_GROUPS);
}
char iw_out[1024] = {0};
char iw_bssid[24] = {0};
char iw_ssid[96] = {0};
int iw_chan = 0, iw_sig = 0;
const char *iw_cmd[] = {"iw", "dev", "wlan0", "link", NULL};
bool iw_ok = (gemui_run_cmd_timeout(iw_cmd, iw_out, sizeof(iw_out), 700) == 0) &&
geminux_parse_iw_link(iw_out, iw_bssid, sizeof(iw_bssid),
iw_ssid, sizeof(iw_ssid), &iw_chan, &iw_sig);
char dev_out[1024] = {0};
bool nm_conn = false;
char nm_con[96] = {0};
char ip4[48] = {0};
const char *dev_cmd[] = {"nmcli", "-t", "-f", "GENERAL.STATE,GENERAL.CONNECTION,IP4.ADDRESS",
"dev", "show", "wlan0", NULL};
if (gemui_run_cmd_timeout(dev_cmd, dev_out, sizeof(dev_out), 900) == 0) {
char *save = NULL;
for (char *ln = strtok_r(dev_out, "\n", &save); ln; ln = strtok_r(NULL, "\n", &save)) {
strip_eol(ln);
char *sep = strchr(ln, ':');
if (!sep) continue;
*sep = '\0';
const char *val = sep + 1;
if (strcmp(ln, "GENERAL.STATE") == 0) {
if (atoi(val) == 100) nm_conn = true;
} else if (strcmp(ln, "GENERAL.CONNECTION") == 0) {
if (val[0] && strcmp(val, "--") != 0) snprintf(nm_con, sizeof(nm_con), "%s", val);
} else if (strncmp(ln, "IP4.ADDRESS", 11) == 0) {
if (val[0] && !ip4[0]) {
snprintf(ip4, sizeof(ip4), "%s", val);
char *slash = strchr(ip4, '/');
if (slash) *slash = '\0';
}
}
}
}
char active_ssid[96] = {0};
if (iw_ssid[0]) {
snprintf(active_ssid, sizeof(active_ssid), "%s", iw_ssid);
} else if (nm_conn && nm_con[0]) {
char nm_ssid[128] = {0};
const char *gs[] = {"nmcli", "-g", "802-11-wireless.ssid",
"connection", "show", "id", nm_con, NULL};
if (gemui_run_cmd_timeout(gs, nm_ssid, sizeof(nm_ssid), 800) == 0) {
strip_eol(nm_ssid);
if (nm_ssid[0]) snprintf(active_ssid, sizeof(active_ssid), "%s", nm_ssid);
}
if (!active_ssid[0]) snprintf(active_ssid, sizeof(active_ssid), "%s", nm_con);
}
char active_sec[48] = {0};
if ((iw_ok || nm_conn) && active_ssid[0]) {
for (int i = 0; i < ng; i++) {
if (strcmp(st->wifi_groups[i].ssid, active_ssid) == 0 &&
st->wifi_groups[i].security[0] &&
strcmp(st->wifi_groups[i].security, "--") != 0) {
snprintf(active_sec, sizeof(active_sec), "%s", st->wifi_groups[i].security);
break;
}
}
if (!active_sec[0] && nm_con[0]) {
char km[64] = {0};
const char *gk[] = {"nmcli", "-g", "802-11-wireless-security.key-mgmt",
"connection", "show", "id", nm_con, NULL};
if (gemui_run_cmd_timeout(gk, km, sizeof(km), 800) == 0) {
strip_eol(km);
if (strcmp(km, "wpa-psk") == 0) snprintf(active_sec, sizeof(active_sec), "WPA2");
else if (strcmp(km, "sae") == 0) snprintf(active_sec, sizeof(active_sec), "WPA3");
else if (!km[0]) snprintf(active_sec, sizeof(active_sec), "Open");
}
}
ng = geminux_merge_active_wifi(st->wifi_groups, ng, GEMINUX_MAX_WIFI_GROUPS,
active_ssid, iw_bssid, iw_chan, iw_sig, active_sec);
}
st->wifi_connected = ((iw_ok || nm_conn) && active_ssid[0] != '\0');
st->num_rows = ng;
for (int i = 0; i < ng; i++) {
const geminux_wifi_group_t *g = &st->wifi_groups[i];
net_row_t *r = &st->rows[i];
snprintf(r->col1, sizeof(r->col1), "%s%s", g->in_use ? "* " : "", g->ssid);
if (g->in_use) {
if (g->ap_count > 1) {
snprintf(r->col2, sizeof(r->col2), "Connected (%d) %d%%", g->ap_count, g->best_signal);
} else if (g->best_signal > 0) {
snprintf(r->col2, sizeof(r->col2), "Connected %d%%", g->best_signal);
} else {
snprintf(r->col2, sizeof(r->col2), "Connected");
}
} else if (g->ap_count > 1) {
snprintf(r->col2, sizeof(r->col2), "Mesh (%d) %d%%", g->ap_count, g->best_signal);
} else {
snprintf(r->col2, sizeof(r->col2), "Signal %d%%", g->best_signal);
}
snprintf(r->col3, sizeof(r->col3), "%s", g->security);
if (prev_ssid && prev_ssid[0] && strcmp(g->ssid, prev_ssid) == 0) {
st->sel_row = i;
} else if ((!prev_ssid || !prev_ssid[0]) && g->in_use && st->sel_row < 0) {
st->sel_row = i;
}
}
net_clamp_scroll(st);
if (st->wifi_connected) {
if (ip4[0]) {
snprintf(st->status_text, sizeof(st->status_text), "Connected: %s (%s)", active_ssid, ip4);
} else {
snprintf(st->status_text, sizeof(st->status_text), "Connected: %s", active_ssid);
}
} else {
snprintf(st->status_text, sizeof(st->status_text), "Wi-Fi %s (Not connected)",
st->wifi_on ? "Enabled" : "Disabled");
}
}
static void refresh_dns(net_state_t *st) {
int mode = GEMINUX_DNS_DEFAULT;
char saved_custom[192] = {0};
FILE *f = fopen("/etc/geminux/dns.conf", "r");
if (f) {
char line[256];
while (fgets(line, sizeof(line), f)) {
strip_eol(line);
if (strncmp(line, "mode=", 5) == 0) {
int m = geminux_dns_mode_index(line + 5);
if (m >= 0) mode = m;
} else if (strncmp(line, "custom=", 7) == 0) {
snprintf(saved_custom, sizeof(saved_custom), "%s", line + 7);
}
}
fclose(f);
}
st->dns_mode = mode;
if (!st->focus_dns && saved_custom[0]) {
snprintf(st->custom_dns, sizeof(st->custom_dns), "%s", saved_custom);
}
st->active_dns[0] = '\0';
FILE *rf = fopen("/etc/resolv.conf", "r");
if (rf) {
char rbuf[1024];
size_t nr = fread(rbuf, 1, sizeof(rbuf) - 1, rf);
rbuf[nr] = '\0';
fclose(rf);
geminux_parse_resolv_conf(rbuf, st->active_dns, sizeof(st->active_dns));
}
st->num_rows = GEMINUX_DNS_COUNT;
for (int i = 0; i < GEMINUX_DNS_COUNT; i++) {
const geminux_dns_preset_t *p = &GEMINUX_DNS_PRESETS[i];
net_row_t *r = &st->rows[i];
bool act = (i == mode);
if (i == GEMINUX_DNS_DEFAULT) {
snprintf(r->col1, sizeof(r->col1), "%s%s", act ? "* " : "", p->label);
snprintf(r->col2, sizeof(r->col2), "Automatic");
} else if (i == GEMINUX_DNS_CUSTOM) {
const char *shown = st->custom_dns[0] ? st->custom_dns
: (saved_custom[0] ? saved_custom : "Enter below...");
snprintf(r->col1, sizeof(r->col1), "%s%s: %.64s", act ? "* " : "", p->label, shown);
snprintf(r->col2, sizeof(r->col2), "Custom");
} else {
snprintf(r->col1, sizeof(r->col1), "%s%s (%s)", act ? "* " : "", p->label, p->v4_csv);
snprintf(r->col2, sizeof(r->col2), p->v6_csv[0] ? "IPv4 + IPv6" : "IPv4");
}
snprintf(r->col3, sizeof(r->col3), "%s", act ? "Active" : "Select");
}
if (st->focus_dns) st->sel_row = GEMINUX_DNS_CUSTOM;
else st->sel_row = mode;
net_clamp_scroll(st);
snprintf(st->status_text, sizeof(st->status_text), "DNS: %s | resolv.conf: %s",
GEMINUX_DNS_PRESETS[mode].label,
st->active_dns[0] ? st->active_dns : "none");
}
static void refresh_bt(net_state_t *st, const char *prev_mac) {
static char all_out[8192];
static char pair_out[4096];
static char conn_out[4096];
char show_out[1024] = {0};
char rf_out[256] = {0};
const char *rf[] = {"rfkill", "list", "bluetooth", NULL};
gemui_run_cmd_timeout(rf, rf_out, sizeof(rf_out), 500);
bool rf_unblocked = (strstr(rf_out, "Soft blocked: no") != NULL);
const char *sh[] = {"bluetoothctl", "--timeout", "1", "show", NULL};
int sh_rc = gemui_run_cmd_timeout(sh, show_out, sizeof(show_out), 900);
st->bt_svc_up = (sh_rc == 0 && strstr(show_out, "Controller ") != NULL);
if (st->bt_svc_up) {
st->bt_on = (strstr(show_out, "Powered: yes") != NULL) && rf_unblocked;
} else {
st->bt_on = false;
}
st->num_rows = 0;
st->sel_row = -1;
if (st->bt_svc_up) {
all_out[0] = '\0';
pair_out[0] = '\0';
conn_out[0] = '\0';
const char *b_all[] = {"bluetoothctl", "--timeout", "1", "devices", NULL};
const char *b_pair[] = {"bluetoothctl", "--timeout", "1", "devices", "Paired", NULL};
const char *b_conn[] = {"bluetoothctl", "--timeout", "1", "devices", "Connected", NULL};
gemui_run_cmd_timeout(b_all, all_out, sizeof(all_out), 900);
gemui_run_cmd_timeout(b_pair, pair_out, sizeof(pair_out), 800);
gemui_run_cmd_timeout(b_conn, conn_out, sizeof(conn_out), 800);
int nb = geminux_parse_bt_devices(all_out, pair_out, conn_out,
st->bt_devs, GEMINUX_MAX_BT_DEVS);
st->num_rows = nb;
for (int i = 0; i < nb; i++) {
const geminux_bt_dev_t *d = &st->bt_devs[i];
net_row_t *r = &st->rows[i];
snprintf(r->col1, sizeof(r->col1), "%s%s", d->connected ? "* " : "", d->name);
snprintf(r->col2, sizeof(r->col2), "%s",
d->connected ? "Connected" : (d->paired ? "Paired" : "Discovered"));
snprintf(r->col3, sizeof(r->col3), "%s", d->mac);
if (prev_mac[0] && strcmp(d->mac, prev_mac) == 0) {
st->sel_row = i;
}
}
}
net_clamp_scroll(st);
if (!st->bt_svc_up) {
snprintf(st->status_text, sizeof(st->status_text),
"Bluetooth service offline (toggle Radio ON to start)");
} else {
snprintf(st->status_text, sizeof(st->status_text),
"Bluetooth %s (Dual BR/EDR + BLE + HID + Audio)",
st->bt_on ? "Powered ON" : "Powered OFF");
}
}
static void parse_esim_list_output(net_state_t *st, const char *raw) {
st->esim_count = 0;
st->esim_loaded = true;
if (!raw || !raw[0]) return;
char tmp[4096];
snprintf(tmp, sizeof(tmp), "%s", raw);
char *save = NULL;
for (char *ln = strtok_r(tmp, "\r\n", &save); ln; ln = strtok_r(NULL, "\r\n", &save)) {
if (strcmp(ln, "NO_PROFILES") == 0) {
st->esim_count = 0;
return;
}
if (strncmp(ln, "PROFILE\t", 8) != 0) continue;
if (st->esim_count >= 16) break;
char *f1 = ln + 8;
char *t2 = strchr(f1, '\t');
if (!t2) continue;
*t2++ = '\0';
char *t3 = strchr(t2, '\t');
if (!t3) continue;
*t3++ = '\0';
char *t4 = strchr(t3, '\t');
if (!t4) continue;
*t4++ = '\0';
esim_ui_row_t *p = &st->esim_profiles[st->esim_count++];
memset(p, 0, sizeof(*p));
snprintf(p->idx_tag, sizeof(p->idx_tag), "%s", f1);
p->enabled = (strcmp(t2, "enabled") == 0);
snprintf(p->masked_iccid, sizeof(p->masked_iccid), "%s", t3);
snprintf(p->label, sizeof(p->label), "%s", t4[0] ? t4 : "eSIM Profile");
}
}
static void refresh_cellular(net_state_t *st) {
char out[4096] = {0};
st->scroll_off = 0;
gemui_unix_cmd("/run/geminux/ccci.sock", "status", out, sizeof(out));
st->cellular_on = strstr(out, "READY") != NULL;
snprintf(st->cell_slot, sizeof(st->cell_slot), "esim");
snprintf(st->cell_apn, sizeof(st->cell_apn), "internet");
st->cell_ims_on = true;
st->cell_data_up = false;
st->cell_physical_sim = false;
st->cell_ip[0] = '\0';
st->cell_summary[0] = '\0';
char op[64] = "", tech[32] = "", sig[16] = "", mmst[32] = "offline";
out[0] = '\0';
const char *st_cmd[] = {"doas", "/usr/sbin/geminux-cellctl", "status", NULL};
if (gemui_run_cmd_timeout(st_cmd, out, sizeof(out), 1500) == 0 && out[0]) {
char *save = NULL;
for (char *ln = strtok_r(out, "\r\n", &save); ln; ln = strtok_r(NULL, "\r\n", &save)) {
if (strncmp(ln, "slot=", 5) == 0) {
snprintf(st->cell_slot, sizeof(st->cell_slot), "%s", ln + 5);
st->cell_physical_sim = (strcmp(st->cell_slot, "physical") == 0);
} else if (strncmp(ln, "apn=", 4) == 0) {
snprintf(st->cell_apn, sizeof(st->cell_apn), "%s", ln + 4);
} else if (strncmp(ln, "ims=", 4) == 0) {
st->cell_ims_on = (strcmp(ln + 4, "0") != 0);
} else if (strncmp(ln, "ccmni0_up=", 10) == 0) {
st->cell_data_up = (atoi(ln + 10) != 0);
} else if (strncmp(ln, "ccmni0_ip=", 10) == 0) {
if (strcmp(ln + 10, "none") != 0) snprintf(st->cell_ip, sizeof(st->cell_ip), "%s", ln + 10);
} else if (strncmp(ln, "mm_state=", 9) == 0) {
snprintf(mmst, sizeof(mmst), "%s", ln + 9);
} else if (strncmp(ln, "operator=", 9) == 0 && strcmp(ln + 9, "none") != 0) {
snprintf(op, sizeof(op), "%s", ln + 9);
} else if (strncmp(ln, "access_tech=", 12) == 0 && strcmp(ln + 12, "none") != 0) {
snprintf(tech, sizeof(tech), "%s", ln + 12);
} else if (strncmp(ln, "signal=", 7) == 0) {
snprintf(sig, sizeof(sig), "%s", ln + 7);
}
}
}
int r = 0;
snprintf(st->rows[r].col1, sizeof(st->rows[0].col1),
"SIM Slot: %s", st->cell_physical_sim ? "Physical Micro-SIM (Tray)" : "Soldered eSIM (ST33 eUICC)");
snprintf(st->rows[r].col2, sizeof(st->rows[0].col2), "%s",
st->cell_physical_sim ? "Physical SIM" : "Embedded eSIM");
snprintf(st->rows[r].col3, sizeof(st->rows[0].col3), "Switch Slot");
r++;
snprintf(st->rows[r].col1, sizeof(st->rows[0].col1),
"4G Data (ccmni0) — APN: %s", st->cell_apn);
snprintf(st->rows[r].col2, sizeof(st->rows[0].col2), "%s",
(st->cell_data_up && st->cell_ip[0]) ? st->cell_ip : "Disconnected");
snprintf(st->rows[r].col3, sizeof(st->rows[0].col3), "%s",
st->cell_data_up ? "Active" : "Down");
r++;
snprintf(st->rows[r].col1, sizeof(st->rows[0].col1),
"IMS / VoLTE & VoWiFi Calling");
if (strcmp(st->ims_state, "VoLTE") == 0) {
snprintf(st->rows[r].col2, sizeof(st->rows[0].col2), "VoLTE Registered");
} else if (strcmp(st->ims_state, "VoWiFi") == 0) {
snprintf(st->rows[r].col2, sizeof(st->rows[0].col2), "VoWiFi Registered");
} else {
snprintf(st->rows[r].col2, sizeof(st->rows[0].col2), "%s",
st->cell_ims_on ? "Enabled (Standby)" : "Disabled");
}
snprintf(st->rows[r].col3, sizeof(st->rows[0].col3), "%s",
(strcmp(st->ims_state, "VoLTE") == 0 || strcmp(st->ims_state, "VoWiFi") == 0)
? st->ims_state
: (st->cell_ims_on ? "ON" : "OFF"));
r++;
if (!st->esim_loaded) {
snprintf(st->rows[r].col1, sizeof(st->rows[0].col1),
"eSIM Profiles: Press 'Scan eSIM Profiles' to query ST33");
snprintf(st->rows[r].col2, sizeof(st->rows[0].col2), "lpac AT Bridge");
snprintf(st->rows[r].col3, sizeof(st->rows[0].col3), "Ready");
r++;
} else if (st->esim_count == 0) {
snprintf(st->rows[r].col1, sizeof(st->rows[0].col1),
"eSIM Profiles: Empty (paste LPA:1$... below to install)");
st->rows[r].col2[0] = '\0';
st->rows[r].col3[0] = '\0';
r++;
} else {
for (int i = 0; i < st->esim_count && r < 24; i++) {
const esim_ui_row_t *p = &st->esim_profiles[i];
snprintf(st->rows[r].col1, sizeof(st->rows[0].col1),
"%seSIM %s: %.100s", p->enabled ? "* " : "", p->idx_tag, p->label);
snprintf(st->rows[r].col2, sizeof(st->rows[0].col2), "%s",
p->enabled ? "Enabled" : "Disabled");
snprintf(st->rows[r].col3, sizeof(st->rows[0].col3), "%s", p->masked_iccid);
r++;
}
}
st->num_rows = r;
if (st->sel_row < 0 || st->sel_row >= st->num_rows) st->sel_row = 0;
net_clamp_scroll(st);
if (op[0]) {
snprintf(st->status_text, sizeof(st->status_text),
"MD1: %s | %.48s (%.24s %.8s%%) | %.36s",
st->cellular_on ? "READY" : "GATED",
op, tech[0] ? tech : mmst, sig[0] ? sig : "0",
st->cell_data_up && st->cell_ip[0] ? st->cell_ip : st->cell_slot);
} else {
snprintf(st->status_text, sizeof(st->status_text),
"MT6797 MD1: %s | State: %.24s | Slot: %s | Data: %.36s",
st->cellular_on ? "READY" : "GATED", mmst, st->cell_slot,
st->cell_data_up && st->cell_ip[0] ? st->cell_ip : "down");
}
}
static void refresh_tab(net_state_t *st) {
char air[16] = {0};
st->airplane_mode = (gemui_read_file_line("/run/geminux/airplane-mode", air, sizeof(air)) == 0 && air[0] == '1');
st->ims_state[0] = '\0';
gemui_read_file_line("/run/geminux/ims-state", st->ims_state, sizeof(st->ims_state));
char prev_key[96] = {0};
if (st->tab == 0 && st->sel_row >= 0 && st->sel_row < st->num_rows) {
snprintf(prev_key, sizeof(prev_key), "%s", st->wifi_groups[st->sel_row].ssid);
} else if (st->tab == 3 && st->sel_row >= 0 && st->sel_row < st->num_rows) {
snprintf(prev_key, sizeof(prev_key), "%s", st->bt_devs[st->sel_row].mac);
}
st->num_rows = 0;
if (st->tab != 2) st->sel_row = -1;
static char out[16384];
out[0] = '\0';
if (st->tab == 0) {
refresh_wifi(st, prev_key);
} else if (st->tab == 1) {
refresh_dns(st);
} else if (st->tab == 2) {
refresh_cellular(st);
} else if (st->tab == 3) {
refresh_bt(st, prev_key);
} else {
st->scroll_off = 0;
st->hdmi_on = false;
const char *xr[] = {"xrandr", "--query", NULL};
if (gemui_run_cmd_timeout(xr, out, sizeof(out), 900) == 0) {
char *save = NULL;
for (char *ln = strtok_r(out, "\n", &save); ln && st->num_rows < 24; ln = strtok_r(NULL, "\n", &save)) {
if (strstr(ln, "connected")) {
if (strstr(ln, "HDMI") && !strstr(ln, "disconnected")) st->hdmi_on = true;
snprintf(st->rows[st->num_rows].col1, sizeof(st->rows[0].col1), "%s", ln);
st->rows[st->num_rows].col2[0] = '\0';
st->rows[st->num_rows].col3[0] = '\0';
st->num_rows++;
}
}
}
snprintf(st->status_text, sizeof(st->status_text),
"USB NCM Gadget: 172.16.42.1/24 | External HDMI (SiI9024)");
}
if (st->airplane_mode) {
st->wifi_on = false;
st->bt_on = false;
st->cellular_on = false;
snprintf(st->status_text, sizeof(st->status_text),
"[AIRPLANE MODE ON] All radios powered off (Fn+F or toggle radio to restore)");
}
}
static void on_worker_timer(gemui_app_t *app, void *ud);
typedef int (*net_child_fn)(void *ctx);
typedef struct {
char cmd[32];
char arg1[256];
} cell_job_t;
static bool start_worker(net_state_t *st, int kind, const char *label,
int timeout_ms, net_child_fn fn, void *ctx);
static int child_cell_run(void *ctx);
static void cancel_worker(net_state_t *st, const char *toast) {
if (st->worker_pid <= 0) return;
kill(-st->worker_pid, SIGTERM);
kill(st->worker_pid, SIGTERM);
if (st->worker_fd >= 0) {
gemui_del_fd(st->app, st->worker_fd);
close(st->worker_fd);
st->worker_fd = -1;
}
int status = 0;
waitpid(st->worker_pid, &status, 0);
st->worker_pid = 0;
st->worker_kind = WORK_NONE;
gemui_set_timer_ms(st->app, 0, NULL, NULL);
if (toast) gemui_show_toast(st->app, toast, 1600);
refresh_tab(st);
gemui_request_redraw(st->app);
}
static void extract_last_error(const char *raw, char *dst, size_t dst_sz) {
dst[0] = '\0';
if (!raw || !raw[0]) return;
char tmp[1024];
snprintf(tmp, sizeof(tmp), "%s", raw);
char *last = NULL;
char *save = NULL;
for (char *ln = strtok_r(tmp, "\r\n", &save); ln; ln = strtok_r(NULL, "\r\n", &save)) {
while (*ln == ' ' || *ln == '\t') ln++;
if (ln[0]) last = ln;
}
if (!last) return;
if (strncmp(last, "Error: ", 7) == 0) last += 7;
snprintf(dst, dst_sz, "%s", last);
}
static void on_worker_fd(gemui_app_t *app, int fd, void *ud) {
net_state_t *st = ud;
char buf[512];
bool eof = false;
while (1) {
ssize_t n = read(fd, buf, sizeof(buf));
if (n > 0) {
if (st->worker_len + 1 < sizeof(st->worker_out)) {
size_t cp = (size_t)n;
if (cp > sizeof(st->worker_out) - 1 - st->worker_len) {
cp = sizeof(st->worker_out) - 1 - st->worker_len;
}
memcpy(st->worker_out + st->worker_len, buf, cp);
st->worker_len += cp;
st->worker_out[st->worker_len] = '\0';
}
} else if (n == 0) {
eof = true;
break;
} else {
if (errno == EINTR) continue;
if (errno == EAGAIN || errno == EWOULDBLOCK) break;
eof = true;
break;
}
}
if (!eof) return;
gemui_del_fd(app, fd);
close(fd);
st->worker_fd = -1;
int status = 0;
if (st->worker_pid > 0) {
while (waitpid(st->worker_pid, &status, 0) < 0 && errno == EINTR) {}
}
int rc = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
int kind = st->worker_kind;
st->worker_pid = 0;
st->worker_kind = WORK_NONE;
gemui_set_timer_ms(app, 0, NULL, NULL);
char err[192] = {0};
extract_last_error(st->worker_out, err, sizeof(err));
char toast[256];
if (kind == WORK_WIFI_CONN) {
if (rc == 0) {
memset(st->psk, 0, sizeof(st->psk));
st->show_psk = false;
gemui_show_toast(app, "Connected to Wi-Fi", 2200);
} else {
snprintf(toast, sizeof(toast), err[0] ? "Wi-Fi failed: %.180s" : "Wi-Fi connect failed", err);
gemui_show_toast(app, toast, 3500);
}
} else if (kind == WORK_WIFI_SCAN) {
gemui_show_toast(app, rc == 0 ? "Wi-Fi scan updated" : "Wi-Fi rescan failed", 1800);
} else if (kind == WORK_WIFI_DISC) {
gemui_show_toast(app, rc == 0 ? "Disconnected Wi-Fi" : "Wi-Fi disconnect failed", 2000);
} else if (kind == WORK_DNS_APPLY) {
if (rc == 0) {
st->focus_dns = false;
gemui_show_toast(app, "DNS settings applied", 2200);
} else {
snprintf(toast, sizeof(toast), err[0] ? "DNS error: %.180s" : "Failed to apply DNS", err);
gemui_show_toast(app, toast, 3200);
}
} else if (kind == WORK_DNS_TEST) {
if (err[0]) {
gemui_show_toast(app, err, 3600);
} else {
gemui_show_toast(app, rc == 0 ? "DNS lookup succeeded" : "DNS lookup failed", 2600);
}
} else if (kind == WORK_BT_PWR) {
if (rc == 0) gemui_show_toast(app, "Updated Bluetooth power", 1800);
else {
snprintf(toast, sizeof(toast), err[0] ? "Bluetooth power: %.180s" : "Bluetooth power failed", err);
gemui_show_toast(app, toast, 2600);
}
} else if (kind == WORK_BT_SCAN) {
gemui_show_toast(app, "Bluetooth scan finished", 1800);
} else if (kind == WORK_BT_CONN) {
if (rc == 0) gemui_show_toast(app, "Bluetooth device connected", 2200);
else {
snprintf(toast, sizeof(toast), err[0] ? "BT connect failed: %.170s" : "Bluetooth connect failed", err);
gemui_show_toast(app, toast, 3000);
}
} else if (kind == WORK_BT_DISC) {
gemui_show_toast(app, rc == 0 ? "Bluetooth device disconnected" : "Disconnect failed", 2000);
} else if (kind == WORK_BT_REM) {
gemui_show_toast(app, rc == 0 ? "Removed Bluetooth device" : "Remove failed", 2000);
} else if (kind == WORK_ESIM_LIST) {
if (rc == 0) {
parse_esim_list_output(st, st->worker_out);
snprintf(toast, sizeof(toast), "ST33 eUICC: %d profile(s)", st->esim_count);
gemui_show_toast(app, toast, 2200);
} else {
snprintf(toast, sizeof(toast), err[0] ? "eSIM scan: %.175s" : "eSIM scan failed", err);
gemui_show_toast(app, toast, 3200);
}
} else if (kind == WORK_ESIM_DL || kind == WORK_ESIM_USE || kind == WORK_ESIM_DEL) {
if (rc == 0) {
if (kind == WORK_ESIM_DL) memset(st->cell_input, 0, sizeof(st->cell_input));
const char *msg = (kind == WORK_ESIM_DL) ? "eSIM profile installed"
: (kind == WORK_ESIM_DEL) ? "eSIM profile deleted"
: "eSIM profile state updated";
gemui_show_toast(app, msg, 2200);
cell_job_t rjob;
memset(&rjob, 0, sizeof(rjob));
snprintf(rjob.cmd, sizeof(rjob.cmd), "esim-list");
start_worker(st, WORK_ESIM_LIST, "Refreshing eSIM profiles...", 25000, child_cell_run, &rjob);
return;
} else {
snprintf(toast, sizeof(toast), err[0] ? "eSIM error: %.175s" : "eSIM operation failed", err);
gemui_show_toast(app, toast, 3400);
}
} else if (kind == WORK_CELL_DATA) {
if (rc == 0) {
gemui_show_toast(app, "4G LTE data state updated", 2200);
} else {
snprintf(toast, sizeof(toast), err[0] ? "4G error: %.175s" : "4G connection failed", err);
gemui_show_toast(app, toast, 3400);
}
} else if (kind == WORK_CELL_CFG) {
if (rc == 0) {
if (strncmp(st->cell_input, "LPA:", 4) != 0 && strncmp(st->cell_input, "1$", 2) != 0) {
memset(st->cell_input, 0, sizeof(st->cell_input));
}
gemui_show_toast(app, err[0] ? err : "Cellular setting updated", 2200);
} else {
snprintf(toast, sizeof(toast), err[0] ? "Cellular: %.175s" : "Cellular update failed", err);
gemui_show_toast(app, toast, 3200);
}
}
refresh_tab(st);
gemui_request_redraw(app);
}
static void on_worker_timer(gemui_app_t *app, void *ud) {
net_state_t *st = ud;
if (st->worker_pid <= 0) {
gemui_set_timer_ms(app, 0, NULL, NULL);
return;
}
uint64_t now = net_now_ms();
if (st->worker_deadline_ms > 0 && now >= st->worker_deadline_ms) {
kill(-st->worker_pid, SIGKILL);
kill(st->worker_pid, SIGKILL);
return;
}
st->worker_tick++;
if (st->worker_kind == WORK_BT_SCAN && (st->worker_tick % 4) == 0 && st->tab == 3) {
char prev_mac[24] = {0};
if (st->sel_row >= 0 && st->sel_row < st->num_rows) {
snprintf(prev_mac, sizeof(prev_mac), "%s", st->bt_devs[st->sel_row].mac);
}
refresh_bt(st, prev_mac);
}
gemui_request_redraw(app);
}
static bool start_worker(net_state_t *st, int kind, const char *label,
int timeout_ms, net_child_fn fn, void *ctx) {
if (st->worker_pid > 0) {
gemui_show_toast(st->app, "Operation in progress (press Cancel to abort)", 2000);
return false;
}
int pfd[2];
if (pipe(pfd) < 0) return false;
pid_t pid = fork();
if (pid < 0) {
close(pfd[0]);
close(pfd[1]);
return false;
}
if (pid == 0) {
setpgid(0, 0);
int devnull = open("/dev/null", O_RDONLY);
if (devnull >= 0) {
dup2(devnull, STDIN_FILENO);
if (devnull > 2) close(devnull);
} else {
close(STDIN_FILENO);
}
close(pfd[0]);
dup2(pfd[1], STDOUT_FILENO);
dup2(pfd[1], STDERR_FILENO);
if (pfd[1] > 2) close(pfd[1]);
int rc = fn(ctx);
_exit(rc);
}
close(pfd[1]);
int fl = fcntl(pfd[0], F_GETFL, 0);
if (fl >= 0) fcntl(pfd[0], F_SETFL, fl | O_NONBLOCK);
int fdfl = fcntl(pfd[0], F_GETFD, 0);
if (fdfl >= 0) fcntl(pfd[0], F_SETFD, fdfl | FD_CLOEXEC);
st->worker_pid = pid;
st->worker_fd = pfd[0];
st->worker_kind = kind;
st->worker_tick = 0;
st->worker_len = 0;
st->worker_out[0] = '\0';
st->worker_deadline_ms = net_now_ms() + (uint64_t)(timeout_ms > 0 ? timeout_ms : 30000);
snprintf(st->worker_label, sizeof(st->worker_label), "%s", label);
gemui_add_fd(st->app, pfd[0], on_worker_fd, st);
gemui_set_timer_ms(st->app, 500, on_worker_timer, st);
gemui_show_toast(st->app, label, 2500);
gemui_request_redraw(st->app);
return true;
}
typedef struct {
char ssid[96];
char bssid[24];
char psk[128];
int ap_count;
bool is_open;
bool has_psk;
} wifi_job_t;
static void wifi_delete_ssid_profiles(const char *ssid) {
const char *del_id[] = {"nmcli", "connection", "delete", "id", ssid, NULL};
gemui_run_cmd_timeout(del_id, NULL, 0, 5000);
char list[4096] = {0};
const char *show[] = {"nmcli", "-t", "-f", "UUID,TYPE", "connection", "show", NULL};
if (gemui_run_cmd_timeout(show, list, sizeof(list), 2000) != 0) return;
char *save = NULL;
for (char *ln = strtok_r(list, "\n", &save); ln; ln = strtok_r(NULL, "\n", &save)) {
char *sep = strchr(ln, ':');
if (!sep) continue;
*sep = '\0';
const char *uuid = ln;
const char *type = sep + 1;
if (strcmp(type, "802-11-wireless") != 0 || !uuid[0]) continue;
char p_ssid[128] = {0};
const char *get_ssid[] = {"nmcli", "-g", "802-11-wireless.ssid", "connection", "show", "uuid", uuid, NULL};
if (gemui_run_cmd_timeout(get_ssid, p_ssid, sizeof(p_ssid), 1200) == 0) {
strip_eol(p_ssid);
if (strcmp(p_ssid, ssid) == 0) {
const char *del_u[] = {"nmcli", "connection", "delete", "uuid", uuid, NULL};
gemui_run_cmd_timeout(del_u, NULL, 0, 5000);
}
}
}
}
static void ensure_wifi_iface(void) {
char air[16] = {0};
if (gemui_read_file_line("/run/geminux/airplane-mode", air, sizeof(air)) == 0 && air[0] == '1') {
return;
}
if (access("/sys/class/net/wlan0", F_OK) == 0) return;
const char *rst[] = {"doas", "/usr/bin/geminux-power-ctl", "wifi-restore", NULL};
gemui_run_cmd_timeout(rst, NULL, 0, 8000);
for (int i = 0; i < 25 && access("/sys/class/net/wlan0", F_OK) != 0; i++) {
poll(NULL, 0, 100);
}
if (access("/sys/class/net/wlan0", F_OK) == 0) {
const char *on[] = {"nmcli", "radio", "wifi", "on", NULL};
gemui_run_cmd_timeout(on, NULL, 0, 4000);
}
}
static int child_wifi_connect(void *ctx) {
wifi_job_t *j = ctx;
ensure_wifi_iface();
char out[512] = {0};
int cfg_rc = 0;
if (j->is_open || j->has_psk) {
wifi_delete_ssid_profiles(j->ssid);
const char *add[20];
int n = 0;
add[n++] = "nmcli";
add[n++] = "connection";
add[n++] = "add";
add[n++] = "type";
add[n++] = "wifi";
add[n++] = "con-name";
add[n++] = j->ssid;
add[n++] = "ifname";
add[n++] = "wlan0";
add[n++] = "ssid";
add[n++] = j->ssid;
if (!j->is_open) {
add[n++] = "wifi-sec.key-mgmt";
add[n++] = "wpa-psk";
add[n++] = "wifi-sec.pmf";
add[n++] = "disable";
add[n++] = "wifi-sec.psk";
add[n++] = j->psk;
}
add[n] = NULL;
cfg_rc = gemui_run_cmd_timeout(add, out, sizeof(out), 8000);
} else {
const char *mod[] = {"nmcli", "connection", "modify", "id", j->ssid,
"802-11-wireless.bssid", "", "wifi-sec.pmf", "disable", NULL};
cfg_rc = gemui_run_cmd_timeout(mod, out, sizeof(out), 5000);
}
memset(j->psk, 0, sizeof(j->psk));
int rc = cfg_rc;
if (rc == 0) {
if (j->bssid[0]) {
const char *wait_sec = (j->ap_count > 1) ? "14" : "16";
const char *up_ap[] = {"nmcli", "--wait", wait_sec, "connection", "up",
"id", j->ssid, "ifname", "wlan0", "ap", j->bssid, NULL};
out[0] = '\0';
rc = gemui_run_cmd_timeout(up_ap, out, sizeof(out), 20000);
}
if (rc != 0 || !j->bssid[0]) {
const char *up[] = {"nmcli", "--wait", "18", "connection", "up",
"id", j->ssid, "ifname", "wlan0", NULL};
out[0] = '\0';
rc = gemui_run_cmd_timeout(up, out, sizeof(out), 22000);
}
}
if (rc == 0) {
const char *dns_apply[] = {"doas", "/usr/sbin/geminux-dnsctl", "apply", NULL};
gemui_run_cmd_timeout(dns_apply, NULL, 0, 5000);
} else if (out[0]) {
fputs(out, stdout);
}
return rc;
}
static int child_wifi_rescan(void *ctx) {
(void)ctx;
ensure_wifi_iface();
char out[256] = {0};
const char *argv[] = {"nmcli", "dev", "wifi", "rescan", NULL};
int rc = gemui_run_cmd_timeout(argv, out, sizeof(out), 8000);
if (rc != 0 && out[0]) fputs(out, stdout);
return rc;
}
static int child_wifi_disconnect(void *ctx) {
(void)ctx;
char out[256] = {0};
const char *argv[] = {"nmcli", "device", "disconnect", "wlan0", NULL};
int rc = gemui_run_cmd_timeout(argv, out, sizeof(out), 8000);
if (rc != 0 && out[0]) fputs(out, stdout);
return rc;
}
static int child_cell_run(void *ctx) {
const cell_job_t *j = ctx;
char out[4096] = {0};
const char *argv[5];
int n = 0;
argv[n++] = "doas";
argv[n++] = "/usr/sbin/geminux-cellctl";
argv[n++] = j->cmd;
if (j->arg1[0]) argv[n++] = j->arg1;
argv[n] = NULL;
int rc = gemui_run_cmd_timeout(argv, out, sizeof(out), 95000);
if (out[0]) fputs(out, stdout);
return rc;
}
static void paste_clipboard_into(char *dst, size_t dst_sz) {
char buf[512] = {0};
const char *clip_argv[] = {"xclip", "-o", "-selection", "clipboard", NULL};
if (gemui_run_cmd_timeout(clip_argv, buf, sizeof(buf), 400) != 0 || !buf[0]) {
const char *prim_argv[] = {"xclip", "-o", "-selection", "primary", NULL};
if (gemui_run_cmd_timeout(prim_argv, buf, sizeof(buf), 400) != 0 || !buf[0]) return;
}
size_t w = 0;
for (size_t i = 0; buf[i] && w + 1 < dst_sz; i++) {
unsigned char c = (unsigned char)buf[i];
if (c < 0x20 || c == ' ' || c == '\t') continue;
dst[w++] = (char)c;
}
if (w > 0) dst[w] = '\0';
}
static void trigger_cell_add(net_state_t *st) {
if (!st->cell_input[0]) {
gemui_show_toast(st->app, "Enter an LPA:1$... code or APN first", 1800);
st->focus_cell = true;
gemui_request_redraw(st->app);
return;
}
cell_job_t job;
memset(&job, 0, sizeof(job));
if (strncmp(st->cell_input, "LPA:", 4) == 0 || strncmp(st->cell_input, "lpa:", 4) == 0 ||
strncmp(st->cell_input, "1$", 2) == 0) {
snprintf(job.cmd, sizeof(job.cmd), "esim-download");
snprintf(job.arg1, sizeof(job.arg1), "%s", st->cell_input);
start_worker(st, WORK_ESIM_DL, "Downloading eSIM profile...", 95000, child_cell_run, &job);
} else {
snprintf(job.cmd, sizeof(job.cmd), "apn-set");
snprintf(job.arg1, sizeof(job.arg1), "%s", st->cell_input);
start_worker(st, WORK_CELL_CFG, "Saving cellular APN...", 8000, child_cell_run, &job);
}
}
static void trigger_cell_use(net_state_t *st) {
cell_job_t job;
memset(&job, 0, sizeof(job));
if (st->sel_row == 0) {
snprintf(job.cmd, sizeof(job.cmd), "sim-slot");
snprintf(job.arg1, sizeof(job.arg1), "%s", st->cell_physical_sim ? "esim" : "physical");
start_worker(st, WORK_CELL_CFG, "Switching SIM slot...", 12000, child_cell_run, &job);
} else if (st->sel_row == 1) {
snprintf(job.cmd, sizeof(job.cmd), "%s", st->cell_data_up ? "data-down" : "data-up");
start_worker(st, WORK_CELL_DATA,
st->cell_data_up ? "Disconnecting 4G LTE..." : "Connecting 4G LTE (ccmni0)...",
45000, child_cell_run, &job);
} else if (st->sel_row == 2) {
snprintf(job.cmd, sizeof(job.cmd), "ims-set");
snprintf(job.arg1, sizeof(job.arg1), "%s", st->cell_ims_on ? "off" : "on");
start_worker(st, WORK_CELL_CFG, "Updating IMS / VoLTE...", 12000, child_cell_run, &job);
} else if (st->esim_loaded && st->sel_row >= 3 && st->sel_row < 3 + st->esim_count) {
const esim_ui_row_t *p = &st->esim_profiles[st->sel_row - 3];
snprintf(job.cmd, sizeof(job.cmd), "%s", p->enabled ? "esim-disable" : "esim-enable");
snprintf(job.arg1, sizeof(job.arg1), "%s", p->idx_tag);
start_worker(st, WORK_ESIM_USE,
p->enabled ? "Disabling eSIM profile..." : "Enabling eSIM profile...",
30000, child_cell_run, &job);
} else {
gemui_show_toast(st->app, "Select Slot, 4G Data, IMS, or an eSIM profile", 1800);
}
}
static void trigger_cell_del(net_state_t *st) {
if (!st->esim_loaded || st->sel_row < 3 || st->sel_row >= 3 + st->esim_count) {
gemui_show_toast(st->app, "Select an eSIM profile row to delete", 1800);
return;
}
const esim_ui_row_t *p = &st->esim_profiles[st->sel_row - 3];
cell_job_t job;
memset(&job, 0, sizeof(job));
snprintf(job.cmd, sizeof(job.cmd), "esim-delete");
snprintf(job.arg1, sizeof(job.arg1), "%s", p->idx_tag);
start_worker(st, WORK_ESIM_DEL, "Deleting eSIM profile...", 30000, child_cell_run, &job);
}
typedef struct {
int mode;
char custom[192];
} dns_job_t;
static int child_dns_apply(void *ctx) {
dns_job_t *j = ctx;
char out[256] = {0};
const char *mode_str = GEMINUX_DNS_PRESETS[j->mode].mode;
const char *argv_custom[] = {"doas", "/usr/sbin/geminux-dnsctl", "set", mode_str, j->custom, NULL};
const char *argv_preset[] = {"doas", "/usr/sbin/geminux-dnsctl", "set", mode_str, NULL};
int rc = gemui_run_cmd_timeout((j->mode == GEMINUX_DNS_CUSTOM) ? argv_custom : argv_preset,
out, sizeof(out), 8000);
if (rc != 0 && out[0]) fputs(out, stdout);
return rc;
}
static int child_dns_test(void *ctx) {
(void)ctx;
struct timespec t0, t1;
clock_gettime(CLOCK_MONOTONIC, &t0);
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
struct addrinfo *res = NULL;
int rc = getaddrinfo("dl-cdn.alpinelinux.org", NULL, &hints, &res);
clock_gettime(CLOCK_MONOTONIC, &t1);
long ms = (long)((t1.tv_sec - t0.tv_sec) * 1000L + (t1.tv_nsec - t0.tv_nsec) / 1000000L);
if (rc != 0 || !res) {
printf("DNS failed (%ld ms): %s\n", ms, gai_strerror(rc));
return 1;
}
char host[64] = {0};
getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof(host), NULL, 0, NI_NUMERICHOST);
freeaddrinfo(res);
printf("DNS OK: alpinelinux -> %s (%ld ms)\n", host[0] ? host : "resolved", ms);
return 0;
}
typedef struct {
char mac[24];
bool enable;
bool paired;
} bt_job_t;
static int child_bt_power(void *ctx) {
bt_job_t *j = ctx;
char out[512] = {0};
if (j->enable) {
const char *unb[] = {"rfkill", "unblock", "bluetooth", NULL};
gemui_run_cmd_timeout(unb, NULL, 0, 1500);
const char *sh[] = {"bluetoothctl", "--timeout", "1", "show", NULL};
if (gemui_run_cmd_timeout(sh, out, sizeof(out), 1500) != 0 ||
!strstr(out, "Controller ")) {
const char *start[] = {"doas", "/sbin/rc-service", "bluetooth", "start", NULL};
gemui_run_cmd_timeout(start, out, sizeof(out), 6000);
}
const char *pon[] = {"bluetoothctl", "--timeout", "4", "power", "on", NULL};
int rc = gemui_run_cmd_timeout(pon, out, sizeof(out), 5000);
if (rc != 0 && out[0]) fputs(out, stdout);
return rc;
} else {
const char *poff[] = {"bluetoothctl", "--timeout", "3", "power", "off", NULL};
gemui_run_cmd_timeout(poff, NULL, 0, 3500);
const char *blk[] = {"rfkill", "block", "bluetooth", NULL};
return gemui_run_cmd_timeout(blk, NULL, 0, 1500);
}
}
static int child_bt_scan(void *ctx) {
(void)ctx;
const char *unb[] = {"rfkill", "unblock", "bluetooth", NULL};
gemui_run_cmd_timeout(unb, NULL, 0, 1500);
const char *pon[] = {"bluetoothctl", "--timeout", "3", "power", "on", NULL};
gemui_run_cmd_timeout(pon, NULL, 0, 3500);
const char *sc[] = {"bluetoothctl", "--timeout", "12", "scan", "on", NULL};
return gemui_run_cmd_timeout(sc, NULL, 0, 13000);
}
static int child_bt_connect(void *ctx) {
bt_job_t *j = ctx;
char out[512] = {0};
const char *tr[] = {"bluetoothctl", "--timeout", "4", "trust", j->mac, NULL};
gemui_run_cmd_timeout(tr, NULL, 0, 4500);
int rc = 0;
if (!j->paired) {
const char *pr[] = {"bluetoothctl", "--timeout", "18", "pair", j->mac, NULL};
out[0] = '\0';
rc = gemui_run_cmd_timeout(pr, out, sizeof(out), 19000);
if (rc != 0 && !strstr(out, "Already Exists")) {
if (out[0]) fputs(out, stdout);
return rc;
}
}
const char *cn[] = {"bluetoothctl", "--timeout", "15", "connect", j->mac, NULL};
out[0] = '\0';
rc = gemui_run_cmd_timeout(cn, out, sizeof(out), 16000);
if (rc != 0 && out[0]) fputs(out, stdout);
return rc;
}
static int child_bt_disconnect(void *ctx) {
bt_job_t *j = ctx;
char out[256] = {0};
const char *dc[] = {"bluetoothctl", "--timeout", "8", "disconnect", j->mac, NULL};
int rc = gemui_run_cmd_timeout(dc, out, sizeof(out), 9000);
if (rc != 0 && out[0]) fputs(out, stdout);
return rc;
}
static int child_bt_remove(void *ctx) {
bt_job_t *j = ctx;
char out[256] = {0};
const char *rm[] = {"bluetoothctl", "--timeout", "8", "remove", j->mac, NULL};
int rc = gemui_run_cmd_timeout(rm, out, sizeof(out), 9000);
if (rc != 0 && out[0]) fputs(out, stdout);
return rc;
}
static bool wifi_profile_exists(const char *ssid) {
char out[128] = {0};
const char *argv[] = {"nmcli", "-g", "connection.uuid", "connection", "show", "id", ssid, NULL};
return gemui_run_cmd_timeout(argv, out, sizeof(out), 900) == 0 && out[0] != '\0';
}
static void connect_selected_wifi(net_state_t *st) {
if (st->sel_row < 0 || st->sel_row >= st->num_rows) {
gemui_show_toast(st->app, "Select an SSID row first", 1600);
return;
}
const geminux_wifi_group_t *grp = &st->wifi_groups[st->sel_row];
bool is_open = (strcmp(grp->security, "Open") == 0 || grp->security[0] == '\0');
bool has_psk = (st->psk[0] != '\0');
if (!is_open) {
if (!has_psk && !wifi_profile_exists(grp->ssid)) {
gemui_show_toast(st->app, "Enter Wi-Fi passphrase first", 2000);
st->focus_psk = true;
gemui_request_redraw(st->app);
return;
}
if (has_psk && strlen(st->psk) < 8) {
gemui_show_toast(st->app, "Passphrase must be at least 8 characters", 2200);
st->focus_psk = true;
gemui_request_redraw(st->app);
return;
}
}
wifi_job_t job;
memset(&job, 0, sizeof(job));
snprintf(job.ssid, sizeof(job.ssid), "%s", grp->ssid);
snprintf(job.bssid, sizeof(job.bssid), "%s", grp->best_bssid);
snprintf(job.psk, sizeof(job.psk), "%s", st->psk);
job.ap_count = grp->ap_count;
job.is_open = is_open;
job.has_psk = has_psk;
start_worker(st, WORK_WIFI_CONN, "Connecting to Wi-Fi...", 48000, child_wifi_connect, &job);
memset(&job, 0, sizeof(job));
}
static void disconnect_wifi(net_state_t *st) {
if (!st->wifi_connected) {
gemui_show_toast(st->app, "Wi-Fi is not connected", 1600);
return;
}
start_worker(st, WORK_WIFI_DISC, "Disconnecting Wi-Fi...", 10000, child_wifi_disconnect, NULL);
}
static void apply_dns_mode(net_state_t *st, int mode) {
if (mode < 0 || mode >= GEMINUX_DNS_COUNT) {
gemui_show_toast(st->app, "Select a DNS profile first", 1600);
return;
}
dns_job_t job;
memset(&job, 0, sizeof(job));
job.mode = mode;
if (mode == GEMINUX_DNS_CUSTOM) {
char v4[GEMINUX_DNS_MAX_SERVERS][16];
char v6[GEMINUX_DNS_MAX_SERVERS][46];
int n4 = 0, n6 = 0;
char norm[192] = {0};
if (geminux_parse_dns_servers(st->custom_dns, v4, &n4, GEMINUX_DNS_MAX_SERVERS,
v6, &n6, GEMINUX_DNS_MAX_SERVERS,
norm, sizeof(norm)) <= 0) {
gemui_show_toast(st->app, "Enter valid comma/space IPv4 or IPv6 nameservers", 2600);
st->focus_dns = true;
st->sel_row = GEMINUX_DNS_CUSTOM;
gemui_request_redraw(st->app);
return;
}
snprintf(st->custom_dns, sizeof(st->custom_dns), "%s", norm);
snprintf(job.custom, sizeof(job.custom), "%s", norm);
}
start_worker(st, WORK_DNS_APPLY, "Applying DNS configuration...", 10000, child_dns_apply, &job);
}
static void apply_selected_dns(net_state_t *st) {
int mode = st->focus_dns ? GEMINUX_DNS_CUSTOM : st->sel_row;
apply_dns_mode(st, mode);
}
static void start_bt_dev_action(net_state_t *st, int kind) {
if (st->sel_row < 0 || st->sel_row >= st->num_rows) {
gemui_show_toast(st->app, "Select a Bluetooth device first", 1600);
return;
}
bt_job_t job;
memset(&job, 0, sizeof(job));
snprintf(job.mac, sizeof(job.mac), "%s", st->bt_devs[st->sel_row].mac);
job.paired = st->bt_devs[st->sel_row].paired;
if (kind == WORK_BT_CONN) {
start_worker(st, WORK_BT_CONN, "Pairing & connecting Bluetooth...", 38000, child_bt_connect, &job);
} else if (kind == WORK_BT_DISC) {
start_worker(st, WORK_BT_DISC, "Disconnecting Bluetooth device...", 10000, child_bt_disconnect, &job);
} else {
start_worker(st, WORK_BT_REM, "Removing Bluetooth device...", 10000, child_bt_remove, &job);
}
}
static void on_draw(gemui_app_t *app, cairo_t *cr, int w, int h, void *ud) {
net_state_t *st = ud;
const gemui_theme_t *t = gemui_theme(app);
char badge[280];
if (st->worker_pid > 0) {
int sec = (st->worker_tick + 1) / 2;
snprintf(badge, sizeof(badge), "%s (%ds)", st->worker_label, sec);
} else {
snprintf(badge, sizeof(badge), "%s", st->status_text);
}
gemui_draw_header(app, cr, "\xef\x87\xab", "Geminux Network & Radios", badge);
int pad = 20;
int top = 92;
int tab_w = (w - pad * 2 - 4 * 12) / 5;
static const char *tabs[5] = {"Wi-Fi", "DNS", "4G / eSIM", "Bluetooth", "USB & HDMI"};
for (int i = 0; i < 5; i++) {
gemui_draw_button(app, cr, (gemui_rect_t){pad + i * (tab_w + 12), top, tab_w, 66},
tabs[i], HIT_TAB, i, false, st->tab == i, false);
}
int cy = top + 82;
int ctrl_w = (w - pad * 2 - 12) / 2;
if (st->tab == 0) {
gemui_draw_toggle(app, cr, (gemui_rect_t){pad, cy, ctrl_w, 72},
"Wi-Fi Radio", st->wifi_on, HIT_TOGGLE, 0, false);
const char *act_lbl = (st->worker_pid > 0) ? "Cancel Operation" : "Rescan Networks";
gemui_draw_button(app, cr, (gemui_rect_t){pad + ctrl_w + 12, cy, ctrl_w, 72},
act_lbl, HIT_ACTION, 0, st->worker_pid == 0, st->worker_pid > 0, false);
} else if (st->tab == 1) {
gemui_draw_button(app, cr, (gemui_rect_t){pad, cy, ctrl_w, 72},
"Reset to Router DNS", HIT_TOGGLE, 1,
false, st->dns_mode == GEMINUX_DNS_DEFAULT, false);
const char *dns_act = (st->worker_pid > 0) ? "Cancel Operation" : "Test DNS Lookup";
gemui_draw_button(app, cr, (gemui_rect_t){pad + ctrl_w + 12, cy, ctrl_w, 72},
dns_act, HIT_ACTION, 1, st->worker_pid == 0, st->worker_pid > 0, false);
} else if (st->tab == 2) {
gemui_draw_toggle(app, cr, (gemui_rect_t){pad, cy, ctrl_w, 72},
"MT6797 Cellular Radio", st->cellular_on, HIT_TOGGLE, 2, false);
const char *esim_act = (st->worker_pid > 0) ? "Cancel Operation" : "Scan eSIM Profiles";
gemui_draw_button(app, cr, (gemui_rect_t){pad + ctrl_w + 12, cy, ctrl_w, 72},
esim_act, HIT_ACTION, 2, st->worker_pid == 0, st->worker_pid > 0, false);
} else if (st->tab == 3) {
gemui_draw_toggle(app, cr, (gemui_rect_t){pad, cy, ctrl_w, 72},
"Bluetooth Radio", st->bt_on, HIT_TOGGLE, 3, false);
const char *bt_lbl = (st->worker_kind == WORK_BT_SCAN) ? "Stop Scan"
: (st->worker_pid > 0) ? "Cancel Operation" : "Scan 12s";
gemui_draw_button(app, cr, (gemui_rect_t){pad + ctrl_w + 12, cy, ctrl_w, 72},
bt_lbl, HIT_ACTION, 3, st->worker_pid == 0, st->worker_pid > 0, false);
} else {
gemui_draw_toggle(app, cr, (gemui_rect_t){pad, cy, ctrl_w, 72},
"SiI9024 HDMI Extend", st->hdmi_on, HIT_TOGGLE, 4, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + ctrl_w + 12, cy, ctrl_w, 72},
"Mirror Primary Display", HIT_ACTION, 4, true, false, false);
}
int foot_h = (st->tab <= 3) ? 94 : 0;
gemui_rect_t box = {pad, cy + 88, w - pad * 2, h - (cy + 88) - pad - foot_h};
gemui_fill_round(cr, box, 12, t->bg_bar);
gemui_stroke_round(cr, box, 12, 1.5, t->border);
if (st->num_rows == 0) {
const char *empty_msg = (st->tab == 3 && st->bt_svc_up)
? "No Bluetooth devices found — press Scan 12s"
: "No interfaces or scan results available";
gemui_draw_text(cr, t, box, empty_msg, 0, false, GEMUI_ALIGN_CENTER, t->fg_muted);
} else {
int rh = 72;
for (int vis = 0; st->scroll_off + vis < st->num_rows; vis++) {
int i = st->scroll_off + vis;
gemui_rect_t rr = {box.x + 10, box.y + 8 + vis * rh, box.w - 20, rh - 6};
if (rr.y + rr.h > box.y + box.h) break;
bool sel = (i == st->sel_row);
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 + 16, rr.y, rr.w - 460, rr.h},
st->rows[i].col1, -1, true, GEMUI_ALIGN_LEFT,
sel ? t->sel_fg : t->fg_bright);
gemui_draw_text(cr, t, (gemui_rect_t){rr.x + rr.w - 440, rr.y, 210, rr.h},
st->rows[i].col2, -2, false, GEMUI_ALIGN_RIGHT,
sel ? t->sel_fg : t->accent_alt);
gemui_draw_text(cr, t, (gemui_rect_t){rr.x + rr.w - 220, rr.y, 206, rr.h},
st->rows[i].col3, -3, false, GEMUI_ALIGN_RIGHT,
sel ? t->sel_fg : t->fg_muted);
gemui_hit_add(app, rr, HIT_ROW, i);
}
}
if (st->tab == 0) {
int cw = 250;
int dw = 210;
int sw = 180;
int iw = w - pad * 2 - cw - dw - sw - 36;
gemui_draw_input(app, cr, (gemui_rect_t){pad, h - 92, iw, 72},
"Wi-Fi passphrase (if connecting to secured SSID)...",
st->psk, !st->show_psk, st->focus_psk, HIT_PSK, 0);
gemui_draw_button(app, cr, (gemui_rect_t){pad + iw + 12, h - 92, sw, 72},
st->show_psk ? "Hide PW" : "Show PW", HIT_SHOW_PSK, 0,
false, st->show_psk, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + iw + sw + 24, h - 92, dw, 72},
"Disconnect", HIT_WIFI_DISC, 0, false, false, false);
gemui_draw_button(app, cr, (gemui_rect_t){w - pad - cw, h - 92, cw, 72},
"Connect Wi-Fi", HIT_CONNECT, 0, true, false, false);
} else if (st->tab == 1) {
int aw = 290;
int iw = w - pad * 2 - aw - 12;
gemui_draw_input(app, cr, (gemui_rect_t){pad, h - 92, iw, 72},
"Custom nameservers (e.g. 88.198.92.222, 1.1.1.1)...",
st->custom_dns, false, st->focus_dns, HIT_DNS_CUSTOM, 0);
gemui_draw_button(app, cr, (gemui_rect_t){w - pad - aw, h - 92, aw, 72},
"Apply DNS Profile", HIT_DNS_APPLY, 0, true, false, false);
} else if (st->tab == 2) {
int dw = 210;
int rmw = 180;
int usew = 220;
int addw = 210;
int iw = w - pad * 2 - dw - rmw - usew - addw - 48;
gemui_draw_input(app, cr, (gemui_rect_t){pad, h - 92, iw, 72},
"Paste LPA:1$smdp$code (Ctrl+V) or enter APN...",
st->cell_input, false, st->focus_cell, HIT_CELL_INPUT, 0);
gemui_draw_button(app, cr, (gemui_rect_t){pad + iw + 12, h - 92, addw, 72},
"Add eSIM / APN", HIT_CELL_ADD, 0, true, false, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + iw + addw + 24, h - 92, usew, 72},
"Enable / Toggle", HIT_CELL_USE, 0, false, false, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + iw + addw + usew + 36, h - 92, rmw, 72},
"Delete eSIM", HIT_CELL_DEL, 0, false, false, false);
gemui_draw_button(app, cr, (gemui_rect_t){w - pad - dw, h - 92, dw, 72},
st->cell_data_up ? "Disconnect 4G" : "Connect 4G",
HIT_CELL_DATA, 0, !st->cell_data_up, st->cell_data_up, false);
} else if (st->tab == 3) {
int bw = (w - pad * 2 - 3 * 12) / 4;
gemui_draw_button(app, cr, (gemui_rect_t){pad, h - 92, bw, 72},
"Pair & Connect", HIT_BT_CONN, 0, true, false, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + bw + 12, h - 92, bw, 72},
"Disconnect", HIT_BT_DISC, 0, false, false, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + 2 * (bw + 12), h - 92, bw, 72},
"Forget Device", HIT_BT_REM, 0, false, false, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + 3 * (bw + 12), h - 92, bw, 72},
"Sound & Mixer", HIT_AUDIO, 0, false, false, false);
}
}
static void on_key(gemui_app_t *app, KeySym ks, unsigned int state, const char *utf8, void *ud) {
net_state_t *st = ud;
if (ks == XK_Escape) {
gemui_quit(app, 0);
} else if (ks == XK_Tab) {
st->tab = (st->tab + 1) % 5;
st->focus_psk = false;
st->focus_dns = false;
st->focus_cell = false;
refresh_tab(st);
gemui_request_redraw(app);
} else if (ks == XK_Up && st->num_rows > 0) {
st->sel_row = (st->sel_row > 0) ? st->sel_row - 1 : 0;
if (st->tab == 1) st->focus_dns = (st->sel_row == GEMINUX_DNS_CUSTOM);
net_clamp_scroll(st);
gemui_request_redraw(app);
} else if (ks == XK_Down && st->num_rows > 0) {
st->sel_row = (st->sel_row + 1 < st->num_rows) ? st->sel_row + 1 : st->num_rows - 1;
if (st->tab == 1) st->focus_dns = (st->sel_row == GEMINUX_DNS_CUSTOM);
net_clamp_scroll(st);
gemui_request_redraw(app);
} else if (ks == XK_Return || ks == XK_KP_Enter) {
if (st->tab == 0) connect_selected_wifi(st);
else if (st->tab == 1) apply_selected_dns(st);
else if (st->tab == 2) {
if (st->focus_cell && st->cell_input[0]) trigger_cell_add(st);
else trigger_cell_use(st);
} else if (st->tab == 3) start_bt_dev_action(st, WORK_BT_CONN);
} else if ((ks == XK_BackSpace || ks == XK_Delete) && st->tab == 0 && (st->focus_psk || st->sel_row >= 0)) {
st->focus_psk = true;
gemui_utf8_pop_char(st->psk);
gemui_request_redraw(app);
} else if ((ks == XK_BackSpace || ks == XK_Delete) && st->tab == 1 && (st->focus_dns || st->sel_row == GEMINUX_DNS_CUSTOM)) {
st->focus_dns = true;
st->sel_row = GEMINUX_DNS_CUSTOM;
gemui_utf8_pop_char(st->custom_dns);
gemui_request_redraw(app);
} else if ((ks == XK_BackSpace || ks == XK_Delete) && st->tab == 2 && (st->focus_cell || st->cell_input[0])) {
st->focus_cell = true;
gemui_utf8_pop_char(st->cell_input);
gemui_request_redraw(app);
} else if ((state & ControlMask) && (ks == XK_u || ks == XK_U) && st->tab == 0) {
st->focus_psk = true;
memset(st->psk, 0, sizeof(st->psk));
gemui_request_redraw(app);
} else if ((state & ControlMask) && (ks == XK_u || ks == XK_U) && st->tab == 1) {
st->focus_dns = true;
st->sel_row = GEMINUX_DNS_CUSTOM;
memset(st->custom_dns, 0, sizeof(st->custom_dns));
gemui_request_redraw(app);
} else if ((state & ControlMask) && (ks == XK_u || ks == XK_U) && st->tab == 2) {
st->focus_cell = true;
memset(st->cell_input, 0, sizeof(st->cell_input));
gemui_request_redraw(app);
} else if ((state & ControlMask) && (ks == XK_v || ks == XK_V) && st->tab == 2) {
paste_clipboard_into(st->cell_input, sizeof(st->cell_input));
st->focus_cell = true;
gemui_request_redraw(app);
} else if (st->tab == 0 && (st->focus_psk || st->sel_row >= 0) && utf8 && utf8[0]) {
st->focus_psk = true;
size_t cur = strlen(st->psk), add = strlen(utf8);
if (cur + add < sizeof(st->psk)) {
memcpy(st->psk + cur, utf8, add + 1);
gemui_request_redraw(app);
}
} else if (st->tab == 1 && (st->focus_dns || st->sel_row == GEMINUX_DNS_CUSTOM) && utf8 && utf8[0]) {
st->focus_dns = true;
st->sel_row = GEMINUX_DNS_CUSTOM;
size_t cur = strlen(st->custom_dns), add = strlen(utf8);
if (cur + add < sizeof(st->custom_dns)) {
memcpy(st->custom_dns + cur, utf8, add + 1);
gemui_request_redraw(app);
}
} else if (st->tab == 2 && utf8 && utf8[0]) {
st->focus_cell = true;
size_t cur = strlen(st->cell_input), add = strlen(utf8);
if (cur + add < sizeof(st->cell_input)) {
memcpy(st->cell_input + cur, utf8, add + 1);
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;
net_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) {
st->scroll_off++;
net_clamp_scroll(st);
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_TAB) {
st->tab = hit->val;
st->focus_psk = false;
st->focus_dns = false;
st->focus_cell = false;
refresh_tab(st);
} else if (hit->id == HIT_ROW) {
st->sel_row = hit->val;
if (st->tab == 0) st->focus_psk = true;
else if (st->tab == 1) st->focus_dns = (hit->val == GEMINUX_DNS_CUSTOM);
else if (st->tab == 2) st->focus_cell = false;
} else if (hit->id == HIT_PSK) {
st->focus_psk = true;
} else if (hit->id == HIT_SHOW_PSK) {
st->show_psk = !st->show_psk;
st->focus_psk = true;
} else if (hit->id == HIT_WIFI_DISC) {
disconnect_wifi(st);
return;
} else if (hit->id == HIT_CONNECT) {
connect_selected_wifi(st);
return;
} else if (hit->id == HIT_DNS_CUSTOM) {
st->focus_dns = true;
st->sel_row = GEMINUX_DNS_CUSTOM;
} else if (hit->id == HIT_DNS_APPLY) {
apply_selected_dns(st);
return;
} else if (hit->id == HIT_CELL_INPUT) {
st->focus_cell = true;
} else if (hit->id == HIT_CELL_ADD) {
trigger_cell_add(st);
return;
} else if (hit->id == HIT_CELL_USE) {
trigger_cell_use(st);
return;
} else if (hit->id == HIT_CELL_DEL) {
trigger_cell_del(st);
return;
} else if (hit->id == HIT_CELL_DATA) {
cell_job_t job;
memset(&job, 0, sizeof(job));
snprintf(job.cmd, sizeof(job.cmd), "%s", st->cell_data_up ? "data-down" : "data-up");
start_worker(st, WORK_CELL_DATA,
st->cell_data_up ? "Disconnecting 4G LTE..." : "Connecting 4G LTE (ccmni0)...",
45000, child_cell_run, &job);
return;
} else if (hit->id == HIT_BT_CONN) {
start_bt_dev_action(st, WORK_BT_CONN);
return;
} else if (hit->id == HIT_BT_DISC) {
start_bt_dev_action(st, WORK_BT_DISC);
return;
} else if (hit->id == HIT_BT_REM) {
start_bt_dev_action(st, WORK_BT_REM);
return;
} else if (hit->id == HIT_AUDIO) {
const char *acmd[] = {"geminux-audio", NULL};
gemui_spawn_detached(acmd);
return;
} else if (hit->id == HIT_TOGGLE) {
if (st->airplane_mode && (hit->val == 0 || hit->val == 2 || hit->val == 3)) {
const char *aoff[] = {"doas", "/usr/bin/geminux-power-ctl", "airplane", "off", NULL};
gemui_run_cmd_timeout(aoff, NULL, 0, 5000);
gemui_show_toast(app, "Exiting Airplane Mode...", 2000);
refresh_tab(st);
gemui_request_redraw(app);
return;
}
if (hit->val == 0) {
const char *argv[] = {"nmcli", "radio", "wifi", st->wifi_on ? "off" : "on", NULL};
gemui_run_cmd_timeout(argv, NULL, 0, 2000);
refresh_tab(st);
} else if (hit->val == 1) {
apply_dns_mode(st, GEMINUX_DNS_DEFAULT);
return;
} else if (hit->val == 2) {
gemui_unix_cmd("/run/geminux/ccci.sock",
st->cellular_on ? "flight on" : "flight off", NULL, 0);
refresh_tab(st);
} else if (hit->val == 3) {
bt_job_t job;
memset(&job, 0, sizeof(job));
job.enable = !st->bt_on;
start_worker(st, WORK_BT_PWR,
job.enable ? "Enabling Bluetooth..." : "Disabling Bluetooth...",
10000, child_bt_power, &job);
return;
} else if (hit->val == 4) {
const char *argv[] = {"xrandr", "--output", "HDMI-1", st->hdmi_on ? "--off" : "--auto", NULL};
gemui_run_cmd_timeout(argv, NULL, 0, 2000);
st->hdmi_on = !st->hdmi_on;
refresh_tab(st);
}
} else if (hit->id == HIT_ACTION) {
if (hit->val == 0) {
if (st->worker_pid > 0) {
cancel_worker(st, "Cancelled operation");
} else {
start_worker(st, WORK_WIFI_SCAN, "Rescanning Wi-Fi...", 10000, child_wifi_rescan, NULL);
}
return;
} else if (hit->val == 1) {
if (st->worker_pid > 0) {
cancel_worker(st, "Cancelled operation");
} else {
start_worker(st, WORK_DNS_TEST, "Testing DNS lookup...", 8000, child_dns_test, NULL);
}
return;
} else if (hit->val == 2) {
if (st->worker_pid > 0) {
cancel_worker(st, "Cancelled operation");
} else {
cell_job_t job;
memset(&job, 0, sizeof(job));
snprintf(job.cmd, sizeof(job.cmd), "esim-list");
start_worker(st, WORK_ESIM_LIST, "Scanning ST33 eUICC profiles...", 25000,
child_cell_run, &job);
}
return;
} else if (hit->val == 3) {
if (st->worker_pid > 0) {
cancel_worker(st, st->worker_kind == WORK_BT_SCAN ? "Bluetooth scan stopped" : "Cancelled");
} else {
start_worker(st, WORK_BT_SCAN, "Scanning Bluetooth (12s)...", 15000, child_bt_scan, NULL);
}
return;
} else if (hit->val == 4) {
const char *argv[] = {"xrandr", "--output", "HDMI-1", "--same-as", "DSI-1", NULL};
gemui_run_cmd_timeout(argv, NULL, 0, 2000);
refresh_tab(st);
}
}
gemui_request_redraw(app);
}
int main(void) {
net_state_t st;
memset(&st, 0, sizeof(st));
st.sel_row = -1;
st.worker_fd = -1;
refresh_tab(&st);
st.app = gemui_create("geminux-net", "Geminux Network & Radios", 1920, 940, 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);
int rc = gemui_run(st.app);
if (st.worker_pid > 0) {
kill(-st.worker_pid, SIGTERM);
kill(st.worker_pid, SIGTERM);
waitpid(st.worker_pid, NULL, 0);
}
if (st.worker_fd >= 0) close(st.worker_fd);
memset(st.psk, 0, sizeof(st.psk));
gemui_destroy(st.app);
return rc;
}