فهرست منبع

Add typematic repeat and text selection to ngui inputs

Johannes Zellner 2 هفته پیش
والد
کامیت
cd9c611e6c
7فایلهای تغییر یافته به همراه281 افزوده شده و 56 حذف شده
  1. 7 2
      AGENTS.md
  2. 3 1
      Makefile
  3. 8 4
      gallery.c
  4. 83 16
      io.c
  5. 3 0
      io.h
  6. 172 33
      ngui.c
  7. 5 0
      ngui.h

+ 7 - 2
AGENTS.md

@@ -254,7 +254,9 @@ run area), so adding code no longer moves any address.
   `gallery` (`gallery.c`) is the live showcase of every control and primitive
   the toolkit exports.  Buttons fire on the release edge and keep `active`
   until `ui->released`: inferring release from the sampled `ui->buttons`
-  races the WM event queue and drops clicks.
+  races the WM event queue and drops clicks.  Text fields support mouse
+  drag-selection, Ctrl+Left/Right word jumps, Ctrl+Delete word deletion and
+  Ctrl+A select-all; typing or Backspace/Delete replaces a selection.
 - **Graphics**: `vga.c/h`, `vga_copy.S`, `blit.c/h`, `gpu.c`, `ati.c`, `nvidia.c`,
   `nv50.c`, `pci.h` — VESA LFB + shadow buffer.  Drawing goes into a
   `struct surface` render target: `vga_target()` is the screen's shadow buffer
@@ -891,7 +893,10 @@ so addresses do not collide.
   (modifier byte + 6 keycodes); they are diffed against the previous report
   and decoded through a HID-usage → scancode-set-1 table (including 0xE0
   extended keys), then injected with `io_inject_scancode()` so the
-  terminal/WM see exactly what a PS/2 keyboard would produce.
+  terminal/WM see exactly what a PS/2 keyboard would produce.  Since a boot
+  keyboard only reports changes, `io.c` adds software typematic repeat
+  (500 ms delay, 33 ms rate) for the last make code; real PS/2 hardware
+  repeat keeps resetting the same timer.
   `usbhid_poll()` (from `mouse_poll_update()`) also runs the hotplug
   watcher: root-port connect changes are enumerated (throttled to 250 ms),
   `CCS` low or a few TD errors detach a device.  A full scan (`lsusb`) bumps

+ 3 - 1
Makefile

@@ -581,8 +581,10 @@ ucmd_term.elf: ucmd_term.o uwm_lib.o font_8x16.o ucmd.ld
 	$(OBJCOPY) --strip-all $@ $@
 
 # ngui: the userland immediate-mode GUI toolkit and its apps.
+# -Os: every GUI app links the whole toolkit, and the legacy rootfs pack has
+# a hard 255 KB cap; -Os saves ~10 KB compressed across the pack.
 ngui.o: ngui.c ngui.h ucmd_lib.h syscall.h wm_abi.h font_8x16.h
-	$(CC) $(CFLAGS) -o $@ $<
+	$(CC) $(CFLAGS) -Os -o $@ $<
 
 ucmd_files.o: ucmd_files.c ucmd_lib.h syscall.h wm_abi.h ngui.h img.h
 	$(CC) $(CFLAGS) -o $@ $<

+ 8 - 4
gallery.c

@@ -813,7 +813,7 @@ static void draw_inputs(struct ngui *g, struct ngui_rect card)
 
 	help_x = x + label_w + iw + 24;
 	help_w = card.x + card.w - CARD_PAD - help_x;
-	help_h = 164;
+	help_h = 182;
 
 	if (help_w >= 150) {
 		int hx = help_x;
@@ -831,11 +831,15 @@ static void draw_inputs(struct ngui *g, struct ngui_rect card)
 			  g->theme.text_dim);
 		ngui_text(g, hx + 10, hy + 72, "Arrows - move caret",
 			  g->theme.text_dim);
