|
| 1 | +/* |
| 2 | +in_gyro.c - System gyroscope input code |
| 3 | +Copyright (C) 2026 Xash3D FWGS contributors |
| 4 | +
|
| 5 | +This program is free software: you can redistribute it and/or modify |
| 6 | +it under the terms of the GNU General Public License as published by |
| 7 | +the Free Software Foundation, either version 3 of the License, or |
| 8 | +(at your option) any later version. |
| 9 | +
|
| 10 | +This program is distributed in the hope that it will be useful, |
| 11 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +GNU General Public License for more details. |
| 14 | +*/ |
| 15 | + |
| 16 | +#include "common.h" |
| 17 | +#include "input.h" |
| 18 | +#include "client.h" |
| 19 | + |
| 20 | +static CVAR_DEFINE_AUTO( gyro_enable, "0", FCVAR_ARCHIVE | FCVAR_FILTERABLE, "enables aiming with built-in device gyroscope" ); |
| 21 | +static CVAR_DEFINE_AUTO( gyro_available, "0", FCVAR_READ_ONLY, "tells whether system gyroscope hardware is available or not" ); |
| 22 | +static CVAR_DEFINE_AUTO( gyro_pitch, "1.0", FCVAR_ARCHIVE | FCVAR_FILTERABLE, "built-in gyroscope sensitivity for looking up and down" ); |
| 23 | +static CVAR_DEFINE_AUTO( gyro_yaw, "1.0", FCVAR_ARCHIVE | FCVAR_FILTERABLE, "built-in gyroscope sensitivity for turning left and right" ); |
| 24 | +static CVAR_DEFINE_AUTO( gyro_roll, "0.0", FCVAR_ARCHIVE | FCVAR_FILTERABLE, "built-in gyroscope sensitivity when tilting the device sideways" ); |
| 25 | +static CVAR_DEFINE_AUTO( gyro_pitch_deadzone, "0.5", FCVAR_ARCHIVE | FCVAR_FILTERABLE, "built-in gyroscope pitch axis deadzone (deg/s)" ); |
| 26 | +static CVAR_DEFINE_AUTO( gyro_yaw_deadzone, "0.5", FCVAR_ARCHIVE | FCVAR_FILTERABLE, "built-in gyroscope yaw axis deadzone (deg/s)" ); |
| 27 | +static CVAR_DEFINE_AUTO( gyro_roll_deadzone, "0.5", FCVAR_ARCHIVE | FCVAR_FILTERABLE, "built-in gyroscope roll axis deadzone (deg/s)" ); |
| 28 | + |
| 29 | +// stores the latest instantaneous rotation rates from built-in gyroscope |
| 30 | +static vec3_t gyro_speed; |
| 31 | + |
| 32 | +/* |
| 33 | +============== |
| 34 | +IN_GyroInit |
| 35 | +
|
| 36 | +============== |
| 37 | +*/ |
| 38 | +void IN_GyroInit( void ) |
| 39 | +{ |
| 40 | + Cvar_RegisterVariable( &gyro_enable ); |
| 41 | + Cvar_RegisterVariable( &gyro_available ); |
| 42 | + Cvar_RegisterVariable( &gyro_pitch ); |
| 43 | + Cvar_RegisterVariable( &gyro_yaw ); |
| 44 | + Cvar_RegisterVariable( &gyro_roll ); |
| 45 | + Cvar_RegisterVariable( &gyro_pitch_deadzone ); |
| 46 | + Cvar_RegisterVariable( &gyro_yaw_deadzone ); |
| 47 | + Cvar_RegisterVariable( &gyro_roll_deadzone ); |
| 48 | +} |
| 49 | + |
| 50 | +/* |
| 51 | +============== |
| 52 | +IN_GyroCheckAvailability |
| 53 | +
|
| 54 | +One-time late check called after startup and configs |
| 55 | +============== |
| 56 | +*/ |
| 57 | +void IN_GyroCheckAvailability( void ) |
| 58 | +{ |
| 59 | +#if XASH_SDL |
| 60 | + if( gyro_available.value ) |
| 61 | + return; |
| 62 | + |
| 63 | + if( SDLash_GyroIsAvailable() ) |
| 64 | + { |
| 65 | + Cvar_FullSet( "gyro_available", "1", FCVAR_READ_ONLY ); |
| 66 | + } |
| 67 | +#endif |
| 68 | +} |
| 69 | + |
| 70 | +/* |
| 71 | +============= |
| 72 | +IN_GyroEvent |
| 73 | +
|
| 74 | +System gyroscope events from platform |
| 75 | +============= |
| 76 | +*/ |
| 77 | +void IN_GyroEvent( vec3_t data ) |
| 78 | +{ |
| 79 | + VectorCopy( data, gyro_speed ); |
| 80 | +} |
| 81 | + |
| 82 | +/* |
| 83 | +============= |
| 84 | +IN_GyroFinalizeMove |
| 85 | +
|
| 86 | +Apply gyro movement to view angles |
| 87 | +============= |
| 88 | +*/ |
| 89 | +void IN_GyroFinalizeMove( float *fw, float *side, float *dpitch, float *dyaw ) |
| 90 | +{ |
| 91 | + platform_orientation_t orient; |
| 92 | + float orient_scale = 1.0f; |
| 93 | + |
| 94 | + if( !gyro_enable.value || !gyro_available.value ) |
| 95 | + return; |
| 96 | + |
| 97 | + orient = Platform_GetDisplayOrientation(); |
| 98 | + if( orient == ORIENTATION_LANDSCAPE_FLIPPED ) |
| 99 | + orient_scale = -1.0f; |
| 100 | + |
| 101 | + // In Landscape mode axes are swapped relative to natural (Portrait) orientation |
| 102 | + // Y axis rotation becomes Pitch (up/down) |
| 103 | + // X axis rotation becomes Yaw (left/right) |
| 104 | + float pitch_speed = orient_scale * gyro_speed[1] * ( 180.0f / M_PI ); |
| 105 | + float yaw_speed = -orient_scale * gyro_speed[0] * ( 180.0f / M_PI ); |
| 106 | + float roll_speed = orient_scale * gyro_speed[2] * ( 180.0f / M_PI ); |
| 107 | + |
| 108 | + if( fabs( pitch_speed ) < gyro_pitch_deadzone.value ) |
| 109 | + pitch_speed = 0.0f; |
| 110 | + if( fabs( yaw_speed ) < gyro_yaw_deadzone.value ) |
| 111 | + yaw_speed = 0.0f; |
| 112 | + if( fabs( roll_speed ) < gyro_roll_deadzone.value ) |
| 113 | + roll_speed = 0.0f; |
| 114 | + |
| 115 | + *dpitch -= gyro_pitch.value * pitch_speed * host.realframetime; |
| 116 | + *dyaw += gyro_yaw.value * yaw_speed * host.realframetime; |
| 117 | + *dyaw += gyro_roll.value * roll_speed * host.realframetime; |
| 118 | + |
| 119 | + VectorClear( gyro_speed ); |
| 120 | +} |
0 commit comments