diff options
| author | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
| commit | 5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch) | |
| tree | 8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/src/sensor/vita/SDL_vitasensor.c | |
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/src/sensor/vita/SDL_vitasensor.c')
| -rw-r--r-- | contrib/SDL-3.2.8/src/sensor/vita/SDL_vitasensor.c | 204 |
1 files changed, 204 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/sensor/vita/SDL_vitasensor.c b/contrib/SDL-3.2.8/src/sensor/vita/SDL_vitasensor.c new file mode 100644 index 0000000..0acc215 --- /dev/null +++ b/contrib/SDL-3.2.8/src/sensor/vita/SDL_vitasensor.c | |||
| @@ -0,0 +1,204 @@ | |||
| 1 | /* | ||
| 2 | Simple DirectMedia Layer | ||
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
| 4 | |||
| 5 | This software is provided 'as-is', without any express or implied | ||
| 6 | warranty. In no event will the authors be held liable for any damages | ||
| 7 | arising from the use of this software. | ||
| 8 | |||
| 9 | Permission is granted to anyone to use this software for any purpose, | ||
| 10 | including commercial applications, and to alter it and redistribute it | ||
| 11 | freely, subject to the following restrictions: | ||
| 12 | |||
| 13 | 1. The origin of this software must not be misrepresented; you must not | ||
| 14 | claim that you wrote the original software. If you use this software | ||
| 15 | in a product, an acknowledgment in the product documentation would be | ||
| 16 | appreciated but is not required. | ||
| 17 | 2. Altered source versions must be plainly marked as such, and must not be | ||
| 18 | misrepresented as being the original software. | ||
| 19 | 3. This notice may not be removed or altered from any source distribution. | ||
| 20 | */ | ||
| 21 | #include "SDL_internal.h" | ||
| 22 | |||
| 23 | #ifdef SDL_SENSOR_VITA | ||
| 24 | |||
| 25 | #include "SDL_vitasensor.h" | ||
| 26 | #include "../SDL_syssensor.h" | ||
| 27 | #include <psp2/motion.h> | ||
| 28 | |||
| 29 | #ifndef SCE_MOTION_MAX_NUM_STATES | ||
| 30 | #define SCE_MOTION_MAX_NUM_STATES 64 | ||
| 31 | #endif | ||
| 32 | |||
| 33 | typedef struct | ||
| 34 | { | ||
| 35 | SDL_SensorType type; | ||
| 36 | SDL_SensorID instance_id; | ||
| 37 | } SDL_VitaSensor; | ||
| 38 | |||
| 39 | static SDL_VitaSensor *SDL_sensors; | ||
| 40 | static int SDL_sensors_count; | ||
| 41 | |||
| 42 | static bool SDL_VITA_SensorInit(void) | ||
| 43 | { | ||
| 44 | sceMotionReset(); | ||
| 45 | sceMotionStartSampling(); | ||
| 46 | // not sure if these are needed, we are reading unfiltered state | ||
| 47 | sceMotionSetAngleThreshold(0); | ||
| 48 | sceMotionSetDeadband(SCE_FALSE); | ||
| 49 | sceMotionSetTiltCorrection(SCE_FALSE); | ||
| 50 | |||
| 51 | SDL_sensors_count = 2; | ||
| 52 | |||
| 53 | SDL_sensors = (SDL_VitaSensor *)SDL_calloc(SDL_sensors_count, sizeof(*SDL_sensors)); | ||
| 54 | if (!SDL_sensors) { | ||
| 55 | return false; | ||
| 56 | } | ||
| 57 | |||
| 58 | SDL_sensors[0].type = SDL_SENSOR_ACCEL; | ||
| 59 | SDL_sensors[0].instance_id = SDL_GetNextObjectID(); | ||
| 60 | SDL_sensors[1].type = SDL_SENSOR_GYRO; | ||
| 61 | SDL_sensors[1].instance_id = SDL_GetNextObjectID(); | ||
| 62 | |||
| 63 | return true; | ||
| 64 | } | ||
| 65 | |||
| 66 | static int SDL_VITA_SensorGetCount(void) | ||
| 67 | { | ||
| 68 | return SDL_sensors_count; | ||
| 69 | } | ||
| 70 | |||
| 71 | static void SDL_VITA_SensorDetect(void) | ||
| 72 | { | ||
| 73 | } | ||
| 74 | |||
| 75 | static const char *SDL_VITA_SensorGetDeviceName(int device_index) | ||
| 76 | { | ||
| 77 | if (device_index < SDL_sensors_count) { | ||
| 78 | switch (SDL_sensors[device_index].type) { | ||
| 79 | case SDL_SENSOR_ACCEL: | ||
| 80 | return "Accelerometer"; | ||
| 81 | case SDL_SENSOR_GYRO: | ||
| 82 | return "Gyro"; | ||
| 83 | default: | ||
| 84 | return "Unknown"; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | return NULL; | ||
| 89 | } | ||
| 90 | |||
| 91 | static SDL_SensorType SDL_VITA_SensorGetDeviceType(int device_index) | ||
| 92 | { | ||
| 93 | if (device_index < SDL_sensors_count) { | ||
| 94 | return SDL_sensors[device_index].type; | ||
| 95 | } | ||
| 96 | |||
| 97 | return SDL_SENSOR_INVALID; | ||
| 98 | } | ||
| 99 | |||
| 100 | static int SDL_VITA_SensorGetDeviceNonPortableType(int device_index) | ||
| 101 | { | ||
| 102 | if (device_index < SDL_sensors_count) { | ||
| 103 | return SDL_sensors[device_index].type; | ||
| 104 | } | ||
| 105 | return -1; | ||
| 106 | } | ||
| 107 | |||
| 108 | static SDL_SensorID SDL_VITA_SensorGetDeviceInstanceID(int device_index) | ||
| 109 | { | ||
| 110 | if (device_index < SDL_sensors_count) { | ||
| 111 | return SDL_sensors[device_index].instance_id; | ||
| 112 | } | ||
| 113 | return -1; | ||
| 114 | } | ||
| 115 | |||
| 116 | static bool SDL_VITA_SensorOpen(SDL_Sensor *sensor, int device_index) | ||
| 117 | { | ||
| 118 | struct sensor_hwdata *hwdata; | ||
| 119 | |||
| 120 | hwdata = (struct sensor_hwdata *)SDL_calloc(1, sizeof(*hwdata)); | ||
| 121 | if (!hwdata) { | ||
| 122 | return false; | ||
| 123 | } | ||
| 124 | sensor->hwdata = hwdata; | ||
| 125 | |||
| 126 | return true; | ||
| 127 | } | ||
| 128 | |||
| 129 | static void SDL_VITA_SensorUpdate(SDL_Sensor *sensor) | ||
| 130 | { | ||
| 131 | int err = 0; | ||
| 132 | SceMotionSensorState motionState[SCE_MOTION_MAX_NUM_STATES]; | ||
| 133 | Uint64 timestamp = SDL_GetTicksNS(); | ||
| 134 | |||
| 135 | SDL_zero(motionState); | ||
| 136 | err = sceMotionGetSensorState(motionState, SCE_MOTION_MAX_NUM_STATES); | ||
| 137 | if (err != 0) { | ||
| 138 | return; | ||
| 139 | } | ||
| 140 | |||
| 141 | for (int i = 0; i < SCE_MOTION_MAX_NUM_STATES; i++) { | ||
| 142 | if (sensor->hwdata->counter < motionState[i].counter) { | ||
| 143 | unsigned int tick = motionState[i].timestamp; | ||
| 144 | unsigned int delta; | ||
| 145 | |||
| 146 | sensor->hwdata->counter = motionState[i].counter; | ||
| 147 | |||
| 148 | if (sensor->hwdata->last_tick > tick) { | ||
| 149 | SDL_COMPILE_TIME_ASSERT(tick, sizeof(tick) == sizeof(Uint32)); | ||
| 150 | delta = (SDL_MAX_UINT32 - sensor->hwdata->last_tick + tick + 1); | ||
| 151 | } else { | ||
| 152 | delta = (tick - sensor->hwdata->last_tick); | ||
| 153 | } | ||
| 154 | sensor->hwdata->sensor_timestamp += SDL_US_TO_NS(delta); | ||
| 155 | sensor->hwdata->last_tick = tick; | ||
| 156 | |||
| 157 | switch (sensor->type) { | ||
| 158 | case SDL_SENSOR_ACCEL: | ||
| 159 | { | ||
| 160 | float data[3]; | ||
| 161 | data[0] = motionState[i].accelerometer.x * SDL_STANDARD_GRAVITY; | ||
| 162 | data[1] = motionState[i].accelerometer.y * SDL_STANDARD_GRAVITY; | ||
| 163 | data[2] = motionState[i].accelerometer.z * SDL_STANDARD_GRAVITY; | ||
| 164 | SDL_SendSensorUpdate(timestamp, sensor, sensor->hwdata->sensor_timestamp, data, SDL_arraysize(data)); | ||
| 165 | } break; | ||
| 166 | case SDL_SENSOR_GYRO: | ||
| 167 | { | ||
| 168 | float data[3]; | ||
| 169 | data[0] = motionState[i].gyro.x; | ||
| 170 | data[1] = motionState[i].gyro.y; | ||
| 171 | data[2] = motionState[i].gyro.z; | ||
| 172 | SDL_SendSensorUpdate(timestamp, sensor, sensor->hwdata->sensor_timestamp, data, SDL_arraysize(data)); | ||
| 173 | } break; | ||
| 174 | default: | ||
| 175 | break; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | static void SDL_VITA_SensorClose(SDL_Sensor *sensor) | ||
| 182 | { | ||
| 183 | } | ||
| 184 | |||
| 185 | static void SDL_VITA_SensorQuit(void) | ||
| 186 | { | ||
| 187 | sceMotionStopSampling(); | ||
| 188 | } | ||
| 189 | |||
| 190 | SDL_SensorDriver SDL_VITA_SensorDriver = { | ||
| 191 | SDL_VITA_SensorInit, | ||
| 192 | SDL_VITA_SensorGetCount, | ||
| 193 | SDL_VITA_SensorDetect, | ||
| 194 | SDL_VITA_SensorGetDeviceName, | ||
| 195 | SDL_VITA_SensorGetDeviceType, | ||
| 196 | SDL_VITA_SensorGetDeviceNonPortableType, | ||
| 197 | SDL_VITA_SensorGetDeviceInstanceID, | ||
| 198 | SDL_VITA_SensorOpen, | ||
| 199 | SDL_VITA_SensorUpdate, | ||
| 200 | SDL_VITA_SensorClose, | ||
| 201 | SDL_VITA_SensorQuit, | ||
| 202 | }; | ||
| 203 | |||
| 204 | #endif // SDL_SENSOR_VITA | ||
