Procházet zdrojové kódy

Add ngui toolkit and graphical file manager

Johannes Zellner před 2 týdny
rodič
revize
ad25453bd4
16 změnil soubory, kde provedl 2437 přidání a 12 odebrání
  1. 15 5
      AGENTS.md
  2. 14 2
      Makefile
  3. 5 0
      io.c
  4. 10 5
      io.h
  5. 929 0
      ngui.c
  6. 175 0
      ngui.h
  7. 28 0
      sys_wm.c
  8. 2 0
      sys_wm.h
  9. 4 0
      syscall.c
  10. 2 0
      syscall.h
  11. 1098 0
      ucmd_files.c
  12. 10 0
      ucmd_lib.h
  13. 8 0
      ucmd_wm.c
  14. 110 0
      wm.c
  15. 5 0
      wm.h
  16. 22 0
      wm_abi.h

+ 15 - 5
AGENTS.md

@@ -125,8 +125,9 @@ below the run area), so adding code no longer moves any address.
   trampoline (`kexec_tramp.S`) clears CR0.PG before the copy so it can overwrite
   the old kernel's page tables safely.
 - **Window manager**: `wm.c/h` — slot 0 runs `wm_run()`; fixed-size draggable
-  terminal windows (64×32) with a system bar (banner + purple "+ New" button +
-  clock + rainbow). Click-to-focus, drag-by-title-bar, close (×). The "+ New"
+  terminal windows (64×32) with a system bar (banner + purple "+ New", "+ Clock",
+  "+ Colors" and "Files" buttons + clock + rainbow). Click-to-focus,
+  drag-by-title-bar, close (×). The "+ New"
   button and first boot spawn terminals; there is no `term` command.  Content is
   repainted per damage rect (`wm_redraw_rect`: desktop fill + every intersecting
   window back-to-front, clipped by `wm_draw_window_clip`), not by full redraws —
@@ -142,7 +143,13 @@ below the run area), so adding code no longer moves any address.
   Keys of a window with a live session go to the session (`SYS_KEY_POLL`; Ctrl+C
   still reaches the terminal), closing a window kills its session, and
   `sys_gfx_reap`/`sys_gfx_collect` free the canvas after exit/crash.  `clock`
-  and `colors` are its users.
+  and `colors` are its users.  Generic canvas windows (`SYS_WIN_CREATE`) also
+  get pointer input: `SYS_WIN_POINTER` reports the content-relative position,
+  buttons and focus, and `SYS_WIN_EVENT` queues press/release edges with a
+  capture window, so a click that starts in a window still gets its release
+  after the pointer leaves.  The userland `ngui.c/h` immediate-mode toolkit
+  builds on those: clipped RGB565 primitives, buttons, text input, procedural
+  icons and scrolling panes.  `files` (`ucmd_files.c`) is its first user.
 - **Graphics**: `vga.c/h`, `vga_copy.S`, `blit.c/h`, `gpu.c`, `ati.c`, `nvidia.c`,
   `nv50.c`, `pci.h` — VESA LFB + shadow buffer.  Drawing goes into a
   `struct surface` render target: `vga_target()` is the screen's shadow buffer
@@ -320,7 +327,10 @@ below the run area), so adding code no longer moves any address.
   syscalls resolve relative paths against it, so commands keep working after
   `cd` (the shell passes arguments verbatim).  The one argument is passed as a
   C string on the top of the user stack (`process_create_user(..., arg)`), so
-  `_start(const char *arg)` needs no crt0.
+  `_start(const char *arg)` needs no crt0.  `ngui.c/h` is the shared userland
+  GUI toolkit linked into `ucmd_files.c` (buttons, text fields, icons, layout
+  cuts and scroll panes); `files` opens the selected text file through
+  `/ramfs/bin/edit`.
 - **Network service**: `sys_net.c/h` — the NIC and the stack stay in the
   kernel; ring-3 commands get `SYS_NET_INFO`/config, the DNS and DHCP clients
   and the raw frame paths (`NET_TX/RX/DECODE/REPLY/LOOPBACK/REGS/RESET`), so
@@ -372,7 +382,7 @@ nested). `/` is the read-only VFS root listing the mounted filesystems (the
 filesystem commands operate inside a mount, e.g. `cat /floppy1/HELLO.TXT`).
 
 `cpuinfo`, `ls`, `cat`, `save`, `rm`, `mkdir`, `rmdir`, `meminfo`, `top`, `spin`,
-`clock`, `colors`, `mouseinfo`, `storage`, `netinfo`, `netip`, `netdns`, `host`, `dhcp`, `ping`,
+`clock`, `colors`, `files`, `mouseinfo`, `storage`, `netinfo`, `netip`, `netdns`, `host`, `dhcp`, `ping`,
 `pong`, `netsend`, `netrecv`, `netlog`, `netconsole`, `netregs`, `netloop`,
 `netreset`, `curl` and `hello` are files in `/ramfs/bin` (packed by the
 Makefile), found through the shell's PATH; the rest are kernel commands

+ 14 - 2
Makefile

@@ -379,7 +379,8 @@ UCMDS = ucmd_cpuinfo ucmd_ls ucmd_cat ucmd_save ucmd_rm ucmd_mkdir ucmd_rmdir \
         ucmd_clock ucmd_colors ucmd_edit ucmd_wm ucmd_term ucmd_spin \
         ucmd_netinfo ucmd_netip ucmd_netdns ucmd_host ucmd_dhcp \
         ucmd_netsend ucmd_netrecv ucmd_ping ucmd_pong ucmd_netlog \
-        ucmd_netconsole ucmd_netregs ucmd_netloop ucmd_netreset ucmd_curl
+        ucmd_netconsole ucmd_netregs ucmd_netloop ucmd_netreset ucmd_curl \
+        ucmd_files
 
 # Keep the linked ELFs around (make would treat them as disposable
 # intermediates between the pattern rules).
@@ -418,6 +419,17 @@ ucmd_term.elf: ucmd_term.o uwm_lib.o font_8x16.o ucmd.ld
 	$(LD) -T ucmd.ld -z max-page-size=0x100 -nostdlib -m elf_i386 -o $@ $(filter %.o,$^)
 	$(OBJCOPY) --strip-all $@ $@
 
+# ngui: the userland immediate-mode GUI toolkit and its first app.
+ngui.o: ngui.c ngui.h ucmd_lib.h syscall.h wm_abi.h font_8x16.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+ucmd_files.o: ucmd_files.c ucmd_lib.h syscall.h wm_abi.h ngui.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+ucmd_files.elf: ucmd_files.o ngui.o font_8x16.o ucmd.ld
+	$(LD) -T ucmd.ld -z max-page-size=0x100 -nostdlib -m elf_i386 -o $@ $(filter %.o,$^)
+	$(OBJCOPY) --strip-all $@ $@
+
 # TLS 1.3 client library (ring-3, linked into ucmd_curl; see tls.h).  The
 # crypto files are pure freestanding C, so tls_test can exercise them on
 # the host (make test-crypto).
@@ -657,4 +669,4 @@ net-dev: kernel.bin netkexec_send
 	./netkexec_send $(BOARD_IP) $(NETKEXEC_PORT) 0x$$entry kernel.bin
 
 clean:
-	rm -f boot.o vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o blit.o cpu.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o font_8x16.o fbtext.o gfx.o sys_gfx.o display.o mouse.o uhci.o ohci.o usb.o usbhid.o lsusb.o panic.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o acpi.o lapic.o smp.o ap_tramp.o ap_tramp.elf ap_tramp.bin ap_tramp_embed.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin hello.elf hello_embed.o prog_embed.o space_embed.o cmds.pack cmds_pack.o ramfs.o payload.o mkpack uwm_lib.o *.boot.tmp $(addsuffix .o,$(UCMDS)) $(addsuffix .elf,$(UCMDS)) $(TLSLIB_OBJS) tls_test bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor
+	rm -f boot.o vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o blit.o cpu.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o font_8x16.o fbtext.o gfx.o sys_gfx.o display.o mouse.o uhci.o ohci.o usb.o usbhid.o lsusb.o panic.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o acpi.o lapic.o smp.o ap_tramp.o ap_tramp.elf ap_tramp.bin ap_tramp_embed.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin hello.elf hello_embed.o prog_embed.o space_embed.o cmds.pack cmds_pack.o ramfs.o payload.o mkpack uwm_lib.o ngui.o *.boot.tmp $(addsuffix .o,$(UCMDS)) $(addsuffix .elf,$(UCMDS)) $(TLSLIB_OBJS) tls_test bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor

+ 5 - 0
io.c

@@ -306,6 +306,11 @@ static int kbd_process_byte(unsigned char sc)
 		if (sc == 0x4D) return IO_KEY_RIGHT;
 		if (sc == 0x48) return IO_KEY_UP;
 		if (sc == 0x50) return IO_KEY_DOWN;
