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 * Include file for SDL sensor event handling
26 *
27 */
28
29#ifndef SDL_sensor_h_
30#define SDL_sensor_h_
31
32#include <SDL3/SDL_stdinc.h>
33#include <SDL3/SDL_error.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 * \brief 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.
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 held in portrait mode and game controllers held in front of you,
94 * the axes are defined as follows:
95 * -X ... +X : left ... right
96 * -Y ... +Y : bottom ... top
97 * -Z ... +Z : farther ... closer
98 *
99 * The axis data is not changed when the phone is rotated.
100 *
101 * \sa SDL_GetDisplayOrientation()
102 */
103#define SDL_STANDARD_GRAVITY 9.80665f
104
105/**
106 * Gyroscope sensor
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,
110 * an observer looking from a positive location on one of the axes would
111 * see 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 held in portrait mode and game controllers held in front of you,
119 * the axes are defined as follows:
120 * -X ... +X : left ... right
121 * -Y ... +Y : bottom ... top
122 * -Z ... +Z : farther ... closer
123 *
124 * The axis data is not changed when the phone or controller is rotated.
125 *
126 * \sa SDL_GetDisplayOrientation()
127 */
128
129/* Function prototypes */
130
131/**
132 * Get a list of currently connected sensors.
133 *
134 * \param count a pointer filled in with the number of sensors returned
135 * \returns a 0 terminated array of sensor instance IDs which should be freed
136 * with SDL_free(), or NULL on error; call SDL_GetError() for more
137 * details.
138 *
139 * \since This function is available since SDL 3.0.0.
140 */
141extern DECLSPEC SDL_SensorID *SDLCALL SDL_GetSensors(int *count);
142
143/**
144 * Get the implementation dependent name of a sensor.
145 *
146 * \param instance_id the sensor instance ID
147 * \returns the sensor name, or NULL if `instance_id` is not valid
148 *
149 * \since This function is available since SDL 3.0.0.
150 */
151extern DECLSPEC const char *SDLCALL SDL_GetSensorInstanceName(SDL_SensorID instance_id);
152
153/**
154 * Get the type of a sensor.
155 *
156 * \param instance_id the sensor instance ID
157 * \returns the SDL_SensorType, or `SDL_SENSOR_INVALID` if `instance_id` is
158 * not valid
159 *
160 * \since This function is available since SDL 3.0.0.
161 */
162extern DECLSPEC SDL_SensorType SDLCALL SDL_GetSensorInstanceType(SDL_SensorID instance_id);
163
164/**
165 * Get the platform dependent type of a sensor.
166 *
167 * \param instance_id the sensor instance ID
168 * \returns the sensor platform dependent type, or -1 if `instance_id` is not
169 * valid
170 *
171 * \since This function is available since SDL 3.0.0.
172 */
173extern DECLSPEC int SDLCALL SDL_GetSensorInstanceNonPortableType(SDL_SensorID instance_id);
174
175/**
176 * Open a sensor for use.
177 *
178 * \param instance_id the sensor instance ID
179 * \returns an SDL_Sensor sensor object, or NULL if an error occurred.
180 *
181 * \since This function is available since SDL 3.0.0.
182 */
183extern DECLSPEC SDL_Sensor *SDLCALL SDL_OpenSensor(SDL_SensorID instance_id);
184
185/**
186 * Return the SDL_Sensor associated with an instance ID.
187 *
188 * \param instance_id the sensor instance ID
189 * \returns an SDL_Sensor object.
190 *
191 * \since This function is available since SDL 3.0.0.
192 */
193extern DECLSPEC SDL_Sensor *SDLCALL SDL_GetSensorFromInstanceID(SDL_SensorID instance_id);
194
195/**
196 * Get the implementation dependent name of a sensor
197 *
198 * \param sensor The SDL_Sensor object
199 * \returns the sensor name, or NULL if `sensor` is NULL.
200 *
201 * \since This function is available since SDL 3.0.0.
202 */
203extern DECLSPEC const char *SDLCALL SDL_GetSensorName(SDL_Sensor *sensor);
204
205/**
206 * Get the type of a sensor.
207 *
208 * \param sensor The SDL_Sensor object to inspect
209 * \returns the SDL_SensorType type, or `SDL_SENSOR_INVALID` if `sensor` is
210 * NULL.
211 *
212 * \since This function is available since SDL 3.0.0.
213 */
214extern DECLSPEC SDL_SensorType SDLCALL SDL_GetSensorType(SDL_Sensor *sensor);
215
216/**
217 * Get the platform dependent type of a sensor.
218 *
219 * \param sensor The SDL_Sensor object to inspect
220 * \returns the sensor platform dependent type, or -1 if `sensor` is NULL.
221 *
222 * \since This function is available since SDL 3.0.0.
223 */
224extern DECLSPEC int SDLCALL SDL_GetSensorNonPortableType(SDL_Sensor *sensor);
225
226/**
227 * Get the instance ID of a sensor.
228 *
229 * \param sensor The SDL_Sensor object to inspect
230 * \returns the sensor instance ID, or 0 if `sensor` is NULL.
231 *
232 * \since This function is available since SDL 3.0.0.
233 */
234extern DECLSPEC SDL_SensorID SDLCALL SDL_GetSensorInstanceID(SDL_Sensor *sensor);
235
236/**
237 * Get the current state of an opened sensor.
238 *
239 * The number of values and interpretation of the data is sensor dependent.
240 *
241 * \param sensor The SDL_Sensor object to query
242 * \param data A pointer filled with the current sensor state
243 * \param num_values The number of values to write to data
244 * \returns 0 or -1 if an error occurred.
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_ */
GLuint GLuint GLsizei count
Definition: SDL_opengl.h:1564
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1967
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_Sensor * SDL_GetSensorFromInstanceID(SDL_SensorID instance_id)
uint32_t Uint32
Definition: SDL_stdinc.h:171