SDL_sensor.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 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. * Include file for SDL sensor event handling
  22. *
  23. */
  24. #ifndef SDL_sensor_h_
  25. #define SDL_sensor_h_
  26. #include "SDL_stdinc.h"
  27. #include "SDL_error.h"
  28. #include "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. * \brief 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 system,
  46. * and is never reused for the lifetime of the application.
  47. *
  48. * The ID value starts at 0 and increments from there. The value -1 is an invalid ID.
  49. */
  50. typedef Sint32 SDL_SensorID;
  51. /* The different sensors defined by SDL
  52. *
  53. * Additional sensors may be available, using platform dependent semantics.
  54. *
  55. * Hare are the additional Android sensors:
  56. * https://developer.android.com/reference/android/hardware/SensorEvent.html#values
  57. */
  58. typedef enum
  59. {
  60. SDL_SENSOR_INVALID = -1, /**< Returned for an invalid sensor */
  61. SDL_SENSOR_UNKNOWN, /**< Unknown sensor type */
  62. SDL_SENSOR_ACCEL, /**< Accelerometer */
  63. SDL_SENSOR_GYRO /**< Gyroscope */
  64. } SDL_SensorType;
  65. /**
  66. * Accelerometer sensor
  67. *
  68. * The accelerometer returns the current acceleration in SI meters per
  69. * second squared. This measurement includes the force of gravity, so
  70. * a device at rest will have an value of SDL_STANDARD_GRAVITY away
  71. * from the center of the earth.
  72. *
  73. * values[0]: Acceleration on the x axis
  74. * values[1]: Acceleration on the y axis
  75. * values[2]: Acceleration on the z axis
  76. *
  77. * For phones held in portrait mode and game controllers held in front of you,
  78. * the axes are defined as follows:
  79. * -X ... +X : left ... right
  80. * -Y ... +Y : bottom ... top
  81. * -Z ... +Z : farther ... closer
  82. *
  83. * The axis data is not changed when the phone is rotated.
  84. *
  85. * \sa SDL_GetDisplayOrientation()
  86. */
  87. #define SDL_STANDARD_GRAVITY 9.80665f
  88. /**
  89. * Gyroscope sensor
  90. *
  91. * The gyroscope returns the current rate of rotation in radians per second.
  92. * The rotation is positive in the counter-clockwise direction. That is,
  93. * an observer looking from a positive location on one of the axes would
  94. * see positive rotation on that axis when it appeared to be rotating
  95. * counter-clockwise.
  96. *
  97. * values[0]: Angular speed around the x axis (pitch)
  98. * values[1]: Angular speed around the y axis (yaw)
  99. * values[2]: Angular speed around the z axis (roll)
  100. *
  101. * For phones held in portrait mode and game controllers held in front of you,
  102. * the axes are defined as follows:
  103. * -X ... +X : left ... right
  104. * -Y ... +Y : bottom ... top
  105. * -Z ... +Z : farther ... closer
  106. *
  107. * The axis data is not changed when the phone or controller is rotated.
  108. *
  109. * \sa SDL_GetDisplayOrientation()
  110. */
  111. /* Function prototypes */
  112. /**
  113. * Locking for multi-threaded access to the sensor API
  114. *
  115. * If you are using the sensor API or handling events from multiple threads
  116. * you should use these locking functions to protect access to the sensors.
  117. *
  118. * In particular, you are guaranteed that the sensor list won't change, so
  119. * the API functions that take a sensor index will be valid, and sensor
  120. * events will not be delivered.
  121. */
  122. extern DECLSPEC void SDLCALL SDL_LockSensors(void);
  123. extern DECLSPEC void SDLCALL SDL_UnlockSensors(void);
  124. /**
  125. * Count the number of sensors attached to the system right now.
  126. *
  127. * \returns The number of sensors detected.
  128. */
  129. extern DECLSPEC int SDLCALL SDL_NumSensors(void);
  130. /**
  131. * Get the implementation dependent name of a sensor.
  132. *
  133. * \param device_index The sensor to obtain name from
  134. * \returns The sensor name, or NULL if `device_index` is out of range.
  135. */
  136. extern DECLSPEC const char *SDLCALL SDL_SensorGetDeviceName(int device_index);
  137. /**
  138. * Get the type of a sensor.
  139. *
  140. * \param device_index The sensor to get the type from
  141. * \returns The SDL_SensorType, or `SDL_SENSOR_INVALID` if `device_index` is
  142. * out of range.
  143. */
  144. extern DECLSPEC SDL_SensorType SDLCALL SDL_SensorGetDeviceType(int device_index);
  145. /**
  146. * Get the platform dependent type of a sensor.
  147. *
  148. * \param device_index The sensor to check
  149. * \returns The sensor platform dependent type, or -1 if `device_index` is out
  150. * of range.
  151. */
  152. extern DECLSPEC int SDLCALL SDL_SensorGetDeviceNonPortableType(int device_index);
  153. /**
  154. * Get the instance ID of a sensor.
  155. *
  156. * \param device_index The sensor to get instance id from
  157. * \returns The sensor instance ID, or -1 if `device_index` is out of range.
  158. */
  159. extern DECLSPEC SDL_SensorID SDLCALL SDL_SensorGetDeviceInstanceID(int device_index);
  160. /**
  161. * Open a sensor for use.
  162. *
  163. * \param device_index The sensor to open
  164. * \returns An SDL_Sensor sensor object, or NULL if an error occurred.
  165. */
  166. extern DECLSPEC SDL_Sensor *SDLCALL SDL_SensorOpen(int device_index);
  167. /**
  168. * Return the SDL_Sensor associated with an instance id.
  169. *
  170. * \param instance_id The sensor from instance id
  171. * \returns An SDL_Sensor object.
  172. */
  173. extern DECLSPEC SDL_Sensor *SDLCALL SDL_SensorFromInstanceID(SDL_SensorID instance_id);
  174. /**
  175. * Get the implementation dependent name of a sensor
  176. *
  177. * \param sensor The SDL_Sensor object
  178. * \returns The sensor name, or NULL if `sensor` is NULL.
  179. */
  180. extern DECLSPEC const char *SDLCALL SDL_SensorGetName(SDL_Sensor *sensor);
  181. /**
  182. * Get the type of a sensor.
  183. *
  184. * \param sensor The SDL_Sensor object to inspect
  185. * \returns The SDL_SensorType type, or `SDL_SENSOR_INVALID` if `sensor` is
  186. * NULL.
  187. */
  188. extern DECLSPEC SDL_SensorType SDLCALL SDL_SensorGetType(SDL_Sensor *sensor);
  189. /**
  190. * Get the platform dependent type of a sensor.
  191. *
  192. * \param sensor The SDL_Sensor object to inspect
  193. * \returns The sensor platform dependent type, or -1 if `sensor` is NULL.
  194. */
  195. extern DECLSPEC int SDLCALL SDL_SensorGetNonPortableType(SDL_Sensor *sensor);
  196. /**
  197. * Get the instance ID of a sensor.
  198. *
  199. * \param sensor The SDL_Sensor object to inspect
  200. * \returns The sensor instance ID, or -1 if `sensor` is NULL.
  201. */
  202. extern DECLSPEC SDL_SensorID SDLCALL SDL_SensorGetInstanceID(SDL_Sensor *sensor);
  203. /**
  204. * Get the current state of an opened sensor.
  205. *
  206. * The number of values and interpretation of the data is sensor dependent.
  207. *
  208. * \param sensor The SDL_Sensor object to query
  209. * \param data A pointer filled with the current sensor state
  210. * \param num_values The number of values to write to data
  211. * \returns 0 or -1 if an error occurred.
  212. */
  213. extern DECLSPEC int SDLCALL SDL_SensorGetData(SDL_Sensor * sensor, float *data, int num_values);
  214. /**
  215. * Close a sensor previously opened with SDL_SensorOpen().
  216. *
  217. * \param sensor The SDL_Sensor object to close
  218. */
  219. extern DECLSPEC void SDLCALL SDL_SensorClose(SDL_Sensor * sensor);
  220. /**
  221. * Update the current state of the open sensors.
  222. *
  223. * This is called automatically by the event loop if sensor events are
  224. * enabled.
  225. *
  226. * This needs to be called from the thread that initialized the sensor
  227. * subsystem.
  228. */
  229. extern DECLSPEC void SDLCALL SDL_SensorUpdate(void);
  230. /* Ends C function definitions when using C++ */
  231. #ifdef __cplusplus
  232. /* *INDENT-OFF* */
  233. }
  234. /* *INDENT-ON* */
  235. #endif
  236. #include "close_code.h"
  237. #endif /* SDL_sensor_h_ */
  238. /* vi: set ts=4 sw=4 expandtab: */