Commit: 654067bc97ec3674218f917dc4b3eb5344e66fcc
Parent: 9c7c4ffbe08a4671360b79e213563272432d715b
Author: Randy Palamar
Date: Thu, 7 Nov 2024 06:15:41 -0700
move scrolling code into the platform layer
Diffstat:
4 files changed, 38 insertions(+), 42 deletions(-)
diff --git a/os_unix.c b/os_unix.c
@@ -107,7 +107,10 @@ os_open(u8 *name, u32 attr)
static PLATFORM_WRITE_FN(posix_write)
{
- size result = pwrite(file, raw.data, raw.len, offset);
+ size result;
+ /* NOTE: pwrite can't be used on forked child even with an offset of 0 :( thanks linux */
+ if (offset > 0) result = pwrite(file, raw.data, raw.len, offset);
+ else result = write(file, raw.data, raw.len);
return result == raw.len;
}
diff --git a/platform_linux_x11.c b/platform_linux_x11.c
@@ -108,6 +108,15 @@ fb_callback(GLFWwindow *win, i32 w, i32 h)
}
static void
+scroll_callback(GLFWwindow *win, f64 xoff, f64 yoff)
+{
+ tmp_user_ctx *ctx = glfwGetWindowUserPointer(win);
+ TerminalInput *input = ctx->input;
+ input->mouse_scroll.x = xoff;
+ input->mouse_scroll.y = yoff;
+}
+
+static void
mouse_button_callback(GLFWwindow *win, i32 button, i32 action, i32 modifiers)
{
tmp_user_ctx *ctx = glfwGetWindowUserPointer(win);
@@ -157,6 +166,7 @@ init_window(tmp_user_ctx *ctx, iv2 window_size)
glfwSwapInterval(1);
glfwSetFramebufferSizeCallback(window, fb_callback);
+ glfwSetScrollCallback(window, scroll_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
return window;
diff --git a/terminal.c b/terminal.c
@@ -1171,9 +1171,7 @@ push_line(Term *t, Line *line, Arena a)
if (line->has_unicode) cp = get_utf8(&l);
else cp = get_ascii(&l);
- /* TODO: handle error case */
ASSERT(cp != (u32)-1);
-
if (ISCONTROL(cp)) {
push_control(t, &l, cp, a);
continue;
@@ -1217,15 +1215,7 @@ blit_lines(Term *t, Arena a, size line_count)
CLAMP(line_count, 0, tv->lines.filled);
for (size idx = -line_count; idx <= 0; idx++) {
size line_idx = get_line_idx(&tv->lines, idx - off);
- #if 0
- if (line_idx == tv->last_line_idx)
- t->cursor.pos = tv->last_cursor_pos;
- tv->last_cursor_pos = t->cursor.pos;
- tv->last_line_idx = line_idx;
- #endif
push_line(t, tv->lines.buf + line_idx, a);
- /* TODO: can we avoid this? */
- ASSERT(t->escape == 0);
}
t->gl.flags &= ~(NEEDS_FULL_REFILL|NEEDS_REFILL);
@@ -1264,7 +1254,7 @@ handle_input(Term *t, Arena a, s8 raw)
raw.len = start_len;
goto end;
}
- if (t->cursor.pos.y != old_curs_y)
+ if (!t->escape && (cp == '\n' || t->cursor.pos.y != old_curs_y))
feed_line(&tv->lines, raw.data, t->cursor.style);
continue;
diff --git a/vtgl.c b/vtgl.c
@@ -728,10 +728,32 @@ key_callback(GLFWwindow *win, i32 key, i32 sc, i32 act, i32 mods)
}
static void
-handle_keybindings(Term *t, TerminalInput *input)
+handle_keybindings(Term *t, TerminalInput *input, PlatformAPI *platform)
{
/* TODO: map other mouse buttons */
update_selection(t, input->mouse, input->mouse_buttons + MOUSE_LEFT);
+
+ GLFWwindow *win = t->gl.window;
+ if (input->mouse_scroll.y) {
+ if (t->mode & TM_ALTSCREEN) {
+ b32 left_shift_state = glfwGetKey(win, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS;
+ b32 right_shift_state = glfwGetKey(win, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS;
+ b32 shift_down = left_shift_state || right_shift_state;
+ 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 (glfwGetKey(win, GLFW_KEY_LEFT_SHIFT) ||
+ glfwGetKey(win, GLFW_KEY_RIGHT_SHIFT))
+ a.i *= 5;
+ scroll(t, a);
+ }
+ }
}
static void
@@ -748,34 +770,6 @@ char_callback(GLFWwindow *win, u32 codepoint)
}
static void
-scroll_callback(GLFWwindow *win, f64 xoff, f64 yoff)
-{
- (void)xoff;
-
- tmp_user_ctx *ctx = glfwGetWindowUserPointer(win);
- Term *t = ctx->memory->memory;
-
- if (t->mode & TM_ALTSCREEN) {
- b32 left_shift_state = glfwGetKey(win, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS;
- b32 right_shift_state = glfwGetKey(win, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS;
- b32 shift_down = left_shift_state || right_shift_state;
- if (yoff > 0) {
- if (shift_down) os_child_put_s8(t->child, s8("\x1B[5;2~"));
- else os_child_put_s8(t->child, s8("\x19"));
- } else {
- if (shift_down) os_child_put_s8(t->child, s8("\x1B[6;2~"));
- else os_child_put_s8(t->child, s8("\x05"));
- }
- } else {
- Arg a = {.i = (i32)yoff};
- if (glfwGetKey(win, GLFW_KEY_LEFT_SHIFT) ||
- glfwGetKey(win, GLFW_KEY_RIGHT_SHIFT))
- a.i *= 5;
- scroll(t, a);
- }
-}
-
-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;
@@ -814,7 +808,6 @@ reset_terminal(TerminalMemory *memory)
glfwSetCharCallback(t->gl.window, char_callback);
glfwSetKeyCallback(t->gl.window, key_callback);
//glfwSetWindowRefreshCallback(t->gl.window, refresh_callback);
- glfwSetScrollCallback(t->gl.window, scroll_callback);
}
DEBUG_EXPORT VTGL_INITIALIZE_FN(vtgl_initialize)