Skip to content

Commit 508a7c7

Browse files
committed
Handle TextInput event
1 parent 4fa504b commit 508a7c7

File tree

6 files changed

+26
-6
lines changed

6 files changed

+26
-6
lines changed

api/syscalls.json

+5
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,11 @@
415415
"u16",
416416
5
417417
],
418+
[
419+
"EVENT_TEXTINPUT",
420+
"u16",
421+
6
422+
],
418423
[
419424
"KEY_BACKSPACE",
420425
"u16",

doc/syscalls.md

+1
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ These are the constants associated with the window subsystem:
225225
- `u16 EVENT_MOUSEDOWN = 3`
226226
- `u16 EVENT_MOUSEUP = 4`
227227
- `u16 EVENT_MOUSEMOVE = 5`
228+
- `u16 EVENT_TEXTINPUT = 6`
228229
- `u16 KEY_BACKSPACE = 8`
229230
- `u16 KEY_TAB = 9`
230231
- `u16 KEY_RETURN = 10`

ncc/include/uvm/syscalls.h

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
#define EVENT_MOUSEDOWN 3
120120
#define EVENT_MOUSEUP 4
121121
#define EVENT_MOUSEMOVE 5
122+
#define EVENT_TEXTINPUT 6
122123
#define KEY_BACKSPACE 8
123124
#define KEY_TAB 9
124125
#define KEY_RETURN 10

ncc/include/uvm/window.h

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ typedef struct
1212
u16 button;
1313
i32 x;
1414
i32 y;
15+
char text[64];
1516
} Event;
1617

1718
// Stack allocation of structs not yet supported

vm/src/constants.rs

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub const EVENT_KEYUP: u16 = 2;
8383
pub const EVENT_MOUSEDOWN: u16 = 3;
8484
pub const EVENT_MOUSEUP: u16 = 4;
8585
pub const EVENT_MOUSEMOVE: u16 = 5;
86+
pub const EVENT_TEXTINPUT: u16 = 6;
8687
pub const KEY_BACKSPACE: u16 = 8;
8788
pub const KEY_TAB: u16 = 9;
8889
pub const KEY_RETURN: u16 = 10;

vm/src/window.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ pub fn window_draw_frame(thread: &mut Thread, window_id: Value, src_addr: Value)
156156
window.canvas.present();
157157
}
158158

159+
const EVENT_TEXT_MAX_BYTES: usize = 64;
160+
159161
// C event struct
160162
#[repr(C)]
161163
struct CEvent
@@ -166,6 +168,7 @@ struct CEvent
166168
button: u16,
167169
x: i32,
168170
y: i32,
171+
text: [u8; EVENT_TEXT_MAX_BYTES],
169172
}
170173

171174
/// 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
281284
true
282285
}
283286

284-
/*
285287
Event::TextInput { window_id, text, .. } => {
288+
c_event.kind = EVENT_TEXTINPUT;
289+
c_event.window_id = 0;
290+
291+
let text_bytes = text.bytes();
292+
293+
// This should never happen
294+
if text_bytes.len() > EVENT_TEXT_MAX_BYTES {
295+
panic!();
296+
}
297+
286298
// For each UTF-8 byte of input
287-
for ch in text.bytes() {
288-
if let ExitReason::Exit(val) = window_call_textinput(vm, window_id, ch) {
289-
return ExitReason::Exit(val);
290-
}
299+
for (i, ch) in text_bytes.enumerate() {
300+
c_event.text[i] = ch;
291301
}
302+
303+
true
292304
}
293-
*/
294305

295306
_ => false
296307
}

0 commit comments

Comments
 (0)