| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974 |
- /* 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++;
- 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;
- for (i = 0; i < n; i++)
- out[i] = (b->ssid[i] >= 32 && b->ssid[i] < 127) ?
- (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 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();
- 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)"/share/netmenu.wifi",
- (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(&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(&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++ = (wst.ssid[i] >= 32 && wst.ssid[i] < 127) ?
- (char)wst.ssid[i] : '?';
- p = sput(p, " ");
- p = put_signed(p, wst.rssi);
- p = sput(p, " dBm");
- }
- }
- *p = '\0';
- ngui_text_ellipsis(&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(&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(&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(&ui, r, r.x, r.y, line, ui.theme.text);
- r = ngui_cut_top(&body, 22);
- if (status[0])
- ngui_text(&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(&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 >= 32 && c < 127) ? c : '.';
- }
- if (line_y) {
- line[line_y] = '\0';
- ngui_text(&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);
- }
|