openhmd.h 17 KB

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