vtgl

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

Commit: ab9b90435bdfc1d7880a1ff6798a447f7e576a7f
Parent: 07bbc2d5ebc9ab37ea29ab9aa1b076aa4b1e56aa
Author: Randy Palamar
Date:   Mon, 19 Aug 2024 23:14:08 -0600

introduce configurable keybinds starting with font resizing

Diffstat:
Mconfig.def.h | 22++++++++++++++++++++++
Mfont.c | 11+++++++----
Mutil.h | 11++++++++++-
Mvtgl.c | 40++++++++++++++++++++++++++++------------
4 files changed, 67 insertions(+), 17 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -31,3 +31,25 @@ struct { u8 fgidx; u8 bgidx; } g_colours = {base16_colours, 7, 0}; + +#define KEYBIND_FN(name) void name(Term *t, Arg *a) +typedef KEYBIND_FN(KeyBind_Fn); + +/* NOTE: Bindable Functions */ +KEYBIND_FN(zoom); /* arg: .i = font size increment */ + +#define MODKEY (GLFW_MOD_ALT) +#define TERMMOD (GLFW_MOD_CONTROL|GLFW_MOD_SHIFT) +#define ALTMOD (GLFW_MOD_ALT|GLFW_MOD_SHIFT) +//#define XXX (GLFW_MOD_SUPER) + +struct hotkey { + u32 key; + KeyBind_Fn *fn; + Arg arg; +} g_hotkeys[] = { + {ENCODE_KEY(GLFW_PRESS, TERMMOD, GLFW_KEY_MINUS), zoom, {.i = -1}}, + {ENCODE_KEY(GLFW_REPEAT, TERMMOD, GLFW_KEY_MINUS), zoom, {.i = -1}}, + {ENCODE_KEY(GLFW_PRESS, TERMMOD, GLFW_KEY_EQUAL), zoom, {.i = +1}}, + {ENCODE_KEY(GLFW_REPEAT, TERMMOD, GLFW_KEY_EQUAL), zoom, {.i = +1}}, +}; diff --git a/font.c b/font.c @@ -97,12 +97,15 @@ update_font_metrics(Term *t) } static i32 -set_font_sizes(FontAtlas *fa, u32 fontsize) +shift_font_sizes(FontAtlas *fa, i32 size_delta) { - /* invalidate cache first incase a font fails to resize */ - for (u32 i = 0; i < fa->nfonts; i++) - if (FT_Set_Pixel_Sizes(fa->fonts[i].face, 0, fontsize)) + for (u32 i = 0; i < fa->nfonts; i++) { + i32 newsize = fa->fonts[i].fontsize + size_delta; + CLAMP(newsize, 8, MAX_FONT_SIZE); + if (FT_Set_Pixel_Sizes(fa->fonts[i].face, 0, newsize)) return -1; + fa->fonts[i].fontsize = newsize; + } return 0; } diff --git a/util.h b/util.h @@ -244,6 +244,9 @@ typedef struct { #include <ft2build.h> #include FT_FREETYPE_H #include <fontconfig/fontconfig.h> + +#define MAX_FONT_SIZE 128.0f + #include "util.c" #ifdef __unix__ @@ -255,7 +258,7 @@ typedef struct { typedef struct { u8 *buf; i32 bufsize; - u32 fontsize; + i32 fontsize; FcPattern *pattern; FT_Face face; } Font; @@ -269,6 +272,12 @@ typedef struct { i32 deltay; } FontAtlas; +typedef union { + i32 i; + f32 f; + void *v; +} Arg; + enum terminal_mode { TM_ALTSCREEN = 1 << 0, TM_REPLACE = 1 << 1, diff --git a/vtgl.c b/vtgl.c @@ -13,7 +13,6 @@ #include "debug.c" #endif -#define MAX_FONT_SIZE 128.0f #define TEXTURE_GLYPH_COUNT PUSH_BUFFER_CAP #define X(name) "u_"#name, @@ -63,6 +62,17 @@ static void resize(Term *t) { v2 ws = t->gl.window_size; + v2 cs = get_cell_size(t); + t->size.w = (u32)(ws.w / cs.w); + t->size.h = (u32)(ws.h / cs.h); + + /* TODO: Proper padding */ + //v2 ws = {.x = t->size.w * cs.w, .y = t->size.h * cs.h}; + //f32 xpad = (w - ws.w) / 2; + //f32 ypad = (h - ws.h) / 2; + //glViewport(xpad, ypad, ws.w, ws.h); + glViewport(0, 0, ws.w, ws.h); + os_alloc_framebuffer(&t->views[0].fb, t->size.h, t->size.w); os_alloc_framebuffer(&t->views[1].fb, t->size.h, t->size.w); os_set_term_size(t->child, t->size.h, t->size.w, ws.w, ws.h); @@ -287,6 +297,13 @@ render_framebuffer(Term *t, RenderPushBuffer *rpb) } } +KEYBIND_FN(zoom) +{ + shift_font_sizes(&t->fa, a.i); + update_font_textures(t); + t->gl.flags |= NEEDS_RESIZE; +} + /* NOTE: called when the window was resized */ static void fb_callback(GLFWwindow *win, i32 w, i32 h) @@ -294,17 +311,6 @@ fb_callback(GLFWwindow *win, i32 w, i32 h) Term *t = glfwGetWindowUserPointer(win); t->gl.window_size = (v2){.w = w, .h = h}; - v2 cs = get_cell_size(t); - t->size.w = (u32)(w / cs.w); - t->size.h = (u32)(h / cs.h); - - /* TODO: Proper padding */ - //v2 ws = {.x = t->size.w * cs.w, .y = t->size.h * cs.h}; - //f32 xpad = (w - ws.w) / 2; - //f32 ypad = (h - ws.h) / 2; - //glViewport(xpad, ypad, ws.w, ws.h); - glViewport(0, 0, w, h); - glActiveTexture(GL_TEXTURE0 + t->gl.fb_tex_unit); glBindTexture(GL_TEXTURE_2D, t->gl.fb_tex); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); @@ -339,6 +345,16 @@ key_callback(GLFWwindow *win, i32 key, i32 sc, i32 act, i32 mods) } } + /* NOTE: handle mapped keybindings */ + u32 enc = ENCODE_KEY(act, mods, key); + for (u32 i = 0; i < ARRAY_COUNT(g_hotkeys); i++) { + struct hotkey *hk = g_hotkeys + i; + if (hk->key == enc) { + hk->fn(t, hk->arg); + return; + } + } + switch (ENCODE_KEY(act, 0, key)) { case ENCODE_KEY(GLFW_PRESS, 0, GLFW_KEY_TAB): case ENCODE_KEY(GLFW_REPEAT, 0, GLFW_KEY_TAB):