ucmd_settings.c 69 KB

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