-		ngui_text(g, hx + 10, hy + 90, "Home / End - line ends",
+		ngui_text(g, hx + 10, hy + 90, "Ctrl+Left/Right - words",
 			  g->theme.text_dim);
-		ngui_text(g, hx + 10, hy + 108, "Ctrl+U - clear",
+		ngui_text(g, hx + 10, hy + 108, "Ctrl+Del - delete word",
 			  g->theme.text_dim);
-		ngui_text(g, hx + 10, hy + 126, "Tab - focus first",
+		ngui_text(g, hx + 10, hy + 126, "Drag - select, Ctrl+A all",
+			  g->theme.text_dim);
+		ngui_text(g, hx + 10, hy + 144, "Home/End, Ctrl+U - ends/clear",
+			  g->theme.text_dim);
+		ngui_text(g, hx + 10, hy + 162, "Tab - focus first",
 			  g->theme.text_dim);
 	}
 

+ 83 - 16
io.c

@@ -49,6 +49,19 @@ static int shift_pressed;
 static int ctrl_pressed;
 static int kbd_extended;  /* 1 after 0xE0, next byte is extended scancode */
 
+/* Software typematic repeat.  PS/2 keyboards repeat in hardware; the USB HID
+ * backend only reports changes, so a held key would otherwise produce exactly
+ * one event.  Track the last make code here (both backends end up in this
+ * decoder) and re-deliver it after the usual delay/rate. */
+#define KBD_REPEAT_DELAY_MS 500
+#define KBD_REPEAT_RATE_MS  33
+
+static int repeat_key;
+static int repeat_active;
+static unsigned int repeat_ext;
+static unsigned int repeat_sc;
+static unsigned int repeat_at;
+
 #define KBD_BUF_SIZE 16
 static volatile unsigned char kbd_buf[KBD_BUF_SIZE];
 static volatile unsigned int kbd_head, kbd_tail;
@@ -289,33 +302,82 @@ void io_drain_keyboard(void)
 		(void)inb(KB_DATA);
 	kbd_head = kbd_tail = 0;
 	kbd_extended = 0;
+	repeat_active = 0;
+}
+
+static void kbd_repeat_start(int key, unsigned int ext, unsigned int sc)
+{
+	repeat_key = key;
+	repeat_ext = ext;
+	repeat_sc = sc;
+	repeat_at = get_ticks_ms() + KBD_REPEAT_DELAY_MS;
+	repeat_active = 1;
+}
+
+static void kbd_repeat_stop(unsigned int ext, unsigned int sc)
+{
+	if (repeat_active && repeat_ext == ext && repeat_sc == sc)
+		repeat_active = 0;
+}
+
+/* Return a synthetic repeat event when the held key is due.  Called only when
+ * the real scancode queue is empty, so a pending break code is decoded first
+ * and cancels the repeat before it can leak through. */
+static int kbd_repeat_poll(void)
+{
+	unsigned int now;
+
+	if (!repeat_active)
+		return 0;
+	now = get_ticks_ms();
+	if (now - repeat_at > 0x7FFFFFFFu)   /* signed deadline comparison */
+		return 0;
+	repeat_at = now + KBD_REPEAT_RATE_MS;
+	return repeat_key;
 }
 
 /* Process one scancode byte; shared by io_poll_key and io_poll_key_with_hw. */
 static int kbd_process_byte(unsigned char sc)
 {
+	int key;
+
 	if (sc == 0xE0) {
 		kbd_extended = 1;
 		return 0;
 	}
 	if (kbd_extended) {
 		kbd_extended = 0;
-		if (sc & 0x80)
+		if (sc & 0x80) {
+			kbd_repeat_stop(1, sc & 0x7F);
 			return 0;  /* key release */
-		if (sc == 0x4B) return IO_KEY_LEFT;
-		if (sc == 0x4D) return IO_KEY_RIGHT;
-		if (sc == 0x48) return IO_KEY_UP;
-		if (sc == 0x50) return IO_KEY_DOWN;
-		if (sc == 0x49) return IO_KEY_PAGE_UP;
-		if (sc == 0x51) return IO_KEY_PAGE_DOWN;
-		if (sc == 0x47) return IO_KEY_HOME;
-		if (sc == 0x4F) return IO_KEY_END;
-		if (sc == 0x53) return IO_KEY_DELETE;
-		return 0;
+		}
+		if (sc == 0x4B)
+			key = ctrl_pressed ? IO_KEY_CTRL_LEFT : IO_KEY_LEFT;
+		else if (sc == 0x4D)
+			key = ctrl_pressed ? IO_KEY_CTRL_RIGHT : IO_KEY_RIGHT;
+		else if (sc == 0x48)
+			key = IO_KEY_UP;
+		else if (sc == 0x50)
+			key = IO_KEY_DOWN;
+		else if (sc == 0x49)
+			key = IO_KEY_PAGE_UP;
+		else if (sc == 0x51)
+			key = IO_KEY_PAGE_DOWN;
+		else if (sc == 0x47)
+			key = IO_KEY_HOME;
+		else if (sc == 0x4F)
+			key = IO_KEY_END;
+		else if (sc == 0x53)
+			key = ctrl_pressed ? IO_KEY_CTRL_DELETE : IO_KEY_DELETE;
+		else
+			return 0;
+		kbd_repeat_start(key, 1, sc);
+		return key;
 	}
 	if (sc & 0x80) {
 		if (sc == 0xAA || sc == 0xB6) shift_pressed = 0;
 		if (sc == 0x9D) ctrl_pressed = 0;
+		kbd_repeat_stop(0, sc & 0x7F);
 		return 0;
 	}
 	if (sc == 0x2A || sc == 0x36) {
@@ -333,10 +395,14 @@ static int kbd_process_byte(unsigned char sc)
 			if (ctrl_pressed) {
 				/* Ctrl+letter -> control character (e.g. Ctrl+C -> 0x03). */
 				if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
-					return (unsigned char)(c & 0x1F);
-				return 0;
+					key = (unsigned char)(c & 0x1F);
+				else
+					return 0;
+			} else {
+				key = (unsigned char)c;
 			}
-			return (unsigned char)c;
+			kbd_repeat_start(key, 0, sc);
+			return key;
 		}
 	}
 	return 0;
@@ -345,8 +411,9 @@ static int kbd_process_byte(unsigned char sc)
 int io_poll_key(void)
 {
 	unsigned char sc;
+
 	if (!kbd_buf_get(&sc))
-		return 0;
+		return kbd_repeat_poll();
 	return kbd_process_byte(sc);
 }
 
@@ -377,7 +444,7 @@ int io_poll_key_with_hw(void)
 	__asm__ volatile ("pushl %0; popfl" : : "r"(flags));
 
 	if (!got)
-		return 0;
+		return kbd_repeat_poll();
 	return kbd_process_byte(sc);
 }
 

