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