aboutsummaryrefslogtreecommitdiff
path: root/src/input.c
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2026-10-15 09:05:16 +0200
committerNicholas Rodrigues Lordello <[email protected]>2026-10-15 09:05:16 +0200
commitf97bb735065392d639462e90d9abd1bb5f738c61 (patch)
tree98b89f059fcd12aed31f94cf0bf4f55ece0a4f7f /src/input.c
parentb5cc8d759a09ad2754efb671dc1af12d52fc542e (diff)
wire up SDL events
Diffstat (limited to 'src/input.c')
-rw-r--r--src/input.c132
1 files changed, 114 insertions, 18 deletions
diff --git a/src/input.c b/src/input.c
index fb3e55a..412cf88 100644
--- a/src/input.c
+++ b/src/input.c
@@ -125,7 +125,7 @@ static bool handle_button(struct pw_input *input, struct pw_players *players,
return true;
}
-static bool enqueue_item(struct pw_keyboard_queue *queue, int16_t item) {
+static bool enqueue_item(struct pw_keyboard_queue *queue, int32_t item) {
size_t next = (queue->end + 1) % PW_KEYBOARD_BUFFER_SIZE;
if (next == queue->start) {
return false;
@@ -136,6 +136,16 @@ static bool enqueue_item(struct pw_keyboard_queue *queue, int16_t item) {
return true;
}
+static bool dequeue_item(struct pw_keyboard_queue *queue, int32_t *item) {
+ if (queue->start == queue->end) {
+ return false;
+ }
+
+ *item = queue->items[queue->start];
+ queue->start = (queue->start + 1) % PW_KEYBOARD_BUFFER_SIZE;
+ return true;
+}
+
static int8_t scancode_to_p8scii(SDL_Scancode sc, SDL_Keymod mod) {
switch (sc) {
case SDL_SCANCODE_TAB:
@@ -167,22 +177,36 @@ static int8_t scancode_to_p8scii(SDL_Scancode sc, SDL_Keymod mod) {
return (int8_t)keycode;
}
+// SDL has a `SDL_SCANCODE_COUNT` of 512, so we can mask the scan code with
+// `512 - 1 = 0x1ff`.
+#define PW_SCANCODE_MASK 0x1ff
+#define PW_SCANCODE_PRESSED 0x80000000
+#define PW_SCANCODE_REPEAT 0x40000000
+#define PW_SCANCODE_MOD_SHIFT 9
+
static bool handle_key(struct pw_input *input, SDL_KeyboardEvent *event) {
- if (event->scancode < 0 || event->scancode > INT16_MAX) {
+ if (event->scancode < 0 || event->scancode > PW_SCANCODE_MASK) {
return false;
}
- int16_t scancode = (int16_t)event->scancode;
- if (!event->down) {
- scancode = -scancode;
+ uint32_t sc = (uint32_t)event->scancode;
+ if (event->down) {
+ sc |= PW_SCANCODE_PRESSED;
+ }
+ if (event->repeat) {
+ sc |= PW_SCANCODE_REPEAT;
}
- if (!enqueue_item(&input->keyboard.scancodes, scancode)) {
+ sc |= (uint32_t)event->mod << PW_SCANCODE_MOD_SHIFT;
+
+ if (!enqueue_item(&input->keyboard.scancodes, (int32_t)sc)) {
SDL_LogDebug(0, "keyboard event queue full, dropping scancode");
}
- int16_t key = (int16_t)scancode_to_p8scii(event->scancode, event->mod);
- if (!enqueue_item(&input->keyboard.text, key)) {
- SDL_LogDebug(0, "keyboard text queue full, dropping key");
+ if (event->down) {
+ int8_t key = scancode_to_p8scii(event->scancode, event->mod);
+ if (!enqueue_item(&input->keyboard.text, (int32_t)key)) {
+ SDL_LogDebug(0, "keyboard text queue full, dropping key %d", key);
+ }
}
return true;
@@ -217,28 +241,57 @@ bool pw_input_event(struct pw_input *input, struct pw_players *players,
}
}
-static void update_keymod(struct pw_input *input) {
- input->keyboard.mod = PW_KEYMOD_NONE;
+static uint32_t convert_scancode(SDL_Scancode scancode) {
+ // Some of the scancodes have slightly different numbering.
+ switch (scancode) {
+ // TODO: remap scancodes that are different in case we run into any issues.
+ default:
+ return scancode;
+ }
+}
+
+static int32_t convert_keycode(SDL_Scancode scancode, SDL_Keymod mod) {
+ SDL_Keycode keycode = SDL_GetKeyFromScancode(scancode, mod, true);
+ if (keycode & SDLK_EXTENDED_MASK) {
+ // Not supported in older SDL versions.
+ return 0;
+ } else if (keycode & SDLK_SCANCODE_MASK) {
+ // Scancode keys are encoded differently by Emscripten, so imitate that
+ // here:
+ return (int32_t)(keycode & ~SDLK_SCANCODE_MASK) + 1024;
+ } else {
+ return (int32_t)keycode;
+ }
+}
+
+static enum pw_keymod convert_keymod_state(SDL_Keymod state) {
+ enum pw_keymod mod = PW_KEYMOD_NONE;
- SDL_Keymod state = SDL_GetModState();
if (state & SDL_KMOD_LSHIFT) {
- input->keyboard.mod |= PW_KEYMOD_LSHIFT;
+ mod |= PW_KEYMOD_LSHIFT;
}
if (state & SDL_KMOD_RSHIFT) {
- input->keyboard.mod |= PW_KEYMOD_RSHIFT;
+ mod |= PW_KEYMOD_RSHIFT;
}
if (state & SDL_KMOD_LCTRL) {
- input->keyboard.mod |= PW_KEYMOD_LCTRL;
+ mod |= PW_KEYMOD_LCTRL;
}
if (state & SDL_KMOD_RCTRL) {
- input->keyboard.mod |= PW_KEYMOD_RCTRL;
+ mod |= PW_KEYMOD_RCTRL;
}
if (state & SDL_KMOD_LALT) {
- input->keyboard.mod |= PW_KEYMOD_LALT;
+ mod |= PW_KEYMOD_LALT;
}
if (state & SDL_KMOD_RALT) {
- input->keyboard.mod |= PW_KEYMOD_RALT;
+ mod |= PW_KEYMOD_RALT;
}
+
+ return mod;
+}
+
+static void update_keymod(struct pw_input *input) {
+ SDL_Keymod state = SDL_GetModState();
+ input->keyboard.mod = convert_keymod_state(state);
}
static bool update_mouse(struct pw_input *input, struct pw_framebuffer *fb) {
@@ -327,3 +380,46 @@ enum pw_mouse_button pw_input_mouse(struct pw_input *input,
}
return input->mouse.button;
}
+
+bool pw_input_poll(struct pw_input *input, union pw_input_event *event) {
+ if (!input) {
+ SDL_InvalidParamError("input");
+ return false;
+ }
+
+ if (!event) {
+ // Just check whether or not there is queued event and return.
+ return input->keyboard.scancodes.start != input->keyboard.scancodes.end ||
+ input->mouse.xrel || input->mouse.yrel;
+ }
+
+ int32_t item;
+ if (dequeue_item(&input->keyboard.scancodes, &item)) {
+ uint32_t sc = (uint32_t)item;
+ SDL_Scancode scancode = (SDL_Scancode)(sc & PW_SCANCODE_MASK);
+ SDL_Keymod mod = (SDL_Keymod)(sc >> PW_SCANCODE_MOD_SHIFT);
+
+ event->type = PW_INPUT_EVENT_KEY;
+ event->key.pressed = sc & PW_SCANCODE_PRESSED;
+ event->key.repeat = sc & PW_SCANCODE_REPEAT;
+ event->key.scancode = convert_scancode(scancode);
+ event->key.keycode = convert_keycode(scancode, mod);
+ event->key.mod = convert_keymod_state(mod);
+
+ return true;
+ }
+
+ if (input->mouse.xrel || input->mouse.yrel) {
+ event->type = PW_INPUT_EVENT_MOUSE;
+ event->mouse.button = input->mouse.button;
+ event->mouse.x = input->mouse.pos.x;
+ event->mouse.y = input->mouse.pos.y;
+ event->mouse.xrel = input->mouse.xrel;
+ event->mouse.yrel = input->mouse.yrel;
+
+ input->mouse.xrel = input->mouse.yrel = 0;
+ return true;
+ }
+
+ return false;
+}