openhmd.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * OpenHMD - Free and Open Source API and drivers for immersive technology.
  3. * Copyright (C) 2013 Fredrik Hultin.
  4. * Copyright (C) 2013 Jakob Bornecrantz.
  5. * Distributed under the Boost 1.0 licence, see LICENSE for full text.
  6. */
  7. /** \file openhmd.h
  8. * Main header for OpenHMD public API.
  9. */
  10. #ifndef OPENHMD_H
  11. #define OPENHMD_H
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. #ifdef _WIN32
  16. #ifdef DLL_EXPORT
  17. #define OHMD_APIENTRY __cdecl
  18. #define OHMD_APIENTRYDLL __declspec( dllexport )
  19. #else
  20. #ifdef OHMD_STATIC
  21. #define OHMD_APIENTRY __cdecl
  22. #define OHMD_APIENTRYDLL
  23. #else
  24. #define OHMD_APIENTRY __cdecl
  25. #define OHMD_APIENTRYDLL __declspec( dllimport )
  26. #endif
  27. #endif
  28. #else
  29. #define OHMD_APIENTRY
  30. #define OHMD_APIENTRYDLL
  31. #endif
  32. /** Maximum length of a string, including termination, in OpenHMD. */
  33. #define OHMD_STR_SIZE 256
  34. /** Return status codes, used for all functions that can return an error. */
  35. typedef enum {
  36. OHMD_S_OK = 0,
  37. OHMD_S_UNKNOWN_ERROR = -1,
  38. OHMD_S_INVALID_PARAMETER = -2,
  39. OHMD_S_UNSUPPORTED = -3,
  40. /** OHMD_S_USER_RESERVED and below can be used for user purposes, such as errors within ohmd wrappers, etc. */
  41. OHMD_S_USER_RESERVED = -16384,
  42. } ohmd_status;
  43. /** A collection of string value information types, used for getting information with ohmd_list_gets(). */
  44. typedef enum {
  45. OHMD_VENDOR = 0,
  46. OHMD_PRODUCT = 1,
  47. OHMD_PATH = 2,
  48. } ohmd_string_value;
  49. /** A collection of float value information types, used for getting and setting information with
  50. ohmd_device_getf() and ohmd_device_setf(). */
  51. typedef enum {
  52. /** float[4] (get): Absolute rotation of the device, in space, as a quaternion (x, y, z, w). */
  53. OHMD_ROTATION_QUAT = 1,
  54. /** float[16] (get): A "ready to use" OpenGL style 4x4 matrix with a modelview matrix for the
  55. left eye of the HMD. */
  56. OHMD_LEFT_EYE_GL_MODELVIEW_MATRIX = 2,
  57. /** float[16] (get): A "ready to use" OpenGL style 4x4 matrix with a modelview matrix for the
  58. right eye of the HMD. */
  59. OHMD_RIGHT_EYE_GL_MODELVIEW_MATRIX = 3,
  60. /** float[16] (get): A "ready to use" OpenGL style 4x4 matrix with a projection matrix for the
  61. left eye of the HMD. */
  62. OHMD_LEFT_EYE_GL_PROJECTION_MATRIX = 4,
  63. /** float[16] (get): A "ready to use" OpenGL style 4x4 matrix with a projection matrix for the
  64. right eye of the HMD. */
  65. OHMD_RIGHT_EYE_GL_PROJECTION_MATRIX = 5,
  66. /** float[3] (get): A 3-D vector representing the absolute position of the device, in space. */
  67. OHMD_POSITION_VECTOR = 6,
  68. /** float[1] (get): Physical width of the device screen in centimetres. */
  69. OHMD_SCREEN_HORIZONTAL_SIZE = 7,
  70. /** float[1] (get): Physical height of the device screen in centimetres. */
  71. OHMD_SCREEN_VERTICAL_SIZE = 8,
  72. /** float[1] (get): Physical separation of the device lenses in centimetres. */
  73. OHMD_LENS_HORIZONTAL_SEPARATION = 9,
  74. /** float[1] (get): Physical vertical position of the lenses in centimetres. */
  75. OHMD_LENS_VERTICAL_POSITION = 10,
  76. /** float[1] (get): Physical field of view for the left eye in degrees. */
  77. OHMD_LEFT_EYE_FOV = 11,
  78. /** float[1] (get): Physical display aspect ratio for the left eye screen. */
  79. OHMD_LEFT_EYE_ASPECT_RATIO = 12,
  80. /** float[1] (get): Physical field of view for the left right in degrees. */
  81. OHMD_RIGHT_EYE_FOV = 13,
  82. /** float[1] (get): Physical display aspect ratio for the right eye screen. */
  83. OHMD_RIGHT_EYE_ASPECT_RATIO = 14,
  84. /** float[1] (get, set): Physical interpupillary distance of the user in centimetres. */
  85. OHMD_EYE_IPD = 15,
  86. /** float[1] (get, set): Z-far value for the projection matrix calculations (i.e. drawing distance). */
  87. OHMD_PROJECTION_ZFAR = 16,
  88. /** float[1] (get, set): Z-near value for the projection matrix calculations (i.e. close clipping distance). */
  89. OHMD_PROJECTION_ZNEAR = 17,
  90. /** float[6] (get): Device specific distortion value. */
  91. OHMD_DISTORTION_K = 18,
  92. /**
  93. * float[10] (set): Perform sensor fusion on values from external sensors.
  94. *
  95. * Values are: dt (time since last update in seconds) X, Y, Z gyro, X, Y, Z accelerometer and X, Y, Z magnetometer.
  96. **/
  97. OHMD_EXTERNAL_SENSOR_FUSION = 19,
  98. } ohmd_float_value;
  99. /** A collection of int value information types used for getting information with ohmd_device_geti(). */
  100. typedef enum {
  101. /** int[1] (get): Physical horizontal resolution of the device screen. */
  102. OHMD_SCREEN_HORIZONTAL_RESOLUTION = 0,
  103. /** int[1] (get): Physical vertical resolution of the device screen. */
  104. OHMD_SCREEN_VERTICAL_RESOLUTION = 1,
  105. } ohmd_int_value;
  106. /** A collection of data information types used for setting information with ohmd_set_data(). */
  107. typedef enum {
  108. /** void* (set): Set void* data for use in the internal drivers. */
  109. OHMD_DRIVER_DATA = 0,
  110. /**
  111. * ohmd_device_properties* (set):
  112. * Set the device properties based on the ohmd_device_properties struct for use in the internal drivers.
  113. *
  114. * This can be used to fill in information about the device internally, such as Android, or for setting profiles.
  115. **/
  116. OHMD_DRIVER_PROPERTIES = 1,
  117. } ohmd_data_value;
  118. /** An opaque pointer to a context structure. */
  119. typedef struct ohmd_context ohmd_context;
  120. /** An opaque pointer to a structure representing a device, such as an HMD. */
  121. typedef struct ohmd_device ohmd_device;
  122. /**
  123. * Create an OpenHMD context.
  124. *
  125. * @return a pointer to an allocated ohmd_context on success or NULL if it fails.
  126. */
  127. OHMD_APIENTRYDLL ohmd_context* OHMD_APIENTRY ohmd_ctx_create();
  128. /**
  129. * Destroy an OpenHMD context.
  130. *
  131. * ohmd_ctx_destroy de-initializes and de-allocates an OpenHMD context allocated with ohmd_ctx_create.
  132. * All devices associated with the context are automatically closed.
  133. *
  134. * @param ctx The context to destroy.
  135. */
  136. OHMD_APIENTRYDLL void OHMD_APIENTRY ohmd_ctx_destroy(ohmd_context* ctx);
  137. /**
  138. * Get the last error as a human readable string.
  139. *
  140. * If a function taking a context as an argument (ohmd_context "methods") returns non-successfully,
  141. * a human readable error message describing what went wrong can be retrieved with this function.
  142. *
  143. * @param ctx The context to retrieve the error message from.
  144. * @return a pointer to the error message.
  145. */
  146. OHMD_APIENTRYDLL const char* OHMD_APIENTRY ohmd_ctx_get_error(ohmd_context* ctx);
  147. /**
  148. * Update a context.
  149. *
  150. * Performs tasks like pumping events from the device. The exact details are up to the driver
  151. * but try to call it quite frequently.
  152. * Once per frame in a "game loop" should be sufficient.
  153. * If OpenHMD is handled in a background thread, calling ohmd_ctx_update and then sleeping for 10-20 ms
  154. * is recommended.
  155. *
  156. * @param ctx The context that needs updating.
  157. */
  158. OHMD_APIENTRYDLL void OHMD_APIENTRY ohmd_ctx_update(ohmd_context* ctx);
  159. /**
  160. * Probe for devices.
  161. *
  162. * Probes for and enumerates supported devices attached to the system.
  163. *
  164. * @param ctx A context with no currently open devices.
  165. * @return the number of devices found on the system.
  166. */
  167. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_ctx_probe(ohmd_context* ctx);
  168. /**
  169. * Get device description from enumeration list index.
  170. *
  171. * Gets a human readable device description string from a zero indexed enumeration index
  172. * between 0 and (max - 1), where max is the number ohmd_ctx_probe returned
  173. * (i.e. if ohmd_ctx_probe returns 3, valid indices are 0, 1 and 2).
  174. * The function can return three types of data. The vendor name, the product name and
  175. * a driver specific path where the device is attached.
  176. *
  177. * ohmd_ctx_probe must be called before calling ohmd_list_gets.
  178. *
  179. * @param ctx A (probed) context.
  180. * @param index An index, between 0 and the value returned from ohmd_ctx_probe.
  181. * @param type The type of data to fetch. One of OHMD_VENDOR, OHMD_PRODUCT and OHMD_PATH.
  182. * @return a string with a human readable device name.
  183. */
  184. OHMD_APIENTRYDLL const char* OHMD_APIENTRY ohmd_list_gets(ohmd_context* ctx, int index, ohmd_string_value type);
  185. /**
  186. * Open a device.
  187. *
  188. * Opens a device from a zero indexed enumeration index between 0 and (max - 1)
  189. * where max is the number ohmd_ctx_probe returned (i.e. if ohmd_ctx_probe returns 3,
  190. * valid indices are 0, 1 and 2).
  191. *
  192. * ohmd_ctx_probe must be called before calling ohmd_list_open_device.
  193. *
  194. * @param ctx A (probed) context.
  195. * @param index An index, between 0 and the value returned from ohmd_ctx_probe.
  196. * @return a pointer to an ohmd_device, which represents a hardware device, such as an HMD.
  197. */
  198. OHMD_APIENTRYDLL ohmd_device* OHMD_APIENTRY ohmd_list_open_device(ohmd_context* ctx, int index);
  199. /**
  200. * Close a device.
  201. *
  202. * Closes a device opened by ohmd_list_open_device. Note that ohmd_ctx_destroy automatically closes any open devices
  203. * associated with the context being destroyed.
  204. *
  205. * @param device The open device.
  206. * @return 0 on success, <0 on failure.
  207. */
  208. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_close_device(ohmd_device* device);
  209. /**
  210. * Get a floating point value from a device.
  211. *
  212. *
  213. * @param device An open device to retrieve the value from.
  214. * @param type What type of value to retrieve, see ohmd_float_value section for more information.
  215. * @param[out] out A pointer to a float, or float array where the retrieved value should be written.
  216. * @return 0 on success, <0 on failure.
  217. */
  218. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_device_getf(ohmd_device* device, ohmd_float_value type, float* out);
  219. /**
  220. * Set a floating point value for a device.
  221. *
  222. * @param device An open device to set the value in.
  223. * @param type What type of value to set, see ohmd_float_value section for more information.
  224. * @param in A pointer to a float, or float array where the new value is stored.
  225. * @return 0 on success, <0 on failure.
  226. */
  227. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_device_setf(ohmd_device* device, ohmd_float_value type, float* in);
  228. /**
  229. * Get an integer value from a device.
  230. *
  231. * @param device An open device to retrieve the value from.
  232. * @param type What type of value to retrieve, ohmd_int_value section for more information.
  233. * @param[out] out A pointer to an integer, or integer array where the retrieved value should be written.
  234. * @return 0 on success, <0 on failure.
  235. */
  236. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_device_geti(ohmd_device* device, ohmd_int_value type, int* out);
  237. /**
  238. * Set an integer value for a device.
  239. *
  240. * @param device An open device to set the value in.
  241. * @param type What type of value to set, see ohmd_float_value section for more information.
  242. * @param in A pointer to a int, or int array where the new value is stored.
  243. * @return 0 on success, <0 on failure.
  244. */
  245. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_device_seti(ohmd_device* device, ohmd_int_value type, int* in);
  246. /**
  247. * Set an void* data value for a device.
  248. *
  249. * @param device An open device to set the value in.
  250. * @param type What type of value to set, see ohmd_float_value section for more information.
  251. * @param in A pointer to the void* casted object.
  252. * @return 0 on success, <0 on failure.
  253. */
  254. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_device_set_data(ohmd_device* device, ohmd_data_value type, void* in);
  255. #ifdef __cplusplus
  256. }
  257. #endif
  258. #endif