aboutsummaryrefslogtreecommitdiff
path: root/src/keyboard
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-02-24 17:55:57 +0100
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-03-30 08:20:21 +0200
commit9eee43d3bb24000077602a62dfdfeee2606f1589 (patch)
tree0e5f8efef62b068e252fe9c98c14fec723e0a7a3 /src/keyboard
parent18717569acda82b26099c62410df3b398d596ba1 (diff)
downloadrts-9eee43d3bb24000077602a62dfdfeee2606f1589.tar.gz
Add support for keyboard and mouse
Diffstat (limited to 'src/keyboard')
-rw-r--r--src/keyboard/CMakeLists.txt13
-rw-r--r--src/keyboard/inc/keyboard.h38
-rw-r--r--src/keyboard/inc/keyboard_key.h70
-rw-r--r--src/keyboard/ps1/src/keyboard.c5
-rw-r--r--src/keyboard/sdl-1.2/src/keyboard.c109
-rw-r--r--src/keyboard/src/keyboard.c69
6 files changed, 304 insertions, 0 deletions
diff --git a/src/keyboard/CMakeLists.txt b/src/keyboard/CMakeLists.txt
new file mode 100644
index 0000000..e4c0e8e
--- /dev/null
+++ b/src/keyboard/CMakeLists.txt
@@ -0,0 +1,13 @@
+set(src "src/keyboard.c")
+set(inc "inc")
+
+if(PS1_BUILD)
+ set(src ${src} "ps1/src/keyboard.c")
+elseif(SDL1_2_BUILD)
+ set(src ${src} "sdl-1.2/src/keyboard.c")
+ set(deps ${deps} SDL)
+endif()
+
+add_library(keyboard ${src})
+target_include_directories(keyboard PUBLIC ${inc})
+target_link_libraries(keyboard PUBLIC ${deps} PRIVATE ${privdeps})
diff --git a/src/keyboard/inc/keyboard.h b/src/keyboard/inc/keyboard.h
new file mode 100644
index 0000000..5c53ba8
--- /dev/null
+++ b/src/keyboard/inc/keyboard.h
@@ -0,0 +1,38 @@
+#ifndef KEYBOARD_H
+#define KEYBOARD_H
+
+#include <keyboard_key.h>
+#include <stdbool.h>
+#include <stddef.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#define KEYBOARD_COMBO(...) (const struct keyboard_combo){.keys = {__VA_ARGS__}}
+
+enum {KEYBOARD_MAX_COMBO_KEYS = 3};
+
+struct keyboard
+{
+ struct keyboard_combo
+ {
+ enum keyboard_key keys[KEYBOARD_MAX_COMBO_KEYS];
+ } combo, oldcombo;
+
+ size_t i;
+};
+
+void keyboard_init(struct keyboard *k);
+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);
+const char *keyboard_key_str(enum keyboard_key k);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* KEYBOARD_H */
diff --git a/src/keyboard/inc/keyboard_key.h b/src/keyboard/inc/keyboard_key.h
new file mode 100644
index 0000000..337a09f
--- /dev/null
+++ b/src/keyboard/inc/keyboard_key.h
@@ -0,0 +1,70 @@
+#ifndef KEYBOARD_KEYS_H
+#define KEYBOARD_KEYS_H
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#define KEYBOARD_KEYS \
+ X(KEYBOARD_KEY_NONE) \
+ X(KEYBOARD_KEY_A) \
+ X(KEYBOARD_KEY_B) \
+ X(KEYBOARD_KEY_C) \
+ X(KEYBOARD_KEY_D) \
+ X(KEYBOARD_KEY_E) \
+ X(KEYBOARD_KEY_F) \
+ X(KEYBOARD_KEY_G) \
+ X(KEYBOARD_KEY_H) \
+ X(KEYBOARD_KEY_I) \
+ X(KEYBOARD_KEY_J) \
+ X(KEYBOARD_KEY_K) \
+ X(KEYBOARD_KEY_L) \
+ X(KEYBOARD_KEY_M) \
+ X(KEYBOARD_KEY_N) \
+ X(KEYBOARD_KEY_O) \
+ X(KEYBOARD_KEY_P) \
+ X(KEYBOARD_KEY_Q) \
+ X(KEYBOARD_KEY_R) \
+ X(KEYBOARD_KEY_S) \
+ X(KEYBOARD_KEY_T) \
+ X(KEYBOARD_KEY_U) \
+ X(KEYBOARD_KEY_V) \
+ X(KEYBOARD_KEY_W) \
+ X(KEYBOARD_KEY_X) \
+ X(KEYBOARD_KEY_Y) \
+ X(KEYBOARD_KEY_Z) \
+ X(KEYBOARD_KEY_0) \
+ X(KEYBOARD_KEY_1) \
+ X(KEYBOARD_KEY_2) \
+ X(KEYBOARD_KEY_3) \
+ X(KEYBOARD_KEY_4) \
+ X(KEYBOARD_KEY_5) \
+ X(KEYBOARD_KEY_6) \
+ X(KEYBOARD_KEY_7) \
+ X(KEYBOARD_KEY_8) \
+ X(KEYBOARD_KEY_9) \
+ X(KEYBOARD_KEY_LSHIFT) \
+ X(KEYBOARD_KEY_RSHIFT) \
+ X(KEYBOARD_KEY_LCTRL) \
+ X(KEYBOARD_KEY_RCTRL) \
+ X(KEYBOARD_KEY_F11) \
+ X(KEYBOARD_KEY_ESC) \
+ X(KEYBOARD_KEY_LEFT) \
+ X(KEYBOARD_KEY_RIGHT) \
+ X(KEYBOARD_KEY_UP) \
+ X(KEYBOARD_KEY_DOWN) \
+ X(KEYBOARD_KEY_EXIT)
+
+enum keyboard_key
+{
+#define X(x) x,
+ KEYBOARD_KEYS
+#undef X
+};
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* KEYBOARD_KEYS_H */
diff --git a/src/keyboard/ps1/src/keyboard.c b/src/keyboard/ps1/src/keyboard.c
new file mode 100644
index 0000000..69e7a90
--- /dev/null
+++ b/src/keyboard/ps1/src/keyboard.c
@@ -0,0 +1,5 @@
+#include <keyboard.h>
+
+void keyboard_update(struct keyboard *const k)
+{
+}
diff --git a/src/keyboard/sdl-1.2/src/keyboard.c b/src/keyboard/sdl-1.2/src/keyboard.c
new file mode 100644
index 0000000..fafa754
--- /dev/null
+++ b/src/keyboard/sdl-1.2/src/keyboard.c
@@ -0,0 +1,109 @@
+#include <keyboard.h>
+#include <keyboard_key.h>
+#include <SDL/SDL.h>
+#include <stdio.h>
+
+static void append_key(const enum keyboard_key key, struct keyboard *const k)
+{
+ struct keyboard_combo *const c = &k->combo;
+
+ for (size_t i = 0; i < sizeof c->keys / sizeof *c->keys; i++)
+ {
+ enum keyboard_key *const sel = &c->keys[i];
+
+ if (*sel == KEYBOARD_KEY_NONE)
+ {
+ *sel = key;
+ break;
+ }
+ }
+}
+
+static void remove_key(const enum keyboard_key key, struct keyboard *const k)
+{
+ struct keyboard_combo *const c = &k->combo;
+
+ for (size_t i = 0; i < sizeof c->keys / sizeof *c->keys; i++)
+ {
+ enum keyboard_key *const sel = &c->keys[i];
+
+ if (*sel == key)
+ *sel = KEYBOARD_KEY_NONE;
+ }
+}
+
+static void key_event(const SDL_KeyboardEvent *const ev,
+ struct keyboard *const k)
+{
+ static const struct keymap
+ {
+ enum keyboard_key key;
+ SDLKey sdl_key;
+ } keymap[] =
+ {
+ {.key = KEYBOARD_KEY_LEFT, .sdl_key = SDLK_LEFT},
+ {.key = KEYBOARD_KEY_RIGHT, .sdl_key = SDLK_RIGHT},
+ {.key = KEYBOARD_KEY_UP, .sdl_key = SDLK_UP},
+ {.key = KEYBOARD_KEY_DOWN, .sdl_key = SDLK_DOWN},
+ {.key = KEYBOARD_KEY_LCTRL, .sdl_key = SDLK_LCTRL},
+ {.key = KEYBOARD_KEY_RCTRL, .sdl_key = SDLK_RCTRL},
+ {.key = KEYBOARD_KEY_LSHIFT, .sdl_key = SDLK_LSHIFT},
+ {.key = KEYBOARD_KEY_RSHIFT, .sdl_key = SDLK_RSHIFT},
+ {.key = KEYBOARD_KEY_EXIT, .sdl_key = SDLK_ESCAPE},
+ {.key = KEYBOARD_KEY_F11, .sdl_key = SDLK_F11},
+ {.key = KEYBOARD_KEY_W, .sdl_key = SDLK_w},
+ {.key = KEYBOARD_KEY_A, .sdl_key = SDLK_a},
+ {.key = KEYBOARD_KEY_S, .sdl_key = SDLK_s},
+ {.key = KEYBOARD_KEY_D, .sdl_key = SDLK_d}
+ };
+
+ for (size_t i = 0; i < sizeof keymap / sizeof *keymap; i++)
+ {
+ const struct keymap *const km = &keymap[i];
+
+ if (ev->keysym.sym == km->sdl_key)
+ {
+ if (ev->state == SDL_PRESSED)
+ append_key(km->key, k);
+ else
+ remove_key(km->key, k);
+ }
+ }
+}
+
+void keyboard_update(struct keyboard *const k)
+{
+ SDL_Event ev;
+ int n;
+
+ k->oldcombo = k->combo;
+
+ while ((n = SDL_PeepEvents(&ev, 1, SDL_GETEVENT,
+ SDL_KEYEVENTMASK | SDL_QUITMASK)) > 0)
+ {
+ switch (ev.type)
+ {
+ case SDL_KEYDOWN:
+ /* Fall through. */
+ case SDL_KEYUP:
+ key_event(&ev.key, k);
+ break;
+
+ case SDL_QUIT:
+ append_key(KEYBOARD_KEY_EXIT, k);
+ break;
+
+ default:
+ fprintf(stderr, "%s: unexpected SDL_Event %d\n",
+ __func__, ev.type);
+ break;
+ }
+ }
+
+ if (n < 0)
+ {
+ fprintf(stderr, "%s: SDL_PeepEvents: %s\n",
+ __func__, SDL_GetError());
+ return;
+ }
+}
diff --git a/src/keyboard/src/keyboard.c b/src/keyboard/src/keyboard.c
new file mode 100644
index 0000000..c247b16
--- /dev/null
+++ b/src/keyboard/src/keyboard.c
@@ -0,0 +1,69 @@
+#include <keyboard.h>
+#include <keyboard_key.h>
+#include <stdbool.h>
+#include <string.h>
+
+static bool combo_pressed(const struct keyboard_combo *const ref,
+ const struct keyboard_combo *const c)
+{
+ bool ret = false;
+
+ for (size_t i = 0; i < sizeof c->keys / sizeof *c->keys; i++)
+ {
+ const enum keyboard_key key = c->keys[i];
+
+ if (key != KEYBOARD_KEY_NONE)
+ {
+ bool success = false;
+
+ for (size_t i = 0; i < sizeof ref->keys / sizeof *ref->keys; i++)
+ {
+ if (key == ref->keys[i])
+ {
+ success = true;
+ break;
+ }
+ }
+
+ if (!(ret = success))
+ break;
+ }
+ }
+
+ return ret;
+}
+
+bool keyboard_pressed(const struct keyboard *const k,
+ const struct keyboard_combo *const c)
+{
+ return combo_pressed(&k->combo, c);
+}
+
+bool keyboard_justreleased(const struct keyboard *const k,
+ const struct keyboard_combo *const c)
+{
+ return !combo_pressed(&k->combo, c) && combo_pressed(&k->oldcombo, c);
+}
+
+bool keyboard_justpressed(const struct keyboard *const k,
+ const struct keyboard_combo *const c)
+{
+ return combo_pressed(&k->combo, c) && !combo_pressed(&k->oldcombo, c);
+}
+
+void keyboard_init(struct keyboard *const k)
+{
+ memset(k, 0, sizeof *k);
+}
+
+const char *keyboard_key_str(const enum keyboard_key k)
+{
+ static const char *const s[] =
+ {
+#define X(x) [x] = #x,
+ KEYBOARD_KEYS
+#undef X
+ };
+
+ return s[k];
+}