ucmd_wm.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752
  1. /* Ring-3 window manager: maps the shadow buffer and each window's content,
  2. * draws the desktop/chrome and composites the terminal cell grids or gfx
  3. * canvases. All glyphs and colours are userland; the kernel only routes
  4. * input, stores window state and copies damage to VRAM. */
  5. #include "ucmd_lib.h"
  6. #include "uwm_lib.h"
  7. #include "version.h"
  8. #define COL_BG 0x202030u
  9. /* The active title bar uses the toolkit highlight (ngui_theme_dark.selection);
  10. * the inactive one stays neutral. Windows have no visible frame. */
  11. #define COL_TITLE_ON 0x365CA6u
  12. #define COL_TITLE_OFF 0x646464u
  13. #define COL_CLOSE 0xC64040u
  14. #define COL_WHITE 0xFFFFFFu
  15. #define COL_BANNER 0x55FF55u
  16. struct uwin {
  17. struct wm_window_info info;
  18. struct wm_map_info map;
  19. int mapped;
  20. unsigned int ack_seq;
  21. /* WIN_NORMAL, or maximized/snapped to a screen half; restore_* is the
  22. * geometry to return to when the window leaves that state. */
  23. int state;
  24. int restore_x, restore_y;
  25. unsigned int restore_w, restore_h;
  26. };
  27. /* Double-click detection on the title bar. */
  28. #define WM_DBLCLICK_MS 500
  29. #define WM_DBLCLICK_SLOP 8
  30. /* A maximized window restores only after the pointer actually starts a drag,
  31. * so a plain click on its title bar leaves it maximized. */
  32. #define WM_DRAG_START 3
  33. /* Window placement states. Maximize fills the work area below the bar; the
  34. * snap states fill its left or right half at full height. */
  35. #define WIN_NORMAL 0
  36. #define WIN_MAXIMIZED 1
  37. #define WIN_SNAP_LEFT 2
  38. #define WIN_SNAP_RIGHT 3
  39. /* Releasing a title-bar drag with the pointer this close to the left/right
  40. * screen edge snaps the window to that half; at the top edge it maximizes. */
  41. #define WM_SNAP_EDGE 2
  42. /* Edge resistance: a dragged window's edge stops at a screen edge or at the
  43. * facing edge of another window until the pointer overshoots it by this
  44. * many pixels, which makes exact side-by-side placement easy. */
  45. #define WM_RESIST 24
  46. static struct usurf screen;
  47. static struct uwin wins[WM_MAX_WINDOWS];
  48. static unsigned int fbw, fbh;
  49. /* Desktop wallpaper: the viewer stores a header plus RGB565 pixels; the WM
  50. * only has to stretch that small buffer to the framebuffer, so no image
  51. * decoder is linked in. The static buffer avoids pulling malloc into the
  52. * WM and stays within the 4 MB user image window. */
  53. #define WALL_MAP_MAX 2048
  54. static unsigned char wall_data[sizeof(struct wm_wallpaper_hdr) +
  55. (unsigned int)WM_WALLPAPER_MAX_W * WM_WALLPAPER_MAX_H * 2u + 512];
  56. static unsigned int wall_w, wall_h;
  57. static int wall_ready;
  58. static unsigned int wall_seq_seen;
  59. static int wall_boot_seen;
  60. static int wall_retries;
  61. static unsigned int wall_retry_ms;
  62. static unsigned short wall_xmap[WALL_MAP_MAX];
  63. static unsigned short wall_ymap[WALL_MAP_MAX];
  64. /* RGB565 -> 0xFFRRGGBB, built when the wallpaper loads so the 32 bpp
  65. * desktop path is a table lookup instead of per-pixel shifts. */
  66. static unsigned int wall_rgb32[65536];
  67. static int drag_id = -1;
  68. static int drag_dx, drag_dy;
  69. static int drag_mx, drag_my; /* press position, for restore-on-drag */
  70. static int drag_moved; /* the pointer passed WM_DRAG_START */
  71. static int resize_id = -1;
  72. static int resize_edges; /* WM_EDGE_* bits captured at the press */
  73. static int resize_ox, resize_oy;
  74. static unsigned int resize_mx, resize_my, resize_ow, resize_oh;
  75. static unsigned int last_title_ms;
  76. static int last_title_id = -1, last_title_x, last_title_y;
  77. static unsigned int prev_buttons;
  78. static int full_redraw = 1;
  79. static int boot_term_spawned;
  80. static int boot_scan_spawned;
  81. static int boot_scan_pid;
  82. static int boot_scan_placed;
  83. static unsigned int boot_scan_deadline;
  84. static unsigned int bar_kexec_busy, bar_kexec_progress, bar_kexec_total;
  85. static void remap_window_contents(void);
  86. static int bar_temp_c = -1;
  87. static int bar_batt_pct = -1;
  88. static int bar_batt_charging;
  89. static int bar_ac_online = -1;
  90. /* Both top-bar buttons share this geometry: one rounded pill that stops one
  91. * pixel above the rainbow separator (which starts at WM_BAR_H - 5). */
  92. #define BAR_BTN_Y 2
  93. #define BAR_BTN_H 22
  94. #define BAR_BTN_RADIUS 6
  95. /* Battery/AC status icon: the Adwaita AC adapter on AC, the battery outline
  96. * with our own level fill otherwise. The rightmost bar item. */
  97. #define BAR_PWR_W 24
  98. #define BAR_PWR_Y 7
  99. /* The clock, temperature, network and AC/battery labels are one status
  100. * button. draw_bar() records their bounding rectangle; only that rectangle
  101. * opens the control panel, so the empty part of the bar is not clickable. */
  102. static int bar_status_x = -1;
  103. static unsigned int bar_status_w;
  104. static int bar_status_hot;
  105. /* Brightness OSD: a centered bubble shown after the kernel's backlight
  106. * seq changes, then hidden after OSD_MS. */
  107. #define OSD_MS 3000
  108. #define OSD_W 210
  109. #define OSD_H 60
  110. static int osd_show; /* should be visible this frame */
  111. static int osd_visible; /* pixels are on screen */
  112. static int osd_level;
  113. static unsigned int osd_until;
  114. static unsigned int osd_seen_seq;
  115. static int osd_seen_valid;
  116. static int osd_x, osd_y;
  117. static unsigned int osd_w, osd_h;
  118. static char bar_msg[24];
  119. static unsigned int bar_msg_until;
  120. static int bar_msg_dirty;
  121. static unsigned int stat_frames, stat_ms, stat_px, stat_max, stat_last;
  122. /* ---- Start menu --------------------------------------------------------
  123. * The NebOS banner in the top-left corner is the start button. It opens a
  124. * Windows-like panel drawn by the WM itself (not a window, so it never
  125. * consumes a WM_MAX_WINDOWS slot and floats above every canvas). The panel
  126. * lists every GUI app in the pack and ends with a Power row whose submenu
  127. * reboots or powers off the machine through the privileged kernel command
  128. * registry (SYS_SPAWN_KCMD). */
  129. /* The panel mirrors ngui_theme_dark and the netmenu popover: a flat
  130. * theme.bg surface without a rim, rounded theme.panel summary cards,
  131. * theme.panel_alt hover rows and the selection colour for an active item. */
  132. #define SM_BG 0x181C28u
  133. #define SM_PANEL 0x262B3Au
  134. #define SM_PANEL_ALT 0x32384Au
  135. #define SM_TEXT 0xE4E6F0u
  136. #define SM_TEXT_DIM 0x9197AAu
  137. #define SM_SEL 0x365CA6u
  138. #define SM_BTN_HOT 0x46516Cu
  139. #define SM_ERROR 0xEE7474u
  140. #define SM_W 252
  141. #define SM_PAD 12
  142. #define SM_TOP 10
  143. #define SM_GAP 6
  144. #define SM_SECT_H 22
  145. #define SM_ROW_H 26
  146. #define SM_MIN_ROW 20
  147. #define SM_BOTTOM 10
  148. #define SM_RADIUS 6
  149. #define SM_SUB_W 160
  150. #define SM_TILE 18
  151. #define SM_HIT_NONE (-1)
  152. #define SM_HIT_START (-2)
  153. #define SM_HIT_POWER (-3)
  154. #define SM_HIT_REBOOT (-4)
  155. #define SM_HIT_POWEROFF (-5)
  156. #define SM_HIT_MENU (-6) /* inside the panel but not on an entry */
  157. #define SM_ACT_SPAWN 0
  158. #define SM_ACT_TERM 1
  159. #define SM_ACT_SETTINGS 2
  160. struct sm_app {
  161. const char *label;
  162. const char *path;
  163. const char *arg;
  164. unsigned int tile;
  165. char letter;
  166. unsigned char action;
  167. };
  168. /* Every self-contained GUI application in the pack. Terminal and Settings
  169. * are WM actions (a new terminal canvas and the control panel); edit gets a
  170. * scratch file so it can start without a file argument. */
  171. static const struct sm_app sm_apps[] = {
  172. { "Terminal", "", "", 0x3C8CD2u, 'T', SM_ACT_TERM },
  173. { "Files", "/ramfs/bin/files", "", 0xE0A030u, 'F', SM_ACT_SPAWN },
  174. { "Web", "/ramfs/bin/browser", "", 0x4CA0E0u, 'W', SM_ACT_SPAWN },
  175. { "Gallery", "/ramfs/bin/gallery", "", 0xC864B4u, 'G', SM_ACT_SPAWN },
  176. { "Chat", "/ramfs/bin/chat", "", 0x55A055u, 'C', SM_ACT_SPAWN },
  177. { "Clock", "/ramfs/bin/clock", "", 0x8C8C9Cu, 'C', SM_ACT_SPAWN },
  178. { "Text editor", "/ramfs/bin/edit", "/share/UNTITLED.TXT", 0xD27850u, 'E', SM_ACT_SPAWN },
  179. { "Colors", "/ramfs/bin/colors", "", 0xC85A5Au, 'P', SM_ACT_SPAWN },
  180. { "Mines", "/ramfs/bin/mines", "", 0x8C6CD2u, 'M', SM_ACT_SPAWN },
  181. { "Cards", "/ramfs/bin/solitaire","", 0x3CA08Cu, 'S', SM_ACT_SPAWN },
  182. { "Disks", "/ramfs/bin/disks", "", 0x5A8CC8u, 'D', SM_ACT_SPAWN },
  183. { "Tasks", "/ramfs/bin/process", "", 0xE0B040u, 'P', SM_ACT_SPAWN },
  184. { "Settings", "", "", 0x7A7A9Au, 'S', SM_ACT_SETTINGS },
  185. { "Date & Time", "/ramfs/bin/settime", "", 0x5AA0A0u, 'D', SM_ACT_SPAWN },
  186. };
  187. #define SM_NAPP ((int)(sizeof(sm_apps) / sizeof(sm_apps[0])))
  188. struct sm_layout {
  189. int x, y, w, h;
  190. int row_h;
  191. int sect_y;
  192. int row_x, row_w;
  193. int list_y;
  194. int power_y;
  195. int sub_x, sub_y, sub_w, sub_h;
  196. };
  197. static int sm_open;
  198. static int sm_power_open;
  199. static int sm_start_hot;
  200. static int sm_hot; /* hovered app index, -1 = none */
  201. static int sm_power_hot; /* 0 none, 1 Power, 2 Reboot, 3 Power off */
  202. static void sm_layout_get(struct sm_layout *l);
  203. static void sm_draw(void);
  204. static void sm_add_present(struct wm_present_info *p);
  205. static int sm_update_hover(unsigned int mx, unsigned int my);
  206. static void sm_close(void);
  207. static void sm_launch(int idx);
  208. /* Network status icon (right side of the bar). */
  209. #define BAR_NET_W 24
  210. #define BAR_NET_Y 7
  211. #define BAR_NET_NONE 0
  212. #define BAR_NET_WIRED 1
  213. #define BAR_NET_WIFI 2
  214. #define BAR_NET_USB 3
  215. static int bar_net_kind;
  216. static int bar_net_bars;
  217. static unsigned int bar_net_color = 0xFF5050u;
  218. static int control_pid;
  219. static void bar_notice(const char *msg)
  220. {
  221. unsigned int i;
  222. for (i = 0; msg[i] && i + 1 < sizeof(bar_msg); i++)
  223. bar_msg[i] = msg[i];
  224. bar_msg[i] = '\0';
  225. bar_msg_until = u_ticks_ms() + 2000;
  226. bar_msg_dirty = 1;
  227. }
  228. static int free_window_slot(void)
  229. {
  230. unsigned int i;
  231. for (i = 0; i < WM_MAX_WINDOWS; i++)
  232. if (!wins[i].info.used)
  233. return 1;
  234. return 0;
  235. }
  236. /* Find a window by owner pid without waiting for the cached sync state:
  237. * boot ordering needs it before the terminal is spawned. */
  238. static int window_index_pid(int pid, struct wm_window_info *out)
  239. {
  240. unsigned int i;
  241. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  242. struct wm_window_info info;
  243. if (u_wm_info(i, &info) == 1 && info.used &&
  244. (int)info.pid == pid) {
  245. if (out)
  246. *out = info;
  247. return (int)i;
  248. }
  249. }
  250. return -1;
  251. }
  252. static unsigned int rgb16(unsigned int r, unsigned int g, unsigned int b)
  253. {
  254. return (r << 16) | (g << 8) | b;
  255. }
  256. /* Rainbow gradient: hue 0..1535 (6*256) maps to R→Y→G→C→B→M→R, the same
  257. * integer interpolation the kernel bar uses. */
  258. static unsigned int rainbow_rgb(unsigned int hue)
  259. {
  260. unsigned int seg = hue / 256, f = hue % 256;
  261. unsigned int r1, g1, b1, r2, g2, b2;
  262. switch (seg) {
  263. case 0: r1=255; g1=0; b1=0; r2=255; g2=255; b2=0; break;
  264. case 1: r1=255; g1=255; b1=0; r2=0; g2=255; b2=0; break;
  265. case 2: r1=0; g1=255; b1=0; r2=0; g2=255; b2=255; break;
  266. case 3: r1=0; g1=255; b1=255; r2=0; g2=0; b2=255; break;
  267. case 4: r1=0; g1=0; b1=255; r2=255; g2=0; b2=255; break;
  268. default:r1=255; g1=0; b1=255; r2=255; g2=0; b2=0; break;
  269. }
  270. return rgb16((r1 * (256 - f) + r2 * f) / 256,
  271. (g1 * (256 - f) + g2 * f) / 256,
  272. (b1 * (256 - f) + b2 * f) / 256);
  273. }
  274. static void fill_clip(int x, int y, unsigned int w, unsigned int h,
  275. unsigned int rgb, int cx, int cy,
  276. unsigned int cw, unsigned int ch)
  277. {
  278. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  279. if (x0 < cx) x0 = cx;
  280. if (y0 < cy) y0 = cy;
  281. if (x1 > cx + (int)cw) x1 = cx + (int)cw;
  282. if (y1 > cy + (int)ch) y1 = cy + (int)ch;
  283. if (x1 > x0 && y1 > y0)
  284. usurf_fill(&screen, x0, y0, (unsigned int)(x1 - x0),
  285. (unsigned int)(y1 - y0), rgb);
  286. }
  287. /* Rounded rectangle fill, matching ngui_fill_round_rect()'s pixel-centre
  288. * corner sampling (the popover's cards and tiles use a 6 px radius). */
  289. static int sm_isqrt(int v)
  290. {
  291. int r = 0, bit = 1 << 30;
  292. if (v <= 0)
  293. return 0;
  294. while (bit > v)
  295. bit >>= 2;
  296. while (bit) {
  297. if (v >= r + bit) {
  298. v -= r + bit;
  299. r = (r >> 1) + bit;
  300. } else {
  301. r >>= 1;
  302. }
  303. bit >>= 2;
  304. }
  305. return r;
  306. }
  307. static int sm_round_inset(int y, int h, int r)
  308. {
  309. int dy2, d2;
  310. if (r <= 0)
  311. return 0;
  312. if (y < r)
  313. dy2 = 2 * r - 2 * y - 1;
  314. else if (y >= h - r)
  315. dy2 = 2 * (y - (h - r)) + 1;
  316. else
  317. return 0;
  318. d2 = 4 * r * r - dy2 * dy2;
  319. if (d2 < 0)
  320. d2 = 0;
  321. return r - (sm_isqrt(d2) + 1) / 2;
  322. }
  323. static void sm_fill_round(int x, int y, int w, int h, int r,
  324. unsigned int rgb)
  325. {
  326. int yy, half;
  327. if (w <= 0 || h <= 0)
  328. return;
  329. half = w < h ? w / 2 : h / 2;
  330. if (r > half)
  331. r = half;
  332. if (r <= 0) {
  333. usurf_fill(&screen, x, y, (unsigned int)w, (unsigned int)h,
  334. rgb);
  335. return;
  336. }
  337. for (yy = 0; yy < h; yy++) {
  338. int li = 0, ri = 0;
  339. if (yy < r) {
  340. li = sm_round_inset(yy, h, r);
  341. ri = li;
  342. }
  343. if (yy >= h - r) {
  344. int i = sm_round_inset(yy, h, r);
  345. if (i > li)
  346. li = i;
  347. if (i > ri)
  348. ri = i;
  349. }
  350. if (w - li - ri > 0)
  351. usurf_fill(&screen, x + li, y + yy,
  352. (unsigned int)(w - li - ri), 1, rgb);
  353. }
  354. }
  355. /* One shared renderer for the start and status buttons: the same height,
  356. * corner radius and hover/active colours. */
  357. static void bar_button_bg(int x, int w, int active, int hot)
  358. {
  359. if (active)
  360. sm_fill_round(x, BAR_BTN_Y, w, BAR_BTN_H, BAR_BTN_RADIUS,
  361. SM_SEL);
  362. else if (hot)
  363. sm_fill_round(x, BAR_BTN_Y, w, BAR_BTN_H, BAR_BTN_RADIUS,
  364. SM_PANEL_ALT);
  365. }
  366. /* The NebOS banner doubles as the start button. Its width follows the
  367. * proportional font, so the hit area and the highlight use the same
  368. * geometry. */
  369. static void bar_start_geom(int *x, int *y, int *w, int *h)
  370. {
  371. *x = 2;
  372. *y = BAR_BTN_Y;
  373. *h = BAR_BTN_H;
  374. *w = (int)usurf_text_width(&screen, OS_NAME " v" OS_VERSION) + 20;
  375. }
  376. static int bar_start_hit(unsigned int mx, unsigned int my)
  377. {
  378. int x, y, w, h;
  379. bar_start_geom(&x, &y, &w, &h);
  380. return (int)mx >= x && (int)mx < x + w &&
  381. (int)my >= y && (int)my < y + h;
  382. }
  383. static void bar_start_button(void)
  384. {
  385. int x, y, w, h;
  386. bar_start_geom(&x, &y, &w, &h);
  387. bar_button_bg(x, w, sm_open, sm_start_hot);
  388. usurf_text(&screen, x + 10, y + (h - 16) / 2,
  389. OS_NAME " v" OS_VERSION,
  390. sm_open ? COL_WHITE : COL_BANNER, 0, 0, fbw, WM_BAR_H);
  391. }
  392. static int name_is(const char *a, const char *b)
  393. {
  394. while (*a && *b) {
  395. if (*a != *b)
  396. return 0;
  397. a++;
  398. b++;
  399. }
  400. return *a == *b;
  401. }
  402. static int net_rssi_bars(int rssi)
  403. {
  404. if (rssi >= -55)
  405. return 4;
  406. if (rssi >= -65)
  407. return 3;
  408. if (rssi >= -75)
  409. return 2;
  410. return 1;
  411. }
  412. /* Small top-bar pictograms: Adwaita symbolic masks from the pack, centered
  413. * in their 24 px hit slots. */
  414. static void bar_draw_net_icon(int x, int y)
  415. {
  416. enum uwm_icon ic;
  417. switch (bar_net_kind) {
  418. case BAR_NET_WIRED:
  419. ic = UWM_ICON_NET_WIRED;
  420. break;
  421. case BAR_NET_WIFI:
  422. ic = bar_net_bars > 0 ? UWM_ICON_NET_WIFI_0 + bar_net_bars :
  423. UWM_ICON_NET_WIFI_OFF;
  424. break;
  425. case BAR_NET_USB:
  426. ic = UWM_ICON_NET_USB;
  427. break;
  428. default:
  429. ic = UWM_ICON_NET_WIRED_OFF;
  430. break;
  431. }
  432. (void)usurf_icon(&screen, x + 4, y, ic, bar_net_color);
  433. }
  434. /* 1 Hz poll: cached state -> the bar's network icon. The WiFi status call is
  435. * only made when WiFi is the active netdev, so a machine without a card does
  436. * not run a PCI scan on every tick. */
  437. static int poll_net_status(void)
  438. {
  439. struct sys_netinfo ni;
  440. int kind = BAR_NET_NONE, bars = 0, have_ip = 0;
  441. unsigned int color = 0xFF5050u;
  442. int changed;
  443. int i;
  444. if (u_net_info(&ni) == 1 && ni.present) {
  445. for (i = 0; i < 4; i++)
  446. if (ni.ip[i])
  447. have_ip = 1;
  448. if (name_is(ni.name, "wifi")) {
  449. struct sys_wifi_status ws;
  450. if (u_wifi(SYS_WIFI_STATUS, (int)&ws, 0) == 0 &&
  451. ws.present) {
  452. kind = BAR_NET_WIFI;
  453. if (ws.state >= SYS_WIFI_ST_ASSOC) {
  454. bars = net_rssi_bars(ws.rssi);
  455. color = (ws.state >= SYS_WIFI_ST_LINK &&
  456. have_ip) ?
  457. 0x55FF55u : 0xFFD250u;
  458. } else {
  459. /* Card present, not associated. */
  460. bars = 0;
  461. color = 0xFF5050u;
  462. }
  463. }
  464. } else if (name_is(ni.name, "rndis")) {
  465. kind = BAR_NET_USB;
  466. color = have_ip ? 0x55FF55u : 0xFFD250u;
  467. } else {
  468. kind = BAR_NET_WIRED;
  469. color = have_ip ? 0x55FF55u : 0xFFD250u;
  470. }
  471. }
  472. changed = kind != bar_net_kind || bars != bar_net_bars ||
  473. color != bar_net_color;
  474. bar_net_kind = kind;
  475. bar_net_bars = bars;
  476. bar_net_color = color;
  477. return changed;
  478. }
  479. static unsigned int temp_color(int c)
  480. {
  481. if (c >= 80)
  482. return 0xFF5050u;
  483. if (c >= 60)
  484. return 0xFFD250u;
  485. return 0x55FF55u;
  486. }
  487. /* "43C" (no libc printf here). */
  488. static unsigned int temp_text(char *b, int c)
  489. {
  490. unsigned int n = 0, v = (unsigned int)c;
  491. if (v >= 100)
  492. b[n++] = (char)('0' + (v / 100) % 10);
  493. if (v >= 10)
  494. b[n++] = (char)('0' + (v / 10) % 10);
  495. b[n++] = (char)('0' + v % 10);
  496. b[n++] = 'C';
  497. b[n] = '\0';
  498. return n;
  499. }
  500. static unsigned int batt_color(int pct)
  501. {
  502. if (pct <= 10)
  503. return 0xFF5050u;
  504. return 0x55FF55u;
  505. }
  506. /* Battery: the Adwaita outline with our own level fill over the lower
  507. * interior. The symbol's body is striped, so the interior is cleared to a
  508. * dim shade of the state colour first; the level then reads as a bright
  509. * block inside the outline. A critical charge turns the outline red too.
  510. * Geometry of battery-symbolic at 16 px: body x2..13, cavity x4..11 and
  511. * y4..13. */
  512. static unsigned int batt_dim(unsigned int c)
  513. {
  514. return (c >> 2) & 0x3F3F3F;
  515. }
  516. static void bar_icon_battery(int x, int y, int pct, unsigned int c)
  517. {
  518. unsigned int fill = (unsigned int)((10 * pct + 99) / 100);
  519. int ix = x + 4;
  520. if (fill > 10)
  521. fill = 10;
  522. if (!usurf_icon(&screen, ix, y, UWM_ICON_BATTERY, c))
  523. return;
  524. usurf_fill(&screen, ix + 4, y + 4, 8, 10, batt_dim(c));
  525. if (fill)
  526. usurf_fill(&screen, ix + 4, y + 14 - (int)fill, 8, fill, c);
  527. }
  528. /* AC adapter: the Adwaita plug symbol. */
  529. static void bar_icon_ac(int x, int y, unsigned int c)
  530. {
  531. (void)usurf_icon(&screen, x + 4, y, UWM_ICON_AC, c);
  532. }
  533. static void draw_bar(void)
  534. {
  535. char dt[48];
  536. unsigned int n, i, x, y;
  537. usurf_fill(&screen, 0, 0, fbw, WM_BAR_H, COL_BG);
  538. bar_start_button();
  539. if (bar_msg_until &&
  540. (int)(u_ticks_ms() - bar_msg_until) < 0)
  541. usurf_text(&screen, (int)(WM_BAR_LEFT_END + 8),
  542. 5, bar_msg, 0xFFD250u, 0, 0, fbw, WM_BAR_H);
  543. /* Rainbow separator (same y as the kernel's old bar). */
  544. for (x = 0; x < fbw; x++) {
  545. unsigned int c = rainbow_rgb((x * (6 * 256)) / (fbw ? fbw : 1));
  546. for (y = WM_BAR_H - 5; y < WM_BAR_H - 3; y++)
  547. usurf_fill(&screen, (int)x, (int)y, 1, 1, c);
  548. }
  549. if (bar_kexec_busy) {
  550. static const char msg[] = "loading new kernel over serial";
  551. unsigned int dots = bar_kexec_total ?
  552. (bar_kexec_progress * 12 / bar_kexec_total) : 0;
  553. bar_status_x = -1;
  554. bar_status_w = 0;
  555. n = 0;
  556. while (msg[n])
  557. dt[n] = msg[n], n++;
  558. for (i = 0; i < 12; i++)
  559. dt[n + i] = (i < dots) ? '.' : ' ';
  560. n += 12;
  561. dt[n] = '\0';
  562. usurf_text(&screen,
  563. (int)fbw - (int)usurf_text_width(&screen, dt) - 4,
  564. 5, dt, 0xFFC864u, 0, 0, fbw, WM_BAR_H);
  565. } else {
  566. int right = (int)fbw - 4;
  567. int min_x = (int)WM_BAR_LEFT_END + 4;
  568. int pwr_px = -1, net_nx = -1, clock_tx = -1, temp_tx = -1;
  569. int temp_tw;
  570. int left;
  571. char cdt[12], tb[12];
  572. const char *clock_txt = dt;
  573. /* Lay the cluster out right to left: AC/battery, network, the
  574. * date/time and the CPU temperature. */
  575. if (bar_batt_pct >= 0 || bar_ac_online > 0) {
  576. pwr_px = right - BAR_PWR_W;
  577. if (pwr_px >= min_x)
  578. right = pwr_px - 8;
  579. else
  580. pwr_px = -1;
  581. }
  582. net_nx = right - BAR_NET_W;
  583. if (net_nx >= min_x)
  584. right = net_nx - 8;
  585. else
  586. net_nx = -1;
  587. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  588. int tw = (int)usurf_text_width(&screen, dt);
  589. int tx = right - tw;
  590. unsigned int k;
  591. if (tx < min_x) {
  592. /* Compact form "MM-DD HH:MM" from
  593. * "YYYY-MM-DD HH:MM:SS" when the status
  594. * cluster is too wide for the full date. */
  595. for (k = 0; k < 11 && dt[5 + k]; k++)
  596. cdt[k] = dt[5 + k];
  597. cdt[k] = '\0';
  598. clock_txt = cdt;
  599. tw = (int)usurf_text_width(&screen, cdt);
  600. tx = right - tw;
  601. }
  602. if (tx >= min_x) {
  603. clock_tx = tx;
  604. right = tx - 8;
  605. }
  606. }
  607. if (bar_temp_c >= 0) {
  608. (void)temp_text(tb, bar_temp_c);
  609. temp_tw = (int)usurf_text_width(&screen, tb);
  610. temp_tx = right - temp_tw;
  611. if (temp_tx < min_x)
  612. temp_tx = -1;
  613. }
  614. /* The status labels form one click target: record the whole
  615. * cluster and paint one rounded hover pill behind it. */
  616. left = pwr_px;
  617. if (net_nx >= 0)
  618. left = net_nx;
  619. if (clock_tx >= 0)
  620. left = clock_tx;
  621. if (temp_tx >= 0)
  622. left = temp_tx;
  623. if (left >= 0) {
  624. bar_status_x = left - 4;
  625. if (bar_status_x < 0)
  626. bar_status_x = 0;
  627. bar_status_w = (unsigned int)((int)fbw - 2 -
  628. bar_status_x);
  629. bar_button_bg(bar_status_x, (int)bar_status_w,
  630. control_pid > 0, bar_status_hot);
  631. } else {
  632. bar_status_x = -1;
  633. bar_status_w = 0;
  634. }
  635. if (pwr_px >= 0) {
  636. if (bar_ac_online > 0)
  637. bar_icon_ac(pwr_px, BAR_PWR_Y, 0x55FF55u);
  638. else
  639. bar_icon_battery(pwr_px, BAR_PWR_Y,
  640. bar_batt_pct,
  641. batt_color(bar_batt_pct));
  642. }
  643. if (net_nx >= 0)
  644. bar_draw_net_icon(net_nx, BAR_NET_Y);
  645. if (clock_tx >= 0)
  646. usurf_text(&screen, clock_tx, 5, clock_txt, COL_BANNER,
  647. 0, 0, fbw, WM_BAR_H);
  648. if (temp_tx >= 0)
  649. usurf_text(&screen, temp_tx, 5, tb,
  650. temp_color(bar_temp_c), 0, 0, fbw,
  651. WM_BAR_H);
  652. }
  653. }
  654. /* A popover is a flat, chromeless panel: the canvas fills the window. */
  655. static void draw_popover_clip(unsigned int idx, int cx, int cy,
  656. unsigned int cw, unsigned int ch)
  657. {
  658. struct uwin *u = &wins[idx];
  659. struct wm_window_info *w = &u->info;
  660. unsigned int cwid = w->content_w, chei = w->content_h;
  661. if (u->mapped && u->map.kind == WM_KIND_GFX) {
  662. struct uwm_canvas c;
  663. c.px = (const unsigned short *)u->map.va;
  664. c.w = u->map.w;
  665. c.h = u->map.h;
  666. if (c.w == cwid && c.h == chei) {
  667. usurf_canvas(&screen, &c, w->x, w->y,
  668. cx, cy, cw, ch);
  669. return;
  670. }
  671. }
  672. fill_clip(w->x, w->y, cwid, chei, 0x181C28u, cx, cy, cw, ch);
  673. }
  674. static void draw_window_clip(unsigned int idx, int cx, int cy,
  675. unsigned int cw, unsigned int ch)
  676. {
  677. struct uwin *u = &wins[idx];
  678. struct wm_window_info *w = &u->info;
  679. int x = w->x, y = w->y;
  680. unsigned int pw = w->w;
  681. int bx = x + (int)pw - WM_BORDER_W - WM_CLOSE_W;
  682. int cony = y + WM_BORDER_W + WM_TITLE_H;
  683. int content_x = x + WM_BORDER_W;
  684. unsigned int content_w = w->content_w;
  685. unsigned int content_h = w->content_h;
  686. if (w->popover) {
  687. draw_popover_clip(idx, cx, cy, cw, ch);
  688. return;
  689. }
  690. /* Title bar and close button; windows have no visible frame. */
  691. fill_clip(x, y, pw, WM_TITLE_H,
  692. w->focused ? COL_TITLE_ON : COL_TITLE_OFF, cx, cy, cw, ch);
  693. usurf_text(&screen, x + 3, y + 2, w->title, COL_WHITE,
  694. cx, cy, cw, ch);
  695. fill_clip(bx, y, WM_CLOSE_W, WM_TITLE_H, COL_CLOSE, cx, cy, cw, ch);
  696. usurf_text(&screen, bx + 4, y + 2, "x", COL_WHITE,
  697. cx, cy, cw, ch);
  698. /* Content: every window presents an RGB565 canvas (the terminal
  699. * renders its cells into one; gfx commands own theirs). A mapped
  700. * canvas covers the whole content area, so it does not need the black
  701. * underlay. */
  702. if (u->mapped && u->map.kind == WM_KIND_GFX) {
  703. struct uwm_canvas c;
  704. c.px = (const unsigned short *)u->map.va;
  705. c.w = u->map.w;
  706. c.h = u->map.h;
  707. if (c.w != content_w || c.h != content_h)
  708. fill_clip(content_x, cony, content_w, content_h,
  709. 0x000000u, cx, cy, cw, ch);
  710. usurf_canvas(&screen, &c, content_x, cony,
  711. cx, cy, cw, ch);
  712. } else {
  713. fill_clip(content_x, cony, content_w, content_h,
  714. 0x000000u, cx, cy, cw, ch);
  715. }
  716. }
  717. /* Front-to-back window order: popovers float above everything, then the
  718. * focused window, then the others in descending slot order. Painting and
  719. * hit-testing share it. */
  720. static unsigned int build_order(int *order)
  721. {
  722. unsigned int i, n = 0;
  723. for (i = 0; i < WM_MAX_WINDOWS; i++)
  724. if (wins[i].info.used && wins[i].info.popover)
  725. order[n++] = (int)i;
  726. for (i = 0; i < WM_MAX_WINDOWS; i++)
  727. if (wins[i].info.used && wins[i].info.focused &&
  728. !wins[i].info.popover)
  729. order[n++] = (int)i;
  730. for (i = WM_MAX_WINDOWS; i-- > 0;)
  731. if (wins[i].info.used && !wins[i].info.focused &&
  732. !wins[i].info.popover)
  733. order[n++] = (int)i;
  734. return n;
  735. }
  736. struct orect {
  737. int x, y;
  738. unsigned int w, h;
  739. };
  740. #define OCC_MAX 64
  741. /* r minus cut, split into up to four disjoint rectangles. */
  742. static unsigned int rect_sub(struct orect *out, const struct orect *r,
  743. const struct orect *cut)
  744. {
  745. int rx0 = r->x, ry0 = r->y;
  746. int rx1 = r->x + (int)r->w, ry1 = r->y + (int)r->h;
  747. int cx0 = cut->x, cy0 = cut->y;
  748. int cx1 = cut->x + (int)cut->w, cy1 = cut->y + (int)cut->h;
  749. unsigned int n = 0;
  750. if (cx0 < rx0) cx0 = rx0;
  751. if (cy0 < ry0) cy0 = ry0;
  752. if (cx1 > rx1) cx1 = rx1;
  753. if (cy1 > ry1) cy1 = ry1;
  754. if (cx1 <= cx0 || cy1 <= cy0) {
  755. out[0] = *r;
  756. return 1;
  757. }
  758. if (cy0 > ry0) {
  759. out[n].x = rx0;
  760. out[n].y = ry0;
  761. out[n].w = (unsigned int)(rx1 - rx0);
  762. out[n].h = (unsigned int)(cy0 - ry0);
  763. n++;
  764. }
  765. if (cy1 < ry1) {
  766. out[n].x = rx0;
  767. out[n].y = cy1;
  768. out[n].w = (unsigned int)(rx1 - rx0);
  769. out[n].h = (unsigned int)(ry1 - cy1);
  770. n++;
  771. }
  772. if (cx0 > rx0) {
  773. out[n].x = rx0;
  774. out[n].y = cy0;
  775. out[n].w = (unsigned int)(cx0 - rx0);
  776. out[n].h = (unsigned int)(cy1 - cy0);
  777. n++;
  778. }
  779. if (cx1 < rx1) {
  780. out[n].x = cx1;
  781. out[n].y = cy0;
  782. out[n].w = (unsigned int)(rx1 - cx1);
  783. out[n].h = (unsigned int)(cy1 - cy0);
  784. n++;
  785. }
  786. return n;
  787. }
  788. static void wall_build_maps(void)
  789. {
  790. unsigned int i;
  791. if (screen.bpp == 32) {
  792. for (i = 0; i < 65536; i++) {
  793. unsigned int r = (i >> 11) & 31;
  794. unsigned int g = (i >> 5) & 63;
  795. unsigned int b = i & 31;
  796. wall_rgb32[i] = 0xFF000000u |
  797. ((r << 3 | r >> 2) << 16) |
  798. ((g << 2 | g >> 4) << 8) |
  799. (b << 3 | b >> 2);
  800. }
  801. }
  802. for (i = 0; i < WALL_MAP_MAX; i++) {
  803. unsigned int sx = 0, sy = 0;
  804. if (fbw && i < fbw)
  805. sx = (unsigned int)((unsigned long long)i * wall_w / fbw);
  806. if (fbh && i < fbh)
  807. sy = (unsigned int)((unsigned long long)i * wall_h / fbh);
  808. if (sx >= wall_w)
  809. sx = wall_w - 1;
  810. if (sy >= wall_h)
  811. sy = wall_h - 1;
  812. wall_xmap[i] = (unsigned short)sx;
  813. wall_ymap[i] = (unsigned short)sy;
  814. }
  815. }
  816. static int wall_magic_ok(const struct wm_wallpaper_hdr *h)
  817. {
  818. unsigned int i;
  819. for (i = 0; i < sizeof(h->magic); i++)
  820. if (h->magic[i] != WM_WALLPAPER_MAGIC[i])
  821. return 0;
  822. return 1;
  823. }
  824. /* Read the persistent copy first, then the RAM copy. The FAT driver may
  825. * write a final partial sector past the requested length, so wall_data has
  826. * 512 bytes of slack and the read is capped one sector below its size. */
  827. static void wall_load(void)
  828. {
  829. static const char *const paths[] = {
  830. WM_WALLPAPER_DATA, WM_WALLPAPER_SHARE
  831. };
  832. struct wm_wallpaper_hdr *h = (struct wm_wallpaper_hdr *)wall_data;
  833. unsigned int i, pixbytes;
  834. int rn;
  835. wall_ready = 0;
  836. for (i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) {
  837. rn = u_syscall3(SYS_READ_FILE, (int)paths[i], (int)wall_data,
  838. (int)(sizeof(wall_data) - 512));
  839. if (rn < (int)sizeof(*h) || !wall_magic_ok(h))
  840. continue;
  841. if (!h->width || !h->height ||
  842. h->width > WM_WALLPAPER_MAX_W ||
  843. h->height > WM_WALLPAPER_MAX_H)
  844. continue;
  845. pixbytes = h->width * h->height * 2u;
  846. if ((unsigned int)rn < sizeof(*h) + pixbytes)
  847. continue;
  848. wall_w = h->width;
  849. wall_h = h->height;
  850. wall_ready = 1;
  851. wall_build_maps();
  852. return;
  853. }
  854. }
  855. /* Paint the desktop part of one damage rectangle. */
  856. static void wall_draw(int x, int y, unsigned int w, unsigned int h)
  857. {
  858. const unsigned short *src = (const unsigned short *)(wall_data +
  859. sizeof(struct wm_wallpaper_hdr));
  860. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  861. unsigned int yy, xx;
  862. if (!wall_ready) {
  863. usurf_fill(&screen, x, y, w, h, COL_BG);
  864. return;
  865. }
  866. if (x0 < 0) x0 = 0;
  867. if (y0 < 0) y0 = 0;
  868. if (x1 > (int)fbw) x1 = (int)fbw;
  869. if (y1 > (int)fbh) y1 = (int)fbh;
  870. if (x1 > WALL_MAP_MAX) x1 = WALL_MAP_MAX;
  871. if (y1 > WALL_MAP_MAX) y1 = WALL_MAP_MAX;
  872. if (x1 <= x0 || y1 <= y0)
  873. return;
  874. usurf_pixels += (unsigned int)(x1 - x0) * (unsigned int)(y1 - y0);
  875. for (yy = (unsigned int)y0; yy < (unsigned int)y1; yy++) {
  876. const unsigned short *srow = src + (unsigned int)wall_ymap[yy] *
  877. wall_w;
  878. /* d points at the start of the row; the loops index it with
  879. * absolute screen x. Pointing d at x0 and still indexing with
  880. * x shifted every damage rect's wallpaper right by x0, which
  881. * left old window pixels in the uncovered strip and made
  882. * dragging windows leave a trail. */
  883. if (screen.bpp == 16) {
  884. unsigned short *d = (unsigned short *)(screen.px +
  885. (unsigned long)yy * screen.pitch);
  886. for (xx = (unsigned int)x0; xx < (unsigned int)x1; xx++)
  887. d[xx] = srow[wall_xmap[xx]];
  888. } else if (screen.bpp == 32) {
  889. unsigned int *d = (unsigned int *)(screen.px +
  890. (unsigned long)yy * screen.pitch);
  891. for (xx = (unsigned int)x0; xx < (unsigned int)x1; xx++)
  892. d[xx] = wall_rgb32[srow[wall_xmap[xx]]];
  893. } else {
  894. unsigned char *d = screen.px +
  895. (unsigned long)yy * screen.pitch + x0 * 3;
  896. for (xx = (unsigned int)x0; xx < (unsigned int)x1; xx++) {
  897. unsigned int c = srow[wall_xmap[xx]];
  898. unsigned int r = (c >> 11) & 31;
  899. unsigned int g = (c >> 5) & 63;
  900. unsigned int b = c & 31;
  901. d[0] = (unsigned char)(b << 3 | b >> 2);
  902. d[1] = (unsigned char)(g << 2 | g >> 4);
  903. d[2] = (unsigned char)(r << 3 | r >> 2);
  904. d += 3;
  905. }
  906. }
  907. }
  908. }
  909. /* Back-to-front repaint, used only if the visible-region list overflows. */
  910. static void draw_rect_naive(int x, int y, unsigned int w, unsigned int h)
  911. {
  912. unsigned int i;
  913. wall_draw(x, y, w, h);
  914. for (i = 0; i < WM_MAX_WINDOWS; i++)
  915. if (wins[i].info.used && !wins[i].info.focused &&
  916. !wins[i].info.popover &&
  917. x < wins[i].info.x + (int)wins[i].info.w &&
  918. x + (int)w > wins[i].info.x &&
  919. y < wins[i].info.y + (int)wins[i].info.h &&
  920. y + (int)h > wins[i].info.y)
  921. draw_window_clip(i, x, y, w, h);
  922. for (i = 0; i < WM_MAX_WINDOWS; i++)
  923. if (wins[i].info.used && wins[i].info.focused &&
  924. !wins[i].info.popover &&
  925. x < wins[i].info.x + (int)wins[i].info.w &&
  926. x + (int)w > wins[i].info.x &&
  927. y < wins[i].info.y + (int)wins[i].info.h &&
  928. y + (int)h > wins[i].info.y)
  929. draw_window_clip(i, x, y, w, h);
  930. for (i = 0; i < WM_MAX_WINDOWS; i++)
  931. if (wins[i].info.used && wins[i].info.popover &&
  932. x < wins[i].info.x + (int)wins[i].info.w &&
  933. x + (int)w > wins[i].info.x &&
  934. y < wins[i].info.y + (int)wins[i].info.h &&
  935. y + (int)h > wins[i].info.y)
  936. draw_window_clip(i, x, y, w, h);
  937. }
  938. static void draw_rect(int x, int y, unsigned int w, unsigned int h)
  939. {
  940. static struct orect cur[OCC_MAX], nxt[OCC_MAX];
  941. struct orect dmg;
  942. int order[WM_MAX_WINDOWS];
  943. unsigned int n, i, j, ncur, nnext;
  944. int overflow = 0;
  945. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  946. if (w == 0 || h == 0)
  947. return;
  948. if (x0 < 0) x0 = 0;
  949. if (y0 < 0) y0 = 0;
  950. if (x1 > (int)fbw) x1 = (int)fbw;
  951. if (y1 > (int)fbh) y1 = (int)fbh;
  952. if (x1 <= x0 || y1 <= y0)
  953. return;
  954. dmg.x = x0;
  955. dmg.y = y0;
  956. dmg.w = (unsigned int)(x1 - x0);
  957. dmg.h = (unsigned int)(y1 - y0);
  958. wall_draw(dmg.x, dmg.y, dmg.w, dmg.h);
  959. n = build_order(order);
  960. for (i = 0; i < n; i++) {
  961. struct uwin *u = &wins[order[i]];
  962. int wx0 = u->info.x, wy0 = u->info.y;
  963. int wx1 = wx0 + (int)u->info.w, wy1 = wy0 + (int)u->info.h;
  964. if (wx0 < dmg.x) wx0 = dmg.x;
  965. if (wy0 < dmg.y) wy0 = dmg.y;
  966. if (wx1 > x1) wx1 = x1;
  967. if (wy1 > y1) wy1 = y1;
  968. if (wx1 <= wx0 || wy1 <= wy0)
  969. continue;
  970. cur[0].x = wx0;
  971. cur[0].y = wy0;
  972. cur[0].w = (unsigned int)(wx1 - wx0);
  973. cur[0].h = (unsigned int)(wy1 - wy0);
  974. ncur = 1;
  975. for (j = 0; j < i && ncur; j++) {
  976. struct uwin *f = &wins[order[j]];
  977. struct orect fr;
  978. unsigned int k;
  979. fr.x = f->info.x;
  980. fr.y = f->info.y;
  981. fr.w = f->info.w;
  982. fr.h = f->info.h;
  983. nnext = 0;
  984. for (k = 0; k < ncur; k++) {
  985. if (nnext + 4 > OCC_MAX) {
  986. overflow = 1;
  987. break;
  988. }
  989. nnext += rect_sub(&nxt[nnext], &cur[k], &fr);
  990. }
  991. if (overflow)
  992. break;
  993. for (k = 0; k < nnext; k++)
  994. cur[k] = nxt[k];
  995. ncur = nnext;
  996. }
  997. if (overflow)
  998. break;
  999. for (j = 0; j < ncur; j++)
  1000. draw_window_clip((unsigned int)order[i], cur[j].x,
  1001. cur[j].y, cur[j].w, cur[j].h);
  1002. }
  1003. if (overflow)
  1004. draw_rect_naive(dmg.x, dmg.y, dmg.w, dmg.h);
  1005. }
  1006. /* Invisible resize border: edge bits for a point in the band around the
  1007. * window's outer edge (WM_RESIZE_IN px inside and WM_RESIZE_OUT px outside).
  1008. * Popovers have no chrome and never resize. */
  1009. #define WM_EDGE_LEFT 1
  1010. #define WM_EDGE_RIGHT 2
  1011. #define WM_EDGE_TOP 4
  1012. #define WM_EDGE_BOTTOM 8
  1013. static int resize_edges_at(const struct uwin *u, int mx, int my)
  1014. {
  1015. const struct wm_window_info *w = &u->info;
  1016. int l = w->x, t = w->y;
  1017. int r = l + (int)w->w, b = t + (int)w->h;
  1018. int near_x, near_y, e = 0;
  1019. if (w->popover)
  1020. return 0;
  1021. near_x = mx >= l - WM_RESIZE_OUT && mx < r + WM_RESIZE_OUT;
  1022. near_y = my >= t - WM_RESIZE_OUT && my < b + WM_RESIZE_OUT;
  1023. if (near_y && mx >= l - WM_RESIZE_OUT && mx < l + WM_RESIZE_IN)
  1024. e |= WM_EDGE_LEFT;
  1025. if (near_y && mx >= r - WM_RESIZE_IN && mx < r + WM_RESIZE_OUT)
  1026. e |= WM_EDGE_RIGHT;
  1027. if (near_x && my >= t - WM_RESIZE_OUT && my < t + WM_RESIZE_IN)
  1028. e |= WM_EDGE_TOP;
  1029. if (near_x && my >= b - WM_RESIZE_IN && my < b + WM_RESIZE_OUT)
  1030. e |= WM_EDGE_BOTTOM;
  1031. return e;
  1032. }
  1033. /* Topmost window at (mx,my): its resize band when the point is inside one,
  1034. * otherwise the window rectangle. *edges is 0 for a plain rectangle hit.
  1035. * Bands are not considered inside the top bar, which the WM owns. */
  1036. static int resize_hit(int mx, int my, int *edges)
  1037. {
  1038. int order[WM_MAX_WINDOWS];
  1039. unsigned int n = build_order(order), i;
  1040. *edges = 0;
  1041. for (i = 0; i < n; i++) {
  1042. struct uwin *u = &wins[order[i]];
  1043. int e = 0;
  1044. if (!u->info.used)
  1045. continue;
  1046. if (my >= WM_BAR_H)
  1047. e = resize_edges_at(u, mx, my);
  1048. if (e) {
  1049. *edges = e;
  1050. return order[i];
  1051. }
  1052. if (mx >= u->info.x && my >= u->info.y &&
  1053. mx < u->info.x + (int)u->info.w &&
  1054. my < u->info.y + (int)u->info.h)
  1055. return order[i];
  1056. }
  1057. return -1;
  1058. }
  1059. static int resize_cursor(int edges)
  1060. {
  1061. switch (edges) {
  1062. case WM_EDGE_LEFT:
  1063. case WM_EDGE_RIGHT:
  1064. return CURSOR_EW_RESIZE;
  1065. case WM_EDGE_TOP:
  1066. case WM_EDGE_BOTTOM:
  1067. return CURSOR_NS_RESIZE;
  1068. case WM_EDGE_LEFT | WM_EDGE_TOP:
  1069. case WM_EDGE_RIGHT | WM_EDGE_BOTTOM:
  1070. return CURSOR_NWSE_RESIZE;
  1071. case WM_EDGE_RIGHT | WM_EDGE_TOP:
  1072. case WM_EDGE_LEFT | WM_EDGE_BOTTOM:
  1073. return CURSOR_NESW_RESIZE;
  1074. default:
  1075. return CURSOR_DEFAULT;
  1076. }
  1077. }
  1078. /* Close button hit area (the top-right WM_CLOSE_W columns of the title bar). */
  1079. static int close_hit(const struct wm_window_info *w, int mx, int my)
  1080. {
  1081. if (w->popover)
  1082. return 0;
  1083. return mx >= w->x + (int)w->w - WM_CLOSE_W &&
  1084. mx < w->x + (int)w->w &&
  1085. my >= w->y && my < w->y + WM_TITLE_H;
  1086. }
  1087. static void clamp_geom(struct uwin *u, int *x, int *y)
  1088. {
  1089. /* Match wm_clamp_window(): allow the window to hang off the left,
  1090. * right and bottom edges but keep the title bar reachable and below
  1091. * the system bar. */
  1092. int minx = WM_DRAG_VISIBLE - (int)u->info.w;
  1093. int maxx = (int)fbw - WM_DRAG_VISIBLE;
  1094. int miny = WM_BAR_H;
  1095. int maxy = (int)fbh - WM_BORDER_W - WM_TITLE_H;
  1096. if (minx > maxx)
  1097. minx = maxx = 0;
  1098. if (miny > maxy)
  1099. miny = maxy = WM_BAR_H;
  1100. if (*x < minx)
  1101. *x = minx;
  1102. else if (*x > maxx)
  1103. *x = maxx;
  1104. if (*y < miny)
  1105. *y = miny;
  1106. else if (*y > maxy)
  1107. *y = maxy;
  1108. }
  1109. static void add_rect(struct wm_present_info *p, int x, int y,
  1110. unsigned int w, unsigned int h)
  1111. {
  1112. if (w == 0 || h == 0)
  1113. return;
  1114. if (p->count < 4) {
  1115. p->rect[p->count].x = x;
  1116. p->rect[p->count].y = y;
  1117. p->rect[p->count].w = w;
  1118. p->rect[p->count].h = h;
  1119. p->count++;
  1120. } else {
  1121. /* Four is enough for normal updates; fold the rest into the
  1122. * last rect rather than dropping pixels. */
  1123. int x0 = p->rect[3].x, y0 = p->rect[3].y;
  1124. int x1 = x0 + (int)p->rect[3].w, y1 = y0 + (int)p->rect[3].h;
  1125. int nx1 = x + (int)w, ny1 = y + (int)h;
  1126. if (x < x0) x0 = x;
  1127. if (y < y0) y0 = y;
  1128. if (nx1 > x1) x1 = nx1;
  1129. if (ny1 > y1) y1 = ny1;
  1130. p->rect[3].x = x0;
  1131. p->rect[3].y = y0;
  1132. p->rect[3].w = (unsigned int)(x1 - x0);
  1133. p->rect[3].h = (unsigned int)(y1 - y0);
  1134. }
  1135. }
  1136. /* Center the brightness bubble in the work area, clamped on small screens. */
  1137. static void osd_geom(void)
  1138. {
  1139. int x, y;
  1140. osd_w = OSD_W;
  1141. osd_h = OSD_H;
  1142. x = ((int)fbw - (int)osd_w) / 2;
  1143. y = WM_BAR_H + ((int)fbh - WM_BAR_H - (int)osd_h) * 3 / 5;
  1144. if (x < 0)
  1145. x = 0;
  1146. if (x + (int)osd_w > (int)fbw)
  1147. x = (int)fbw - (int)osd_w;
  1148. if (y < WM_BAR_H)
  1149. y = WM_BAR_H;
  1150. if (y + (int)osd_h > (int)fbh)
  1151. y = (int)fbh - (int)osd_h;
  1152. osd_x = x;
  1153. osd_y = y;
  1154. }
  1155. /* A small sun: a filled circle plus four rays, all from 1-pixel fills. */
  1156. static void osd_sun(int cx, int cy)
  1157. {
  1158. static const unsigned char half[11] = { 2, 4, 5, 6, 6, 6, 6, 6, 5, 4, 2 };
  1159. int i;
  1160. for (i = -5; i <= 5; i++)
  1161. usurf_fill(&screen, cx - half[i + 5], cy + i,
  1162. half[i + 5] * 2 + 1, 1, 0xFFD250u);
  1163. usurf_fill(&screen, cx - 1, cy - 9, 2, 3, 0xFFD250u);
  1164. usurf_fill(&screen, cx - 1, cy + 7, 2, 3, 0xFFD250u);
  1165. usurf_fill(&screen, cx - 9, cy - 1, 3, 2, 0xFFD250u);
  1166. usurf_fill(&screen, cx + 7, cy - 1, 3, 2, 0xFFD250u);
  1167. }
  1168. static void osd_draw(void)
  1169. {
  1170. char pct[8];
  1171. unsigned int n = 0;
  1172. unsigned int bar_x, bar_y, bar_w;
  1173. int x, y, tw;
  1174. osd_geom();
  1175. x = osd_x;
  1176. y = osd_y;
  1177. usurf_fill(&screen, x, y, osd_w, osd_h, 0x26263Au);
  1178. usurf_fill(&screen, x, y, osd_w, 1, 0x70709Au);
  1179. usurf_fill(&screen, x, y + (int)osd_h - 1, osd_w, 1, 0x70709Au);
  1180. usurf_fill(&screen, x, y + 1, 1, osd_h - 2, 0x70709Au);
  1181. usurf_fill(&screen, x + (int)osd_w - 1, y + 1, 1, osd_h - 2,
  1182. 0x70709Au);
  1183. osd_sun(x + 22, y + (int)osd_h / 2);
  1184. if (osd_level >= 100) {
  1185. pct[n++] = '1';
  1186. pct[n++] = '0';
  1187. pct[n++] = '0';
  1188. } else {
  1189. if (osd_level >= 10)
  1190. pct[n++] = (char)('0' + osd_level / 10);
  1191. pct[n++] = (char)('0' + osd_level % 10);
  1192. }
  1193. pct[n++] = '%';
  1194. pct[n] = '\0';
  1195. usurf_text(&screen, x + 42, y + 9, "Brightness", 0xE8E8F6u,
  1196. x, y, osd_w, osd_h);
  1197. tw = (int)usurf_text_width(&screen, pct);
  1198. usurf_text(&screen, x + (int)osd_w - 14 - tw, y + 9, pct,
  1199. 0xFFD250u, x, y, osd_w, osd_h);
  1200. bar_x = (unsigned int)(x + 42);
  1201. bar_y = (unsigned int)(y + 35);
  1202. bar_w = osd_w - 42 - 14;
  1203. usurf_fill(&screen, (int)bar_x, (int)bar_y, bar_w, 10, 0x44445Au);
  1204. usurf_fill(&screen, (int)bar_x, (int)bar_y,
  1205. (bar_w * (unsigned int)osd_level + 50) / 100, 10,
  1206. 0xFFD250u);
  1207. }
  1208. /* Put the brightness OSD and the start menu back on top after a render pass
  1209. * that may have overwritten them. */
  1210. static void wm_overlays(struct wm_present_info *p)
  1211. {
  1212. if (osd_show) {
  1213. osd_draw();
  1214. add_rect(p, osd_x, osd_y, osd_w, osd_h);
  1215. }
  1216. if (sm_open) {
  1217. sm_draw();
  1218. sm_add_present(p);
  1219. }
  1220. }
  1221. /* A focused window is topmost, so the shadow contains exactly its pixels in
  1222. * the old rectangle. Move that block and repaint only the old part the new
  1223. * rectangle does not cover. When either rectangle is partly off-screen the
  1224. * shadow has no valid pixels for the hidden part, so repaint the new rect
  1225. * instead of copying desktop background into the window. */
  1226. static void move_window_pixels(struct uwin *u, int ox, int oy, int nx, int ny)
  1227. {
  1228. struct wm_present_info present;
  1229. struct orect oldr, newr, pieces[4];
  1230. unsigned int np, k;
  1231. int w = (int)u->info.w, h = (int)u->info.h;
  1232. int old_on = ox >= 0 && oy >= 0 &&
  1233. ox + w <= (int)fbw && oy + h <= (int)fbh;
  1234. int new_on = nx >= 0 && ny >= 0 &&
  1235. nx + w <= (int)fbw && ny + h <= (int)fbh;
  1236. if (u_wm_begin() != 0)
  1237. return;
  1238. remap_window_contents();
  1239. if (old_on && new_on)
  1240. usurf_copy(&screen, nx, ny, ox, oy, u->info.w, u->info.h);
  1241. else
  1242. draw_rect(nx, ny, u->info.w, u->info.h);
  1243. oldr.x = ox;
  1244. oldr.y = oy;
  1245. oldr.w = u->info.w;
  1246. oldr.h = u->info.h;
  1247. newr.x = nx;
  1248. newr.y = ny;
  1249. newr.w = u->info.w;
  1250. newr.h = u->info.h;
  1251. np = rect_sub(pieces, &oldr, &newr);
  1252. for (k = 0; k < np; k++)
  1253. draw_rect(pieces[k].x, pieces[k].y, pieces[k].w, pieces[k].h);
  1254. present.count = 0;
  1255. present.immediate = 1;
  1256. /* Present the union of the old and new rectangles as one rect.
  1257. * The shadow is correct for all of it, and one rect cannot be cut
  1258. * off by the kernel's four-entry present list. */
  1259. {
  1260. int ux0 = ox < nx ? ox : nx;
  1261. int uy0 = oy < ny ? oy : ny;
  1262. int ux1 = (ox > nx ? ox : nx) + (int)u->info.w;
  1263. int uy1 = (oy > ny ? oy : ny) + (int)u->info.h;
  1264. add_rect(&present, ux0, uy0, (unsigned int)(ux1 - ux0),
  1265. (unsigned int)(uy1 - uy0));
  1266. }
  1267. wm_overlays(&present);
  1268. u_wm_present(&present);
  1269. }
  1270. static int iabs(int v)
  1271. {
  1272. return v < 0 ? -v : v;
  1273. }
  1274. /* Repaint the old and new rectangles of a geometry change. */
  1275. static void redraw_geom(struct uwin *u, int ox, int oy,
  1276. unsigned int ow, unsigned int oh)
  1277. {
  1278. struct wm_present_info present;
  1279. unsigned int i;
  1280. present.count = 0;
  1281. present.immediate = 1;
  1282. add_rect(&present, ox, oy, ow, oh);
  1283. add_rect(&present, u->info.x, u->info.y, u->info.w, u->info.h);
  1284. if (u_wm_begin() == 0) {
  1285. remap_window_contents();
  1286. for (i = 0; i < present.count; i++)
  1287. draw_rect(present.rect[i].x, present.rect[i].y,
  1288. present.rect[i].w, present.rect[i].h);
  1289. wm_overlays(&present);
  1290. u_wm_present(&present);
  1291. }
  1292. }
  1293. /* Resize (when needed), move and remap a window in one step. */
  1294. static int set_window_geometry(struct uwin *u, unsigned int idx,
  1295. int nx, int ny, unsigned int nw, unsigned int nh)
  1296. {
  1297. struct wm_window_info ni;
  1298. int ox = u->info.x, oy = u->info.y;
  1299. unsigned int ow = u->info.w, oh = u->info.h;
  1300. if ((int)nw != (int)ow || (int)nh != (int)oh) {
  1301. if (u_wm_resize(idx, nw - 2 * WM_BORDER_W,
  1302. nh - WM_TITLE_H - 2 * WM_BORDER_W) != 0)
  1303. return -1;
  1304. }
  1305. if (nx != ox || ny != oy)
  1306. u_wm_set_geom(idx, nx, ny);
  1307. u->mapped = 0;
  1308. if (u_wm_info(idx, &ni) == 1)
  1309. u->info = ni;
  1310. if (u_wm_map(idx, &u->map) == 0)
  1311. u->mapped = 1;
  1312. redraw_geom(u, ox, oy, ow, oh);
  1313. return 0;
  1314. }
  1315. static int restore_window(struct uwin *u, unsigned int idx)
  1316. {
  1317. if (u->state == WIN_NORMAL)
  1318. return 0;
  1319. if (set_window_geometry(u, idx, u->restore_x, u->restore_y,
  1320. u->restore_w, u->restore_h) != 0)
  1321. return -1;
  1322. u->state = WIN_NORMAL;
  1323. return 0;
  1324. }
  1325. /* Maximize or snap a window to a half of the work area below the system
  1326. * bar. The restore geometry is recorded only when the window leaves the
  1327. * normal state, so maximize -> snap -> restore returns to the original. */
  1328. static int place_window(struct uwin *u, unsigned int idx, int state)
  1329. {
  1330. unsigned int half = fbw / 2;
  1331. unsigned int h = fbh - WM_BAR_H;
  1332. int x = 0;
  1333. unsigned int w = fbw;
  1334. if (state == u->state)
  1335. return 0;
  1336. if (state == WIN_SNAP_LEFT) {
  1337. w = half;
  1338. } else if (state == WIN_SNAP_RIGHT) {
  1339. x = (int)half;
  1340. w = fbw - half;
  1341. }
  1342. if (u->state == WIN_NORMAL) {
  1343. u->restore_x = u->info.x;
  1344. u->restore_y = u->info.y;
  1345. u->restore_w = u->info.w;
  1346. u->restore_h = u->info.h;
  1347. }
  1348. if (set_window_geometry(u, idx, x, WM_BAR_H, w, h) != 0)
  1349. return -1;
  1350. u->state = state;
  1351. return 0;
  1352. }
  1353. /* Double-click title bar: fill the work area below the system bar, or go back
  1354. * to the geometry the window had before it was maximized. A snapped window
  1355. * maximizes (keeping its pre-snap restore geometry). */
  1356. static int toggle_maximize(struct uwin *u, unsigned int idx)
  1357. {
  1358. if (u->state == WIN_MAXIMIZED)
  1359. return restore_window(u, idx);
  1360. return place_window(u, idx, WIN_MAXIMIZED);
  1361. }
  1362. /* One axis of edge resistance. lo/len is the window's span on that axis
  1363. * before the move, *pos the proposed start. dir < 0: the window's start side
  1364. * (left/top) resists edge e while moving towards lower coordinates; dir > 0:
  1365. * its end side (right/bottom) while moving higher. Within WM_RESIST pixels
  1366. * of overshoot the window stays on the edge; *best keeps the nearest edge. */
  1367. static void resist_edge(int lo, int len, int *pos, int e, int dir, int *best)
  1368. {
  1369. int over;
  1370. if (dir < 0 && *pos < lo && lo >= e && *pos < e) {
  1371. over = e - *pos;
  1372. if (over <= WM_RESIST && over < *best) {
  1373. *best = over;
  1374. *pos = e;
  1375. }
  1376. } else if (dir > 0 && *pos > lo && lo + len <= e && *pos + len > e) {
  1377. over = *pos + len - e;
  1378. if (over <= WM_RESIST && over < *best) {
  1379. *best = over;
  1380. *pos = e - len;
  1381. }
  1382. }
  1383. }
  1384. /* Span overlap test: edges of another window only resist when the two
  1385. * windows share some extent on the other axis (they would actually touch). */
  1386. static int spans_overlap(int a, int alen, int b, int blen)
  1387. {
  1388. return a < b + blen && b < a + alen;
  1389. }
  1390. /* Apply edge resistance to a drag from (u->info.x, u->info.y) to (*nx, *ny):
  1391. * screen edges and the facing edges of every other window. The proposed
  1392. * position is still pure pointer arithmetic, so once the overshoot passes
  1393. * WM_RESIST the window jumps to follow the pointer again. */
  1394. static void resist_geom(struct uwin *u, int idx, int *nx, int *ny)
  1395. {
  1396. int ox = u->info.x, oy = u->info.y;
  1397. int w = (int)u->info.w, h = (int)u->info.h;
  1398. int px = *nx, py = *ny;
  1399. int bestx = WM_RESIST + 1, besty = WM_RESIST + 1;
  1400. int i;
  1401. resist_edge(ox, w, &px, 0, -1, &bestx);
  1402. resist_edge(ox, w, &px, (int)fbw, 1, &bestx);
  1403. resist_edge(oy, h, &py, (int)fbh, 1, &besty);
  1404. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1405. struct wm_window_info *o = &wins[i].info;
  1406. int t;
  1407. if (i == idx || !o->used)
  1408. continue;
  1409. if (spans_overlap(oy, h, o->y, (int)o->h)) {
  1410. /* our left side against its right, our right
  1411. * side against its left */
  1412. t = *nx;
  1413. resist_edge(ox, w, &t, o->x + (int)o->w, -1, &bestx);
  1414. resist_edge(ox, w, &t, o->x, 1, &bestx);
  1415. if (t != *nx)
  1416. px = t;
  1417. }
  1418. if (spans_overlap(ox, w, o->x, (int)o->w)) {
  1419. t = *ny;
  1420. resist_edge(oy, h, &t, o->y + (int)o->h, -1, &besty);
  1421. resist_edge(oy, h, &t, o->y, 1, &besty);
  1422. if (t != *ny)
  1423. py = t;
  1424. }
  1425. }
  1426. *nx = px;
  1427. *ny = py;
  1428. }
  1429. /* The top-bar date/time text is also a button: it opens the Date & Time
  1430. * editor. The rectangle is recorded by draw_bar(). */
  1431. /* The whole right status cluster is one button: the clock, temperature,
  1432. * network and power items plus the gaps between them. Only the rectangle
  1433. * draw_bar() recorded is clickable, so the empty bar between the start
  1434. * button and the cluster does nothing. */
  1435. static int bar_status_hit(unsigned int mx, unsigned int my)
  1436. {
  1437. return bar_status_w && bar_status_x >= 0 &&
  1438. (int)my >= BAR_BTN_Y && (int)my < BAR_BTN_Y + BAR_BTN_H &&
  1439. (int)mx >= bar_status_x &&
  1440. (int)mx < bar_status_x + (int)bar_status_w;
  1441. }
  1442. static void sm_layout_get(struct sm_layout *l)
  1443. {
  1444. int avail = (int)fbh - WM_BAR_H - 8;
  1445. int row = SM_ROW_H;
  1446. int total = SM_TOP + SM_SECT_H + SM_NAPP * row + SM_GAP + row +
  1447. SM_BOTTOM;
  1448. /* Shrink the rows to the work area, so a 540-600 px panel still fits
  1449. * the whole list. The low bound is the smallest legible row. */
  1450. while (row > SM_MIN_ROW && total > avail) {
  1451. row--;
  1452. total = SM_TOP + SM_SECT_H + SM_NAPP * row + SM_GAP + row +
  1453. SM_BOTTOM;
  1454. }
  1455. l->x = 4;
  1456. l->y = WM_BAR_H + 2;
  1457. l->w = SM_W;
  1458. l->h = total;
  1459. l->row_h = row;
  1460. l->sect_y = l->y + SM_TOP;
  1461. l->row_x = l->x + SM_PAD;
  1462. l->row_w = l->w - 2 * SM_PAD;
  1463. l->list_y = l->sect_y + SM_SECT_H;
  1464. l->power_y = l->list_y + SM_NAPP * row + SM_GAP;
  1465. l->sub_w = SM_SUB_W;
  1466. l->sub_h = 2 * row + 8;
  1467. l->sub_x = l->x + l->w + 4;
  1468. if (l->sub_x + l->sub_w > (int)fbw - 2) {
  1469. if (l->x - l->sub_w - 4 >= 0)
  1470. l->sub_x = l->x - l->sub_w - 4;
  1471. else
  1472. l->sub_x = (int)fbw - 2 - l->sub_w;
  1473. }
  1474. /* The power row is the last menu row: open the submenu upwards and
  1475. * align its bottom with that row. */
  1476. l->sub_y = l->power_y + row - l->sub_h;
  1477. if (l->sub_y < WM_BAR_H + 2)
  1478. l->sub_y = WM_BAR_H + 2;
  1479. if (l->sub_y + l->sub_h > (int)fbh - 2)
  1480. l->sub_y = (int)fbh - 2 - l->sub_h;
  1481. if (l->sub_y < 0)
  1482. l->sub_y = 0;
  1483. }
  1484. static void sm_tile(int x, int y, int size, unsigned int rgb, char letter)
  1485. {
  1486. char s[2];
  1487. int tw, tx, ty;
  1488. if (size < 12)
  1489. size = 12;
  1490. s[0] = letter;
  1491. s[1] = '\0';
  1492. sm_fill_round(x, y, size, size, 4, rgb);
  1493. tw = (int)usurf_text_width(&screen, s);
  1494. tx = x + (size - tw) / 2;
  1495. ty = y + (size - 16) / 2;
  1496. usurf_text(&screen, tx, ty, s, COL_WHITE,
  1497. x, y, (unsigned int)size, (unsigned int)size);
  1498. }
  1499. static void sm_draw_app(int i, const struct sm_layout *l)
  1500. {
  1501. const struct sm_app *a = &sm_apps[i];
  1502. int ry = l->list_y + i * l->row_h;
  1503. int ty = ry + (l->row_h - 16) / 2;
  1504. int tile = l->row_h - 8;
  1505. if (tile > SM_TILE)
  1506. tile = SM_TILE;
  1507. if (sm_hot == i)
  1508. sm_fill_round(l->row_x, ry + 1, l->row_w,
  1509. l->row_h - 2, SM_RADIUS, SM_PANEL_ALT);
  1510. sm_tile(l->row_x + 10, ry + (l->row_h - tile) / 2, tile,
  1511. a->tile, a->letter);
  1512. usurf_text(&screen, l->row_x + 36, ty, a->label, SM_TEXT,
  1513. l->x, l->y, (unsigned int)l->w, (unsigned int)l->h);
  1514. }
  1515. static void sm_draw(void)
  1516. {
  1517. struct sm_layout l;
  1518. int i, ty;
  1519. if (!sm_open)
  1520. return;
  1521. sm_layout_get(&l);
  1522. /* Flat theme.bg panel with no rim, exactly like the netmenu popover.
  1523. * The Applications caption is the first item: there is no separate
  1524. * banner card. */
  1525. usurf_fill(&screen, l.x, l.y, (unsigned int)l.w, (unsigned int)l.h,
  1526. SM_BG);
  1527. usurf_text(&screen, l.x + SM_PAD, l.sect_y + 3, "Applications",
  1528. SM_TEXT_DIM, l.x, l.y, (unsigned int)l.w,
  1529. (unsigned int)l.h);
  1530. for (i = 0; i < SM_NAPP; i++)
  1531. sm_draw_app(i, &l);
  1532. /* The Power row is a quick-settings tile rather than a plain list
  1533. * row, so the submenu entry reads as the panel's last action. */
  1534. sm_fill_round(l.row_x, l.power_y, l.row_w, l.row_h, SM_RADIUS,
  1535. sm_power_open ? SM_SEL :
  1536. (sm_power_hot == 1 ? SM_BTN_HOT : SM_PANEL_ALT));
  1537. ty = l.power_y + (l.row_h - 16) / 2;
  1538. usurf_text(&screen, l.row_x + 12, ty, "Power",
  1539. sm_power_open || sm_power_hot == 1 ? COL_WHITE : SM_TEXT,
  1540. l.x, l.y, (unsigned int)l.w, (unsigned int)l.h);
  1541. {
  1542. const char *arrow = sm_power_open ? "v" : ">";
  1543. int aw = (int)usurf_text_width(&screen, arrow);
  1544. usurf_text(&screen, l.row_x + l.row_w - 12 - aw, ty, arrow,
  1545. sm_power_open || sm_power_hot == 1 ? COL_WHITE :
  1546. SM_TEXT_DIM,
  1547. l.x, l.y, (unsigned int)l.w, (unsigned int)l.h);
  1548. }
  1549. if (!sm_power_open)
  1550. return;
  1551. /* Submenu: a rounded panel card with two rounded selection rows. */
  1552. sm_fill_round(l.sub_x, l.sub_y, l.sub_w, l.sub_h, SM_RADIUS,
  1553. SM_PANEL);
  1554. for (i = 0; i < 2; i++) {
  1555. int sy = l.sub_y + 4 + i * l.row_h;
  1556. int sty = sy + (l.row_h - 16) / 2;
  1557. if (sm_power_hot == i + 2)
  1558. sm_fill_round(l.sub_x + 4, sy + 1, l.sub_w - 8,
  1559. l.row_h - 2, 4, SM_SEL);
  1560. usurf_text(&screen, l.sub_x + 12, sty,
  1561. i == 0 ? "Reboot" : "Power off",
  1562. i == 1 ? (sm_power_hot == 3 ? COL_WHITE : SM_ERROR)
  1563. : (sm_power_hot == 2 ? COL_WHITE : SM_TEXT),
  1564. l.sub_x, l.sub_y, (unsigned int)l.sub_w,
  1565. (unsigned int)l.sub_h);
  1566. }
  1567. }
  1568. static int sm_hit(unsigned int mx, unsigned int my)
  1569. {
  1570. struct sm_layout l;
  1571. int x = (int)mx, y = (int)my;
  1572. if (bar_start_hit(mx, my))
  1573. return SM_HIT_START;
  1574. if (!sm_open)
  1575. return SM_HIT_NONE;
  1576. sm_layout_get(&l);
  1577. if (sm_power_open &&
  1578. x >= l.sub_x && x < l.sub_x + l.sub_w &&
  1579. y >= l.sub_y && y < l.sub_y + l.sub_h) {
  1580. if (y >= l.sub_y + 4 && y < l.sub_y + 4 + 2 * l.row_h)
  1581. return (y - (l.sub_y + 4)) / l.row_h == 0 ?
  1582. SM_HIT_REBOOT : SM_HIT_POWEROFF;
  1583. return SM_HIT_MENU;
  1584. }
  1585. if (x < l.x || x >= l.x + l.w || y < l.y || y >= l.y + l.h)
  1586. return SM_HIT_NONE;
  1587. if (y >= l.list_y && y < l.list_y + SM_NAPP * l.row_h)
  1588. return (y - l.list_y) / l.row_h;
  1589. if (y >= l.power_y && y < l.power_y + l.row_h)
  1590. return SM_HIT_POWER;
  1591. return SM_HIT_MENU;
  1592. }
  1593. static int sm_update_hover(unsigned int mx, unsigned int my)
  1594. {
  1595. int hot = -1, pwr = 0, hit;
  1596. if (sm_open) {
  1597. hit = sm_hit(mx, my);
  1598. if (hit >= 0)
  1599. hot = hit;
  1600. else if (hit == SM_HIT_POWER)
  1601. pwr = 1;
  1602. else if (hit == SM_HIT_REBOOT)
  1603. pwr = 2;
  1604. else if (hit == SM_HIT_POWEROFF)
  1605. pwr = 3;
  1606. }
  1607. if (hot == sm_hot && pwr == sm_power_hot)
  1608. return 0;
  1609. sm_hot = hot;
  1610. sm_power_hot = pwr;
  1611. return 1;
  1612. }
  1613. static void sm_add_present(struct wm_present_info *p)
  1614. {
  1615. struct sm_layout l;
  1616. if (!sm_open)
  1617. return;
  1618. sm_layout_get(&l);
  1619. add_rect(p, l.x, l.y, (unsigned int)l.w, (unsigned int)l.h);
  1620. if (sm_power_open)
  1621. add_rect(p, l.sub_x, l.sub_y, (unsigned int)l.sub_w,
  1622. (unsigned int)l.sub_h);
  1623. }
  1624. static void sm_close(void)
  1625. {
  1626. if (!sm_open && !sm_power_open)
  1627. return;
  1628. sm_open = 0;
  1629. sm_power_open = 0;
  1630. sm_hot = -1;
  1631. sm_power_hot = 0;
  1632. full_redraw = 1;
  1633. }
  1634. /* A client can self-resize between the pre-frame info/map refresh and
  1635. * u_wm_begin(); that unmaps and remaps its content slot, leaving the map
  1636. * cached above pointing at released pages. Once the frame is active the
  1637. * kernel refuses further self-resizes, so re-reading the generation and
  1638. * remapping here makes the cached canvas safe for the draw calls. The
  1639. * window info itself is left for the next loop, which then sees the
  1640. * generation change and does a full redraw. */
  1641. static void remap_window_contents(void)
  1642. {
  1643. unsigned int i;
  1644. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1645. struct wm_window_info info;
  1646. struct wm_map_info map;
  1647. if (u_wm_info(i, &info) != 1 || !info.used) {
  1648. wins[i].mapped = 0;
  1649. continue;
  1650. }
  1651. if (wins[i].mapped &&
  1652. wins[i].map.generation == info.content_gen)
  1653. continue;
  1654. if (u_wm_map(i, &map) == 0) {
  1655. wins[i].map = map;
  1656. wins[i].mapped = 1;
  1657. } else {
  1658. wins[i].mapped = 0;
  1659. }
  1660. }
  1661. }
  1662. static void do_update(struct wm_status *st)
  1663. {
  1664. struct wm_present_info present;
  1665. int need = 0;
  1666. int bar_dirty = 0;
  1667. unsigned int i;
  1668. char dt[20];
  1669. static char last_dt[20];
  1670. int kexec_changed = (st->kexec_busy != bar_kexec_busy) ||
  1671. (st->kexec_busy && st->kexec_progress != bar_kexec_progress);
  1672. unsigned int msg_now = u_ticks_ms();
  1673. int osd_hide = 0, osd_repaint = 0;
  1674. int sm_dirty;
  1675. /* The kernel bumps backlight_seq on every brightness key/command.
  1676. * Pop the OSD on a change and keep it up for OSD_MS after the last
  1677. * one. The first poll only records the current sequence, so a WM
  1678. * restart does not flash a stale bubble. */
  1679. if (st->backlight_present) {
  1680. if (!osd_seen_valid) {
  1681. osd_seen_valid = 1;
  1682. osd_seen_seq = st->backlight_seq;
  1683. } else if (st->backlight_seq != osd_seen_seq) {
  1684. osd_seen_seq = st->backlight_seq;
  1685. osd_level = (int)st->backlight_level;
  1686. osd_until = msg_now + OSD_MS;
  1687. osd_show = 1;
  1688. }
  1689. } else if (osd_show) {
  1690. osd_show = 0;
  1691. osd_hide = 1;
  1692. }
  1693. if (osd_show && (int)(msg_now - osd_until) >= 0) {
  1694. osd_show = 0;
  1695. osd_hide = 1;
  1696. }
  1697. if (osd_hide && osd_visible)
  1698. osd_repaint = 1;
  1699. sm_dirty = sm_update_hover(st->mouse.x, st->mouse.y);
  1700. /* The boot-scan window is created before the terminal so the terminal
  1701. * stays the newest (last) window and keeps the keyboard focus. Wait
  1702. * for the scan window to exist before starting the terminal; a failed
  1703. * or wedged helper is bounded so the shell is never held back. */
  1704. if (st->boot_ready && !boot_term_spawned) {
  1705. if (!boot_scan_spawned) {
  1706. boot_scan_spawned = 1;
  1707. if (free_window_slot()) {
  1708. boot_scan_pid = u_spawn("/ramfs/bin/bootscan",
  1709. "", 0, "/");
  1710. if (boot_scan_pid > 0)
  1711. boot_scan_deadline = msg_now + 2000u;
  1712. }
  1713. if (boot_scan_pid <= 0)
  1714. boot_scan_placed = 1;
  1715. }
  1716. if (boot_scan_pid > 0 && !boot_scan_placed) {
  1717. struct wm_window_info wi;
  1718. int idx = window_index_pid(boot_scan_pid, &wi);
  1719. if (idx >= 0) {
  1720. int nx = (int)fbw - (int)wi.w - 16;
  1721. int ny = WM_BAR_H + 8;
  1722. if (nx < 8)
  1723. nx = 8;
  1724. u_wm_set_geom((unsigned int)idx, nx, ny);
  1725. boot_scan_placed = 1;
  1726. } else if (!u_alive((unsigned int)boot_scan_pid) ||
  1727. (int)(msg_now - boot_scan_deadline) >= 0) {
  1728. if (u_alive((unsigned int)boot_scan_pid))
  1729. u_kill((unsigned int)boot_scan_pid);
  1730. boot_scan_placed = 1;
  1731. }
  1732. }
  1733. if (boot_scan_placed) {
  1734. boot_term_spawned = 1;
  1735. if (!free_window_slot()) {
  1736. bar_notice("window limit");
  1737. full_redraw = 1;
  1738. } else if (u_wm_create_term() < 0) {
  1739. bar_notice("cannot start term");
  1740. full_redraw = 1;
  1741. } else {
  1742. full_redraw = 1;
  1743. }
  1744. }
  1745. }
  1746. if (bar_msg_dirty ||
  1747. (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)) {
  1748. bar_dirty = 1;
  1749. bar_msg_dirty = 0;
  1750. if (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)
  1751. bar_msg_until = 0;
  1752. }
  1753. bar_kexec_busy = st->kexec_busy;
  1754. bar_kexec_progress = st->kexec_progress;
  1755. bar_kexec_total = st->kexec_total;
  1756. if (kexec_changed)
  1757. bar_dirty = 1;
  1758. {
  1759. int hot = bar_start_hit(st->mouse.x, st->mouse.y);
  1760. if (hot != sm_start_hot) {
  1761. sm_start_hot = hot;
  1762. bar_dirty = 1;
  1763. }
  1764. }
  1765. {
  1766. int hot = bar_status_hit(st->mouse.x, st->mouse.y);
  1767. if (hot != bar_status_hot) {
  1768. bar_status_hot = hot;
  1769. bar_dirty = 1;
  1770. }
  1771. }
  1772. /* Refresh cached window state and mappings before opening a frame:
  1773. * MAP may unmap/remap content. */
  1774. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1775. struct wm_window_info info;
  1776. int r = u_wm_info(i, &info);
  1777. if (r < 0) {
  1778. u_exit(1);
  1779. }
  1780. if (r == 0 || !info.used) {
  1781. if (wins[i].info.used)
  1782. full_redraw = 1;
  1783. wins[i].info.used = 0;
  1784. wins[i].mapped = 0;
  1785. continue;
  1786. }
  1787. {
  1788. int pid_changed = wins[i].info.used &&
  1789. info.pid != wins[i].info.pid;
  1790. if (!wins[i].info.used ||
  1791. info.content_gen != wins[i].info.content_gen ||
  1792. pid_changed)
  1793. full_redraw = 1;
  1794. wins[i].info = info;
  1795. if (!wins[i].mapped || pid_changed ||
  1796. wins[i].map.generation != info.content_gen) {
  1797. struct wm_map_info map;
  1798. if (u_wm_map(i, &map) == 0) {
  1799. wins[i].map = map;
  1800. wins[i].mapped = 1;
  1801. } else {
  1802. wins[i].mapped = 0;
  1803. }
  1804. }
  1805. }
  1806. }
  1807. /* Popovers are WM-anchored overlays, flush against the bottom of the
  1808. * bar and the right screen edge. The WM owns the geometry, so the app
  1809. * does not need a placement ABI. */
  1810. {
  1811. int control_found = 0;
  1812. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1813. struct wm_window_info ni;
  1814. int nx, ny;
  1815. if (!wins[i].info.used || !wins[i].info.popover)
  1816. continue;
  1817. if (control_pid > 0 &&
  1818. wins[i].info.pid == (unsigned int)control_pid)
  1819. control_found = 1;
  1820. nx = (int)fbw - (int)wins[i].info.w;
  1821. ny = WM_BAR_H;
  1822. if (nx != wins[i].info.x || ny != wins[i].info.y) {
  1823. if (u_wm_set_geom(i, nx, ny) == 0 &&
  1824. u_wm_info(i, &ni) == 1) {
  1825. wins[i].info = ni;
  1826. full_redraw = 1;
  1827. }
  1828. }
  1829. }
  1830. if (!control_found && control_pid > 0 &&
  1831. !u_alive((unsigned int)control_pid)) {
  1832. control_pid = 0;
  1833. bar_dirty = 1;
  1834. }
  1835. }
  1836. /* The RTC is slow (CMOS polling) and the temperature/battery state only
  1837. * changes once a second; query them at 1 Hz instead of every 10 ms. */
  1838. {
  1839. static unsigned int last_rtc_ms;
  1840. unsigned int now_ms = u_ticks_ms();
  1841. if (full_redraw || now_ms - last_rtc_ms >= 1000) {
  1842. last_rtc_ms = now_ms;
  1843. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  1844. for (i = 0; i < sizeof(dt); i++)
  1845. if (dt[i] != last_dt[i]) {
  1846. for (i = 0; i < sizeof(dt); i++)
  1847. last_dt[i] = dt[i];
  1848. bar_dirty = 1;
  1849. break;
  1850. }
  1851. }
  1852. {
  1853. struct sys_cputemp ct;
  1854. if (u_cpu_temp(&ct) == 0 &&
  1855. ct.temp_c != bar_temp_c) {
  1856. bar_temp_c = ct.temp_c;
  1857. bar_dirty = 1;
  1858. }
  1859. }
  1860. {
  1861. struct sys_powerinfo pi;
  1862. int pct = -1, charging = 0, ac = -1;
  1863. unsigned int bi;
  1864. if (u_power_info(&pi, 0) == 0) {
  1865. /* A laptop EC (ThinkPad or Chrome
  1866. * EC) marks a machine with AC/battery;
  1867. * desktops show neither. */
  1868. if (pi.present) {
  1869. ac = pi.ac_online;
  1870. for (bi = 0; bi < SYS_POWER_BATT_MAX;
  1871. bi++) {
  1872. struct sys_battery *b =
  1873. &pi.battery[bi];
  1874. if (!b->present ||
  1875. !b->percent_valid)
  1876. continue;
  1877. pct = (int)b->percent;
  1878. charging = (int)b->charging;
  1879. break;
  1880. }
  1881. }
  1882. if (pct != bar_batt_pct ||
  1883. charging != bar_batt_charging ||
  1884. ac != bar_ac_online) {
  1885. bar_batt_pct = pct;
  1886. bar_batt_charging = charging;
  1887. bar_ac_online = ac;
  1888. bar_dirty = 1;
  1889. }
  1890. }
  1891. }
  1892. if (poll_net_status())
  1893. bar_dirty = 1;
  1894. }
  1895. }
  1896. if (full_redraw) {
  1897. if (u_wm_begin() != 0) {
  1898. full_redraw = 1;
  1899. return;
  1900. }
  1901. full_redraw = 0;
  1902. remap_window_contents();
  1903. need = 1;
  1904. {
  1905. draw_rect(0, 0, fbw, fbh);
  1906. draw_bar();
  1907. if (osd_show)
  1908. osd_draw();
  1909. osd_visible = osd_show;
  1910. sm_draw();
  1911. present.count = 0;
  1912. add_rect(&present, 0, 0, fbw, fbh);
  1913. present.immediate = 0;
  1914. u_wm_present(&present);
  1915. /* Ack every window that was clean at the time of the
  1916. * redraw. If new damage arrived during it, its seq
  1917. * differs and the kernel leaves it dirty. */
  1918. for (i = 0; i < WM_MAX_WINDOWS; i++)
  1919. if (wins[i].info.used && wins[i].mapped)
  1920. u_wm_ack(i, wins[i].info.dirty_seq);
  1921. }
  1922. return;
  1923. }
  1924. /* Partial cell/canvas updates and the bar clock. */
  1925. present.count = 0;
  1926. present.immediate = 0;
  1927. if (osd_repaint) {
  1928. add_rect(&present, osd_x, osd_y, osd_w, osd_h);
  1929. need = 1;
  1930. }
  1931. if (osd_show)
  1932. need = 1;
  1933. if (bar_dirty) {
  1934. need = 1;
  1935. add_rect(&present, 0, 0, fbw, WM_BAR_H);
  1936. }
  1937. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1938. struct uwin *u = &wins[i];
  1939. struct wm_window_info *w = &u->info;
  1940. int rx, ry;
  1941. unsigned int rw, rh;
  1942. if (!w->used || !w->dirty)
  1943. continue;
  1944. if (!w->dmg_w || !w->dmg_h)
  1945. continue;
  1946. rx = w->x + (w->popover ? 0 : WM_BORDER_W) + (int)w->dmg_x;
  1947. ry = w->y + (w->popover ? 0 : WM_BORDER_W + WM_TITLE_H) +
  1948. (int)w->dmg_y;
  1949. rw = w->dmg_w;
  1950. rh = w->dmg_h;
  1951. add_rect(&present, rx, ry, rw, rh);
  1952. u->ack_seq = u->mapped ? w->dirty_seq : 0;
  1953. need = 1;
  1954. }
  1955. if (sm_dirty && sm_open)
  1956. need = 1;
  1957. if (!need) {
  1958. /* Drag movement is handled below; a stationary mouse can be
  1959. * left to the kernel's cursor overlay. */
  1960. return;
  1961. }
  1962. if (sm_open)
  1963. sm_add_present(&present);
  1964. if (u_wm_begin() != 0)
  1965. return;
  1966. remap_window_contents();
  1967. for (i = 0; i < present.count; i++)
  1968. draw_rect(present.rect[i].x, present.rect[i].y,
  1969. present.rect[i].w, present.rect[i].h);
  1970. if (bar_dirty)
  1971. draw_bar();
  1972. if (osd_show) {
  1973. osd_draw();
  1974. add_rect(&present, osd_x, osd_y, osd_w, osd_h);
  1975. }
  1976. osd_visible = osd_show;
  1977. sm_draw();
  1978. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1979. struct uwin *u = &wins[i];
  1980. if (!u->info.used || !u->info.dirty || !u->mapped ||
  1981. !u->ack_seq)
  1982. continue;
  1983. if (u->ack_seq == u->info.dirty_seq)
  1984. u_wm_ack(i, u->info.dirty_seq);
  1985. }
  1986. u_wm_present(&present);
  1987. }
  1988. static void spawn_app_arg_checked(const char *path, const char *arg)
  1989. {
  1990. if (!free_window_slot()) {
  1991. bar_notice("window limit");
  1992. full_redraw = 1;
  1993. return;
  1994. }
  1995. if (u_spawn(path, arg, 0, "/") < 0) {
  1996. bar_notice("spawn failed");
  1997. full_redraw = 1;
  1998. } else {
  1999. full_redraw = 1;
  2000. }
  2001. }
  2002. /* Any status target opens one control panel; a second click closes it. */
  2003. static void open_control_panel(void)
  2004. {
  2005. unsigned int i;
  2006. if (control_pid > 0 && u_alive((unsigned int)control_pid)) {
  2007. for (i = 0; i < WM_MAX_WINDOWS; i++)
  2008. if (wins[i].info.used &&
  2009. wins[i].info.pid == (unsigned int)control_pid) {
  2010. if (u_wm_close(i) == 0) {
  2011. control_pid = 0;
  2012. full_redraw = 1;
  2013. } else {
  2014. u_wm_focus(i);
  2015. full_redraw = 1;
  2016. }
  2017. return;
  2018. }
  2019. /* The process is alive but has not created its window yet. */
  2020. return;
  2021. }
  2022. if (!free_window_slot()) {
  2023. bar_notice("window limit");
  2024. full_redraw = 1;
  2025. return;
  2026. }
  2027. control_pid = u_spawn("/ramfs/bin/netmenu", "", 0, "/");
  2028. if (control_pid < 0) {
  2029. control_pid = 0;
  2030. bar_notice("spawn failed");
  2031. full_redraw = 1;
  2032. } else {
  2033. full_redraw = 1;
  2034. }
  2035. }
  2036. /* Launch one start-menu entry. The terminal and the control panel are WM
  2037. * actions; every other entry spawns its ELF from the pack. */
  2038. static void sm_launch(int idx)
  2039. {
  2040. const struct sm_app *a;
  2041. if (idx < 0 || idx >= SM_NAPP) {
  2042. sm_close();
  2043. return;
  2044. }
  2045. a = &sm_apps[idx];
  2046. sm_close();
  2047. if (a->action == SM_ACT_TERM) {
  2048. if (!free_window_slot()) {
  2049. bar_notice("window limit");
  2050. full_redraw = 1;
  2051. } else if (u_wm_create_term() < 0) {
  2052. bar_notice("cannot start term");
  2053. full_redraw = 1;
  2054. } else {
  2055. full_redraw = 1;
  2056. }
  2057. } else if (a->action == SM_ACT_SETTINGS) {
  2058. open_control_panel();
  2059. } else {
  2060. spawn_app_arg_checked(a->path, a->arg);
  2061. }
  2062. }
  2063. /* Index of a popover containing the point, or -1. */
  2064. static int popover_under(unsigned int mx, unsigned int my)
  2065. {
  2066. unsigned int i;
  2067. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  2068. struct wm_window_info *w = &wins[i].info;
  2069. if (w->used && w->popover &&
  2070. mx >= (unsigned int)w->x && my >= (unsigned int)w->y &&
  2071. mx < (unsigned int)(w->x + (int)w->w) &&
  2072. my < (unsigned int)(w->y + (int)w->h))
  2073. return (int)i;
  2074. }
  2075. return -1;
  2076. }
  2077. /* A press anywhere outside an open popover dismisses it; the click then
  2078. * continues normally so the target under the pointer still reacts. */
  2079. static void dismiss_popover_outside(unsigned int mx, unsigned int my)
  2080. {
  2081. unsigned int i;
  2082. if (popover_under(mx, my) >= 0)
  2083. return;
  2084. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  2085. if (!wins[i].info.used || !wins[i].info.popover)
  2086. continue;
  2087. if (wins[i].info.pid == (unsigned int)control_pid)
  2088. control_pid = 0;
  2089. if (u_wm_close(i) == 0) {
  2090. wins[i].info.used = 0;
  2091. wins[i].mapped = 0;
  2092. full_redraw = 1;
  2093. }
  2094. break;
  2095. }
  2096. }
  2097. /* Apply one edge-drag step. The grabbed edges follow the pointer while the
  2098. * opposite edges stay put; the work area clamps the active edges. */
  2099. static void resize_window_drag(struct uwin *u, unsigned int idx,
  2100. unsigned int mx, unsigned int my)
  2101. {
  2102. int dx = (int)mx - (int)resize_mx;
  2103. int dy = (int)my - (int)resize_my;
  2104. int left = resize_ox;
  2105. int right = resize_ox + (int)resize_ow;
  2106. int top = resize_oy;
  2107. int bottom = resize_oy + (int)resize_oh;
  2108. int nx, ny, nw, nh;
  2109. if (resize_edges & WM_EDGE_LEFT) {
  2110. left = resize_ox + dx;
  2111. if (left < 0)
  2112. left = 0;
  2113. } else if (resize_edges & WM_EDGE_RIGHT) {
  2114. right = resize_ox + (int)resize_ow + dx;
  2115. if (right > (int)fbw)
  2116. right = (int)fbw;
  2117. }
  2118. if (resize_edges & WM_EDGE_TOP) {
  2119. top = resize_oy + dy;
  2120. if (top < WM_BAR_H)
  2121. top = WM_BAR_H;
  2122. } else if (resize_edges & WM_EDGE_BOTTOM) {
  2123. bottom = resize_oy + (int)resize_oh + dy;
  2124. if (bottom > (int)fbh)
  2125. bottom = (int)fbh;
  2126. }
  2127. if ((resize_edges & (WM_EDGE_LEFT | WM_EDGE_RIGHT)) &&
  2128. right - left < 200) {
  2129. if (resize_edges & WM_EDGE_LEFT)
  2130. left = right - 200;
  2131. else
  2132. right = left + 200;
  2133. }
  2134. if ((resize_edges & (WM_EDGE_TOP | WM_EDGE_BOTTOM)) &&
  2135. bottom - top < 140) {
  2136. if (resize_edges & WM_EDGE_TOP)
  2137. top = bottom - 140;
  2138. else
  2139. bottom = top + 140;
  2140. }
  2141. if (left < 0)
  2142. left = 0;
  2143. if (top < WM_BAR_H)
  2144. top = WM_BAR_H;
  2145. nx = left;
  2146. ny = top;
  2147. nw = right - left;
  2148. nh = bottom - top;
  2149. if (nw > (int)fbw)
  2150. nw = (int)fbw;
  2151. if (nh > (int)fbh - WM_BAR_H)
  2152. nh = (int)fbh - WM_BAR_H;
  2153. if (nx == u->info.x && ny == u->info.y &&
  2154. nw == (int)u->info.w && nh == (int)u->info.h)
  2155. return;
  2156. if (set_window_geometry(u, idx, nx, ny, (unsigned int)nw,
  2157. (unsigned int)nh) == 0)
  2158. u->state = WIN_NORMAL;
  2159. }
  2160. /* Capture the pointer for an edge resize that started at (mx,my). */
  2161. static void begin_resize(unsigned int idx, int edges, unsigned int mx,
  2162. unsigned int my)
  2163. {
  2164. struct uwin *u = &wins[idx];
  2165. resize_id = (int)idx;
  2166. resize_edges = edges;
  2167. resize_mx = mx;
  2168. resize_my = my;
  2169. resize_ox = u->info.x;
  2170. resize_oy = u->info.y;
  2171. resize_ow = u->info.w;
  2172. resize_oh = u->info.h;
  2173. u_wm_mouse_capture(1);
  2174. }
  2175. /* The WM's cursor override (SYS_WM_SET_CURSOR): resize arrows on the
  2176. * invisible resize border and while resizing, the move cross while a
  2177. * title-bar drag is under way, a hand over the start button, the open start
  2178. * menu and the status area. Everywhere else the kernel picks the window's
  2179. * own content cursor (or the arrow). */
  2180. static void update_cursor(unsigned int mx, unsigned int my)
  2181. {
  2182. static int current = CURSOR_NO_OVERRIDE;
  2183. int want = CURSOR_NO_OVERRIDE;
  2184. if (resize_id >= 0) {
  2185. want = resize_cursor(resize_edges);
  2186. } else if (drag_id >= 0) {
  2187. if (drag_moved)
  2188. want = CURSOR_MOVE;
  2189. } else if (sm_open) {
  2190. /* The menu is modal: a hand over its items, the arrow
  2191. * elsewhere, never a window's content cursor underneath. */
  2192. int hit = sm_hit(mx, my);
  2193. want = (hit == SM_HIT_NONE || hit == SM_HIT_MENU) ?
  2194. CURSOR_DEFAULT : CURSOR_POINTER;
  2195. } else if (my < WM_BAR_H) {
  2196. if (bar_start_hit(mx, my) || bar_status_hit(mx, my))
  2197. want = CURSOR_POINTER;
  2198. } else {
  2199. int edges = 0;
  2200. int idx = resize_hit((int)mx, (int)my, &edges);
  2201. if (idx >= 0 && edges &&
  2202. !close_hit(&wins[idx].info, (int)mx, (int)my))
  2203. want = resize_cursor(edges);
  2204. }
  2205. if (want != current && u_wm_set_cursor(want) == 0)
  2206. current = want;
  2207. }
  2208. static void handle_mouse(struct wm_status *st)
  2209. {
  2210. unsigned int buttons = st->mouse.buttons;
  2211. unsigned int mx = st->mouse.x, my = st->mouse.y;
  2212. int down = (buttons & 1) != 0;
  2213. int was = (prev_buttons & 1) != 0;
  2214. if (down && !was) {
  2215. int prev_title_id = last_title_id;
  2216. unsigned int now = u_ticks_ms();
  2217. int hit;
  2218. /* The start menu is modal: a press on an entry launches it,
  2219. * a press on Power opens its submenu, and any other press
  2220. * dismisses the menu without reaching the window below. */
  2221. if (sm_open) {
  2222. hit = sm_hit(mx, my);
  2223. last_title_id = -1;
  2224. if (hit == SM_HIT_START) {
  2225. sm_close();
  2226. } else if (hit == SM_HIT_POWER) {
  2227. sm_power_open = !sm_power_open;
  2228. full_redraw = 1;
  2229. } else if (hit == SM_HIT_REBOOT) {
  2230. sm_close();
  2231. if (u_spawn_kcmd("reboot", "", 0) < 0)
  2232. bar_notice("reboot failed");
  2233. } else if (hit == SM_HIT_POWEROFF) {
  2234. sm_close();
  2235. if (u_spawn_kcmd("poweroff", "", 0) < 0)
  2236. bar_notice("poweroff failed");
  2237. } else if (hit >= 0) {
  2238. sm_launch(hit);
  2239. } else if (hit != SM_HIT_MENU) {
  2240. sm_close();
  2241. }
  2242. goto mouse_done;
  2243. }
  2244. if (bar_start_hit(mx, my)) {
  2245. /* Opening the menu also closes an open control
  2246. * panel, so only one floating panel is visible. */
  2247. dismiss_popover_outside(mx, my);
  2248. last_title_id = -1;
  2249. sm_open = 1;
  2250. sm_power_open = 0;
  2251. sm_hot = -1;
  2252. sm_power_hot = 0;
  2253. full_redraw = 1;
  2254. goto mouse_done;
  2255. }
  2256. if (!bar_status_hit(mx, my))
  2257. dismiss_popover_outside(mx, my);
  2258. last_title_id = -1;
  2259. if (bar_status_hit(mx, my)) {
  2260. open_control_panel();
  2261. } else {
  2262. int edges = 0;
  2263. int idx = resize_hit((int)mx, (int)my, &edges);
  2264. if (idx >= 0) {
  2265. struct uwin *u = &wins[idx];
  2266. int pmx = (int)mx, pmy = (int)my;
  2267. u_wm_focus((unsigned int)idx);
  2268. if (u->info.popover) {
  2269. /* A popover has no title bar, close
  2270. * button or resize border; the app owns
  2271. * its content. */
  2272. } else if (close_hit(&u->info, pmx, pmy)) {
  2273. u_wm_close((unsigned int)idx);
  2274. full_redraw = 1;
  2275. } else if (edges) {
  2276. begin_resize((unsigned int)idx, edges,
  2277. mx, my);
  2278. } else if (pmy < u->info.y + WM_TITLE_H) {
  2279. if (prev_title_id == idx &&
  2280. now - last_title_ms <= WM_DBLCLICK_MS &&
  2281. iabs(pmx - last_title_x) <= WM_DBLCLICK_SLOP &&
  2282. iabs(pmy - last_title_y) <= WM_DBLCLICK_SLOP) {
  2283. last_title_ms = 0;
  2284. if (toggle_maximize(u,
  2285. (unsigned int)idx) != 0)
  2286. bar_notice("resize failed");
  2287. } else {
  2288. last_title_id = idx;
  2289. last_title_ms = now;
  2290. last_title_x = pmx;
  2291. last_title_y = pmy;
  2292. drag_id = idx;
  2293. drag_mx = pmx;
  2294. drag_my = pmy;
  2295. drag_dx = pmx - u->info.x;
  2296. drag_dy = pmy - u->info.y;
  2297. drag_moved = 0;
  2298. u_wm_mouse_capture(1);
  2299. }
  2300. }
  2301. }
  2302. }
  2303. } else if (down && was && resize_id >= 0) {
  2304. struct uwin *u = &wins[resize_id];
  2305. if (u->info.used)
  2306. resize_window_drag(u, (unsigned int)resize_id, mx, my);
  2307. else {
  2308. resize_id = -1;
  2309. u_wm_mouse_capture(0);
  2310. }
  2311. } else if (down && was && drag_id >= 0) {
  2312. struct uwin *u = &wins[drag_id];
  2313. if (u->info.used) {
  2314. int ox, oy, nx, ny;
  2315. if (iabs((int)mx - drag_mx) >= WM_DRAG_START ||
  2316. iabs((int)my - drag_my) >= WM_DRAG_START)
  2317. drag_moved = 1;
  2318. /* A maximized or snapped window is restored as soon as
  2319. * the drag starts. Keep the grab point at the same
  2320. * fraction of the title bar, so the restored (narrower)
  2321. * window stays under the pointer. A click that does
  2322. * not move must leave it in place. */
  2323. if (u->state != WIN_NORMAL && drag_moved) {
  2324. unsigned int ow = u->info.w;
  2325. int gx = drag_mx - u->info.x;
  2326. int gy = drag_my - u->info.y;
  2327. if (restore_window(u,
  2328. (unsigned int)drag_id) == 0) {
  2329. if (ow)
  2330. gx = gx * (int)u->info.w /
  2331. (int)ow;
  2332. drag_dx = gx;
  2333. drag_dy = gy;
  2334. }
  2335. }
  2336. if (u->state == WIN_NORMAL) {
  2337. ox = u->info.x;
  2338. oy = u->info.y;
  2339. nx = (int)mx - drag_dx;
  2340. ny = (int)my - drag_dy;
  2341. resist_geom(u, drag_id, &nx, &ny);
  2342. clamp_geom(u, &nx, &ny);
  2343. if (nx != ox || ny != oy) {
  2344. struct wm_present_info present;
  2345. unsigned int i;
  2346. u_wm_set_geom((unsigned int)drag_id,
  2347. nx, ny);
  2348. u->info.x = nx;
  2349. u->info.y = ny;
  2350. if (u->info.focused) {
  2351. move_window_pixels(u, ox, oy,
  2352. nx, ny);
  2353. } else {
  2354. present.count = 0;
  2355. present.immediate = 1;
  2356. add_rect(&present, ox, oy,
  2357. u->info.w, u->info.h);
  2358. add_rect(&present, nx, ny,
  2359. u->info.w, u->info.h);
  2360. if (u_wm_begin() == 0) {
  2361. remap_window_contents();
  2362. for (i = 0;
  2363. i < present.count; i++)
  2364. draw_rect(
  2365. present.rect[i].x,
  2366. present.rect[i].y,
  2367. present.rect[i].w,
  2368. present.rect[i].h);
  2369. wm_overlays(&present);
  2370. u_wm_present(&present);
  2371. }
  2372. }
  2373. }
  2374. }
  2375. } else {
  2376. drag_id = -1;
  2377. u_wm_mouse_capture(0);
  2378. }
  2379. } else if (!down && was) {
  2380. /* Dropping a window with the pointer on the left/right screen
  2381. * edge snaps it to that half; on the top edge it maximizes. */
  2382. if (drag_id >= 0 && drag_moved && wins[drag_id].info.used &&
  2383. wins[drag_id].state == WIN_NORMAL) {
  2384. struct uwin *u = &wins[drag_id];
  2385. int st = WIN_NORMAL;
  2386. if ((int)mx < WM_SNAP_EDGE)
  2387. st = WIN_SNAP_LEFT;
  2388. else if ((int)mx >= (int)fbw - WM_SNAP_EDGE)
  2389. st = WIN_SNAP_RIGHT;
  2390. else if ((int)my < WM_SNAP_EDGE)
  2391. st = WIN_MAXIMIZED;
  2392. if (st != WIN_NORMAL &&
  2393. place_window(u, (unsigned int)drag_id, st) != 0)
  2394. bar_notice("resize failed");
  2395. }
  2396. drag_id = -1;
  2397. resize_id = -1;
  2398. u_wm_mouse_capture(0);
  2399. }
  2400. mouse_done:
  2401. prev_buttons = buttons;
  2402. update_cursor(mx, my);
  2403. }
  2404. static char *put_str(char *p, const char *s)
  2405. {
  2406. while (*s)
  2407. *p++ = *s++;
  2408. return p;
  2409. }
  2410. static char *put_dec(char *p, unsigned int v)
  2411. {
  2412. char b[10];
  2413. unsigned int n = 0;
  2414. do {
  2415. b[n++] = (char)('0' + v % 10);
  2416. v /= 10;
  2417. } while (v);
  2418. while (n)
  2419. *p++ = b[--n];
  2420. return p;
  2421. }
  2422. /* Expose the last interval in /share so the target can be measured without
  2423. * a serial console. */
  2424. static void write_stats(void)
  2425. {
  2426. char b[80], *p = b;
  2427. unsigned int now = u_ticks_ms();
  2428. p = put_str(p, "t=");
  2429. p = put_dec(p, now / 1000);
  2430. p = put_str(p, "s frames=");
  2431. p = put_dec(p, stat_frames);
  2432. p = put_str(p, " ms=");
  2433. p = put_dec(p, stat_ms);
  2434. p = put_str(p, " max=");
  2435. p = put_dec(p, stat_max);
  2436. p = put_str(p, " px=");
  2437. p = put_dec(p, stat_px);
  2438. p = put_str(p, "\n");
  2439. u_syscall3(SYS_WRITE_FILE, (int)"/share/wmstats", (int)b,
  2440. (int)(p - b));
  2441. }
  2442. void _start(const char *arg)
  2443. {
  2444. struct wm_screen_info si;
  2445. struct wm_status st;
  2446. unsigned int i;
  2447. (void)arg;
  2448. if (u_wm_attach(&si) != 0)
  2449. u_exit(1);
  2450. usurf_init(&screen, si.fb, si.width, si.height, si.pitch, si.bpp);
  2451. usurf_font_attach(&screen, "/ramfs/fonts/sans.ttf", 14);
  2452. fbw = si.width;
  2453. fbh = si.height;
  2454. wall_load();
  2455. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  2456. wins[i].info.used = 0;
  2457. wins[i].mapped = 0;
  2458. }
  2459. full_redraw = 1;
  2460. for (;;) {
  2461. unsigned int t0, t1;
  2462. if (u_wm_poll(&st) != 0)
  2463. u_exit(1);
  2464. if (st.wallpaper_seq != wall_seq_seen) {
  2465. wall_seq_seen = st.wallpaper_seq;
  2466. wall_load();
  2467. full_redraw = 1;
  2468. }
  2469. /* The WM starts before the storage scan, so the first load may not
  2470. * see /data yet. Reload when boot_ready says the mounts are up,
  2471. * and retry for ten seconds for the deferred USB /data volume. */
  2472. if (st.boot_ready && !wall_boot_seen) {
  2473. wall_boot_seen = 1;
  2474. wall_retries = 0;
  2475. wall_retry_ms = u_ticks_ms() + 1000;
  2476. wall_load();
  2477. full_redraw = 1;
  2478. } else if (wall_boot_seen && !wall_ready && wall_retries < 10 &&
  2479. (int)(u_ticks_ms() - wall_retry_ms) >= 0) {
  2480. wall_retries++;
  2481. wall_retry_ms += 1000;
  2482. wall_load();
  2483. if (wall_ready)
  2484. full_redraw = 1;
  2485. }
  2486. if (st.full_redraw)
  2487. full_redraw = 1;
  2488. usurf_pixels = 0;
  2489. t0 = u_ticks_ms();
  2490. handle_mouse(&st);
  2491. do_update(&st);
  2492. t1 = u_ticks_ms();
  2493. if (usurf_pixels) {
  2494. unsigned int dt = t1 - t0;
  2495. stat_frames++;
  2496. stat_ms += dt;
  2497. stat_px += usurf_pixels;
  2498. if (dt > stat_max)
  2499. stat_max = dt;
  2500. }
  2501. if (t1 - stat_last >= 2000) {
  2502. stat_last = t1;
  2503. if (stat_frames) {
  2504. write_stats();
  2505. stat_frames = stat_ms = stat_px = stat_max = 0;
  2506. }
  2507. }
  2508. u_sleep_ms(10);
  2509. }
  2510. }