ucmd_settings.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930
  1. /* settings: the real Settings window.
  2. *
  3. * A normal titled canvas window with a section sidebar on the left and the
  4. * selected page on the right:
  5. *
  6. * Network NIC selection, DHCP, Wi-Fi scan/join/saved networks and the
  7. * current connection details
  8. * Power P-state mode, panel brightness and AC/battery state
  9. * Date & Time CMOS clock editor and SNTP sync
  10. * Appearance desktop colour and a wallpaper image picker
  11. * Windows window shadows, edge snapping and drag resistance
  12. * About version, screen, memory and temperature
  13. *
  14. * The top-bar control-center popover's pills call SYS_WM_OPEN_SETTINGS, but
  15. * a running Settings window is reused: the WM focuses it and writes the
  16. * requested section to WM_SETTINGS_REQUEST, which this app polls. Window
  17. * preferences are pushed through SYS_WM_PREFS and persisted in
  18. * /data/SETTINGS.CFG (or /share/settings.cfg before the data volume is
  19. * mounted), so the ring-3 WM applies them immediately and after a restart. */
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "ucmd_lib.h"
  23. #include "ngui.h"
  24. #include "img.h"
  25. #include "img_file.h"
  26. #include "wifi_store.h"
  27. #include "version.h"
  28. #define MAX_DEVS 8
  29. #define MAX_BSS 32
  30. #define SIDEBAR_W 190
  31. #define SIDEBAR_MIN_W 154
  32. #define NAV_ROW_H 44
  33. #define NAV_GAP 4
  34. #define HEADER_H 54
  35. #define FOOTER_H 28
  36. #define SECTION_H 28
  37. #define GAP 10
  38. #define CARD_H 64
  39. #define DEV_ROW_H 42
  40. #define BSS_ROW_H 42
  41. #define INFO_ROW_H 30
  42. #define SAVED_ROW_H 42
  43. #define STATUS_MS 6000
  44. #define PICK_MAX 96
  45. #define PICK_ROW_H 38
  46. #define PICK_PATH_MAX 256
  47. #define WALL_FILE_CAP (4u * 1024u * 1024u)
  48. #define WALL_HEAP_CAP (24u * 1024u * 1024u)
  49. #define ID_NAV_BASE 10
  50. #define ID_NET_REFRESH 100
  51. #define ID_NET_SCAN 101
  52. #define ID_NET_DISCONNECT 102
  53. #define ID_NET_SAVED 103
  54. #define ID_NET_SAVED_BACK 104
  55. #define ID_NET_PASS_INPUT 105
  56. #define ID_NET_PASS_OK 106
  57. #define ID_NET_PASS_CANCEL 107
  58. #define ID_NET_CONN_CANCEL 108
  59. #define ID_DEV_BASE 120
  60. #define ID_BSS_BASE 140
  61. #define ID_INFO_BASE 180
  62. #define ID_SAVED_ROW_BASE 200
  63. #define ID_SAVED_FORGET_BASE 230
  64. #define ID_POWER_MODE_BASE 300
  65. #define ID_BRIGHTNESS 310
  66. #define ID_DATE_FIELD 400
  67. #define ID_TIME_FIELD 401
  68. #define ID_NTP_FIELD 402
  69. #define ID_DATE_SET 403
  70. #define ID_DATE_NOW 404
  71. #define ID_DATE_NTP 405
  72. #define ID_BG_BASE 500
  73. #define ID_WALL_CHOOSE 540
  74. #define ID_WALL_REMOVE 541
  75. #define ID_PICK_UP 550
  76. #define ID_PICK_CANCEL 551
  77. #define ID_PICK_ROW_BASE 600
  78. #define ID_WIN_SHADOW 750
  79. #define ID_WIN_SNAP 751
  80. #define ID_WIN_RESIST 752
  81. enum {
  82. SEC_NETWORK,
  83. SEC_POWER,
  84. SEC_DATETIME,
  85. SEC_APPEARANCE,
  86. SEC_WINDOWS,
  87. SEC_ABOUT,
  88. SEC_COUNT
  89. };
  90. enum {
  91. NET_MAIN,
  92. NET_PASS,
  93. NET_CONNECT,
  94. NET_SAVED
  95. };
  96. enum {
  97. A_NONE = 0,
  98. A_NAV,
  99. A_REFRESH,
  100. A_SCAN,
  101. A_DISCONNECT,
  102. A_DEV,
  103. A_BSS,
  104. A_SAVED,
  105. A_SAVED_BACK,
  106. A_FORGET,
  107. A_PASS_OK,
  108. A_PASS_CANCEL,
  109. A_CONN_CANCEL,
  110. A_POWER,
  111. A_DATE_SET,
  112. A_DATE_NOW,
  113. A_DATE_NTP,
  114. A_BG,
  115. A_WALL_CHOOSE,
  116. A_WALL_REMOVE,
  117. A_PICK_UP,
  118. A_PICK_CANCEL,
  119. A_PICK_ROW,
  120. A_WIN_SHADOW,
  121. A_WIN_SNAP,
  122. A_WIN_RESIST
  123. };
  124. struct section_info {
  125. const char *label;
  126. const char *subtitle;
  127. const char *arg;
  128. };
  129. static const struct section_info sections[SEC_COUNT] = {
  130. { "Network", "Devices and Wi-Fi", "network" },
  131. { "Power", "Mode and brightness", "power" },
  132. { "Date & Time", "Clock and time sync", "time" },
  133. { "Appearance", "Desktop background", "appearance" },
  134. { "Windows", "Shadows and snapping", "windows" },
  135. { "About", "System information", "about" }
  136. };
  137. struct bg_choice {
  138. const char *name;
  139. unsigned int rgb;
  140. };
  141. static const struct bg_choice bg_choices[] = {
  142. { "Midnight", 0x202030u },
  143. { "Deep blue", 0x152238u },
  144. { "Forest", 0x18281Cu },
  145. { "Plum", 0x2A1A2Eu },
  146. { "Cocoa", 0x2C2118u },
  147. { "Graphite", 0x222428u },
  148. { "Slate", 0x23303Au },
  149. { "Wine", 0x301A22u }
  150. };
  151. #define NBG ((int)(sizeof(bg_choices) / sizeof(bg_choices[0])))
  152. static struct ngui ui;
  153. static int cur_section = SEC_NETWORK;
  154. static int quit;
  155. static int pending_action;
  156. static int pending_arg;
  157. static struct ngui_scroll content_scroll;
  158. static struct ngui_slider brightness_slider;
  159. static char status[160];
  160. static int status_error;
  161. static unsigned int status_until;
  162. static unsigned int last_status_ms;
  163. static unsigned int last_request_ms;
  164. static struct wm_prefs prefs;
  165. /* Network state. */
  166. static struct sys_netdev devs[MAX_DEVS];
  167. static int ndev;
  168. static int devs_loaded;
  169. static struct sys_wifi_bss bss[MAX_BSS];
  170. static int nbss;
  171. static struct wifi_cfg saved[WIFI_CFG_MAX];
  172. static int nsaved;
  173. static struct sys_wifi_status wst;
  174. static struct sys_netinfo net;
  175. static int net_present;
  176. static int wifi_checked;
  177. static int wifi_present;
  178. static int net_view = NET_MAIN;
  179. static int sel_bss = -1;
  180. static char sel_ssid[33];
  181. static int scanning;
  182. static unsigned int scan_start;
  183. static int connect_pid = -1;
  184. static int connect_tty;
  185. static unsigned int connect_start;
  186. static char conn_log[2048];
  187. static unsigned int conn_log_len;
  188. static struct ngui_input pass_in;
  189. /* Power state. */
  190. static int brightness = -1;
  191. static int cpufreq_mode = -1;
  192. static struct sys_powerinfo powerinfo;
  193. static int power_ok;
  194. /* Clock state. */
  195. static struct sys_datetime now;
  196. static int now_ok;
  197. static int date_loaded;
  198. static struct ngui_input date_in;
  199. static struct ngui_input time_in;
  200. static struct ngui_input ntp_in;
  201. /* Appearance state. */
  202. static int wallpaper_present;
  203. static char wallpaper_source[32];
  204. static int picker_mode;
  205. static char pick_path[PICK_PATH_MAX];
  206. static char pick_names[PICK_MAX][64];
  207. static unsigned int pick_sizes[PICK_MAX];
  208. static unsigned char pick_dirs[PICK_MAX];
  209. static int pick_count;
  210. static struct ngui_scroll picker_scroll;
  211. static unsigned int s_len(const char *s)
  212. {
  213. unsigned int n = 0;
  214. while (s && s[n])
  215. n++;
  216. return n;
  217. }
  218. static void settings_draw_frame(void);
  219. static void draw_picker(struct ngui_rect area, int *yy);
  220. static void set_wallpaper_file(const char *path);
  221. static int s_eq(const char *a, const char *b)
  222. {
  223. while (*a && *a == *b) {
  224. a++;
  225. b++;
  226. }
  227. return *a == *b;
  228. }
  229. static void s_copy(char *d, const char *s, unsigned int cap)
  230. {
  231. unsigned int i = 0;
  232. if (!cap)
  233. return;
  234. while (s && s[i] && i + 1 < cap) {
  235. d[i] = s[i];
  236. i++;
  237. }
  238. d[i] = '\0';
  239. }
  240. static char *s_put(char *p, const char *s)
  241. {
  242. while (s && *s)
  243. *p++ = *s++;
  244. *p = '\0';
  245. return p;
  246. }
  247. static char *s_put_dec(char *p, unsigned int v)
  248. {
  249. char b[10];
  250. unsigned int n = 0;
  251. do {
  252. b[n++] = (char)('0' + v % 10);
  253. v /= 10;
  254. } while (v);
  255. while (n)
  256. *p++ = b[--n];
  257. return p;
  258. }
  259. static char *s_put_2(char *p, unsigned int v)
  260. {
  261. *p++ = (char)('0' + (v / 10) % 10);
  262. *p++ = (char)('0' + v % 10);
  263. return p;
  264. }
  265. static void set_status(const char *msg, int err)
  266. {
  267. unsigned int i;
  268. for (i = 0; msg && msg[i] && i + 1 < sizeof(status); i++)
  269. status[i] = msg[i];
  270. status[i] = '\0';
  271. status_error = err;
  272. status_until = msg && msg[0] ? u_ticks_ms() + STATUS_MS : 0;
  273. ngui_invalidate(&ui);
  274. }
  275. static void request_action(int action, int arg)
  276. {
  277. if (!pending_action) {
  278. pending_action = action;
  279. pending_arg = arg;
  280. }
  281. }
  282. /* Paint the current state before a blocking action (DHCP, a WiFi scan or an
  283. * NTP request), so the message and the pressed control are visible. */
  284. static void paint_current(void)
  285. {
  286. if (ngui_begin(&ui)) {
  287. settings_draw_frame();
  288. ngui_end(&ui);
  289. }
  290. }
  291. static int section_from_name(const char *name)
  292. {
  293. int i;
  294. if (!name || !name[0])
  295. return SEC_NETWORK;
  296. for (i = 0; i < SEC_COUNT; i++)
  297. if (s_eq(name, sections[i].arg))
  298. return i;
  299. return SEC_NETWORK;
  300. }
  301. /* ------------------------------------------------------------------ */
  302. /* Window-manager preferences */
  303. static void prefs_defaults(void)
  304. {
  305. memset(&prefs, 0, sizeof(prefs));
  306. prefs.shadow = 1;
  307. prefs.snap = 1;
  308. prefs.resist = 1;
  309. prefs.bg_rgb = WM_DEFAULT_BG_RGB;
  310. }
  311. static int prefs_file_magic_ok(const struct wm_prefs_file *f)
  312. {
  313. unsigned int i;
  314. for (i = 0; i < sizeof(f->magic); i++)
  315. if (f->magic[i] != WM_PREFS_MAGIC[i])
  316. return 0;
  317. return 1;
  318. }
  319. static int prefs_file_load(void)
  320. {
  321. /* A FAT read may write a final partial sector past the requested
  322. * length, so keep a sector of slack around the header. */
  323. unsigned char buf[sizeof(struct wm_prefs_file) + 512];
  324. struct wm_prefs_file *f = (struct wm_prefs_file *)buf;
  325. const char *paths[2] = { WM_PREFS_DATA, WM_PREFS_SHARE };
  326. int i;
  327. for (i = 0; i < 2; i++) {
  328. int n = u_syscall3(SYS_READ_FILE, (int)paths[i], (int)buf,
  329. (int)sizeof(*f));
  330. if (n < (int)sizeof(*f) || !prefs_file_magic_ok(f) ||
  331. f->version != WM_PREFS_VERSION)
  332. continue;
  333. prefs = f->prefs;
  334. prefs.shadow = prefs.shadow ? 1u : 0u;
  335. prefs.snap = prefs.snap ? 1u : 0u;
  336. prefs.resist = prefs.resist ? 1u : 0u;
  337. prefs.bg_rgb &= 0x00FFFFFFu;
  338. memset(prefs.reserved, 0, sizeof(prefs.reserved));
  339. return 0;
  340. }
  341. return -1;
  342. }
  343. static int prefs_file_save(void)
  344. {
  345. struct wm_prefs_file f;
  346. struct sys_stat st;
  347. const char *path = WM_PREFS_SHARE;
  348. memset(&f, 0, sizeof(f));
  349. memcpy(f.magic, WM_PREFS_MAGIC, sizeof(f.magic));
  350. f.version = WM_PREFS_VERSION;
  351. f.prefs = prefs;
  352. if (u_stat("/data", &st) == 0 && st.is_dir)
  353. path = WM_PREFS_DATA;
  354. return u_syscall3(SYS_WRITE_FILE, (int)path, (int)&f, (int)sizeof(f));
  355. }
  356. static void commit_prefs(const char *ok_msg)
  357. {
  358. if (u_wm_set_prefs(&prefs) != 0) {
  359. set_status("The window manager refused the settings", 1);
  360. return;
  361. }
  362. if (prefs_file_save() < 0)
  363. set_status("Applied, but the settings file could not be saved", 1);
  364. else if (ok_msg && ok_msg[0])
  365. set_status(ok_msg, 0);
  366. else
  367. set_status("Settings saved", 0);
  368. }
  369. /* ------------------------------------------------------------------ */
  370. /* Small drawing helpers */
  371. static unsigned short rgb565(unsigned int rgb)
  372. {
  373. return (unsigned short)((((rgb >> 16) & 0xFF) >> 3) << 11 |
  374. (((rgb >> 8) & 0xFF) >> 2) << 5 |
  375. ((rgb & 0xFF) >> 3));
  376. }
  377. static void format_size(char *b, unsigned int bytes)
  378. {
  379. char *p = b;
  380. if (bytes >= 1024u * 1024u) {
  381. p = s_put_dec(p, bytes / (1024u * 1024u));
  382. p = s_put(p, " MB");
  383. } else if (bytes >= 1024u) {
  384. p = s_put_dec(p, (bytes + 1023u) / 1024u);
  385. p = s_put(p, " KB");
  386. } else {
  387. p = s_put_dec(p, bytes);
  388. p = s_put(p, " B");
  389. }
  390. *p = '\0';
  391. }
  392. /* One clickable settings row: an optional colour swatch, a title, a dim
  393. * subtitle and a right-aligned value. Mirrors the ngui list-row press /
  394. * release semantics so clicks agree with the rest of the toolkit. */
  395. static int settings_row(unsigned int id, struct ngui_rect r,
  396. const char *title, const char *subtitle,
  397. const char *trailing, int selected, int swatch_rgb)
  398. {
  399. int hot = ui.inside && ngui_rect_contains(&r, ui.mouse_x, ui.mouse_y);
  400. int press_hot = ui.pressed &&
  401. ngui_rect_contains(&r, ui.press_x, ui.press_y);
  402. int release_hot = ngui_rect_contains(&r, ui.release_x, ui.release_y);
  403. int fired = 0;
  404. int left = r.x + 10, right = r.x + r.w - 10;
  405. int title_h, block_h, ty;
  406. unsigned short bg = 0, fg, sub_fg, trail_fg;
  407. if (press_hot)
  408. ui.active = id;
  409. if (ui.pressed && ui.released && press_hot && release_hot)
  410. fired = 1;
  411. if (ui.released && ui.active == id) {
  412. if (release_hot)
  413. fired = 1;
  414. ui.active = 0;
  415. }
  416. if (selected)
  417. bg = ui.theme.selection;
  418. else if (hot || ui.active == id)
  419. bg = ui.theme.panel_alt;
  420. if (bg)
  421. ngui_fill_round_rect(&ui, r, NGUI_RADIUS, NGUI_RADIUS,
  422. NGUI_RADIUS, NGUI_RADIUS, bg);
  423. if (swatch_rgb >= 0) {
  424. struct ngui_rect sw = { left, r.y + (r.h - 16) / 2, 16, 16 };
  425. ngui_fill_round_rect(&ui, sw, 4, 4, 4, 4,
  426. rgb565((unsigned int)swatch_rgb));
  427. ngui_frame_round(&ui, sw, 4, 4, 4, 4, 1, ui.theme.border);
  428. left += 26;
  429. }
  430. fg = ui.theme.text;
  431. sub_fg = selected ? ui.theme.text : ui.theme.text_dim;
  432. trail_fg = selected ? ui.theme.text : ui.theme.text_dim;
  433. if (trailing && trailing[0]) {
  434. int tw = (int)ngui_text_width_ui(&ui, trailing);
  435. right -= tw + 8;
  436. ngui_text_utf8(&ui, right + 8, r.y + (r.h - 16) / 2,
  437. trailing, trail_fg);
  438. }
  439. if (right <= left)
  440. return fired;
  441. title_h = ngui_text_line_height(&ui, ui.font_px, 0);
  442. block_h = subtitle && subtitle[0] ? title_h * 2 - 2 : title_h;
  443. ty = r.y + (r.h - block_h) / 2;
  444. if (ty < r.y)
  445. ty = r.y;
  446. ngui_push_clip(&ui, (struct ngui_rect){ left, r.y,
  447. right - left, r.h });
  448. ngui_text_utf8(&ui, left, ty, title, fg);
  449. if (subtitle && subtitle[0])
  450. ngui_text_utf8(&ui, left, ty + title_h - 2, subtitle, sub_fg);
  451. ngui_pop_clip(&ui);
  452. if (hot) {
  453. ui.hot = id;
  454. ngui_set_cursor(&ui, CURSOR_POINTER);
  455. }
  456. return fired;
  457. }
  458. static int info_row(struct ngui_rect area, int *y, const char *label,
  459. const char *value)
  460. {
  461. struct ngui_rect r = { area.x, *y, area.w, INFO_ROW_H };
  462. int th = ngui_text_line_height(&ui, ui.font_px, 0);
  463. ngui_text_utf8(&ui, r.x + 10, r.y + (r.h - th) / 2, label,
  464. ui.theme.text_dim);
  465. ngui_text_utf8(&ui, r.x + r.w / 2, r.y + (r.h - th) / 2, value,
  466. ui.theme.text);
  467. *y += INFO_ROW_H;
  468. return 0;
  469. }
  470. /* ------------------------------------------------------------------ */
  471. /* Network */
  472. static void drain_tty(void)
  473. {
  474. char buf[256];
  475. int n;
  476. if (connect_tty <= 0)
  477. return;
  478. while ((n = u_tty_read(connect_tty, buf, sizeof(buf))) > 0) {
  479. int i;
  480. for (i = 0; i < n; i++) {
  481. if (conn_log_len + 1 >= sizeof(conn_log)) {
  482. unsigned int half = sizeof(conn_log) / 2;
  483. unsigned int j;
  484. for (j = 0; j < half; j++)
  485. conn_log[j] = conn_log[conn_log_len -
  486. half + j];
  487. conn_log_len = half;
  488. }
  489. conn_log[conn_log_len++] = buf[i];
  490. }
  491. conn_log[conn_log_len] = '\0';
  492. ngui_invalidate(&ui);
  493. }
  494. }
  495. static void load_saved(void)
  496. {
  497. int n = wifi_store_load(saved, WIFI_CFG_MAX);
  498. if (n > WIFI_CFG_MAX)
  499. n = WIFI_CFG_MAX;
  500. nsaved = n > 0 ? n : 0;
  501. }
  502. static int bss_saved(const struct sys_wifi_bss *b)
  503. {
  504. return wifi_store_find(saved, nsaved, b->ssid, b->ssid_len);
  505. }
  506. static const char *security_name(unsigned int s)
  507. {
  508. return s == SYS_WIFI_SEC_OPEN ? "open" :
  509. s == SYS_WIFI_SEC_PSK ? "WPA2" : "unsupported";
  510. }
  511. static int rssi_bars(int rssi)
  512. {
  513. if (rssi >= -55)
  514. return 4;
  515. if (rssi >= -65)
  516. return 3;
  517. if (rssi >= -75)
  518. return 2;
  519. return 1;
  520. }
  521. static int bss_is_active(const struct sys_wifi_bss *b)
  522. {
  523. unsigned int i;
  524. if (wst.state < SYS_WIFI_ST_ASSOC)
  525. return 0;
  526. for (i = 0; i < 6; i++)
  527. if (wst.bssid[i] != b->bssid[i])
  528. return 0;
  529. return 1;
  530. }
  531. static void load_status(void)
  532. {
  533. int i;
  534. net_present = (u_net_info(&net) == 1 && net.present);
  535. if (!wifi_checked || wifi_present) {
  536. if (u_wifi(SYS_WIFI_STATUS, (int)&wst, 0) == 0) {
  537. wifi_checked = 1;
  538. wifi_present = wst.present != 0;
  539. } else {
  540. wifi_checked = 1;
  541. wifi_present = 0;
  542. }
  543. }
  544. for (i = 0; i < ndev; i++) {
  545. devs[i].active = (net_present && s_eq(net.name, devs[i].name)) ?
  546. 1u : 0u;
  547. if (devs[i].active)
  548. devs[i].link = net.link;
  549. }
  550. power_ok = (u_power_info(&powerinfo, 0) == 0);
  551. now_ok = (u_rtc_get(&now) == 0);
  552. brightness = u_backlight_get();
  553. cpufreq_mode = u_cpufreq_get();
  554. }
  555. static void load_devices(void)
  556. {
  557. int n = u_net_devlist(devs, MAX_DEVS);
  558. if (n > MAX_DEVS)
  559. n = MAX_DEVS;
  560. ndev = n > 0 ? n : 0;
  561. devs_loaded = 1;
  562. load_status();
  563. }
  564. static void sort_bss(void)
  565. {
  566. int i, j;
  567. for (i = 1; i < nbss; i++)
  568. for (j = i; j > 0 && bss[j].rssi > bss[j - 1].rssi; j--) {
  569. struct sys_wifi_bss t = bss[j];
  570. bss[j] = bss[j - 1];
  571. bss[j - 1] = t;
  572. }
  573. }
  574. static int same_ssid(const struct sys_wifi_bss *a,
  575. const struct sys_wifi_bss *b)
  576. {
  577. unsigned int i;
  578. if (a->ssid_len == 0 || a->ssid_len != b->ssid_len)
  579. return 0;
  580. for (i = 0; i < a->ssid_len; i++)
  581. if (a->ssid[i] != b->ssid[i])
  582. return 0;
  583. return 1;
  584. }
  585. static void filter_bss(void)
  586. {
  587. int i, j, out = 0;
  588. for (i = 0; i < nbss; i++) {
  589. int dup = -1;
  590. if (bss[i].ssid_len == 0)
  591. continue;
  592. for (j = 0; j < out; j++)
  593. if (bss[i].security == bss[j].security &&
  594. same_ssid(&bss[i], &bss[j])) {
  595. dup = j;
  596. break;
  597. }
  598. if (dup < 0) {
  599. bss[out++] = bss[i];
  600. } else if (bss_is_active(&bss[i]) &&
  601. !bss_is_active(&bss[dup])) {
  602. bss[dup] = bss[i];
  603. }
  604. }
  605. nbss = out;
  606. }
  607. static void ssid_name(const struct sys_wifi_bss *b, char out[33])
  608. {
  609. unsigned int i, n = b->ssid_len;
  610. if (n > 32)
  611. n = 32;
  612. for (i = 0; i < n; i++)
  613. out[i] = (char)b->ssid[i];
  614. out[n] = '\0';
  615. if (!n) {
  616. s_copy(out, "(hidden)", 33);
  617. }
  618. }
  619. static void cfg_ssid_name(const struct wifi_cfg *c, char out[33])
  620. {
  621. unsigned int i, n = c->ssid_len;
  622. if (n > 32)
  623. n = 32;
  624. for (i = 0; i < n; i++)
  625. out[i] = (char)c->ssid[i];
  626. out[n] = '\0';
  627. }
  628. static void start_scan(void)
  629. {
  630. int r;
  631. if (scanning || !wifi_present)
  632. return;
  633. set_status("Scanning for WiFi networks...", 0);
  634. paint_current();
  635. r = u_wifi(SYS_WIFI_SCAN, 0, 0);
  636. if (r < 0) {
  637. char m[64], *p;
  638. u_wifi(SYS_WIFI_STATUS, (int)&wst, 0);
  639. p = s_put(m, "WiFi: ");
  640. p = s_put(p, wst.detail[0] ? wst.detail : "scan failed");
  641. *p = '\0';
  642. set_status(m, 1);
  643. return;
  644. }
  645. scanning = 1;
  646. scan_start = u_ticks_ms();
  647. nbss = 0;
  648. content_scroll.offset = 0;
  649. ngui_invalidate(&ui);
  650. }
  651. static void poll_scan(void)
  652. {
  653. int n;
  654. if (!scanning)
  655. return;
  656. n = u_wifi(SYS_WIFI_RESULTS, (int)bss, MAX_BSS);
  657. if (n == -2) {
  658. if (u_ticks_ms() - scan_start > 15000) {
  659. scanning = 0;
  660. set_status("WiFi scan timed out", 1);
  661. }
  662. return;
  663. }
  664. scanning = 0;
  665. if (n < 0) {
  666. set_status("WiFi scan failed", 1);
  667. return;
  668. }
  669. if (n > MAX_BSS)
  670. n = MAX_BSS;
  671. nbss = n;
  672. sort_bss();
  673. filter_bss();
  674. if (nbss == 0)
  675. set_status("No WiFi networks found", 1);
  676. else
  677. set_status("WiFi scan complete", 0);
  678. ngui_invalidate(&ui);
  679. }
  680. static int write_wifi_cfg(const struct sys_wifi_bss *b, const char *pass)
  681. {
  682. struct wifi_cfg cfg;
  683. unsigned int i, n = s_len(pass);
  684. int r;
  685. memset(&cfg, 0, sizeof(cfg));
  686. cfg.magic = WIFI_CFG_MAGIC;
  687. cfg.ssid_len = b->ssid_len;
  688. cfg.pass_len = (unsigned char)(n > 64 ? 64 : n);
  689. for (i = 0; i < 6; i++)
  690. cfg.bssid[i] = b->bssid[i];
  691. for (i = 0; i < b->ssid_len && i < 32; i++)
  692. cfg.ssid[i] = b->ssid[i];
  693. for (i = 0; i < cfg.pass_len; i++)
  694. cfg.pass[i] = pass[i];
  695. r = u_syscall3(SYS_WRITE_FILE, (int)WIFI_CFG_STAGED,
  696. (int)&cfg, (int)sizeof(cfg));
  697. memset(&cfg, 0, sizeof(cfg));
  698. return r;
  699. }
  700. static void start_connect(const char *pass)
  701. {
  702. struct sys_wifi_bss *b;
  703. char msg[96], *p;
  704. int pid;
  705. if (sel_bss < 0 || sel_bss >= nbss)
  706. return;
  707. if (connect_pid > 0 && u_alive((unsigned int)connect_pid)) {
  708. set_status("A WiFi connection is already in progress", 1);
  709. return;
  710. }
  711. b = &bss[sel_bss];
  712. ssid_name(b, sel_ssid);
  713. if (write_wifi_cfg(b, pass) != 0) {
  714. set_status("Cannot stage the WiFi request in /share", 1);
  715. return;
  716. }
  717. if (connect_tty <= 0) {
  718. connect_tty = u_tty_create();
  719. if (connect_tty < 0)
  720. connect_tty = 0;
  721. }
  722. conn_log_len = 0;
  723. conn_log[0] = '\0';
  724. connect_start = u_ticks_ms();
  725. pid = u_spawn_detached("/ramfs/bin/wifi",
  726. "connectfile /share/netmenu.wifi",
  727. connect_tty, "/");
  728. if (pid < 0) {
  729. u_syscall1(SYS_UNLINK, (int)"/share/netmenu.wifi");
  730. set_status("Cannot start the WiFi helper", 1);
  731. return;
  732. }
  733. connect_pid = pid;
  734. net_view = NET_CONNECT;
  735. p = s_put(msg, "Connecting to \"");
  736. p = s_put(p, sel_ssid);
  737. p = s_put(p, "\"...");
  738. *p = '\0';
  739. set_status(msg, 0);
  740. }
  741. static void cancel_connect(void)
  742. {
  743. if (connect_pid > 0 && u_alive((unsigned int)connect_pid))
  744. u_kill((unsigned int)connect_pid);
  745. connect_pid = -1;
  746. u_wifi(SYS_WIFI_DISCONNECT, 0, 0);
  747. net_view = NET_MAIN;
  748. set_status("WiFi connection cancelled", 0);
  749. load_status();
  750. }
  751. static void poll_connect(void)
  752. {
  753. int i;
  754. if (connect_pid <= 0)
  755. return;
  756. if (!u_alive((unsigned int)connect_pid)) {
  757. connect_pid = -1;
  758. load_status();
  759. load_saved();
  760. if (wst.state >= SYS_WIFI_ST_LINK) {
  761. int have_ip = 0;
  762. for (i = 0; i < 4; i++)
  763. if (net.ip[i])
  764. have_ip = 1;
  765. net_view = NET_MAIN;
  766. if (net_present && s_eq(net.name, "wifi")) {
  767. if (have_ip)
  768. set_status("WiFi connected and DHCP is up", 0);
  769. else
  770. set_status("WiFi connected, but DHCP got no lease",
  771. 1);
  772. } else {
  773. set_status("WiFi associated, but the netdev is busy",
  774. 1);
  775. }
  776. } else {
  777. set_status("WiFi connection failed", 1);
  778. }
  779. ngui_invalidate(&ui);
  780. return;
  781. }
  782. if (u_ticks_ms() - connect_start > 45000) {
  783. u_kill((unsigned int)connect_pid);
  784. connect_pid = -1;
  785. u_wifi(SYS_WIFI_DISCONNECT, 0, 0);
  786. set_status("WiFi connection timed out", 1);
  787. }
  788. }
  789. static void select_device(int i)
  790. {
  791. char msg[64], *p;
  792. if (i < 0 || i >= ndev)
  793. return;
  794. p = s_put(msg, "Selecting ");
  795. p = s_put(p, devs[i].name);
  796. *p = '\0';
  797. set_status(msg, 0);
  798. paint_current();
  799. if (u_net_select(devs[i].name) != 0) {
  800. set_status("Cannot select that netdev (busy or unavailable)", 1);
  801. return;
  802. }
  803. set_status("Running DHCP...", 0);
  804. if (u_dhcp_run(6000) == 0)
  805. set_status("Device selected; DHCP lease acquired", 0);
  806. else
  807. set_status("Device selected, but DHCP got no lease", 1);
  808. load_devices();
  809. ngui_invalidate(&ui);
  810. }
  811. static void select_bss(int i)
  812. {
  813. if (i < 0 || i >= nbss)
  814. return;
  815. if (bss[i].ssid_len == 0) {
  816. set_status("Hidden network: use `wifi connect` from a terminal",
  817. 1);
  818. return;
  819. }
  820. sel_bss = i;
  821. ssid_name(&bss[i], sel_ssid);
  822. if (bss[i].security == SYS_WIFI_SEC_OTHER) {
  823. set_status("Unsupported security (only open and WPA2-PSK)", 1);
  824. return;
  825. }
  826. if (bss[i].security == SYS_WIFI_SEC_PSK) {
  827. int si = bss_saved(&bss[i]);
  828. if (si >= 0 && saved[si].pass_len > 0) {
  829. char pass[65];
  830. unsigned int j;
  831. for (j = 0; j < saved[si].pass_len; j++)
  832. pass[j] = saved[si].pass[j];
  833. pass[saved[si].pass_len] = '\0';
  834. start_connect(pass);
  835. for (j = 0; j < sizeof(pass); j++)
  836. ((volatile char *)pass)[j] = 0;
  837. return;
  838. }
  839. ngui_input_set(&pass_in, "");
  840. pass_in.secret = 1;
  841. pass_in.placeholder = "WPA2 passphrase";
  842. net_view = NET_PASS;
  843. set_status("", 0);
  844. ngui_set_focus(&ui, ID_NET_PASS_INPUT);
  845. ngui_invalidate(&ui);
  846. return;
  847. }
  848. start_connect("");
  849. }
  850. static void draw_status_card(struct ngui_rect r)
  851. {
  852. struct ngui_rect line_r = { r.x + 34, r.y + 34, r.w - 44, 16 };
  853. struct ngui_rect dot = { r.x + 14, r.y + 25, 10, 10 };
  854. char line[160], *p;
  855. unsigned short dot_color;
  856. int i, have_ip = 0;
  857. for (i = 0; i < 4; i++)
  858. if (net.ip[i])
  859. have_ip = 1;
  860. if (!net_present)
  861. dot_color = ui.theme.error;
  862. else if (!have_ip ||
  863. (s_eq(net.name, "wifi") && wst.state < SYS_WIFI_ST_LINK))
  864. dot_color = ui.theme.warning;
  865. else
  866. dot_color = ui.theme.success;
  867. ngui_fill_round_rect(&ui, r, NGUI_RADIUS, NGUI_RADIUS,
  868. NGUI_RADIUS, NGUI_RADIUS, ui.theme.panel);
  869. ngui_frame_round(&ui, r, NGUI_RADIUS, NGUI_RADIUS,
  870. NGUI_RADIUS, NGUI_RADIUS, 1, ui.theme.panel_alt);
  871. ngui_fill_round_rect(&ui, dot, 5, 5, 5, 5, dot_color);
  872. p = line;
  873. if (!net_present)
  874. p = s_put(p, "Offline");
  875. else if (s_eq(net.name, "wifi"))
  876. p = s_put(p, wst.state >= SYS_WIFI_ST_LINK ?
  877. "Wi-Fi connected" : "Wi-Fi");
  878. else if (s_eq(net.name, "rndis"))
  879. p = s_put(p, "USB tethering");
  880. else
  881. p = s_put(p, "Wired network");
  882. *p = '\0';
  883. ngui_text_px(&ui, r.x + 34, r.y + 10, line, s_len(line),
  884. ui.theme.text, 14, 1, 0, 0);
  885. p = line;
  886. if (net_present) {
  887. p = s_put(p, net.name);
  888. p = s_put(p, " \xC2\xB7 ");
  889. if (have_ip) {
  890. p = s_put_dec(p, net.ip[0]); *p++ = '.';
  891. p = s_put_dec(p, net.ip[1]); *p++ = '.';
  892. p = s_put_dec(p, net.ip[2]); *p++ = '.';
  893. p = s_put_dec(p, net.ip[3]);
  894. } else {
  895. p = s_put(p, "no IP");
  896. }
  897. if (s_eq(net.name, "wifi") && wst.state >= SYS_WIFI_ST_ASSOC) {
  898. p = s_put(p, " \xC2\xB7 ");
  899. for (i = 0; i < wst.ssid_len && i < 32; i++)
  900. *p++ = (char)wst.ssid[i];
  901. p = s_put(p, " ");
  902. if (wst.rssi < 0) {
  903. *p++ = '-';
  904. p = s_put_dec(p, (unsigned int)(-wst.rssi));
  905. } else {
  906. p = s_put_dec(p, (unsigned int)wst.rssi);
  907. }
  908. p = s_put(p, " dBm");
  909. }
  910. } else {
  911. p = s_put(p, "No network device active");
  912. }
  913. *p = '\0';
  914. ngui_text_ellipsis_utf8(&ui, line_r, line_r.x, line_r.y, line,
  915. ui.theme.text_dim);
  916. }
  917. static void draw_net_main(struct ngui_rect area, int *yy)
  918. {
  919. struct ngui_row row;
  920. struct ngui_rect r;
  921. char ssid[33], sub[80], *p, value[48];
  922. int i, shown = 0, more, n, y = *yy;
  923. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  924. ngui_section(&ui, r, "Connection", 0, 0);
  925. y += SECTION_H;
  926. r = (struct ngui_rect){ area.x, y, area.w, CARD_H };
  927. draw_status_card(r);
  928. y += CARD_H + GAP;
  929. for (i = 0; i < ndev; i++)
  930. if (devs[i].kind != SYS_NETDEV_WIFI)
  931. shown++;
  932. more = shown > 4 ? shown - 4 : 0;
  933. if (shown > 4)
  934. shown = 4;
  935. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  936. if (ngui_section(&ui, r, "Devices", ID_NET_REFRESH, "Refresh"))
  937. request_action(A_REFRESH, 0);
  938. y += SECTION_H;
  939. n = 0;
  940. for (i = 0; i < ndev && n < shown; i++) {
  941. if (devs[i].kind == SYS_NETDEV_WIFI)
  942. continue;
  943. r = (struct ngui_rect){ area.x, y, area.w, DEV_ROW_H };
  944. if (r.y + r.h > area.y && r.y < area.y + area.h) {
  945. row.title = devs[i].name;
  946. row.subtitle = devs[i].chip;
  947. row.trailing = devs[i].active ? "Connected" :
  948. (devs[i].link ? "link" : 0);
  949. row.bars = -1;
  950. row.selected = devs[i].active;
  951. if (ngui_list_row(&ui, ID_DEV_BASE + i, r, &row))
  952. request_action(A_DEV, i);
  953. }
  954. y += DEV_ROW_H;
  955. n++;
  956. }
  957. if (!shown) {
  958. const char *msg = wifi_present ?
  959. "No wired or USB device" :
  960. "No device found - plug one in and Refresh";
  961. ngui_text_utf8(&ui, area.x + 12, y + 12, msg,
  962. ui.theme.text_dim);
  963. y += DEV_ROW_H;
  964. } else if (more) {
  965. char line[32], *lp = line;
  966. lp = s_put(lp, "+");
  967. lp = s_put_dec(lp, (unsigned int)more);
  968. lp = s_put(lp, " more devices");
  969. *lp = '\0';
  970. ngui_text_utf8(&ui, area.x + 12, y + 2, line,
  971. ui.theme.text_dim);
  972. y += 20;
  973. }
  974. y += GAP;
  975. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  976. if (wifi_present && wst.state >= SYS_WIFI_ST_ASSOC) {
  977. if (ngui_section(&ui, r, "Wi-Fi", ID_NET_DISCONNECT,
  978. "Disconnect"))
  979. request_action(A_DISCONNECT, 0);
  980. } else if (wifi_present && !scanning) {
  981. if (ngui_section(&ui, r, "Wi-Fi", ID_NET_SCAN, "Scan"))
  982. request_action(A_SCAN, 0);
  983. } else {
  984. ngui_section(&ui, r, "Wi-Fi", 0, 0);
  985. }
  986. y += SECTION_H;
  987. if (!wifi_present) {
  988. ngui_text_utf8(&ui, area.x + 12, y + 12,
  989. "No Wi-Fi card detected", ui.theme.text_dim);
  990. y += BSS_ROW_H;
  991. } else if (scanning) {
  992. ngui_text_utf8(&ui, area.x + 12, y + 12,
  993. "Scanning for networks...", ui.theme.accent);
  994. y += BSS_ROW_H;
  995. } else if (nbss == 0) {
  996. const char *msg = wst.state >= SYS_WIFI_ST_ASSOC ?
  997. "Connected - disconnect to scan for other networks" :
  998. "Press Scan to list Wi-Fi networks";
  999. ngui_text_utf8(&ui, area.x + 12, y + 12, msg,
  1000. ui.theme.text_dim);
  1001. y += BSS_ROW_H;
  1002. } else {
  1003. for (i = 0; i < nbss; i++) {
  1004. struct sys_wifi_bss *b = &bss[i];
  1005. struct ngui_rect br = { area.x, y, area.w, BSS_ROW_H };
  1006. if (br.y + br.h > area.y && br.y < area.y + area.h) {
  1007. ssid_name(b, ssid);
  1008. p = sub;
  1009. p = s_put(p, security_name(b->security));
  1010. if (bss_saved(b) >= 0)
  1011. p = s_put(p, " \xC2\xB7 saved");
  1012. if (bss_is_active(b))
  1013. p = s_put(p, " \xC2\xB7 connected");
  1014. *p = '\0';
  1015. row.title = ssid;
  1016. row.subtitle = sub;
  1017. row.trailing = 0;
  1018. row.bars = b->security == SYS_WIFI_SEC_OTHER ?
  1019. -1 : rssi_bars(b->rssi);
  1020. row.selected = bss_is_active(b);
  1021. if (ngui_list_row_utf8(&ui, ID_BSS_BASE + i,
  1022. br, &row))
  1023. request_action(A_BSS, i);
  1024. }
  1025. y += BSS_ROW_H;
  1026. }
  1027. }
  1028. if (net_present) {
  1029. static const char *labels[4] = {
  1030. "IP address", "Gateway", "DNS", "MAC address"
  1031. };
  1032. int di;
  1033. y += GAP;
  1034. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  1035. ngui_section(&ui, r, "Details", 0, 0);
  1036. y += SECTION_H;
  1037. for (di = 0; di < 4; di++) {
  1038. char *vp = value;
  1039. if (di == 0) {
  1040. if (net.ip[0] || net.ip[1] || net.ip[2] ||
  1041. net.ip[3]) {
  1042. vp = s_put_dec(vp, net.ip[0]); *vp++ = '.';
  1043. vp = s_put_dec(vp, net.ip[1]); *vp++ = '.';
  1044. vp = s_put_dec(vp, net.ip[2]); *vp++ = '.';
  1045. vp = s_put_dec(vp, net.ip[3]);
  1046. } else {
  1047. vp = s_put(vp, "not configured");
  1048. }
  1049. } else if (di == 1) {
  1050. if (net.gw[0] || net.gw[1] || net.gw[2] ||
  1051. net.gw[3]) {
  1052. vp = s_put_dec(vp, net.gw[0]); *vp++ = '.';
  1053. vp = s_put_dec(vp, net.gw[1]); *vp++ = '.';
  1054. vp = s_put_dec(vp, net.gw[2]); *vp++ = '.';
  1055. vp = s_put_dec(vp, net.gw[3]);
  1056. } else {
  1057. vp = s_put(vp, "-");
  1058. }
  1059. } else if (di == 2) {
  1060. if (net.dns[0] || net.dns[1] || net.dns[2] ||
  1061. net.dns[3]) {
  1062. vp = s_put_dec(vp, net.dns[0]); *vp++ = '.';
  1063. vp = s_put_dec(vp, net.dns[1]); *vp++ = '.';
  1064. vp = s_put_dec(vp, net.dns[2]); *vp++ = '.';
  1065. vp = s_put_dec(vp, net.dns[3]);
  1066. } else {
  1067. vp = s_put(vp, "-");
  1068. }
  1069. } else {
  1070. static const char hex[] = "0123456789abcdef";
  1071. int k;
  1072. for (k = 0; k < 6; k++) {
  1073. *vp++ = hex[net.mac[k] >> 4];
  1074. *vp++ = hex[net.mac[k] & 15];
  1075. if (k != 5)
  1076. *vp++ = ':';
  1077. }
  1078. }
  1079. *vp = '\0';
  1080. r = (struct ngui_rect){ area.x, y, area.w, INFO_ROW_H };
  1081. if (r.y + r.h > area.y && r.y < area.y + area.h) {
  1082. row.title = labels[di];
  1083. row.subtitle = 0;
  1084. row.trailing = value;
  1085. row.bars = -1;
  1086. row.selected = 0;
  1087. ngui_list_row(&ui, ID_INFO_BASE + di, r, &row);
  1088. }
  1089. y += INFO_ROW_H;
  1090. }
  1091. }
  1092. if (nsaved) {
  1093. char nbuf[12], *np = nbuf;
  1094. y += GAP;
  1095. r = (struct ngui_rect){ area.x, y, area.w, SAVED_ROW_H };
  1096. if (r.y + r.h > area.y && r.y < area.y + area.h) {
  1097. np = s_put_dec(np, (unsigned int)nsaved);
  1098. *np = '\0';
  1099. row.title = "Saved networks";
  1100. row.subtitle = 0;
  1101. row.trailing = nbuf;
  1102. row.bars = -1;
  1103. row.selected = 0;
  1104. if (ngui_list_row(&ui, ID_NET_SAVED, r, &row))
  1105. request_action(A_SAVED, 0);
  1106. }
  1107. y += SAVED_ROW_H;
  1108. }
  1109. *yy = y;
  1110. }
  1111. static void draw_net_pass(struct ngui_rect area, int *yy)
  1112. {
  1113. struct ngui_rect r;
  1114. char line[96], *p;
  1115. int ev, y = *yy;
  1116. r = (struct ngui_rect){ area.x, y, area.w, 24 };
  1117. p = s_put(line, "Join \"");
  1118. p = s_put(p, sel_ssid);
  1119. p = s_put(p, "\"");
  1120. *p = '\0';
  1121. ngui_text_ellipsis_utf8(&ui, r, r.x, r.y, line, ui.theme.text);
  1122. y += 28;
  1123. r = (struct ngui_rect){ area.x, y, area.w, 20 };
  1124. ngui_text_px(&ui, r.x, r.y, "WPA2-PSK passphrase", 19,
  1125. ui.theme.text_dim, 14, 1, 0, 0);
  1126. y += 20;
  1127. r = (struct ngui_rect){ area.x, y, area.w, 34 };
  1128. ev = ngui_text_input(&ui, ID_NET_PASS_INPUT, r, &pass_in);
  1129. if (ev & NGUI_INPUT_SUBMIT)
  1130. request_action(A_PASS_OK, 0);
  1131. if (ev & NGUI_INPUT_CANCEL)
  1132. request_action(A_PASS_CANCEL, 0);
  1133. y += 34 + GAP;
  1134. ngui_text_utf8(&ui, area.x, y,
  1135. "8-63 characters, or 64 hex digits for a raw PSK.",
  1136. ui.theme.text_dim);
  1137. y += 24 + GAP;
  1138. r = (struct ngui_rect){ area.x, y, 110, 28 };
  1139. if (ngui_button(&ui, ID_NET_PASS_OK, r, "Connect"))
  1140. request_action(A_PASS_OK, 0);
  1141. r.x += 120;
  1142. if (ngui_button(&ui, ID_NET_PASS_CANCEL, r, "Cancel"))
  1143. request_action(A_PASS_CANCEL, 0);
  1144. y += 28;
  1145. *yy = y;
  1146. }
  1147. static void draw_net_connect(struct ngui_rect area, int *yy)
  1148. {
  1149. struct ngui_rect log, b, r;
  1150. char line[96], *p;
  1151. unsigned int start = 0, lines = 0, i;
  1152. int y = *yy, line_y = 0;
  1153. r = (struct ngui_rect){ area.x, y, area.w, 22 };
  1154. p = s_put(line, connect_pid > 0 ? "Connecting to \"" :
  1155. "Connection to \"");
  1156. p = s_put(p, sel_ssid);
  1157. p = s_put(p, "\"");
  1158. *p = '\0';
  1159. ngui_text_ellipsis_utf8(&ui, r, r.x, r.y, line, ui.theme.text);
  1160. y += 28;
  1161. log = (struct ngui_rect){ area.x, y, area.w, 240 };
  1162. ngui_fill_round_rect(&ui, log, NGUI_RADIUS, NGUI_RADIUS,
  1163. NGUI_RADIUS, NGUI_RADIUS, ui.theme.panel);
  1164. ngui_push_clip(&ui, (struct ngui_rect){ log.x + 8, log.y + 8,
  1165. log.w - 16, log.h - 16 });
  1166. for (i = conn_log_len; i > 0; i--) {
  1167. if (conn_log[i - 1] == '\n' && ++lines > 12) {
  1168. start = i;
  1169. break;
  1170. }
  1171. }
  1172. y = log.y + 10;
  1173. for (i = start; i < conn_log_len && y + 17 <= log.y + log.h - 8; i++) {
  1174. char c = conn_log[i];
  1175. if (c == '\r')
  1176. continue;
  1177. if (c == '\n') {
  1178. line[line_y] = '\0';
  1179. if (line_y)
  1180. ngui_text_px(&ui, log.x + 10, y, line,
  1181. (unsigned int)line_y,
  1182. ui.theme.text_dim, 14, 0, 0, 1);
  1183. y += 17;
  1184. line_y = 0;
  1185. continue;
  1186. }
  1187. if ((unsigned int)line_y + 1 < sizeof(line))
  1188. line[line_y++] = c;
  1189. }
  1190. if (line_y) {
  1191. line[line_y] = '\0';
  1192. ngui_text_px(&ui, log.x + 10, y, line, (unsigned int)line_y,
  1193. ui.theme.text_dim, 14, 0, 0, 1);
  1194. }
  1195. ngui_pop_clip(&ui);
  1196. y = log.y + log.h + GAP;
  1197. b = (struct ngui_rect){ area.x, y, 110, 28 };
  1198. if (ngui_button(&ui, ID_NET_CONN_CANCEL, b,
  1199. connect_pid > 0 ? "Cancel" : "Back"))
  1200. request_action(A_CONN_CANCEL, 0);
  1201. y += 28;
  1202. *yy = y;
  1203. }
  1204. static void draw_net_saved(struct ngui_rect area, int *yy)
  1205. {
  1206. struct ngui_rect r, bt;
  1207. struct ngui_row desc;
  1208. char ssid[33];
  1209. int i, y = *yy;
  1210. r = (struct ngui_rect){ area.x, y, area.w, 22 };
  1211. ngui_text_px(&ui, r.x, r.y, "Saved Wi-Fi networks", 20,
  1212. ui.theme.text, 14, 1, 0, 0);
  1213. y += 28;
  1214. if (!nsaved) {
  1215. ngui_text_utf8(&ui, area.x, y, "No saved networks",
  1216. ui.theme.text_dim);
  1217. y += 24;
  1218. } else {
  1219. for (i = 0; i < nsaved; i++) {
  1220. r = (struct ngui_rect){ area.x, y, area.w,
  1221. SAVED_ROW_H };
  1222. if (r.y + r.h > area.y && r.y < area.y + area.h) {
  1223. cfg_ssid_name(&saved[i], ssid);
  1224. bt = r;
  1225. bt.x = r.x + r.w - 74;
  1226. bt.w = 64;
  1227. bt.h = 28;
  1228. bt.y = r.y + 7;
  1229. r.w -= 84;
  1230. desc.title = ssid;
  1231. desc.subtitle = saved[i].pass_len ? "WPA2-PSK" :
  1232. "Open";
  1233. desc.trailing = 0;
  1234. desc.bars = -1;
  1235. desc.selected = 0;
  1236. ngui_list_row_utf8(&ui, ID_SAVED_ROW_BASE + i,
  1237. r, &desc);
  1238. if (ngui_button(&ui, ID_SAVED_FORGET_BASE + i,
  1239. bt, "Forget"))
  1240. request_action(A_FORGET, i);
  1241. }
  1242. y += SAVED_ROW_H;
  1243. }
  1244. }
  1245. r = (struct ngui_rect){ area.x, y, 100, 28 };
  1246. if (ngui_button(&ui, ID_NET_SAVED_BACK, r, "Back"))
  1247. request_action(A_SAVED_BACK, 0);
  1248. y += 28;
  1249. *yy = y;
  1250. }
  1251. static void draw_network(struct ngui_rect area, int *yy)
  1252. {
  1253. if (net_view == NET_PASS)
  1254. draw_net_pass(area, yy);
  1255. else if (net_view == NET_CONNECT)
  1256. draw_net_connect(area, yy);
  1257. else if (net_view == NET_SAVED)
  1258. draw_net_saved(area, yy);
  1259. else
  1260. draw_net_main(area, yy);
  1261. }
  1262. /* ------------------------------------------------------------------ */
  1263. /* Power */
  1264. static const char *power_mode_name(int mode)
  1265. {
  1266. switch (mode) {
  1267. case SYS_CPUFREQ_MAX:
  1268. return "Performance";
  1269. case SYS_CPUFREQ_MIN:
  1270. return "Power saver";
  1271. case SYS_CPUFREQ_OFF:
  1272. return "Off";
  1273. case SYS_CPUFREQ_FIXED:
  1274. return "Fixed";
  1275. case SYS_CPUFREQ_AUTO:
  1276. return "Balanced";
  1277. default:
  1278. return "Unavailable";
  1279. }
  1280. }
  1281. static void set_power_mode(int mode)
  1282. {
  1283. int got = u_cpufreq_set(mode);
  1284. char msg[48], *p;
  1285. if (got < 0) {
  1286. set_status("Power mode is not available", 1);
  1287. return;
  1288. }
  1289. cpufreq_mode = got;
  1290. p = s_put(msg, "Power mode: ");
  1291. p = s_put(p, power_mode_name(got));
  1292. *p = '\0';
  1293. set_status(msg, 0);
  1294. ngui_invalidate(&ui);
  1295. }
  1296. static void draw_power(struct ngui_rect area, int *yy)
  1297. {
  1298. static const char *labels[3] = {
  1299. "Balanced", "Performance", "Power saver"
  1300. };
  1301. static const char *subs[3] = {
  1302. "Automatic race-to-idle", "Maximum non-turbo P-state",
  1303. "Minimum P-state"
  1304. };
  1305. static const int modes[3] = {
  1306. SYS_CPUFREQ_AUTO, SYS_CPUFREQ_MAX, SYS_CPUFREQ_MIN
  1307. };
  1308. struct ngui_rect r;
  1309. struct ngui_row row;
  1310. int i, y = *yy;
  1311. int batt = -1, charging = 0, discharging = 0, ac = -1;
  1312. char value[48], *p;
  1313. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  1314. ngui_section(&ui, r, "Power mode", 0, 0);
  1315. y += SECTION_H;
  1316. for (i = 0; i < 3; i++) {
  1317. r = (struct ngui_rect){ area.x, y, area.w, 42 };
  1318. if (r.y + r.h > area.y && r.y < area.y + area.h) {
  1319. row.title = labels[i];
  1320. row.subtitle = subs[i];
  1321. row.trailing = 0;
  1322. row.bars = -1;
  1323. row.selected = cpufreq_mode == modes[i];
  1324. if (ngui_list_row(&ui, ID_POWER_MODE_BASE + i, r,
  1325. &row))
  1326. request_action(A_POWER, modes[i]);
  1327. }
  1328. y += 42;
  1329. }
  1330. y += GAP;
  1331. ngui_text_utf8(&ui, area.x + 10, y,
  1332. "Changes the P-state governor immediately.",
  1333. ui.theme.text_dim);
  1334. y += 22;
  1335. if (brightness >= 0) {
  1336. struct ngui_rect slider;
  1337. int v = brightness;
  1338. char b[8], *bp = b;
  1339. y += GAP;
  1340. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  1341. ngui_section(&ui, r, "Brightness", 0, 0);
  1342. y += SECTION_H;
  1343. bp = s_put_dec(bp, (unsigned int)brightness);
  1344. *bp++ = '%';
  1345. *bp = '\0';
  1346. ngui_text_utf8(&ui, area.x + 10, y, b, ui.theme.text);
  1347. y += 20;
  1348. slider = (struct ngui_rect){ area.x + 10, y,
  1349. area.w - 20, 16 };
  1350. if (ngui_slider(&ui, ID_BRIGHTNESS, &brightness_slider,
  1351. slider, &v, 5, 100)) {
  1352. int got = u_backlight_set(v);
  1353. brightness = got >= 0 ? got : v;
  1354. }
  1355. y += 24;
  1356. }
  1357. if (power_ok) {
  1358. ac = powerinfo.ac_online;
  1359. for (i = 0; i < SYS_POWER_BATT_MAX; i++) {
  1360. struct sys_battery *b = &powerinfo.battery[i];
  1361. if (!b->present)
  1362. continue;
  1363. if (b->percent_valid)
  1364. batt = (int)b->percent;
  1365. charging = (int)b->charging;
  1366. discharging = (int)b->discharging;
  1367. break;
  1368. }
  1369. }
  1370. y += GAP;
  1371. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  1372. ngui_section(&ui, r, "Power source", 0, 0);
  1373. y += SECTION_H;
  1374. if (!power_ok) {
  1375. ngui_text_utf8(&ui, area.x + 10, y + 8,
  1376. "No battery or AC information on this machine.",
  1377. ui.theme.text_dim);
  1378. y += 32;
  1379. } else {
  1380. p = value;
  1381. if (ac > 0)
  1382. p = s_put(p, "AC online");
  1383. else if (ac == 0)
  1384. p = s_put(p, "Battery");
  1385. else
  1386. p = s_put(p, "Unknown");
  1387. *p = '\0';
  1388. info_row(area, &y, "Source", value);
  1389. p = value;
  1390. if (batt >= 0) {
  1391. p = s_put_dec(p, (unsigned int)batt);
  1392. *p++ = '%';
  1393. if (charging)
  1394. p = s_put(p, " charging");
  1395. else if (discharging)
  1396. p = s_put(p, " discharging");
  1397. } else {
  1398. p = s_put(p, "not present");
  1399. }
  1400. *p = '\0';
  1401. info_row(area, &y, "Battery", value);
  1402. }
  1403. *yy = y;
  1404. }
  1405. /* ------------------------------------------------------------------ */
  1406. /* Date & time */
  1407. static unsigned int days_in_month(unsigned int y, unsigned int m)
  1408. {
  1409. static const unsigned char dm[12] = {
  1410. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  1411. };
  1412. if (m < 1 || m > 12)
  1413. return 0;
  1414. if (m == 2 && ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0))
  1415. return 29;
  1416. return dm[m - 1];
  1417. }
  1418. static int parse_num(const char *s, unsigned int n, unsigned int *out)
  1419. {
  1420. unsigned int v = 0, i;
  1421. if (!n)
  1422. return -1;
  1423. for (i = 0; i < n; i++) {
  1424. if (s[i] < '0' || s[i] > '9')
  1425. return -1;
  1426. v = v * 10 + (unsigned int)(s[i] - '0');
  1427. }
  1428. *out = v;
  1429. return 0;
  1430. }
  1431. static int parse_date(const char *s, unsigned int *y, unsigned int *mo,
  1432. unsigned int *d)
  1433. {
  1434. if (s_len(s) != 10)
  1435. return -1;
  1436. if ((s[4] != '-' && s[4] != '/') ||
  1437. (s[7] != '-' && s[7] != '/'))
  1438. return -1;
  1439. if (parse_num(s, 4, y) != 0 ||
  1440. parse_num(s + 5, 2, mo) != 0 ||
  1441. parse_num(s + 8, 2, d) != 0)
  1442. return -1;
  1443. return 0;
  1444. }
  1445. static int parse_time(const char *s, unsigned int *h, unsigned int *mi,
  1446. unsigned int *sec)
  1447. {
  1448. unsigned int len = s_len(s);
  1449. if (len == 5) {
  1450. *sec = 0;
  1451. } else if (len == 8) {
  1452. if (s[5] != ':' || parse_num(s + 6, 2, sec) != 0)
  1453. return -1;
  1454. } else {
  1455. return -1;
  1456. }
  1457. if (s[2] != ':' ||
  1458. parse_num(s, 2, h) != 0 ||
  1459. parse_num(s + 3, 2, mi) != 0)
  1460. return -1;
  1461. return 0;
  1462. }
  1463. static void format_date(char *b, const struct sys_datetime *dt)
  1464. {
  1465. char *p = s_put_dec(b, dt->year);
  1466. *p++ = '-';
  1467. p = s_put_2(p, dt->month);
  1468. *p++ = '-';
  1469. p = s_put_2(p, dt->day);
  1470. *p = '\0';
  1471. }
  1472. static void format_time_sec(char *b, const struct sys_datetime *dt)
  1473. {
  1474. char *p = s_put_2(b, dt->hour);
  1475. *p++ = ':';
  1476. p = s_put_2(p, dt->minute);
  1477. *p++ = ':';
  1478. p = s_put_2(p, dt->second);
  1479. *p = '\0';
  1480. }
  1481. static void load_datetime_fields(int force)
  1482. {
  1483. char b[24];
  1484. if (date_loaded && !force)
  1485. return;
  1486. if (force || !now_ok) {
  1487. if (u_rtc_get(&now) != 0) {
  1488. now_ok = 0;
  1489. return;
  1490. }
  1491. now_ok = 1;
  1492. }
  1493. format_date(b, &now);
  1494. ngui_input_set(&date_in, b);
  1495. format_time_sec(b, &now);
  1496. ngui_input_set(&time_in, b);
  1497. if (!ntp_in.len)
  1498. ngui_input_set(&ntp_in, "pool.ntp.org");
  1499. date_loaded = 1;
  1500. }
  1501. static void apply_datetime(void)
  1502. {
  1503. struct sys_datetime dt;
  1504. unsigned int y, mo, d, h, mi, s;
  1505. if (parse_date(date_in.text, &y, &mo, &d) != 0) {
  1506. set_status("Date must be YYYY-MM-DD", 1);
  1507. return;
  1508. }
  1509. if (parse_time(time_in.text, &h, &mi, &s) != 0) {
  1510. set_status("Time must be HH:MM:SS", 1);
  1511. return;
  1512. }
  1513. if (y < 1970 || y > 2069) {
  1514. set_status("Year must be 1970-2069", 1);
  1515. return;
  1516. }
  1517. if (mo < 1 || mo > 12) {
  1518. set_status("Month must be 1-12", 1);
  1519. return;
  1520. }
  1521. if (d < 1 || d > days_in_month(y, mo)) {
  1522. set_status("Day is not valid for that month", 1);
  1523. return;
  1524. }
  1525. if (h > 23 || mi > 59 || s > 59) {
  1526. set_status("Time is out of range", 1);
  1527. return;
  1528. }
  1529. dt.year = y;
  1530. dt.month = mo;
  1531. dt.day = d;
  1532. dt.hour = h;
  1533. dt.minute = mi;
  1534. dt.second = s;
  1535. dt.wday = 0;
  1536. if (u_rtc_set(&dt) != 0) {
  1537. set_status("RTC write failed", 1);
  1538. return;
  1539. }
  1540. load_status();
  1541. load_datetime_fields(1);
  1542. set_status("Date and time updated", 0);
  1543. }
  1544. static const char *ntp_error(int r)
  1545. {
  1546. switch (r) {
  1547. case SYS_NTP_ENONIC:
  1548. return "NTP: no network adapter";
  1549. case SYS_NTP_ENAME:
  1550. return "NTP: cannot resolve the server";
  1551. case SYS_NTP_EARP:
  1552. return "NTP: server is unreachable (ARP)";
  1553. case SYS_NTP_ETX:
  1554. return "NTP: transmit failed";
  1555. case SYS_NTP_ETIMEOUT:
  1556. return "NTP: server did not respond";
  1557. case SYS_NTP_EREPLY:
  1558. return "NTP: invalid server reply";
  1559. case SYS_NTP_EBUSY:
  1560. return "NTP: network is busy";
  1561. default:
  1562. return "NTP: sync failed";
  1563. }
  1564. }
  1565. static void sync_ntp(void)
  1566. {
  1567. struct sys_datetime dt;
  1568. const char *server = ntp_in.text[0] ? ntp_in.text : "pool.ntp.org";
  1569. int r;
  1570. set_status("Syncing NTP...", 0);
  1571. paint_current();
  1572. r = u_ntp_sync(server, 5000, &dt);
  1573. if (r != 0) {
  1574. set_status(ntp_error(r), 1);
  1575. return;
  1576. }
  1577. if (u_rtc_set(&dt) != 0) {
  1578. set_status("RTC write failed", 1);
  1579. return;
  1580. }
  1581. load_status();
  1582. load_datetime_fields(1);
  1583. set_status("Date and time synced", 0);
  1584. }
  1585. static void date_field(struct ngui_rect area, int *yy, const char *label,
  1586. unsigned int id, struct ngui_input *in,
  1587. unsigned int next_id)
  1588. {
  1589. struct ngui_rect r;
  1590. int ev;
  1591. r = (struct ngui_rect){ area.x + 10, *yy, area.w - 20, 18 };
  1592. ngui_text_utf8(&ui, r.x, r.y, label, ui.theme.text_dim);
  1593. *yy += 18;
  1594. r = (struct ngui_rect){ area.x + 10, *yy, area.w - 20, 32 };
  1595. ev = ngui_text_input(&ui, id, r, in);
  1596. if (ev & NGUI_INPUT_SUBMIT)
  1597. request_action(id == ID_NTP_FIELD ? A_DATE_NTP : A_DATE_SET, 0);
  1598. if (ev & NGUI_INPUT_NEXT)
  1599. ngui_set_focus(&ui, next_id);
  1600. if (ev & NGUI_INPUT_CANCEL)
  1601. ngui_set_focus(&ui, 0);
  1602. *yy += 32 + 8;
  1603. }
  1604. static void draw_datetime(struct ngui_rect area, int *yy)
  1605. {
  1606. struct ngui_rect r;
  1607. char current[64], *p;
  1608. int y = *yy;
  1609. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  1610. ngui_section(&ui, r, "Date & Time", 0, 0);
  1611. y += SECTION_H;
  1612. p = current;
  1613. p = s_put(p, "Current: ");
  1614. if (now_ok) {
  1615. format_date(p, &now);
  1616. p += s_len(p);
  1617. *p++ = ' ';
  1618. format_time_sec(p, &now);
  1619. } else {
  1620. s_put(p, "unavailable");
  1621. }
  1622. ngui_text_utf8(&ui, area.x + 10, y, current, ui.theme.text_dim);
  1623. y += 24;
  1624. date_field(area, &y, "Date (YYYY-MM-DD)", ID_DATE_FIELD, &date_in,
  1625. ID_TIME_FIELD);
  1626. date_field(area, &y, "Time (HH:MM:SS)", ID_TIME_FIELD, &time_in,
  1627. ID_NTP_FIELD);
  1628. date_field(area, &y, "NTP server (UTC)", ID_NTP_FIELD, &ntp_in,
  1629. ID_DATE_FIELD);
  1630. r = (struct ngui_rect){ area.x + 10, y, 80, 28 };
  1631. if (ngui_button(&ui, ID_DATE_SET, r, "Set"))
  1632. request_action(A_DATE_SET, 0);
  1633. r.x += 88;
  1634. if (ngui_button(&ui, ID_DATE_NOW, r, "Now"))
  1635. request_action(A_DATE_NOW, 0);
  1636. r.x += 88;
  1637. r.w = 110;
  1638. if (ngui_button(&ui, ID_DATE_NTP, r, "Sync NTP"))
  1639. request_action(A_DATE_NTP, 0);
  1640. y += 28 + GAP;
  1641. ngui_text_utf8(&ui, area.x + 10, y,
  1642. "Sync NTP fetches UTC and writes the RTC.",
  1643. ui.theme.text_dim);
  1644. y += 22;
  1645. ngui_text_utf8(&ui, area.x + 10, y,
  1646. "Year range: 1970-2069. Enter applies.",
  1647. ui.theme.text_dim);
  1648. y += 22;
  1649. *yy = y;
  1650. }
  1651. /* ------------------------------------------------------------------ */
  1652. /* Appearance and the wallpaper picker */
  1653. static int path_is_file(const char *path)
  1654. {
  1655. struct sys_stat st;
  1656. return u_stat(path, &st) == 0 && !st.is_dir && st.size > 0;
  1657. }
  1658. static void wallpaper_refresh(void)
  1659. {
  1660. if (path_is_file(WM_WALLPAPER_DATA)) {
  1661. wallpaper_present = 1;
  1662. s_copy(wallpaper_source, "/data/WALLPAPR.BIN",
  1663. sizeof(wallpaper_source));
  1664. } else if (path_is_file(WM_WALLPAPER_SHARE)) {
  1665. wallpaper_present = 1;
  1666. s_copy(wallpaper_source, "/share/wallpaper",
  1667. sizeof(wallpaper_source));
  1668. } else {
  1669. wallpaper_present = 0;
  1670. wallpaper_source[0] = '\0';
  1671. }
  1672. }
  1673. static int lower_char(int c)
  1674. {
  1675. return c >= 'A' && c <= 'Z' ? c + 'a' - 'A' : c;
  1676. }
  1677. static int ext_is_image(const char *name)
  1678. {
  1679. static const char *const exts[] = {
  1680. ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tga", ".ppm",
  1681. ".pgm", ".pbm", ".ico", ".cur", ".pam"
  1682. };
  1683. unsigned int i;
  1684. int n = (int)s_len(name), e;
  1685. for (e = n - 1; e >= 0 && name[e] != '.'; e--)
  1686. ;
  1687. if (e < 0)
  1688. return 0;
  1689. for (i = 0; i < sizeof(exts) / sizeof(exts[0]); i++) {
  1690. const char *x = exts[i];
  1691. int k = 0;
  1692. while (x[k] && e + k < n &&
  1693. lower_char((unsigned char)name[e + k]) == x[k])
  1694. k++;
  1695. if (!x[k] && e + k == n)
  1696. return 1;
  1697. }
  1698. return 0;
  1699. }
  1700. static void trim_name(char *s)
  1701. {
  1702. int n = (int)s_len(s);
  1703. while (n > 0 && (s[n - 1] == ' ' || s[n - 1] == '\t'))
  1704. s[--n] = '\0';
  1705. }
  1706. static void path_join(char *out, unsigned int cap, const char *base,
  1707. const char *name)
  1708. {
  1709. unsigned int n = s_len(base);
  1710. int slash = n > 0 && base[n - 1] == '/';
  1711. if (n >= cap - 1) {
  1712. out[0] = '\0';
  1713. return;
  1714. }
  1715. s_copy(out, base, cap);
  1716. if (!slash && s_len(out) + 1 < cap) {
  1717. out[s_len(out)] = '/';
  1718. out[s_len(out) + 1] = '\0';
  1719. }
  1720. if (s_len(out) + s_len(name) + 1 < cap)
  1721. s_copy(out + s_len(out), name, cap - s_len(out));
  1722. }
  1723. static void load_picker_dir(const char *path)
  1724. {
  1725. struct sys_dirent d;
  1726. int i = 0, n;
  1727. s_copy(pick_path, path && path[0] ? path : "/", sizeof(pick_path));
  1728. if (s_len(pick_path) > 1 && pick_path[s_len(pick_path) - 1] == '/')
  1729. pick_path[s_len(pick_path) - 1] = '\0';
  1730. pick_count = 0;
  1731. picker_scroll.offset = 0;
  1732. if (s_eq(pick_path, "/")) {
  1733. /* The VFS root is the mount table, not a directory with
  1734. * entries; mirror the files app and list the mounts. */
  1735. struct sys_mount m;
  1736. while (pick_count < PICK_MAX &&
  1737. u_syscall2(SYS_LIST_MOUNTS, i++, (int)&m) == 1) {
  1738. trim_name(m.name);
  1739. if (!m.name[0])
  1740. continue;
  1741. s_copy(pick_names[pick_count], m.name, 64);
  1742. pick_sizes[pick_count] = 0;
  1743. pick_dirs[pick_count] = 1;
  1744. pick_count++;
  1745. }
  1746. } else {
  1747. while (pick_count < PICK_MAX &&
  1748. (n = u_syscall3(SYS_LIST_DIR, (int)pick_path, i++,
  1749. (int)&d)) == 1) {
  1750. char disp[64];
  1751. if (d.raw_83) {
  1752. u_fat_name(d.name, disp, sizeof(disp));
  1753. } else {
  1754. trim_name(d.name);
  1755. s_copy(disp, d.name, sizeof(disp));
  1756. }
  1757. if (!disp[0] || s_eq(disp, ".") || s_eq(disp, ".."))
  1758. continue;
  1759. if (!d.is_dir && !ext_is_image(disp))
  1760. continue;
  1761. s_copy(pick_names[pick_count], disp, 64);
  1762. pick_sizes[pick_count] = d.size;
  1763. pick_dirs[pick_count] = d.is_dir ? 1 : 0;
  1764. pick_count++;
  1765. }
  1766. }
  1767. /* Directories first, then case-insensitive name order. */
  1768. for (i = 1; i < pick_count; i++) {
  1769. int j = i;
  1770. while (j > 0) {
  1771. int swap;
  1772. int a = j - 1, b = j;
  1773. unsigned int k;
  1774. if (pick_dirs[a] != pick_dirs[b]) {
  1775. swap = pick_dirs[b] > pick_dirs[a];
  1776. } else {
  1777. swap = 0;
  1778. for (k = 0; pick_names[a][k] &&
  1779. pick_names[b][k]; k++) {
  1780. int ca = lower_char((unsigned char)
  1781. pick_names[a][k]);
  1782. int cb = lower_char((unsigned char)
  1783. pick_names[b][k]);
  1784. if (ca != cb) {
  1785. swap = cb < ca;
  1786. break;
  1787. }
  1788. }
  1789. if (!swap && !pick_names[a][k] &&
  1790. pick_names[b][k])
  1791. swap = 1;
  1792. }
  1793. if (!swap)
  1794. break;
  1795. {
  1796. char tn[64];
  1797. unsigned int ts = pick_sizes[a];
  1798. unsigned char td = pick_dirs[a];
  1799. s_copy(tn, pick_names[a], sizeof(tn));
  1800. s_copy(pick_names[a], pick_names[b], 64);
  1801. pick_sizes[a] = pick_sizes[b];
  1802. pick_dirs[a] = pick_dirs[b];
  1803. s_copy(pick_names[b], tn, 64);
  1804. pick_sizes[b] = ts;
  1805. pick_dirs[b] = td;
  1806. }
  1807. j--;
  1808. }
  1809. }
  1810. }
  1811. static void pick_parent(char *out, unsigned int cap)
  1812. {
  1813. char tmp[PICK_PATH_MAX];
  1814. unsigned int n;
  1815. s_copy(tmp, pick_path, sizeof(tmp));
  1816. n = s_len(tmp);
  1817. if (n && tmp[n - 1] == '/')
  1818. tmp[--n] = '\0';
  1819. while (n > 0 && tmp[n - 1] != '/')
  1820. n--;
  1821. if (n == 0)
  1822. s_copy(out, "/", cap);
  1823. else {
  1824. tmp[n - 1] = '\0';
  1825. s_copy(out, n == 1 ? "/" : tmp, cap);
  1826. }
  1827. }
  1828. static void picker_activate(int idx)
  1829. {
  1830. char full[PICK_PATH_MAX + 72];
  1831. if (idx < 0 || idx >= pick_count)
  1832. return;
  1833. path_join(full, sizeof(full), pick_path, pick_names[idx]);
  1834. if (pick_dirs[idx]) {
  1835. load_picker_dir(full);
  1836. ngui_invalidate(&ui);
  1837. return;
  1838. }
  1839. set_status("Loading wallpaper...", 0);
  1840. paint_current();
  1841. set_wallpaper_file(full);
  1842. picker_mode = 0;
  1843. wallpaper_refresh();
  1844. ngui_invalidate(&ui);
  1845. }
  1846. static void set_wallpaper_file(const char *path)
  1847. {
  1848. struct sys_stat st;
  1849. struct img_info info;
  1850. struct img_options opt;
  1851. struct img_mem mem;
  1852. struct img_image img;
  1853. struct img_blit b;
  1854. struct wm_wallpaper_hdr *h;
  1855. unsigned char *file = 0, *arena = 0, *out = 0;
  1856. unsigned int cap, est, sw, sh, tw, th, pixbytes, total, i;
  1857. unsigned long long sx, sy, scale, dw, dh;
  1858. int n, r;
  1859. if (u_stat(path, &st) != 0 || st.is_dir || st.size == 0) {
  1860. set_status("Cannot read that image", 1);
  1861. return;
  1862. }
  1863. if (st.size >= WALL_FILE_CAP) {
  1864. set_status("Image is larger than 4 MB", 1);
  1865. return;
  1866. }
  1867. cap = st.size + 512; /* a FAT read may round up to a sector */
  1868. file = (unsigned char *)malloc(cap);
  1869. if (!file) {
  1870. set_status("Not enough memory for the image", 1);
  1871. return;
  1872. }
  1873. n = img_read_file(path, file, cap);
  1874. if (n < 0) {
  1875. set_status(img_error_string(n), 1);
  1876. goto out;
  1877. }
  1878. r = img_sniff(file, (unsigned int)n, &info);
  1879. if (r != 0) {
  1880. set_status(img_error_string(r), 1);
  1881. goto out;
  1882. }
  1883. est = img_estimate(&info);
  1884. if (est == 0xFFFFFFFFu || est > WALL_HEAP_CAP) {
  1885. set_status("Image needs too much memory", 1);
  1886. goto out;
  1887. }
  1888. arena = (unsigned char *)malloc(est);
  1889. if (!arena) {
  1890. set_status("Not enough memory for the image", 1);
  1891. goto out;
  1892. }
  1893. img_mem_init(&mem, arena, est);
  1894. memset(&opt, 0, sizeof(opt));
  1895. r = img_decode(file, (unsigned int)n, &opt, &mem, &img);
  1896. if (r != 0) {
  1897. set_status(img_error_string(r), 1);
  1898. goto out;
  1899. }
  1900. if (u_screen_size(&sw, &sh) != 0 || !sw || !sh) {
  1901. set_status("Cannot read the screen size", 1);
  1902. goto out;
  1903. }
  1904. tw = sw;
  1905. th = sh;
  1906. if (tw > WM_WALLPAPER_MAX_W) {
  1907. th = (unsigned int)((unsigned long long)th *
  1908. WM_WALLPAPER_MAX_W / tw);
  1909. tw = WM_WALLPAPER_MAX_W;
  1910. }
  1911. if (th > WM_WALLPAPER_MAX_H) {
  1912. tw = (unsigned int)((unsigned long long)tw *
  1913. WM_WALLPAPER_MAX_H / th);
  1914. th = WM_WALLPAPER_MAX_H;
  1915. }
  1916. if (!tw)
  1917. tw = 1;
  1918. if (!th)
  1919. th = 1;
  1920. pixbytes = tw * th * 2u;
  1921. total = sizeof(*h) + pixbytes;
  1922. out = (unsigned char *)malloc(total);
  1923. if (!out) {
  1924. set_status("Not enough memory for the wallpaper", 1);
  1925. goto out;
  1926. }
  1927. memset(out, 0, total);
  1928. h = (struct wm_wallpaper_hdr *)out;
  1929. for (i = 0; i < sizeof(h->magic); i++)
  1930. h->magic[i] = WM_WALLPAPER_MAGIC[i];
  1931. h->width = tw;
  1932. h->height = th;
  1933. sx = (((unsigned long long)tw << 16) + img.w - 1) / img.w;
  1934. sy = (((unsigned long long)th << 16) + img.h - 1) / img.h;
  1935. scale = sx > sy ? sx : sy;
  1936. if (!scale)
  1937. scale = 1;
  1938. dw = ((unsigned long long)img.w * scale) >> 16;
  1939. dh = ((unsigned long long)img.h * scale) >> 16;
  1940. memset(&b, 0, sizeof(b));
  1941. b.dst = (unsigned short *)(out + sizeof(*h));
  1942. b.dst_w = (int)tw;
  1943. b.dst_h = (int)th;
  1944. b.clip_w = (int)tw;
  1945. b.clip_h = (int)th;
  1946. b.x = ((int)tw - (int)dw) / 2;
  1947. b.y = ((int)th - (int)dh) / 2;
  1948. b.scale = (unsigned int)scale;
  1949. img_draw(&img, &b);
  1950. r = u_syscall3(SYS_WRITE_FILE, (int)WM_WALLPAPER_DATA, (int)out,
  1951. (int)total);
  1952. if (r < 0) {
  1953. struct sys_stat dst;
  1954. /* Only fall back to the RAM copy when there is no data
  1955. * volume at all. Writing the share copy while an older
  1956. * /data image exists would make the WM load the stale one. */
  1957. if (u_stat("/data", &dst) == 0 && dst.is_dir) {
  1958. set_status("Cannot save the wallpaper to /data", 1);
  1959. goto out;
  1960. }
  1961. r = u_syscall3(SYS_WRITE_FILE, (int)WM_WALLPAPER_SHARE,
  1962. (int)out, (int)total);
  1963. }
  1964. if (r < 0) {
  1965. set_status("Cannot save the wallpaper", 1);
  1966. goto out;
  1967. }
  1968. u_syscall1(SYS_WM_WALLPAPER, 1);
  1969. wallpaper_present = 1;
  1970. set_status("Wallpaper set", 0);
  1971. out:
  1972. free(out);
  1973. free(arena);
  1974. free(file);
  1975. }
  1976. static void draw_appearance(struct ngui_rect area, int *yy)
  1977. {
  1978. struct ngui_rect r;
  1979. char line[96], *p;
  1980. int i, y = *yy;
  1981. if (picker_mode) {
  1982. draw_picker(area, yy);
  1983. return;
  1984. }
  1985. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  1986. ngui_section(&ui, r, "Desktop colour", 0, 0);
  1987. y += SECTION_H;
  1988. ngui_text_utf8(&ui, area.x + 10, y,
  1989. "Used when no wallpaper image is installed.",
  1990. ui.theme.text_dim);
  1991. y += 24;
  1992. for (i = 0; i < NBG; i++) {
  1993. char sub[16], *sp = sub;
  1994. sp = s_put(sp, "#");
  1995. {
  1996. static const char hex[] = "0123456789abcdef";
  1997. unsigned int rgb = bg_choices[i].rgb;
  1998. int k;
  1999. for (k = 5; k >= 0; k--)
  2000. *sp++ = hex[(rgb >> (k * 4)) & 15];
  2001. }
  2002. *sp = '\0';
  2003. r = (struct ngui_rect){ area.x, y, area.w, 36 };
  2004. if (settings_row(ID_BG_BASE + i, r, bg_choices[i].name, sub,
  2005. prefs.bg_rgb == bg_choices[i].rgb ?
  2006. "Selected" : "",
  2007. prefs.bg_rgb == bg_choices[i].rgb ? 1 : 0,
  2008. (int)bg_choices[i].rgb))
  2009. request_action(A_BG, i);
  2010. y += 38;
  2011. }
  2012. y += GAP;
  2013. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  2014. ngui_section(&ui, r, "Wallpaper", 0, 0);
  2015. y += SECTION_H;
  2016. if (wallpaper_present) {
  2017. p = s_put(line, "Image: ");
  2018. p = s_put(p, wallpaper_source);
  2019. *p = '\0';
  2020. ngui_text_utf8(&ui, area.x + 10, y, line, ui.theme.text_dim);
  2021. y += 24;
  2022. ngui_text_utf8(&ui, area.x + 10, y,
  2023. "The image covers the desktop colour.",
  2024. ui.theme.text_dim);
  2025. y += 24;
  2026. } else {
  2027. ngui_text_utf8(&ui, area.x + 10, y,
  2028. "No wallpaper image is installed.",
  2029. ui.theme.text_dim);
  2030. y += 24;
  2031. }
  2032. r = (struct ngui_rect){ area.x + 10, y, 150, 28 };
  2033. if (ngui_button(&ui, ID_WALL_CHOOSE, r,
  2034. wallpaper_present ? "Change image..." :
  2035. "Choose image..."))
  2036. request_action(A_WALL_CHOOSE, 0);
  2037. if (wallpaper_present) {
  2038. r.x += 160;
  2039. r.w = 110;
  2040. if (ngui_button(&ui, ID_WALL_REMOVE, r, "Remove"))
  2041. request_action(A_WALL_REMOVE, 0);
  2042. }
  2043. y += 28 + GAP;
  2044. ngui_text_utf8(&ui, area.x + 10, y,
  2045. "Images are scaled to cover the screen and stored on the",
  2046. ui.theme.text_dim);
  2047. y += 20;
  2048. ngui_text_utf8(&ui, area.x + 10, y,
  2049. "data volume (or /share without one).",
  2050. ui.theme.text_dim);
  2051. y += 22;
  2052. *yy = y;
  2053. }
  2054. static void draw_picker(struct ngui_rect area, int *yy)
  2055. {
  2056. struct ngui_rect r;
  2057. struct ngui_row row;
  2058. char size[24];
  2059. int i, y = *yy;
  2060. r = (struct ngui_rect){ area.x, y, area.w, 22 };
  2061. ngui_text_px(&ui, r.x, r.y, "Choose a wallpaper image", 24,
  2062. ui.theme.text, 14, 1, 0, 0);
  2063. y += 26;
  2064. ngui_text_ellipsis_utf8(&ui, (struct ngui_rect){ area.x, y,
  2065. area.w, 18 },
  2066. area.x, y, pick_path, ui.theme.text_dim);
  2067. y += 24;
  2068. r = (struct ngui_rect){ area.x, y, 70, 26 };
  2069. if (ngui_button(&ui, ID_PICK_UP, r, "Up"))
  2070. request_action(A_PICK_UP, 0);
  2071. r.x += 80;
  2072. r.w = 90;
  2073. if (ngui_button(&ui, ID_PICK_CANCEL, r, "Cancel"))
  2074. request_action(A_PICK_CANCEL, 0);
  2075. y += 26 + GAP;
  2076. picker_scroll.content_h = pick_count * PICK_ROW_H;
  2077. picker_scroll.view_h = area.y + area.h - y;
  2078. {
  2079. struct ngui_rect list_r = { area.x, y, area.w,
  2080. area.y + area.h - y };
  2081. ngui_scroll_begin(&ui, list_r, &picker_scroll);
  2082. ngui_scroll_wheel(&ui, list_r, &picker_scroll);
  2083. ngui_scroll_keys(&ui, &picker_scroll);
  2084. y -= picker_scroll.offset;
  2085. if (!pick_count) {
  2086. ngui_text_utf8(&ui, area.x + 10, y,
  2087. "No folders or image files here",
  2088. ui.theme.text_dim);
  2089. y += PICK_ROW_H;
  2090. }
  2091. for (i = 0; i < pick_count; i++) {
  2092. r = (struct ngui_rect){ area.x,
  2093. y + i * PICK_ROW_H, area.w,
  2094. PICK_ROW_H };
  2095. if (r.y + r.h <= area.y || r.y >= area.y + area.h)
  2096. continue;
  2097. if (pick_dirs[i]) {
  2098. s_copy(size, "Folder", sizeof(size));
  2099. } else {
  2100. format_size(size, pick_sizes[i]);
  2101. }
  2102. row.title = pick_names[i];
  2103. row.subtitle = size;
  2104. row.trailing = 0;
  2105. row.bars = -1;
  2106. row.selected = 0;
  2107. if (ngui_list_row_utf8(&ui, ID_PICK_ROW_BASE + i,
  2108. r, &row))
  2109. request_action(A_PICK_ROW, i);
  2110. }
  2111. ngui_scroll_end(&ui, list_r, &picker_scroll);
  2112. }
  2113. *yy = area.y + area.h;
  2114. }
  2115. /* ------------------------------------------------------------------ */
  2116. /* Windows and About */
  2117. static void draw_windows(struct ngui_rect area, int *yy)
  2118. {
  2119. struct ngui_rect r;
  2120. int y = *yy;
  2121. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  2122. ngui_section(&ui, r, "Window manager", 0, 0);
  2123. y += SECTION_H;
  2124. ngui_text_utf8(&ui, area.x + 10, y,
  2125. "Changes apply immediately and are saved for the next "
  2126. "boot.", ui.theme.text_dim);
  2127. y += 26;
  2128. r = (struct ngui_rect){ area.x, y, area.w, 44 };
  2129. if (settings_row(ID_WIN_SHADOW, r, "Window shadows",
  2130. "Soft drop shadow around normal windows",
  2131. prefs.shadow ? "On" : "Off", prefs.shadow ? 1 : 0, -1))
  2132. request_action(A_WIN_SHADOW, 0);
  2133. y += 46;
  2134. r = (struct ngui_rect){ area.x, y, area.w, 44 };
  2135. if (settings_row(ID_WIN_SNAP, r, "Snap windows",
  2136. "Drag a title bar to a screen edge to snap it",
  2137. prefs.snap ? "On" : "Off", prefs.snap ? 1 : 0, -1))
  2138. request_action(A_WIN_SNAP, 0);
  2139. y += 46;
  2140. r = (struct ngui_rect){ area.x, y, area.w, 44 };
  2141. if (settings_row(ID_WIN_RESIST, r, "Edge resistance",
  2142. "Hold a window briefly at screen and window edges",
  2143. prefs.resist ? "On" : "Off", prefs.resist ? 1 : 0,
  2144. -1))
  2145. request_action(A_WIN_RESIST, 0);
  2146. y += 46 + GAP;
  2147. ngui_text_utf8(&ui, area.x + 10, y,
  2148. "Double-clicking a title bar still maximizes or "
  2149. "restores a window.", ui.theme.text_dim);
  2150. y += 22;
  2151. *yy = y;
  2152. }
  2153. static void draw_about(struct ngui_rect area, int *yy)
  2154. {
  2155. struct ngui_rect r;
  2156. struct sys_meminfo mi;
  2157. struct sys_cputemp ct;
  2158. char value[64], *p;
  2159. unsigned int sw = 0, sh = 0, secs, h, m, s;
  2160. int y = *yy;
  2161. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  2162. ngui_section(&ui, r, "System", 0, 0);
  2163. y += SECTION_H;
  2164. p = value;
  2165. p = s_put(p, OS_NAME);
  2166. p = s_put(p, " ");
  2167. p = s_put(p, OS_VERSION);
  2168. *p = '\0';
  2169. info_row(area, &y, "Operating system", value);
  2170. u_screen_size(&sw, &sh);
  2171. p = value;
  2172. p = s_put_dec(p, sw);
  2173. p = s_put(p, " x ");
  2174. p = s_put_dec(p, sh);
  2175. *p = '\0';
  2176. info_row(area, &y, "Display", value);
  2177. secs = u_ticks_ms() / 1000;
  2178. h = secs / 3600;
  2179. m = (secs / 60) % 60;
  2180. s = secs % 60;
  2181. p = value;
  2182. p = s_put_dec(p, h);
  2183. p = s_put(p, "h ");
  2184. p = s_put_dec(p, m);
  2185. p = s_put(p, "m ");
  2186. p = s_put_dec(p, s);
  2187. p = s_put(p, "s");
  2188. *p = '\0';
  2189. info_row(area, &y, "Uptime", value);
  2190. if (u_syscall1(SYS_MEMINFO, (int)&mi) == 0) {
  2191. p = value;
  2192. p = s_put_dec(p, mi.total_kb / 1024u);
  2193. p = s_put(p, " MB");
  2194. *p = '\0';
  2195. info_row(area, &y, "Memory", value);
  2196. p = value;
  2197. p = s_put_dec(p, (mi.free_pages * mi.page_size) /
  2198. (1024u * 1024u));
  2199. p = s_put(p, " MB");
  2200. *p = '\0';
  2201. info_row(area, &y, "Free", value);
  2202. }
  2203. if (u_cpu_temp(&ct) == 0 && ct.temp_c >= 0) {
  2204. p = value;
  2205. p = s_put_dec(p, (unsigned int)ct.temp_c);
  2206. p = s_put(p, " C");
  2207. *p = '\0';
  2208. info_row(area, &y, "CPU temperature", value);
  2209. }
  2210. y += GAP;
  2211. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  2212. ngui_section(&ui, r, "Settings", 0, 0);
  2213. y += SECTION_H;
  2214. ngui_text_utf8(&ui, area.x + 10, y,
  2215. "Window preferences: /data/SETTINGS.CFG",
  2216. ui.theme.text_dim);
  2217. y += 22;
  2218. ngui_text_utf8(&ui, area.x + 10, y,
  2219. "Without a data volume the RAM copy is "
  2220. "/share/settings.cfg.", ui.theme.text_dim);
  2221. y += 22;
  2222. *yy = y;
  2223. }
  2224. /* ------------------------------------------------------------------ */
  2225. /* Frame */
  2226. static int sidebar_width(void)
  2227. {
  2228. return ui.w < 620 ? SIDEBAR_MIN_W : SIDEBAR_W;
  2229. }
  2230. static void settings_draw_frame(void)
  2231. {
  2232. struct ngui_rect all = { 0, 0, ui.w, ui.h };
  2233. struct ngui_rect body = all, side, header, content, footer;
  2234. int side_w = sidebar_width();
  2235. int i, y;
  2236. ngui_fill_rect(&ui, all, ui.theme.bg);
  2237. side = ngui_cut_left(&body, side_w);
  2238. ngui_fill_rect(&ui, side, ui.theme.panel);
  2239. ngui_text_px(&ui, side.x + 14, side.y + 14, "Settings",
  2240. s_len("Settings"), ui.theme.text, 20, 1, 0, 0);
  2241. ngui_text(&ui, side.x + 14, side.y + 40, OS_NAME " " OS_VERSION,
  2242. ui.theme.text_dim);
  2243. y = side.y + 70;
  2244. for (i = 0; i < SEC_COUNT; i++) {
  2245. struct ngui_rect r = { side.x + 8, y, side.w - 16, NAV_ROW_H };
  2246. if (settings_row(ID_NAV_BASE + i, r, sections[i].label,
  2247. sections[i].subtitle, 0,
  2248. cur_section == i ? 1 : 0, -1))
  2249. request_action(A_NAV, i);
  2250. y += NAV_ROW_H + NAV_GAP;
  2251. }
  2252. header = ngui_cut_top(&body, HEADER_H);
  2253. ngui_text_px(&ui, header.x + 16, header.y + 8, sections[cur_section].label,
  2254. s_len(sections[cur_section].label), ui.theme.text, 20, 1,
  2255. 0, 0);
  2256. ngui_text_utf8(&ui, header.x + 16, header.y + 32,
  2257. sections[cur_section].subtitle, ui.theme.text_dim);
  2258. ngui_fill_rect(&ui, (struct ngui_rect){ header.x + 16,
  2259. header.y + HEADER_H - 1,
  2260. header.w - 32, 1 },
  2261. ui.theme.panel_alt);
  2262. footer = ngui_cut_bottom(&body, FOOTER_H);
  2263. ngui_fill_rect(&ui, footer, ui.theme.panel);
  2264. ngui_fill_rect(&ui, (struct ngui_rect){ footer.x, footer.y,
  2265. footer.w, 1 },
  2266. ui.theme.panel_alt);
  2267. if (status[0] && status_until &&
  2268. (int)(u_ticks_ms() - status_until) < 0) {
  2269. ngui_text_ellipsis_utf8(&ui,
  2270. (struct ngui_rect){ footer.x + 12, footer.y + 6,
  2271. footer.w - 24, 18 },
  2272. footer.x + 12, footer.y + 6, status,
  2273. status_error ? ui.theme.error : ui.theme.success);
  2274. } else {
  2275. ngui_text_utf8(&ui, footer.x + 12, footer.y + 6,
  2276. "Changes are saved automatically.",
  2277. ui.theme.text_dim);
  2278. }
  2279. content = body;
  2280. content.x += 16;
  2281. content.w -= 24;
  2282. content.y += 6;
  2283. content.h -= 8;
  2284. {
  2285. int picker_only = (cur_section == SEC_APPEARANCE &&
  2286. picker_mode != 0);
  2287. if (picker_only) {
  2288. /* The picker has its own scroll pane; do not let the
  2289. * outer pane eat the wheel or PgUp/PgDn keys. */
  2290. content_scroll.offset = 0;
  2291. ngui_push_clip(&ui, content);
  2292. y = content.y;
  2293. } else {
  2294. ngui_scroll_begin(&ui, content, &content_scroll);
  2295. ngui_scroll_wheel(&ui, content, &content_scroll);
  2296. ngui_scroll_keys(&ui, &content_scroll);
  2297. y = content.y - content_scroll.offset;
  2298. }
  2299. switch (cur_section) {
  2300. case SEC_NETWORK:
  2301. draw_network(content, &y);
  2302. break;
  2303. case SEC_POWER:
  2304. draw_power(content, &y);
  2305. break;
  2306. case SEC_DATETIME:
  2307. draw_datetime(content, &y);
  2308. break;
  2309. case SEC_APPEARANCE:
  2310. draw_appearance(content, &y);
  2311. break;
  2312. case SEC_WINDOWS:
  2313. draw_windows(content, &y);
  2314. break;
  2315. case SEC_ABOUT:
  2316. draw_about(content, &y);
  2317. break;
  2318. default:
  2319. break;
  2320. }
  2321. if (picker_only) {
  2322. ngui_pop_clip(&ui);
  2323. } else {
  2324. content_scroll.content_h =
  2325. y + content_scroll.offset - content.y + GAP;
  2326. ngui_scroll_end(&ui, content, &content_scroll);
  2327. }
  2328. }
  2329. }
  2330. /* ------------------------------------------------------------------ */
  2331. /* Actions */
  2332. static void set_section(int s)
  2333. {
  2334. if (s < 0 || s >= SEC_COUNT)
  2335. return;
  2336. if (s == cur_section)
  2337. return;
  2338. cur_section = s;
  2339. content_scroll.offset = 0;
  2340. picker_mode = 0;
  2341. if (s == SEC_NETWORK && !devs_loaded) {
  2342. set_status("Loading network devices...", 0);
  2343. paint_current();
  2344. load_devices();
  2345. set_status("", 0);
  2346. if (wifi_present && wst.state < SYS_WIFI_ST_ASSOC)
  2347. start_scan();
  2348. }
  2349. if (s == SEC_DATETIME)
  2350. load_datetime_fields(0);
  2351. if (s == SEC_APPEARANCE)
  2352. wallpaper_refresh();
  2353. ngui_set_focus(&ui, 0);
  2354. ngui_invalidate(&ui);
  2355. }
  2356. static void handle_action(void)
  2357. {
  2358. int a = pending_action, arg = pending_arg;
  2359. pending_action = A_NONE;
  2360. pending_arg = 0;
  2361. switch (a) {
  2362. case A_NAV:
  2363. set_section(arg);
  2364. break;
  2365. case A_REFRESH:
  2366. set_status("Refreshing network devices...", 0);
  2367. load_devices();
  2368. set_status("Device list refreshed", 0);
  2369. ngui_invalidate(&ui);
  2370. break;
  2371. case A_SCAN:
  2372. start_scan();
  2373. break;
  2374. case A_DISCONNECT:
  2375. u_wifi(SYS_WIFI_DISCONNECT, 0, 0);
  2376. load_status();
  2377. set_status("WiFi disconnected", 0);
  2378. ngui_invalidate(&ui);
  2379. break;
  2380. case A_DEV:
  2381. select_device(arg);
  2382. break;
  2383. case A_BSS:
  2384. select_bss(arg);
  2385. break;
  2386. case A_SAVED:
  2387. load_saved();
  2388. content_scroll.offset = 0;
  2389. net_view = NET_SAVED;
  2390. ngui_invalidate(&ui);
  2391. break;
  2392. case A_SAVED_BACK:
  2393. net_view = NET_MAIN;
  2394. content_scroll.offset = 0;
  2395. ngui_invalidate(&ui);
  2396. break;
  2397. case A_FORGET:
  2398. if (arg >= 0 && arg < nsaved) {
  2399. if (wifi_store_forget(saved[arg].ssid,
  2400. saved[arg].ssid_len) > 0) {
  2401. load_saved();
  2402. set_status("Saved network removed", 0);
  2403. } else {
  2404. set_status("Could not remove the saved network", 1);
  2405. }
  2406. }
  2407. ngui_invalidate(&ui);
  2408. break;
  2409. case A_PASS_OK: {
  2410. char pass[65];
  2411. unsigned int i, n;
  2412. ngui_input_value(&pass_in, pass, sizeof(pass));
  2413. n = s_len(pass);
  2414. if (n == 0) {
  2415. set_status("Enter the WPA2 passphrase", 1);
  2416. } else if (n < 8 || n > 63) {
  2417. int hex = n == 64;
  2418. if (hex) {
  2419. unsigned int j;
  2420. for (j = 0; j < n; j++) {
  2421. char c = pass[j];
  2422. if (!((c >= '0' && c <= '9') ||
  2423. (c >= 'a' && c <= 'f') ||
  2424. (c >= 'A' && c <= 'F'))) {
  2425. hex = 0;
  2426. break;
  2427. }
  2428. }
  2429. }
  2430. if (hex)
  2431. start_connect(pass);
  2432. else
  2433. set_status("Use 8-63 characters or 64 hex digits",
  2434. 1);
  2435. } else {
  2436. start_connect(pass);
  2437. }
  2438. for (i = 0; i < sizeof(pass); i++)
  2439. ((volatile char *)pass)[i] = 0;
  2440. break;
  2441. }
  2442. case A_PASS_CANCEL:
  2443. case A_CONN_CANCEL:
  2444. if (net_view == NET_CONNECT)
  2445. cancel_connect();
  2446. else {
  2447. net_view = NET_MAIN;
  2448. set_status("", 0);
  2449. ngui_invalidate(&ui);
  2450. }
  2451. break;
  2452. case A_POWER:
  2453. set_power_mode(arg);
  2454. break;
  2455. case A_DATE_SET:
  2456. apply_datetime();
  2457. break;
  2458. case A_DATE_NOW:
  2459. load_datetime_fields(1);
  2460. set_status("Date and time reloaded", 0);
  2461. break;
  2462. case A_DATE_NTP:
  2463. sync_ntp();
  2464. break;
  2465. case A_BG:
  2466. if (arg >= 0 && arg < NBG) {
  2467. prefs.bg_rgb = bg_choices[arg].rgb;
  2468. commit_prefs("Background colour saved");
  2469. }
  2470. break;
  2471. case A_WALL_CHOOSE: {
  2472. struct sys_stat st;
  2473. picker_mode = 1;
  2474. content_scroll.offset = 0;
  2475. if (u_stat("/data", &st) == 0 && st.is_dir)
  2476. load_picker_dir("/data");
  2477. else if (u_stat("/share", &st) == 0 && st.is_dir)
  2478. load_picker_dir("/share");
  2479. else
  2480. load_picker_dir("/");
  2481. ngui_invalidate(&ui);
  2482. break;
  2483. }
  2484. case A_WALL_REMOVE:
  2485. u_syscall1(SYS_UNLINK, (int)WM_WALLPAPER_DATA);
  2486. u_syscall1(SYS_UNLINK, (int)WM_WALLPAPER_SHARE);
  2487. u_syscall1(SYS_WM_WALLPAPER, 1);
  2488. wallpaper_refresh();
  2489. if (wallpaper_present)
  2490. set_status("The wallpaper on the data volume could not be removed",
  2491. 1);
  2492. else
  2493. set_status("Wallpaper removed; the desktop colour is visible",
  2494. 0);
  2495. ngui_invalidate(&ui);
  2496. break;
  2497. case A_PICK_UP: {
  2498. char parent[PICK_PATH_MAX];
  2499. pick_parent(parent, sizeof(parent));
  2500. load_picker_dir(parent);
  2501. ngui_invalidate(&ui);
  2502. break;
  2503. }
  2504. case A_PICK_CANCEL:
  2505. picker_mode = 0;
  2506. ngui_invalidate(&ui);
  2507. break;
  2508. case A_PICK_ROW:
  2509. picker_activate(arg);
  2510. break;
  2511. case A_WIN_SHADOW:
  2512. prefs.shadow = prefs.shadow ? 0 : 1;
  2513. commit_prefs(prefs.shadow ? "Window shadows enabled" :
  2514. "Window shadows disabled");
  2515. break;
  2516. case A_WIN_SNAP:
  2517. prefs.snap = prefs.snap ? 0 : 1;
  2518. commit_prefs(prefs.snap ? "Edge snapping enabled" :
  2519. "Edge snapping disabled");
  2520. break;
  2521. case A_WIN_RESIST:
  2522. prefs.resist = prefs.resist ? 0 : 1;
  2523. commit_prefs(prefs.resist ? "Edge resistance enabled" :
  2524. "Edge resistance disabled");
  2525. break;
  2526. default:
  2527. break;
  2528. }
  2529. }
  2530. static void check_section_request(void)
  2531. {
  2532. char buf[24];
  2533. unsigned int now_ms = u_ticks_ms();
  2534. int n, i;
  2535. if (now_ms - last_request_ms < 250)
  2536. return;
  2537. last_request_ms = now_ms;
  2538. n = u_read_file(WM_SETTINGS_REQUEST, buf, sizeof(buf) - 1);
  2539. if (n <= 0)
  2540. return;
  2541. buf[n] = '\0';
  2542. while (n > 0 && (buf[n - 1] == '\n' || buf[n - 1] == '\r' ||
  2543. buf[n - 1] == ' '))
  2544. buf[--n] = '\0';
  2545. /* Consume the request: the WM writes a fresh file for every pill
  2546. * click, and a stale one must not pull the user back after they
  2547. * picked another section by hand. */
  2548. u_syscall1(SYS_UNLINK, (int)WM_SETTINGS_REQUEST);
  2549. if (!buf[0])
  2550. return;
  2551. for (i = 0; i < SEC_COUNT; i++) {
  2552. if (s_eq(buf, sections[i].arg)) {
  2553. if (i != cur_section)
  2554. set_section(i);
  2555. break;
  2556. }
  2557. }
  2558. }
  2559. void _start(const char *arg)
  2560. {
  2561. struct win_info wi;
  2562. unsigned int sw = 0, sh = 0;
  2563. int w, h;
  2564. u_screen_size(&sw, &sh);
  2565. if (!sw || !sh) {
  2566. sw = 1024;
  2567. sh = 768;
  2568. }
  2569. w = (int)sw - 40;
  2570. h = (int)sh - 80;
  2571. if (w > 900)
  2572. w = 900;
  2573. if (h > 620)
  2574. h = 620;
  2575. if (w < 460)
  2576. w = 460;
  2577. if (h < 360)
  2578. h = 360;
  2579. prefs_defaults();
  2580. if (u_wm_get_prefs(&prefs) != 0)
  2581. prefs_defaults();
  2582. /* The WM may not have reached the data volume yet (the first boot
  2583. * or a quick launch); prefer the persisted file when it is valid. */
  2584. if (prefs_file_load() == 0)
  2585. u_wm_set_prefs(&prefs);
  2586. cur_section = section_from_name(arg);
  2587. if (u_win_create((unsigned int)w, (unsigned int)h, "Settings", &wi) != 0) {
  2588. u_puts("settings: no graphics window\n");
  2589. u_exit(1);
  2590. }
  2591. ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
  2592. ngui_font_attach(&ui);
  2593. pass_in.secret = 1;
  2594. pass_in.placeholder = "WPA2 passphrase";
  2595. ntp_in.placeholder = "pool.ntp.org";
  2596. if (cur_section == SEC_NETWORK) {
  2597. set_status("Loading network devices...", 0);
  2598. paint_current();
  2599. }
  2600. load_status();
  2601. load_saved();
  2602. wallpaper_refresh();
  2603. if (cur_section == SEC_NETWORK) {
  2604. load_devices();
  2605. set_status("", 0);
  2606. if (wifi_present && wst.state < SYS_WIFI_ST_ASSOC)
  2607. start_scan();
  2608. }
  2609. if (cur_section == SEC_DATETIME)
  2610. load_datetime_fields(0);
  2611. last_status_ms = u_ticks_ms();
  2612. for (;;) {
  2613. static int first_frame = 1;
  2614. unsigned int now_ms = u_ticks_ms();
  2615. drain_tty();
  2616. poll_scan();
  2617. poll_connect();
  2618. check_section_request();
  2619. if (now_ms - last_status_ms >= 1000) {
  2620. last_status_ms = now_ms;
  2621. load_status();
  2622. if (cur_section == SEC_DATETIME)
  2623. load_datetime_fields(0);
  2624. ngui_invalidate(&ui);
  2625. }
  2626. if (status_until && (int)(now_ms - status_until) >= 0) {
  2627. status[0] = '\0';
  2628. status_until = 0;
  2629. ngui_invalidate(&ui);
  2630. }
  2631. if (ngui_begin(&ui)) {
  2632. settings_draw_frame();
  2633. ngui_end(&ui);
  2634. if (first_frame) {
  2635. first_frame = 0;
  2636. ngui_invalidate(&ui);
  2637. }
  2638. }
  2639. if (pending_action)
  2640. handle_action();
  2641. if (ngui_key_peek(&ui) == NGUI_KEY_ESC) {
  2642. ngui_key(&ui);
  2643. if (picker_mode) {
  2644. picker_mode = 0;
  2645. ngui_invalidate(&ui);
  2646. } else if (net_view != NET_MAIN) {
  2647. net_view = NET_MAIN;
  2648. set_status("", 0);
  2649. ngui_invalidate(&ui);
  2650. } else if (ui.focus == 0) {
  2651. quit = 1;
  2652. }
  2653. }
  2654. if (quit)
  2655. break;
  2656. u_sleep_ms(20);
  2657. }
  2658. ngui_font_detach(&ui);
  2659. u_win_close();
  2660. u_exit(0);
  2661. }