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_SensorType; |
143 | |
144 | |
145 | /* Function prototypes */ |
146 | |
147 | /** |
148 | * Get a list of currently connected sensors. |
149 | * |
150 | * \param count a pointer filled in with the number of sensors returned, may |
151 | * be NULL. |
152 | * \returns a 0 terminated array of sensor instance IDs or NULL on failure; |
153 | * call SDL_GetError() for more information. This should be freed |
154 | * with SDL_free() when it is no longer needed. |
155 | * |
156 | * \since This function is available since SDL 3.2.0. |
157 | */ |
158 | extern SDL_DECLSPEC SDL_SensorID * SDLCALL SDL_GetSensors(int *count); |
159 | |
160 | /** |
161 | * Get the implementation dependent name of a sensor. |
162 | * |
163 | * This can be called before any sensors are opened. |
164 | * |
165 | * \param instance_id the sensor instance ID. |
166 | * \returns the sensor name, or NULL if `instance_id` is not valid. |
167 | * |
168 | * \since This function is available since SDL 3.2.0. |
169 | */ |
170 | extern SDL_DECLSPEC const char * SDLCALL SDL_GetSensorNameForID(SDL_SensorID instance_id); |
171 | |
172 | /** |
173 | * Get the type of a sensor. |
174 | * |
175 | * This can be called before any sensors are opened. |
176 | * |
177 | * \param instance_id the sensor instance ID. |
178 | * \returns the SDL_SensorType, or `SDL_SENSOR_INVALID` if `instance_id` is |
179 | * not valid. |
180 | * |
181 | * \since This function is available since SDL 3.2.0. |
182 | */ |
183 | extern SDL_DECLSPEC SDL_SensorType SDLCALL SDL_GetSensorTypeForID(SDL_SensorID instance_id); |
184 | |
185 | /** |
186 | * Get the platform dependent type of a sensor. |
187 | * |
188 | * This can be called before any sensors are opened. |
189 | * |
190 | * \param instance_id the sensor instance ID. |
191 | * \returns the sensor platform dependent type, or -1 if `instance_id` is not |
192 | * valid. |
193 | * |
194 | * \since This function is available since SDL 3.2.0. |
195 | */ |
196 | extern SDL_DECLSPEC int SDLCALL SDL_GetSensorNonPortableTypeForID(SDL_SensorID instance_id); |
197 | |
198 | /** |
199 | * Open a sensor for use. |
200 | * |
201 | * \param instance_id the sensor instance ID. |
202 | * \returns an SDL_Sensor object or NULL on failure; call SDL_GetError() for |
203 | * more information. |
204 | * |
205 | * \since This function is available since SDL 3.2.0. |
206 | */ |
207 | extern SDL_DECLSPEC SDL_Sensor * SDLCALL SDL_OpenSensor(SDL_SensorID instance_id); |
208 | |
209 | /** |
210 | * Return the SDL_Sensor associated with an instance ID. |
211 | * |
212 | * \param instance_id the sensor instance ID. |
213 | * \returns an SDL_Sensor object or NULL on failure; call SDL_GetError() for |
214 | * more information. |
215 | * |
216 | * \since This function is available since SDL 3.2.0. |
217 | */ |
218 | extern SDL_DECLSPEC SDL_Sensor * SDLCALL SDL_GetSensorFromID(SDL_SensorID instance_id); |
219 | |
220 | /** |
221 | * Get the properties associated with a sensor. |
222 | * |
223 | * \param sensor the SDL_Sensor object. |
224 | * \returns a valid property ID on success or 0 on failure; call |
225 | * SDL_GetError() for more information. |
226 | * |
227 | * \since This function is available since SDL 3.2.0. |
228 | */ |
229 | extern SDL_DECLSPEC SDL_PropertiesID SDLCALL SDL_GetSensorProperties(SDL_Sensor *sensor); |
230 | |
231 | /** |
232 | * Get the implementation dependent name of a sensor. |
233 | * |
234 | * \param sensor the SDL_Sensor object. |
235 | * \returns the sensor name or NULL on failure; call SDL_GetError() for more |
236 | * information. |
237 | * |
238 | * \since This function is available since SDL 3.2.0. |
239 | */ |
240 | extern SDL_DECLSPEC const char * SDLCALL SDL_GetSensorName(SDL_Sensor *sensor); |
241 | |
242 | /** |
243 | * Get the type of a sensor. |
244 | * |
245 | * \param sensor the SDL_Sensor object to inspect. |
246 | * \returns the SDL_SensorType type, or `SDL_SENSOR_INVALID` if `sensor` is |
247 | * NULL. |
248 | * |
249 | * \since This function is available since SDL 3.2.0. |
250 | */ |
251 | extern SDL_DECLSPEC SDL_SensorType SDLCALL SDL_GetSensorType(SDL_Sensor *sensor); |
252 | |
253 | /** |
254 | * Get the platform dependent type of a sensor. |
255 | * |
256 | * \param sensor the SDL_Sensor object to inspect. |
257 | * \returns the sensor platform dependent type, or -1 if `sensor` is NULL. |
258 | * |
259 | * \since This function is available since SDL 3.2.0. |
260 | */ |
261 | extern SDL_DECLSPEC int SDLCALL SDL_GetSensorNonPortableType(SDL_Sensor *sensor); |
262 | |
263 | /** |
264 | * Get the instance ID of a sensor. |
265 | * |
266 | * \param sensor the SDL_Sensor object to inspect. |
267 | * \returns the sensor instance ID, or 0 on failure; call SDL_GetError() for |
268 | * more information. |
269 | * |
270 | * \since This function is available since SDL 3.2.0. |
271 | */ |
272 | extern SDL_DECLSPEC SDL_SensorID SDLCALL SDL_GetSensorID(SDL_Sensor *sensor); |
273 | |
274 | /** |
275 | * Get the current state of an opened sensor. |
276 | * |
277 | * The number of values and interpretation of the data is sensor dependent. |
278 | * |
279 | * \param sensor the SDL_Sensor object to query. |
280 | * \param data a pointer filled with the current sensor state. |
281 | * \param num_values the number of values to write to data. |
282 | * \returns true on success or false on failure; call SDL_GetError() for more |
283 | * information. |
284 | * |
285 | * \since This function is available since SDL 3.2.0. |
286 | */ |
287 | extern SDL_DECLSPEC bool SDLCALL SDL_GetSensorData(SDL_Sensor *sensor, float *data, int num_values); |
288 | |
289 | /** |
290 | * Close a sensor previously opened with SDL_OpenSensor(). |
291 | * |
292 | * \param sensor the SDL_Sensor object to close. |
293 | * |
294 | * \since This function is available since SDL 3.2.0. |
295 | */ |
296 | extern SDL_DECLSPEC void SDLCALL SDL_CloseSensor(SDL_Sensor *sensor); |
297 | |
298 | /** |
299 | * Update the current state of the open sensors. |
300 | * |
301 | * This is called automatically by the event loop if sensor events are |
302 | * enabled. |
303 | * |
304 | * This needs to be called from the thread that initialized the sensor |
305 | * subsystem. |
306 | * |
307 | * \since This function is available since SDL 3.2.0. |
308 | */ |
309 | extern SDL_DECLSPEC void SDLCALL SDL_UpdateSensors(void); |
310 | |
311 | |
312 | /* Ends C function definitions when using C++ */ |
313 | #ifdef __cplusplus |
314 | /* *INDENT-OFF* */ |
315 | } |
316 | /* *INDENT-ON* */ |
317 | #endif |
318 | #include <SDL3/SDL_close_code.h> |
319 | |
320 | #endif /* SDL_sensor_h_ */ |
321 | |