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  * Include file for SDL sensor event handling
26  */
27 
28 #ifndef SDL_sensor_h_
29 #define SDL_sensor_h_
30 
31 #include "SDL_stdinc.h"
32 #include "SDL_error.h"
33 
34 #include "begin_code.h"
35 /* Set up for C function definitions, even when using C++ */
36 #ifdef __cplusplus
37 /* *INDENT-OFF* */
38 extern "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 
50 struct _SDL_Sensor;
51 typedef struct _SDL_Sensor SDL_Sensor;
52 
53 /**
54  * This is a unique ID for a sensor for the time it is connected to the
55  * system, and is never reused for the lifetime of the application.
56  *
57  * The ID value starts at 0 and increments from there. The value -1 is an
58  * invalid ID.
59  */
60 typedef Sint32 SDL_SensorID;
61 
62 /**
63  * The different sensors defined by SDL.
64  *
65  * Additional sensors may be available, using platform dependent semantics.
66  *
67  * Here are the additional Android sensors:
68  *
69  * https://developer.android.com/reference/android/hardware/SensorEvent.html#values
70  *
71  * Accelerometer sensor notes:
72  *
73  * The accelerometer returns the current acceleration in SI meters per second
74  * squared. This measurement includes the force of gravity, so a device at
75  * rest will have an value of SDL_STANDARD_GRAVITY away from the center of the
76  * earth, which is a positive Y value.
77  *
78  * - `values[0]`: Acceleration on the x axis
79  * - `values[1]`: Acceleration on the y axis
80  * - `values[2]`: Acceleration on the z axis
81  *
82  * For phones and tablets held in natural orientation and game controllers
83  * held in front of you, the axes are defined as follows:
84  *
85  * - -X ... +X : left ... right
86  * - -Y ... +Y : bottom ... top
87  * - -Z ... +Z : farther ... closer
88  *
89  * The accelerometer axis data is not changed when the device is rotated.
90  *
91  * Gyroscope sensor notes:
92  *
93  * The gyroscope returns the current rate of rotation in radians per second.
94  * The rotation is positive in the counter-clockwise direction. That is, an
95  * observer looking from a positive location on one of the axes would see
96  * positive rotation on that axis when it appeared to be rotating
97  * counter-clockwise.
98  *
99  * - `values[0]`: Angular speed around the x axis (pitch)
100  * - `values[1]`: Angular speed around the y axis (yaw)
101  * - `values[2]`: Angular speed around the z axis (roll)
102  *
103  * For phones and tablets held in natural orientation and game controllers
104  * held in front of you, the axes are defined as follows:
105  *
106  * - -X ... +X : left ... right
107  * - -Y ... +Y : bottom ... top
108  * - -Z ... +Z : farther ... closer
109  *
110  * The gyroscope axis data is not changed when the device is rotated.
111  *
112  * \sa SDL_GetDisplayOrientation
113  */
114 typedef enum SDL_SensorType
115 {
116     SDL_SENSOR_INVALID = -1,    /**< Returned for an invalid sensor */
117     SDL_SENSOR_UNKNOWN,         /**< Unknown sensor type */
118     SDL_SENSOR_ACCEL,           /**< Accelerometer */
119     SDL_SENSOR_GYRO,            /**< Gyroscope */
120     SDL_SENSOR_ACCEL_L,         /**< Accelerometer for left Joy-Con controller and Wii nunchuk */
121     SDL_SENSOR_GYRO_L,          /**< Gyroscope for left Joy-Con controller */
122     SDL_SENSOR_ACCEL_R,         /**< Accelerometer for right Joy-Con controller */
123     SDL_SENSOR_GYRO_R           /**< Gyroscope for right Joy-Con controller */
124 } SDL_SensorType;
125 
126 /**
127  * A constant to represent standard gravity for accelerometer sensors.
128  *
129  * The accelerometer returns the current acceleration in SI meters per second
130  * squared. This measurement includes the force of gravity, so a device at
131  * rest will have an value of SDL_STANDARD_GRAVITY away from the center of the
132  * earth, which is a positive Y value.
133  */
134 #define SDL_STANDARD_GRAVITY    9.80665f
135 
136 /* Function prototypes */
137 
138 /**
139  * Locking for multi-threaded access to the sensor API
140  *
141  * If you are using the sensor API or handling events from multiple threads
142  * you should use these locking functions to protect access to the sensors.
143  *
144  * In particular, you are guaranteed that the sensor list won't change, so the
145  * API functions that take a sensor index will be valid, and sensor events
146  * will not be delivered.
147  *
148  * \since This function is available since SDL 2.0.14.
149  */
150 extern DECLSPEC void SDLCALL SDL_LockSensors(void);
151 extern DECLSPEC void SDLCALL SDL_UnlockSensors(void);
152 
153 /**
154  * Count the number of sensors attached to the system right now.
155  *
156  * \returns the number of sensors detected.
157  *
158  * \since This function is available since SDL 2.0.9.
159  */
160 extern DECLSPEC int SDLCALL SDL_NumSensors(void);
161 
162 /**
163  * Get the implementation dependent name of a sensor.
164  *
165  * \param device_index The sensor to obtain name from.
166  * \returns the sensor name, or NULL if `device_index` is out of range.
167  *
168  * \since This function is available since SDL 2.0.9.
169  */
170 extern DECLSPEC const char *SDLCALL SDL_SensorGetDeviceName(int device_index);
171 
172 /**
173  * Get the type of a sensor.
174  *
175  * \param device_index The sensor to get the type from.
176  * \returns the SDL_SensorType, or `SDL_SENSOR_INVALID` if `device_index` is
177  *          out of range.
178  *
179  * \since This function is available since SDL 2.0.9.
180  */
181 extern DECLSPEC SDL_SensorType SDLCALL SDL_SensorGetDeviceType(int device_index);
182 
183 /**
184  * Get the platform dependent type of a sensor.
185  *
186  * \param device_index The sensor to check.
187  * \returns the sensor platform dependent type, or -1 if `device_index` is out
188  *          of range.
189  *
190  * \since This function is available since SDL 2.0.9.
191  */
192 extern DECLSPEC int SDLCALL SDL_SensorGetDeviceNonPortableType(int device_index);
193 
194 /**
195  * Get the instance ID of a sensor.
196  *
197  * \param device_index The sensor to get instance id from.
198  * \returns the sensor instance ID, or -1 if `device_index` is out of range.
199  *
200  * \since This function is available since SDL 2.0.9.
201  */
202 extern DECLSPEC SDL_SensorID SDLCALL SDL_SensorGetDeviceInstanceID(int device_index);
203 
204 /**
205  * Open a sensor for use.
206  *
207  * \param device_index The sensor to open.
208  * \returns an SDL_Sensor sensor object, or NULL if an error occurred.
209  *
210  * \since This function is available since SDL 2.0.9.
211  */
212 extern DECLSPEC SDL_Sensor *SDLCALL SDL_SensorOpen(int device_index);
213 
214 /**
215  * Return the SDL_Sensor associated with an instance id.
216  *
217  * \param instance_id The sensor from instance id.
218  * \returns an SDL_Sensor object.
219  *
220  * \since This function is available since SDL 2.0.9.
221  */
222 extern DECLSPEC SDL_Sensor *SDLCALL SDL_SensorFromInstanceID(SDL_SensorID instance_id);
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 2.0.9.
231  */
232 extern DECLSPEC const char *SDLCALL SDL_SensorGetName(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 2.0.9.
242  */
243 extern DECLSPEC SDL_SensorType SDLCALL SDL_SensorGetType(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 2.0.9.
252  */
253 extern DECLSPEC int SDLCALL SDL_SensorGetNonPortableType(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 -1 if `sensor` is NULL.
260  *
261  * \since This function is available since SDL 2.0.9.
262  */
263 extern DECLSPEC SDL_SensorID SDLCALL SDL_SensorGetInstanceID(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 or -1 if an error occurred.
274  *
275  * \since This function is available since SDL 2.0.9.
276  */
277 extern DECLSPEC int SDLCALL SDL_SensorGetData(SDL_Sensor *sensor, float *data, int num_values);
278 
279 /**
280  * Get the current state of an opened sensor with the timestamp of the last
281  * update.
282  *
283  * The number of values and interpretation of the data is sensor dependent.
284  *
285  * \param sensor The SDL_Sensor object to query.
286  * \param timestamp A pointer filled with the timestamp in microseconds of the
287  *                  current sensor reading if available, or 0 if not.
288  * \param data A pointer filled with the current sensor state.
289  * \param num_values The number of values to write to data.
290  * \returns 0 or -1 if an error occurred.
291  *
292  * \since This function is available since SDL 2.26.0.
293  */
294 extern DECLSPEC int SDLCALL SDL_SensorGetDataWithTimestamp(SDL_Sensor *sensor, Uint64 *timestamp, float *data, int num_values);
295 
296 /**
297  * Close a sensor previously opened with SDL_SensorOpen().
298  *
299  * \param sensor The SDL_Sensor object to close.
300  *
301  * \since This function is available since SDL 2.0.9.
302  */
303 extern DECLSPEC void SDLCALL SDL_SensorClose(SDL_Sensor *sensor);
304 
305 /**
306  * Update the current state of the open sensors.
307  *
308  * This is called automatically by the event loop if sensor events are
309  * enabled.
310  *
311  * This needs to be called from the thread that initialized the sensor
312  * subsystem.
313  *
314  * \since This function is available since SDL 2.0.9.
315  */
316 extern DECLSPEC void SDLCALL SDL_SensorUpdate(void);
317 
318 
319 /* Ends C function definitions when using C++ */
320 #ifdef __cplusplus
321 /* *INDENT-OFF* */
322 }
323 /* *INDENT-ON* */
324 #endif
325 #include "close_code.h"
326 
327 #endif /* SDL_sensor_h_ */
328 
329 /* vi: set ts=4 sw=4 expandtab: */