Преглед изворни кода

Render terminal windows as canvases

The ring-3 WM had two content paths: text windows mapped their 64x32
cell grid and the WM rendered glyphs itself, while gfx commands mapped
an RGB565 canvas.  The text path kept the 8x10 cell metric in the window
server and an 8x16 metric in the WM, and would have needed its own layer
and damage code.

Give every window a canvas.  The window server now renders the fallback
kernel terminal's cell grid into a 512x512 RGB565 canvas through the new
text_canvas renderer (8x16 cells, VGA palette, damaged cell blocks,
cursor overlay) and exposes it through the normal canvas map/damage/ack
path.  The ring-3 WM composites canvases only, uses a content-relative
pixel damage rect, and drops the WM_KIND_TEXT/usurf_cells/text-cursor
path.  Focus changes repaint the cursor cell in the text canvas; the
text-only wm_window_info fields and WM_KIND_TEXT are gone.

Add a host test for the renderer (make test-textcanvas) with a golden
hash plus explicit glyph, palette, cell-boundary, cursor and block
checks, and raise the QMP test boot waits for the larger terminal.

Verified with the live ring-3 terminal and with a pack built without
bin/term (the kernel fallback terminal under the ring-3 WM); both render
8x16 and accept `help`.  The QMP cursor suite passes in all modes and
the non-hotplug/xHCI/EHCI keyboard tests pass; the OHCI keyboard hotplug
case still fails on the existing USB hotplug path.  The pack is 259,818
bytes, 1,301 below the legacy cap.
Johannes Zellner пре 1 недеља
родитељ
комит
41d8e5b8be
11 измењених фајлова са 573 додато и 164 уклоњено
  1. 1 0
      .gitignore
  2. 13 3
      Makefile
  3. 118 0
      text_canvas.c
  4. 28 0
      text_canvas.h
  5. 233 0
      text_canvas_test.c
  6. 1 1
      tools/qmp_cursor_test.py
  7. 2 2
      tools/qmp_keyboard_test.py
  8. 19 43
      ucmd_wm.c
  9. 151 108
      wm.c
  10. 5 0
      wm.h
  11. 2 7
      wm_abi.h

+ 1 - 0
.gitignore

@@ -13,4 +13,5 @@ mkpack
 tls_test
 img_test
 img_test_asan
+text_canvas_test
 mkrootfs

+ 13 - 3
Makefile

@@ -31,7 +31,7 @@ EFI_LDFLAGS = -nostdlib -Wl,--subsystem,10 -Wl,-e,efi_main \
               -Wl,--image-base,0x0 -Wl,--dynamicbase \
               -Wl,--enable-reloc-section -Wl,--no-insert-timestamp
 
-.PHONY: all clean run run-smp run-floppy run-usb run-uhci run-ehci run-ehci-asix run-xhci run-xhci-asix run-net run-e1000 run-ahci test-usb run-serial floppy usb uefi run-uefi test-uefi flash space prog bootstrap dev net-dev cmds-dev net-cmds browser-dev gallery-dev netmon monitor test-crypto test-images test-images-asan kernel-dev kernel-net-dev rootfs
+.PHONY: all clean run run-smp run-floppy run-usb run-uhci run-ehci run-ehci-asix run-xhci run-xhci-asix run-net run-e1000 run-ahci test-usb run-serial floppy usb uefi run-uefi test-uefi flash space prog bootstrap dev net-dev cmds-dev net-cmds browser-dev gallery-dev netmon monitor test-crypto test-images test-images-asan test-textcanvas kernel-dev kernel-net-dev rootfs
 
 all: floppy.img usb.img final.bin root.img
 
@@ -56,6 +56,9 @@ font_8x8.o: font_8x8.c font_8x8.h
 font_8x16.o: font_8x16.c font_8x16.h
 	$(CC) $(CFLAGS) -o $@ $<
 
+text_canvas.o: text_canvas.c text_canvas.h font_8x16.h
+	$(CC) $(CFLAGS) -o $@ $<
+
 fbtext.o: fbtext.c fbtext.h font_8x8.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
@@ -344,7 +347,7 @@ sys_net.o: sys_net.c sys_net.h syscall.h dhcp.h dns.h net.h netdev.h netlog.h pr
 syscall_stub.o: syscall_stub.S irqret.inc
 	$(AS) $(ASFLAGS) -o $@ $<
 
-kernel.elf: kernel.ld 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 reboot.o poweroff.o disk.o fat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o blit.o cpu.o thermal.o it87.o tpec.o power.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o intel.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o gfx.o sys_gfx.o display.o mouse.o uhci.o ohci.o ehci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o sys_net.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o acpi.o lapic.o smp.o ap_tramp_embed.o prog_embed.o space_embed.o inflate.o ramfs.o share.o payload.o
+kernel.elf: kernel.ld 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 reboot.o poweroff.o disk.o fat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o blit.o cpu.o thermal.o it87.o tpec.o power.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o intel.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o font_8x16.o text_canvas.o fbtext.o gfx.o sys_gfx.o display.o mouse.o uhci.o ohci.o ehci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o sys_net.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o acpi.o lapic.o smp.o ap_tramp_embed.o prog_embed.o space_embed.o inflate.o ramfs.o share.o payload.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $(filter-out kernel.ld,$^)
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -714,6 +717,13 @@ test-images-asan: $(IMGTEST_SRCS) $(IMGTEST_DEPS)
 	gcc -O1 -g -fwrapv -fsanitize=address,undefined -I. -o img_test_asan $(IMGTEST_SRCS)
 	./img_test_asan
 
+# Host test for the 8x16 terminal-cell renderer (text_canvas.c).
+text_canvas_test: text_canvas_test.c text_canvas.c text_canvas.h font_8x16.c font_8x16.h
+	gcc -O2 -Wall -Wextra -I. -o $@ text_canvas_test.c text_canvas.c font_8x16.c
+
+test-textcanvas: text_canvas_test
+	./text_canvas_test
+
 mkpack: mkpack.c
 	gcc -O2 -o $@ $<
 
