SDL_sensor.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  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. * \file SDL_sensor.h
  20. *
  21. * Include file for SDL sensor event handling
  22. */
  23. #ifndef SDL_sensor_h_
  24. #define SDL_sensor_h_
  25. #include <SDL3/SDL_stdinc.h>
  26. #include <SDL3/SDL_error.h>
  27. #include <SDL3/SDL_properties.h>
  28. #include <SDL3/SDL_begin_code.h>
  29. /* Set up for C function definitions, even when using C++ */
  30. #ifdef __cplusplus
  31. /* *INDENT-OFF* */
  32. extern "C" {
  33. /* *INDENT-ON* */
  34. #endif
  35. /**
  36. * SDL_sensor.h
  37. *
  38. * In order to use these functions, SDL_Init() must have been called
  39. * with the ::SDL_INIT_SENSOR flag. This causes SDL to scan the system
  40. * for sensors, and load appropriate drivers.
  41. */
  42. struct SDL_Sensor;
  43. typedef struct SDL_Sensor SDL_Sensor;
  44. /**
  45. * This is a unique ID for a sensor for the time it is connected to the
  46. * system, and is never reused for the lifetime of the application.
  47. *
  48. * The ID value starts at 1 and increments from there. The value 0 is an
  49. * invalid ID.
  50. *
  51. * \since This datatype is available since SDL 3.0.0.
  52. */
  53. typedef Uint32 SDL_SensorID;
  54. /**
  55. * A constant to represent standard gravity for accelerometer sensors.
  56. *
  57. * The accelerometer returns the current acceleration in SI meters per second
  58. * squared. This measurement includes the force of gravity, so a device at
  59. * rest will have an value of SDL_STANDARD_GRAVITY away from the center of the
  60. * earth, which is a positive Y value.
  61. *
  62. * \since This macro is available since SDL 3.0.0.
  63. */
  64. #define SDL_STANDARD_GRAVITY 9.80665f
  65. /**
  66. * The different sensors defined by SDL.
  67. *
  68. * Additional sensors may be available, using platform dependent semantics.
  69. *
  70. * Hare are the additional Android sensors:
  71. * https://developer.android.com/reference/android/hardware/SensorEvent.html#values
  72. *
  73. * Accelerometer sensor notes:
  74. *
  75. * The accelerometer returns the current acceleration in SI meters per second
  76. * squared. This measurement includes the force of gravity, so a device at
  77. * rest will have an value of SDL_STANDARD_GRAVITY away from the center of the
  78. * earth, which is a positive Y value.
  79. *
  80. * - `values[0]`: Acceleration on the x axis
  81. * - `values[1]`: Acceleration on the y axis
  82. * - `values[2]`: Acceleration on the z axis
  83. *
  84. * For phones and tablets held in natural orientation and game controllers
  85. * held in front of you, the axes are defined as follows:
  86. *
  87. * - -X ... +X : left ... right
  88. * - -Y ... +Y : bottom ... top
  89. * - -Z ... +Z : farther ... closer
  90. *
  91. * The accelerometer axis data is not changed when the device is rotated.
  92. *
  93. * Gyroscope sensor notes:
  94. *
  95. * The gyroscope returns the current rate of rotation in radians per second.
  96. * The rotation is positive in the counter-clockwise direction. That is,
  97. * an observer looking from a positive location on one of the axes would
  98. * see positive rotation on that axis when it appeared to be rotating
  99. * counter-clockwise.
  100. *
  101. * - `values[0]`: Angular speed around the x axis (pitch)
  102. * - `values[1]`: Angular speed around the y axis (yaw)
  103. * - `values[2]`: Angular speed around the z axis (roll)
  104. *
  105. * For phones and tablets held in natural orientation and game controllers held in front of you, the axes are defined as follows:
  106. *
  107. * - -X ... +X : left ... right
  108. * - -Y ... +Y : bottom ... top
  109. * - -Z ... +Z : farther ... closer
  110. *
  111. * The gyroscope axis data is not changed when the device is rotated.
  112. *
  113. * \since This enum is available since SDL 3.0.0.
  114. *
  115. * \sa SDL_GetCurrentDisplayOrientation
  116. */
  117. typedef enum SDL_SensorType
  118. {
  119. SDL_SENSOR_INVALID = -1, /**< Returned for an invalid sensor */
  120. SDL_SENSOR_UNKNOWN, /**< Unknown sensor type */
  121. SDL_SENSOR_ACCEL, /**< Accelerometer */
  122. SDL_SENSOR_GYRO, /**< Gyroscope */
  123. SDL_SENSOR_ACCEL_L, /**< Accelerometer for left Joy-Con controller and Wii nunchuk */
  124. SDL_SENSOR_GYRO_L, /**< Gyroscope for left Joy-Con controller */
  125. SDL_SENSOR_ACCEL_R, /**< Accelerometer for right Joy-Con controller */
  126. SDL_SENSOR_GYRO_R /**< Gyroscope for right Joy-Con controller */
  127. } SDL_SensorType;
  128. /* Function prototypes */
  129. /**
  130. * Get a list of currently connected sensors.
  131. *
  132. * \param count a pointer filled in with the number of sensors returned
  133. * \returns a 0 terminated array of sensor instance IDs which should be freed
  134. * with SDL_free(), or NULL on error; call SDL_GetError() for more
  135. * details.
  136. *
  137. * \since This function is available since SDL 3.0.0.
  138. */
  139. extern DECLSPEC SDL_SensorID *SDLCALL SDL_GetSensors(int *count);
  140. /**
  141. * Get the implementation dependent name of a sensor.
  142. *
  143. * \param instance_id the sensor instance ID
  144. * \returns the sensor name, or NULL if `instance_id` is not valid
  145. *
  146. * \since This function is available since SDL 3.0.0.
  147. */
  148. extern DECLSPEC const char *SDLCALL SDL_GetSensorInstanceName(SDL_SensorID instance_id);
  149. /**
  150. * Get the type of a sensor.
  151. *
  152. * \param instance_id the sensor instance ID
  153. * \returns the SDL_SensorType, or `SDL_SENSOR_INVALID` if `instance_id` is
  154. * not valid
  155. *
  156. * \since This function is available since SDL 3.0.0.
  157. */
  158. extern DECLSPEC SDL_SensorType SDLCALL SDL_GetSensorInstanceType(SDL_SensorID instance_id);
  159. /**
  160. * Get the platform dependent type of a sensor.
  161. *
  162. * \param instance_id the sensor instance ID
  163. * \returns the sensor platform dependent type, or -1 if `instance_id` is not
  164. * valid
  165. *
  166. * \since This function is available since SDL 3.0.0.
  167. */
  168. extern DECLSPEC int SDLCALL SDL_GetSensorInstanceNonPortableType(SDL_SensorID instance_id);
  169. /**
  170. * Open a sensor for use.
  171. *
  172. * \param instance_id the sensor instance ID
  173. * \returns an SDL_Sensor sensor object, or NULL if an error occurred.
  174. *
  175. * \since This function is available since SDL 3.0.0.
  176. */
  177. extern DECLSPEC SDL_Sensor *SDLCALL SDL_OpenSensor(SDL_SensorID instance_id);
  178. /**
  179. * Return the SDL_Sensor associated with an instance ID.
  180. *
  181. * \param instance_id the sensor instance ID
  182. * \returns an SDL_Sensor object.
  183. *
  184. * \since This function is available since SDL 3.0.0.
  185. */
  186. extern DECLSPEC SDL_Sensor *SDLCALL SDL_GetSensorFromInstanceID(SDL_SensorID instance_id);
  187. /**
  188. * Get the properties associated with a sensor.
  189. *
  190. * \param sensor The SDL_Sensor object
  191. * \returns a valid property ID on success or 0 on failure; call
  192. * SDL_GetError() for more information.
  193. *
  194. * \since This function is available since SDL 3.0.0.
  195. *
  196. * \sa SDL_GetProperty
  197. * \sa SDL_SetProperty
  198. */
  199. extern DECLSPEC SDL_PropertiesID SDLCALL SDL_GetSensorProperties(SDL_Sensor *sensor);
  200. /**
  201. * Get the implementation dependent name of a sensor.
  202. *
  203. * \param sensor The SDL_Sensor object
  204. * \returns the sensor name, or NULL if `sensor` is NULL.
  205. *
  206. * \since This function is available since SDL 3.0.0.
  207. */
  208. extern DECLSPEC const char *SDLCALL SDL_GetSensorName(SDL_Sensor *sensor);
  209. /**
  210. * Get the type of a sensor.
  211. *
  212. * \param sensor The SDL_Sensor object to inspect
  213. * \returns the SDL_SensorType type, or `SDL_SENSOR_INVALID` if `sensor` is
  214. * NULL.
  215. *
  216. * \since This function is available since SDL 3.0.0.
  217. */
  218. extern DECLSPEC SDL_SensorType SDLCALL SDL_GetSensorType(SDL_Sensor *sensor);
  219. /**
  220. * Get the platform dependent type of a sensor.
  221. *
  222. * \param sensor The SDL_Sensor object to inspect
  223. * \returns the sensor platform dependent type, or -1 if `sensor` is NULL.
  224. *
  225. * \since This function is available since SDL 3.0.0.
  226. */
  227. extern DECLSPEC int SDLCALL SDL_GetSensorNonPortableType(SDL_Sensor *sensor);
  228. /**
  229. * Get the instance ID of a sensor.
  230. *
  231. * \param sensor The SDL_Sensor object to inspect
  232. * \returns the sensor instance ID, or 0 if `sensor` is NULL.
  233. *
  234. * \since This function is available since SDL 3.0.0.
  235. */
  236. extern DECLSPEC SDL_SensorID SDLCALL SDL_GetSensorInstanceID(SDL_Sensor *sensor);
  237. /**
  238. * Get the current state of an opened sensor.
  239. *
  240. * The number of values and interpretation of the data is sensor dependent.
  241. *
  242. * \param sensor The SDL_Sensor object to query
  243. * \param data A pointer filled with the current sensor state
  244. * \param num_values The number of values to write to data
  245. * \returns 0 on success or a negative error code on failure; call
  246. * SDL_GetError() for more information.
  247. *
  248. * \since This function is available since SDL 3.0.0.
  249. */
  250. extern DECLSPEC int SDLCALL SDL_GetSensorData(SDL_Sensor *sensor, float *data, int num_values);
  251. /**
  252. * Close a sensor previously opened with SDL_OpenSensor().
  253. *
  254. * \param sensor The SDL_Sensor object to close
  255. *
  256. * \since This function is available since SDL 3.0.0.
  257. */
  258. extern DECLSPEC void SDLCALL SDL_CloseSensor(SDL_Sensor *sensor);
  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. */
  270. extern DECLSPEC void SDLCALL SDL_UpdateSensors(void);
  271. /* Ends C function definitions when using C++ */
  272. #ifdef __cplusplus
  273. /* *INDENT-OFF* */
  274. }
  275. /* *INDENT-ON* */
  276. #endif
  277. #include <SDL3/SDL_close_code.h>
  278. #endif /* SDL_sensor_h_ */