| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630 |
- /* Window manager: fixed-size draggable terminal windows + a system bar.
- Slot 0 runs wm_run(); each terminal window is a separate process. */
- #include "wm.h"
- #include "cirrus.h"
- #include "fbtext.h"
- #include "idle.h"
- #include "io.h"
- #include "kdbg.h"
- #include "kexec.h"
- #include "mouse.h"
- #include "netkexec.h"
- #include "netlog.h"
- #include "process.h"
- #include "rtc.h"
- #include "timer.h"
- #include "version.h"
- #include "vga.h"
- static struct window windows[MAX_WINDOWS];
- static struct term terms[MAX_WINDOWS];
- static unsigned int term_counter;
- static int wm_full_redraw;
- /* Mouse drag state. */
- static int drag_active;
- static int drag_window; /* index into windows[] */
- static int drag_dx, drag_dy; /* mouse offset from window origin */
- static unsigned int prev_buttons;
- void wm_request_redraw(void)
- {
- wm_full_redraw = 1;
- }
- struct window *wm_focused_window(void)
- {
- unsigned int i;
- for (i = 0; i < MAX_WINDOWS; i++)
- if (windows[i].used && windows[i].focused)
- return &windows[i];
- return (void *)0;
- }
- int wm_is_focused(const struct window *w)
- {
- return w->focused;
- }
- static int wm_point_in_rect(unsigned int px, unsigned int py,
- unsigned int rx, unsigned int ry, unsigned int rw, unsigned int rh)
- {
- return px >= rx && px < rx + rw && py >= ry && py < ry + rh;
- }
- static void wm_make_title(struct window *w, unsigned int n)
- {
- char num[4];
- unsigned int i = 0, k = 0;
- w->title[k++] = 't'; w->title[k++] = 'e'; w->title[k++] = 'r';
- w->title[k++] = 'm'; w->title[k++] = ' ';
- do {
- num[i++] = (char)('0' + (n % 10));
- n /= 10;
- } while (n && i < 3);
- while (i)
- w->title[k++] = num[--i];
- w->title[k] = '\0';
- }
- int wm_spawn_terminal(void)
- {
- unsigned int i, j, fbw = vga_fb_width();
- struct window *w;
- struct term *t;
- int pid;
- unsigned int n;
- for (i = 0; i < MAX_WINDOWS; i++)
- if (!windows[i].used)
- break;
- if (i == MAX_WINDOWS)
- return -1;
- w = &windows[i];
- t = &terms[i];
- screen_init(&t->screen);
- t->screen.cursor_visible = 1;
- t->key_head = t->key_tail = 0;
- t->history_count = 0;
- t->history_next = 0;
- t->cwd[0] = '/';
- t->cwd[1] = '\0';
- w->used = 1;
- w->term = t;
- w->pid = 0;
- w->focused = 0;
- w->pw = SCREEN_COLS * 8 + 2;
- w->ph = WM_TITLE_H + SCREEN_ROWS * 10 + 2;
- /* Cascade position, clamped to the desktop area. */
- n = term_counter % 5;
- w->x = 24 + n * 32;
- w->y = WM_BAR_H + 12 + n * 28;
- if (w->x + w->pw > fbw)
- w->x = (fbw > w->pw) ? fbw - w->pw : 0;
- if (w->y + w->ph > vga_fb_height())
- w->y = WM_BAR_H + 4;
- term_counter++;
- wm_make_title(w, term_counter);
- pid = process_create_ptr("term", term_entry, t);
- if (pid < 0) {
- w->used = 0;
- return -1;
- }
- w->pid = (unsigned int)pid;
- for (j = 0; j < MAX_WINDOWS; j++)
- if (windows[j].used)
- windows[j].focused = (j == i);
- wm_request_redraw();
- return pid;
- }
- static void wm_focus_index(unsigned int idx)
- {
- unsigned int i;
- if (windows[idx].used && windows[idx].focused)
- return;
- for (i = 0; i < MAX_WINDOWS; i++)
- if (windows[i].used)
- windows[i].focused = (i == idx);
- wm_request_redraw();
- }
- static void wm_focus_top(void)
- {
- unsigned int i;
- for (i = 0; i < MAX_WINDOWS; i++)
- if (windows[i].used) {
- wm_focus_index(i);
- return;
- }
- }
- static void wm_close_window(unsigned int idx)
- {
- struct window *w = &windows[idx];
- if (w->pid)
- process_kill(w->pid);
- w->used = 0;
- w->pid = 0;
- w->focused = 0;
- wm_focus_top();
- wm_request_redraw();
- }
- /* Topmost window containing (px,py), or -1. */
- static int wm_window_at(unsigned int px, unsigned int py)
- {
- unsigned int i;
- for (i = 0; i < MAX_WINDOWS; i++)
- if (windows[i].used && windows[i].focused &&
- wm_point_in_rect(px, py, windows[i].x, windows[i].y,
- windows[i].pw, windows[i].ph))
- return (int)i;
- for (i = 0; i < MAX_WINDOWS; i++)
- if (windows[i].used && !windows[i].focused &&
- wm_point_in_rect(px, py, windows[i].x, windows[i].y,
- windows[i].pw, windows[i].ph))
- return (int)i;
- return -1;
- }
- static void wm_clamp_window(struct window *w, int nx, int ny)
- {
- unsigned int fbw = vga_fb_width();
- unsigned int fbh = vga_fb_height();
- int maxx = (int)fbw - (int)w->pw;
- int maxy = (int)fbh - (int)w->ph;
- if (maxx < 0)
- maxx = 0;
- if (maxy < (int)WM_BAR_H)
- maxy = (int)WM_BAR_H;
- if (nx < 0)
- nx = 0;
- else if (nx > maxx)
- nx = maxx;
- if (ny < (int)WM_BAR_H)
- ny = (int)WM_BAR_H;
- else if (ny > maxy)
- ny = maxy;
- w->x = (unsigned int)nx;
- w->y = (unsigned int)ny;
- }
- /* Byte copy that handles overlapping source/destination. */
- static void wm_memmove(unsigned char *dst, const unsigned char *src, unsigned int n)
- {
- if (dst == src || n == 0)
- return;
- if (dst < src) {
- unsigned int i;
- for (i = 0; i < n; i++)
- dst[i] = src[i];
- } else {
- while (n--)
- dst[n] = src[n];
- }
- }
- /* Copy a rectangle within the shadow buffer (to mirror a BitBLT'd drag). */
- static void wm_shadow_copy(unsigned int ox, unsigned int oy,
- unsigned int nx, unsigned int ny,
- unsigned int w, unsigned int h)
- {
- unsigned char *fb = (unsigned char *)vga_fb_ptr();
- unsigned int pitch = vga_fb_pitch();
- unsigned int bpp = vga_fb_bpp();
- unsigned int pb = (bpp == 16) ? 2 : (bpp == 24) ? 3 : 4;
- unsigned int row_bytes = w * pb;
- unsigned int r;
- if (ny > oy || (ny == oy && nx > ox)) {
- for (r = h; r-- > 0; )
- wm_memmove(fb + (ny + r) * pitch + nx * pb,
- fb + (oy + r) * pitch + ox * pb, row_bytes);
- } else {
- for (r = 0; r < h; r++)
- wm_memmove(fb + (ny + r) * pitch + nx * pb,
- fb + (oy + r) * pitch + ox * pb, row_bytes);
- }
- }
- /* Fill the intersection of rect (rx,ry,rw,rh) with clip rect (cx,cy,cw,ch). */
- static void wm_fill_rect_clip(unsigned int rx, unsigned int ry, unsigned int rw, unsigned int rh,
- unsigned int cx, unsigned int cy, unsigned int cw, unsigned int ch,
- unsigned int r, unsigned int g, unsigned int b)
- {
- unsigned int x0 = (rx > cx) ? rx : cx;
- unsigned int y0 = (ry > cy) ? ry : cy;
- unsigned int x1 = (rx + rw < cx + cw) ? rx + rw : cx + cw;
- unsigned int y1 = (ry + rh < cy + ch) ? ry + rh : cy + ch;
- if (x1 > x0 && y1 > y0)
- vga_fill_rect(x0, y0, x1 - x0, y1 - y0, r, g, b);
- }
- /* Redraw a rectangular region of the desktop (background + underlying windows,
- excluding the window being dragged). Restores the exposed sliver. */
- static void wm_redraw_sliver(unsigned int x, unsigned int y, unsigned int w, unsigned int h)
- {
- unsigned int i;
- vga_fill_rect(x, y, w, h, 32, 32, 48);
- for (i = 0; i < MAX_WINDOWS; i++) {
- struct window *w2 = &windows[i];
- if (!w2->used || (int)i == drag_window)
- continue;
- if (!(x < w2->x + w2->pw && x + w > w2->x &&
- y < w2->y + w2->ph && y + h > w2->y))
- continue;
- /* Border. */
- wm_fill_rect_clip(w2->x, w2->y, w2->pw, w2->ph,
- x, y, w, h, 200, 200, 200);
- /* Title bar. */
- if (w2->focused)
- wm_fill_rect_clip(w2->x + 1, w2->y + 1, w2->pw - 2, WM_TITLE_H,
- x, y, w, h, 70, 90, 150);
- else
- wm_fill_rect_clip(w2->x + 1, w2->y + 1, w2->pw - 2, WM_TITLE_H,
- x, y, w, h, 100, 100, 100);
- /* Content background + cells. */
- wm_fill_rect_clip(w2->x + 1, w2->y + 1 + WM_TITLE_H,
- w2->pw - 2, SCREEN_ROWS * 10, x, y, w, h, 0, 0, 0);
- fbtext_draw_cells_clip(w2->term->screen.cells,
- w2->term->screen.cols, w2->term->screen.rows,
- w2->x + 1, w2->y + 1 + WM_TITLE_H, x, y, w, h);
- }
- }
- /* Move a window in the shadow buffer: copy its rect, redraw the exposed
- sliver, keep the cursor on top, and mark the swept rows dirty. Does not
- touch VRAM; the 30 Hz flush copies the affected rows. */
- static void wm_drag_shadow(struct window *w, unsigned int ox, unsigned int oy,
- unsigned int nx, unsigned int ny)
- {
- int dx = (int)nx - (int)ox;
- int dy = (int)ny - (int)oy;
- unsigned int y0 = (oy < ny) ? oy : ny;
- unsigned int y1 = ((oy > ny) ? oy : ny) + w->ph;
- mouse_cursor_erase();
- wm_shadow_copy(ox, oy, nx, ny, w->pw, w->ph);
- if (dy > 0)
- wm_redraw_sliver(ox, oy, w->pw, (unsigned int)dy);
- else if (dy < 0)
- wm_redraw_sliver(ox, ny + w->ph, w->pw, (unsigned int)(-dy));
- if (dx > 0)
- wm_redraw_sliver(ox, oy, (unsigned int)dx, w->ph);
- else if (dx < 0)
- wm_redraw_sliver(nx + w->pw, oy, (unsigned int)(-dx), w->ph);
- /* Mark the swept band so the flush copies the moved window + sliver.
- Limit the horizontal span to the window's swept x-range so the flush
- copies only ~pw columns instead of the full framebuffer width. */
- {
- unsigned int x0 = (ox < nx) ? ox : nx;
- unsigned int x1 = ((ox > nx) ? ox : nx) + w->pw;
- vga_fb_mark_dirty_rect_xy(y0, y1 - y0, x0, x1);
- }
- mouse_redraw_cursor();
- }
- /* Cirrus fast drag: also move the window in VRAM with the BitBLT engine, then
- mirror in the shadow buffer and flush immediately. */
- static void wm_drag_fast(struct window *w, unsigned int ox, unsigned int oy,
- unsigned int nx, unsigned int ny)
- {
- cirrus_blit(ox, oy, nx, ny, w->pw, w->ph);
- wm_drag_shadow(w, ox, oy, nx, ny);
- vga_fb_flush();
- }
- static void wm_handle_mouse(void)
- {
- unsigned int buttons = mouse_buttons_state();
- unsigned int mx = mouse_get_x();
- unsigned int my = mouse_get_y();
- int left_down = (buttons & 1) != 0;
- int left_was_down = (prev_buttons & 1) != 0;
- if (left_down && !left_was_down) {
- /* Press. */
- if (wm_point_in_rect(mx, my, WM_BTN_X, WM_BTN_Y, WM_BTN_W, WM_BTN_H)) {
- wm_spawn_terminal();
- } else {
- int idx = wm_window_at(mx, my);
- if (idx >= 0) {
- struct window *w = &windows[idx];
- wm_focus_index((unsigned int)idx);
- /* Close button? */
- if (wm_point_in_rect(mx, my,
- w->x + w->pw - 1 - WM_CLOSE_W, w->y + 1,
- WM_CLOSE_W, WM_TITLE_H)) {
- wm_close_window((unsigned int)idx);
- } else if (my < w->y + WM_TITLE_H) {
- /* Title bar: start drag. */
- drag_active = 1;
- drag_window = idx;
- drag_dx = (int)mx - (int)w->x;
- drag_dy = (int)my - (int)w->y;
- }
- }
- }
- } else if (left_down && left_was_down && drag_active) {
- struct window *w = &windows[drag_window];
- if (w->used) {
- unsigned int ox = w->x, oy = w->y;
- wm_clamp_window(w, (int)mx - drag_dx, (int)my - drag_dy);
- if (ox != w->x || oy != w->y) {
- if (cirrus_available())
- wm_drag_fast(w, ox, oy, w->x, w->y);
- else
- wm_drag_shadow(w, ox, oy, w->x, w->y);
- }
- } else {
- drag_active = 0;
- }
- } else if (!left_down && left_was_down) {
- drag_active = 0;
- }
- prev_buttons = buttons;
- }
- static void wm_handle_keyboard(void)
- {
- struct window *fw = wm_focused_window();
- static unsigned int last_hw_poll;
- unsigned int now = get_ticks_ms();
- int handled = 0;
- int n;
- if (io_keyboard_raw())
- return; /* an inline command is polling for keys itself */
- if (!fw || !fw->term)
- return;
- for (n = 0; n < 8; n++) {
- int k = io_poll_key();
- if (!k && (now - last_hw_poll) >= (kdbg_irq_masked ? 5 : 50)) {
- /* Fallback paths: IRQ1 was storm-masked, or no IRQ is
- * delivering; sample the controller directly (faster
- * when masked, so fast typing isn't dropped). */
- k = io_poll_key_with_hw();
- last_hw_poll = now;
- }
- if (!k)
- break;
- kdbg_wm_count++;
- kdbg_wm_last = (unsigned int)k;
- handled = 1;
- term_key_push(fw->term, k);
- }
- if (handled)
- kdbg_status();
- }
- static void wm_draw_bar(void)
- {
- unsigned int fbw = vga_fb_width();
- char dt[20];
- /* Banner (light green). */
- fbtext_draw_string_xy(4, 5, OS_NAME " v" OS_VERSION, 85, 255, 85, 0);
- /* "+ New" button: purple box, white text. */
- vga_fill_rect(WM_BTN_X, WM_BTN_Y, WM_BTN_W, WM_BTN_H, 150, 60, 210);
- fbtext_draw_string_xy(WM_BTN_X + 4, WM_BTN_Y + 3, "+ New", 255, 255, 255, 1);
- if (kexec_busy) {
- /* Kexec progress, in place of the clock. */
- static const char msg[] = "loading new kernel over serial";
- char buf[48];
- unsigned int n = 0, dots, i;
- while (msg[n])
- buf[n] = msg[n], n++;
- dots = (kexec_total > 0) ? (kexec_progress * 12 / kexec_total) : 0;
- for (i = 0; i < 12; i++)
- buf[n + i] = (i < dots) ? '.' : ' ';
- n += 12;
- buf[n] = '\0';
- fbtext_draw_string_xy(fbw - n * 8 - 4, 5, buf, 255, 200, 100, 0);
- } else {
- /* Live clock (light green, right-aligned). */
- rtc_read_datetime(dt);
- fbtext_draw_string_xy(fbw - 19 * 8 - 4, 5, dt, 85, 255, 85, 0);
- }
- /* Rainbow separator. */
- fbtext_draw_rainbow_separator();
- }
- static void wm_draw_window(struct window *w)
- {
- struct text_screen *s = &w->term->screen;
- unsigned int cx = w->x + 1;
- unsigned int cy = w->y + 1 + WM_TITLE_H;
- /* Border. */
- vga_fill_rect(w->x, w->y, w->pw, w->ph, 200, 200, 200);
- /* Title bar. */
- if (w->focused)
- vga_fill_rect(cx, w->y + 1, w->pw - 2, WM_TITLE_H, 70, 90, 150);
- else
- vga_fill_rect(cx, w->y + 1, w->pw - 2, WM_TITLE_H, 100, 100, 100);
- /* Title text. */
- fbtext_draw_string_xy(w->x + 4, w->y + 3, w->title, 255, 255, 255, 1);
- /* Close button. */
- {
- unsigned int bx = w->x + w->pw - 1 - WM_CLOSE_W;
- vga_fill_rect(bx, w->y + 1, WM_CLOSE_W, WM_TITLE_H, 190, 60, 60);
- fbtext_draw_string_xy(bx + 3, w->y + 3, "x", 255, 255, 255, 1);
- }
- /* Content (black background) + cell buffer. */
- vga_fill_rect(cx, cy, w->pw - 2, SCREEN_ROWS * 10, 0, 0, 0);
- fbtext_draw_cells(s->cells, s->cols, s->rows, cx, cy);
- /* Blinking block cursor for the focused window. */
- if (w->focused && s->cursor_visible) {
- unsigned int col = s->cursor % s->cols;
- unsigned int row = s->cursor / s->cols;
- vga_fill_rect(cx + col * 8, cy + row * 10, 2, 10, 255, 255, 255);
- }
- }
- static void wm_render_full(void)
- {
- unsigned int fbw = vga_fb_width();
- unsigned int fbh = vga_fb_height();
- unsigned int i;
- /* Erase the mouse cursor so the scene is drawn onto a clean background. */
- mouse_cursor_erase();
- /* Desktop background. */
- vga_fill_rect(0, 0, fbw, fbh, 32, 32, 48);
- /* Windows back-to-front (focused drawn last = on top). */
- for (i = 0; i < MAX_WINDOWS; i++)
- if (windows[i].used && !windows[i].focused)
- wm_draw_window(&windows[i]);
- for (i = 0; i < MAX_WINDOWS; i++)
- if (windows[i].used && windows[i].focused)
- wm_draw_window(&windows[i]);
- /* System bar always on top. */
- wm_draw_bar();
- /* Mouse cursor on top of everything. */
- mouse_redraw_cursor();
- }
- static void wm_render_bar(void)
- {
- unsigned int fbw = vga_fb_width();
- mouse_cursor_erase();
- vga_fill_rect(0, 0, fbw, WM_BAR_H, 32, 32, 48);
- wm_draw_bar();
- mouse_redraw_cursor();
- }
- static void wm_redraw_if_needed(void)
- {
- unsigned int i;
- static unsigned int last_sec = 0xFFFFFFFF;
- static unsigned int last_kexec_tick;
- unsigned int now_ms = get_ticks_ms();
- unsigned int now_sec = now_ms / 1000;
- int clock_changed = (now_sec != last_sec);
- int kexec_changed = kexec_busy && (now_ms - last_kexec_tick >= 100);
- int need_full = wm_full_redraw;
- if (kexec_changed)
- last_kexec_tick = now_ms;
- /* A background window's content changed -> full redraw (rare). */
- for (i = 0; i < MAX_WINDOWS; i++)
- if (windows[i].used && !windows[i].focused && windows[i].term->screen.dirty)
- need_full = 1;
- if (need_full) {
- wm_render_full();
- for (i = 0; i < MAX_WINDOWS; i++)
- if (windows[i].used)
- windows[i].term->screen.dirty = 0;
- wm_full_redraw = 0;
- last_sec = now_sec;
- return;
- }
- if (clock_changed || kexec_changed) {
- wm_render_bar();
- last_sec = now_sec;
- }
- /* Focused window's content changed -> redraw only that window. */
- for (i = 0; i < MAX_WINDOWS; i++) {
- if (windows[i].used && windows[i].focused && windows[i].term->screen.dirty) {
- mouse_cursor_erase();
- wm_draw_window(&windows[i]);
- mouse_redraw_cursor();
- windows[i].term->screen.dirty = 0;
- }
- }
- }
- static void wm_init(void)
- {
- unsigned int i;
- for (i = 0; i < MAX_WINDOWS; i++) {
- windows[i].used = 0;
- windows[i].term = &terms[i];
- }
- term_counter = 0;
- wm_full_redraw = 1;
- drag_active = 0;
- prev_buttons = 0;
- wm_spawn_terminal();
- }
- static void wm_blink_cursor(void)
- {
- static unsigned int last_cursor_tick;
- struct window *fw = wm_focused_window();
- unsigned int now = get_ticks_ms();
- if (!fw || !fw->term)
- return;
- if (now - last_cursor_tick >= CURSOR_MS) {
- last_cursor_tick = now;
- fw->term->screen.cursor_visible = !fw->term->screen.cursor_visible;
- fw->term->screen.dirty = 1;
- }
- }
- static void wm_handle_kexec(void)
- {
- if (kexec_busy)
- return;
- if (kexec_pending()) {
- kexec_busy = 1;
- if (process_create("kexec", program_kexec_main) < 0)
- kexec_busy = 0;
- return;
- }
- /* Same over UDP: the host's `make net-dev` is picked up from here. */
- if (netkexec_pending()) {
- kexec_busy = 1;
- if (process_create("netkexec", program_netkexec_main) < 0)
- kexec_busy = 0;
- }
- }
- void wm_run(void)
- {
- wm_init();
- for (;;) {
- mouse_poll_update();
- wm_handle_mouse();
- wm_handle_keyboard();
- wm_blink_cursor();
- netlog_flush();
- wm_handle_kexec();
- wm_redraw_if_needed();
- vga_fb_flush_pending_check();
- cpu_idle();
- }
- }
|