Johannes Zellner 1 месяц назад
Родитель
Сommit
2819a82437
21 измененных файлов с 1097 добавлено и 324 удалено
  1. 11 3
      AGENTS.md
  2. 10 4
      Makefile
  3. 60 8
      fbtext.c
  4. 5 0
      fbtext.h
  5. 29 80
      io.c
  6. 2 2
      kernel.c
  7. 5 0
      kexec.c
  8. 4 0
      kexec.h
  9. 32 0
      mouse.c
  10. 12 0
      mouse.h
  11. 48 6
      process.c
  12. 5 0
      process.h
  13. 119 0
      screen.c
  14. 31 0
      screen.h
  15. 1 1
      switch.S
  16. 126 217
      terminal.c
  17. 46 3
      terminal.h
  18. 53 0
      vga.c
  19. 2 0
      vga.h
  20. 460 0
      wm.c
  21. 36 0
      wm.h

+ 11 - 3
AGENTS.md

@@ -55,12 +55,20 @@ make clean
 
 - **Multitasking**: `process.c/h`, `switch.S` — ring-0 preemptive round-robin,
   8 slots, 20 ms timeslice, `ps`/`kill`. Context switch happens in the PIT stub.
+  Each process carries a `struct text_screen *screen` output target (child
+  processes inherit it, so command output lands in the owning terminal).
+- **Window manager**: `wm.c/h` — slot 0 runs `wm_run()`; fixed-size draggable
+  terminal windows (64×20) with a system bar (banner + purple "+ New" button +
+  clock + rainbow). Click-to-focus, drag-by-title-bar, close (×). The "+ New"
+  button and first boot spawn terminals; there is no `term` command.
 - **Graphics**: `vga.c/h`, `vga_copy.S`, `gpu.c`, `ati.c`, `nvidia.c`, `pci.h` —
   VESA LFB + shadow buffer, 30 Hz dirty-row flush. ATI Mach64 CRTC fixup; NVIDIA no-op.
-- **Text console**: `fbtext.c`, `font_8x8.c/h`, `io.c/h` — framebuffer text,
-  8×8 font, rainbow separator, live datetime header, blinking cursor.
+- **Text console**: `fbtext.c`, `font_8x8.c/h`, `io.c/h`, `screen.c/h` — 8×8 font,
+  framebuffer glyph blitting (`fbtext_draw_cells`) and per-terminal cell buffers
+  (`struct text_screen`).
 - **Shell**: `terminal.c` — history, line editing, command dispatch (hand-rolled
-  `is_cmd_*` parsers).
+  `is_cmd_*` parsers). Runs in a window (`term_run(struct term *)`); reads keys
+  from its per-terminal queue (filled by the WM).
 - **Filesystem**: `fat.c` (FAT12/16/32 read + FAT12/16 write, device-parameterized),
   `vfs.c` (mounts every FAT device under `/`), `fatdetect.c`/`fatformat.c`,
   `blkdev.c` (floppy + IDE registry), `ata.c` (ATA PIO), `disk.c` (FDC + ISA DMA ch2, retries).

+ 10 - 4
Makefile

@@ -23,7 +23,10 @@ boot.o: boot.S
 kernel.o: kernel.c idle.h io.h mouse.h process.h terminal.h timer.h serial.h version.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-io.o: io.c io.h fbtext.h
+screen.o: screen.c screen.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+io.o: io.c io.h fbtext.h process.h screen.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 font_8x8.o: font_8x8.c font_8x8.h
@@ -32,7 +35,10 @@ font_8x8.o: font_8x8.c font_8x8.h
 fbtext.o: fbtext.c fbtext.h font_8x8.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-terminal.o: terminal.c terminal.h io.h fbtext.h idle.h kexec.h mouse.h process.h rtc.h timer.h disk.h version.h
+terminal.o: terminal.c terminal.h screen.h io.h dbg.h idle.h kexec.h process.h timer.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+wm.o: wm.c wm.h terminal.h screen.h fbtext.h idle.h io.h mouse.h process.h rtc.h timer.h version.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 hello.o: hello.c io.h
@@ -179,7 +185,7 @@ switch.o: switch.S
 process.o: process.c process.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o poweroff.o disk.o fat.o ls.o cat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.o blkdev.o ata.o fatdetect.o storage.o fatformat.o format.o vfs.o idle.o kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.o
+kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o disk.o fat.o ls.o cat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.o blkdev.o ata.o fatdetect.o storage.o fatformat.o format.o vfs.o idle.o kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -328,4 +334,4 @@ dev: kernel.bin kexec_send
 	./kexec_send $(SERIAL_DEV) $(SERIAL_BAUD) $$entry kernel.bin
 
 clean:
-	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o poweroff.o fat.o ls.o cat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.o blkdev.o ata.o fatdetect.o storage.o fatformat.o format.o vfs.o idle.o kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.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 bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send
+	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o ls.o cat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.o blkdev.o ata.o fatdetect.o storage.o fatformat.o format.o vfs.o idle.o kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.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 bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send

+ 60 - 8
fbtext.c

