Skip to content

Commit

Permalink
Handle TextInput event
Browse files Browse the repository at this point in the history
  • Loading branch information
maximecb committed Sep 3, 2024
1 parent 4fa504b commit 508a7c7
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 6 deletions.
5 changes: 5 additions & 0 deletions api/syscalls.json
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,11 @@
"u16",
5
],
[
"EVENT_TEXTINPUT",
"u16",
6
],
[
"KEY_BACKSPACE",
"u16",
Expand Down
1 change: 1 addition & 0 deletions doc/syscalls.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
1 change: 1 addition & 0 deletions ncc/include/uvm/syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions ncc/include/uvm/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ typedef struct
u16 button;
i32 x;
i32 y;
char text[64];
} Event;

// Stack allocation of structs not yet supported
Expand Down
1 change: 1 addition & 0 deletions vm/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 17 additions & 6 deletions vm/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 508a7c7

Please sign in to comment.