| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526 |
- /* Window manager: fixed-size draggable terminal windows + a system bar.
- * wm_run() is a normal kernel process; the idle loop keeps input routing,
- * cursor blinking, kexec detection and gfx session cleanup running even when
- * the WM process is not scheduled. */
- #include "wm.h"
- #include "blit.h"
- #include "fbtext.h"
- #include "idle.h"
- #include "io.h"
- #include "kexec.h"
- #include "mm.h"
- #include "mouse.h"
- #include "netkexec.h"
- #include "netlog.h"
- #include "paging.h"
- #include "payload.h"
- #include "process.h"
- #include "rtc.h"
- #include "serial.h"
- #include "sys_gfx.h"
- #include "timer.h"
- #include "version.h"
- #include "vga.h"
- #include "vfs.h"
- static struct window windows[MAX_WINDOWS];
- /* The terminal state is large and only needed for live windows. Allocate it
- * from the frame allocator (page-aligned) instead of carrying it in BSS: the
- * cell grid is exactly one page, so a ring-3 WM can map just that page. */
- #define TERM_STRIDE (2 * PAGE_SIZE)
- static struct term *terms[MAX_WINDOWS];
- static unsigned int term_counter;
- static int wm_full_redraw;
- _Static_assert(sizeof(struct term) <= TERM_STRIDE,
- "struct term no longer fits two pages");
- _Static_assert(__builtin_offsetof(struct term, screen) == 0,
- "term->screen must be the first field");
- _Static_assert(__builtin_offsetof(struct text_screen, cells) == 0,
- "text_screen.cells must be the first field");
- _Static_assert(sizeof(((struct text_screen *)0)->cells) == PAGE_SIZE,
- "the cell grid must be exactly one page");
- static int wm_terms_init(void)
- {
- unsigned int base = mm_alloc_pages(MAX_WINDOWS * (TERM_STRIDE / PAGE_SIZE));
- unsigned char *p;
- unsigned int i;
- if (!base)
- return -1;
- p = (unsigned char *)base;
- for (i = 0; i < MAX_WINDOWS * TERM_STRIDE; i++)
- p[i] = 0;
- for (i = 0; i < MAX_WINDOWS; i++)
- terms[i] = (struct term *)(base + i * TERM_STRIDE);
- return 0;
- }
- /* 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;
- /* Ring-3 WM lifecycle. The kernel only admits the process it spawned; a
- * stale pid cannot keep drawing after a restart. */
- static unsigned int wm_expected_pid;
- static unsigned int wm_pid;
- static unsigned int wm_cr3;
- static int wm_attached;
- static int wm_frame_active;
- static unsigned int wm_last_start;
- static unsigned int wm_last_seen;
- static inline unsigned int irq_save_disable(void)
- {
- unsigned int flags;
- __asm__ volatile ("pushfl; popl %0; cli" : "=r"(flags));
- return flags;
- }
- static inline void irq_restore(unsigned int flags)
- {
- __asm__ volatile ("pushl %0; popfl" :: "r"(flags));
- }
- static inline void invlpg_va(unsigned int va)
- {
- __asm__ volatile ("invlpg (%0)" : : "r"(va) : "memory");
- }
- static void wm_focus_index(unsigned int idx);
- static void wm_focus_top(void);
- static void wm_close_window(unsigned int idx);
- static void wm_clamp_window(struct window *w, int nx, int ny);
- static void wm_server_unmap_index(unsigned int idx);
- 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;
- }
- /* The terminal whose screen is `screen`, or NULL: a spawned ring-3 command
- * inherits its terminal's screen pointer, so this maps it back to the window
- * (used by the sys_gfx window-canvas service). */
- struct term *wm_term_for_screen(struct text_screen *screen)
- {
- unsigned int i;
- for (i = 0; i < MAX_WINDOWS; i++)
- if (windows[i].used && windows[i].term &&
- &windows[i].term->screen == screen)
- return windows[i].term;
- return 0;
- }
- /* Pixel size of a window's content area (the size of its gfx canvas). */
- int wm_content_size(struct term *t, unsigned int *w, unsigned int *h)
- {
- unsigned int i;
- for (i = 0; i < MAX_WINDOWS; i++)
- if (windows[i].used && windows[i].term == t) {
- *w = windows[i].pw - 2;
- *h = SCREEN_ROWS * 10;
- return 0;
- }
- return -1;
- }
- /* ---- Ring-3 WM ABI ---------------------------------------------------- */
- int wm_server_is_wm(void)
- {
- return wm_attached && procs[current_pid].pid == wm_pid;
- }
- int wm_server_frame_active(void)
- {
- return wm_frame_active;
- }
- /* Safe point for freeing another window's canvas: the WM is not between
- * SYS_WM_INFO/MAP and PRESENT. A ring-3 WM sleeps at the end of every loop;
- * the kernel fallback has no cached mappings. */
- int wm_server_can_reap(void)
- {
- if (wm_frame_active)
- return 0;
- return !wm_attached || process_is_sleeping(wm_pid);
- }
- static void wm_server_unmap_index(unsigned int idx)
- {
- struct window *w = &windows[idx];
- unsigned int va, i;
- if (!wm_cr3 || idx >= MAX_WINDOWS || !w->mapped_pages)
- return;
- va = WM_CONTENT_VA_BASE + idx * WM_CONTENT_SLOT;
- paging_release_user_range(wm_cr3, va, WM_CONTENT_SLOT, 0);
- for (i = 0; i < w->mapped_pages; i++)
- invlpg_va(va + i * PAGE_SIZE);
- w->mapped_kind = WM_KIND_NONE;
- w->mapped_gen = 0;
- w->mapped_pages = 0;
- w->mapped_src = 0;
- }
- void wm_server_unmap_screen(struct text_screen *screen)
- {
- unsigned int i;
- for (i = 0; i < MAX_WINDOWS; i++)
- if (windows[i].used && windows[i].term &&
- &windows[i].term->screen == screen)
- wm_server_unmap_index(i);
- }
- /* A dead gfx session can coexist with a newer session on the same screen (the
- * old canvas is freed later). Only drop the WM mapping when it still points
- * at *this* canvas, so collecting the old session cannot unmap the new one. */
- void wm_server_unmap_canvas(struct text_screen *screen, const void *canvas)
- {
- unsigned int i;
- for (i = 0; i < MAX_WINDOWS; i++)
- if (windows[i].used && windows[i].term &&
- &windows[i].term->screen == screen &&
- windows[i].mapped_kind == WM_KIND_GFX &&
- windows[i].mapped_src == canvas)
- wm_server_unmap_index(i);
- }
- static int wm_window_for_pid(unsigned int pid)
- {
- unsigned int i;
- for (i = 0; i < MAX_WINDOWS; i++)
- if (windows[i].used && windows[i].pid == pid)
- return (int)i;
- return -1;
- }
- static void wm_unmap_back(struct window *w, unsigned int pages)
- {
- unsigned int i;
- if (w->owner_cr3 && pages) {
- paging_release_user_range(w->owner_cr3, USER_GFX_BASE,
- pages * PAGE_SIZE, 0);
- for (i = 0; i < pages; i++)
- invlpg_va(USER_GFX_BASE + i * PAGE_SIZE);
- }
- w->owner_cr3 = 0;
- }
- static void wm_free_canvas_window(unsigned int idx)
- {
- struct window *w = &windows[idx];
- unsigned int i;
- if (!w->canvas_window)
- return;
- wm_unmap_back(w, w->back_pages);
- if (w->back) {
- for (i = 0; i < w->back_pages; i++)
- mm_free_page((unsigned int)w->back + i * PAGE_SIZE);
- w->back = 0;
- }
- w->back_pages = 0;
- gfx_free(&w->canvas);
- w->canvas_window = 0;
- }
- int wm_server_win_create(const struct win_create *in, struct win_info *out)
- {
- struct window *w;
- struct gfx_canvas *c;
- unsigned short *back;
- unsigned int i, j, pages, cw, ch, base;
- unsigned int flags;
- if (!in || !out)
- return -1;
- if (procs[current_pid].pid == 0 ||
- wm_window_for_pid(procs[current_pid].pid) >= 0)
- return -1;
- cw = in->w;
- ch = in->h;
- if (cw == 0 || ch == 0 || cw > 1024 || ch > 1024)
- return -1;
- if (cw + 2 > vga_fb_width() || ch + WM_TITLE_H + 2 > vga_fb_height())
- return -1;
- for (i = 0; i < MAX_WINDOWS; i++)
- if (!windows[i].used)
- break;
- if (i == MAX_WINDOWS)
- return -1;
- w = &windows[i];
- c = &w->canvas;
- if (gfx_alloc(c, cw, ch) != 0)
- return -1;
- pages = (cw * ch * 2 + PAGE_SIZE - 1) / PAGE_SIZE;
- back = (unsigned short *)mm_alloc_pages(pages);
- if (!back) {
- gfx_free(c);
- return -1;
- }
- for (j = 0; j < cw * ch; j++)
- back[j] = 0;
- flags = irq_save_disable();
- base = procs[current_pid].cr3;
- for (j = 0; j < pages; j++) {
- if (!paging_map_page(base, USER_GFX_BASE + j * PAGE_SIZE,
- (unsigned int)back + j * PAGE_SIZE,
- PTE_USER | PTE_WRITABLE | PTE_PRESENT)) {
- irq_restore(flags);
- paging_release_user_range(base, USER_GFX_BASE,
- (j ? j : 1) * PAGE_SIZE, 0);
- for (i = 0; i < pages; i++)
- mm_free_page((unsigned int)back + i * PAGE_SIZE);
- gfx_free(c);
- return -1;
- }
- invlpg_va(USER_GFX_BASE + j * PAGE_SIZE);
- }
- irq_restore(flags);
- w->used = 1;
- w->canvas_window = 1;
- w->term = 0;
- w->pid = procs[current_pid].pid;
- w->owner_cr3 = base;
- w->back = back;
- w->back_pages = pages;
- w->canvas.seq = 1;
- w->canvas.drawn = 0;
- w->pw = cw + 2;
- w->ph = ch + WM_TITLE_H + 2;
- w->x = 24 + i * 32;
- w->y = WM_BAR_H + 12 + i * 28;
- if (w->x + w->pw > vga_fb_width())
- w->x = (vga_fb_width() > w->pw) ? vga_fb_width() - w->pw : 0;
- if (w->y + w->ph > vga_fb_height())
- w->y = WM_BAR_H + 4;
- w->focused = 0;
- w->content_gen = 1;
- w->mapped_kind = WM_KIND_NONE;
- w->mapped_gen = 0;
- w->mapped_pages = 0;
- w->mapped_src = 0;
- w->key_head = w->key_tail = 0;
- for (j = 0; j < sizeof(w->title) - 1 && in->title[j]; j++)
- w->title[j] = in->title[j];
- w->title[j] = '\0';
- for (j = 0; j < MAX_WINDOWS; j++)
- if (windows[j].used)
- windows[j].focused = (j == i);
- wm_request_redraw();
- out->fb = (void *)USER_GFX_BASE;
- out->width = cw;
- out->height = ch;
- out->id = i;
- return 0;
- }
- int wm_server_win_present(void)
- {
- int idx = wm_window_for_pid(procs[current_pid].pid);
- struct window *w;
- const unsigned short *src;
- unsigned int n, i;
- if (idx < 0)
- return -1;
- w = &windows[idx];
- if (!w->canvas_window || !w->back || !w->canvas.px)
- return -1;
- src = (const unsigned short *)USER_GFX_BASE;
- n = w->canvas.w * w->canvas.h;
- for (i = 0; i + 1 < n; i += 2)
- ((unsigned int *)w->canvas.px)[i / 2] = ((const unsigned int *)src)[i / 2];
- if (n & 1)
- w->canvas.px[n - 1] = src[n - 1];
- w->canvas.seq++;
- return 0;
- }
- int wm_server_win_close(void)
- {
- int idx = wm_window_for_pid(procs[current_pid].pid);
- if (idx < 0 || !wm_server_can_reap())
- return -1;
- wm_server_unmap_index((unsigned int)idx);
- wm_free_canvas_window((unsigned int)idx);
- windows[idx].used = 0;
- windows[idx].pid = 0;
- windows[idx].focused = 0;
- windows[idx].content_gen++;
- wm_focus_top();
- wm_request_redraw();
- return 0;
- }
- int wm_server_key_poll(void)
- {
- int idx = wm_window_for_pid(procs[current_pid].pid);
- struct window *w;
- int k;
- if (idx < 0)
- return 0;
- w = &windows[idx];
- if (w->key_tail == w->key_head)
- return 0;
- k = w->keys[w->key_tail];
- w->key_tail = (w->key_tail + 1) % 16;
- return k;
- }
- void wm_content_changed(struct text_screen *screen)
- {
- unsigned int i;
- for (i = 0; i < MAX_WINDOWS; i++)
- if (windows[i].used && windows[i].term &&
- &windows[i].term->screen == screen)
- windows[i].content_gen++;
- wm_request_redraw();
- }
- int wm_server_attach(struct wm_screen_info *out)
- {
- struct surface *s;
- unsigned int pages, i, bytes;
- if (!out || !wm_expected_pid ||
- procs[current_pid].pid != wm_expected_pid || wm_attached) {
- serial_puts(SERIAL_COM1, "wm: attach rejected (expected ");
- serial_puthex(SERIAL_COM1, wm_expected_pid);
- serial_puts(SERIAL_COM1, " current ");
- serial_puthex(SERIAL_COM1, procs[current_pid].pid);
- serial_puts(SERIAL_COM1, ")\n");
- return -1;
- }
- if (!vga_shadow_is_active()) {
- serial_puts(SERIAL_COM1, "wm: attach: no shadow buffer\n");
- return -1;
- }
- s = vga_screen_surface();
- if (!s || !s->px)
- return -1;
- bytes = s->pitch * s->h;
- pages = (bytes + PAGE_SIZE - 1) / PAGE_SIZE;
- wm_cr3 = procs[current_pid].cr3;
- for (i = 0; i < pages; i++) {
- unsigned int va = WM_SHADOW_VA + i * PAGE_SIZE;
- if (!paging_map_page(wm_cr3, va,
- (unsigned int)s->px + i * PAGE_SIZE,
- PTE_USER | PTE_WRITABLE | PTE_PRESENT)) {
- paging_release_user_range(wm_cr3, WM_SHADOW_VA,
- i * PAGE_SIZE, 0);
- wm_cr3 = 0;
- return -1;
- }
- invlpg_va(va);
- }
- for (i = 0; i < MAX_WINDOWS; i++) {
- windows[i].mapped_kind = WM_KIND_NONE;
- windows[i].mapped_gen = 0;
- windows[i].mapped_pages = 0;
- windows[i].mapped_src = 0;
- }
- wm_pid = procs[current_pid].pid;
- wm_attached = 1;
- wm_frame_active = 0;
- wm_last_seen = get_ticks_ms();
- mouse_set_auto_draw(1);
- wm_full_redraw = 1;
- out->width = s->w;
- out->height = s->h;
- out->pitch = s->pitch;
- out->bpp = s->bpp;
- out->bytes = bytes;
- out->fb = (void *)WM_SHADOW_VA;
- serial_puts(SERIAL_COM1, "wm: ring-3 WM attached\n");
- return 0;
- }
- int wm_server_info(unsigned int index, struct wm_window_info *out)
- {
- struct window *w;
- struct gfx_canvas *g = 0;
- struct text_screen *s = 0;
- unsigned int i;
- if (!wm_server_is_wm() || !out || index >= MAX_WINDOWS)
- return -1;
- w = &windows[index];
- if (!w->used)
- return 0;
- out->used = 1;
- out->kind = WM_KIND_NONE;
- out->focused = w->focused ? 1 : 0;
- out->pid = w->pid;
- out->x = (int)w->x;
- out->y = (int)w->y;
- out->w = w->pw;
- out->h = w->ph;
- out->cols = 0;
- out->rows = 0;
- out->cursor = 0;
- out->cursor_visible = 0;
- out->dirty = 0;
- out->dirty_seq = 0;
- out->dmg_col0 = out->dmg_row0 = 0;
- out->dmg_cols = out->dmg_rows = 0;
- out->content_gen = w->content_gen;
- for (i = 0; i < sizeof(out->title) - 1 && w->title[i]; i++)
- out->title[i] = w->title[i];
- out->title[i] = '\0';
- if (w->canvas_window) {
- g = &w->canvas;
- } else if (w->term) {
- s = &w->term->screen;
- g = sys_gfx_canvas_for(s);
- } else {
- return 1;
- }
- if (g) {
- out->kind = WM_KIND_GFX;
- out->content_w = g->w;
- out->content_h = g->h;
- out->dirty = (g->seq != g->drawn);
- out->dirty_seq = g->seq;
- } else if (s) {
- unsigned int c0, r0, nc, nr;
- out->kind = WM_KIND_TEXT;
- out->content_w = w->pw - 2;
- out->content_h = SCREEN_ROWS * 10;
- out->cols = s->cols;
- out->rows = s->rows;
- out->cursor = s->cursor;
- out->cursor_visible = s->cursor_visible;
- out->dirty_seq = s->damage_seq;
- if (screen_damage_block(s, &c0, &r0, &nc, &nr)) {
- out->dirty = 1;
- out->dmg_col0 = c0;
- out->dmg_row0 = r0;
- out->dmg_cols = nc;
- out->dmg_rows = nr;
- }
- }
- return 1;
- }
- int wm_server_map(unsigned int index, struct wm_map_info *out)
- {
- struct window *w;
- struct gfx_canvas *g = 0;
- struct text_screen *s = 0;
- const void *src;
- unsigned int kind, pages, i, va;
- unsigned int wpx, hpx, stride;
- if (!wm_server_is_wm() || !out || index >= MAX_WINDOWS || !wm_cr3)
- return -1;
- w = &windows[index];
- if (!w->used)
- return -1;
- if (w->canvas_window) {
- g = &w->canvas;
- } else if (w->term) {
- s = &w->term->screen;
- g = sys_gfx_canvas_for(s);
- } else {
- return -1;
- }
- kind = g ? WM_KIND_GFX : WM_KIND_TEXT;
- src = g ? (const void *)g->px : (const void *)w->term;
- if (w->mapped_kind == kind && w->mapped_gen == w->content_gen &&
- w->mapped_src == src) {
- out->va = (void *)(WM_CONTENT_VA_BASE + index * WM_CONTENT_SLOT);
- out->kind = kind;
- out->generation = w->content_gen;
- if (g) {
- out->w = g->w;
- out->h = g->h;
- out->stride = g->w * 2;
- } else {
- out->w = s->cols;
- out->h = s->rows;
- out->stride = s->cols * 2;
- }
- return 0;
- }
- wm_server_unmap_index(index);
- if (g) {
- wpx = g->w;
- hpx = g->h;
- stride = g->w * 2;
- pages = (wpx * hpx * 2 + PAGE_SIZE - 1) / PAGE_SIZE;
- if (pages * PAGE_SIZE > WM_CONTENT_SLOT)
- return -1;
- } else {
- wpx = s->cols;
- hpx = s->rows;
- stride = s->cols * 2;
- pages = 1;
- }
- va = WM_CONTENT_VA_BASE + index * WM_CONTENT_SLOT;
- for (i = 0; i < pages; i++) {
- if (!paging_map_page(wm_cr3, va + i * PAGE_SIZE,
- (unsigned int)src + i * PAGE_SIZE,
- PTE_USER | PTE_PRESENT)) {
- paging_release_user_range(wm_cr3, va,
- (i ? i : 1) * PAGE_SIZE, 0);
- return -1;
- }
- invlpg_va(va + i * PAGE_SIZE);
- }
- w->mapped_kind = kind;
- w->mapped_gen = w->content_gen;
- w->mapped_pages = pages;
- w->mapped_src = src;
- out->va = (void *)va;
- out->kind = kind;
- out->w = wpx;
- out->h = hpx;
- out->stride = stride;
- out->generation = w->content_gen;
- return 0;
- }
- int wm_server_ack(unsigned int index, unsigned int seq)
- {
- struct window *w;
- struct gfx_canvas *g;
- if (!wm_server_is_wm() || index >= MAX_WINDOWS)
- return -1;
- w = &windows[index];
- if (!w->used)
- return -1;
- if (w->canvas_window) {
- if (seq == w->canvas.seq)
- w->canvas.drawn = seq;
- return 0;
- }
- if (!w->term)
- return -1;
- g = sys_gfx_canvas_for(&w->term->screen);
- if (g) {
- if (seq == g->seq)
- g->drawn = seq;
- } else if (seq == w->term->screen.damage_seq) {
- screen_clear_damage(&w->term->screen);
- }
- return 0;
- }
- int wm_server_focus(unsigned int index)
- {
- if (!wm_server_is_wm() || index >= MAX_WINDOWS ||
- !windows[index].used)
- return -1;
- wm_focus_index(index);
- return 0;
- }
- int wm_server_set_geom(unsigned int index, int x, int y)
- {
- if (!wm_server_is_wm() || index >= MAX_WINDOWS ||
- !windows[index].used)
- return -1;
- wm_clamp_window(&windows[index], x, y);
- return 0;
- }
- int wm_server_create_term(void)
- {
- int idx;
- if (!wm_server_is_wm())
- return -1;
- idx = wm_spawn_terminal();
- return idx;
- }
- int wm_server_close(unsigned int index)
- {
- if (!wm_server_is_wm() || wm_frame_active ||
- index >= MAX_WINDOWS || !windows[index].used)
- return -1;
- wm_close_window(index);
- return 0;
- }
- int wm_server_poll(struct wm_status *out)
- {
- if (!wm_server_is_wm() || !out)
- return -1;
- wm_last_seen = get_ticks_ms();
- out->mouse.x = mouse_get_x();
- out->mouse.y = mouse_get_y();
- out->mouse.buttons = mouse_buttons_state();
- out->full_redraw = wm_full_redraw ? 1 : 0;
- out->kexec_busy = kexec_busy ? 1 : 0;
- out->kexec_progress = kexec_progress;
- out->kexec_total = kexec_total;
- wm_full_redraw = 0;
- return 0;
- }
- int wm_server_begin(void)
- {
- if (!wm_server_is_wm() || wm_frame_active)
- return -1;
- mouse_cursor_erase();
- mouse_set_auto_draw(0);
- wm_frame_active = 1;
- return 0;
- }
- int wm_server_present(const struct wm_present_info *in)
- {
- unsigned int i;
- if (!wm_server_is_wm() || !wm_frame_active || !in)
- return -1;
- for (i = 0; i < in->count && i < 4; i++) {
- int x0 = in->rect[i].x;
- int y0 = in->rect[i].y;
- int x1 = x0 + (int)in->rect[i].w;
- int y1 = y0 + (int)in->rect[i].h;
- if (x0 < 0) x0 = 0;
- if (y0 < 0) y0 = 0;
- if (x1 < 0) x1 = 0;
- if (y1 < 0) y1 = 0;
- if (x1 > (int)vga_fb_width()) x1 = (int)vga_fb_width();
- if (y1 > (int)vga_fb_height()) y1 = (int)vga_fb_height();
- if (x1 > x0 && y1 > y0)
- vga_fb_mark_dirty_rect_xy((unsigned int)y0,
- (unsigned int)(y1 - y0),
- (unsigned int)x0, (unsigned int)x1);
- }
- mouse_redraw_cursor();
- mouse_set_auto_draw(1);
- wm_frame_active = 0;
- /* The copy can be long; let IRQs run. vga_present_shadow() keeps the
- * scheduler from switching away while it owns the damage list. */
- __asm__ volatile ("sti" ::: "memory");
- if (in->immediate)
- vga_fb_flush();
- else
- vga_fb_flush_budget(VGA_FLUSH_BUDGET_BYTES);
- sys_gfx_collect();
- return 0;
- }
- int wm_server_sync(void)
- {
- if (wm_frame_active)
- return -1;
- sys_gfx_collect();
- return 0;
- }
- static void wm_server_wm_died(void)
- {
- unsigned int i;
- if (wm_frame_active) {
- mouse_set_auto_draw(1);
- mouse_redraw_cursor();
- }
- wm_frame_active = 0;
- wm_attached = 0;
- wm_pid = 0;
- wm_cr3 = 0;
- for (i = 0; i < MAX_WINDOWS; i++) {
- windows[i].mapped_kind = WM_KIND_NONE;
- windows[i].mapped_gen = 0;
- windows[i].mapped_pages = 0;
- windows[i].mapped_src = 0;
- }
- wm_full_redraw = 1;
- serial_puts(SERIAL_COM1, "wm: ring-3 WM stopped; restarting\n");
- }
- void wm_server_start(void)
- {
- struct vfs_file f;
- struct vfs_mount *m = 0;
- unsigned int flags;
- int pid;
- if (wm_expected_pid && process_is_alive(wm_expected_pid))
- return;
- if (wm_last_start && get_ticks_ms() - wm_last_start < 1000)
- return;
- wm_last_start = get_ticks_ms();
- if (vfs_load_file("/ramfs/bin/wm", &f, &m) == 0) {
- flags = irq_save_disable();
- pid = process_create_user("wm", f.data, f.size, "", "/");
- if (pid > 0)
- wm_expected_pid = (unsigned int)pid;
- irq_restore(flags);
- vfs_release_file(m, &f);
- if (pid > 0) {
- serial_puts(SERIAL_COM1, "wm: ring-3 WM started\n");
- return;
- }
- serial_puts(SERIAL_COM1, "wm: cannot load /ramfs/bin/wm\n");
- } else {
- serial_puts(SERIAL_COM1, "wm: /ramfs/bin/wm not found\n");
- }
- /* Temporary fallback during the transition: the old kernel WM. */
- flags = irq_save_disable();
- pid = process_create("wm", wm_run);
- if (pid > 0)
- wm_expected_pid = (unsigned int)pid;
- irq_restore(flags);
- if (pid > 0)
- serial_puts(SERIAL_COM1, "wm: kernel WM fallback\n");
- }
- void wm_server_supervise(void)
- {
- unsigned int i, now = get_ticks_ms();
- /* Watchdog: a WM that never attaches, or stops polling for several
- * seconds, is treated like a crash so a wedged UI can recover. */
- if (wm_expected_pid && process_is_alive(wm_expected_pid)) {
- if ((wm_attached && now - wm_last_seen > 5000) ||
- (!wm_attached && now - wm_last_start > 3000)) {
- serial_puts(SERIAL_COM1, "wm: watchdog, killing WM\n");
- process_kill(wm_expected_pid);
- }
- }
- if (wm_expected_pid && !process_is_alive(wm_expected_pid)) {
- wm_server_wm_died();
- wm_expected_pid = 0;
- }
- /* Owner processes that exited close their windows. Only at a safe
- * point: the WM may still be between INFO/MAP and PRESENT. */
- if (wm_server_can_reap()) {
- for (i = 0; i < MAX_WINDOWS; i++) {
- struct window *w = &windows[i];
- if (!w->used || !w->pid || process_is_alive(w->pid))
- continue;
- if (w->canvas_window)
- w->owner_cr3 = 0; /* cr3 is gone/reaped */
- wm_close_window(i);
- }
- }
- if (wm_expected_pid && process_is_alive(wm_expected_pid))
- return;
- wm_server_start();
- }
- void wm_server_init(void)
- {
- unsigned int i;
- int have_terms = (wm_terms_init() == 0);
- if (!have_terms)
- serial_puts(SERIAL_COM1, "wm: no memory for terminal state\n");
- for (i = 0; i < MAX_WINDOWS; i++) {
- windows[i].used = 0;
- windows[i].term = terms[i];
- windows[i].mapped_kind = WM_KIND_NONE;
- windows[i].mapped_gen = 0;
- windows[i].mapped_pages = 0;
- windows[i].mapped_src = 0;
- }
- term_counter = 0;
- wm_full_redraw = 1;
- drag_active = 0;
- prev_buttons = 0;
- wm_expected_pid = 0;
- wm_pid = 0;
- wm_cr3 = 0;
- wm_attached = 0;
- wm_frame_active = 0;
- if (have_terms)
- wm_spawn_terminal();
- }
- 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;
- /* Preferred path: the ring-3 terminal owns a canvas window and creates
- * its own tty. The in-kernel cell terminal below is only a fallback for
- * a pack without /ramfs/bin/term. */
- {
- struct vfs_file f;
- struct vfs_mount *m = 0;
- if (vfs_load_file("/ramfs/bin/term", &f, &m) == 0) {
- unsigned int flags = irq_save_disable();
- pid = process_create_user_tty("term", f.data, f.size,
- "", "/", 0);
- irq_restore(flags);
- vfs_release_file(m, &f);
- if (pid > 0) {
- wm_request_redraw();
- return 0;
- }
- serial_puts(SERIAL_COM1, "wm: cannot start ring-3 term\n");
- }
- }
- for (i = 0; i < MAX_WINDOWS; i++)
- if (!windows[i].used && terms[i])
- 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->canvas_window = 0;
- w->canvas.px = 0;
- w->canvas.w = w->canvas.h = 0;
- w->canvas.seq = w->canvas.drawn = 0;
- w->back = 0;
- w->back_pages = 0;
- w->owner_cr3 = 0;
- w->key_head = w->key_tail = 0;
- w->pid = 0;
- w->focused = 0;
- w->pw = SCREEN_COLS * 8 + 2;
- w->ph = WM_TITLE_H + SCREEN_ROWS * 10 + 2;
- w->content_gen = 1;
- w->mapped_kind = WM_KIND_NONE;
- w->mapped_gen = 0;
- w->mapped_pages = 0;
- w->mapped_src = 0;
- /* 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 (int)i;
- }
- 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->term) {
- sys_gfx_kill_for_screen(&w->term->screen); /* stop a demo */
- if (w->pid)
- process_kill(w->pid);
- /* Free the dead session before this term/window slot can be
- * reused, so a late collect cannot unmap a new window that
- * lands on the same screen. */
- sys_gfx_collect();
- } else if (w->canvas_window) {
- if (w->pid)
- process_kill(w->pid);
- wm_server_unmap_index(idx);
- wm_free_canvas_window(idx);
- } else if (w->pid) {
- process_kill(w->pid);
- }
- wm_server_unmap_index(idx);
- w->used = 0;
- w->pid = 0;
- w->focused = 0;
- w->content_gen++;
- w->mapped_kind = WM_KIND_NONE;
- w->mapped_gen = 0;
- w->mapped_pages = 0;
- w->mapped_src = 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;
- }
- /* 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);
- }
- static void wm_draw_window_clip(struct window *w, unsigned int cx, unsigned int cy,
- unsigned int cw, unsigned int ch);
- /* Repaint the desktop rectangle (x, y, w, h): background, then every
- intersecting window back-to-front (focused last), then mark it dirty. The
- caller handles the mouse cursor. The rect must lie below the system bar
- (windows are clamped to y >= WM_BAR_H, so the bar is never overlapped and
- needs no clip variant). */
- static void wm_redraw_rect(unsigned int x, unsigned int y,
- unsigned int w, unsigned int h)
- {
- unsigned int i;
- if (w == 0 || h == 0)
- return;
- 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 && !w2->focused &&
- x < w2->x + w2->pw && x + w > w2->x &&
- y < w2->y + w2->ph && y + h > w2->y)
- wm_draw_window_clip(w2, x, y, w, h);
- }
- for (i = 0; i < MAX_WINDOWS; i++) {
- struct window *w2 = &windows[i];
- if (w2->used && w2->focused &&
- x < w2->x + w2->pw && x + w > w2->x &&
- y < w2->y + w2->ph && y + h > w2->y)
- wm_draw_window_clip(w2, x, y, w, h);
- }
- vga_fb_mark_dirty_rect_xy(y, h, x, x + w);
- }
- /* Move a window in the shadow buffer: copy its rect, redraw the exposed
- sliver, keep the cursor on top, and flush the swept band immediately so a
- drag step is always one consistent frame (a budgeted flush would copy only
- the top rows of the band per iteration and the window would appear to
- stretch while the mouse keeps moving). */
- 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;
- mouse_cursor_erase();
- blit->copy(nx, ny, ox, oy, w->pw, w->ph);
- if (dy > 0)
- wm_redraw_rect(ox, oy, w->pw, (unsigned int)dy);
- else if (dy < 0)
- wm_redraw_rect(ox, ny + w->ph, w->pw, (unsigned int)(-dy));
- if (dx > 0)
- wm_redraw_rect(ox, oy, (unsigned int)dx, w->ph);
- else if (dx < 0)
- wm_redraw_rect(nx + w->pw, oy, (unsigned int)(-dx), w->ph);
- /* Only the moved window's new rect needs marking: the repaints above
- cover the exposed slivers exactly, and the cursor marks itself. The
- damage list merges these when the step is small (the union is then
- the swept hull anyway) and keeps them apart after a big jump. */
- vga_fb_mark_dirty_rect_xy(w->y, w->ph, w->x, w->x + w->pw);
- mouse_redraw_cursor();
- 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)
- 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;
- }
- void wm_pump_keyboard(void)
- {
- struct window *fw = wm_focused_window();
- static unsigned int last_hw_poll;
- unsigned int now = get_ticks_ms();
- int n;
- if (io_keyboard_raw())
- return; /* an inline command is polling for keys itself */
- if (!fw)
- return;
- for (n = 0; n < 8; n++) {
- int k = io_poll_key();
- if (!k && (now - last_hw_poll) >= (io_keyboard_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;
- if (fw->term) {
- /* A graphics command owns its window's keys (ESC etc.);
- * Ctrl+C always reaches the terminal so its foreground
- * kill keeps working. */
- if (k == IO_KEY_CTRL_C ||
- !sys_gfx_push_key(&fw->term->screen, k))
- term_key_push(fw->term, k);
- } else if (fw->canvas_window) {
- unsigned int next = (fw->key_head + 1) % 16;
- if (next != fw->key_tail) {
- fw->keys[fw->key_head] = k;
- fw->key_head = next;
- }
- }
- }
- }
- 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();
- }
- /* Draw window w clipped to (cx, cy, cw, ch). Chrome uses wm_fill_rect_clip,
- * content the cells/canvas clip variants, so pixels outside the clip are
- * untouched. */
- static void wm_draw_window_clip(struct window *w, unsigned int cx, unsigned int cy,
- unsigned int cw, unsigned int ch)
- {
- struct text_screen *s;
- struct gfx_canvas *g;
- if (!w->term)
- return; /* generic canvas windows are drawn by the ring-3 WM */
- s = &w->term->screen;
- g = sys_gfx_canvas_for(s);
- unsigned int bx = w->x + w->pw - 1 - WM_CLOSE_W;
- unsigned int cony = w->y + 1 + WM_TITLE_H;
- /* Border. */
- wm_fill_rect_clip(w->x, w->y, w->pw, w->ph, cx, cy, cw, ch, 200, 200, 200);
- /* Title bar. */
- if (w->focused)
- wm_fill_rect_clip(w->x + 1, w->y + 1, w->pw - 2, WM_TITLE_H,
- cx, cy, cw, ch, 70, 90, 150);
- else
- wm_fill_rect_clip(w->x + 1, w->y + 1, w->pw - 2, WM_TITLE_H,
- cx, cy, cw, ch, 100, 100, 100);
- /* Title text. */
- fbtext_draw_string_xy_clip(w->x + 4, w->y + 3, w->title, 255, 255, 255,
- cx, cy, cw, ch);
- /* Close button. */
- wm_fill_rect_clip(bx, w->y + 1, WM_CLOSE_W, WM_TITLE_H,
- cx, cy, cw, ch, 190, 60, 60);
- fbtext_draw_string_xy_clip(bx + 3, w->y + 3, "x", 255, 255, 255,
- cx, cy, cw, ch);
- /* Content: text cells or the gfx canvas. */
- wm_fill_rect_clip(w->x + 1, cony, w->pw - 2, SCREEN_ROWS * 10,
- cx, cy, cw, ch, 0, 0, 0);
- if (g)
- blit->canvas(g, w->x + 1, cony, cx, cy, cw, ch);
- else
- fbtext_draw_cells_clip(s->cells, s->cols, s->rows,
- w->x + 1, cony, cx, cy, cw, ch);
- /* Blinking block cursor (text windows only). */
- if (!g && w->focused && s->cursor_visible) {
- unsigned int col = s->cursor % s->cols;
- unsigned int row = s->cursor / s->cols;
- wm_fill_rect_clip(w->x + 1 + col * 8, cony + row * 10, 2, 10,
- cx, cy, cw, ch, 255, 255, 255);
- }
- }
- static void wm_draw_window(struct window *w)
- {
- wm_draw_window_clip(w, w->x, w->y, w->pw, w->ph);
- }
- 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 kexec_changed = kexec_busy && (now_ms - last_kexec_tick >= 100);
- int bar_changed;
- if (kexec_changed)
- last_kexec_tick = now_ms;
- bar_changed = (now_sec != last_sec) || kexec_changed;
- if (wm_full_redraw) {
- unsigned int seq_before[MAX_WINDOWS];
- for (i = 0; i < MAX_WINDOWS; i++) {
- struct gfx_canvas *g = (windows[i].used && windows[i].term) ?
- sys_gfx_canvas_for(&windows[i].term->screen) : 0;
- seq_before[i] = g ? g->seq : 0;
- }
- wm_render_full();
- for (i = 0; i < MAX_WINDOWS; i++)
- if (windows[i].used && windows[i].term) {
- struct gfx_canvas *g = sys_gfx_canvas_for(&windows[i].term->screen);
- screen_clear_damage(&windows[i].term->screen);
- if (g)
- g->drawn = seq_before[i]; /* keep a frame
- presented during the render */
- }
- wm_full_redraw = 0;
- last_sec = now_sec;
- return;
- }
- if (bar_changed) {
- wm_render_bar();
- last_sec = now_sec;
- }
- /* Any window whose content changed (focused or not): repaint just its
- damaged cell block (a canvas window repaints its whole content)
- through the damage-rect compositor. */
- for (i = 0; i < MAX_WINDOWS; i++) {
- struct text_screen *s;
- struct gfx_canvas *g;
- unsigned int seq, col0, row0, ncols, nrows;
- if (!windows[i].used || !windows[i].term)
- continue;
- s = &windows[i].term->screen;
- g = sys_gfx_canvas_for(s);
- if (!screen_damage_block(s, &col0, &row0, &ncols, &nrows) &&
- !(g && g->seq != g->drawn))
- continue;
- seq = g ? g->seq : 0; /* capture before compositing */
- mouse_cursor_erase();
- if (g)
- wm_redraw_rect(windows[i].x + 1,
- windows[i].y + 1 + WM_TITLE_H,
- windows[i].pw - 2, SCREEN_ROWS * 10);
- else
- wm_redraw_rect(windows[i].x + 1 + col0 * 8,
- windows[i].y + 1 + WM_TITLE_H + row0 * 10,
- ncols * 8, nrows * 10);
- mouse_redraw_cursor();
- screen_clear_damage(s);
- if (g)
- g->drawn = seq;
- }
- }
- 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 (sys_gfx_canvas_for(&fw->term->screen))
- return; /* no text cursor over a graphics command */
- if (now - last_cursor_tick >= CURSOR_MS) {
- last_cursor_tick = now;
- fw->term->screen.cursor_visible = !fw->term->screen.cursor_visible;
- screen_damage_cursor(&fw->term->screen);
- }
- }
- void wm_handle_kexec(void)
- {
- if (kexec_busy)
- return;
- /* A command pack for /ramfs (checked before kexec: both start with
- * 'K', and the kexec detector drops anything that is not KXEC). */
- if (payload_pending()) {
- kexec_busy = 1;
- if (process_create("payload", program_payload_main) < 0)
- kexec_busy = 0;
- 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)
- {
- for (;;) {
- wm_handle_mouse();
- wm_redraw_if_needed();
- vga_fb_flush_budget(VGA_FLUSH_BUDGET_BYTES);
- cpu_idle();
- }
- }
|