@@ -22,7 +22,9 @@ static inline unsigned int row_to_y0(unsigned int row)
 }
 
 /* Draw one 8x8 glyph at pixel (x0, y0). height 8 = glyph only; 10 = glyph + 2 rows bg (terminal row).
-   skip_bg: 1 = only draw foreground (transparent background for overlays). */
+   skip_bg: 1 = only draw foreground (transparent background for overlays).
+   Writes directly into the shadow buffer (no per-pixel function call); the
+   caller is responsible for marking the affected rows dirty. */
 static void draw_glyph_at(unsigned int x0, unsigned int y0, unsigned char c,
 		unsigned int r_fg, unsigned int g_fg, unsigned int b_fg,
 		unsigned int r_bg, unsigned int g_bg, unsigned int b_bg,
@@ -30,16 +32,46 @@ static void draw_glyph_at(unsigned int x0, unsigned int y0, unsigned char c,
 {
 	unsigned int x, y;
 	const unsigned char *glyph = &font_8x8[(unsigned int)c * FONT_8X8_ROWS];
+	unsigned char *fb = (unsigned char *)vga_fb_ptr();
+	unsigned int pitch = vga_fb_pitch();
+	unsigned int bpp = vga_fb_bpp();
+	unsigned int fg32 = (r_fg << 16) | (g_fg << 8) | b_fg;
+	unsigned int bg32 = (r_bg << 16) | (g_bg << 8) | b_bg;
+	unsigned short fg16 = (unsigned short)(((r_fg >> 3) << 11) | ((g_fg >> 2) << 5) | (b_fg >> 3));
+	unsigned short bg16 = (unsigned short)(((r_bg >> 3) << 11) | ((g_bg >> 2) << 5) | (b_bg >> 3));
 
 	for (y = 0; y < height; y++) {
 		unsigned char row_bits = (y < FONT_8X8_ROWS) ? glyph[y] : 0;
-		for (x = 0; x < 8; x++) {
-			unsigned int px = x0 + x;
-			unsigned int py = y0 + y;
-			if (row_bits & (0x80u >> x))
-				vga_put_pixel_rgb(px, py, r_fg, g_fg, b_fg);
-			else if (!skip_bg)
-				vga_put_pixel_rgb(px, py, r_bg, g_bg, b_bg);
+		unsigned int py = y0 + y;
+
+		if (bpp == 16) {
+			unsigned short *p = (unsigned short *)(fb + (unsigned long)py * pitch + x0 * 2);
+			for (x = 0; x < 8; x++) {
+				if (row_bits & (0x80u >> x))
+					*p = fg16;
+				else if (!skip_bg)
+					*p = bg16;
+				p++;
+			}
+		} else if (bpp == 24) {
+			unsigned char *p = fb + (unsigned long)py * pitch + x0 * 3;
+			for (x = 0; x < 8; x++) {
+				if (row_bits & (0x80u >> x)) {
+					p[0] = (unsigned char)b_fg; p[1] = (unsigned char)g_fg; p[2] = (unsigned char)r_fg;
+				} else if (!skip_bg) {
+					p[0] = (unsigned char)b_bg; p[1] = (unsigned char)g_bg; p[2] = (unsigned char)r_bg;
+				}
+				p += 3;
+			}
+		} else {
+			unsigned int *p = (unsigned int *)(fb + (unsigned long)py * pitch + x0 * 4);
+			for (x = 0; x < 8; x++) {
+				if (row_bits & (0x80u >> x))
+					*p = fg32;
+				else if (!skip_bg)
+					*p = bg32;
+				p++;
+			}
 		}
 	}
 }
@@ -122,6 +154,25 @@ void fbtext_put_char_at(unsigned int col, unsigned int row, unsigned char c, uns
 	draw_glyph(col, row, c, attr);
 }
 
+void fbtext_draw_cells(const unsigned short *cells, unsigned int cols, unsigned int rows,
+		unsigned int x0, unsigned int y0)
+{
+	unsigned int row, col;
+	for (row = 0; row < rows; row++) {
+		for (col = 0; col < cols; col++) {
+			unsigned short cell = cells[row * cols + col];
+			unsigned char c = (unsigned char)(cell & 0xFF);
+			unsigned char attr = (unsigned char)(cell >> 8);
+			unsigned int rf, gf, bf, rb, gb, bb;
+			attr_to_rgb(attr, 1, &rf, &gf, &bf);
+			attr_to_rgb(attr, 0, &rb, &gb, &bb);
+			draw_glyph_at(x0 + col * 8, y0 + row * ROW_HEIGHT, c,
+				rf, gf, bf, rb, gb, bb, ROW_HEIGHT, 0);
+		}
+	}
+	vga_fb_mark_dirty_rect(y0, rows * ROW_HEIGHT);
+}
+
 void fbtext_scroll_up_one_line(void)
 {
 	unsigned int y, i;
@@ -240,6 +291,7 @@ void fbtext_draw_string_xy(unsigned int x0, unsigned int y0, const char *str,
 		draw_glyph_at(x0 + i * 8, y0, (unsigned char)str[i], r_fg, g_fg, b_fg, 0, 0, 0, FONT_8X8_ROWS, transparent_bg);
 		i++;
 	}
+	vga_fb_mark_dirty_rect(y0, FONT_8X8_ROWS);
 }
 
 #define RAINBOW_LINE_HEIGHT  2   /* 2-pixel tall line for visibility */

+ 5 - 0
fbtext.h

@@ -10,6 +10,11 @@ unsigned int fbtext_get_rows(void);
 
 void fbtext_clear(void);
 void fbtext_put_char_at(unsigned int col, unsigned int row, unsigned char c, unsigned char attr);
+
+/* Blit a cell buffer (cells of (attr<<8)|ch) into the shadow buffer at pixel
+   origin (x0, y0). 8 px per column, 10 px per row. */
+void fbtext_draw_cells(const unsigned short *cells, unsigned int cols, unsigned int rows,
+		unsigned int x0, unsigned int y0);
 void fbtext_scroll_up_one_line(void);
 void fbtext_set_cursor(unsigned int col, unsigned int row);
 void fbtext_show_cursor(void);

+ 29 - 80
io.c

@@ -1,7 +1,11 @@
-/* I/O library: framebuffer text output and PS/2 keyboard. */
+/* I/O library: PS/2 keyboard and text output.
+   Text output is routed to the current process's screen (see process.c);
+   slot 0 / boot fall back to a static screen the WM does not render. */
 
 #include "io.h"
 #include "fbtext.h"
+#include "process.h"
+#include "screen.h"
 
 #define VGA_ATTR 0x0A
 
@@ -40,16 +44,22 @@ static const unsigned char scancode_to_ascii[256] __attribute__((section(".rodat
 };
 
 static int shift_pressed;
-static unsigned int cursor;
-static unsigned short cursor_cell;
 static unsigned char last_scancode;
-static int scroll_occurred;
 static int kbd_extended;  /* 1 after 0xE0, next byte is extended scancode */
 
 #define KBD_BUF_SIZE 16
 static volatile unsigned char kbd_buf[KBD_BUF_SIZE];
 static volatile unsigned int kbd_head, kbd_tail;
 
+/* Fallback screen for slot 0 / boot / exception output (not rendered by WM). */
+static struct text_screen io_fallback;
+
+static struct text_screen *io_current_screen(void)
+{
+	struct text_screen *s = procs[current_pid].screen;
+	return s ? s : &io_fallback;
+}
+
 void kbd_irq(void)
 {
 	unsigned char sc = inb(KB_DATA);
@@ -86,14 +96,6 @@ static char apply_shift(char c)
 	}
 }
 
-static void hide_cursor_at(unsigned int pos)
-{
-	unsigned int col = pos % io_cols;
-	unsigned int row = pos / io_cols;
-	fbtext_set_cursor(col, row);
-	fbtext_hide_cursor((unsigned char)(cursor_cell & 0xFF), (unsigned char)(cursor_cell >> 8));
-}
-
 unsigned int io_get_cols(void)
 {
 	return io_cols;
@@ -109,115 +111,62 @@ void io_init(void)
 	fbtext_init();
 	io_cols = fbtext_get_cols();
 	io_rows = fbtext_get_rows();
-	io_clear_screen();
-	cursor = IO_CONTENT_OFFSET;
-	cursor_cell = (unsigned short)(VGA_ATTR << 8);
+	screen_init(&io_fallback);
 	io_drain_keyboard();
 }
 
 void io_clear_screen(void)
 {
-	fbtext_clear();
+	screen_clear(io_current_screen());
 }
 
 int io_did_scroll(void)
 {
-	if (!scroll_occurred)
-		return 0;
-	scroll_occurred = 0;
-	return 1;
+	return screen_did_scroll(io_current_screen());
 }
 
 void io_putchar(char c)
 {
-	unsigned int ncols = io_cols;
-	unsigned int content_off = ncols;
-	unsigned int content_size = (io_rows - 1) * ncols;
-
-	if (c == '\n') {
-		hide_cursor_at(cursor);
-		cursor = (cursor / ncols + 1) * ncols;
-		if (cursor >= content_off + content_size) {
-			fbtext_scroll_up_one_line();
-			cursor = content_off + (io_rows - 2) * ncols;
-			scroll_occurred = 1;
-		}
-		cursor_cell = (unsigned short)(VGA_ATTR << 8);
-		return;
-	}
-	if (c == '\b') {
-		if (cursor > content_off) {
-			hide_cursor_at(cursor);
-			cursor--;
-			{
-				unsigned int col = cursor % ncols;
-				unsigned int row = cursor / ncols;
-				fbtext_put_char_at(col, row, ' ', VGA_ATTR);
-			}
-			cursor_cell = (unsigned short)((VGA_ATTR << 8) | ' ');
-		}
-		return;
-	}
-	if (cursor < content_off + content_size) {
-		{
-			unsigned int col = cursor % ncols;
-			unsigned int row = cursor / ncols;
-			fbtext_put_char_at(col, row, (unsigned char)c, VGA_ATTR);
-		}
-		cursor++;
-		cursor_cell = (unsigned short)(VGA_ATTR << 8);
-	}
+	screen_putchar(io_current_screen(), c);
 }
 
 void io_putstr(const char *s)
 {
-	while (*s) io_putchar(*s++);
+	screen_putstr(io_current_screen(), s);
 }
 
 void io_puthex(unsigned int n)
 {
-	static const char hex[] = "0123456789ABCDEF";
-	char buf[9];
-	unsigned int i = 8;
-	buf[8] = '\0';
-	do {
-		buf[--i] = hex[n & 0xF];
-		n >>= 4;
-	} while (i > 0);
-	io_putstr(buf);
+	screen_puthex(io_current_screen(), n);
 }
 
 unsigned int io_get_cursor(void)
 {
-	return cursor;
+	return screen_get_cursor(io_current_screen());
 }
 
 void io_set_cursor(unsigned int pos)
 {
-	if (pos >= io_rows * io_cols)
-		return;
-	hide_cursor_at(cursor);
-	cursor = pos;
-	cursor_cell = (unsigned short)(VGA_ATTR << 8);
-	fbtext_set_cursor(cursor % io_cols, cursor / io_cols);
+	screen_set_cursor(io_current_screen(), pos);
 }
 
 void io_cursor_show_block(void)
 {
-	fbtext_set_cursor(cursor % io_cols, cursor / io_cols);
-	fbtext_show_cursor();
+	struct text_screen *s = io_current_screen();
+	s->cursor_visible = 1;
+	s->dirty = 1;
 }
 
 void io_cursor_hide_block(void)
 {
-	fbtext_hide_cursor((unsigned char)(cursor_cell & 0xFF), (unsigned char)(cursor_cell >> 8));
+	struct text_screen *s = io_current_screen();
+	s->cursor_visible = 0;
+	s->dirty = 1;
 }
 
 void io_vga_write(unsigned int pos, unsigned short value)
 {
-	unsigned int col = pos % io_cols;
-	unsigned int row = pos / io_cols;
-	fbtext_put_char_at(col, row, (unsigned char)(value & 0xFF), (unsigned char)(value >> 8));
+	screen_put_at(io_current_screen(), pos, value);
 }
 
 void io_drain_keyboard(void)

+ 2 - 2
kernel.c

@@ -4,8 +4,8 @@
 #include "io.h"
 #include "mouse.h"
 #include "process.h"
-#include "terminal.h"
 #include "timer.h"
+#include "wm.h"
 #include "serial.h"
 #include "version.h"
 #include "vga.h"
@@ -139,5 +139,5 @@ void kernel_main(void)
 	process_init();
 	vga_shadow_init();
 	vga_fb_flush();
-	terminal_run();
+	wm_run();
 }

+ 5 - 0
kexec.c

@@ -32,6 +32,8 @@
 #define KEXEC_MAX   0x28200    /* max = kernel region 0x7E00..0x30000 */
 
 volatile int kexec_busy;
+volatile unsigned int kexec_progress;
+volatile unsigned int kexec_total;
 
 extern unsigned char kexec_trampoline[];
 extern unsigned char kexec_trampoline_end[];
@@ -107,12 +109,15 @@ static int kexec_recv(unsigned int *entry, unsigned int *size)
 	}
 	if (*size == 0 || *size > KEXEC_MAX)
 		return kexec_fail("kexec: bad size\n");
+	kexec_total = *size;
+	kexec_progress = 0;
 
 	for (n = 0; n < *size; n++) {
 		if (!serial_ring_wait(5000, &c))
 			return kexec_fail("kexec: timeout (data)\n");
 		dst[n] = c;
 		sum += c;
+		kexec_progress = n + 1;
 		if ((n & 0xFFF) == 0xFFF)
 			io_putchar('.');
 	}

+ 4 - 0
kexec.h

@@ -7,6 +7,10 @@ void program_kexec_main(void);
 /* Non-zero while a kexec receive is in progress (prevents double-trigger). */
 extern volatile int kexec_busy;
 
+/* Receive progress, in bytes. kexec_total is 0 until the header is parsed. */
+extern volatile unsigned int kexec_progress;
+extern volatile unsigned int kexec_total;
+
 /* Returns 1 if the serial ring buffer starts with the "KXEC" magic, i.e. the
  * host has begun sending a new kernel image. */
 int kexec_pending(void);

+ 32 - 0
mouse.c

@@ -43,6 +43,7 @@ static int mouse_streaming_flag;
 static unsigned int mouse_id;
 static int mouse_cursor_flag;
 static unsigned int mouse_pkt_size;
+static unsigned int mouse_buttons;  /* bit0=left bit1=right bit2=middle */
 
 static unsigned int fbw, fbh;
 static unsigned int cur_x, cur_y;
@@ -254,6 +255,7 @@ void mouse_poll_update(void)
 		pkt_idx = 0;
 
 		b0 = pkt[0];
+		mouse_buttons = b0 & 0x07;
 		dx = (int)(signed char)pkt[1];
 		dy = (int)(signed char)pkt[2];
 		if (b0 & 0x10)
@@ -304,3 +306,33 @@ unsigned int mouse_packet_size(void)
 {
 	return mouse_pkt_size;
 }
+
+unsigned int mouse_get_x(void)
+{
+	return cur_x;
+}
+
+unsigned int mouse_get_y(void)
+{
+	return cur_y;
+}
+
+unsigned int mouse_buttons_state(void)
+{
+	return mouse_buttons;
+}
+
+void mouse_redraw_cursor(void)
+{
+	if (!mouse_cursor_flag)
+		return;
+	vga_fb_read_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H, cur_saved);
+	mouse_draw_cursor(cur_x, cur_y);
+}
+
+void mouse_cursor_erase(void)
+{
+	if (!mouse_cursor_flag)
+		return;
+	vga_fb_write_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H, cur_saved);
+}

