Atlas - SDL_androidsensor.c

Home / ext / SDL2 / src / sensor / android Lines: 1 | Size: 5899 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2018 Sam Lantinga <[email protected]> 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 22#include "SDL_config.h" 23 24#ifdef SDL_SENSOR_ANDROID 25 26/* This is the system specific header for the SDL sensor API */ 27#include <android/sensor.h> 28 29#include "SDL_error.h" 30#include "SDL_sensor.h" 31#include "SDL_androidsensor.h" 32#include "../SDL_syssensor.h" 33#include "../SDL_sensor_c.h" 34//#include "../../core/android/SDL_android.h" 35 36#ifndef LOOPER_ID_USER 37#define LOOPER_ID_USER 3 38#endif 39 40typedef struct 41{ 42 ASensorRef asensor; 43 SDL_SensorID instance_id; 44} SDL_AndroidSensor; 45 46static ASensorManager* SDL_sensor_manager; 47static ALooper* SDL_sensor_looper; 48static SDL_AndroidSensor *SDL_sensors; 49static int SDL_sensors_count; 50 51static int 52SDL_ANDROID_SensorInit(void) 53{ 54 int i, sensors_count; 55 ASensorList sensors; 56 57 SDL_sensor_manager = ASensorManager_getInstance(); 58 if (!SDL_sensor_manager) { 59 return SDL_SetError("Couldn't create sensor manager"); 60 } 61 62 SDL_sensor_looper = ALooper_forThread(); 63 if (!SDL_sensor_looper) { 64 SDL_sensor_looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS); 65 if (!SDL_sensor_looper) { 66 return SDL_SetError("Couldn't create sensor event loop"); 67 } 68 } 69 70 /* FIXME: Is the sensor list dynamic? */ 71 sensors_count = ASensorManager_getSensorList(SDL_sensor_manager, &sensors); 72 if (sensors_count > 0) { 73 SDL_sensors = (SDL_AndroidSensor *)SDL_calloc(sensors_count, sizeof(*SDL_sensors)); 74 if (!SDL_sensors) { 75 return SDL_OutOfMemory(); 76 } 77 78 for (i = 0; i < sensors_count; ++i) { 79 SDL_sensors[i].asensor = sensors[i]; 80 SDL_sensors[i].instance_id = SDL_GetNextSensorInstanceID(); 81 } 82 SDL_sensors_count = sensors_count; 83 } 84 return 0; 85} 86 87static int 88SDL_ANDROID_SensorGetCount(void) 89{ 90 return SDL_sensors_count; 91} 92 93static void 94SDL_ANDROID_SensorDetect(void) 95{ 96} 97 98static const char * 99SDL_ANDROID_SensorGetDeviceName(int device_index) 100{ 101 return ASensor_getName(SDL_sensors[device_index].asensor); 102} 103 104static SDL_SensorType 105SDL_ANDROID_SensorGetDeviceType(int device_index) 106{ 107 switch (ASensor_getType(SDL_sensors[device_index].asensor)) { 108 case 0x00000001: 109 return SDL_SENSOR_ACCEL; 110 case 0x00000004: 111 return SDL_SENSOR_GYRO; 112 default: 113 return SDL_SENSOR_UNKNOWN; 114 } 115} 116 117static int 118SDL_ANDROID_SensorGetDeviceNonPortableType(int device_index) 119{ 120 return ASensor_getType(SDL_sensors[device_index].asensor); 121} 122 123static SDL_SensorID 124SDL_ANDROID_SensorGetDeviceInstanceID(int device_index) 125{ 126 return SDL_sensors[device_index].instance_id; 127} 128 129static int 130SDL_ANDROID_SensorOpen(SDL_Sensor *sensor, int device_index) 131{ 132 struct sensor_hwdata *hwdata; 133 134 hwdata = (struct sensor_hwdata *)SDL_calloc(1, sizeof(*hwdata)); 135 if (hwdata == NULL) { 136 return SDL_OutOfMemory(); 137 } 138 139 hwdata->asensor = SDL_sensors[device_index].asensor; 140 hwdata->eventqueue = ASensorManager_createEventQueue(SDL_sensor_manager, SDL_sensor_looper, LOOPER_ID_USER, NULL, NULL); 141 if (!hwdata->eventqueue) { 142 SDL_free(hwdata); 143 return SDL_SetError("Couldn't create sensor event queue"); 144 } 145 146 if (ASensorEventQueue_enableSensor(hwdata->eventqueue, hwdata->asensor) < 0) { 147 ASensorManager_destroyEventQueue(SDL_sensor_manager, hwdata->eventqueue); 148 SDL_free(hwdata); 149 return SDL_SetError("Couldn't enable sensor"); 150 } 151 152 /* FIXME: What rate should we set for this sensor? 60 FPS? Let's try the default rate for now... */ 153 154 sensor->hwdata = hwdata; 155 return 0; 156} 157 158static void 159SDL_ANDROID_SensorUpdate(SDL_Sensor *sensor) 160{ 161 int events; 162 ASensorEvent event; 163 struct android_poll_source* source; 164 165 if (ALooper_pollAll(0, NULL, &events, (void**)&source) == LOOPER_ID_USER) { 166 SDL_zero(event); 167 while (ASensorEventQueue_getEvents(sensor->hwdata->eventqueue, &event, 1) > 0) { 168 SDL_PrivateSensorUpdate(sensor, event.data, SDL_arraysize(event.data)); 169 } 170 } 171} 172 173static void 174SDL_ANDROID_SensorClose(SDL_Sensor *sensor) 175{ 176 if (sensor->hwdata) { 177 ASensorEventQueue_disableSensor(sensor->hwdata->eventqueue, sensor->hwdata->asensor); 178 ASensorManager_destroyEventQueue(SDL_sensor_manager, sensor->hwdata->eventqueue); 179 SDL_free(sensor->hwdata); 180 sensor->hwdata = NULL; 181 } 182} 183 184static void 185SDL_ANDROID_SensorQuit(void) 186{ 187 if (SDL_sensors) { 188 SDL_free(SDL_sensors); 189 SDL_sensors = NULL; 190 SDL_sensors_count = 0; 191 } 192} 193 194SDL_SensorDriver SDL_ANDROID_SensorDriver = 195{ 196 SDL_ANDROID_SensorInit, 197 SDL_ANDROID_SensorGetCount, 198 SDL_ANDROID_SensorDetect, 199 SDL_ANDROID_SensorGetDeviceName, 200 SDL_ANDROID_SensorGetDeviceType, 201 SDL_ANDROID_SensorGetDeviceNonPortableType, 202 SDL_ANDROID_SensorGetDeviceInstanceID, 203 SDL_ANDROID_SensorOpen, 204 SDL_ANDROID_SensorUpdate, 205 SDL_ANDROID_SensorClose, 206 SDL_ANDROID_SensorQuit, 207}; 208 209#endif /* SDL_SENSOR_ANDROID */ 210 211/* vi: set ts=4 sw=4 expandtab: */ 212
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.