/* settings: the real Settings window. * * A normal titled canvas window with a section sidebar on the left and the * selected page on the right: * * Network NIC selection, DHCP, Wi-Fi scan/join/saved networks and the * current connection details * Power P-state mode, panel brightness and AC/battery state * Date & Time CMOS clock editor and SNTP sync * Appearance desktop colour and a wallpaper image picker * Windows window shadows, edge snapping and drag resistance * About version, screen, memory and temperature * * The top-bar control-center popover's pills call SYS_WM_OPEN_SETTINGS, but * a running Settings window is reused: the WM focuses it and writes the * requested section to WM_SETTINGS_REQUEST, which this app polls. Window * preferences are pushed through SYS_WM_PREFS and persisted in * /data/SETTINGS.CFG (or /share/settings.cfg before the data volume is * mounted), so the ring-3 WM applies them immediately and after a restart. */ #include #include #include "ucmd_lib.h" #include "ngui.h" #include "img.h" #include "img_file.h" #include "wifi_store.h" #include "version.h" #define MAX_DEVS 8 #define MAX_BSS 32 #define SIDEBAR_W 190 #define SIDEBAR_MIN_W 154 #define NAV_ROW_H 44 #define NAV_GAP 4 #define HEADER_H 54 #define FOOTER_H 28 #define SECTION_H 28 #define GAP 10 #define CARD_H 64 #define DEV_ROW_H 42 #define BSS_ROW_H 42 #define INFO_ROW_H 30 #define SAVED_ROW_H 42 #define STATUS_MS 6000 #define PICK_MAX 96 #define PICK_ROW_H 38 #define PICK_PATH_MAX 256 #define WALL_FILE_CAP (4u * 1024u * 1024u) #define WALL_HEAP_CAP (24u * 1024u * 1024u) #define ID_NAV_BASE 10 #define ID_NET_REFRESH 100 #define ID_NET_SCAN 101 #define ID_NET_DISCONNECT 102 #define ID_NET_SAVED 103 #define ID_NET_SAVED_BACK 104 #define ID_NET_PASS_INPUT 105 #define ID_NET_PASS_OK 106 #define ID_NET_PASS_CANCEL 107 #define ID_NET_CONN_CANCEL 108 #define ID_DEV_BASE 120 #define ID_BSS_BASE 140 #define ID_INFO_BASE 180 #define ID_SAVED_ROW_BASE 200 #define ID_SAVED_FORGET_BASE 230 #define ID_POWER_MODE_BASE 300 #define ID_BRIGHTNESS 310 #define ID_DATE_FIELD 400 #define ID_TIME_FIELD 401 #define ID_NTP_FIELD 402 #define ID_DATE_SET 403 #define ID_DATE_NOW 404 #define ID_DATE_NTP 405 #define ID_BG_BASE 500 #define ID_WALL_CHOOSE 540 #define ID_WALL_REMOVE 541 #define ID_PICK_UP 550 #define ID_PICK_CANCEL 551 #define ID_PICK_ROW_BASE 600 #define ID_WIN_SHADOW 750 #define ID_WIN_SNAP 751 #define ID_WIN_RESIST 752 enum { SEC_NETWORK, SEC_POWER, SEC_DATETIME, SEC_APPEARANCE, SEC_WINDOWS, SEC_ABOUT, SEC_COUNT }; enum { NET_MAIN, NET_PASS, NET_CONNECT, NET_SAVED }; enum { A_NONE = 0, A_NAV, A_REFRESH, A_SCAN, A_DISCONNECT, A_DEV, A_BSS, A_SAVED, A_SAVED_BACK, A_FORGET, A_PASS_OK, A_PASS_CANCEL, A_CONN_CANCEL, A_POWER, A_DATE_SET, A_DATE_NOW, A_DATE_NTP, A_BG, A_WALL_CHOOSE, A_WALL_REMOVE, A_PICK_UP, A_PICK_CANCEL, A_PICK_ROW, A_WIN_SHADOW, A_WIN_SNAP, A_WIN_RESIST }; struct section_info { const char *label; const char *subtitle; const char *arg; }; static const struct section_info sections[SEC_COUNT] = { { "Network", "Devices and Wi-Fi", "network" }, { "Power", "Mode and brightness", "power" }, { "Date & Time", "Clock and time sync", "time" }, { "Appearance", "Desktop background", "appearance" }, { "Windows", "Shadows and snapping", "windows" }, { "About", "System information", "about" } }; struct bg_choice { const char *name; unsigned int rgb; }; static const struct bg_choice bg_choices[] = { { "Midnight", 0x202030u }, { "Deep blue", 0x152238u }, { "Forest", 0x18281Cu }, { "Plum", 0x2A1A2Eu }, { "Cocoa", 0x2C2118u }, { "Graphite", 0x222428u }, { "Slate", 0x23303Au }, { "Wine", 0x301A22u } }; #define NBG ((int)(sizeof(bg_choices) / sizeof(bg_choices[0]))) static struct ngui ui; static int cur_section = SEC_NETWORK; static int quit; static int pending_action; static int pending_arg; static struct ngui_scroll content_scroll; static struct ngui_slider brightness_slider; static char status[160]; static int status_error; static unsigned int status_until; static unsigned int last_status_ms; static unsigned int last_request_ms; static struct wm_prefs prefs; /* Network state. */ static struct sys_netdev devs[MAX_DEVS]; static int ndev; static int devs_loaded; 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 net_view = NET_MAIN; 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[2048]; static unsigned int conn_log_len; static struct ngui_input pass_in; /* Power state. */ static int brightness = -1; static int cpufreq_mode = -1; static struct sys_powerinfo powerinfo; static int power_ok; /* Clock state. */ static struct sys_datetime now; static int now_ok; static int date_loaded; static struct ngui_input date_in; static struct ngui_input time_in; static struct ngui_input ntp_in; /* Appearance state. */ static int wallpaper_present; static char wallpaper_source[32]; static int picker_mode; static char pick_path[PICK_PATH_MAX]; static char pick_names[PICK_MAX][64]; static unsigned int pick_sizes[PICK_MAX]; static unsigned char pick_dirs[PICK_MAX]; static int pick_count; static struct ngui_scroll picker_scroll; static unsigned int s_len(const char *s) { unsigned int n = 0; while (s && s[n]) n++; return n; } static void settings_draw_frame(void); static void draw_picker(struct ngui_rect area, int *yy); static void set_wallpaper_file(const char *path); static int s_eq(const char *a, const char *b) { while (*a && *a == *b) { a++; b++; } return *a == *b; } static void s_copy(char *d, const char *s, unsigned int cap) { unsigned int i = 0; if (!cap) return; while (s && s[i] && i + 1 < cap) { d[i] = s[i]; i++; } d[i] = '\0'; } static char *s_put(char *p, const char *s) { while (s && *s) *p++ = *s++; *p = '\0'; return p; } static char *s_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 *s_put_2(char *p, unsigned int v) { *p++ = (char)('0' + (v / 10) % 10); *p++ = (char)('0' + v % 10); return p; } 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() + STATUS_MS : 0; ngui_invalidate(&ui); } static void request_action(int action, int arg) { if (!pending_action) { pending_action = action; pending_arg = arg; } } /* Paint the current state before a blocking action (DHCP, a WiFi scan or an * NTP request), so the message and the pressed control are visible. */ static void paint_current(void) { if (ngui_begin(&ui)) { settings_draw_frame(); ngui_end(&ui); } } static int section_from_name(const char *name) { int i; if (!name || !name[0]) return SEC_NETWORK; for (i = 0; i < SEC_COUNT; i++) if (s_eq(name, sections[i].arg)) return i; return SEC_NETWORK; } /* ------------------------------------------------------------------ */ /* Window-manager preferences */ static void prefs_defaults(void) { memset(&prefs, 0, sizeof(prefs)); prefs.shadow = 1; prefs.snap = 1; prefs.resist = 1; prefs.bg_rgb = WM_DEFAULT_BG_RGB; } static int prefs_file_magic_ok(const struct wm_prefs_file *f) { unsigned int i; for (i = 0; i < sizeof(f->magic); i++) if (f->magic[i] != WM_PREFS_MAGIC[i]) return 0; return 1; } static int prefs_file_load(void) { /* A FAT read may write a final partial sector past the requested * length, so keep a sector of slack around the header. */ unsigned char buf[sizeof(struct wm_prefs_file) + 512]; struct wm_prefs_file *f = (struct wm_prefs_file *)buf; const char *paths[2] = { WM_PREFS_DATA, WM_PREFS_SHARE }; int i; for (i = 0; i < 2; i++) { int n = u_syscall3(SYS_READ_FILE, (int)paths[i], (int)buf, (int)sizeof(*f)); if (n < (int)sizeof(*f) || !prefs_file_magic_ok(f) || f->version != WM_PREFS_VERSION) continue; prefs = f->prefs; prefs.shadow = prefs.shadow ? 1u : 0u; prefs.snap = prefs.snap ? 1u : 0u; prefs.resist = prefs.resist ? 1u : 0u; prefs.bg_rgb &= 0x00FFFFFFu; memset(prefs.reserved, 0, sizeof(prefs.reserved)); return 0; } return -1; } static int prefs_file_save(void) { struct wm_prefs_file f; struct sys_stat st; const char *path = WM_PREFS_SHARE; memset(&f, 0, sizeof(f)); memcpy(f.magic, WM_PREFS_MAGIC, sizeof(f.magic)); f.version = WM_PREFS_VERSION; f.prefs = prefs; if (u_stat("/data", &st) == 0 && st.is_dir) path = WM_PREFS_DATA; return u_syscall3(SYS_WRITE_FILE, (int)path, (int)&f, (int)sizeof(f)); } static void commit_prefs(const char *ok_msg) { if (u_wm_set_prefs(&prefs) != 0) { set_status("The window manager refused the settings", 1); return; } if (prefs_file_save() < 0) set_status("Applied, but the settings file could not be saved", 1); else if (ok_msg && ok_msg[0]) set_status(ok_msg, 0); else set_status("Settings saved", 0); } /* ------------------------------------------------------------------ */ /* Small drawing helpers */ static unsigned short rgb565(unsigned int rgb) { return (unsigned short)((((rgb >> 16) & 0xFF) >> 3) << 11 | (((rgb >> 8) & 0xFF) >> 2) << 5 | ((rgb & 0xFF) >> 3)); } static void format_size(char *b, unsigned int bytes) { char *p = b; if (bytes >= 1024u * 1024u) { p = s_put_dec(p, bytes / (1024u * 1024u)); p = s_put(p, " MB"); } else if (bytes >= 1024u) { p = s_put_dec(p, (bytes + 1023u) / 1024u); p = s_put(p, " KB"); } else { p = s_put_dec(p, bytes); p = s_put(p, " B"); } *p = '\0'; } /* One clickable settings row: an optional colour swatch, a title, a dim * subtitle and a right-aligned value. Mirrors the ngui list-row press / * release semantics so clicks agree with the rest of the toolkit. */ static int settings_row(unsigned int id, struct ngui_rect r, const char *title, const char *subtitle, const char *trailing, int selected, int swatch_rgb) { int hot = ui.inside && ngui_rect_contains(&r, ui.mouse_x, ui.mouse_y); int press_hot = ui.pressed && ngui_rect_contains(&r, ui.press_x, ui.press_y); int release_hot = ngui_rect_contains(&r, ui.release_x, ui.release_y); int fired = 0; int left = r.x + 10, right = r.x + r.w - 10; int title_h, block_h, ty; unsigned short bg = 0, fg, sub_fg, trail_fg; if (press_hot) ui.active = id; if (ui.pressed && ui.released && press_hot && release_hot) fired = 1; if (ui.released && ui.active == id) { if (release_hot) fired = 1; ui.active = 0; } if (selected) bg = ui.theme.selection; else if (hot || ui.active == id) bg = ui.theme.panel_alt; if (bg) ngui_fill_round_rect(&ui, r, NGUI_RADIUS, NGUI_RADIUS, NGUI_RADIUS, NGUI_RADIUS, bg); if (swatch_rgb >= 0) { struct ngui_rect sw = { left, r.y + (r.h - 16) / 2, 16, 16 }; ngui_fill_round_rect(&ui, sw, 4, 4, 4, 4, rgb565((unsigned int)swatch_rgb)); ngui_frame_round(&ui, sw, 4, 4, 4, 4, 1, ui.theme.border); left += 26; } fg = ui.theme.text; sub_fg = selected ? ui.theme.text : ui.theme.text_dim; trail_fg = selected ? ui.theme.text : ui.theme.text_dim; if (trailing && trailing[0]) { int tw = (int)ngui_text_width_ui(&ui, trailing); right -= tw + 8; ngui_text_utf8(&ui, right + 8, r.y + (r.h - 16) / 2, trailing, trail_fg); } if (right <= left) return fired; title_h = ngui_text_line_height(&ui, ui.font_px, 0); block_h = subtitle && subtitle[0] ? title_h * 2 - 2 : title_h; ty = r.y + (r.h - block_h) / 2; if (ty < r.y) ty = r.y; ngui_push_clip(&ui, (struct ngui_rect){ left, r.y, right - left, r.h }); ngui_text_utf8(&ui, left, ty, title, fg); if (subtitle && subtitle[0]) ngui_text_utf8(&ui, left, ty + title_h - 2, subtitle, sub_fg); ngui_pop_clip(&ui); if (hot) { ui.hot = id; ngui_set_cursor(&ui, CURSOR_POINTER); } return fired; } static int info_row(struct ngui_rect area, int *y, const char *label, const char *value) { struct ngui_rect r = { area.x, *y, area.w, INFO_ROW_H }; int th = ngui_text_line_height(&ui, ui.font_px, 0); ngui_text_utf8(&ui, r.x + 10, r.y + (r.h - th) / 2, label, ui.theme.text_dim); ngui_text_utf8(&ui, r.x + r.w / 2, r.y + (r.h - th) / 2, value, ui.theme.text); *y += INFO_ROW_H; return 0; } /* ------------------------------------------------------------------ */ /* Network */ 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 load_saved(void) { int n = wifi_store_load(saved, WIFI_CFG_MAX); if (n > WIFI_CFG_MAX) n = 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 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 { wifi_checked = 1; wifi_present = 0; } } for (i = 0; i < ndev; i++) { devs[i].active = (net_present && s_eq(net.name, devs[i].name)) ? 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_devices(void) { int n = u_net_devlist(devs, MAX_DEVS); if (n > MAX_DEVS) n = MAX_DEVS; ndev = n > 0 ? n : 0; devs_loaded = 1; load_status(); } 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; } 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 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] = (char)b->ssid[i]; out[n] = '\0'; if (!n) { s_copy(out, "(hidden)", 33); } } 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 start_scan(void) { int r; if (scanning || !wifi_present) return; set_status("Scanning for WiFi networks...", 0); paint_current(); r = u_wifi(SYS_WIFI_SCAN, 0, 0); if (r < 0) { char m[64], *p; u_wifi(SYS_WIFI_STATUS, (int)&wst, 0); p = s_put(m, "WiFi: "); p = s_put(p, wst.detail[0] ? wst.detail : "scan failed"); *p = '\0'; set_status(m, 1); return; } scanning = 1; scan_start = u_ticks_ms(); nbss = 0; content_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; } if (n > MAX_BSS) n = MAX_BSS; 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 = s_len(pass); int r; memset(&cfg, 0, sizeof(cfg)); 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)); memset(&cfg, 0, sizeof(cfg)); return r; } static void start_connect(const char *pass) { struct sys_wifi_bss *b; char msg[96], *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_detached("/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; net_view = NET_CONNECT; p = s_put(msg, "Connecting to \""); p = s_put(p, sel_ssid); p = s_put(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); net_view = NET_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; net_view = NET_MAIN; if (net_present && s_eq(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 { 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 = s_put(msg, "Selecting "); p = s_put(p, devs[i].name); *p = '\0'; set_status(msg, 0); paint_current(); if (u_net_select(devs[i].name) != 0) { set_status("Cannot select that netdev (busy or unavailable)", 1); return; } set_status("Running DHCP...", 0); 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"; net_view = NET_PASS; set_status("", 0); ngui_set_focus(&ui, ID_NET_PASS_INPUT); ngui_invalidate(&ui); return; } start_connect(""); } 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 || (s_eq(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, NGUI_RADIUS, NGUI_RADIUS, NGUI_RADIUS, NGUI_RADIUS, ui.theme.panel); ngui_frame_round(&ui, r, NGUI_RADIUS, NGUI_RADIUS, NGUI_RADIUS, NGUI_RADIUS, 1, ui.theme.panel_alt); ngui_fill_round_rect(&ui, dot, 5, 5, 5, 5, dot_color); p = line; if (!net_present) p = s_put(p, "Offline"); else if (s_eq(net.name, "wifi")) p = s_put(p, wst.state >= SYS_WIFI_ST_LINK ? "Wi-Fi connected" : "Wi-Fi"); else if (s_eq(net.name, "rndis")) p = s_put(p, "USB tethering"); else p = s_put(p, "Wired network"); *p = '\0'; ngui_text_px(&ui, r.x + 34, r.y + 10, line, s_len(line), ui.theme.text, 14, 1, 0, 0); p = line; if (net_present) { p = s_put(p, net.name); p = s_put(p, " \xC2\xB7 "); if (have_ip) { p = s_put_dec(p, net.ip[0]); *p++ = '.'; p = s_put_dec(p, net.ip[1]); *p++ = '.'; p = s_put_dec(p, net.ip[2]); *p++ = '.'; p = s_put_dec(p, net.ip[3]); } else { p = s_put(p, "no IP"); } if (s_eq(net.name, "wifi") && wst.state >= SYS_WIFI_ST_ASSOC) { p = s_put(p, " \xC2\xB7 "); for (i = 0; i < wst.ssid_len && i < 32; i++) *p++ = (char)wst.ssid[i]; p = s_put(p, " "); if (wst.rssi < 0) { *p++ = '-'; p = s_put_dec(p, (unsigned int)(-wst.rssi)); } else { p = s_put_dec(p, (unsigned int)wst.rssi); } p = s_put(p, " dBm"); } } else { p = s_put(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_net_main(struct ngui_rect area, int *yy) { struct ngui_row row; struct ngui_rect r; char ssid[33], sub[80], *p, value[48]; int i, shown = 0, more, n, y = *yy; r = (struct ngui_rect){ area.x, y, area.w, SECTION_H }; ngui_section(&ui, r, "Connection", 0, 0); y += SECTION_H; r = (struct ngui_rect){ area.x, y, area.w, CARD_H }; draw_status_card(r); y += CARD_H + 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_NET_REFRESH, "Refresh")) request_action(A_REFRESH, 0); 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)) request_action(A_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_utf8(&ui, area.x + 12, y + 12, msg, ui.theme.text_dim); y += DEV_ROW_H; } else if (more) { char line[32], *lp = line; lp = s_put(lp, "+"); lp = s_put_dec(lp, (unsigned int)more); lp = s_put(lp, " more devices"); *lp = '\0'; ngui_text_utf8(&ui, area.x + 12, y + 2, line, ui.theme.text_dim); y += 20; } y += 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_NET_DISCONNECT, "Disconnect")) request_action(A_DISCONNECT, 0); } else if (wifi_present && !scanning) { if (ngui_section(&ui, r, "Wi-Fi", ID_NET_SCAN, "Scan")) request_action(A_SCAN, 0); } else { ngui_section(&ui, r, "Wi-Fi", 0, 0); } y += SECTION_H; if (!wifi_present) { ngui_text_utf8(&ui, area.x + 12, y + 12, "No Wi-Fi card detected", ui.theme.text_dim); y += BSS_ROW_H; } else if (scanning) { ngui_text_utf8(&ui, area.x + 12, y + 12, "Scanning for networks...", ui.theme.accent); y += BSS_ROW_H; } else 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_utf8(&ui, area.x + 12, y + 12, msg, ui.theme.text_dim); y += BSS_ROW_H; } else { for (i = 0; i < nbss; i++) { struct sys_wifi_bss *b = &bss[i]; struct ngui_rect br = { area.x, y, area.w, BSS_ROW_H }; if (br.y + br.h > area.y && br.y < area.y + area.h) { ssid_name(b, ssid); p = sub; p = s_put(p, security_name(b->security)); if (bss_saved(b) >= 0) p = s_put(p, " \xC2\xB7 saved"); if (bss_is_active(b)) p = s_put(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, br, &row)) request_action(A_BSS, i); } y += BSS_ROW_H; } } if (net_present) { static const char *labels[4] = { "IP address", "Gateway", "DNS", "MAC address" }; int di; y += GAP; r = (struct ngui_rect){ area.x, y, area.w, SECTION_H }; ngui_section(&ui, r, "Details", 0, 0); y += SECTION_H; for (di = 0; di < 4; di++) { char *vp = value; if (di == 0) { if (net.ip[0] || net.ip[1] || net.ip[2] || net.ip[3]) { vp = s_put_dec(vp, net.ip[0]); *vp++ = '.'; vp = s_put_dec(vp, net.ip[1]); *vp++ = '.'; vp = s_put_dec(vp, net.ip[2]); *vp++ = '.'; vp = s_put_dec(vp, net.ip[3]); } else { vp = s_put(vp, "not configured"); } } else if (di == 1) { if (net.gw[0] || net.gw[1] || net.gw[2] || net.gw[3]) { vp = s_put_dec(vp, net.gw[0]); *vp++ = '.'; vp = s_put_dec(vp, net.gw[1]); *vp++ = '.'; vp = s_put_dec(vp, net.gw[2]); *vp++ = '.'; vp = s_put_dec(vp, net.gw[3]); } else { vp = s_put(vp, "-"); } } else if (di == 2) { if (net.dns[0] || net.dns[1] || net.dns[2] || net.dns[3]) { vp = s_put_dec(vp, net.dns[0]); *vp++ = '.'; vp = s_put_dec(vp, net.dns[1]); *vp++ = '.'; vp = s_put_dec(vp, net.dns[2]); *vp++ = '.'; vp = s_put_dec(vp, net.dns[3]); } else { vp = s_put(vp, "-"); } } else { static const char hex[] = "0123456789abcdef"; int k; for (k = 0; k < 6; k++) { *vp++ = hex[net.mac[k] >> 4]; *vp++ = hex[net.mac[k] & 15]; if (k != 5) *vp++ = ':'; } } *vp = '\0'; r = (struct ngui_rect){ area.x, y, area.w, INFO_ROW_H }; 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 += INFO_ROW_H; } } if (nsaved) { char nbuf[12], *np = nbuf; y += 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 = s_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_NET_SAVED, r, &row)) request_action(A_SAVED, 0); } y += SAVED_ROW_H; } *yy = y; } static void draw_net_pass(struct ngui_rect area, int *yy) { struct ngui_rect r; char line[96], *p; int ev, y = *yy; r = (struct ngui_rect){ area.x, y, area.w, 24 }; p = s_put(line, "Join \""); p = s_put(p, sel_ssid); p = s_put(p, "\""); *p = '\0'; ngui_text_ellipsis_utf8(&ui, r, r.x, r.y, line, ui.theme.text); y += 28; r = (struct ngui_rect){ area.x, y, area.w, 20 }; ngui_text_px(&ui, r.x, r.y, "WPA2-PSK passphrase", 19, ui.theme.text_dim, 14, 1, 0, 0); y += 20; r = (struct ngui_rect){ area.x, y, area.w, 34 }; ev = ngui_text_input(&ui, ID_NET_PASS_INPUT, r, &pass_in); if (ev & NGUI_INPUT_SUBMIT) request_action(A_PASS_OK, 0); if (ev & NGUI_INPUT_CANCEL) request_action(A_PASS_CANCEL, 0); y += 34 + GAP; ngui_text_utf8(&ui, area.x, y, "8-63 characters, or 64 hex digits for a raw PSK.", ui.theme.text_dim); y += 24 + GAP; r = (struct ngui_rect){ area.x, y, 110, 28 }; if (ngui_button(&ui, ID_NET_PASS_OK, r, "Connect")) request_action(A_PASS_OK, 0); r.x += 120; if (ngui_button(&ui, ID_NET_PASS_CANCEL, r, "Cancel")) request_action(A_PASS_CANCEL, 0); y += 28; *yy = y; } static void draw_net_connect(struct ngui_rect area, int *yy) { struct ngui_rect log, b, r; char line[96], *p; unsigned int start = 0, lines = 0, i; int y = *yy, line_y = 0; r = (struct ngui_rect){ area.x, y, area.w, 22 }; p = s_put(line, connect_pid > 0 ? "Connecting to \"" : "Connection to \""); p = s_put(p, sel_ssid); p = s_put(p, "\""); *p = '\0'; ngui_text_ellipsis_utf8(&ui, r, r.x, r.y, line, ui.theme.text); y += 28; log = (struct ngui_rect){ area.x, y, area.w, 240 }; ngui_fill_round_rect(&ui, log, NGUI_RADIUS, NGUI_RADIUS, NGUI_RADIUS, NGUI_RADIUS, 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); y = log.y + log.h + GAP; b = (struct ngui_rect){ area.x, y, 110, 28 }; if (ngui_button(&ui, ID_NET_CONN_CANCEL, b, connect_pid > 0 ? "Cancel" : "Back")) request_action(A_CONN_CANCEL, 0); y += 28; *yy = y; } static void draw_net_saved(struct ngui_rect area, int *yy) { struct ngui_rect r, bt; struct ngui_row desc; char ssid[33]; int i, y = *yy; r = (struct ngui_rect){ area.x, y, area.w, 22 }; ngui_text_px(&ui, r.x, r.y, "Saved Wi-Fi networks", 20, ui.theme.text, 14, 1, 0, 0); y += 28; if (!nsaved) { ngui_text_utf8(&ui, area.x, y, "No saved networks", ui.theme.text_dim); y += 24; } else { for (i = 0; i < nsaved; i++) { 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) { cfg_ssid_name(&saved[i], ssid); bt = r; bt.x = r.x + r.w - 74; bt.w = 64; bt.h = 28; bt.y = r.y + 7; r.w -= 84; 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, r, &desc); if (ngui_button(&ui, ID_SAVED_FORGET_BASE + i, bt, "Forget")) request_action(A_FORGET, i); } y += SAVED_ROW_H; } } r = (struct ngui_rect){ area.x, y, 100, 28 }; if (ngui_button(&ui, ID_NET_SAVED_BACK, r, "Back")) request_action(A_SAVED_BACK, 0); y += 28; *yy = y; } static void draw_network(struct ngui_rect area, int *yy) { if (net_view == NET_PASS) draw_net_pass(area, yy); else if (net_view == NET_CONNECT) draw_net_connect(area, yy); else if (net_view == NET_SAVED) draw_net_saved(area, yy); else draw_net_main(area, yy); } /* ------------------------------------------------------------------ */ /* Power */ 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 void set_power_mode(int mode) { int got = u_cpufreq_set(mode); char msg[48], *p; if (got < 0) { set_status("Power mode is not available", 1); return; } cpufreq_mode = got; p = s_put(msg, "Power mode: "); p = s_put(p, power_mode_name(got)); *p = '\0'; set_status(msg, 0); ngui_invalidate(&ui); } static void draw_power(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 }; struct ngui_rect r; struct ngui_row row; int i, y = *yy; int batt = -1, charging = 0, discharging = 0, ac = -1; char value[48], *p; 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_MODE_BASE + i, r, &row)) request_action(A_POWER, modes[i]); } y += 42; } y += GAP; ngui_text_utf8(&ui, area.x + 10, y, "Changes the P-state governor immediately.", ui.theme.text_dim); y += 22; if (brightness >= 0) { struct ngui_rect slider; int v = brightness; char b[8], *bp = b; y += GAP; r = (struct ngui_rect){ area.x, y, area.w, SECTION_H }; ngui_section(&ui, r, "Brightness", 0, 0); y += SECTION_H; bp = s_put_dec(bp, (unsigned int)brightness); *bp++ = '%'; *bp = '\0'; ngui_text_utf8(&ui, area.x + 10, y, b, ui.theme.text); y += 20; slider = (struct ngui_rect){ area.x + 10, y, area.w - 20, 16 }; if (ngui_slider(&ui, ID_BRIGHTNESS, &brightness_slider, slider, &v, 5, 100)) { int got = u_backlight_set(v); brightness = got >= 0 ? got : v; } y += 24; } 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; } } y += GAP; r = (struct ngui_rect){ area.x, y, area.w, SECTION_H }; ngui_section(&ui, r, "Power source", 0, 0); y += SECTION_H; if (!power_ok) { ngui_text_utf8(&ui, area.x + 10, y + 8, "No battery or AC information on this machine.", ui.theme.text_dim); y += 32; } else { p = value; if (ac > 0) p = s_put(p, "AC online"); else if (ac == 0) p = s_put(p, "Battery"); else p = s_put(p, "Unknown"); *p = '\0'; info_row(area, &y, "Source", value); p = value; if (batt >= 0) { p = s_put_dec(p, (unsigned int)batt); *p++ = '%'; if (charging) p = s_put(p, " charging"); else if (discharging) p = s_put(p, " discharging"); } else { p = s_put(p, "not present"); } *p = '\0'; info_row(area, &y, "Battery", value); } *yy = y; } /* ------------------------------------------------------------------ */ /* Date & time */ 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 (s_len(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 = s_len(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 format_date(char *b, const struct sys_datetime *dt) { char *p = s_put_dec(b, dt->year); *p++ = '-'; p = s_put_2(p, dt->month); *p++ = '-'; p = s_put_2(p, dt->day); *p = '\0'; } static void format_time_sec(char *b, const struct sys_datetime *dt) { char *p = s_put_2(b, dt->hour); *p++ = ':'; p = s_put_2(p, dt->minute); *p++ = ':'; p = s_put_2(p, dt->second); *p = '\0'; } 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 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_current(); 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 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 + 10, *yy, area.w - 20, 18 }; ngui_text_utf8(&ui, r.x, r.y, label, ui.theme.text_dim); *yy += 18; r = (struct ngui_rect){ area.x + 10, *yy, area.w - 20, 32 }; ev = ngui_text_input(&ui, id, r, in); if (ev & NGUI_INPUT_SUBMIT) request_action(id == ID_NTP_FIELD ? A_DATE_NTP : A_DATE_SET, 0); 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_datetime(struct ngui_rect area, int *yy) { struct ngui_rect r; char current[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 = current; p = s_put(p, "Current: "); if (now_ok) { format_date(p, &now); p += s_len(p); *p++ = ' '; format_time_sec(p, &now); } else { s_put(p, "unavailable"); } ngui_text_utf8(&ui, area.x + 10, y, current, ui.theme.text_dim); y += 24; 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 + 10, y, 80, 28 }; if (ngui_button(&ui, ID_DATE_SET, r, "Set")) request_action(A_DATE_SET, 0); r.x += 88; if (ngui_button(&ui, ID_DATE_NOW, r, "Now")) request_action(A_DATE_NOW, 0); r.x += 88; r.w = 110; if (ngui_button(&ui, ID_DATE_NTP, r, "Sync NTP")) request_action(A_DATE_NTP, 0); y += 28 + GAP; ngui_text_utf8(&ui, area.x + 10, y, "Sync NTP fetches UTC and writes the RTC.", ui.theme.text_dim); y += 22; ngui_text_utf8(&ui, area.x + 10, y, "Year range: 1970-2069. Enter applies.", ui.theme.text_dim); y += 22; *yy = y; } /* ------------------------------------------------------------------ */ /* Appearance and the wallpaper picker */ static int path_is_file(const char *path) { struct sys_stat st; return u_stat(path, &st) == 0 && !st.is_dir && st.size > 0; } static void wallpaper_refresh(void) { if (path_is_file(WM_WALLPAPER_DATA)) { wallpaper_present = 1; s_copy(wallpaper_source, "/data/WALLPAPR.BIN", sizeof(wallpaper_source)); } else if (path_is_file(WM_WALLPAPER_SHARE)) { wallpaper_present = 1; s_copy(wallpaper_source, "/share/wallpaper", sizeof(wallpaper_source)); } else { wallpaper_present = 0; wallpaper_source[0] = '\0'; } } static int lower_char(int c) { return c >= 'A' && c <= 'Z' ? c + 'a' - 'A' : c; } static int ext_is_image(const char *name) { static const char *const exts[] = { ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tga", ".ppm", ".pgm", ".pbm", ".ico", ".cur", ".pam" }; unsigned int i; int n = (int)s_len(name), e; for (e = n - 1; e >= 0 && name[e] != '.'; e--) ; if (e < 0) return 0; for (i = 0; i < sizeof(exts) / sizeof(exts[0]); i++) { const char *x = exts[i]; int k = 0; while (x[k] && e + k < n && lower_char((unsigned char)name[e + k]) == x[k]) k++; if (!x[k] && e + k == n) return 1; } return 0; } static void trim_name(char *s) { int n = (int)s_len(s); while (n > 0 && (s[n - 1] == ' ' || s[n - 1] == '\t')) s[--n] = '\0'; } static void path_join(char *out, unsigned int cap, const char *base, const char *name) { unsigned int n = s_len(base); int slash = n > 0 && base[n - 1] == '/'; if (n >= cap - 1) { out[0] = '\0'; return; } s_copy(out, base, cap); if (!slash && s_len(out) + 1 < cap) { out[s_len(out)] = '/'; out[s_len(out) + 1] = '\0'; } if (s_len(out) + s_len(name) + 1 < cap) s_copy(out + s_len(out), name, cap - s_len(out)); } static void load_picker_dir(const char *path) { struct sys_dirent d; int i = 0, n; s_copy(pick_path, path && path[0] ? path : "/", sizeof(pick_path)); if (s_len(pick_path) > 1 && pick_path[s_len(pick_path) - 1] == '/') pick_path[s_len(pick_path) - 1] = '\0'; pick_count = 0; picker_scroll.offset = 0; if (s_eq(pick_path, "/")) { /* The VFS root is the mount table, not a directory with * entries; mirror the files app and list the mounts. */ struct sys_mount m; while (pick_count < PICK_MAX && u_syscall2(SYS_LIST_MOUNTS, i++, (int)&m) == 1) { trim_name(m.name); if (!m.name[0]) continue; s_copy(pick_names[pick_count], m.name, 64); pick_sizes[pick_count] = 0; pick_dirs[pick_count] = 1; pick_count++; } } else { while (pick_count < PICK_MAX && (n = u_syscall3(SYS_LIST_DIR, (int)pick_path, i++, (int)&d)) == 1) { char disp[64]; if (d.raw_83) { u_fat_name(d.name, disp, sizeof(disp)); } else { trim_name(d.name); s_copy(disp, d.name, sizeof(disp)); } if (!disp[0] || s_eq(disp, ".") || s_eq(disp, "..")) continue; if (!d.is_dir && !ext_is_image(disp)) continue; s_copy(pick_names[pick_count], disp, 64); pick_sizes[pick_count] = d.size; pick_dirs[pick_count] = d.is_dir ? 1 : 0; pick_count++; } } /* Directories first, then case-insensitive name order. */ for (i = 1; i < pick_count; i++) { int j = i; while (j > 0) { int swap; int a = j - 1, b = j; unsigned int k; if (pick_dirs[a] != pick_dirs[b]) { swap = pick_dirs[b] > pick_dirs[a]; } else { swap = 0; for (k = 0; pick_names[a][k] && pick_names[b][k]; k++) { int ca = lower_char((unsigned char) pick_names[a][k]); int cb = lower_char((unsigned char) pick_names[b][k]); if (ca != cb) { swap = cb < ca; break; } } if (!swap && !pick_names[a][k] && pick_names[b][k]) swap = 1; } if (!swap) break; { char tn[64]; unsigned int ts = pick_sizes[a]; unsigned char td = pick_dirs[a]; s_copy(tn, pick_names[a], sizeof(tn)); s_copy(pick_names[a], pick_names[b], 64); pick_sizes[a] = pick_sizes[b]; pick_dirs[a] = pick_dirs[b]; s_copy(pick_names[b], tn, 64); pick_sizes[b] = ts; pick_dirs[b] = td; } j--; } } } static void pick_parent(char *out, unsigned int cap) { char tmp[PICK_PATH_MAX]; unsigned int n; s_copy(tmp, pick_path, sizeof(tmp)); n = s_len(tmp); if (n && tmp[n - 1] == '/') tmp[--n] = '\0'; while (n > 0 && tmp[n - 1] != '/') n--; if (n == 0) s_copy(out, "/", cap); else { tmp[n - 1] = '\0'; s_copy(out, n == 1 ? "/" : tmp, cap); } } static void picker_activate(int idx) { char full[PICK_PATH_MAX + 72]; if (idx < 0 || idx >= pick_count) return; path_join(full, sizeof(full), pick_path, pick_names[idx]); if (pick_dirs[idx]) { load_picker_dir(full); ngui_invalidate(&ui); return; } set_status("Loading wallpaper...", 0); paint_current(); set_wallpaper_file(full); picker_mode = 0; wallpaper_refresh(); ngui_invalidate(&ui); } static void set_wallpaper_file(const char *path) { struct sys_stat st; struct img_info info; struct img_options opt; struct img_mem mem; struct img_image img; struct img_blit b; struct wm_wallpaper_hdr *h; unsigned char *file = 0, *arena = 0, *out = 0; unsigned int cap, est, sw, sh, tw, th, pixbytes, total, i; unsigned long long sx, sy, scale, dw, dh; int n, r; if (u_stat(path, &st) != 0 || st.is_dir || st.size == 0) { set_status("Cannot read that image", 1); return; } if (st.size >= WALL_FILE_CAP) { set_status("Image is larger than 4 MB", 1); return; } cap = st.size + 512; /* a FAT read may round up to a sector */ file = (unsigned char *)malloc(cap); if (!file) { set_status("Not enough memory for the image", 1); return; } n = img_read_file(path, file, cap); if (n < 0) { set_status(img_error_string(n), 1); goto out; } r = img_sniff(file, (unsigned int)n, &info); if (r != 0) { set_status(img_error_string(r), 1); goto out; } est = img_estimate(&info); if (est == 0xFFFFFFFFu || est > WALL_HEAP_CAP) { set_status("Image needs too much memory", 1); goto out; } arena = (unsigned char *)malloc(est); if (!arena) { set_status("Not enough memory for the image", 1); goto out; } img_mem_init(&mem, arena, est); memset(&opt, 0, sizeof(opt)); r = img_decode(file, (unsigned int)n, &opt, &mem, &img); if (r != 0) { set_status(img_error_string(r), 1); goto out; } if (u_screen_size(&sw, &sh) != 0 || !sw || !sh) { set_status("Cannot read the screen size", 1); goto out; } tw = sw; th = sh; if (tw > WM_WALLPAPER_MAX_W) { th = (unsigned int)((unsigned long long)th * WM_WALLPAPER_MAX_W / tw); tw = WM_WALLPAPER_MAX_W; } if (th > WM_WALLPAPER_MAX_H) { tw = (unsigned int)((unsigned long long)tw * WM_WALLPAPER_MAX_H / th); th = WM_WALLPAPER_MAX_H; } if (!tw) tw = 1; if (!th) th = 1; pixbytes = tw * th * 2u; total = sizeof(*h) + pixbytes; out = (unsigned char *)malloc(total); if (!out) { set_status("Not enough memory for the wallpaper", 1); goto out; } memset(out, 0, total); h = (struct wm_wallpaper_hdr *)out; for (i = 0; i < sizeof(h->magic); i++) h->magic[i] = WM_WALLPAPER_MAGIC[i]; h->width = tw; h->height = th; sx = (((unsigned long long)tw << 16) + img.w - 1) / img.w; sy = (((unsigned long long)th << 16) + img.h - 1) / img.h; scale = sx > sy ? sx : sy; if (!scale) scale = 1; dw = ((unsigned long long)img.w * scale) >> 16; dh = ((unsigned long long)img.h * scale) >> 16; memset(&b, 0, sizeof(b)); b.dst = (unsigned short *)(out + sizeof(*h)); b.dst_w = (int)tw; b.dst_h = (int)th; b.clip_w = (int)tw; b.clip_h = (int)th; b.x = ((int)tw - (int)dw) / 2; b.y = ((int)th - (int)dh) / 2; b.scale = (unsigned int)scale; img_draw(&img, &b); r = u_syscall3(SYS_WRITE_FILE, (int)WM_WALLPAPER_DATA, (int)out, (int)total); if (r < 0) { struct sys_stat dst; /* Only fall back to the RAM copy when there is no data * volume at all. Writing the share copy while an older * /data image exists would make the WM load the stale one. */ if (u_stat("/data", &dst) == 0 && dst.is_dir) { set_status("Cannot save the wallpaper to /data", 1); goto out; } r = u_syscall3(SYS_WRITE_FILE, (int)WM_WALLPAPER_SHARE, (int)out, (int)total); } if (r < 0) { set_status("Cannot save the wallpaper", 1); goto out; } u_syscall1(SYS_WM_WALLPAPER, 1); wallpaper_present = 1; set_status("Wallpaper set", 0); out: free(out); free(arena); free(file); } static void draw_appearance(struct ngui_rect area, int *yy) { struct ngui_rect r; char line[96], *p; int i, y = *yy; if (picker_mode) { draw_picker(area, yy); return; } r = (struct ngui_rect){ area.x, y, area.w, SECTION_H }; ngui_section(&ui, r, "Desktop colour", 0, 0); y += SECTION_H; ngui_text_utf8(&ui, area.x + 10, y, "Used when no wallpaper image is installed.", ui.theme.text_dim); y += 24; for (i = 0; i < NBG; i++) { char sub[16], *sp = sub; sp = s_put(sp, "#"); { static const char hex[] = "0123456789abcdef"; unsigned int rgb = bg_choices[i].rgb; int k; for (k = 5; k >= 0; k--) *sp++ = hex[(rgb >> (k * 4)) & 15]; } *sp = '\0'; r = (struct ngui_rect){ area.x, y, area.w, 36 }; if (settings_row(ID_BG_BASE + i, r, bg_choices[i].name, sub, prefs.bg_rgb == bg_choices[i].rgb ? "Selected" : "", prefs.bg_rgb == bg_choices[i].rgb ? 1 : 0, (int)bg_choices[i].rgb)) request_action(A_BG, i); y += 38; } y += GAP; r = (struct ngui_rect){ area.x, y, area.w, SECTION_H }; ngui_section(&ui, r, "Wallpaper", 0, 0); y += SECTION_H; if (wallpaper_present) { p = s_put(line, "Image: "); p = s_put(p, wallpaper_source); *p = '\0'; ngui_text_utf8(&ui, area.x + 10, y, line, ui.theme.text_dim); y += 24; ngui_text_utf8(&ui, area.x + 10, y, "The image covers the desktop colour.", ui.theme.text_dim); y += 24; } else { ngui_text_utf8(&ui, area.x + 10, y, "No wallpaper image is installed.", ui.theme.text_dim); y += 24; } r = (struct ngui_rect){ area.x + 10, y, 150, 28 }; if (ngui_button(&ui, ID_WALL_CHOOSE, r, wallpaper_present ? "Change image..." : "Choose image...")) request_action(A_WALL_CHOOSE, 0); if (wallpaper_present) { r.x += 160; r.w = 110; if (ngui_button(&ui, ID_WALL_REMOVE, r, "Remove")) request_action(A_WALL_REMOVE, 0); } y += 28 + GAP; ngui_text_utf8(&ui, area.x + 10, y, "Images are scaled to cover the screen and stored on the", ui.theme.text_dim); y += 20; ngui_text_utf8(&ui, area.x + 10, y, "data volume (or /share without one).", ui.theme.text_dim); y += 22; *yy = y; } static void draw_picker(struct ngui_rect area, int *yy) { struct ngui_rect r; struct ngui_row row; char size[24]; int i, y = *yy; r = (struct ngui_rect){ area.x, y, area.w, 22 }; ngui_text_px(&ui, r.x, r.y, "Choose a wallpaper image", 24, ui.theme.text, 14, 1, 0, 0); y += 26; ngui_text_ellipsis_utf8(&ui, (struct ngui_rect){ area.x, y, area.w, 18 }, area.x, y, pick_path, ui.theme.text_dim); y += 24; r = (struct ngui_rect){ area.x, y, 70, 26 }; if (ngui_button(&ui, ID_PICK_UP, r, "Up")) request_action(A_PICK_UP, 0); r.x += 80; r.w = 90; if (ngui_button(&ui, ID_PICK_CANCEL, r, "Cancel")) request_action(A_PICK_CANCEL, 0); y += 26 + GAP; picker_scroll.content_h = pick_count * PICK_ROW_H; picker_scroll.view_h = area.y + area.h - y; { struct ngui_rect list_r = { area.x, y, area.w, area.y + area.h - y }; ngui_scroll_begin(&ui, list_r, &picker_scroll); ngui_scroll_wheel(&ui, list_r, &picker_scroll); ngui_scroll_keys(&ui, &picker_scroll); y -= picker_scroll.offset; if (!pick_count) { ngui_text_utf8(&ui, area.x + 10, y, "No folders or image files here", ui.theme.text_dim); y += PICK_ROW_H; } for (i = 0; i < pick_count; i++) { r = (struct ngui_rect){ area.x, y + i * PICK_ROW_H, area.w, PICK_ROW_H }; if (r.y + r.h <= area.y || r.y >= area.y + area.h) continue; if (pick_dirs[i]) { s_copy(size, "Folder", sizeof(size)); } else { format_size(size, pick_sizes[i]); } row.title = pick_names[i]; row.subtitle = size; row.trailing = 0; row.bars = -1; row.selected = 0; if (ngui_list_row_utf8(&ui, ID_PICK_ROW_BASE + i, r, &row)) request_action(A_PICK_ROW, i); } ngui_scroll_end(&ui, list_r, &picker_scroll); } *yy = area.y + area.h; } /* ------------------------------------------------------------------ */ /* Windows and About */ static void draw_windows(struct ngui_rect area, int *yy) { struct ngui_rect r; int y = *yy; r = (struct ngui_rect){ area.x, y, area.w, SECTION_H }; ngui_section(&ui, r, "Window manager", 0, 0); y += SECTION_H; ngui_text_utf8(&ui, area.x + 10, y, "Changes apply immediately and are saved for the next " "boot.", ui.theme.text_dim); y += 26; r = (struct ngui_rect){ area.x, y, area.w, 44 }; if (settings_row(ID_WIN_SHADOW, r, "Window shadows", "Soft drop shadow around normal windows", prefs.shadow ? "On" : "Off", prefs.shadow ? 1 : 0, -1)) request_action(A_WIN_SHADOW, 0); y += 46; r = (struct ngui_rect){ area.x, y, area.w, 44 }; if (settings_row(ID_WIN_SNAP, r, "Snap windows", "Drag a title bar to a screen edge to snap it", prefs.snap ? "On" : "Off", prefs.snap ? 1 : 0, -1)) request_action(A_WIN_SNAP, 0); y += 46; r = (struct ngui_rect){ area.x, y, area.w, 44 }; if (settings_row(ID_WIN_RESIST, r, "Edge resistance", "Hold a window briefly at screen and window edges", prefs.resist ? "On" : "Off", prefs.resist ? 1 : 0, -1)) request_action(A_WIN_RESIST, 0); y += 46 + GAP; ngui_text_utf8(&ui, area.x + 10, y, "Double-clicking a title bar still maximizes or " "restores a window.", ui.theme.text_dim); y += 22; *yy = y; } static void draw_about(struct ngui_rect area, int *yy) { struct ngui_rect r; struct sys_meminfo mi; struct sys_cputemp ct; char value[64], *p; unsigned int sw = 0, sh = 0, secs, h, m, s; int y = *yy; r = (struct ngui_rect){ area.x, y, area.w, SECTION_H }; ngui_section(&ui, r, "System", 0, 0); y += SECTION_H; p = value; p = s_put(p, OS_NAME); p = s_put(p, " "); p = s_put(p, OS_VERSION); *p = '\0'; info_row(area, &y, "Operating system", value); u_screen_size(&sw, &sh); p = value; p = s_put_dec(p, sw); p = s_put(p, " x "); p = s_put_dec(p, sh); *p = '\0'; info_row(area, &y, "Display", value); secs = u_ticks_ms() / 1000; h = secs / 3600; m = (secs / 60) % 60; s = secs % 60; p = value; p = s_put_dec(p, h); p = s_put(p, "h "); p = s_put_dec(p, m); p = s_put(p, "m "); p = s_put_dec(p, s); p = s_put(p, "s"); *p = '\0'; info_row(area, &y, "Uptime", value); if (u_syscall1(SYS_MEMINFO, (int)&mi) == 0) { p = value; p = s_put_dec(p, mi.total_kb / 1024u); p = s_put(p, " MB"); *p = '\0'; info_row(area, &y, "Memory", value); p = value; p = s_put_dec(p, (mi.free_pages * mi.page_size) / (1024u * 1024u)); p = s_put(p, " MB"); *p = '\0'; info_row(area, &y, "Free", value); } if (u_cpu_temp(&ct) == 0 && ct.temp_c >= 0) { p = value; p = s_put_dec(p, (unsigned int)ct.temp_c); p = s_put(p, " C"); *p = '\0'; info_row(area, &y, "CPU temperature", value); } y += GAP; r = (struct ngui_rect){ area.x, y, area.w, SECTION_H }; ngui_section(&ui, r, "Settings", 0, 0); y += SECTION_H; ngui_text_utf8(&ui, area.x + 10, y, "Window preferences: /data/SETTINGS.CFG", ui.theme.text_dim); y += 22; ngui_text_utf8(&ui, area.x + 10, y, "Without a data volume the RAM copy is " "/share/settings.cfg.", ui.theme.text_dim); y += 22; *yy = y; } /* ------------------------------------------------------------------ */ /* Frame */ static int sidebar_width(void) { return ui.w < 620 ? SIDEBAR_MIN_W : SIDEBAR_W; } static void settings_draw_frame(void) { struct ngui_rect all = { 0, 0, ui.w, ui.h }; struct ngui_rect body = all, side, header, content, footer; int side_w = sidebar_width(); int i, y; ngui_fill_rect(&ui, all, ui.theme.bg); side = ngui_cut_left(&body, side_w); ngui_fill_rect(&ui, side, ui.theme.panel); ngui_text_px(&ui, side.x + 14, side.y + 14, "Settings", s_len("Settings"), ui.theme.text, 20, 1, 0, 0); ngui_text(&ui, side.x + 14, side.y + 40, OS_NAME " " OS_VERSION, ui.theme.text_dim); y = side.y + 70; for (i = 0; i < SEC_COUNT; i++) { struct ngui_rect r = { side.x + 8, y, side.w - 16, NAV_ROW_H }; if (settings_row(ID_NAV_BASE + i, r, sections[i].label, sections[i].subtitle, 0, cur_section == i ? 1 : 0, -1)) request_action(A_NAV, i); y += NAV_ROW_H + NAV_GAP; } header = ngui_cut_top(&body, HEADER_H); ngui_text_px(&ui, header.x + 16, header.y + 8, sections[cur_section].label, s_len(sections[cur_section].label), ui.theme.text, 20, 1, 0, 0); ngui_text_utf8(&ui, header.x + 16, header.y + 32, sections[cur_section].subtitle, ui.theme.text_dim); ngui_fill_rect(&ui, (struct ngui_rect){ header.x + 16, header.y + HEADER_H - 1, header.w - 32, 1 }, ui.theme.panel_alt); footer = ngui_cut_bottom(&body, FOOTER_H); ngui_fill_rect(&ui, footer, ui.theme.panel); ngui_fill_rect(&ui, (struct ngui_rect){ footer.x, footer.y, footer.w, 1 }, ui.theme.panel_alt); if (status[0] && status_until && (int)(u_ticks_ms() - status_until) < 0) { ngui_text_ellipsis_utf8(&ui, (struct ngui_rect){ footer.x + 12, footer.y + 6, footer.w - 24, 18 }, footer.x + 12, footer.y + 6, status, status_error ? ui.theme.error : ui.theme.success); } else { ngui_text_utf8(&ui, footer.x + 12, footer.y + 6, "Changes are saved automatically.", ui.theme.text_dim); } content = body; content.x += 16; content.w -= 24; content.y += 6; content.h -= 8; { int picker_only = (cur_section == SEC_APPEARANCE && picker_mode != 0); if (picker_only) { /* The picker has its own scroll pane; do not let the * outer pane eat the wheel or PgUp/PgDn keys. */ content_scroll.offset = 0; ngui_push_clip(&ui, content); y = content.y; } else { ngui_scroll_begin(&ui, content, &content_scroll); ngui_scroll_wheel(&ui, content, &content_scroll); ngui_scroll_keys(&ui, &content_scroll); y = content.y - content_scroll.offset; } switch (cur_section) { case SEC_NETWORK: draw_network(content, &y); break; case SEC_POWER: draw_power(content, &y); break; case SEC_DATETIME: draw_datetime(content, &y); break; case SEC_APPEARANCE: draw_appearance(content, &y); break; case SEC_WINDOWS: draw_windows(content, &y); break; case SEC_ABOUT: draw_about(content, &y); break; default: break; } if (picker_only) { ngui_pop_clip(&ui); } else { content_scroll.content_h = y + content_scroll.offset - content.y + GAP; ngui_scroll_end(&ui, content, &content_scroll); } } } /* ------------------------------------------------------------------ */ /* Actions */ static void set_section(int s) { if (s < 0 || s >= SEC_COUNT) return; if (s == cur_section) return; cur_section = s; content_scroll.offset = 0; picker_mode = 0; if (s == SEC_NETWORK && !devs_loaded) { set_status("Loading network devices...", 0); paint_current(); load_devices(); set_status("", 0); if (wifi_present && wst.state < SYS_WIFI_ST_ASSOC) start_scan(); } if (s == SEC_DATETIME) load_datetime_fields(0); if (s == SEC_APPEARANCE) wallpaper_refresh(); ngui_set_focus(&ui, 0); ngui_invalidate(&ui); } static void handle_action(void) { int a = pending_action, arg = pending_arg; pending_action = A_NONE; pending_arg = 0; switch (a) { case A_NAV: set_section(arg); break; case A_REFRESH: set_status("Refreshing network devices...", 0); load_devices(); set_status("Device list refreshed", 0); ngui_invalidate(&ui); break; case A_SCAN: start_scan(); break; case A_DISCONNECT: u_wifi(SYS_WIFI_DISCONNECT, 0, 0); load_status(); set_status("WiFi disconnected", 0); ngui_invalidate(&ui); break; case A_DEV: select_device(arg); break; case A_BSS: select_bss(arg); break; case A_SAVED: load_saved(); content_scroll.offset = 0; net_view = NET_SAVED; ngui_invalidate(&ui); break; case A_SAVED_BACK: net_view = NET_MAIN; content_scroll.offset = 0; ngui_invalidate(&ui); break; case A_FORGET: if (arg >= 0 && arg < nsaved) { if (wifi_store_forget(saved[arg].ssid, saved[arg].ssid_len) > 0) { load_saved(); set_status("Saved network removed", 0); } else { set_status("Could not remove the saved network", 1); } } ngui_invalidate(&ui); break; case A_PASS_OK: { char pass[65]; unsigned int i, n; ngui_input_value(&pass_in, pass, sizeof(pass)); n = s_len(pass); if (n == 0) { set_status("Enter the WPA2 passphrase", 1); } else if (n < 8 || n > 63) { 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; break; } case A_PASS_CANCEL: case A_CONN_CANCEL: if (net_view == NET_CONNECT) cancel_connect(); else { net_view = NET_MAIN; set_status("", 0); ngui_invalidate(&ui); } break; case A_POWER: set_power_mode(arg); break; case A_DATE_SET: apply_datetime(); break; case A_DATE_NOW: load_datetime_fields(1); set_status("Date and time reloaded", 0); break; case A_DATE_NTP: sync_ntp(); break; case A_BG: if (arg >= 0 && arg < NBG) { prefs.bg_rgb = bg_choices[arg].rgb; commit_prefs("Background colour saved"); } break; case A_WALL_CHOOSE: { struct sys_stat st; picker_mode = 1; content_scroll.offset = 0; if (u_stat("/data", &st) == 0 && st.is_dir) load_picker_dir("/data"); else if (u_stat("/share", &st) == 0 && st.is_dir) load_picker_dir("/share"); else load_picker_dir("/"); ngui_invalidate(&ui); break; } case A_WALL_REMOVE: u_syscall1(SYS_UNLINK, (int)WM_WALLPAPER_DATA); u_syscall1(SYS_UNLINK, (int)WM_WALLPAPER_SHARE); u_syscall1(SYS_WM_WALLPAPER, 1); wallpaper_refresh(); if (wallpaper_present) set_status("The wallpaper on the data volume could not be removed", 1); else set_status("Wallpaper removed; the desktop colour is visible", 0); ngui_invalidate(&ui); break; case A_PICK_UP: { char parent[PICK_PATH_MAX]; pick_parent(parent, sizeof(parent)); load_picker_dir(parent); ngui_invalidate(&ui); break; } case A_PICK_CANCEL: picker_mode = 0; ngui_invalidate(&ui); break; case A_PICK_ROW: picker_activate(arg); break; case A_WIN_SHADOW: prefs.shadow = prefs.shadow ? 0 : 1; commit_prefs(prefs.shadow ? "Window shadows enabled" : "Window shadows disabled"); break; case A_WIN_SNAP: prefs.snap = prefs.snap ? 0 : 1; commit_prefs(prefs.snap ? "Edge snapping enabled" : "Edge snapping disabled"); break; case A_WIN_RESIST: prefs.resist = prefs.resist ? 0 : 1; commit_prefs(prefs.resist ? "Edge resistance enabled" : "Edge resistance disabled"); break; default: break; } } static void check_section_request(void) { char buf[24]; unsigned int now_ms = u_ticks_ms(); int n, i; if (now_ms - last_request_ms < 250) return; last_request_ms = now_ms; n = u_read_file(WM_SETTINGS_REQUEST, buf, sizeof(buf) - 1); if (n <= 0) return; buf[n] = '\0'; while (n > 0 && (buf[n - 1] == '\n' || buf[n - 1] == '\r' || buf[n - 1] == ' ')) buf[--n] = '\0'; /* Consume the request: the WM writes a fresh file for every pill * click, and a stale one must not pull the user back after they * picked another section by hand. */ u_syscall1(SYS_UNLINK, (int)WM_SETTINGS_REQUEST); if (!buf[0]) return; for (i = 0; i < SEC_COUNT; i++) { if (s_eq(buf, sections[i].arg)) { if (i != cur_section) set_section(i); break; } } } void _start(const char *arg) { struct win_info wi; unsigned int sw = 0, sh = 0; int w, h; u_screen_size(&sw, &sh); if (!sw || !sh) { sw = 1024; sh = 768; } w = (int)sw - 40; h = (int)sh - 80; if (w > 900) w = 900; if (h > 620) h = 620; if (w < 460) w = 460; if (h < 360) h = 360; prefs_defaults(); if (u_wm_get_prefs(&prefs) != 0) prefs_defaults(); /* The WM may not have reached the data volume yet (the first boot * or a quick launch); prefer the persisted file when it is valid. */ if (prefs_file_load() == 0) u_wm_set_prefs(&prefs); cur_section = section_from_name(arg); if (u_win_create((unsigned int)w, (unsigned int)h, "Settings", &wi) != 0) { u_puts("settings: no graphics window\n"); 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 (cur_section == SEC_NETWORK) { set_status("Loading network devices...", 0); paint_current(); } load_status(); load_saved(); wallpaper_refresh(); if (cur_section == SEC_NETWORK) { load_devices(); set_status("", 0); if (wifi_present && wst.state < SYS_WIFI_ST_ASSOC) start_scan(); } if (cur_section == SEC_DATETIME) load_datetime_fields(0); last_status_ms = u_ticks_ms(); for (;;) { static int first_frame = 1; unsigned int now_ms = u_ticks_ms(); drain_tty(); poll_scan(); poll_connect(); check_section_request(); if (now_ms - last_status_ms >= 1000) { last_status_ms = now_ms; load_status(); if (cur_section == SEC_DATETIME) load_datetime_fields(0); ngui_invalidate(&ui); } if (status_until && (int)(now_ms - status_until) >= 0) { status[0] = '\0'; status_until = 0; ngui_invalidate(&ui); } if (ngui_begin(&ui)) { settings_draw_frame(); ngui_end(&ui); if (first_frame) { first_frame = 0; ngui_invalidate(&ui); } } if (pending_action) handle_action(); if (ngui_key_peek(&ui) == NGUI_KEY_ESC) { ngui_key(&ui); if (picker_mode) { picker_mode = 0; ngui_invalidate(&ui); } else if (net_view != NET_MAIN) { net_view = NET_MAIN; set_status("", 0); ngui_invalidate(&ui); } else if (ui.focus == 0) { quit = 1; } } if (quit) break; u_sleep_ms(20); } ngui_font_detach(&ui); u_win_close(); u_exit(0); }