SDL 3.0
SDL_sensor.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2024 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/**
23 * \file SDL_sensor.h
24 *
25 * Include file for SDL sensor event handling
26 */
27
28#ifndef SDL_sensor_h_
29#define SDL_sensor_h_
30
31#include <SDL3/SDL_stdinc.h>
32#include <SDL3/SDL_error.h>
33#include <SDL3/SDL_properties.h>
34
35#include <SDL3/SDL_begin_code.h>
36/* Set up for C function definitions, even when using C++ */
37#ifdef __cplusplus
38/* *INDENT-OFF* */
39extern "C" {
40/* *INDENT-ON* */
41#endif
42
43/**
44 * SDL_sensor.h
45 *
46 * In order to use these functions, SDL_Init() must have been called
47 * with the ::SDL_INIT_SENSOR flag. This causes SDL to scan the system
48 * for sensors, and load appropriate drivers.
49 */
50
51struct SDL_Sensor;
52typedef struct SDL_Sensor SDL_Sensor;
53
54/**
55 * This is a unique ID for a sensor for the time it is connected to the system,
56 * and is never reused for the lifetime of the application.
57 *
58 * The ID value starts at 1 and increments from there. The value 0 is an invalid ID.
59 */
61
62/* The different sensors defined by SDL
63 *
64 * Additional sensors may be available, using platform dependent semantics.
65 *
66 * Hare are the additional Android sensors:
67 * https://developer.android.com/reference/android/hardware/SensorEvent.html#values
68 */
69typedef enum
70{
71 SDL_SENSOR_INVALID = -1, /**< Returned for an invalid sensor */
72 SDL_SENSOR_UNKNOWN, /**< Unknown sensor type */
73 SDL_SENSOR_ACCEL, /**< Accelerometer */
74 SDL_SENSOR_GYRO, /**< Gyroscope */
75 SDL_SENSOR_ACCEL_L, /**< Accelerometer for left Joy-Con controller and Wii nunchuk */
76 SDL_SENSOR_GYRO_L, /**< Gyroscope for left Joy-Con controller */
77 SDL_SENSOR_ACCEL_R, /**< Accelerometer for right Joy-Con controller */
78 SDL_SENSOR_GYRO_R /**< Gyroscope for right Joy-Con controller */
80
81/**
82 * Accelerometer sensor
83 *
84 * The accelerometer returns the current acceleration in SI meters per
85 * second squared. This measurement includes the force of gravity, so
86 * a device at rest will have an value of SDL_STANDARD_GRAVITY away
87 * from the center of the earth, which is a positive Y value.
88 *
89 * values[0]: Acceleration on the x axis
90 * values[1]: Acceleration on the y axis
91 * values[2]: Acceleration on the z axis
92 *
93 * For phones and tablets held in natural orientation and game controllers held in front of you, the axes are defined as follows:
94 * -X ... +X : left ... right
95 * -Y ... +Y : bottom ... top
96 * -Z ... +Z : farther ... closer
97 *
98 * The axis data is not changed when the device is rotated.
99 *
100 * \sa SDL_GetCurrentDisplayOrientation()
101 */
102#define SDL_STANDARD_GRAVITY 9.80665f
103
104/**
105 * Gyroscope sensor
106 *
107 * The gyroscope returns the current rate of rotation in radians per second.
108 * The rotation is positive in the counter-clockwise direction. That is,
109 * an observer looking from a positive location on one of the axes would
110 * see positive rotation on that axis when it appeared to be rotating
111 * counter-clockwise.
112 *
113 * values[0]: Angular speed around the x axis (pitch)
114 * values[1]: Angular speed around the y axis (yaw)
115 * values[2]: Angular speed around the z axis (roll)
116 *
117 * For phones and tablets held in natural orientation and game controllers held in front of you, the axes are defined as follows:
118 * -X ... +X : left ... right
119 * -Y ... +Y : bottom ... top
120 * -Z ... +Z : farther ... closer
121 *
122 * The axis data is not changed when the device is rotated.
123 *
124 * \sa SDL_GetCurrentDisplayOrientation()
125 */
126
127/* Function prototypes */
128
129/**
130 * Get a list of currently connected sensors.
131 *
132 * \param count a pointer filled in with the number of sensors returned
133 * \returns a 0 terminated array of sensor instance IDs which should be freed
134 * with SDL_free(), or NULL on error; call SDL_GetError() for more
135 * details.
136 *
137 * \since This function is available since SDL 3.0.0.
138 */
139extern DECLSPEC SDL_SensorID *SDLCALL SDL_GetSensors(int *count);
140
141/**
142 * Get the implementation dependent name of a sensor.
143 *
144 * \param instance_id the sensor instance ID
145 * \returns the sensor name, or NULL if `instance_id` is not valid
146 *
147 * \since This function is available since SDL 3.0.0.
148 */
149extern DECLSPEC const char *SDLCALL SDL_GetSensorInstanceName(SDL_SensorID instance_id);
150
151/**
152 * Get the type of a sensor.
153 *
154 * \param instance_id the sensor instance ID
155 * \returns the SDL_SensorType, or `SDL_SENSOR_INVALID` if `instance_id` is
156 * not valid
157 *
158 * \since This function is available since SDL 3.0.0.
159 */
160extern DECLSPEC SDL_SensorType SDLCALL SDL_GetSensorInstanceType(SDL_SensorID instance_id);
161
162/**
163 * Get the platform dependent type of a sensor.
164 *
165 * \param instance_id the sensor instance ID
166 * \returns the sensor platform dependent type, or -1 if `instance_id` is not
167 * valid
168 *
169 * \since This function is available since SDL 3.0.0.
170 */
171extern DECLSPEC int SDLCALL SDL_GetSensorInstanceNonPortableType(SDL_SensorID instance_id);
172
173/**
174 * Open a sensor for use.
175 *
176 * \param instance_id the sensor instance ID
177 * \returns an SDL_Sensor sensor object, or NULL if an error occurred.
178 *
179 * \since This function is available since SDL 3.0.0.
180 */
181extern DECLSPEC SDL_Sensor *SDLCALL SDL_OpenSensor(SDL_SensorID instance_id);
182
183/**
184 * Return the SDL_Sensor associated with an instance ID.
185 *
186 * \param instance_id the sensor instance ID
187 * \returns an SDL_Sensor object.
188 *
189 * \since This function is available since SDL 3.0.0.
190 */
191extern DECLSPEC SDL_Sensor *SDLCALL SDL_GetSensorFromInstanceID(SDL_SensorID instance_id);
192
193/**
194 * Get the properties associated with a sensor.
195 *
196 * \param sensor The SDL_Sensor object
197 * \returns a valid property ID on success or 0 on failure; call
198 * SDL_GetError() for more information.
199 *
200 * \since This function is available since SDL 3.0.0.
201 *
202 * \sa SDL_GetProperty
203 * \sa SDL_SetProperty
204 */
205extern DECLSPEC SDL_PropertiesID SDLCALL SDL_GetSensorProperties(SDL_Sensor *sensor);
206
207/**
208 * Get the implementation dependent name of a sensor
209 *
210 * \param sensor The SDL_Sensor object
211 * \returns the sensor name, or NULL if `sensor` is NULL.
212 *
213 * \since This function is available since SDL 3.0.0.
214 */
215extern DECLSPEC const char *SDLCALL SDL_GetSensorName(SDL_Sensor *sensor);
216
217/**
218 * Get the type of a sensor.
219 *
220 * \param sensor The SDL_Sensor object to inspect
221 * \returns the SDL_SensorType type, or `SDL_SENSOR_INVALID` if `sensor` is
222 * NULL.
223 *
224 * \since This function is available since SDL 3.0.0.
225 */
226extern DECLSPEC SDL_SensorType SDLCALL SDL_GetSensorType(SDL_Sensor *sensor);
227
228/**
229 * Get the platform dependent type of a sensor.
230 *
231 * \param sensor The SDL_Sensor object to inspect
232 * \returns the sensor platform dependent type, or -1 if `sensor` is NULL.
233 *
234 * \since This function is available since SDL 3.0.0.
235 */
236extern DECLSPEC int SDLCALL SDL_GetSensorNonPortableType(SDL_Sensor *sensor);
237
238/**
239 * Get the instance ID of a sensor.
240 *
241 * \param sensor The SDL_Sensor object to inspect
242 * \returns the sensor instance ID, or 0 if `sensor` is NULL.
243 *
244 * \since This function is available since SDL 3.0.0.
245 */
246extern DECLSPEC SDL_SensorID SDLCALL SDL_GetSensorInstanceID(SDL_Sensor *sensor);
247
248/**
249 * Get the current state of an opened sensor.
250 *
251 * The number of values and interpretation of the data is sensor dependent.
252 *
253 * \param sensor The SDL_Sensor object to query
254 * \param data A pointer filled with the current sensor state
255 * \param num_values The number of values to write to data
256 * \returns 0 on success or a negative error code on failure; call
257 * SDL_GetError() for more information.
258 *
259 * \since This function is available since SDL 3.0.0.
260 */
261extern DECLSPEC int SDLCALL SDL_GetSensorData(SDL_Sensor *sensor, float *data, int num_values);
262
263/**
264 * Close a sensor previously opened with SDL_OpenSensor().
265 *
266 * \param sensor The SDL_Sensor object to close
267 *
268 * \since This function is available since SDL 3.0.0.
269 */
270extern DECLSPEC void SDLCALL SDL_CloseSensor(SDL_Sensor *sensor);
271
272/**
273 * Update the current state of the open sensors.
274 *
275 * This is called automatically by the event loop if sensor events are
276 * enabled.
277 *
278 * This needs to be called from the thread that initialized the sensor
279 * subsystem.
280 *
281 * \since This function is available since SDL 3.0.0.
282 */
283extern DECLSPEC void SDLCALL SDL_UpdateSensors(void);
284
285
286/* Ends C function definitions when using C++ */
287#ifdef __cplusplus
288/* *INDENT-OFF* */
289}
290/* *INDENT-ON* */
291#endif
292#include <SDL3/SDL_close_code.h>
293
294#endif /* SDL_sensor_h_ */
Uint32 SDL_PropertiesID
SDL_SensorType SDL_GetSensorType(SDL_Sensor *sensor)
SDL_Sensor * SDL_OpenSensor(SDL_SensorID instance_id)
int SDL_GetSensorData(SDL_Sensor *sensor, float *data, int num_values)
int SDL_GetSensorNonPortableType(SDL_Sensor *sensor)
SDL_SensorType
Definition SDL_sensor.h:70
@ SDL_SENSOR_GYRO_L
Definition SDL_sensor.h:76
@ SDL_SENSOR_INVALID
Definition SDL_sensor.h:71
@ SDL_SENSOR_GYRO
Definition SDL_sensor.h:74
@ SDL_SENSOR_ACCEL_R
Definition SDL_sensor.h:77
@ SDL_SENSOR_UNKNOWN
Definition SDL_sensor.h:72
@ SDL_SENSOR_ACCEL
Definition SDL_sensor.h:73
@ SDL_SENSOR_ACCEL_L
Definition SDL_sensor.h:75
@ SDL_SENSOR_GYRO_R
Definition SDL_sensor.h:78
SDL_SensorID SDL_GetSensorInstanceID(SDL_Sensor *sensor)
SDL_SensorType SDL_GetSensorInstanceType(SDL_SensorID instance_id)
SDL_SensorID * SDL_GetSensors(int *count)
void SDL_CloseSensor(SDL_Sensor *sensor)
Uint32 SDL_SensorID
Definition SDL_sensor.h:60
const char * SDL_GetSensorInstanceName(SDL_SensorID instance_id)
int SDL_GetSensorInstanceNonPortableType(SDL_SensorID instance_id)
const char * SDL_GetSensorName(SDL_Sensor *sensor)
void SDL_UpdateSensors(void)
struct SDL_Sensor SDL_Sensor
Definition SDL_sensor.h:52
SDL_PropertiesID SDL_GetSensorProperties(SDL_Sensor *sensor)
SDL_Sensor * SDL_GetSensorFromInstanceID(SDL_SensorID instance_id)
uint32_t Uint32
Definition SDL_stdinc.h:173