openhmd.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. /**
  8. * \file openhmd.h
  9. * Main header for OpenHMD public API.
  10. **/
  11. #ifndef OPENHMD_H
  12. #define OPENHMD_H
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #ifdef _WIN32
  17. #ifdef DLL_EXPORT
  18. #define OHMD_APIENTRY __cdecl
  19. #define OHMD_APIENTRYDLL __declspec( dllexport )
  20. #else
  21. #ifdef OHMD_STATIC
  22. #define OHMD_APIENTRY __cdecl
  23. #define OHMD_APIENTRYDLL
  24. #else
  25. #define OHMD_APIENTRY __cdecl
  26. #define OHMD_APIENTRYDLL __declspec( dllimport )
  27. #endif
  28. #endif
  29. #else
  30. #define OHMD_APIENTRY
  31. #define OHMD_APIENTRYDLL
  32. #endif
  33. /** Maximum length of a string, including termination, in OpenHMD. */
  34. #define OHMD_STR_SIZE 256
  35. /** Return status codes, used for all functions that can return an error. */
  36. typedef enum {
  37. OHMD_S_OK = 0,
  38. OHMD_S_UNKNOWN_ERROR = -1,
  39. OHMD_S_INVALID_PARAMETER = -2,
  40. OHMD_S_UNSUPPORTED = -3,
  41. OHMD_S_INVALID_OPERATION = -4,
  42. /** OHMD_S_USER_RESERVED and below can be used for user purposes, such as errors within ohmd wrappers, etc. */
  43. OHMD_S_USER_RESERVED = -16384,
  44. } ohmd_status;
  45. /** A collection of string value information types, used for getting information with ohmd_list_gets(). */
  46. typedef enum {
  47. OHMD_VENDOR = 0,
  48. OHMD_PRODUCT = 1,
  49. OHMD_PATH = 2,
  50. } ohmd_string_value;
  51. /** A collection of string descriptions, used for getting strings with ohmd_gets(). */
  52. typedef enum {
  53. OHMD_GLSL_DISTORTION_VERT_SRC = 0,
  54. OHMD_GLSL_DISTORTION_FRAG_SRC = 1,
  55. } ohmd_string_description;
  56. /** A collection of float value information types, used for getting and setting information with
  57. ohmd_device_getf() and ohmd_device_setf(). */
  58. typedef enum {
  59. /** float[4] (get): Absolute rotation of the device, in space, as a quaternion (x, y, z, w). */
  60. OHMD_ROTATION_QUAT = 1,
  61. /** float[16] (get): A "ready to use" OpenGL style 4x4 matrix with a modelview matrix for the
  62. left eye of the HMD. */
  63. OHMD_LEFT_EYE_GL_MODELVIEW_MATRIX = 2,
  64. /** float[16] (get): A "ready to use" OpenGL style 4x4 matrix with a modelview matrix for the
  65. right eye of the HMD. */
  66. OHMD_RIGHT_EYE_GL_MODELVIEW_MATRIX = 3,
  67. /** float[16] (get): A "ready to use" OpenGL style 4x4 matrix with a projection matrix for the
  68. left eye of the HMD. */
  69. OHMD_LEFT_EYE_GL_PROJECTION_MATRIX = 4,
  70. /** float[16] (get): A "ready to use" OpenGL style 4x4 matrix with a projection matrix for the
  71. right eye of the HMD. */
  72. OHMD_RIGHT_EYE_GL_PROJECTION_MATRIX = 5,
  73. /** float[3] (get): A 3-D vector representing the absolute position of the device, in space. */
  74. OHMD_POSITION_VECTOR = 6,
  75. /** float[1] (get): Physical width of the device screen in metres. */
  76. OHMD_SCREEN_HORIZONTAL_SIZE = 7,
  77. /** float[1] (get): Physical height of the device screen in metres. */
  78. OHMD_SCREEN_VERTICAL_SIZE = 8,
  79. /** float[1] (get): Physical separation of the device lenses in metres. */
  80. OHMD_LENS_HORIZONTAL_SEPARATION = 9,
  81. /** float[1] (get): Physical vertical position of the lenses in metres. */
  82. OHMD_LENS_VERTICAL_POSITION = 10,
  83. /** float[1] (get): Physical field of view for the left eye in degrees. */
  84. OHMD_LEFT_EYE_FOV = 11,
  85. /** float[1] (get): Physical display aspect ratio for the left eye screen. */
  86. OHMD_LEFT_EYE_ASPECT_RATIO = 12,
  87. /** float[1] (get): Physical field of view for the left right in degrees. */
  88. OHMD_RIGHT_EYE_FOV = 13,
  89. /** float[1] (get): Physical display aspect ratio for the right eye screen. */
  90. OHMD_RIGHT_EYE_ASPECT_RATIO = 14,
  91. /** float[1] (get, set): Physical interpupillary distance of the user in metres. */
  92. OHMD_EYE_IPD = 15,
  93. /** float[1] (get, set): Z-far value for the projection matrix calculations (i.e. drawing distance). */
  94. OHMD_PROJECTION_ZFAR = 16,
  95. /** float[1] (get, set): Z-near value for the projection matrix calculations (i.e. close clipping distance). */
  96. OHMD_PROJECTION_ZNEAR = 17,
  97. /** float[6] (get): Device specific distortion value. */
  98. OHMD_DISTORTION_K = 18,
  99. /**
  100. * float[10] (set): Perform sensor fusion on values from external sensors.
  101. *
  102. * Values are: dt (time since last update in seconds) X, Y, Z gyro, X, Y, Z accelerometer and X, Y, Z magnetometer.
  103. **/
  104. OHMD_EXTERNAL_SENSOR_FUSION = 19,
  105. /** float[4] (get): Universal shader distortion coefficients (PanoTools model <a,b,c,d>. */
  106. OHMD_UNIVERSAL_DISTORTION_K = 20,
  107. /** float[3] (get): Universal shader aberration coefficients (post warp scaling <r,g,b>. */
  108. OHMD_UNIVERSAL_ABERRATION_K = 21,
  109. /** float[OHMD_ANALOG_AXIS_COUNT] (get): Get the state of the analog axes on the device. */
  110. OHMD_ANALOG_AXES_STATE = 22,
  111. } ohmd_float_value;
  112. /** A collection of int value information types used for getting information with ohmd_device_geti(). */
  113. typedef enum {
  114. /** int[1] (get, ohmd_geti()): Physical horizontal resolution of the device screen. */
  115. OHMD_SCREEN_HORIZONTAL_RESOLUTION = 0,
  116. /** int[1] (get, ohmd_geti()): Physical vertical resolution of the device screen. */
  117. OHMD_SCREEN_VERTICAL_RESOLUTION = 1,
  118. /** int[1] (get, ohmd_geti()): Get number of events waiting in digital input event queue. */
  119. OHMD_BUTTON_EVENT_COUNT = 2,
  120. /** int[1] (get, ohmd_geti()): Get if the there was an overflow in the event queue causing events to be dropped. */
  121. OHMD_BUTTON_EVENT_OVERFLOW = 3,
  122. /** int[1] (get, ohmd_geti()): Get the number of physical digital input buttons on the device. */
  123. OHMD_BUTTON_COUNT = 4,
  124. /** int[2] (get, ohmd_geti()): Performs an event pop action. Format: [button_index, button_state], where button_state is either OHMD_BUTTON_DOWN or OHMD_BUTTON_UP */
  125. OHMD_BUTTON_POP_EVENT = 5,
  126. /** int[1] (get, ohmd_geti()/ohmd_list_geti()): Gets the class of the device. See: ohmd_device_class. */
  127. OHMD_DEVICE_CLASS = 6,
  128. /** int[1] (get, ohmd_geti()/ohmd_list_geti()): Gets the flags of the device. See: ohmd_device_flags. */
  129. OHMD_DEVICE_FLAGS = 7,
  130. /** int[1] (get, ohmd_geti()): Get the number of analog axes on the device. */
  131. OHMD_ANALOG_AXIS_COUNT = 8,
  132. } ohmd_int_value;
  133. /** A collection of data information types used for setting information with ohmd_set_data(). */
  134. typedef enum {
  135. /** void* (set): Set void* data for use in the internal drivers. */
  136. OHMD_DRIVER_DATA = 0,
  137. /**
  138. * ohmd_device_properties* (set):
  139. * Set the device properties based on the ohmd_device_properties struct for use in the internal drivers.
  140. *
  141. * This can be used to fill in information about the device internally, such as Android, or for setting profiles.
  142. **/
  143. OHMD_DRIVER_PROPERTIES = 1,
  144. } ohmd_data_value;
  145. typedef enum {
  146. /** int[1] (set, default: 1): Set this to 0 to prevent OpenHMD from creating background threads to do automatic device ticking.
  147. Call ohmd_update(); must be called frequently, at least 10 times per second, if the background threads are disabled. */
  148. OHMD_IDS_AUTOMATIC_UPDATE = 0,
  149. } ohmd_int_settings;
  150. /** Button states for digital input events. */
  151. typedef enum {
  152. /** Button was pressed. */
  153. OHMD_BUTTON_DOWN = 0,
  154. /** Button was released. */
  155. OHMD_BUTTON_UP = 1
  156. } ohmd_button_state;
  157. /** Device classes. */
  158. typedef enum
  159. {
  160. /** HMD device. */
  161. OHMD_DEVICE_CLASS_HMD = 0,
  162. /** Controller device. */
  163. OHMD_DEVICE_CLASS_CONTROLLER = 1,
  164. /** Generic tracker device. */
  165. OHMD_DEVICE_CLASS_GENERIC_TRACKER = 2,
  166. } ohmd_device_class;
  167. /** Device flags. */
  168. typedef enum
  169. {
  170. /** Device is a null (dummy) device. */
  171. OHMD_DEVICE_FLAGS_NULL_DEVICE = 1,
  172. OHMD_DEVICE_FLAGS_POSITIONAL_TRACKING = 2,
  173. OHMD_DEVICE_FLAGS_ROTATIONAL_TRACKING = 4,
  174. OHMD_DEVICE_FLAGS_LEFT_CONTROLLER = 8,
  175. OHMD_DEVICE_FLAGS_RIGHT_CONTROLLER = 16,
  176. } ohmd_device_flags;
  177. /** An opaque pointer to a context structure. */
  178. typedef struct ohmd_context ohmd_context;
  179. /** An opaque pointer to a structure representing a device, such as an HMD. */
  180. typedef struct ohmd_device ohmd_device;
  181. /** An opaque pointer to a structure representing arguments for a device. */
  182. typedef struct ohmd_device_settings ohmd_device_settings;
  183. /**
  184. * Create an OpenHMD context.
  185. *
  186. * @return a pointer to an allocated ohmd_context on success or NULL if it fails.
  187. **/
  188. OHMD_APIENTRYDLL ohmd_context* OHMD_APIENTRY ohmd_ctx_create(void);
  189. /**
  190. * Destroy an OpenHMD context.
  191. *
  192. * ohmd_ctx_destroy de-initializes and de-allocates an OpenHMD context allocated with ohmd_ctx_create.
  193. * All devices associated with the context are automatically closed.
  194. *
  195. * @param ctx The context to destroy.
  196. **/
  197. OHMD_APIENTRYDLL void OHMD_APIENTRY ohmd_ctx_destroy(ohmd_context* ctx);
  198. /**
  199. * Get the last error as a human readable string.
  200. *
  201. * If a function taking a context as an argument (ohmd_context "methods") returns non-successfully,
  202. * a human readable error message describing what went wrong can be retrieved with this function.
  203. *
  204. * @param ctx The context to retrieve the error message from.
  205. * @return a pointer to the error message.
  206. **/
  207. OHMD_APIENTRYDLL const char* OHMD_APIENTRY ohmd_ctx_get_error(ohmd_context* ctx);
  208. /**
  209. * Update a context.
  210. *
  211. * Update the values for the devices handled by a context.
  212. *
  213. * If background threads are disabled, this performs tasks like pumping events from the device. The exact details
  214. * are up to the driver but try to call it quite frequently.
  215. * Once per frame in a "game loop" should be sufficient.
  216. * If OpenHMD is handled in a background thread in your program, calling ohmd_ctx_update and then sleeping for 10-20 ms
  217. * is recommended.
  218. *
  219. * @param ctx The context that needs updating.
  220. **/
  221. OHMD_APIENTRYDLL void OHMD_APIENTRY ohmd_ctx_update(ohmd_context* ctx);
  222. /**
  223. * Probe for devices.
  224. *
  225. * Probes for and enumerates supported devices attached to the system.
  226. *
  227. * @param ctx A context with no currently open devices.
  228. * @return the number of devices found on the system.
  229. **/
  230. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_ctx_probe(ohmd_context* ctx);
  231. /**
  232. * Get string from openhmd.
  233. *
  234. * Gets a string from OpenHMD. This is where non-device specific strings reside.
  235. * This is where the distortion shader sources can be retrieved.
  236. *
  237. * @param type The name of the string to fetch. One of OHMD_GLSL_DISTORTION_FRAG_SRC, and OHMD_GLSL_DISTORTION_FRAG_SRC.
  238. * @param out The location to return a const char*
  239. * @return 0 on success, <0 on failure.
  240. **/
  241. OHMD_APIENTRYDLL int ohmd_gets(ohmd_string_description type, const char** out);
  242. /**
  243. * Get device description from enumeration list index.
  244. *
  245. * Gets a human readable device description string from a zero indexed enumeration index
  246. * between 0 and (max - 1), where max is the number ohmd_ctx_probe returned
  247. * (i.e. if ohmd_ctx_probe returns 3, valid indices are 0, 1 and 2).
  248. * The function can return three types of data. The vendor name, the product name and
  249. * a driver specific path where the device is attached.
  250. *
  251. * ohmd_ctx_probe must be called before calling ohmd_list_gets.
  252. *
  253. * @param ctx A (probed) context.
  254. * @param index An index, between 0 and the value returned from ohmd_ctx_probe.
  255. * @param type The type of data to fetch. One of OHMD_VENDOR, OHMD_PRODUCT and OHMD_PATH.
  256. * @return a string with a human readable device name.
  257. **/
  258. OHMD_APIENTRYDLL const char* OHMD_APIENTRY ohmd_list_gets(ohmd_context* ctx, int index, ohmd_string_value type);
  259. /**
  260. * Get integer value from enumeration list index.
  261. *
  262. *
  263. *
  264. * ohmd_ctx_probe must be called before calling ohmd_list_gets.
  265. *
  266. * @param ctx A (probed) context.
  267. * @param index An index, between 0 and the value returned from ohmd_ctx_probe.
  268. * @param type What type of value to retrieve, ohmd_int_value section for more information.
  269. * @return 0 on success, <0 on failure.
  270. **/
  271. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_list_geti(ohmd_context* ctx, int index, ohmd_int_value type, int* out);
  272. /**
  273. * Open a device.
  274. *
  275. * Opens a device from a zero indexed enumeration index between 0 and (max - 1)
  276. * where max is the number ohmd_ctx_probe returned (i.e. if ohmd_ctx_probe returns 3,
  277. * valid indices are 0, 1 and 2).
  278. *
  279. * ohmd_ctx_probe must be called before calling ohmd_list_open_device.
  280. *
  281. * @param ctx A (probed) context.
  282. * @param index An index, between 0 and the value returned from ohmd_ctx_probe.
  283. * @return a pointer to an ohmd_device, which represents a hardware device, such as an HMD.
  284. **/
  285. OHMD_APIENTRYDLL ohmd_device* OHMD_APIENTRY ohmd_list_open_device(ohmd_context* ctx, int index);
  286. /**
  287. * Open a device with additional settings provided.
  288. *
  289. * Opens a device from a zero indexed enumeration index between 0 and (max - 1)
  290. * where max is the number ohmd_ctx_probe returned (i.e. if ohmd_ctx_probe returns 3,
  291. * valid indices are 0, 1 and 2).
  292. *
  293. * ohmd_ctx_probe must be called before calling ohmd_list_open_device.
  294. *
  295. * @param ctx A (probed) context.
  296. * @param index An index, between 0 and the value returned from ohmd_ctx_probe.
  297. * @param settings A pointer to a device settings struct.
  298. * @return a pointer to an ohmd_device, which represents a hardware device, such as an HMD.
  299. **/
  300. OHMD_APIENTRYDLL ohmd_device* OHMD_APIENTRY ohmd_list_open_device_s(ohmd_context* ctx, int index, ohmd_device_settings* settings);
  301. /**
  302. * Specify int settings in a device settings struct.
  303. *
  304. * @param settings The device settings struct to set values to.
  305. * @param key The specefic setting you wish to set.
  306. * @param value A pointer to an int or int array (containing the expected number of elements) with the value(s) you wish to set.
  307. **/
  308. OHMD_APIENTRYDLL ohmd_status OHMD_APIENTRY ohmd_device_settings_seti(ohmd_device_settings* settings, ohmd_int_settings key, const int* val);
  309. /**
  310. * Create a device settings instance.
  311. *
  312. * @param ctx A pointer to a valid ohmd_context.
  313. * @return a pointer to an allocated ohmd_context on success or NULL if it fails.
  314. **/
  315. OHMD_APIENTRYDLL ohmd_device_settings* OHMD_APIENTRY ohmd_device_settings_create(ohmd_context* ctx);
  316. /**
  317. * Destroy a device settings instance.
  318. *
  319. * @param ctx The device settings instance to destroy.
  320. **/
  321. OHMD_APIENTRYDLL void OHMD_APIENTRY ohmd_device_settings_destroy(ohmd_device_settings* settings);
  322. /**
  323. * Close a device.
  324. *
  325. * Closes a device opened by ohmd_list_open_device. Note that ohmd_ctx_destroy automatically closes any open devices
  326. * associated with the context being destroyed.
  327. *
  328. * @param device The open device.
  329. * @return 0 on success, <0 on failure.
  330. **/
  331. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_close_device(ohmd_device* device);
  332. /**
  333. * Get a floating point value from a device.
  334. *
  335. *
  336. * @param device An open device to retrieve the value from.
  337. * @param type What type of value to retrieve, see ohmd_float_value section for more information.
  338. * @param[out] out A pointer to a float, or float array where the retrieved value should be written.
  339. * @return 0 on success, <0 on failure.
  340. **/
  341. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_device_getf(ohmd_device* device, ohmd_float_value type, float* out);
  342. /**
  343. * Set a floating point value for a device.
  344. *
  345. * @param device An open device to set the value in.
  346. * @param type What type of value to set, see ohmd_float_value section for more information.
  347. * @param in A pointer to a float, or float array where the new value is stored.
  348. * @return 0 on success, <0 on failure.
  349. **/
  350. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_device_setf(ohmd_device* device, ohmd_float_value type, const float* in);
  351. /**
  352. * Get an integer value from a device.
  353. *
  354. * @param device An open device to retrieve the value from.
  355. * @param type What type of value to retrieve, ohmd_int_value section for more information.
  356. * @param[out] out A pointer to an integer, or integer array where the retrieved value should be written.
  357. * @return 0 on success, <0 on failure.
  358. **/
  359. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_device_geti(ohmd_device* device, ohmd_int_value type, int* out);
  360. /**
  361. * Set an integer value for a device.
  362. *
  363. * @param device An open device to set the value in.
  364. * @param type What type of value to set, see ohmd_float_value section for more information.
  365. * @param in A pointer to a int, or int array where the new value is stored.
  366. * @return 0 on success, <0 on failure.
  367. **/
  368. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_device_seti(ohmd_device* device, ohmd_int_value type, const int* in);
  369. /**
  370. * Set an void* data value for a device.
  371. *
  372. * @param device An open device to set the value in.
  373. * @param type What type of value to set, see ohmd_float_value section for more information.
  374. * @param in A pointer to the void* casted object.
  375. * @return 0 on success, <0 on failure.
  376. **/
  377. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_device_set_data(ohmd_device* device, ohmd_data_value type, const void* in);
  378. #ifdef __cplusplus
  379. }
  380. #endif
  381. #endif