+		if (sc == 0x49) return IO_KEY_PAGE_UP;
+		if (sc == 0x51) return IO_KEY_PAGE_DOWN;
+		if (sc == 0x47) return IO_KEY_HOME;
+		if (sc == 0x4F) return IO_KEY_END;
+		if (sc == 0x53) return IO_KEY_DELETE;
 		return 0;
 	}
 	if (sc & 0x80) {

+ 10 - 5
io.h

@@ -28,12 +28,17 @@ void io_cursor_hide_block(void);
 void io_vga_write(unsigned int pos, unsigned short value);
 void io_drain_keyboard(void);
 
-/* Poll keyboard; returns 0 = no key, 1..255 = ASCII, or IO_KEY_LEFT/IO_KEY_RIGHT/IO_KEY_UP/IO_KEY_DOWN. */
+/* Poll keyboard; returns 0 = no key, 1..255 = ASCII, or an IO_KEY_* code. */
 #define IO_KEY_CTRL_C 0x03  /* Ctrl+C (ASCII ETX), kills the foreground job */
-#define IO_KEY_LEFT   0x100
-#define IO_KEY_RIGHT  0x101
-#define IO_KEY_UP     0x102
-#define IO_KEY_DOWN   0x103
+#define IO_KEY_LEFT      0x100
+#define IO_KEY_RIGHT     0x101
+#define IO_KEY_UP        0x102
+#define IO_KEY_DOWN      0x103
+#define IO_KEY_PAGE_UP   0x104
+#define IO_KEY_PAGE_DOWN 0x105
+#define IO_KEY_HOME      0x106
+#define IO_KEY_END       0x107
+#define IO_KEY_DELETE    0x108
 int io_poll_key(void);
 /* Same as io_poll_key but also reads port 0x60 when buffer empty (for full-screen programs). */
 int io_poll_key_with_hw(void);

+ 929 - 0
ngui.c

@@ -0,0 +1,929 @@
+/* ngui: immediate-mode widgets for RGB565 canvas windows.  See ngui.h. */
+
+#include "ngui.h"
+#include "ucmd_lib.h"
+#include "font_8x16.h"
+
+const struct ngui_theme ngui_theme_dark = {
+	.bg          = NGUI_RGB(24, 28, 40),
+	.panel       = NGUI_RGB(38, 43, 58),
+	.panel_alt   = NGUI_RGB(50, 56, 74),
+	.border      = NGUI_RGB(92, 99, 124),
+	.text        = NGUI_RGB(228, 230, 240),
+	.text_dim    = NGUI_RGB(145, 151, 170),
+	.accent      = NGUI_RGB(84, 132, 224),
+	.selection   = NGUI_RGB(54, 92, 166),
+	.button      = NGUI_RGB(54, 61, 80),
+	.button_hot  = NGUI_RGB(70, 81, 108),
+	.button_down = NGUI_RGB(48, 112, 204),
+	.icon_folder = NGUI_RGB(238, 198, 88),
+	.icon_file   = NGUI_RGB(204, 210, 226),
+	.icon_disk   = NGUI_RGB(112, 174, 232),
+	.error       = NGUI_RGB(238, 116, 116),
+};
+
+static unsigned int slen(const char *s)
+{
+	unsigned int n = 0;
+
+	while (s && s[n])
+		n++;
+	return n;
+}
+
+static void scopy(char *dst, const char *src, unsigned int cap)
+{
+	unsigned int i = 0;
+
+	if (!cap)
+		return;
+	while (src && src[i] && i + 1 < cap) {
+		dst[i] = src[i];
+		i++;
+	}
+	dst[i] = '\0';
+}
+
+static int rect_empty(const struct ngui_rect *r)
+{
+	return r->w <= 0 || r->h <= 0;
+}
+
+static int rect_intersect(const struct ngui_rect *a, const struct ngui_rect *b,
+			  struct ngui_rect *out)
+{
+	int x0 = a->x > b->x ? a->x : b->x;
+	int y0 = a->y > b->y ? a->y : b->y;
+	int x1 = a->x + a->w < b->x + b->w ? a->x + a->w : b->x + b->w;
+	int y1 = a->y + a->h < b->y + b->h ? a->y + a->h : b->y + b->h;
+
+	out->x = x0;
+	out->y = y0;
+	out->w = x1 - x0;
+	out->h = y1 - y0;
+	return !rect_empty(out);
+}
+
+static struct ngui_rect rect_make(int x, int y, int w, int h)
+{
+	struct ngui_rect r;
+
+	r.x = x;
+	r.y = y;
+	r.w = w;
+	r.h = h;
+	return r;
+}
+
+int ngui_rect_contains(const struct ngui_rect *r, int x, int y)
+{
+	return x >= r->x && y >= r->y && x < r->x + r->w && y < r->y + r->h;
+}
+
+void ngui_init(struct ngui *ui, unsigned short *fb, int w, int h)
+{
+	unsigned int i;
+
+	for (i = 0; i < sizeof(*ui); i++)
+		((unsigned char *)ui)[i] = 0;
+	ui->px = fb;
+	ui->w = w;
+	ui->h = h;
+	ui->theme = ngui_theme_dark;
+	ui->nclip = 0;
+	ui->dirty = 1;
+	ui->caret_on = 1;
+	ui->last_blink = u_ticks_ms();
+	ui->last_mouse_x = -1;
+	ui->last_mouse_y = -1;
+}
+
+void ngui_invalidate(struct ngui *ui)
+{
+	ui->dirty = 1;
+}
+
+void ngui_set_focus(struct ngui *ui, unsigned int id)
+{
+	if (ui->focus != id) {
+		ui->focus = id;
+		ui->last_blink = u_ticks_ms();
+		ui->caret_on = 1;
+		ui->dirty = 1;
+	}
+}
+
+unsigned int ngui_focus(struct ngui *ui)
+{
+	return ui->focus;
+}
+
+static void key_push(struct ngui *ui, int k)
+{
+	unsigned int next = (ui->key_head + 1) % 32;
+
+	if (next == ui->key_tail)
+		return;
+	ui->keys[ui->key_head] = k;
+	ui->key_head = next;
+}
+
+int ngui_key_peek(struct ngui *ui)
+{
+	if (ui->key_tail == ui->key_head)
+		return 0;
+	return ui->keys[ui->key_tail];
+}
+
+int ngui_key(struct ngui *ui)
+{
+	int k = ngui_key_peek(ui);
+
+	if (!k)
+		return 0;
+	ui->key_tail = (ui->key_tail + 1) % 32;
+	return k;
+}
+
+int ngui_begin(struct ngui *ui)
+{
+	struct win_info wi;
+	struct win_pointer wp;
+	struct win_event ev;
+	unsigned int now;
+	int want = ui->dirty;
+	int i, k;
+
+	ui->dirty = 0;
+	ui->pressed = ui->released = 0;
+	ui->clicked = 0;
+	ui->focus_claimed = 0;
+	ui->hot = 0;
+
+	if (u_win_info(&wi) == 0) {
+		if (wi.width != (unsigned int)ui->w ||
+		    wi.height != (unsigned int)ui->h) {
+			if (u_win_ack_resize() == 0) {
+				ui->px = (unsigned short *)wi.fb;
+				ui->w = (int)wi.width;
+				ui->h = (int)wi.height;
+			}
+			want = 1;
+		} else if (wi.fb && wi.fb != (void *)ui->px) {
+			ui->px = (unsigned short *)wi.fb;
+		}
+	}
+
+	if (u_win_pointer(&wp) == 0) {
+		ui->mouse_x = wp.x;
+		ui->mouse_y = wp.y;
+		ui->buttons = wp.buttons;
+		ui->inside = wp.inside ? 1 : 0;
+		ui->focused = wp.focused ? 1 : 0;
+	} else {
+		ui->mouse_x = ui->mouse_y = 0;
+		ui->buttons = 0;
+		ui->inside = 0;
+		ui->focused = 0;
+	}
+
+	for (i = 0; i < 16; i++) {
+		int r = u_win_event(&ev);
+
+		if (r != 1)
+			break;
+		if (ev.type == WIN_EV_POINTER_DOWN) {
+			ui->pressed |= 1;
+			ui->press_x = ev.x;
+			ui->press_y = ev.y;
+			ui->clicked = 1;
+		} else if (ev.type == WIN_EV_POINTER_UP) {
+			ui->released |= 1;
+			ui->release_x = ev.x;
+			ui->release_y = ev.y;
+		}
+	}
+
+	for (i = 0; i < 32; i++) {
+		k = u_key_poll();
+		if (!k)
+			break;
+		key_push(ui, k);
+		want = 1;
+	}
+
+	now = u_ticks_ms();
+	if (ui->focus) {
+		if (now - ui->last_blink >= 500) {
+			ui->last_blink = now;
+			ui->caret_on = !ui->caret_on;
+			want = 1;
+		}
+	} else {
+		ui->caret_on = 1;
+	}
+
+	if (ui->inside != ui->last_inside) {
+		want = 1;
+	} else if (ui->inside &&
+		   (ui->mouse_x != ui->last_mouse_x ||
+		    ui->mouse_y != ui->last_mouse_y)) {
+		want = 1;
+	}
+	if (ui->focused != ui->last_focused)
+		want = 1;
+	ui->last_mouse_x = ui->mouse_x;
+	ui->last_mouse_y = ui->mouse_y;
+	ui->last_inside = ui->inside;
+	ui->last_focused = ui->focused;
+
+	if (ui->pressed || ui->released)
+		want = 1;
+	return want;
+}
+
+void ngui_end(struct ngui *ui)
+{
+	if (u_win_present() != 0)
+		ui->dirty = 1;
+
+	if (ui->clicked && !ui->focus_claimed)
+		ui->focus = 0;
+	if (ui->released || !ui->buttons)
+		ui->active = 0;
+
+	ui->pressed = ui->released = 0;
+	ui->clicked = 0;
+	ui->focus_claimed = 0;
+	ui->press_x = ui->press_y = 0;
+	ui->release_x = ui->release_y = 0;
+}
+
+struct ngui_rect ngui_cut_top(struct ngui_rect *r, int h)
+{
+	struct ngui_rect out = *r;
+
+	if (h < 0)
+		h = 0;
+	if (h > r->h)
+		h = r->h;
+	out.h = h;
+	r->y += h;
+	r->h -= h;
+	return out;
+}
+
+struct ngui_rect ngui_cut_bottom(struct ngui_rect *r, int h)
+{
+	struct ngui_rect out = *r;
+
+	if (h < 0)
+		h = 0;
+	if (h > r->h)
+		h = r->h;
+	out.y = r->y + r->h - h;
+	out.h = h;
+	r->h -= h;
+	return out;
+}
+
+struct ngui_rect ngui_cut_left(struct ngui_rect *r, int w)
+{
+	struct ngui_rect out = *r;
+
+	if (w < 0)
+		w = 0;
+	if (w > r->w)
+		w = r->w;
+	out.w = w;
+	r->x += w;
+	r->w -= w;
+	return out;
+}
+
+struct ngui_rect ngui_cut_right(struct ngui_rect *r, int w)
+{
+	struct ngui_rect out = *r;
+
+	if (w < 0)
+		w = 0;
+	if (w > r->w)
+		w = r->w;
+	out.x = r->x + r->w - w;
+	out.w = w;
+	r->w -= w;
+	return out;
+}
+
+void ngui_inset(struct ngui_rect *r, int n)
+{
+	r->x += n;
+	r->y += n;
+	r->w -= n * 2;
+	r->h -= n * 2;
+	if (r->w < 0)
+		r->w = 0;
+	if (r->h < 0)
+		r->h = 0;
+}
+
+void ngui_push_clip(struct ngui *ui, struct ngui_rect r)
+{
+	struct ngui_rect out;
+	struct ngui_rect canvas;
+
+	if (ui->nclip >= 4)
+		return;
+	canvas = rect_make(0, 0, ui->w, ui->h);
+	if (!rect_intersect(&r, &canvas, &out))
+		out = rect_make(0, 0, 0, 0);
+	if (ui->nclip > 0) {
+		struct ngui_rect clipped = out;
+
+		if (!rect_intersect(&clipped, &ui->clip[ui->nclip - 1], &out))
+			out = rect_make(0, 0, 0, 0);
+	}
+	ui->clip[ui->nclip++] = out;
+}
+
+void ngui_pop_clip(struct ngui *ui)
+{
+	if (ui->nclip > 0)
+		ui->nclip--;
+}
+
+static int pixel_visible(struct ngui *ui, int x, int y)
+{
+	struct ngui_rect *c;
+
+	if (x < 0 || y < 0 || x >= ui->w || y >= ui->h)
+		return 0;
+	if (ui->nclip <= 0)
+		return 1;
+	c = &ui->clip[ui->nclip - 1];
+	return x >= c->x && y >= c->y &&
+	       x < c->x + c->w && y < c->y + c->h;
+}
+
+void ngui_fill_rect(struct ngui *ui, struct ngui_rect r, unsigned short color)
+{
+	struct ngui_rect canvas;
+	int x, y;
+
+	if (rect_empty(&r))
+		return;
+	canvas = rect_make(0, 0, ui->w, ui->h);
+	if (!rect_intersect(&r, &canvas, &r))
+		return;
+	if (ui->nclip > 0 &&
+	    !rect_intersect(&r, &ui->clip[ui->nclip - 1], &r))
+		return;
+	for (y = r.y; y < r.y + r.h; y++) {
+		unsigned short *p = ui->px + y * ui->w + r.x;
+
+		for (x = 0; x < r.w; x++)
+			p[x] = color;
+	}
+}
+
+void ngui_frame(struct ngui *ui, struct ngui_rect r, unsigned short color)
+{
+	if (r.w <= 0 || r.h <= 0)
+		return;
+	ngui_fill_rect(ui, rect_make(r.x, r.y, r.w, 1), color);
+	ngui_fill_rect(ui, rect_make(r.x, r.y + r.h - 1, r.w, 1), color);
+	if (r.h > 2) {
+		ngui_fill_rect(ui, rect_make(r.x, r.y + 1, 1, r.h - 2), color);
+		ngui_fill_rect(ui, rect_make(r.x + r.w - 1, r.y + 1, 1,
+					     r.h - 2), color);
+	}
+}
+
+static void ngui_pixel(struct ngui *ui, int x, int y, unsigned short color)
+{
+	if (pixel_visible(ui, x, y))
+		ui->px[y * ui->w + x] = color;
+}
+
+void ngui_line(struct ngui *ui, int x0, int y0, int x1, int y1,
+	       unsigned short color)
+{
+	int dx = x1 - x0;
+	int dy = y1 - y0;
+	int sx = dx < 0 ? -1 : 1;
+	int sy = dy < 0 ? -1 : 1;
+	int err, e2;
+
+	if (dx < 0)
+		dx = -dx;
+	if (dy < 0)
+		dy = -dy;
+	dy = -dy;
+	err = dx + dy;
+	for (;;) {
+		ngui_pixel(ui, x0, y0, color);
+		if (x0 == x1 && y0 == y1)
+			break;
+		e2 = err * 2;
+		if (e2 >= dy) {
+			err += dy;
+			x0 += sx;
+		}
+		if (e2 <= dx) {
+			err += dx;
+			y0 += sy;
+		}
+	}
+}
+
+static void ngui_glyph(struct ngui *ui, int x, int y, unsigned char c,
+		       unsigned short color)
+{
+	const unsigned char *g = &font_8x16[(unsigned int)c * FONT_8X16_ROWS];
+	int row, col;
+
+	for (row = 0; row < FONT_8X16_ROWS; row++) {
+		unsigned char bits = g[row];
+
+		if (!bits)
+			continue;
+		for (col = 0; col < 8; col++)
+			if (bits & (0x80u >> col))
+				ngui_pixel(ui, x + col, y + row, color);
+	}
+}
+
+unsigned int ngui_text_width(const char *s)
+{
+	return slen(s) * 8;
+}
+
+void ngui_text(struct ngui *ui, int x, int y, const char *s,
+	       unsigned short color)
+{
+	int i;
+
+	for (i = 0; s && s[i]; i++)
+		ngui_glyph(ui, x + i * 8, y, (unsigned char)s[i], color);
+}
+
+void ngui_text_ellipsis(struct ngui *ui, struct ngui_rect r, int x, int y,
+			const char *s, unsigned short color)
+{
+	unsigned int n = slen(s);
+	int max = (r.x + r.w - x) / 8;
+	unsigned int i;
+
+	if (max <= 0)
+		return;
+	if (n <= (unsigned int)max || max < 4) {
+		for (i = 0; i < n && (int)i < max; i++)
+			ngui_glyph(ui, x + (int)i * 8, y,
+				   (unsigned char)s[i], color);
+		return;
+	}
+	for (i = 0; i < (unsigned int)(max - 3); i++)
+		ngui_glyph(ui, x + (int)i * 8, y, (unsigned char)s[i], color);
+	ngui_glyph(ui, x + (max - 3) * 8, y, '.', color);
+	ngui_glyph(ui, x + (max - 2) * 8, y, '.', color);
+	ngui_glyph(ui, x + (max - 1) * 8, y, '.', color);
+}
+
+void ngui_icon(struct ngui *ui, struct ngui_rect r, enum ngui_icon icon,
+	       unsigned short color)
+{
+	int x = r.x + r.w / 2 - 8;
+	int y = r.y + r.h / 2 - 8;
+
+	switch (icon) {
+	case NGUI_ICON_FOLDER:
+		ngui_fill_rect(ui, rect_make(x + 1, y + 3, 7, 4), color);
+		ngui_fill_rect(ui, rect_make(x, y + 6, 16, 9), color);
+		break;
+	case NGUI_ICON_FILE:
+		ngui_frame(ui, rect_make(x + 2, y, 12, 16), color);
+		ngui_line(ui, x + 10, y, x + 13, y + 3, color);
+		ngui_fill_rect(ui, rect_make(x + 4, y + 7, 8, 1), color);
+		ngui_fill_rect(ui, rect_make(x + 4, y + 10, 8, 1), color);
+		break;
+	case NGUI_ICON_DISK:
+		ngui_frame(ui, rect_make(x, y + 2, 16, 12), color);
+		ngui_fill_rect(ui, rect_make(x + 2, y + 5, 12, 2), color);
+		ngui_fill_rect(ui, rect_make(x + 11, y + 9, 3, 3), color);
+		break;
+	case NGUI_ICON_UP:
+		ngui_line(ui, x + 8, y + 2, x + 3, y + 8, color);
+		ngui_line(ui, x + 8, y + 2, x + 13, y + 8, color);
+		ngui_fill_rect(ui, rect_make(x + 6, y + 7, 5, 7), color);
+		break;
+	case NGUI_ICON_HOME:
+		ngui_line(ui, x + 2, y + 8, x + 8, y + 2, color);
+		ngui_line(ui, x + 8, y + 2, x + 14, y + 8, color);
+		ngui_frame(ui, rect_make(x + 3, y + 8, 10, 7), color);
+		ngui_fill_rect(ui, rect_make(x + 7, y + 11, 3, 4), color);
+		break;
+	case NGUI_ICON_REFRESH:
+		ngui_line(ui, x + 3, y + 8, x + 6, y + 4, color);
+		ngui_line(ui, x + 6, y + 4, x + 10, y + 3, color);
+		ngui_line(ui, x + 10, y + 3, x + 13, y + 6, color);
+		ngui_line(ui, x + 13, y + 6, x + 13, y + 9, color);
+		ngui_fill_rect(ui, rect_make(x + 11, y + 1, 4, 4), color);
+		break;
+	case NGUI_ICON_PLUS:
+		ngui_fill_rect(ui, rect_make(x + 7, y + 3, 2, 10), color);
+		ngui_fill_rect(ui, rect_make(x + 3, y + 7, 10, 2), color);
+		break;
+	case NGUI_ICON_CHEVRON_LEFT:
+		ngui_line(ui, x + 10, y + 3, x + 6, y + 8, color);
+		ngui_line(ui, x + 6, y + 8, x + 10, y + 13, color);
+		break;
+	case NGUI_ICON_CHEVRON_RIGHT:
+		ngui_line(ui, x + 6, y + 3, x + 10, y + 8, color);
+		ngui_line(ui, x + 10, y + 8, x + 6, y + 13, color);
+		break;
+	default:
+		break;
+	}
+}
+
+int ngui_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
+		const char *label)
+{
+	int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
+	int press_hot = ui->pressed &&
+		ngui_rect_contains(&r, ui->press_x, ui->press_y);
+	int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
+	int fired = 0;
+	unsigned short bg;
+	int tw, tx, ty;
+
+	if (press_hot)
+		ui->active = id;
+	if (ui->pressed && ui->released && press_hot && release_hot)
+		fired = 1;
+	if (ui->released && ui->active == id) {
+		if (release_hot)
+			fired = 1;
+		ui->active = 0;
+	}
+
+	if (ui->active == id)
+		bg = ui->theme.button_down;
+	else if (hot)
+		bg = ui->theme.button_hot;
+	else
+		bg = ui->theme.button;
+	ngui_fill_rect(ui, r, bg);
+	ngui_frame(ui, r, hot ? ui->theme.accent : ui->theme.border);
+
+	if (label && label[0]) {
+		tw = (int)ngui_text_width(label);
+		tx = r.x + (r.w - tw) / 2;
+		ty = r.y + (r.h - 16) / 2;
+		ngui_push_clip(ui, r);
+		ngui_text(ui, tx, ty, label, ui->theme.text);
+		ngui_pop_clip(ui);
+	}
+	if (hot)
+		ui->hot = id;
+	return fired;
+}
+
+int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
+		     enum ngui_icon icon)
+{
+	int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
+	int press_hot = ui->pressed &&
+		ngui_rect_contains(&r, ui->press_x, ui->press_y);
+	int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
+	int fired = 0;
+	unsigned short bg;
+
+	if (press_hot)
+		ui->active = id;
+	if (ui->pressed && ui->released && press_hot && release_hot)
+		fired = 1;
+	if (ui->released && ui->active == id) {
+		if (release_hot)
+			fired = 1;
+		ui->active = 0;
+	}
+
+	if (ui->active == id)
+		bg = ui->theme.button_down;
+	else if (hot)
+		bg = ui->theme.button_hot;
+	else
+		bg = ui->theme.button;
+	ngui_fill_rect(ui, r, bg);
+	ngui_frame(ui, r, hot ? ui->theme.accent : ui->theme.border);
+	ngui_icon(ui, r, icon, ui->theme.text);
+	if (hot)
+		ui->hot = id;
+	return fired;
+}
+
+void ngui_input_set(struct ngui_input *in, const char *s)
+{
+	scopy(in->text, s, NGUI_INPUT_MAX);
+	in->len = slen(in->text);
+	in->cursor = in->len;
+	in->scroll = 0;
+}
+
+static void input_insert(struct ngui_input *in, char c)
+{
+	unsigned int i;
+
+	if (in->len + 1 >= NGUI_INPUT_MAX)
+		return;
+	i = in->len;
+	while (i > in->cursor) {
+		in->text[i] = in->text[i - 1];
+		i--;
+	}
+	in->text[in->cursor] = c;
+	in->cursor++;
+	in->len++;
+	in->text[in->len] = '\0';
+}
+
+static void input_delete(struct ngui_input *in, int back)
+{
+	unsigned int i, pos;
+
+	if (back) {
+		if (in->cursor == 0)
+			return;
+		pos = in->cursor - 1;
+	} else {
+		if (in->cursor >= in->len)
+			return;
+		pos = in->cursor;
+	}
+	i = pos;
+	while (i + 1 < in->len) {
+		in->text[i] = in->text[i + 1];
+		i++;
+	}
+	in->len--;
+	if (back)
+		in->cursor--;
+	in->text[in->len] = '\0';
+}
+
+int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
+		    struct ngui_input *in)
+{
+	struct ngui_rect inner;
+	int focused = (ui->focus == id);
+	int result = 0;
+	int cols, k;
+	int changed = 0;
+
+	if (ui->pressed &&
+	    ngui_rect_contains(&r, ui->press_x, ui->press_y)) {
+		int col = (ui->press_x - (r.x + 4)) / 8;
+		unsigned int pos;
+
+		if (col < 0)
+			col = 0;
+		pos = in->scroll + (unsigned int)col;
+		if (pos > in->len)
+			pos = in->len;
+		in->cursor = pos;
+		ui->focus = id;
+		ui->focus_claimed = 1;
+		ui->last_blink = u_ticks_ms();
+		ui->caret_on = 1;
+		focused = 1;
+	}
+
+	if (focused) {
+		while ((k = ngui_key(ui)) != 0) {
+			if (k == '\n' || k == '\r') {
+				result |= NGUI_INPUT_SUBMIT;
+				break;
+			}
+			if (k == NGUI_KEY_ESC || k == NGUI_KEY_TAB) {
+				result |= NGUI_INPUT_CANCEL;
+				ui->focus = 0;
+				break;
+			}
+			if (k == NGUI_KEY_BACKSPACE || k == 0x7F) {
+				input_delete(in, 1);
+				changed = 1;
+			} else if (k == NGUI_KEY_DELETE) {
+				input_delete(in, 0);
+				changed = 1;
+			} else if (k == NGUI_KEY_LEFT) {
+				if (in->cursor > 0)
+					in->cursor--;
+			} else if (k == NGUI_KEY_RIGHT) {
+				if (in->cursor < in->len)
+					in->cursor++;
+			} else if (k == NGUI_KEY_HOME) {
+				in->cursor = 0;
+			} else if (k == NGUI_KEY_END) {
+				in->cursor = in->len;
+			} else if (k == 0x15) {   /* Ctrl+U */
+				in->len = 0;
+				in->cursor = 0;
+				in->text[0] = '\0';
+				changed = 1;
+			} else if (k >= 0x20 && k <= 0x7E) {
+				input_insert(in, (char)k);
+				changed = 1;
+			}
+		}
+	}
+
+	inner = rect_make(r.x + 4, r.y + (r.h - 16) / 2, r.w - 8, 16);
+	if (inner.w < 0)
+		inner.w = 0;
+	ngui_fill_rect(ui, r, focused ? ui->theme.panel_alt : ui->theme.panel);
+	ngui_frame(ui, r, focused ? ui->theme.accent : ui->theme.border);
+
+	cols = inner.w / 8;
+	if (cols < 0)
+		cols = 0;
+	if (in->scroll > in->len)
+		in->scroll = in->len;
+	if (in->cursor < in->scroll)
+		in->scroll = in->cursor;
+	if (cols > 0 && in->cursor >= in->scroll + (unsigned int)cols)
+		in->scroll = in->cursor - (unsigned int)cols + 1;
+	if (in->scroll > in->len)
+		in->scroll = in->len;
+
+	ngui_push_clip(ui, inner);
+	if (in->len == 0 && in->placeholder)
+		ngui_text(ui, inner.x, inner.y, in->placeholder,
+			  ui->theme.text_dim);
+	else
+		ngui_text(ui, inner.x, inner.y, in->text + in->scroll,
+			  ui->theme.text);
+
+	if (focused && ui->caret_on) {
+		int col = (int)in->cursor - (int)in->scroll;
+
+		if (col >= 0 && col <= cols)
+			ngui_fill_rect(ui, rect_make(inner.x + col * 8, inner.y,
+						     1, 16), ui->theme.accent);
+	}
+	ngui_pop_clip(ui);
+
+	if (changed)
+		result |= NGUI_INPUT_CHANGED;
+	return result;
+}
+
+static int scrollable(const struct ngui_scroll *s)
+{
+	return s->content_h > s->view_h && s->view_h > 0;
+}
+
+static int scroll_thumb_h(const struct ngui_rect *r,
+			  const struct ngui_scroll *s)
+{
+	int h = 0;
+
+	if (s->content_h > 0)
+		h = r->h * s->view_h / s->content_h;
+	if (h < 12)
+		h = 12;
+	if (h > r->h)
+		h = r->h;
+	return h;
+}
+
+static int scroll_thumb_y(const struct ngui_rect *r,
+			  const struct ngui_scroll *s)
+{
+	int th = scroll_thumb_h(r, s);
+	int avail = r->h - th;
+	int max = s->content_h - s->view_h;
+
+	if (avail <= 0 || max <= 0)
+		return r->y;
+	return r->y + s->offset * avail / max;
+}
+
+static void scroll_clamp(struct ngui_scroll *s)
+{
+	int max = s->content_h - s->view_h;
+
+	if (max < 0)
+		max = 0;
+	if (s->offset > max)
+		s->offset = max;
+	if (s->offset < 0)
+		s->offset = 0;
+}
+
+void ngui_scroll_to(struct ngui_scroll *s, int offset)
+{
+	s->offset = offset;
+	scroll_clamp(s);
+}
+
+void ngui_scroll_ensure_visible(struct ngui_scroll *s, int y, int h)
+{
+	if (y < s->offset)
+		s->offset = y;
+	else if (y + h > s->offset + s->view_h)
+		s->offset = y + h - s->view_h;
+	scroll_clamp(s);
+}
+
+void ngui_scroll_begin(struct ngui *ui, struct ngui_rect r,
+		       struct ngui_scroll *s)
+{
+	struct ngui_rect bar;
+	int scrolls = scrollable(s);
+
+	s->view_h = r.h;
+	scroll_clamp(s);
+	if (!ui->buttons)
+		s->dragging = 0;
+	if (scrolls) {
+		int th = scroll_thumb_h(&r, s);
+		int ty = scroll_thumb_y(&r, s);
+
+		bar = rect_make(r.x + r.w - 9, r.y, 8, r.h);
+		if (ui->pressed &&
+		    ngui_rect_contains(&bar, ui->press_x, ui->press_y)) {
+			if (ui->press_y >= ty && ui->press_y < ty + th) {
+				s->dragging = 1;
+				s->drag_dy = ui->press_y - ty;
+			} else {
+				int rel = ui->press_y - r.y - th / 2;
+				int avail = r.h - th;
+				int max = s->content_h - s->view_h;
+
+				if (rel < 0)
+					rel = 0;
+				if (rel > avail)
+					rel = avail;
+				if (avail > 0)
+					s->offset = rel * max / avail;
+				s->dragging = 1;
+				s->drag_dy = th / 2;
+				scroll_clamp(s);
+			}
+		}
+		if (s->dragging && (ui->buttons & 1)) {
+			int rel = ui->mouse_y - r.y - s->drag_dy;
+			int avail = r.h - th;
+			int max = s->content_h - s->view_h;
+
+			if (rel < 0)
+				rel = 0;
+			if (rel > avail)
+				rel = avail;
+			if (avail > 0)
+				s->offset = rel * max / avail;
+			scroll_clamp(s);
+		}
+		if (ui->released)
+			s->dragging = 0;
+	}
+	ngui_push_clip(ui, r);
+}
+
+void ngui_scroll_end(struct ngui *ui, struct ngui_rect r,
+		     struct ngui_scroll *s)
+{
+	ngui_pop_clip(ui);
+	if (!scrollable(s))
+		return;
+	{
+		struct ngui_rect bar = rect_make(r.x + r.w - 9, r.y, 8, r.h);
+		int th = scroll_thumb_h(&r, s);
+		int ty = scroll_thumb_y(&r, s);
+		struct ngui_rect thumb = rect_make(bar.x, ty, 8, th);
+
+		ngui_fill_rect(ui, bar, ui->theme.panel_alt);
+		ngui_fill_rect(ui, thumb, s->dragging ?
+			       ui->theme.accent : ui->theme.text_dim);
+	}
+}
+
+int ngui_scroll_keys(struct ngui *ui, struct ngui_scroll *s)
+{
+	int k = ngui_key_peek(ui);
+	int page;
+
+	if (k != NGUI_KEY_PAGE_UP && k != NGUI_KEY_PAGE_DOWN)
+		return 0;
+	page = s->view_h - 22;
+	if (page < 22)
+		page = 22;
+	ngui_key(ui);
+	if (k == NGUI_KEY_PAGE_UP)
+		s->offset -= page;
+	else
+		s->offset += page;
+	scroll_clamp(s);
+	return 1;
+}