+ 3 - 0
io.h

@@ -39,6 +39,9 @@ void io_drain_keyboard(void);
 #define IO_KEY_HOME      0x106
 #define IO_KEY_END       0x107
 #define IO_KEY_DELETE    0x108
+#define IO_KEY_CTRL_LEFT  0x109
+#define IO_KEY_CTRL_RIGHT 0x10A
+#define IO_KEY_CTRL_DELETE 0x10B
 int io_poll_key(void);
 /* Same as io_poll_key but also reads port 0x60 when buffer empty (for full-screen programs). */
 int io_poll_key_with_hw(void);

+ 172 - 33
ngui.c

@@ -758,12 +758,87 @@ void ngui_input_set(struct ngui_input *in, const char *s)
 	in->len = slen(in->text);
 	in->cursor = in->len;
 	in->scroll = 0;
+	in->anchor = in->cursor;
+	in->dragging = 0;
+}
+
+static int input_word_char(char c)
+{
+	return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
+	       (c >= '0' && c <= '9') || c == '_';
+}
+
+/* Next word start after `pos` (Windows edit-control semantics). */
+static unsigned int input_word_next(const struct ngui_input *in,
+				    unsigned int pos)
+{
+	while (pos < in->len && input_word_char(in->text[pos]))
+		pos++;
+	while (pos < in->len && !input_word_char(in->text[pos]))
+		pos++;
+	return pos;
+}
+
+/* Start of the previous word before `pos`. */
+static unsigned int input_word_prev(const struct ngui_input *in,
+				    unsigned int pos)
+{
+	while (pos > 0 && !input_word_char(in->text[pos - 1]))
+		pos--;
+	while (pos > 0 && input_word_char(in->text[pos - 1]))
+		pos--;
+	return pos;
+}
+
+static void input_sel_bounds(const struct ngui_input *in,
+			     unsigned int *a, unsigned int *b)
+{
+	if (in->anchor < in->cursor) {
+		*a = in->anchor;
+		*b = in->cursor;
+	} else {
+		*a = in->cursor;
+		*b = in->anchor;
+	}
+}
+
+/* Remove [a, b) and keep cursor/anchor consistent.  Returns 1 when the text
+ * changed. */
+static int input_delete_range(struct ngui_input *in, unsigned int a,
+			      unsigned int b)
+{
+	unsigned int i;
+
+	if (a >= b || b > in->len)
+		return 0;
+	for (i = b; i < in->len; i++)
+		in->text[i - (b - a)] = in->text[i];
+	in->len -= b - a;
+	in->text[in->len] = '\0';
+	if (in->cursor >= b)
+		in->cursor -= b - a;
+	else if (in->cursor > a)
+		in->cursor = a;
+	if (in->anchor >= b)
+		in->anchor -= b - a;
+	else if (in->anchor > a)
+		in->anchor = a;
+	return 1;
+}
+
+static int input_delete_selection(struct ngui_input *in)
+{
+	unsigned int a, b;
+
+	input_sel_bounds(in, &a, &b);
+	return input_delete_range(in, a, b);
 }
 
 static void input_insert(struct ngui_input *in, char c)
 {
 	unsigned int i;
 
+	input_delete_selection(in);
 	if (in->len + 1 >= NGUI_INPUT_MAX)
 		return;
 	i = in->len;
@@ -775,11 +850,12 @@ static void input_insert(struct ngui_input *in, char c)
 	in->cursor++;
 	in->len++;
 	in->text[in->len] = '\0';
+	in->anchor = in->cursor;
 }
 
 static void input_delete(struct ngui_input *in, int back)
 {
-	unsigned int i, pos;
+	unsigned int pos;
 
 	if (back) {
 		if (in->cursor == 0)
@@ -790,15 +866,50 @@ static void input_delete(struct ngui_input *in, int back)
 			return;
 		pos = in->cursor;
 	}
-	i = pos;
-	while (i + 1 < in->len) {
-		in->text[i] = in->text[i + 1];
-		i++;
-	}
-	in->len--;
-	if (back)
-		in->cursor--;
-	in->text[in->len] = '\0';
+	input_delete_range(in, pos, pos + 1);
+}
+
+static void input_move_to(struct ngui_input *in, unsigned int pos)
+{
+	if (pos > in->len)
+		pos = in->len;
+	in->cursor = pos;
+	in->anchor = pos;
+}
+
+/* Character position under a content-relative x, clamped to the visible
+ * part of the field. */
+static unsigned int input_pos_at(const struct ngui_input *in, int inner_x,
+				 int cols, int x)
+{
+	int col = (x - inner_x) / 8;
+	unsigned int pos;
+
+	if (col < 0)
+		col = 0;
+	if (cols > 0 && col > cols)
+		col = cols;
+	pos = in->scroll + (unsigned int)col;
+	if (pos > in->len)
+		pos = in->len;
+	return pos;
+}
+
+static void input_draw_selection(struct ngui *ui, const struct ngui_input *in,
+				 struct ngui_rect inner, int cols)
+{
+	unsigned int a, b, va, vb;
+
+	input_sel_bounds(in, &a, &b);
+	if (a == b || cols <= 0)
+		return;
+	va = a < in->scroll ? in->scroll : a;
+	vb = b > in->scroll + (unsigned int)cols
+	     ? in->scroll + (unsigned int)cols : b;
+	if (va < vb)
+		ngui_fill_rect(ui, rect_make(inner.x + (int)(va - in->scroll) * 8,
+					     inner.y, (int)(vb - va) * 8, 16),
+			       ui->theme.selection);
 }
 
 int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
