diff options
Diffstat (limited to 'contrib/SDL-3.2.8/src/sensor/coremotion')
| -rw-r--r-- | contrib/SDL-3.2.8/src/sensor/coremotion/SDL_coremotionsensor.h | 27 | ||||
| -rw-r--r-- | contrib/SDL-3.2.8/src/sensor/coremotion/SDL_coremotionsensor.m | 214 |
2 files changed, 241 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/sensor/coremotion/SDL_coremotionsensor.h b/contrib/SDL-3.2.8/src/sensor/coremotion/SDL_coremotionsensor.h new file mode 100644 index 0000000..09fc0dc --- /dev/null +++ b/contrib/SDL-3.2.8/src/sensor/coremotion/SDL_coremotionsensor.h | |||
| @@ -0,0 +1,27 @@ | |||
| 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 | // The private structure used to keep track of a sensor | ||
| 24 | struct sensor_hwdata | ||
| 25 | { | ||
| 26 | float data[3]; | ||
| 27 | }; | ||
diff --git a/contrib/SDL-3.2.8/src/sensor/coremotion/SDL_coremotionsensor.m b/contrib/SDL-3.2.8/src/sensor/coremotion/SDL_coremotionsensor.m new file mode 100644 index 0000000..e5f890f --- /dev/null +++ b/contrib/SDL-3.2.8/src/sensor/coremotion/SDL_coremotionsensor.m | |||
| @@ -0,0 +1,214 @@ | |||
| 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_COREMOTION | ||
| 24 | |||
| 25 | // This is the system specific header for the SDL sensor API | ||
| 26 | #include <CoreMotion/CoreMotion.h> | ||
| 27 | |||
| 28 | #include "SDL_coremotionsensor.h" | ||
| 29 | #include "../SDL_syssensor.h" | ||
| 30 | #include "../SDL_sensor_c.h" | ||
| 31 | |||
| 32 | typedef struct | ||
| 33 | { | ||
| 34 | SDL_SensorType type; | ||
| 35 | SDL_SensorID instance_id; | ||
| 36 | } SDL_CoreMotionSensor; | ||
| 37 | |||
| 38 | static CMMotionManager *SDL_motion_manager; | ||
| 39 | static SDL_CoreMotionSensor *SDL_sensors; | ||
| 40 | static int SDL_sensors_count; | ||
| 41 | |||
| 42 | static bool SDL_COREMOTION_SensorInit(void) | ||
| 43 | { | ||
| 44 | int i, sensors_count = 0; | ||
| 45 | |||
| 46 | if (!SDL_motion_manager) { | ||
| 47 | SDL_motion_manager = [[CMMotionManager alloc] init]; | ||
| 48 | } | ||
| 49 | |||
| 50 | if (SDL_motion_manager.accelerometerAvailable) { | ||
| 51 | ++sensors_count; | ||
| 52 | } | ||
| 53 | if (SDL_motion_manager.gyroAvailable) { | ||
| 54 | ++sensors_count; | ||
| 55 | } | ||
| 56 | |||
| 57 | if (sensors_count > 0) { | ||
| 58 | SDL_sensors = (SDL_CoreMotionSensor *)SDL_calloc(sensors_count, sizeof(*SDL_sensors)); | ||
| 59 | if (!SDL_sensors) { | ||
| 60 | return false; | ||
| 61 | } | ||
| 62 | |||
| 63 | i = 0; | ||
| 64 | if (SDL_motion_manager.accelerometerAvailable) { | ||
| 65 | SDL_sensors[i].type = SDL_SENSOR_ACCEL; | ||
| 66 | SDL_sensors[i].instance_id = SDL_GetNextObjectID(); | ||
| 67 | ++i; | ||
| 68 | } | ||
| 69 | if (SDL_motion_manager.gyroAvailable) { | ||
| 70 | SDL_sensors[i].type = SDL_SENSOR_GYRO; | ||
| 71 | SDL_sensors[i].instance_id = SDL_GetNextObjectID(); | ||
| 72 | ++i; | ||
| 73 | } | ||
| 74 | SDL_sensors_count = sensors_count; | ||
| 75 | } | ||
| 76 | return true; | ||
| 77 | } | ||
| 78 | |||
| 79 | static int SDL_COREMOTION_SensorGetCount(void) | ||
| 80 | { | ||
| 81 | return SDL_sensors_count; | ||
| 82 | } | ||
| 83 | |||
| 84 | static void SDL_COREMOTION_SensorDetect(void) | ||
| 85 | { | ||
| 86 | } | ||
| 87 | |||
| 88 | static const char *SDL_COREMOTION_SensorGetDeviceName(int device_index) | ||
| 89 | { | ||
| 90 | switch (SDL_sensors[device_index].type) { | ||
| 91 | case SDL_SENSOR_ACCEL: | ||
| 92 | return "Accelerometer"; | ||
| 93 | case SDL_SENSOR_GYRO: | ||
| 94 | return "Gyro"; | ||
| 95 | default: | ||
| 96 | return "Unknown"; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | static SDL_SensorType SDL_COREMOTION_SensorGetDeviceType(int device_index) | ||
| 101 | { | ||
| 102 | return SDL_sensors[device_index].type; | ||
| 103 | } | ||
| 104 | |||
| 105 | static int SDL_COREMOTION_SensorGetDeviceNonPortableType(int device_index) | ||
| 106 | { | ||
| 107 | return SDL_sensors[device_index].type; | ||
| 108 | } | ||
| 109 | |||
| 110 | static SDL_SensorID SDL_COREMOTION_SensorGetDeviceInstanceID(int device_index) | ||
| 111 | { | ||
| 112 | return SDL_sensors[device_index].instance_id; | ||
| 113 | } | ||
| 114 | |||
| 115 | static bool SDL_COREMOTION_SensorOpen(SDL_Sensor *sensor, int device_index) | ||
| 116 | { | ||
| 117 | struct sensor_hwdata *hwdata; | ||
| 118 | |||
| 119 | hwdata = (struct sensor_hwdata *)SDL_calloc(1, sizeof(*hwdata)); | ||
| 120 | if (hwdata == NULL) { | ||
| 121 | return false; | ||
| 122 | } | ||
| 123 | sensor->hwdata = hwdata; | ||
| 124 | |||
| 125 | switch (sensor->type) { | ||
| 126 | case SDL_SENSOR_ACCEL: | ||
| 127 | [SDL_motion_manager startAccelerometerUpdates]; | ||
| 128 | break; | ||
| 129 | case SDL_SENSOR_GYRO: | ||
| 130 | [SDL_motion_manager startGyroUpdates]; | ||
| 131 | break; | ||
| 132 | default: | ||
| 133 | break; | ||
| 134 | } | ||
| 135 | return true; | ||
| 136 | } | ||
| 137 | |||
| 138 | static void SDL_COREMOTION_SensorUpdate(SDL_Sensor *sensor) | ||
| 139 | { | ||
| 140 | Uint64 timestamp = SDL_GetTicksNS(); | ||
| 141 | |||
| 142 | switch (sensor->type) { | ||
| 143 | case SDL_SENSOR_ACCEL: | ||
| 144 | { | ||
| 145 | CMAccelerometerData *accelerometerData = SDL_motion_manager.accelerometerData; | ||
| 146 | if (accelerometerData) { | ||
| 147 | CMAcceleration acceleration = accelerometerData.acceleration; | ||
| 148 | float data[3]; | ||
| 149 | data[0] = -acceleration.x * SDL_STANDARD_GRAVITY; | ||
| 150 | data[1] = -acceleration.y * SDL_STANDARD_GRAVITY; | ||
| 151 | data[2] = -acceleration.z * SDL_STANDARD_GRAVITY; | ||
| 152 | if (SDL_memcmp(data, sensor->hwdata->data, sizeof(data)) != 0) { | ||
| 153 | SDL_SendSensorUpdate(timestamp, sensor, timestamp, data, SDL_arraysize(data)); | ||
| 154 | SDL_memcpy(sensor->hwdata->data, data, sizeof(data)); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | } break; | ||
| 158 | case SDL_SENSOR_GYRO: | ||
| 159 | { | ||
| 160 | CMGyroData *gyroData = SDL_motion_manager.gyroData; | ||
| 161 | if (gyroData) { | ||
| 162 | CMRotationRate rotationRate = gyroData.rotationRate; | ||
| 163 | float data[3]; | ||
| 164 | data[0] = rotationRate.x; | ||
| 165 | data[1] = rotationRate.y; | ||
| 166 | data[2] = rotationRate.z; | ||
| 167 | if (SDL_memcmp(data, sensor->hwdata->data, sizeof(data)) != 0) { | ||
| 168 | SDL_SendSensorUpdate(timestamp, sensor, timestamp, data, SDL_arraysize(data)); | ||
| 169 | SDL_memcpy(sensor->hwdata->data, data, sizeof(data)); | ||
| 170 | } | ||
| 171 | } | ||
| 172 | } break; | ||
| 173 | default: | ||
| 174 | break; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | static void SDL_COREMOTION_SensorClose(SDL_Sensor *sensor) | ||
| 179 | { | ||
| 180 | if (sensor->hwdata) { | ||
| 181 | switch (sensor->type) { | ||
| 182 | case SDL_SENSOR_ACCEL: | ||
| 183 | [SDL_motion_manager stopAccelerometerUpdates]; | ||
| 184 | break; | ||
| 185 | case SDL_SENSOR_GYRO: | ||
| 186 | [SDL_motion_manager stopGyroUpdates]; | ||
| 187 | break; | ||
| 188 | default: | ||
| 189 | break; | ||
| 190 | } | ||
| 191 | SDL_free(sensor->hwdata); | ||
| 192 | sensor->hwdata = NULL; | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | static void SDL_COREMOTION_SensorQuit(void) | ||
| 197 | { | ||
| 198 | } | ||
| 199 | |||
| 200 | SDL_SensorDriver SDL_COREMOTION_SensorDriver = { | ||
| 201 | SDL_COREMOTION_SensorInit, | ||
| 202 | SDL_COREMOTION_SensorGetCount, | ||
| 203 | SDL_COREMOTION_SensorDetect, | ||
| 204 | SDL_COREMOTION_SensorGetDeviceName, | ||
| 205 | SDL_COREMOTION_SensorGetDeviceType, | ||
| 206 | SDL_COREMOTION_SensorGetDeviceNonPortableType, | ||
| 207 | SDL_COREMOTION_SensorGetDeviceInstanceID, | ||
| 208 | SDL_COREMOTION_SensorOpen, | ||
| 209 | SDL_COREMOTION_SensorUpdate, | ||
| 210 | SDL_COREMOTION_SensorClose, | ||
| 211 | SDL_COREMOTION_SensorQuit, | ||
| 212 | }; | ||
| 213 | |||
| 214 | #endif // SDL_SENSOR_COREMOTION | ||