+ 175 - 0
ngui.h

@@ -0,0 +1,175 @@
+/* ngui: tiny immediate-mode GUI toolkit for ring-3 canvas windows.
+ *
+ * The toolkit owns no memory: the app passes its framebuffer and keeps the
+ * state structs for text fields and scroll panes.  Everything is RGB565 and
+ * ASCII, using the kernel's 8x16 font.  See ucmd_files.c for a user. */
+#ifndef NGUI_H
+#define NGUI_H
+
+#include "syscall.h"
+#include "wm_abi.h"
+
+#define NGUI_INPUT_MAX 128
+
+#define NGUI_INPUT_CHANGED 1
+#define NGUI_INPUT_SUBMIT  2
+#define NGUI_INPUT_CANCEL  4
+
+/* Key codes from io.c.  Arrows are 0x100..; the page/home/end keys are
+ * produced by the extended PS/2 path. */
+#define NGUI_KEY_ESC        0x1B
+#define NGUI_KEY_BACKSPACE  0x08
+#define NGUI_KEY_TAB        0x09
+#define NGUI_KEY_LEFT       0x100
+#define NGUI_KEY_RIGHT      0x101
+#define NGUI_KEY_UP         0x102
+#define NGUI_KEY_DOWN       0x103
+#define NGUI_KEY_PAGE_UP    0x104
+#define NGUI_KEY_PAGE_DOWN  0x105
+#define NGUI_KEY_HOME       0x106
+#define NGUI_KEY_END        0x107
+#define NGUI_KEY_DELETE     0x108
+
+#define NGUI_RGB(r, g, b) \
+	((unsigned short)((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3)))
+
+struct ngui_rect {
+	int x, y, w, h;
+};
+
+struct ngui_theme {
+	unsigned short bg;
+	unsigned short panel;
+	unsigned short panel_alt;
+	unsigned short border;
+	unsigned short text;
+	unsigned short text_dim;
+	unsigned short accent;
+	unsigned short selection;
+	unsigned short button;
+	unsigned short button_hot;
+	unsigned short button_down;
+	unsigned short icon_folder;
+	unsigned short icon_file;
+	unsigned short icon_disk;
+	unsigned short error;
+};
+
+extern const struct ngui_theme ngui_theme_dark;
+
+enum ngui_icon {
+	NGUI_ICON_FOLDER,
+	NGUI_ICON_FILE,
+	NGUI_ICON_DISK,
+	NGUI_ICON_UP,
+	NGUI_ICON_HOME,
+	NGUI_ICON_REFRESH,
+	NGUI_ICON_PLUS,
+	NGUI_ICON_CHEVRON_LEFT,
+	NGUI_ICON_CHEVRON_RIGHT
+};
+
+struct ngui_input {
+	char text[NGUI_INPUT_MAX];
+	unsigned int len;
+	unsigned int cursor;
+	unsigned int scroll;          /* first visible character */
+	const char *placeholder;
+};
+
+struct ngui_scroll {
+	int offset;                   /* pixels scrolled from the top */
+	int content_h;
+	int view_h;
+	int dragging;
+	int drag_dy;
+};
+
+struct ngui {
+	unsigned short *px;
+	int w, h;
+	struct ngui_theme theme;
+
+	/* Current pointer state, content-relative. */
+	int mouse_x, mouse_y;
+	unsigned int buttons;
+	int inside;
+	int focused;
+
+	/* Press/release edges collected by ngui_begin(). */
+	unsigned int pressed;
+	unsigned int released;
+	int press_x, press_y;
+	int release_x, release_y;
+	int clicked;
+	int focus_claimed;
+
+	unsigned int hot;
+	unsigned int active;
+	unsigned int focus;
+
+	struct ngui_rect clip[4];
+	int nclip;
+
+	int dirty;
+	unsigned int last_blink;
+	int caret_on;
+
+	int keys[32];
+	unsigned int key_head, key_tail;
+
+	int last_mouse_x, last_mouse_y;
+	int last_inside, last_focused;
+};
+
+void ngui_init(struct ngui *ui, unsigned short *fb, int w, int h);
+int  ngui_begin(struct ngui *ui);       /* 1 = app should redraw */
+void ngui_end(struct ngui *ui);
+void ngui_invalidate(struct ngui *ui);
+void ngui_set_focus(struct ngui *ui, unsigned int id);
+unsigned int ngui_focus(struct ngui *ui);
+int  ngui_key(struct ngui *ui);
+int  ngui_key_peek(struct ngui *ui);
+
+/* Layout: cut a rectangle off one side of `r` and return the piece. */
+struct ngui_rect ngui_cut_top(struct ngui_rect *r, int h);
+struct ngui_rect ngui_cut_bottom(struct ngui_rect *r, int h);
+struct ngui_rect ngui_cut_left(struct ngui_rect *r, int w);
+struct ngui_rect ngui_cut_right(struct ngui_rect *r, int w);
+void ngui_inset(struct ngui_rect *r, int n);
+int  ngui_rect_contains(const struct ngui_rect *r, int x, int y);
+
+/* Primitives.  All drawing is clipped to the current clip stack and canvas. */
+void ngui_push_clip(struct ngui *ui, struct ngui_rect r);
+void ngui_pop_clip(struct ngui *ui);
+void ngui_fill_rect(struct ngui *ui, struct ngui_rect r, unsigned short color);
+void ngui_frame(struct ngui *ui, struct ngui_rect r, unsigned short color);
+void ngui_line(struct ngui *ui, int x0, int y0, int x1, int y1,
+	       unsigned short color);
+void ngui_text(struct ngui *ui, int x, int y, const char *s,
+	       unsigned short color);
+void ngui_text_ellipsis(struct ngui *ui, struct ngui_rect r, int x, int y,
+			const char *s, unsigned short color);
+unsigned int ngui_text_width(const char *s);
+void ngui_icon(struct ngui *ui, struct ngui_rect r, enum ngui_icon icon,
+	       unsigned short color);
+
+/* Widgets.  IDs must be stable across frames. */
+int ngui_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
+		const char *label);
+int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
+		     enum ngui_icon icon);
+int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
+		    struct ngui_input *in);
+void ngui_input_set(struct ngui_input *in, const char *s);
+
+/* Scrolling pane: call begin, draw the content with scroll->offset, end. */
+void ngui_scroll_begin(struct ngui *ui, struct ngui_rect r,
+		       struct ngui_scroll *s);
+void ngui_scroll_end(struct ngui *ui, struct ngui_rect r,
+		     struct ngui_scroll *s);
+void ngui_scroll_to(struct ngui_scroll *s, int offset);
+void ngui_scroll_ensure_visible(struct ngui_scroll *s, int y, int h);
+int  ngui_scroll_keys(struct ngui *ui, struct ngui_scroll *s);
+
+#endif