@@ -1041,4 +1051,4 @@ kernel-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 thermal.o it87.o tpec.o power.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o intel.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 ehci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o inflate.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.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_gz.bin cmds_pack.o rootfs.bin cmds.img mkrootfs ramfs.o share.o payload.o mkpack uwm_lib.o ngui.o img.o img_mem.o img_blit.o img_png.o img_bmp.o img_gif.o img_jpeg.o img_tga.o img_pnm.o img_ico.o img_pam.o img_gif_anim.o img_file.o browser.o browser_html.o uhttp.o browser.elf img_test img_test_asan *.boot.tmp $(addsuffix .o,$(UCMDS)) $(addsuffix .elf,$(UCMDS)) gallery.o gallery.elf $(TLSLIB_OBJS) tls_test bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor BOOTX64.EFI esp.img usb-efi.img efi/main.o efi/payload.o efi/tramp.o efi/tramp.elf efi/tramp.bin efi/efi_entry.h
+	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 thermal.o it87.o tpec.o power.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o intel.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o font_8x16.o text_canvas.o fbtext.o gfx.o sys_gfx.o display.o mouse.o uhci.o ohci.o ehci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o inflate.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.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_gz.bin cmds_pack.o rootfs.bin cmds.img mkrootfs ramfs.o share.o payload.o mkpack uwm_lib.o ngui.o img.o img_mem.o img_blit.o img_png.o img_bmp.o img_gif.o img_jpeg.o img_tga.o img_pnm.o img_ico.o img_pam.o img_gif_anim.o img_file.o browser.o browser_html.o uhttp.o browser.elf img_test img_test_asan text_canvas_test *.boot.tmp $(addsuffix .o,$(UCMDS)) $(addsuffix .elf,$(UCMDS)) gallery.o gallery.elf $(TLSLIB_OBJS) tls_test bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor BOOTX64.EFI esp.img usb-efi.img efi/main.o efi/payload.o efi/tramp.o efi/tramp.elf efi/tramp.bin efi/efi_entry.h

+ 118 - 0
text_canvas.c

@@ -0,0 +1,118 @@
+/* 8x16 text-cell renderer for window content canvases (see text_canvas.h).
+ *
+ * The cell grid is the terminal's internal state (char + VGA attribute); this
+ * file is the only place that knows the terminal cell geometry.  The window
+ * server calls it with the damaged cell block and composites the resulting
+ * RGB565 canvas.  No kernel dependencies, so the host test exercises the same
+ * code. */
+
+#include "text_canvas.h"
+#include "font_8x16.h"
+
+#define TEXT_CURSOR_W 2
+
+/* Standard VGA palette expanded to RGB565; the same values as fbtext.c's
+ * attr_to_rgb() and the userland WM palette. */
+static const unsigned short text_pal565[16] = {
+	0x0000, /*  0 black */
+	0x0015, /*  1 blue */
+	0x0540, /*  2 green */
+	0x0555, /*  3 cyan */
+	0xA800, /*  4 red */
+	0xA815, /*  5 magenta */
+	0xAAA0, /*  6 brown */
+	0xAD55, /*  7 light gray */
+	0x52AA, /*  8 dark gray */
+	0x52BF, /*  9 light blue */
+	0x57EA, /* 10 light green */
+	0x57FF, /* 11 light cyan */
+	0xFAAA, /* 12 light red */
+	0xFABF, /* 13 light magenta */
+	0xFFEA, /* 14 yellow */
+	0xFFFF, /* 15 white */
+};
+
+static void put_cell(struct text_canvas *c, unsigned int col, unsigned int row,
+		     unsigned char ch, unsigned char attr, int cursor)
+{
+	unsigned int x0 = col * TEXT_CELL_W;
+	unsigned int y0 = row * TEXT_CELL_H;
+	unsigned short fg = text_pal565[attr & 0x0F];
+	unsigned short bg = text_pal565[(attr >> 4) & 0x0F];
+	const unsigned char *glyph;
+	unsigned int gy, gx;
+
+	if (!c || !c->px || x0 >= c->w || y0 >= c->h)
+		return;
+
+	glyph = &font_8x16[(unsigned int)ch * FONT_8X16_ROWS];
+	for (gy = 0; gy < TEXT_CELL_H; gy++) {
+		unsigned int y = y0 + gy;
+		unsigned short *dst;
+		unsigned char bits;
+
+		if (y >= c->h)
+			break;
+		dst = c->px + (unsigned long)y * c->w;
+		bits = (gy < FONT_8X16_ROWS) ? glyph[gy] : 0;
+		for (gx = 0; gx < TEXT_CELL_W; gx++) {
+			unsigned int x = x0 + gx;
+
+			if (x >= c->w)
+				break;
+			dst[x] = (bits & (0x80u >> gx)) ? fg : bg;
+		}
+	}
+
+	if (!cursor)
+		return;
+
+	/* Block cursor: a full-cell-height bar in the first two columns. */
+	for (gy = 0; gy < TEXT_CELL_H; gy++) {
+		unsigned int y = y0 + gy;
+		unsigned short *dst;
+		unsigned int x;
+
+		if (y >= c->h)
+			break;
+		dst = c->px + (unsigned long)y * c->w;
+		for (x = 0; x < TEXT_CURSOR_W; x++) {
+			if (x0 + x >= c->w)
+				break;
+			dst[x0 + x] = text_pal565[15];
+		}
+	}
+}
+
+void text_canvas_render(struct text_canvas *c,
+			const unsigned short *cells,
+			unsigned int cols, unsigned int rows,
+			unsigned int col0, unsigned int row0,
+			unsigned int ncols, unsigned int nrows,
+			unsigned int cursor, int cursor_visible, int focused)
+{
+	unsigned int col, row;
+
+	if (!c || !c->px || !cells || !cols || !rows)
+		return;
+	if (col0 >= cols || row0 >= rows)
+		return;
+	if (ncols > cols - col0)
+		ncols = cols - col0;
+	if (nrows > rows - row0)
+		nrows = rows - row0;
+	if (!ncols || !nrows)
+		return;
+
+	for (row = row0; row < row0 + nrows; row++) {
+		for (col = col0; col < col0 + ncols; col++) {
+			unsigned int idx = row * cols + col;
+			unsigned short cell = cells[idx];
+			unsigned char ch = (unsigned char)(cell & 0xFF);
+			unsigned char attr = (unsigned char)(cell >> 8);
+			int cur = (idx == cursor && cursor_visible && focused);
+
+			put_cell(c, col, row, ch, attr, cur);
+		}
+	}
+}

