ucmd_wm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 0xBE3C3Cu
  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 unsigned int prev_buttons;
  27. static int full_redraw = 1;
  28. static unsigned int bar_kexec_busy, bar_kexec_progress, bar_kexec_total;
  29. static unsigned int rgb16(unsigned int r, unsigned int g, unsigned int b)
  30. {
  31. return (r << 16) | (g << 8) | b;
  32. }
  33. static void fill_clip(int x, int y, unsigned int w, unsigned int h,
  34. unsigned int rgb, int cx, int cy,
  35. unsigned int cw, unsigned int ch)
  36. {
  37. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  38. if (x0 < cx) x0 = cx;
  39. if (y0 < cy) y0 = cy;
  40. if (x1 > cx + (int)cw) x1 = cx + (int)cw;
  41. if (y1 > cy + (int)ch) y1 = cy + (int)ch;
  42. if (x1 > x0 && y1 > y0)
  43. usurf_fill(&screen, x0, y0, (unsigned int)(x1 - x0),
  44. (unsigned int)(y1 - y0), rgb);
  45. }
  46. static void draw_bar(void)
  47. {
  48. char dt[48];
  49. unsigned int n, i, x, y;
  50. usurf_fill(&screen, 0, 0, fbw, WM_BAR_H, COL_BG);
  51. usurf_text(&screen, 4, 5, OS_NAME " v" OS_VERSION, COL_BANNER,
  52. 0, 0, fbw, WM_BAR_H);
  53. usurf_fill(&screen, WM_BTN_X, WM_BTN_Y, WM_BTN_W, WM_BTN_H, 0x963CD2u);
  54. usurf_text(&screen, WM_BTN_X + 4, WM_BTN_Y + 3, "+ New", COL_WHITE,
  55. 0, 0, fbw, WM_BAR_H);
  56. /* Rainbow separator (same y as the kernel's old bar). */
  57. for (x = 0; x < fbw; x++) {
  58. unsigned int seg = (x * (6 * 256)) / (fbw ? fbw : 1);
  59. unsigned int s = seg / 256, f = seg % 256;
  60. unsigned int r1, g1, b1, r2, g2, b2, r, g, b;
  61. switch (s) {
  62. case 0: r1=255; g1=0; b1=0; r2=255; g2=255; b2=0; break;
  63. case 1: r1=255; g1=255; b1=0; r2=0; g2=255; b2=0; break;
  64. case 2: r1=0; g1=255; b1=0; r2=0; g2=255; b2=255; break;
  65. case 3: r1=0; g1=255; b1=255; r2=0; g2=0; b2=255; break;
  66. case 4: r1=0; g1=0; b1=255; r2=255; g2=0; b2=255; break;
  67. default:r1=255; g1=0; b1=255; r2=255; g2=0; b2=0; break;
  68. }
  69. r = (r1 * (256 - f) + r2 * f) / 256;
  70. g = (g1 * (256 - f) + g2 * f) / 256;
  71. b = (b1 * (256 - f) + b2 * f) / 256;
  72. for (y = 18; y < 20; y++)
  73. usurf_fill(&screen, (int)x, (int)y, 1, 1, rgb16(r, g, b));
  74. }
  75. if (bar_kexec_busy) {
  76. static const char msg[] = "loading new kernel over serial";
  77. unsigned int dots = bar_kexec_total ?
  78. (bar_kexec_progress * 12 / bar_kexec_total) : 0;
  79. n = 0;
  80. while (msg[n])
  81. dt[n] = msg[n], n++;
  82. for (i = 0; i < 12; i++)
  83. dt[n + i] = (i < dots) ? '.' : ' ';
  84. n += 12;
  85. dt[n] = '\0';
  86. usurf_text(&screen, (int)fbw - (int)n * 8 - 4, 5, dt,
  87. 0xFFC864u, 0, 0, fbw, WM_BAR_H);
  88. } else if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  89. n = 0;
  90. while (dt[n] && n < sizeof(dt) - 1)
  91. n++;
  92. usurf_text(&screen, (int)fbw - (int)n * 8 - 4, 5, dt,
  93. COL_BANNER, 0, 0, fbw, WM_BAR_H);
  94. }
  95. }
  96. static void draw_window_clip(unsigned int idx, int cx, int cy,
  97. unsigned int cw, unsigned int ch)
  98. {
  99. struct uwin *u = &wins[idx];
  100. struct wm_window_info *w = &u->info;
  101. int x = w->x, y = w->y;
  102. unsigned int pw = w->w, ph = w->h;
  103. unsigned int bx = (unsigned int)(x + (int)pw - 1 - WM_CLOSE_W);
  104. unsigned int cony = (unsigned int)(y + 1 + WM_TITLE_H);
  105. unsigned int content_x = (unsigned int)(x + 1);
  106. unsigned int content_w = w->content_w;
  107. unsigned int content_h = w->content_h;
  108. /* Border, title bar, close button. */
  109. fill_clip(x, y, pw, ph, COL_BORDER, cx, cy, cw, ch);
  110. fill_clip(x + 1, y + 1, pw - 2, WM_TITLE_H,
  111. w->focused ? COL_TITLE_ON : COL_TITLE_OFF, cx, cy, cw, ch);
  112. usurf_text(&screen, x + 4, y + 3, w->title, COL_WHITE,
  113. cx, cy, cw, ch);
  114. fill_clip((int)bx, y + 1, WM_CLOSE_W, WM_TITLE_H, COL_CLOSE,
  115. cx, cy, cw, ch);
  116. usurf_text(&screen, (int)bx + 3, y + 3, "x", COL_WHITE,
  117. cx, cy, cw, ch);
  118. /* Content. */
  119. fill_clip((int)content_x, (int)cony, content_w, content_h,
  120. 0x000000u, cx, cy, cw, ch);
  121. if (u->mapped) {
  122. if (w->kind == WM_KIND_GFX) {
  123. struct uwm_canvas c;
  124. c.px = (const unsigned short *)u->map.va;
  125. c.w = u->map.w;
  126. c.h = u->map.h;
  127. usurf_canvas(&screen, &c, (int)content_x, (int)cony,
  128. cx, cy, cw, ch);
  129. } else if (u->map.kind == WM_KIND_TEXT) {
  130. usurf_cells(&screen, (const unsigned short *)u->map.va,
  131. w->cols, w->rows, (int)content_x, (int)cony,
  132. cx, cy, cw, ch);
  133. }
  134. }
  135. /* Text cursor overlay (only for the focused terminal). */
  136. if (w->kind == WM_KIND_TEXT && w->focused && w->cursor_visible &&
  137. w->cols && w->rows && w->cursor < w->cols * w->rows) {
  138. unsigned int col = w->cursor % w->cols;
  139. unsigned int row = w->cursor / w->cols;
  140. unsigned int px = content_x + col * 8;
  141. unsigned int py = cony + row * 10;
  142. fill_clip((int)px, (int)py, 2, 10, COL_WHITE,
  143. cx, cy, cw, ch);
  144. }
  145. }
  146. static void draw_rect(int x, int y, unsigned int w, unsigned int h)
  147. {
  148. unsigned int i;
  149. if (w == 0 || h == 0 || x >= (int)fbw || y >= (int)fbh)
  150. return;
  151. usurf_fill(&screen, x, y, w, h, COL_BG);
  152. for (i = 0; i < WM_MAX_WINDOWS; i++)
  153. if (wins[i].info.used && !wins[i].info.focused &&
  154. x < wins[i].info.x + (int)wins[i].info.w &&
  155. x + (int)w > wins[i].info.x &&
  156. y < wins[i].info.y + (int)wins[i].info.h &&
  157. y + (int)h > wins[i].info.y)
  158. draw_window_clip(i, x, y, w, h);
  159. for (i = 0; i < WM_MAX_WINDOWS; i++)
  160. if (wins[i].info.used && wins[i].info.focused &&
  161. x < wins[i].info.x + (int)wins[i].info.w &&
  162. x + (int)w > wins[i].info.x &&
  163. y < wins[i].info.y + (int)wins[i].info.h &&
  164. y + (int)h > wins[i].info.y)
  165. draw_window_clip(i, x, y, w, h);
  166. }
  167. static int window_at(unsigned int mx, unsigned int my)
  168. {
  169. unsigned int i;
  170. for (i = 0; i < WM_MAX_WINDOWS; i++)
  171. if (wins[i].info.used && wins[i].info.focused &&
  172. mx >= (unsigned int)wins[i].info.x &&
  173. my >= (unsigned int)wins[i].info.y &&
  174. mx < (unsigned int)(wins[i].info.x + (int)wins[i].info.w) &&
  175. my < (unsigned int)(wins[i].info.y + (int)wins[i].info.h))
  176. return (int)i;
  177. for (i = 0; i < WM_MAX_WINDOWS; i++)
  178. if (wins[i].info.used && !wins[i].info.focused &&
  179. mx >= (unsigned int)wins[i].info.x &&
  180. my >= (unsigned int)wins[i].info.y &&
  181. mx < (unsigned int)(wins[i].info.x + (int)wins[i].info.w) &&
  182. my < (unsigned int)(wins[i].info.y + (int)wins[i].info.h))
  183. return (int)i;
  184. return -1;
  185. }
  186. static void clamp_geom(struct uwin *u, int *x, int *y)
  187. {
  188. int maxx = (int)fbw - (int)u->info.w;
  189. int maxy = (int)fbh - (int)u->info.h;
  190. if (*x < 0) *x = 0;
  191. if (*y < WM_BAR_H) *y = WM_BAR_H;
  192. if (maxx < 0) maxx = 0;
  193. if (maxy < WM_BAR_H) maxy = WM_BAR_H;
  194. if (*x > maxx) *x = maxx;
  195. if (*y > maxy) *y = maxy;
  196. }
  197. static void add_rect(struct wm_present_info *p, int x, int y,
  198. unsigned int w, unsigned int h)
  199. {
  200. if (w == 0 || h == 0)
  201. return;
  202. if (p->count < 4) {
  203. p->rect[p->count].x = x;
  204. p->rect[p->count].y = y;
  205. p->rect[p->count].w = w;
  206. p->rect[p->count].h = h;
  207. p->count++;
  208. } else {
  209. /* Four is enough for normal updates; fold the rest into the
  210. * last rect rather than dropping pixels. */
  211. int x0 = p->rect[3].x, y0 = p->rect[3].y;
  212. int x1 = x0 + (int)p->rect[3].w, y1 = y0 + (int)p->rect[3].h;
  213. int nx1 = x + (int)w, ny1 = y + (int)h;
  214. if (x < x0) x0 = x;
  215. if (y < y0) y0 = y;
  216. if (nx1 > x1) x1 = nx1;
  217. if (ny1 > y1) y1 = ny1;
  218. p->rect[3].x = x0;
  219. p->rect[3].y = y0;
  220. p->rect[3].w = (unsigned int)(x1 - x0);
  221. p->rect[3].h = (unsigned int)(y1 - y0);
  222. }
  223. }
  224. static void do_update(struct wm_status *st)
  225. {
  226. struct wm_present_info present;
  227. int need = 0;
  228. int bar_dirty = 0;
  229. unsigned int i;
  230. char dt[20];
  231. static char last_dt[20];
  232. int kexec_changed = (st->kexec_busy != bar_kexec_busy) ||
  233. (st->kexec_busy && st->kexec_progress != bar_kexec_progress);
  234. bar_kexec_busy = st->kexec_busy;
  235. bar_kexec_progress = st->kexec_progress;
  236. bar_kexec_total = st->kexec_total;
  237. if (kexec_changed)
  238. bar_dirty = 1;
  239. /* Refresh cached window state and mappings before opening a frame:
  240. * MAP may unmap/remap content. */
  241. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  242. struct wm_window_info info;
  243. int r = u_wm_info(i, &info);
  244. if (r < 0) {
  245. u_exit(1);
  246. }
  247. if (r == 0 || !info.used) {
  248. if (wins[i].info.used)
  249. full_redraw = 1;
  250. wins[i].info.used = 0;
  251. wins[i].mapped = 0;
  252. continue;
  253. }
  254. if (!wins[i].info.used || info.content_gen != wins[i].info.content_gen ||
  255. info.kind != wins[i].info.kind)
  256. full_redraw = 1;
  257. wins[i].info = info;
  258. if (!wins[i].mapped || wins[i].map.kind != info.kind ||
  259. wins[i].map.generation != info.content_gen) {
  260. struct wm_map_info map;
  261. if (u_wm_map(i, &map) == 0) {
  262. wins[i].map = map;
  263. wins[i].mapped = 1;
  264. } else {
  265. wins[i].mapped = 0;
  266. }
  267. }
  268. }
  269. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  270. for (i = 0; i < sizeof(dt); i++)
  271. if (dt[i] != last_dt[i]) {
  272. for (i = 0; i < sizeof(dt); i++)
  273. last_dt[i] = dt[i];
  274. bar_dirty = 1;
  275. break;
  276. }
  277. }
  278. if (full_redraw) {
  279. if (u_wm_begin() != 0) {
  280. full_redraw = 1;
  281. return;
  282. }
  283. full_redraw = 0;
  284. need = 1;
  285. {
  286. usurf_fill(&screen, 0, 0, fbw, fbh, COL_BG);
  287. for (i = 0; i < WM_MAX_WINDOWS; i++)
  288. if (wins[i].info.used && !wins[i].info.focused)
  289. draw_window_clip(i, 0, 0, fbw, fbh);
  290. for (i = 0; i < WM_MAX_WINDOWS; i++)
  291. if (wins[i].info.used && wins[i].info.focused)
  292. draw_window_clip(i, 0, 0, fbw, fbh);
  293. draw_bar();
  294. present.count = 0;
  295. add_rect(&present, 0, 0, fbw, fbh);
  296. present.immediate = 0;
  297. u_wm_present(&present);
  298. /* Ack every window that was clean at the time of the
  299. * redraw. If new damage arrived during it, its seq
  300. * differs and the kernel leaves it dirty. */
  301. for (i = 0; i < WM_MAX_WINDOWS; i++)
  302. if (wins[i].info.used && wins[i].mapped)
  303. u_wm_ack(i, wins[i].info.dirty_seq);
  304. }
  305. return;
  306. }
  307. /* Partial cell/canvas updates and the bar clock. */
  308. present.count = 0;
  309. present.immediate = 0;
  310. if (bar_dirty) {
  311. need = 1;
  312. add_rect(&present, 0, 0, fbw, WM_BAR_H);
  313. }
  314. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  315. struct uwin *u = &wins[i];
  316. struct wm_window_info *w = &u->info;
  317. int rx, ry;
  318. unsigned int rw, rh;
  319. if (!w->used || !w->dirty)
  320. continue;
  321. if (w->kind == WM_KIND_GFX) {
  322. rx = w->x + 1;
  323. ry = w->y + 1 + WM_TITLE_H;
  324. rw = w->content_w;
  325. rh = w->content_h;
  326. } else {
  327. if (!w->dmg_cols || !w->dmg_rows)
  328. continue;
  329. rx = w->x + 1 + (int)w->dmg_col0 * 8;
  330. ry = w->y + 1 + WM_TITLE_H + (int)w->dmg_row0 * 10;
  331. rw = w->dmg_cols * 8;
  332. rh = w->dmg_rows * 10;
  333. }
  334. add_rect(&present, rx, ry, rw, rh);
  335. u->ack_seq = u->mapped ? w->dirty_seq : 0;
  336. need = 1;
  337. }
  338. if (!need) {
  339. /* Drag movement is handled below; a stationary mouse can be
  340. * left to the kernel's cursor overlay. */
  341. return;
  342. }
  343. if (u_wm_begin() != 0)
  344. return;
  345. for (i = 0; i < present.count; i++)
  346. draw_rect(present.rect[i].x, present.rect[i].y,
  347. present.rect[i].w, present.rect[i].h);
  348. if (bar_dirty)
  349. draw_bar();
  350. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  351. struct uwin *u = &wins[i];
  352. if (!u->info.used || !u->info.dirty || !u->mapped ||
  353. !u->ack_seq)
  354. continue;
  355. if (u->ack_seq == u->info.dirty_seq)
  356. u_wm_ack(i, u->info.dirty_seq);
  357. }
  358. u_wm_present(&present);
  359. }
  360. static void handle_mouse(struct wm_status *st)
  361. {
  362. unsigned int buttons = st->mouse.buttons;
  363. unsigned int mx = st->mouse.x, my = st->mouse.y;
  364. int down = (buttons & 1) != 0;
  365. int was = (prev_buttons & 1) != 0;
  366. if (down && !was) {
  367. if (mx >= WM_BTN_X && my >= WM_BTN_Y &&
  368. mx < WM_BTN_X + WM_BTN_W && my < WM_BTN_Y + WM_BTN_H) {
  369. u_wm_create_term();
  370. full_redraw = 1;
  371. } else {
  372. int idx = window_at(mx, my);
  373. if (idx >= 0) {
  374. struct uwin *u = &wins[idx];
  375. u_wm_focus((unsigned int)idx);
  376. if (mx >= (unsigned int)(u->info.x + (int)u->info.w -
  377. 1 - WM_CLOSE_W) &&
  378. mx < (unsigned int)(u->info.x + (int)u->info.w) &&
  379. my >= (unsigned int)(u->info.y + 1) &&
  380. my < (unsigned int)(u->info.y + 1 + WM_TITLE_H)) {
  381. u_wm_close((unsigned int)idx);
  382. full_redraw = 1;
  383. } else if (my < (unsigned int)(u->info.y + WM_TITLE_H)) {
  384. drag_id = idx;
  385. drag_dx = (int)mx - u->info.x;
  386. drag_dy = (int)my - u->info.y;
  387. }
  388. }
  389. }
  390. } else if (down && was && drag_id >= 0) {
  391. struct uwin *u = &wins[drag_id];
  392. if (u->info.used) {
  393. int ox = u->info.x, oy = u->info.y;
  394. int nx = (int)mx - drag_dx;
  395. int ny = (int)my - drag_dy;
  396. clamp_geom(u, &nx, &ny);
  397. if (nx != ox || ny != oy) {
  398. struct wm_present_info present;
  399. unsigned int i;
  400. u_wm_set_geom((unsigned int)drag_id, nx, ny);
  401. u->info.x = nx;
  402. u->info.y = ny;
  403. present.count = 0;
  404. present.immediate = 1;
  405. add_rect(&present, ox, oy, u->info.w, u->info.h);
  406. add_rect(&present, nx, ny, u->info.w, u->info.h);
  407. if (u_wm_begin() == 0) {
  408. for (i = 0; i < present.count; i++)
  409. draw_rect(present.rect[i].x,
  410. present.rect[i].y,
  411. present.rect[i].w,
  412. present.rect[i].h);
  413. u_wm_present(&present);
  414. }
  415. }
  416. } else {
  417. drag_id = -1;
  418. }
  419. } else if (!down && was) {
  420. drag_id = -1;
  421. }
  422. prev_buttons = buttons;
  423. }
  424. void _start(const char *arg)
  425. {
  426. struct wm_screen_info si;
  427. struct wm_status st;
  428. unsigned int i;
  429. (void)arg;
  430. if (u_wm_attach(&si) != 0)
  431. u_exit(1);
  432. usurf_init(&screen, si.fb, si.width, si.height, si.pitch, si.bpp);
  433. fbw = si.width;
  434. fbh = si.height;
  435. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  436. wins[i].info.used = 0;
  437. wins[i].mapped = 0;
  438. }
  439. full_redraw = 1;
  440. for (;;) {
  441. if (u_wm_poll(&st) != 0)
  442. u_exit(1);
  443. if (st.full_redraw)
  444. full_redraw = 1;
  445. handle_mouse(&st);
  446. do_update(&st);
  447. u_sleep_ms(10);
  448. }
  449. }