From ca9abfc9f8810ac98126b69c4b78547958a4f883 Mon Sep 17 00:00:00 2001 From: minenice55 Date: Sun, 15 Feb 2026 23:27:46 -0500 Subject: [PATCH] prelim work for sensors and other outputs --- src/d_event.h | 1 + src/g_input.c | 14 ++++++++++++ src/g_input.h | 11 +++++---- src/i_gamepad.h | 5 +++++ src/sdl/i_gamepad.c | 55 +++++++++++++++++++++++++++++++++++++++++++++ src/sdl/i_video.cpp | 47 ++++++++++++++++++++++++++++++++++++++ src/sdl/sdlmain.h | 11 +++++++++ 7 files changed, 140 insertions(+), 4 deletions(-) diff --git a/src/d_event.h b/src/d_event.h index 0726a9873..eb207aa4e 100644 --- a/src/d_event.h +++ b/src/d_event.h @@ -29,6 +29,7 @@ typedef enum ev_console, ev_mouse, ev_joystick, + ev_sensor, } evtype_t; // Event structure. diff --git a/src/g_input.c b/src/g_input.c index c0d8015ec..14f75950e 100644 --- a/src/g_input.c +++ b/src/g_input.c @@ -471,6 +471,20 @@ static keyname_t keynames[] = {KEY_AXIS1+7, "R-STICK DOWN"}, {KEY_AXIS1+8, "L TRIGGER"}, {KEY_AXIS1+9, "R TRIGGER"}, + + {KEY_ACCELEROMETER1+0, "TILT -X"}, + {KEY_ACCELEROMETER1+1, "TILT +X"}, + {KEY_ACCELEROMETER1+2, "TILT -Y"}, + {KEY_ACCELEROMETER1+3, "TILT +Y"}, + {KEY_ACCELEROMETER1+4, "TILT -Z"}, + {KEY_ACCELEROMETER1+5, "TILT +Z"}, + + {KEY_GYROSCOPE1+0, "ROTATE -X"}, + {KEY_GYROSCOPE1+1, "ROTATE +X"}, + {KEY_GYROSCOPE1+2, "ROTATE -Y"}, + {KEY_GYROSCOPE1+3, "ROTATE +Y"}, + {KEY_GYROSCOPE1+4, "ROTATE -Z"}, + {KEY_GYROSCOPE1+5, "ROTATE +Z"}, }; static const char *gamecontrolname[num_gamecontrols] = diff --git a/src/g_input.h b/src/g_input.h index 256f61574..aa3d27e1e 100644 --- a/src/g_input.h +++ b/src/g_input.h @@ -32,10 +32,11 @@ extern "C" { #define JOYBUTTONS 26 // 26 buttons, to match SDL_GameControllerButton #define JOY_DPAD_UP 11 -#define JOYANALOGS 4 // 4 analog stick axes (2 sticks * 2 axes) -#define JOYTRIGGERS 2 // 2 trigger axes, positive only -#define JOYAXISES (JOYANALOGS + JOYTRIGGERS) -#define JOYAXISKEYS ((2 * JOYANALOGS) + JOYTRIGGERS) +#define JOYANALOGS 4 // 4 analog stick axes (2 sticks * 2 axes) +#define JOYTRIGGERS 2 // 2 trigger axes, positive only +#define JOYIMUAXISES 6 // 3 accelerometer, 3 gyroscope axes (x, y, z) +#define JOYAXISES (JOYANALOGS + JOYTRIGGERS + JOYIMUAXISES) +#define JOYAXISKEYS ((2 * JOYANALOGS) + JOYTRIGGERS + (2 * JOYIMUAXISES)) #define MAXINPUTMAPPING 4 @@ -47,6 +48,8 @@ typedef enum KEY_JOY1 = NUMKEYS, KEY_HAT1 = KEY_JOY1 + JOY_DPAD_UP, KEY_AXIS1 = KEY_JOY1 + JOYBUTTONS, + KEY_ACCELEROMETER1 = KEY_AXIS1 + JOYANALOGS + JOYTRIGGERS, + KEY_GYROSCOPE1 = KEY_ACCELEROMETER1 + 6, JOYINPUTEND = KEY_AXIS1 + JOYAXISKEYS, KEY_MOUSE1 = JOYINPUTEND, diff --git a/src/i_gamepad.h b/src/i_gamepad.h index 08656051d..a8b2371ae 100644 --- a/src/i_gamepad.h +++ b/src/i_gamepad.h @@ -108,7 +108,12 @@ const char *I_GetControllerName(INT32 joyid); INT32 I_GetControllerSlotfromID(INT32 id); boolean I_ControllerSupportsRumble(INT32 playernum); +boolean I_ControllerSupportsIndicatorMono(INT32 playernum); boolean I_ControllerSupportsIndicatorColor(INT32 playernum); +boolean I_ControllerSupportsIndicatorPlayer(INT32 playernum); + +boolean I_ControllerSupportsSensorAccelerometer(INT32 playernum); +boolean I_ControllerSupportsSensorGyro(INT32 playernum); void I_ControllerRumble(INT32 playernum, UINT16 low_strength, UINT16 high_strength, UINT32 duration); void I_SetControllerIndicatorColor(INT32 playernum, UINT8 red, UINT8 green, UINT8 blue); diff --git a/src/sdl/i_gamepad.c b/src/sdl/i_gamepad.c index 020cef1ef..d37ece3da 100644 --- a/src/sdl/i_gamepad.c +++ b/src/sdl/i_gamepad.c @@ -54,7 +54,12 @@ static void I_ControllerReset(controllerinfo_t *controller) controller->id = -1; //controller->name = NULL; controller->hasrumble = false; + controller->hasmonoled = false; controller->hasrgbled = false; + controller->hasplayerled = false; + controller->hasaccelerometer = false; + controller->hasgyro = false; + controller->flipbuttons = false; } // @@ -218,6 +223,12 @@ void I_InitController(UINT8 playernum) SDL_PropertiesID controllerproperties = SDL_GetGamepadProperties(newcontroller); controller->hasrumble = SDL_GetBooleanProperty(controllerproperties, SDL_PROP_GAMEPAD_CAP_RUMBLE_BOOLEAN, false); controller->hasrgbled = SDL_GetBooleanProperty(controllerproperties, SDL_PROP_GAMEPAD_CAP_RGB_LED_BOOLEAN, false); + controller->hasmonoled = SDL_GetBooleanProperty(controllerproperties, SDL_PROP_GAMEPAD_CAP_MONO_LED_BOOLEAN, false) && (!controller->hasrgbled); + controller->hasplayerled = SDL_GetBooleanProperty(controllerproperties, SDL_PROP_GAMEPAD_CAP_PLAYER_LED_BOOLEAN, false); + + controller->hasaccelerometer = SDL_GamepadHasSensor(newcontroller, SDL_SENSOR_ACCEL); + controller->hasgyro = SDL_GamepadHasSensor(newcontroller, SDL_SENSOR_GYRO); + controller->flipbuttons = SDL_GetGamepadButtonLabel(newcontroller, SDL_GAMEPAD_BUTTON_SOUTH) == SDL_GAMEPAD_BUTTON_LABEL_B; } @@ -378,11 +389,31 @@ boolean I_ControllerSupportsRumble(INT32 playernum) return (controllerInfo[playernum].dev && controllerInfo[playernum].hasrumble); } +boolean I_ControllerSupportsIndicatorMono(INT32 playernum) +{ + return (controllerInfo[playernum].dev && controllerInfo[playernum].hasmonoled); +} + boolean I_ControllerSupportsIndicatorColor(INT32 playernum) { return (controllerInfo[playernum].dev && controllerInfo[playernum].hasrgbled); } +boolean I_ControllerSupportsIndicatorPlayer(INT32 playernum) +{ + return (controllerInfo[playernum].dev && controllerInfo[playernum].hasplayerled); +} + +boolean I_ControllerSupportsSensorAccelerometer(INT32 playernum) +{ + return (controllerInfo[playernum].dev && controllerInfo[playernum].hasaccelerometer); +} + +boolean I_ControllerSupportsSensorGyro(INT32 playernum) +{ + return (controllerInfo[playernum].dev && controllerInfo[playernum].hasgyro); +} + void I_ControllerRumble(INT32 playernum, UINT16 low_strength, UINT16 high_strength, UINT32 duration) { SDL_Gamepad *controller = controllerInfo[playernum].dev; @@ -407,4 +438,28 @@ void I_SetControllerIndicatorColor(INT32 playernum, UINT8 red, UINT8 green, UINT SDL_SetGamepadLED(controller, red, green, blue); } +void I_SetControllerIndicatorMonochrome(INT32 playernum, UINT8 brightness) +{ + SDL_Gamepad *controller = controllerInfo[playernum].dev; + + if (controller == NULL || !I_ControllerSupportsIndicatorMono(playernum)) + { + return; + } + + SDL_SetGamepadLED(controller, brightness, 0, 0); +} + +void I_SetControllerPlayerNumber(INT32 playernum, INT32 number) +{ + SDL_Gamepad *controller = controllerInfo[playernum].dev; + + if (controller == NULL || !I_ControllerSupportsIndicatorPlayer(playernum)) + { + return; + } + + SDL_SetGamepadPlayerIndex(controller, number); +} + #endif diff --git a/src/sdl/i_video.cpp b/src/sdl/i_video.cpp index c2c9e7cfd..9a2fd0bea 100644 --- a/src/sdl/i_video.cpp +++ b/src/sdl/i_video.cpp @@ -937,6 +937,50 @@ static void Impl_HandleControllerButtonEvent(SDL_GamepadButtonEvent evt, Uint32 } } +static void Impl_HandleControllerSensorEvent(SDL_GamepadSensorEvent evt) +{ + event_t event; + + event.type = ev_sensor; + + event.device = 1 + evt.which; + + event.data1 = event.data2 = event.data3 = INT32_MAX; + + if (event.device == INT32_MAX) + { + return; + } + + switch (evt.sensor) + { + // data[0]: x acceleration in m/s + // data[1]: y acceleration in m/s + // data[2]: z acceleration in m/s + case SDL_SENSOR_ACCEL: + event.data1 = FLOAT_TO_FIXED(evt.data[0]); + event.data2 = FLOAT_TO_FIXED(evt.data[1]); + event.data3 = FLOAT_TO_FIXED(evt.data[2]); + break; + + // data[0]: delta pitch in rad/s + // data[1]: delta yaw in rad/s + // data[2]: delta roll in rad/s + case SDL_SENSOR_GYRO: +#define RAD2DEG 57.295779513f + event.data1 = FixedAngle(FLOAT_TO_FIXED(RAD2DEG * evt.data[0])); + event.data2 = FixedAngle(FLOAT_TO_FIXED(RAD2DEG * evt.data[1])); + event.data3 = FixedAngle(FLOAT_TO_FIXED(RAD2DEG * evt.data[2])); +#undef RAD2DEG + break; + + default: + break; + } + + D_PostEvent(&event); +} + static void Impl_HandleControllerAddEvent(SDL_Event evt) { UINT8 i; @@ -1027,6 +1071,9 @@ void I_GetEvent(void) case SDL_EVENT_GAMEPAD_BUTTON_DOWN: Impl_HandleControllerButtonEvent(evt.gbutton, evt.type); break; + case SDL_EVENT_GAMEPAD_SENSOR_UPDATE: + Impl_HandleControllerSensorEvent(evt.gsensor); + break; case SDL_EVENT_GAMEPAD_ADDED: Impl_HandleControllerAddEvent(evt); break; diff --git a/src/sdl/sdlmain.h b/src/sdl/sdlmain.h index 90592fae8..ffebe3c15 100644 --- a/src/sdl/sdlmain.h +++ b/src/sdl/sdlmain.h @@ -52,10 +52,21 @@ typedef struct SDLJoyInfo_s //const char *name; /// scale of axises INT32 scale; + // Rumble Support boolean hasrumble; + // Mono LED Support + boolean hasmonoled; // RGB LED Support boolean hasrgbled; + // Player LED Support + boolean hasplayerled; + + // Accelerometer Support + boolean hasaccelerometer; + // Gyroscope Support + boolean hasgyro; + // you can pry my nintendo button layout out of my cold dead hands, SDL3 boolean flipbuttons; } controllerinfo_t;