@@ -810,23 +921,40 @@ int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
 	int cols, k;
 	int changed = 0;
 
+	inner = rect_make(r.x + 4, r.y + (r.h - 16) / 2, r.w - 8, 16);
+	if (inner.w < 0)
+		inner.w = 0;
+	cols = inner.w / 8;
+	if (cols < 0)
+		cols = 0;
+
 	if (ui->pressed &&
 	    ngui_rect_contains(&r, ui->press_x, ui->press_y)) {
-		int col = (ui->press_x - (r.x + 4)) / 8;
-		unsigned int pos;
-
-		if (col < 0)
-			col = 0;
-		pos = in->scroll + (unsigned int)col;
-		if (pos > in->len)
-			pos = in->len;
+		unsigned int pos = input_pos_at(in, inner.x, cols, ui->press_x);
+
 		in->cursor = pos;
+		in->anchor = pos;
+		in->dragging = 1;
 		ui->focus = id;
 		ui->focus_claimed = 1;
 		ui->last_blink = u_ticks_ms();
 		ui->caret_on = 1;
 		focused = 1;
+	} else if (ui->pressed) {
+		in->dragging = 0;
+	}
+
+	/* Extend the selection while the button that started in this field is
+	 * still held.  Sampled coordinates may be outside the canvas.  The
+	 * release *event* is authoritative: the sampled button state can lag
+	 * the event queue by a frame. */
+	if (in->dragging && (ui->buttons & 1) && !ui->released) {
+		in->cursor = input_pos_at(in, inner.x, cols, ui->mouse_x);
+		/* Keep redrawing when the pointer leaves the window. */
+		ngui_invalidate(ui);
 	}
