wm.c 11 KB

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