+ 12 - 0
mouse.h

@@ -10,4 +10,16 @@ unsigned int mouse_device_id(void);
 int mouse_cursor_visible(void);
 unsigned int mouse_packet_size(void);
 
+/* Current cursor position (hotspot) in framebuffer pixels. */
+unsigned int mouse_get_x(void);
+unsigned int mouse_get_y(void);
+/* Button state bitmask: bit0 = left, bit1 = right, bit2 = middle. */
+unsigned int mouse_buttons_state(void);
+/* Restore the saved background under the cursor (erase the cursor). Call
+   before a scene redraw so mouse_redraw_cursor() re-saves a clean background. */
+void mouse_cursor_erase(void);
+/* Re-save the region under the cursor and redraw it on top (after the WM
+   redraws windows underneath a stationary cursor). */
+void mouse_redraw_cursor(void);
+
 #endif

+ 48 - 6
process.c

@@ -28,9 +28,10 @@ void process_init(void)
 	for (i = 0; i < MAX_PROCS; i++)
 		procs[i].state = PROC_UNUSED;
 
-	procs[0].state = PROC_RUNNING;
-	procs[0].pid   = 0;
-	name_copy(procs[0].name, "terminal");
+	procs[0].state  = PROC_RUNNING;
+	procs[0].pid    = 0;
+	procs[0].screen = (void *)0;
+	name_copy(procs[0].name, "wm");
 	current_pid = 0;
 }
 
@@ -97,10 +98,13 @@ static int setup_process(const char *name, unsigned int entry,
 	*--sp = 0;              /* ESI */
 	*--sp = 0;              /* EDI */
 
-	procs[slot].esp   = (unsigned int)sp;
-	procs[slot].state = PROC_READY;
-	procs[slot].pid   = next_pid_counter++;
+	procs[slot].esp    = (unsigned int)sp;
+	procs[slot].state  = PROC_READY;
+	procs[slot].pid    = next_pid_counter++;
 	name_copy(procs[slot].name, name);
+	/* Child inherits the parent's output screen (command output lands in the
+	   terminal that spawned it). */
+	procs[slot].screen = procs[current_pid].screen;
 
 	return (int)procs[slot].pid;
 }
@@ -116,6 +120,44 @@ int process_create_arg(const char *name, void (*entry)(const char *),
 	return setup_process(name, (unsigned int)entry, 1, arg);
 }
 
