ucmd_wm.c 69 KB

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