vtgl

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

Commit: 4633fa50525ccf12d23a198bc9c26870b77df03a
Parent: b077594494d7b58f869a7faede560369adb8823a
Author: Randy Palamar
Date:   Fri, 15 Nov 2024 18:06:44 -0700

move mouse scrolling to interaction code

also maintain an active modifiers variable in the input

Diffstat:
Mplatform_linux_x11.c | 16++++++++++++++++
Mvtgl.c | 49++++++++++++++++++++++---------------------------
Mvtgl.h | 1+
3 files changed, 39 insertions(+), 27 deletions(-)

diff --git a/platform_linux_x11.c b/platform_linux_x11.c @@ -160,27 +160,43 @@ key_callback(GLFWwindow *win, i32 key, i32 scancode, i32 action, i32 modifiers) switch (key) { case GLFW_KEY_LEFT_SHIFT: button_action(input->keys + KEY_LEFT_SHIFT, action == GLFW_PRESS); + if (action == GLFW_RELEASE) input->modifiers &= ~MOD_SHIFT; + else input->modifiers |= MOD_SHIFT; break; case GLFW_KEY_LEFT_CONTROL: button_action(input->keys + KEY_LEFT_CONTROL, action == GLFW_PRESS); + if (action == GLFW_RELEASE) input->modifiers &= ~MOD_CONTROL; + else input->modifiers |= MOD_CONTROL; break; case GLFW_KEY_LEFT_ALT: button_action(input->keys + KEY_LEFT_ALT, action == GLFW_PRESS); + if (action == GLFW_RELEASE) input->modifiers &= ~MOD_ALT; + else input->modifiers |= MOD_ALT; break; case GLFW_KEY_LEFT_SUPER: button_action(input->keys + KEY_LEFT_SUPER, action == GLFW_PRESS); + if (action == GLFW_RELEASE) input->modifiers &= ~MOD_SUPER; + else input->modifiers |= MOD_SUPER; break; case GLFW_KEY_RIGHT_SHIFT: button_action(input->keys + KEY_RIGHT_SHIFT, action == GLFW_PRESS); + if (action == GLFW_RELEASE) input->modifiers &= ~MOD_SHIFT; + else input->modifiers |= MOD_SHIFT; break; case GLFW_KEY_RIGHT_CONTROL: button_action(input->keys + KEY_RIGHT_CONTROL, action == GLFW_PRESS); + if (action == GLFW_RELEASE) input->modifiers &= ~MOD_CONTROL; + else input->modifiers |= MOD_CONTROL; break; case GLFW_KEY_RIGHT_ALT: button_action(input->keys + KEY_RIGHT_ALT, action == GLFW_PRESS); + if (action == GLFW_RELEASE) input->modifiers &= ~MOD_ALT; + else input->modifiers |= MOD_ALT; break; case GLFW_KEY_RIGHT_SUPER: button_action(input->keys + KEY_RIGHT_SUPER, action == GLFW_PRESS); + if (action == GLFW_RELEASE) input->modifiers &= ~MOD_SUPER; + else input->modifiers |= MOD_SUPER; break; case GLFW_KEY_MENU: button_action(input->keys + KEY_MENU, action == GLFW_PRESS); diff --git a/vtgl.c b/vtgl.c @@ -866,30 +866,6 @@ DEBUG_EXPORT VTGL_HANDLE_KEYS_FN(vtgl_handle_keys) } static void -handle_keybindings(Term *t, TerminalInput *input, PlatformAPI *platform) -{ - if (input->mouse_scroll.y) { - b32 left_shift_state = input->keys[KEY_LEFT_SHIFT].ended_down; - b32 right_shift_state = input->keys[KEY_RIGHT_SHIFT].ended_down; - b32 shift_down = left_shift_state || right_shift_state; - if (t->mode & TM_ALTSCREEN) { - if (input->mouse_scroll.y > 0) { - if (shift_down) platform->write(t->child, s8("\x1B[5;2~"), 0); - else platform->write(t->child, s8("\x19"), 0); - } else { - if (shift_down) platform->write(t->child, s8("\x1B[6;2~"), 0); - else platform->write(t->child, s8("\x05"), 0); - } - } else { - Arg a = {.i = (i32)input->mouse_scroll.y}; - if (shift_down) - a.i *= 5; - scroll(t, platform, a); - } - } -} - -static void begin_interaction(InteractionState *is, TerminalInput *input) { is->click_count++; @@ -931,6 +907,7 @@ handle_interactions(Term *t, TerminalInput *input, PlatformAPI *platform) is->multi_click_t -= dt_for_frame; ButtonState *mouse_left = input->keys + MOUSE_LEFT; + b32 shift_down = input->modifiers & MOD_SHIFT; if (is->state != IS_NONE) { if (pressed_last_frame(mouse_left)) { end_interaction(is); @@ -948,7 +925,27 @@ handle_interactions(Term *t, TerminalInput *input, PlatformAPI *platform) case IS_DRAG: /* TODO */ break; case IS_TERM: { update_selection(t, input); - /* TODO: mouse scroll also goes here */ + if (input->mouse_scroll.y) { + if (t->mode & TM_ALTSCREEN) { + iptr child = t->child; + if (input->mouse_scroll.y > 0) { + if (shift_down) + platform->write(child, s8("\x1B[5;2~"), 0); + else + platform->write(child, s8("\x19"), 0); + } else { + if (shift_down) + platform->write(child, s8("\x1B[6;2~"), 0); + else + platform->write(child, s8("\x05"), 0); + } + } else { + Arg a = {.i = (i32)input->mouse_scroll.y}; + if (shift_down) + a.i *= 5; + scroll(t, platform, a); + } + } if (pressed_last_frame(input->keys + MOUSE_MIDDLE)) paste(t, platform, (Arg){.i = CLIPBOARD_1}); } break; @@ -1168,9 +1165,7 @@ DEBUG_EXPORT VTGL_FRAME_STEP_FN(vtgl_frame_step) } memory->platform_api.write(t->child, input->character_input, 0); } - handle_keybindings(t, input, &memory->platform_api); - /* NOTE: do this after the keybinds so that the user can rebind mouse buttons */ handle_interactions(t, input, &memory->platform_api); /* NOTE: default state which can be overwritten later in the frame */ diff --git a/vtgl.h b/vtgl.h @@ -220,6 +220,7 @@ typedef struct TerminalInput { v2 last_mouse; v2 mouse_scroll; ButtonState keys[INPUT_KEY_COUNT]; + u32 modifiers; /* TODO: do we want the 32bit codepoints instead? */ s8 character_input;