From 0d97b47728734d199e0204c642af0d3cc528553d Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Sat, 12 Jul 2025 22:38:18 -0500 Subject: [PATCH] support for gamepad events --- source/cell.c | 2 +- source/qjs_sdl_input.c | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/source/cell.c b/source/cell.c index 1ea75475..f8a99d1e 100644 --- a/source/cell.c +++ b/source/cell.c @@ -877,7 +877,7 @@ int main(int argc, char **argv) profile_enabled = 1; script_start = 2; } - if (!SDL_Init(SDL_INIT_EVENTS)) { + if (!SDL_Init(SDL_INIT_EVENTS | SDL_INIT_GAMEPAD)) { printf("CRITICAL ERROR: %s\n", SDL_GetError()); exit(1); } diff --git a/source/qjs_sdl_input.c b/source/qjs_sdl_input.c index 40032748..44af8ddc 100644 --- a/source/qjs_sdl_input.c +++ b/source/qjs_sdl_input.c @@ -653,16 +653,26 @@ static void event2wota_write(WotaBuffer *wb, const SDL_Event *e, int c) { wota_write_text(wb, "which"); wota_write_number(wb, (double)e->gaxis.which); wota_write_text(wb, "axis"); - wota_write_number(wb, (double)e->gaxis.axis); + wota_write_text(wb, SDL_GetGamepadStringForAxis(e->gaxis.axis)); wota_write_text(wb, "value"); - wota_write_number(wb, (double)e->gaxis.value); + // Normalize axis values + double normalized_value; + if (e->gaxis.axis == SDL_GAMEPAD_AXIS_LEFT_TRIGGER || + e->gaxis.axis == SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) { + // Triggers: 0 to 32767 -> 0 to 1 + normalized_value = (double)e->gaxis.value / 32767.0; + } else { + // Thumbsticks: -32768 to 32767 -> -1 to 1 + normalized_value = (double)e->gaxis.value / 32767.0; + } + wota_write_number(wb, normalized_value); break; case SDL_EVENT_GAMEPAD_BUTTON_DOWN: case SDL_EVENT_GAMEPAD_BUTTON_UP: wota_write_text(wb, "which"); wota_write_number(wb, (double)e->gbutton.which); wota_write_text(wb, "button"); - wota_write_number(wb, (double)e->gbutton.button); + wota_write_text(wb, SDL_GetGamepadStringForButton(e->gbutton.button)); wota_write_text(wb, "down"); wota_write_sym(wb, e->gbutton.down ? WOTA_TRUE : WOTA_FALSE); break;