From 508a7c7e60c04311d3bebc0628399fa8f6228619 Mon Sep 17 00:00:00 2001 From: Maxime Chevalier-Boisvert Date: Tue, 3 Sep 2024 13:36:36 -0400 Subject: [PATCH] Handle TextInput event --- api/syscalls.json | 5 +++++ doc/syscalls.md | 1 + ncc/include/uvm/syscalls.h | 1 + ncc/include/uvm/window.h | 1 + vm/src/constants.rs | 1 + vm/src/window.rs | 23 +++++++++++++++++------ 6 files changed, 26 insertions(+), 6 deletions(-) diff --git a/api/syscalls.json b/api/syscalls.json index 779cf58..4a44cca 100644 --- a/api/syscalls.json +++ b/api/syscalls.json @@ -415,6 +415,11 @@ "u16", 5 ], + [ + "EVENT_TEXTINPUT", + "u16", + 6 + ], [ "KEY_BACKSPACE", "u16", diff --git a/doc/syscalls.md b/doc/syscalls.md index 0afb715..3c3f783 100644 --- a/doc/syscalls.md +++ b/doc/syscalls.md @@ -225,6 +225,7 @@ These are the constants associated with the window subsystem: - `u16 EVENT_MOUSEDOWN = 3` - `u16 EVENT_MOUSEUP = 4` - `u16 EVENT_MOUSEMOVE = 5` +- `u16 EVENT_TEXTINPUT = 6` - `u16 KEY_BACKSPACE = 8` - `u16 KEY_TAB = 9` - `u16 KEY_RETURN = 10` diff --git a/ncc/include/uvm/syscalls.h b/ncc/include/uvm/syscalls.h index aa624c3..5859b71 100644 --- a/ncc/include/uvm/syscalls.h +++ b/ncc/include/uvm/syscalls.h @@ -119,6 +119,7 @@ #define EVENT_MOUSEDOWN 3 #define EVENT_MOUSEUP 4 #define EVENT_MOUSEMOVE 5 +#define EVENT_TEXTINPUT 6 #define KEY_BACKSPACE 8 #define KEY_TAB 9 #define KEY_RETURN 10 diff --git a/ncc/include/uvm/window.h b/ncc/include/uvm/window.h index 0dc907a..f5df4fb 100644 --- a/ncc/include/uvm/window.h +++ b/ncc/include/uvm/window.h @@ -12,6 +12,7 @@ typedef struct u16 button; i32 x; i32 y; + char text[64]; } Event; // Stack allocation of structs not yet supported diff --git a/vm/src/constants.rs b/vm/src/constants.rs index 2fe3058..8e0e914 100644 --- a/vm/src/constants.rs +++ b/vm/src/constants.rs @@ -83,6 +83,7 @@ pub const EVENT_KEYUP: u16 = 2; pub const EVENT_MOUSEDOWN: u16 = 3; pub const EVENT_MOUSEUP: u16 = 4; pub const EVENT_MOUSEMOVE: u16 = 5; +pub const EVENT_TEXTINPUT: u16 = 6; pub const KEY_BACKSPACE: u16 = 8; pub const KEY_TAB: u16 = 9; pub const KEY_RETURN: u16 = 10; diff --git a/vm/src/window.rs b/vm/src/window.rs index 9514df5..964f2dd 100644 --- a/vm/src/window.rs +++ b/vm/src/window.rs @@ -156,6 +156,8 @@ pub fn window_draw_frame(thread: &mut Thread, window_id: Value, src_addr: Value) window.canvas.present(); } +const EVENT_TEXT_MAX_BYTES: usize = 64; + // C event struct #[repr(C)] struct CEvent @@ -166,6 +168,7 @@ struct CEvent button: u16, x: i32, y: i32, + text: [u8; EVENT_TEXT_MAX_BYTES], } /// Takes a pointer ot an event struct to write into @@ -281,16 +284,24 @@ fn translate_event(sdl_event: Event, c_event: &mut CEvent) -> bool true } - /* Event::TextInput { window_id, text, .. } => { + c_event.kind = EVENT_TEXTINPUT; + c_event.window_id = 0; + + let text_bytes = text.bytes(); + + // This should never happen + if text_bytes.len() > EVENT_TEXT_MAX_BYTES { + panic!(); + } + // For each UTF-8 byte of input - for ch in text.bytes() { - if let ExitReason::Exit(val) = window_call_textinput(vm, window_id, ch) { - return ExitReason::Exit(val); - } + for (i, ch) in text_bytes.enumerate() { + c_event.text[i] = ch; } + + true } - */ _ => false }