| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429 |
- /* netmenu: the top-bar control center popover.
- *
- * A compact quick panel: battery/AC and clock summary, a brightness slider
- * and one tile per Settings section. Clicking a tile asks the WM to open
- * that section in the real Settings window (SYS_WM_OPEN_SETTINGS) and
- * dismisses the popover; the section editors themselves live in
- * /ramfs/bin/settings, not here. */
- #include "ucmd_lib.h"
- #include "ngui.h"
- #define MENU_GAP 10
- #define TILE_H 56
- #define TILE_COLS 2
- #define SECTION_H 28
- #define STATUS_MS 5000
- static struct ngui ui;
- static struct ngui_popover pop;
- static struct ngui_slider brightness_slider;
- static struct ngui_scroll main_scroll;
- static struct sys_netinfo net;
- static int net_present;
- static struct sys_powerinfo powerinfo;
- static int power_ok;
- static struct sys_datetime now;
- static int now_ok;
- static int brightness = -1;
- static int cpufreq_mode = -1;
- static unsigned int screen_w = 1024, screen_h = 768;
- static char status[160];
- static int status_error;
- static unsigned int status_until;
- static unsigned int last_status;
- static unsigned int s_len(const char *s)
- {
- unsigned int n = 0;
- while (s && s[n])
- n++;
- return n;
- }
- static char *s_put(char *p, const char *s)
- {
- while (s && *s)
- *p++ = *s++;
- *p = '\0';
- return p;
- }
- static char *put_dec(char *p, unsigned int v)
- {
- char b[10];
- unsigned int n = 0;
- do {
- b[n++] = (char)('0' + v % 10);
- v /= 10;
- } while (v);
- while (n)
- *p++ = b[--n];
- return p;
- }
- static char *put_2(char *p, unsigned int v)
- {
- *p++ = (char)('0' + (v / 10) % 10);
- *p++ = (char)('0' + v % 10);
- return p;
- }
- static void format_time(char *b, const struct sys_datetime *dt)
- {
- char *p = put_2(b, dt->hour);
- *p++ = ':';
- p = put_2(p, dt->minute);
- *p = '\0';
- }
- static void format_date(char *b, const struct sys_datetime *dt)
- {
- char *p = put_dec(b, dt->year);
- *p++ = '-';
- p = put_2(p, dt->month);
- *p++ = '-';
- p = put_2(p, dt->day);
- *p = '\0';
- }
- static void set_status(const char *msg, int err)
- {
- unsigned int i;
- for (i = 0; msg && msg[i] && i + 1 < sizeof(status); i++)
- status[i] = msg[i];
- status[i] = '\0';
- status_error = err;
- status_until = msg && msg[0] ? u_ticks_ms() + STATUS_MS : 0;
- ngui_invalidate(&ui);
- }
- static const char *power_mode_name(int mode)
- {
- switch (mode) {
- case SYS_CPUFREQ_MAX:
- return "Performance";
- case SYS_CPUFREQ_MIN:
- return "Power saver";
- case SYS_CPUFREQ_OFF:
- return "Off";
- case SYS_CPUFREQ_FIXED:
- return "Fixed";
- case SYS_CPUFREQ_AUTO:
- return "Balanced";
- default:
- return "Power and brightness";
- }
- }
- static void load_status(void)
- {
- net_present = (u_net_info(&net) == 1 && net.present);
- power_ok = (u_power_info(&powerinfo, 0) == 0);
- now_ok = (u_rtc_get(&now) == 0);
- brightness = u_backlight_get();
- cpufreq_mode = u_cpufreq_get();
- }
- /* Battery/AC on the left, wall clock on the right. */
- static void draw_summary(struct ngui_rect r)
- {
- char tbuf[16], dbuf[16], title[32];
- char *p;
- int batt = -1, charging = 0, discharging = 0, ac = -1;
- int tw;
- unsigned short dot;
- const char *sub_p = "";
- unsigned int i;
- if (power_ok) {
- ac = powerinfo.ac_online;
- for (i = 0; i < SYS_POWER_BATT_MAX; i++) {
- struct sys_battery *b = &powerinfo.battery[i];
- if (!b->present)
- continue;
- if (b->percent_valid)
- batt = (int)b->percent;
- charging = (int)b->charging;
- discharging = (int)b->discharging;
- break;
- }
- }
- if (batt >= 0) {
- p = s_put(title, "Battery ");
- p = put_dec(p, (unsigned int)batt);
- *p++ = '%';
- *p = '\0';
- if (charging)
- sub_p = "Charging";
- else if (discharging)
- sub_p = "On battery";
- else if (ac > 0)
- sub_p = "Plugged in";
- dot = batt <= 10 ? ui.theme.error :
- batt <= 25 ? ui.theme.warning : ui.theme.success;
- } else if (power_ok && ac > 0) {
- s_put(title, "AC power");
- sub_p = "No battery";
- dot = ui.theme.success;
- } else if (power_ok) {
- s_put(title, "Desktop");
- sub_p = "No battery";
- dot = ui.theme.text_dim;
- } else {
- s_put(title, "Power");
- sub_p = "Unavailable";
- dot = ui.theme.warning;
- }
- ngui_fill_round_rect(&ui, r, NGUI_RADIUS, NGUI_RADIUS,
- NGUI_RADIUS, NGUI_RADIUS, ui.theme.panel);
- ngui_fill_round_rect(&ui,
- (struct ngui_rect){ r.x + 14, r.y + 23, 10, 10 },
- 5, 5, 5, 5, dot);
- ngui_text_px(&ui, r.x + 30, r.y + 8, title, s_len(title),
- ui.theme.text, 14, 1, 0, 0);
- ngui_text_ellipsis_utf8(&ui,
- (struct ngui_rect){ r.x + 30, r.y + 30, r.w / 2 - 34, 18 },
- r.x + 30, r.y + 30, sub_p, ui.theme.text_dim);
- ngui_fill_rect(&ui,
- (struct ngui_rect){ r.x + r.w / 2, r.y + 10, 1, r.h - 20 },
- ui.theme.button);
- if (now_ok) {
- format_time(tbuf, &now);
- format_date(dbuf, &now);
- } else {
- s_put(tbuf, "--:--");
- s_put(dbuf, "No clock");
- }
- tw = (int)ngui_text_width_ui(&ui, tbuf);
- ngui_text_px(&ui, r.x + r.w - 14 - tw, r.y + 8, tbuf, s_len(tbuf),
- ui.theme.text, 14, 1, 0, 0);
- tw = (int)ngui_text_width_ui(&ui, dbuf);
- ngui_text_px(&ui, r.x + r.w - 14 - tw, r.y + 30, dbuf, s_len(dbuf),
- ui.theme.text_dim, 14, 0, 0, 0);
- }
- static void draw_brightness(struct ngui_rect r)
- {
- struct ngui_rect slider = { r.x + 14, r.y + 26, r.w - 28, 16 };
- char b[8], *p;
- int v = brightness;
- int tw;
- ngui_fill_round_rect(&ui, r, NGUI_RADIUS, NGUI_RADIUS,
- NGUI_RADIUS, NGUI_RADIUS, ui.theme.panel);
- ngui_text_px(&ui, r.x + 14, r.y + 6, "Brightness", 10,
- ui.theme.text, 14, 1, 0, 0);
- p = put_dec(b, (unsigned int)(v < 0 ? 0 : v));
- *p++ = '%';
- *p = '\0';
- tw = (int)ngui_text_width_ui(&ui, b);
- ngui_text_px(&ui, r.x + r.w - 14 - tw, r.y + 6, b, s_len(b),
- ui.theme.text_dim, 14, 0, 0, 0);
- if (ngui_slider(&ui, 11, &brightness_slider, slider, &v, 5, 100)) {
- int got = u_backlight_set(v);
- brightness = got >= 0 ? got : v;
- }
- }
- static int tile_grid_height(void)
- {
- return (TILE_H * 3) + MENU_GAP * 2;
- }
- static void draw_tiles(struct ngui_rect area, int *yy)
- {
- static const int secs[6] = {
- WM_SETTINGS_NETWORK, WM_SETTINGS_POWER, WM_SETTINGS_DATETIME,
- WM_SETTINGS_APPEARANCE, WM_SETTINGS_WINDOWS, WM_SETTINGS_ABOUT
- };
- char titles[6][18], subs[6][48];
- int i, row = 0, colw = (area.w - MENU_GAP) / 2;
- int y = *yy;
- s_put(titles[0], "Network");
- if (net_present)
- s_put(subs[0], net.name);
- else
- s_put(subs[0], "Offline");
- s_put(titles[1], "Power");
- s_put(subs[1], power_mode_name(cpufreq_mode));
- s_put(titles[2], "Date & Time");
- if (now_ok)
- format_time(subs[2], &now);
- else
- s_put(subs[2], "Set clock");
- s_put(titles[3], "Appearance");
- s_put(subs[3], "Wallpaper and colour");
- s_put(titles[4], "Windows");
- s_put(subs[4], "Shadows and snapping");
- s_put(titles[5], "About");
- s_put(subs[5], "System information");
- for (i = 0; i < 6; i++) {
- int col = i & 1;
- int w = (i + 1 < 6) ? colw : (col == 0 ? area.w : colw);
- struct ngui_rect r = {
- area.x + col * (colw + MENU_GAP),
- y,
- w,
- TILE_H
- };
- if (r.y + r.h > area.y && r.y < area.y + area.h &&
- ngui_tile(&ui, 100 + i, r, titles[i], subs[i], 0)) {
- if (u_open_settings(secs[i]) != 0)
- set_status("Cannot open Settings", 1);
- else {
- u_win_close();
- u_exit(0);
- }
- }
- if (col == 1 || i + 1 >= 6) {
- y += TILE_H;
- row++;
- if (row < 3)
- y += MENU_GAP;
- }
- }
- *yy = y;
- }
- static void draw(void)
- {
- struct ngui_rect body;
- struct ngui_rect card, bright, area;
- int content_h, y;
- ngui_popover_begin(&ui, &pop);
- body = pop.body;
- card = ngui_cut_top(&body, 56);
- draw_summary(card);
- body.y += MENU_GAP;
- body.h -= MENU_GAP;
- if (brightness >= 0) {
- bright = ngui_cut_top(&body, 52);
- draw_brightness(bright);
- body.y += MENU_GAP;
- body.h -= MENU_GAP;
- }
- content_h = SECTION_H + tile_grid_height();
- main_scroll.content_h = content_h;
- area = body;
- ngui_scroll_begin(&ui, area, &main_scroll);
- ngui_scroll_wheel(&ui, area, &main_scroll);
- ngui_scroll_keys(&ui, &main_scroll);
- y = area.y - main_scroll.offset;
- ngui_text_px(&ui, area.x, y + (SECTION_H - 17) / 2,
- "Quick settings", s_len("Quick settings"),
- ui.theme.text_dim, 14, 1, 0, 0);
- y += SECTION_H;
- draw_tiles(area, &y);
- ngui_scroll_end(&ui, area, &main_scroll);
- if (status[0] && status_until &&
- (int)(u_ticks_ms() - status_until) < 0)
- ngui_popover_footer(&ui, &pop, status,
- status_error ? ui.theme.error :
- ui.theme.success);
- }
- static int desired_height(void)
- {
- int h;
- h = 12 + 56 + MENU_GAP;
- if (brightness >= 0)
- h += 52 + MENU_GAP;
- h += SECTION_H + tile_grid_height() + 10 + 34;
- if (h > (int)screen_h - 38)
- h = (int)screen_h - 38;
- if (h < 220)
- h = 220;
- return h;
- }
- static int panel_width(void)
- {
- int w = 420;
- if (w > (int)screen_w - 8)
- w = (int)screen_w - 8;
- if (w < 340)
- w = 340;
- return w;
- }
- void _start(const char *arg)
- {
- struct win_info wi;
- int w, h;
- (void)arg;
- u_screen_size(&screen_w, &screen_h);
- load_status();
- w = panel_width();
- h = desired_height();
- if (u_win_create_popover((unsigned int)w, (unsigned int)h,
- "Control Center", &wi) != 0 &&
- u_win_create_popover((unsigned int)(w > 400 ? 400 : w),
- (unsigned int)(h > 480 ? 480 : h),
- "Control Center", &wi) != 0 &&
- u_win_create_popover(340, 380, "Control Center", &wi) != 0) {
- u_exit(1);
- }
- ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
- ngui_font_attach(&ui);
- for (;;) {
- static int first_frame = 1;
- unsigned int now_ms = u_ticks_ms();
- if (now_ms - last_status >= 1000) {
- last_status = now_ms;
- load_status();
- ngui_invalidate(&ui);
- }
- if (status_until && (int)(now_ms - status_until) >= 0) {
- status[0] = '\0';
- status_until = 0;
- ngui_invalidate(&ui);
- }
- if (ngui_begin(&ui)) {
- draw();
- ngui_end(&ui);
- if (first_frame) {
- first_frame = 0;
- ngui_invalidate(&ui);
- }
- }
- if (ngui_key_peek(&ui) == NGUI_KEY_ESC) {
- ngui_key(&ui);
- break;
- }
- u_sleep_ms(25);
- }
- ngui_font_detach(&ui);
- u_win_close();
- u_exit(0);
- }
|