aboutsummaryrefslogtreecommitdiff
path: root/input_field.c
diff options
context:
space:
mode:
authorJack Jamison <jackqjamison@gmail.com>2025-10-12 00:07:02 -0400
committerJack Jamison <jackqjamison@gmail.com>2025-10-12 00:07:02 -0400
commit60585faf7951cffb6046280bdd5f91275fb97d65 (patch)
tree30aad6fb91d33ed3b9a291d2d3fff7d9ed9a6f0c /input_field.c
parent36d12796644b296f300d9daf631f7702e1d81b86 (diff)
input buffer and key repeat
Diffstat (limited to 'input_field.c')
-rw-r--r--input_field.c49
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);
+ }
+}