ucmd_wm.c 43 KB

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