#define _POSIX_C_SOURCE 200809L #include "gemui.h" #include #include #include #define HIT_TAB 1 #define HIT_TOGGLE 2 #define HIT_ACTION 3 #define HIT_ROW 4 #define HIT_PSK 5 #define HIT_CONNECT 6 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 hdmi_on; net_row_t rows[32]; int num_rows; int sel_row; char status_text[256]; char psk[128]; bool focus_psk; } net_state_t; static void refresh_tab(net_state_t *st) { st->num_rows = 0; st->sel_row = -1; char out[4096] = {0}; const char *wargv[] = {"nmcli", "-t", "-f", "WIFI", "general", NULL}; if (gemui_run_cmd_capture(wargv, out, sizeof(out)) == 0) { st->wifi_on = strstr(out, "enabled") != NULL; } if (st->tab == 0) { const char *scan[] = {"nmcli", "-t", "-f", "IN-USE,SSID,SIGNAL,SECURITY", "dev", "wifi", "list", NULL}; if (gemui_run_cmd_capture(scan, out, sizeof(out)) == 0) { char *save = NULL; for (char *ln = strtok_r(out, "\n", &save); ln && st->num_rows < 32; ln = strtok_r(NULL, "\n", &save)) { char *c1 = strchr(ln, ':'); char *c2 = c1 ? strchr(c1 + 1, ':') : NULL; char *c3 = c2 ? strchr(c2 + 1, ':') : NULL; if (!c1 || !c2 || !c3) continue; *c1 = *c2 = *c3 = '\0'; net_row_t *r = &st->rows[st->num_rows++]; snprintf(r->col1, sizeof(r->col1), "%s%s", (ln[0] == '*') ? "* " : "", c1 + 1); snprintf(r->col2, sizeof(r->col2), "Signal %s%%", c2 + 1); snprintf(r->col3, sizeof(r->col3), "%s", (c3 + 1)[0] ? (c3 + 1) : "Open"); } } snprintf(st->status_text, sizeof(st->status_text), "Wi-Fi %s (powersave=3)", st->wifi_on ? "Enabled" : "Disabled"); } else if (st->tab == 1) { 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_capture(mm, out, sizeof(out)) == 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) { const char *bt[] = {"bluetoothctl", "devices", NULL}; if (gemui_run_cmd_capture(bt, out, sizeof(out)) == 0) { char *save = NULL; for (char *ln = strtok_r(out, "\n", &save); ln && st->num_rows < 32; ln = strtok_r(NULL, "\n", &save)) { 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++; } } const char *rf[] = {"rfkill", "list", "bluetooth", NULL}; gemui_run_cmd_capture(rf, out, sizeof(out)); st->bt_on = strstr(out, "Soft blocked: no") != NULL; snprintf(st->status_text, sizeof(st->status_text), "Bluetooth %s (AutoEnable=false at boot)", st->bt_on ? "Powered ON" : "Powered OFF"); } else { const char *xr[] = {"xrandr", "--query", NULL}; if (gemui_run_cmd_capture(xr, out, sizeof(out)) == 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->num_rows++; } } } snprintf(st->status_text, sizeof(st->status_text), "USB NCM Gadget: 172.16.42.1/24 | External HDMI (SiI9024)"); } } 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); gemui_draw_header(app, cr, "\xef\x87\xab", "Geminux Network & Radios", st->status_text); 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); gemui_draw_button(app, cr, (gemui_rect_t){pad + ctrl_w + 12, cy, ctrl_w, 72}, "Rescan Networks", HIT_ACTION, 0, true, false, 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); gemui_draw_button(app, cr, (gemui_rect_t){pad + ctrl_w + 12, cy, ctrl_w, 72}, "Scan 10s", HIT_ACTION, 2, true, false, 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) ? 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) { gemui_draw_text(cr, t, box, "No interfaces or scan results available", 0, false, GEMUI_ALIGN_CENTER, t->fg_muted); } else { int rh = 72; for (int i = 0; i < st->num_rows; i++) { gemui_rect_t rr = {box.x + 10, box.y + 8 + i * 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 - 360, 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 - 340, rr.y, 190, 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 - 140, rr.y, 126, rr.h}, st->rows[i].col3, -2, 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; gemui_draw_input(app, cr, (gemui_rect_t){pad, h - 92, w - pad * 2 - cw - 12, 72}, "Wi-Fi passphrase (if connecting to secured SSID)...", st->psk, true, st->focus_psk, HIT_PSK, 0); gemui_draw_button(app, cr, (gemui_rect_t){w - pad - cw, h - 92, cw, 72}, "Connect Wi-Fi", HIT_CONNECT, 0, true, false, false); } } 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 char *ssid = st->rows[st->sel_row].col1; if (ssid[0] == '*' && ssid[1] == ' ') ssid += 2; char out[256] = {0}; int rc; if (st->psk[0]) { const char *argv[] = {"nmcli", "dev", "wifi", "connect", ssid, "password", st->psk, NULL}; rc = gemui_run_cmd_capture(argv, out, sizeof(out)); } else { const char *argv[] = {"nmcli", "dev", "wifi", "connect", ssid, NULL}; rc = gemui_run_cmd_capture(argv, out, sizeof(out)); } gemui_show_toast(st->app, rc == 0 ? "Connected to Wi-Fi" : "Wi-Fi connect failed", 2200); refresh_tab(st); gemui_request_redraw(st->app); } static void on_key(gemui_app_t *app, KeySym ks, unsigned int state, const char *utf8, void *ud) { (void)state; net_state_t *st = ud; if (ks == XK_Escape) { gemui_quit(app, 0); } else if (ks == XK_Tab) { st->tab = (st->tab + 1) % 4; refresh_tab(st); gemui_request_redraw(app); } else if (ks == XK_Return && st->tab == 0) { connect_selected_wifi(st); } else if (ks == XK_BackSpace && st->focus_psk) { gemui_utf8_pop_char(st->psk); gemui_request_redraw(app); } else if (st->focus_psk && utf8 && utf8[0]) { 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_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; } else if (hit->id == HIT_PSK) { st->focus_psk = true; } else if (hit->id == HIT_CONNECT) { connect_selected_wifi(st); 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_capture(argv, NULL, 0); } else if (hit->val == 1) { gemui_unix_cmd("/run/geminux/ccci.sock", st->cellular_on ? "flight on" : "flight off", NULL, 0); } else if (hit->val == 2) { const char *argv[] = {"rfkill", st->bt_on ? "block" : "unblock", "bluetooth", NULL}; gemui_run_cmd_capture(argv, NULL, 0); } else if (hit->val == 3) { const char *argv[] = {"xrandr", "--output", "HDMI-1", st->hdmi_on ? "--off" : "--auto", NULL}; gemui_run_cmd_capture(argv, NULL, 0); st->hdmi_on = !st->hdmi_on; } refresh_tab(st); } else if (hit->id == HIT_ACTION) { if (hit->val == 0) { const char *argv[] = {"nmcli", "dev", "wifi", "rescan", NULL}; gemui_run_cmd_capture(argv, NULL, 0); gemui_show_toast(app, "Rescanning Wi-Fi...", 1400); } else if (hit->val == 1) { gemui_unix_cmd("/run/geminux/ccci.sock", "reset", NULL, 0); gemui_show_toast(app, "Sent CCCI reset request", 1600); } else if (hit->val == 2) { const char *argv[] = {"bluetoothctl", "--timeout", "10", "scan", "on", NULL}; gemui_spawn_detached(argv); gemui_show_toast(app, "Bluetooth discovery active (10s)", 1800); } else if (hit->val == 3) { const char *argv[] = {"xrandr", "--output", "HDMI-1", "--same-as", "DSI-1", NULL}; gemui_run_cmd_capture(argv, NULL, 0); } refresh_tab(st); } gemui_request_redraw(app); } int main(void) { net_state_t st; memset(&st, 0, sizeof(st)); st.sel_row = -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); gemui_destroy(st.app); return rc; }