| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566 |
- /* Ring-3 window manager: maps the shadow buffer and each window's content,
- * draws the desktop/chrome and composites the terminal cell grids or gfx
- * canvases. All glyphs and colours are userland; the kernel only routes
- * input, stores window state and copies damage to VRAM. */
- #include "ucmd_lib.h"
- #include "uwm_lib.h"
- #include "version.h"
- #define COL_BG 0x202030u
- /* The active title bar uses the toolkit highlight (ngui_theme_dark.selection);
- * the inactive one stays neutral. Windows have no visible frame. */
- #define COL_TITLE_ON 0x365CA6u
- #define COL_TITLE_OFF 0x646464u
- #define COL_CLOSE 0xC64040u
- #define COL_WHITE 0xFFFFFFu
- #define COL_BANNER 0x55FF55u
- struct uwin {
- struct wm_window_info info;
- struct wm_map_info map;
- int mapped;
- unsigned int ack_seq;
- /* WIN_NORMAL, or maximized/snapped to a screen half; restore_* is the
- * geometry to return to when the window leaves that state. */
- int state;
- int restore_x, restore_y;
- unsigned int restore_w, restore_h;
- };
- /* Double-click detection on the title bar. */
- #define WM_DBLCLICK_MS 500
- #define WM_DBLCLICK_SLOP 8
- /* A maximized window restores only after the pointer actually starts a drag,
- * so a plain click on its title bar leaves it maximized. */
- #define WM_DRAG_START 3
- /* Window placement states. Maximize fills the work area below the bar; the
- * snap states fill its left or right half at full height. */
- #define WIN_NORMAL 0
- #define WIN_MAXIMIZED 1
- #define WIN_SNAP_LEFT 2
- #define WIN_SNAP_RIGHT 3
- /* Releasing a title-bar drag with the pointer this close to the left/right
- * screen edge snaps the window to that half; at the top edge it maximizes. */
- #define WM_SNAP_EDGE 2
- /* Edge resistance: a dragged window's edge stops at a screen edge or at the
- * facing edge of another window until the pointer overshoots it by this
- * many pixels, which makes exact side-by-side placement easy. */
- #define WM_RESIST 24
- static struct usurf screen;
- static struct uwin wins[WM_MAX_WINDOWS];
- static unsigned int fbw, fbh;
- static int drag_id = -1;
- static int drag_dx, drag_dy;
- static int drag_mx, drag_my; /* press position, for restore-on-drag */
- static int drag_moved; /* the pointer passed WM_DRAG_START */
- static int resize_id = -1;
- static int resize_edges; /* WM_EDGE_* bits captured at the press */
- static int resize_ox, resize_oy;
- static unsigned int resize_mx, resize_my, resize_ow, resize_oh;
- static unsigned int last_title_ms;
- static int last_title_id = -1, last_title_x, last_title_y;
- static unsigned int prev_buttons;
- static int full_redraw = 1;
- static int boot_term_spawned;
- static int boot_scan_spawned;
- static int boot_scan_pid;
- static int boot_scan_placed;
- static unsigned int boot_scan_deadline;
- static unsigned int bar_kexec_busy, bar_kexec_progress, bar_kexec_total;
- static void remap_window_contents(void);
- static int bar_temp_c = -1;
- static int bar_batt_pct = -1;
- static int bar_batt_charging;
- static int bar_ac_online = -1;
- /* Both top-bar buttons share this geometry: one rounded pill that stops one
- * pixel above the rainbow separator (which starts at WM_BAR_H - 5). */
- #define BAR_BTN_Y 2
- #define BAR_BTN_H 22
- #define BAR_BTN_RADIUS 6
- /* Battery/AC status icon: the Adwaita AC adapter on AC, the battery outline
- * with our own level fill otherwise. The rightmost bar item. */
- #define BAR_PWR_W 24
- #define BAR_PWR_Y 7
- /* The clock, temperature, network and AC/battery labels are one status
- * button. draw_bar() records their bounding rectangle; only that rectangle
- * opens the control panel, so the empty part of the bar is not clickable. */
- static int bar_status_x = -1;
- static unsigned int bar_status_w;
- static int bar_status_hot;
- /* Brightness OSD: a centered bubble shown after the kernel's backlight
- * seq changes, then hidden after OSD_MS. */
- #define OSD_MS 3000
- #define OSD_W 210
- #define OSD_H 60
- static int osd_show; /* should be visible this frame */
- static int osd_visible; /* pixels are on screen */
- static int osd_level;
- static unsigned int osd_until;
- static unsigned int osd_seen_seq;
- static int osd_seen_valid;
- static int osd_x, osd_y;
- static unsigned int osd_w, osd_h;
- static char bar_msg[24];
- static unsigned int bar_msg_until;
- static int bar_msg_dirty;
- static unsigned int stat_frames, stat_ms, stat_px, stat_max, stat_last;
- /* ---- Start menu --------------------------------------------------------
- * The NebOS banner in the top-left corner is the start button. It opens a
- * Windows-like panel drawn by the WM itself (not a window, so it never
- * consumes a WM_MAX_WINDOWS slot and floats above every canvas). The panel
- * lists every GUI app in the pack and ends with a Power row whose submenu
- * reboots or powers off the machine through the privileged kernel command
- * registry (SYS_SPAWN_KCMD). */
- /* The panel mirrors ngui_theme_dark and the netmenu popover: a flat
- * theme.bg surface without a rim, rounded theme.panel summary cards,
- * theme.panel_alt hover rows and the selection colour for an active item. */
- #define SM_BG 0x181C28u
- #define SM_PANEL 0x262B3Au
- #define SM_PANEL_ALT 0x32384Au
- #define SM_TEXT 0xE4E6F0u
- #define SM_TEXT_DIM 0x9197AAu
- #define SM_SEL 0x365CA6u
- #define SM_BTN_HOT 0x46516Cu
- #define SM_ERROR 0xEE7474u
- #define SM_W 252
- #define SM_PAD 12
- #define SM_TOP 10
- #define SM_GAP 6
- #define SM_SECT_H 22
- #define SM_ROW_H 26
- #define SM_MIN_ROW 20
- #define SM_BOTTOM 10
- #define SM_RADIUS 6
- #define SM_SUB_W 160
- #define SM_TILE 18
- #define SM_HIT_NONE (-1)
- #define SM_HIT_START (-2)
- #define SM_HIT_POWER (-3)
- #define SM_HIT_REBOOT (-4)
- #define SM_HIT_POWEROFF (-5)
- #define SM_HIT_MENU (-6) /* inside the panel but not on an entry */
- #define SM_ACT_SPAWN 0
- #define SM_ACT_TERM 1
- #define SM_ACT_SETTINGS 2
- struct sm_app {
- const char *label;
- const char *path;
- const char *arg;
- unsigned int tile;
- char letter;
- unsigned char action;
- };
- /* Every self-contained GUI application in the pack. Terminal and Settings
- * are WM actions (a new terminal canvas and the control panel); edit gets a
- * scratch file so it can start without a file argument. */
- static const struct sm_app sm_apps[] = {
- { "Terminal", "", "", 0x3C8CD2u, 'T', SM_ACT_TERM },
- { "Files", "/ramfs/bin/files", "", 0xE0A030u, 'F', SM_ACT_SPAWN },
- { "Web", "/ramfs/bin/browser", "", 0x4CA0E0u, 'W', SM_ACT_SPAWN },
- { "Gallery", "/ramfs/bin/gallery", "", 0xC864B4u, 'G', SM_ACT_SPAWN },
- { "Chat", "/ramfs/bin/chat", "", 0x55A055u, 'C', SM_ACT_SPAWN },
- { "Clock", "/ramfs/bin/clock", "", 0x8C8C9Cu, 'C', SM_ACT_SPAWN },
- { "Text editor", "/ramfs/bin/edit", "/share/UNTITLED.TXT", 0xD27850u, 'E', SM_ACT_SPAWN },
- { "Colors", "/ramfs/bin/colors", "", 0xC85A5Au, 'P', SM_ACT_SPAWN },
- { "Mines", "/ramfs/bin/mines", "", 0x8C6CD2u, 'M', SM_ACT_SPAWN },
- { "Cards", "/ramfs/bin/solitaire","", 0x3CA08Cu, 'S', SM_ACT_SPAWN },
- { "Disks", "/ramfs/bin/disks", "", 0x5A8CC8u, 'D', SM_ACT_SPAWN },
- { "Tasks", "/ramfs/bin/process", "", 0xE0B040u, 'P', SM_ACT_SPAWN },
- { "Settings", "", "", 0x7A7A9Au, 'S', SM_ACT_SETTINGS },
- { "Date & Time", "/ramfs/bin/settime", "", 0x5AA0A0u, 'D', SM_ACT_SPAWN },
- };
- #define SM_NAPP ((int)(sizeof(sm_apps) / sizeof(sm_apps[0])))
- struct sm_layout {
- int x, y, w, h;
- int row_h;
- int sect_y;
- int row_x, row_w;
- int list_y;
- int power_y;
- int sub_x, sub_y, sub_w, sub_h;
- };
- static int sm_open;
- static int sm_power_open;
- static int sm_start_hot;
- static int sm_hot; /* hovered app index, -1 = none */
- static int sm_power_hot; /* 0 none, 1 Power, 2 Reboot, 3 Power off */
- static void sm_layout_get(struct sm_layout *l);
- static void sm_draw(void);
- static void sm_add_present(struct wm_present_info *p);
- static int sm_update_hover(unsigned int mx, unsigned int my);
- static void sm_close(void);
- static void sm_launch(int idx);
- /* Network status icon (right side of the bar). */
- #define BAR_NET_W 24
- #define BAR_NET_Y 7
- #define BAR_NET_NONE 0
- #define BAR_NET_WIRED 1
- #define BAR_NET_WIFI 2
- #define BAR_NET_USB 3
- static int bar_net_kind;
- static int bar_net_bars;
- static unsigned int bar_net_color = 0xFF5050u;
- static int control_pid;
- static void bar_notice(const char *msg)
- {
- unsigned int i;
- for (i = 0; msg[i] && i + 1 < sizeof(bar_msg); i++)
- bar_msg[i] = msg[i];
- bar_msg[i] = '\0';
- bar_msg_until = u_ticks_ms() + 2000;
- bar_msg_dirty = 1;
- }
- static int free_window_slot(void)
- {
- unsigned int i;
- for (i = 0; i < WM_MAX_WINDOWS; i++)
- if (!wins[i].info.used)
- return 1;
- return 0;
- }
- /* Find a window by owner pid without waiting for the cached sync state:
- * boot ordering needs it before the terminal is spawned. */
- static int window_index_pid(int pid, struct wm_window_info *out)
- {
- unsigned int i;
- for (i = 0; i < WM_MAX_WINDOWS; i++) {
- struct wm_window_info info;
- if (u_wm_info(i, &info) == 1 && info.used &&
- (int)info.pid == pid) {
- if (out)
- *out = info;
- return (int)i;
- }
- }
- return -1;
- }
- static unsigned int rgb16(unsigned int r, unsigned int g, unsigned int b)
- {
- return (r << 16) | (g << 8) | b;
- }
- /* Rainbow gradient: hue 0..1535 (6*256) maps to R→Y→G→C→B→M→R, the same
- * integer interpolation the kernel bar uses. */
- static unsigned int rainbow_rgb(unsigned int hue)
- {
- unsigned int seg = hue / 256, f = hue % 256;
- unsigned int r1, g1, b1, r2, g2, b2;
- switch (seg) {
- case 0: r1=255; g1=0; b1=0; r2=255; g2=255; b2=0; break;
- case 1: r1=255; g1=255; b1=0; r2=0; g2=255; b2=0; break;
- case 2: r1=0; g1=255; b1=0; r2=0; g2=255; b2=255; break;
- case 3: r1=0; g1=255; b1=255; r2=0; g2=0; b2=255; break;
- case 4: r1=0; g1=0; b1=255; r2=255; g2=0; b2=255; break;
- default:r1=255; g1=0; b1=255; r2=255; g2=0; b2=0; break;
- }
- return rgb16((r1 * (256 - f) + r2 * f) / 256,
- (g1 * (256 - f) + g2 * f) / 256,
- (b1 * (256 - f) + b2 * f) / 256);
- }
- static void fill_clip(int x, int y, unsigned int w, unsigned int h,
- unsigned int rgb, int cx, int cy,
- unsigned int cw, unsigned int ch)
- {
- int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
- if (x0 < cx) x0 = cx;
- if (y0 < cy) y0 = cy;
- if (x1 > cx + (int)cw) x1 = cx + (int)cw;
- if (y1 > cy + (int)ch) y1 = cy + (int)ch;
- if (x1 > x0 && y1 > y0)
- usurf_fill(&screen, x0, y0, (unsigned int)(x1 - x0),
- (unsigned int)(y1 - y0), rgb);
- }
- /* Rounded rectangle fill, matching ngui_fill_round_rect()'s pixel-centre
- * corner sampling (the popover's cards and tiles use a 6 px radius). */
- static int sm_isqrt(int v)
- {
- int r = 0, bit = 1 << 30;
- if (v <= 0)
- return 0;
- while (bit > v)
- bit >>= 2;
- while (bit) {
- if (v >= r + bit) {
- v -= r + bit;
- r = (r >> 1) + bit;
- } else {
- r >>= 1;
- }
- bit >>= 2;
- }
- return r;
- }
- static int sm_round_inset(int y, int h, int r)
- {
- int dy2, d2;
- if (r <= 0)
- return 0;
- if (y < r)
- dy2 = 2 * r - 2 * y - 1;
- else if (y >= h - r)
- dy2 = 2 * (y - (h - r)) + 1;
- else
- return 0;
- d2 = 4 * r * r - dy2 * dy2;
- if (d2 < 0)
- d2 = 0;
- return r - (sm_isqrt(d2) + 1) / 2;
- }
- static void sm_fill_round(int x, int y, int w, int h, int r,
- unsigned int rgb)
- {
- int yy, half;
- if (w <= 0 || h <= 0)
- return;
- half = w < h ? w / 2 : h / 2;
- if (r > half)
- r = half;
- if (r <= 0) {
- usurf_fill(&screen, x, y, (unsigned int)w, (unsigned int)h,
- rgb);
- return;
- }
- for (yy = 0; yy < h; yy++) {
- int li = 0, ri = 0;
- if (yy < r) {
- li = sm_round_inset(yy, h, r);
- ri = li;
- }
- if (yy >= h - r) {
- int i = sm_round_inset(yy, h, r);
- if (i > li)
- li = i;
- if (i > ri)
- ri = i;
- }
- if (w - li - ri > 0)
- usurf_fill(&screen, x + li, y + yy,
- (unsigned int)(w - li - ri), 1, rgb);
- }
- }
- /* One shared renderer for the start and status buttons: the same height,
- * corner radius and hover/active colours. */
- static void bar_button_bg(int x, int w, int active, int hot)
- {
- if (active)
- sm_fill_round(x, BAR_BTN_Y, w, BAR_BTN_H, BAR_BTN_RADIUS,
- SM_SEL);
- else if (hot)
- sm_fill_round(x, BAR_BTN_Y, w, BAR_BTN_H, BAR_BTN_RADIUS,
- SM_PANEL_ALT);
- }
- /* The NebOS banner doubles as the start button. Its width follows the
- * proportional font, so the hit area and the highlight use the same
- * geometry. */
- static void bar_start_geom(int *x, int *y, int *w, int *h)
- {
- *x = 2;
- *y = BAR_BTN_Y;
- *h = BAR_BTN_H;
- *w = (int)usurf_text_width(&screen, OS_NAME " v" OS_VERSION) + 20;
- }
- static int bar_start_hit(unsigned int mx, unsigned int my)
- {
- int x, y, w, h;
- bar_start_geom(&x, &y, &w, &h);
- return (int)mx >= x && (int)mx < x + w &&
- (int)my >= y && (int)my < y + h;
- }
- static void bar_start_button(void)
- {
- int x, y, w, h;
- bar_start_geom(&x, &y, &w, &h);
- bar_button_bg(x, w, sm_open, sm_start_hot);
- usurf_text(&screen, x + 10, y + (h - 16) / 2,
- OS_NAME " v" OS_VERSION,
- sm_open ? COL_WHITE : COL_BANNER, 0, 0, fbw, WM_BAR_H);
- }
- static int name_is(const char *a, const char *b)
- {
- while (*a && *b) {
- if (*a != *b)
- return 0;
- a++;
- b++;
- }
- return *a == *b;
- }
- static int net_rssi_bars(int rssi)
- {
- if (rssi >= -55)
- return 4;
- if (rssi >= -65)
- return 3;
- if (rssi >= -75)
- return 2;
- return 1;
- }
- /* Small top-bar pictograms: Adwaita symbolic masks from the pack, centered
- * in their 24 px hit slots. */
- static void bar_draw_net_icon(int x, int y)
- {
- enum uwm_icon ic;
- switch (bar_net_kind) {
- case BAR_NET_WIRED:
- ic = UWM_ICON_NET_WIRED;
- break;
- case BAR_NET_WIFI:
- ic = bar_net_bars > 0 ? UWM_ICON_NET_WIFI_0 + bar_net_bars :
- UWM_ICON_NET_WIFI_OFF;
- break;
- case BAR_NET_USB:
- ic = UWM_ICON_NET_USB;
- break;
- default:
- ic = UWM_ICON_NET_WIRED_OFF;
- break;
- }
- (void)usurf_icon(&screen, x + 4, y, ic, bar_net_color);
- }
- /* 1 Hz poll: cached state -> the bar's network icon. The WiFi status call is
- * only made when WiFi is the active netdev, so a machine without a card does
- * not run a PCI scan on every tick. */
- static int poll_net_status(void)
- {
- struct sys_netinfo ni;
- int kind = BAR_NET_NONE, bars = 0, have_ip = 0;
- unsigned int color = 0xFF5050u;
- int changed;
- int i;
- if (u_net_info(&ni) == 1 && ni.present) {
- for (i = 0; i < 4; i++)
- if (ni.ip[i])
- have_ip = 1;
- if (name_is(ni.name, "wifi")) {
- struct sys_wifi_status ws;
- if (u_wifi(SYS_WIFI_STATUS, (int)&ws, 0) == 0 &&
- ws.present) {
- kind = BAR_NET_WIFI;
- if (ws.state >= SYS_WIFI_ST_ASSOC) {
- bars = net_rssi_bars(ws.rssi);
- color = (ws.state >= SYS_WIFI_ST_LINK &&
- have_ip) ?
- 0x55FF55u : 0xFFD250u;
- } else {
- /* Card present, not associated. */
- bars = 0;
- color = 0xFF5050u;
- }
- }
- } else if (name_is(ni.name, "rndis")) {
- kind = BAR_NET_USB;
- color = have_ip ? 0x55FF55u : 0xFFD250u;
- } else {
- kind = BAR_NET_WIRED;
- color = have_ip ? 0x55FF55u : 0xFFD250u;
- }
- }
- changed = kind != bar_net_kind || bars != bar_net_bars ||
- color != bar_net_color;
- bar_net_kind = kind;
- bar_net_bars = bars;
- bar_net_color = color;
- return changed;
- }
- static unsigned int temp_color(int c)
- {
- if (c >= 80)
- return 0xFF5050u;
- if (c >= 60)
- return 0xFFD250u;
- return 0x55FF55u;
- }
- /* "43C" (no libc printf here). */
- static unsigned int temp_text(char *b, int c)
- {
- unsigned int n = 0, v = (unsigned int)c;
- if (v >= 100)
- b[n++] = (char)('0' + (v / 100) % 10);
- if (v >= 10)
- b[n++] = (char)('0' + (v / 10) % 10);
- b[n++] = (char)('0' + v % 10);
- b[n++] = 'C';
- b[n] = '\0';
- return n;
- }
- static unsigned int batt_color(int pct)
- {
- if (pct <= 10)
- return 0xFF5050u;
- return 0x55FF55u;
- }
- /* Battery: the Adwaita outline with our own level fill over the lower
- * interior. The symbol's body is striped, so the interior is cleared to a
- * dim shade of the state colour first; the level then reads as a bright
- * block inside the outline. A critical charge turns the outline red too.
- * Geometry of battery-symbolic at 16 px: body x2..13, cavity x4..11 and
- * y4..13. */
- static unsigned int batt_dim(unsigned int c)
- {
- return (c >> 2) & 0x3F3F3F;
- }
- static void bar_icon_battery(int x, int y, int pct, unsigned int c)
- {
- unsigned int fill = (unsigned int)((10 * pct + 99) / 100);
- int ix = x + 4;
- if (fill > 10)
- fill = 10;
- if (!usurf_icon(&screen, ix, y, UWM_ICON_BATTERY, c))
- return;
- usurf_fill(&screen, ix + 4, y + 4, 8, 10, batt_dim(c));
- if (fill)
- usurf_fill(&screen, ix + 4, y + 14 - (int)fill, 8, fill, c);
- }
- /* AC adapter: the Adwaita plug symbol. */
- static void bar_icon_ac(int x, int y, unsigned int c)
- {
- (void)usurf_icon(&screen, x + 4, y, UWM_ICON_AC, c);
- }
- static void draw_bar(void)
- {
- char dt[48];
- unsigned int n, i, x, y;
- usurf_fill(&screen, 0, 0, fbw, WM_BAR_H, COL_BG);
- bar_start_button();
- if (bar_msg_until &&
- (int)(u_ticks_ms() - bar_msg_until) < 0)
- usurf_text(&screen, (int)(WM_BAR_LEFT_END + 8),
- 5, bar_msg, 0xFFD250u, 0, 0, fbw, WM_BAR_H);
- /* Rainbow separator (same y as the kernel's old bar). */
- for (x = 0; x < fbw; x++) {
- unsigned int c = rainbow_rgb((x * (6 * 256)) / (fbw ? fbw : 1));
- for (y = WM_BAR_H - 5; y < WM_BAR_H - 3; y++)
- usurf_fill(&screen, (int)x, (int)y, 1, 1, c);
- }
- if (bar_kexec_busy) {
- static const char msg[] = "loading new kernel over serial";
- unsigned int dots = bar_kexec_total ?
- (bar_kexec_progress * 12 / bar_kexec_total) : 0;
- bar_status_x = -1;
- bar_status_w = 0;
- n = 0;
- while (msg[n])
- dt[n] = msg[n], n++;
- for (i = 0; i < 12; i++)
- dt[n + i] = (i < dots) ? '.' : ' ';
- n += 12;
- dt[n] = '\0';
- usurf_text(&screen,
- (int)fbw - (int)usurf_text_width(&screen, dt) - 4,
- 5, dt, 0xFFC864u, 0, 0, fbw, WM_BAR_H);
- } else {
- int right = (int)fbw - 4;
- int min_x = (int)WM_BAR_LEFT_END + 4;
- int pwr_px = -1, net_nx = -1, clock_tx = -1, temp_tx = -1;
- int temp_tw;
- int left;
- char cdt[12], tb[12];
- const char *clock_txt = dt;
- /* Lay the cluster out right to left: AC/battery, network, the
- * date/time and the CPU temperature. */
- if (bar_batt_pct >= 0 || bar_ac_online > 0) {
- pwr_px = right - BAR_PWR_W;
- if (pwr_px >= min_x)
- right = pwr_px - 8;
- else
- pwr_px = -1;
- }
- net_nx = right - BAR_NET_W;
- if (net_nx >= min_x)
- right = net_nx - 8;
- else
- net_nx = -1;
- if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
- int tw = (int)usurf_text_width(&screen, dt);
- int tx = right - tw;
- unsigned int k;
- if (tx < min_x) {
- /* Compact form "MM-DD HH:MM" from
- * "YYYY-MM-DD HH:MM:SS" when the status
- * cluster is too wide for the full date. */
- for (k = 0; k < 11 && dt[5 + k]; k++)
- cdt[k] = dt[5 + k];
- cdt[k] = '\0';
- clock_txt = cdt;
- tw = (int)usurf_text_width(&screen, cdt);
- tx = right - tw;
- }
- if (tx >= min_x) {
- clock_tx = tx;
- right = tx - 8;
- }
- }
- if (bar_temp_c >= 0) {
- (void)temp_text(tb, bar_temp_c);
- temp_tw = (int)usurf_text_width(&screen, tb);
- temp_tx = right - temp_tw;
- if (temp_tx < min_x)
- temp_tx = -1;
- }
- /* The status labels form one click target: record the whole
- * cluster and paint one rounded hover pill behind it. */
- left = pwr_px;
- if (net_nx >= 0)
- left = net_nx;
- if (clock_tx >= 0)
- left = clock_tx;
- if (temp_tx >= 0)
- left = temp_tx;
- if (left >= 0) {
- bar_status_x = left - 4;
- if (bar_status_x < 0)
- bar_status_x = 0;
- bar_status_w = (unsigned int)((int)fbw - 2 -
- bar_status_x);
- bar_button_bg(bar_status_x, (int)bar_status_w,
- control_pid > 0, bar_status_hot);
- } else {
- bar_status_x = -1;
- bar_status_w = 0;
- }
- if (pwr_px >= 0) {
- if (bar_ac_online > 0)
- bar_icon_ac(pwr_px, BAR_PWR_Y, 0x55FF55u);
- else
- bar_icon_battery(pwr_px, BAR_PWR_Y,
- bar_batt_pct,
- batt_color(bar_batt_pct));
- }
- if (net_nx >= 0)
- bar_draw_net_icon(net_nx, BAR_NET_Y);
- if (clock_tx >= 0)
- usurf_text(&screen, clock_tx, 5, clock_txt, COL_BANNER,
- 0, 0, fbw, WM_BAR_H);
- if (temp_tx >= 0)
- usurf_text(&screen, temp_tx, 5, tb,
- temp_color(bar_temp_c), 0, 0, fbw,
- WM_BAR_H);
- }
- }
- /* A popover is a flat, chromeless panel: the canvas fills the window. */
- static void draw_popover_clip(unsigned int idx, int cx, int cy,
- unsigned int cw, unsigned int ch)
- {
- struct uwin *u = &wins[idx];
- struct wm_window_info *w = &u->info;
- unsigned int cwid = w->content_w, chei = w->content_h;
- if (u->mapped && u->map.kind == WM_KIND_GFX) {
- struct uwm_canvas c;
- c.px = (const unsigned short *)u->map.va;
- c.w = u->map.w;
- c.h = u->map.h;
- if (c.w == cwid && c.h == chei) {
- usurf_canvas(&screen, &c, w->x, w->y,
- cx, cy, cw, ch);
- return;
- }
- }
- fill_clip(w->x, w->y, cwid, chei, 0x181C28u, cx, cy, cw, ch);
- }
- static void draw_window_clip(unsigned int idx, int cx, int cy,
- unsigned int cw, unsigned int ch)
- {
- struct uwin *u = &wins[idx];
- struct wm_window_info *w = &u->info;
- int x = w->x, y = w->y;
- unsigned int pw = w->w;
- int bx = x + (int)pw - WM_BORDER_W - WM_CLOSE_W;
- int cony = y + WM_BORDER_W + WM_TITLE_H;
- int content_x = x + WM_BORDER_W;
- unsigned int content_w = w->content_w;
- unsigned int content_h = w->content_h;
- if (w->popover) {
- draw_popover_clip(idx, cx, cy, cw, ch);
- return;
- }
- /* Title bar and close button; windows have no visible frame. */
- fill_clip(x, y, pw, WM_TITLE_H,
- w->focused ? COL_TITLE_ON : COL_TITLE_OFF, cx, cy, cw, ch);
- usurf_text(&screen, x + 3, y + 2, w->title, COL_WHITE,
- cx, cy, cw, ch);
- fill_clip(bx, y, WM_CLOSE_W, WM_TITLE_H, COL_CLOSE, cx, cy, cw, ch);
- usurf_text(&screen, bx + 4, y + 2, "x", COL_WHITE,
- cx, cy, cw, ch);
- /* Content: every window presents an RGB565 canvas (the terminal
- * renders its cells into one; gfx commands own theirs). A mapped
- * canvas covers the whole content area, so it does not need the black
- * underlay. */
- if (u->mapped && u->map.kind == WM_KIND_GFX) {
- struct uwm_canvas c;
- c.px = (const unsigned short *)u->map.va;
- c.w = u->map.w;
- c.h = u->map.h;
- if (c.w != content_w || c.h != content_h)
- fill_clip(content_x, cony, content_w, content_h,
- 0x000000u, cx, cy, cw, ch);
- usurf_canvas(&screen, &c, content_x, cony,
- cx, cy, cw, ch);
- } else {
- fill_clip(content_x, cony, content_w, content_h,
- 0x000000u, cx, cy, cw, ch);
- }
- }
- /* Front-to-back window order: popovers float above everything, then the
- * focused window, then the others in descending slot order. Painting and
- * hit-testing share it. */
- static unsigned int build_order(int *order)
- {
- unsigned int i, n = 0;
- for (i = 0; i < WM_MAX_WINDOWS; i++)
- if (wins[i].info.used && wins[i].info.popover)
- order[n++] = (int)i;
- for (i = 0; i < WM_MAX_WINDOWS; i++)
- if (wins[i].info.used && wins[i].info.focused &&
- !wins[i].info.popover)
- order[n++] = (int)i;
- for (i = WM_MAX_WINDOWS; i-- > 0;)
- if (wins[i].info.used && !wins[i].info.focused &&
- !wins[i].info.popover)
- order[n++] = (int)i;
- return n;
- }
- struct orect {
- int x, y;
- unsigned int w, h;
- };
- #define OCC_MAX 64
- /* r minus cut, split into up to four disjoint rectangles. */
- static unsigned int rect_sub(struct orect *out, const struct orect *r,
- const struct orect *cut)
- {
- int rx0 = r->x, ry0 = r->y;
- int rx1 = r->x + (int)r->w, ry1 = r->y + (int)r->h;
- int cx0 = cut->x, cy0 = cut->y;
- int cx1 = cut->x + (int)cut->w, cy1 = cut->y + (int)cut->h;
- unsigned int n = 0;
- if (cx0 < rx0) cx0 = rx0;
- if (cy0 < ry0) cy0 = ry0;
- if (cx1 > rx1) cx1 = rx1;
- if (cy1 > ry1) cy1 = ry1;
- if (cx1 <= cx0 || cy1 <= cy0) {
- out[0] = *r;
- return 1;
- }
- if (cy0 > ry0) {
- out[n].x = rx0;
- out[n].y = ry0;
- out[n].w = (unsigned int)(rx1 - rx0);
- out[n].h = (unsigned int)(cy0 - ry0);
- n++;
- }
- if (cy1 < ry1) {
- out[n].x = rx0;
- out[n].y = cy1;
- out[n].w = (unsigned int)(rx1 - rx0);
- out[n].h = (unsigned int)(ry1 - cy1);
- n++;
- }
- if (cx0 > rx0) {
- out[n].x = rx0;
- out[n].y = cy0;
- out[n].w = (unsigned int)(cx0 - rx0);
- out[n].h = (unsigned int)(cy1 - cy0);
- n++;
- }
- if (cx1 < rx1) {
- out[n].x = cx1;
- out[n].y = cy0;
- out[n].w = (unsigned int)(rx1 - cx1);
- out[n].h = (unsigned int)(cy1 - cy0);
- n++;
- }
- return n;
- }
- /* Back-to-front repaint, used only if the visible-region list overflows. */
- static void draw_rect_naive(int x, int y, unsigned int w, unsigned int h)
- {
- unsigned int i;
- usurf_fill(&screen, x, y, w, h, COL_BG);
- for (i = 0; i < WM_MAX_WINDOWS; i++)
- if (wins[i].info.used && !wins[i].info.focused &&
- !wins[i].info.popover &&
- x < wins[i].info.x + (int)wins[i].info.w &&
- x + (int)w > wins[i].info.x &&
- y < wins[i].info.y + (int)wins[i].info.h &&
- y + (int)h > wins[i].info.y)
- draw_window_clip(i, x, y, w, h);
- for (i = 0; i < WM_MAX_WINDOWS; i++)
- if (wins[i].info.used && wins[i].info.focused &&
- !wins[i].info.popover &&
- x < wins[i].info.x + (int)wins[i].info.w &&
- x + (int)w > wins[i].info.x &&
- y < wins[i].info.y + (int)wins[i].info.h &&
- y + (int)h > wins[i].info.y)
- draw_window_clip(i, x, y, w, h);
- for (i = 0; i < WM_MAX_WINDOWS; i++)
- if (wins[i].info.used && wins[i].info.popover &&
- x < wins[i].info.x + (int)wins[i].info.w &&
- x + (int)w > wins[i].info.x &&
- y < wins[i].info.y + (int)wins[i].info.h &&
- y + (int)h > wins[i].info.y)
- draw_window_clip(i, x, y, w, h);
- }
- static void draw_rect(int x, int y, unsigned int w, unsigned int h)
- {
- static struct orect cur[OCC_MAX], nxt[OCC_MAX];
- struct orect dmg;
- int order[WM_MAX_WINDOWS];
- unsigned int n, i, j, ncur, nnext;
- int overflow = 0;
- int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
- if (w == 0 || h == 0)
- return;
- if (x0 < 0) x0 = 0;
- if (y0 < 0) y0 = 0;
- if (x1 > (int)fbw) x1 = (int)fbw;
- if (y1 > (int)fbh) y1 = (int)fbh;
- if (x1 <= x0 || y1 <= y0)
- return;
- dmg.x = x0;
- dmg.y = y0;
- dmg.w = (unsigned int)(x1 - x0);
- dmg.h = (unsigned int)(y1 - y0);
- usurf_fill(&screen, dmg.x, dmg.y, dmg.w, dmg.h, COL_BG);
- n = build_order(order);
- for (i = 0; i < n; i++) {
- struct uwin *u = &wins[order[i]];
- int wx0 = u->info.x, wy0 = u->info.y;
- int wx1 = wx0 + (int)u->info.w, wy1 = wy0 + (int)u->info.h;
- if (wx0 < dmg.x) wx0 = dmg.x;
- if (wy0 < dmg.y) wy0 = dmg.y;
- if (wx1 > x1) wx1 = x1;
- if (wy1 > y1) wy1 = y1;
- if (wx1 <= wx0 || wy1 <= wy0)
- continue;
- cur[0].x = wx0;
- cur[0].y = wy0;
- cur[0].w = (unsigned int)(wx1 - wx0);
- cur[0].h = (unsigned int)(wy1 - wy0);
- ncur = 1;
- for (j = 0; j < i && ncur; j++) {
- struct uwin *f = &wins[order[j]];
- struct orect fr;
- unsigned int k;
- fr.x = f->info.x;
- fr.y = f->info.y;
- fr.w = f->info.w;
- fr.h = f->info.h;
- nnext = 0;
- for (k = 0; k < ncur; k++) {
- if (nnext + 4 > OCC_MAX) {
- overflow = 1;
- break;
- }
- nnext += rect_sub(&nxt[nnext], &cur[k], &fr);
- }
- if (overflow)
- break;
- for (k = 0; k < nnext; k++)
- cur[k] = nxt[k];
- ncur = nnext;
- }
- if (overflow)
- break;
- for (j = 0; j < ncur; j++)
- draw_window_clip((unsigned int)order[i], cur[j].x,
- cur[j].y, cur[j].w, cur[j].h);
- }
- if (overflow)
- draw_rect_naive(dmg.x, dmg.y, dmg.w, dmg.h);
- }
- /* Invisible resize border: edge bits for a point in the band around the
- * window's outer edge (WM_RESIZE_IN px inside and WM_RESIZE_OUT px outside).
- * Popovers have no chrome and never resize. */
- #define WM_EDGE_LEFT 1
- #define WM_EDGE_RIGHT 2
- #define WM_EDGE_TOP 4
- #define WM_EDGE_BOTTOM 8
- static int resize_edges_at(const struct uwin *u, int mx, int my)
- {
- const struct wm_window_info *w = &u->info;
- int l = w->x, t = w->y;
- int r = l + (int)w->w, b = t + (int)w->h;
- int near_x, near_y, e = 0;
- if (w->popover)
- return 0;
- near_x = mx >= l - WM_RESIZE_OUT && mx < r + WM_RESIZE_OUT;
- near_y = my >= t - WM_RESIZE_OUT && my < b + WM_RESIZE_OUT;
- if (near_y && mx >= l - WM_RESIZE_OUT && mx < l + WM_RESIZE_IN)
- e |= WM_EDGE_LEFT;
- if (near_y && mx >= r - WM_RESIZE_IN && mx < r + WM_RESIZE_OUT)
- e |= WM_EDGE_RIGHT;
- if (near_x && my >= t - WM_RESIZE_OUT && my < t + WM_RESIZE_IN)
- e |= WM_EDGE_TOP;
- if (near_x && my >= b - WM_RESIZE_IN && my < b + WM_RESIZE_OUT)
- e |= WM_EDGE_BOTTOM;
- return e;
- }
- /* Topmost window at (mx,my): its resize band when the point is inside one,
- * otherwise the window rectangle. *edges is 0 for a plain rectangle hit.
- * Bands are not considered inside the top bar, which the WM owns. */
- static int resize_hit(int mx, int my, int *edges)
- {
- int order[WM_MAX_WINDOWS];
- unsigned int n = build_order(order), i;
- *edges = 0;
- for (i = 0; i < n; i++) {
- struct uwin *u = &wins[order[i]];
- int e = 0;
- if (!u->info.used)
- continue;
- if (my >= WM_BAR_H)
- e = resize_edges_at(u, mx, my);
- if (e) {
- *edges = e;
- return order[i];
- }
- if (mx >= u->info.x && my >= u->info.y &&
- mx < u->info.x + (int)u->info.w &&
- my < u->info.y + (int)u->info.h)
- return order[i];
- }
- return -1;
- }
- static int resize_cursor(int edges)
- {
- switch (edges) {
- case WM_EDGE_LEFT:
- case WM_EDGE_RIGHT:
- return CURSOR_EW_RESIZE;
- case WM_EDGE_TOP:
- case WM_EDGE_BOTTOM:
- return CURSOR_NS_RESIZE;
- case WM_EDGE_LEFT | WM_EDGE_TOP:
- case WM_EDGE_RIGHT | WM_EDGE_BOTTOM:
- return CURSOR_NWSE_RESIZE;
- case WM_EDGE_RIGHT | WM_EDGE_TOP:
- case WM_EDGE_LEFT | WM_EDGE_BOTTOM:
- return CURSOR_NESW_RESIZE;
- default:
- return CURSOR_DEFAULT;
- }
- }
- /* Close button hit area (the top-right WM_CLOSE_W columns of the title bar). */
- static int close_hit(const struct wm_window_info *w, int mx, int my)
- {
- if (w->popover)
- return 0;
- return mx >= w->x + (int)w->w - WM_CLOSE_W &&
- mx < w->x + (int)w->w &&
- my >= w->y && my < w->y + WM_TITLE_H;
- }
- static void clamp_geom(struct uwin *u, int *x, int *y)
- {
- /* Match wm_clamp_window(): allow the window to hang off the left,
- * right and bottom edges but keep the title bar reachable and below
- * the system bar. */
- int minx = WM_DRAG_VISIBLE - (int)u->info.w;
- int maxx = (int)fbw - WM_DRAG_VISIBLE;
- int miny = WM_BAR_H;
- int maxy = (int)fbh - WM_BORDER_W - WM_TITLE_H;
- if (minx > maxx)
- minx = maxx = 0;
- if (miny > maxy)
- miny = maxy = WM_BAR_H;
- if (*x < minx)
- *x = minx;
- else if (*x > maxx)
- *x = maxx;
- if (*y < miny)
- *y = miny;
- else if (*y > maxy)
- *y = maxy;
- }
- static void add_rect(struct wm_present_info *p, int x, int y,
- unsigned int w, unsigned int h)
- {
- if (w == 0 || h == 0)
- return;
- if (p->count < 4) {
- p->rect[p->count].x = x;
- p->rect[p->count].y = y;
- p->rect[p->count].w = w;
- p->rect[p->count].h = h;
- p->count++;
- } else {
- /* Four is enough for normal updates; fold the rest into the
- * last rect rather than dropping pixels. */
- int x0 = p->rect[3].x, y0 = p->rect[3].y;
- int x1 = x0 + (int)p->rect[3].w, y1 = y0 + (int)p->rect[3].h;
- int nx1 = x + (int)w, ny1 = y + (int)h;
- if (x < x0) x0 = x;
- if (y < y0) y0 = y;
- if (nx1 > x1) x1 = nx1;
- if (ny1 > y1) y1 = ny1;
- p->rect[3].x = x0;
- p->rect[3].y = y0;
- p->rect[3].w = (unsigned int)(x1 - x0);
- p->rect[3].h = (unsigned int)(y1 - y0);
- }
- }
- /* Center the brightness bubble in the work area, clamped on small screens. */
- static void osd_geom(void)
- {
- int x, y;
- osd_w = OSD_W;
- osd_h = OSD_H;
- x = ((int)fbw - (int)osd_w) / 2;
- y = WM_BAR_H + ((int)fbh - WM_BAR_H - (int)osd_h) * 3 / 5;
- if (x < 0)
- x = 0;
- if (x + (int)osd_w > (int)fbw)
- x = (int)fbw - (int)osd_w;
- if (y < WM_BAR_H)
- y = WM_BAR_H;
- if (y + (int)osd_h > (int)fbh)
- y = (int)fbh - (int)osd_h;
- osd_x = x;
- osd_y = y;
- }
- /* A small sun: a filled circle plus four rays, all from 1-pixel fills. */
- static void osd_sun(int cx, int cy)
- {
- static const unsigned char half[11] = { 2, 4, 5, 6, 6, 6, 6, 6, 5, 4, 2 };
- int i;
- for (i = -5; i <= 5; i++)
- usurf_fill(&screen, cx - half[i + 5], cy + i,
- half[i + 5] * 2 + 1, 1, 0xFFD250u);
- usurf_fill(&screen, cx - 1, cy - 9, 2, 3, 0xFFD250u);
- usurf_fill(&screen, cx - 1, cy + 7, 2, 3, 0xFFD250u);
- usurf_fill(&screen, cx - 9, cy - 1, 3, 2, 0xFFD250u);
- usurf_fill(&screen, cx + 7, cy - 1, 3, 2, 0xFFD250u);
- }
- static void osd_draw(void)
- {
- char pct[8];
- unsigned int n = 0;
- unsigned int bar_x, bar_y, bar_w;
- int x, y, tw;
- osd_geom();
- x = osd_x;
- y = osd_y;
- usurf_fill(&screen, x, y, osd_w, osd_h, 0x26263Au);
- usurf_fill(&screen, x, y, osd_w, 1, 0x70709Au);
- usurf_fill(&screen, x, y + (int)osd_h - 1, osd_w, 1, 0x70709Au);
- usurf_fill(&screen, x, y + 1, 1, osd_h - 2, 0x70709Au);
- usurf_fill(&screen, x + (int)osd_w - 1, y + 1, 1, osd_h - 2,
- 0x70709Au);
- osd_sun(x + 22, y + (int)osd_h / 2);
- if (osd_level >= 100) {
- pct[n++] = '1';
- pct[n++] = '0';
- pct[n++] = '0';
- } else {
- if (osd_level >= 10)
- pct[n++] = (char)('0' + osd_level / 10);
- pct[n++] = (char)('0' + osd_level % 10);
- }
- pct[n++] = '%';
- pct[n] = '\0';
- usurf_text(&screen, x + 42, y + 9, "Brightness", 0xE8E8F6u,
- x, y, osd_w, osd_h);
- tw = (int)usurf_text_width(&screen, pct);
- usurf_text(&screen, x + (int)osd_w - 14 - tw, y + 9, pct,
- 0xFFD250u, x, y, osd_w, osd_h);
- bar_x = (unsigned int)(x + 42);
- bar_y = (unsigned int)(y + 35);
- bar_w = osd_w - 42 - 14;
- usurf_fill(&screen, (int)bar_x, (int)bar_y, bar_w, 10, 0x44445Au);
- usurf_fill(&screen, (int)bar_x, (int)bar_y,
- (bar_w * (unsigned int)osd_level + 50) / 100, 10,
- 0xFFD250u);
- }
- /* Put the brightness OSD and the start menu back on top after a render pass
- * that may have overwritten them. */
- static void wm_overlays(struct wm_present_info *p)
- {
- if (osd_show) {
- osd_draw();
- add_rect(p, osd_x, osd_y, osd_w, osd_h);
- }
- if (sm_open) {
- sm_draw();
- sm_add_present(p);
- }
- }
- /* A focused window is topmost, so the shadow contains exactly its pixels in
- * the old rectangle. Move that block and repaint only the old part the new
- * rectangle does not cover. When either rectangle is partly off-screen the
- * shadow has no valid pixels for the hidden part, so repaint the new rect
- * instead of copying desktop background into the window. */
- static void move_window_pixels(struct uwin *u, int ox, int oy, int nx, int ny)
- {
- struct wm_present_info present;
- struct orect oldr, newr, pieces[4];
- unsigned int np, k;
- int w = (int)u->info.w, h = (int)u->info.h;
- int old_on = ox >= 0 && oy >= 0 &&
- ox + w <= (int)fbw && oy + h <= (int)fbh;
- int new_on = nx >= 0 && ny >= 0 &&
- nx + w <= (int)fbw && ny + h <= (int)fbh;
- if (u_wm_begin() != 0)
- return;
- remap_window_contents();
- if (old_on && new_on)
- usurf_copy(&screen, nx, ny, ox, oy, u->info.w, u->info.h);
- else
- draw_rect(nx, ny, u->info.w, u->info.h);
- oldr.x = ox;
- oldr.y = oy;
- oldr.w = u->info.w;
- oldr.h = u->info.h;
- newr.x = nx;
- newr.y = ny;
- newr.w = u->info.w;
- newr.h = u->info.h;
- np = rect_sub(pieces, &oldr, &newr);
- for (k = 0; k < np; k++)
- draw_rect(pieces[k].x, pieces[k].y, pieces[k].w, pieces[k].h);
- present.count = 0;
- present.immediate = 1;
- add_rect(&present, nx, ny, u->info.w, u->info.h);
- for (k = 0; k < np; k++)
- add_rect(&present, pieces[k].x, pieces[k].y,
- pieces[k].w, pieces[k].h);
- wm_overlays(&present);
- u_wm_present(&present);
- }
- static int iabs(int v)
- {
- return v < 0 ? -v : v;
- }
- /* Repaint the old and new rectangles of a geometry change. */
- static void redraw_geom(struct uwin *u, int ox, int oy,
- unsigned int ow, unsigned int oh)
- {
- struct wm_present_info present;
- unsigned int i;
- present.count = 0;
- present.immediate = 1;
- add_rect(&present, ox, oy, ow, oh);
- add_rect(&present, u->info.x, u->info.y, u->info.w, u->info.h);
- if (u_wm_begin() == 0) {
- remap_window_contents();
- for (i = 0; i < present.count; i++)
- draw_rect(present.rect[i].x, present.rect[i].y,
- present.rect[i].w, present.rect[i].h);
- wm_overlays(&present);
- u_wm_present(&present);
- }
- }
- /* Resize (when needed), move and remap a window in one step. */
- static int set_window_geometry(struct uwin *u, unsigned int idx,
- int nx, int ny, unsigned int nw, unsigned int nh)
- {
- struct wm_window_info ni;
- int ox = u->info.x, oy = u->info.y;
- unsigned int ow = u->info.w, oh = u->info.h;
- if ((int)nw != (int)ow || (int)nh != (int)oh) {
- if (u_wm_resize(idx, nw - 2 * WM_BORDER_W,
- nh - WM_TITLE_H - 2 * WM_BORDER_W) != 0)
- return -1;
- }
- if (nx != ox || ny != oy)
- u_wm_set_geom(idx, nx, ny);
- u->mapped = 0;
- if (u_wm_info(idx, &ni) == 1)
- u->info = ni;
- if (u_wm_map(idx, &u->map) == 0)
- u->mapped = 1;
- redraw_geom(u, ox, oy, ow, oh);
- return 0;
- }
- static int restore_window(struct uwin *u, unsigned int idx)
- {
- if (u->state == WIN_NORMAL)
- return 0;
- if (set_window_geometry(u, idx, u->restore_x, u->restore_y,
- u->restore_w, u->restore_h) != 0)
- return -1;
- u->state = WIN_NORMAL;
- return 0;
- }
- /* Maximize or snap a window to a half of the work area below the system
- * bar. The restore geometry is recorded only when the window leaves the
- * normal state, so maximize -> snap -> restore returns to the original. */
- static int place_window(struct uwin *u, unsigned int idx, int state)
- {
- unsigned int half = fbw / 2;
- unsigned int h = fbh - WM_BAR_H;
- int x = 0;
- unsigned int w = fbw;
- if (state == u->state)
- return 0;
- if (state == WIN_SNAP_LEFT) {
- w = half;
- } else if (state == WIN_SNAP_RIGHT) {
- x = (int)half;
- w = fbw - half;
- }
- if (u->state == WIN_NORMAL) {
- u->restore_x = u->info.x;
- u->restore_y = u->info.y;
- u->restore_w = u->info.w;
- u->restore_h = u->info.h;
- }
- if (set_window_geometry(u, idx, x, WM_BAR_H, w, h) != 0)
- return -1;
- u->state = state;
- return 0;
- }
- /* Double-click title bar: fill the work area below the system bar, or go back
- * to the geometry the window had before it was maximized. A snapped window
- * maximizes (keeping its pre-snap restore geometry). */
- static int toggle_maximize(struct uwin *u, unsigned int idx)
- {
- if (u->state == WIN_MAXIMIZED)
- return restore_window(u, idx);
- return place_window(u, idx, WIN_MAXIMIZED);
- }
- /* One axis of edge resistance. lo/len is the window's span on that axis
- * before the move, *pos the proposed start. dir < 0: the window's start side
- * (left/top) resists edge e while moving towards lower coordinates; dir > 0:
- * its end side (right/bottom) while moving higher. Within WM_RESIST pixels
- * of overshoot the window stays on the edge; *best keeps the nearest edge. */
- static void resist_edge(int lo, int len, int *pos, int e, int dir, int *best)
- {
- int over;
- if (dir < 0 && *pos < lo && lo >= e && *pos < e) {
- over = e - *pos;
- if (over <= WM_RESIST && over < *best) {
- *best = over;
- *pos = e;
- }
- } else if (dir > 0 && *pos > lo && lo + len <= e && *pos + len > e) {
- over = *pos + len - e;
- if (over <= WM_RESIST && over < *best) {
- *best = over;
- *pos = e - len;
- }
- }
- }
- /* Span overlap test: edges of another window only resist when the two
- * windows share some extent on the other axis (they would actually touch). */
- static int spans_overlap(int a, int alen, int b, int blen)
- {
- return a < b + blen && b < a + alen;
- }
- /* Apply edge resistance to a drag from (u->info.x, u->info.y) to (*nx, *ny):
- * screen edges and the facing edges of every other window. The proposed
- * position is still pure pointer arithmetic, so once the overshoot passes
- * WM_RESIST the window jumps to follow the pointer again. */
- static void resist_geom(struct uwin *u, int idx, int *nx, int *ny)
- {
- int ox = u->info.x, oy = u->info.y;
- int w = (int)u->info.w, h = (int)u->info.h;
- int px = *nx, py = *ny;
- int bestx = WM_RESIST + 1, besty = WM_RESIST + 1;
- int i;
- resist_edge(ox, w, &px, 0, -1, &bestx);
- resist_edge(ox, w, &px, (int)fbw, 1, &bestx);
- resist_edge(oy, h, &py, (int)fbh, 1, &besty);
- for (i = 0; i < WM_MAX_WINDOWS; i++) {
- struct wm_window_info *o = &wins[i].info;
- int t;
- if (i == idx || !o->used)
- continue;
- if (spans_overlap(oy, h, o->y, (int)o->h)) {
- /* our left side against its right, our right
- * side against its left */
- t = *nx;
- resist_edge(ox, w, &t, o->x + (int)o->w, -1, &bestx);
- resist_edge(ox, w, &t, o->x, 1, &bestx);
- if (t != *nx)
- px = t;
- }
- if (spans_overlap(ox, w, o->x, (int)o->w)) {
- t = *ny;
- resist_edge(oy, h, &t, o->y + (int)o->h, -1, &besty);
- resist_edge(oy, h, &t, o->y, 1, &besty);
- if (t != *ny)
- py = t;
- }
- }
- *nx = px;
- *ny = py;
- }
- /* The top-bar date/time text is also a button: it opens the Date & Time
- * editor. The rectangle is recorded by draw_bar(). */
- /* The whole right status cluster is one button: the clock, temperature,
- * network and power items plus the gaps between them. Only the rectangle
- * draw_bar() recorded is clickable, so the empty bar between the start
- * button and the cluster does nothing. */
- static int bar_status_hit(unsigned int mx, unsigned int my)
- {
- return bar_status_w && bar_status_x >= 0 &&
- (int)my >= BAR_BTN_Y && (int)my < BAR_BTN_Y + BAR_BTN_H &&
- (int)mx >= bar_status_x &&
- (int)mx < bar_status_x + (int)bar_status_w;
- }
- static void sm_layout_get(struct sm_layout *l)
- {
- int avail = (int)fbh - WM_BAR_H - 8;
- int row = SM_ROW_H;
- int total = SM_TOP + SM_SECT_H + SM_NAPP * row + SM_GAP + row +
- SM_BOTTOM;
- /* Shrink the rows to the work area, so a 540-600 px panel still fits
- * the whole list. The low bound is the smallest legible row. */
- while (row > SM_MIN_ROW && total > avail) {
- row--;
- total = SM_TOP + SM_SECT_H + SM_NAPP * row + SM_GAP + row +
- SM_BOTTOM;
- }
- l->x = 4;
- l->y = WM_BAR_H + 2;
- l->w = SM_W;
- l->h = total;
- l->row_h = row;
- l->sect_y = l->y + SM_TOP;
- l->row_x = l->x + SM_PAD;
- l->row_w = l->w - 2 * SM_PAD;
- l->list_y = l->sect_y + SM_SECT_H;
- l->power_y = l->list_y + SM_NAPP * row + SM_GAP;
- l->sub_w = SM_SUB_W;
- l->sub_h = 2 * row + 8;
- l->sub_x = l->x + l->w + 4;
- if (l->sub_x + l->sub_w > (int)fbw - 2) {
- if (l->x - l->sub_w - 4 >= 0)
- l->sub_x = l->x - l->sub_w - 4;
- else
- l->sub_x = (int)fbw - 2 - l->sub_w;
- }
- /* The power row is the last menu row: open the submenu upwards and
- * align its bottom with that row. */
- l->sub_y = l->power_y + row - l->sub_h;
- if (l->sub_y < WM_BAR_H + 2)
- l->sub_y = WM_BAR_H + 2;
- if (l->sub_y + l->sub_h > (int)fbh - 2)
- l->sub_y = (int)fbh - 2 - l->sub_h;
- if (l->sub_y < 0)
- l->sub_y = 0;
- }
- static void sm_tile(int x, int y, int size, unsigned int rgb, char letter)
- {
- char s[2];
- int tw, tx, ty;
- if (size < 12)
- size = 12;
- s[0] = letter;
- s[1] = '\0';
- sm_fill_round(x, y, size, size, 4, rgb);
- tw = (int)usurf_text_width(&screen, s);
- tx = x + (size - tw) / 2;
- ty = y + (size - 16) / 2;
- usurf_text(&screen, tx, ty, s, COL_WHITE,
- x, y, (unsigned int)size, (unsigned int)size);
- }
- static void sm_draw_app(int i, const struct sm_layout *l)
- {
- const struct sm_app *a = &sm_apps[i];
- int ry = l->list_y + i * l->row_h;
- int ty = ry + (l->row_h - 16) / 2;
- int tile = l->row_h - 8;
- if (tile > SM_TILE)
- tile = SM_TILE;
- if (sm_hot == i)
- sm_fill_round(l->row_x, ry + 1, l->row_w,
- l->row_h - 2, SM_RADIUS, SM_PANEL_ALT);
- sm_tile(l->row_x + 10, ry + (l->row_h - tile) / 2, tile,
- a->tile, a->letter);
- usurf_text(&screen, l->row_x + 36, ty, a->label, SM_TEXT,
- l->x, l->y, (unsigned int)l->w, (unsigned int)l->h);
- }
- static void sm_draw(void)
- {
- struct sm_layout l;
- int i, ty;
- if (!sm_open)
- return;
- sm_layout_get(&l);
- /* Flat theme.bg panel with no rim, exactly like the netmenu popover.
- * The Applications caption is the first item: there is no separate
- * banner card. */
- usurf_fill(&screen, l.x, l.y, (unsigned int)l.w, (unsigned int)l.h,
- SM_BG);
- usurf_text(&screen, l.x + SM_PAD, l.sect_y + 3, "Applications",
- SM_TEXT_DIM, l.x, l.y, (unsigned int)l.w,
- (unsigned int)l.h);
- for (i = 0; i < SM_NAPP; i++)
- sm_draw_app(i, &l);
- /* The Power row is a quick-settings tile rather than a plain list
- * row, so the submenu entry reads as the panel's last action. */
- sm_fill_round(l.row_x, l.power_y, l.row_w, l.row_h, SM_RADIUS,
- sm_power_open ? SM_SEL :
- (sm_power_hot == 1 ? SM_BTN_HOT : SM_PANEL_ALT));
- ty = l.power_y + (l.row_h - 16) / 2;
- usurf_text(&screen, l.row_x + 12, ty, "Power",
- sm_power_open || sm_power_hot == 1 ? COL_WHITE : SM_TEXT,
- l.x, l.y, (unsigned int)l.w, (unsigned int)l.h);
- {
- const char *arrow = sm_power_open ? "v" : ">";
- int aw = (int)usurf_text_width(&screen, arrow);
- usurf_text(&screen, l.row_x + l.row_w - 12 - aw, ty, arrow,
- sm_power_open || sm_power_hot == 1 ? COL_WHITE :
- SM_TEXT_DIM,
- l.x, l.y, (unsigned int)l.w, (unsigned int)l.h);
- }
- if (!sm_power_open)
- return;
- /* Submenu: a rounded panel card with two rounded selection rows. */
- sm_fill_round(l.sub_x, l.sub_y, l.sub_w, l.sub_h, SM_RADIUS,
- SM_PANEL);
- for (i = 0; i < 2; i++) {
- int sy = l.sub_y + 4 + i * l.row_h;
- int sty = sy + (l.row_h - 16) / 2;
- if (sm_power_hot == i + 2)
- sm_fill_round(l.sub_x + 4, sy + 1, l.sub_w - 8,
- l.row_h - 2, 4, SM_SEL);
- usurf_text(&screen, l.sub_x + 12, sty,
- i == 0 ? "Reboot" : "Power off",
- i == 1 ? (sm_power_hot == 3 ? COL_WHITE : SM_ERROR)
- : (sm_power_hot == 2 ? COL_WHITE : SM_TEXT),
- l.sub_x, l.sub_y, (unsigned int)l.sub_w,
- (unsigned int)l.sub_h);
- }
- }
- static int sm_hit(unsigned int mx, unsigned int my)
- {
- struct sm_layout l;
- int x = (int)mx, y = (int)my;
- if (bar_start_hit(mx, my))
- return SM_HIT_START;
- if (!sm_open)
- return SM_HIT_NONE;
- sm_layout_get(&l);
- if (sm_power_open &&
- x >= l.sub_x && x < l.sub_x + l.sub_w &&
- y >= l.sub_y && y < l.sub_y + l.sub_h) {
- if (y >= l.sub_y + 4 && y < l.sub_y + 4 + 2 * l.row_h)
- return (y - (l.sub_y + 4)) / l.row_h == 0 ?
- SM_HIT_REBOOT : SM_HIT_POWEROFF;
- return SM_HIT_MENU;
- }
- if (x < l.x || x >= l.x + l.w || y < l.y || y >= l.y + l.h)
- return SM_HIT_NONE;
- if (y >= l.list_y && y < l.list_y + SM_NAPP * l.row_h)
- return (y - l.list_y) / l.row_h;
- if (y >= l.power_y && y < l.power_y + l.row_h)
- return SM_HIT_POWER;
- return SM_HIT_MENU;
- }
- static int sm_update_hover(unsigned int mx, unsigned int my)
- {
- int hot = -1, pwr = 0, hit;
- if (sm_open) {
- hit = sm_hit(mx, my);
- if (hit >= 0)
- hot = hit;
- else if (hit == SM_HIT_POWER)
- pwr = 1;
- else if (hit == SM_HIT_REBOOT)
- pwr = 2;
- else if (hit == SM_HIT_POWEROFF)
- pwr = 3;
- }
- if (hot == sm_hot && pwr == sm_power_hot)
- return 0;
- sm_hot = hot;
- sm_power_hot = pwr;
- return 1;
- }
- static void sm_add_present(struct wm_present_info *p)
- {
- struct sm_layout l;
- if (!sm_open)
- return;
- sm_layout_get(&l);
- add_rect(p, l.x, l.y, (unsigned int)l.w, (unsigned int)l.h);
- if (sm_power_open)
- add_rect(p, l.sub_x, l.sub_y, (unsigned int)l.sub_w,
- (unsigned int)l.sub_h);
- }
- static void sm_close(void)
- {
- if (!sm_open && !sm_power_open)
- return;
- sm_open = 0;
- sm_power_open = 0;
- sm_hot = -1;
- sm_power_hot = 0;
- full_redraw = 1;
- }
- /* A client can self-resize between the pre-frame info/map refresh and
- * u_wm_begin(); that unmaps and remaps its content slot, leaving the map
- * cached above pointing at released pages. Once the frame is active the
- * kernel refuses further self-resizes, so re-reading the generation and
- * remapping here makes the cached canvas safe for the draw calls. The
- * window info itself is left for the next loop, which then sees the
- * generation change and does a full redraw. */
- static void remap_window_contents(void)
- {
- unsigned int i;
- for (i = 0; i < WM_MAX_WINDOWS; i++) {
- struct wm_window_info info;
- struct wm_map_info map;
- if (u_wm_info(i, &info) != 1 || !info.used) {
- wins[i].mapped = 0;
- continue;
- }
- if (wins[i].mapped &&
- wins[i].map.generation == info.content_gen)
- continue;
- if (u_wm_map(i, &map) == 0) {
- wins[i].map = map;
- wins[i].mapped = 1;
- } else {
- wins[i].mapped = 0;
- }
- }
- }
- static void do_update(struct wm_status *st)
- {
- struct wm_present_info present;
- int need = 0;
- int bar_dirty = 0;
- unsigned int i;
- char dt[20];
- static char last_dt[20];
- int kexec_changed = (st->kexec_busy != bar_kexec_busy) ||
- (st->kexec_busy && st->kexec_progress != bar_kexec_progress);
- unsigned int msg_now = u_ticks_ms();
- int osd_hide = 0, osd_repaint = 0;
- int sm_dirty;
- /* The kernel bumps backlight_seq on every brightness key/command.
- * Pop the OSD on a change and keep it up for OSD_MS after the last
- * one. The first poll only records the current sequence, so a WM
- * restart does not flash a stale bubble. */
- if (st->backlight_present) {
- if (!osd_seen_valid) {
- osd_seen_valid = 1;
- osd_seen_seq = st->backlight_seq;
- } else if (st->backlight_seq != osd_seen_seq) {
- osd_seen_seq = st->backlight_seq;
- osd_level = (int)st->backlight_level;
- osd_until = msg_now + OSD_MS;
- osd_show = 1;
- }
- } else if (osd_show) {
- osd_show = 0;
- osd_hide = 1;
- }
- if (osd_show && (int)(msg_now - osd_until) >= 0) {
- osd_show = 0;
- osd_hide = 1;
- }
- if (osd_hide && osd_visible)
- osd_repaint = 1;
- sm_dirty = sm_update_hover(st->mouse.x, st->mouse.y);
- /* The boot-scan window is created before the terminal so the terminal
- * stays the newest (last) window and keeps the keyboard focus. Wait
- * for the scan window to exist before starting the terminal; a failed
- * or wedged helper is bounded so the shell is never held back. */
- if (st->boot_ready && !boot_term_spawned) {
- if (!boot_scan_spawned) {
- boot_scan_spawned = 1;
- if (free_window_slot()) {
- boot_scan_pid = u_spawn("/ramfs/bin/bootscan",
- "", 0, "/");
- if (boot_scan_pid > 0)
- boot_scan_deadline = msg_now + 2000u;
- }
- if (boot_scan_pid <= 0)
- boot_scan_placed = 1;
- }
- if (boot_scan_pid > 0 && !boot_scan_placed) {
- struct wm_window_info wi;
- int idx = window_index_pid(boot_scan_pid, &wi);
- if (idx >= 0) {
- int nx = (int)fbw - (int)wi.w - 16;
- int ny = WM_BAR_H + 8;
- if (nx < 8)
- nx = 8;
- u_wm_set_geom((unsigned int)idx, nx, ny);
- boot_scan_placed = 1;
- } else if (!u_alive((unsigned int)boot_scan_pid) ||
- (int)(msg_now - boot_scan_deadline) >= 0) {
- if (u_alive((unsigned int)boot_scan_pid))
- u_kill((unsigned int)boot_scan_pid);
- boot_scan_placed = 1;
- }
- }
- if (boot_scan_placed) {
- boot_term_spawned = 1;
- if (!free_window_slot()) {
- bar_notice("window limit");
- full_redraw = 1;
- } else if (u_wm_create_term() < 0) {
- bar_notice("cannot start term");
- full_redraw = 1;
- } else {
- full_redraw = 1;
- }
- }
- }
- if (bar_msg_dirty ||
- (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)) {
- bar_dirty = 1;
- bar_msg_dirty = 0;
- if (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)
- bar_msg_until = 0;
- }
- bar_kexec_busy = st->kexec_busy;
- bar_kexec_progress = st->kexec_progress;
- bar_kexec_total = st->kexec_total;
- if (kexec_changed)
- bar_dirty = 1;
- {
- int hot = bar_start_hit(st->mouse.x, st->mouse.y);
- if (hot != sm_start_hot) {
- sm_start_hot = hot;
- bar_dirty = 1;
- }
- }
- {
- int hot = bar_status_hit(st->mouse.x, st->mouse.y);
- if (hot != bar_status_hot) {
- bar_status_hot = hot;
- bar_dirty = 1;
- }
- }
- /* Refresh cached window state and mappings before opening a frame:
- * MAP may unmap/remap content. */
- for (i = 0; i < WM_MAX_WINDOWS; i++) {
- struct wm_window_info info;
- int r = u_wm_info(i, &info);
- if (r < 0) {
- u_exit(1);
- }
- if (r == 0 || !info.used) {
- if (wins[i].info.used)
- full_redraw = 1;
- wins[i].info.used = 0;
- wins[i].mapped = 0;
- continue;
- }
- {
- int pid_changed = wins[i].info.used &&
- info.pid != wins[i].info.pid;
- if (!wins[i].info.used ||
- info.content_gen != wins[i].info.content_gen ||
- pid_changed)
- full_redraw = 1;
- wins[i].info = info;
- if (!wins[i].mapped || pid_changed ||
- wins[i].map.generation != info.content_gen) {
- struct wm_map_info map;
- if (u_wm_map(i, &map) == 0) {
- wins[i].map = map;
- wins[i].mapped = 1;
- } else {
- wins[i].mapped = 0;
- }
- }
- }
- }
- /* Popovers are WM-anchored overlays, flush against the bottom of the
- * bar and the right screen edge. The WM owns the geometry, so the app
- * does not need a placement ABI. */
- {
- int control_found = 0;
- for (i = 0; i < WM_MAX_WINDOWS; i++) {
- struct wm_window_info ni;
- int nx, ny;
- if (!wins[i].info.used || !wins[i].info.popover)
- continue;
- if (control_pid > 0 &&
- wins[i].info.pid == (unsigned int)control_pid)
- control_found = 1;
- nx = (int)fbw - (int)wins[i].info.w;
- ny = WM_BAR_H;
- if (nx != wins[i].info.x || ny != wins[i].info.y) {
- if (u_wm_set_geom(i, nx, ny) == 0 &&
- u_wm_info(i, &ni) == 1) {
- wins[i].info = ni;
- full_redraw = 1;
- }
- }
- }
- if (!control_found && control_pid > 0 &&
- !u_alive((unsigned int)control_pid)) {
- control_pid = 0;
- bar_dirty = 1;
- }
- }
- /* The RTC is slow (CMOS polling) and the temperature/battery state only
- * changes once a second; query them at 1 Hz instead of every 10 ms. */
- {
- static unsigned int last_rtc_ms;
- unsigned int now_ms = u_ticks_ms();
- if (full_redraw || now_ms - last_rtc_ms >= 1000) {
- last_rtc_ms = now_ms;
- if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
- for (i = 0; i < sizeof(dt); i++)
- if (dt[i] != last_dt[i]) {
- for (i = 0; i < sizeof(dt); i++)
- last_dt[i] = dt[i];
- bar_dirty = 1;
- break;
- }
- }
- {
- struct sys_cputemp ct;
- if (u_cpu_temp(&ct) == 0 &&
- ct.temp_c != bar_temp_c) {
- bar_temp_c = ct.temp_c;
- bar_dirty = 1;
- }
- }
- {
- struct sys_powerinfo pi;
- int pct = -1, charging = 0, ac = -1;
- unsigned int bi;
- if (u_power_info(&pi, 0) == 0) {
- /* A laptop EC (ThinkPad or Chrome
- * EC) marks a machine with AC/battery;
- * desktops show neither. */
- if (pi.present) {
- ac = pi.ac_online;
- for (bi = 0; bi < SYS_POWER_BATT_MAX;
- bi++) {
- struct sys_battery *b =
- &pi.battery[bi];
- if (!b->present ||
- !b->percent_valid)
- continue;
- pct = (int)b->percent;
- charging = (int)b->charging;
- break;
- }
- }
- if (pct != bar_batt_pct ||
- charging != bar_batt_charging ||
- ac != bar_ac_online) {
- bar_batt_pct = pct;
- bar_batt_charging = charging;
- bar_ac_online = ac;
- bar_dirty = 1;
- }
- }
- }
- if (poll_net_status())
- bar_dirty = 1;
- }
- }
- if (full_redraw) {
- if (u_wm_begin() != 0) {
- full_redraw = 1;
- return;
- }
- full_redraw = 0;
- remap_window_contents();
- need = 1;
- {
- draw_rect(0, 0, fbw, fbh);
- draw_bar();
- if (osd_show)
- osd_draw();
- osd_visible = osd_show;
- sm_draw();
- present.count = 0;
- add_rect(&present, 0, 0, fbw, fbh);
- present.immediate = 0;
- u_wm_present(&present);
- /* Ack every window that was clean at the time of the
- * redraw. If new damage arrived during it, its seq
- * differs and the kernel leaves it dirty. */
- for (i = 0; i < WM_MAX_WINDOWS; i++)
- if (wins[i].info.used && wins[i].mapped)
- u_wm_ack(i, wins[i].info.dirty_seq);
- }
- return;
- }
- /* Partial cell/canvas updates and the bar clock. */
- present.count = 0;
- present.immediate = 0;
- if (osd_repaint) {
- add_rect(&present, osd_x, osd_y, osd_w, osd_h);
- need = 1;
- }
- if (osd_show)
- need = 1;
- if (bar_dirty) {
- need = 1;
- add_rect(&present, 0, 0, fbw, WM_BAR_H);
- }
- for (i = 0; i < WM_MAX_WINDOWS; i++) {
- struct uwin *u = &wins[i];
- struct wm_window_info *w = &u->info;
- int rx, ry;
- unsigned int rw, rh;
- if (!w->used || !w->dirty)
- continue;
- if (!w->dmg_w || !w->dmg_h)
- continue;
- rx = w->x + (w->popover ? 0 : WM_BORDER_W) + (int)w->dmg_x;
- ry = w->y + (w->popover ? 0 : WM_BORDER_W + WM_TITLE_H) +
- (int)w->dmg_y;
- rw = w->dmg_w;
- rh = w->dmg_h;
- add_rect(&present, rx, ry, rw, rh);
- u->ack_seq = u->mapped ? w->dirty_seq : 0;
- need = 1;
- }
- if (sm_dirty && sm_open)
- need = 1;
- if (!need) {
- /* Drag movement is handled below; a stationary mouse can be
- * left to the kernel's cursor overlay. */
- return;
- }
- if (sm_open)
- sm_add_present(&present);
- if (u_wm_begin() != 0)
- return;
- remap_window_contents();
- for (i = 0; i < present.count; i++)
- draw_rect(present.rect[i].x, present.rect[i].y,
- present.rect[i].w, present.rect[i].h);
- if (bar_dirty)
- draw_bar();
- if (osd_show) {
- osd_draw();
- add_rect(&present, osd_x, osd_y, osd_w, osd_h);
- }
- osd_visible = osd_show;
- sm_draw();
- for (i = 0; i < WM_MAX_WINDOWS; i++) {
- struct uwin *u = &wins[i];
- if (!u->info.used || !u->info.dirty || !u->mapped ||
- !u->ack_seq)
- continue;
- if (u->ack_seq == u->info.dirty_seq)
- u_wm_ack(i, u->info.dirty_seq);
- }
- u_wm_present(&present);
- }
- static void spawn_app_arg_checked(const char *path, const char *arg)
- {
- if (!free_window_slot()) {
- bar_notice("window limit");
- full_redraw = 1;
- return;
- }
- if (u_spawn(path, arg, 0, "/") < 0) {
- bar_notice("spawn failed");
- full_redraw = 1;
- } else {
- full_redraw = 1;
- }
- }
- /* Any status target opens one control panel; a second click closes it. */
- static void open_control_panel(void)
- {
- unsigned int i;
- if (control_pid > 0 && u_alive((unsigned int)control_pid)) {
- for (i = 0; i < WM_MAX_WINDOWS; i++)
- if (wins[i].info.used &&
- wins[i].info.pid == (unsigned int)control_pid) {
- if (u_wm_close(i) == 0) {
- control_pid = 0;
- full_redraw = 1;
- } else {
- u_wm_focus(i);
- full_redraw = 1;
- }
- return;
- }
- /* The process is alive but has not created its window yet. */
- return;
- }
- if (!free_window_slot()) {
- bar_notice("window limit");
- full_redraw = 1;
- return;
- }
- control_pid = u_spawn("/ramfs/bin/netmenu", "", 0, "/");
- if (control_pid < 0) {
- control_pid = 0;
- bar_notice("spawn failed");
- full_redraw = 1;
- } else {
- full_redraw = 1;
- }
- }
- /* Launch one start-menu entry. The terminal and the control panel are WM
- * actions; every other entry spawns its ELF from the pack. */
- static void sm_launch(int idx)
- {
- const struct sm_app *a;
- if (idx < 0 || idx >= SM_NAPP) {
- sm_close();
- return;
- }
- a = &sm_apps[idx];
- sm_close();
- if (a->action == SM_ACT_TERM) {
- if (!free_window_slot()) {
- bar_notice("window limit");
- full_redraw = 1;
- } else if (u_wm_create_term() < 0) {
- bar_notice("cannot start term");
- full_redraw = 1;
- } else {
- full_redraw = 1;
- }
- } else if (a->action == SM_ACT_SETTINGS) {
- open_control_panel();
- } else {
- spawn_app_arg_checked(a->path, a->arg);
- }
- }
- /* Index of a popover containing the point, or -1. */
- static int popover_under(unsigned int mx, unsigned int my)
- {
- unsigned int i;
- for (i = 0; i < WM_MAX_WINDOWS; i++) {
- struct wm_window_info *w = &wins[i].info;
- if (w->used && w->popover &&
- mx >= (unsigned int)w->x && my >= (unsigned int)w->y &&
- mx < (unsigned int)(w->x + (int)w->w) &&
- my < (unsigned int)(w->y + (int)w->h))
- return (int)i;
- }
- return -1;
- }
- /* A press anywhere outside an open popover dismisses it; the click then
- * continues normally so the target under the pointer still reacts. */
- static void dismiss_popover_outside(unsigned int mx, unsigned int my)
- {
- unsigned int i;
- if (popover_under(mx, my) >= 0)
- return;
- for (i = 0; i < WM_MAX_WINDOWS; i++) {
- if (!wins[i].info.used || !wins[i].info.popover)
- continue;
- if (wins[i].info.pid == (unsigned int)control_pid)
- control_pid = 0;
- if (u_wm_close(i) == 0) {
- wins[i].info.used = 0;
- wins[i].mapped = 0;
- full_redraw = 1;
- }
- break;
- }
- }
- /* Apply one edge-drag step. The grabbed edges follow the pointer while the
- * opposite edges stay put; the work area clamps the active edges. */
- static void resize_window_drag(struct uwin *u, unsigned int idx,
- unsigned int mx, unsigned int my)
- {
- int dx = (int)mx - (int)resize_mx;
- int dy = (int)my - (int)resize_my;
- int left = resize_ox;
- int right = resize_ox + (int)resize_ow;
- int top = resize_oy;
- int bottom = resize_oy + (int)resize_oh;
- int nx, ny, nw, nh;
- if (resize_edges & WM_EDGE_LEFT) {
- left = resize_ox + dx;
- if (left < 0)
- left = 0;
- } else if (resize_edges & WM_EDGE_RIGHT) {
- right = resize_ox + (int)resize_ow + dx;
- if (right > (int)fbw)
- right = (int)fbw;
- }
- if (resize_edges & WM_EDGE_TOP) {
- top = resize_oy + dy;
- if (top < WM_BAR_H)
- top = WM_BAR_H;
- } else if (resize_edges & WM_EDGE_BOTTOM) {
- bottom = resize_oy + (int)resize_oh + dy;
- if (bottom > (int)fbh)
- bottom = (int)fbh;
- }
- if ((resize_edges & (WM_EDGE_LEFT | WM_EDGE_RIGHT)) &&
- right - left < 200) {
- if (resize_edges & WM_EDGE_LEFT)
- left = right - 200;
- else
- right = left + 200;
- }
- if ((resize_edges & (WM_EDGE_TOP | WM_EDGE_BOTTOM)) &&
- bottom - top < 140) {
- if (resize_edges & WM_EDGE_TOP)
- top = bottom - 140;
- else
- bottom = top + 140;
- }
- if (left < 0)
- left = 0;
- if (top < WM_BAR_H)
- top = WM_BAR_H;
- nx = left;
- ny = top;
- nw = right - left;
- nh = bottom - top;
- if (nw > (int)fbw)
- nw = (int)fbw;
- if (nh > (int)fbh - WM_BAR_H)
- nh = (int)fbh - WM_BAR_H;
- if (nx == u->info.x && ny == u->info.y &&
- nw == (int)u->info.w && nh == (int)u->info.h)
- return;
- if (set_window_geometry(u, idx, nx, ny, (unsigned int)nw,
- (unsigned int)nh) == 0)
- u->state = WIN_NORMAL;
- }
- /* Capture the pointer for an edge resize that started at (mx,my). */
- static void begin_resize(unsigned int idx, int edges, unsigned int mx,
- unsigned int my)
- {
- struct uwin *u = &wins[idx];
- resize_id = (int)idx;
- resize_edges = edges;
- resize_mx = mx;
- resize_my = my;
- resize_ox = u->info.x;
- resize_oy = u->info.y;
- resize_ow = u->info.w;
- resize_oh = u->info.h;
- u_wm_mouse_capture(1);
- }
- /* The WM's cursor override (SYS_WM_SET_CURSOR): resize arrows on the
- * invisible resize border and while resizing, the move cross while a
- * title-bar drag is under way, a hand over the start button, the open start
- * menu and the status area. Everywhere else the kernel picks the window's
- * own content cursor (or the arrow). */
- static void update_cursor(unsigned int mx, unsigned int my)
- {
- static int current = CURSOR_NO_OVERRIDE;
- int want = CURSOR_NO_OVERRIDE;
- if (resize_id >= 0) {
- want = resize_cursor(resize_edges);
- } else if (drag_id >= 0) {
- if (drag_moved)
- want = CURSOR_MOVE;
- } else if (sm_open) {
- /* The menu is modal: a hand over its items, the arrow
- * elsewhere, never a window's content cursor underneath. */
- int hit = sm_hit(mx, my);
- want = (hit == SM_HIT_NONE || hit == SM_HIT_MENU) ?
- CURSOR_DEFAULT : CURSOR_POINTER;
- } else if (my < WM_BAR_H) {
- if (bar_start_hit(mx, my) || bar_status_hit(mx, my))
- want = CURSOR_POINTER;
- } else {
- int edges = 0;
- int idx = resize_hit((int)mx, (int)my, &edges);
- if (idx >= 0 && edges &&
- !close_hit(&wins[idx].info, (int)mx, (int)my))
- want = resize_cursor(edges);
- }
- if (want != current && u_wm_set_cursor(want) == 0)
- current = want;
- }
- static void handle_mouse(struct wm_status *st)
- {
- unsigned int buttons = st->mouse.buttons;
- unsigned int mx = st->mouse.x, my = st->mouse.y;
- int down = (buttons & 1) != 0;
- int was = (prev_buttons & 1) != 0;
- if (down && !was) {
- int prev_title_id = last_title_id;
- unsigned int now = u_ticks_ms();
- int hit;
- /* The start menu is modal: a press on an entry launches it,
- * a press on Power opens its submenu, and any other press
- * dismisses the menu without reaching the window below. */
- if (sm_open) {
- hit = sm_hit(mx, my);
- last_title_id = -1;
- if (hit == SM_HIT_START) {
- sm_close();
- } else if (hit == SM_HIT_POWER) {
- sm_power_open = !sm_power_open;
- full_redraw = 1;
- } else if (hit == SM_HIT_REBOOT) {
- sm_close();
- if (u_spawn_kcmd("reboot", "", 0) < 0)
- bar_notice("reboot failed");
- } else if (hit == SM_HIT_POWEROFF) {
- sm_close();
- if (u_spawn_kcmd("poweroff", "", 0) < 0)
- bar_notice("poweroff failed");
- } else if (hit >= 0) {
- sm_launch(hit);
- } else if (hit != SM_HIT_MENU) {
- sm_close();
- }
- goto mouse_done;
- }
- if (bar_start_hit(mx, my)) {
- /* Opening the menu also closes an open control
- * panel, so only one floating panel is visible. */
- dismiss_popover_outside(mx, my);
- last_title_id = -1;
- sm_open = 1;
- sm_power_open = 0;
- sm_hot = -1;
- sm_power_hot = 0;
- full_redraw = 1;
- goto mouse_done;
- }
- if (!bar_status_hit(mx, my))
- dismiss_popover_outside(mx, my);
- last_title_id = -1;
- if (bar_status_hit(mx, my)) {
- open_control_panel();
- } else {
- int edges = 0;
- int idx = resize_hit((int)mx, (int)my, &edges);
- if (idx >= 0) {
- struct uwin *u = &wins[idx];
- int pmx = (int)mx, pmy = (int)my;
- u_wm_focus((unsigned int)idx);
- if (u->info.popover) {
- /* A popover has no title bar, close
- * button or resize border; the app owns
- * its content. */
- } else if (close_hit(&u->info, pmx, pmy)) {
- u_wm_close((unsigned int)idx);
- full_redraw = 1;
- } else if (edges) {
- begin_resize((unsigned int)idx, edges,
- mx, my);
- } else if (pmy < u->info.y + WM_TITLE_H) {
- if (prev_title_id == idx &&
- now - last_title_ms <= WM_DBLCLICK_MS &&
- iabs(pmx - last_title_x) <= WM_DBLCLICK_SLOP &&
- iabs(pmy - last_title_y) <= WM_DBLCLICK_SLOP) {
- last_title_ms = 0;
- if (toggle_maximize(u,
- (unsigned int)idx) != 0)
- bar_notice("resize failed");
- } else {
- last_title_id = idx;
- last_title_ms = now;
- last_title_x = pmx;
- last_title_y = pmy;
- drag_id = idx;
- drag_mx = pmx;
- drag_my = pmy;
- drag_dx = pmx - u->info.x;
- drag_dy = pmy - u->info.y;
- drag_moved = 0;
- u_wm_mouse_capture(1);
- }
- }
- }
- }
- } else if (down && was && resize_id >= 0) {
- struct uwin *u = &wins[resize_id];
- if (u->info.used)
- resize_window_drag(u, (unsigned int)resize_id, mx, my);
- else {
- resize_id = -1;
- u_wm_mouse_capture(0);
- }
- } else if (down && was && drag_id >= 0) {
- struct uwin *u = &wins[drag_id];
- if (u->info.used) {
- int ox, oy, nx, ny;
- if (iabs((int)mx - drag_mx) >= WM_DRAG_START ||
- iabs((int)my - drag_my) >= WM_DRAG_START)
- drag_moved = 1;
- /* A maximized or snapped window is restored as soon as
- * the drag starts. Keep the grab point at the same
- * fraction of the title bar, so the restored (narrower)
- * window stays under the pointer. A click that does
- * not move must leave it in place. */
- if (u->state != WIN_NORMAL && drag_moved) {
- unsigned int ow = u->info.w;
- int gx = drag_mx - u->info.x;
- int gy = drag_my - u->info.y;
- if (restore_window(u,
- (unsigned int)drag_id) == 0) {
- if (ow)
- gx = gx * (int)u->info.w /
- (int)ow;
- drag_dx = gx;
- drag_dy = gy;
- }
- }
- if (u->state == WIN_NORMAL) {
- ox = u->info.x;
- oy = u->info.y;
- nx = (int)mx - drag_dx;
- ny = (int)my - drag_dy;
- resist_geom(u, drag_id, &nx, &ny);
- clamp_geom(u, &nx, &ny);
- if (nx != ox || ny != oy) {
- struct wm_present_info present;
- unsigned int i;
- u_wm_set_geom((unsigned int)drag_id,
- nx, ny);
- u->info.x = nx;
- u->info.y = ny;
- if (u->info.focused) {
- move_window_pixels(u, ox, oy,
- nx, ny);
- } else {
- present.count = 0;
- present.immediate = 1;
- add_rect(&present, ox, oy,
- u->info.w, u->info.h);
- add_rect(&present, nx, ny,
- u->info.w, u->info.h);
- if (u_wm_begin() == 0) {
- remap_window_contents();
- for (i = 0;
- i < present.count; i++)
- draw_rect(
- present.rect[i].x,
- present.rect[i].y,
- present.rect[i].w,
- present.rect[i].h);
- wm_overlays(&present);
- u_wm_present(&present);
- }
- }
- }
- }
- } else {
- drag_id = -1;
- u_wm_mouse_capture(0);
- }
- } else if (!down && was) {
- /* Dropping a window with the pointer on the left/right screen
- * edge snaps it to that half; on the top edge it maximizes. */
- if (drag_id >= 0 && drag_moved && wins[drag_id].info.used &&
- wins[drag_id].state == WIN_NORMAL) {
- struct uwin *u = &wins[drag_id];
- int st = WIN_NORMAL;
- if ((int)mx < WM_SNAP_EDGE)
- st = WIN_SNAP_LEFT;
- else if ((int)mx >= (int)fbw - WM_SNAP_EDGE)
- st = WIN_SNAP_RIGHT;
- else if ((int)my < WM_SNAP_EDGE)
- st = WIN_MAXIMIZED;
- if (st != WIN_NORMAL &&
- place_window(u, (unsigned int)drag_id, st) != 0)
- bar_notice("resize failed");
- }
- drag_id = -1;
- resize_id = -1;
- u_wm_mouse_capture(0);
- }
- mouse_done:
- prev_buttons = buttons;
- update_cursor(mx, my);
- }
- static char *put_str(char *p, const char *s)
- {
- while (*s)
- *p++ = *s++;
- 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;
- }
- /* Expose the last interval in /share so the target can be measured without
- * a serial console. */
- static void write_stats(void)
- {
- char b[80], *p = b;
- unsigned int now = u_ticks_ms();
- p = put_str(p, "t=");
- p = put_dec(p, now / 1000);
- p = put_str(p, "s frames=");
- p = put_dec(p, stat_frames);
- p = put_str(p, " ms=");
- p = put_dec(p, stat_ms);
- p = put_str(p, " max=");
- p = put_dec(p, stat_max);
- p = put_str(p, " px=");
- p = put_dec(p, stat_px);
- p = put_str(p, "\n");
- u_syscall3(SYS_WRITE_FILE, (int)"/share/wmstats", (int)b,
- (int)(p - b));
- }
- void _start(const char *arg)
- {
- struct wm_screen_info si;
- struct wm_status st;
- unsigned int i;
- (void)arg;
- if (u_wm_attach(&si) != 0)
- u_exit(1);
- usurf_init(&screen, si.fb, si.width, si.height, si.pitch, si.bpp);
- usurf_font_attach(&screen, "/ramfs/fonts/sans.ttf", 14);
- fbw = si.width;
- fbh = si.height;
- for (i = 0; i < WM_MAX_WINDOWS; i++) {
- wins[i].info.used = 0;
- wins[i].mapped = 0;
- }
- full_redraw = 1;
- for (;;) {
- unsigned int t0, t1;
- if (u_wm_poll(&st) != 0)
- u_exit(1);
- if (st.full_redraw)
- full_redraw = 1;
- usurf_pixels = 0;
- t0 = u_ticks_ms();
- handle_mouse(&st);
- do_update(&st);
- t1 = u_ticks_ms();
- if (usurf_pixels) {
- unsigned int dt = t1 - t0;
- stat_frames++;
- stat_ms += dt;
- stat_px += usurf_pixels;
- if (dt > stat_max)
- stat_max = dt;
- }
- if (t1 - stat_last >= 2000) {
- stat_last = t1;
- if (stat_frames) {
- write_stats();
- stat_frames = stat_ms = stat_px = stat_max = 0;
- }
- }
- u_sleep_ms(10);
- }
- }
|