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