From 0e5ccf4cc3723f47d592cf0ba2375edd954c24ad Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Sun, 17 Mar 2024 19:09:20 +0100 Subject: [PATCH] jinput: add keymap customization function --- include/justui/jinput.h | 15 +++++++++++++++ src/jfileselect.c | 4 ++-- src/jinput.c | 11 ++++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/include/justui/jinput.h b/include/justui/jinput.h index f58f48c..affbd35 100644 --- a/include/justui/jinput.h +++ b/include/justui/jinput.h @@ -11,6 +11,9 @@ #include +/* Keymap function for jinput. See jinput_set_keymap_function(). */ +typedef uint32_t jinput_keymap_function_t(int key, bool shift, bool alpha); + /* jinput: One-line input field This widget is used to read input from the user. It has a single line of @@ -63,6 +66,9 @@ typedef struct { /* Timer ID for the cursor state */ int8_t timer; + /* Custom keymap function (may be NULL) */ + jinput_keymap_function_t *keymap_fun; + } jinput; /* Type IDs */ @@ -81,6 +87,15 @@ void jinput_set_text_color(jinput *input, int color); void jinput_set_font(jinput *input, font_t const *font); void jinput_set_prompt(jinput *input, char const *prompt); +/* Set a custom keymap function. The keymap function is called when a key is + pressed that should produce input in the field. The following parameters are + provided: + * key is the code for the pressed key () + * shift and alpha indicate the state of modifiers + The function should return a Unicode code point. Note that jinput can deal + with any Unicode code point but the font used for the jinput might not! */ +void jinput_set_keymap_function(jinput *input, jinput_keymap_function_t *kf); + /* Current value visible in the widget, normally useful upon receiving the JINPUT_VALIDATED event, not guaranteed otherwise */ char const *jinput_value(jinput *input); diff --git a/src/jfileselect.c b/src/jfileselect.c index d817f50..c28fca6 100644 --- a/src/jfileselect.c +++ b/src/jfileselect.c @@ -28,9 +28,9 @@ struct fileinfo { /* Entry name (owned by the structure) */ char *name; /* File size in bytes if file, number of entries if folder */ - int size; + int size :24; /* Type from [struct dirent], -1 for the "Save As" entry */ - int type; + int8_t type; }; jfileselect *jfileselect_create(void *parent) diff --git a/src/jinput.c b/src/jinput.c index 152c72c..2c32a0a 100644 --- a/src/jinput.c +++ b/src/jinput.c @@ -56,6 +56,7 @@ jinput *jinput_create(char const *prompt, size_t length, void *parent) i->mode = JINPUT_FLAT; i->timer = -1; + i->keymap_fun = NULL; return i; } @@ -79,6 +80,11 @@ void jinput_set_prompt(jinput *i, char const *prompt) i->widget.dirty = 1; } +void jinput_set_keymap_function(jinput *i, jinput_keymap_function_t *kf) +{ + i->keymap_fun = kf; +} + //--- // Input helpers //--- @@ -305,7 +311,10 @@ static bool jinput_poly_event(void *i0, jevent e) i->mode |= JINPUT_ALPHA; } else { - uint32_t code_point = keymap_translate(ev.key, + jinput_keymap_function_t *kf = i->keymap_fun; + if(!kf) + kf = &keymap_translate; + uint32_t code_point = (*kf)(ev.key, (i->mode & JINPUT_SHIFT) || (i->mode & JINPUT_SHIFT_LOCK), (i->mode & JINPUT_ALPHA) || (i->mode & JINPUT_ALPHA_LOCK) );