-
-
Notifications
You must be signed in to change notification settings - Fork 410
Implement built-in gyroscope support for SDL2 platforms #2540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* | ||
| in_gyro.c - System gyroscope input code | ||
| Copyright (C) 2026 Xash3D FWGS contributors | ||
|
|
||
| This program is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU General Public License as published by | ||
| the Free Software Foundation, either version 3 of the License, or | ||
| (at your option) any later version. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
| */ | ||
|
|
||
| #include "common.h" | ||
| #include "input.h" | ||
| #include "client.h" | ||
|
|
||
| static CVAR_DEFINE_AUTO( gyro_enable, "0", FCVAR_ARCHIVE | FCVAR_FILTERABLE, "enables aiming with built-in device gyroscope" ); | ||
| static CVAR_DEFINE_AUTO( gyro_available, "0", FCVAR_READ_ONLY, "tells whether system gyroscope hardware is available or not" ); | ||
| static CVAR_DEFINE_AUTO( gyro_pitch, "1.0", FCVAR_ARCHIVE | FCVAR_FILTERABLE, "built-in gyroscope sensitivity for looking up and down" ); | ||
| static CVAR_DEFINE_AUTO( gyro_yaw, "1.0", FCVAR_ARCHIVE | FCVAR_FILTERABLE, "built-in gyroscope sensitivity for turning left and right" ); | ||
| static CVAR_DEFINE_AUTO( gyro_roll, "0.0", FCVAR_ARCHIVE | FCVAR_FILTERABLE, "built-in gyroscope sensitivity when tilting the device sideways" ); | ||
| static CVAR_DEFINE_AUTO( gyro_pitch_deadzone, "0.5", FCVAR_ARCHIVE | FCVAR_FILTERABLE, "built-in gyroscope pitch axis deadzone (deg/s)" ); | ||
| static CVAR_DEFINE_AUTO( gyro_yaw_deadzone, "0.5", FCVAR_ARCHIVE | FCVAR_FILTERABLE, "built-in gyroscope yaw axis deadzone (deg/s)" ); | ||
| static CVAR_DEFINE_AUTO( gyro_roll_deadzone, "0.5", FCVAR_ARCHIVE | FCVAR_FILTERABLE, "built-in gyroscope roll axis deadzone (deg/s)" ); | ||
|
|
||
| // stores the latest instantaneous rotation rates from built-in gyroscope | ||
| static vec3_t gyro_speed; | ||
|
|
||
| /* | ||
| ============== | ||
| IN_GyroInit | ||
|
|
||
| ============== | ||
| */ | ||
| void IN_GyroInit( void ) | ||
| { | ||
| Cvar_RegisterVariable( &gyro_enable ); | ||
| Cvar_RegisterVariable( &gyro_available ); | ||
| Cvar_RegisterVariable( &gyro_pitch ); | ||
| Cvar_RegisterVariable( &gyro_yaw ); | ||
| Cvar_RegisterVariable( &gyro_roll ); | ||
| Cvar_RegisterVariable( &gyro_pitch_deadzone ); | ||
| Cvar_RegisterVariable( &gyro_yaw_deadzone ); | ||
| Cvar_RegisterVariable( &gyro_roll_deadzone ); | ||
| } | ||
|
|
||
| /* | ||
| ============== | ||
| IN_GyroCheckAvailability | ||
|
|
||
| One-time late check called after startup and configs | ||
| ============== | ||
| */ | ||
| void IN_GyroCheckAvailability( void ) | ||
| { | ||
| #if XASH_SDL | ||
| if( gyro_available.value ) | ||
| return; | ||
|
|
||
| if( SDLash_GyroIsAvailable() ) | ||
| { | ||
| Cvar_FullSet( "gyro_available", "1", FCVAR_READ_ONLY ); | ||
| } | ||
| #endif | ||
| } | ||
|
|
||
| /* | ||
| ============= | ||
| IN_GyroEvent | ||
|
|
||
| System gyroscope events from platform | ||
| ============= | ||
| */ | ||
| void IN_GyroEvent( vec3_t data ) | ||
| { | ||
| VectorCopy( data, gyro_speed ); | ||
| } | ||
|
|
||
| /* | ||
| ============= | ||
| IN_GyroFinalizeMove | ||
|
|
||
| Apply gyro movement to view angles | ||
| ============= | ||
| */ | ||
| void IN_GyroFinalizeMove( float *fw, float *side, float *dpitch, float *dyaw ) | ||
| { | ||
| platform_orientation_t orient; | ||
| float orient_scale = 1.0f; | ||
|
|
||
| if( !gyro_enable.value || !gyro_available.value ) | ||
| return; | ||
|
|
||
| orient = Platform_GetDisplayOrientation(); | ||
| if( orient == ORIENTATION_LANDSCAPE_FLIPPED ) | ||
| orient_scale = -1.0f; | ||
|
|
||
| // In Landscape mode axes are swapped relative to natural (Portrait) orientation | ||
| // Y axis rotation becomes Pitch (up/down) | ||
| // X axis rotation becomes Yaw (left/right) | ||
| float pitch_speed = -orient_scale * gyro_speed[1] * ( 180.0f / M_PI ); | ||
| float yaw_speed = -orient_scale * gyro_speed[0] * ( 180.0f / M_PI ); | ||
| float roll_speed = orient_scale * gyro_speed[2] * ( 180.0f / M_PI ); | ||
|
|
||
| if( fabs( pitch_speed ) < gyro_pitch_deadzone.value ) | ||
| pitch_speed = 0.0f; | ||
| if( fabs( yaw_speed ) < gyro_yaw_deadzone.value ) | ||
| yaw_speed = 0.0f; | ||
| if( fabs( roll_speed ) < gyro_roll_deadzone.value ) | ||
| roll_speed = 0.0f; | ||
|
|
||
| *dpitch -= gyro_pitch.value * pitch_speed * host.realframetime; | ||
| *dyaw += gyro_yaw.value * yaw_speed * host.realframetime; | ||
| *dyaw += gyro_roll.value * roll_speed * host.realframetime; | ||
|
|
||
| VectorClear( gyro_speed ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* | ||
| sensor_sdl2.c - SDL2 sensor handling | ||
| Copyright (C) 2026 Xash3D FWGS contributors | ||
|
|
||
| This program is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU General Public License as published by | ||
| the Free Software Foundation, either version 3 of the License, or | ||
| (at your option) any later version. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
| */ | ||
|
|
||
| #include <SDL.h> | ||
| #include "common.h" | ||
| #include "input.h" | ||
| #include "platform_sdl2.h" | ||
|
|
||
| #if SDL_VERSION_ATLEAST( 2, 0, 14 ) | ||
| static SDL_SensorID g_system_gyro_id = -1; | ||
| static SDL_Sensor *g_system_gyro; | ||
|
|
||
| /* | ||
| ============== | ||
| SDLash_InitSensors | ||
|
|
||
| ============== | ||
| */ | ||
| void SDLash_InitSensors( void ) | ||
| { | ||
| if( SDL_InitSubSystem( SDL_INIT_SENSOR ) == 0 ) | ||
| { | ||
| int num_sensors = SDL_NumSensors(); | ||
|
|
||
| for( int i = 0; i < num_sensors; i++ ) | ||
| { | ||
| if( SDL_SensorGetDeviceType( i ) == SDL_SENSOR_GYRO ) | ||
| { | ||
| g_system_gyro = SDL_SensorOpen( i ); | ||
| if( g_system_gyro ) | ||
| { | ||
| g_system_gyro_id = SDL_SensorGetInstanceID( g_system_gyro ); | ||
| Con_Printf( "SDL: Opened built-in gyroscope: %s\n", SDL_SensorGetName( g_system_gyro ) ); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| else | ||
| { | ||
| Con_Reportf( S_ERROR "Failed to init SDL Sensor subsystem: %s\n", SDL_GetError() ); | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| ============== | ||
| SDLash_ShutdownSensors | ||
|
|
||
| ============== | ||
| */ | ||
| void SDLash_ShutdownSensors( void ) | ||
| { | ||
| if( g_system_gyro ) | ||
| { | ||
| SDL_SensorClose( g_system_gyro ); | ||
| g_system_gyro = NULL; | ||
| g_system_gyro_id = -1; | ||
| } | ||
|
|
||
| SDL_QuitSubSystem( SDL_INIT_SENSOR ); | ||
| } | ||
|
|
||
| /* | ||
| ============== | ||
| SDLash_GyroIsAvailable | ||
|
|
||
| ============== | ||
| */ | ||
| qboolean SDLash_GyroIsAvailable( void ) | ||
| { | ||
| return ( g_system_gyro != NULL ); | ||
| } | ||
|
|
||
| /* | ||
| ============== | ||
| SDLash_SensorUpdate | ||
|
|
||
| ============== | ||
| */ | ||
| void SDLash_SensorUpdate( SDL_SensorEvent sensor ) | ||
| { | ||
| if( sensor.which == g_system_gyro_id ) | ||
| { | ||
| IN_GyroEvent( sensor.data ); | ||
|
Vladislav4KZ marked this conversation as resolved.
|
||
| } | ||
| } | ||
| #else | ||
| void SDLash_InitSensors( void ) { } | ||
| void SDLash_ShutdownSensors( void ) { } | ||
| qboolean SDLash_GyroIsAvailable( void ) { return false; } | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.