ucmd_wm.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900
  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. #define COL_BORDER 0xC8C8C8u
  10. #define COL_TITLE_ON 0x465A96u
  11. #define COL_TITLE_OFF 0x646464u
  12. #define COL_CLOSE 0xC64040u
  13. #define COL_WHITE 0xFFFFFFu
  14. #define COL_BANNER 0x55FF55u
  15. struct uwin {
  16. struct wm_window_info info;
  17. struct wm_map_info map;
  18. int mapped;
  19. unsigned int ack_seq;
  20. /* Double-click title bar: the geometry to return to when maximized. */
  21. int maximized;
  22. int restore_x, restore_y;
  23. unsigned int restore_w, restore_h;
  24. };
  25. /* Double-click detection on the title bar. */
  26. #define WM_DBLCLICK_MS 500
  27. #define WM_DBLCLICK_SLOP 8
  28. /* A maximized window restores only after the pointer actually starts a drag,
  29. * so a plain click on its title bar leaves it maximized. */
  30. #define WM_DRAG_START 3
  31. static struct usurf screen;
  32. static struct uwin wins[WM_MAX_WINDOWS];
  33. static unsigned int fbw, fbh;
  34. static int drag_id = -1;
  35. static int drag_dx, drag_dy;
  36. static int drag_mx, drag_my; /* press position, for restore-on-drag */
  37. static int resize_id = -1;
  38. static unsigned int resize_mx, resize_my, resize_ow, resize_oh;
  39. static unsigned int last_title_ms;
  40. static int last_title_id = -1, last_title_x, last_title_y;
  41. static unsigned int prev_buttons;
  42. static int full_redraw = 1;
  43. static int boot_term_spawned;
  44. static unsigned int bar_kexec_busy, bar_kexec_progress, bar_kexec_total;
  45. static int bar_temp_c = -1;
  46. static int bar_batt_pct = -1;
  47. static int bar_batt_charging;
  48. static int bar_ac_online = -1;
  49. /* Battery/AC status icon: a plug on AC, a level-filled battery otherwise.
  50. * The rightmost bar item; hovering it shows a percentage tooltip below it. */
  51. #define BAR_PWR_W 24
  52. #define BAR_PWR_Y 6
  53. #define BAR_TIP_H 20
  54. static int bar_pwr_x = -1;
  55. static int bar_pwr_hot;
  56. static int bar_pwr_tip_x = -1;
  57. static int bar_pwr_tip_y;
  58. static unsigned int bar_pwr_tip_w, bar_pwr_tip_h;
  59. static int bar_pwr_tip_want; /* tooltip computed for this frame */
  60. static int bar_pwr_tip_on; /* tooltip pixels currently on screen */
  61. static int bar_pwr_tip_on_x, bar_pwr_tip_on_y;
  62. static unsigned int bar_pwr_tip_on_w, bar_pwr_tip_on_h;
  63. /* Brightness OSD: a centered bubble shown after the kernel's backlight
  64. * seq changes, then hidden after OSD_MS. */
  65. #define OSD_MS 3000
  66. #define OSD_W 210
  67. #define OSD_H 60
  68. static int osd_show; /* should be visible this frame */
  69. static int osd_visible; /* pixels are on screen */
  70. static int osd_level;
  71. static unsigned int osd_until;
  72. static unsigned int osd_seen_seq;
  73. static int osd_seen_valid;
  74. static int osd_x, osd_y;
  75. static unsigned int osd_w, osd_h;
  76. static char bar_msg[24];
  77. static unsigned int bar_msg_until;
  78. static int bar_msg_dirty;
  79. /* Rectangle of the right-aligned date/time text, so a click there can open
  80. * the Date & Time app. w == 0 means "not drawn" (e.g. kexec banner). */
  81. static int bar_clock_x = -1;
  82. static unsigned int bar_clock_w;
  83. static int bar_clock_hot;
  84. static unsigned int stat_frames, stat_ms, stat_px, stat_max, stat_last;
  85. /* Network status icon (right side of the bar). */
  86. #define BAR_NET_W 24
  87. #define BAR_NET_Y 6
  88. #define BAR_NET_NONE 0
  89. #define BAR_NET_WIRED 1
  90. #define BAR_NET_WIFI 2
  91. #define BAR_NET_USB 3
  92. static int bar_net_x = -1;
  93. static unsigned int bar_net_w;
  94. static int bar_net_hot;
  95. static int bar_net_kind;
  96. static int bar_net_bars;
  97. static unsigned int bar_net_color = 0xFF5050u;
  98. static int netmenu_pid;
  99. static void bar_notice(const char *msg)
  100. {
  101. unsigned int i;
  102. for (i = 0; msg[i] && i + 1 < sizeof(bar_msg); i++)
  103. bar_msg[i] = msg[i];
  104. bar_msg[i] = '\0';
  105. bar_msg_until = u_ticks_ms() + 2000;
  106. bar_msg_dirty = 1;
  107. }
  108. static int free_window_slot(void)
  109. {
  110. unsigned int i;
  111. for (i = 0; i < WM_MAX_WINDOWS; i++)
  112. if (!wins[i].info.used)
  113. return 1;
  114. return 0;
  115. }
  116. static unsigned int rgb16(unsigned int r, unsigned int g, unsigned int b)
  117. {
  118. return (r << 16) | (g << 8) | b;
  119. }
  120. /* Rainbow gradient: hue 0..1535 (6*256) maps to R→Y→G→C→B→M→R, the same
  121. * integer interpolation the kernel bar uses. */
  122. static unsigned int rainbow_rgb(unsigned int hue)
  123. {
  124. unsigned int seg = hue / 256, f = hue % 256;
  125. unsigned int r1, g1, b1, r2, g2, b2;
  126. switch (seg) {
  127. case 0: r1=255; g1=0; b1=0; r2=255; g2=255; b2=0; break;
  128. case 1: r1=255; g1=255; b1=0; r2=0; g2=255; b2=0; break;
  129. case 2: r1=0; g1=255; b1=0; r2=0; g2=255; b2=255; break;
  130. case 3: r1=0; g1=255; b1=255; r2=0; g2=0; b2=255; break;
  131. case 4: r1=0; g1=0; b1=255; r2=255; g2=0; b2=255; break;
  132. default:r1=255; g1=0; b1=255; r2=255; g2=0; b2=0; break;
  133. }
  134. return rgb16((r1 * (256 - f) + r2 * f) / 256,
  135. (g1 * (256 - f) + g2 * f) / 256,
  136. (b1 * (256 - f) + b2 * f) / 256);
  137. }
  138. static void fill_clip(int x, int y, unsigned int w, unsigned int h,
  139. unsigned int rgb, int cx, int cy,
  140. unsigned int cw, unsigned int ch)
  141. {
  142. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  143. if (x0 < cx) x0 = cx;
  144. if (y0 < cy) y0 = cy;
  145. if (x1 > cx + (int)cw) x1 = cx + (int)cw;
  146. if (y1 > cy + (int)ch) y1 = cy + (int)ch;
  147. if (x1 > x0 && y1 > y0)
  148. usurf_fill(&screen, x0, y0, (unsigned int)(x1 - x0),
  149. (unsigned int)(y1 - y0), rgb);
  150. }
  151. static void bar_button(unsigned int x, unsigned int y, unsigned int w,
  152. unsigned int h, const char *label)
  153. {
  154. int tw = (int)usurf_text_width(&screen, label);
  155. usurf_fill(&screen, x, y, w, h, 0x963CD2u);
  156. usurf_text(&screen, (int)x + ((int)w - tw) / 2, (int)(y + 3),
  157. label, COL_WHITE, 0, 0, fbw, WM_BAR_H);
  158. }
  159. static int name_is(const char *a, const char *b)
  160. {
  161. while (*a && *b) {
  162. if (*a != *b)
  163. return 0;
  164. a++;
  165. b++;
  166. }
  167. return *a == *b;
  168. }
  169. static int net_rssi_bars(int rssi)
  170. {
  171. if (rssi >= -55)
  172. return 4;
  173. if (rssi >= -65)
  174. return 3;
  175. if (rssi >= -75)
  176. return 2;
  177. return 1;
  178. }
  179. /* Small top-bar pictograms, drawn directly into the shadow. */
  180. static void bar_icon_cable(int x, int y, unsigned int c)
  181. {
  182. usurf_fill(&screen, x + 1, y + 6, 9, 8, c);
  183. usurf_fill(&screen, x + 3, y + 8, 2, 2, COL_BG);
  184. usurf_fill(&screen, x + 6, y + 8, 2, 2, COL_BG);
  185. usurf_fill(&screen, x + 10, y + 9, 7, 2, c);
  186. usurf_fill(&screen, x + 17, y + 5, 5, 10, c);
  187. usurf_fill(&screen, x + 19, y + 7, 2, 3, COL_BG);
  188. }
  189. static void bar_icon_wifi(int x, int y, unsigned int c, int bars)
  190. {
  191. int i;
  192. for (i = 0; i < 4; i++) {
  193. int h = 3 + i * 3;
  194. usurf_fill(&screen, x + 3 + i * 5, y + 16 - h, 3, h,
  195. i < bars ? c : 0x4A4A5Au);
  196. }
  197. }
  198. static void bar_icon_x(int x, int y, unsigned int c)
  199. {
  200. int i;
  201. for (i = 0; i < 12; i++) {
  202. usurf_fill(&screen, x + 6 + i, y + 4 + i, 2, 2, c);
  203. usurf_fill(&screen, x + 17 - i, y + 4 + i, 2, 2, c);
  204. }
  205. }
  206. static void bar_icon_usb(int x, int y, unsigned int c)
  207. {
  208. usurf_text(&screen, x + 1, y + 1, "USB", c, 0, 0, fbw, WM_BAR_H);
  209. }
  210. static void bar_draw_net_icon(int x, int y)
  211. {
  212. switch (bar_net_kind) {
  213. case BAR_NET_WIRED:
  214. bar_icon_cable(x, y, bar_net_color);
  215. break;
  216. case BAR_NET_WIFI:
  217. bar_icon_wifi(x, y, bar_net_color, bar_net_bars);
  218. break;
  219. case BAR_NET_USB:
  220. bar_icon_usb(x, y, bar_net_color);
  221. break;
  222. default:
  223. bar_icon_x(x, y, bar_net_color);
  224. break;
  225. }
  226. }
  227. /* 1 Hz poll: cached state -> the bar's network icon. The WiFi status call is
  228. * only made when WiFi is the active netdev, so a machine without a card does
  229. * not run a PCI scan on every tick. */
  230. static int poll_net_status(void)
  231. {
  232. struct sys_netinfo ni;
  233. int kind = BAR_NET_NONE, bars = 0, have_ip = 0;
  234. unsigned int color = 0xFF5050u;
  235. int changed;
  236. int i;
  237. if (u_net_info(&ni) == 1 && ni.present) {
  238. for (i = 0; i < 4; i++)
  239. if (ni.ip[i])
  240. have_ip = 1;
  241. if (name_is(ni.name, "wifi")) {
  242. struct sys_wifi_status ws;
  243. if (u_wifi(SYS_WIFI_STATUS, (int)&ws, 0) == 0 &&
  244. ws.present && ws.state >= SYS_WIFI_ST_ASSOC) {
  245. kind = BAR_NET_WIFI;
  246. bars = net_rssi_bars(ws.rssi);
  247. color = (ws.state >= SYS_WIFI_ST_LINK && have_ip) ?
  248. 0x55FF55u : 0xFFD250u;
  249. }
  250. } else if (name_is(ni.name, "rndis")) {
  251. kind = BAR_NET_USB;
  252. color = have_ip ? 0x55FF55u : 0xFFD250u;
  253. } else {
  254. kind = BAR_NET_WIRED;
  255. color = have_ip ? 0x55FF55u : 0xFFD250u;
  256. }
  257. }
  258. changed = kind != bar_net_kind || bars != bar_net_bars ||
  259. color != bar_net_color;
  260. bar_net_kind = kind;
  261. bar_net_bars = bars;
  262. bar_net_color = color;
  263. return changed;
  264. }
  265. static unsigned int temp_color(int c)
  266. {
  267. if (c >= 80)
  268. return 0xFF5050u;
  269. if (c >= 60)
  270. return 0xFFD250u;
  271. return 0x55FF55u;
  272. }
  273. /* "43C" (no libc printf here). */
  274. static unsigned int temp_text(char *b, int c)
  275. {
  276. unsigned int n = 0, v = (unsigned int)c;
  277. if (v >= 100)
  278. b[n++] = (char)('0' + (v / 100) % 10);
  279. if (v >= 10)
  280. b[n++] = (char)('0' + (v / 10) % 10);
  281. b[n++] = (char)('0' + v % 10);
  282. b[n++] = 'C';
  283. b[n] = '\0';
  284. return n;
  285. }
  286. /* "87%" (no libc printf here). */
  287. static unsigned int pct_text(char *b, int pct)
  288. {
  289. unsigned int n = 0, v = (unsigned int)pct;
  290. if (v >= 100)
  291. b[n++] = (char)('0' + (v / 100) % 10);
  292. if (v >= 10)
  293. b[n++] = (char)('0' + (v / 10) % 10);
  294. b[n++] = (char)('0' + v % 10);
  295. b[n++] = '%';
  296. b[n] = '\0';
  297. return n;
  298. }
  299. static unsigned int batt_color(int pct)
  300. {
  301. if (pct <= 10)
  302. return 0xFF5050u;
  303. return 0x55FF55u;
  304. }
  305. /* Battery pictogram: outline, terminal and a fill proportional to the
  306. * percentage. A critical charge turns the outline red as well. */
  307. static void bar_icon_battery(int x, int y, int pct, unsigned int c)
  308. {
  309. unsigned int frame = c;
  310. unsigned int fill = (unsigned int)((14 * pct + 99) / 100);
  311. usurf_fill(&screen, x + 1, y + 2, 18, 2, frame);
  312. usurf_fill(&screen, x + 1, y + 14, 18, 2, frame);
  313. usurf_fill(&screen, x + 1, y + 2, 2, 14, frame);
  314. usurf_fill(&screen, x + 17, y + 2, 2, 14, frame);
  315. usurf_fill(&screen, x + 19, y + 6, 3, 6, frame);
  316. if (fill > 14)
  317. fill = 14;
  318. if (fill)
  319. usurf_fill(&screen, x + 3, y + 4, fill, 10, c);
  320. }
  321. /* AC plug pictogram: two prongs and the body, kept compact in height. */
  322. static void bar_icon_plug(int x, int y, unsigned int c)
  323. {
  324. usurf_fill(&screen, x + 7, y + 2, 3, 5, c);
  325. usurf_fill(&screen, x + 14, y + 2, 3, 5, c);
  326. usurf_fill(&screen, x + 5, y + 7, 14, 7, c);
  327. usurf_fill(&screen, x + 7, y + 9, 10, 3, COL_BG);
  328. }
  329. /* Percentage tooltip: a small box below the status icon. It is drawn after
  330. * the windows (see bar_tooltip_overlay), so it always sits on top. */
  331. static void bar_draw_tooltip(void)
  332. {
  333. char lb[8];
  334. unsigned int lw;
  335. int tx;
  336. if (!bar_pwr_tip_want || bar_pwr_tip_x < 0)
  337. return;
  338. usurf_fill(&screen, bar_pwr_tip_x, bar_pwr_tip_y,
  339. bar_pwr_tip_w, bar_pwr_tip_h, 0x161622u);
  340. usurf_fill(&screen, bar_pwr_tip_x, bar_pwr_tip_y,
  341. bar_pwr_tip_w, 1, 0x646464u);
  342. usurf_fill(&screen, bar_pwr_tip_x,
  343. bar_pwr_tip_y + (int)bar_pwr_tip_h - 1,
  344. bar_pwr_tip_w, 1, 0x646464u);
  345. usurf_fill(&screen, bar_pwr_tip_x, bar_pwr_tip_y,
  346. 1, bar_pwr_tip_h, 0x646464u);
  347. usurf_fill(&screen, bar_pwr_tip_x + (int)bar_pwr_tip_w - 1,
  348. bar_pwr_tip_y, 1, bar_pwr_tip_h, 0x646464u);
  349. (void)pct_text(lb, bar_batt_pct);
  350. lw = usurf_text_width(&screen, lb);
  351. tx = bar_pwr_tip_x + ((int)bar_pwr_tip_w - (int)lw) / 2;
  352. usurf_text(&screen, tx, bar_pwr_tip_y + 3, lb,
  353. batt_color(bar_batt_pct), 0, 0, fbw, fbh);
  354. }
  355. /* Publish the geometry computed by draw_bar() and redraw the tooltip. */
  356. static void bar_tooltip_commit(void)
  357. {
  358. if (bar_pwr_tip_want) {
  359. bar_draw_tooltip();
  360. bar_pwr_tip_on = 1;
  361. bar_pwr_tip_on_x = bar_pwr_tip_x;
  362. bar_pwr_tip_on_y = bar_pwr_tip_y;
  363. bar_pwr_tip_on_w = bar_pwr_tip_w;
  364. bar_pwr_tip_on_h = bar_pwr_tip_h;
  365. } else {
  366. bar_pwr_tip_on = 0;
  367. }
  368. }
  369. static void draw_bar(void)
  370. {
  371. char dt[48];
  372. unsigned int n, i, x, y;
  373. usurf_fill(&screen, 0, 0, fbw, WM_BAR_H, COL_BG);
  374. usurf_text(&screen, 4, 5, OS_NAME " v" OS_VERSION, COL_BANNER,
  375. 0, 0, fbw, WM_BAR_H);
  376. bar_button(WM_BTN_X, WM_BTN_Y, WM_BTN_W, WM_BTN_H, "New");
  377. bar_button(WM_BTN_CLOCK_X, WM_BTN_CLOCK_Y, WM_BTN_CLOCK_W,
  378. WM_BTN_CLOCK_H, "Clock");
  379. bar_button(WM_BTN_COLORS_X, WM_BTN_COLORS_Y, WM_BTN_COLORS_W,
  380. WM_BTN_COLORS_H, "Colors");
  381. bar_button(WM_BTN_FILES_X, WM_BTN_FILES_Y, WM_BTN_FILES_W,
  382. WM_BTN_FILES_H, "Files");
  383. bar_button(WM_BTN_MINES_X, WM_BTN_MINES_Y, WM_BTN_MINES_W,
  384. WM_BTN_MINES_H, "Mines");
  385. bar_button(WM_BTN_WEB_X, WM_BTN_WEB_Y, WM_BTN_WEB_W,
  386. WM_BTN_WEB_H, "Web");
  387. bar_button(WM_BTN_CARDS_X, WM_BTN_CARDS_Y, WM_BTN_CARDS_W,
  388. WM_BTN_CARDS_H, "Cards");
  389. bar_button(WM_BTN_GALLERY_X, WM_BTN_GALLERY_Y, WM_BTN_GALLERY_W,
  390. WM_BTN_GALLERY_H, "Gallery");
  391. bar_button(WM_BTN_CHAT_X, WM_BTN_CHAT_Y, WM_BTN_CHAT_W,
  392. WM_BTN_CHAT_H, "Chat");
  393. if (bar_msg_until &&
  394. (int)(u_ticks_ms() - bar_msg_until) < 0)
  395. usurf_text(&screen, (int)(WM_BTN_END_X + 8),
  396. 5, bar_msg, 0xFFD250u, 0, 0, fbw, WM_BAR_H);
  397. /* Rainbow separator (same y as the kernel's old bar). */
  398. for (x = 0; x < fbw; x++) {
  399. unsigned int c = rainbow_rgb((x * (6 * 256)) / (fbw ? fbw : 1));
  400. for (y = WM_BAR_H - 5; y < WM_BAR_H - 3; y++)
  401. usurf_fill(&screen, (int)x, (int)y, 1, 1, c);
  402. }
  403. if (bar_kexec_busy) {
  404. static const char msg[] = "loading new kernel over serial";
  405. unsigned int dots = bar_kexec_total ?
  406. (bar_kexec_progress * 12 / bar_kexec_total) : 0;
  407. bar_clock_x = -1;
  408. bar_clock_w = 0;
  409. bar_clock_hot = 0;
  410. bar_net_x = -1;
  411. bar_net_w = 0;
  412. bar_pwr_x = -1;
  413. bar_pwr_tip_x = -1;
  414. bar_pwr_tip_want = 0;
  415. n = 0;
  416. while (msg[n])
  417. dt[n] = msg[n], n++;
  418. for (i = 0; i < 12; i++)
  419. dt[n + i] = (i < dots) ? '.' : ' ';
  420. n += 12;
  421. dt[n] = '\0';
  422. usurf_text(&screen,
  423. (int)fbw - (int)usurf_text_width(&screen, dt) - 4,
  424. 5, dt, 0xFFC864u, 0, 0, fbw, WM_BAR_H);
  425. } else {
  426. unsigned int clock_chars = 0;
  427. int right = (int)fbw - 4;
  428. int min_x = (int)WM_BTN_END_X + 4;
  429. bar_clock_x = -1;
  430. bar_clock_w = 0;
  431. bar_net_x = -1;
  432. bar_net_w = 0;
  433. bar_pwr_x = -1;
  434. bar_pwr_tip_x = -1;
  435. bar_pwr_tip_want = 0;
  436. /* The AC/battery status icon is the rightmost item. */
  437. if (bar_batt_pct >= 0 || bar_ac_online > 0) {
  438. int px = right - BAR_PWR_W;
  439. if (px >= min_x) {
  440. if (bar_pwr_hot)
  441. usurf_fill(&screen, px - 3, 2,
  442. BAR_PWR_W + 6,
  443. WM_BAR_H - 7, 0x3A3A55u);
  444. if (bar_ac_online > 0)
  445. bar_icon_plug(px, BAR_PWR_Y, 0x55FF55u);
  446. else
  447. bar_icon_battery(px, BAR_PWR_Y,
  448. bar_batt_pct,
  449. batt_color(bar_batt_pct));
  450. bar_pwr_x = px;
  451. right = px - 8;
  452. if (bar_pwr_hot && bar_batt_pct >= 0) {
  453. char lb[8];
  454. unsigned int lw, bw;
  455. int bx;
  456. (void)pct_text(lb, bar_batt_pct);
  457. lw = usurf_text_width(&screen, lb);
  458. bw = lw + 10;
  459. bx = px + BAR_PWR_W - (int)bw;
  460. if (bx < 4)
  461. bx = 4;
  462. if (bx + (int)bw > (int)fbw - 4)
  463. bx = (int)fbw - 4 - (int)bw;
  464. bar_pwr_tip_x = bx;
  465. bar_pwr_tip_y = WM_BAR_H + 2;
  466. bar_pwr_tip_w = bw;
  467. bar_pwr_tip_h = BAR_TIP_H;
  468. bar_pwr_tip_want = 1;
  469. }
  470. }
  471. }
  472. /* The network status sits to the left of the power icon, so it
  473. * survives narrow screens and a full temperature cluster. */
  474. {
  475. int nx = right - BAR_NET_W;
  476. if (nx >= min_x) {
  477. if (bar_net_hot)
  478. usurf_fill(&screen, nx - 3, 2,
  479. BAR_NET_W + 6,
  480. WM_BAR_H - 7, 0x3A3A55u);
  481. bar_draw_net_icon(nx, BAR_NET_Y);
  482. bar_net_x = nx;
  483. bar_net_w = BAR_NET_W;
  484. right = nx - 8;
  485. }
  486. }
  487. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  488. int tw = (int)usurf_text_width(&screen, dt);
  489. int tx = right - tw;
  490. n = 0;
  491. while (dt[n] && n < sizeof(dt) - 1)
  492. n++;
  493. bar_clock_x = tx;
  494. bar_clock_w = (unsigned int)tw;
  495. if (bar_clock_hot)
  496. usurf_fill(&screen, tx - 5, 2,
  497. (unsigned int)tw + 10,
  498. WM_BAR_H - 7, 0x3A3A55u);
  499. usurf_text(&screen, tx, 5, dt, COL_BANNER, 0, 0,
  500. fbw, WM_BAR_H);
  501. clock_chars = (unsigned int)tw;
  502. }
  503. if (clock_chars)
  504. right -= (int)clock_chars + 8;
  505. if (bar_temp_c >= 0) {
  506. char tb[12];
  507. int tx;
  508. (void)temp_text(tb, bar_temp_c);
  509. tx = right - (int)usurf_text_width(&screen, tb);
  510. if (tx >= min_x) {
  511. usurf_text(&screen, tx, 5, tb,
  512. temp_color(bar_temp_c), 0, 0,
  513. fbw, WM_BAR_H);
  514. right = tx - 8;
  515. }
  516. }
  517. }
  518. }
  519. /* A popover is a flat, chromeless panel: the canvas fills the window. */
  520. static void draw_popover_clip(unsigned int idx, int cx, int cy,
  521. unsigned int cw, unsigned int ch)
  522. {
  523. struct uwin *u = &wins[idx];
  524. struct wm_window_info *w = &u->info;
  525. unsigned int cwid = w->content_w, chei = w->content_h;
  526. if (u->mapped && u->map.kind == WM_KIND_GFX) {
  527. struct uwm_canvas c;
  528. c.px = (const unsigned short *)u->map.va;
  529. c.w = u->map.w;
  530. c.h = u->map.h;
  531. if (c.w == cwid && c.h == chei) {
  532. usurf_canvas(&screen, &c, w->x, w->y,
  533. cx, cy, cw, ch);
  534. return;
  535. }
  536. }
  537. fill_clip(w->x, w->y, cwid, chei, 0x181C28u, cx, cy, cw, ch);
  538. }
  539. static void draw_window_clip(unsigned int idx, int cx, int cy,
  540. unsigned int cw, unsigned int ch)
  541. {
  542. struct uwin *u = &wins[idx];
  543. struct wm_window_info *w = &u->info;
  544. int x = w->x, y = w->y;
  545. unsigned int pw = w->w, ph = w->h;
  546. int bx = x + (int)pw - 1 - WM_CLOSE_W;
  547. int cony = y + 1 + WM_TITLE_H;
  548. int content_x = x + 1;
  549. unsigned int content_w = w->content_w;
  550. unsigned int content_h = w->content_h;
  551. if (w->popover) {
  552. draw_popover_clip(idx, cx, cy, cw, ch);
  553. return;
  554. }
  555. /* Border, title bar, close button. */
  556. fill_clip(x, y, pw, ph, COL_BORDER, cx, cy, cw, ch);
  557. fill_clip(x + 1, y + 1, pw - 2, WM_TITLE_H,
  558. w->focused ? COL_TITLE_ON : COL_TITLE_OFF, cx, cy, cw, ch);
  559. usurf_text(&screen, x + 4, y + 3, w->title, COL_WHITE,
  560. cx, cy, cw, ch);
  561. fill_clip(bx, y + 1, WM_CLOSE_W, WM_TITLE_H, COL_CLOSE,
  562. cx, cy, cw, ch);
  563. usurf_text(&screen, bx + 4, y + 3, "x", COL_WHITE,
  564. cx, cy, cw, ch);
  565. /* Content: every window presents an RGB565 canvas (the terminal
  566. * renders its cells into one; gfx commands own theirs). A mapped
  567. * canvas covers the whole content area, so it does not need the black
  568. * underlay. */
  569. if (u->mapped && u->map.kind == WM_KIND_GFX) {
  570. struct uwm_canvas c;
  571. c.px = (const unsigned short *)u->map.va;
  572. c.w = u->map.w;
  573. c.h = u->map.h;
  574. if (c.w != content_w || c.h != content_h)
  575. fill_clip(content_x, cony, content_w, content_h,
  576. 0x000000u, cx, cy, cw, ch);
  577. usurf_canvas(&screen, &c, content_x, cony,
  578. cx, cy, cw, ch);
  579. } else {
  580. fill_clip(content_x, cony, content_w, content_h,
  581. 0x000000u, cx, cy, cw, ch);
  582. }
  583. /* Resize grip (bottom-right corner). */
  584. if (w->focused) {
  585. unsigned int i;
  586. for (i = 0; i < 10; i++) {
  587. fill_clip(x + (int)pw - 3 - (int)i,
  588. y + (int)ph - 4, 1, 1, 0xD2D2D2u,
  589. cx, cy, cw, ch);
  590. fill_clip(x + (int)pw - 4,
  591. y + (int)ph - 3 - (int)i, 1, 1, 0xD2D2D2u,
  592. cx, cy, cw, ch);
  593. }
  594. }
  595. }
  596. /* Front-to-back window order: popovers float above everything, then the
  597. * focused window, then the others in descending slot order. Painting and
  598. * hit-testing share it. */
  599. static unsigned int build_order(int *order)
  600. {
  601. unsigned int i, n = 0;
  602. for (i = 0; i < WM_MAX_WINDOWS; i++)
  603. if (wins[i].info.used && wins[i].info.popover)
  604. order[n++] = (int)i;
  605. for (i = 0; i < WM_MAX_WINDOWS; i++)
  606. if (wins[i].info.used && wins[i].info.focused &&
  607. !wins[i].info.popover)
  608. order[n++] = (int)i;
  609. for (i = WM_MAX_WINDOWS; i-- > 0;)
  610. if (wins[i].info.used && !wins[i].info.focused &&
  611. !wins[i].info.popover)
  612. order[n++] = (int)i;
  613. return n;
  614. }
  615. struct orect {
  616. int x, y;
  617. unsigned int w, h;
  618. };
  619. #define OCC_MAX 64
  620. /* r minus cut, split into up to four disjoint rectangles. */
  621. static unsigned int rect_sub(struct orect *out, const struct orect *r,
  622. const struct orect *cut)
  623. {
  624. int rx0 = r->x, ry0 = r->y;
  625. int rx1 = r->x + (int)r->w, ry1 = r->y + (int)r->h;
  626. int cx0 = cut->x, cy0 = cut->y;
  627. int cx1 = cut->x + (int)cut->w, cy1 = cut->y + (int)cut->h;
  628. unsigned int n = 0;
  629. if (cx0 < rx0) cx0 = rx0;
  630. if (cy0 < ry0) cy0 = ry0;
  631. if (cx1 > rx1) cx1 = rx1;
  632. if (cy1 > ry1) cy1 = ry1;
  633. if (cx1 <= cx0 || cy1 <= cy0) {
  634. out[0] = *r;
  635. return 1;
  636. }
  637. if (cy0 > ry0) {
  638. out[n].x = rx0;
  639. out[n].y = ry0;
  640. out[n].w = (unsigned int)(rx1 - rx0);
  641. out[n].h = (unsigned int)(cy0 - ry0);
  642. n++;
  643. }
  644. if (cy1 < ry1) {
  645. out[n].x = rx0;
  646. out[n].y = cy1;
  647. out[n].w = (unsigned int)(rx1 - rx0);
  648. out[n].h = (unsigned int)(ry1 - cy1);
  649. n++;
  650. }
  651. if (cx0 > rx0) {
  652. out[n].x = rx0;
  653. out[n].y = cy0;
  654. out[n].w = (unsigned int)(cx0 - rx0);
  655. out[n].h = (unsigned int)(cy1 - cy0);
  656. n++;
  657. }
  658. if (cx1 < rx1) {
  659. out[n].x = cx1;
  660. out[n].y = cy0;
  661. out[n].w = (unsigned int)(rx1 - cx1);
  662. out[n].h = (unsigned int)(cy1 - cy0);
  663. n++;
  664. }
  665. return n;
  666. }
  667. /* Back-to-front repaint, used only if the visible-region list overflows. */
  668. static void draw_rect_naive(int x, int y, unsigned int w, unsigned int h)
  669. {
  670. unsigned int i;
  671. usurf_fill(&screen, x, y, w, h, COL_BG);
  672. for (i = 0; i < WM_MAX_WINDOWS; i++)
  673. if (wins[i].info.used && !wins[i].info.focused &&
  674. !wins[i].info.popover &&
  675. x < wins[i].info.x + (int)wins[i].info.w &&
  676. x + (int)w > wins[i].info.x &&
  677. y < wins[i].info.y + (int)wins[i].info.h &&
  678. y + (int)h > wins[i].info.y)
  679. draw_window_clip(i, x, y, w, h);
  680. for (i = 0; i < WM_MAX_WINDOWS; i++)
  681. if (wins[i].info.used && wins[i].info.focused &&
  682. !wins[i].info.popover &&
  683. x < wins[i].info.x + (int)wins[i].info.w &&
  684. x + (int)w > wins[i].info.x &&
  685. y < wins[i].info.y + (int)wins[i].info.h &&
  686. y + (int)h > wins[i].info.y)
  687. draw_window_clip(i, x, y, w, h);
  688. for (i = 0; i < WM_MAX_WINDOWS; i++)
  689. if (wins[i].info.used && wins[i].info.popover &&
  690. x < wins[i].info.x + (int)wins[i].info.w &&
  691. x + (int)w > wins[i].info.x &&
  692. y < wins[i].info.y + (int)wins[i].info.h &&
  693. y + (int)h > wins[i].info.y)
  694. draw_window_clip(i, x, y, w, h);
  695. }
  696. static void draw_rect(int x, int y, unsigned int w, unsigned int h)
  697. {
  698. static struct orect cur[OCC_MAX], nxt[OCC_MAX];
  699. struct orect dmg;
  700. int order[WM_MAX_WINDOWS];
  701. unsigned int n, i, j, ncur, nnext;
  702. int overflow = 0;
  703. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  704. if (w == 0 || h == 0)
  705. return;
  706. if (x0 < 0) x0 = 0;
  707. if (y0 < 0) y0 = 0;
  708. if (x1 > (int)fbw) x1 = (int)fbw;
  709. if (y1 > (int)fbh) y1 = (int)fbh;
  710. if (x1 <= x0 || y1 <= y0)
  711. return;
  712. dmg.x = x0;
  713. dmg.y = y0;
  714. dmg.w = (unsigned int)(x1 - x0);
  715. dmg.h = (unsigned int)(y1 - y0);
  716. usurf_fill(&screen, dmg.x, dmg.y, dmg.w, dmg.h, COL_BG);
  717. n = build_order(order);
  718. for (i = 0; i < n; i++) {
  719. struct uwin *u = &wins[order[i]];
  720. int wx0 = u->info.x, wy0 = u->info.y;
  721. int wx1 = wx0 + (int)u->info.w, wy1 = wy0 + (int)u->info.h;
  722. if (wx0 < dmg.x) wx0 = dmg.x;
  723. if (wy0 < dmg.y) wy0 = dmg.y;
  724. if (wx1 > x1) wx1 = x1;
  725. if (wy1 > y1) wy1 = y1;
  726. if (wx1 <= wx0 || wy1 <= wy0)
  727. continue;
  728. cur[0].x = wx0;
  729. cur[0].y = wy0;
  730. cur[0].w = (unsigned int)(wx1 - wx0);
  731. cur[0].h = (unsigned int)(wy1 - wy0);
  732. ncur = 1;
  733. for (j = 0; j < i && ncur; j++) {
  734. struct uwin *f = &wins[order[j]];
  735. struct orect fr;
  736. unsigned int k;
  737. fr.x = f->info.x;
  738. fr.y = f->info.y;
  739. fr.w = f->info.w;
  740. fr.h = f->info.h;
  741. nnext = 0;
  742. for (k = 0; k < ncur; k++) {
  743. if (nnext + 4 > OCC_MAX) {
  744. overflow = 1;
  745. break;
  746. }
  747. nnext += rect_sub(&nxt[nnext], &cur[k], &fr);
  748. }
  749. if (overflow)
  750. break;
  751. for (k = 0; k < nnext; k++)
  752. cur[k] = nxt[k];
  753. ncur = nnext;
  754. }
  755. if (overflow)
  756. break;
  757. for (j = 0; j < ncur; j++)
  758. draw_window_clip((unsigned int)order[i], cur[j].x,
  759. cur[j].y, cur[j].w, cur[j].h);
  760. }
  761. if (overflow)
  762. draw_rect_naive(dmg.x, dmg.y, dmg.w, dmg.h);
  763. }
  764. static int window_at(int mx, int my)
  765. {
  766. int order[WM_MAX_WINDOWS];
  767. unsigned int n = build_order(order), i;
  768. for (i = 0; i < n; i++) {
  769. struct wm_window_info *w = &wins[order[i]].info;
  770. if (!w->popover)
  771. continue;
  772. if (mx >= w->x && my >= w->y &&
  773. mx < w->x + (int)w->w &&
  774. my < w->y + (int)w->h)
  775. return order[i];
  776. }
  777. for (i = 0; i < n; i++) {
  778. struct wm_window_info *w = &wins[order[i]].info;
  779. if (w->popover)
  780. continue;
  781. if (mx >= w->x && my >= w->y &&
  782. mx < w->x + (int)w->w &&
  783. my < w->y + (int)w->h)
  784. return order[i];
  785. }
  786. return -1;
  787. }
  788. static void clamp_geom(struct uwin *u, int *x, int *y)
  789. {
  790. /* Match wm_clamp_window(): allow the window to hang off the left,
  791. * right and bottom edges but keep the title bar reachable and below
  792. * the system bar. */
  793. int minx = WM_DRAG_VISIBLE - (int)u->info.w;
  794. int maxx = (int)fbw - WM_DRAG_VISIBLE;
  795. int miny = WM_BAR_H;
  796. int maxy = (int)fbh - WM_TITLE_H;
  797. if (minx > maxx)
  798. minx = maxx = 0;
  799. if (miny > maxy)
  800. miny = maxy = WM_BAR_H;
  801. if (*x < minx)
  802. *x = minx;
  803. else if (*x > maxx)
  804. *x = maxx;
  805. if (*y < miny)
  806. *y = miny;
  807. else if (*y > maxy)
  808. *y = maxy;
  809. }
  810. static void add_rect(struct wm_present_info *p, int x, int y,
  811. unsigned int w, unsigned int h)
  812. {
  813. if (w == 0 || h == 0)
  814. return;
  815. if (p->count < 4) {
  816. p->rect[p->count].x = x;
  817. p->rect[p->count].y = y;
  818. p->rect[p->count].w = w;
  819. p->rect[p->count].h = h;
  820. p->count++;
  821. } else {
  822. /* Four is enough for normal updates; fold the rest into the
  823. * last rect rather than dropping pixels. */
  824. int x0 = p->rect[3].x, y0 = p->rect[3].y;
  825. int x1 = x0 + (int)p->rect[3].w, y1 = y0 + (int)p->rect[3].h;
  826. int nx1 = x + (int)w, ny1 = y + (int)h;
  827. if (x < x0) x0 = x;
  828. if (y < y0) y0 = y;
  829. if (nx1 > x1) x1 = nx1;
  830. if (ny1 > y1) y1 = ny1;
  831. p->rect[3].x = x0;
  832. p->rect[3].y = y0;
  833. p->rect[3].w = (unsigned int)(x1 - x0);
  834. p->rect[3].h = (unsigned int)(y1 - y0);
  835. }
  836. }
  837. /* Redraw the power tooltip above whatever a render pass just painted. */
  838. static void bar_tooltip_overlay(struct wm_present_info *p)
  839. {
  840. if (!bar_pwr_tip_on || !bar_pwr_tip_want)
  841. return;
  842. bar_draw_tooltip();
  843. add_rect(p, bar_pwr_tip_on_x, bar_pwr_tip_on_y,
  844. bar_pwr_tip_on_w, bar_pwr_tip_on_h);
  845. }
  846. /* Center the brightness bubble in the work area, clamped on small screens. */
  847. static void osd_geom(void)
  848. {
  849. int x, y;
  850. osd_w = OSD_W;
  851. osd_h = OSD_H;
  852. x = ((int)fbw - (int)osd_w) / 2;
  853. y = WM_BAR_H + ((int)fbh - WM_BAR_H - (int)osd_h) * 3 / 5;
  854. if (x < 0)
  855. x = 0;
  856. if (x + (int)osd_w > (int)fbw)
  857. x = (int)fbw - (int)osd_w;
  858. if (y < WM_BAR_H)
  859. y = WM_BAR_H;
  860. if (y + (int)osd_h > (int)fbh)
  861. y = (int)fbh - (int)osd_h;
  862. osd_x = x;
  863. osd_y = y;
  864. }
  865. /* A small sun: a filled circle plus four rays, all from 1-pixel fills. */
  866. static void osd_sun(int cx, int cy)
  867. {
  868. static const unsigned char half[11] = { 2, 4, 5, 6, 6, 6, 6, 6, 5, 4, 2 };
  869. int i;
  870. for (i = -5; i <= 5; i++)
  871. usurf_fill(&screen, cx - half[i + 5], cy + i,
  872. half[i + 5] * 2 + 1, 1, 0xFFD250u);
  873. usurf_fill(&screen, cx - 1, cy - 9, 2, 3, 0xFFD250u);
  874. usurf_fill(&screen, cx - 1, cy + 7, 2, 3, 0xFFD250u);
  875. usurf_fill(&screen, cx - 9, cy - 1, 3, 2, 0xFFD250u);
  876. usurf_fill(&screen, cx + 7, cy - 1, 3, 2, 0xFFD250u);
  877. }
  878. static void osd_draw(void)
  879. {
  880. char pct[8];
  881. unsigned int n = 0;
  882. unsigned int bar_x, bar_y, bar_w;
  883. int x, y, tw;
  884. osd_geom();
  885. x = osd_x;
  886. y = osd_y;
  887. usurf_fill(&screen, x, y, osd_w, osd_h, 0x26263Au);
  888. usurf_fill(&screen, x, y, osd_w, 1, 0x70709Au);
  889. usurf_fill(&screen, x, y + (int)osd_h - 1, osd_w, 1, 0x70709Au);
  890. usurf_fill(&screen, x, y + 1, 1, osd_h - 2, 0x70709Au);
  891. usurf_fill(&screen, x + (int)osd_w - 1, y + 1, 1, osd_h - 2,
  892. 0x70709Au);
  893. osd_sun(x + 22, y + (int)osd_h / 2);
  894. if (osd_level >= 100) {
  895. pct[n++] = '1';
  896. pct[n++] = '0';
  897. pct[n++] = '0';
  898. } else {
  899. if (osd_level >= 10)
  900. pct[n++] = (char)('0' + osd_level / 10);
  901. pct[n++] = (char)('0' + osd_level % 10);
  902. }
  903. pct[n++] = '%';
  904. pct[n] = '\0';
  905. usurf_text(&screen, x + 42, y + 9, "Brightness", 0xE8E8F6u,
  906. x, y, osd_w, osd_h);
  907. tw = (int)usurf_text_width(&screen, pct);
  908. usurf_text(&screen, x + (int)osd_w - 14 - tw, y + 9, pct,
  909. 0xFFD250u, x, y, osd_w, osd_h);
  910. bar_x = (unsigned int)(x + 42);
  911. bar_y = (unsigned int)(y + 35);
  912. bar_w = osd_w - 42 - 14;
  913. usurf_fill(&screen, (int)bar_x, (int)bar_y, bar_w, 10, 0x44445Au);
  914. usurf_fill(&screen, (int)bar_x, (int)bar_y,
  915. (bar_w * (unsigned int)osd_level + 50) / 100, 10,
  916. 0xFFD250u);
  917. }
  918. /* Put the power tooltip and the brightness OSD back on top after a render
  919. * pass that may have overwritten them. */
  920. static void wm_overlays(struct wm_present_info *p)
  921. {
  922. bar_tooltip_overlay(p);
  923. if (osd_show) {
  924. osd_draw();
  925. add_rect(p, osd_x, osd_y, osd_w, osd_h);
  926. }
  927. }
  928. /* A focused window is topmost, so the shadow contains exactly its pixels in
  929. * the old rectangle. Move that block and repaint only the old part the new
  930. * rectangle does not cover. When either rectangle is partly off-screen the
  931. * shadow has no valid pixels for the hidden part, so repaint the new rect
  932. * instead of copying desktop background into the window. */
  933. static void move_window_pixels(struct uwin *u, int ox, int oy, int nx, int ny)
  934. {
  935. struct wm_present_info present;
  936. struct orect oldr, newr, pieces[4];
  937. unsigned int np, k;
  938. int w = (int)u->info.w, h = (int)u->info.h;
  939. int old_on = ox >= 0 && oy >= 0 &&
  940. ox + w <= (int)fbw && oy + h <= (int)fbh;
  941. int new_on = nx >= 0 && ny >= 0 &&
  942. nx + w <= (int)fbw && ny + h <= (int)fbh;
  943. if (u_wm_begin() != 0)
  944. return;
  945. if (old_on && new_on)
  946. usurf_copy(&screen, nx, ny, ox, oy, u->info.w, u->info.h);
  947. else
  948. draw_rect(nx, ny, u->info.w, u->info.h);
  949. oldr.x = ox;
  950. oldr.y = oy;
  951. oldr.w = u->info.w;
  952. oldr.h = u->info.h;
  953. newr.x = nx;
  954. newr.y = ny;
  955. newr.w = u->info.w;
  956. newr.h = u->info.h;
  957. np = rect_sub(pieces, &oldr, &newr);
  958. for (k = 0; k < np; k++)
  959. draw_rect(pieces[k].x, pieces[k].y, pieces[k].w, pieces[k].h);
  960. present.count = 0;
  961. present.immediate = 1;
  962. add_rect(&present, nx, ny, u->info.w, u->info.h);
  963. for (k = 0; k < np; k++)
  964. add_rect(&present, pieces[k].x, pieces[k].y,
  965. pieces[k].w, pieces[k].h);
  966. wm_overlays(&present);
  967. u_wm_present(&present);
  968. }
  969. static int iabs(int v)
  970. {
  971. return v < 0 ? -v : v;
  972. }
  973. /* Repaint the old and new rectangles of a geometry change. */
  974. static void redraw_geom(struct uwin *u, int ox, int oy,
  975. unsigned int ow, unsigned int oh)
  976. {
  977. struct wm_present_info present;
  978. unsigned int i;
  979. present.count = 0;
  980. present.immediate = 1;
  981. add_rect(&present, ox, oy, ow, oh);
  982. add_rect(&present, u->info.x, u->info.y, u->info.w, u->info.h);
  983. if (u_wm_begin() == 0) {
  984. for (i = 0; i < present.count; i++)
  985. draw_rect(present.rect[i].x, present.rect[i].y,
  986. present.rect[i].w, present.rect[i].h);
  987. wm_overlays(&present);
  988. u_wm_present(&present);
  989. }
  990. }
  991. /* Resize (when needed), move and remap a window in one step. */
  992. static int set_window_geometry(struct uwin *u, unsigned int idx,
  993. int nx, int ny, unsigned int nw, unsigned int nh)
  994. {
  995. struct wm_window_info ni;
  996. int ox = u->info.x, oy = u->info.y;
  997. unsigned int ow = u->info.w, oh = u->info.h;
  998. if ((int)nw != (int)ow || (int)nh != (int)oh) {
  999. if (u_wm_resize(idx, nw - 2, nh - WM_TITLE_H - 2) != 0)
  1000. return -1;
  1001. }
  1002. if (nx != ox || ny != oy)
  1003. u_wm_set_geom(idx, nx, ny);
  1004. u->mapped = 0;
  1005. if (u_wm_info(idx, &ni) == 1)
  1006. u->info = ni;
  1007. if (u_wm_map(idx, &u->map) == 0)
  1008. u->mapped = 1;
  1009. redraw_geom(u, ox, oy, ow, oh);
  1010. return 0;
  1011. }
  1012. static int restore_window(struct uwin *u, unsigned int idx)
  1013. {
  1014. if (!u->maximized)
  1015. return 0;
  1016. if (set_window_geometry(u, idx, u->restore_x, u->restore_y,
  1017. u->restore_w, u->restore_h) != 0)
  1018. return -1;
  1019. u->maximized = 0;
  1020. return 0;
  1021. }
  1022. /* Double-click title bar: fill the work area below the system bar, or go back
  1023. * to the geometry the window had before it was maximized. */
  1024. static int toggle_maximize(struct uwin *u, unsigned int idx)
  1025. {
  1026. if (u->maximized)
  1027. return restore_window(u, idx);
  1028. u->restore_x = u->info.x;
  1029. u->restore_y = u->info.y;
  1030. u->restore_w = u->info.w;
  1031. u->restore_h = u->info.h;
  1032. if (set_window_geometry(u, idx, 0, WM_BAR_H,
  1033. fbw, fbh - WM_BAR_H) != 0)
  1034. return -1;
  1035. u->maximized = 1;
  1036. return 0;
  1037. }
  1038. /* The top-bar date/time text is also a button: it opens the Date & Time
  1039. * editor. The rectangle is recorded by draw_bar(). */
  1040. static int bar_clock_hit(unsigned int mx, unsigned int my)
  1041. {
  1042. return bar_clock_w && bar_clock_x >= 0 && my < WM_BAR_H &&
  1043. (int)mx >= bar_clock_x - 5 &&
  1044. (int)mx < bar_clock_x + (int)bar_clock_w + 5;
  1045. }
  1046. static int bar_net_hit(unsigned int mx, unsigned int my)
  1047. {
  1048. return bar_net_w && bar_net_x >= 0 && my < WM_BAR_H &&
  1049. (int)mx >= bar_net_x - 3 &&
  1050. (int)mx < bar_net_x + (int)bar_net_w + 3;
  1051. }
  1052. /* The AC/battery icon has no click action, but hovering it reveals the
  1053. * percentage tooltip below it; the tooltip area keeps the hover alive. */
  1054. static int bar_pwr_hit(unsigned int mx, unsigned int my)
  1055. {
  1056. if (my >= WM_BAR_H)
  1057. return bar_pwr_tip_on &&
  1058. (int)mx >= bar_pwr_tip_on_x &&
  1059. (int)mx < bar_pwr_tip_on_x + (int)bar_pwr_tip_on_w &&
  1060. (int)my >= bar_pwr_tip_on_y &&
  1061. (int)my < bar_pwr_tip_on_y + (int)bar_pwr_tip_on_h;
  1062. return bar_pwr_x >= 0 &&
  1063. (int)mx >= bar_pwr_x - 3 &&
  1064. (int)mx < bar_pwr_x + BAR_PWR_W + 3;
  1065. }
  1066. static void do_update(struct wm_status *st)
  1067. {
  1068. struct wm_present_info present;
  1069. int need = 0;
  1070. int bar_dirty = 0;
  1071. unsigned int i;
  1072. char dt[20];
  1073. static char last_dt[20];
  1074. int kexec_changed = (st->kexec_busy != bar_kexec_busy) ||
  1075. (st->kexec_busy && st->kexec_progress != bar_kexec_progress);
  1076. unsigned int msg_now = u_ticks_ms();
  1077. int osd_hide = 0, osd_repaint = 0;
  1078. /* The kernel bumps backlight_seq on every brightness key/command.
  1079. * Pop the OSD on a change and keep it up for OSD_MS after the last
  1080. * one. The first poll only records the current sequence, so a WM
  1081. * restart does not flash a stale bubble. */
  1082. if (st->backlight_present) {
  1083. if (!osd_seen_valid) {
  1084. osd_seen_valid = 1;
  1085. osd_seen_seq = st->backlight_seq;
  1086. } else if (st->backlight_seq != osd_seen_seq) {
  1087. osd_seen_seq = st->backlight_seq;
  1088. osd_level = (int)st->backlight_level;
  1089. osd_until = msg_now + OSD_MS;
  1090. osd_show = 1;
  1091. }
  1092. } else if (osd_show) {
  1093. osd_show = 0;
  1094. osd_hide = 1;
  1095. }
  1096. if (osd_show && (int)(msg_now - osd_until) >= 0) {
  1097. osd_show = 0;
  1098. osd_hide = 1;
  1099. }
  1100. if (osd_hide && osd_visible)
  1101. osd_repaint = 1;
  1102. /* Spawn the first terminal once the kernel says the storage scan is
  1103. * done, so a shell command can never race the mount table. */
  1104. if (st->boot_ready && !boot_term_spawned) {
  1105. boot_term_spawned = 1;
  1106. if (!free_window_slot()) {
  1107. bar_notice("window limit");
  1108. full_redraw = 1;
  1109. } else if (u_wm_create_term() < 0) {
  1110. bar_notice("cannot start term");
  1111. full_redraw = 1;
  1112. } else {
  1113. full_redraw = 1;
  1114. }
  1115. }
  1116. if (bar_msg_dirty ||
  1117. (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)) {
  1118. bar_dirty = 1;
  1119. bar_msg_dirty = 0;
  1120. if (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)
  1121. bar_msg_until = 0;
  1122. }
  1123. bar_kexec_busy = st->kexec_busy;
  1124. bar_kexec_progress = st->kexec_progress;
  1125. bar_kexec_total = st->kexec_total;
  1126. if (kexec_changed)
  1127. bar_dirty = 1;
  1128. {
  1129. int hot = bar_clock_hit(st->mouse.x, st->mouse.y);
  1130. if (hot != bar_clock_hot) {
  1131. bar_clock_hot = hot;
  1132. bar_dirty = 1;
  1133. }
  1134. }
  1135. {
  1136. int hot = bar_net_hit(st->mouse.x, st->mouse.y);
  1137. if (hot != bar_net_hot) {
  1138. bar_net_hot = hot;
  1139. bar_dirty = 1;
  1140. }
  1141. }
  1142. {
  1143. int hot = bar_pwr_hit(st->mouse.x, st->mouse.y);
  1144. if (hot != bar_pwr_hot) {
  1145. bar_pwr_hot = hot;
  1146. bar_dirty = 1;
  1147. }
  1148. }
  1149. /* Refresh cached window state and mappings before opening a frame:
  1150. * MAP may unmap/remap content. */
  1151. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1152. struct wm_window_info info;
  1153. int r = u_wm_info(i, &info);
  1154. if (r < 0) {
  1155. u_exit(1);
  1156. }
  1157. if (r == 0 || !info.used) {
  1158. if (wins[i].info.used)
  1159. full_redraw = 1;
  1160. wins[i].info.used = 0;
  1161. wins[i].mapped = 0;
  1162. continue;
  1163. }
  1164. if (!wins[i].info.used ||
  1165. info.content_gen != wins[i].info.content_gen)
  1166. full_redraw = 1;
  1167. wins[i].info = info;
  1168. if (!wins[i].mapped ||
  1169. wins[i].map.generation != info.content_gen) {
  1170. struct wm_map_info map;
  1171. if (u_wm_map(i, &map) == 0) {
  1172. wins[i].map = map;
  1173. wins[i].mapped = 1;
  1174. } else {
  1175. wins[i].mapped = 0;
  1176. }
  1177. }
  1178. }
  1179. /* Popovers are WM-anchored overlays, right-aligned to the network
  1180. * status icon and just below the bar. The WM owns the geometry, so
  1181. * the app does not need a placement ABI. */
  1182. {
  1183. int netmenu_found = 0;
  1184. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1185. struct wm_window_info ni;
  1186. int nx, ny;
  1187. if (!wins[i].info.used || !wins[i].info.popover)
  1188. continue;
  1189. if (netmenu_pid > 0 &&
  1190. wins[i].info.pid == (unsigned int)netmenu_pid)
  1191. netmenu_found = 1;
  1192. nx = (bar_net_x >= 0 ? bar_net_x + (int)bar_net_w :
  1193. (int)fbw - 4) - (int)wins[i].info.w;
  1194. ny = WM_BAR_H + 4;
  1195. if (nx != wins[i].info.x || ny != wins[i].info.y) {
  1196. if (u_wm_set_geom(i, nx, ny) == 0 &&
  1197. u_wm_info(i, &ni) == 1) {
  1198. wins[i].info = ni;
  1199. full_redraw = 1;
  1200. }
  1201. }
  1202. }
  1203. if (!netmenu_found && netmenu_pid > 0 &&
  1204. !u_alive((unsigned int)netmenu_pid))
  1205. netmenu_pid = 0;
  1206. }
  1207. /* The RTC is slow (CMOS polling) and the temperature/battery state only
  1208. * changes once a second; query them at 1 Hz instead of every 10 ms. */
  1209. {
  1210. static unsigned int last_rtc_ms;
  1211. unsigned int now_ms = u_ticks_ms();
  1212. if (full_redraw || now_ms - last_rtc_ms >= 1000) {
  1213. last_rtc_ms = now_ms;
  1214. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  1215. for (i = 0; i < sizeof(dt); i++)
  1216. if (dt[i] != last_dt[i]) {
  1217. for (i = 0; i < sizeof(dt); i++)
  1218. last_dt[i] = dt[i];
  1219. bar_dirty = 1;
  1220. break;
  1221. }
  1222. }
  1223. {
  1224. struct sys_cputemp ct;
  1225. if (u_cpu_temp(&ct) == 0 &&
  1226. ct.temp_c != bar_temp_c) {
  1227. bar_temp_c = ct.temp_c;
  1228. bar_dirty = 1;
  1229. }
  1230. }
  1231. {
  1232. struct sys_powerinfo pi;
  1233. int pct = -1, charging = 0, ac = -1;
  1234. unsigned int bi;
  1235. if (u_power_info(&pi, 0) == 0) {
  1236. /* A laptop EC (ThinkPad or Chrome
  1237. * EC) marks a machine with AC/battery;
  1238. * desktops show neither. */
  1239. if (pi.present) {
  1240. ac = pi.ac_online;
  1241. for (bi = 0; bi < SYS_POWER_BATT_MAX;
  1242. bi++) {
  1243. struct sys_battery *b =
  1244. &pi.battery[bi];
  1245. if (!b->present ||
  1246. !b->percent_valid)
  1247. continue;
  1248. pct = (int)b->percent;
  1249. charging = (int)b->charging;
  1250. break;
  1251. }
  1252. }
  1253. if (pct != bar_batt_pct ||
  1254. charging != bar_batt_charging ||
  1255. ac != bar_ac_online) {
  1256. bar_batt_pct = pct;
  1257. bar_batt_charging = charging;
  1258. bar_ac_online = ac;
  1259. bar_dirty = 1;
  1260. }
  1261. }
  1262. }
  1263. if (poll_net_status())
  1264. bar_dirty = 1;
  1265. }
  1266. }
  1267. if (full_redraw) {
  1268. if (u_wm_begin() != 0) {
  1269. full_redraw = 1;
  1270. return;
  1271. }
  1272. full_redraw = 0;
  1273. need = 1;
  1274. {
  1275. draw_rect(0, 0, fbw, fbh);
  1276. draw_bar();
  1277. bar_tooltip_commit();
  1278. if (osd_show)
  1279. osd_draw();
  1280. osd_visible = osd_show;
  1281. present.count = 0;
  1282. add_rect(&present, 0, 0, fbw, fbh);
  1283. present.immediate = 0;
  1284. u_wm_present(&present);
  1285. /* Ack every window that was clean at the time of the
  1286. * redraw. If new damage arrived during it, its seq
  1287. * differs and the kernel leaves it dirty. */
  1288. for (i = 0; i < WM_MAX_WINDOWS; i++)
  1289. if (wins[i].info.used && wins[i].mapped)
  1290. u_wm_ack(i, wins[i].info.dirty_seq);
  1291. }
  1292. return;
  1293. }
  1294. /* Partial cell/canvas updates and the bar clock. */
  1295. present.count = 0;
  1296. present.immediate = 0;
  1297. if (osd_repaint) {
  1298. add_rect(&present, osd_x, osd_y, osd_w, osd_h);
  1299. need = 1;
  1300. }
  1301. if (osd_show)
  1302. need = 1;
  1303. if (bar_dirty) {
  1304. need = 1;
  1305. add_rect(&present, 0, 0, fbw, WM_BAR_H);
  1306. }
  1307. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1308. struct uwin *u = &wins[i];
  1309. struct wm_window_info *w = &u->info;
  1310. int rx, ry;
  1311. unsigned int rw, rh;
  1312. if (!w->used || !w->dirty)
  1313. continue;
  1314. if (!w->dmg_w || !w->dmg_h)
  1315. continue;
  1316. rx = w->x + (w->popover ? 0 : 1) + (int)w->dmg_x;
  1317. ry = w->y + (w->popover ? 0 : 1 + WM_TITLE_H) +
  1318. (int)w->dmg_y;
  1319. rw = w->dmg_w;
  1320. rh = w->dmg_h;
  1321. add_rect(&present, rx, ry, rw, rh);
  1322. u->ack_seq = u->mapped ? w->dirty_seq : 0;
  1323. need = 1;
  1324. }
  1325. /* Repaint the previous tooltip area so hiding or moving it restores
  1326. * the window/desktop underneath; the new one is drawn after the
  1327. * windows below. */
  1328. if (bar_pwr_tip_on) {
  1329. need = 1;
  1330. add_rect(&present, bar_pwr_tip_on_x, bar_pwr_tip_on_y,
  1331. bar_pwr_tip_on_w, bar_pwr_tip_on_h);
  1332. }
  1333. if (!need) {
  1334. /* Drag movement is handled below; a stationary mouse can be
  1335. * left to the kernel's cursor overlay. */
  1336. return;
  1337. }
  1338. if (u_wm_begin() != 0)
  1339. return;
  1340. for (i = 0; i < present.count; i++)
  1341. draw_rect(present.rect[i].x, present.rect[i].y,
  1342. present.rect[i].w, present.rect[i].h);
  1343. if (bar_dirty)
  1344. draw_bar();
  1345. if (bar_pwr_tip_want) {
  1346. bar_tooltip_commit();
  1347. add_rect(&present, bar_pwr_tip_on_x, bar_pwr_tip_on_y,
  1348. bar_pwr_tip_on_w, bar_pwr_tip_on_h);
  1349. } else {
  1350. bar_pwr_tip_on = 0;
  1351. }
  1352. if (osd_show) {
  1353. osd_draw();
  1354. add_rect(&present, osd_x, osd_y, osd_w, osd_h);
  1355. }
  1356. osd_visible = osd_show;
  1357. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1358. struct uwin *u = &wins[i];
  1359. if (!u->info.used || !u->info.dirty || !u->mapped ||
  1360. !u->ack_seq)
  1361. continue;
  1362. if (u->ack_seq == u->info.dirty_seq)
  1363. u_wm_ack(i, u->info.dirty_seq);
  1364. }
  1365. u_wm_present(&present);
  1366. }
  1367. static int spawn_app(const char *path)
  1368. {
  1369. int pid = u_spawn(path, "", 0, "/");
  1370. if (pid > 0)
  1371. full_redraw = 1;
  1372. return pid;
  1373. }
  1374. static void spawn_app_checked(const char *path)
  1375. {
  1376. if (!free_window_slot()) {
  1377. bar_notice("window limit");
  1378. full_redraw = 1;
  1379. return;
  1380. }
  1381. if (spawn_app(path) < 0) {
  1382. bar_notice("spawn failed");
  1383. full_redraw = 1;
  1384. }
  1385. }
  1386. /* The network icon opens one menu at a time; a second click closes it. */
  1387. static void open_netmenu(void)
  1388. {
  1389. unsigned int i;
  1390. if (netmenu_pid > 0 && u_alive((unsigned int)netmenu_pid)) {
  1391. for (i = 0; i < WM_MAX_WINDOWS; i++)
  1392. if (wins[i].info.used &&
  1393. wins[i].info.pid == (unsigned int)netmenu_pid) {
  1394. if (u_wm_close(i) == 0) {
  1395. netmenu_pid = 0;
  1396. full_redraw = 1;
  1397. } else {
  1398. u_wm_focus(i);
  1399. full_redraw = 1;
  1400. }
  1401. return;
  1402. }
  1403. /* The process is alive but has not created its window yet. */
  1404. return;
  1405. }
  1406. if (!free_window_slot()) {
  1407. bar_notice("window limit");
  1408. full_redraw = 1;
  1409. return;
  1410. }
  1411. netmenu_pid = u_spawn("/ramfs/bin/netmenu", "", 0, "/");
  1412. if (netmenu_pid < 0) {
  1413. netmenu_pid = 0;
  1414. bar_notice("spawn failed");
  1415. full_redraw = 1;
  1416. } else {
  1417. full_redraw = 1;
  1418. }
  1419. }
  1420. /* Index of a popover containing the point, or -1. */
  1421. static int popover_under(unsigned int mx, unsigned int my)
  1422. {
  1423. unsigned int i;
  1424. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1425. struct wm_window_info *w = &wins[i].info;
  1426. if (w->used && w->popover &&
  1427. mx >= (unsigned int)w->x && my >= (unsigned int)w->y &&
  1428. mx < (unsigned int)(w->x + (int)w->w) &&
  1429. my < (unsigned int)(w->y + (int)w->h))
  1430. return (int)i;
  1431. }
  1432. return -1;
  1433. }
  1434. /* A press anywhere outside an open popover dismisses it; the click then
  1435. * continues normally so the target under the pointer still reacts. */
  1436. static void dismiss_popover_outside(unsigned int mx, unsigned int my)
  1437. {
  1438. unsigned int i;
  1439. if (popover_under(mx, my) >= 0)
  1440. return;
  1441. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1442. if (!wins[i].info.used || !wins[i].info.popover)
  1443. continue;
  1444. if (wins[i].info.pid == (unsigned int)netmenu_pid)
  1445. netmenu_pid = 0;
  1446. if (u_wm_close(i) == 0) {
  1447. wins[i].info.used = 0;
  1448. wins[i].mapped = 0;
  1449. full_redraw = 1;
  1450. }
  1451. break;
  1452. }
  1453. }
  1454. static void handle_mouse(struct wm_status *st)
  1455. {
  1456. unsigned int buttons = st->mouse.buttons;
  1457. unsigned int mx = st->mouse.x, my = st->mouse.y;
  1458. int down = (buttons & 1) != 0;
  1459. int was = (prev_buttons & 1) != 0;
  1460. if (down && !was) {
  1461. int prev_title_id = last_title_id;
  1462. unsigned int now = u_ticks_ms();
  1463. if (!bar_net_hit(mx, my))
  1464. dismiss_popover_outside(mx, my);
  1465. last_title_id = -1;
  1466. if (bar_net_hit(mx, my)) {
  1467. open_netmenu();
  1468. } else if (bar_clock_hit(mx, my)) {
  1469. spawn_app_checked("/ramfs/bin/settime");
  1470. } else if (mx >= WM_BTN_X && my >= WM_BTN_Y &&
  1471. mx < WM_BTN_X + WM_BTN_W && my < WM_BTN_Y + WM_BTN_H) {
  1472. if (!free_window_slot()) {
  1473. bar_notice("window limit");
  1474. full_redraw = 1;
  1475. } else if (u_wm_create_term() < 0) {
  1476. bar_notice("cannot start term");
  1477. full_redraw = 1;
  1478. } else {
  1479. full_redraw = 1;
  1480. }
  1481. } else if (mx >= WM_BTN_CLOCK_X && my >= WM_BTN_CLOCK_Y &&
  1482. mx < WM_BTN_CLOCK_X + WM_BTN_CLOCK_W &&
  1483. my < WM_BTN_CLOCK_Y + WM_BTN_CLOCK_H) {
  1484. spawn_app_checked("/ramfs/bin/clock");
  1485. } else if (mx >= WM_BTN_COLORS_X && my >= WM_BTN_COLORS_Y &&
  1486. mx < WM_BTN_COLORS_X + WM_BTN_COLORS_W &&
  1487. my < WM_BTN_COLORS_Y + WM_BTN_COLORS_H) {
  1488. spawn_app_checked("/ramfs/bin/colors");
  1489. } else if (mx >= WM_BTN_FILES_X && my >= WM_BTN_FILES_Y &&
  1490. mx < WM_BTN_FILES_X + WM_BTN_FILES_W &&
  1491. my < WM_BTN_FILES_Y + WM_BTN_FILES_H) {
  1492. spawn_app_checked("/ramfs/bin/files");
  1493. } else if (mx >= WM_BTN_MINES_X && my >= WM_BTN_MINES_Y &&
  1494. mx < WM_BTN_MINES_X + WM_BTN_MINES_W &&
  1495. my < WM_BTN_MINES_Y + WM_BTN_MINES_H) {
  1496. spawn_app_checked("/ramfs/bin/mines");
  1497. } else if (mx >= WM_BTN_WEB_X && my >= WM_BTN_WEB_Y &&
  1498. mx < WM_BTN_WEB_X + WM_BTN_WEB_W &&
  1499. my < WM_BTN_WEB_Y + WM_BTN_WEB_H) {
  1500. spawn_app_checked("/ramfs/bin/browser");
  1501. } else if (mx >= WM_BTN_CARDS_X && my >= WM_BTN_CARDS_Y &&
  1502. mx < WM_BTN_CARDS_X + WM_BTN_CARDS_W &&
  1503. my < WM_BTN_CARDS_Y + WM_BTN_CARDS_H) {
  1504. spawn_app_checked("/ramfs/bin/solitaire");
  1505. } else if (mx >= WM_BTN_GALLERY_X && my >= WM_BTN_GALLERY_Y &&
  1506. mx < WM_BTN_GALLERY_X + WM_BTN_GALLERY_W &&
  1507. my < WM_BTN_GALLERY_Y + WM_BTN_GALLERY_H) {
  1508. spawn_app_checked("/ramfs/bin/gallery");
  1509. } else if (mx >= WM_BTN_CHAT_X && my >= WM_BTN_CHAT_Y &&
  1510. mx < WM_BTN_CHAT_X + WM_BTN_CHAT_W &&
  1511. my < WM_BTN_CHAT_Y + WM_BTN_CHAT_H) {
  1512. spawn_app_checked("/ramfs/bin/chat");
  1513. } else {
  1514. int idx = window_at((int)mx, (int)my);
  1515. if (idx >= 0) {
  1516. struct uwin *u = &wins[idx];
  1517. int pmx = (int)mx, pmy = (int)my;
  1518. int rx = u->info.x + (int)u->info.w - 1 -
  1519. WM_CLOSE_W;
  1520. int ry = u->info.y + 1;
  1521. u_wm_focus((unsigned int)idx);
  1522. if (u->info.popover) {
  1523. /* A popover has no title bar, close
  1524. * button or resize grip; the app owns
  1525. * its content. */
  1526. } else if (pmx >= rx &&
  1527. pmx < u->info.x + (int)u->info.w &&
  1528. pmy >= ry && pmy < ry + WM_TITLE_H) {
  1529. u_wm_close((unsigned int)idx);
  1530. full_redraw = 1;
  1531. } else if (pmx >= u->info.x + (int)u->info.w - 16 &&
  1532. pmy >= u->info.y + (int)u->info.h - 16) {
  1533. resize_id = idx;
  1534. resize_mx = mx;
  1535. resize_my = my;
  1536. resize_ow = u->info.w;
  1537. resize_oh = u->info.h;
  1538. } else if (pmy < u->info.y + WM_TITLE_H) {
  1539. if (prev_title_id == idx &&
  1540. now - last_title_ms <= WM_DBLCLICK_MS &&
  1541. iabs(pmx - last_title_x) <= WM_DBLCLICK_SLOP &&
  1542. iabs(pmy - last_title_y) <= WM_DBLCLICK_SLOP) {
  1543. last_title_ms = 0;
  1544. if (toggle_maximize(u,
  1545. (unsigned int)idx) != 0)
  1546. bar_notice("resize failed");
  1547. } else {
  1548. last_title_id = idx;
  1549. last_title_ms = now;
  1550. last_title_x = pmx;
  1551. last_title_y = pmy;
  1552. drag_id = idx;
  1553. drag_mx = pmx;
  1554. drag_my = pmy;
  1555. drag_dx = pmx - u->info.x;
  1556. drag_dy = pmy - u->info.y;
  1557. }
  1558. }
  1559. }
  1560. }
  1561. } else if (down && was && resize_id >= 0) {
  1562. struct uwin *u = &wins[resize_id];
  1563. if (u->info.used) {
  1564. int nw = (int)resize_ow + (int)mx - (int)resize_mx;
  1565. int nh = (int)resize_oh + (int)my - (int)resize_my;
  1566. int maxw = (int)fbw;
  1567. int maxh = (int)fbh - WM_BAR_H;
  1568. if (nw < 200) nw = 200;
  1569. if (nh < 140) nh = 140;
  1570. if (nw > maxw) nw = maxw;
  1571. if (nh > maxh) nh = maxh;
  1572. /* Dragging the grip into the screen corner means "as
  1573. * large as the work area", even though the grip starts
  1574. * 16 px inside the window edge. */
  1575. if ((int)mx >= (int)fbw - 1)
  1576. nw = maxw;
  1577. if ((int)my >= (int)fbh - 1)
  1578. nh = maxh;
  1579. if (nw != (int)u->info.w || nh != (int)u->info.h) {
  1580. if (set_window_geometry(u,
  1581. (unsigned int)resize_id, u->info.x,
  1582. u->info.y, (unsigned int)nw,
  1583. (unsigned int)nh) == 0)
  1584. u->maximized = 0;
  1585. }
  1586. } else {
  1587. resize_id = -1;
  1588. }
  1589. } else if (down && was && drag_id >= 0) {
  1590. struct uwin *u = &wins[drag_id];
  1591. if (u->info.used) {
  1592. int ox, oy, nx, ny;
  1593. int started = !u->maximized ||
  1594. iabs((int)mx - drag_mx) >= WM_DRAG_START ||
  1595. iabs((int)my - drag_my) >= WM_DRAG_START;
  1596. /* A maximized window is restored as soon as the drag
  1597. * starts; keep the pointer's grab point relative to the
  1598. * restored geometry. A click that does not move must
  1599. * leave it maximized. */
  1600. if (u->maximized && started &&
  1601. restore_window(u, (unsigned int)drag_id) == 0) {
  1602. drag_dx = drag_mx - u->info.x;
  1603. drag_dy = drag_my - u->info.y;
  1604. }
  1605. if (!u->maximized) {
  1606. ox = u->info.x;
  1607. oy = u->info.y;
  1608. nx = (int)mx - drag_dx;
  1609. ny = (int)my - drag_dy;
  1610. clamp_geom(u, &nx, &ny);
  1611. if (nx != ox || ny != oy) {
  1612. struct wm_present_info present;
  1613. unsigned int i;
  1614. u_wm_set_geom((unsigned int)drag_id,
  1615. nx, ny);
  1616. u->info.x = nx;
  1617. u->info.y = ny;
  1618. if (u->info.focused) {
  1619. move_window_pixels(u, ox, oy,
  1620. nx, ny);
  1621. } else {
  1622. present.count = 0;
  1623. present.immediate = 1;
  1624. add_rect(&present, ox, oy,
  1625. u->info.w, u->info.h);
  1626. add_rect(&present, nx, ny,
  1627. u->info.w, u->info.h);
  1628. if (u_wm_begin() == 0) {
  1629. for (i = 0;
  1630. i < present.count; i++)
  1631. draw_rect(
  1632. present.rect[i].x,
  1633. present.rect[i].y,
  1634. present.rect[i].w,
  1635. present.rect[i].h);
  1636. wm_overlays(&present);
  1637. u_wm_present(&present);
  1638. }
  1639. }
  1640. }
  1641. }
  1642. } else {
  1643. drag_id = -1;
  1644. }
  1645. } else if (!down && was) {
  1646. drag_id = -1;
  1647. resize_id = -1;
  1648. }
  1649. prev_buttons = buttons;
  1650. }
  1651. static char *put_str(char *p, const char *s)
  1652. {
  1653. while (*s)
  1654. *p++ = *s++;
  1655. return p;
  1656. }
  1657. static char *put_dec(char *p, unsigned int v)
  1658. {
  1659. char b[10];
  1660. unsigned int n = 0;
  1661. do {
  1662. b[n++] = (char)('0' + v % 10);
  1663. v /= 10;
  1664. } while (v);
  1665. while (n)
  1666. *p++ = b[--n];
  1667. return p;
  1668. }
  1669. /* Expose the last interval in /share so the target can be measured without
  1670. * a serial console. */
  1671. static void write_stats(void)
  1672. {
  1673. char b[80], *p = b;
  1674. unsigned int now = u_ticks_ms();
  1675. p = put_str(p, "t=");
  1676. p = put_dec(p, now / 1000);
  1677. p = put_str(p, "s frames=");
  1678. p = put_dec(p, stat_frames);
  1679. p = put_str(p, " ms=");
  1680. p = put_dec(p, stat_ms);
  1681. p = put_str(p, " max=");
  1682. p = put_dec(p, stat_max);
  1683. p = put_str(p, " px=");
  1684. p = put_dec(p, stat_px);
  1685. p = put_str(p, "\n");
  1686. u_syscall3(SYS_WRITE_FILE, (int)"/share/wmstats", (int)b,
  1687. (int)(p - b));
  1688. }
  1689. void _start(const char *arg)
  1690. {
  1691. struct wm_screen_info si;
  1692. struct wm_status st;
  1693. unsigned int i;
  1694. (void)arg;
  1695. if (u_wm_attach(&si) != 0)
  1696. u_exit(1);
  1697. usurf_init(&screen, si.fb, si.width, si.height, si.pitch, si.bpp);
  1698. usurf_font_attach(&screen, "/ramfs/fonts/sans.ttf", 14);
  1699. fbw = si.width;
  1700. fbh = si.height;
  1701. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1702. wins[i].info.used = 0;
  1703. wins[i].mapped = 0;
  1704. }
  1705. full_redraw = 1;
  1706. for (;;) {
  1707. unsigned int t0, t1;
  1708. if (u_wm_poll(&st) != 0)
  1709. u_exit(1);
  1710. if (st.full_redraw)
  1711. full_redraw = 1;
  1712. usurf_pixels = 0;
  1713. t0 = u_ticks_ms();
  1714. handle_mouse(&st);
  1715. do_update(&st);
  1716. t1 = u_ticks_ms();
  1717. if (usurf_pixels) {
  1718. unsigned int dt = t1 - t0;
  1719. stat_frames++;
  1720. stat_ms += dt;
  1721. stat_px += usurf_pixels;
  1722. if (dt > stat_max)
  1723. stat_max = dt;
  1724. }
  1725. if (t1 - stat_last >= 2000) {
  1726. stat_last = t1;
  1727. if (stat_frames) {
  1728. write_stats();
  1729. stat_frames = stat_ms = stat_px = stat_max = 0;
  1730. }
  1731. }
  1732. u_sleep_ms(10);
  1733. }
  1734. }