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.
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 held in portrait mode and game controllers held in front of you,
93 * 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 phone is rotated.
99 *
100 * \sa SDL_GetDisplayOrientation()
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 held in portrait mode and game controllers held in front of you,
118 * the axes are defined as follows:
119 * -X ... +X : left ... right
120 * -Y ... +Y : bottom ... top
121 * -Z ... +Z : farther ... closer
122 *
123 * The axis data is not changed when the phone or controller is rotated.
124 *
125 * \sa SDL_GetDisplayOrientation()
126 */
127
128/* Function prototypes */
129
130/**
131 * Get a list of currently connected sensors.
132 *
133 * \param count a pointer filled in with the number of sensors returned
134 * \returns a 0 terminated array of sensor instance IDs which should be freed
135 * with SDL_free(), or NULL on error; call SDL_GetError() for more
136 * details.
137 *
138 * \since This function is available since SDL 3.0.0.
139 */
140extern DECLSPEC SDL_SensorID *SDLCALL SDL_GetSensors(int *count);
141
142/**
143 * Get the implementation dependent name of a sensor.
144 *
145 * \param instance_id the sensor instance ID
146 * \returns the sensor name, or NULL if `instance_id` is not valid
147 *
148 * \since This function is available since SDL 3.0.0.
149 */
150extern DECLSPEC const char *SDLCALL SDL_GetSensorInstanceName(SDL_SensorID instance_id);
151
152/**
153 * Get the type of a sensor.
154 *
155 * \param instance_id the sensor instance ID
156 * \returns the SDL_SensorType, or `SDL_SENSOR_INVALID` if `instance_id` is
157 * not valid
158 *
159 * \since This function is available since SDL 3.0.0.
160 */
161extern DECLSPEC SDL_SensorType SDLCALL SDL_GetSensorInstanceType(SDL_SensorID instance_id);
162
163/**
164 * Get the platform dependent type of a sensor.
165 *
166 * \param instance_id the sensor instance ID
167 * \returns the sensor platform dependent type, or -1 if `instance_id` is not
168 * valid
169 *
170 * \since This function is available since SDL 3.0.0.
171 */
172extern DECLSPEC int SDLCALL SDL_GetSensorInstanceNonPortableType(SDL_SensorID instance_id);
173
174/**
175 * Open a sensor for use.
176 *
177 * \param instance_id the sensor instance ID
178 * \returns an SDL_Sensor sensor object, or NULL if an error occurred.
179 *
180 * \since This function is available since SDL 3.0.0.
181 */
182extern DECLSPEC SDL_Sensor *SDLCALL SDL_OpenSensor(SDL_SensorID instance_id);
183
184/**
185 * Return the SDL_Sensor associated with an instance ID.
186 *
187 * \param instance_id the sensor instance ID
188 * \returns an SDL_Sensor object.
189 *
190 * \since This function is available since SDL 3.0.0.
191 */
192extern DECLSPEC SDL_Sensor *SDLCALL SDL_GetSensorFromInstanceID(SDL_SensorID instance_id);
193
194/**
195 * Get the implementation dependent name of a sensor
196 *
197 * \param sensor The SDL_Sensor object
198 * \returns the sensor name, or NULL if `sensor` is NULL.
199 *
200 * \since This function is available since SDL 3.0.0.
201 */
202extern DECLSPEC const char *SDLCALL SDL_GetSensorName(SDL_Sensor *sensor);
203
204/**
205 * Get the type of a sensor.
206 *
207 * \param sensor The SDL_Sensor object to inspect
208 * \returns the SDL_SensorType type, or `SDL_SENSOR_INVALID` if `sensor` is
209 * NULL.
210 *
211 * \since This function is available since SDL 3.0.0.
212 */
213extern DECLSPEC SDL_SensorType SDLCALL SDL_GetSensorType(SDL_Sensor *sensor);
214
215/**
216 * Get the platform dependent type of a sensor.
217 *
218 * \param sensor The SDL_Sensor object to inspect
219 * \returns the sensor platform dependent type, or -1 if `sensor` is NULL.
220 *
221 * \since This function is available since SDL 3.0.0.
222 */
223extern DECLSPEC int SDLCALL SDL_GetSensorNonPortableType(SDL_Sensor *sensor);
224
225/**
226 * Get the instance ID of a sensor.
227 *
228 * \param sensor The SDL_Sensor object to inspect
229 * \returns the sensor instance ID, or 0 if `sensor` is NULL.
230 *
231 * \since This function is available since SDL 3.0.0.
232 */
233extern DECLSPEC SDL_SensorID SDLCALL SDL_GetSensorInstanceID(SDL_Sensor *sensor);
234
235/**
236 * Get the current state of an opened sensor.
237 *
238 * The number of values and interpretation of the data is sensor dependent.
239 *
240 * \param sensor The SDL_Sensor object to query
241 * \param data A pointer filled with the current sensor state
242 * \param num_values The number of values to write to data
243 * \returns 0 on success or a negative error code on failure; call
244 * SDL_GetError() for more information.
245 *
246 * \since This function is available since SDL 3.0.0.
247 */
248extern DECLSPEC int SDLCALL SDL_GetSensorData(SDL_Sensor *sensor, float *data, int num_values);
249
250/**
251 * Close a sensor previously opened with SDL_OpenSensor().
252 *
253 * \param sensor The SDL_Sensor object to close
254 *
255 * \since This function is available since SDL 3.0.0.
256 */
257extern DECLSPEC void SDLCALL SDL_CloseSensor(SDL_Sensor *sensor);
258
259/**
260 * Update the current state of the open sensors.
261 *
262 * This is called automatically by the event loop if sensor events are
263 * enabled.
264 *
265 * This needs to be called from the thread that initialized the sensor
266 * subsystem.
267 *
268 * \since This function is available since SDL 3.0.0.
269 */
270extern DECLSPEC void SDLCALL SDL_UpdateSensors(void);
271
272
273/* Ends C function definitions when using C++ */
274#ifdef __cplusplus
275/* *INDENT-OFF* */
276}
277/* *INDENT-ON* */
278#endif
279#include <SDL3/SDL_close_code.h>
280
281#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