/* netmenu: the top-bar control panel. * * One flat popover for the status area: battery/AC and clock in the summary, * a brightness slider, the NIC list and WiFi scan/join/saved-network views, * connection details and quick tiles for Wi-Fi, the P-state power mode and * the Date & Time editor. Selecting a wired or USB device makes it the * active netdev and runs DHCP; selecting a WiFi network joins it with the * passphrase saved in /data/WIFI.CFG when one is known, otherwise it asks for * the WPA2 passphrase, 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). A Saved view lists the known * networks and forgets individual entries. The supplicant itself runs DHCP * once the data port opens. * * The WM opens one instance from the network, battery or clock status area; * an argument ("network"/"power"/"clock") scrolls the panel to that * section. */ #include "ucmd_lib.h" #include "ngui.h" #include "wifi_store.h" #define MAX_DEVS 8 #define MAX_BSS 32 #define DEV_ROW_H 42 #define WIFI_ROW_H 42 #define SAVED_ROW_H 30 #define SAVED_LIST_ROW_H 42 #define SECTION_H 28 #define MENU_GAP 10 #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_SAVED 8 #define ID_SAVED_BACK 9 #define ID_DEV_BASE 100 #define ID_BSS_BASE 200 #define ID_SAVED_BASE 300 #define ID_INFO_BASE 400 #define ID_SAVED_ROW_BASE 500 #define ID_BRIGHT 11 #define ID_TILE_NETWORK 12 #define ID_TILE_POWER 13 #define ID_TILE_DATE 14 #define ID_DATE_FIELD 20 #define ID_TIME_FIELD 21 #define ID_NTP_FIELD 22 #define ID_DATE_SET 23 #define ID_DATE_NOW 24 #define ID_DATE_NTP 25 #define ID_POWER_BASE 30 #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) #define ACT_SAVED (1u << 5) #define ACT_FORGET (1u << 6) #define ACT_NET_TOGGLE (1u << 7) #define ACT_POWER_TOGGLE (1u << 8) #define ACT_DATE_TOGGLE (1u << 9) #define ACT_DATE_SET (1u << 10) #define ACT_DATE_NOW (1u << 11) #define ACT_DATE_NTP (1u << 12) #define ACT_POWER_AUTO (1u << 13) #define ACT_POWER_MAX (1u << 14) #define ACT_POWER_MIN (1u << 15) enum { VIEW_MAIN, VIEW_PASS, VIEW_CONNECT, VIEW_SAVED }; enum { EXPAND_NONE, EXPAND_NETWORK, EXPAND_POWER, EXPAND_DATE }; static struct ngui ui; static struct ngui_popover pop; static struct ngui_input pass_in; static struct ngui_input date_in, time_in, ntp_in; static struct ngui_scroll main_scroll; static struct ngui_scroll saved_scroll; static struct ngui_slider brightness_slider; static struct sys_netdev devs[MAX_DEVS]; static int ndev; static struct sys_wifi_bss bss[MAX_BSS]; static int nbss; static struct wifi_cfg saved[WIFI_CFG_MAX]; static int nsaved; 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 brightness = -1; static int cpufreq_mode = -1; static struct sys_powerinfo powerinfo; static int power_ok; static struct sys_datetime now; static int now_ok; static int expanded; static int date_loaded; static unsigned int screen_w = 1024, screen_h = 768; static int resize_pending; 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 int action_saved = -1; static void draw(void); static void paint_frame(void); static const char *power_mode_name(int mode); static void resize_panel(void); static void load_datetime_fields(int force); static void toggle_expand(int which); 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 char *put_mac(char *p, const unsigned char mac[6]) { static const char hex[] = "0123456789abcdef"; int i; for (i = 0; i < 6; i++) { *p++ = hex[mac[i] >> 4]; *p++ = hex[mac[i] & 15]; if (i != 5) *p++ = ':'; } return p; } static int ip_any(const unsigned char ip[4]) { return ip[0] || ip[1] || ip[2] || ip[3]; } static char *put_2(char *p, unsigned int v) { *p++ = (char)('0' + (v / 10) % 10); *p++ = (char)('0' + v % 10); return p; } static void format_time(char *b, const struct sys_datetime *dt) { char *p = put_2(b, dt->hour); *p++ = ':'; p = put_2(p, dt->minute); *p = '\0'; } static void format_date(char *b, const struct sys_datetime *dt) { char *p = put_dec(b, dt->year); *p++ = '-'; p = put_2(p, dt->month); *p++ = '-'; p = put_2(p, dt->day); *p = '\0'; } static void format_time_sec(char *b, const struct sys_datetime *dt) { char *p = put_2(b, dt->hour); *p++ = ':'; p = put_2(p, dt->minute); *p++ = ':'; p = put_2(p, dt->second); *p = '\0'; } 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 void cfg_ssid_name(const struct wifi_cfg *c, char out[33]) { unsigned int i, n = c->ssid_len; if (n > 32) n = 32; for (i = 0; i < n; i++) out[i] = (char)c->ssid[i]; out[n] = '\0'; } static void load_saved(void) { int n = wifi_store_load(saved, WIFI_CFG_MAX); nsaved = n > 0 ? n : 0; } static int bss_saved(const struct sys_wifi_bss *b) { return wifi_store_find(saved, nsaved, b->ssid, b->ssid_len); } 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; } power_ok = (u_power_info(&powerinfo, 0) == 0); now_ok = (u_rtc_get(&now) == 0); brightness = u_backlight_get(); cpufreq_mode = u_cpufreq_get(); } static void load_datetime_fields(int force) { char b[24]; if (date_loaded && !force) return; if (force || !now_ok) { if (u_rtc_get(&now) != 0) { now_ok = 0; return; } now_ok = 1; } format_date(b, &now); ngui_input_set(&date_in, b); format_time_sec(b, &now); ngui_input_set(&time_in, b); if (!ntp_in.len) ngui_input_set(&ntp_in, "pool.ntp.org"); date_loaded = 1; } 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; main_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(); load_saved(); 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) { int si = bss_saved(&bss[i]); if (si >= 0 && saved[si].pass_len > 0) { char pass[65]; unsigned int j; for (j = 0; j < saved[si].pass_len; j++) pass[j] = saved[si].pass[j]; pass[saved[si].pass_len] = '\0'; start_connect(pass); for (j = 0; j < sizeof(pass); j++) ((volatile char *)pass)[j] = 0; return; } 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 unsigned int days_in_month(unsigned int y, unsigned int m) { static const unsigned char dm[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (m < 1 || m > 12) return 0; if (m == 2 && ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)) return 29; return dm[m - 1]; } static int parse_num(const char *s, unsigned int n, unsigned int *out) { unsigned int v = 0, i; if (!n) return -1; for (i = 0; i < n; i++) { if (s[i] < '0' || s[i] > '9') return -1; v = v * 10 + (unsigned int)(s[i] - '0'); } *out = v; return 0; } static int parse_date(const char *s, unsigned int *y, unsigned int *mo, unsigned int *d) { if (slen(s) != 10) return -1; if ((s[4] != '-' && s[4] != '/') || (s[7] != '-' && s[7] != '/')) return -1; if (parse_num(s, 4, y) != 0 || parse_num(s + 5, 2, mo) != 0 || parse_num(s + 8, 2, d) != 0) return -1; return 0; } static int parse_time(const char *s, unsigned int *h, unsigned int *mi, unsigned int *sec) { unsigned int len = slen(s); if (len == 5) { *sec = 0; } else if (len == 8) { if (s[5] != ':' || parse_num(s + 6, 2, sec) != 0) return -1; } else { return -1; } if (s[2] != ':' || parse_num(s, 2, h) != 0 || parse_num(s + 3, 2, mi) != 0) return -1; return 0; } static void apply_datetime(void) { struct sys_datetime dt; unsigned int y, mo, d, h, mi, s; if (parse_date(date_in.text, &y, &mo, &d) != 0) { set_status("Date must be YYYY-MM-DD", 1); return; } if (parse_time(time_in.text, &h, &mi, &s) != 0) { set_status("Time must be HH:MM:SS", 1); return; } if (y < 1970 || y > 2069) { set_status("Year must be 1970-2069", 1); return; } if (mo < 1 || mo > 12) { set_status("Month must be 1-12", 1); return; } if (d < 1 || d > days_in_month(y, mo)) { set_status("Day is not valid for that month", 1); return; } if (h > 23 || mi > 59 || s > 59) { set_status("Time is out of range", 1); return; } dt.year = y; dt.month = mo; dt.day = d; dt.hour = h; dt.minute = mi; dt.second = s; dt.wday = 0; if (u_rtc_set(&dt) != 0) { set_status("RTC write failed", 1); return; } load_status(); load_datetime_fields(1); set_status("Date and time updated", 0); } static const char *ntp_error(int r) { switch (r) { case SYS_NTP_ENONIC: return "NTP: no network adapter"; case SYS_NTP_ENAME: return "NTP: cannot resolve the server"; case SYS_NTP_EARP: return "NTP: server is unreachable (ARP)"; case SYS_NTP_ETX: return "NTP: transmit failed"; case SYS_NTP_ETIMEOUT: return "NTP: server did not respond"; case SYS_NTP_EREPLY: return "NTP: invalid server reply"; case SYS_NTP_EBUSY: return "NTP: network is busy"; default: return "NTP: sync failed"; } } static void sync_ntp(void) { struct sys_datetime dt; const char *server = ntp_in.text[0] ? ntp_in.text : "pool.ntp.org"; int r; set_status("Syncing NTP...", 0); paint_frame(); r = u_ntp_sync(server, 5000, &dt); if (r != 0) { set_status(ntp_error(r), 1); return; } if (u_rtc_set(&dt) != 0) { set_status("RTC write failed", 1); return; } load_status(); load_datetime_fields(1); set_status("Date and time synced", 0); } static void set_power_mode(int mode) { int got = u_cpufreq_set(mode); if (got < 0) { set_status("Power mode is not available", 1); return; } cpufreq_mode = got; { char msg[36], *p = sput(msg, "Power mode: "); sput(p, power_mode_name(got)); set_status(msg, 0); } ngui_invalidate(&ui); } static void handle_actions(void) { unsigned int a = actions; int di = action_dev, bi = action_bss, fi = action_saved; actions = 0; action_dev = -1; action_bss = -1; action_saved = -1; if (a & ACT_NET_TOGGLE) { toggle_expand(EXPAND_NETWORK); return; } if (a & ACT_POWER_TOGGLE) { toggle_expand(EXPAND_POWER); return; } if (a & ACT_DATE_TOGGLE) { toggle_expand(EXPAND_DATE); return; } if (a & ACT_DATE_SET) { apply_datetime(); return; } if (a & ACT_DATE_NOW) { load_datetime_fields(1); set_status("Date and time reloaded", 0); return; } if (a & ACT_DATE_NTP) { sync_ntp(); return; } if (a & ACT_POWER_AUTO) { set_power_mode(SYS_CPUFREQ_AUTO); return; } if (a & ACT_POWER_MAX) { set_power_mode(SYS_CPUFREQ_MAX); return; } if (a & ACT_POWER_MIN) { set_power_mode(SYS_CPUFREQ_MIN); return; } if (a & ACT_BACK) { if (view == VIEW_CONNECT) cancel_connect(); else { view = VIEW_MAIN; set_status("", 0); } return; } if (a & ACT_SAVED) { load_saved(); saved_scroll.offset = 0; view = VIEW_SAVED; set_status("", 0); ngui_invalidate(&ui); return; } if (a & ACT_FORGET) { if (fi >= 0 && fi < nsaved) { if (wifi_store_forget(saved[fi].ssid, saved[fi].ssid_len) > 0) { load_saved(); saved_scroll.offset = 0; set_status("Saved network removed", 0); } else { set_status("Could not remove the saved network", 1); } } ngui_invalidate(&ui); 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_status(void) { if (status[0] && status_until && (int)(u_ticks_ms() - status_until) < 0) ngui_popover_footer(&ui, &pop, status, status_error ? ui.theme.error : ui.theme.success); } /* The active connection at the top of the panel: a status dot, the link * state and the device address (plus the SSID and RSSI on Wi-Fi). */ static void draw_status_card(struct ngui_rect r) { struct ngui_rect line_r = { r.x + 34, r.y + 34, r.w - 44, 16 }; struct ngui_rect dot = { r.x + 14, r.y + 25, 10, 10 }; char line[160], *p; unsigned short dot_color; int i, have_ip = 0; for (i = 0; i < 4; i++) if (net.ip[i]) have_ip = 1; if (!net_present) dot_color = ui.theme.error; else if (!have_ip || (seq(net.name, "wifi") && wst.state < SYS_WIFI_ST_LINK)) dot_color = ui.theme.warning; else dot_color = ui.theme.success; ngui_fill_round_rect(&ui, r, 8, 8, 8, 8, ui.theme.panel); ngui_frame_round(&ui, r, 8, 8, 8, 8, 1, ui.theme.panel_alt); ngui_fill_round_rect(&ui, dot, 5, 5, 5, 5, dot_color); p = line; if (!net_present) p = sput(p, "Offline"); else if (seq(net.name, "wifi")) p = sput(p, wst.state >= SYS_WIFI_ST_LINK ? "Wi-Fi connected" : "Wi-Fi"); else if (seq(net.name, "rndis")) p = sput(p, "USB tethering"); else p = sput(p, "Wired network"); *p = '\0'; ngui_text_px(&ui, r.x + 34, r.y + 10, line, slen(line), ui.theme.text, 14, 1, 0, 0); p = line; if (net_present) { p = sput(p, net.name); p = sput(p, " \xC2\xB7 "); if (have_ip) p = put_ip(p, net.ip); else p = sput(p, "no IP"); if (seq(net.name, "wifi") && wst.state >= SYS_WIFI_ST_ASSOC) { p = sput(p, " \xC2\xB7 "); 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"); } } else { p = sput(p, "No network device active"); } *p = '\0'; ngui_text_ellipsis_utf8(&ui, line_r, line_r.x, line_r.y, line, ui.theme.text_dim); } static void draw_wifi_list(struct ngui_rect area, int *y) { struct ngui_row row; char ssid[33], sub[72], *p; int i; if (!wifi_present) { ngui_text(&ui, area.x + 12, *y + 12, "No Wi-Fi card detected", ui.theme.text_dim); *y += WIFI_ROW_H; return; } if (scanning) { ngui_text(&ui, area.x + 12, *y + 12, "Scanning for networks...", ui.theme.accent); *y += WIFI_ROW_H; return; } if (nbss == 0) { const char *msg = wst.state >= SYS_WIFI_ST_ASSOC ? "Connected - disconnect to scan for other networks" : "Press Scan to list Wi-Fi networks"; ngui_text(&ui, area.x + 12, *y + 12, msg, ui.theme.text_dim); *y += WIFI_ROW_H; return; } for (i = 0; i < nbss; i++) { struct sys_wifi_bss *b = &bss[i]; struct ngui_rect r = { area.x, *y, area.w, WIFI_ROW_H }; if (r.y + r.h > area.y && r.y < area.y + area.h) { ssid_name(b, ssid); p = sub; p = sput(p, security_name(b->security)); if (bss_saved(b) >= 0) p = sput(p, " \xC2\xB7 saved"); if (bss_is_active(b)) p = sput(p, " \xC2\xB7 connected"); *p = '\0'; row.title = ssid; row.subtitle = sub; row.trailing = 0; row.bars = b->security == SYS_WIFI_SEC_OTHER ? -1 : rssi_bars(b->rssi); row.selected = bss_is_active(b); if (ngui_list_row_utf8(&ui, ID_BSS_BASE + i, r, &row)) action_bss = i; } *y += WIFI_ROW_H; } } /* Top summary: battery/AC on the left, wall clock on the right. */ static void draw_summary(struct ngui_rect r) { char tbuf[16], dbuf[16], title[32]; char *p; int batt = -1, charging = 0, discharging = 0, ac = -1; int tw; unsigned short dot; const char *sub_p = ""; unsigned int i; if (power_ok) { ac = powerinfo.ac_online; for (i = 0; i < SYS_POWER_BATT_MAX; i++) { struct sys_battery *b = &powerinfo.battery[i]; if (!b->present) continue; if (b->percent_valid) batt = (int)b->percent; charging = (int)b->charging; discharging = (int)b->discharging; break; } } if (batt >= 0) { p = sput(title, "Battery "); p = put_dec(p, (unsigned int)batt); *p++ = '%'; *p = '\0'; if (charging) sub_p = "Charging"; else if (discharging) sub_p = "On battery"; else if (ac > 0) sub_p = "Plugged in"; else sub_p = ""; dot = batt <= 10 ? ui.theme.error : batt <= 25 ? ui.theme.warning : ui.theme.success; } else if (power_ok && ac > 0) { sput(title, "AC power"); sub_p = "No battery"; dot = ui.theme.success; } else if (power_ok) { sput(title, "Desktop"); sub_p = "No battery"; dot = ui.theme.text_dim; } else { sput(title, "Power"); sub_p = "Unavailable"; dot = ui.theme.warning; } ngui_fill_round_rect(&ui, r, 10, 10, 10, 10, ui.theme.panel); ngui_fill_round_rect(&ui, (struct ngui_rect){ r.x + 14, r.y + 23, 10, 10 }, 5, 5, 5, 5, dot); ngui_text_px(&ui, r.x + 30, r.y + 8, title, slen(title), ui.theme.text, 14, 1, 0, 0); ngui_text_ellipsis_utf8(&ui, (struct ngui_rect){ r.x + 30, r.y + 30, r.w / 2 - 34, 18 }, r.x + 30, r.y + 30, sub_p, ui.theme.text_dim); ngui_fill_rect(&ui, (struct ngui_rect){ r.x + r.w / 2, r.y + 10, 1, r.h - 20 }, ui.theme.button); if (now_ok) { format_time(tbuf, &now); format_date(dbuf, &now); } else { sput(tbuf, "--:--"); sput(dbuf, "No clock"); } tw = (int)ngui_text_width_ui(&ui, tbuf); ngui_text_px(&ui, r.x + r.w - 14 - tw, r.y + 8, tbuf, slen(tbuf), ui.theme.text, 14, 1, 0, 0); tw = (int)ngui_text_width_ui(&ui, dbuf); ngui_text_px(&ui, r.x + r.w - 14 - tw, r.y + 30, dbuf, slen(dbuf), ui.theme.text_dim, 14, 0, 0, 0); } /* Brightness slider. Only drawn when a panel PWM exists. */ static void draw_brightness(struct ngui_rect r) { struct ngui_rect slider = { r.x + 14, r.y + 26, r.w - 28, 16 }; char b[8], *p; int v = brightness; int tw; ngui_fill_round_rect(&ui, r, 10, 10, 10, 10, ui.theme.panel); ngui_text_px(&ui, r.x + 14, r.y + 6, "Brightness", 10, ui.theme.text, 14, 1, 0, 0); p = put_dec(b, (unsigned int)(v < 0 ? 0 : v)); *p++ = '%'; *p = '\0'; tw = (int)ngui_text_width_ui(&ui, b); ngui_text_px(&ui, r.x + r.w - 14 - tw, r.y + 6, b, slen(b), ui.theme.text_dim, 14, 0, 0, 0); if (ngui_slider(&ui, ID_BRIGHT, &brightness_slider, slider, &v, 5, 100)) { int got = u_backlight_set(v); brightness = got >= 0 ? got : v; } } static const char *power_mode_name(int mode) { switch (mode) { case SYS_CPUFREQ_MAX: return "Performance"; case SYS_CPUFREQ_MIN: return "Power saver"; case SYS_CPUFREQ_OFF: return "Off"; case SYS_CPUFREQ_FIXED: return "Fixed"; case SYS_CPUFREQ_AUTO: return "Balanced"; default: return "Unavailable"; } } static int quick_tile_count(void) { return 2 + (cpufreq_mode >= 0 ? 1 : 0); } static int quick_tile_height(int n) { int rows = (n + 1) / 2; return rows * 56 + (rows - 1) * MENU_GAP; } static void draw_tiles(struct ngui_rect area, int *yy) { unsigned int ids[3]; char titles[3][16], subs[3][48]; int ons[3], acts[3], n = 0, i, row = 0; int nrows, colw; ids[n] = ID_TILE_NETWORK; sput(titles[n], "Network"); if (net_present && seq(net.name, "wifi") && wst.state >= SYS_WIFI_ST_ASSOC && wst.ssid_len) { unsigned int j, len = wst.ssid_len; char *sp = subs[n]; if (len > 32) len = 32; for (j = 0; j < len; j++) *sp++ = (char)wst.ssid[j]; *sp = '\0'; } else if (net_present && seq(net.name, "wifi")) { sput(subs[n], "Wi-Fi"); } else if (net_present) { sput(subs[n], net.name); } else if (wifi_present) { sput(subs[n], "Wi-Fi"); } else { sput(subs[n], "Offline"); } ons[n] = expanded == EXPAND_NETWORK; acts[n] = ACT_NET_TOGGLE; n++; if (cpufreq_mode >= 0) { ids[n] = ID_TILE_POWER; sput(titles[n], "Power mode"); sput(subs[n], power_mode_name(cpufreq_mode)); ons[n] = expanded == EXPAND_POWER; acts[n] = ACT_POWER_TOGGLE; n++; } ids[n] = ID_TILE_DATE; sput(titles[n], "Date & Time"); if (now_ok) format_time(subs[n], &now); else sput(subs[n], "Set clock"); ons[n] = expanded == EXPAND_DATE; acts[n] = ACT_DATE_TOGGLE; n++; nrows = (n + 1) / 2; colw = (area.w - MENU_GAP) / 2; for (i = 0; i < n; i++) { int col = i & 1; int w = (i + 1 < n) ? colw : (col == 0 ? area.w : colw); struct ngui_rect r = { area.x + col * (colw + MENU_GAP), *yy, w, 56 }; if (r.y + r.h > area.y && r.y < area.y + area.h && ngui_tile(&ui, ids[i], r, titles[i], subs[i], ons[i])) actions |= (unsigned int)acts[i]; if (col == 1 || i + 1 >= n) { *yy += 56; row++; if (row < nrows) *yy += MENU_GAP; } } } static int network_content_height(void) { int i, shown = 0, more, h; for (i = 0; i < ndev; i++) if (devs[i].kind != SYS_NETDEV_WIFI) shown++; more = shown > 4 ? shown - 4 : 0; if (shown > 4) shown = 4; h = SECTION_H + 64 + MENU_GAP; h += SECTION_H + (shown ? shown * DEV_ROW_H : DEV_ROW_H) + (more ? 20 : 0) + MENU_GAP; h += SECTION_H + ((!wifi_present || scanning || nbss == 0) ? WIFI_ROW_H : nbss * WIFI_ROW_H); if (net_present) h += MENU_GAP + SECTION_H + 4 * 30; if (nsaved) h += MENU_GAP + SAVED_ROW_H; return h; } static void draw_network_section(struct ngui_rect area, int *yy) { struct ngui_row row; struct ngui_rect r; int i, shown = 0, more, n, y = *yy; r = (struct ngui_rect){ area.x, y, area.w, SECTION_H }; ngui_section(&ui, r, "Network", 0, 0); y += SECTION_H; r = (struct ngui_rect){ area.x, y, area.w, 64 }; draw_status_card(r); y += 64 + MENU_GAP; for (i = 0; i < ndev; i++) if (devs[i].kind != SYS_NETDEV_WIFI) shown++; more = shown > 4 ? shown - 4 : 0; if (shown > 4) shown = 4; r = (struct ngui_rect){ area.x, y, area.w, SECTION_H }; if (ngui_section(&ui, r, "Devices", ID_REFRESH, "Refresh")) actions |= ACT_REFRESH; y += SECTION_H; n = 0; for (i = 0; i < ndev && n < shown; i++) { if (devs[i].kind == SYS_NETDEV_WIFI) continue; r = (struct ngui_rect){ area.x, y, area.w, DEV_ROW_H }; if (r.y + r.h > area.y && r.y < area.y + area.h) { row.title = devs[i].name; row.subtitle = devs[i].chip; row.trailing = devs[i].active ? "Connected" : (devs[i].link ? "link" : 0); row.bars = -1; row.selected = devs[i].active; if (ngui_list_row(&ui, ID_DEV_BASE + i, r, &row)) action_dev = i; } y += DEV_ROW_H; n++; } if (!shown) { const char *msg = wifi_present ? "No wired or USB device" : "No device found - plug one in and Refresh"; ngui_text(&ui, area.x + 12, y + 12, msg, ui.theme.text_dim); y += DEV_ROW_H; } else if (more) { char line[32], *p = line; p = sput(p, "+"); p = put_dec(p, (unsigned int)more); p = sput(p, " more devices"); *p = '\0'; ngui_text(&ui, area.x + 12, y + 2, line, ui.theme.text_dim); y += 20; } y += MENU_GAP; r = (struct ngui_rect){ area.x, y, area.w, SECTION_H }; if (wifi_present && wst.state >= SYS_WIFI_ST_ASSOC) { if (ngui_section(&ui, r, "Wi-Fi", ID_DISCONNECT, "Disconnect")) actions |= ACT_DISCONNECT; } else if (wifi_present && !scanning) { if (ngui_section(&ui, r, "Wi-Fi", ID_SCAN, "Scan")) actions |= ACT_SCAN; } else { ngui_section(&ui, r, "Wi-Fi", 0, 0); } y += SECTION_H; draw_wifi_list(area, &y); if (net_present) { static const char *labels[4] = { "IP address", "Gateway", "DNS", "MAC address" }; char value[40]; int di; y += MENU_GAP; r = (struct ngui_rect){ area.x, y, area.w, SECTION_H }; ngui_section(&ui, r, "Connection", 0, 0); y += SECTION_H; for (di = 0; di < 4; di++) { char *vp = value; if (di == 0) vp = ip_any(net.ip) ? put_ip(vp, net.ip) : sput(vp, "not configured"); else if (di == 1) vp = ip_any(net.gw) ? put_ip(vp, net.gw) : sput(vp, "-"); else if (di == 2) vp = ip_any(net.dns) ? put_ip(vp, net.dns) : sput(vp, "-"); else vp = put_mac(vp, net.mac); *vp = '\0'; r = (struct ngui_rect){ area.x, y, area.w, 30 }; if (r.y + r.h > area.y && r.y < area.y + area.h) { row.title = labels[di]; row.subtitle = 0; row.trailing = value; row.bars = -1; row.selected = 0; ngui_list_row(&ui, ID_INFO_BASE + di, r, &row); } y += 30; } } if (nsaved) { char nbuf[12], *np = nbuf; y += MENU_GAP; r = (struct ngui_rect){ area.x, y, area.w, SAVED_ROW_H }; if (r.y + r.h > area.y && r.y < area.y + area.h) { np = put_dec(np, (unsigned int)nsaved); *np = '\0'; row.title = "Saved networks"; row.subtitle = 0; row.trailing = nbuf; row.bars = -1; row.selected = 0; if (ngui_list_row(&ui, ID_SAVED, r, &row)) actions |= ACT_SAVED; } } *yy = y; } static int power_content_height(void) { return SECTION_H + 3 * 42 + MENU_GAP + 20; } static void draw_power_section(struct ngui_rect area, int *yy) { static const char *labels[3] = { "Balanced", "Performance", "Power saver" }; static const char *subs[3] = { "Automatic race-to-idle", "Maximum non-turbo P-state", "Minimum P-state" }; static const int modes[3] = { SYS_CPUFREQ_AUTO, SYS_CPUFREQ_MAX, SYS_CPUFREQ_MIN }; static const unsigned int acts[3] = { ACT_POWER_AUTO, ACT_POWER_MAX, ACT_POWER_MIN }; struct ngui_row row; struct ngui_rect r; int i, y = *yy; r = (struct ngui_rect){ area.x, y, area.w, SECTION_H }; ngui_section(&ui, r, "Power mode", 0, 0); y += SECTION_H; for (i = 0; i < 3; i++) { r = (struct ngui_rect){ area.x, y, area.w, 42 }; if (r.y + r.h > area.y && r.y < area.y + area.h) { row.title = labels[i]; row.subtitle = subs[i]; row.trailing = 0; row.bars = -1; row.selected = cpufreq_mode == modes[i]; if (ngui_list_row(&ui, ID_POWER_BASE + i, r, &row)) actions |= acts[i]; } y += 42; } y += MENU_GAP; ngui_text(&ui, area.x + 12, y, "Changes the P-state governor immediately.", ui.theme.text_dim); y += 20; *yy = y; } static int date_content_height(void) { return SECTION_H + 20 + 3 * (18 + 32 + 8) + 28 + MENU_GAP + 40; } static void date_field(struct ngui_rect area, int *yy, const char *label, unsigned int id, struct ngui_input *in, unsigned int next_id) { struct ngui_rect r; int ev; r = (struct ngui_rect){ area.x + 12, *yy, area.w - 24, 18 }; ngui_text(&ui, r.x, r.y, label, ui.theme.text_dim); *yy += 18; r = (struct ngui_rect){ area.x + 12, *yy, area.w - 24, 32 }; ev = ngui_text_input(&ui, id, r, in); if (ev & NGUI_INPUT_SUBMIT) actions |= id == ID_NTP_FIELD ? ACT_DATE_NTP : ACT_DATE_SET; if (ev & NGUI_INPUT_NEXT) ngui_set_focus(&ui, next_id); if (ev & NGUI_INPUT_CANCEL) ngui_set_focus(&ui, 0); *yy += 32 + 8; } static void draw_date_section(struct ngui_rect area, int *yy) { struct ngui_rect r, b; char cur[64], *p; int y = *yy; r = (struct ngui_rect){ area.x, y, area.w, SECTION_H }; ngui_section(&ui, r, "Date & Time", 0, 0); y += SECTION_H; p = cur; p = sput(p, "Current: "); if (now_ok) { format_date(p, &now); p += slen(p); *p++ = ' '; format_time_sec(p, &now); } else { sput(p, "unavailable"); } ngui_text(&ui, area.x + 12, y, cur, ui.theme.text_dim); y += 20; date_field(area, &y, "Date (YYYY-MM-DD)", ID_DATE_FIELD, &date_in, ID_TIME_FIELD); date_field(area, &y, "Time (HH:MM:SS)", ID_TIME_FIELD, &time_in, ID_NTP_FIELD); date_field(area, &y, "NTP server (UTC)", ID_NTP_FIELD, &ntp_in, ID_DATE_FIELD); r = (struct ngui_rect){ area.x + 12, y, 80, 28 }; b = r; if (ngui_button(&ui, ID_DATE_SET, b, "Set")) actions |= ACT_DATE_SET; b.x = r.x + 88; if (ngui_button(&ui, ID_DATE_NOW, b, "Now")) actions |= ACT_DATE_NOW; b.x = r.x + 176; b.w = 110; if (ngui_button(&ui, ID_DATE_NTP, b, "Sync NTP")) actions |= ACT_DATE_NTP; y += 28 + MENU_GAP; ngui_text(&ui, area.x + 12, y, "Sync NTP fetches UTC and writes the RTC.", ui.theme.text_dim); y += 20; ngui_text(&ui, area.x + 12, y, "Year range: 1970-2069. Enter applies.", ui.theme.text_dim); y += 20; *yy = y; } static int panel_width(void) { int w = 420; if (w > (int)screen_w - 8) w = (int)screen_w - 8; if (w < 340) w = 340; return w; } static int desired_panel_height(void) { int content, h, max; if (view == VIEW_PASS) { h = 340; } else if (view == VIEW_CONNECT) { h = 420; } else if (view == VIEW_SAVED) { h = 460; } else { content = SECTION_H + quick_tile_height(quick_tile_count()); if (expanded == EXPAND_NETWORK) content += MENU_GAP + network_content_height(); else if (expanded == EXPAND_POWER) content += MENU_GAP + power_content_height(); else if (expanded == EXPAND_DATE) content += MENU_GAP + date_content_height(); h = 112 + (brightness >= 0 ? 62 : 0) + content; } max = (int)screen_h - 38; if (h > max) h = max; if (h < 180) h = 180; return h; } static void resize_panel(void) { static int resize_sent; int want = desired_panel_height(); if (resize_pending) { if (ui.h == resize_sent) resize_pending = 0; return; } if (ui.h != want) { if (u_win_resize_self((unsigned int)ui.w, (unsigned int)want) == 0) { resize_sent = want; resize_pending = 1; } } } static void toggle_expand(int which) { expanded = expanded == which ? EXPAND_NONE : which; main_scroll.offset = 0; if (expanded == EXPAND_DATE) { load_datetime_fields(0); ngui_set_focus(&ui, ID_DATE_FIELD); } else { ngui_set_focus(&ui, 0); } ngui_invalidate(&ui); } static void draw_main(void) { struct ngui_rect body = pop.body; struct ngui_rect card, bright, area; int content_h, y; card = ngui_cut_top(&body, 56); draw_summary(card); body.y += MENU_GAP; body.h -= MENU_GAP; if (brightness >= 0) { bright = ngui_cut_top(&body, 52); draw_brightness(bright); body.y += MENU_GAP; body.h -= MENU_GAP; } content_h = SECTION_H + quick_tile_height(quick_tile_count()); if (expanded == EXPAND_NETWORK) content_h += MENU_GAP + network_content_height(); else if (expanded == EXPAND_POWER) content_h += MENU_GAP + power_content_height(); else if (expanded == EXPAND_DATE) content_h += MENU_GAP + date_content_height(); main_scroll.content_h = content_h; area = body; ngui_scroll_begin(&ui, area, &main_scroll); ngui_scroll_wheel(&ui, area, &main_scroll); ngui_scroll_keys(&ui, &main_scroll); y = area.y - main_scroll.offset; { int ty = y + (SECTION_H - 17) / 2; ngui_text_px(&ui, area.x, ty, "Quick settings", slen("Quick settings"), ui.theme.text_dim, 14, 1, 0, 0); } y += SECTION_H; draw_tiles(area, &y); if (expanded == EXPAND_NETWORK) { y += MENU_GAP; draw_network_section(area, &y); } else if (expanded == EXPAND_POWER) { y += MENU_GAP; draw_power_section(area, &y); } else if (expanded == EXPAND_DATE) { y += MENU_GAP; draw_date_section(area, &y); } ngui_scroll_end(&ui, area, &main_scroll); } static void draw_pass(void) { struct ngui_rect body = pop.body; struct ngui_rect r, b1, b2; char line[80], *p; int ev; r = ngui_cut_top(&body, 24); p = sput(line, "Join \""); 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_px(&ui, r.x, r.y, "WPA2-PSK passphrase", 19, ui.theme.text_dim, 14, 1, 0, 0); r = ngui_cut_top(&body, 34); 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 += MENU_GAP; body.h -= MENU_GAP; 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); body.y += MENU_GAP; body.h -= MENU_GAP; r = ngui_cut_top(&body, 28); b1 = r; b1.w = 110; b2 = r; b2.x = r.x + 120; b2.w = 110; if (ngui_button(&ui, ID_PASS_OK, b1, "Connect")) actions |= ACT_CONNECT; if (ngui_button(&ui, ID_PASS_CANCEL, b2, "Cancel")) actions |= ACT_BACK; } static void draw_connect(void) { struct ngui_rect body = pop.body; struct ngui_rect log, b, r; char line[96], *p; unsigned int start = 0, lines = 0, i; int y, line_y = 0; r = ngui_cut_top(&body, 22); p = sput(line, connect_pid > 0 ? "Connecting to \"" : "Connection to \""); p = sput(p, sel_ssid); p = sput(p, "\""); *p = '\0'; ngui_text_ellipsis_utf8(&ui, r, r.x, r.y, line, ui.theme.text); /* The supplicant trail in a recessed card; the connection state is in * the popover footer. */ log = body; log.h -= 38; ngui_fill_round_rect(&ui, log, 6, 6, 6, 6, ui.theme.panel); ngui_push_clip(&ui, (struct ngui_rect){ log.x + 8, log.y + 8, log.w - 16, log.h - 16 }); for (i = conn_log_len; i > 0; i--) { if (conn_log[i - 1] == '\n' && ++lines > 12) { start = i; break; } } y = log.y + 10; for (i = start; i < conn_log_len && y + 17 <= log.y + log.h - 8; i++) { char c = conn_log[i]; if (c == '\r') continue; if (c == '\n') { line[line_y] = '\0'; if (line_y) ngui_text_px(&ui, log.x + 10, y, line, (unsigned int)line_y, ui.theme.text_dim, 14, 0, 0, 1); y += 17; 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_px(&ui, log.x + 10, y, line, (unsigned int)line_y, ui.theme.text_dim, 14, 0, 0, 1); } ngui_pop_clip(&ui); b.x = body.x; b.y = body.y + body.h - 28; b.w = 110; b.h = 28; if (ngui_button(&ui, ID_CONN_CANCEL, b, connect_pid > 0 ? "Cancel" : "Back")) actions |= ACT_BACK; } static void draw_saved(void) { struct ngui_rect body = pop.body; struct ngui_rect back, row, bt, list, r; struct ngui_row desc; char ssid[33]; int i; r = ngui_cut_top(&body, 22); ngui_text_px(&ui, r.x, r.y, "Saved Wi-Fi networks", 20, ui.theme.text, 14, 1, 0, 0); if (!nsaved) { ngui_text(&ui, body.x + 2, body.y + 4, "No saved networks", ui.theme.text_dim); } else { list = body; list.h -= 38; saved_scroll.content_h = nsaved * SAVED_LIST_ROW_H; ngui_scroll_begin(&ui, list, &saved_scroll); ngui_scroll_wheel(&ui, list, &saved_scroll); ngui_scroll_keys(&ui, &saved_scroll); for (i = 0; i < nsaved; i++) { row.x = list.x; row.y = list.y - saved_scroll.offset + i * SAVED_LIST_ROW_H; row.w = list.w; row.h = SAVED_LIST_ROW_H; if (row.y + row.h <= list.y || row.y >= list.y + list.h) continue; cfg_ssid_name(&saved[i], ssid); bt = row; bt.x = row.x + row.w - 64; bt.w = 60; bt.h = 28; bt.y = row.y + 7; row.w -= 76; desc.title = ssid; desc.subtitle = saved[i].pass_len ? "WPA2-PSK" : "Open"; desc.trailing = 0; desc.bars = -1; desc.selected = 0; ngui_list_row_utf8(&ui, ID_SAVED_ROW_BASE + i, row, &desc); if (ngui_button(&ui, ID_SAVED_BASE + i, bt, "Forget")) { action_saved = i; actions |= ACT_FORGET; } } ngui_scroll_end(&ui, list, &saved_scroll); } back.x = body.x; back.y = body.y + body.h - 28; back.w = 100; back.h = 28; if (ngui_button(&ui, ID_SAVED_BACK, back, "Back")) actions |= ACT_BACK; } static void draw(void) { ngui_popover_begin(&ui, &pop); if (view == VIEW_PASS) draw_pass(); else if (view == VIEW_CONNECT) draw_connect(); else if (view == VIEW_SAVED) draw_saved(); else draw_main(); draw_status(); } static void paint_frame(void) { if (ngui_begin(&ui)) { draw(); ngui_end(&ui); } actions = 0; action_dev = -1; action_bss = -1; action_saved = -1; } void _start(const char *arg) { struct win_info wi; unsigned int last_status = 0; int w, h; (void)arg; u_screen_size(&screen_w, &screen_h); view = VIEW_MAIN; expanded = EXPAND_NONE; main_scroll.offset = 0; main_scroll.hide_bar = 1; saved_scroll.hide_bar = 1; load_devices(); load_status(); load_saved(); w = panel_width(); h = desired_panel_height(); if (u_win_create_popover((unsigned int)w, (unsigned int)h, "Control Center", &wi) != 0 && u_win_create_popover((unsigned int)(w > 400 ? 400 : w), (unsigned int)(h > 540 ? 540 : h), "Control Center", &wi) != 0 && u_win_create_popover((unsigned int)(w > 380 ? 380 : w), (unsigned int)(h > 460 ? 460 : h), "Control Center", &wi) != 0 && u_win_create_popover(340, 360, "Control Center", &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"; ntp_in.placeholder = "pool.ntp.org"; if (wifi_present && wst.state != SYS_WIFI_ST_DOWN && wst.state < SYS_WIFI_ST_ASSOC) { start_scan(); paint_frame(); } for (;;) { static int first_frame = 1; 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); } resize_panel(); if (ngui_begin(&ui)) { draw(); ngui_end(&ui); if (first_frame) { /* The first present can race the WM's initial * mapping of the popover canvas; paint a second * frame once so the panel never shows a black * tail. */ first_frame = 0; ngui_invalidate(&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 (view == VIEW_SAVED) { view = VIEW_MAIN; set_status("", 0); } else if (expanded != EXPAND_NONE) { toggle_expand(expanded); } else if (ui.focus == 0) { break; } } u_sleep_ms(25); } ngui_font_detach(&ui); u_win_close(); u_exit(0); }