| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604 |
- /* Kernel window server for the ring-3 WM: window bookkeeping, canvas
- * mapping/presentation and input routing. There is no in-kernel WM. */
- #include "wm.h"
- #include "cursor_abi.h"
- #include "backlight.h"
- #include "bootcon.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 "serial.h"
- #include "timer.h"
- #include "vga.h"
- #include "vfs.h"
- static struct window windows[MAX_WINDOWS];
- static int wm_full_redraw;
- static unsigned int wm_content_seq;
- static unsigned int wm_next_content_gen(void)
- {
- if (++wm_content_seq == 0)
- wm_content_seq++;
- return wm_content_seq;
- }
- /* Content pointer routing for canvas windows. wm_server_poll() is
- * called by the ring-3 WM every loop, so it is the cheapest place to turn the
- * sampled mouse state into press/release edges. */
- static unsigned int wm_ptr_buttons;
- /* SYS_WM_SET_CURSOR: the WM's shape for its own chrome (resize border,
- * title-bar drag, launchers), or CURSOR_NO_OVERRIDE. */
- static int wm_cursor_override = CURSOR_NO_OVERRIDE;
- /* The WM owns the pointer while it drags or resizes a window: no pointer
- * events reach canvas clients and SYS_WIN_POINTER reports no buttons. */
- static int wm_pointer_owned;
- static int wm_ptr_capture = -1;
- /* 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 int wm_first_frame;
- static int wm_boot_ready;
- static int wm_boot_logged;
- static unsigned int wm_wallpaper_seq;
- 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);
- static int wm_window_at(int px, int py);
- static int wm_resize_target(int px, int py, int *edges);
- /* Content origin: a normal window has no visible frame, so its content
- * starts one title bar below the window origin; a popover is a flat
- * chromeless panel whose canvas fills the window. */
- static int win_content_x(const struct window *w)
- {
- return w->x + (w->popover ? 0 : WM_BORDER_W);
- }
- static int win_content_y(const struct window *w)
- {
- return w->y + (w->popover ? 0 : WM_BORDER_W + WM_TITLE_H);
- }
- 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;
- }
- /* ---- Ring-3 WM ABI ---------------------------------------------------- */
- int wm_server_is_wm(void)
- {
- return wm_attached && current_proc()->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;
- }
- /* 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. */
- 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_WIN_BASE,
- pages * PAGE_SIZE, 0);
- for (i = 0; i < pages; i++)
- invlpg_va(USER_WIN_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;
- if (w->pending_back) {
- for (i = 0; i < w->pending_pages; i++)
- mm_free_page((unsigned int)w->pending_back + i * PAGE_SIZE);
- w->pending_back = 0;
- }
- w->pending_pages = 0;
- w->resize_pending = 0;
- w->canvas_dmg_w = w->canvas_dmg_h = 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;
- int popover = (in && (in->flags & WIN_F_POPOVER)) ? 1 : 0;
- unsigned int pad = popover ? 0 : WM_BORDER_W;
- unsigned int top = popover ? 0 : WM_TITLE_H + WM_BORDER_W;
- if (!in || !out)
- return -1;
- if (current_proc()->pid == 0 ||
- wm_window_for_pid(current_proc()->pid) >= 0)
- return -1;
- cw = in->w;
- ch = in->h;
- if (cw == 0 || ch == 0 || cw > WM_WIN_MAX_W || ch > WM_WIN_MAX_H)
- return -1;
- if (cw * ch * 2 > WM_CONTENT_SLOT || cw * ch * 2 > USER_WIN_RANGE)
- return -1;
- if (cw + pad * 2 > vga_fb_width() ||
- ch + top + pad > vga_fb_height())
- return -1;
- for (i = 0; i < MAX_WINDOWS; i++)
- if (!windows[i].used)
- break;
- if (i == MAX_WINDOWS) {
- serial_puts(SERIAL_COM1, "wm: win_create no window slot\n");
- 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 = current_proc()->cr3;
- for (j = 0; j < pages; j++) {
- if (!paging_map_page(base, USER_WIN_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_WIN_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_WIN_BASE + j * PAGE_SIZE);
- }
- irq_restore(flags);
- w->used = 1;
- w->popover = popover;
- w->canvas_window = 1;
- w->pid = current_proc()->pid;
- w->owner_cr3 = base;
- w->back = back;
- w->back_pages = pages;
- w->canvas.seq = 1;
- w->canvas.drawn = 0;
- w->pending_back = 0;
- w->pending_pages = 0;
- w->resize_pending = 0;
- w->scroll_x = w->scroll_y = 0;
- w->cursor = CURSOR_DEFAULT;
- w->canvas_dmg_x = w->canvas_dmg_y = 0;
- w->canvas_dmg_w = w->canvas_dmg_h = 0;
- w->pw = cw + pad * 2;
- w->ph = ch + top + pad;
- if (popover) {
- w->x = (int)vga_fb_width() - (int)w->pw;
- w->y = WM_BAR_H;
- } else {
- w->x = 24 + (int)i * 32;
- w->y = WM_BAR_H + 12 + (int)i * 28;
- if (w->x + (int)w->pw > (int)vga_fb_width())
- w->x = ((int)vga_fb_width() > (int)w->pw) ?
- (int)vga_fb_width() - (int)w->pw : 0;
- if (w->y + (int)w->ph > (int)vga_fb_height())
- w->y = WM_BAR_H + 4;
- }
- wm_clamp_window(w, w->x, w->y);
- w->focused = 0;
- w->content_gen = wm_next_content_gen();
- 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;
- wait_queue_init(&w->key_wq);
- w->ptr_head = w->ptr_tail = 0;
- for (j = 0; j < sizeof(w->title) - 1 && in->title[j]; j++)
- w->title[j] = in->title[j];
- w->title[j] = '\0';
- wm_focus_index(i);
- out->fb = (void *)USER_WIN_BASE;
- out->width = cw;
- out->height = ch;
- out->id = i;
- return 0;
- }
- int wm_server_win_present(const struct wm_win_present *in)
- {
- int idx = wm_window_for_pid(current_proc()->pid);
- struct window *w;
- const unsigned short *src;
- unsigned int x = 0, y = 0, rw, rh, row;
- if (idx < 0)
- return -1;
- w = &windows[idx];
- if (!w->canvas_window || !w->back || !w->canvas.px ||
- w->resize_pending)
- return -1;
- rw = w->canvas.w;
- rh = w->canvas.h;
- if (in && in->w && in->h) {
- x = in->x;
- y = in->y;
- rw = in->w;
- rh = in->h;
- if (x >= w->canvas.w || y >= w->canvas.h)
- return -1;
- if (rw > w->canvas.w - x)
- rw = w->canvas.w - x;
- if (rh > w->canvas.h - y)
- rh = w->canvas.h - y;
- }
- src = (const unsigned short *)USER_WIN_BASE;
- for (row = 0; row < rh; row++) {
- const unsigned short *s = src + (unsigned long)(y + row) *
- w->canvas.w + x;
- unsigned short *d = w->canvas.px +
- (unsigned long)(y + row) * w->canvas.w + x;
- unsigned int n = rw;
- __asm__ volatile ("cld; rep movsw"
- : "+D"(d), "+c"(n), "+S"(s)
- :
- : "memory");
- }
- if (!w->canvas_dmg_w || !w->canvas_dmg_h) {
- w->canvas_dmg_x = x;
- w->canvas_dmg_y = y;
- w->canvas_dmg_w = rw;
- w->canvas_dmg_h = rh;
- } else {
- unsigned int x1 = w->canvas_dmg_x + w->canvas_dmg_w;
- unsigned int y1 = w->canvas_dmg_y + w->canvas_dmg_h;
- if (x < w->canvas_dmg_x)
- w->canvas_dmg_x = x;
- if (y < w->canvas_dmg_y)
- w->canvas_dmg_y = y;
- if (x + rw > x1)
- x1 = x + rw;
- if (y + rh > y1)
- y1 = y + rh;
- w->canvas_dmg_w = x1 - w->canvas_dmg_x;
- w->canvas_dmg_h = y1 - w->canvas_dmg_y;
- }
- w->canvas.seq++;
- return 0;
- }
- int wm_server_win_close(void)
- {
- int idx = wm_window_for_pid(current_proc()->pid);
- int was_focused;
- if (idx < 0 || !wm_server_can_reap())
- return -1;
- was_focused = windows[idx].focused;
- 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++;
- if (was_focused)
- wm_focus_top();
- wm_request_redraw();
- return 0;
- }
- int wm_server_win_info(struct win_info *out)
- {
- int idx = wm_window_for_pid(current_proc()->pid);
- struct window *w;
- if (!out || idx < 0)
- return -1;
- w = &windows[idx];
- if (!w->canvas_window)
- return -1;
- out->fb = (void *)USER_WIN_BASE;
- out->width = w->canvas.w;
- out->height = w->canvas.h;
- out->id = (unsigned int)idx;
- return 0;
- }
- int wm_server_win_ack_resize(void)
- {
- int idx = wm_window_for_pid(current_proc()->pid);
- struct window *w;
- unsigned int i, old_pages;
- if (idx < 0)
- return -1;
- w = &windows[idx];
- if (!w->canvas_window || !w->resize_pending || !w->pending_back)
- return -1;
- old_pages = w->back_pages;
- if (w->owner_cr3)
- paging_release_user_range(w->owner_cr3, USER_WIN_BASE,
- old_pages * PAGE_SIZE, 0);
- for (i = 0; i < w->pending_pages; i++) {
- if (!paging_map_page(current_proc()->cr3,
- USER_WIN_BASE + i * PAGE_SIZE,
- (unsigned int)w->pending_back + i * PAGE_SIZE,
- PTE_USER | PTE_WRITABLE | PTE_PRESENT)) {
- /* Put the old mapping back; this is a last-resort path. */
- paging_release_user_range(current_proc()->cr3,
- USER_WIN_BASE,
- i * PAGE_SIZE, 0);
- for (i = 0; i < old_pages; i++)
- paging_map_page(current_proc()->cr3,
- USER_WIN_BASE + i * PAGE_SIZE,
- (unsigned int)w->back + i * PAGE_SIZE,
- PTE_USER | PTE_WRITABLE | PTE_PRESENT);
- for (i = 0; i < w->pending_pages; i++)
- mm_free_page((unsigned int)w->pending_back +
- i * PAGE_SIZE);
- w->pending_back = 0;
- w->pending_pages = 0;
- w->resize_pending = 0;
- return -1;
- }
- invlpg_va(USER_WIN_BASE + i * PAGE_SIZE);
- }
- for (i = 0; i < old_pages; i++)
- mm_free_page((unsigned int)w->back + i * PAGE_SIZE);
- w->back = w->pending_back;
- w->back_pages = w->pending_pages;
- w->pending_back = 0;
- w->pending_pages = 0;
- w->resize_pending = 0;
- w->canvas_dmg_w = w->canvas_dmg_h = 0;
- return 0;
- }
- int wm_server_win_pointer(struct win_pointer *out)
- {
- int idx = wm_window_for_pid(current_proc()->pid);
- struct window *w;
- unsigned int mx, my;
- int cx, cy;
- if (!out || idx < 0 || !windows[idx].canvas_window)
- return -1;
- w = &windows[idx];
- mx = mouse_get_x();
- my = mouse_get_y();
- cx = (int)mx - win_content_x(w);
- cy = (int)my - win_content_y(w);
- out->x = cx;
- out->y = cy;
- if (wm_pointer_owned) {
- out->buttons = 0;
- out->inside = 0;
- } else {
- out->buttons = mouse_buttons_state();
- out->inside = (cx >= 0 && cy >= 0 &&
- cx < (int)w->canvas.w && cy < (int)w->canvas.h &&
- (w->focused ||
- wm_window_at((int)mx, (int)my) == idx)) ?
- 1 : 0;
- }
- out->focused = w->focused ? 1 : 0;
- return 0;
- }
- int wm_server_win_event(struct win_event *out)
- {
- int idx = wm_window_for_pid(current_proc()->pid);
- struct window *w;
- if (!out || idx < 0 || !windows[idx].canvas_window)
- return -1;
- w = &windows[idx];
- if (w->ptr_tail == w->ptr_head)
- return 0;
- *out = w->ptr_events[w->ptr_tail];
- w->ptr_tail = (w->ptr_tail + 1) % 8;
- return 1;
- }
- int wm_server_win_scroll(struct win_scroll *out)
- {
- int idx = wm_window_for_pid(current_proc()->pid);
- struct window *w;
- if (!out || idx < 0 || !windows[idx].canvas_window)
- return -1;
- w = &windows[idx];
- out->dx = w->scroll_x;
- out->dy = w->scroll_y;
- w->scroll_x = w->scroll_y = 0;
- return (out->dx || out->dy) ? 1 : 0;
- }
- static int wm_resize_window(unsigned int index, unsigned int cw, unsigned int ch)
- {
- struct window *w;
- struct gfx_canvas nf;
- unsigned short *nb;
- unsigned int i, pages, pad, top;
- if (index >= MAX_WINDOWS || !windows[index].used)
- return -1;
- w = &windows[index];
- if (!w->canvas_window || !process_is_alive(w->pid))
- return -1;
- pad = w->popover ? 0 : WM_BORDER_W;
- top = w->popover ? 0 : WM_TITLE_H + WM_BORDER_W;
- if (cw < 160 || ch < 80 || cw > WM_WIN_MAX_W || ch > WM_WIN_MAX_H)
- return -1;
- if (cw * ch * 2 > WM_CONTENT_SLOT || cw * ch * 2 > USER_WIN_RANGE)
- return -1;
- if (cw + pad * 2 > vga_fb_width() || ch + top + pad > vga_fb_height())
- return -1;
- if (gfx_alloc(&nf, cw, ch) != 0)
- return -1;
- pages = (cw * ch * 2 + PAGE_SIZE - 1) / PAGE_SIZE;
- nb = (unsigned short *)mm_alloc_pages(pages);
- if (!nb) {
- gfx_free(&nf);
- return -1;
- }
- for (i = 0; i < cw * ch; i++)
- nb[i] = 0;
- wm_server_unmap_index(index);
- gfx_free(&w->canvas);
- if (w->pending_back) {
- for (i = 0; i < w->pending_pages; i++)
- mm_free_page((unsigned int)w->pending_back + i * PAGE_SIZE);
- }
- w->canvas = nf;
- w->canvas.seq = 1;
- w->canvas.drawn = 0;
- w->canvas_dmg_x = w->canvas_dmg_y = 0;
- w->canvas_dmg_w = w->canvas_dmg_h = 0;
- w->pending_back = nb;
- w->pending_pages = pages;
- w->resize_pending = 1;
- w->pw = cw + pad * 2;
- w->ph = ch + top + pad;
- w->content_gen = wm_next_content_gen();
- /* The owner is probably parked in SYS_WIN_KEY_WAIT; wake it so it
- * acknowledges the resize (and reflows its content) immediately. */
- wait_wake_one(&w->key_wq);
- return 0;
- }
- int wm_server_resize(unsigned int index, unsigned int cw, unsigned int ch)
- {
- if (!wm_server_is_wm())
- return -1;
- return wm_resize_window(index, cw, ch);
- }
- /* A canvas app resizes its own popover when it expands or collapses a
- * section. Refuse while the WM owns a frame: it may still be reading the
- * old front canvas. The app retries on its next loop. */
- int wm_server_win_resize_self(unsigned int cw, unsigned int ch)
- {
- int idx = wm_window_for_pid(current_proc()->pid);
- if (idx < 0 || wm_frame_active)
- return -1;
- return wm_resize_window((unsigned int)idx, cw, ch);
- }
- int wm_server_win_set_cursor(int shape)
- {
- int idx = wm_window_for_pid(current_proc()->pid);
- if (idx < 0 || shape < 0 || shape >= CURSOR_COUNT)
- return -1;
- windows[idx].cursor = shape;
- return 0;
- }
- int wm_server_set_cursor(int shape)
- {
- if (!wm_server_is_wm())
- return -1;
- if (shape != CURSOR_NO_OVERRIDE && (shape < 0 || shape >= CURSOR_COUNT))
- return -1;
- wm_cursor_override = shape;
- return 0;
- }
- /* Take/release the pointer for a WM drag or resize. While owned, a held
- * button is invisible to the canvas clients, so a press that started on a
- * window edge cannot also drag a scrollbar inside the window. */
- int wm_server_set_pointer_capture(int on)
- {
- if (!wm_server_is_wm())
- return -1;
- wm_pointer_owned = on ? 1 : 0;
- if (on) {
- wm_ptr_capture = -1;
- wm_ptr_buttons = mouse_buttons_state();
- }
- return 0;
- }
- /* Invisible resize border inside a normal window's outer edge. Presses here
- * belong to the ring-3 WM's resize path, not to the content client. Only the
- * WM_RESIZE_IN part of the band lies inside the window; the rest is outside
- * the rectangle and never reaches the content anyway. */
- static int win_in_resize_border(const struct window *w, unsigned int mx,
- unsigned int my)
- {
- if (w->popover)
- return 0;
- return (int)mx < w->x + WM_RESIZE_IN ||
- (int)mx >= w->x + (int)w->pw - WM_RESIZE_IN ||
- (int)my < w->y + WM_RESIZE_IN ||
- (int)my >= w->y + (int)w->ph - WM_RESIZE_IN;
- }
- /* The shape for the pointer at (mx,my): the WM's override, else the
- * content cursor of the window that holds the pointer capture (a drag that
- * started in the content keeps its cursor), else of the window whose
- * content is under the pointer (not on a normal window's resize border),
- * else the default arrow (title bars, the desktop, the top bar). */
- static int wm_cursor_at(unsigned int mx, unsigned int my)
- {
- int idx;
- struct window *w;
- int cx, cy;
- int edges = 0;
- if (wm_cursor_override != CURSOR_NO_OVERRIDE)
- return wm_cursor_override;
- if (wm_ptr_capture >= 0 && windows[wm_ptr_capture].used &&
- windows[wm_ptr_capture].canvas_window)
- return windows[wm_ptr_capture].cursor;
- idx = wm_resize_target((int)mx, (int)my, &edges);
- if (idx < 0 || edges)
- return CURSOR_DEFAULT;
- w = &windows[idx];
- if (!w->canvas_window)
- return CURSOR_DEFAULT;
- cx = (int)mx - win_content_x(w);
- cy = (int)my - win_content_y(w);
- if (cx < 0 || cy < 0 || cx >= (int)w->canvas.w ||
- cy >= (int)w->canvas.h)
- return CURSOR_DEFAULT;
- if (win_in_resize_border(w, mx, my))
- return CURSOR_DEFAULT;
- return w->cursor;
- }
- int wm_server_key_poll(void)
- {
- int idx = wm_window_for_pid(current_proc()->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;
- }
- /* Block until a key arrives for the caller's window, or `timeout_ms` passes
- * (0 = wait forever). Returns the key, 0 on timeout/no key, -1 if the caller
- * has no window. */
- int wm_server_key_wait(unsigned int timeout_ms)
- {
- int idx = wm_window_for_pid(current_proc()->pid);
- struct window *w;
- int k;
- if (idx < 0)
- return -1;
- w = &windows[idx];
- k = wm_server_key_poll();
- if (k)
- return k;
- if (timeout_ms)
- sched_block_timeout(&w->key_wq, timeout_ms);
- else
- sched_block_on(&w->key_wq);
- return wm_server_key_poll();
- }
- int wm_server_attach(struct wm_screen_info *out)
- {
- struct surface *s;
- unsigned int pages, i, bytes;
- if (!out || !wm_expected_pid ||
- current_proc()->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, current_proc()->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 = current_proc()->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 = current_proc()->pid;
- wm_attached = 1;
- wm_frame_active = 0;
- wm_boot_logged = 0;
- wm_last_seen = get_ticks_ms();
- mouse_set_auto_draw(1);
- wm_full_redraw = 1;
- /* The WM owns the display from its first frame on; stop the boot
- * console before a later boot line can draw over the desktop. */
- bootcon_set_enabled(0);
- 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;
- 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->focused = w->focused ? 1 : 0;
- out->pid = w->pid;
- out->popover = w->popover ? 1 : 0;
- out->x = (int)w->x;
- out->y = (int)w->y;
- out->w = w->pw;
- out->h = w->ph;
- out->dirty = 0;
- out->dirty_seq = 0;
- out->dmg_x = out->dmg_y = out->dmg_w = out->dmg_h = 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)
- return 1;
- g = &w->canvas;
- out->content_w = g->w;
- out->content_h = g->h;
- out->dirty = (g->seq != g->drawn);
- out->dirty_seq = g->seq;
- if (out->dirty && w->canvas_dmg_w && w->canvas_dmg_h) {
- out->dmg_x = w->canvas_dmg_x;
- out->dmg_y = w->canvas_dmg_y;
- out->dmg_w = w->canvas_dmg_w;
- out->dmg_h = w->canvas_dmg_h;
- } else if (out->dirty) {
- out->dmg_w = g->w;
- out->dmg_h = g->h;
- }
- return 1;
- }
- int wm_server_map(unsigned int index, struct wm_map_info *out)
- {
- struct window *w;
- struct gfx_canvas *g = 0;
- const void *src;
- unsigned int pages, i, va, stride;
- if (!wm_server_is_wm() || !out || index >= MAX_WINDOWS || !wm_cr3)
- return -1;
- w = &windows[index];
- if (!w->used || !w->canvas_window)
- return -1;
- g = &w->canvas;
- src = (const void *)g->px;
- if (!src)
- return -1;
- stride = g->w * 2;
- if (w->mapped_kind == WM_KIND_GFX && w->mapped_gen == w->content_gen &&
- w->mapped_src == src) {
- out->va = (void *)(WM_CONTENT_VA_BASE + index * WM_CONTENT_SLOT);
- out->kind = WM_KIND_GFX;
- out->generation = w->content_gen;
- out->w = g->w;
- out->h = g->h;
- out->stride = stride;
- return 0;
- }
- wm_server_unmap_index(index);
- pages = (g->w * g->h * 2 + PAGE_SIZE - 1) / PAGE_SIZE;
- if (pages * PAGE_SIZE > WM_CONTENT_SLOT)
- return -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 = WM_KIND_GFX;
- w->mapped_gen = w->content_gen;
- w->mapped_pages = pages;
- w->mapped_src = src;
- out->va = (void *)va;
- out->kind = WM_KIND_GFX;
- out->w = g->w;
- out->h = g->h;
- out->stride = stride;
- out->generation = w->content_gen;
- return 0;
- }
- int wm_server_ack(unsigned int index, unsigned int seq)
- {
- struct window *w;
- if (!wm_server_is_wm() || index >= MAX_WINDOWS)
- return -1;
- w = &windows[index];
- if (!w->used || !w->canvas_window)
- return -1;
- if (seq == w->canvas.seq) {
- w->canvas.drawn = seq;
- w->canvas_dmg_w = w->canvas_dmg_h = 0;
- }
- 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_spawn_terminal(void)
- {
- struct vfs_file f;
- struct vfs_mount *m = 0;
- unsigned int flags;
- int pid;
- if (vfs_load_file("/ramfs/bin/term", &f, &m) != 0) {
- serial_puts(SERIAL_COM1, "wm: /ramfs/bin/term not found\n");
- return -1;
- }
- 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) {
- serial_puts(SERIAL_COM1, "wm: cannot start ring-3 term\n");
- return -1;
- }
- process_set_weight((unsigned int)pid, 12);
- wm_request_redraw();
- return 0;
- }
- int wm_server_create_term(void)
- {
- if (!wm_server_is_wm())
- return -1;
- return wm_spawn_terminal();
- }
- 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;
- }
- static void wm_queue_ptr_event(struct window *w, unsigned int type,
- int x, int y, unsigned int buttons,
- unsigned int button)
- {
- unsigned int next = (w->ptr_head + 1) % 8;
- if (next == w->ptr_tail)
- return; /* full: drop the event, not the app */
- w->ptr_events[w->ptr_head].type = type;
- w->ptr_events[w->ptr_head].x = x;
- w->ptr_events[w->ptr_head].y = y;
- w->ptr_events[w->ptr_head].buttons = buttons;
- w->ptr_events[w->ptr_head].button = button;
- w->ptr_head = next;
- /* Wake a window blocked in SYS_WIN_KEY_WAIT so a click (e.g. on the
- * terminal's scrollbar) is handled now, not on the next poll timeout. */
- wait_wake_one(&w->key_wq);
- }
- /* Turn the sampled button state into content-relative press/release events for
- * the topmost canvas window. The kernel WM still owns focus, drag and resize;
- * only clicks that land in the content area are forwarded. */
- static void wm_route_pointer(unsigned int mx, unsigned int my,
- unsigned int buttons)
- {
- unsigned int press = buttons & ~wm_ptr_buttons;
- unsigned int release = wm_ptr_buttons & ~buttons;
- if (wm_pointer_owned) {
- wm_ptr_buttons = buttons;
- return;
- }
- if (press & 3) {
- int idx = wm_ptr_capture;
- if (idx < 0) {
- int edges = 0;
- idx = wm_resize_target((int)mx, (int)my, &edges);
- if (edges)
- idx = -1;
- }
- if (idx >= 0 && idx < (int)MAX_WINDOWS) {
- struct window *w = &windows[idx];
- int cx = (int)mx - win_content_x(w);
- int cy = (int)my - win_content_y(w);
- /* A second button pressed after a capture may leave the
- * content, but still belongs to the window that owns the
- * pointer. */
- if (w->used && w->canvas_window &&
- (wm_ptr_capture == idx ||
- (cx >= 0 && cy >= 0 && cx < (int)w->canvas.w &&
- cy < (int)w->canvas.h &&
- (w->popover || !win_in_resize_border(w, mx, my))))) {
- wm_queue_ptr_event(w, WIN_EV_POINTER_DOWN,
- cx, cy, buttons, press & 3);
- wm_ptr_capture = idx;
- }
- }
- }
- if ((release & 3) && wm_ptr_capture >= 0) {
- struct window *w = &windows[wm_ptr_capture];
- if (w->used && w->canvas_window) {
- int cx = (int)mx - win_content_x(w);
- int cy = (int)my - win_content_y(w);
- wm_queue_ptr_event(w, WIN_EV_POINTER_UP,
- cx, cy, buttons, release & 3);
- }
- if (!(buttons & 3))
- wm_ptr_capture = -1;
- }
- wm_ptr_buttons = buttons;
- }
- /* Route wheel/touchpad scroll to the canvas window under the pointer. The
- * accumulator is drained by SYS_WIN_SCROLL, so a slow client can catch up. */
- static void wm_route_scroll(unsigned int mx, unsigned int my)
- {
- int sdx, sdy;
- int edges = 0;
- int idx = wm_resize_target((int)mx, (int)my, &edges);
- struct window *w;
- int cx, cy;
- mouse_scroll_take(&sdx, &sdy);
- if (wm_pointer_owned)
- return;
- if (edges || (!sdx && !sdy) || idx < 0)
- return;
- w = &windows[idx];
- if (!w->canvas_window)
- return;
- cx = (int)mx - win_content_x(w);
- cy = (int)my - win_content_y(w);
- if (cx < 0 || cy < 0 || cx >= (int)w->canvas.w ||
- cy >= (int)w->canvas.h)
- return;
- w->scroll_x += sdx;
- w->scroll_y += sdy;
- if (w->scroll_x > 100000)
- w->scroll_x = 100000;
- else if (w->scroll_x < -100000)
- w->scroll_x = -100000;
- if (w->scroll_y > 100000)
- w->scroll_y = 100000;
- else if (w->scroll_y < -100000)
- w->scroll_y = -100000;
- /* Wake clients blocked in SYS_WIN_KEY_WAIT so they drain the scroll. */
- wait_wake_one(&w->key_wq);
- }
- 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();
- wm_route_pointer(out->mouse.x, out->mouse.y, out->mouse.buttons);
- wm_route_scroll(out->mouse.x, out->mouse.y);
- mouse_set_shape(wm_cursor_at(out->mouse.x, out->mouse.y));
- out->full_redraw = wm_full_redraw ? 1 : 0;
- out->boot_ready = wm_boot_ready ? 1 : 0;
- out->kexec_busy = kexec_busy ? 1 : 0;
- out->kexec_progress = kexec_progress;
- out->kexec_total = kexec_total;
- {
- int level = backlight_percent();
- out->backlight_present = level >= 0 ? 1 : 0;
- out->backlight_level = level > 0 ? (unsigned int)level : 0;
- out->backlight_seq = backlight_seq();
- }
- out->wallpaper_seq = wm_wallpaper_seq;
- wm_full_redraw = 0;
- return 0;
- }
- void wm_server_boot_ready(void)
- {
- wm_boot_ready = 1;
- }
- /* SYS_WM_WALLPAPER: a client (the image viewer) wrote a new wallpaper file.
- * Bumping the sequence makes the WM reload it and repaint the desktop. */
- void wm_server_wallpaper_changed(void)
- {
- wm_wallpaper_seq++;
- }
- 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);
- if (!wm_first_frame) {
- wm_first_frame = 1;
- serial_puts(SERIAL_COM1, "wm: first frame\n");
- }
- return 0;
- }
- int wm_server_sync(void)
- {
- if (wm_frame_active)
- return -1;
- 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;
- wm_ptr_buttons = 0;
- wm_ptr_capture = -1;
- wm_cursor_override = CURSOR_NO_OVERRIDE;
- wm_pointer_owned = 0;
- mouse_set_shape(CURSOR_DEFAULT);
- 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) {
- if (!wm_boot_logged) {
- wm_boot_logged = 1;
- serial_puts(SERIAL_COM1, "wm: /ramfs/bin/wm not found\n");
- bootcon_puts("wm: /ramfs/bin/wm not found\n");
- }
- return;
- }
- 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) {
- if (!wm_boot_logged) {
- wm_boot_logged = 1;
- serial_puts(SERIAL_COM1, "wm: cannot load /ramfs/bin/wm\n");
- bootcon_puts("wm: cannot load /ramfs/bin/wm\n");
- }
- return;
- }
- process_set_weight((unsigned int)pid, 16);
- wm_boot_logged = 0;
- serial_puts(SERIAL_COM1, "wm: ring-3 WM started\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) &&
- !process_is_sleeping(wm_expected_pid)) {
- /* A sleeping WM is usually blocked on the BKL or its normal
- * 10 ms poll sleep; do not treat a long disk scan as a hang. */
- if ((wm_attached && now - wm_last_seen > 15000) ||
- (!wm_attached && now - wm_last_start > 10000)) {
- 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;
- for (i = 0; i < MAX_WINDOWS; i++) {
- windows[i].used = 0;
- 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;
- wm_boot_ready = 0;
- wm_boot_logged = 0;
- wm_ptr_buttons = 0;
- wm_ptr_capture = -1;
- wm_expected_pid = 0;
- wm_pid = 0;
- wm_cr3 = 0;
- wm_attached = 0;
- wm_frame_active = 0;
- wm_first_frame = 0;
- /* The ring-3 WM spawns the first terminal once wm_server_boot_ready()
- * says the storage scan is done. */
- }
- static int wm_point_in_rect(int px, int py,
- int rx, int ry, unsigned int rw, unsigned int rh)
- {
- return px >= rx && px < rx + (int)rw &&
- py >= ry && py < ry + (int)rh;
- }
- 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++) {
- int now = windows[i].used && (i == idx);
- if (!windows[i].used || windows[i].focused == now)
- continue;
- windows[i].focused = now;
- }
- wm_request_redraw();
- }
- static void wm_focus_top(void)
- {
- unsigned int i;
- /* With no explicit z-order the highest used slot is painted last, so
- * it is the topmost window left after a close. */
- for (i = MAX_WINDOWS; i-- > 0; )
- if (windows[i].used) {
- wm_focus_index(i);
- return;
- }
- }
- static void wm_close_window(unsigned int idx)
- {
- struct window *w = &windows[idx];
- int was_focused = w->focused;
- if (wm_ptr_capture == (int)idx)
- wm_ptr_capture = -1;
- wait_wake_all(&w->key_wq);
- 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;
- if (was_focused)
- wm_focus_top();
- wm_request_redraw();
- }
- /* Topmost window containing (px,py), or -1. A popover overrides the normal
- * focus/slot order: it is an overlay panel and always receives the click. */
- static int wm_window_at(int px, int py)
- {
- unsigned int i;
- for (i = 0; i < MAX_WINDOWS; i++)
- if (windows[i].used && windows[i].popover &&
- 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;
- /* Inactive windows are painted in slot order, so the highest slot is
- * the topmost one under the cursor. */
- for (i = MAX_WINDOWS; i-- > 0; )
- 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;
- }
- /* Resize band of a normal window: WM_RESIZE_IN px inside and WM_RESIZE_OUT
- * px outside every edge. Mirrors the ring-3 WM's resize_edges_at(). */
- static int wm_in_resize_band(const struct window *w, int mx, int my)
- {
- int l = w->x, t = w->y;
- int r = l + (int)w->pw, b = t + (int)w->ph;
- int near_x, near_y;
- if (w->popover || my < WM_BAR_H)
- return 0;
- near_x = mx >= l - WM_RESIZE_OUT && mx < r + WM_RESIZE_OUT;
- near_y = my >= t - WM_RESIZE_OUT && my < b + WM_RESIZE_OUT;
- return (near_y && mx >= l - WM_RESIZE_OUT && mx < l + WM_RESIZE_IN) ||
- (near_y && mx >= r - WM_RESIZE_IN && mx < r + WM_RESIZE_OUT) ||
- (near_x && my >= t - WM_RESIZE_OUT && my < t + WM_RESIZE_IN) ||
- (near_x && my >= b - WM_RESIZE_IN && my < b + WM_RESIZE_OUT);
- }
- static int wm_hit_one(struct window *w, int mx, int my, int *edges)
- {
- *edges = 0;
- if (!w->used)
- return 0;
- if (wm_in_resize_band(w, mx, my)) {
- *edges = 1;
- return 1;
- }
- return wm_point_in_rect(mx, my, w->x, w->y, w->pw, w->ph);
- }
- /* Topmost window for a pointer position, in the WM's painting order. A
- * resize band wins over a plain rectangle hit, so a front window's outside
- * band is not stolen by a window behind it. *edges is 1 for a band hit. */
- static int wm_resize_target(int px, int py, int *edges)
- {
- unsigned int i;
- *edges = 0;
- for (i = 0; i < MAX_WINDOWS; i++)
- if (windows[i].popover &&
- wm_hit_one(&windows[i], px, py, edges))
- return (int)i;
- for (i = 0; i < MAX_WINDOWS; i++)
- if (windows[i].focused && !windows[i].popover &&
- wm_hit_one(&windows[i], px, py, edges))
- return (int)i;
- for (i = MAX_WINDOWS; i-- > 0;)
- if (!windows[i].focused && !windows[i].popover &&
- wm_hit_one(&windows[i], px, py, edges))
- return (int)i;
- return -1;
- }
- static void wm_clamp_window(struct window *w, int nx, int ny)
- {
- int fbw = (int)vga_fb_width();
- int fbh = (int)vga_fb_height();
- /* Allow a window to hang off the left, right and bottom edges, but
- * keep a strip of the title bar reachable and never let it overlap the
- * system bar at the top. */
- int minx = WM_DRAG_VISIBLE - (int)w->pw;
- int maxx = fbw - WM_DRAG_VISIBLE;
- int miny = WM_BAR_H;
- int maxy = fbh - WM_BORDER_W - WM_TITLE_H;
- if (w->popover) {
- /* A popover is an overlay panel: keep all of it on screen,
- * flush under the bar and against the right edge. */
- minx = 0;
- maxx = fbw - (int)w->pw;
- miny = WM_BAR_H;
- maxy = fbh - (int)w->ph;
- if (minx > maxx)
- minx = maxx = 0;
- if (miny > maxy)
- miny = maxy = WM_BAR_H + 2;
- if (nx < minx)
- nx = minx;
- else if (nx > maxx)
- nx = maxx;
- if (ny < miny)
- ny = miny;
- else if (ny > maxy)
- ny = maxy;
- w->x = nx;
- w->y = ny;
- return;
- }
- if (minx > maxx)
- minx = maxx = 0;
- if (miny > maxy)
- miny = maxy = WM_BAR_H;
- if (nx < minx)
- nx = minx;
- else if (nx > maxx)
- nx = maxx;
- if (ny < miny)
- ny = miny;
- else if (ny > maxy)
- ny = maxy;
- w->x = nx;
- w->y = ny;
- }
- /* Fill the intersection of rect (rx,ry,rw,rh) with clip rect (cx,cy,cw,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). */
- /* 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). */
- 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->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;
- }
- wait_wake_one(&fw->key_wq);
- }
- }
- }
- 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 KXE2). */
- 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;
- }
- }
|