+	if (ui->released)
+		in->dragging = 0;
 
 	if (focused) {
 		while ((k = ngui_key(ui)) != 0) {
@@ -840,24 +968,38 @@ int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
 				break;
 			}
 			if (k == NGUI_KEY_BACKSPACE || k == 0x7F) {
-				input_delete(in, 1);
+				if (!input_delete_selection(in))
+					input_delete(in, 1);
 				changed = 1;
 			} else if (k == NGUI_KEY_DELETE) {
-				input_delete(in, 0);
+				if (!input_delete_selection(in))
+					input_delete(in, 0);
 				changed = 1;
+			} else if (k == NGUI_KEY_CTRL_DELETE) {
+				if (input_delete_selection(in) ||
+				    input_delete_range(in, in->cursor,
+						input_word_next(in, in->cursor)))
+					changed = 1;
 			} else if (k == NGUI_KEY_LEFT) {
-				if (in->cursor > 0)
-					in->cursor--;
+				input_move_to(in, in->cursor > 0 ? in->cursor - 1 : 0);
 			} else if (k == NGUI_KEY_RIGHT) {
-				if (in->cursor < in->len)
-					in->cursor++;
+				input_move_to(in, in->cursor < in->len
+						  ? in->cursor + 1 : in->len);
+			} else if (k == NGUI_KEY_CTRL_LEFT) {
+				input_move_to(in, input_word_prev(in, in->cursor));
+			} else if (k == NGUI_KEY_CTRL_RIGHT) {
+				input_move_to(in, input_word_next(in, in->cursor));
 			} else if (k == NGUI_KEY_HOME) {
-				in->cursor = 0;
+				input_move_to(in, 0);
 			} else if (k == NGUI_KEY_END) {
+				input_move_to(in, in->len);
+			} else if (k == 0x01) {   /* Ctrl+A: select all */
+				in->anchor = 0;
 				in->cursor = in->len;
 			} else if (k == 0x15) {   /* Ctrl+U */
 				in->len = 0;
 				in->cursor = 0;
+				in->anchor = 0;
 				in->text[0] = '\0';
 				changed = 1;
 			} else if (k >= 0x20 && k <= 0x7E) {
@@ -867,15 +1009,9 @@ int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
 		}
 	}
 
-	inner = rect_make(r.x + 4, r.y + (r.h - 16) / 2, r.w - 8, 16);
-	if (inner.w < 0)
-		inner.w = 0;
 	ngui_fill_rect(ui, r, focused ? ui->theme.panel_alt : ui->theme.panel);
 	ngui_frame(ui, r, focused ? ui->theme.accent : ui->theme.border);
 
-	cols = inner.w / 8;
-	if (cols < 0)
-		cols = 0;
 	if (in->scroll > in->len)
 		in->scroll = in->len;
 	if (in->cursor < in->scroll)
@@ -886,12 +1022,15 @@ int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
 		in->scroll = in->len;
 
 	ngui_push_clip(ui, inner);
-	if (in->len == 0 && in->placeholder)
+	if (in->len == 0 && in->placeholder) {
 		ngui_text(ui, inner.x, inner.y, in->placeholder,
 			  ui->theme.text_dim);
-	else
+	} else {
+		if (focused)
+			input_draw_selection(ui, in, inner, cols);
 		ngui_text(ui, inner.x, inner.y, in->text + in->scroll,
 			  ui->theme.text);
+	}
 
 	if (focused && ui->caret_on) {
 		int col = (int)in->cursor - (int)in->scroll;

+ 5 - 0
ngui.h

@@ -29,6 +29,9 @@
 #define NGUI_KEY_HOME       0x106
 #define NGUI_KEY_END        0x107
 #define NGUI_KEY_DELETE     0x108
+#define NGUI_KEY_CTRL_LEFT  0x109
+#define NGUI_KEY_CTRL_RIGHT 0x10A
+#define NGUI_KEY_CTRL_DELETE 0x10B
 
 #define NGUI_RGB(r, g, b) \
 	((unsigned short)((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3)))
@@ -74,6 +77,8 @@ struct ngui_input {
 	unsigned int len;
 	unsigned int cursor;
 	unsigned int scroll;          /* first visible character */
+	unsigned int anchor;          /* mouse selection anchor (== cursor: none) */
+	int dragging;                 /* a press in the field is extending a selection */
 	const char *placeholder;
 };