ucmd_wm.c 26 KB

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