Skip to content

Commit

Permalink
Fix examples/textedit.c
Browse files Browse the repository at this point in the history
maximecb committed Sep 3, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 508a7c7 commit 4015993
Showing 2 changed files with 32 additions and 13 deletions.
42 changes: 30 additions & 12 deletions ncc/examples/textedit.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <uvm/syscalls.h>
#include <uvm/window.h>
#include <uvm/utils.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>

@@ -30,7 +32,7 @@ size_t row_len(size_t row_idx)
return NUM_COLS;
}

void textinput(u64 window_id, char ch)
void textinput(char ch)
{
//print_i64(ch);
//print_endl();
@@ -50,7 +52,7 @@ void textinput(u64 window_id, char ch)
redraw();
}

void keydown(u64 window_id, u16 keycode)
void keydown(u16 keycode)
{
if (keycode == KEY_ESCAPE)
{
@@ -131,25 +133,41 @@ void redraw()
window_draw_frame(0, frame_buffer);
}

void anim_callback()
{
benchmark(redraw());

time_delay_cb(400, anim_callback);
}
Event event;

void main()
{
window_create(800, 600, "Text Editor Demo", 0);

redraw();

window_on_keydown(0, keydown);
window_on_textinput(0, textinput);
for (;;)
{
while (window_poll_event(&event))
{
if (event.kind == EVENT_QUIT)
{
exit(0);
}

if (event.kind == EVENT_KEYDOWN)
{
keydown(event.key);
}

time_delay_cb(0, anim_callback);
if (event.kind == EVENT_TEXTINPUT)
{
size_t len = strlen(event.text);
for (size_t i = 0; i < len; ++i)
{
textinput(event.text[i]);
}
}
}

enable_event_loop();
benchmark(redraw());
thread_sleep(20);
}
}

//===========================================================================
3 changes: 2 additions & 1 deletion vm/src/window.rs
Original file line number Diff line number Diff line change
@@ -287,11 +287,12 @@ fn translate_event(sdl_event: Event, c_event: &mut CEvent) -> bool
Event::TextInput { window_id, text, .. } => {
c_event.kind = EVENT_TEXTINPUT;
c_event.window_id = 0;
c_event.text.fill(0);

let text_bytes = text.bytes();

// This should never happen
if text_bytes.len() > EVENT_TEXT_MAX_BYTES {
if text_bytes.len() > EVENT_TEXT_MAX_BYTES - 1 {
panic!();
}

0 comments on commit 4015993

Please sign in to comment.