jinput: add keymap customization function

This commit is contained in:
Lephenixnoir 2024-03-17 19:09:20 +01:00
parent bea113f09e
commit 0e5ccf4cc3
No known key found for this signature in database
GPG key ID: 1BBA026E13FC0495
3 changed files with 27 additions and 3 deletions

View file

@ -11,6 +11,9 @@
#include <gint/display.h>
/* 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 (<gint/keycodes.h>)
* 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);

View file

@ -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)

View file

@ -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)
);