SDL_sensor.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /**
  19. * # CategorySensor
  20. *
  21. * Include file for SDL sensor event handling
  22. */
  23. #ifndef SDL_sensor_h_
  24. #define SDL_sensor_h_
  25. #include "SDL_stdinc.h"
  26. #include "SDL_error.h"
  27. #include "begin_code.h"
  28. /* Set up for C function definitions, even when using C++ */
  29. #ifdef __cplusplus
  30. /* *INDENT-OFF* */
  31. extern "C" {
  32. /* *INDENT-ON* */
  33. #endif
  34. /**
  35. * \brief SDL_sensor.h
  36. *
  37. * In order to use these functions, SDL_Init() must have been called
  38. * with the SDL_INIT_SENSOR flag. This causes SDL to scan the system
  39. * for sensors, and load appropriate drivers.
  40. */
  41. struct _SDL_Sensor;
  42. typedef struct _SDL_Sensor SDL_Sensor;
  43. /**
  44. * This is a unique ID for a sensor for the time it is connected to the
  45. * system, and is never reused for the lifetime of the application.
  46. *
  47. * The ID value starts at 0 and increments from there. The value -1 is an
  48. * invalid ID.
  49. */
  50. typedef Sint32 SDL_SensorID;
  51. /**
  52. * The different sensors defined by SDL.
  53. *
  54. * Additional sensors may be available, using platform dependent semantics.
  55. *
  56. * Here are the additional Android sensors:
  57. *
  58. * https://developer.android.com/reference/android/hardware/SensorEvent.html#values
  59. *
  60. * Accelerometer sensor notes:
  61. *
  62. * The accelerometer returns the current acceleration in SI meters per second
  63. * squared. This measurement includes the force of gravity, so a device at
  64. * rest will have an value of SDL_STANDARD_GRAVITY away from the center of the
  65. * earth, which is a positive Y value.
  66. *
  67. * - `values[0]`: Acceleration on the x axis
  68. * - `values[1]`: Acceleration on the y axis
  69. * - `values[2]`: Acceleration on the z axis
  70. *
  71. * For phones and tablets held in natural orientation and game controllers
  72. * held in front of you, the axes are defined as follows:
  73. *
  74. * - -X ... +X : left ... right
  75. * - -Y ... +Y : bottom ... top
  76. * - -Z ... +Z : farther ... closer
  77. *
  78. * The accelerometer axis data is not changed when the device is rotated.
  79. *
  80. * Gyroscope sensor notes:
  81. *
  82. * The gyroscope returns the current rate of rotation in radians per second.
  83. * The rotation is positive in the counter-clockwise direction. That is, an
  84. * observer looking from a positive location on one of the axes would see
  85. * positive rotation on that axis when it appeared to be rotating
  86. * counter-clockwise.
  87. *
  88. * - `values[0]`: Angular speed around the x axis (pitch)
  89. * - `values[1]`: Angular speed around the y axis (yaw)
  90. * - `values[2]`: Angular speed around the z axis (roll)
  91. *
  92. * For phones and tablets held in natural orientation and game controllers
  93. * held in front of you, the axes are defined as follows:
  94. *
  95. * - -X ... +X : left ... right
  96. * - -Y ... +Y : bottom ... top
  97. * - -Z ... +Z : farther ... closer
  98. *
  99. * The gyroscope axis data is not changed when the device is rotated.
  100. *
  101. * \sa SDL_GetDisplayOrientation
  102. */
  103. typedef enum SDL_SensorType
  104. {
  105. SDL_SENSOR_INVALID = -1, /**< Returned for an invalid sensor */
  106. SDL_SENSOR_UNKNOWN, /**< Unknown sensor type */
  107. SDL_SENSOR_ACCEL, /**< Accelerometer */
  108. SDL_SENSOR_GYRO, /**< Gyroscope */
  109. SDL_SENSOR_ACCEL_L, /**< Accelerometer for left Joy-Con controller and Wii nunchuk */
  110. SDL_SENSOR_GYRO_L, /**< Gyroscope for left Joy-Con controller */
  111. SDL_SENSOR_ACCEL_R, /**< Accelerometer for right Joy-Con controller */
  112. SDL_SENSOR_GYRO_R /**< Gyroscope for right Joy-Con controller */
  113. } SDL_SensorType;
  114. /**
  115. * A constant to represent standard gravity for accelerometer sensors.
  116. *
  117. * The accelerometer returns the current acceleration in SI meters per second
  118. * squared. This measurement includes the force of gravity, so a device at
  119. * rest will have an value of SDL_STANDARD_GRAVITY away from the center of the
  120. * earth, which is a positive Y value.
  121. */
  122. #define SDL_STANDARD_GRAVITY 9.80665f
  123. /* Function prototypes */
  124. /**
  125. * Locking for multi-threaded access to the sensor API
  126. *
  127. * If you are using the sensor API or handling events from multiple threads
  128. * you should use these locking functions to protect access to the sensors.
  129. *
  130. * In particular, you are guaranteed that the sensor list won't change, so the
  131. * API functions that take a sensor index will be valid, and sensor events
  132. * will not be delivered.
  133. *
  134. * \since This function is available since SDL 2.0.14.
  135. */
  136. extern DECLSPEC void SDLCALL SDL_LockSensors(void);
  137. extern DECLSPEC void SDLCALL SDL_UnlockSensors(void);
  138. /**
  139. * Count the number of sensors attached to the system right now.
  140. *
  141. * \returns the number of sensors detected.
  142. *
  143. * \since This function is available since SDL 2.0.9.
  144. */
  145. extern DECLSPEC int SDLCALL SDL_NumSensors(void);
  146. /**
  147. * Get the implementation dependent name of a sensor.
  148. *
  149. * \param device_index The sensor to obtain name from.
  150. * \returns the sensor name, or NULL if `device_index` is out of range.
  151. *
  152. * \since This function is available since SDL 2.0.9.
  153. */
  154. extern DECLSPEC const char *SDLCALL SDL_SensorGetDeviceName(int device_index);
  155. /**
  156. * Get the type of a sensor.
  157. *
  158. * \param device_index The sensor to get the type from.
  159. * \returns the SDL_SensorType, or `SDL_SENSOR_INVALID` if `device_index` is
  160. * out of range.
  161. *
  162. * \since This function is available since SDL 2.0.9.
  163. */
  164. extern DECLSPEC SDL_SensorType SDLCALL SDL_SensorGetDeviceType(int device_index);
  165. /**
  166. * Get the platform dependent type of a sensor.
  167. *
  168. * \param device_index The sensor to check.
  169. * \returns the sensor platform dependent type, or -1 if `device_index` is out
  170. * of range.
  171. *
  172. * \since This function is available since SDL 2.0.9.
  173. */
  174. extern DECLSPEC int SDLCALL SDL_SensorGetDeviceNonPortableType(int device_index);
  175. /**
  176. * Get the instance ID of a sensor.
  177. *
  178. * \param device_index The sensor to get instance id from.
  179. * \returns the sensor instance ID, or -1 if `device_index` is out of range.
  180. *
  181. * \since This function is available since SDL 2.0.9.
  182. */
  183. extern DECLSPEC SDL_SensorID SDLCALL SDL_SensorGetDeviceInstanceID(int device_index);
  184. /**
  185. * Open a sensor for use.
  186. *
  187. * \param device_index The sensor to open.
  188. * \returns an SDL_Sensor sensor object, or NULL if an error occurred.
  189. *
  190. * \since This function is available since SDL 2.0.9.
  191. */
  192. extern DECLSPEC SDL_Sensor *SDLCALL SDL_SensorOpen(int device_index);
  193. /**
  194. * Return the SDL_Sensor associated with an instance id.
  195. *
  196. * \param instance_id The sensor from instance id.
  197. * \returns an SDL_Sensor object.
  198. *
  199. * \since This function is available since SDL 2.0.9.
  200. */
  201. extern DECLSPEC SDL_Sensor *SDLCALL SDL_SensorFromInstanceID(SDL_SensorID instance_id);
  202. /**
  203. * Get the implementation dependent name of a sensor
  204. *
  205. * \param sensor The SDL_Sensor object.
  206. * \returns the sensor name, or NULL if `sensor` is NULL.
  207. *
  208. * \since This function is available since SDL 2.0.9.
  209. */
  210. extern DECLSPEC const char *SDLCALL SDL_SensorGetName(SDL_Sensor *sensor);
  211. /**
  212. * Get the type of a sensor.
  213. *
  214. * \param sensor The SDL_Sensor object to inspect.
  215. * \returns the SDL_SensorType type, or `SDL_SENSOR_INVALID` if `sensor` is
  216. * NULL.
  217. *
  218. * \since This function is available since SDL 2.0.9.
  219. */
  220. extern DECLSPEC SDL_SensorType SDLCALL SDL_SensorGetType(SDL_Sensor *sensor);
  221. /**
  222. * Get the platform dependent type of a sensor.
  223. *
  224. * \param sensor The SDL_Sensor object to inspect.
  225. * \returns the sensor platform dependent type, or -1 if `sensor` is NULL.
  226. *
  227. * \since This function is available since SDL 2.0.9.
  228. */
  229. extern DECLSPEC int SDLCALL SDL_SensorGetNonPortableType(SDL_Sensor *sensor);
  230. /**
  231. * Get the instance ID of a sensor.
  232. *
  233. * \param sensor The SDL_Sensor object to inspect.
  234. * \returns the sensor instance ID, or -1 if `sensor` is NULL.
  235. *
  236. * \since This function is available since SDL 2.0.9.
  237. */
  238. extern DECLSPEC SDL_SensorID SDLCALL SDL_SensorGetInstanceID(SDL_Sensor *sensor);
  239. /**
  240. * Get the current state of an opened sensor.
  241. *
  242. * The number of values and interpretation of the data is sensor dependent.
  243. *
  244. * \param sensor The SDL_Sensor object to query.
  245. * \param data A pointer filled with the current sensor state.
  246. * \param num_values The number of values to write to data.
  247. * \returns 0 or -1 if an error occurred.
  248. *
  249. * \since This function is available since SDL 2.0.9.
  250. */
  251. extern DECLSPEC int SDLCALL SDL_SensorGetData(SDL_Sensor *sensor, float *data, int num_values);
  252. /**
  253. * Get the current state of an opened sensor with the timestamp of the last
  254. * update.
  255. *
  256. * The number of values and interpretation of the data is sensor dependent.
  257. *
  258. * \param sensor The SDL_Sensor object to query.
  259. * \param timestamp A pointer filled with the timestamp in microseconds of the
  260. * current sensor reading if available, or 0 if not.
  261. * \param data A pointer filled with the current sensor state.
  262. * \param num_values The number of values to write to data.
  263. * \returns 0 or -1 if an error occurred.
  264. *
  265. * \since This function is available since SDL 2.26.0.
  266. */
  267. extern DECLSPEC int SDLCALL SDL_SensorGetDataWithTimestamp(SDL_Sensor *sensor, Uint64 *timestamp, float *data, int num_values);
  268. /**
  269. * Close a sensor previously opened with SDL_SensorOpen().
  270. *
  271. * \param sensor The SDL_Sensor object to close.
  272. *
  273. * \since This function is available since SDL 2.0.9.
  274. */
  275. extern DECLSPEC void SDLCALL SDL_SensorClose(SDL_Sensor *sensor);
  276. /**
  277. * Update the current state of the open sensors.
  278. *
  279. * This is called automatically by the event loop if sensor events are
  280. * enabled.
  281. *
  282. * This needs to be called from the thread that initialized the sensor
  283. * subsystem.
  284. *
  285. * \since This function is available since SDL 2.0.9.
  286. */
  287. extern DECLSPEC void SDLCALL SDL_SensorUpdate(void);
  288. /* Ends C function definitions when using C++ */
  289. #ifdef __cplusplus
  290. /* *INDENT-OFF* */
  291. }
  292. /* *INDENT-ON* */
  293. #endif
  294. #include "close_code.h"
  295. #endif /* SDL_sensor_h_ */
  296. /* vi: set ts=4 sw=4 expandtab: */