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

953 lines
33 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 <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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
enum {
WORK_NONE = 0,
WORK_WIFI_CONN,
WORK_WIFI_SCAN,
WORK_BT_PWR,
WORK_BT_SCAN,
WORK_BT_CONN,
WORK_BT_DISC,
WORK_BT_REM,
};
typedef struct {
char col1[96];
char col2[96];
char col3[48];
} net_row_t;
typedef struct {
gemui_app_t *app;
int tab; /* 0=Wi-Fi, 1=Cellular/4G, 2=Bluetooth, 3=USB/HDMI */
bool wifi_on;
bool cellular_on;
bool bt_on;
bool bt_svc_up;
bool hdmi_on;
net_row_t rows[32];
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];
bool focus_psk;
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 == 0 || st->tab == 2) ? 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 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;
}
out[0] = '\0';
const char *scan[] = {"nmcli", "-t", "-f", "IN-USE,BSSID,SSID,CHAN,SIGNAL,SECURITY", "dev", "wifi", "list", NULL};
if (gemui_run_cmd_timeout(scan, out, sizeof(out), 1200) == 0) {
int ng = geminux_parse_wifi_scan(out, st->wifi_groups, GEMINUX_MAX_WIFI_GROUPS);
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->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[0] && strcmp(g->ssid, prev_ssid) == 0) {
st->sel_row = i;
}
}
}
net_clamp_scroll(st);
snprintf(st->status_text, sizeof(st->status_text), "Wi-Fi %s (powersave=3)",
st->wifi_on ? "Enabled" : "Disabled");
}
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 refresh_tab(net_state_t *st) {
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 == 2 && 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;
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) {
st->scroll_off = 0;
gemui_unix_cmd("/run/geminux/ccci.sock", "status", out, sizeof(out));
st->cellular_on = strstr(out, "READY") != NULL;
snprintf(st->status_text, sizeof(st->status_text), "CCCI Modem: %s",
out[0] ? out : "Degraded / No MD1 firmware loaded");
const char *mm[] = {"mmcli", "-m", "0", NULL};
if (gemui_run_cmd_timeout(mm, out, sizeof(out), 900) == 0) {
char *save = NULL;
for (char *ln = strtok_r(out, "\n", &save); ln && st->num_rows < 20; ln = strtok_r(NULL, "\n", &save)) {
while (*ln == ' ' || *ln == '|') ln++;
if (!ln[0]) continue;
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++;
}
}
} else if (st->tab == 2) {
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)");
}
}
static void on_worker_timer(gemui_app_t *app, void *ud);
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_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);
}
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 == 2) {
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);
}
typedef int (*net_child_fn)(void *ctx);
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) {
size_t n = strlen(p_ssid);
while (n > 0 && (p_ssid[n - 1] == '\n' || p_ssid[n - 1] == '\r')) p_ssid[--n] = '\0';
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 int child_wifi_connect(void *ctx) {
wifi_job_t *j = ctx;
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 && out[0]) {
fputs(out, stdout);
}
return rc;
}
static int child_wifi_rescan(void *ctx) {
(void)ctx;
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;
}
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 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 - 3 * 12) / 4;
static const char *tabs[4] = {"Wi-Fi", "4G / CCCI", "Bluetooth", "USB & HDMI"};
for (int i = 0; i < 4; 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_toggle(app, cr, (gemui_rect_t){pad, cy, ctrl_w, 72},
"MT6797 Modem Active", st->cellular_on, HIT_TOGGLE, 1, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + ctrl_w + 12, cy, ctrl_w, 72},
"Reset CCCI Modem", HIT_ACTION, 1, false, false, false);
} else if (st->tab == 2) {
gemui_draw_toggle(app, cr, (gemui_rect_t){pad, cy, ctrl_w, 72},
"Bluetooth Radio", st->bt_on, HIT_TOGGLE, 2, 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, 2, 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, 3, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + ctrl_w + 12, cy, ctrl_w, 72},
"Mirror Primary Display", HIT_ACTION, 3, true, false, false);
}
int foot_h = (st->tab == 0 || st->tab == 2) ? 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 == 2 && 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 = 260;
int sw = 200;
int iw = w - pad * 2 - cw - sw - 24;
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){w - pad - cw, h - 92, cw, 72},
"Connect Wi-Fi", HIT_CONNECT, 0, true, false, false);
} else if (st->tab == 2) {
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) % 4;
st->focus_psk = 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;
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;
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 == 2) 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 ((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 (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);
}
}
}
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;
refresh_tab(st);
} else if (hit->id == HIT_ROW) {
st->sel_row = hit->val;
if (st->tab == 0) st->focus_psk = true;
} 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_CONNECT) {
connect_selected_wifi(st);
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 (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) {
gemui_unix_cmd("/run/geminux/ccci.sock",
st->cellular_on ? "flight on" : "flight off", NULL, 0);
refresh_tab(st);
} else if (hit->val == 2) {
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 == 3) {
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) {
gemui_unix_cmd("/run/geminux/ccci.sock", "reset", NULL, 0);
gemui_show_toast(app, "Sent CCCI reset request", 1600);
refresh_tab(st);
} else if (hit->val == 2) {
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 == 3) {
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;
}