wm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 "fbtext.h"
  5. #include "idle.h"
  6. #include "io.h"
  7. #include "kexec.h"
  8. #include "mouse.h"
  9. #include "process.h"
  10. #include "rtc.h"
  11. #include "timer.h"
  12. #include "version.h"
  13. #include "vga.h"
  14. static struct window windows[MAX_WINDOWS];
  15. static struct term terms[MAX_WINDOWS];
  16. static unsigned int term_counter;
  17. static int wm_full_redraw;
  18. /* Mouse drag state. */
  19. static int drag_active;
  20. static int drag_window; /* index into windows[] */
  21. static int drag_dx, drag_dy; /* mouse offset from window origin */
  22. static unsigned int prev_buttons;
  23. /* Drag damage: accumulated y-band of the moving window, flushed as a region. */
  24. static int drag_render_needed;
  25. static int drag_band_valid;
  26. static unsigned int drag_band_y0, drag_band_y1;
  27. static unsigned int last_drag_render;
  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. static int wm_point_in_rect(unsigned int px, unsigned int py,
  45. unsigned int rx, unsigned int ry, unsigned int rw, unsigned int rh)
  46. {
  47. return px >= rx && px < rx + rw && py >= ry && py < ry + rh;
  48. }
  49. static void wm_make_title(struct window *w, unsigned int n)
  50. {
  51. char num[4];
  52. unsigned int i = 0, k = 0;
  53. w->title[k++] = 't'; w->title[k++] = 'e'; w->title[k++] = 'r';
  54. w->title[k++] = 'm'; w->title[k++] = ' ';
  55. do {
  56. num[i++] = (char)('0' + (n % 10));
  57. n /= 10;
  58. } while (n && i < 3);
  59. while (i)
  60. w->title[k++] = num[--i];
  61. w->title[k] = '\0';
  62. }
  63. int wm_spawn_terminal(void)
  64. {
  65. unsigned int i, j, fbw = vga_fb_width();
  66. struct window *w;
  67. struct term *t;
  68. int pid;
  69. unsigned int n;
  70. for (i = 0; i < MAX_WINDOWS; i++)
  71. if (!windows[i].used)
  72. break;
  73. if (i == MAX_WINDOWS)
  74. return -1;
  75. w = &windows[i];
  76. t = &terms[i];
  77. screen_init(&t->screen);
  78. t->screen.cursor_visible = 1;
  79. t->key_head = t->key_tail = 0;
  80. t->history_count = 0;
  81. t->history_next = 0;
  82. t->cwd[0] = '/';
  83. t->cwd[1] = '\0';
  84. w->used = 1;
  85. w->term = t;
  86. w->pid = 0;
  87. w->focused = 0;
  88. w->pw = SCREEN_COLS * 8 + 2;
  89. w->ph = WM_TITLE_H + SCREEN_ROWS * 10 + 2;
  90. /* Cascade position, clamped to the desktop area. */
  91. n = term_counter % 5;
  92. w->x = 24 + n * 32;
  93. w->y = WM_BAR_H + 12 + n * 28;
  94. if (w->x + w->pw > fbw)
  95. w->x = (fbw > w->pw) ? fbw - w->pw : 0;
  96. if (w->y + w->ph > vga_fb_height())
  97. w->y = WM_BAR_H + 4;
  98. term_counter++;
  99. wm_make_title(w, term_counter);
  100. pid = process_create_ptr("term", term_entry, t);
  101. if (pid < 0) {
  102. w->used = 0;
  103. return -1;
  104. }
  105. w->pid = (unsigned int)pid;
  106. for (j = 0; j < MAX_WINDOWS; j++)
  107. if (windows[j].used)
  108. windows[j].focused = (j == i);
  109. wm_request_redraw();
  110. return pid;
  111. }
  112. static void wm_focus_index(unsigned int idx)
  113. {
  114. unsigned int i;
  115. if (windows[idx].used && windows[idx].focused)
  116. return;
  117. for (i = 0; i < MAX_WINDOWS; i++)
  118. if (windows[i].used)
  119. windows[i].focused = (i == idx);
  120. wm_request_redraw();
  121. }
  122. static void wm_focus_top(void)
  123. {
  124. unsigned int i;
  125. for (i = 0; i < MAX_WINDOWS; i++)
  126. if (windows[i].used) {
  127. wm_focus_index(i);
  128. return;
  129. }
  130. }
  131. static void wm_close_window(unsigned int idx)
  132. {
  133. struct window *w = &windows[idx];
  134. if (w->pid)
  135. process_kill(w->pid);
  136. w->used = 0;
  137. w->pid = 0;
  138. w->focused = 0;
  139. wm_focus_top();
  140. wm_request_redraw();
  141. }
  142. /* Topmost window containing (px,py), or -1. */
  143. static int wm_window_at(unsigned int px, unsigned int py)
  144. {
  145. unsigned int i;
  146. for (i = 0; i < MAX_WINDOWS; i++)
  147. if (windows[i].used && windows[i].focused &&
  148. wm_point_in_rect(px, py, windows[i].x, windows[i].y,
  149. windows[i].pw, windows[i].ph))
  150. return (int)i;
  151. for (i = 0; i < MAX_WINDOWS; i++)
  152. if (windows[i].used && !windows[i].focused &&
  153. wm_point_in_rect(px, py, windows[i].x, windows[i].y,
  154. windows[i].pw, windows[i].ph))
  155. return (int)i;
  156. return -1;
  157. }
  158. static void wm_clamp_window(struct window *w, int nx, int ny)
  159. {
  160. unsigned int fbw = vga_fb_width();
  161. unsigned int fbh = vga_fb_height();
  162. int maxx = (int)fbw - (int)w->pw;
  163. int maxy = (int)fbh - (int)w->ph;
  164. if (maxx < 0)
  165. maxx = 0;
  166. if (maxy < (int)WM_BAR_H)
  167. maxy = (int)WM_BAR_H;
  168. if (nx < 0)
  169. nx = 0;
  170. else if (nx > maxx)
  171. nx = maxx;
  172. if (ny < (int)WM_BAR_H)
  173. ny = (int)WM_BAR_H;
  174. else if (ny > maxy)
  175. ny = maxy;
  176. w->x = (unsigned int)nx;
  177. w->y = (unsigned int)ny;
  178. }
  179. /* Grow the drag's accumulated dirty band to cover the window's old+new rects.
  180. Only the y-range matters: the flush copies whole rows. */
  181. static void wm_drag_accumulate(unsigned int oy, unsigned int ny, unsigned int ph)
  182. {
  183. unsigned int y0 = (oy < ny) ? oy : ny;
  184. unsigned int y1 = ((oy > ny) ? oy : ny) + ph;
  185. if (!drag_band_valid) {
  186. drag_band_y0 = y0;
  187. drag_band_y1 = y1;
  188. drag_band_valid = 1;
  189. } else {
  190. if (y0 < drag_band_y0)
  191. drag_band_y0 = y0;
  192. if (y1 > drag_band_y1)
  193. drag_band_y1 = y1;
  194. }
  195. }
  196. static void wm_handle_mouse(void)
  197. {
  198. unsigned int buttons = mouse_buttons_state();
  199. unsigned int mx = mouse_get_x();
  200. unsigned int my = mouse_get_y();
  201. int left_down = (buttons & 1) != 0;
  202. int left_was_down = (prev_buttons & 1) != 0;
  203. if (left_down && !left_was_down) {
  204. /* Press. */
  205. if (wm_point_in_rect(mx, my, WM_BTN_X, WM_BTN_Y, WM_BTN_W, WM_BTN_H)) {
  206. wm_spawn_terminal();
  207. } else {
  208. int idx = wm_window_at(mx, my);
  209. if (idx >= 0) {
  210. struct window *w = &windows[idx];
  211. wm_focus_index((unsigned int)idx);
  212. /* Close button? */
  213. if (wm_point_in_rect(mx, my,
  214. w->x + w->pw - 1 - WM_CLOSE_W, w->y + 1,
  215. WM_CLOSE_W, WM_TITLE_H)) {
  216. wm_close_window((unsigned int)idx);
  217. } else if (my < w->y + WM_TITLE_H) {
  218. /* Title bar: start drag. */
  219. drag_active = 1;
  220. drag_window = idx;
  221. drag_dx = (int)mx - (int)w->x;
  222. drag_dy = (int)my - (int)w->y;
  223. drag_band_valid = 0;
  224. drag_render_needed = 0;
  225. }
  226. }
  227. }
  228. } else if (left_down && left_was_down && drag_active) {
  229. struct window *w = &windows[drag_window];
  230. if (w->used) {
  231. unsigned int ox = w->x, oy = w->y;
  232. wm_clamp_window(w, (int)mx - drag_dx, (int)my - drag_dy);
  233. if (ox != w->x || oy != w->y) {
  234. wm_drag_accumulate(oy, w->y, w->ph);
  235. drag_render_needed = 1;
  236. }
  237. } else {
  238. drag_active = 0;
  239. }
  240. } else if (!left_down && left_was_down) {
  241. drag_active = 0;
  242. }
  243. prev_buttons = buttons;
  244. }
  245. static void wm_handle_keyboard(void)
  246. {
  247. struct window *fw = wm_focused_window();
  248. int n;
  249. if (!fw || !fw->term)
  250. return;
  251. for (n = 0; n < 8; n++) {
  252. int k = io_poll_key();
  253. if (!k)
  254. break;
  255. term_key_push(fw->term, k);
  256. }
  257. }
  258. static void wm_draw_bar(void)
  259. {
  260. unsigned int fbw = vga_fb_width();
  261. char dt[20];
  262. /* Banner (light green). */
  263. fbtext_draw_string_xy(4, 5, OS_NAME " v" OS_VERSION, 85, 255, 85, 0);
  264. /* "+ New" button: purple box, white text. */
  265. vga_fill_rect(WM_BTN_X, WM_BTN_Y, WM_BTN_W, WM_BTN_H, 150, 60, 210);
  266. fbtext_draw_string_xy(WM_BTN_X + 4, WM_BTN_Y + 3, "+ New", 255, 255, 255, 1);
  267. if (kexec_busy) {
  268. /* Kexec progress, in place of the clock. */
  269. static const char msg[] = "loading new kernel over serial";
  270. char buf[48];
  271. unsigned int n = 0, dots, i;
  272. while (msg[n])
  273. buf[n] = msg[n], n++;
  274. dots = (kexec_total > 0) ? (kexec_progress * 12 / kexec_total) : 0;
  275. for (i = 0; i < 12; i++)
  276. buf[n + i] = (i < dots) ? '.' : ' ';
  277. n += 12;
  278. buf[n] = '\0';
  279. fbtext_draw_string_xy(fbw - n * 8 - 4, 5, buf, 255, 200, 100, 0);
  280. } else {
  281. /* Live clock (light green, right-aligned). */
  282. rtc_read_datetime(dt);
  283. fbtext_draw_string_xy(fbw - 19 * 8 - 4, 5, dt, 85, 255, 85, 0);
  284. }
  285. /* Rainbow separator. */
  286. fbtext_draw_rainbow_separator();
  287. }
  288. static void wm_draw_window(struct window *w)
  289. {
  290. struct text_screen *s = &w->term->screen;
  291. unsigned int cx = w->x + 1;
  292. unsigned int cy = w->y + 1 + WM_TITLE_H;
  293. /* Border. */
  294. vga_fill_rect(w->x, w->y, w->pw, w->ph, 200, 200, 200);
  295. /* Title bar. */
  296. if (w->focused)
  297. vga_fill_rect(cx, w->y + 1, w->pw - 2, WM_TITLE_H, 70, 90, 150);
  298. else
  299. vga_fill_rect(cx, w->y + 1, w->pw - 2, WM_TITLE_H, 100, 100, 100);
  300. /* Title text. */
  301. fbtext_draw_string_xy(w->x + 4, w->y + 3, w->title, 255, 255, 255, 1);
  302. /* Close button. */
  303. {
  304. unsigned int bx = w->x + w->pw - 1 - WM_CLOSE_W;
  305. vga_fill_rect(bx, w->y + 1, WM_CLOSE_W, WM_TITLE_H, 190, 60, 60);
  306. fbtext_draw_string_xy(bx + 3, w->y + 3, "x", 255, 255, 255, 1);
  307. }
  308. /* Content (black background) + cell buffer. */
  309. vga_fill_rect(cx, cy, w->pw - 2, SCREEN_ROWS * 10, 0, 0, 0);
  310. fbtext_draw_cells(s->cells, s->cols, s->rows, cx, cy);
  311. /* Blinking block cursor for the focused window. */
  312. if (w->focused && s->cursor_visible) {
  313. unsigned int col = s->cursor % s->cols;
  314. unsigned int row = s->cursor / s->cols;
  315. vga_fill_rect(cx + col * 8, cy + row * 10, 2, 10, 255, 255, 255);
  316. }
  317. }
  318. static void wm_render_full(void)
  319. {
  320. unsigned int fbw = vga_fb_width();
  321. unsigned int fbh = vga_fb_height();
  322. unsigned int i;
  323. /* Erase the mouse cursor so the scene is drawn onto a clean background. */
  324. mouse_cursor_erase();
  325. /* Desktop background. */
  326. vga_fill_rect(0, 0, fbw, fbh, 32, 32, 48);
  327. /* Windows back-to-front (focused drawn last = on top). */
  328. for (i = 0; i < MAX_WINDOWS; i++)
  329. if (windows[i].used && !windows[i].focused)
  330. wm_draw_window(&windows[i]);
  331. for (i = 0; i < MAX_WINDOWS; i++)
  332. if (windows[i].used && windows[i].focused)
  333. wm_draw_window(&windows[i]);
  334. /* System bar always on top. */
  335. wm_draw_bar();
  336. /* Mouse cursor on top of everything. */
  337. mouse_redraw_cursor();
  338. }
  339. static void wm_render_bar(void)
  340. {
  341. unsigned int fbw = vga_fb_width();
  342. mouse_cursor_erase();
  343. vga_fill_rect(0, 0, fbw, WM_BAR_H, 32, 32, 48);
  344. wm_draw_bar();
  345. mouse_redraw_cursor();
  346. }
  347. static void wm_redraw_if_needed(void)
  348. {
  349. unsigned int i;
  350. static unsigned int last_sec = 0xFFFFFFFF;
  351. static unsigned int last_kexec_tick;
  352. unsigned int now_ms = get_ticks_ms();
  353. unsigned int now_sec = now_ms / 1000;
  354. int clock_changed = (now_sec != last_sec);
  355. int kexec_changed = kexec_busy && (now_ms - last_kexec_tick >= 100);
  356. int need_full = wm_full_redraw;
  357. if (kexec_changed)
  358. last_kexec_tick = now_ms;
  359. /* A background window's content changed -> full redraw (rare). */
  360. for (i = 0; i < MAX_WINDOWS; i++)
  361. if (windows[i].used && !windows[i].focused && windows[i].term->screen.dirty)
  362. need_full = 1;
  363. if (need_full) {
  364. wm_render_full();
  365. for (i = 0; i < MAX_WINDOWS; i++)
  366. if (windows[i].used)
  367. windows[i].term->screen.dirty = 0;
  368. wm_full_redraw = 0;
  369. drag_render_needed = 0;
  370. drag_band_valid = 0;
  371. last_sec = now_sec;
  372. return;
  373. }
  374. /* Drag: redraw the full scene into the shadow buffer, but flush only the
  375. accumulated y-band (the window's swept region) to VRAM. Throttled to the
  376. flush rate so rapid mouse moves coalesce. */
  377. if (drag_render_needed && (now_ms - last_drag_render >= 33)) {
  378. last_drag_render = now_ms;
  379. wm_render_full();
  380. vga_fb_clear_dirty();
  381. if (drag_band_valid)
  382. vga_fb_mark_dirty_rect(drag_band_y0, drag_band_y1 - drag_band_y0);
  383. if (clock_changed)
  384. vga_fb_mark_dirty_rect(0, WM_BAR_H);
  385. for (i = 0; i < MAX_WINDOWS; i++)
  386. if (windows[i].used)
  387. windows[i].term->screen.dirty = 0;
  388. drag_render_needed = 0;
  389. last_sec = now_sec;
  390. return;
  391. }
  392. if (clock_changed || kexec_changed) {
  393. wm_render_bar();
  394. last_sec = now_sec;
  395. }
  396. /* Focused window's content changed -> redraw only that window. */
  397. for (i = 0; i < MAX_WINDOWS; i++) {
  398. if (windows[i].used && windows[i].focused && windows[i].term->screen.dirty) {
  399. mouse_cursor_erase();
  400. wm_draw_window(&windows[i]);
  401. mouse_redraw_cursor();
  402. windows[i].term->screen.dirty = 0;
  403. }
  404. }
  405. }
  406. static void wm_init(void)
  407. {
  408. unsigned int i;
  409. for (i = 0; i < MAX_WINDOWS; i++) {
  410. windows[i].used = 0;
  411. windows[i].term = &terms[i];
  412. }
  413. term_counter = 0;
  414. wm_full_redraw = 1;
  415. drag_active = 0;
  416. prev_buttons = 0;
  417. wm_spawn_terminal();
  418. }
  419. static void wm_blink_cursor(void)
  420. {
  421. static unsigned int last_cursor_tick;
  422. struct window *fw = wm_focused_window();
  423. unsigned int now = get_ticks_ms();
  424. if (!fw || !fw->term)
  425. return;
  426. if (now - last_cursor_tick >= CURSOR_MS) {
  427. last_cursor_tick = now;
  428. fw->term->screen.cursor_visible = !fw->term->screen.cursor_visible;
  429. fw->term->screen.dirty = 1;
  430. }
  431. }
  432. static void wm_handle_kexec(void)
  433. {
  434. if (!kexec_busy && kexec_pending()) {
  435. kexec_busy = 1;
  436. if (process_create("kexec", program_kexec_main) < 0)
  437. kexec_busy = 0;
  438. }
  439. }
  440. void wm_run(void)
  441. {
  442. wm_init();
  443. for (;;) {
  444. mouse_poll_update();
  445. wm_handle_mouse();
  446. wm_handle_keyboard();
  447. wm_blink_cursor();
  448. wm_handle_kexec();
  449. wm_redraw_if_needed();
  450. vga_fb_flush_pending_check();
  451. cpu_idle();
  452. }
  453. }