aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-09-20 13:49:32 +0200
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-09-20 13:52:14 +0200
commitb8d3eff412c1c91fa09435238998ba1fc17df3fe (patch)
tree3a4e19553f3afe230cc40c98477fa1be9be137a3
parent39dc70c38578d56dd4981e48fcb724ad51190d46 (diff)
keyboard: Add new public functions
These will be used by future commits.
-rw-r--r--src/keyboard/inc/keyboard.h3
-rw-r--r--src/keyboard/src/keyboard.c80
2 files changed, 83 insertions, 0 deletions
diff --git a/src/keyboard/inc/keyboard.h b/src/keyboard/inc/keyboard.h
index 5c53ba8..0fb0179 100644
--- a/src/keyboard/inc/keyboard.h
+++ b/src/keyboard/inc/keyboard.h
@@ -29,6 +29,9 @@ void keyboard_update(struct keyboard *k);
bool keyboard_justpressed(const struct keyboard *k, const struct keyboard_combo *c);
bool keyboard_pressed(const struct keyboard *k, const struct keyboard_combo *c);
bool keyboard_justreleased(const struct keyboard *k, const struct keyboard_combo *c);
+bool keyboard_any_justpressed(const struct keyboard *k, struct keyboard_combo *c);
+bool keyboard_any_pressed(const struct keyboard *k, struct keyboard_combo *c);
+char keyboard_to_char(const struct keyboard *k, enum keyboard_key key);
const char *keyboard_key_str(enum keyboard_key k);
#ifdef __cplusplus
diff --git a/src/keyboard/src/keyboard.c b/src/keyboard/src/keyboard.c
index 958ac65..0b0489c 100644
--- a/src/keyboard/src/keyboard.c
+++ b/src/keyboard/src/keyboard.c
@@ -1,8 +1,88 @@
#include <keyboard.h>
#include <keyboard_key.h>
#include <stdbool.h>
+#include <stddef.h>
#include <string.h>
+bool keyboard_any_justpressed(const struct keyboard *const k,
+ struct keyboard_combo *const c)
+{
+ bool ret = false;
+
+ for (size_t i = 0; i < sizeof c->keys / sizeof c->keys; i++)
+ c->keys[i] = KEYBOARD_KEY_NONE;
+
+ for (size_t i = 0, j = 0;
+ i < sizeof k->combo.keys / sizeof *k->combo.keys; i++)
+ {
+ const enum keyboard_key key = k->combo.keys[i];
+
+ if (key != KEYBOARD_KEY_NONE
+ && k->oldcombo.keys[i] == KEYBOARD_KEY_NONE)
+ {
+ c->keys[j++] = key;
+ ret = true;
+ }
+ }
+
+ return ret;
+}
+
+bool keyboard_any_pressed(const struct keyboard *const k,
+ struct keyboard_combo *const c)
+{
+ for (size_t i = 0; i < sizeof k->combo.keys / sizeof *k->combo.keys; i++)
+ {
+ const enum keyboard_key key = k->combo.keys[i];
+
+ if (key != KEYBOARD_KEY_NONE)
+ {
+ *c = k->combo;
+ return true;
+ }
+ }
+
+ return false;
+}
+
+char keyboard_to_char(const struct keyboard *const k,
+ const enum keyboard_key key)
+{
+ if (key >= KEYBOARD_KEY_0 && key <= KEYBOARD_KEY_9)
+ return '0' + key - KEYBOARD_KEY_0;
+ else if (key >= KEYBOARD_KEY_A && key <= KEYBOARD_KEY_Z)
+ {
+ if (keyboard_pressed(k, &KEYBOARD_COMBO(KEYBOARD_KEY_LSHIFT))
+ || keyboard_pressed(k, &KEYBOARD_COMBO(KEYBOARD_KEY_RSHIFT)))
+ return 'A' + key - KEYBOARD_KEY_A;
+ else
+ return 'a' + key - KEYBOARD_KEY_A;
+ }
+ else
+ {
+ static const struct map
+ {
+ enum keyboard_key key;
+ char ch;
+ } map[] =
+ {
+ {.key = KEYBOARD_KEY_DOT, .ch = '.'},
+ {.key = KEYBOARD_KEY_SPACE, .ch = ' '},
+ {.key = KEYBOARD_KEY_MINUS, .ch = '-'}
+ };
+
+ for (size_t i = 0; i < sizeof map / sizeof *map; i++)
+ {
+ const struct map *const m = &map[i];
+
+ if (key == m->key)
+ return m->ch;
+ }
+ }
+
+ return -1;
+}
+
static bool combo_pressed(const struct keyboard_combo *const ref,
const struct keyboard_combo *const c)
{