+ 28 - 0
sys_wm.c

@@ -188,6 +188,34 @@ int sys_win_ack_resize(void)
 	return wm_server_win_ack_resize();
 }
 
+int sys_win_pointer(struct win_pointer *out)
+{
+	struct win_pointer p;
+	int r;
+
+	if (!user_ok(out, sizeof(p), 1))
+		return -1;
+	zero_struct(&p, sizeof(p));
+	r = wm_server_win_pointer(&p);
+	if (r == 0)
+		copy_to_user(out, &p, sizeof(p));
+	return r;
+}
+
+int sys_win_event(struct win_event *out)
+{
+	struct win_event e;
+	int r;
+
+	if (!user_ok(out, sizeof(e), 1))
+		return -1;
+	zero_struct(&e, sizeof(e));
+	r = wm_server_win_event(&e);
+	if (r == 1)
+		copy_to_user(out, &e, sizeof(e));
+	return r;
+}
+
 int sys_wm_resize(unsigned int index, const struct wm_size *in)
 {
 	struct wm_size s;

+ 2 - 0
sys_wm.h

@@ -22,6 +22,8 @@ int sys_win_present(void);
 int sys_win_close(void);
 int sys_win_info(struct win_info *out);
 int sys_win_ack_resize(void);
+int sys_win_pointer(struct win_pointer *out);
+int sys_win_event(struct win_event *out);
 int sys_wm_resize(unsigned int index, const struct wm_size *in);
 int sys_win_key_wait(unsigned int timeout_ms);
 

+ 4 - 0
syscall.c

@@ -239,6 +239,10 @@ static int syscall_dispatch_locked(int num, int a1, int a2, int a3)
 		return sys_win_info((struct win_info *)a1);
 	case SYS_WIN_ACK_RESIZE:
 		return sys_win_ack_resize();
+	case SYS_WIN_POINTER:
+		return sys_win_pointer((struct win_pointer *)a1);
+	case SYS_WIN_EVENT:
+		return sys_win_event((struct win_event *)a1);
 	case SYS_WIN_KEY_WAIT:
 		return sys_win_key_wait((unsigned int)a1);
 	case SYS_SCHEDINFO:

+ 2 - 0
syscall.h

@@ -106,6 +106,8 @@
 #define SYS_WM_RESIZE      83  /* (index, struct wm_size *content) -> 0/-1 */
 #define SYS_WIN_INFO       84  /* (struct win_info *out) -> 0/-1 */
 #define SYS_WIN_ACK_RESIZE 85  /* () -> 0/-1; swap in the pending canvas */
+#define SYS_WIN_POINTER    86  /* (struct win_pointer *out) -> 0/-1 */
+#define SYS_WIN_EVENT      87  /* (struct win_event *out) -> 1 event, 0 none */
 
 /* Terminal / process services used by the ring-3 shell. */
 #define SYS_TTY_CREATE     73  /* () -> tty id 1..TTY_MAX / -1 */

+ 1098 - 0
ucmd_files.c

@@ -0,0 +1,1098 @@
+/* files: a small graphical file manager built on the ngui toolkit.
+ *
+ * The window is a canvas window (SYS_WIN_CREATE), so it owns its pixels and
+ * receives keys and pointer events.  It browses the VFS: "/" lists the mounted
+ * filesystems, everything else lists a directory through SYS_LIST_DIR. */
+
+#include "ucmd_lib.h"
+#include "ngui.h"
+
+#define FM_PATH_MAX     128
+#define FM_MAX_ENTRIES  256
+#define FM_MAX_MOUNTS   8
+#define FM_ROW_H        22
+#define FM_TOOLBAR_H    30
+#define FM_PATH_H       26
+#define FM_STATUS_H     18
+#define FM_SIDEBAR_W    140
+#define FM_SCROLL_W     9
+#define FM_EDIT_MAX     16384
+#define FM_STATUS_MS    3000
+
+enum {
+	ID_UP = 1,
+	ID_HOME,
+	ID_REFRESH,
+	ID_NEW,
+	ID_GO,
+	ID_PATH,
+	ID_NEW_INPUT,
+	ID_CREATE,
+	ID_CANCEL,
+};
+
+#define ACT_UP       (1u << 0)
+#define ACT_HOME     (1u << 1)
+#define ACT_REFRESH  (1u << 2)
+#define ACT_NEW      (1u << 3)
+#define ACT_NAV      (1u << 4)
+#define ACT_LOAD     (1u << 5)
+#define ACT_OPEN     (1u << 6)
+#define ACT_PARENT   (1u << 7)
+#define ACT_CREATE   (1u << 8)
+#define ACT_CANCEL   (1u << 9)
+#define ACT_CLOSE    (1u << 10)
+
+struct fm_entry {
+	char name[16];
+	char detail[8];
+	unsigned int size;
+	unsigned int is_dir;
+	unsigned int is_mount;
+};
+
+static struct fm_entry entries[FM_MAX_ENTRIES];
+static struct fm_entry load_entries[FM_MAX_ENTRIES];
+static unsigned int entry_count;
+static int truncated;
+static int selected;
+static char cwd[FM_PATH_MAX];
+static char action_path[FM_PATH_MAX];
+
+static struct sys_mount mounts[FM_MAX_MOUNTS];
+static unsigned int mount_count;
+
+static struct ngui ui;
+static struct ngui_input path_in;
+static struct ngui_input new_in;
+static struct ngui_scroll list_scroll;
+static int modal;
+static int last_click_index = -1;
+static unsigned int last_click_ms;
+
+static char status_text[96];
+static unsigned int status_until;
+static int status_error;
+
+/* ---- tiny freestanding string helpers ---- */
+
+static unsigned int s_len(const char *s)
+{
+	unsigned int n = 0;
+
+	while (s && s[n])
+		n++;
+	return n;
+}
+
+static void s_copy(char *d, const char *s, unsigned int cap)
+{
+	unsigned int i = 0;
+
+	if (!cap)
+		return;
+	while (s && s[i] && i + 1 < cap) {
+		d[i] = s[i];
+		i++;
+	}
+	d[i] = '\0';
+}
+
+static void s_append(char *d, unsigned int *n, const char *s, unsigned int cap)
+{
+	while (s && *s && *n + 1 < cap)
+		d[(*n)++] = *s++;
+	d[*n] = '\0';
+}
+
+static void s_append_uint(char *d, unsigned int *n, unsigned int v,
+			  unsigned int cap)
+{
+	char tmp[12];
+	unsigned int i = 0, j;
+
+	do {
+		tmp[i++] = (char)('0' + v % 10);
+		v /= 10;
+	} while (v && i < sizeof(tmp));
+	for (j = 0; j < i && *n + 1 < cap; j++)
+		d[(*n)++] = tmp[i - 1 - j];
+	d[*n] = '\0';
+}
+
+static int s_eq(const char *a, const char *b)
+{
+	while (*a && *b) {
+		if (*a != *b)
+			return 0;
+		a++;
+		b++;
+	}
+	return *a == *b;
+}
+
+static int s_ci_cmp(const char *a, const char *b)
+{
+	for (;;) {
+		unsigned char ca = (unsigned char)*a++;
+		unsigned char cb = (unsigned char)*b++;
+
+		if (ca >= 'a' && ca <= 'z')
+			ca = (unsigned char)(ca - 32);
+		if (cb >= 'a' && cb <= 'z')
+			cb = (unsigned char)(cb - 32);
+		if (ca != cb)
+			return (int)ca - (int)cb;
+		if (!ca)
+			return 0;
+	}
+}
+
+static int is_root(const char *p)
+{
+	return p && p[0] == '/' && p[1] == '\0';
+}
+
+static void path_join(const char *dir, const char *name, char *out,
+		      unsigned int cap)
+{
+	unsigned int n = 0;
+
+	if (!dir || !dir[0])
+		dir = "/";
+	if (name && name[0] == '/') {
+		s_copy(out, name, cap);
+		return;
+	}
+	s_append(out, &n, dir, cap);
+	if (!is_root(dir))
+		s_append(out, &n, "/", cap);
+	s_append(out, &n, name, cap);
+}
+
+static void path_parent(const char *in, char *out, unsigned int cap)
+{
+	unsigned int n, i, last = 0;
+
+	s_copy(out, in, cap);
+	if (!out[0] || is_root(out)) {
+		s_copy(out, "/", cap);
+		return;
+	}
+	n = s_len(out);
+	if (n > 1 && out[n - 1] == '/')
+		out[--n] = '\0';
+	for (i = 0; i < n; i++)
+		if (out[i] == '/')
+			last = i;
+	if (last == 0)
+		out[1] = '\0';
+	else
+		out[last] = '\0';
+	if (!out[0])
+		s_copy(out, "/", cap);
+}
+
+static void make_abs(const char *p, char *out, unsigned int cap)
+{
+	if (!p || !p[0]) {
+		s_copy(out, cwd, cap);
+		return;
+	}
+	if (p[0] == '/')
+		s_copy(out, p, cap);
+	else
+		path_join(cwd, p, out, cap);
+}
+
+static void format_name(const char *raw, char *out, unsigned int cap)
+{
+	unsigned int n = 0, i, ext = 8;
+	int dotted = 0, lower = 0, space = 0;
+
+	/* ramfs names are plain strings (usually lowercase, sometimes with a
+	 * dot); FAT names are the raw 11-byte, space-padded 8.3 form. */
+	for (i = 0; i < 11 && raw[i]; i++) {
+		if (raw[i] == '.')
+			dotted = 1;
+		if (raw[i] >= 'a' && raw[i] <= 'z')
+			lower = 1;
+		if (raw[i] == ' ')
+			space = 1;
+	}
+	if (dotted || (lower && !space)) {
+		s_copy(out, raw, cap);
+		return;
+	}
+	while (ext < 11 && raw[ext] == ' ')
+		ext++;
+	for (i = 0; i < 8 && raw[i] && raw[i] != ' ' && n + 1 < cap; i++)
+		out[n++] = raw[i];
+	if (ext < 11 && raw[ext]) {
+		if (n + 1 < cap)
+			out[n++] = '.';
+		for (i = ext; i < 11 && raw[i] && raw[i] != ' ' &&
+		     n + 1 < cap; i++)
+			out[n++] = raw[i];
+	}
+	out[n] = '\0';
+	if (!n && cap)
+		s_copy(out, "?", cap);
+}
+
+static void format_size(unsigned int bytes, char *out, unsigned int cap)
+{
+	unsigned int n = 0;
+
+	if (bytes < 1024) {
+		s_append_uint(out, &n, bytes, cap);
+		s_append(out, &n, " B", cap);
+	} else if (bytes < 1024u * 1024u) {
+		s_append_uint(out, &n, bytes / 1024, cap);
+		s_append(out, &n, " KB", cap);
+	} else {
+		s_append_uint(out, &n, bytes / (1024u * 1024u), cap);
+		s_append(out, &n, " MB", cap);
+	}
+}
+
+/* ---- state / filesystem loading ---- */
+
+static void set_status(const char *text, int error)
+{
+	s_copy(status_text, text, sizeof(status_text));
+	status_until = u_ticks_ms() + FM_STATUS_MS;
+	status_error = error;
+	if (!status_until)
+		status_until = 1;
+	ngui_invalidate(&ui);
+}
+
+static void clear_status(void)
+{
+	status_text[0] = '\0';
+	status_until = 0;
+	status_error = 0;
+}
+
+static void load_mounts(void)
+{
+	struct sys_mount m;
+	unsigned int i;
+
+	mount_count = 0;
+	for (i = 0; i < FM_MAX_MOUNTS; i++) {
+		if (u_syscall2(SYS_LIST_MOUNTS, (int)i, (int)&m) != 1)
+			break;
+		mounts[mount_count++] = m;
+	}
+}
+
+static int entry_before(const struct fm_entry *a, const struct fm_entry *b)
+{
+	int adir = (int)(a->is_dir || a->is_mount);
+	int bdir = (int)(b->is_dir || b->is_mount);
+
+	if (adir != bdir)
+		return adir > bdir;
+	return s_ci_cmp(a->name, b->name) < 0;
+}
+
+static void sort_entries(struct fm_entry *list, unsigned int n)
+{
+	unsigned int i, j;
+
+	for (i = 1; i < n; i++) {
+		struct fm_entry key = list[i];
+
+		j = i;
+		while (j > 0 && entry_before(&key, &list[j - 1])) {
+			list[j] = list[j - 1];
+			j--;
+		}
+		list[j] = key;
+	}
+}
+
+static void commit_list(unsigned int n, const char *path)
+{
+	unsigned int i;
+
+	for (i = 0; i < n; i++)
+		entries[i] = load_entries[i];
+	entry_count = n;
+	s_copy(cwd, path, sizeof(cwd));
+	selected = 0;
+	list_scroll.offset = 0;
+	list_scroll.content_h = (int)(n * FM_ROW_H);
+	last_click_index = -1;
+	ngui_input_set(&path_in, cwd);
+	ngui_invalidate(&ui);
+}
+
+static int load_root(void)
+{
+	unsigned int i;
+
+	load_mounts();
+	for (i = 0; i < mount_count; i++) {
+		s_copy(load_entries[i].name, mounts[i].name,
+		       sizeof(load_entries[i].name));
+		s_copy(load_entries[i].detail, mounts[i].type,
+		       sizeof(load_entries[i].detail));
+		load_entries[i].size = 0;
+		load_entries[i].is_dir = 1;
+		load_entries[i].is_mount = 1;
+	}
+	sort_entries(load_entries, mount_count);
+	truncated = 0;
+	commit_list(mount_count, "/");
+	clear_status();
+	return 0;
+}
+
+static void list_error(int r)
+{
+	switch (r) {
+	case SYS_ENOENT:
+		set_status("No such file or directory", 1);
+		break;
+	case SYS_ENODEV:
+		set_status("No such device", 1);
+		break;
+	case SYS_ENOFS:
+		set_status("No filesystem on device", 1);
+		break;
+	case SYS_EINVAL:
+		set_status("Not a directory", 1);
+		break;
+	case SYS_EIO:
+		set_status("Disk read error", 1);
+		break;
+	default:
+		set_status("Cannot open directory", 1);
+		break;
+	}
+}
+
+static int load_path(const char *path)
+{
+	struct sys_dirent d;
+	unsigned int n = 0, i;
+	int r;
+
+	if (!path || !path[0] || is_root(path))
+		return load_root();
+
+	truncated = 0;
+	for (i = 0; ; i++) {
+		r = u_syscall3(SYS_LIST_DIR, (int)path, (int)i, (int)&d);
+		if (r != 1) {
+			if (r < 0) {
+				list_error(r);
+				return -1;
+			}
+			break;
+		}
+		if (d.name[0] == '.')
+			continue;
+		if (n >= FM_MAX_ENTRIES) {
+			truncated = 1;
+			break;
+		}
+		format_name(d.name, load_entries[n].name,
+			    sizeof(load_entries[n].name));
+		load_entries[n].detail[0] = '\0';
+		load_entries[n].size = d.size;
+		load_entries[n].is_dir = d.is_dir;
+		load_entries[n].is_mount = 0;
+		n++;
+	}
+	sort_entries(load_entries, n);
+	commit_list(n, path);
+	clear_status();
+	if (truncated)
+		set_status("Showing the first 256 entries", 0);
+	return 0;
+}
+
+static void navigate(const char *path)
+{
+	char abs[FM_PATH_MAX];
+
+	if (!path || !path[0]) {
+		set_status("Empty path", 1);
+		return;
+	}
+	make_abs(path, abs, sizeof(abs));
+	if (load_path(abs) == 0)
+		ngui_set_focus(&ui, 0);
+}
+
+static void go_parent(void)
+{
+	char parent[FM_PATH_MAX];
+
+	path_parent(cwd, parent, sizeof(parent));
+	if (s_eq(parent, cwd)) {
+		set_status("Already at the root", 0);
+		return;
+	}
+	load_path(parent);
+}
+
+static void go_home(void)
+{
+	if (!is_root(cwd))
+		load_path("/");
+}
+
+static void select_index(unsigned int idx)
+{
+	if (idx >= entry_count)
+		return;
+	selected = (int)idx;
+	ngui_scroll_ensure_visible(&list_scroll, (int)idx * FM_ROW_H, FM_ROW_H);
+	ngui_invalidate(&ui);
+}
+
+static void select_by_name(const char *name)
+{
+	unsigned int i;
+
+	for (i = 0; i < entry_count; i++)
+		if (s_ci_cmp(entries[i].name, name) == 0) {
+			select_index(i);
+			return;
+		}
+}
+
+static void refresh(void)
+{
+	char keep[16];
+	int had = 0;
+
+	if (entry_count && selected >= 0 && selected < (int)entry_count) {
+		s_copy(keep, entries[selected].name, sizeof(keep));
+		had = 1;
+	}
+	if (load_path(cwd) != 0)
+		return;
+	if (had)
+		select_by_name(keep);
+}
+
+static void open_selected(void)
+{
+	struct fm_entry *e;
+	char path[FM_PATH_MAX];
+	int pid;
+
+	if (!entry_count || selected < 0 || selected >= (int)entry_count)
+		return;
+	e = &entries[selected];
+	if (e->is_mount || e->is_dir) {
+		if (e->is_mount)
+			path_join("/", e->name, path, sizeof(path));
+		else
+			path_join(cwd, e->name, path, sizeof(path));
+		load_path(path);
+		return;
+	}
+	if (e->size > FM_EDIT_MAX) {
+		set_status("File too large for the editor", 1);
+		return;
+	}
+	path_join(cwd, e->name, path, sizeof(path));
+	pid = u_spawn("/ramfs/bin/edit", path, 0, cwd);
+	if (pid <= 0)
+		set_status("Cannot launch the editor", 1);
+	else
+		set_status("Opened in the editor", 0);
+}
+
+static void new_folder(void)
+{
+	if (is_root(cwd)) {
+		set_status("Select a filesystem first", 1);
+		return;
+	}
+	modal = 1;
+	ngui_input_set(&new_in, "");
+	new_in.placeholder = "NAME";
+	ngui_set_focus(&ui, ID_NEW_INPUT);
+	ngui_invalidate(&ui);
+}
+
+static void create_folder(void)
+{
+	char path[FM_PATH_MAX];
+	char made[16];
+	int r;
+
+	if (!new_in.text[0]) {
+		set_status("Enter a folder name", 1);
+		return;
+	}
+	path_join(cwd, new_in.text, path, sizeof(path));
+	r = u_syscall1(SYS_MKDIR, (int)path);
+	if (r != 0) {
+		switch (r) {
+		case SYS_EROFS:
+			set_status("The filesystem is read-only", 1);
+			break;
+		case SYS_EINVAL:
+			set_status("Invalid folder name", 1);
+			break;
+		case SYS_EIO:
+			set_status("Disk write error", 1);
+			break;
+		default:
+			set_status("Cannot create the folder", 1);
+			break;
+		}
+		return;
+	}
+	s_copy(made, new_in.text, sizeof(made));
+	modal = 0;
+	ngui_set_focus(&ui, 0);
+	refresh();
+	select_by_name(made);
+	clear_status();
+	set_status("Folder created", 0);
+}
+
+static void do_action(unsigned int action)
+{
+	if (action & ACT_LOAD)
+		load_path(action_path);
+	if (action & ACT_NAV)
+		navigate(path_in.text);
+	if (action & ACT_UP)
+		go_parent();
+	if (action & ACT_PARENT)
+		go_parent();
+	if (action & ACT_HOME)
+		go_home();
+	if (action & ACT_REFRESH)
+		refresh();
+	if (action & ACT_NEW)
+		new_folder();
+	if (action & ACT_OPEN)
+		open_selected();
+	if (action & ACT_CREATE) {
+		create_folder();
+		if (modal)
+			ngui_set_focus(&ui, ID_NEW_INPUT);
+	}
+	if (action & ACT_CANCEL) {
+		modal = 0;
+		ngui_set_focus(&ui, 0);
+		ngui_invalidate(&ui);
+	}
+}
+
+/* ---- drawing ---- */
+
+static void draw_toolbar(struct ngui *g, struct ngui_rect r, unsigned int *action)
+{
+	int compact = g->w < 480;
+	int y = r.y + 4;
+	int h = 22;
+	int x = r.x + 6;
+
+	ngui_fill_rect(g, r, g->theme.panel);
+	ngui_fill_rect(g, (struct ngui_rect){r.x, r.y + r.h - 1, r.w, 1},
+		       g->theme.border);
+
+	if (compact) {
+		if (ngui_icon_button(g, ID_UP,
+				     (struct ngui_rect){x, y, 30, h},
+				     NGUI_ICON_UP))
+			*action |= ACT_UP;
+		x += 36;
+		if (ngui_icon_button(g, ID_HOME,
+				     (struct ngui_rect){x, y, 30, h},
+				     NGUI_ICON_HOME))
+			*action |= ACT_HOME;
+		x += 36;
+		if (ngui_icon_button(g, ID_REFRESH,
+				     (struct ngui_rect){x, y, 30, h},
+				     NGUI_ICON_REFRESH))
+			*action |= ACT_REFRESH;
+		x += 36;
+		if (ngui_icon_button(g, ID_NEW,
+				     (struct ngui_rect){x, y, 30, h},
+				     NGUI_ICON_PLUS))
+			*action |= ACT_NEW;
+	} else {
+		int w;
+
+		w = (int)ngui_text_width("Up") + 18;
+		if (ngui_button(g, ID_UP, (struct ngui_rect){x, y, w, h}, "Up"))
+			*action |= ACT_UP;
+		x += w + 6;
+		w = (int)ngui_text_width("Home") + 18;
+		if (ngui_button(g, ID_HOME, (struct ngui_rect){x, y, w, h},
+				"Home"))
+			*action |= ACT_HOME;
+		x += w + 6;
+		w = (int)ngui_text_width("Refresh") + 18;
+		if (ngui_button(g, ID_REFRESH, (struct ngui_rect){x, y, w, h},
+				"Refresh"))
+			*action |= ACT_REFRESH;
+		x += w + 6;
+		w = (int)ngui_text_width("New Folder") + 18;
+		if (ngui_button(g, ID_NEW, (struct ngui_rect){x, y, w, h},
+				"New Folder"))
+			*action |= ACT_NEW;
+	}
+}
+
+static void draw_path(struct ngui *g, struct ngui_rect r, unsigned int *action)
+{
+	int go_w = g->w < 260 ? 40 : 50;
+	struct ngui_rect in = {r.x + 6, r.y + 3, r.w - 12 - go_w - 6, 20};
+	struct ngui_rect go = {r.x + r.w - 6 - go_w, r.y + 3, go_w, 20};
+	int res;
+
+	ngui_fill_rect(g, r, g->theme.panel);
+	if (in.w < 40)
+		in.w = 40;
+	res = ngui_text_input(g, ID_PATH, in, &path_in);
+	if (res & NGUI_INPUT_SUBMIT)
+		*action |= ACT_NAV;
+	if (res & NGUI_INPUT_CANCEL)
+		ngui_set_focus(g, 0);
+	if (ngui_button(g, ID_GO, go, "Go"))
+		*action |= ACT_NAV;
+}
+
+static int path_under(const char *path, const char *base)
+{
+	unsigned int n = s_len(base);
+
+	if (!path || !base)
+		return 0;
+	if (s_eq(path, base))
+		return 1;
+	if (s_len(path) > n && path[n] == '/' &&
+	    path[0] == base[0]) {
+		unsigned int i;
+
+		for (i = 0; i < n; i++)
+			if (path[i] != base[i])
+				return 0;
+		return 1;
+	}
+	return 0;
+}
+
+static void draw_sidebar(struct ngui *g, struct ngui_rect r, unsigned int *action)
+{
+	int y = r.y + 4;
+	int row_y = r.y + 24;
+	unsigned int i;
+
+	ngui_fill_rect(g, r, g->theme.panel);
+	ngui_text(g, r.x + 8, y, "Places", g->theme.text_dim);
+
+	{
+		int hot = g->inside && g->mouse_x >= r.x &&
+			g->mouse_x < r.x + r.w &&
+			g->mouse_y >= row_y && g->mouse_y < row_y + FM_ROW_H;
+		int pressed = g->pressed && g->press_x >= r.x &&
+			g->press_x < r.x + r.w &&
+			g->press_y >= row_y && g->press_y < row_y + FM_ROW_H;
+
+		if (is_root(cwd))
+			ngui_fill_rect(g, (struct ngui_rect){r.x + 1, row_y,
+						       r.w - 2, FM_ROW_H - 1},
+				       g->theme.selection);
+		else if (hot)
+			ngui_fill_rect(g, (struct ngui_rect){r.x + 1, row_y,
+						       r.w - 2, FM_ROW_H - 1},
+				       g->theme.panel_alt);
+		ngui_icon(g, (struct ngui_rect){r.x + 6, row_y + 3, 16, 16},
+			  NGUI_ICON_HOME, g->theme.icon_folder);
+		ngui_text(g, r.x + 28, row_y + 3, "Root", g->theme.text);
+		if (pressed) {
+			s_copy(action_path, "/", sizeof(action_path));
+			*action |= ACT_LOAD;
+		}
+	}
+	row_y += FM_ROW_H;
+
+	for (i = 0; i < mount_count && row_y + FM_ROW_H <= r.y + r.h; i++) {
+		char mpath[FM_PATH_MAX];
+		int hot;
+		int active;
+
+		int pressed;
+
+		path_join("/", mounts[i].name, mpath, sizeof(mpath));
+		hot = g->inside && g->mouse_x >= r.x &&
+			g->mouse_x < r.x + r.w &&
+			g->mouse_y >= row_y && g->mouse_y < row_y + FM_ROW_H;
+		pressed = g->pressed && g->press_x >= r.x &&
+			g->press_x < r.x + r.w &&
+			g->press_y >= row_y && g->press_y < row_y + FM_ROW_H;
+		active = path_under(cwd, mpath);
+		if (active)
+			ngui_fill_rect(g, (struct ngui_rect){r.x + 1, row_y,
+						       r.w - 2, FM_ROW_H - 1},
+				       g->theme.selection);
+		else if (hot)
+			ngui_fill_rect(g, (struct ngui_rect){r.x + 1, row_y,
+						       r.w - 2, FM_ROW_H - 1},
+				       g->theme.panel_alt);
+		ngui_icon(g, (struct ngui_rect){r.x + 6, row_y + 3, 16, 16},
+			  NGUI_ICON_DISK, g->theme.icon_disk);
+		ngui_text_ellipsis(g, (struct ngui_rect){r.x + 28, row_y,
+					 r.w - 34, FM_ROW_H},
+				   r.x + 28, row_y + 3, mounts[i].name,
+				   g->theme.text);
+		if (pressed) {
+			s_copy(action_path, mpath, sizeof(action_path));
+			*action |= ACT_LOAD;
+		}
+		row_y += FM_ROW_H;
+	}
+	if (!mount_count)
+		ngui_text(g, r.x + 8, r.y + 50, "No volumes", g->theme.text_dim);
+	ngui_fill_rect(g, (struct ngui_rect){r.x + r.w - 1, r.y, 1, r.h},
+		       g->theme.border);
+}
+
+static void draw_list(struct ngui *g, struct ngui_rect r, unsigned int *action)
+{
+	unsigned int i;
+	int scrolls;
+	int row_w;
+	int hover_idx = -1;
+	int press_idx = -1;
+
+	ngui_fill_rect(g, r, g->theme.bg);
+	list_scroll.content_h = (int)(entry_count * FM_ROW_H);
+	ngui_scroll_begin(g, r, &list_scroll);
+	scrolls = list_scroll.content_h > list_scroll.view_h;
+	row_w = r.w - (scrolls ? FM_SCROLL_W : 0);
+
+	if (g->inside && g->mouse_x >= r.x && g->mouse_x < r.x + row_w &&
+	    g->mouse_y >= r.y && g->mouse_y < r.y + r.h &&
+	    list_scroll.content_h > 0) {
+		int hi = (g->mouse_y - r.y + list_scroll.offset) / FM_ROW_H;
+
+		if (hi >= 0 && hi < (int)entry_count)
+			hover_idx = hi;
+	}
+	if (g->pressed && g->press_x >= r.x &&
+	    g->press_x < r.x + row_w && g->press_y >= r.y &&
+	    g->press_y < r.y + r.h) {
+		press_idx = (g->press_y - r.y + list_scroll.offset) / FM_ROW_H;
+		if (press_idx < 0 || press_idx >= (int)entry_count)
+			press_idx = -1;
+	}
+
+	for (i = 0; i < entry_count; i++) {
+		struct fm_entry *e = &entries[i];
+		int y = r.y + (int)i * FM_ROW_H - list_scroll.offset;
+		int detail_x;
+		char detail[20];
+		unsigned int dn = 0;
+
+		if (y + FM_ROW_H <= r.y || y >= r.y + r.h)
+			continue;
+		if ((int)i == selected)
+			ngui_fill_rect(g, (struct ngui_rect){r.x, y, row_w,
+						       FM_ROW_H - 1},
+				       g->theme.selection);
+		else if ((int)i == hover_idx)
+			ngui_fill_rect(g, (struct ngui_rect){r.x, y, row_w,
+						       FM_ROW_H - 1},
+				       g->theme.panel_alt);
+		ngui_icon(g, (struct ngui_rect){r.x + 4, y + 3, 16, 16},
+			  e->is_mount ? NGUI_ICON_DISK :
+			  e->is_dir ? NGUI_ICON_FOLDER : NGUI_ICON_FILE,
+			  e->is_mount ? g->theme.icon_disk :
+			  e->is_dir ? g->theme.icon_folder : g->theme.icon_file);
+
+		if (e->is_mount) {
+			s_append(detail, &dn, e->detail, sizeof(detail));
+		} else if (e->is_dir) {
+			s_append(detail, &dn, "<DIR>", sizeof(detail));
+		} else {
+			format_size(e->size, detail, sizeof(detail));
+		}
+		detail_x = r.x + row_w - 6 - (int)ngui_text_width(detail);
+		if (detail_x < r.x + 30) {
+			detail[0] = '\0';
+			detail_x = r.x + row_w;
+		}
+		ngui_text_ellipsis(g, (struct ngui_rect){r.x + 28, y,
+					 detail_x - r.x - 30, FM_ROW_H},
+				   r.x + 28, y + 3, e->name,
+				   g->theme.text);
+		if (detail[0])
+			ngui_text(g, detail_x, y + 3, detail,
+				  g->theme.text_dim);
+	}
+	if (!entry_count)
+		ngui_text(g, r.x + 10, r.y + 8, "Empty directory",
+			  g->theme.text_dim);
+	ngui_scroll_end(g, r, &list_scroll);
+
+	if (press_idx >= 0) {
+		unsigned int now = u_ticks_ms();
+
+		select_index((unsigned int)press_idx);
+		if (press_idx == last_click_index &&
+		    now - last_click_ms < 500) {
+			*action |= ACT_OPEN;
+			last_click_index = -1;
+		} else {
+			last_click_index = press_idx;
+			last_click_ms = now;
+		}
+	}
+}
+
+static void draw_status(struct ngui *g, struct ngui_rect r)
+{
+	char buf[160];
+	unsigned int n = 0;
+	unsigned short color = g->theme.text;
+
+	ngui_fill_rect(g, r, g->theme.panel);
+	ngui_fill_rect(g, (struct ngui_rect){r.x, r.y, r.w, 1},
+		       g->theme.border);
+
+	if (status_text[0]) {
+		s_append(buf, &n, status_text, sizeof(buf));
+		if (status_error)
+			color = g->theme.error;
+	} else {
+		s_append_uint(buf, &n, entry_count, sizeof(buf));
+		s_append(buf, &n, entry_count == 1 ? " item" : " items", sizeof(buf));
+		if (truncated)
+			s_append(buf, &n, " (first 256)", sizeof(buf));
+		if (entry_count && selected >= 0 &&
+		    selected < (int)entry_count) {
+			s_append(buf, &n, "   ", sizeof(buf));
+			s_append(buf, &n, entries[selected].name, sizeof(buf));
+			if (entries[selected].is_mount) {
+				s_append(buf, &n, "  ", sizeof(buf));
+				s_append(buf, &n, entries[selected].detail,
+					 sizeof(buf));
+			} else if (!entries[selected].is_dir) {
+				char sz[20];
+
+				format_size(entries[selected].size, sz, sizeof(sz));
+				s_append(buf, &n, "  ", sizeof(buf));
+				s_append(buf, &n, sz, sizeof(buf));
+			}
+		}
+	}
+	ngui_text_ellipsis(g, r, r.x + 6, r.y + (r.h - 16) / 2, buf, color);
+}
+
+static void draw_modal(struct ngui *g, unsigned int *action)
+{
+	struct ngui_rect all = {0, 0, g->w, g->h};
+	int pw = g->w - 24;
+	int ph = g->h - 16;
+	int x, y, title_y, label_y, input_y, buttons_y;
+	int bw, bx;
+	struct ngui_rect input;
+	int res;
+
+	if (pw > 340)
+		pw = 340;
+	if (pw < 160)
+		pw = 160;
+	if (ph > 130)
+		ph = 130;
+	if (ph < 96)
+		ph = 96;
+	x = (g->w - pw) / 2;
+	y = (g->h - ph) / 2;
+	title_y = y + (ph >= 120 ? 9 : 6);
+	label_y = y + (ph >= 120 ? 30 : 22);
+	input_y = y + (ph >= 120 ? 48 : 38);
+	buttons_y = y + ph - 34;
+	bw = (pw - 36) / 2;
+	if (bw > 80)
+		bw = 80;
+	if (bw < 50)
+		bw = 50;
+	bx = x + (pw - (bw * 2 + 8)) / 2;
+
+	ngui_fill_rect(g, all, g->theme.bg);
+	ngui_fill_rect(g, (struct ngui_rect){x + 3, y + 3, pw, ph},
+		       NGUI_RGB(8, 10, 16));
+	ngui_fill_rect(g, (struct ngui_rect){x, y, pw, ph}, g->theme.panel);
+	ngui_frame(g, (struct ngui_rect){x, y, pw, ph}, g->theme.accent);
+	ngui_text(g, x + 12, title_y, "New folder", g->theme.text);
+	ngui_text(g, x + 12, label_y, "Name", g->theme.text_dim);
+
+	input = (struct ngui_rect){x + 12, input_y, pw - 24, 22};
+	res = ngui_text_input(g, ID_NEW_INPUT, input, &new_in);
+	if (res & NGUI_INPUT_SUBMIT)
+		*action |= ACT_CREATE;
+	if (res & NGUI_INPUT_CANCEL)
+		*action |= ACT_CANCEL;
+	if (buttons_y > input_y + 44)
+		ngui_text(g, x + 12, input_y + 28,
+			  "Enter creates, Esc cancels", g->theme.text_dim);
+
+	if (ngui_button(g, ID_CREATE,
+			(struct ngui_rect){bx, buttons_y, bw, 24}, "Create"))
+		*action |= ACT_CREATE;
+	if (ngui_button(g, ID_CANCEL,
+			(struct ngui_rect){bx + bw + 8, buttons_y, bw, 24},
+			"Cancel"))
+		*action |= ACT_CANCEL;
+}
+
+static void draw(struct ngui *g, unsigned int *action)
+{
+	struct ngui_rect r = {0, 0, g->w, g->h};
+	struct ngui_rect toolbar, path, status, body;
+	int side_w;
+
+	if (modal) {
+		draw_modal(g, action);
+		return;
+	}
+	toolbar = ngui_cut_top(&r, FM_TOOLBAR_H);
+	path = ngui_cut_top(&r, FM_PATH_H);
+	status = ngui_cut_bottom(&r, FM_STATUS_H);
+	body = r;
+
+	draw_toolbar(g, toolbar, action);
+	draw_path(g, path, action);
+	if (body.w > 0 && body.h > 0) {
+		side_w = g->w >= 520 ? FM_SIDEBAR_W : 0;
+		if (side_w) {
+			struct ngui_rect side = ngui_cut_left(&body, side_w);
+
+			draw_sidebar(g, side, action);
+		}
+		draw_list(g, body, action);
+	}
+	draw_status(g, status);
+}
+
+static void handle_keys(struct ngui *g, unsigned int *action)
+{
+	int k;
+
+	if (ngui_focus(g))
+		return;
+	if (ngui_scroll_keys(g, &list_scroll))
+		ngui_invalidate(g);
+	while ((k = ngui_key(g)) != 0) {
+		switch (k) {
+		case NGUI_KEY_UP:
+			if (entry_count && selected > 0)
+				select_index((unsigned int)(selected - 1));
+			break;
+		case NGUI_KEY_DOWN:
+			if (entry_count &&
+			    selected + 1 < (int)entry_count)
+				select_index((unsigned int)(selected + 1));
+			break;
+		case NGUI_KEY_HOME:
+			if (entry_count)
+				select_index(0);
+			break;
+		case NGUI_KEY_END:
+			if (entry_count)
+				select_index(entry_count - 1);
+			break;
+		case '\n':
+		case '\r':
+			*action |= ACT_OPEN;
+			break;
+		case NGUI_KEY_BACKSPACE:
+			*action |= ACT_PARENT;
+			break;
+		case NGUI_KEY_TAB:
+		case 0x0C:              /* Ctrl+L */
+			ngui_set_focus(g, ID_PATH);
+			break;
+		case 0x0E:              /* Ctrl+N */
+			*action |= ACT_NEW;
+			break;
+		case 0x12:              /* Ctrl+R */
+			*action |= ACT_REFRESH;
+			break;
+		case NGUI_KEY_ESC:
+			*action |= ACT_CLOSE;
+			break;
+		default:
+			break;
+		}
+	}
+}
+
+void _start(const char *arg)
+{
+	struct win_info wi;
+	char start[FM_PATH_MAX];
+	int quit = 0;
+
+	(void)arg;
+	if (u_win_create(600, 400, "Files", &wi) != 0) {
+		u_puts("files: no graphics window\n");
+		u_exit(1);
+	}
+	ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
+	path_in.placeholder = "/path";
+	new_in.placeholder = "NAME";
+	list_scroll.offset = 0;
+	load_mounts();
+
+	if (u_getcwd(start, sizeof(start)) != 0 || !start[0])
+		s_copy(start, "/", sizeof(start));
+	if (load_path(start) != 0)
+		load_path("/");
+
+	for (;;) {
+		unsigned int action = 0;
+		unsigned int now;
+
+		now = u_ticks_ms();
+		if (status_until && (int)(now - status_until) >= 0) {
+			status_text[0] = '\0';
+			status_until = 0;
+			status_error = 0;
+			ngui_invalidate(&ui);
+		}
+		if (!ngui_begin(&ui)) {
+			u_sleep_ms(10);
+			continue;
+		}
+		draw(&ui, &action);
+		if (modal) {
+			int k = ngui_key_peek(&ui);
+
+			if (k == NGUI_KEY_ESC) {
+				ngui_key(&ui);
+				action |= ACT_CANCEL;
+			}
+		} else {
+			handle_keys(&ui, &action);
+		}
+		ngui_end(&ui);
+		if (action)
+			do_action(action);
+		if (action & ACT_CLOSE)
+			quit = 1;
+		if (quit)
+			break;
+		u_sleep_ms(10);
+	}
+	u_win_close();
+	u_exit(0);
+}

+ 10 - 0
ucmd_lib.h

@@ -295,6 +295,16 @@ static inline int u_win_ack_resize(void)
 	return u_syscall0(SYS_WIN_ACK_RESIZE);
 }
 
