SDL 2.0
SDL_androidsensor.c
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2019 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
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];
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) {
167 while (ASensorEventQueue_getEvents(sensor->hwdata->eventqueue, &event, 1) > 0) {
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) {
190 SDL_sensors_count = 0;
191 }
192}
193
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: */
#define SDL_SetError
#define SDL_free
#define SDL_calloc
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
struct _cl_event * event
GLsizei GLsizei GLchar * source
static SDL_Sensor * SDL_sensors
Definition: SDL_sensor.c:46
SDL_SensorID SDL_GetNextSensorInstanceID()
Definition: SDL_sensor.c:112
int SDL_PrivateSensorUpdate(SDL_Sensor *sensor, float *data, int num_values)
Definition: SDL_sensor.c:476
Sint32 SDL_SensorID
Definition: SDL_sensor.h:60
SDL_SensorType
Definition: SDL_sensor.h:70
@ SDL_SENSOR_GYRO
Definition: SDL_sensor.h:74
@ SDL_SENSOR_UNKNOWN
Definition: SDL_sensor.h:72
@ SDL_SENSOR_ACCEL
Definition: SDL_sensor.h:73
#define SDL_zero(x)
Definition: SDL_stdinc.h:416
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:115
SDL_SensorDriver SDL_ANDROID_SensorDriver
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
#define NULL
Definition: begin_code.h:167
ASensorEventQueue * eventqueue
ASensorRef asensor
static SDL_Event events[EVENT_BUF_SIZE]
Definition: testgesture.c:39