+ 28 - 0
text_canvas.h

@@ -0,0 +1,28 @@
+/* 8x16 text-cell renderer for window content canvases.
+ *
+ * The terminal keeps its 64x32 char+attribute grid; the window server renders
+ * damaged cells into an RGB565 canvas and the WM composites that canvas like
+ * any other window content.  Freestanding and host-testable. */
+#ifndef TEXT_CANVAS_H
+#define TEXT_CANVAS_H
+
+#define TEXT_CELL_W 8
+#define TEXT_CELL_H 16
+
+struct text_canvas {
+	unsigned short *px;   /* RGB565, w*h, tightly packed */
+	unsigned int w, h;
+};
+
+/* Render the cell block [col0, col0+ncols) x [row0, row0+nrows) of a
+ * cols x rows cell grid at the canvas top-left.  Every pixel of the block is
+ * written (background included).  `cursor` is a linear cell index; the cursor
+ * bar is drawn when visible and focused.  Out-of-range cells are clipped. */
+void text_canvas_render(struct text_canvas *c,
+			const unsigned short *cells,
+			unsigned int cols, unsigned int rows,
+			unsigned int col0, unsigned int row0,
+			unsigned int ncols, unsigned int nrows,
+			unsigned int cursor, int cursor_visible, int focused);
+
+#endif

+ 233 - 0
text_canvas_test.c

