vtgl

terminal emulator implemented in OpenGL
git clone anongit@rnpnr.xyz:vtgl.git
Log | Files | Refs | Feed | LICENSE

Commit: fb2a386b925dd7b30837831039ae6742e8e7c88a
Parent: 0aadfd7588980817e18cd43d564ad6e2b43074d4
Author: Randy Palamar
Date:   Thu,  7 Nov 2024 20:36:18 -0700

move character input handling into the platform layer

Diffstat:
Mplatform_linux_x11.c | 32+++++++++++++++++++++++---------
Mvtgl.c | 23++++++++---------------
Mvtgl.h | 6++++++
3 files changed, 37 insertions(+), 24 deletions(-)

diff --git a/platform_linux_x11.c b/platform_linux_x11.c @@ -109,6 +109,14 @@ glfw_error_callback(int code, const char *desc) os_write_err_msg(s8("\n")); } +static void +char_callback(GLFWwindow *win, u32 codepoint) +{ + tmp_user_ctx *ctx = glfwGetWindowUserPointer(win); + TerminalInput *input = ctx->input; + stream_push_s8(&input->char_stream, utf8_encode(codepoint)); +} + /* NOTE: called when the window was resized */ static void fb_callback(GLFWwindow *win, i32 w, i32 h) @@ -180,9 +188,10 @@ init_window(tmp_user_ctx *ctx, iv2 window_size) /* TODO: swap interval is not needed because we will sleep on waiting for terminal input */ glfwSwapInterval(1); + glfwSetCharCallback(window, char_callback); glfwSetFramebufferSizeCallback(window, fb_callback); - glfwSetScrollCallback(window, scroll_callback); glfwSetMouseButtonCallback(window, mouse_button_callback); + glfwSetScrollCallback(window, scroll_callback); return window; } @@ -296,7 +305,7 @@ check_shaders(GLCtx *gl, Arena a, Stream *err) } static void -update_input(GLFWwindow *win, TerminalInput *input) +update_input(GLFWwindow *win, TerminalInput *input, posix_platform_process child) { /* NOTE: mouse */ input->mouse_scroll = (v2){0}; @@ -308,6 +317,13 @@ update_input(GLFWwindow *win, TerminalInput *input) for (u32 i = 0; i < ARRAY_COUNT(input->mouse_buttons); i++) input->mouse_buttons[i].transitions = 0; + + input->char_stream.widx = 0; + + glfwPollEvents(); + + input->character_input = stream_to_s8(&input->char_stream); + input->data_available = os_child_data_available(child.handle); } static PLATFORM_GET_CLIPBOARD_FN(x11_get_clipboard) @@ -440,6 +456,7 @@ main(i32 argc, char *argv[], char *envp[]) glfwGetMonitorWorkarea(mon, NULL, NULL, &monitor_size.w, &monitor_size.h); TerminalInput input = {0}; + input.char_stream = arena_stream(platform_arena); /* TODO: delete !!!! */ tmp_user_ctx ctx; @@ -486,7 +503,10 @@ main(i32 argc, char *argv[], char *envp[]) Range last_sel = {0}; f64 last_time = os_get_time(); while (!glfwWindowShouldClose(window)) { - update_input(window, &input); + if (os_child_exited(child.process_id)) + break; + + update_input(window, &input, child); do_debug(&term_memory, &error_stream); @@ -503,12 +523,6 @@ main(i32 argc, char *argv[], char *envp[]) check_shaders(&t->gl, t->arena_for_frame, &error_stream); } - glfwPollEvents(); - - input.data_available = os_child_data_available(child.handle); - if (os_child_exited(child.process_id)) - break; - vtgl_frame_step(&term_memory, &input); Range current_sel = vtgl_active_selection(&term_memory, 0); diff --git a/vtgl.c b/vtgl.c @@ -34,7 +34,7 @@ clear_colour(void) static b32 pressed_this_frame(ButtonState *button) { - b32 result = button->ended_down && button->transitions; + b32 result = (button->transitions > 1) || (button->ended_down && button->transitions == 1); return result; } @@ -764,19 +764,6 @@ handle_keybindings(Term *t, TerminalInput *input, PlatformAPI *platform) } static void -char_callback(GLFWwindow *win, u32 codepoint) -{ - tmp_user_ctx *ctx = glfwGetWindowUserPointer(win); - Term *t = ctx->memory->memory; - - if (t->scroll_offset) { - t->scroll_offset = 0; - t->gl.flags |= NEEDS_FULL_REFILL; - } - os_child_put_s8(t->child, utf8_encode(codepoint)); -} - -static void gl_debug_logger(u32 src, u32 type, u32 id, u32 lvl, i32 len, const char *msg, const void *data) { (void)src; (void)type; (void)id; @@ -812,7 +799,6 @@ DEBUG_EXPORT void reset_terminal(TerminalMemory *memory) { Term *t = memory->memory; - glfwSetCharCallback(t->gl.window, char_callback); glfwSetKeyCallback(t->gl.window, key_callback); //glfwSetWindowRefreshCallback(t->gl.window, refresh_callback); } @@ -974,6 +960,13 @@ DEBUG_EXPORT VTGL_FRAME_STEP_FN(vtgl_frame_step) END_NAMED_BLOCK(input_from_child); BEGIN_NAMED_BLOCK(mouse_and_keyboard_input); + if (input->character_input.len) { + if (t->scroll_offset) { + t->scroll_offset = 0; + t->gl.flags |= NEEDS_FULL_REFILL; + } + memory->platform_api.write(t->child, input->character_input, 0); + } handle_keybindings(t, input, &memory->platform_api); END_NAMED_BLOCK(mouse_and_keyboard_input); diff --git a/vtgl.h b/vtgl.h @@ -55,6 +55,12 @@ typedef struct TerminalInput { v2 mouse_scroll; ButtonState mouse_buttons[MOUSE_BUTTON_COUNT]; + /* TODO: move this out of here once callbacks are removed from terminal */ + Stream char_stream; + + /* TODO: do we want the 32bit codepoints instead? */ + s8 character_input; + f32 dt; } TerminalInput;