SDL  2.0
SDL_androidsensor.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2020 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 
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 
40 typedef struct
41 {
42  ASensorRef asensor;
43  SDL_SensorID instance_id;
44 } SDL_AndroidSensor;
45 
46 static ASensorManager* SDL_sensor_manager;
47 static ALooper* SDL_sensor_looper;
48 static SDL_AndroidSensor *SDL_sensors;
49 static int SDL_sensors_count;
50 
51 static int
52 SDL_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 
87 static int
88 SDL_ANDROID_SensorGetCount(void)
89 {
90  return SDL_sensors_count;
91 }
92 
93 static void
94 SDL_ANDROID_SensorDetect(void)
95 {
96 }
97 
98 static const char *
99 SDL_ANDROID_SensorGetDeviceName(int device_index)
100 {
101  return ASensor_getName(SDL_sensors[device_index].asensor);
102 }
103 
104 static SDL_SensorType
105 SDL_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 
117 static int
118 SDL_ANDROID_SensorGetDeviceNonPortableType(int device_index)
119 {
120  return ASensor_getType(SDL_sensors[device_index].asensor);
121 }
122 
123 static SDL_SensorID
124 SDL_ANDROID_SensorGetDeviceInstanceID(int device_index)
125 {
126  return SDL_sensors[device_index].instance_id;
127 }
128 
129 static int
130 SDL_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 
158 static void
159 SDL_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 
173 static void
174 SDL_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 
184 static void
185 SDL_ANDROID_SensorQuit(void)
186 {
187  if (SDL_sensors) {
189  SDL_sensors = NULL;
190  SDL_sensors_count = 0;
191  }
192 }
193 
194 SDL_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: */
SDL_zero
#define SDL_zero(x)
Definition: SDL_stdinc.h:418
SDL_PrivateSensorUpdate
int SDL_PrivateSensorUpdate(SDL_Sensor *sensor, float *data, int num_values)
Definition: SDL_sensor.c:476
SDL_SENSOR_UNKNOWN
@ SDL_SENSOR_UNKNOWN
Definition: SDL_sensor.h:72
source
GLsizei GLsizei GLchar * source
Definition: SDL_opengl_glext.h:680
NULL
#define NULL
Definition: begin_code.h:167
SDL_error.h
SDL_androidsensor.h
sensor_hwdata::asensor
ASensorRef asensor
Definition: SDL_androidsensor.h:26
SDL_SENSOR_GYRO
@ SDL_SENSOR_GYRO
Definition: SDL_sensor.h:74
SDL_SensorType
SDL_SensorType
Definition: SDL_sensor.h:69
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2652
sensor_hwdata
Definition: SDL_androidsensor.h:24
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
SDL_sensor.h
SDL_OutOfMemory
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_ANDROID_SensorDriver
SDL_SensorDriver SDL_ANDROID_SensorDriver
SDL_arraysize
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:115
SDL_calloc
#define SDL_calloc
Definition: SDL_dynapi_overrides.h:375
events
static SDL_Event events[EVENT_BUF_SIZE]
Definition: testgesture.c:39
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30
SDL_sensors
static SDL_Sensor * SDL_sensors
Definition: SDL_sensor.c:46
SDL_GetNextSensorInstanceID
SDL_SensorID SDL_GetNextSensorInstanceID()
Definition: SDL_sensor.c:112
SDL_SENSOR_ACCEL
@ SDL_SENSOR_ACCEL
Definition: SDL_sensor.h:73
sensor_hwdata::eventqueue
ASensorEventQueue * eventqueue
Definition: SDL_androidsensor.h:27
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
SDL_SensorID
Sint32 SDL_SensorID
Definition: SDL_sensor.h:60