ucmd_wm.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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. };
  21. static struct usurf screen;
  22. static struct uwin wins[WM_MAX_WINDOWS];
  23. static unsigned int fbw, fbh;
  24. static int drag_id = -1;
  25. static int drag_dx, drag_dy;
  26. static int resize_id = -1;
  27. static unsigned int resize_mx, resize_my, resize_ow, resize_oh;
  28. static unsigned int prev_buttons;
  29. static int full_redraw = 1;
  30. static int boot_term_spawned;
  31. static unsigned int bar_kexec_busy, bar_kexec_progress, bar_kexec_total;
  32. static int bar_temp_c = -1;
  33. static int bar_batt_pct = -1;
  34. static int bar_batt_charging;
  35. static int bar_batt_ac;
  36. static char bar_msg[24];
  37. static unsigned int bar_msg_until;
  38. static int bar_msg_dirty;
  39. static unsigned int stat_frames, stat_ms, stat_px, stat_max, stat_last;
  40. static void bar_notice(const char *msg)
  41. {
  42. unsigned int i;
  43. for (i = 0; msg[i] && i + 1 < sizeof(bar_msg); i++)
  44. bar_msg[i] = msg[i];
  45. bar_msg[i] = '\0';
  46. bar_msg_until = u_ticks_ms() + 2000;
  47. bar_msg_dirty = 1;
  48. }
  49. static int free_window_slot(void)
  50. {
  51. unsigned int i;
  52. for (i = 0; i < WM_MAX_WINDOWS; i++)
  53. if (!wins[i].info.used)
  54. return 1;
  55. return 0;
  56. }
  57. static unsigned int rgb16(unsigned int r, unsigned int g, unsigned int b)
  58. {
  59. return (r << 16) | (g << 8) | b;
  60. }
  61. /* Rainbow gradient: hue 0..1535 (6*256) maps to R→Y→G→C→B→M→R, the same
  62. * integer interpolation the kernel bar uses. */
  63. static unsigned int rainbow_rgb(unsigned int hue)
  64. {
  65. unsigned int seg = hue / 256, f = hue % 256;
  66. unsigned int r1, g1, b1, r2, g2, b2;
  67. switch (seg) {
  68. case 0: r1=255; g1=0; b1=0; r2=255; g2=255; b2=0; break;
  69. case 1: r1=255; g1=255; b1=0; r2=0; g2=255; b2=0; break;
  70. case 2: r1=0; g1=255; b1=0; r2=0; g2=255; b2=255; break;
  71. case 3: r1=0; g1=255; b1=255; r2=0; g2=0; b2=255; break;
  72. case 4: r1=0; g1=0; b1=255; r2=255; g2=0; b2=255; break;
  73. default:r1=255; g1=0; b1=255; r2=255; g2=0; b2=0; break;
  74. }
  75. return rgb16((r1 * (256 - f) + r2 * f) / 256,
  76. (g1 * (256 - f) + g2 * f) / 256,
  77. (b1 * (256 - f) + b2 * f) / 256);
  78. }
  79. static void fill_clip(int x, int y, unsigned int w, unsigned int h,
  80. unsigned int rgb, int cx, int cy,
  81. unsigned int cw, unsigned int ch)
  82. {
  83. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  84. if (x0 < cx) x0 = cx;
  85. if (y0 < cy) y0 = cy;
  86. if (x1 > cx + (int)cw) x1 = cx + (int)cw;
  87. if (y1 > cy + (int)ch) y1 = cy + (int)ch;
  88. if (x1 > x0 && y1 > y0)
  89. usurf_fill(&screen, x0, y0, (unsigned int)(x1 - x0),
  90. (unsigned int)(y1 - y0), rgb);
  91. }
  92. static void bar_button(unsigned int x, unsigned int y, unsigned int w,
  93. unsigned int h, const char *label)
  94. {
  95. int tw = (int)usurf_text_width(&screen, label);
  96. usurf_fill(&screen, x, y, w, h, 0x963CD2u);
  97. usurf_text(&screen, (int)x + ((int)w - tw) / 2, (int)(y + 3),
  98. label, COL_WHITE, 0, 0, fbw, WM_BAR_H);
  99. }
  100. static unsigned int temp_color(int c)
  101. {
  102. if (c >= 80)
  103. return 0xFF5050u;
  104. if (c >= 60)
  105. return 0xFFD250u;
  106. return 0x55FF55u;
  107. }
  108. /* "CPU 43C" (no libc printf here). */
  109. static unsigned int temp_text(char *b, int c)
  110. {
  111. unsigned int n = 0, v = (unsigned int)c;
  112. b[n++] = 'C';
  113. b[n++] = 'P';
  114. b[n++] = 'U';
  115. b[n++] = ' ';
  116. if (v >= 100)
  117. b[n++] = (char)('0' + (v / 100) % 10);
  118. if (v >= 10)
  119. b[n++] = (char)('0' + (v / 10) % 10);
  120. b[n++] = (char)('0' + v % 10);
  121. b[n++] = 'C';
  122. b[n] = '\0';
  123. return n;
  124. }
  125. /* "BAT 87%" (no libc printf here). */
  126. static unsigned int batt_text(char *b, int pct)
  127. {
  128. unsigned int n = 0, v = (unsigned int)pct;
  129. b[n++] = 'B';
  130. b[n++] = 'A';
  131. b[n++] = 'T';
  132. b[n++] = ' ';
  133. if (v >= 100)
  134. b[n++] = (char)('0' + (v / 100) % 10);
  135. if (v >= 10)
  136. b[n++] = (char)('0' + (v / 10) % 10);
  137. b[n++] = (char)('0' + v % 10);
  138. b[n++] = '%';
  139. b[n] = '\0';
  140. return n;
  141. }
  142. static unsigned int batt_color(int pct, int charging, int ac)
  143. {
  144. if (pct <= 10)
  145. return 0xFF5050u;
  146. if (pct <= 20)
  147. return 0xFFD250u;
  148. if (charging || ac)
  149. return 0x55FF55u;
  150. return 0x55D2FFu;
  151. }
  152. static void draw_bar(void)
  153. {
  154. char dt[48];
  155. unsigned int n, i, x, y;
  156. usurf_fill(&screen, 0, 0, fbw, WM_BAR_H, COL_BG);
  157. usurf_text(&screen, 4, 5, OS_NAME " v" OS_VERSION, COL_BANNER,
  158. 0, 0, fbw, WM_BAR_H);
  159. bar_button(WM_BTN_X, WM_BTN_Y, WM_BTN_W, WM_BTN_H, "New");
  160. bar_button(WM_BTN_CLOCK_X, WM_BTN_CLOCK_Y, WM_BTN_CLOCK_W,
  161. WM_BTN_CLOCK_H, "Clock");
  162. bar_button(WM_BTN_COLORS_X, WM_BTN_COLORS_Y, WM_BTN_COLORS_W,
  163. WM_BTN_COLORS_H, "Colors");
  164. bar_button(WM_BTN_FILES_X, WM_BTN_FILES_Y, WM_BTN_FILES_W,
  165. WM_BTN_FILES_H, "Files");
  166. bar_button(WM_BTN_MINES_X, WM_BTN_MINES_Y, WM_BTN_MINES_W,
  167. WM_BTN_MINES_H, "Mines");
  168. bar_button(WM_BTN_WEB_X, WM_BTN_WEB_Y, WM_BTN_WEB_W,
  169. WM_BTN_WEB_H, "Web");
  170. bar_button(WM_BTN_CARDS_X, WM_BTN_CARDS_Y, WM_BTN_CARDS_W,
  171. WM_BTN_CARDS_H, "Cards");
  172. bar_button(WM_BTN_GALLERY_X, WM_BTN_GALLERY_Y, WM_BTN_GALLERY_W,
  173. WM_BTN_GALLERY_H, "Gallery");
  174. if (bar_msg_until &&
  175. (int)(u_ticks_ms() - bar_msg_until) < 0)
  176. usurf_text(&screen, (int)(WM_BTN_GALLERY_X + WM_BTN_GALLERY_W + 8),
  177. 5, bar_msg, 0xFFD250u, 0, 0, fbw, WM_BAR_H);
  178. /* Rainbow separator (same y as the kernel's old bar). */
  179. for (x = 0; x < fbw; x++) {
  180. unsigned int c = rainbow_rgb((x * (6 * 256)) / (fbw ? fbw : 1));
  181. for (y = WM_BAR_H - 5; y < WM_BAR_H - 3; y++)
  182. usurf_fill(&screen, (int)x, (int)y, 1, 1, c);
  183. }
  184. if (bar_kexec_busy) {
  185. static const char msg[] = "loading new kernel over serial";
  186. unsigned int dots = bar_kexec_total ?
  187. (bar_kexec_progress * 12 / bar_kexec_total) : 0;
  188. n = 0;
  189. while (msg[n])
  190. dt[n] = msg[n], n++;
  191. for (i = 0; i < 12; i++)
  192. dt[n + i] = (i < dots) ? '.' : ' ';
  193. n += 12;
  194. dt[n] = '\0';
  195. usurf_text(&screen,
  196. (int)fbw - (int)usurf_text_width(&screen, dt) - 4,
  197. 5, dt, 0xFFC864u, 0, 0, fbw, WM_BAR_H);
  198. } else {
  199. unsigned int clock_chars = 0;
  200. int right = (int)fbw - 4;
  201. int min_x = WM_BTN_GALLERY_X + (int)WM_BTN_GALLERY_W + 4;
  202. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  203. n = 0;
  204. while (dt[n] && n < sizeof(dt) - 1)
  205. n++;
  206. usurf_text(&screen,
  207. right - (int)usurf_text_width(&screen, dt),
  208. 5, dt, COL_BANNER, 0, 0, fbw, WM_BAR_H);
  209. clock_chars = (unsigned int)usurf_text_width(&screen, dt);
  210. }
  211. if (clock_chars)
  212. right -= (int)clock_chars + 8;
  213. if (bar_temp_c >= 0) {
  214. char tb[12];
  215. int tx;
  216. (void)temp_text(tb, bar_temp_c);
  217. tx = right - (int)usurf_text_width(&screen, tb);
  218. if (tx >= min_x) {
  219. usurf_text(&screen, tx, 5, tb,
  220. temp_color(bar_temp_c), 0, 0,
  221. fbw, WM_BAR_H);
  222. right = tx - 8;
  223. }
  224. }
  225. if (bar_batt_pct >= 0) {
  226. char bb[10];
  227. int bx;
  228. (void)batt_text(bb, bar_batt_pct);
  229. bx = right - (int)usurf_text_width(&screen, bb);
  230. if (bx >= min_x)
  231. usurf_text(&screen, bx, 5, bb,
  232. batt_color(bar_batt_pct,
  233. bar_batt_charging,
  234. bar_batt_ac),
  235. 0, 0, fbw, WM_BAR_H);
  236. }
  237. }
  238. }
  239. static void draw_window_clip(unsigned int idx, int cx, int cy,
  240. unsigned int cw, unsigned int ch)
  241. {
  242. struct uwin *u = &wins[idx];
  243. struct wm_window_info *w = &u->info;
  244. int x = w->x, y = w->y;
  245. unsigned int pw = w->w, ph = w->h;
  246. unsigned int bx = (unsigned int)(x + (int)pw - 1 - WM_CLOSE_W);
  247. unsigned int cony = (unsigned int)(y + 1 + WM_TITLE_H);
  248. unsigned int content_x = (unsigned int)(x + 1);
  249. unsigned int content_w = w->content_w;
  250. unsigned int content_h = w->content_h;
  251. /* Border, title bar, close button. */
  252. fill_clip(x, y, pw, ph, COL_BORDER, cx, cy, cw, ch);
  253. fill_clip(x + 1, y + 1, pw - 2, WM_TITLE_H,
  254. w->focused ? COL_TITLE_ON : COL_TITLE_OFF, cx, cy, cw, ch);
  255. usurf_text(&screen, x + 4, y + 3, w->title, COL_WHITE,
  256. cx, cy, cw, ch);
  257. fill_clip((int)bx, y + 1, WM_CLOSE_W, WM_TITLE_H, COL_CLOSE,
  258. cx, cy, cw, ch);
  259. usurf_text(&screen, (int)bx + 4, y + 3, "x", COL_WHITE,
  260. cx, cy, cw, ch);
  261. /* Content: every window presents an RGB565 canvas (the terminal
  262. * renders its cells into one; gfx commands own theirs). A mapped
  263. * canvas covers the whole content area, so it does not need the black
  264. * underlay. */
  265. if (u->mapped && u->map.kind == WM_KIND_GFX) {
  266. struct uwm_canvas c;
  267. c.px = (const unsigned short *)u->map.va;
  268. c.w = u->map.w;
  269. c.h = u->map.h;
  270. if (c.w != content_w || c.h != content_h)
  271. fill_clip((int)content_x, (int)cony, content_w, content_h,
  272. 0x000000u, cx, cy, cw, ch);
  273. usurf_canvas(&screen, &c, (int)content_x, (int)cony,
  274. cx, cy, cw, ch);
  275. } else {
  276. fill_clip((int)content_x, (int)cony, content_w, content_h,
  277. 0x000000u, cx, cy, cw, ch);
  278. }
  279. /* Resize grip (bottom-right corner). */
  280. if (w->focused) {
  281. unsigned int i;
  282. for (i = 0; i < 10; i++) {
  283. fill_clip(x + (int)pw - 3 - (int)i,
  284. y + (int)ph - 4, 1, 1, 0xD2D2D2u,
  285. cx, cy, cw, ch);
  286. fill_clip(x + (int)pw - 4,
  287. y + (int)ph - 3 - (int)i, 1, 1, 0xD2D2D2u,
  288. cx, cy, cw, ch);
  289. }
  290. }
  291. }
  292. /* Front-to-back window order: the focused window is on top, the others
  293. * follow in descending slot order. Painting and hit-testing share it. */
  294. static unsigned int build_order(int *order)
  295. {
  296. unsigned int i, n = 0;
  297. for (i = 0; i < WM_MAX_WINDOWS; i++)
  298. if (wins[i].info.used && wins[i].info.focused)
  299. order[n++] = (int)i;
  300. for (i = WM_MAX_WINDOWS; i-- > 0;)
  301. if (wins[i].info.used && !wins[i].info.focused)
  302. order[n++] = (int)i;
  303. return n;
  304. }
  305. struct orect {
  306. int x, y;
  307. unsigned int w, h;
  308. };
  309. #define OCC_MAX 64
  310. /* r minus cut, split into up to four disjoint rectangles. */
  311. static unsigned int rect_sub(struct orect *out, const struct orect *r,
  312. const struct orect *cut)
  313. {
  314. int rx0 = r->x, ry0 = r->y;
  315. int rx1 = r->x + (int)r->w, ry1 = r->y + (int)r->h;
  316. int cx0 = cut->x, cy0 = cut->y;
  317. int cx1 = cut->x + (int)cut->w, cy1 = cut->y + (int)cut->h;
  318. unsigned int n = 0;
  319. if (cx0 < rx0) cx0 = rx0;
  320. if (cy0 < ry0) cy0 = ry0;
  321. if (cx1 > rx1) cx1 = rx1;
  322. if (cy1 > ry1) cy1 = ry1;
  323. if (cx1 <= cx0 || cy1 <= cy0) {
  324. out[0] = *r;
  325. return 1;
  326. }
  327. if (cy0 > ry0) {
  328. out[n].x = rx0;
  329. out[n].y = ry0;
  330. out[n].w = (unsigned int)(rx1 - rx0);
  331. out[n].h = (unsigned int)(cy0 - ry0);
  332. n++;
  333. }
  334. if (cy1 < ry1) {
  335. out[n].x = rx0;
  336. out[n].y = cy1;
  337. out[n].w = (unsigned int)(rx1 - rx0);
  338. out[n].h = (unsigned int)(ry1 - cy1);
  339. n++;
  340. }
  341. if (cx0 > rx0) {
  342. out[n].x = rx0;
  343. out[n].y = cy0;
  344. out[n].w = (unsigned int)(cx0 - rx0);
  345. out[n].h = (unsigned int)(cy1 - cy0);
  346. n++;
  347. }
  348. if (cx1 < rx1) {
  349. out[n].x = cx1;
  350. out[n].y = cy0;
  351. out[n].w = (unsigned int)(rx1 - cx1);
  352. out[n].h = (unsigned int)(cy1 - cy0);
  353. n++;
  354. }
  355. return n;
  356. }
  357. /* Back-to-front repaint, used only if the visible-region list overflows. */
  358. static void draw_rect_naive(int x, int y, unsigned int w, unsigned int h)
  359. {
  360. unsigned int i;
  361. usurf_fill(&screen, x, y, w, h, COL_BG);
  362. for (i = 0; i < WM_MAX_WINDOWS; i++)
  363. if (wins[i].info.used && !wins[i].info.focused &&
  364. x < wins[i].info.x + (int)wins[i].info.w &&
  365. x + (int)w > wins[i].info.x &&
  366. y < wins[i].info.y + (int)wins[i].info.h &&
  367. y + (int)h > wins[i].info.y)
  368. draw_window_clip(i, x, y, w, h);
  369. for (i = 0; i < WM_MAX_WINDOWS; i++)
  370. if (wins[i].info.used && wins[i].info.focused &&
  371. x < wins[i].info.x + (int)wins[i].info.w &&
  372. x + (int)w > wins[i].info.x &&
  373. y < wins[i].info.y + (int)wins[i].info.h &&
  374. y + (int)h > wins[i].info.y)
  375. draw_window_clip(i, x, y, w, h);
  376. }
  377. static void draw_rect(int x, int y, unsigned int w, unsigned int h)
  378. {
  379. static struct orect cur[OCC_MAX], nxt[OCC_MAX];
  380. struct orect dmg;
  381. int order[WM_MAX_WINDOWS];
  382. unsigned int n, i, j, ncur, nnext;
  383. int overflow = 0;
  384. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  385. if (w == 0 || h == 0)
  386. return;
  387. if (x0 < 0) x0 = 0;
  388. if (y0 < 0) y0 = 0;
  389. if (x1 > (int)fbw) x1 = (int)fbw;
  390. if (y1 > (int)fbh) y1 = (int)fbh;
  391. if (x1 <= x0 || y1 <= y0)
  392. return;
  393. dmg.x = x0;
  394. dmg.y = y0;
  395. dmg.w = (unsigned int)(x1 - x0);
  396. dmg.h = (unsigned int)(y1 - y0);
  397. usurf_fill(&screen, dmg.x, dmg.y, dmg.w, dmg.h, COL_BG);
  398. n = build_order(order);
  399. for (i = 0; i < n; i++) {
  400. struct uwin *u = &wins[order[i]];
  401. int wx0 = u->info.x, wy0 = u->info.y;
  402. int wx1 = wx0 + (int)u->info.w, wy1 = wy0 + (int)u->info.h;
  403. if (wx0 < dmg.x) wx0 = dmg.x;
  404. if (wy0 < dmg.y) wy0 = dmg.y;
  405. if (wx1 > x1) wx1 = x1;
  406. if (wy1 > y1) wy1 = y1;
  407. if (wx1 <= wx0 || wy1 <= wy0)
  408. continue;
  409. cur[0].x = wx0;
  410. cur[0].y = wy0;
  411. cur[0].w = (unsigned int)(wx1 - wx0);
  412. cur[0].h = (unsigned int)(wy1 - wy0);
  413. ncur = 1;
  414. for (j = 0; j < i && ncur; j++) {
  415. struct uwin *f = &wins[order[j]];
  416. struct orect fr;
  417. unsigned int k;
  418. fr.x = f->info.x;
  419. fr.y = f->info.y;
  420. fr.w = f->info.w;
  421. fr.h = f->info.h;
  422. nnext = 0;
  423. for (k = 0; k < ncur; k++) {
  424. if (nnext + 4 > OCC_MAX) {
  425. overflow = 1;
  426. break;
  427. }
  428. nnext += rect_sub(&nxt[nnext], &cur[k], &fr);
  429. }
  430. if (overflow)
  431. break;
  432. for (k = 0; k < nnext; k++)
  433. cur[k] = nxt[k];
  434. ncur = nnext;
  435. }
  436. if (overflow)
  437. break;
  438. for (j = 0; j < ncur; j++)
  439. draw_window_clip((unsigned int)order[i], cur[j].x,
  440. cur[j].y, cur[j].w, cur[j].h);
  441. }
  442. if (overflow)
  443. draw_rect_naive(dmg.x, dmg.y, dmg.w, dmg.h);
  444. }
  445. static int window_at(unsigned int mx, unsigned int my)
  446. {
  447. int order[WM_MAX_WINDOWS];
  448. unsigned int n = build_order(order), i;
  449. for (i = 0; i < n; i++) {
  450. struct wm_window_info *w = &wins[order[i]].info;
  451. if (mx >= (unsigned int)w->x && my >= (unsigned int)w->y &&
  452. mx < (unsigned int)(w->x + (int)w->w) &&
  453. my < (unsigned int)(w->y + (int)w->h))
  454. return order[i];
  455. }
  456. return -1;
  457. }
  458. static void clamp_geom(struct uwin *u, int *x, int *y)
  459. {
  460. int maxx = (int)fbw - (int)u->info.w;
  461. int maxy = (int)fbh - (int)u->info.h;
  462. if (*x < 0) *x = 0;
  463. if (*y < WM_BAR_H) *y = WM_BAR_H;
  464. if (maxx < 0) maxx = 0;
  465. if (maxy < WM_BAR_H) maxy = WM_BAR_H;
  466. if (*x > maxx) *x = maxx;
  467. if (*y > maxy) *y = maxy;
  468. }
  469. static void add_rect(struct wm_present_info *p, int x, int y,
  470. unsigned int w, unsigned int h)
  471. {
  472. if (w == 0 || h == 0)
  473. return;
  474. if (p->count < 4) {
  475. p->rect[p->count].x = x;
  476. p->rect[p->count].y = y;
  477. p->rect[p->count].w = w;
  478. p->rect[p->count].h = h;
  479. p->count++;
  480. } else {
  481. /* Four is enough for normal updates; fold the rest into the
  482. * last rect rather than dropping pixels. */
  483. int x0 = p->rect[3].x, y0 = p->rect[3].y;
  484. int x1 = x0 + (int)p->rect[3].w, y1 = y0 + (int)p->rect[3].h;
  485. int nx1 = x + (int)w, ny1 = y + (int)h;
  486. if (x < x0) x0 = x;
  487. if (y < y0) y0 = y;
  488. if (nx1 > x1) x1 = nx1;
  489. if (ny1 > y1) y1 = ny1;
  490. p->rect[3].x = x0;
  491. p->rect[3].y = y0;
  492. p->rect[3].w = (unsigned int)(x1 - x0);
  493. p->rect[3].h = (unsigned int)(y1 - y0);
  494. }
  495. }
  496. /* A focused window is topmost, so the shadow contains exactly its pixels in
  497. * the old rectangle. Move that block and repaint only the old part the new
  498. * rectangle does not cover. */
  499. static void move_window_pixels(struct uwin *u, int ox, int oy, int nx, int ny)
  500. {
  501. struct wm_present_info present;
  502. struct orect oldr, newr, pieces[4];
  503. unsigned int np, k;
  504. if (u_wm_begin() != 0)
  505. return;
  506. usurf_copy(&screen, nx, ny, ox, oy, u->info.w, u->info.h);
  507. oldr.x = ox;
  508. oldr.y = oy;
  509. oldr.w = u->info.w;
  510. oldr.h = u->info.h;
  511. newr.x = nx;
  512. newr.y = ny;
  513. newr.w = u->info.w;
  514. newr.h = u->info.h;
  515. np = rect_sub(pieces, &oldr, &newr);
  516. for (k = 0; k < np; k++)
  517. draw_rect(pieces[k].x, pieces[k].y, pieces[k].w, pieces[k].h);
  518. present.count = 0;
  519. present.immediate = 1;
  520. add_rect(&present, nx, ny, u->info.w, u->info.h);
  521. for (k = 0; k < np; k++)
  522. add_rect(&present, pieces[k].x, pieces[k].y,
  523. pieces[k].w, pieces[k].h);
  524. u_wm_present(&present);
  525. }
  526. static void do_update(struct wm_status *st)
  527. {
  528. struct wm_present_info present;
  529. int need = 0;
  530. int bar_dirty = 0;
  531. unsigned int i;
  532. char dt[20];
  533. static char last_dt[20];
  534. int kexec_changed = (st->kexec_busy != bar_kexec_busy) ||
  535. (st->kexec_busy && st->kexec_progress != bar_kexec_progress);
  536. unsigned int msg_now = u_ticks_ms();
  537. /* Spawn the first terminal once the kernel says the storage scan is
  538. * done, so a shell command can never race the mount table. */
  539. if (st->boot_ready && !boot_term_spawned) {
  540. boot_term_spawned = 1;
  541. if (!free_window_slot()) {
  542. bar_notice("window limit");
  543. full_redraw = 1;
  544. } else if (u_wm_create_term() < 0) {
  545. bar_notice("cannot start term");
  546. full_redraw = 1;
  547. } else {
  548. full_redraw = 1;
  549. }
  550. }
  551. if (bar_msg_dirty ||
  552. (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)) {
  553. bar_dirty = 1;
  554. bar_msg_dirty = 0;
  555. if (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)
  556. bar_msg_until = 0;
  557. }
  558. bar_kexec_busy = st->kexec_busy;
  559. bar_kexec_progress = st->kexec_progress;
  560. bar_kexec_total = st->kexec_total;
  561. if (kexec_changed)
  562. bar_dirty = 1;
  563. /* Refresh cached window state and mappings before opening a frame:
  564. * MAP may unmap/remap content. */
  565. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  566. struct wm_window_info info;
  567. int r = u_wm_info(i, &info);
  568. if (r < 0) {
  569. u_exit(1);
  570. }
  571. if (r == 0 || !info.used) {
  572. if (wins[i].info.used)
  573. full_redraw = 1;
  574. wins[i].info.used = 0;
  575. wins[i].mapped = 0;
  576. continue;
  577. }
  578. if (!wins[i].info.used ||
  579. info.content_gen != wins[i].info.content_gen)
  580. full_redraw = 1;
  581. wins[i].info = info;
  582. if (!wins[i].mapped ||
  583. wins[i].map.generation != info.content_gen) {
  584. struct wm_map_info map;
  585. if (u_wm_map(i, &map) == 0) {
  586. wins[i].map = map;
  587. wins[i].mapped = 1;
  588. } else {
  589. wins[i].mapped = 0;
  590. }
  591. }
  592. }
  593. /* The RTC is slow (CMOS polling) and the temperature/battery state only
  594. * changes once a second; query them at 1 Hz instead of every 10 ms. */
  595. {
  596. static unsigned int last_rtc_ms;
  597. unsigned int now_ms = u_ticks_ms();
  598. if (full_redraw || now_ms - last_rtc_ms >= 1000) {
  599. last_rtc_ms = now_ms;
  600. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  601. for (i = 0; i < sizeof(dt); i++)
  602. if (dt[i] != last_dt[i]) {
  603. for (i = 0; i < sizeof(dt); i++)
  604. last_dt[i] = dt[i];
  605. bar_dirty = 1;
  606. break;
  607. }
  608. }
  609. {
  610. struct sys_cputemp ct;
  611. if (u_cpu_temp(&ct) == 0 &&
  612. ct.temp_c != bar_temp_c) {
  613. bar_temp_c = ct.temp_c;
  614. bar_dirty = 1;
  615. }
  616. }
  617. {
  618. struct sys_powerinfo pi;
  619. int pct = -1, charging = 0, ac = 0;
  620. unsigned int bi;
  621. if (u_power_info(&pi, 0) == 0) {
  622. ac = pi.ac_online > 0;
  623. if (pi.present) {
  624. for (bi = 0; bi < SYS_POWER_BATT_MAX;
  625. bi++) {
  626. struct sys_battery *b =
  627. &pi.battery[bi];
  628. if (!b->present ||
  629. !b->percent_valid)
  630. continue;
  631. pct = (int)b->percent;
  632. charging = (int)b->charging;
  633. break;
  634. }
  635. }
  636. if (pct != bar_batt_pct ||
  637. charging != bar_batt_charging ||
  638. ac != bar_batt_ac) {
  639. bar_batt_pct = pct;
  640. bar_batt_charging = charging;
  641. bar_batt_ac = ac;
  642. bar_dirty = 1;
  643. }
  644. }
  645. }
  646. }
  647. }
  648. if (full_redraw) {
  649. if (u_wm_begin() != 0) {
  650. full_redraw = 1;
  651. return;
  652. }
  653. full_redraw = 0;
  654. need = 1;
  655. {
  656. draw_rect(0, 0, fbw, fbh);
  657. draw_bar();
  658. present.count = 0;
  659. add_rect(&present, 0, 0, fbw, fbh);
  660. present.immediate = 0;
  661. u_wm_present(&present);
  662. /* Ack every window that was clean at the time of the
  663. * redraw. If new damage arrived during it, its seq
  664. * differs and the kernel leaves it dirty. */
  665. for (i = 0; i < WM_MAX_WINDOWS; i++)
  666. if (wins[i].info.used && wins[i].mapped)
  667. u_wm_ack(i, wins[i].info.dirty_seq);
  668. }
  669. return;
  670. }
  671. /* Partial cell/canvas updates and the bar clock. */
  672. present.count = 0;
  673. present.immediate = 0;
  674. if (bar_dirty) {
  675. need = 1;
  676. add_rect(&present, 0, 0, fbw, WM_BAR_H);
  677. }
  678. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  679. struct uwin *u = &wins[i];
  680. struct wm_window_info *w = &u->info;
  681. int rx, ry;
  682. unsigned int rw, rh;
  683. if (!w->used || !w->dirty)
  684. continue;
  685. if (!w->dmg_w || !w->dmg_h)
  686. continue;
  687. rx = w->x + 1 + (int)w->dmg_x;
  688. ry = w->y + 1 + WM_TITLE_H + (int)w->dmg_y;
  689. rw = w->dmg_w;
  690. rh = w->dmg_h;
  691. add_rect(&present, rx, ry, rw, rh);
  692. u->ack_seq = u->mapped ? w->dirty_seq : 0;
  693. need = 1;
  694. }
  695. if (!need) {
  696. /* Drag movement is handled below; a stationary mouse can be
  697. * left to the kernel's cursor overlay. */
  698. return;
  699. }
  700. if (u_wm_begin() != 0)
  701. return;
  702. for (i = 0; i < present.count; i++)
  703. draw_rect(present.rect[i].x, present.rect[i].y,
  704. present.rect[i].w, present.rect[i].h);
  705. if (bar_dirty)
  706. draw_bar();
  707. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  708. struct uwin *u = &wins[i];
  709. if (!u->info.used || !u->info.dirty || !u->mapped ||
  710. !u->ack_seq)
  711. continue;
  712. if (u->ack_seq == u->info.dirty_seq)
  713. u_wm_ack(i, u->info.dirty_seq);
  714. }
  715. u_wm_present(&present);
  716. }
  717. static int spawn_app(const char *path)
  718. {
  719. int pid = u_spawn(path, "", 0, "/");
  720. if (pid > 0)
  721. full_redraw = 1;
  722. return pid;
  723. }
  724. static void spawn_app_checked(const char *path)
  725. {
  726. if (!free_window_slot()) {
  727. bar_notice("window limit");
  728. full_redraw = 1;
  729. return;
  730. }
  731. if (spawn_app(path) < 0) {
  732. bar_notice("spawn failed");
  733. full_redraw = 1;
  734. }
  735. }
  736. static void handle_mouse(struct wm_status *st)
  737. {
  738. unsigned int buttons = st->mouse.buttons;
  739. unsigned int mx = st->mouse.x, my = st->mouse.y;
  740. int down = (buttons & 1) != 0;
  741. int was = (prev_buttons & 1) != 0;
  742. if (down && !was) {
  743. if (mx >= WM_BTN_X && my >= WM_BTN_Y &&
  744. mx < WM_BTN_X + WM_BTN_W && my < WM_BTN_Y + WM_BTN_H) {
  745. if (!free_window_slot()) {
  746. bar_notice("window limit");
  747. full_redraw = 1;
  748. } else if (u_wm_create_term() < 0) {
  749. bar_notice("cannot start term");
  750. full_redraw = 1;
  751. } else {
  752. full_redraw = 1;
  753. }
  754. } else if (mx >= WM_BTN_CLOCK_X && my >= WM_BTN_CLOCK_Y &&
  755. mx < WM_BTN_CLOCK_X + WM_BTN_CLOCK_W &&
  756. my < WM_BTN_CLOCK_Y + WM_BTN_CLOCK_H) {
  757. spawn_app_checked("/ramfs/bin/clock");
  758. } else if (mx >= WM_BTN_COLORS_X && my >= WM_BTN_COLORS_Y &&
  759. mx < WM_BTN_COLORS_X + WM_BTN_COLORS_W &&
  760. my < WM_BTN_COLORS_Y + WM_BTN_COLORS_H) {
  761. spawn_app_checked("/ramfs/bin/colors");
  762. } else if (mx >= WM_BTN_FILES_X && my >= WM_BTN_FILES_Y &&
  763. mx < WM_BTN_FILES_X + WM_BTN_FILES_W &&
  764. my < WM_BTN_FILES_Y + WM_BTN_FILES_H) {
  765. spawn_app_checked("/ramfs/bin/files");
  766. } else if (mx >= WM_BTN_MINES_X && my >= WM_BTN_MINES_Y &&
  767. mx < WM_BTN_MINES_X + WM_BTN_MINES_W &&
  768. my < WM_BTN_MINES_Y + WM_BTN_MINES_H) {
  769. spawn_app_checked("/ramfs/bin/mines");
  770. } else if (mx >= WM_BTN_WEB_X && my >= WM_BTN_WEB_Y &&
  771. mx < WM_BTN_WEB_X + WM_BTN_WEB_W &&
  772. my < WM_BTN_WEB_Y + WM_BTN_WEB_H) {
  773. spawn_app_checked("/ramfs/bin/browser");
  774. } else if (mx >= WM_BTN_CARDS_X && my >= WM_BTN_CARDS_Y &&
  775. mx < WM_BTN_CARDS_X + WM_BTN_CARDS_W &&
  776. my < WM_BTN_CARDS_Y + WM_BTN_CARDS_H) {
  777. spawn_app_checked("/ramfs/bin/solitaire");
  778. } else if (mx >= WM_BTN_GALLERY_X && my >= WM_BTN_GALLERY_Y &&
  779. mx < WM_BTN_GALLERY_X + WM_BTN_GALLERY_W &&
  780. my < WM_BTN_GALLERY_Y + WM_BTN_GALLERY_H) {
  781. spawn_app_checked("/ramfs/bin/gallery");
  782. } else {
  783. int idx = window_at(mx, my);
  784. if (idx >= 0) {
  785. struct uwin *u = &wins[idx];
  786. u_wm_focus((unsigned int)idx);
  787. if (mx >= (unsigned int)(u->info.x + (int)u->info.w -
  788. 1 - WM_CLOSE_W) &&
  789. mx < (unsigned int)(u->info.x + (int)u->info.w) &&
  790. my >= (unsigned int)(u->info.y + 1) &&
  791. my < (unsigned int)(u->info.y + 1 + WM_TITLE_H)) {
  792. u_wm_close((unsigned int)idx);
  793. full_redraw = 1;
  794. } else if (mx >= (unsigned int)(u->info.x + (int)u->info.w - 16) &&
  795. my >= (unsigned int)(u->info.y + (int)u->info.h - 16)) {
  796. resize_id = idx;
  797. resize_mx = mx;
  798. resize_my = my;
  799. resize_ow = u->info.w;
  800. resize_oh = u->info.h;
  801. } else if (my < (unsigned int)(u->info.y + WM_TITLE_H)) {
  802. drag_id = idx;
  803. drag_dx = (int)mx - u->info.x;
  804. drag_dy = (int)my - u->info.y;
  805. }
  806. }
  807. }
  808. } else if (down && was && resize_id >= 0) {
  809. struct uwin *u = &wins[resize_id];
  810. if (u->info.used) {
  811. unsigned int oldw = u->info.w, oldh = u->info.h;
  812. int nw = (int)resize_ow + (int)mx - (int)resize_mx;
  813. int nh = (int)resize_oh + (int)my - (int)resize_my;
  814. int maxw = (int)fbw - u->info.x;
  815. int maxh = (int)fbh - u->info.y;
  816. if (nw < 200) nw = 200;
  817. if (nh < 140) nh = 140;
  818. if (nw > maxw) nw = maxw;
  819. if (nh > maxh) nh = maxh;
  820. if (nw != (int)u->info.w || nh != (int)u->info.h) {
  821. struct wm_window_info ni;
  822. struct wm_map_info nm;
  823. struct wm_present_info present;
  824. unsigned int i;
  825. if (u_wm_resize((unsigned int)resize_id,
  826. (unsigned int)(nw - 2),
  827. (unsigned int)(nh - WM_TITLE_H - 2)) == 0) {
  828. if (u_wm_info((unsigned int)resize_id, &ni) == 1)
  829. u->info = ni;
  830. u->mapped = 0;
  831. if (u_wm_map((unsigned int)resize_id, &nm) == 0) {
  832. u->map = nm;
  833. u->mapped = 1;
  834. }
  835. present.count = 0;
  836. present.immediate = 1;
  837. add_rect(&present, u->info.x, u->info.y, oldw, oldh);
  838. add_rect(&present, u->info.x, u->info.y,
  839. u->info.w, u->info.h);
  840. if (u_wm_begin() == 0) {
  841. for (i = 0; i < present.count; i++)
  842. draw_rect(present.rect[i].x,
  843. present.rect[i].y,
  844. present.rect[i].w,
  845. present.rect[i].h);
  846. u_wm_present(&present);
  847. }
  848. }
  849. }
  850. } else {
  851. resize_id = -1;
  852. }
  853. } else if (down && was && drag_id >= 0) {
  854. struct uwin *u = &wins[drag_id];
  855. if (u->info.used) {
  856. int ox = u->info.x, oy = u->info.y;
  857. int nx = (int)mx - drag_dx;
  858. int ny = (int)my - drag_dy;
  859. clamp_geom(u, &nx, &ny);
  860. if (nx != ox || ny != oy) {
  861. struct wm_present_info present;
  862. unsigned int i;
  863. u_wm_set_geom((unsigned int)drag_id, nx, ny);
  864. u->info.x = nx;
  865. u->info.y = ny;
  866. if (u->info.focused) {
  867. move_window_pixels(u, ox, oy, nx, ny);
  868. } else {
  869. present.count = 0;
  870. present.immediate = 1;
  871. add_rect(&present, ox, oy, u->info.w, u->info.h);
  872. add_rect(&present, nx, ny, u->info.w, u->info.h);
  873. if (u_wm_begin() == 0) {
  874. for (i = 0; i < present.count; i++)
  875. draw_rect(present.rect[i].x,
  876. present.rect[i].y,
  877. present.rect[i].w,
  878. present.rect[i].h);
  879. u_wm_present(&present);
  880. }
  881. }
  882. }
  883. } else {
  884. drag_id = -1;
  885. }
  886. } else if (!down && was) {
  887. drag_id = -1;
  888. resize_id = -1;
  889. }
  890. prev_buttons = buttons;
  891. }
  892. static char *put_str(char *p, const char *s)
  893. {
  894. while (*s)
  895. *p++ = *s++;
  896. return p;
  897. }
  898. static char *put_dec(char *p, unsigned int v)
  899. {
  900. char b[10];
  901. unsigned int n = 0;
  902. do {
  903. b[n++] = (char)('0' + v % 10);
  904. v /= 10;
  905. } while (v);
  906. while (n)
  907. *p++ = b[--n];
  908. return p;
  909. }
  910. /* Expose the last interval in /share so the target can be measured without
  911. * a serial console. */
  912. static void write_stats(void)
  913. {
  914. char b[80], *p = b;
  915. unsigned int now = u_ticks_ms();
  916. p = put_str(p, "t=");
  917. p = put_dec(p, now / 1000);
  918. p = put_str(p, "s frames=");
  919. p = put_dec(p, stat_frames);
  920. p = put_str(p, " ms=");
  921. p = put_dec(p, stat_ms);
  922. p = put_str(p, " max=");
  923. p = put_dec(p, stat_max);
  924. p = put_str(p, " px=");
  925. p = put_dec(p, stat_px);
  926. p = put_str(p, "\n");
  927. u_syscall3(SYS_WRITE_FILE, (int)"/share/wmstats", (int)b,
  928. (int)(p - b));
  929. }
  930. void _start(const char *arg)
  931. {
  932. struct wm_screen_info si;
  933. struct wm_status st;
  934. unsigned int i;
  935. (void)arg;
  936. if (u_wm_attach(&si) != 0)
  937. u_exit(1);
  938. usurf_init(&screen, si.fb, si.width, si.height, si.pitch, si.bpp);
  939. usurf_font_attach(&screen, "/ramfs/fonts/sans.ttf", 14);
  940. fbw = si.width;
  941. fbh = si.height;
  942. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  943. wins[i].info.used = 0;
  944. wins[i].mapped = 0;
  945. }
  946. full_redraw = 1;
  947. for (;;) {
  948. unsigned int t0, t1;
  949. if (u_wm_poll(&st) != 0)
  950. u_exit(1);
  951. if (st.full_redraw)
  952. full_redraw = 1;
  953. usurf_pixels = 0;
  954. t0 = u_ticks_ms();
  955. handle_mouse(&st);
  956. do_update(&st);
  957. t1 = u_ticks_ms();
  958. if (usurf_pixels) {
  959. unsigned int dt = t1 - t0;
  960. stat_frames++;
  961. stat_ms += dt;
  962. stat_px += usurf_pixels;
  963. if (dt > stat_max)
  964. stat_max = dt;
  965. }
  966. if (t1 - stat_last >= 2000) {
  967. stat_last = t1;
  968. if (stat_frames) {
  969. write_stats();
  970. stat_frames = stat_ms = stat_px = stat_max = 0;
  971. }
  972. }
  973. u_sleep_ms(10);
  974. }
  975. }