wm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /* Window manager: fixed-size draggable terminal windows + a system bar.
  2. Slot 0 runs wm_run(); each terminal window is a separate process. */
  3. #include "wm.h"
  4. #include "cirrus.h"
  5. #include "fbtext.h"
  6. #include "idle.h"
  7. #include "io.h"
  8. #include "kdbg.h"
  9. #include "kexec.h"
  10. #include "mouse.h"
  11. #include "netkexec.h"
  12. #include "netlog.h"
  13. #include "payload.h"
  14. #include "process.h"
  15. #include "rtc.h"
  16. #include "sys_gfx.h"
  17. #include "timer.h"
  18. #include "version.h"
  19. #include "vga.h"
  20. static struct window windows[MAX_WINDOWS];
  21. static struct term terms[MAX_WINDOWS];
  22. static unsigned int term_counter;
  23. static int wm_full_redraw;
  24. /* Mouse drag state. */
  25. static int drag_active;
  26. static int drag_window; /* index into windows[] */
  27. static int drag_dx, drag_dy; /* mouse offset from window origin */
  28. static unsigned int prev_buttons;
  29. void wm_request_redraw(void)
  30. {
  31. wm_full_redraw = 1;
  32. }
  33. struct window *wm_focused_window(void)
  34. {
  35. unsigned int i;
  36. for (i = 0; i < MAX_WINDOWS; i++)
  37. if (windows[i].used && windows[i].focused)
  38. return &windows[i];
  39. return (void *)0;
  40. }
  41. int wm_is_focused(const struct window *w)
  42. {
  43. return w->focused;
  44. }
  45. /* The terminal whose screen is `screen`, or NULL: a spawned ring-3 command
  46. * inherits its terminal's screen pointer, so this maps it back to the window
  47. * (used by the sys_gfx window-canvas service). */
  48. struct term *wm_term_for_screen(struct text_screen *screen)
  49. {
  50. unsigned int i;
  51. for (i = 0; i < MAX_WINDOWS; i++)
  52. if (windows[i].used && windows[i].term &&
  53. &windows[i].term->screen == screen)
  54. return windows[i].term;
  55. return 0;
  56. }
  57. /* Pixel size of a window's content area (the size of its gfx canvas). */
  58. int wm_content_size(struct term *t, unsigned int *w, unsigned int *h)
  59. {
  60. unsigned int i;
  61. for (i = 0; i < MAX_WINDOWS; i++)
  62. if (windows[i].used && windows[i].term == t) {
  63. *w = windows[i].pw - 2;
  64. *h = SCREEN_ROWS * 10;
  65. return 0;
  66. }
  67. return -1;
  68. }
  69. static int wm_point_in_rect(unsigned int px, unsigned int py,
  70. unsigned int rx, unsigned int ry, unsigned int rw, unsigned int rh)
  71. {
  72. return px >= rx && px < rx + rw && py >= ry && py < ry + rh;
  73. }
  74. static void wm_make_title(struct window *w, unsigned int n)
  75. {
  76. char num[4];
  77. unsigned int i = 0, k = 0;
  78. w->title[k++] = 't'; w->title[k++] = 'e'; w->title[k++] = 'r';
  79. w->title[k++] = 'm'; w->title[k++] = ' ';
  80. do {
  81. num[i++] = (char)('0' + (n % 10));
  82. n /= 10;
  83. } while (n && i < 3);
  84. while (i)
  85. w->title[k++] = num[--i];
  86. w->title[k] = '\0';
  87. }
  88. int wm_spawn_terminal(void)
  89. {
  90. unsigned int i, j, fbw = vga_fb_width();
  91. struct window *w;
  92. struct term *t;
  93. int pid;
  94. unsigned int n;
  95. for (i = 0; i < MAX_WINDOWS; i++)
  96. if (!windows[i].used)
  97. break;
  98. if (i == MAX_WINDOWS)
  99. return -1;
  100. w = &windows[i];
  101. t = &terms[i];
  102. screen_init(&t->screen);
  103. t->screen.cursor_visible = 1;
  104. t->key_head = t->key_tail = 0;
  105. t->history_count = 0;
  106. t->history_next = 0;
  107. t->cwd[0] = '/';
  108. t->cwd[1] = '\0';
  109. w->used = 1;
  110. w->term = t;
  111. w->pid = 0;
  112. w->focused = 0;
  113. w->pw = SCREEN_COLS * 8 + 2;
  114. w->ph = WM_TITLE_H + SCREEN_ROWS * 10 + 2;
  115. /* Cascade position, clamped to the desktop area. */
  116. n = term_counter % 5;
  117. w->x = 24 + n * 32;
  118. w->y = WM_BAR_H + 12 + n * 28;
  119. if (w->x + w->pw > fbw)
  120. w->x = (fbw > w->pw) ? fbw - w->pw : 0;
  121. if (w->y + w->ph > vga_fb_height())
  122. w->y = WM_BAR_H + 4;
  123. term_counter++;
  124. wm_make_title(w, term_counter);
  125. pid = process_create_ptr("term", term_entry, t);
  126. if (pid < 0) {
  127. w->used = 0;
  128. return -1;
  129. }
  130. w->pid = (unsigned int)pid;
  131. for (j = 0; j < MAX_WINDOWS; j++)
  132. if (windows[j].used)
  133. windows[j].focused = (j == i);
  134. wm_request_redraw();
  135. return pid;
  136. }
  137. static void wm_focus_index(unsigned int idx)
  138. {
  139. unsigned int i;
  140. if (windows[idx].used && windows[idx].focused)
  141. return;
  142. for (i = 0; i < MAX_WINDOWS; i++)
  143. if (windows[i].used)
  144. windows[i].focused = (i == idx);
  145. wm_request_redraw();
  146. }
  147. static void wm_focus_top(void)
  148. {
  149. unsigned int i;
  150. for (i = 0; i < MAX_WINDOWS; i++)
  151. if (windows[i].used) {
  152. wm_focus_index(i);
  153. return;
  154. }
  155. }
  156. static void wm_close_window(unsigned int idx)
  157. {
  158. struct window *w = &windows[idx];
  159. sys_gfx_kill_for_screen(&w->term->screen); /* stop a running demo */
  160. if (w->pid)
  161. process_kill(w->pid);
  162. w->used = 0;
  163. w->pid = 0;
  164. w->focused = 0;
  165. wm_focus_top();
  166. wm_request_redraw();
  167. }
  168. /* Topmost window containing (px,py), or -1. */
  169. static int wm_window_at(unsigned int px, unsigned int py)
  170. {
  171. unsigned int i;
  172. for (i = 0; i < MAX_WINDOWS; i++)
  173. if (windows[i].used && windows[i].focused &&
  174. wm_point_in_rect(px, py, windows[i].x, windows[i].y,
  175. windows[i].pw, windows[i].ph))
  176. return (int)i;
  177. for (i = 0; i < MAX_WINDOWS; i++)
  178. if (windows[i].used && !windows[i].focused &&
  179. wm_point_in_rect(px, py, windows[i].x, windows[i].y,
  180. windows[i].pw, windows[i].ph))
  181. return (int)i;
  182. return -1;
  183. }
  184. static void wm_clamp_window(struct window *w, int nx, int ny)
  185. {
  186. unsigned int fbw = vga_fb_width();
  187. unsigned int fbh = vga_fb_height();
  188. int maxx = (int)fbw - (int)w->pw;
  189. int maxy = (int)fbh - (int)w->ph;
  190. if (maxx < 0)
  191. maxx = 0;
  192. if (maxy < (int)WM_BAR_H)
  193. maxy = (int)WM_BAR_H;
  194. if (nx < 0)
  195. nx = 0;
  196. else if (nx > maxx)
  197. nx = maxx;
  198. if (ny < (int)WM_BAR_H)
  199. ny = (int)WM_BAR_H;
  200. else if (ny > maxy)
  201. ny = maxy;
  202. w->x = (unsigned int)nx;
  203. w->y = (unsigned int)ny;
  204. }
  205. /* Overlap-safe copy. Moving a window copies most of its area, so take four
  206. bytes at a time when both rows are dword aligned (32 bpp rows always are)
  207. and fall back to bytes otherwise (24 bpp at an odd x). */
  208. static void wm_memmove(unsigned char *dst, const unsigned char *src, unsigned int n)
  209. {
  210. if (dst == src || n == 0)
  211. return;
  212. if ((((unsigned int)dst | (unsigned int)src) & 3) == 0) {
  213. unsigned int words = n >> 2;
  214. unsigned int i;
  215. unsigned int *dw = (unsigned int *)dst;
  216. const unsigned int *sw = (const unsigned int *)src;
  217. if (dst < src) {
  218. for (i = 0; i < words; i++)
  219. dw[i] = sw[i];
  220. for (i = words << 2; i < n; i++)
  221. dst[i] = src[i];
  222. } else {
  223. /* Overlapping move down: back to front. */
  224. for (i = n; i > (words << 2); i--)
  225. dst[i - 1] = src[i - 1];
  226. for (i = words; i-- > 0; )
  227. dw[i] = sw[i];
  228. }
  229. return;
  230. }
  231. if (dst < src) {
  232. unsigned int i;
  233. for (i = 0; i < n; i++)
  234. dst[i] = src[i];
  235. } else {
  236. while (n--)
  237. dst[n] = src[n];
  238. }
  239. }
  240. /* Copy a rectangle within the shadow buffer (to mirror a BitBLT'd drag). */
  241. static void wm_shadow_copy(unsigned int ox, unsigned int oy,
  242. unsigned int nx, unsigned int ny,
  243. unsigned int w, unsigned int h)
  244. {
  245. unsigned char *fb = (unsigned char *)vga_fb_ptr();
  246. unsigned int pitch = vga_fb_pitch();
  247. unsigned int bpp = vga_fb_bpp();
  248. unsigned int pb = (bpp == 16) ? 2 : (bpp == 24) ? 3 : 4;
  249. unsigned int row_bytes = w * pb;
  250. unsigned int r;
  251. if (ny > oy || (ny == oy && nx > ox)) {
  252. for (r = h; r-- > 0; )
  253. wm_memmove(fb + (ny + r) * pitch + nx * pb,
  254. fb + (oy + r) * pitch + ox * pb, row_bytes);
  255. } else {
  256. for (r = 0; r < h; r++)
  257. wm_memmove(fb + (ny + r) * pitch + nx * pb,
  258. fb + (oy + r) * pitch + ox * pb, row_bytes);
  259. }
  260. }
  261. /* Fill the intersection of rect (rx,ry,rw,rh) with clip rect (cx,cy,cw,ch). */
  262. static void wm_fill_rect_clip(unsigned int rx, unsigned int ry, unsigned int rw, unsigned int rh,
  263. unsigned int cx, unsigned int cy, unsigned int cw, unsigned int ch,
  264. unsigned int r, unsigned int g, unsigned int b)
  265. {
  266. unsigned int x0 = (rx > cx) ? rx : cx;
  267. unsigned int y0 = (ry > cy) ? ry : cy;
  268. unsigned int x1 = (rx + rw < cx + cw) ? rx + rw : cx + cw;
  269. unsigned int y1 = (ry + rh < cy + ch) ? ry + rh : cy + ch;
  270. if (x1 > x0 && y1 > y0)
  271. vga_fill_rect(x0, y0, x1 - x0, y1 - y0, r, g, b);
  272. }
  273. static void wm_draw_window_clip(struct window *w, unsigned int cx, unsigned int cy,
  274. unsigned int cw, unsigned int ch);
  275. /* Repaint the desktop rectangle (x, y, w, h): background, then every
  276. intersecting window back-to-front (focused last), then mark it dirty. The
  277. caller handles the mouse cursor. The rect must lie below the system bar
  278. (windows are clamped to y >= WM_BAR_H, so the bar is never overlapped and
  279. needs no clip variant). */
  280. static void wm_redraw_rect(unsigned int x, unsigned int y,
  281. unsigned int w, unsigned int h)
  282. {
  283. unsigned int i;
  284. if (w == 0 || h == 0)
  285. return;
  286. vga_fill_rect(x, y, w, h, 32, 32, 48);
  287. for (i = 0; i < MAX_WINDOWS; i++) {
  288. struct window *w2 = &windows[i];
  289. if (w2->used && !w2->focused &&
  290. x < w2->x + w2->pw && x + w > w2->x &&
  291. y < w2->y + w2->ph && y + h > w2->y)
  292. wm_draw_window_clip(w2, x, y, w, h);
  293. }
  294. for (i = 0; i < MAX_WINDOWS; i++) {
  295. struct window *w2 = &windows[i];
  296. if (w2->used && w2->focused &&
  297. x < w2->x + w2->pw && x + w > w2->x &&
  298. y < w2->y + w2->ph && y + h > w2->y)
  299. wm_draw_window_clip(w2, x, y, w, h);
  300. }
  301. vga_fb_mark_dirty_rect_xy(y, h, x, x + w);
  302. }
  303. /* Move a window in the shadow buffer: copy its rect, redraw the exposed
  304. sliver, keep the cursor on top, and flush the swept band immediately so a
  305. drag step is always one consistent frame (a budgeted flush would copy only
  306. the top rows of the band per iteration and the window would appear to
  307. stretch while the mouse keeps moving). */
  308. static void wm_drag_shadow(struct window *w, unsigned int ox, unsigned int oy,
  309. unsigned int nx, unsigned int ny)
  310. {
  311. int dx = (int)nx - (int)ox;
  312. int dy = (int)ny - (int)oy;
  313. unsigned int y0 = (oy < ny) ? oy : ny;
  314. unsigned int y1 = ((oy > ny) ? oy : ny) + w->ph;
  315. mouse_cursor_erase();
  316. wm_shadow_copy(ox, oy, nx, ny, w->pw, w->ph);
  317. if (dy > 0)
  318. wm_redraw_rect(ox, oy, w->pw, (unsigned int)dy);
  319. else if (dy < 0)
  320. wm_redraw_rect(ox, ny + w->ph, w->pw, (unsigned int)(-dy));
  321. if (dx > 0)
  322. wm_redraw_rect(ox, oy, (unsigned int)dx, w->ph);
  323. else if (dx < 0)
  324. wm_redraw_rect(nx + w->pw, oy, (unsigned int)(-dx), w->ph);
  325. /* Mark the swept band so the flush copies the moved window + sliver.
  326. Limit the horizontal span to the window's swept x-range so the flush
  327. copies only ~pw columns instead of the full framebuffer width. */
  328. {
  329. unsigned int x0 = (ox < nx) ? ox : nx;
  330. unsigned int x1 = ((ox > nx) ? ox : nx) + w->pw;
  331. vga_fb_mark_dirty_rect_xy(y0, y1 - y0, x0, x1);
  332. }
  333. mouse_redraw_cursor();
  334. vga_fb_flush();
  335. }
  336. /* Cirrus fast drag: also move the window in VRAM with the BitBLT engine
  337. (wm_drag_shadow flushes the mirrored copy in the shadow buffer). */
  338. static void wm_drag_fast(struct window *w, unsigned int ox, unsigned int oy,
  339. unsigned int nx, unsigned int ny)
  340. {
  341. cirrus_blit(ox, oy, nx, ny, w->pw, w->ph);
  342. wm_drag_shadow(w, ox, oy, nx, ny);
  343. }
  344. static void wm_handle_mouse(void)
  345. {
  346. unsigned int buttons = mouse_buttons_state();
  347. unsigned int mx = mouse_get_x();
  348. unsigned int my = mouse_get_y();
  349. int left_down = (buttons & 1) != 0;
  350. int left_was_down = (prev_buttons & 1) != 0;
  351. if (left_down && !left_was_down) {
  352. /* Press. */
  353. if (wm_point_in_rect(mx, my, WM_BTN_X, WM_BTN_Y, WM_BTN_W, WM_BTN_H)) {
  354. wm_spawn_terminal();
  355. } else {
  356. int idx = wm_window_at(mx, my);
  357. if (idx >= 0) {
  358. struct window *w = &windows[idx];
  359. wm_focus_index((unsigned int)idx);
  360. /* Close button? */
  361. if (wm_point_in_rect(mx, my,
  362. w->x + w->pw - 1 - WM_CLOSE_W, w->y + 1,
  363. WM_CLOSE_W, WM_TITLE_H)) {
  364. wm_close_window((unsigned int)idx);
  365. } else if (my < w->y + WM_TITLE_H) {
  366. /* Title bar: start drag. */
  367. drag_active = 1;
  368. drag_window = idx;
  369. drag_dx = (int)mx - (int)w->x;
  370. drag_dy = (int)my - (int)w->y;
  371. }
  372. }
  373. }
  374. } else if (left_down && left_was_down && drag_active) {
  375. struct window *w = &windows[drag_window];
  376. if (w->used) {
  377. unsigned int ox = w->x, oy = w->y;
  378. wm_clamp_window(w, (int)mx - drag_dx, (int)my - drag_dy);
  379. if (ox != w->x || oy != w->y) {
  380. if (cirrus_available())
  381. wm_drag_fast(w, ox, oy, w->x, w->y);
  382. else
  383. wm_drag_shadow(w, ox, oy, w->x, w->y);
  384. }
  385. } else {
  386. drag_active = 0;
  387. }
  388. } else if (!left_down && left_was_down) {
  389. drag_active = 0;
  390. }
  391. prev_buttons = buttons;
  392. }
  393. static void wm_handle_keyboard(void)
  394. {
  395. struct window *fw = wm_focused_window();
  396. static unsigned int last_hw_poll;
  397. unsigned int now = get_ticks_ms();
  398. int handled = 0;
  399. int n;
  400. if (io_keyboard_raw())
  401. return; /* an inline command is polling for keys itself */
  402. if (!fw || !fw->term)
  403. return;
  404. for (n = 0; n < 8; n++) {
  405. int k = io_poll_key();
  406. if (!k && (now - last_hw_poll) >= (kdbg_irq_masked ? 5 : 50)) {
  407. /* Fallback paths: IRQ1 was storm-masked, or no IRQ is
  408. * delivering; sample the controller directly (faster
  409. * when masked, so fast typing isn't dropped). */
  410. k = io_poll_key_with_hw();
  411. last_hw_poll = now;
  412. }
  413. if (!k)
  414. break;
  415. kdbg_wm_count++;
  416. kdbg_wm_last = (unsigned int)k;
  417. handled = 1;
  418. /* A graphics command owns its window's keys (ESC etc.);
  419. * Ctrl+C always reaches the terminal so its foreground kill
  420. * keeps working. */
  421. if (k == IO_KEY_CTRL_C ||
  422. !sys_gfx_push_key(&fw->term->screen, k))
  423. term_key_push(fw->term, k);
  424. }
  425. if (handled)
  426. kdbg_status();
  427. }
  428. static void wm_draw_bar(void)
  429. {
  430. unsigned int fbw = vga_fb_width();
  431. char dt[20];
  432. /* Banner (light green). */
  433. fbtext_draw_string_xy(4, 5, OS_NAME " v" OS_VERSION, 85, 255, 85, 0);
  434. /* "+ New" button: purple box, white text. */
  435. vga_fill_rect(WM_BTN_X, WM_BTN_Y, WM_BTN_W, WM_BTN_H, 150, 60, 210);
  436. fbtext_draw_string_xy(WM_BTN_X + 4, WM_BTN_Y + 3, "+ New", 255, 255, 255, 1);
  437. if (kexec_busy) {
  438. /* Kexec progress, in place of the clock. */
  439. static const char msg[] = "loading new kernel over serial";
  440. char buf[48];
  441. unsigned int n = 0, dots, i;
  442. while (msg[n])
  443. buf[n] = msg[n], n++;
  444. dots = (kexec_total > 0) ? (kexec_progress * 12 / kexec_total) : 0;
  445. for (i = 0; i < 12; i++)
  446. buf[n + i] = (i < dots) ? '.' : ' ';
  447. n += 12;
  448. buf[n] = '\0';
  449. fbtext_draw_string_xy(fbw - n * 8 - 4, 5, buf, 255, 200, 100, 0);
  450. } else {
  451. /* Live clock (light green, right-aligned). */
  452. rtc_read_datetime(dt);
  453. fbtext_draw_string_xy(fbw - 19 * 8 - 4, 5, dt, 85, 255, 85, 0);
  454. }
  455. /* Rainbow separator. */
  456. fbtext_draw_rainbow_separator();
  457. }
  458. /* Draw window w clipped to (cx, cy, cw, ch). Chrome uses wm_fill_rect_clip,
  459. * content the cells/canvas clip variants, so pixels outside the clip are
  460. * untouched. */
  461. static void wm_draw_window_clip(struct window *w, unsigned int cx, unsigned int cy,
  462. unsigned int cw, unsigned int ch)
  463. {
  464. struct text_screen *s = &w->term->screen;
  465. struct gfx_canvas *g = sys_gfx_canvas_for(&w->term->screen);
  466. unsigned int bx = w->x + w->pw - 1 - WM_CLOSE_W;
  467. unsigned int cony = w->y + 1 + WM_TITLE_H;
  468. /* Border. */
  469. wm_fill_rect_clip(w->x, w->y, w->pw, w->ph, cx, cy, cw, ch, 200, 200, 200);
  470. /* Title bar. */
  471. if (w->focused)
  472. wm_fill_rect_clip(w->x + 1, w->y + 1, w->pw - 2, WM_TITLE_H,
  473. cx, cy, cw, ch, 70, 90, 150);
  474. else
  475. wm_fill_rect_clip(w->x + 1, w->y + 1, w->pw - 2, WM_TITLE_H,
  476. cx, cy, cw, ch, 100, 100, 100);
  477. /* Title text. */
  478. fbtext_draw_string_xy_clip(w->x + 4, w->y + 3, w->title, 255, 255, 255,
  479. cx, cy, cw, ch);
  480. /* Close button. */
  481. wm_fill_rect_clip(bx, w->y + 1, WM_CLOSE_W, WM_TITLE_H,
  482. cx, cy, cw, ch, 190, 60, 60);
  483. fbtext_draw_string_xy_clip(bx + 3, w->y + 3, "x", 255, 255, 255,
  484. cx, cy, cw, ch);
  485. /* Content: text cells or the gfx canvas. */
  486. wm_fill_rect_clip(w->x + 1, cony, w->pw - 2, SCREEN_ROWS * 10,
  487. cx, cy, cw, ch, 0, 0, 0);
  488. if (g)
  489. gfx_blit_clip(g, w->x + 1, cony, cx, cy, cw, ch);
  490. else
  491. fbtext_draw_cells_clip(s->cells, s->cols, s->rows,
  492. w->x + 1, cony, cx, cy, cw, ch);
  493. /* Blinking block cursor (text windows only). */
  494. if (!g && w->focused && s->cursor_visible) {
  495. unsigned int col = s->cursor % s->cols;
  496. unsigned int row = s->cursor / s->cols;
  497. wm_fill_rect_clip(w->x + 1 + col * 8, cony + row * 10, 2, 10,
  498. cx, cy, cw, ch, 255, 255, 255);
  499. }
  500. }
  501. static void wm_draw_window(struct window *w)
  502. {
  503. wm_draw_window_clip(w, w->x, w->y, w->pw, w->ph);
  504. }
  505. static void wm_render_full(void)
  506. {
  507. unsigned int fbw = vga_fb_width();
  508. unsigned int fbh = vga_fb_height();
  509. unsigned int i;
  510. /* Erase the mouse cursor so the scene is drawn onto a clean background. */
  511. mouse_cursor_erase();
  512. /* Desktop background. */
  513. vga_fill_rect(0, 0, fbw, fbh, 32, 32, 48);
  514. /* Windows back-to-front (focused drawn last = on top). */
  515. for (i = 0; i < MAX_WINDOWS; i++)
  516. if (windows[i].used && !windows[i].focused)
  517. wm_draw_window(&windows[i]);
  518. for (i = 0; i < MAX_WINDOWS; i++)
  519. if (windows[i].used && windows[i].focused)
  520. wm_draw_window(&windows[i]);
  521. /* System bar always on top. */
  522. wm_draw_bar();
  523. /* Mouse cursor on top of everything. */
  524. mouse_redraw_cursor();
  525. }
  526. static void wm_render_bar(void)
  527. {
  528. unsigned int fbw = vga_fb_width();
  529. mouse_cursor_erase();
  530. vga_fill_rect(0, 0, fbw, WM_BAR_H, 32, 32, 48);
  531. wm_draw_bar();
  532. mouse_redraw_cursor();
  533. }
  534. static void wm_redraw_if_needed(void)
  535. {
  536. unsigned int i;
  537. static unsigned int last_sec = 0xFFFFFFFF;
  538. static unsigned int last_kexec_tick;
  539. unsigned int now_ms = get_ticks_ms();
  540. unsigned int now_sec = now_ms / 1000;
  541. int kexec_changed = kexec_busy && (now_ms - last_kexec_tick >= 100);
  542. int bar_changed;
  543. if (kexec_changed)
  544. last_kexec_tick = now_ms;
  545. bar_changed = (now_sec != last_sec) || kexec_changed;
  546. if (wm_full_redraw) {
  547. unsigned int seq_before[MAX_WINDOWS];
  548. for (i = 0; i < MAX_WINDOWS; i++) {
  549. struct gfx_canvas *g = windows[i].used ?
  550. sys_gfx_canvas_for(&windows[i].term->screen) : 0;
  551. seq_before[i] = g ? g->seq : 0;
  552. }
  553. wm_render_full();
  554. for (i = 0; i < MAX_WINDOWS; i++)
  555. if (windows[i].used) {
  556. struct gfx_canvas *g = sys_gfx_canvas_for(&windows[i].term->screen);
  557. windows[i].term->screen.dirty = 0;
  558. if (g)
  559. g->drawn = seq_before[i]; /* keep a frame
  560. presented during the render */
  561. }
  562. wm_full_redraw = 0;
  563. last_sec = now_sec;
  564. return;
  565. }
  566. if (bar_changed) {
  567. wm_render_bar();
  568. last_sec = now_sec;
  569. }
  570. /* Any window whose content changed (focused or not): repaint just its
  571. content rect through the damage-rect compositor. */
  572. for (i = 0; i < MAX_WINDOWS; i++) {
  573. struct gfx_canvas *g;
  574. unsigned int seq;
  575. if (!windows[i].used)
  576. continue;
  577. g = sys_gfx_canvas_for(&windows[i].term->screen);
  578. if (!windows[i].term->screen.dirty && !(g && g->seq != g->drawn))
  579. continue;
  580. seq = g ? g->seq : 0; /* capture before compositing */
  581. mouse_cursor_erase();
  582. wm_redraw_rect(windows[i].x + 1, windows[i].y + 1 + WM_TITLE_H,
  583. windows[i].pw - 2, SCREEN_ROWS * 10);
  584. mouse_redraw_cursor();
  585. windows[i].term->screen.dirty = 0;
  586. if (g)
  587. g->drawn = seq;
  588. }
  589. }
  590. static void wm_init(void)
  591. {
  592. unsigned int i;
  593. for (i = 0; i < MAX_WINDOWS; i++) {
  594. windows[i].used = 0;
  595. windows[i].term = &terms[i];
  596. }
  597. term_counter = 0;
  598. wm_full_redraw = 1;
  599. drag_active = 0;
  600. prev_buttons = 0;
  601. wm_spawn_terminal();
  602. }
  603. static void wm_blink_cursor(void)
  604. {
  605. static unsigned int last_cursor_tick;
  606. struct window *fw = wm_focused_window();
  607. unsigned int now = get_ticks_ms();
  608. if (!fw || !fw->term)
  609. return;
  610. if (sys_gfx_canvas_for(&fw->term->screen))
  611. return; /* no text cursor over a graphics command */
  612. if (now - last_cursor_tick >= CURSOR_MS) {
  613. last_cursor_tick = now;
  614. fw->term->screen.cursor_visible = !fw->term->screen.cursor_visible;
  615. fw->term->screen.dirty = 1;
  616. }
  617. }
  618. static void wm_handle_kexec(void)
  619. {
  620. if (kexec_busy)
  621. return;
  622. /* A command pack for /ramfs (checked before kexec: both start with
  623. * 'K', and the kexec detector drops anything that is not KXEC). */
  624. if (payload_pending()) {
  625. kexec_busy = 1;
  626. if (process_create("payload", program_payload_main) < 0)
  627. kexec_busy = 0;
  628. return;
  629. }
  630. if (kexec_pending()) {
  631. kexec_busy = 1;
  632. if (process_create("kexec", program_kexec_main) < 0)
  633. kexec_busy = 0;
  634. return;
  635. }
  636. /* Same over UDP: the host's `make net-dev` is picked up from here. */
  637. if (netkexec_pending()) {
  638. kexec_busy = 1;
  639. if (process_create("netkexec", program_netkexec_main) < 0)
  640. kexec_busy = 0;
  641. }
  642. }
  643. void wm_run(void)
  644. {
  645. wm_init();
  646. for (;;) {
  647. mouse_poll_update();
  648. wm_handle_mouse();
  649. wm_handle_keyboard();
  650. wm_blink_cursor();
  651. netlog_flush();
  652. wm_handle_kexec();
  653. sys_gfx_collect();
  654. wm_redraw_if_needed();
  655. vga_fb_flush_budget(VGA_FLUSH_BUDGET_BYTES);
  656. cpu_idle();
  657. }
  658. }