wm.c 18 KB

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