Просмотр исходного кода

Implement moving cursor in terminal

Johannes Zellner 8 месяцев назад
Родитель
Сommit
82b2794823
3 измененных файлов с 80 добавлено и 17 удалено
  1. 25 2
      io.c
  2. 5 2
      io.h
  3. 50 13
      terminal.c

+ 25 - 2
io.c

@@ -46,6 +46,7 @@ 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];
@@ -159,6 +160,15 @@ unsigned int io_get_cursor(void)
 	return cursor;
 }
 
+void io_set_cursor(unsigned int pos)
+{
+	if (pos >= IO_VGA_ROWS * IO_VGA_COLS)
+		return;
+	hide_cursor_at(cursor);
+	cursor = pos;
+	cursor_cell = VGA_BASE[cursor];
+}
+
 #define CURSOR_BLOCK ((unsigned short)((VGA_ATTR << 8) | 0xDB))
 
 void io_cursor_show_block(void)
@@ -181,13 +191,26 @@ void io_drain_keyboard(void)
 	while (inb(KB_STATUS) & 1)
 		(void)inb(KB_DATA);
 	kbd_head = kbd_tail = 0;
+	kbd_extended = 0;
 }
 
-char io_poll_key(void)
+int io_poll_key(void)
 {
 	unsigned char sc;
 	if (!kbd_buf_get(&sc))
 		return 0;
+	if (sc == 0xE0) {
+		kbd_extended = 1;
+		return 0;
+	}
+	if (kbd_extended) {
+		kbd_extended = 0;
+		if (sc & 0x80)
+			return 0;  /* key release */
+		if (sc == 0x4B) return IO_KEY_LEFT;
+		if (sc == 0x4D) return IO_KEY_RIGHT;
+		return 0;
+	}
 	if (sc & 0x80) {
 		if (sc == 0xAA || sc == 0xB6) shift_pressed = 0;
 		return 0;
@@ -200,7 +223,7 @@ char io_poll_key(void)
 		char c = (char)scancode_to_ascii[sc];
 		if (c) {
 			last_scancode = sc;
-			return apply_shift(c);
+			return (unsigned char)apply_shift(c);
 		}
 	}
 	return 0;

+ 5 - 2
io.h

@@ -18,13 +18,16 @@ int io_did_scroll(void);  /* returns 1 once after a scroll, then 0 until next sc
 void io_putchar(char c);
 void io_putstr(const char *s);
 unsigned int io_get_cursor(void);
+void io_set_cursor(unsigned int pos);
 void io_cursor_show_block(void);
 void io_cursor_hide_block(void);
 void io_vga_write(unsigned int pos, unsigned short value);
 void io_drain_keyboard(void);
 
-/* Poll keyboard; returns 0 or ASCII character. Shift applied. */
-char io_poll_key(void);
+/* Poll keyboard; returns 0 = no key, 1..255 = ASCII, or IO_KEY_LEFT/IO_KEY_RIGHT. */
+#define IO_KEY_LEFT  0x100
+#define IO_KEY_RIGHT 0x101
+int io_poll_key(void);
 unsigned char io_last_scancode(void);
 
 #endif

+ 50 - 13
terminal.c

@@ -157,37 +157,67 @@ static int is_cmd_drive(const char *line, unsigned int line_len, unsigned int *d
 	return (i == line_len);
 }
 
+/* Redraw line from index `from` to end, clear one cell after, set cursor to line_pos. */
+static void redraw_line_from(const char *line, unsigned int line_len,
+	unsigned int line_pos, unsigned int line_start, unsigned int from)
+{
+	unsigned int i;
+	io_set_cursor(line_start + from);
+	for (i = from; i < line_len; i++)
+		io_putchar(line[i]);
+	io_putchar(' ');
+	io_set_cursor(line_start + line_pos);
+}
+
 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 */
 
 	last_cursor_tick   = get_ticks_ms();
 	last_datetime_tick = get_ticks_ms();
 	cursor_visible     = 1;
 	line_len           = 0;
+	line_pos           = 0;
 	line[0]            = '\0';
 
 	io_drain_keyboard();
 	draw_header_row();
 	io_putstr(prompt_first);
+	line_start = io_get_cursor();
 
 	for (;;) {
 		update_datetime();
 		update_cursor_blink();
 		{
-			char c = io_poll_key();
-			if (c) {
-				if (c == '\b') {
-					if (line_len > 0) {
+			int k = io_poll_key();
+			if (k) {
+				if (k == IO_KEY_LEFT) {
+					if (line_pos > 0) {
+						line_pos--;
+						io_set_cursor(line_start + line_pos);
+					}
+				} else if (k == IO_KEY_RIGHT) {
+					if (line_pos < line_len) {
+						line_pos++;
+						io_set_cursor(line_start + line_pos);
+					}
+				} else if (k == '\b') {
+					if (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';
-						io_putchar(c);
+						redraw_line_from(line, line_len, line_pos, line_start, line_pos);
 					}
-				} else if (c == '\n') {
+				} else if (k == '\n') {
 					static char cat_fname[CAT_FNAME_MAX];
 					unsigned int drive_sel;
-					io_putchar(c);
+					io_putchar('\n');
 					if (is_cmd_hello(line, line_len)) {
 						program_hello_main();
 						io_drain_keyboard();
@@ -208,20 +238,27 @@ void terminal_run(void)
 						io_putchar((char)('0' + disk_get_drive()));
 						io_putstr(" selected\n");
 					}
-					line_len = 0;
-					line[0]  = '\0';
+					line_len  = 0;
+					line_pos  = 0;
+					line[0]   = '\0';
 					io_putstr(prompt);
+					line_start = io_get_cursor();
 					if (io_did_scroll())
 						redraw_datetime();
 				} else {
 					{
-						unsigned char u = (unsigned char)c;
+						unsigned char u = (unsigned char)k;
 						if (u >= 0x20 && u < 0x7F && line_len < LINE_MAX - 1) {
-							line[line_len++] = c;
-							line[line_len]   = '\0';
+							unsigned int i;
+							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);
 						}
 					}
-					io_putchar(c);
 				}
 			} else {
 				__asm__ volatile ("hlt");