comparison SDL3/SDL_sensor.h @ 1:20d02a178406 default tip

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