@@ -0,0 +1,233 @@
+/* Host test for the 8x16 terminal-cell renderer (make test-textcanvas).
+ * Exercises the renderer directly; no kernel headers are involved. */
+
+#include <stdio.h>
+#include "text_canvas.h"
+#include "font_8x16.h"
+
+#define COLS 64u
+#define ROWS 32u
+#define W (COLS * TEXT_CELL_W)
+#define H (ROWS * TEXT_CELL_H)
+
+/* Hash of the fully rendered reference grid.  Regenerate only when the font,
+ * palette or cell metrics intentionally change. */
+#define GOLDEN 0x8598C6C9u
+
+static unsigned short cells[COLS * ROWS];
+static unsigned short fb[W * H];
+static int failures;
+
+/* Same standard VGA palette as text_canvas.c, in RGB565. */
+static const unsigned short pal565[16] = {
+	0x0000, 0x0015, 0x0540, 0x0555,
+	0xA800, 0xA815, 0xAAA0, 0xAD55,
+	0x52AA, 0x52BF, 0x57EA, 0x57FF,
+	0xFAAA, 0xFABF, 0xFFEA, 0xFFFF,
+};
+
+static void expect(unsigned int x, unsigned int y, unsigned short want,
+		   const char *what)
+{
+	unsigned short got;
+
+	if (x >= W || y >= H) {
+		printf("FAIL %s: (%u,%u) out of range\n", what, x, y);
+		failures++;
+		return;
+	}
+	got = fb[y * W + x];
+	if (got != want) {
+		printf("FAIL %s at (%u,%u): got 0x%04X want 0x%04X\n",
+		       what, x, y, got, want);
+		failures++;
+	}
+}
+
+static int glyph_bit(char ch, unsigned int row, unsigned int col)
+{
+	return (font_8x16[(unsigned int)(unsigned char)ch * FONT_8X16_ROWS +
+			  row] >> (7 - col)) & 1;
+}
+
+static void fill_cells(unsigned short value)
+{
+	unsigned int i;
+
+	for (i = 0; i < COLS * ROWS; i++)
+		cells[i] = value;
+}
+
+static void put_text(unsigned int col, unsigned int row, const char *s,
+		     unsigned char attr)
+{
+	unsigned int i;
+
+	for (i = 0; s[i] && col + i < COLS; i++)
+		cells[row * COLS + col + i] =
+			(unsigned short)(((unsigned short)attr << 8) |
+					(unsigned char)s[i]);
+}
+
+static unsigned int hash565(const unsigned short *px, unsigned int n)
+{
+	unsigned int h = 2166136261u;
+	unsigned int i;
+
+	for (i = 0; i < n; i++) {
+		h = (h ^ (px[i] & 0xFF)) * 16777619u;
+		h = (h ^ (px[i] >> 8)) * 16777619u;
+	}
+	return h;
+}
+
+int main(void)
+{
+	struct text_canvas c = { fb, W, H };
+	unsigned int cursor = 3 * COLS + 5;
+	unsigned int i, hash;
+
+	fill_cells(0x0720);              /* space, light gray on black */
+	put_text(0, 0, "Nebs OS 8x16", 0x0F);
+	put_text(0, 1, "0123456789", 0x1E);
+	put_text(2, 2, "ABCDEFGH", 0x4A);
+	cells[1 * COLS + 12] = 0x1E20;   /* blue background space */
+	cells[3 * COLS + 4] = 0x4A20;    /* red background space sentinel */
+	cells[3 * COLS + 5] = 0x0020;    /* black space under the cursor */
+
+	/* The full render must cover every pixel. */
+	for (i = 0; i < W * H; i++)
+		fb[i] = 0xDEAD;
+	text_canvas_render(&c, cells, COLS, ROWS, 0, 0, COLS, ROWS,
+			   cursor, 1, 1);
+	for (i = 0; i < W * H; i++) {
+		if (fb[i] == 0xDEAD) {
+			printf("FAIL full render left sentinel at pixel %u\n", i);
+			failures++;
+			break;
+		}
+	}
+
+	hash = hash565(fb, W * H);
+	if (hash != GOLDEN) {
+		printf("FAIL full render hash 0x%08X, expected 0x%08X\n",
+		       hash, GOLDEN);
+		failures++;
+	}
+
+	/* 8x16 glyph mapping: 'A' at (2,2), attr 0x4A = light green on red. */
+	{
+		unsigned int row, bit;
+		int set_seen = 0, clear_seen = 0;
+
+		for (row = 0; row < FONT_8X16_ROWS; row++) {
+			for (bit = 0; bit < 8; bit++) {
+				unsigned int x = 2 * TEXT_CELL_W + bit;
+				unsigned int y = 2 * TEXT_CELL_H + row;
+				int on = glyph_bit('A', row, bit);
+
+				if (on && !set_seen) {
+					expect(x, y, pal565[10], "glyph set");
+					set_seen = 1;
+				}
+				if (!on && !clear_seen) {
+					expect(x, y, pal565[4], "glyph clear");
+					clear_seen = 1;
+				}
+			}
+		}
+		if (!set_seen || !clear_seen) {
+			printf("FAIL glyph test could not find set/clear bits\n");
+			failures++;
+		}
+	}
+
+	/* Cell boundary: row 0 background at y=15, row 1 at y=16. */
+	expect(12 * TEXT_CELL_W, 15, pal565[0], "row 0 bottom");
+	expect(12 * TEXT_CELL_W, 16, pal565[1], "row 1 top");
+
+	/* Cursor: black space at (5,3), white 2x16 bar. */
+	expect(5 * TEXT_CELL_W + 0, 3 * TEXT_CELL_H + 0, pal565[15],
+	       "cursor x0");
+	expect(5 * TEXT_CELL_W + 1, 3 * TEXT_CELL_H + 0, pal565[15],
+	       "cursor x1");
+	expect(5 * TEXT_CELL_W + 2, 3 * TEXT_CELL_H + 0, pal565[0],
+	       "cursor x2");
+	expect(5 * TEXT_CELL_W + 0, 3 * TEXT_CELL_H + 15, pal565[15],
+	       "cursor bottom");
+	expect(5 * TEXT_CELL_W + 0, 4 * TEXT_CELL_H, pal565[0],
+	       "cursor next row");
+
+	/* Unfocused windows must not show the cursor. */
+	text_canvas_render(&c, cells, COLS, ROWS, 0, 0, COLS, ROWS,
+			   cursor, 1, 0);
+	expect(5 * TEXT_CELL_W + 0, 3 * TEXT_CELL_H + 0, pal565[0],
+	       "cursor unfocused");
+
+	/* Block update: only cell (5,3) changes. */
+	expect(4 * TEXT_CELL_W, 3 * TEXT_CELL_H, pal565[4],
+	       "sentinel before block");
+	cells[3 * COLS + 5] = (0x0F << 8) | 'X';
+	text_canvas_render(&c, cells, COLS, ROWS, 5, 3, 1, 1,
+			   0xFFFFFFFFu, 0, 0);
+	expect(4 * TEXT_CELL_W, 3 * TEXT_CELL_H, pal565[4],
+	       "sentinel after block");
+	expect(5 * TEXT_CELL_W, 4 * TEXT_CELL_H, pal565[0],
+	       "row 4 after block");
+	{
+		unsigned int row, bit;
+		int set_seen = 0, clear_seen = 0;
+
+		for (row = 0; row < FONT_8X16_ROWS; row++) {
+			for (bit = 0; bit < 8; bit++) {
+				unsigned int x = 5 * TEXT_CELL_W + bit;
+				unsigned int y = 3 * TEXT_CELL_H + row;
+				int on = glyph_bit('X', row, bit);
+
+				if (on && !set_seen) {
+					expect(x, y, pal565[15], "X set");
+					set_seen = 1;
+				}
+				if (!on && !clear_seen) {
+					expect(x, y, pal565[0], "X clear");
+					clear_seen = 1;
+				}
+			}
+		}
+		if (!set_seen || !clear_seen) {
+			printf("FAIL X glyph test could not find bits\n");
+			failures++;
+		}
+	}
+
+	/* A block at the last cell must clamp without touching the canvas
+	 * bounds, and still render the cell. */
+	cells[(ROWS - 1) * COLS + (COLS - 1)] = (0x0F << 8) | 'X';
+	text_canvas_render(&c, cells, COLS, ROWS, COLS - 1, ROWS - 1, 4, 4,
+			   0xFFFFFFFFu, 0, 0);
+	{
+		unsigned int row, bit;
+		int set_seen = 0;
+
+		for (row = 0; row < FONT_8X16_ROWS && !set_seen; row++)
+			for (bit = 0; bit < 8; bit++)
+				if (glyph_bit('X', row, bit)) {
+					expect((COLS - 1) * TEXT_CELL_W + bit,
+					       (ROWS - 1) * TEXT_CELL_H + row,
+					       pal565[15], "edge block");
+					set_seen = 1;
+					break;
+				}
+		if (!set_seen) {
+			printf("FAIL edge block glyph bits not found\n");
+			failures++;
+		}
+	}
+
+	if (failures) {
+		printf("%d text canvas test(s) failed\n", failures);
+		return 1;
+	}
+	printf("all text canvas tests passed\n");
+	return 0;
+}

+ 1 - 1
tools/qmp_cursor_test.py

@@ -165,7 +165,7 @@ def main():
                 break
             time.sleep(0.1)
         s = qmp_connect(sock)
