SDL_sensor.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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. * \file SDL_sensor.h
  20. *
  21. * \brief 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_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 system,
  45. * and is never reused for the lifetime of the application.
  46. *
  47. * The ID value starts at 1 and increments from there. The value 0 is an invalid ID.
  48. */
  49. typedef Uint32 SDL_SensorID;
  50. /* The different sensors defined by SDL
  51. *
  52. * Additional sensors may be available, using platform dependent semantics.
  53. *
  54. * Hare are the additional Android sensors:
  55. * https://developer.android.com/reference/android/hardware/SensorEvent.html#values
  56. */
  57. typedef enum
  58. {
  59. SDL_SENSOR_INVALID = -1, /**< Returned for an invalid sensor */
  60. SDL_SENSOR_UNKNOWN, /**< Unknown sensor type */
  61. SDL_SENSOR_ACCEL, /**< Accelerometer */
  62. SDL_SENSOR_GYRO, /**< Gyroscope */
  63. SDL_SENSOR_ACCEL_L, /**< Accelerometer for left Joy-Con controller and Wii nunchuk */
  64. SDL_SENSOR_GYRO_L, /**< Gyroscope for left Joy-Con controller */
  65. SDL_SENSOR_ACCEL_R, /**< Accelerometer for right Joy-Con controller */
  66. SDL_SENSOR_GYRO_R /**< Gyroscope for right Joy-Con controller */
  67. } SDL_SensorType;
  68. /**
  69. * Accelerometer sensor
  70. *
  71. * The accelerometer returns the current acceleration in SI meters per
  72. * second squared. This measurement includes the force of gravity, so
  73. * a device at rest will have an value of SDL_STANDARD_GRAVITY away
  74. * from the center of the earth, which is a positive Y value.
  75. *
  76. * values[0]: Acceleration on the x axis
  77. * values[1]: Acceleration on the y axis
  78. * values[2]: Acceleration on the z axis
  79. *
  80. * For phones and tablets held in natural orientation and game controllers held in front of you, the axes are defined as follows:
  81. * -X ... +X : left ... right
  82. * -Y ... +Y : bottom ... top
  83. * -Z ... +Z : farther ... closer
  84. *
  85. * The axis data is not changed when the device is rotated.
  86. *
  87. * \sa SDL_GetCurrentDisplayOrientation()
  88. */
  89. #define SDL_STANDARD_GRAVITY 9.80665f
  90. /**
  91. * Gyroscope sensor
  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,
  95. * an observer looking from a positive location on one of the axes would
  96. * see 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 held in front of you, the axes are defined as follows:
  104. * -X ... +X : left ... right
  105. * -Y ... +Y : bottom ... top
  106. * -Z ... +Z : farther ... closer
  107. *
  108. * The axis data is not changed when the device is rotated.
  109. *
  110. * \sa SDL_GetCurrentDisplayOrientation()
  111. */
  112. /* Function prototypes */
  113. /**
  114. * Get a list of currently connected sensors.
  115. *
  116. * \param count a pointer filled in with the number of sensors returned
  117. * \returns a 0 terminated array of sensor instance IDs which should be freed
  118. * with SDL_free(), or NULL on error; call SDL_GetError() for more
  119. * details.
  120. *
  121. * \since This function is available since SDL 3.0.0.
  122. */
  123. extern DECLSPEC SDL_SensorID *SDLCALL SDL_GetSensors(int *count);
  124. /**
  125. * Get the implementation dependent name of a sensor.
  126. *
  127. * \param instance_id the sensor instance ID
  128. * \returns the sensor name, or NULL if `instance_id` is not valid
  129. *
  130. * \since This function is available since SDL 3.0.0.
  131. */
  132. extern DECLSPEC const char *SDLCALL SDL_GetSensorInstanceName(SDL_SensorID instance_id);
  133. /**
  134. * Get the type of a sensor.
  135. *
  136. * \param instance_id the sensor instance ID
  137. * \returns the SDL_SensorType, or `SDL_SENSOR_INVALID` if `instance_id` is
  138. * not valid
  139. *
  140. * \since This function is available since SDL 3.0.0.
  141. */
  142. extern DECLSPEC SDL_SensorType SDLCALL SDL_GetSensorInstanceType(SDL_SensorID instance_id);
  143. /**
  144. * Get the platform dependent type of a sensor.
  145. *
  146. * \param instance_id the sensor instance ID
  147. * \returns the sensor platform dependent type, or -1 if `instance_id` is not
  148. * valid
  149. *
  150. * \since This function is available since SDL 3.0.0.
  151. */
  152. extern DECLSPEC int SDLCALL SDL_GetSensorInstanceNonPortableType(SDL_SensorID instance_id);
  153. /**
  154. * Open a sensor for use.
  155. *
  156. * \param instance_id the sensor instance ID
  157. * \returns an SDL_Sensor sensor object, or NULL if an error occurred.
  158. *
  159. * \since This function is available since SDL 3.0.0.
  160. */
  161. extern DECLSPEC SDL_Sensor *SDLCALL SDL_OpenSensor(SDL_SensorID instance_id);
  162. /**
  163. * Return the SDL_Sensor associated with an instance ID.
  164. *
  165. * \param instance_id the sensor instance ID
  166. * \returns an SDL_Sensor object.
  167. *
  168. * \since This function is available since SDL 3.0.0.
  169. */
  170. extern DECLSPEC SDL_Sensor *SDLCALL SDL_GetSensorFromInstanceID(SDL_SensorID instance_id);
  171. /**
  172. * Get the implementation dependent name of a sensor
  173. *
  174. * \param sensor The SDL_Sensor object
  175. * \returns the sensor name, or NULL if `sensor` is NULL.
  176. *
  177. * \since This function is available since SDL 3.0.0.
  178. */
  179. extern DECLSPEC const char *SDLCALL SDL_GetSensorName(SDL_Sensor *sensor);
  180. /**
  181. * Get the type of a sensor.
  182. *
  183. * \param sensor The SDL_Sensor object to inspect
  184. * \returns the SDL_SensorType type, or `SDL_SENSOR_INVALID` if `sensor` is
  185. * NULL.
  186. *
  187. * \since This function is available since SDL 3.0.0.
  188. */
  189. extern DECLSPEC SDL_SensorType SDLCALL SDL_GetSensorType(SDL_Sensor *sensor);
  190. /**
  191. * Get the platform dependent type of a sensor.
  192. *
  193. * \param sensor The SDL_Sensor object to inspect
  194. * \returns the sensor platform dependent type, or -1 if `sensor` is NULL.
  195. *
  196. * \since This function is available since SDL 3.0.0.
  197. */
  198. extern DECLSPEC int SDLCALL SDL_GetSensorNonPortableType(SDL_Sensor *sensor);
  199. /**
  200. * Get the instance ID of a sensor.
  201. *
  202. * \param sensor The SDL_Sensor object to inspect
  203. * \returns the sensor instance ID, or 0 if `sensor` is NULL.
  204. *
  205. * \since This function is available since SDL 3.0.0.
  206. */
  207. extern DECLSPEC SDL_SensorID SDLCALL SDL_GetSensorInstanceID(SDL_Sensor *sensor);
  208. /**
  209. * Get the current state of an opened sensor.
  210. *
  211. * The number of values and interpretation of the data is sensor dependent.
  212. *
  213. * \param sensor The SDL_Sensor object to query
  214. * \param data A pointer filled with the current sensor state
  215. * \param num_values The number of values to write to data
  216. * \returns 0 on success or a negative error code on failure; call
  217. * SDL_GetError() for more information.
  218. *
  219. * \since This function is available since SDL 3.0.0.
  220. */
  221. extern DECLSPEC int SDLCALL SDL_GetSensorData(SDL_Sensor *sensor, float *data, int num_values);
  222. /**
  223. * Close a sensor previously opened with SDL_OpenSensor().
  224. *
  225. * \param sensor The SDL_Sensor object to close
  226. *
  227. * \since This function is available since SDL 3.0.0.
  228. */
  229. extern DECLSPEC void SDLCALL SDL_CloseSensor(SDL_Sensor *sensor);
  230. /**
  231. * Update the current state of the open sensors.
  232. *
  233. * This is called automatically by the event loop if sensor events are
  234. * enabled.
  235. *
  236. * This needs to be called from the thread that initialized the sensor
  237. * subsystem.
  238. *
  239. * \since This function is available since SDL 3.0.0.
  240. */
  241. extern DECLSPEC void SDLCALL SDL_UpdateSensors(void);
  242. /* Ends C function definitions when using C++ */
  243. #ifdef __cplusplus
  244. /* *INDENT-OFF* */
  245. }
  246. /* *INDENT-ON* */
  247. #endif
  248. #include <SDL3/SDL_close_code.h>
  249. #endif /* SDL_sensor_h_ */