ucmd_netmenu.c 9.6 KB

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