-        time.sleep(4)                       # boot
+        time.sleep(8.0)                     # boot
 
         if mode == "hotplug":
             qmp_cmd(s, "device_add", {"driver": "usb-mouse", "id": "mouse"})

+ 2 - 2
tools/qmp_keyboard_test.py

@@ -107,11 +107,11 @@ def main():
         s.connect(sock)
         qmp_recv(s)                  # greeting
         qmp_cmd(s, "qmp_capabilities")
-        time.sleep(5)                # boot
+        time.sleep(8.0)              # boot
 
         if hotplug:
             qmp_cmd(s, "device_add", {"driver": "usb-kbd", "id": "kbd"})
-            time.sleep(2)
+            time.sleep(4.0)
 
         if os.path.exists(ppm):
             os.unlink(ppm)

+ 19 - 43
ucmd_wm.c

@@ -237,35 +237,18 @@ static void draw_window_clip(unsigned int idx, int cx, int cy,
 	usurf_text(&screen, (int)bx + 4, y + 3, "x", COL_WHITE,
 		   cx, cy, cw, ch);
 
-	/* Content. */
+	/* Content: every window presents an RGB565 canvas (the terminal
+	 * renders its cells into one; gfx commands own theirs). */
 	fill_clip((int)content_x, (int)cony, content_w, content_h,
 		  0x000000u, cx, cy, cw, ch);
-	if (u->mapped) {
-		if (w->kind == WM_KIND_GFX) {
-			struct uwm_canvas c;
-
-			c.px = (const unsigned short *)u->map.va;
-			c.w = u->map.w;
-			c.h = u->map.h;
-			usurf_canvas(&screen, &c, (int)content_x, (int)cony,
-				     cx, cy, cw, ch);
-		} else if (u->map.kind == WM_KIND_TEXT) {
-			usurf_cells(&screen, (const unsigned short *)u->map.va,
-				    w->cols, w->rows, (int)content_x, (int)cony,
-				    cx, cy, cw, ch);
-		}
-	}
-
-	/* Text cursor overlay (only for the focused terminal). */
-	if (w->kind == WM_KIND_TEXT && w->focused && w->cursor_visible &&
-	    w->cols && w->rows && w->cursor < w->cols * w->rows) {
-		unsigned int col = w->cursor % w->cols;
-		unsigned int row = w->cursor / w->cols;
-		unsigned int px = content_x + col * 8;
-		unsigned int py = cony + row * 10;
-
-		fill_clip((int)px, (int)py, 2, 10, COL_WHITE,
-			  cx, cy, cw, ch);
+	if (u->mapped && u->map.kind == WM_KIND_GFX) {
+		struct uwm_canvas c;
+
+		c.px = (const unsigned short *)u->map.va;
+		c.w = u->map.w;
+		c.h = u->map.h;
+		usurf_canvas(&screen, &c, (int)content_x, (int)cony,
+			     cx, cy, cw, ch);
 	}
 
 	/* Resize grip (bottom-right corner). */
@@ -413,11 +396,11 @@ static void do_update(struct wm_status *st)
 			wins[i].mapped = 0;
 			continue;
 		}
-		if (!wins[i].info.used || info.content_gen != wins[i].info.content_gen ||
-		    info.kind != wins[i].info.kind)
+		if (!wins[i].info.used ||
+		    info.content_gen != wins[i].info.content_gen)
 			full_redraw = 1;
 		wins[i].info = info;
-		if (!wins[i].mapped || wins[i].map.kind != info.kind ||
+		if (!wins[i].mapped ||
 		    wins[i].map.generation != info.content_gen) {
 			struct wm_map_info map;
 
@@ -502,19 +485,12 @@ static void do_update(struct wm_status *st)
 
 		if (!w->used || !w->dirty)
 			continue;
-		if (w->kind == WM_KIND_GFX) {
-			rx = w->x + 1;
-			ry = w->y + 1 + WM_TITLE_H;
-			rw = w->content_w;
-			rh = w->content_h;
-		} else {
-			if (!w->dmg_cols || !w->dmg_rows)
-				continue;
-			rx = w->x + 1 + (int)w->dmg_col0 * 8;
-			ry = w->y + 1 + WM_TITLE_H + (int)w->dmg_row0 * 10;
-			rw = w->dmg_cols * 8;
-			rh = w->dmg_rows * 10;
-		}
+		if (!w->dmg_w || !w->dmg_h)
+			continue;
+		rx = w->x + 1 + (int)w->dmg_x;
+		ry = w->y + 1 + WM_TITLE_H + (int)w->dmg_y;
+		rw = w->dmg_w;
+		rh = w->dmg_h;
 		add_rect(&present, rx, ry, rw, rh);
 		u->ack_seq = u->mapped ? w->dirty_seq : 0;
 		need = 1;

+ 151 - 108
wm.c

@@ -19,6 +19,7 @@
 #include "rtc.h"
 #include "serial.h"
 #include "sys_gfx.h"
+#include "text_canvas.h"
 #include "thermal.h"
 #include "timer.h"
 #include "tty.h"
@@ -149,7 +150,8 @@ int wm_content_size(struct term *t, unsigned int *w, unsigned int *h)
 	for (i = 0; i < MAX_WINDOWS; i++)
 		if (windows[i].used && windows[i].term == t) {
 			*w = windows[i].pw - 2;
-			*h = SCREEN_ROWS * 10;
+			*h = windows[i].canvas.px ? windows[i].canvas.h
+						  : SCREEN_ROWS * TEXT_CELL_H;
 			return 0;
 		}
 	return -1;
@@ -357,10 +359,7 @@ int wm_server_win_create(const struct win_create *in, struct win_info *out)
 		w->title[j] = in->title[j];
 	w->title[j] = '\0';
 
-	for (j = 0; j < MAX_WINDOWS; j++)
-		if (windows[j].used)
-			windows[j].focused = (j == i);
-	wm_request_redraw();
+	wm_focus_index(i);
 
 	out->fb = (void *)USER_GFX_BASE;
 	out->width = cw;
@@ -677,6 +676,48 @@ int wm_server_attach(struct wm_screen_info *out)
 	return 0;
 }
 
+/* Render the fallback kernel terminal's cell grid into its RGB565 canvas.
+ * The ring-3 terminal owns its own canvas and never comes here.  The canvas
+ * stays valid between damage blocks; the WM composites it like any other
+ * window content. */
+static void wm_text_sync(struct window *w)
+{
+	struct text_screen *s;
+	struct text_canvas tc;
+	unsigned int c0, r0, nc, nr;
+
+	if (!w || !w->term || !w->canvas.px)
+		return;
+	s = &w->term->screen;
+	tc.px = w->canvas.px;
+	tc.w = w->canvas.w;
+	tc.h = w->canvas.h;
+
+	if (!w->text_ready) {
+		text_canvas_render(&tc, s->cells, s->cols, s->rows,
+				   0, 0, s->cols, s->rows,
+				   s->cursor, s->cursor_visible, w->focused);
+		w->text_dmg_x = 0;
+		w->text_dmg_y = 0;
+		w->text_dmg_w = w->canvas.w;
+		w->text_dmg_h = w->canvas.h;
+		w->text_ready = 1;
+	} else if (s->damage_seq != w->canvas.seq) {
+		if (!screen_damage_block(s, &c0, &r0, &nc, &nr))
+			return;
+		text_canvas_render(&tc, s->cells, s->cols, s->rows,
+				   c0, r0, nc, nr,
+				   s->cursor, s->cursor_visible, w->focused);
+		w->text_dmg_x = c0 * TEXT_CELL_W;
+		w->text_dmg_y = r0 * TEXT_CELL_H;
+		w->text_dmg_w = nc * TEXT_CELL_W;
+		w->text_dmg_h = nr * TEXT_CELL_H;
+	} else {
+		return;
+	}
+	w->canvas.seq = s->damage_seq;
+}
+
 int wm_server_info(unsigned int index, struct wm_window_info *out)
 {
 	struct window *w;
@@ -691,21 +732,15 @@ int wm_server_info(unsigned int index, struct wm_window_info *out)
 		return 0;
 
 	out->used = 1;
-	out->kind = WM_KIND_NONE;
 	out->focused = w->focused ? 1 : 0;
 	out->pid = w->pid;
 	out->x = (int)w->x;
 	out->y = (int)w->y;
 	out->w = w->pw;
 	out->h = w->ph;
-	out->cols = 0;
-	out->rows = 0;
-	out->cursor = 0;
-	out->cursor_visible = 0;
 	out->dirty = 0;
 	out->dirty_seq = 0;
-	out->dmg_col0 = out->dmg_row0 = 0;
-	out->dmg_cols = out->dmg_rows = 0;
+	out->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];
@@ -716,33 +751,30 @@ int wm_server_info(unsigned int index, struct wm_window_info *out)
 	} else if (w->term) {
 		s = &w->term->screen;
 		g = sys_gfx_canvas_for(s);
+		if (!g) {
+			wm_text_sync(w);
+			if (w->canvas.px)
+				g = &w->canvas;
+		}
 	} else {
 		return 1;
 	}
 
 	if (g) {
-		out->kind = WM_KIND_GFX;
 		out->content_w = g->w;
 		out->content_h = g->h;
 		out->dirty = (g->seq != g->drawn);
 		out->dirty_seq = g->seq;
-	} else if (s) {
-		unsigned int c0, r0, nc, nr;
-
-		out->kind = WM_KIND_TEXT;
-		out->content_w = w->pw - 2;
-		out->content_h = SCREEN_ROWS * 10;
-		out->cols = s->cols;
-		out->rows = s->rows;
-		out->cursor = s->cursor;
-		out->cursor_visible = s->cursor_visible;
-		out->dirty_seq = s->damage_seq;
-		if (screen_damage_block(s, &c0, &r0, &nc, &nr)) {
-			out->dirty = 1;
-			out->dmg_col0 = c0;
-			out->dmg_row0 = r0;
-			out->dmg_cols = nc;
-			out->dmg_rows = nr;
+		if (w->term && g == &w->canvas) {
+			out->dmg_x = w->text_dmg_x;
+			out->dmg_y = w->text_dmg_y;
+			out->dmg_w = w->text_dmg_w;
+			out->dmg_h = w->text_dmg_h;
+		} else {
+			out->dmg_x = 0;
+			out->dmg_y = 0;
+			out->dmg_w = g->w;
+			out->dmg_h = g->h;
 		}
 	}
 	return 1;
@@ -752,10 +784,8 @@ int wm_server_map(unsigned int index, struct wm_map_info *out)
 {
 	struct window *w;
 	struct gfx_canvas *g = 0;
-	struct text_screen *s = 0;
 	const void *src;
-	unsigned int kind, pages, i, va;
-	unsigned int wpx, hpx, stride;
+	unsigned int pages, i, va, stride;
 
 	if (!wm_server_is_wm() || !out || index >= MAX_WINDOWS || !wm_cr3)
 		return -1;
@@ -765,45 +795,35 @@ int wm_server_map(unsigned int index, struct wm_map_info *out)
 	if (w->canvas_window) {
 		g = &w->canvas;
 	} else if (w->term) {
-		s = &w->term->screen;
-		g = sys_gfx_canvas_for(s);
+		g = sys_gfx_canvas_for(&w->term->screen);
+		if (!g) {
+			wm_text_sync(w);
+			if (w->canvas.px)
+				g = &w->canvas;
+		}
+		if (!g)
+			return -1;
 	} else {
 		return -1;
 	}
-	kind = g ? WM_KIND_GFX : WM_KIND_TEXT;
-	src = g ? (const void *)g->px : (const void *)w->term;
+	src = (const void *)g->px;
+	stride = g->w * 2;
 
-	if (w->mapped_kind == kind && w->mapped_gen == w->content_gen &&
+	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 = kind;
+		out->kind = WM_KIND_GFX;
 		out->generation = w->content_gen;
-		if (g) {
-			out->w = g->w;
-			out->h = g->h;
-			out->stride = g->w * 2;
-		} else {
-			out->w = s->cols;
-			out->h = s->rows;
-			out->stride = s->cols * 2;
-		}
+		out->w = g->w;
+		out->h = g->h;
+		out->stride = stride;
 		return 0;
 	}
 
 	wm_server_unmap_index(index);
-	if (g) {
-		wpx = g->w;
-		hpx = g->h;
-		stride = g->w * 2;
-		pages = (wpx * hpx * 2 + PAGE_SIZE - 1) / PAGE_SIZE;
-		if (pages * PAGE_SIZE > WM_CONTENT_SLOT)
-			return -1;
-	} else {
-		wpx = s->cols;
-		hpx = s->rows;
-		stride = s->cols * 2;
-		pages = 1;
-	}
+	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,
@@ -815,15 +835,15 @@ int wm_server_map(unsigned int index, struct wm_map_info *out)
 		}
 		invlpg_va(va + i * PAGE_SIZE);
 	}
-	w->mapped_kind = kind;
+	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 = kind;
-	out->w = wpx;
-	out->h = hpx;
+	out->kind = WM_KIND_GFX;
+	out->w = g->w;
+	out->h = g->h;
 	out->stride = stride;
 	out->generation = w->content_gen;
 	return 0;
@@ -833,6 +853,7 @@ int wm_server_ack(unsigned int index, unsigned int seq)
 {
 	struct window *w;
 	struct gfx_canvas *g;
+	struct text_screen *s;
 
 	if (!wm_server_is_wm() || index >= MAX_WINDOWS)
 		return -1;
@@ -846,13 +867,17 @@ int wm_server_ack(unsigned int index, unsigned int seq)
 	}
 	if (!w->term)
 		return -1;
-	g = sys_gfx_canvas_for(&w->term->screen);
+	s = &w->term->screen;
+	g = sys_gfx_canvas_for(s);
 	if (g) {
 		if (seq == g->seq)
 			g->drawn = seq;
-	} else if (seq == w->term->screen.damage_seq) {
-		screen_clear_damage(&w->term->screen);
+		return 0;
 	}
+	if (w->canvas.px && seq == w->canvas.seq)
+		w->canvas.drawn = seq;
+	if (seq == s->damage_seq)
+		screen_clear_damage(s);
 	return 0;
 }
 
