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