+static inline int u_win_pointer(struct win_pointer *out)
+{
+	return u_syscall1(SYS_WIN_POINTER, (int)out);
+}
+
+static inline int u_win_event(struct win_event *out)
+{
+	return u_syscall1(SYS_WIN_EVENT, (int)out);
+}
+
 static inline int u_wm_resize(unsigned int index, unsigned int cw,
 			      unsigned int ch)
 {

+ 8 - 0
ucmd_wm.c

@@ -72,6 +72,10 @@ static void draw_bar(void)
 		   WM_BTN_COLORS_H, 0x963CD2u);
 	usurf_text(&screen, WM_BTN_COLORS_X + 4, WM_BTN_COLORS_Y + 3, "+ Colors",
 		   COL_WHITE, 0, 0, fbw, WM_BAR_H);
+	usurf_fill(&screen, WM_BTN_FILES_X, WM_BTN_FILES_Y, WM_BTN_FILES_W,
+		   WM_BTN_FILES_H, 0x963CD2u);
+	usurf_text(&screen, WM_BTN_FILES_X + 4, WM_BTN_FILES_Y + 3, "Files",
+		   COL_WHITE, 0, 0, fbw, WM_BAR_H);
 
 	/* Rainbow separator (same y as the kernel's old bar). */
 	for (x = 0; x < fbw; x++) {
@@ -451,6 +455,10 @@ static void handle_mouse(struct wm_status *st)
 			   mx < WM_BTN_COLORS_X + WM_BTN_COLORS_W &&
 			   my < WM_BTN_COLORS_Y + WM_BTN_COLORS_H) {
 			spawn_app("/ramfs/bin/colors");
+		} else if (mx >= WM_BTN_FILES_X && my >= WM_BTN_FILES_Y &&
+			   mx < WM_BTN_FILES_X + WM_BTN_FILES_W &&
+			   my < WM_BTN_FILES_Y + WM_BTN_FILES_H) {
+			spawn_app("/ramfs/bin/files");
 		} else {
 			int idx = window_at(mx, my);
 

+ 110 - 0
wm.c

@@ -64,6 +64,12 @@ static int drag_window;      /* index into windows[] */
 static int drag_dx, drag_dy; /* mouse offset from window origin */
 static unsigned int prev_buttons;
 
+/* Content pointer routing for generic 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;
+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;
@@ -97,6 +103,7 @@ 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(unsigned int px, unsigned int py);
 
 void wm_request_redraw(void)
 {
@@ -340,6 +347,7 @@ int wm_server_win_create(const struct win_create *in, struct win_info *out)
 	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';
@@ -463,6 +471,45 @@ int wm_server_win_ack_resize(void)
 	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 - (int)w->x - 1;
+	cy = (int)my - (int)w->y - 1 - WM_TITLE_H;
+	out->x = cx;
+	out->y = cy;
+	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(mx, 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_resize(unsigned int index, unsigned int cw, unsigned int ch)
 {
 	struct window *w;
@@ -838,6 +885,62 @@ int wm_server_close(unsigned int index)
 	return 0;
 }
 
+static void wm_queue_ptr_event(struct window *w, unsigned int type,
+			       int x, int y, unsigned int buttons)
+{
+	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_head = next;
+}
+
+/* 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 (press & 1) {
+		int idx = wm_window_at(mx, my);
+
+		if (idx >= 0) {
+			struct window *w = &windows[idx];
+			int cx = (int)mx - (int)w->x - 1;
+			int cy = (int)my - (int)w->y - 1 - WM_TITLE_H;
+
+			if (w->canvas_window && cx >= 0 && cy >= 0 &&
+			    cx < (int)w->canvas.w && cy < (int)w->canvas.h &&
+			    !(mx >= w->x + w->pw - 16 &&
+			      my >= w->y + w->ph - 16)) {
+				wm_queue_ptr_event(w, WIN_EV_POINTER_DOWN,
+						   cx, cy, buttons);
+				wm_ptr_capture = idx;
+			}
+		}
+	}
+	if ((release & 1) && wm_ptr_capture >= 0) {
+		struct window *w = &windows[wm_ptr_capture];
+
+		if (w->used && w->canvas_window) {
+			int cx = (int)mx - (int)w->x - 1;
+			int cy = (int)my - (int)w->y - 1 - WM_TITLE_H;
+
+			wm_queue_ptr_event(w, WIN_EV_POINTER_UP,
+					   cx, cy, buttons);
+		}
+		wm_ptr_capture = -1;
+	}
+	wm_ptr_buttons = buttons;
+}
+
 int wm_server_poll(struct wm_status *out)
 {
 	if (!wm_server_is_wm() || !out)
@@ -846,6 +949,7 @@ int wm_server_poll(struct wm_status *out)
 	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);
 	out->full_redraw = wm_full_redraw ? 1 : 0;
 	out->kexec_busy = kexec_busy ? 1 : 0;
 	out->kexec_progress = kexec_progress;
@@ -922,6 +1026,8 @@ static void wm_server_wm_died(void)
 	wm_pid = 0;
 	wm_cr3 = 0;
 	wm_kernel_fallback = 0;
+	wm_ptr_buttons = 0;
+	wm_ptr_capture = -1;
 	for (i = 0; i < MAX_WINDOWS; i++) {
 		windows[i].mapped_kind = WM_KIND_NONE;
 		windows[i].mapped_gen = 0;
@@ -1037,6 +1143,8 @@ void wm_server_init(void)
 	wm_full_redraw = 1;
 	drag_active = 0;
 	prev_buttons = 0;
+	wm_ptr_buttons = 0;
+	wm_ptr_capture = -1;
 	wm_expected_pid = 0;
 	wm_pid = 0;
 	wm_cr3 = 0;
@@ -1193,6 +1301,8 @@ static void wm_close_window(unsigned int idx)
 {
 	struct window *w = &windows[idx];
 
+	if (wm_ptr_capture == (int)idx)
+		wm_ptr_capture = -1;
 	/* A terminal/key reader blocked on this window must not sleep through
 	 * the close; process_kill() also removes it from the queue. */
 	wait_wake_all(&w->key_wq);

+ 5 - 0
wm.h

@@ -28,6 +28,9 @@ struct window {
 	int keys[16];
 	unsigned int key_head, key_tail;
 	struct wait_queue key_wq;
+	/* Pointer press/release edges for a generic canvas window. */
+	struct win_event ptr_events[8];
+	unsigned int ptr_head, ptr_tail;
 	/* Ring-3 WM content mapping/bookkeeping. */
 	unsigned int content_gen; /* bumped when the content kind/session changes */
 	unsigned int mapped_kind;
@@ -71,6 +74,8 @@ int  wm_server_win_ack_resize(void);
 int  wm_server_resize(unsigned int index, unsigned int cw, unsigned int ch);
 int  wm_server_key_poll(void);
 int  wm_server_key_wait(unsigned int timeout_ms);
+int  wm_server_win_pointer(struct win_pointer *out);
+int  wm_server_win_event(struct win_event *out);
 void wm_content_changed(struct text_screen *screen);
 void wm_server_unmap_screen(struct text_screen *screen);
 void wm_server_unmap_canvas(struct text_screen *screen, const void *canvas);

+ 22 - 0
wm_abi.h

@@ -22,6 +22,10 @@
 #define WM_BTN_COLORS_Y WM_BTN_Y
 #define WM_BTN_COLORS_W 72
 #define WM_BTN_COLORS_H WM_BTN_H
+#define WM_BTN_FILES_X (WM_BTN_COLORS_X + WM_BTN_COLORS_W + WM_BTN_GAP)
+#define WM_BTN_FILES_Y WM_BTN_Y
+#define WM_BTN_FILES_W 64
+#define WM_BTN_FILES_H WM_BTN_H
 #define WM_TITLE_H 22
 #define WM_CLOSE_W 16
 
@@ -117,4 +121,22 @@ struct win_info {
 	unsigned int id;
 };
 
+/* Pointer input for generic canvas windows.  The ring-3 WM queues press and
+ * release edges; the command polls the current position for hover and drags. */
+struct win_pointer {
+	int x, y;                  /* relative to the canvas, may be outside */
+	unsigned int buttons;      /* bit0 left, bit1 right, bit2 middle */
+	unsigned int inside;       /* pointer is over this window's content */
+	unsigned int focused;
+};
+
+#define WIN_EV_POINTER_DOWN 1
+#define WIN_EV_POINTER_UP   2
+
+struct win_event {
+	unsigned int type;         /* WIN_EV_POINTER_* */
+	int x, y;                  /* content-relative */
+	unsigned int buttons;      /* buttons after the event */
+};
+
 #endif