@@ -1195,7 +1220,7 @@ static void wm_make_title(struct window *w, unsigned int n)
 
 int wm_spawn_terminal(void)
 {
-	unsigned int i, j, fbw = vga_fb_width();
+	unsigned int i, fbw = vga_fb_width();
 	struct window *w;
 	struct term *t;
 	int pid;
@@ -1255,9 +1280,19 @@ int wm_spawn_terminal(void)
 	w->used = 1;
 	w->term = t;
 	w->canvas_window = 0;
-	w->canvas.px = 0;
-	w->canvas.w = w->canvas.h = 0;
-	w->canvas.seq = w->canvas.drawn = 0;
+	if (gfx_alloc(&w->canvas, SCREEN_COLS * TEXT_CELL_W,
+		      SCREEN_ROWS * TEXT_CELL_H) != 0) {
+		serial_puts(SERIAL_COM1, "wm: no text canvas\n");
+		w->used = 0;
+		w->term = 0;
+		return -1;
+	}
+	w->canvas.drawn = 0xFFFFFFFFu;
+	w->text_ready = 0;
+	w->text_dmg_x = 0;
+	w->text_dmg_y = 0;
+	w->text_dmg_w = w->canvas.w;
+	w->text_dmg_h = w->canvas.h;
 	w->back = 0;
 	w->back_pages = 0;
 	w->owner_cr3 = 0;
@@ -1265,8 +1300,8 @@ int wm_spawn_terminal(void)
 	wait_queue_init(&w->key_wq);
 	w->pid = 0;
 	w->focused = 0;
-	w->pw = SCREEN_COLS * 8 + 2;
-	w->ph = WM_TITLE_H + SCREEN_ROWS * 10 + 2;
+	w->pw = SCREEN_COLS * TEXT_CELL_W + 2;
+	w->ph = WM_TITLE_H + SCREEN_ROWS * TEXT_CELL_H + 2;
 	w->content_gen = 1;
 	w->mapped_kind = WM_KIND_NONE;
 	w->mapped_gen = 0;
@@ -1287,17 +1322,15 @@ int wm_spawn_terminal(void)
 
 	pid = process_create_ptr("term", term_entry, t);
 	if (pid < 0) {
+		gfx_free(&w->canvas);
 		w->used = 0;
+		w->term = 0;
 		return -1;
 	}
 	process_set_weight((unsigned int)pid, 12);
 	w->pid = (unsigned int)pid;
 
-	for (j = 0; j < MAX_WINDOWS; j++)
-		if (windows[j].used)
-			windows[j].focused = (j == i);
-
-	wm_request_redraw();
+	wm_focus_index(i);
 	return (int)i;
 }
 