+/* Create a process whose entry takes a raw pointer argument (no string copy). */
+int process_create_ptr(const char *name, void (*entry)(void *), void *arg)
+{
+	unsigned int slot, top, *sp;
+
+	for (slot = 1; slot < MAX_PROCS; slot++)
+		if (procs[slot].state == PROC_UNUSED)
+			break;
+	if (slot == MAX_PROCS)
+		return -1;
+
+	top = stack_top_for(slot);
+	sp = (unsigned int *)top;
+
+	*--sp = (unsigned int)arg;        /* first C parameter */
+	*--sp = (unsigned int)process_exit;/* return address */
+	*--sp = 0x202;                    /* EFLAGS: IF=1 */
+	*--sp = 0x08;                     /* CS */
+	*--sp = (unsigned int)entry;      /* EIP */
+	*--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0;  /* EAX ECX EDX EBX */
+	*--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0;  /* ESP EBP ESI EDI */
+
+	procs[slot].esp    = (unsigned int)sp;
+	procs[slot].state  = PROC_READY;
+	procs[slot].pid    = next_pid_counter++;
+	name_copy(procs[slot].name, name);
+	procs[slot].screen = procs[current_pid].screen;
+
+	return (int)procs[slot].pid;
+}
+
+/* Set the output screen of the currently-running process (used by a terminal
+   at startup to bind itself to its own screen). */
+void process_set_screen(struct text_screen *screen)
+{
+	procs[current_pid].screen = screen;
+}
+
 void process_exit(void)
 {
 	procs[current_pid].state = PROC_DEAD;

+ 5 - 0
process.h

@@ -15,11 +15,14 @@
 
 #define PROC_NAME_LEN 12
 
+struct text_screen;  /* forward decl (see screen.h) */
+
 struct process {
 	unsigned int esp;
 	unsigned int state;
 	unsigned int pid;
 	char         name[PROC_NAME_LEN];
+	struct text_screen *screen;  /* output target for io_putchar & co. */
 };
 
 extern struct process procs[MAX_PROCS];
@@ -29,6 +32,8 @@ void process_init(void);
 int  process_create(const char *name, void (*entry)(void));
 int  process_create_arg(const char *name, void (*entry)(const char *),
 			const char *arg);
+int  process_create_ptr(const char *name, void (*entry)(void *), void *arg);
+void process_set_screen(struct text_screen *screen);
 void process_exit(void);
 void schedule(void);
 void process_ps(void);

+ 119 - 0
screen.c

@@ -0,0 +1,119 @@
+/* Text screen: cell-buffer text output (no framebuffer access). */
+
+#include "screen.h"
+
+#define SCREEN_ATTR 0x0A  /* light green on black */
+
+void screen_init(struct text_screen *s)
+{
+	s->cols = SCREEN_COLS;
+	s->rows = SCREEN_ROWS;
+	s->cursor = 0;
+	s->cursor_attr = (SCREEN_ATTR << 8);
+	s->cursor_visible = 0;
+	s->dirty = 1;
+	s->scrolled = 0;
+	screen_clear(s);
+}
+
+void screen_clear(struct text_screen *s)
+{
+	unsigned int i, n = s->cols * s->rows;
+	for (i = 0; i < n; i++)
+		s->cells[i] = (unsigned short)((SCREEN_ATTR << 8) | ' ');
+	s->cursor = 0;
+	s->dirty = 1;
+	s->scrolled = 0;
+}
+
+static void screen_scroll_up(struct text_screen *s)
+{
+	unsigned int ncols = s->cols;
+	unsigned int total = s->cols * s->rows;
+	unsigned int i;
+	for (i = 0; i < total - ncols; i++)
+		s->cells[i] = s->cells[i + ncols];
+	for (; i < total; i++)
+		s->cells[i] = (unsigned short)((SCREEN_ATTR << 8) | ' ');
+	s->dirty = 1;
+}
+
+void screen_putchar(struct text_screen *s, char c)
+{
+	unsigned int ncols = s->cols;
+	unsigned int total = s->cols * s->rows;
+
+	if (c == '\n') {
+		s->cursor = (s->cursor / ncols + 1) * ncols;
+		if (s->cursor >= total) {
+			screen_scroll_up(s);
+			s->cursor = total - ncols;
+			s->scrolled = 1;
+		}
+		s->cursor_attr = (SCREEN_ATTR << 8);
+		s->dirty = 1;
+		return;
+	}
+	if (c == '\b') {
+		if (s->cursor > 0) {
+			s->cursor--;
+			s->cells[s->cursor] = (unsigned short)((SCREEN_ATTR << 8) | ' ');
+			s->dirty = 1;
+		}
+		return;
+	}
+	if (s->cursor < total) {
+		s->cells[s->cursor] = (unsigned short)(s->cursor_attr | (unsigned char)c);
+		s->cursor++;
+		s->cursor_attr = (SCREEN_ATTR << 8);
+		s->dirty = 1;
+	}
+}
+
+void screen_putstr(struct text_screen *s, const char *str)
+{
+	while (*str)
+		screen_putchar(s, *str++);
+}
+
+void screen_puthex(struct text_screen *s, unsigned int n)
+{
+	static const char hex[] = "0123456789ABCDEF";
+	char buf[9];
+	unsigned int i = 8;
+	buf[8] = '\0';
+	do {
+		buf[--i] = hex[n & 0xF];
+		n >>= 4;
+	} while (i > 0);
+	screen_putstr(s, buf);
+}
+
+unsigned int screen_get_cursor(const struct text_screen *s)
+{
+	return s->cursor;
+}
+
+void screen_set_cursor(struct text_screen *s, unsigned int pos)
+{
+	if (pos >= s->cols * s->rows)
+		return;
+	s->cursor = pos;
+	s->cursor_attr = (SCREEN_ATTR << 8);
+}
+
+void screen_put_at(struct text_screen *s, unsigned int pos, unsigned short value)
+{
+	if (pos < s->cols * s->rows) {
+		s->cells[pos] = value;
+		s->dirty = 1;
+	}
+}
+
+int screen_did_scroll(struct text_screen *s)
+{
+	if (!s->scrolled)
+		return 0;
+	s->scrolled = 0;
+	return 1;
+}

+ 31 - 0
screen.h

@@ -0,0 +1,31 @@
+/* Text screen: a grid of character cells (char + VGA attribute) plus a cursor.
+   Pure cell-buffer logic — no framebuffer access. The window manager renders
+   these screens into windows. */
+#ifndef SCREEN_H
+#define SCREEN_H
+
+#define SCREEN_COLS 64
+#define SCREEN_ROWS 20
+
+struct text_screen {
+	unsigned short cells[SCREEN_COLS * SCREEN_ROWS];
+	unsigned int cols;
+	unsigned int rows;
+	unsigned int cursor;          /* linear index 0..cols*rows-1 */
+	unsigned int cursor_attr;
+	unsigned int cursor_visible;
+	int dirty;                    /* content changed since last WM render */
+	int scrolled;                 /* one-shot, replaces io_did_scroll */
+};
+
+void screen_init(struct text_screen *s);
+void screen_clear(struct text_screen *s);
+void screen_putchar(struct text_screen *s, char c);
+void screen_putstr(struct text_screen *s, const char *str);
+void screen_puthex(struct text_screen *s, unsigned int n);
+unsigned int screen_get_cursor(const struct text_screen *s);
+void screen_set_cursor(struct text_screen *s, unsigned int pos);
+void screen_put_at(struct text_screen *s, unsigned int pos, unsigned short value);
+int screen_did_scroll(struct text_screen *s);
+
+#endif

+ 1 - 1
switch.S

@@ -24,7 +24,7 @@
 
 /* struct process offsets (must match process.h / process.c) */
 .equ PCB_ESP,   0
-.equ PCB_SIZE,  24      /* sizeof(struct process): 4+4+4+12 = 24 */
+.equ PCB_SIZE,  28      /* sizeof(struct process): 4+4+4+12+4 = 28 */
 
 .equ TIMESLICE, 20
 .equ FLUSH_INTERVAL, 33 /* 1000Hz PIT / 30Hz flush ≈ 33 ticks */

+ 126 - 217
terminal.c

@@ -3,16 +3,11 @@
 
 #include "io.h"
 #include "dbg.h"
-#include "fbtext.h"
 #include "idle.h"
 #include "kexec.h"
-#include "mouse.h"
 #include "process.h"
-#include "rtc.h"
 #include "terminal.h"
 #include "timer.h"
-#include "version.h"
-#include "vga.h"
 
 extern void program_hello_main(void);
 extern void program_reboot_main(void);
@@ -36,68 +31,9 @@ extern void program_lsusb_main(void);
 extern void program_storage_main(void);
 extern void program_format_main(const char *arg);
 
-#define DATETIME_MS 1000
-#define LINE_MAX 64
-#define HISTORY_SIZE 32
-static unsigned int last_datetime_tick;
-static char datetime_buf[IO_DATETIME_LEN + 1];
-static char history[HISTORY_SIZE][LINE_MAX];
-static unsigned int history_count;
-static unsigned int history_next;
-
-static int cursor_visible;
-static unsigned int last_cursor_tick;
-
-static void redraw_datetime(void)
-{
-	rtc_read_datetime(datetime_buf);
-	{
-		unsigned int i;
-		for (i = 0; i < IO_DATETIME_LEN && datetime_buf[i]; i++)
-			io_vga_write(IO_DATETIME_POS + i,
-				(unsigned short)((0x0A << 8) | (unsigned char)datetime_buf[i]));
-	}
-}
-
-static void update_datetime(void)
-{
-	unsigned int t = get_ticks_ms();
-	if (t - last_datetime_tick >= DATETIME_MS) {
-		last_datetime_tick = t;
-		redraw_datetime();
-	}
-}
-
-static void update_cursor_blink(void)
-{
-	unsigned int t = get_ticks_ms();
-	if (t - last_cursor_tick >= CURSOR_MS) {
-		last_cursor_tick = t;
-		cursor_visible = !cursor_visible;
-	}
-	if (cursor_visible)
-		io_cursor_show_block();
-	else
-		io_cursor_hide_block();
-}
-
-static const char banner[] __attribute__((section(".text"))) = OS_NAME " v" OS_VERSION;
-#define BANNER_LEN 14
 static const char prompt[] __attribute__((section(".text"))) = "\n> ";
 static const char prompt_first[] __attribute__((section(".text"))) = "> ";
 
-/* Draw row 0: banner left, datetime right. Call when terminal is active. */
-static void draw_header_row(void)
-{
-	unsigned int i;
-	for (i = 0; i < BANNER_LEN && banner[i]; i++)
-		io_vga_write(i, (unsigned short)((0x0A << 8) | (unsigned char)banner[i]));
-	for (; i < IO_DATETIME_POS; i++)
-		io_vga_write(i, (unsigned short)(0x0A << 8));
-	redraw_datetime();
-	fbtext_draw_rainbow_separator();
-}
-
 static int is_cmd_hello(const char *line, unsigned int line_len)
 {
 	unsigned int i = 0;
@@ -615,27 +551,26 @@ static void redraw_line_clear(const char *line, unsigned int line_len,
 	io_set_cursor(line_start + line_pos);
 }
 
-/* Copy history entry (1 = newest) into line, set line_len/line_pos, redraw from start. */
-static void history_load_into_line(char *line, unsigned int *line_len, unsigned int *line_pos,
-	unsigned int line_start, unsigned int browse)
+/* Copy history entry (1 = newest) into t->line, redraw from start. */
+static void history_load_into_line(struct term *t, unsigned int browse)
 {
 	unsigned int slot, len, old_len;
 	const char *src;
-	if (browse < 1 || browse > history_count)
+	if (browse < 1 || browse > t->history_count)
 		return;
-	old_len = *line_len;
-	slot = (history_next + HISTORY_SIZE - browse) % HISTORY_SIZE;
-	src = history[slot];
+	old_len = t->line_len;
+	slot = (t->history_next + HISTORY_SIZE - browse) % HISTORY_SIZE;
+	src = t->history[slot];
 	for (len = 0; len < LINE_MAX - 1 && src[len]; len++)
-		line[len] = src[len];
-	line[len] = '\0';
-	*line_len = len;
-	*line_pos = len;
-	redraw_line_clear(line, len, len, line_start, old_len > len ? old_len : len);
+		t->line[len] = src[len];
+	t->line[len] = '\0';
+	t->line_len = len;
+	t->line_pos = len;
+	redraw_line_clear(t->line, len, len, t->line_start, old_len > len ? old_len : len);
 }
 
 /* Push current line to history if non-empty (and not duplicate of newest). */
-static void history_push(const char *line, unsigned int line_len)
+static void history_push(struct term *t, const char *line, unsigned int line_len)
 {
 	unsigned int i, slot;
 	int all_spaces = 1;
@@ -649,133 +584,113 @@ static void history_push(const char *line, unsigned int line_len)
 	}
 	if (all_spaces)
 		return;
-	if (history_count > 0) {
-		slot = (history_next + HISTORY_SIZE - 1) % HISTORY_SIZE;
-		for (i = 0; i < line_len && i < LINE_MAX - 1 && history[slot][i]; i++) {
-			if (history[slot][i] != line[i])
+	if (t->history_count > 0) {
+		slot = (t->history_next + HISTORY_SIZE - 1) % HISTORY_SIZE;
+		for (i = 0; i < line_len && i < LINE_MAX - 1 && t->history[slot][i]; i++) {
+			if (t->history[slot][i] != line[i])
 				break;
 		}
-		if (i == line_len && history[slot][i] == '\0')
+		if (i == line_len && t->history[slot][i] == '\0')
 			return;  /* duplicate of newest */
 	}
-	slot = history_next;
+	slot = t->history_next;
 	for (i = 0; i < line_len && i < LINE_MAX - 1; i++)
-		history[slot][i] = line[i];
-	history[slot][i] = '\0';
-	history_next = (history_next + 1) % HISTORY_SIZE;
-	if (history_count < HISTORY_SIZE)
-		history_count++;
-}
-
-void terminal_run(void)
-{
-	static char line[LINE_MAX];
-	static unsigned int line_len;
-	static unsigned int line_pos;   /* cursor index in line, 0..line_len */
-	static unsigned int line_start; /* VGA position of first char of line */
-	static unsigned int history_browse; /* 0 = not browsing, 1 = newest, 2 = older, ... */
-
-	last_cursor_tick   = get_ticks_ms();
-	last_datetime_tick = get_ticks_ms();
-	cursor_visible     = 1;
-	line_len           = 0;
-	line_pos           = 0;
-	line[0]            = '\0';
-	history_browse     = 0;
-
-	io_drain_keyboard();
+		t->history[slot][i] = line[i];
+	t->history[slot][i] = '\0';
+	t->history_next = (t->history_next + 1) % HISTORY_SIZE;
+	if (t->history_count < HISTORY_SIZE)
+		t->history_count++;
+}
+
+void term_run(struct term *t)
+{
+	t->line_len         = 0;
+	t->line_pos         = 0;
+	t->line[0]          = '\0';
+	t->history_browse   = 0;
+
 	io_clear_screen();
-	draw_header_row();
-	io_set_cursor(IO_CONTENT_OFFSET);
 	io_putstr(prompt_first);
-	line_start = io_get_cursor();
+	t->line_start = io_get_cursor();
 
 	for (;;) {
-		vga_fb_flush_pending_check();
-		mouse_poll_update();
-		update_datetime();
-		update_cursor_blink();
 		{
-			int k = io_poll_key();
+			int k = term_key_pop(t);
 			if (k) {
 				if (k == IO_KEY_LEFT) {
-					if (line_pos > 0) {
-						line_pos--;
-						io_set_cursor(line_start + line_pos);
+					if (t->line_pos > 0) {
+						t->line_pos--;
+						io_set_cursor(t->line_start + t->line_pos);
 					}
 				} else if (k == IO_KEY_RIGHT) {
-					if (line_pos < line_len) {
-						line_pos++;
-						io_set_cursor(line_start + line_pos);
+					if (t->line_pos < t->line_len) {
+						t->line_pos++;
+						io_set_cursor(t->line_start + t->line_pos);
 					}
 				} else if (k == IO_KEY_UP) {
-					if (history_count > 0) {
-						if (history_browse < history_count)
-							history_browse++;
-						history_load_into_line(line, &line_len, &line_pos, line_start, history_browse);
+					if (t->history_count > 0) {
+						if (t->history_browse < t->history_count)
+							t->history_browse++;
+						history_load_into_line(t, t->history_browse);
 					}
 				} else if (k == IO_KEY_DOWN) {
-					if (history_browse > 0) {
-						history_browse--;
-						if (history_browse == 0) {
-							{
-								unsigned int clear_len = line_len;
-								line_len = 0;
-								line_pos = 0;
-								line[0]  = '\0';
-								redraw_line_clear(line, 0, 0, line_start, clear_len);
-							}
+					if (t->history_browse > 0) {
+						t->history_browse--;
+						if (t->history_browse == 0) {
+							unsigned int clear_len = t->line_len;
+							t->line_len = 0;
+							t->line_pos = 0;
+							t->line[0]  = '\0';
+							redraw_line_clear(t->line, 0, 0, t->line_start, clear_len);
 						} else {
-							history_load_into_line(line, &line_len, &line_pos, line_start, history_browse);
+							history_load_into_line(t, t->history_browse);
 						}
 					}
 				} else if (k == '\b') {
-					if (line_pos > 0) {
+					if (t->line_pos > 0) {
 						unsigned int i;
-						line_pos--;
-						for (i = line_pos; i < line_len; i++)
-							line[i] = line[i + 1];
-						line_len--;
-						line[line_len] = '\0';
-						redraw_line_from(line, line_len, line_pos, line_start, line_pos);
+						t->line_pos--;
+						for (i = t->line_pos; i < t->line_len; i++)
+							t->line[i] = t->line[i + 1];
+						t->line_len--;
+						t->line[t->line_len] = '\0';
+						redraw_line_from(t->line, t->line_len, t->line_pos, t->line_start, t->line_pos);
 					}
 				} else if (k == '\n') {
-					static char cat_fname[CAT_FNAME_MAX];
-					static char save_args[SAVE_ARGS_MAX];
-					static char rm_fname[RM_FNAME_MAX];
-					static char ls_path[LS_PATH_MAX];
-					static char format_dev[FORMAT_DEV_MAX];
+					char cat_fname[CAT_FNAME_MAX];
+					char save_args[SAVE_ARGS_MAX];
+					char rm_fname[RM_FNAME_MAX];
+					char ls_path[LS_PATH_MAX];
+					char format_dev[FORMAT_DEV_MAX];
 					unsigned int kill_pid;
 					int skip_prompt = 0;
 					io_putchar('\n');
-					history_push(line, line_len);
-					history_browse = 0;
-					if (is_cmd_hello(line, line_len)) {
+					history_push(t, t->line, t->line_len);
+					t->history_browse = 0;
+					if (is_cmd_hello(t->line, t->line_len)) {
 						if (process_create("hello", program_hello_main) < 0)
 							io_putstr("error: too many processes\n");
-					} else if (is_cmd_reboot(line, line_len)) {
+					} else if (is_cmd_reboot(t->line, t->line_len)) {
 						program_reboot_main();
-					} else if (is_cmd_poweroff(line, line_len)) {
+					} else if (is_cmd_poweroff(t->line, t->line_len)) {
 						program_poweroff_main();
-					} else if (is_cmd_ls(line, line_len, ls_path, LS_PATH_MAX)) {
+					} else if (is_cmd_ls(t->line, t->line_len, ls_path, LS_PATH_MAX)) {
 						if (process_create_arg("ls", program_ls_main, ls_path) < 0)
 							io_putstr("error: too many processes\n");
-					} else if (is_cmd_lsusb(line, line_len)) {
+					} else if (is_cmd_lsusb(t->line, t->line_len)) {
 						if (process_create("lsusb", program_lsusb_main) < 0)
 							io_putstr("error: too many processes\n");
-					} else if (is_cmd_storage(line, line_len)) {
+					} else if (is_cmd_storage(t->line, t->line_len)) {
 						if (process_create("storage", program_storage_main) < 0)
 							io_putstr("error: too many processes\n");
-					} else if (is_cmd_clock(line, line_len)) {
-						if (process_create("clock", program_clock_main) < 0)
-							io_putstr("error: too many processes\n");
-					} else if (is_cmd_colors(line, line_len)) {
-						if (process_create("colors", program_colors_main) < 0)
-							io_putstr("error: too many processes\n");
-					} else if (is_cmd_cat(line, line_len, cat_fname, CAT_FNAME_MAX)) {
+					} else if (is_cmd_clock(t->line, t->line_len)) {
+						io_putstr("clock: not available in windowed mode yet\n");
+					} else if (is_cmd_colors(t->line, t->line_len)) {
+						io_putstr("colors: not available in windowed mode yet\n");
+					} else if (is_cmd_cat(t->line, t->line_len, cat_fname, CAT_FNAME_MAX)) {
 						if (process_create_arg("cat", program_cat_main, cat_fname) < 0)
 							io_putstr("error: too many processes\n");
-					} else if (is_cmd_run(line, line_len, cat_fname, CAT_FNAME_MAX)) {
+					} else if (is_cmd_run(t->line, t->line_len, cat_fname, CAT_FNAME_MAX)) {
 						if (cat_fname[0] == 's' && cat_fname[1] == 'e' &&
 						    cat_fname[2] == 'r' && cat_fname[3] == 'i' &&
 						    cat_fname[4] == 'a' && cat_fname[5] == 'l' &&
@@ -783,43 +698,41 @@ void terminal_run(void)
 							if (process_create_arg("run_serial", program_run_serial_main, cat_fname) < 0)
 								io_putstr("error: too many processes\n");
 						} else {
-							if (process_create_arg("run", program_run_main, cat_fname) < 0)
-								io_putstr("error: too many processes\n");
+							io_putstr("run: not available in windowed mode yet\n");
 						}
-					} else if (is_cmd_save(line, line_len, save_args, SAVE_ARGS_MAX)) {
+					} else if (is_cmd_save(t->line, t->line_len, save_args, SAVE_ARGS_MAX)) {
 						if (process_create_arg("save", program_save_main, save_args) < 0)
 							io_putstr("error: too many processes\n");
-					} else if (is_cmd_mkdir(line, line_len, rm_fname, RM_FNAME_MAX)) {
+					} else if (is_cmd_mkdir(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
 						if (process_create_arg("mkdir", program_mkdir_main, rm_fname) < 0)
 							io_putstr("error: too many processes\n");
-					} else if (is_cmd_rmdir(line, line_len, rm_fname, RM_FNAME_MAX)) {
+					} else if (is_cmd_rmdir(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
 						if (process_create_arg("rmdir", program_rmdir_main, rm_fname) < 0)
 							io_putstr("error: too many processes\n");
-					} else if (is_cmd_rm(line, line_len, rm_fname, RM_FNAME_MAX)) {
+					} else if (is_cmd_rm(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
 						if (process_create_arg("rm", program_rm_main, rm_fname) < 0)
 							io_putstr("error: too many processes\n");
-					} else if (is_cmd_space(line, line_len)) {
-						if (process_create_arg("space", program_run_main, "/floppy1/SPACE.BIN") < 0)
-							io_putstr("error: too many processes\n");
-					} else if (is_cmd_display(line, line_len)) {
+					} else if (is_cmd_space(t->line, t->line_len)) {
+						io_putstr("space: not available in windowed mode yet\n");
+					} else if (is_cmd_display(t->line, t->line_len)) {
 						if (process_create("display", program_display_main) < 0)
 							io_putstr("error: too many processes\n");
-					} else if (is_cmd_cpuinfo(line, line_len)) {
+					} else if (is_cmd_cpuinfo(t->line, t->line_len)) {
 						if (process_create("cpuinfo", program_cpuinfo_main) < 0)
 							io_putstr("error: too many processes\n");
-					} else if (is_cmd_mouseinfo(line, line_len)) {
+					} else if (is_cmd_mouseinfo(t->line, t->line_len)) {
 						if (process_create("mouseinfo", program_mouseinfo_main) < 0)
 							io_putstr("error: too many processes\n");
-					} else if (is_cmd_format(line, line_len, format_dev, FORMAT_DEV_MAX)) {
+					} else if (is_cmd_format(t->line, t->line_len, format_dev, FORMAT_DEV_MAX)) {
 						program_format_main(format_dev);
-					} else if (is_cmd_ps(line, line_len)) {
+					} else if (is_cmd_ps(t->line, t->line_len)) {
 						process_ps();
-					} else if (is_cmd_kill(line, line_len, &kill_pid)) {
+					} else if (is_cmd_kill(t->line, t->line_len, &kill_pid)) {
 						if (process_kill(kill_pid) < 0)
 							io_putstr("kill: no such process\n");
 						else
 							io_putstr("killed\n");
-					} else if (is_cmd_kexec(line, line_len)) {
+					} else if (is_cmd_kexec(t->line, t->line_len)) {
 						if (kexec_busy) {
 							io_putstr("kexec: already receiving\n");
 						} else {
@@ -829,9 +742,9 @@ void terminal_run(void)
 								io_putstr("error: too many processes\n");
 							}
 						}
-					} else if (is_cmd_dbg(line, line_len)) {
+					} else if (is_cmd_dbg(t->line, t->line_len)) {
 						program_dbg_main();
-					} else if (is_cmd_help(line, line_len)) {
+					} else if (is_cmd_help(t->line, t->line_len)) {
 						io_putstr("hello\n");
 						io_putstr("reboot\n");
 						io_putstr("poweroff\n");
@@ -857,16 +770,14 @@ void terminal_run(void)
 						io_putstr("dbg\n");
 						io_putstr("serial test|recv\n");
 						io_putstr("clear\n");
-				} else if (is_cmd_clear(line, line_len)) {
+				} else if (is_cmd_clear(t->line, t->line_len)) {
 					io_clear_screen();
-					draw_header_row();
-					io_set_cursor(IO_CONTENT_OFFSET);
 					io_putstr(prompt_first);
-					line_start = io_get_cursor();
+					t->line_start = io_get_cursor();
 					skip_prompt = 1;
 				} else {
-					static char serial_arg[8];
-					if (is_cmd_serial(line, line_len, serial_arg, sizeof(serial_arg))) {
+					char serial_arg[8];
+					if (is_cmd_serial(t->line, t->line_len, serial_arg, sizeof(serial_arg))) {
 						if (serial_arg[0] == 't' && serial_arg[1] == 'e' &&
 						    serial_arg[2] == 's' && serial_arg[3] == 't' &&
 						    serial_arg[4] == '\0') {
@@ -881,45 +792,43 @@ void terminal_run(void)
 						}
 					} else {
 						unsigned int i = 0;
-						while (i < line_len && line[i] == ' ')
+						while (i < t->line_len && t->line[i] == ' ')
 							i++;
-						if (i < line_len)
+						if (i < t->line_len)
 							io_putstr("command not found\n");
 					}
 				}
-				line_len  = 0;
-				line_pos  = 0;
-				line[0]   = '\0';
+				t->line_len = 0;
+				t->line_pos = 0;
+				t->line[0]  = '\0';
 				if (!skip_prompt) {
 					io_putstr(prompt);
-					line_start = io_get_cursor();
+					t->line_start = io_get_cursor();
 				}
-				if (io_did_scroll())
-					redraw_datetime();
 			} else {
-				{
-					unsigned char u = (unsigned char)k;
-					if (u >= 0x20 && u < 0x7F && line_len < LINE_MAX - 1) {
-							unsigned int i;
-							history_browse = 0;
-							for (i = line_len; i > line_pos; i--)
-								line[i] = line[i - 1];
-							line[line_pos] = (char)k;
-							line_len++;
-							line[line_len] = '\0';
-							line_pos++;
-							redraw_line_from(line, line_len, line_pos, line_start, line_pos - 1);
-						}
-					}
-				}
-			} else {
-				if (!kexec_busy && kexec_pending()) {
-					kexec_busy = 1;
-					if (process_create("kexec", program_kexec_main) < 0)
-						kexec_busy = 0;
+				unsigned char u = (unsigned char)k;
+				if (u >= 0x20 && u < 0x7F && t->line_len < LINE_MAX - 1) {
+					unsigned int i;
+					t->history_browse = 0;
+					for (i = t->line_len; i > t->line_pos; i--)
+						t->line[i] = t->line[i - 1];
+					t->line[t->line_pos] = (char)k;
+					t->line_len++;
+					t->line[t->line_len] = '\0';
+					t->line_pos++;
+					redraw_line_from(t->line, t->line_len, t->line_pos, t->line_start, t->line_pos - 1);
 				}
-				cpu_idle();
 			}
+		} else {
+			cpu_idle();
 		}
 	}
 }
+}
+
+void term_entry(void *arg)
+{
+	struct term *t = (struct term *)arg;
+	process_set_screen(&t->screen);
+	term_run(t);
+}

+ 46 - 3
terminal.h

@@ -1,9 +1,52 @@
-/* Terminal: prompt and echo loop. Uses io and timer. */
+/* Terminal-in-a-window: per-instance state and the prompt/echo loop. */
 #ifndef TERMINAL_H
 #define TERMINAL_H
 
-#define CURSOR_MS  500
+#include "screen.h"
 
-void terminal_run(void);
+#define CURSOR_MS    500
+#define LINE_MAX     64
+#define HISTORY_SIZE 32
+#define KEY_BUF_SIZE 16
+
+struct term {
+	struct text_screen screen;
+	char history[HISTORY_SIZE][LINE_MAX];
+	unsigned int history_count;
+	unsigned int history_next;
+	char line[LINE_MAX];
+	unsigned int line_len;
+	unsigned int line_pos;
+	unsigned int line_start;
+	unsigned int history_browse;
+	/* Per-terminal key queue: WM (slot 0) pushes, terminal process pops. */
+	int keys[KEY_BUF_SIZE];
+	unsigned int key_head;
+	unsigned int key_tail;
+};
+
+/* Entry point for a terminal process (arg = struct term *). */
+void term_entry(void *arg);
+void term_run(struct term *t);
+
+/* Single-producer (WM) / single-consumer (terminal) key queue. */
+static inline void term_key_push(struct term *t, int k)
+{
+	unsigned int next = (t->key_head + 1) % KEY_BUF_SIZE;
+	if (next != t->key_tail) {
+		t->keys[t->key_head] = k;
+		t->key_head = next;
+	}
+}
+
+static inline int term_key_pop(struct term *t)
+{
+	int k;
+	if (t->key_tail == t->key_head)
+		return 0;
+	k = t->keys[t->key_tail];
+	t->key_tail = (t->key_tail + 1) % KEY_BUF_SIZE;
+	return k;
+}
 
 #endif

+ 53 - 0
vga.c

@@ -232,6 +232,59 @@ void vga_put_pixel_rgb(unsigned int x, unsigned int y, unsigned int r, unsigned
 	shadow_put_pixel(x, y, r, g, b);
 }
 
+void vga_fill_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
+		unsigned int r, unsigned int g, unsigned int b)
+{
+	unsigned int row, x1, y1;
+	unsigned int pitch;
+	unsigned int bpp;
+	unsigned char *fb;
+
+	vga_cache_boot_fb();
+	pitch = vga_shadow_pitch;
+	bpp = vga_fb_bpp_val;
+	fb = vga_shadow_buf;
+
+	if (!fb || w == 0 || h == 0)
+		return;
+	if (x >= vga_fb_w || y >= vga_fb_h)
+		return;
+	x1 = x + w;
+	y1 = y + h;
+	if (x1 > vga_fb_w) x1 = vga_fb_w;
+	if (y1 > vga_fb_h) y1 = vga_fb_h;
+
+	if (bpp == 16) {
+		unsigned short val = (unsigned short)(((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3));
+		for (row = y; row < y1; row++) {
+			unsigned short *p = (unsigned short *)(fb + (unsigned long)row * pitch + x * 2);
+			unsigned int xx;
+			for (xx = x; xx < x1; xx++)
+				*p++ = val;
+		}
+	} else if (bpp == 24) {
+		for (row = y; row < y1; row++) {
+			unsigned char *p = fb + (unsigned long)row * pitch + x * 3;
+			unsigned int xx;
+			for (xx = x; xx < x1; xx++) {
+				p[0] = (unsigned char)b;
+				p[1] = (unsigned char)g;
+				p[2] = (unsigned char)r;
+				p += 3;
+			}
+		}
+	} else {
+		unsigned int val = ((unsigned int)r << 16) | ((unsigned int)g << 8) | b;
+		for (row = y; row < y1; row++) {
+			unsigned int *p = (unsigned int *)(fb + (unsigned long)row * pitch + x * 4);
+			unsigned int xx;
+			for (xx = x; xx < x1; xx++)
+				*p++ = val;
+		}
+	}
+	mark_dirty_rect(y, y1 - y);
+}
+
 static unsigned int vga_pixel_bytes(void)
 {
 	if (vga_fb_bpp_val == 16)

+ 2 - 0
vga.h

@@ -30,6 +30,8 @@ void *vga_fb_ptr(void);
 int vga_has_vesa(void);
 
 void vga_put_pixel_rgb(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b);
+void vga_fill_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
+		unsigned int r, unsigned int g, unsigned int b);
 void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b);
 
 /* Tightly packed row-major: each row is w * pixel_bytes contiguous (pixel_bytes from bpp). */

+ 460 - 0
wm.c

@@ -0,0 +1,460 @@
+/* Window manager: fixed-size draggable terminal windows + a system bar.
+   Slot 0 runs wm_run(); each terminal window is a separate process. */
+
+#include "wm.h"
+#include "fbtext.h"
+#include "idle.h"
+#include "io.h"
+#include "kexec.h"
+#include "mouse.h"
+#include "process.h"
+#include "rtc.h"
+#include "timer.h"
+#include "version.h"
+#include "vga.h"
+
+static struct window windows[MAX_WINDOWS];
+static struct term terms[MAX_WINDOWS];
+static unsigned int term_counter;
+static int wm_full_redraw;
+
+/* Mouse drag state. */
+static int drag_active;
+static int drag_window;      /* index into windows[] */
+static int drag_dx, drag_dy; /* mouse offset from window origin */
+static unsigned int prev_buttons;
+
+void wm_request_redraw(void)
+{
+	wm_full_redraw = 1;
+}
+
+struct window *wm_focused_window(void)
+{
+	unsigned int i;
+	for (i = 0; i < MAX_WINDOWS; i++)
+		if (windows[i].used && windows[i].focused)
+			return &windows[i];
+	return (void *)0;
+}
+
+int wm_is_focused(const struct window *w)
+{
+	return w->focused;
+}
+
+static int wm_point_in_rect(unsigned int px, unsigned int py,
+	unsigned int rx, unsigned int ry, unsigned int rw, unsigned int rh)
+{
+	return px >= rx && px < rx + rw && py >= ry && py < ry + rh;
+}
+
+static void wm_make_title(struct window *w, unsigned int n)
+{
+	char num[4];
+	unsigned int i = 0, k = 0;
+	w->title[k++] = 't'; w->title[k++] = 'e'; w->title[k++] = 'r';
+	w->title[k++] = 'm'; w->title[k++] = ' ';
+	do {
+		num[i++] = (char)('0' + (n % 10));
+		n /= 10;
+	} while (n && i < 3);
+	while (i)
+		w->title[k++] = num[--i];
+	w->title[k] = '\0';
+}
+
+int wm_spawn_terminal(void)
+{
+	unsigned int i, j, fbw = vga_fb_width();
+	struct window *w;
+	struct term *t;
+	int pid;
+	unsigned int n;
+
+	for (i = 0; i < MAX_WINDOWS; i++)
+		if (!windows[i].used)
+			break;
+	if (i == MAX_WINDOWS)
+		return -1;
+
+	w = &windows[i];
+	t = &terms[i];
+
+	screen_init(&t->screen);
+	t->screen.cursor_visible = 1;
+	t->key_head = t->key_tail = 0;
+	t->history_count = 0;
+	t->history_next = 0;
+
+	w->used = 1;
+	w->term = t;
+	w->pid = 0;
+	w->focused = 0;
+	w->pw = SCREEN_COLS * 8 + 2;
+	w->ph = WM_TITLE_H + SCREEN_ROWS * 10 + 2;
+
+	/* Cascade position, clamped to the desktop area. */
+	n = term_counter % 5;
+	w->x = 24 + n * 32;
+	w->y = WM_BAR_H + 12 + n * 28;
+	if (w->x + w->pw > fbw)
+		w->x = (fbw > w->pw) ? fbw - w->pw : 0;
+	if (w->y + w->ph > vga_fb_height())
+		w->y = WM_BAR_H + 4;
+
+	term_counter++;
+	wm_make_title(w, term_counter);
+
+	pid = process_create_ptr("term", term_entry, t);
+	if (pid < 0) {
+		w->used = 0;
+		return -1;
+	}
+	w->pid = (unsigned int)pid;
+
+	for (j = 0; j < MAX_WINDOWS; j++)
+		if (windows[j].used)
+			windows[j].focused = (j == i);
+
+	wm_request_redraw();
+	return pid;
+}
+
+static void wm_focus_index(unsigned int idx)
+{
+	unsigned int i;
+	if (windows[idx].used && windows[idx].focused)
+		return;
+	for (i = 0; i < MAX_WINDOWS; i++)
+		if (windows[i].used)
+			windows[i].focused = (i == idx);
+	wm_request_redraw();
+}
+
+static void wm_focus_top(void)
+{
+	unsigned int i;
+	for (i = 0; i < MAX_WINDOWS; i++)
+		if (windows[i].used) {
+			wm_focus_index(i);
+			return;
+		}
+}
+
+static void wm_close_window(unsigned int idx)
+{
+	struct window *w = &windows[idx];
+	if (w->pid)
+		process_kill(w->pid);
+	w->used = 0;
+	w->pid = 0;
+	w->focused = 0;
+	wm_focus_top();
+	wm_request_redraw();
+}
+
+/* Topmost window containing (px,py), or -1. */
+static int wm_window_at(unsigned int px, unsigned int py)
+{
+	unsigned int i;
+	for (i = 0; i < MAX_WINDOWS; i++)
+		if (windows[i].used && windows[i].focused &&
+		    wm_point_in_rect(px, py, windows[i].x, windows[i].y,
+		    	windows[i].pw, windows[i].ph))
+			return (int)i;
+	for (i = 0; i < MAX_WINDOWS; i++)
+		if (windows[i].used && !windows[i].focused &&
+		    wm_point_in_rect(px, py, windows[i].x, windows[i].y,
+		    	windows[i].pw, windows[i].ph))
+			return (int)i;
+	return -1;
+}
+
+static void wm_clamp_window(struct window *w, int nx, int ny)
+{
+	unsigned int fbw = vga_fb_width();
+	unsigned int fbh = vga_fb_height();
+	int maxx = (int)fbw - (int)w->pw;
+	int maxy = (int)fbh - (int)w->ph;
+	if (maxx < 0)
+		maxx = 0;
+	if (maxy < (int)WM_BAR_H)
+		maxy = (int)WM_BAR_H;
+	if (nx < 0)
+		nx = 0;
+	else if (nx > maxx)
+		nx = maxx;
+	if (ny < (int)WM_BAR_H)
+		ny = (int)WM_BAR_H;
+	else if (ny > maxy)
+		ny = maxy;
+	w->x = (unsigned int)nx;
+	w->y = (unsigned int)ny;
+}
+
+static void wm_handle_mouse(void)
+{
+	unsigned int buttons = mouse_buttons_state();
+	unsigned int mx = mouse_get_x();
+	unsigned int my = mouse_get_y();
+	int left_down = (buttons & 1) != 0;
+	int left_was_down = (prev_buttons & 1) != 0;
+
+	if (left_down && !left_was_down) {
+		/* Press. */
+		if (wm_point_in_rect(mx, my, WM_BTN_X, WM_BTN_Y, WM_BTN_W, WM_BTN_H)) {
+			wm_spawn_terminal();
+		} else {
+			int idx = wm_window_at(mx, my);
+			if (idx >= 0) {
+				struct window *w = &windows[idx];
+				wm_focus_index((unsigned int)idx);
+				/* Close button? */
+				if (wm_point_in_rect(mx, my,
+				    w->x + w->pw - 1 - WM_CLOSE_W, w->y + 1,
+				    WM_CLOSE_W, WM_TITLE_H)) {
+					wm_close_window((unsigned int)idx);
+				} else if (my < w->y + WM_TITLE_H) {
+					/* Title bar: start drag. */
+					drag_active = 1;
+					drag_window = idx;
+					drag_dx = (int)mx - (int)w->x;
+					drag_dy = (int)my - (int)w->y;
+				}
+			}
+		}
+	} else if (left_down && left_was_down && drag_active) {
+		struct window *w = &windows[drag_window];
+		if (w->used) {
+			wm_clamp_window(w, (int)mx - drag_dx, (int)my - drag_dy);
+			wm_request_redraw();
+		} else {
+			drag_active = 0;
+		}
+	} else if (!left_down && left_was_down) {
+		drag_active = 0;
+	}
+
+	prev_buttons = buttons;
+}
+
+static void wm_handle_keyboard(void)
+{
+	struct window *fw = wm_focused_window();
+	int n;
+	if (!fw || !fw->term)
+		return;
+	for (n = 0; n < 8; n++) {
+		int k = io_poll_key();
+		if (!k)
+			break;
+		term_key_push(fw->term, k);
+	}
+}
+
+static void wm_draw_bar(void)
+{
+	unsigned int fbw = vga_fb_width();
+	char dt[20];
+
+	/* Banner (light green). */
+	fbtext_draw_string_xy(4, 5, OS_NAME " v" OS_VERSION, 85, 255, 85, 0);
+
+	/* "+ New" button: purple box, white text. */
+	vga_fill_rect(WM_BTN_X, WM_BTN_Y, WM_BTN_W, WM_BTN_H, 150, 60, 210);
+	fbtext_draw_string_xy(WM_BTN_X + 4, WM_BTN_Y + 3, "+ New", 255, 255, 255, 1);
+
+	if (kexec_busy) {
+		/* Kexec progress, in place of the clock. */
+		static const char msg[] = "loading new kernel over serial";
+		char buf[48];
+		unsigned int n = 0, dots, i;
+		while (msg[n])
+			buf[n] = msg[n], n++;
+		dots = (kexec_total > 0) ? (kexec_progress * 12 / kexec_total) : 0;
+		for (i = 0; i < 12; i++)
+			buf[n + i] = (i < dots) ? '.' : ' ';
+		n += 12;
+		buf[n] = '\0';
+		fbtext_draw_string_xy(fbw - n * 8 - 4, 5, buf, 255, 200, 100, 0);
+	} else {
+		/* Live clock (light green, right-aligned). */
+		rtc_read_datetime(dt);
+		fbtext_draw_string_xy(fbw - 19 * 8 - 4, 5, dt, 85, 255, 85, 0);
+	}
+
+	/* Rainbow separator. */
+	fbtext_draw_rainbow_separator();
+}
+
+static void wm_draw_window(struct window *w)
+{
+	struct text_screen *s = &w->term->screen;
+	unsigned int cx = w->x + 1;
+	unsigned int cy = w->y + 1 + WM_TITLE_H;
+
+	/* Border. */
+	vga_fill_rect(w->x, w->y, w->pw, w->ph, 200, 200, 200);
+
+	/* Title bar. */
+	if (w->focused)
+		vga_fill_rect(cx, w->y + 1, w->pw - 2, WM_TITLE_H, 70, 90, 150);
+	else
+		vga_fill_rect(cx, w->y + 1, w->pw - 2, WM_TITLE_H, 100, 100, 100);
+
+	/* Title text. */
+	fbtext_draw_string_xy(w->x + 4, w->y + 3, w->title, 255, 255, 255, 1);
+
+	/* Close button. */
+	{
+		unsigned int bx = w->x + w->pw - 1 - WM_CLOSE_W;
+		vga_fill_rect(bx, w->y + 1, WM_CLOSE_W, WM_TITLE_H, 190, 60, 60);
+		fbtext_draw_string_xy(bx + 3, w->y + 3, "x", 255, 255, 255, 1);
+	}
+
+	/* Content (black background) + cell buffer. */
+	vga_fill_rect(cx, cy, w->pw - 2, SCREEN_ROWS * 10, 0, 0, 0);
+	fbtext_draw_cells(s->cells, s->cols, s->rows, cx, cy);
+
+	/* Blinking block cursor for the focused window. */
+	if (w->focused && s->cursor_visible) {
+		unsigned int col = s->cursor % s->cols;
+		unsigned int row = s->cursor / s->cols;
+		vga_fill_rect(cx + col * 8, cy + row * 10, 2, 10, 255, 255, 255);
+	}
+}
+
+static void wm_render_full(void)
+{
+	unsigned int fbw = vga_fb_width();
+	unsigned int fbh = vga_fb_height();
+	unsigned int i;
+
+	/* Erase the mouse cursor so the scene is drawn onto a clean background. */
+	mouse_cursor_erase();
+
+	/* Desktop background. */
+	vga_fill_rect(0, 0, fbw, fbh, 32, 32, 48);
+
+	/* Windows back-to-front (focused drawn last = on top). */
+	for (i = 0; i < MAX_WINDOWS; i++)
+		if (windows[i].used && !windows[i].focused)
+			wm_draw_window(&windows[i]);
+	for (i = 0; i < MAX_WINDOWS; i++)
+		if (windows[i].used && windows[i].focused)
+			wm_draw_window(&windows[i]);
+
+	/* System bar always on top. */
+	wm_draw_bar();
+
+	/* Mouse cursor on top of everything. */
+	mouse_redraw_cursor();
+}
+
+static void wm_render_bar(void)
+{
+	unsigned int fbw = vga_fb_width();
+	mouse_cursor_erase();
+	vga_fill_rect(0, 0, fbw, WM_BAR_H, 32, 32, 48);
+	wm_draw_bar();
+	mouse_redraw_cursor();
+}
+
+static void wm_redraw_if_needed(void)
+{
+	unsigned int i;
+	static unsigned int last_sec = 0xFFFFFFFF;
+	static unsigned int last_kexec_tick;
+	unsigned int now_ms = get_ticks_ms();
+	unsigned int now_sec = now_ms / 1000;
+	int clock_changed = (now_sec != last_sec);
+	int kexec_changed = kexec_busy && (now_ms - last_kexec_tick >= 100);
+	int need_full = wm_full_redraw;
+
+	if (kexec_changed)
+		last_kexec_tick = now_ms;
+
+	/* A background window's content changed -> full redraw (rare). */
+	for (i = 0; i < MAX_WINDOWS; i++)
+		if (windows[i].used && !windows[i].focused && windows[i].term->screen.dirty)
+			need_full = 1;
+
+	if (need_full) {
+		wm_render_full();
+		for (i = 0; i < MAX_WINDOWS; i++)
+			if (windows[i].used)
+				windows[i].term->screen.dirty = 0;
+		wm_full_redraw = 0;
+		last_sec = now_sec;
+		return;
+	}
+
+	if (clock_changed || kexec_changed) {
+		wm_render_bar();
+		last_sec = now_sec;
+	}
+
+	/* Focused window's content changed -> redraw only that window. */
+	for (i = 0; i < MAX_WINDOWS; i++) {
+		if (windows[i].used && windows[i].focused && windows[i].term->screen.dirty) {
+			mouse_cursor_erase();
+			wm_draw_window(&windows[i]);
+			mouse_redraw_cursor();
+			windows[i].term->screen.dirty = 0;
+		}
+	}
+}
+
+static void wm_init(void)
+{
+	unsigned int i;
+	for (i = 0; i < MAX_WINDOWS; i++) {
+		windows[i].used = 0;
+		windows[i].term = &terms[i];
+	}
+	term_counter = 0;
+	wm_full_redraw = 1;
+	drag_active = 0;
+	prev_buttons = 0;
+	wm_spawn_terminal();
+}
+
+static void wm_blink_cursor(void)
+{
+	static unsigned int last_cursor_tick;
+	struct window *fw = wm_focused_window();
+	unsigned int now = get_ticks_ms();
+
+	if (!fw || !fw->term)
+		return;
+	if (now - last_cursor_tick >= CURSOR_MS) {
+		last_cursor_tick = now;
+		fw->term->screen.cursor_visible = !fw->term->screen.cursor_visible;
+		fw->term->screen.dirty = 1;
+	}
+}
+
+static void wm_handle_kexec(void)
+{
+	if (!kexec_busy && kexec_pending()) {
+		kexec_busy = 1;
+		if (process_create("kexec", program_kexec_main) < 0)
+			kexec_busy = 0;
+	}
+}
+
+void wm_run(void)
+{
+	wm_init();
+	for (;;) {
+		mouse_poll_update();
+		wm_handle_mouse();
+		wm_handle_keyboard();
+		wm_blink_cursor();
+		wm_handle_kexec();
+		wm_redraw_if_needed();
+		vga_fb_flush_pending_check();
+		cpu_idle();
+	}
+}

+ 36 - 0
wm.h

@@ -0,0 +1,36 @@
+/* Window manager: fixed-size draggable terminal windows + a system bar. */
+#ifndef WM_H
+#define WM_H
+
+#include "terminal.h"
+
+#define MAX_WINDOWS 8
+
+/* System bar (banner + "+ New" button + clock + rainbow) at the top. */
+#define WM_BAR_H 25
+#define WM_BTN_X 132
+#define WM_BTN_Y 3
+#define WM_BTN_W 56
+#define WM_BTN_H 14
+
+/* Window chrome. */
+#define WM_TITLE_H 16
+#define WM_CLOSE_W 14
+
+struct window {
+	int used;
+	unsigned int x, y;       /* pixel origin (title bar top-left) */
+	unsigned int pw, ph;     /* pixel size */
+	char title[16];
+	int focused;
+	unsigned int pid;        /* owning terminal process pid (0 = none) */
+	struct term *term;
+};
+
+void wm_run(void);
+int  wm_spawn_terminal(void);
+struct window *wm_focused_window(void);
+int  wm_is_focused(const struct window *w);
+void wm_request_redraw(void);
+
+#endif