openhmd.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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_function;
  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()): Get number of events waiting in digital input event queue. */
  141. OHMD_BUTTON_EVENT_COUNT = 2,
  142. /** int[1] (get, ohmd_geti()): Get if the there was an overflow in the event queue causing events to be dropped. */
  143. OHMD_BUTTON_EVENT_OVERFLOW = 3,
  144. /** int[1] (get, ohmd_geti()): Get the number of physical digital input buttons on the device. */
  145. OHMD_BUTTON_COUNT = 4,
  146. /** 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 */
  147. OHMD_BUTTON_POP_EVENT = 5,
  148. /** int[1] (get, ohmd_geti()/ohmd_list_geti()): Gets the class of the device. See: ohmd_device_class. */
  149. OHMD_DEVICE_CLASS = 6,
  150. /** int[1] (get, ohmd_geti()/ohmd_list_geti()): Gets the flags of the device. See: ohmd_device_flags. */
  151. OHMD_DEVICE_FLAGS = 7,
  152. /** int[1] (get, ohmd_geti()): Get the number of analog and digital controls of the device. */
  153. OHMD_CONTROL_COUNT = 8,
  154. /** int[OHMD_CONTROL_COUNT] (get, ohmd_geti()): Get whether controls are digital or analog. */
  155. OHMD_CONTROLS_FUNCTIONS = 9,
  156. /** int[OHMD_CONTROL_COUNT] (get, ohmd_geti()): Get what function controls serve. */
  157. OHMD_CONTROLS_TYPES = 10,
  158. } ohmd_int_value;
  159. /** A collection of data information types used for setting information with ohmd_set_data(). */
  160. typedef enum {
  161. /** void* (set): Set void* data for use in the internal drivers. */
  162. OHMD_DRIVER_DATA = 0,
  163. /**
  164. * ohmd_device_properties* (set):
  165. * Set the device properties based on the ohmd_device_properties struct for use in the internal drivers.
  166. *
  167. * This can be used to fill in information about the device internally, such as Android, or for setting profiles.
  168. **/
  169. OHMD_DRIVER_PROPERTIES = 1,
  170. } ohmd_data_value;
  171. typedef enum {
  172. /** int[1] (set, default: 1): Set this to 0 to prevent OpenHMD from creating background threads to do automatic device ticking.
  173. Call ohmd_update(); must be called frequently, at least 10 times per second, if the background threads are disabled. */
  174. OHMD_IDS_AUTOMATIC_UPDATE = 0,
  175. } ohmd_int_settings;
  176. /** Button states for digital input events. */
  177. typedef enum {
  178. /** Button was pressed. */
  179. OHMD_BUTTON_DOWN = 0,
  180. /** Button was released. */
  181. OHMD_BUTTON_UP = 1
  182. } ohmd_button_state;
  183. /** Device classes. */
  184. typedef enum
  185. {
  186. /** HMD device. */
  187. OHMD_DEVICE_CLASS_HMD = 0,
  188. /** Controller device. */
  189. OHMD_DEVICE_CLASS_CONTROLLER = 1,
  190. /** Generic tracker device. */
  191. OHMD_DEVICE_CLASS_GENERIC_TRACKER = 2,
  192. } ohmd_device_class;
  193. /** Device flags. */
  194. typedef enum
  195. {
  196. /** Device is a null (dummy) device. */
  197. OHMD_DEVICE_FLAGS_NULL_DEVICE = 1,
  198. OHMD_DEVICE_FLAGS_POSITIONAL_TRACKING = 2,
  199. OHMD_DEVICE_FLAGS_ROTATIONAL_TRACKING = 4,
  200. OHMD_DEVICE_FLAGS_LEFT_CONTROLLER = 8,
  201. OHMD_DEVICE_FLAGS_RIGHT_CONTROLLER = 16,
  202. } ohmd_device_flags;
  203. /** An opaque pointer to a context structure. */
  204. typedef struct ohmd_context ohmd_context;
  205. /** An opaque pointer to a structure representing a device, such as an HMD. */
  206. typedef struct ohmd_device ohmd_device;
  207. /** An opaque pointer to a structure representing arguments for a device. */
  208. typedef struct ohmd_device_settings ohmd_device_settings;
  209. /**
  210. * Create an OpenHMD context.
  211. *
  212. * @return a pointer to an allocated ohmd_context on success or NULL if it fails.
  213. **/
  214. OHMD_APIENTRYDLL ohmd_context* OHMD_APIENTRY ohmd_ctx_create(void);
  215. /**
  216. * Destroy an OpenHMD context.
  217. *
  218. * ohmd_ctx_destroy de-initializes and de-allocates an OpenHMD context allocated with ohmd_ctx_create.
  219. * All devices associated with the context are automatically closed.
  220. *
  221. * @param ctx The context to destroy.
  222. **/
  223. OHMD_APIENTRYDLL void OHMD_APIENTRY ohmd_ctx_destroy(ohmd_context* ctx);
  224. /**
  225. * Get the last error as a human readable string.
  226. *
  227. * If a function taking a context as an argument (ohmd_context "methods") returns non-successfully,
  228. * a human readable error message describing what went wrong can be retrieved with this function.
  229. *
  230. * @param ctx The context to retrieve the error message from.
  231. * @return a pointer to the error message.
  232. **/
  233. OHMD_APIENTRYDLL const char* OHMD_APIENTRY ohmd_ctx_get_error(ohmd_context* ctx);
  234. /**
  235. * Update a context.
  236. *
  237. * Update the values for the devices handled by a context.
  238. *
  239. * If background threads are disabled, this performs tasks like pumping events from the device. The exact details
  240. * are up to the driver but try to call it quite frequently.
  241. * Once per frame in a "game loop" should be sufficient.
  242. * If OpenHMD is handled in a background thread in your program, calling ohmd_ctx_update and then sleeping for 10-20 ms
  243. * is recommended.
  244. *
  245. * @param ctx The context that needs updating.
  246. **/
  247. OHMD_APIENTRYDLL void OHMD_APIENTRY ohmd_ctx_update(ohmd_context* ctx);
  248. /**
  249. * Probe for devices.
  250. *
  251. * Probes for and enumerates supported devices attached to the system.
  252. *
  253. * @param ctx A context with no currently open devices.
  254. * @return the number of devices found on the system.
  255. **/
  256. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_ctx_probe(ohmd_context* ctx);
  257. /**
  258. * Get string from openhmd.
  259. *
  260. * Gets a string from OpenHMD. This is where non-device specific strings reside.
  261. * This is where the distortion shader sources can be retrieved.
  262. *
  263. * @param type The name of the string to fetch. One of OHMD_GLSL_DISTORTION_FRAG_SRC, and OHMD_GLSL_DISTORTION_FRAG_SRC.
  264. * @param out The location to return a const char*
  265. * @return 0 on success, <0 on failure.
  266. **/
  267. OHMD_APIENTRYDLL int ohmd_gets(ohmd_string_description type, const char** out);
  268. /**
  269. * Get device description from enumeration list index.
  270. *
  271. * Gets a human readable device description string from a zero indexed enumeration index
  272. * between 0 and (max - 1), where max is the number ohmd_ctx_probe returned
  273. * (i.e. if ohmd_ctx_probe returns 3, valid indices are 0, 1 and 2).
  274. * The function can return three types of data. The vendor name, the product name and
  275. * a driver specific path where the device is attached.
  276. *
  277. * ohmd_ctx_probe must be called before calling ohmd_list_gets.
  278. *
  279. * @param ctx A (probed) context.
  280. * @param index An index, between 0 and the value returned from ohmd_ctx_probe.
  281. * @param type The type of data to fetch. One of OHMD_VENDOR, OHMD_PRODUCT and OHMD_PATH.
  282. * @return a string with a human readable device name.
  283. **/
  284. OHMD_APIENTRYDLL const char* OHMD_APIENTRY ohmd_list_gets(ohmd_context* ctx, int index, ohmd_string_value type);
  285. /**
  286. * Get integer value from enumeration list index.
  287. *
  288. *
  289. *
  290. * ohmd_ctx_probe must be called before calling ohmd_list_gets.
  291. *
  292. * @param ctx A (probed) context.
  293. * @param index An index, between 0 and the value returned from ohmd_ctx_probe.
  294. * @param type What type of value to retrieve, ohmd_int_value section for more information.
  295. * @return 0 on success, <0 on failure.
  296. **/
  297. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_list_geti(ohmd_context* ctx, int index, ohmd_int_value type, int* out);
  298. /**
  299. * Open a device.
  300. *
  301. * Opens a device from a zero indexed enumeration index between 0 and (max - 1)
  302. * where max is the number ohmd_ctx_probe returned (i.e. if ohmd_ctx_probe returns 3,
  303. * valid indices are 0, 1 and 2).
  304. *
  305. * ohmd_ctx_probe must be called before calling ohmd_list_open_device.
  306. *
  307. * @param ctx A (probed) context.
  308. * @param index An index, between 0 and the value returned from ohmd_ctx_probe.
  309. * @return a pointer to an ohmd_device, which represents a hardware device, such as an HMD.
  310. **/
  311. OHMD_APIENTRYDLL ohmd_device* OHMD_APIENTRY ohmd_list_open_device(ohmd_context* ctx, int index);
  312. /**
  313. * Open a device with additional settings provided.
  314. *
  315. * Opens a device from a zero indexed enumeration index between 0 and (max - 1)
  316. * where max is the number ohmd_ctx_probe returned (i.e. if ohmd_ctx_probe returns 3,
  317. * valid indices are 0, 1 and 2).
  318. *
  319. * ohmd_ctx_probe must be called before calling ohmd_list_open_device.
  320. *
  321. * @param ctx A (probed) context.
  322. * @param index An index, between 0 and the value returned from ohmd_ctx_probe.
  323. * @param settings A pointer to a device settings struct.
  324. * @return a pointer to an ohmd_device, which represents a hardware device, such as an HMD.
  325. **/
  326. OHMD_APIENTRYDLL ohmd_device* OHMD_APIENTRY ohmd_list_open_device_s(ohmd_context* ctx, int index, ohmd_device_settings* settings);
  327. /**
  328. * Specify int settings in a device settings struct.
  329. *
  330. * @param settings The device settings struct to set values to.
  331. * @param key The specefic setting you wish to set.
  332. * @param value A pointer to an int or int array (containing the expected number of elements) with the value(s) you wish to set.
  333. **/
  334. OHMD_APIENTRYDLL ohmd_status OHMD_APIENTRY ohmd_device_settings_seti(ohmd_device_settings* settings, ohmd_int_settings key, const int* val);
  335. /**
  336. * Create a device settings instance.
  337. *
  338. * @param ctx A pointer to a valid ohmd_context.
  339. * @return a pointer to an allocated ohmd_context on success or NULL if it fails.
  340. **/
  341. OHMD_APIENTRYDLL ohmd_device_settings* OHMD_APIENTRY ohmd_device_settings_create(ohmd_context* ctx);
  342. /**
  343. * Destroy a device settings instance.
  344. *
  345. * @param ctx The device settings instance to destroy.
  346. **/
  347. OHMD_APIENTRYDLL void OHMD_APIENTRY ohmd_device_settings_destroy(ohmd_device_settings* settings);
  348. /**
  349. * Close a device.
  350. *
  351. * Closes a device opened by ohmd_list_open_device. Note that ohmd_ctx_destroy automatically closes any open devices
  352. * associated with the context being destroyed.
  353. *
  354. * @param device The open device.
  355. * @return 0 on success, <0 on failure.
  356. **/
  357. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_close_device(ohmd_device* device);
  358. /**
  359. * Get a floating point value from a device.
  360. *
  361. *
  362. * @param device An open device to retrieve the value from.
  363. * @param type What type of value to retrieve, see ohmd_float_value section for more information.
  364. * @param[out] out A pointer to a float, or float array where the retrieved value should be written.
  365. * @return 0 on success, <0 on failure.
  366. **/
  367. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_device_getf(ohmd_device* device, ohmd_float_value type, float* out);
  368. /**
  369. * Set a floating point value for a device.
  370. *
  371. * @param device An open device to set the value in.
  372. * @param type What type of value to set, see ohmd_float_value section for more information.
  373. * @param in A pointer to a float, or float array where the new value is stored.
  374. * @return 0 on success, <0 on failure.
  375. **/
  376. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_device_setf(ohmd_device* device, ohmd_float_value type, const float* in);
  377. /**
  378. * Get an integer value from a device.
  379. *
  380. * @param device An open device to retrieve the value from.
  381. * @param type What type of value to retrieve, ohmd_int_value section for more information.
  382. * @param[out] out A pointer to an integer, or integer array where the retrieved value should be written.
  383. * @return 0 on success, <0 on failure.
  384. **/
  385. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_device_geti(ohmd_device* device, ohmd_int_value type, int* out);
  386. /**
  387. * Set an integer value for a device.
  388. *
  389. * @param device An open device to set the value in.
  390. * @param type What type of value to set, see ohmd_float_value section for more information.
  391. * @param in A pointer to a int, or int array where the new value is stored.
  392. * @return 0 on success, <0 on failure.
  393. **/
  394. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_device_seti(ohmd_device* device, ohmd_int_value type, const int* in);
  395. /**
  396. * Set an void* data value for a device.
  397. *
  398. * @param device An open device to set the value in.
  399. * @param type What type of value to set, see ohmd_float_value section for more information.
  400. * @param in A pointer to the void* casted object.
  401. * @return 0 on success, <0 on failure.
  402. **/
  403. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_device_set_data(ohmd_device* device, ohmd_data_value type, const void* in);
  404. #ifdef __cplusplus
  405. }
  406. #endif
  407. #endif