diff options
Diffstat (limited to 'input_field.c')
| -rw-r--r-- | input_field.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/input_field.c b/input_field.c new file mode 100644 index 0000000..026a22c --- /dev/null +++ b/input_field.c @@ -0,0 +1,49 @@ +#include <string.h> + +#include "input_field.h" +#include "array.h" + +void type_key(client_state* state, xkb_keysym_t keysym) { + bool ctrl = xkb_state_mod_name_is_active(state->xkb_state, XKB_MOD_NAME_CTRL, XKB_STATE_MODS_EFFECTIVE); + + // exit app + if ((keysym == XKB_KEY_c && ctrl) || + (keysym == XKB_KEY_g && ctrl) || + (keysym == XKB_KEY_Escape)) { + + state->running = false; + return; + } + + // submit line + if (keysym == XKB_KEY_Return || keysym == XKB_KEY_KP_Enter) { + submit_line(state); + return; + } + + // delete + if (keysym == XKB_KEY_BackSpace || keysym == XKB_KEY_Delete) { + if (ctrl) { + array_clear(state->input_buffer); + } else { + array_pop(state->input_buffer); + } + return; + } + + // type char into buffer + char buf[16] = {0}; + int len = xkb_keysym_to_utf8(keysym, buf, sizeof(buf)); + if (len > 0) { + for (int i = 0; i < strlen(buf); i++) { + array_add(state->input_buffer, buf[i]); + } + } +} + +void submit_line(client_state* state) { + if (state->input_buffer) { + printf("%.*s\n", (int)array_size(state->input_buffer), state->input_buffer); + array_clear(state->input_buffer); + } +} |
