ucmd_netmenu.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /* netmenu: the top-bar control center popover.
  2. *
  3. * A compact quick panel: battery/AC and clock summary, a brightness slider
  4. * and one tile per Settings section. Clicking a tile asks the WM to open
  5. * that section in the real Settings window (SYS_WM_OPEN_SETTINGS) and
  6. * dismisses the popover; the section editors themselves live in
  7. * /ramfs/bin/settings, not here. */
  8. #include "ucmd_lib.h"
  9. #include "ngui.h"
  10. #define MENU_GAP 10
  11. #define TILE_H 56
  12. #define TILE_COLS 2
  13. #define SECTION_H 28
  14. #define STATUS_MS 5000
  15. static struct ngui ui;
  16. static struct ngui_popover pop;
  17. static struct ngui_slider brightness_slider;
  18. static struct ngui_scroll main_scroll;
  19. static struct sys_netinfo net;
  20. static int net_present;
  21. static struct sys_powerinfo powerinfo;
  22. static int power_ok;
  23. static struct sys_datetime now;
  24. static int now_ok;
  25. static int brightness = -1;
  26. static int cpufreq_mode = -1;
  27. static unsigned int screen_w = 1024, screen_h = 768;
  28. static char status[160];
  29. static int status_error;
  30. static unsigned int status_until;
  31. static unsigned int last_status;
  32. static unsigned int s_len(const char *s)
  33. {
  34. unsigned int n = 0;
  35. while (s && s[n])
  36. n++;
  37. return n;
  38. }
  39. static char *s_put(char *p, const char *s)
  40. {
  41. while (s && *s)
  42. *p++ = *s++;
  43. return p;
  44. }
  45. static char *put_dec(char *p, unsigned int v)
  46. {
  47. char b[10];
  48. unsigned int n = 0;
  49. do {
  50. b[n++] = (char)('0' + v % 10);
  51. v /= 10;
  52. } while (v);
  53. while (n)
  54. *p++ = b[--n];
  55. return p;
  56. }
  57. static char *put_2(char *p, unsigned int v)
  58. {
  59. *p++ = (char)('0' + (v / 10) % 10);
  60. *p++ = (char)('0' + v % 10);
  61. return p;
  62. }
  63. static void format_time(char *b, const struct sys_datetime *dt)
  64. {
  65. char *p = put_2(b, dt->hour);
  66. *p++ = ':';
  67. p = put_2(p, dt->minute);
  68. *p = '\0';
  69. }
  70. static void format_date(char *b, const struct sys_datetime *dt)
  71. {
  72. char *p = put_dec(b, dt->year);
  73. *p++ = '-';
  74. p = put_2(p, dt->month);
  75. *p++ = '-';
  76. p = put_2(p, dt->day);
  77. *p = '\0';
  78. }
  79. static void set_status(const char *msg, int err)
  80. {
  81. unsigned int i;
  82. for (i = 0; msg && msg[i] && i + 1 < sizeof(status); i++)
  83. status[i] = msg[i];
  84. status[i] = '\0';
  85. status_error = err;
  86. status_until = msg && msg[0] ? u_ticks_ms() + STATUS_MS : 0;
  87. ngui_invalidate(&ui);
  88. }
  89. static const char *power_mode_name(int mode)
  90. {
  91. switch (mode) {
  92. case SYS_CPUFREQ_MAX:
  93. return "Performance";
  94. case SYS_CPUFREQ_MIN:
  95. return "Power saver";
  96. case SYS_CPUFREQ_OFF:
  97. return "Off";
  98. case SYS_CPUFREQ_FIXED:
  99. return "Fixed";
  100. case SYS_CPUFREQ_AUTO:
  101. return "Balanced";
  102. default:
  103. return "Power and brightness";
  104. }
  105. }
  106. static void load_status(void)
  107. {
  108. net_present = (u_net_info(&net) == 1 && net.present);
  109. power_ok = (u_power_info(&powerinfo, 0) == 0);
  110. now_ok = (u_rtc_get(&now) == 0);
  111. brightness = u_backlight_get();
  112. cpufreq_mode = u_cpufreq_get();
  113. }
  114. /* Battery/AC on the left, wall clock on the right. */
  115. static void draw_summary(struct ngui_rect r)
  116. {
  117. char tbuf[16], dbuf[16], title[32];
  118. char *p;
  119. int batt = -1, charging = 0, discharging = 0, ac = -1;
  120. int tw;
  121. unsigned short dot;
  122. const char *sub_p = "";
  123. unsigned int i;
  124. if (power_ok) {
  125. ac = powerinfo.ac_online;
  126. for (i = 0; i < SYS_POWER_BATT_MAX; i++) {
  127. struct sys_battery *b = &powerinfo.battery[i];
  128. if (!b->present)
  129. continue;
  130. if (b->percent_valid)
  131. batt = (int)b->percent;
  132. charging = (int)b->charging;
  133. discharging = (int)b->discharging;
  134. break;
  135. }
  136. }
  137. if (batt >= 0) {
  138. p = s_put(title, "Battery ");
  139. p = put_dec(p, (unsigned int)batt);
  140. *p++ = '%';
  141. *p = '\0';
  142. if (charging)
  143. sub_p = "Charging";
  144. else if (discharging)
  145. sub_p = "On battery";
  146. else if (ac > 0)
  147. sub_p = "Plugged in";
  148. dot = batt <= 10 ? ui.theme.error :
  149. batt <= 25 ? ui.theme.warning : ui.theme.success;
  150. } else if (power_ok && ac > 0) {
  151. s_put(title, "AC power");
  152. sub_p = "No battery";
  153. dot = ui.theme.success;
  154. } else if (power_ok) {
  155. s_put(title, "Desktop");
  156. sub_p = "No battery";
  157. dot = ui.theme.text_dim;
  158. } else {
  159. s_put(title, "Power");
  160. sub_p = "Unavailable";
  161. dot = ui.theme.warning;
  162. }
  163. ngui_fill_round_rect(&ui, r, NGUI_RADIUS, NGUI_RADIUS,
  164. NGUI_RADIUS, NGUI_RADIUS, ui.theme.panel);
  165. ngui_fill_round_rect(&ui,
  166. (struct ngui_rect){ r.x + 14, r.y + 23, 10, 10 },
  167. 5, 5, 5, 5, dot);
  168. ngui_text_px(&ui, r.x + 30, r.y + 8, title, s_len(title),
  169. ui.theme.text, 14, 1, 0, 0);
  170. ngui_text_ellipsis_utf8(&ui,
  171. (struct ngui_rect){ r.x + 30, r.y + 30, r.w / 2 - 34, 18 },
  172. r.x + 30, r.y + 30, sub_p, ui.theme.text_dim);
  173. ngui_fill_rect(&ui,
  174. (struct ngui_rect){ r.x + r.w / 2, r.y + 10, 1, r.h - 20 },
  175. ui.theme.button);
  176. if (now_ok) {
  177. format_time(tbuf, &now);
  178. format_date(dbuf, &now);
  179. } else {
  180. s_put(tbuf, "--:--");
  181. s_put(dbuf, "No clock");
  182. }
  183. tw = (int)ngui_text_width_ui(&ui, tbuf);
  184. ngui_text_px(&ui, r.x + r.w - 14 - tw, r.y + 8, tbuf, s_len(tbuf),
  185. ui.theme.text, 14, 1, 0, 0);
  186. tw = (int)ngui_text_width_ui(&ui, dbuf);
  187. ngui_text_px(&ui, r.x + r.w - 14 - tw, r.y + 30, dbuf, s_len(dbuf),
  188. ui.theme.text_dim, 14, 0, 0, 0);
  189. }
  190. static void draw_brightness(struct ngui_rect r)
  191. {
  192. struct ngui_rect slider = { r.x + 14, r.y + 26, r.w - 28, 16 };
  193. char b[8], *p;
  194. int v = brightness;
  195. int tw;
  196. ngui_fill_round_rect(&ui, r, NGUI_RADIUS, NGUI_RADIUS,
  197. NGUI_RADIUS, NGUI_RADIUS, ui.theme.panel);
  198. ngui_text_px(&ui, r.x + 14, r.y + 6, "Brightness", 10,
  199. ui.theme.text, 14, 1, 0, 0);
  200. p = put_dec(b, (unsigned int)(v < 0 ? 0 : v));
  201. *p++ = '%';
  202. *p = '\0';
  203. tw = (int)ngui_text_width_ui(&ui, b);
  204. ngui_text_px(&ui, r.x + r.w - 14 - tw, r.y + 6, b, s_len(b),
  205. ui.theme.text_dim, 14, 0, 0, 0);
  206. if (ngui_slider(&ui, 11, &brightness_slider, slider, &v, 5, 100)) {
  207. int got = u_backlight_set(v);
  208. brightness = got >= 0 ? got : v;
  209. }
  210. }
  211. static int tile_grid_height(void)
  212. {
  213. return (TILE_H * 3) + MENU_GAP * 2;
  214. }
  215. static void draw_tiles(struct ngui_rect area, int *yy)
  216. {
  217. static const int secs[6] = {
  218. WM_SETTINGS_NETWORK, WM_SETTINGS_POWER, WM_SETTINGS_DATETIME,
  219. WM_SETTINGS_APPEARANCE, WM_SETTINGS_WINDOWS, WM_SETTINGS_ABOUT
  220. };
  221. char titles[6][18], subs[6][48];
  222. int i, row = 0, colw = (area.w - MENU_GAP) / 2;
  223. int y = *yy;
  224. s_put(titles[0], "Network");
  225. if (net_present)
  226. s_put(subs[0], net.name);
  227. else
  228. s_put(subs[0], "Offline");
  229. s_put(titles[1], "Power");
  230. s_put(subs[1], power_mode_name(cpufreq_mode));
  231. s_put(titles[2], "Date & Time");
  232. if (now_ok)
  233. format_time(subs[2], &now);
  234. else
  235. s_put(subs[2], "Set clock");
  236. s_put(titles[3], "Appearance");
  237. s_put(subs[3], "Wallpaper and colour");
  238. s_put(titles[4], "Windows");
  239. s_put(subs[4], "Shadows and snapping");
  240. s_put(titles[5], "About");
  241. s_put(subs[5], "System information");
  242. for (i = 0; i < 6; i++) {
  243. int col = i & 1;
  244. int w = (i + 1 < 6) ? colw : (col == 0 ? area.w : colw);
  245. struct ngui_rect r = {
  246. area.x + col * (colw + MENU_GAP),
  247. y,
  248. w,
  249. TILE_H
  250. };
  251. if (r.y + r.h > area.y && r.y < area.y + area.h &&
  252. ngui_tile(&ui, 100 + i, r, titles[i], subs[i], 0)) {
  253. if (u_open_settings(secs[i]) != 0)
  254. set_status("Cannot open Settings", 1);
  255. else {
  256. u_win_close();
  257. u_exit(0);
  258. }
  259. }
  260. if (col == 1 || i + 1 >= 6) {
  261. y += TILE_H;
  262. row++;
  263. if (row < 3)
  264. y += MENU_GAP;
  265. }
  266. }
  267. *yy = y;
  268. }
  269. static void draw(void)
  270. {
  271. struct ngui_rect body;
  272. struct ngui_rect card, bright, area;
  273. int content_h, y;
  274. ngui_popover_begin(&ui, &pop);
  275. body = pop.body;
  276. card = ngui_cut_top(&body, 56);
  277. draw_summary(card);
  278. body.y += MENU_GAP;
  279. body.h -= MENU_GAP;
  280. if (brightness >= 0) {
  281. bright = ngui_cut_top(&body, 52);
  282. draw_brightness(bright);
  283. body.y += MENU_GAP;
  284. body.h -= MENU_GAP;
  285. }
  286. content_h = SECTION_H + tile_grid_height();
  287. main_scroll.content_h = content_h;
  288. area = body;
  289. ngui_scroll_begin(&ui, area, &main_scroll);
  290. ngui_scroll_wheel(&ui, area, &main_scroll);
  291. ngui_scroll_keys(&ui, &main_scroll);
  292. y = area.y - main_scroll.offset;
  293. ngui_text_px(&ui, area.x, y + (SECTION_H - 17) / 2,
  294. "Quick settings", s_len("Quick settings"),
  295. ui.theme.text_dim, 14, 1, 0, 0);
  296. y += SECTION_H;
  297. draw_tiles(area, &y);
  298. ngui_scroll_end(&ui, area, &main_scroll);
  299. if (status[0] && status_until &&
  300. (int)(u_ticks_ms() - status_until) < 0)
  301. ngui_popover_footer(&ui, &pop, status,
  302. status_error ? ui.theme.error :
  303. ui.theme.success);
  304. }
  305. static int desired_height(void)
  306. {
  307. int h;
  308. h = 12 + 56 + MENU_GAP;
  309. if (brightness >= 0)
  310. h += 52 + MENU_GAP;
  311. h += SECTION_H + tile_grid_height() + 10 + 34;
  312. if (h > (int)screen_h - 38)
  313. h = (int)screen_h - 38;
  314. if (h < 220)
  315. h = 220;
  316. return h;
  317. }
  318. static int panel_width(void)
  319. {
  320. int w = 420;
  321. if (w > (int)screen_w - 8)
  322. w = (int)screen_w - 8;
  323. if (w < 340)
  324. w = 340;
  325. return w;
  326. }
  327. void _start(const char *arg)
  328. {
  329. struct win_info wi;
  330. int w, h;
  331. (void)arg;
  332. u_screen_size(&screen_w, &screen_h);
  333. load_status();
  334. w = panel_width();
  335. h = desired_height();
  336. if (u_win_create_popover((unsigned int)w, (unsigned int)h,
  337. "Control Center", &wi) != 0 &&
  338. u_win_create_popover((unsigned int)(w > 400 ? 400 : w),
  339. (unsigned int)(h > 480 ? 480 : h),
  340. "Control Center", &wi) != 0 &&
  341. u_win_create_popover(340, 380, "Control Center", &wi) != 0) {
  342. u_exit(1);
  343. }
  344. ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
  345. ngui_font_attach(&ui);
  346. for (;;) {
  347. static int first_frame = 1;
  348. unsigned int now_ms = u_ticks_ms();
  349. if (now_ms - last_status >= 1000) {
  350. last_status = now_ms;
  351. load_status();
  352. ngui_invalidate(&ui);
  353. }
  354. if (status_until && (int)(now_ms - status_until) >= 0) {
  355. status[0] = '\0';
  356. status_until = 0;
  357. ngui_invalidate(&ui);
  358. }
  359. if (ngui_begin(&ui)) {
  360. draw();
  361. ngui_end(&ui);
  362. if (first_frame) {
  363. first_frame = 0;
  364. ngui_invalidate(&ui);
  365. }
  366. }
  367. if (ngui_key_peek(&ui) == NGUI_KEY_ESC) {
  368. ngui_key(&ui);
  369. break;
  370. }
  371. u_sleep_ms(25);
  372. }
  373. ngui_font_detach(&ui);
  374. u_win_close();
  375. u_exit(0);
  376. }