| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018 |
- /* netmenu: the top-bar network menu.
- *
- * Lists the NICs the kernel reports as present (Ethernet, USB Ethernet,
- * RNDIS tethering) and the WiFi networks from a scan. Selecting a wired or
- * USB device makes it the active netdev and runs DHCP; selecting a WiFi
- * network asks for the WPA2 passphrase when needed, then starts the existing
- * `wifi` supplicant with a staged /share/netmenu.wifi request (the SYS_SPAWN
- * argument limit is too small for an SSID plus a long passphrase). The
- * supplicant itself runs DHCP once the data port opens.
- *
- * A small ngui canvas window; the WM places it under the network icon and
- * opens only one instance at a time. */
- #include "ucmd_lib.h"
- #include "ngui.h"
- #include "wifi_cfg.h"
- #define MAX_DEVS 8
- #define MAX_BSS 32
- #define ROW_H 24
- #define LOG_MAX 2048
- #define ID_REFRESH 1
- #define ID_SCAN 2
- #define ID_DISCONNECT 3
- #define ID_PASS_OK 4
- #define ID_PASS_CANCEL 5
- #define ID_CONN_CANCEL 6
- #define ID_PASS 7
- #define ID_DEV_BASE 100
- #define ID_BSS_BASE 200
- #define ACT_REFRESH (1u << 0)
- #define ACT_SCAN (1u << 1)
- #define ACT_DISCONNECT (1u << 2)
- #define ACT_BACK (1u << 3)
- #define ACT_CONNECT (1u << 4)
- enum { VIEW_MAIN, VIEW_PASS, VIEW_CONNECT };
- static struct ngui ui;
- static struct ngui_input pass_in;
- static struct ngui_scroll wifi_scroll;
- static struct sys_netdev devs[MAX_DEVS];
- static int ndev;
- static struct sys_wifi_bss bss[MAX_BSS];
- static int nbss;
- static struct sys_wifi_status wst;
- static struct sys_netinfo net;
- static int net_present;
- static int wifi_checked;
- static int wifi_present;
- static int view;
- static int sel_bss = -1;
- static char sel_ssid[33];
- static int scanning;
- static unsigned int scan_start;
- static int connect_pid = -1;
- static int connect_tty;
- static unsigned int connect_start;
- static char conn_log[LOG_MAX];
- static unsigned int conn_log_len;
- static char status[160];
- static int status_error;
- static unsigned int status_until;
- static unsigned int actions;
- static int action_dev = -1;
- static int action_bss = -1;
- static void draw(void);
- static void paint_frame(void);
- static unsigned int slen(const char *s)
- {
- unsigned int n = 0;
- while (s && s[n])
- n++;
- return n;
- }
- static int seq(const char *a, const char *b)
- {
- while (*a && *b) {
- if (*a != *b)
- return 0;
- a++;
- b++;
- }
- return *a == *b;
- }
- static char *sput(char *p, const char *s)
- {
- while (s && *s)
- *p++ = *s++;
- *p = '\0';
- return p;
- }
- static char *put_dec(char *p, unsigned int v)
- {
- char b[10];
- unsigned int n = 0;
- do {
- b[n++] = (char)('0' + v % 10);
- v /= 10;
- } while (v);
- while (n)
- *p++ = b[--n];
- return p;
- }
- static char *put_signed(char *p, int v)
- {
- if (v < 0) {
- *p++ = '-';
- v = -v;
- }
- return put_dec(p, (unsigned int)v);
- }
- static char *put_ip(char *p, const unsigned char ip[4])
- {
- p = put_dec(p, ip[0]); *p++ = '.';
- p = put_dec(p, ip[1]); *p++ = '.';
- p = put_dec(p, ip[2]); *p++ = '.';
- p = put_dec(p, ip[3]);
- return p;
- }
- static void ssid_name(const struct sys_wifi_bss *b, char out[33])
- {
- unsigned int i, n = b->ssid_len;
- if (n > 32)
- n = 32;
- /* Keep the raw UTF-8 bytes; ngui's UTF-8 renderer handles them and the
- * connection path needs the exact SSID. */
- for (i = 0; i < n; i++)
- out[i] = (char)b->ssid[i];
- out[n] = '\0';
- if (!n) {
- out[0] = '(';
- out[1] = 'h'; out[2] = 'i'; out[3] = 'd';
- out[4] = 'd'; out[5] = 'e'; out[6] = 'n';
- out[7] = ')';
- out[8] = '\0';
- }
- }
- static const char *security_name(unsigned int s)
- {
- return s == SYS_WIFI_SEC_OPEN ? "open" :
- s == SYS_WIFI_SEC_PSK ? "WPA2" : "unsupported";
- }
- static int rssi_bars(int rssi)
- {
- if (rssi >= -55)
- return 4;
- if (rssi >= -65)
- return 3;
- if (rssi >= -75)
- return 2;
- return 1;
- }
- static int bss_is_active(const struct sys_wifi_bss *b)
- {
- unsigned int i;
- if (wst.state < SYS_WIFI_ST_ASSOC)
- return 0;
- for (i = 0; i < 6; i++)
- if (wst.bssid[i] != b->bssid[i])
- return 0;
- return 1;
- }
- static void set_status(const char *msg, int err)
- {
- unsigned int i;
- for (i = 0; msg && msg[i] && i + 1 < sizeof(status); i++)
- status[i] = msg[i];
- status[i] = '\0';
- status_error = err;
- status_until = msg && msg[0] ? u_ticks_ms() + 6000 : 0;
- ngui_invalidate(&ui);
- }
- static int dev_active(const struct sys_netdev *d)
- {
- return net_present && seq(net.name, d->name);
- }
- static void load_status(void)
- {
- int i;
- net_present = (u_net_info(&net) == 1 && net.present);
- if (!wifi_checked || wifi_present) {
- if (u_wifi(SYS_WIFI_STATUS, (int)&wst, 0) == 0) {
- wifi_checked = 1;
- wifi_present = wst.present != 0;
- } else {
- /* Do not repeatedly probe a machine without a card. */
- wifi_checked = 1;
- wifi_present = 0;
- }
- }
- for (i = 0; i < ndev; i++) {
- devs[i].active = dev_active(&devs[i]) ? 1u : 0u;
- if (devs[i].active)
- devs[i].link = net.link;
- }
- }
- static void load_devices(void)
- {
- int n = u_net_devlist(devs, MAX_DEVS);
- ndev = n > 0 ? n : 0;
- load_status();
- }
- static void drain_tty(void)
- {
- char buf[256];
- int n;
- if (connect_tty <= 0)
- return;
- while ((n = u_tty_read(connect_tty, buf, sizeof(buf))) > 0) {
- int i;
- for (i = 0; i < n; i++) {
- if (conn_log_len + 1 >= sizeof(conn_log)) {
- unsigned int half = sizeof(conn_log) / 2;
- unsigned int j;
- for (j = 0; j < half; j++)
- conn_log[j] = conn_log[conn_log_len -
- half + j];
- conn_log_len = half;
- }
- conn_log[conn_log_len++] = buf[i];
- }
- conn_log[conn_log_len] = '\0';
- ngui_invalidate(&ui);
- }
- }
- static void sort_bss(void)
- {
- int i, j;
- for (i = 1; i < nbss; i++)
- for (j = i; j > 0 && bss[j].rssi > bss[j - 1].rssi; j--) {
- struct sys_wifi_bss t = bss[j];
- bss[j] = bss[j - 1];
- bss[j - 1] = t;
- }
- }
- static int same_ssid(const struct sys_wifi_bss *a,
- const struct sys_wifi_bss *b)
- {
- unsigned int i;
- if (a->ssid_len == 0 || a->ssid_len != b->ssid_len)
- return 0;
- for (i = 0; i < a->ssid_len; i++)
- if (a->ssid[i] != b->ssid[i])
- return 0;
- return 1;
- }
- /* Mesh/repeater setups often advertise one SSID from several BSSIDs. The
- * list is RSSI-sorted, so keep the strongest entry of each SSID/security
- * pair (or the active BSSID when one of them is the current connection).
- * Hidden-SSID entries are dropped: the menu cannot join them anyway. */
- static void filter_bss(void)
- {
- int i, j, out = 0;
- for (i = 0; i < nbss; i++) {
- int dup = -1;
- if (bss[i].ssid_len == 0)
- continue;
- for (j = 0; j < out; j++)
- if (bss[i].security == bss[j].security &&
- same_ssid(&bss[i], &bss[j])) {
- dup = j;
- break;
- }
- if (dup < 0) {
- bss[out++] = bss[i];
- } else if (bss_is_active(&bss[i]) &&
- !bss_is_active(&bss[dup])) {
- bss[dup] = bss[i];
- }
- }
- nbss = out;
- }
- static void start_scan(void)
- {
- int r;
- if (scanning || !wifi_present)
- return;
- set_status("Scanning for WiFi networks...", 0);
- paint_frame();
- /* SYS_WIFI_SCAN loads the firmware itself if it is still down. */
- r = u_wifi(SYS_WIFI_SCAN, 0, 0);
- if (r < 0) {
- char m[48], *p;
- u_wifi(SYS_WIFI_STATUS, (int)&wst, 0);
- p = sput(m, "WiFi: ");
- p = sput(p, wst.detail[0] ? wst.detail : "scan failed");
- *p = '\0';
- set_status(m, 1);
- return;
- }
- scanning = 1;
- scan_start = u_ticks_ms();
- nbss = 0;
- wifi_scroll.offset = 0;
- ngui_invalidate(&ui);
- }
- static void poll_scan(void)
- {
- int n;
- if (!scanning)
- return;
- n = u_wifi(SYS_WIFI_RESULTS, (int)bss, MAX_BSS);
- if (n == -2) {
- if (u_ticks_ms() - scan_start > 15000) {
- scanning = 0;
- set_status("WiFi scan timed out", 1);
- }
- return;
- }
- scanning = 0;
- if (n < 0) {
- set_status("WiFi scan failed", 1);
- return;
- }
- nbss = n;
- sort_bss();
- filter_bss();
- if (nbss == 0)
- set_status("No WiFi networks found", 1);
- else
- set_status("WiFi scan complete", 0);
- ngui_invalidate(&ui);
- }
- static int write_wifi_cfg(const struct sys_wifi_bss *b, const char *pass)
- {
- struct wifi_cfg cfg;
- unsigned int i, n = slen(pass);
- int r;
- for (i = 0; i < sizeof(cfg); i++)
- ((unsigned char *)&cfg)[i] = 0;
- cfg.magic = WIFI_CFG_MAGIC;
- cfg.ssid_len = b->ssid_len;
- cfg.pass_len = (unsigned char)(n > 64 ? 64 : n);
- for (i = 0; i < 6; i++)
- cfg.bssid[i] = b->bssid[i];
- for (i = 0; i < b->ssid_len && i < 32; i++)
- cfg.ssid[i] = b->ssid[i];
- for (i = 0; i < cfg.pass_len; i++)
- cfg.pass[i] = pass[i];
- r = u_syscall3(SYS_WRITE_FILE, (int)WIFI_CFG_STAGED,
- (int)&cfg, (int)sizeof(cfg));
- for (i = 0; i < sizeof(cfg); i++)
- ((unsigned char *)&cfg)[i] = 0;
- return r;
- }
- static void start_connect(const char *pass)
- {
- struct sys_wifi_bss *b;
- char msg[80], *p;
- int pid;
- if (sel_bss < 0 || sel_bss >= nbss)
- return;
- if (connect_pid > 0 && u_alive((unsigned int)connect_pid)) {
- set_status("A WiFi connection is already in progress", 1);
- return;
- }
- b = &bss[sel_bss];
- ssid_name(b, sel_ssid);
- if (write_wifi_cfg(b, pass) != 0) {
- set_status("Cannot stage the WiFi request in /share", 1);
- return;
- }
- if (connect_tty <= 0) {
- connect_tty = u_tty_create();
- if (connect_tty < 0)
- connect_tty = 0;
- }
- conn_log_len = 0;
- conn_log[0] = '\0';
- connect_start = u_ticks_ms();
- pid = u_spawn("/ramfs/bin/wifi", "connectfile /share/netmenu.wifi",
- connect_tty, "/");
- if (pid < 0) {
- u_syscall1(SYS_UNLINK, (int)"/share/netmenu.wifi");
- set_status("Cannot start the WiFi helper", 1);
- return;
- }
- connect_pid = pid;
- view = VIEW_CONNECT;
- p = sput(msg, "Connecting to \"");
- p = sput(p, sel_ssid);
- p = sput(p, "\"...");
- *p = '\0';
- set_status(msg, 0);
- }
- static void cancel_connect(void)
- {
- if (connect_pid > 0 && u_alive((unsigned int)connect_pid))
- u_kill((unsigned int)connect_pid);
- connect_pid = -1;
- u_wifi(SYS_WIFI_DISCONNECT, 0, 0);
- view = VIEW_MAIN;
- set_status("WiFi connection cancelled", 0);
- load_status();
- }
- static void poll_connect(void)
- {
- int i;
- if (connect_pid <= 0)
- return;
- if (!u_alive((unsigned int)connect_pid)) {
- connect_pid = -1;
- load_status();
- if (wst.state >= SYS_WIFI_ST_LINK) {
- int have_ip = 0;
- for (i = 0; i < 4; i++)
- if (net.ip[i])
- have_ip = 1;
- view = VIEW_MAIN;
- if (net_present && seq(net.name, "wifi")) {
- if (have_ip)
- set_status("WiFi connected and DHCP is up", 0);
- else
- set_status("WiFi connected, but DHCP got no lease", 1);
- } else {
- set_status("WiFi associated, but the netdev is busy", 1);
- }
- } else {
- /* Keep the log on screen so the failure reason is visible;
- * the Cancel button becomes Back. */
- set_status("WiFi connection failed", 1);
- }
- ngui_invalidate(&ui);
- return;
- }
- if (u_ticks_ms() - connect_start > 45000) {
- u_kill((unsigned int)connect_pid);
- connect_pid = -1;
- u_wifi(SYS_WIFI_DISCONNECT, 0, 0);
- set_status("WiFi connection timed out", 1);
- }
- }
- static void select_device(int i)
- {
- char msg[64], *p;
- if (i < 0 || i >= ndev)
- return;
- p = sput(msg, "Selecting ");
- p = sput(p, devs[i].name);
- *p = '\0';
- set_status(msg, 0);
- paint_frame();
- if (u_net_select(devs[i].name) != 0) {
- set_status("Cannot select that netdev (busy or unavailable)", 1);
- return;
- }
- set_status("Running DHCP...", 0);
- paint_frame();
- if (u_dhcp_run(6000) == 0)
- set_status("Device selected; DHCP lease acquired", 0);
- else
- set_status("Device selected, but DHCP got no lease", 1);
- load_devices();
- ngui_invalidate(&ui);
- }
- static void select_bss(int i)
- {
- if (i < 0 || i >= nbss)
- return;
- if (bss[i].ssid_len == 0) {
- set_status("Hidden network: use `wifi connect` from a terminal", 1);
- return;
- }
- sel_bss = i;
- ssid_name(&bss[i], sel_ssid);
- if (bss[i].security == SYS_WIFI_SEC_OTHER) {
- set_status("Unsupported security (only open and WPA2-PSK)", 1);
- return;
- }
- if (bss[i].security == SYS_WIFI_SEC_PSK) {
- ngui_input_set(&pass_in, "");
- pass_in.secret = 1;
- pass_in.placeholder = "WPA2 passphrase";
- view = VIEW_PASS;
- set_status("", 0);
- ngui_set_focus(&ui, ID_PASS);
- ngui_invalidate(&ui);
- return;
- }
- start_connect("");
- }
- static void handle_actions(void)
- {
- unsigned int a = actions;
- int di = action_dev, bi = action_bss;
- actions = 0;
- action_dev = -1;
- action_bss = -1;
- if (a & ACT_BACK) {
- if (view == VIEW_CONNECT)
- cancel_connect();
- else {
- view = VIEW_MAIN;
- set_status("", 0);
- }
- return;
- }
- if (a & ACT_CONNECT) {
- char pass[65];
- unsigned int i, n;
- ngui_input_value(&pass_in, pass, sizeof(pass));
- n = slen(pass);
- if (n == 0) {
- set_status("Enter the WPA2 passphrase", 1);
- } else if (n < 8 || n > 63) {
- /* A 64-character hex string is a raw PSK. */
- int hex = n == 64;
- if (hex) {
- unsigned int j;
- for (j = 0; j < n; j++) {
- char c = pass[j];
- if (!((c >= '0' && c <= '9') ||
- (c >= 'a' && c <= 'f') ||
- (c >= 'A' && c <= 'F'))) {
- hex = 0;
- break;
- }
- }
- }
- if (hex)
- start_connect(pass);
- else
- set_status("Use 8-63 characters or 64 hex digits", 1);
- } else {
- start_connect(pass);
- }
- for (i = 0; i < sizeof(pass); i++)
- ((volatile char *)pass)[i] = 0;
- return;
- }
- if (a & ACT_REFRESH) {
- set_status("Refreshing network devices...", 0);
- paint_frame();
- load_devices();
- set_status("Device list refreshed", 0);
- ngui_invalidate(&ui);
- } else if (a & ACT_SCAN) {
- start_scan();
- } else if (a & ACT_DISCONNECT) {
- u_wifi(SYS_WIFI_DISCONNECT, 0, 0);
- load_status();
- set_status("WiFi disconnected", 0);
- ngui_invalidate(&ui);
- } else if (di >= 0) {
- select_device(di);
- } else if (bi >= 0) {
- select_bss(bi);
- }
- }
- static void draw_bars(int x, int y, int bars, unsigned short color)
- {
- int i;
- for (i = 0; i < 4; i++) {
- int h = 3 + i * 3;
- struct ngui_rect r;
- r.x = x + i * 5;
- r.y = y + 12 - h;
- r.w = 3;
- r.h = h;
- ngui_fill_rect(&ui, r, i < bars ? color : ui.theme.border);
- }
- }
- static void draw_status(void)
- {
- if (status[0] && status_until &&
- (int)(u_ticks_ms() - status_until) < 0)
- ngui_text_utf8(&ui, 10, ui.h - 22, status,
- status_error ? ui.theme.error : ui.theme.accent);
- }
- static void draw_wifi_list(struct ngui_rect area)
- {
- char label[40], shortcut[20];
- int i;
- if (!wifi_present) {
- ngui_text(&ui, area.x + 2, area.y + 4,
- "No WiFi card detected", ui.theme.text_dim);
- return;
- }
- if (scanning) {
- ngui_text(&ui, area.x + 2, area.y + 4,
- "Scanning...", ui.theme.text_dim);
- return;
- }
- if (nbss == 0) {
- ngui_text(&ui, area.x + 2, area.y + 4,
- wst.state >= SYS_WIFI_ST_ASSOC ?
- "Connected - Disconnect to scan for other networks" :
- "Press Scan to list WiFi networks",
- ui.theme.text_dim);
- return;
- }
- wifi_scroll.content_h = nbss * ROW_H;
- ngui_scroll_begin(&ui, area, &wifi_scroll);
- ngui_scroll_wheel(&ui, area, &wifi_scroll);
- ngui_scroll_keys(&ui, &wifi_scroll);
- for (i = 0; i < nbss; i++) {
- struct ngui_rect rr;
- struct sys_wifi_bss *b = &bss[i];
- char ssid[33];
- int sw, bx, clicked;
- unsigned short signal;
- rr.x = area.x;
- rr.y = area.y - wifi_scroll.offset + i * ROW_H;
- rr.w = area.w - 16;
- rr.h = ROW_H - 2;
- if (rr.y + rr.h <= area.y || rr.y >= area.y + area.h)
- continue;
- ssid_name(b, ssid);
- sput(label, ssid);
- sput(shortcut, security_name(b->security));
- clicked = ngui_menu_item_utf8(&ui, ID_BSS_BASE + i, rr, label,
- shortcut, bss_is_active(b));
- sw = (int)ngui_text_width_ui(&ui, shortcut);
- bx = rr.x + rr.w - 8 - sw - 30;
- if (b->security == SYS_WIFI_SEC_OTHER)
- signal = ui.theme.error;
- else
- signal = rssi_bars(b->rssi) >= 3 ? ui.theme.accent :
- ui.theme.text_dim;
- draw_bars(bx, rr.y + 3, rssi_bars(b->rssi), signal);
- if (clicked) {
- action_bss = i;
- }
- }
- ngui_scroll_end(&ui, area, &wifi_scroll);
- }
- static void draw_main(void)
- {
- struct ngui_rect body = { 10, 8, ui.w - 20, ui.h - 34 };
- struct ngui_rect r, rb;
- char line[128], *p;
- int i, shown;
- /* Active connection summary. */
- r = ngui_cut_top(&body, 20);
- p = line;
- if (!net_present) {
- p = sput(p, "No active network device");
- } else {
- p = sput(p, "Active: ");
- p = sput(p, net.name);
- p = sput(p, " (");
- p = sput(p, net.chip);
- p = sput(p, ")");
- if (seq(net.name, "wifi") && wst.state >= SYS_WIFI_ST_ASSOC) {
- p = sput(p, " ");
- for (i = 0; i < wst.ssid_len && i < 32; i++)
- *p++ = (char)wst.ssid[i];
- p = sput(p, " ");
- p = put_signed(p, wst.rssi);
- p = sput(p, " dBm");
- }
- }
- *p = '\0';
- ngui_text_ellipsis_utf8(&ui, r, r.x, r.y, line, ui.theme.text);
- r = ngui_cut_top(&body, 20);
- p = line;
- if (net_present) {
- p = sput(p, "IP ");
- if (net.ip[0] || net.ip[1] || net.ip[2] || net.ip[3])
- p = put_ip(p, net.ip);
- else
- p = sput(p, "not configured");
- if (seq(net.name, "wifi"))
- p = sput(p, wst.state >= SYS_WIFI_ST_LINK ?
- " link up" : " link down");
- } else if (wifi_present) {
- p = sput(p, "WiFi card present - scan to join a network");
- } else {
- p = sput(p, "Plug in Ethernet or USB tethering, then Refresh");
- }
- *p = '\0';
- ngui_text_ellipsis_utf8(&ui, r, r.x, r.y, line, ui.theme.text_dim);
- body.y += 4;
- body.h -= 4;
- /* Devices. */
- r = ngui_cut_top(&body, 26);
- ngui_text(&ui, r.x, r.y + 3, "Network devices", ui.theme.text_dim);
- rb = r;
- rb.x = r.x + r.w - 78;
- rb.w = 78;
- rb.h = 22;
- if (ngui_button(&ui, ID_REFRESH, rb, "Refresh"))
- actions |= ACT_REFRESH;
- shown = 0;
- for (i = 0; i < ndev && shown < 6; i++) {
- if (devs[i].kind == SYS_NETDEV_WIFI)
- continue;
- p = line;
- p = sput(p, devs[i].name);
- p = sput(p, " ");
- p = sput(p, devs[i].chip);
- *p = '\0';
- r = ngui_cut_top(&body, ROW_H);
- if (ngui_menu_item(&ui, ID_DEV_BASE + i, r, line,
- devs[i].active ? "active" :
- (devs[i].link ? "link" : ""),
- devs[i].active))
- action_dev = i;
- shown++;
- }
- if (!shown) {
- r = ngui_cut_top(&body, ROW_H);
- ngui_text(&ui, r.x + 2, r.y + 3,
- wifi_present ?
- "No wired or USB device - use the WiFi list" :
- "None found - Refresh after plugging a device in",
- ui.theme.text_dim);
- }
- body.y += 6;
- body.h -= 6;
- /* WiFi networks. */
- r = ngui_cut_top(&body, 26);
- ngui_text(&ui, r.x, r.y + 3, "WiFi networks", ui.theme.text_dim);
- rb = r;
- rb.x = r.x + r.w - 96;
- rb.w = 96;
- rb.h = 22;
- if (wifi_present) {
- if (wst.state >= SYS_WIFI_ST_ASSOC) {
- if (ngui_button(&ui, ID_DISCONNECT, rb, "Disconnect"))
- actions |= ACT_DISCONNECT;
- } else if (scanning) {
- ngui_text(&ui, rb.x + 10, rb.y + 3, "Scanning...",
- ui.theme.text_dim);
- } else {
- if (ngui_button(&ui, ID_SCAN, rb, "Scan"))
- actions |= ACT_SCAN;
- }
- }
- draw_wifi_list(body);
- }
- static void draw_pass(void)
- {
- struct ngui_rect body = { 10, 10, ui.w - 20, ui.h - 40 };
- struct ngui_rect r, b1, b2;
- char line[80], *p;
- int ev;
- r = ngui_cut_top(&body, 24);
- p = sput(line, "Join WiFi network \"");
- p = sput(p, sel_ssid);
- p = sput(p, "\"");
- *p = '\0';
- ngui_text_ellipsis_utf8(&ui, r, r.x, r.y, line, ui.theme.text);
- r = ngui_cut_top(&body, 22);
- ngui_text(&ui, r.x, r.y, "WPA2-PSK passphrase", ui.theme.text_dim);
- r = ngui_cut_top(&body, 30);
- ev = ngui_text_input(&ui, ID_PASS, r, &pass_in);
- if (ev & NGUI_INPUT_SUBMIT)
- actions |= ACT_CONNECT;
- if (ev & NGUI_INPUT_CANCEL)
- actions |= ACT_BACK;
- body.y += 6;
- body.h -= 6;
- r = ngui_cut_top(&body, 30);
- b1 = r;
- b1.w = 100;
- b2 = r;
- b2.x = r.x + 110;
- b2.w = 100;
- if (ngui_button(&ui, ID_PASS_OK, b1, "Connect"))
- actions |= ACT_CONNECT;
- if (ngui_button(&ui, ID_PASS_CANCEL, b2, "Cancel"))
- actions |= ACT_BACK;
- r = ngui_cut_top(&body, 22);
- ngui_text(&ui, r.x, r.y,
- "8-63 characters, or 64 hex digits for a raw PSK.",
- ui.theme.text_dim);
- draw_status();
- }
- static void draw_connect(void)
- {
- struct ngui_rect body = { 10, 10, ui.w - 20, ui.h - 40 };
- struct ngui_rect r, b;
- char line[96], *p;
- unsigned int start = 0, lines = 0, i;
- int y, line_y;
- r = ngui_cut_top(&body, 24);
- p = line;
- if (connect_pid > 0) {
- p = sput(p, "Connecting to \"");
- p = sput(p, sel_ssid);
- p = sput(p, "\"...");
- } else {
- p = sput(p, "WiFi connection \"");
- p = sput(p, sel_ssid);
- p = sput(p, "\"");
- }
- *p = '\0';
- ngui_text_ellipsis_utf8(&ui, r, r.x, r.y, line, ui.theme.text);
- r = ngui_cut_top(&body, 22);
- if (status[0])
- ngui_text_utf8(&ui, r.x, r.y, status,
- status_error ? ui.theme.error : ui.theme.accent);
- body.y += 4;
- body.h -= 4;
- /* Last ~12 lines of the helper's terminal output. */
- for (i = conn_log_len; i > 0; i--) {
- if (conn_log[i - 1] == '\n' && ++lines > 12) {
- start = i;
- break;
- }
- }
- y = body.y;
- line_y = 0;
- for (i = start; i < conn_log_len && y + 16 <= body.y + body.h - 34; i++) {
- char c = conn_log[i];
- if (c == '\r')
- continue;
- if (c == '\n') {
- line[line_y] = '\0';
- ngui_text_utf8(&ui, body.x, y, line, ui.theme.text_dim);
- y += 16;
- line_y = 0;
- continue;
- }
- if ((unsigned int)line_y + 1 < sizeof(line))
- line[line_y++] = c;
- }
- if (line_y) {
- line[line_y] = '\0';
- ngui_text_utf8(&ui, body.x, y, line, ui.theme.text_dim);
- }
- /* Cancel at the bottom. */
- b.x = body.x;
- b.y = body.y + body.h - 28;
- b.w = 100;
- b.h = 24;
- if (ngui_button(&ui, ID_CONN_CANCEL, b,
- connect_pid > 0 ? "Cancel" : "Back"))
- actions |= ACT_BACK;
- }
- static void draw(void)
- {
- ngui_fill_rect(&ui, (struct ngui_rect){ 0, 0, ui.w, ui.h },
- ui.theme.bg);
- if (view == VIEW_PASS)
- draw_pass();
- else if (view == VIEW_CONNECT)
- draw_connect();
- else
- draw_main();
- if (view != VIEW_PASS && view != VIEW_CONNECT)
- draw_status();
- }
- static void paint_frame(void)
- {
- if (ngui_begin(&ui)) {
- draw();
- ngui_end(&ui);
- }
- actions = 0;
- action_dev = -1;
- action_bss = -1;
- }
- void _start(const char *arg)
- {
- struct win_info wi;
- unsigned int last_status = 0;
- (void)arg;
- if (u_win_create(460, 500, "Network", &wi) != 0 &&
- u_win_create(400, 420, "Network", &wi) != 0 &&
- u_win_create(340, 360, "Network", &wi) != 0) {
- u_exit(1);
- }
- ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
- ngui_font_attach(&ui);
- pass_in.secret = 1;
- pass_in.placeholder = "WPA2 passphrase";
- view = VIEW_MAIN;
- load_devices();
- load_status();
- if (wifi_present && wst.state != SYS_WIFI_ST_DOWN &&
- wst.state < SYS_WIFI_ST_ASSOC) {
- start_scan();
- paint_frame();
- }
- for (;;) {
- unsigned int now = u_ticks_ms();
- drain_tty();
- poll_scan();
- poll_connect();
- if (now - last_status >= 1000) {
- last_status = now;
- load_status();
- ngui_invalidate(&ui);
- }
- if (status_until && (int)(now - status_until) >= 0) {
- status[0] = '\0';
- status_until = 0;
- ngui_invalidate(&ui);
- }
- if (ngui_begin(&ui)) {
- draw();
- ngui_end(&ui);
- handle_actions();
- }
- /* The password field owns Esc while focused. */
- if (ngui_key_peek(&ui) == NGUI_KEY_ESC) {
- ngui_key(&ui);
- if (view == VIEW_PASS) {
- view = VIEW_MAIN;
- set_status("", 0);
- } else if (view == VIEW_CONNECT) {
- cancel_connect();
- } else if (ui.focus == 0) {
- break;
- }
- }
- u_sleep_ms(25);
- }
- ngui_font_detach(&ui);
- u_win_close();
- u_exit(0);
- }
|