@@ -1306,9 +1339,17 @@ static void wm_focus_index(unsigned int idx)
 	unsigned int i;
 	if (windows[idx].used && windows[idx].focused)
 		return;
-	for (i = 0; i < MAX_WINDOWS; i++)
-		if (windows[i].used)
-			windows[i].focused = (i == idx);
+	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;
+		/* The terminal cursor lives in the text canvas, so a focus
+		 * change must repaint that cell in both windows. */
+		if (windows[i].term && windows[i].canvas.px)
+			screen_damage_cursor(&windows[i].term->screen);
+	}
 	wm_request_redraw();
 }
 
@@ -1360,6 +1401,9 @@ static void wm_close_window(unsigned int idx)
 	w->mapped_gen = 0;
 	w->mapped_pages = 0;
 	w->mapped_src = 0;
+	if (w->term && w->canvas.px)
+		gfx_free(&w->canvas);
+	w->text_ready = 0;
 	wm_focus_top();
 	wm_request_redraw();
 }
@@ -1659,6 +1703,11 @@ static void wm_draw_window_clip(struct window *w, unsigned int cx, unsigned int
 		return;   /* generic canvas windows are drawn by the ring-3 WM */
 	s = &w->term->screen;
 	g = sys_gfx_canvas_for(s);
+	if (!g) {
+		wm_text_sync(w);
+		if (w->canvas.px)
+			g = &w->canvas;
+	}
 	unsigned int bx = w->x + w->pw - 1 - WM_CLOSE_W;
 	unsigned int cony = w->y + 1 + WM_TITLE_H;
 
@@ -1683,23 +1732,12 @@ static void wm_draw_window_clip(struct window *w, unsigned int cx, unsigned int
 	fbtext_draw_string_xy_clip(bx + 4, w->y + 3, "x", 255, 255, 255,
 		cx, cy, cw, ch);
 
-	/* Content: text cells or the gfx canvas. */
-	wm_fill_rect_clip(w->x + 1, cony, w->pw - 2, SCREEN_ROWS * 10,
+	/* Content: the window's canvas (text canvas or a gfx command). */
+	wm_fill_rect_clip(w->x + 1, cony, w->pw - 2,
+		g ? g->h : SCREEN_ROWS * TEXT_CELL_H,
 		cx, cy, cw, ch, 0, 0, 0);
 	if (g)
 		blit->canvas(g, w->x + 1, cony, cx, cy, cw, ch);
-	else
-		fbtext_draw_cells_clip(s->cells, s->cols, s->rows,
-			w->x + 1, cony, cx, cy, cw, ch);
-
-	/* Blinking block cursor (text windows only). */
-	if (!g && w->focused && s->cursor_visible) {
-		unsigned int col = s->cursor % s->cols;
-		unsigned int row = s->cursor / s->cols;
-
-		wm_fill_rect_clip(w->x + 1 + col * 8, cony + row * 10, 2, 10,
-			cx, cy, cw, ch, 255, 255, 255);
-	}
 }
 
 static void wm_draw_window(struct window *w)
@@ -1792,29 +1830,34 @@ static void wm_redraw_if_needed(void)
 	for (i = 0; i < MAX_WINDOWS; i++) {
 		struct text_screen *s;
 		struct gfx_canvas *g;
-		unsigned int seq, col0, row0, ncols, nrows;
+		unsigned int seq;
 
 		if (!windows[i].used || !windows[i].term)
 			continue;
 		s = &windows[i].term->screen;
 		g = sys_gfx_canvas_for(s);
-		if (!screen_damage_block(s, &col0, &row0, &ncols, &nrows) &&
-		    !(g && g->seq != g->drawn))
+		if (!g) {
+			wm_text_sync(&windows[i]);
+			if (windows[i].canvas.px)
+				g = &windows[i].canvas;
+		}
+		if (!g || g->seq == g->drawn)
 			continue;
-		seq = g ? g->seq : 0;   /* capture before compositing */
+		seq = g->seq;   /* capture before compositing */
 		mouse_cursor_erase();
-		if (g)
+		if (g == &windows[i].canvas)
+			wm_redraw_rect(windows[i].x + 1 +
+				       (int)windows[i].text_dmg_x,
+				windows[i].y + 1 + WM_TITLE_H +
+				       (int)windows[i].text_dmg_y,
+				windows[i].text_dmg_w, windows[i].text_dmg_h);
+		else
 			wm_redraw_rect(windows[i].x + 1,
 				windows[i].y + 1 + WM_TITLE_H,
-				windows[i].pw - 2, SCREEN_ROWS * 10);
-		else
-			wm_redraw_rect(windows[i].x + 1 + col0 * 8,
-				windows[i].y + 1 + WM_TITLE_H + row0 * 10,
-				ncols * 8, nrows * 10);
+				g->w, g->h);
 		mouse_redraw_cursor();
 		screen_clear_damage(s);
-		if (g)
-			g->drawn = seq;
+		g->drawn = seq;
 	}
 }
 

