ucmd_netmenu.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090
  1. /* netmenu: the top-bar control panel.
  2. *
  3. * One flat popover for the status area: battery/AC and clock in the summary,
  4. * a brightness slider, the NIC list and WiFi scan/join/saved-network views,
  5. * connection details and quick tiles for Wi-Fi, the P-state power mode and
  6. * the Date & Time editor. Selecting a wired or USB device makes it the
  7. * active netdev and runs DHCP; selecting a WiFi network joins it with the
  8. * passphrase saved in /data/WIFI.CFG when one is known, otherwise it asks for
  9. * the WPA2 passphrase, then starts the existing `wifi` supplicant with a
  10. * staged /share/netmenu.wifi request (the SYS_SPAWN argument limit is too
  11. * small for an SSID plus a long passphrase). A Saved view lists the known
  12. * networks and forgets individual entries. The supplicant itself runs DHCP
  13. * once the data port opens.
  14. *
  15. * The WM opens one instance from the network, battery or clock status area;
  16. * an argument ("network"/"power"/"clock") scrolls the panel to that
  17. * section. */
  18. #include "ucmd_lib.h"
  19. #include "ngui.h"
  20. #include "wifi_store.h"
  21. #define MAX_DEVS 8
  22. #define MAX_BSS 32
  23. #define DEV_ROW_H 42
  24. #define WIFI_ROW_H 42
  25. #define SAVED_ROW_H 30
  26. #define SAVED_LIST_ROW_H 42
  27. #define SECTION_H 28
  28. #define MENU_GAP 10
  29. #define LOG_MAX 2048
  30. #define ID_REFRESH 1
  31. #define ID_SCAN 2
  32. #define ID_DISCONNECT 3
  33. #define ID_PASS_OK 4
  34. #define ID_PASS_CANCEL 5
  35. #define ID_CONN_CANCEL 6
  36. #define ID_PASS 7
  37. #define ID_SAVED 8
  38. #define ID_SAVED_BACK 9
  39. #define ID_DEV_BASE 100
  40. #define ID_BSS_BASE 200
  41. #define ID_SAVED_BASE 300
  42. #define ID_INFO_BASE 400
  43. #define ID_SAVED_ROW_BASE 500
  44. #define ID_BRIGHT 11
  45. #define ID_TILE_NETWORK 12
  46. #define ID_TILE_POWER 13
  47. #define ID_TILE_DATE 14
  48. #define ID_DATE_FIELD 20
  49. #define ID_TIME_FIELD 21
  50. #define ID_NTP_FIELD 22
  51. #define ID_DATE_SET 23
  52. #define ID_DATE_NOW 24
  53. #define ID_DATE_NTP 25
  54. #define ID_POWER_BASE 30
  55. #define ACT_REFRESH (1u << 0)
  56. #define ACT_SCAN (1u << 1)
  57. #define ACT_DISCONNECT (1u << 2)
  58. #define ACT_BACK (1u << 3)
  59. #define ACT_CONNECT (1u << 4)
  60. #define ACT_SAVED (1u << 5)
  61. #define ACT_FORGET (1u << 6)
  62. #define ACT_NET_TOGGLE (1u << 7)
  63. #define ACT_POWER_TOGGLE (1u << 8)
  64. #define ACT_DATE_TOGGLE (1u << 9)
  65. #define ACT_DATE_SET (1u << 10)
  66. #define ACT_DATE_NOW (1u << 11)
  67. #define ACT_DATE_NTP (1u << 12)
  68. #define ACT_POWER_AUTO (1u << 13)
  69. #define ACT_POWER_MAX (1u << 14)
  70. #define ACT_POWER_MIN (1u << 15)
  71. enum { VIEW_MAIN, VIEW_PASS, VIEW_CONNECT, VIEW_SAVED };
  72. enum { EXPAND_NONE, EXPAND_NETWORK, EXPAND_POWER, EXPAND_DATE };
  73. static struct ngui ui;
  74. static struct ngui_popover pop;
  75. static struct ngui_input pass_in;
  76. static struct ngui_input date_in, time_in, ntp_in;
  77. static struct ngui_scroll main_scroll;
  78. static struct ngui_scroll saved_scroll;
  79. static struct ngui_slider brightness_slider;
  80. static struct sys_netdev devs[MAX_DEVS];
  81. static int ndev;
  82. static struct sys_wifi_bss bss[MAX_BSS];
  83. static int nbss;
  84. static struct wifi_cfg saved[WIFI_CFG_MAX];
  85. static int nsaved;
  86. static struct sys_wifi_status wst;
  87. static struct sys_netinfo net;
  88. static int net_present;
  89. static int wifi_checked;
  90. static int wifi_present;
  91. static int brightness = -1;
  92. static int cpufreq_mode = -1;
  93. static struct sys_powerinfo powerinfo;
  94. static int power_ok;
  95. static struct sys_datetime now;
  96. static int now_ok;
  97. static int expanded;
  98. static int date_loaded;
  99. static unsigned int screen_w = 1024, screen_h = 768;
  100. static int resize_pending;
  101. static int view;
  102. static int sel_bss = -1;
  103. static char sel_ssid[33];
  104. static int scanning;
  105. static unsigned int scan_start;
  106. static int connect_pid = -1;
  107. static int connect_tty;
  108. static unsigned int connect_start;
  109. static char conn_log[LOG_MAX];
  110. static unsigned int conn_log_len;
  111. static char status[160];
  112. static int status_error;
  113. static unsigned int status_until;
  114. static unsigned int actions;
  115. static int action_dev = -1;
  116. static int action_bss = -1;
  117. static int action_saved = -1;
  118. static void draw(void);
  119. static void paint_frame(void);
  120. static const char *power_mode_name(int mode);
  121. static void resize_panel(void);
  122. static void load_datetime_fields(int force);
  123. static void toggle_expand(int which);
  124. static unsigned int slen(const char *s)
  125. {
  126. unsigned int n = 0;
  127. while (s && s[n])
  128. n++;
  129. return n;
  130. }
  131. static int seq(const char *a, const char *b)
  132. {
  133. while (*a && *b) {
  134. if (*a != *b)
  135. return 0;
  136. a++;
  137. b++;
  138. }
  139. return *a == *b;
  140. }
  141. static char *sput(char *p, const char *s)
  142. {
  143. while (s && *s)
  144. *p++ = *s++;
  145. *p = '\0';
  146. return p;
  147. }
  148. static char *put_dec(char *p, unsigned int v)
  149. {
  150. char b[10];
  151. unsigned int n = 0;
  152. do {
  153. b[n++] = (char)('0' + v % 10);
  154. v /= 10;
  155. } while (v);
  156. while (n)
  157. *p++ = b[--n];
  158. return p;
  159. }
  160. static char *put_signed(char *p, int v)
  161. {
  162. if (v < 0) {
  163. *p++ = '-';
  164. v = -v;
  165. }
  166. return put_dec(p, (unsigned int)v);
  167. }
  168. static char *put_ip(char *p, const unsigned char ip[4])
  169. {
  170. p = put_dec(p, ip[0]); *p++ = '.';
  171. p = put_dec(p, ip[1]); *p++ = '.';
  172. p = put_dec(p, ip[2]); *p++ = '.';
  173. p = put_dec(p, ip[3]);
  174. return p;
  175. }
  176. static char *put_mac(char *p, const unsigned char mac[6])
  177. {
  178. static const char hex[] = "0123456789abcdef";
  179. int i;
  180. for (i = 0; i < 6; i++) {
  181. *p++ = hex[mac[i] >> 4];
  182. *p++ = hex[mac[i] & 15];
  183. if (i != 5)
  184. *p++ = ':';
  185. }
  186. return p;
  187. }
  188. static int ip_any(const unsigned char ip[4])
  189. {
  190. return ip[0] || ip[1] || ip[2] || ip[3];
  191. }
  192. static char *put_2(char *p, unsigned int v)
  193. {
  194. *p++ = (char)('0' + (v / 10) % 10);
  195. *p++ = (char)('0' + v % 10);
  196. return p;
  197. }
  198. static void format_time(char *b, const struct sys_datetime *dt)
  199. {
  200. char *p = put_2(b, dt->hour);
  201. *p++ = ':';
  202. p = put_2(p, dt->minute);
  203. *p = '\0';
  204. }
  205. static void format_date(char *b, const struct sys_datetime *dt)
  206. {
  207. char *p = put_dec(b, dt->year);
  208. *p++ = '-';
  209. p = put_2(p, dt->month);
  210. *p++ = '-';
  211. p = put_2(p, dt->day);
  212. *p = '\0';
  213. }
  214. static void format_time_sec(char *b, const struct sys_datetime *dt)
  215. {
  216. char *p = put_2(b, dt->hour);
  217. *p++ = ':';
  218. p = put_2(p, dt->minute);
  219. *p++ = ':';
  220. p = put_2(p, dt->second);
  221. *p = '\0';
  222. }
  223. static void ssid_name(const struct sys_wifi_bss *b, char out[33])
  224. {
  225. unsigned int i, n = b->ssid_len;
  226. if (n > 32)
  227. n = 32;
  228. /* Keep the raw UTF-8 bytes; ngui's UTF-8 renderer handles them and the
  229. * connection path needs the exact SSID. */
  230. for (i = 0; i < n; i++)
  231. out[i] = (char)b->ssid[i];
  232. out[n] = '\0';
  233. if (!n) {
  234. out[0] = '(';
  235. out[1] = 'h'; out[2] = 'i'; out[3] = 'd';
  236. out[4] = 'd'; out[5] = 'e'; out[6] = 'n';
  237. out[7] = ')';
  238. out[8] = '\0';
  239. }
  240. }
  241. static void cfg_ssid_name(const struct wifi_cfg *c, char out[33])
  242. {
  243. unsigned int i, n = c->ssid_len;
  244. if (n > 32)
  245. n = 32;
  246. for (i = 0; i < n; i++)
  247. out[i] = (char)c->ssid[i];
  248. out[n] = '\0';
  249. }
  250. static void load_saved(void)
  251. {
  252. int n = wifi_store_load(saved, WIFI_CFG_MAX);
  253. nsaved = n > 0 ? n : 0;
  254. }
  255. static int bss_saved(const struct sys_wifi_bss *b)
  256. {
  257. return wifi_store_find(saved, nsaved, b->ssid, b->ssid_len);
  258. }
  259. static const char *security_name(unsigned int s)
  260. {
  261. return s == SYS_WIFI_SEC_OPEN ? "open" :
  262. s == SYS_WIFI_SEC_PSK ? "WPA2" : "unsupported";
  263. }
  264. static int rssi_bars(int rssi)
  265. {
  266. if (rssi >= -55)
  267. return 4;
  268. if (rssi >= -65)
  269. return 3;
  270. if (rssi >= -75)
  271. return 2;
  272. return 1;
  273. }
  274. static int bss_is_active(const struct sys_wifi_bss *b)
  275. {
  276. unsigned int i;
  277. if (wst.state < SYS_WIFI_ST_ASSOC)
  278. return 0;
  279. for (i = 0; i < 6; i++)
  280. if (wst.bssid[i] != b->bssid[i])
  281. return 0;
  282. return 1;
  283. }
  284. static void set_status(const char *msg, int err)
  285. {
  286. unsigned int i;
  287. for (i = 0; msg && msg[i] && i + 1 < sizeof(status); i++)
  288. status[i] = msg[i];
  289. status[i] = '\0';
  290. status_error = err;
  291. status_until = msg && msg[0] ? u_ticks_ms() + 6000 : 0;
  292. ngui_invalidate(&ui);
  293. }
  294. static int dev_active(const struct sys_netdev *d)
  295. {
  296. return net_present && seq(net.name, d->name);
  297. }
  298. static void load_status(void)
  299. {
  300. int i;
  301. net_present = (u_net_info(&net) == 1 && net.present);
  302. if (!wifi_checked || wifi_present) {
  303. if (u_wifi(SYS_WIFI_STATUS, (int)&wst, 0) == 0) {
  304. wifi_checked = 1;
  305. wifi_present = wst.present != 0;
  306. } else {
  307. /* Do not repeatedly probe a machine without a card. */
  308. wifi_checked = 1;
  309. wifi_present = 0;
  310. }
  311. }
  312. for (i = 0; i < ndev; i++) {
  313. devs[i].active = dev_active(&devs[i]) ? 1u : 0u;
  314. if (devs[i].active)
  315. devs[i].link = net.link;
  316. }
  317. power_ok = (u_power_info(&powerinfo, 0) == 0);
  318. now_ok = (u_rtc_get(&now) == 0);
  319. brightness = u_backlight_get();
  320. cpufreq_mode = u_cpufreq_get();
  321. }
  322. static void load_datetime_fields(int force)
  323. {
  324. char b[24];
  325. if (date_loaded && !force)
  326. return;
  327. if (force || !now_ok) {
  328. if (u_rtc_get(&now) != 0) {
  329. now_ok = 0;
  330. return;
  331. }
  332. now_ok = 1;
  333. }
  334. format_date(b, &now);
  335. ngui_input_set(&date_in, b);
  336. format_time_sec(b, &now);
  337. ngui_input_set(&time_in, b);
  338. if (!ntp_in.len)
  339. ngui_input_set(&ntp_in, "pool.ntp.org");
  340. date_loaded = 1;
  341. }
  342. static void load_devices(void)
  343. {
  344. int n = u_net_devlist(devs, MAX_DEVS);
  345. ndev = n > 0 ? n : 0;
  346. load_status();
  347. }
  348. static void drain_tty(void)
  349. {
  350. char buf[256];
  351. int n;
  352. if (connect_tty <= 0)
  353. return;
  354. while ((n = u_tty_read(connect_tty, buf, sizeof(buf))) > 0) {
  355. int i;
  356. for (i = 0; i < n; i++) {
  357. if (conn_log_len + 1 >= sizeof(conn_log)) {
  358. unsigned int half = sizeof(conn_log) / 2;
  359. unsigned int j;
  360. for (j = 0; j < half; j++)
  361. conn_log[j] = conn_log[conn_log_len -
  362. half + j];
  363. conn_log_len = half;
  364. }
  365. conn_log[conn_log_len++] = buf[i];
  366. }
  367. conn_log[conn_log_len] = '\0';
  368. ngui_invalidate(&ui);
  369. }
  370. }
  371. static void sort_bss(void)
  372. {
  373. int i, j;
  374. for (i = 1; i < nbss; i++)
  375. for (j = i; j > 0 && bss[j].rssi > bss[j - 1].rssi; j--) {
  376. struct sys_wifi_bss t = bss[j];
  377. bss[j] = bss[j - 1];
  378. bss[j - 1] = t;
  379. }
  380. }
  381. static int same_ssid(const struct sys_wifi_bss *a,
  382. const struct sys_wifi_bss *b)
  383. {
  384. unsigned int i;
  385. if (a->ssid_len == 0 || a->ssid_len != b->ssid_len)
  386. return 0;
  387. for (i = 0; i < a->ssid_len; i++)
  388. if (a->ssid[i] != b->ssid[i])
  389. return 0;
  390. return 1;
  391. }
  392. /* Mesh/repeater setups often advertise one SSID from several BSSIDs. The
  393. * list is RSSI-sorted, so keep the strongest entry of each SSID/security
  394. * pair (or the active BSSID when one of them is the current connection).
  395. * Hidden-SSID entries are dropped: the menu cannot join them anyway. */
  396. static void filter_bss(void)
  397. {
  398. int i, j, out = 0;
  399. for (i = 0; i < nbss; i++) {
  400. int dup = -1;
  401. if (bss[i].ssid_len == 0)
  402. continue;
  403. for (j = 0; j < out; j++)
  404. if (bss[i].security == bss[j].security &&
  405. same_ssid(&bss[i], &bss[j])) {
  406. dup = j;
  407. break;
  408. }
  409. if (dup < 0) {
  410. bss[out++] = bss[i];
  411. } else if (bss_is_active(&bss[i]) &&
  412. !bss_is_active(&bss[dup])) {
  413. bss[dup] = bss[i];
  414. }
  415. }
  416. nbss = out;
  417. }
  418. static void start_scan(void)
  419. {
  420. int r;
  421. if (scanning || !wifi_present)
  422. return;
  423. set_status("Scanning for WiFi networks...", 0);
  424. paint_frame();
  425. /* SYS_WIFI_SCAN loads the firmware itself if it is still down. */
  426. r = u_wifi(SYS_WIFI_SCAN, 0, 0);
  427. if (r < 0) {
  428. char m[48], *p;
  429. u_wifi(SYS_WIFI_STATUS, (int)&wst, 0);
  430. p = sput(m, "WiFi: ");
  431. p = sput(p, wst.detail[0] ? wst.detail : "scan failed");
  432. *p = '\0';
  433. set_status(m, 1);
  434. return;
  435. }
  436. scanning = 1;
  437. scan_start = u_ticks_ms();
  438. nbss = 0;
  439. main_scroll.offset = 0;
  440. ngui_invalidate(&ui);
  441. }
  442. static void poll_scan(void)
  443. {
  444. int n;
  445. if (!scanning)
  446. return;
  447. n = u_wifi(SYS_WIFI_RESULTS, (int)bss, MAX_BSS);
  448. if (n == -2) {
  449. if (u_ticks_ms() - scan_start > 15000) {
  450. scanning = 0;
  451. set_status("WiFi scan timed out", 1);
  452. }
  453. return;
  454. }
  455. scanning = 0;
  456. if (n < 0) {
  457. set_status("WiFi scan failed", 1);
  458. return;
  459. }
  460. nbss = n;
  461. sort_bss();
  462. filter_bss();
  463. if (nbss == 0)
  464. set_status("No WiFi networks found", 1);
  465. else
  466. set_status("WiFi scan complete", 0);
  467. ngui_invalidate(&ui);
  468. }
  469. static int write_wifi_cfg(const struct sys_wifi_bss *b, const char *pass)
  470. {
  471. struct wifi_cfg cfg;
  472. unsigned int i, n = slen(pass);
  473. int r;
  474. for (i = 0; i < sizeof(cfg); i++)
  475. ((unsigned char *)&cfg)[i] = 0;
  476. cfg.magic = WIFI_CFG_MAGIC;
  477. cfg.ssid_len = b->ssid_len;
  478. cfg.pass_len = (unsigned char)(n > 64 ? 64 : n);
  479. for (i = 0; i < 6; i++)
  480. cfg.bssid[i] = b->bssid[i];
  481. for (i = 0; i < b->ssid_len && i < 32; i++)
  482. cfg.ssid[i] = b->ssid[i];
  483. for (i = 0; i < cfg.pass_len; i++)
  484. cfg.pass[i] = pass[i];
  485. r = u_syscall3(SYS_WRITE_FILE, (int)WIFI_CFG_STAGED,
  486. (int)&cfg, (int)sizeof(cfg));
  487. for (i = 0; i < sizeof(cfg); i++)
  488. ((unsigned char *)&cfg)[i] = 0;
  489. return r;
  490. }
  491. static void start_connect(const char *pass)
  492. {
  493. struct sys_wifi_bss *b;
  494. char msg[80], *p;
  495. int pid;
  496. if (sel_bss < 0 || sel_bss >= nbss)
  497. return;
  498. if (connect_pid > 0 && u_alive((unsigned int)connect_pid)) {
  499. set_status("A WiFi connection is already in progress", 1);
  500. return;
  501. }
  502. b = &bss[sel_bss];
  503. ssid_name(b, sel_ssid);
  504. if (write_wifi_cfg(b, pass) != 0) {
  505. set_status("Cannot stage the WiFi request in /share", 1);
  506. return;
  507. }
  508. if (connect_tty <= 0) {
  509. connect_tty = u_tty_create();
  510. if (connect_tty < 0)
  511. connect_tty = 0;
  512. }
  513. conn_log_len = 0;
  514. conn_log[0] = '\0';
  515. connect_start = u_ticks_ms();
  516. pid = u_spawn("/ramfs/bin/wifi", "connectfile /share/netmenu.wifi",
  517. connect_tty, "/");
  518. if (pid < 0) {
  519. u_syscall1(SYS_UNLINK, (int)"/share/netmenu.wifi");
  520. set_status("Cannot start the WiFi helper", 1);
  521. return;
  522. }
  523. connect_pid = pid;
  524. view = VIEW_CONNECT;
  525. p = sput(msg, "Connecting to \"");
  526. p = sput(p, sel_ssid);
  527. p = sput(p, "\"...");
  528. *p = '\0';
  529. set_status(msg, 0);
  530. }
  531. static void cancel_connect(void)
  532. {
  533. if (connect_pid > 0 && u_alive((unsigned int)connect_pid))
  534. u_kill((unsigned int)connect_pid);
  535. connect_pid = -1;
  536. u_wifi(SYS_WIFI_DISCONNECT, 0, 0);
  537. view = VIEW_MAIN;
  538. set_status("WiFi connection cancelled", 0);
  539. load_status();
  540. }
  541. static void poll_connect(void)
  542. {
  543. int i;
  544. if (connect_pid <= 0)
  545. return;
  546. if (!u_alive((unsigned int)connect_pid)) {
  547. connect_pid = -1;
  548. load_status();
  549. load_saved();
  550. if (wst.state >= SYS_WIFI_ST_LINK) {
  551. int have_ip = 0;
  552. for (i = 0; i < 4; i++)
  553. if (net.ip[i])
  554. have_ip = 1;
  555. view = VIEW_MAIN;
  556. if (net_present && seq(net.name, "wifi")) {
  557. if (have_ip)
  558. set_status("WiFi connected and DHCP is up", 0);
  559. else
  560. set_status("WiFi connected, but DHCP got no lease", 1);
  561. } else {
  562. set_status("WiFi associated, but the netdev is busy", 1);
  563. }
  564. } else {
  565. /* Keep the log on screen so the failure reason is visible;
  566. * the Cancel button becomes Back. */
  567. set_status("WiFi connection failed", 1);
  568. }
  569. ngui_invalidate(&ui);
  570. return;
  571. }
  572. if (u_ticks_ms() - connect_start > 45000) {
  573. u_kill((unsigned int)connect_pid);
  574. connect_pid = -1;
  575. u_wifi(SYS_WIFI_DISCONNECT, 0, 0);
  576. set_status("WiFi connection timed out", 1);
  577. }
  578. }
  579. static void select_device(int i)
  580. {
  581. char msg[64], *p;
  582. if (i < 0 || i >= ndev)
  583. return;
  584. p = sput(msg, "Selecting ");
  585. p = sput(p, devs[i].name);
  586. *p = '\0';
  587. set_status(msg, 0);
  588. paint_frame();
  589. if (u_net_select(devs[i].name) != 0) {
  590. set_status("Cannot select that netdev (busy or unavailable)", 1);
  591. return;
  592. }
  593. set_status("Running DHCP...", 0);
  594. paint_frame();
  595. if (u_dhcp_run(6000) == 0)
  596. set_status("Device selected; DHCP lease acquired", 0);
  597. else
  598. set_status("Device selected, but DHCP got no lease", 1);
  599. load_devices();
  600. ngui_invalidate(&ui);
  601. }
  602. static void select_bss(int i)
  603. {
  604. if (i < 0 || i >= nbss)
  605. return;
  606. if (bss[i].ssid_len == 0) {
  607. set_status("Hidden network: use `wifi connect` from a terminal", 1);
  608. return;
  609. }
  610. sel_bss = i;
  611. ssid_name(&bss[i], sel_ssid);
  612. if (bss[i].security == SYS_WIFI_SEC_OTHER) {
  613. set_status("Unsupported security (only open and WPA2-PSK)", 1);
  614. return;
  615. }
  616. if (bss[i].security == SYS_WIFI_SEC_PSK) {
  617. int si = bss_saved(&bss[i]);
  618. if (si >= 0 && saved[si].pass_len > 0) {
  619. char pass[65];
  620. unsigned int j;
  621. for (j = 0; j < saved[si].pass_len; j++)
  622. pass[j] = saved[si].pass[j];
  623. pass[saved[si].pass_len] = '\0';
  624. start_connect(pass);
  625. for (j = 0; j < sizeof(pass); j++)
  626. ((volatile char *)pass)[j] = 0;
  627. return;
  628. }
  629. ngui_input_set(&pass_in, "");
  630. pass_in.secret = 1;
  631. pass_in.placeholder = "WPA2 passphrase";
  632. view = VIEW_PASS;
  633. set_status("", 0);
  634. ngui_set_focus(&ui, ID_PASS);
  635. ngui_invalidate(&ui);
  636. return;
  637. }
  638. start_connect("");
  639. }
  640. static unsigned int days_in_month(unsigned int y, unsigned int m)
  641. {
  642. static const unsigned char dm[12] = {
  643. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  644. };
  645. if (m < 1 || m > 12)
  646. return 0;
  647. if (m == 2 && ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0))
  648. return 29;
  649. return dm[m - 1];
  650. }
  651. static int parse_num(const char *s, unsigned int n, unsigned int *out)
  652. {
  653. unsigned int v = 0, i;
  654. if (!n)
  655. return -1;
  656. for (i = 0; i < n; i++) {
  657. if (s[i] < '0' || s[i] > '9')
  658. return -1;
  659. v = v * 10 + (unsigned int)(s[i] - '0');
  660. }
  661. *out = v;
  662. return 0;
  663. }
  664. static int parse_date(const char *s, unsigned int *y, unsigned int *mo,
  665. unsigned int *d)
  666. {
  667. if (slen(s) != 10)
  668. return -1;
  669. if ((s[4] != '-' && s[4] != '/') ||
  670. (s[7] != '-' && s[7] != '/'))
  671. return -1;
  672. if (parse_num(s, 4, y) != 0 ||
  673. parse_num(s + 5, 2, mo) != 0 ||
  674. parse_num(s + 8, 2, d) != 0)
  675. return -1;
  676. return 0;
  677. }
  678. static int parse_time(const char *s, unsigned int *h, unsigned int *mi,
  679. unsigned int *sec)
  680. {
  681. unsigned int len = slen(s);
  682. if (len == 5) {
  683. *sec = 0;
  684. } else if (len == 8) {
  685. if (s[5] != ':' || parse_num(s + 6, 2, sec) != 0)
  686. return -1;
  687. } else {
  688. return -1;
  689. }
  690. if (s[2] != ':' ||
  691. parse_num(s, 2, h) != 0 ||
  692. parse_num(s + 3, 2, mi) != 0)
  693. return -1;
  694. return 0;
  695. }
  696. static void apply_datetime(void)
  697. {
  698. struct sys_datetime dt;
  699. unsigned int y, mo, d, h, mi, s;
  700. if (parse_date(date_in.text, &y, &mo, &d) != 0) {
  701. set_status("Date must be YYYY-MM-DD", 1);
  702. return;
  703. }
  704. if (parse_time(time_in.text, &h, &mi, &s) != 0) {
  705. set_status("Time must be HH:MM:SS", 1);
  706. return;
  707. }
  708. if (y < 1970 || y > 2069) {
  709. set_status("Year must be 1970-2069", 1);
  710. return;
  711. }
  712. if (mo < 1 || mo > 12) {
  713. set_status("Month must be 1-12", 1);
  714. return;
  715. }
  716. if (d < 1 || d > days_in_month(y, mo)) {
  717. set_status("Day is not valid for that month", 1);
  718. return;
  719. }
  720. if (h > 23 || mi > 59 || s > 59) {
  721. set_status("Time is out of range", 1);
  722. return;
  723. }
  724. dt.year = y;
  725. dt.month = mo;
  726. dt.day = d;
  727. dt.hour = h;
  728. dt.minute = mi;
  729. dt.second = s;
  730. dt.wday = 0;
  731. if (u_rtc_set(&dt) != 0) {
  732. set_status("RTC write failed", 1);
  733. return;
  734. }
  735. load_status();
  736. load_datetime_fields(1);
  737. set_status("Date and time updated", 0);
  738. }
  739. static const char *ntp_error(int r)
  740. {
  741. switch (r) {
  742. case SYS_NTP_ENONIC:
  743. return "NTP: no network adapter";
  744. case SYS_NTP_ENAME:
  745. return "NTP: cannot resolve the server";
  746. case SYS_NTP_EARP:
  747. return "NTP: server is unreachable (ARP)";
  748. case SYS_NTP_ETX:
  749. return "NTP: transmit failed";
  750. case SYS_NTP_ETIMEOUT:
  751. return "NTP: server did not respond";
  752. case SYS_NTP_EREPLY:
  753. return "NTP: invalid server reply";
  754. case SYS_NTP_EBUSY:
  755. return "NTP: network is busy";
  756. default:
  757. return "NTP: sync failed";
  758. }
  759. }
  760. static void sync_ntp(void)
  761. {
  762. struct sys_datetime dt;
  763. const char *server = ntp_in.text[0] ? ntp_in.text : "pool.ntp.org";
  764. int r;
  765. set_status("Syncing NTP...", 0);
  766. paint_frame();
  767. r = u_ntp_sync(server, 5000, &dt);
  768. if (r != 0) {
  769. set_status(ntp_error(r), 1);
  770. return;
  771. }
  772. if (u_rtc_set(&dt) != 0) {
  773. set_status("RTC write failed", 1);
  774. return;
  775. }
  776. load_status();
  777. load_datetime_fields(1);
  778. set_status("Date and time synced", 0);
  779. }
  780. static void set_power_mode(int mode)
  781. {
  782. int got = u_cpufreq_set(mode);
  783. if (got < 0) {
  784. set_status("Power mode is not available", 1);
  785. return;
  786. }
  787. cpufreq_mode = got;
  788. {
  789. char msg[36], *p = sput(msg, "Power mode: ");
  790. sput(p, power_mode_name(got));
  791. set_status(msg, 0);
  792. }
  793. ngui_invalidate(&ui);
  794. }
  795. static void handle_actions(void)
  796. {
  797. unsigned int a = actions;
  798. int di = action_dev, bi = action_bss, fi = action_saved;
  799. actions = 0;
  800. action_dev = -1;
  801. action_bss = -1;
  802. action_saved = -1;
  803. if (a & ACT_NET_TOGGLE) {
  804. toggle_expand(EXPAND_NETWORK);
  805. return;
  806. }
  807. if (a & ACT_POWER_TOGGLE) {
  808. toggle_expand(EXPAND_POWER);
  809. return;
  810. }
  811. if (a & ACT_DATE_TOGGLE) {
  812. toggle_expand(EXPAND_DATE);
  813. return;
  814. }
  815. if (a & ACT_DATE_SET) {
  816. apply_datetime();
  817. return;
  818. }
  819. if (a & ACT_DATE_NOW) {
  820. load_datetime_fields(1);
  821. set_status("Date and time reloaded", 0);
  822. return;
  823. }
  824. if (a & ACT_DATE_NTP) {
  825. sync_ntp();
  826. return;
  827. }
  828. if (a & ACT_POWER_AUTO) {
  829. set_power_mode(SYS_CPUFREQ_AUTO);
  830. return;
  831. }
  832. if (a & ACT_POWER_MAX) {
  833. set_power_mode(SYS_CPUFREQ_MAX);
  834. return;
  835. }
  836. if (a & ACT_POWER_MIN) {
  837. set_power_mode(SYS_CPUFREQ_MIN);
  838. return;
  839. }
  840. if (a & ACT_BACK) {
  841. if (view == VIEW_CONNECT)
  842. cancel_connect();
  843. else {
  844. view = VIEW_MAIN;
  845. set_status("", 0);
  846. }
  847. return;
  848. }
  849. if (a & ACT_SAVED) {
  850. load_saved();
  851. saved_scroll.offset = 0;
  852. view = VIEW_SAVED;
  853. set_status("", 0);
  854. ngui_invalidate(&ui);
  855. return;
  856. }
  857. if (a & ACT_FORGET) {
  858. if (fi >= 0 && fi < nsaved) {
  859. if (wifi_store_forget(saved[fi].ssid,
  860. saved[fi].ssid_len) > 0) {
  861. load_saved();
  862. saved_scroll.offset = 0;
  863. set_status("Saved network removed", 0);
  864. } else {
  865. set_status("Could not remove the saved network", 1);
  866. }
  867. }
  868. ngui_invalidate(&ui);
  869. return;
  870. }
  871. if (a & ACT_CONNECT) {
  872. char pass[65];
  873. unsigned int i, n;
  874. ngui_input_value(&pass_in, pass, sizeof(pass));
  875. n = slen(pass);
  876. if (n == 0) {
  877. set_status("Enter the WPA2 passphrase", 1);
  878. } else if (n < 8 || n > 63) {
  879. /* A 64-character hex string is a raw PSK. */
  880. int hex = n == 64;
  881. if (hex) {
  882. unsigned int j;
  883. for (j = 0; j < n; j++) {
  884. char c = pass[j];
  885. if (!((c >= '0' && c <= '9') ||
  886. (c >= 'a' && c <= 'f') ||
  887. (c >= 'A' && c <= 'F'))) {
  888. hex = 0;
  889. break;
  890. }
  891. }
  892. }
  893. if (hex)
  894. start_connect(pass);
  895. else
  896. set_status("Use 8-63 characters or 64 hex digits", 1);
  897. } else {
  898. start_connect(pass);
  899. }
  900. for (i = 0; i < sizeof(pass); i++)
  901. ((volatile char *)pass)[i] = 0;
  902. return;
  903. }
  904. if (a & ACT_REFRESH) {
  905. set_status("Refreshing network devices...", 0);
  906. paint_frame();
  907. load_devices();
  908. set_status("Device list refreshed", 0);
  909. ngui_invalidate(&ui);
  910. } else if (a & ACT_SCAN) {
  911. start_scan();
  912. } else if (a & ACT_DISCONNECT) {
  913. u_wifi(SYS_WIFI_DISCONNECT, 0, 0);
  914. load_status();
  915. set_status("WiFi disconnected", 0);
  916. ngui_invalidate(&ui);
  917. } else if (di >= 0) {
  918. select_device(di);
  919. } else if (bi >= 0) {
  920. select_bss(bi);
  921. }
  922. }
  923. static void draw_status(void)
  924. {
  925. if (status[0] && status_until &&
  926. (int)(u_ticks_ms() - status_until) < 0)
  927. ngui_popover_footer(&ui, &pop, status,
  928. status_error ? ui.theme.error : ui.theme.success);
  929. }
  930. /* The active connection at the top of the panel: a status dot, the link
  931. * state and the device address (plus the SSID and RSSI on Wi-Fi). */
  932. static void draw_status_card(struct ngui_rect r)
  933. {
  934. struct ngui_rect line_r = { r.x + 34, r.y + 34, r.w - 44, 16 };
  935. struct ngui_rect dot = { r.x + 14, r.y + 25, 10, 10 };
  936. char line[160], *p;
  937. unsigned short dot_color;
  938. int i, have_ip = 0;
  939. for (i = 0; i < 4; i++)
  940. if (net.ip[i])
  941. have_ip = 1;
  942. if (!net_present)
  943. dot_color = ui.theme.error;
  944. else if (!have_ip ||
  945. (seq(net.name, "wifi") && wst.state < SYS_WIFI_ST_LINK))
  946. dot_color = ui.theme.warning;
  947. else
  948. dot_color = ui.theme.success;
  949. ngui_fill_round_rect(&ui, r, 8, 8, 8, 8, ui.theme.panel);
  950. ngui_frame_round(&ui, r, 8, 8, 8, 8, 1, ui.theme.panel_alt);
  951. ngui_fill_round_rect(&ui, dot, 5, 5, 5, 5, dot_color);
  952. p = line;
  953. if (!net_present)
  954. p = sput(p, "Offline");
  955. else if (seq(net.name, "wifi"))
  956. p = sput(p, wst.state >= SYS_WIFI_ST_LINK ?
  957. "Wi-Fi connected" : "Wi-Fi");
  958. else if (seq(net.name, "rndis"))
  959. p = sput(p, "USB tethering");
  960. else
  961. p = sput(p, "Wired network");
  962. *p = '\0';
  963. ngui_text_px(&ui, r.x + 34, r.y + 10, line, slen(line),
  964. ui.theme.text, 14, 1, 0, 0);
  965. p = line;
  966. if (net_present) {
  967. p = sput(p, net.name);
  968. p = sput(p, " \xC2\xB7 ");
  969. if (have_ip)
  970. p = put_ip(p, net.ip);
  971. else
  972. p = sput(p, "no IP");
  973. if (seq(net.name, "wifi") && wst.state >= SYS_WIFI_ST_ASSOC) {
  974. p = sput(p, " \xC2\xB7 ");
  975. for (i = 0; i < wst.ssid_len && i < 32; i++)
  976. *p++ = (char)wst.ssid[i];
  977. p = sput(p, " ");
  978. p = put_signed(p, wst.rssi);
  979. p = sput(p, " dBm");
  980. }
  981. } else {
  982. p = sput(p, "No network device active");
  983. }
  984. *p = '\0';
  985. ngui_text_ellipsis_utf8(&ui, line_r, line_r.x, line_r.y, line,
  986. ui.theme.text_dim);
  987. }
  988. static void draw_wifi_list(struct ngui_rect area, int *y)
  989. {
  990. struct ngui_row row;
  991. char ssid[33], sub[72], *p;
  992. int i;
  993. if (!wifi_present) {
  994. ngui_text(&ui, area.x + 12, *y + 12,
  995. "No Wi-Fi card detected", ui.theme.text_dim);
  996. *y += WIFI_ROW_H;
  997. return;
  998. }
  999. if (scanning) {
  1000. ngui_text(&ui, area.x + 12, *y + 12,
  1001. "Scanning for networks...", ui.theme.accent);
  1002. *y += WIFI_ROW_H;
  1003. return;
  1004. }
  1005. if (nbss == 0) {
  1006. const char *msg = wst.state >= SYS_WIFI_ST_ASSOC ?
  1007. "Connected - disconnect to scan for other networks" :
  1008. "Press Scan to list Wi-Fi networks";
  1009. ngui_text(&ui, area.x + 12, *y + 12, msg,
  1010. ui.theme.text_dim);
  1011. *y += WIFI_ROW_H;
  1012. return;
  1013. }
  1014. for (i = 0; i < nbss; i++) {
  1015. struct sys_wifi_bss *b = &bss[i];
  1016. struct ngui_rect r = { area.x, *y, area.w, WIFI_ROW_H };
  1017. if (r.y + r.h > area.y && r.y < area.y + area.h) {
  1018. ssid_name(b, ssid);
  1019. p = sub;
  1020. p = sput(p, security_name(b->security));
  1021. if (bss_saved(b) >= 0)
  1022. p = sput(p, " \xC2\xB7 saved");
  1023. if (bss_is_active(b))
  1024. p = sput(p, " \xC2\xB7 connected");
  1025. *p = '\0';
  1026. row.title = ssid;
  1027. row.subtitle = sub;
  1028. row.trailing = 0;
  1029. row.bars = b->security == SYS_WIFI_SEC_OTHER ? -1 :
  1030. rssi_bars(b->rssi);
  1031. row.selected = bss_is_active(b);
  1032. if (ngui_list_row_utf8(&ui, ID_BSS_BASE + i, r,
  1033. &row))
  1034. action_bss = i;
  1035. }
  1036. *y += WIFI_ROW_H;
  1037. }
  1038. }
  1039. /* Top summary: battery/AC on the left, wall clock on the right. */
  1040. static void draw_summary(struct ngui_rect r)
  1041. {
  1042. char tbuf[16], dbuf[16], title[32];
  1043. char *p;
  1044. int batt = -1, charging = 0, discharging = 0, ac = -1;
  1045. int tw;
  1046. unsigned short dot;
  1047. const char *sub_p = "";
  1048. unsigned int i;
  1049. if (power_ok) {
  1050. ac = powerinfo.ac_online;
  1051. for (i = 0; i < SYS_POWER_BATT_MAX; i++) {
  1052. struct sys_battery *b = &powerinfo.battery[i];
  1053. if (!b->present)
  1054. continue;
  1055. if (b->percent_valid)
  1056. batt = (int)b->percent;
  1057. charging = (int)b->charging;
  1058. discharging = (int)b->discharging;
  1059. break;
  1060. }
  1061. }
  1062. if (batt >= 0) {
  1063. p = sput(title, "Battery ");
  1064. p = put_dec(p, (unsigned int)batt);
  1065. *p++ = '%';
  1066. *p = '\0';
  1067. if (charging)
  1068. sub_p = "Charging";
  1069. else if (discharging)
  1070. sub_p = "On battery";
  1071. else if (ac > 0)
  1072. sub_p = "Plugged in";
  1073. else
  1074. sub_p = "";
  1075. dot = batt <= 10 ? ui.theme.error :
  1076. batt <= 25 ? ui.theme.warning : ui.theme.success;
  1077. } else if (power_ok && ac > 0) {
  1078. sput(title, "AC power");
  1079. sub_p = "No battery";
  1080. dot = ui.theme.success;
  1081. } else if (power_ok) {
  1082. sput(title, "Desktop");
  1083. sub_p = "No battery";
  1084. dot = ui.theme.text_dim;
  1085. } else {
  1086. sput(title, "Power");
  1087. sub_p = "Unavailable";
  1088. dot = ui.theme.warning;
  1089. }
  1090. ngui_fill_round_rect(&ui, r, 10, 10, 10, 10, ui.theme.panel);
  1091. ngui_fill_round_rect(&ui,
  1092. (struct ngui_rect){ r.x + 14, r.y + 23, 10, 10 },
  1093. 5, 5, 5, 5, dot);
  1094. ngui_text_px(&ui, r.x + 30, r.y + 8, title, slen(title),
  1095. ui.theme.text, 14, 1, 0, 0);
  1096. ngui_text_ellipsis_utf8(&ui,
  1097. (struct ngui_rect){ r.x + 30, r.y + 30, r.w / 2 - 34, 18 },
  1098. r.x + 30, r.y + 30, sub_p, ui.theme.text_dim);
  1099. ngui_fill_rect(&ui,
  1100. (struct ngui_rect){ r.x + r.w / 2, r.y + 10, 1, r.h - 20 },
  1101. ui.theme.button);
  1102. if (now_ok) {
  1103. format_time(tbuf, &now);
  1104. format_date(dbuf, &now);
  1105. } else {
  1106. sput(tbuf, "--:--");
  1107. sput(dbuf, "No clock");
  1108. }
  1109. tw = (int)ngui_text_width_ui(&ui, tbuf);
  1110. ngui_text_px(&ui, r.x + r.w - 14 - tw, r.y + 8, tbuf, slen(tbuf),
  1111. ui.theme.text, 14, 1, 0, 0);
  1112. tw = (int)ngui_text_width_ui(&ui, dbuf);
  1113. ngui_text_px(&ui, r.x + r.w - 14 - tw, r.y + 30, dbuf, slen(dbuf),
  1114. ui.theme.text_dim, 14, 0, 0, 0);
  1115. }
  1116. /* Brightness slider. Only drawn when a panel PWM exists. */
  1117. static void draw_brightness(struct ngui_rect r)
  1118. {
  1119. struct ngui_rect slider = { r.x + 14, r.y + 26, r.w - 28, 16 };
  1120. char b[8], *p;
  1121. int v = brightness;
  1122. int tw;
  1123. ngui_fill_round_rect(&ui, r, 10, 10, 10, 10, ui.theme.panel);
  1124. ngui_text_px(&ui, r.x + 14, r.y + 6, "Brightness", 10,
  1125. ui.theme.text, 14, 1, 0, 0);
  1126. p = put_dec(b, (unsigned int)(v < 0 ? 0 : v));
  1127. *p++ = '%';
  1128. *p = '\0';
  1129. tw = (int)ngui_text_width_ui(&ui, b);
  1130. ngui_text_px(&ui, r.x + r.w - 14 - tw, r.y + 6, b, slen(b),
  1131. ui.theme.text_dim, 14, 0, 0, 0);
  1132. if (ngui_slider(&ui, ID_BRIGHT, &brightness_slider, slider,
  1133. &v, 5, 100)) {
  1134. int got = u_backlight_set(v);
  1135. brightness = got >= 0 ? got : v;
  1136. }
  1137. }
  1138. static const char *power_mode_name(int mode)
  1139. {
  1140. switch (mode) {
  1141. case SYS_CPUFREQ_MAX:
  1142. return "Performance";
  1143. case SYS_CPUFREQ_MIN:
  1144. return "Power saver";
  1145. case SYS_CPUFREQ_OFF:
  1146. return "Off";
  1147. case SYS_CPUFREQ_FIXED:
  1148. return "Fixed";
  1149. case SYS_CPUFREQ_AUTO:
  1150. return "Balanced";
  1151. default:
  1152. return "Unavailable";
  1153. }
  1154. }
  1155. static int quick_tile_count(void)
  1156. {
  1157. return 2 + (cpufreq_mode >= 0 ? 1 : 0);
  1158. }
  1159. static int quick_tile_height(int n)
  1160. {
  1161. int rows = (n + 1) / 2;
  1162. return rows * 56 + (rows - 1) * MENU_GAP;
  1163. }
  1164. static void draw_tiles(struct ngui_rect area, int *yy)
  1165. {
  1166. unsigned int ids[3];
  1167. char titles[3][16], subs[3][48];
  1168. int ons[3], acts[3], n = 0, i, row = 0;
  1169. int nrows, colw;
  1170. ids[n] = ID_TILE_NETWORK;
  1171. sput(titles[n], "Network");
  1172. if (net_present && seq(net.name, "wifi") &&
  1173. wst.state >= SYS_WIFI_ST_ASSOC && wst.ssid_len) {
  1174. unsigned int j, len = wst.ssid_len;
  1175. char *sp = subs[n];
  1176. if (len > 32)
  1177. len = 32;
  1178. for (j = 0; j < len; j++)
  1179. *sp++ = (char)wst.ssid[j];
  1180. *sp = '\0';
  1181. } else if (net_present && seq(net.name, "wifi")) {
  1182. sput(subs[n], "Wi-Fi");
  1183. } else if (net_present) {
  1184. sput(subs[n], net.name);
  1185. } else if (wifi_present) {
  1186. sput(subs[n], "Wi-Fi");
  1187. } else {
  1188. sput(subs[n], "Offline");
  1189. }
  1190. ons[n] = expanded == EXPAND_NETWORK;
  1191. acts[n] = ACT_NET_TOGGLE;
  1192. n++;
  1193. if (cpufreq_mode >= 0) {
  1194. ids[n] = ID_TILE_POWER;
  1195. sput(titles[n], "Power mode");
  1196. sput(subs[n], power_mode_name(cpufreq_mode));
  1197. ons[n] = expanded == EXPAND_POWER;
  1198. acts[n] = ACT_POWER_TOGGLE;
  1199. n++;
  1200. }
  1201. ids[n] = ID_TILE_DATE;
  1202. sput(titles[n], "Date & Time");
  1203. if (now_ok)
  1204. format_time(subs[n], &now);
  1205. else
  1206. sput(subs[n], "Set clock");
  1207. ons[n] = expanded == EXPAND_DATE;
  1208. acts[n] = ACT_DATE_TOGGLE;
  1209. n++;
  1210. nrows = (n + 1) / 2;
  1211. colw = (area.w - MENU_GAP) / 2;
  1212. for (i = 0; i < n; i++) {
  1213. int col = i & 1;
  1214. int w = (i + 1 < n) ? colw : (col == 0 ? area.w :
  1215. colw);
  1216. struct ngui_rect r = {
  1217. area.x + col * (colw + MENU_GAP),
  1218. *yy,
  1219. w,
  1220. 56
  1221. };
  1222. if (r.y + r.h > area.y && r.y < area.y + area.h &&
  1223. ngui_tile(&ui, ids[i], r, titles[i], subs[i], ons[i]))
  1224. actions |= (unsigned int)acts[i];
  1225. if (col == 1 || i + 1 >= n) {
  1226. *yy += 56;
  1227. row++;
  1228. if (row < nrows)
  1229. *yy += MENU_GAP;
  1230. }
  1231. }
  1232. }
  1233. static int network_content_height(void)
  1234. {
  1235. int i, shown = 0, more, h;
  1236. for (i = 0; i < ndev; i++)
  1237. if (devs[i].kind != SYS_NETDEV_WIFI)
  1238. shown++;
  1239. more = shown > 4 ? shown - 4 : 0;
  1240. if (shown > 4)
  1241. shown = 4;
  1242. h = SECTION_H + 64 + MENU_GAP;
  1243. h += SECTION_H + (shown ? shown * DEV_ROW_H : DEV_ROW_H) +
  1244. (more ? 20 : 0) + MENU_GAP;
  1245. h += SECTION_H + ((!wifi_present || scanning || nbss == 0) ?
  1246. WIFI_ROW_H : nbss * WIFI_ROW_H);
  1247. if (net_present)
  1248. h += MENU_GAP + SECTION_H + 4 * 30;
  1249. if (nsaved)
  1250. h += MENU_GAP + SAVED_ROW_H;
  1251. return h;
  1252. }
  1253. static void draw_network_section(struct ngui_rect area, int *yy)
  1254. {
  1255. struct ngui_row row;
  1256. struct ngui_rect r;
  1257. int i, shown = 0, more, n, y = *yy;
  1258. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  1259. ngui_section(&ui, r, "Network", 0, 0);
  1260. y += SECTION_H;
  1261. r = (struct ngui_rect){ area.x, y, area.w, 64 };
  1262. draw_status_card(r);
  1263. y += 64 + MENU_GAP;
  1264. for (i = 0; i < ndev; i++)
  1265. if (devs[i].kind != SYS_NETDEV_WIFI)
  1266. shown++;
  1267. more = shown > 4 ? shown - 4 : 0;
  1268. if (shown > 4)
  1269. shown = 4;
  1270. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  1271. if (ngui_section(&ui, r, "Devices", ID_REFRESH, "Refresh"))
  1272. actions |= ACT_REFRESH;
  1273. y += SECTION_H;
  1274. n = 0;
  1275. for (i = 0; i < ndev && n < shown; i++) {
  1276. if (devs[i].kind == SYS_NETDEV_WIFI)
  1277. continue;
  1278. r = (struct ngui_rect){ area.x, y, area.w, DEV_ROW_H };
  1279. if (r.y + r.h > area.y && r.y < area.y + area.h) {
  1280. row.title = devs[i].name;
  1281. row.subtitle = devs[i].chip;
  1282. row.trailing = devs[i].active ? "Connected" :
  1283. (devs[i].link ? "link" : 0);
  1284. row.bars = -1;
  1285. row.selected = devs[i].active;
  1286. if (ngui_list_row(&ui, ID_DEV_BASE + i, r, &row))
  1287. action_dev = i;
  1288. }
  1289. y += DEV_ROW_H;
  1290. n++;
  1291. }
  1292. if (!shown) {
  1293. const char *msg = wifi_present ?
  1294. "No wired or USB device" :
  1295. "No device found - plug one in and Refresh";
  1296. ngui_text(&ui, area.x + 12, y + 12, msg,
  1297. ui.theme.text_dim);
  1298. y += DEV_ROW_H;
  1299. } else if (more) {
  1300. char line[32], *p = line;
  1301. p = sput(p, "+");
  1302. p = put_dec(p, (unsigned int)more);
  1303. p = sput(p, " more devices");
  1304. *p = '\0';
  1305. ngui_text(&ui, area.x + 12, y + 2, line,
  1306. ui.theme.text_dim);
  1307. y += 20;
  1308. }
  1309. y += MENU_GAP;
  1310. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  1311. if (wifi_present && wst.state >= SYS_WIFI_ST_ASSOC) {
  1312. if (ngui_section(&ui, r, "Wi-Fi", ID_DISCONNECT,
  1313. "Disconnect"))
  1314. actions |= ACT_DISCONNECT;
  1315. } else if (wifi_present && !scanning) {
  1316. if (ngui_section(&ui, r, "Wi-Fi", ID_SCAN, "Scan"))
  1317. actions |= ACT_SCAN;
  1318. } else {
  1319. ngui_section(&ui, r, "Wi-Fi", 0, 0);
  1320. }
  1321. y += SECTION_H;
  1322. draw_wifi_list(area, &y);
  1323. if (net_present) {
  1324. static const char *labels[4] = {
  1325. "IP address", "Gateway", "DNS", "MAC address"
  1326. };
  1327. char value[40];
  1328. int di;
  1329. y += MENU_GAP;
  1330. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  1331. ngui_section(&ui, r, "Connection", 0, 0);
  1332. y += SECTION_H;
  1333. for (di = 0; di < 4; di++) {
  1334. char *vp = value;
  1335. if (di == 0)
  1336. vp = ip_any(net.ip) ? put_ip(vp, net.ip) :
  1337. sput(vp, "not configured");
  1338. else if (di == 1)
  1339. vp = ip_any(net.gw) ? put_ip(vp, net.gw) :
  1340. sput(vp, "-");
  1341. else if (di == 2)
  1342. vp = ip_any(net.dns) ? put_ip(vp, net.dns) :
  1343. sput(vp, "-");
  1344. else
  1345. vp = put_mac(vp, net.mac);
  1346. *vp = '\0';
  1347. r = (struct ngui_rect){ area.x, y, area.w, 30 };
  1348. if (r.y + r.h > area.y && r.y < area.y + area.h) {
  1349. row.title = labels[di];
  1350. row.subtitle = 0;
  1351. row.trailing = value;
  1352. row.bars = -1;
  1353. row.selected = 0;
  1354. ngui_list_row(&ui, ID_INFO_BASE + di, r, &row);
  1355. }
  1356. y += 30;
  1357. }
  1358. }
  1359. if (nsaved) {
  1360. char nbuf[12], *np = nbuf;
  1361. y += MENU_GAP;
  1362. r = (struct ngui_rect){ area.x, y, area.w, SAVED_ROW_H };
  1363. if (r.y + r.h > area.y && r.y < area.y + area.h) {
  1364. np = put_dec(np, (unsigned int)nsaved);
  1365. *np = '\0';
  1366. row.title = "Saved networks";
  1367. row.subtitle = 0;
  1368. row.trailing = nbuf;
  1369. row.bars = -1;
  1370. row.selected = 0;
  1371. if (ngui_list_row(&ui, ID_SAVED, r, &row))
  1372. actions |= ACT_SAVED;
  1373. }
  1374. }
  1375. *yy = y;
  1376. }
  1377. static int power_content_height(void)
  1378. {
  1379. return SECTION_H + 3 * 42 + MENU_GAP + 20;
  1380. }
  1381. static void draw_power_section(struct ngui_rect area, int *yy)
  1382. {
  1383. static const char *labels[3] = {
  1384. "Balanced", "Performance", "Power saver"
  1385. };
  1386. static const char *subs[3] = {
  1387. "Automatic race-to-idle",
  1388. "Maximum non-turbo P-state",
  1389. "Minimum P-state"
  1390. };
  1391. static const int modes[3] = {
  1392. SYS_CPUFREQ_AUTO, SYS_CPUFREQ_MAX, SYS_CPUFREQ_MIN
  1393. };
  1394. static const unsigned int acts[3] = {
  1395. ACT_POWER_AUTO, ACT_POWER_MAX, ACT_POWER_MIN
  1396. };
  1397. struct ngui_row row;
  1398. struct ngui_rect r;
  1399. int i, y = *yy;
  1400. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  1401. ngui_section(&ui, r, "Power mode", 0, 0);
  1402. y += SECTION_H;
  1403. for (i = 0; i < 3; i++) {
  1404. r = (struct ngui_rect){ area.x, y, area.w, 42 };
  1405. if (r.y + r.h > area.y && r.y < area.y + area.h) {
  1406. row.title = labels[i];
  1407. row.subtitle = subs[i];
  1408. row.trailing = 0;
  1409. row.bars = -1;
  1410. row.selected = cpufreq_mode == modes[i];
  1411. if (ngui_list_row(&ui, ID_POWER_BASE + i, r, &row))
  1412. actions |= acts[i];
  1413. }
  1414. y += 42;
  1415. }
  1416. y += MENU_GAP;
  1417. ngui_text(&ui, area.x + 12, y,
  1418. "Changes the P-state governor immediately.",
  1419. ui.theme.text_dim);
  1420. y += 20;
  1421. *yy = y;
  1422. }
  1423. static int date_content_height(void)
  1424. {
  1425. return SECTION_H + 20 + 3 * (18 + 32 + 8) + 28 + MENU_GAP + 40;
  1426. }
  1427. static void date_field(struct ngui_rect area, int *yy, const char *label,
  1428. unsigned int id, struct ngui_input *in,
  1429. unsigned int next_id)
  1430. {
  1431. struct ngui_rect r;
  1432. int ev;
  1433. r = (struct ngui_rect){ area.x + 12, *yy, area.w - 24, 18 };
  1434. ngui_text(&ui, r.x, r.y, label, ui.theme.text_dim);
  1435. *yy += 18;
  1436. r = (struct ngui_rect){ area.x + 12, *yy, area.w - 24, 32 };
  1437. ev = ngui_text_input(&ui, id, r, in);
  1438. if (ev & NGUI_INPUT_SUBMIT)
  1439. actions |= id == ID_NTP_FIELD ? ACT_DATE_NTP : ACT_DATE_SET;
  1440. if (ev & NGUI_INPUT_NEXT)
  1441. ngui_set_focus(&ui, next_id);
  1442. if (ev & NGUI_INPUT_CANCEL)
  1443. ngui_set_focus(&ui, 0);
  1444. *yy += 32 + 8;
  1445. }
  1446. static void draw_date_section(struct ngui_rect area, int *yy)
  1447. {
  1448. struct ngui_rect r, b;
  1449. char cur[64], *p;
  1450. int y = *yy;
  1451. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  1452. ngui_section(&ui, r, "Date & Time", 0, 0);
  1453. y += SECTION_H;
  1454. p = cur;
  1455. p = sput(p, "Current: ");
  1456. if (now_ok) {
  1457. format_date(p, &now);
  1458. p += slen(p);
  1459. *p++ = ' ';
  1460. format_time_sec(p, &now);
  1461. } else {
  1462. sput(p, "unavailable");
  1463. }
  1464. ngui_text(&ui, area.x + 12, y, cur, ui.theme.text_dim);
  1465. y += 20;
  1466. date_field(area, &y, "Date (YYYY-MM-DD)", ID_DATE_FIELD, &date_in,
  1467. ID_TIME_FIELD);
  1468. date_field(area, &y, "Time (HH:MM:SS)", ID_TIME_FIELD, &time_in,
  1469. ID_NTP_FIELD);
  1470. date_field(area, &y, "NTP server (UTC)", ID_NTP_FIELD, &ntp_in,
  1471. ID_DATE_FIELD);
  1472. r = (struct ngui_rect){ area.x + 12, y, 80, 28 };
  1473. b = r;
  1474. if (ngui_button(&ui, ID_DATE_SET, b, "Set"))
  1475. actions |= ACT_DATE_SET;
  1476. b.x = r.x + 88;
  1477. if (ngui_button(&ui, ID_DATE_NOW, b, "Now"))
  1478. actions |= ACT_DATE_NOW;
  1479. b.x = r.x + 176;
  1480. b.w = 110;
  1481. if (ngui_button(&ui, ID_DATE_NTP, b, "Sync NTP"))
  1482. actions |= ACT_DATE_NTP;
  1483. y += 28 + MENU_GAP;
  1484. ngui_text(&ui, area.x + 12, y,
  1485. "Sync NTP fetches UTC and writes the RTC.",
  1486. ui.theme.text_dim);
  1487. y += 20;
  1488. ngui_text(&ui, area.x + 12, y,
  1489. "Year range: 1970-2069. Enter applies.",
  1490. ui.theme.text_dim);
  1491. y += 20;
  1492. *yy = y;
  1493. }
  1494. static int panel_width(void)
  1495. {
  1496. int w = 420;
  1497. if (w > (int)screen_w - 8)
  1498. w = (int)screen_w - 8;
  1499. if (w < 340)
  1500. w = 340;
  1501. return w;
  1502. }
  1503. static int desired_panel_height(void)
  1504. {
  1505. int content, h, max;
  1506. if (view == VIEW_PASS) {
  1507. h = 340;
  1508. } else if (view == VIEW_CONNECT) {
  1509. h = 420;
  1510. } else if (view == VIEW_SAVED) {
  1511. h = 460;
  1512. } else {
  1513. content = SECTION_H + quick_tile_height(quick_tile_count());
  1514. if (expanded == EXPAND_NETWORK)
  1515. content += MENU_GAP + network_content_height();
  1516. else if (expanded == EXPAND_POWER)
  1517. content += MENU_GAP + power_content_height();
  1518. else if (expanded == EXPAND_DATE)
  1519. content += MENU_GAP + date_content_height();
  1520. h = 112 + (brightness >= 0 ? 62 : 0) + content;
  1521. }
  1522. max = (int)screen_h - 38;
  1523. if (h > max)
  1524. h = max;
  1525. if (h < 180)
  1526. h = 180;
  1527. return h;
  1528. }
  1529. static void resize_panel(void)
  1530. {
  1531. static int resize_sent;
  1532. int want = desired_panel_height();
  1533. if (resize_pending) {
  1534. if (ui.h == resize_sent)
  1535. resize_pending = 0;
  1536. return;
  1537. }
  1538. if (ui.h != want) {
  1539. if (u_win_resize_self((unsigned int)ui.w,
  1540. (unsigned int)want) == 0) {
  1541. resize_sent = want;
  1542. resize_pending = 1;
  1543. }
  1544. }
  1545. }
  1546. static void toggle_expand(int which)
  1547. {
  1548. expanded = expanded == which ? EXPAND_NONE : which;
  1549. main_scroll.offset = 0;
  1550. if (expanded == EXPAND_DATE) {
  1551. load_datetime_fields(0);
  1552. ngui_set_focus(&ui, ID_DATE_FIELD);
  1553. } else {
  1554. ngui_set_focus(&ui, 0);
  1555. }
  1556. ngui_invalidate(&ui);
  1557. }
  1558. static void draw_main(void)
  1559. {
  1560. struct ngui_rect body = pop.body;
  1561. struct ngui_rect card, bright, area;
  1562. int content_h, y;
  1563. card = ngui_cut_top(&body, 56);
  1564. draw_summary(card);
  1565. body.y += MENU_GAP;
  1566. body.h -= MENU_GAP;
  1567. if (brightness >= 0) {
  1568. bright = ngui_cut_top(&body, 52);
  1569. draw_brightness(bright);
  1570. body.y += MENU_GAP;
  1571. body.h -= MENU_GAP;
  1572. }
  1573. content_h = SECTION_H + quick_tile_height(quick_tile_count());
  1574. if (expanded == EXPAND_NETWORK)
  1575. content_h += MENU_GAP + network_content_height();
  1576. else if (expanded == EXPAND_POWER)
  1577. content_h += MENU_GAP + power_content_height();
  1578. else if (expanded == EXPAND_DATE)
  1579. content_h += MENU_GAP + date_content_height();
  1580. main_scroll.content_h = content_h;
  1581. area = body;
  1582. ngui_scroll_begin(&ui, area, &main_scroll);
  1583. ngui_scroll_wheel(&ui, area, &main_scroll);
  1584. ngui_scroll_keys(&ui, &main_scroll);
  1585. y = area.y - main_scroll.offset;
  1586. {
  1587. int ty = y + (SECTION_H - 17) / 2;
  1588. ngui_text_px(&ui, area.x, ty, "Quick settings",
  1589. slen("Quick settings"), ui.theme.text_dim,
  1590. 14, 1, 0, 0);
  1591. }
  1592. y += SECTION_H;
  1593. draw_tiles(area, &y);
  1594. if (expanded == EXPAND_NETWORK) {
  1595. y += MENU_GAP;
  1596. draw_network_section(area, &y);
  1597. } else if (expanded == EXPAND_POWER) {
  1598. y += MENU_GAP;
  1599. draw_power_section(area, &y);
  1600. } else if (expanded == EXPAND_DATE) {
  1601. y += MENU_GAP;
  1602. draw_date_section(area, &y);
  1603. }
  1604. ngui_scroll_end(&ui, area, &main_scroll);
  1605. }
  1606. static void draw_pass(void)
  1607. {
  1608. struct ngui_rect body = pop.body;
  1609. struct ngui_rect r, b1, b2;
  1610. char line[80], *p;
  1611. int ev;
  1612. r = ngui_cut_top(&body, 24);
  1613. p = sput(line, "Join \"");
  1614. p = sput(p, sel_ssid);
  1615. p = sput(p, "\"");
  1616. *p = '\0';
  1617. ngui_text_ellipsis_utf8(&ui, r, r.x, r.y, line, ui.theme.text);
  1618. r = ngui_cut_top(&body, 22);
  1619. ngui_text_px(&ui, r.x, r.y, "WPA2-PSK passphrase", 19,
  1620. ui.theme.text_dim, 14, 1, 0, 0);
  1621. r = ngui_cut_top(&body, 34);
  1622. ev = ngui_text_input(&ui, ID_PASS, r, &pass_in);
  1623. if (ev & NGUI_INPUT_SUBMIT)
  1624. actions |= ACT_CONNECT;
  1625. if (ev & NGUI_INPUT_CANCEL)
  1626. actions |= ACT_BACK;
  1627. body.y += MENU_GAP;
  1628. body.h -= MENU_GAP;
  1629. r = ngui_cut_top(&body, 22);
  1630. ngui_text(&ui, r.x, r.y,
  1631. "8-63 characters, or 64 hex digits for a raw PSK.",
  1632. ui.theme.text_dim);
  1633. body.y += MENU_GAP;
  1634. body.h -= MENU_GAP;
  1635. r = ngui_cut_top(&body, 28);
  1636. b1 = r;
  1637. b1.w = 110;
  1638. b2 = r;
  1639. b2.x = r.x + 120;
  1640. b2.w = 110;
  1641. if (ngui_button(&ui, ID_PASS_OK, b1, "Connect"))
  1642. actions |= ACT_CONNECT;
  1643. if (ngui_button(&ui, ID_PASS_CANCEL, b2, "Cancel"))
  1644. actions |= ACT_BACK;
  1645. }
  1646. static void draw_connect(void)
  1647. {
  1648. struct ngui_rect body = pop.body;
  1649. struct ngui_rect log, b, r;
  1650. char line[96], *p;
  1651. unsigned int start = 0, lines = 0, i;
  1652. int y, line_y = 0;
  1653. r = ngui_cut_top(&body, 22);
  1654. p = sput(line, connect_pid > 0 ? "Connecting to \"" :
  1655. "Connection to \"");
  1656. p = sput(p, sel_ssid);
  1657. p = sput(p, "\"");
  1658. *p = '\0';
  1659. ngui_text_ellipsis_utf8(&ui, r, r.x, r.y, line, ui.theme.text);
  1660. /* The supplicant trail in a recessed card; the connection state is in
  1661. * the popover footer. */
  1662. log = body;
  1663. log.h -= 38;
  1664. ngui_fill_round_rect(&ui, log, 6, 6, 6, 6, ui.theme.panel);
  1665. ngui_push_clip(&ui, (struct ngui_rect){ log.x + 8, log.y + 8,
  1666. log.w - 16, log.h - 16 });
  1667. for (i = conn_log_len; i > 0; i--) {
  1668. if (conn_log[i - 1] == '\n' && ++lines > 12) {
  1669. start = i;
  1670. break;
  1671. }
  1672. }
  1673. y = log.y + 10;
  1674. for (i = start; i < conn_log_len && y + 17 <= log.y + log.h - 8; i++) {
  1675. char c = conn_log[i];
  1676. if (c == '\r')
  1677. continue;
  1678. if (c == '\n') {
  1679. line[line_y] = '\0';
  1680. if (line_y)
  1681. ngui_text_px(&ui, log.x + 10, y, line,
  1682. (unsigned int)line_y,
  1683. ui.theme.text_dim, 14, 0, 0, 1);
  1684. y += 17;
  1685. line_y = 0;
  1686. continue;
  1687. }
  1688. if ((unsigned int)line_y + 1 < sizeof(line))
  1689. line[line_y++] = c;
  1690. }
  1691. if (line_y) {
  1692. line[line_y] = '\0';
  1693. ngui_text_px(&ui, log.x + 10, y, line, (unsigned int)line_y,
  1694. ui.theme.text_dim, 14, 0, 0, 1);
  1695. }
  1696. ngui_pop_clip(&ui);
  1697. b.x = body.x;
  1698. b.y = body.y + body.h - 28;
  1699. b.w = 110;
  1700. b.h = 28;
  1701. if (ngui_button(&ui, ID_CONN_CANCEL, b,
  1702. connect_pid > 0 ? "Cancel" : "Back"))
  1703. actions |= ACT_BACK;
  1704. }
  1705. static void draw_saved(void)
  1706. {
  1707. struct ngui_rect body = pop.body;
  1708. struct ngui_rect back, row, bt, list, r;
  1709. struct ngui_row desc;
  1710. char ssid[33];
  1711. int i;
  1712. r = ngui_cut_top(&body, 22);
  1713. ngui_text_px(&ui, r.x, r.y, "Saved Wi-Fi networks", 20,
  1714. ui.theme.text, 14, 1, 0, 0);
  1715. if (!nsaved) {
  1716. ngui_text(&ui, body.x + 2, body.y + 4,
  1717. "No saved networks", ui.theme.text_dim);
  1718. } else {
  1719. list = body;
  1720. list.h -= 38;
  1721. saved_scroll.content_h = nsaved * SAVED_LIST_ROW_H;
  1722. ngui_scroll_begin(&ui, list, &saved_scroll);
  1723. ngui_scroll_wheel(&ui, list, &saved_scroll);
  1724. ngui_scroll_keys(&ui, &saved_scroll);
  1725. for (i = 0; i < nsaved; i++) {
  1726. row.x = list.x;
  1727. row.y = list.y - saved_scroll.offset +
  1728. i * SAVED_LIST_ROW_H;
  1729. row.w = list.w;
  1730. row.h = SAVED_LIST_ROW_H;
  1731. if (row.y + row.h <= list.y ||
  1732. row.y >= list.y + list.h)
  1733. continue;
  1734. cfg_ssid_name(&saved[i], ssid);
  1735. bt = row;
  1736. bt.x = row.x + row.w - 64;
  1737. bt.w = 60;
  1738. bt.h = 28;
  1739. bt.y = row.y + 7;
  1740. row.w -= 76;
  1741. desc.title = ssid;
  1742. desc.subtitle = saved[i].pass_len ? "WPA2-PSK" : "Open";
  1743. desc.trailing = 0;
  1744. desc.bars = -1;
  1745. desc.selected = 0;
  1746. ngui_list_row_utf8(&ui, ID_SAVED_ROW_BASE + i, row,
  1747. &desc);
  1748. if (ngui_button(&ui, ID_SAVED_BASE + i, bt,
  1749. "Forget")) {
  1750. action_saved = i;
  1751. actions |= ACT_FORGET;
  1752. }
  1753. }
  1754. ngui_scroll_end(&ui, list, &saved_scroll);
  1755. }
  1756. back.x = body.x;
  1757. back.y = body.y + body.h - 28;
  1758. back.w = 100;
  1759. back.h = 28;
  1760. if (ngui_button(&ui, ID_SAVED_BACK, back, "Back"))
  1761. actions |= ACT_BACK;
  1762. }
  1763. static void draw(void)
  1764. {
  1765. ngui_popover_begin(&ui, &pop);
  1766. if (view == VIEW_PASS)
  1767. draw_pass();
  1768. else if (view == VIEW_CONNECT)
  1769. draw_connect();
  1770. else if (view == VIEW_SAVED)
  1771. draw_saved();
  1772. else
  1773. draw_main();
  1774. draw_status();
  1775. }
  1776. static void paint_frame(void)
  1777. {
  1778. if (ngui_begin(&ui)) {
  1779. draw();
  1780. ngui_end(&ui);
  1781. }
  1782. actions = 0;
  1783. action_dev = -1;
  1784. action_bss = -1;
  1785. action_saved = -1;
  1786. }
  1787. void _start(const char *arg)
  1788. {
  1789. struct win_info wi;
  1790. unsigned int last_status = 0;
  1791. int w, h;
  1792. (void)arg;
  1793. u_screen_size(&screen_w, &screen_h);
  1794. view = VIEW_MAIN;
  1795. expanded = EXPAND_NONE;
  1796. main_scroll.offset = 0;
  1797. main_scroll.hide_bar = 1;
  1798. saved_scroll.hide_bar = 1;
  1799. load_devices();
  1800. load_status();
  1801. load_saved();
  1802. w = panel_width();
  1803. h = desired_panel_height();
  1804. if (u_win_create_popover((unsigned int)w, (unsigned int)h,
  1805. "Control Center", &wi) != 0 &&
  1806. u_win_create_popover((unsigned int)(w > 400 ? 400 : w),
  1807. (unsigned int)(h > 540 ? 540 : h),
  1808. "Control Center", &wi) != 0 &&
  1809. u_win_create_popover((unsigned int)(w > 380 ? 380 : w),
  1810. (unsigned int)(h > 460 ? 460 : h),
  1811. "Control Center", &wi) != 0 &&
  1812. u_win_create_popover(340, 360, "Control Center", &wi) != 0) {
  1813. u_exit(1);
  1814. }
  1815. ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
  1816. ngui_font_attach(&ui);
  1817. pass_in.secret = 1;
  1818. pass_in.placeholder = "WPA2 passphrase";
  1819. ntp_in.placeholder = "pool.ntp.org";
  1820. if (wifi_present && wst.state != SYS_WIFI_ST_DOWN &&
  1821. wst.state < SYS_WIFI_ST_ASSOC) {
  1822. start_scan();
  1823. paint_frame();
  1824. }
  1825. for (;;) {
  1826. static int first_frame = 1;
  1827. unsigned int now = u_ticks_ms();
  1828. drain_tty();
  1829. poll_scan();
  1830. poll_connect();
  1831. if (now - last_status >= 1000) {
  1832. last_status = now;
  1833. load_status();
  1834. ngui_invalidate(&ui);
  1835. }
  1836. if (status_until && (int)(now - status_until) >= 0) {
  1837. status[0] = '\0';
  1838. status_until = 0;
  1839. ngui_invalidate(&ui);
  1840. }
  1841. resize_panel();
  1842. if (ngui_begin(&ui)) {
  1843. draw();
  1844. ngui_end(&ui);
  1845. if (first_frame) {
  1846. /* The first present can race the WM's initial
  1847. * mapping of the popover canvas; paint a second
  1848. * frame once so the panel never shows a black
  1849. * tail. */
  1850. first_frame = 0;
  1851. ngui_invalidate(&ui);
  1852. }
  1853. handle_actions();
  1854. }
  1855. /* The password field owns Esc while focused. */
  1856. if (ngui_key_peek(&ui) == NGUI_KEY_ESC) {
  1857. ngui_key(&ui);
  1858. if (view == VIEW_PASS) {
  1859. view = VIEW_MAIN;
  1860. set_status("", 0);
  1861. } else if (view == VIEW_CONNECT) {
  1862. cancel_connect();
  1863. } else if (view == VIEW_SAVED) {
  1864. view = VIEW_MAIN;
  1865. set_status("", 0);
  1866. } else if (expanded != EXPAND_NONE) {
  1867. toggle_expand(expanded);
  1868. } else if (ui.focus == 0) {
  1869. break;
  1870. }
  1871. }
  1872. u_sleep_ms(25);
  1873. }
  1874. ngui_font_detach(&ui);
  1875. u_win_close();
  1876. u_exit(0);
  1877. }