+ 5 - 0
wm.h

@@ -37,6 +37,11 @@ struct window {
 	unsigned int mapped_gen;
 	unsigned int mapped_pages;
 	const void *mapped_src;
+	/* Fallback kernel terminal canvas: the window server renders the cell
+	 * grid into this RGB565 canvas and the WM composites it like any other
+	 * window content. */
+	int text_ready;
+	unsigned int text_dmg_x, text_dmg_y, text_dmg_w, text_dmg_h;
 };
 
 void wm_run(void);

+ 2 - 7
wm_abi.h

@@ -54,8 +54,7 @@
 
 /* Window content kind. */
 #define WM_KIND_NONE 0
-#define WM_KIND_TEXT 1
-#define WM_KIND_GFX  2
+#define WM_KIND_GFX  1
 
 struct wm_screen_info {
 	unsigned int width;
@@ -82,18 +81,14 @@ struct wm_status {
 
 struct wm_window_info {
 	unsigned int used;
-	unsigned int kind;         /* WM_KIND_* */
 	unsigned int focused;
 	unsigned int pid;
 	int x, y;
 	unsigned int w, h;         /* outer window rectangle */
 	unsigned int content_w, content_h;
-	unsigned int cols, rows;   /* text cells (0 for gfx) */
-	unsigned int cursor;
-	unsigned int cursor_visible;
 	unsigned int dirty;
 	unsigned int dirty_seq;
-	unsigned int dmg_col0, dmg_row0, dmg_cols, dmg_rows;
+	unsigned int dmg_x, dmg_y, dmg_w, dmg_h; /* content-relative pixels */
 	unsigned int content_gen;
 	char title[16];
 };