openhmd.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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_USER_RESERVED and below can be used for user purposes, such as errors within ohmd wrappers, etc. */
  42. OHMD_S_USER_RESERVED = -16384,
  43. } ohmd_status;
  44. /** A collection of string value information types, used for getting information with ohmd_list_gets(). */
  45. typedef enum {
  46. OHMD_VENDOR = 0,
  47. OHMD_PRODUCT = 1,
  48. OHMD_PATH = 2,
  49. } ohmd_string_value;
  50. /** A collection of float value information types, used for getting and setting information with
  51. ohmd_device_getf() and ohmd_device_setf(). */
  52. typedef enum {
  53. /** float[4] (get): Absolute rotation of the device, in space, as a quaternion (x, y, z, w). */
  54. OHMD_ROTATION_QUAT = 1,
  55. /** float[16] (get): A "ready to use" OpenGL style 4x4 matrix with a modelview matrix for the
  56. left eye of the HMD. */
  57. OHMD_LEFT_EYE_GL_MODELVIEW_MATRIX = 2,
  58. /** float[16] (get): A "ready to use" OpenGL style 4x4 matrix with a modelview matrix for the
  59. right eye of the HMD. */
  60. OHMD_RIGHT_EYE_GL_MODELVIEW_MATRIX = 3,
  61. /** float[16] (get): A "ready to use" OpenGL style 4x4 matrix with a projection matrix for the
  62. left eye of the HMD. */
  63. OHMD_LEFT_EYE_GL_PROJECTION_MATRIX = 4,
  64. /** float[16] (get): A "ready to use" OpenGL style 4x4 matrix with a projection matrix for the
  65. right eye of the HMD. */
  66. OHMD_RIGHT_EYE_GL_PROJECTION_MATRIX = 5,
  67. /** float[3] (get): A 3-D vector representing the absolute position of the device, in space. */
  68. OHMD_POSITION_VECTOR = 6,
  69. /** float[1] (get): Physical width of the device screen in metres. */
  70. OHMD_SCREEN_HORIZONTAL_SIZE = 7,
  71. /** float[1] (get): Physical height of the device screen in metres. */
  72. OHMD_SCREEN_VERTICAL_SIZE = 8,
  73. /** float[1] (get): Physical separation of the device lenses in metres. */
  74. OHMD_LENS_HORIZONTAL_SEPARATION = 9,
  75. /** float[1] (get): Physical vertical position of the lenses in metres. */
  76. OHMD_LENS_VERTICAL_POSITION = 10,
  77. /** float[1] (get): Physical field of view for the left eye in degrees. */
  78. OHMD_LEFT_EYE_FOV = 11,
  79. /** float[1] (get): Physical display aspect ratio for the left eye screen. */
  80. OHMD_LEFT_EYE_ASPECT_RATIO = 12,
  81. /** float[1] (get): Physical field of view for the left right in degrees. */
  82. OHMD_RIGHT_EYE_FOV = 13,
  83. /** float[1] (get): Physical display aspect ratio for the right eye screen. */
  84. OHMD_RIGHT_EYE_ASPECT_RATIO = 14,
  85. /** float[1] (get, set): Physical interpupillary distance of the user in metres. */
  86. OHMD_EYE_IPD = 15,
  87. /** float[1] (get, set): Z-far value for the projection matrix calculations (i.e. drawing distance). */
  88. OHMD_PROJECTION_ZFAR = 16,
  89. /** float[1] (get, set): Z-near value for the projection matrix calculations (i.e. close clipping distance). */
  90. OHMD_PROJECTION_ZNEAR = 17,
  91. /** float[6] (get): Device specific distortion value. */
  92. OHMD_DISTORTION_K = 18,
  93. /**
  94. * float[10] (set): Perform sensor fusion on values from external sensors.
  95. *
  96. * Values are: dt (time since last update in seconds) X, Y, Z gyro, X, Y, Z accelerometer and X, Y, Z magnetometer.
  97. **/
  98. OHMD_EXTERNAL_SENSOR_FUSION = 19,
  99. } ohmd_float_value;
  100. /** A collection of int value information types used for getting information with ohmd_device_geti(). */
  101. typedef enum {
  102. /** int[1] (get): Physical horizontal resolution of the device screen. */
  103. OHMD_SCREEN_HORIZONTAL_RESOLUTION = 0,
  104. /** int[1] (get): Physical vertical resolution of the device screen. */
  105. OHMD_SCREEN_VERTICAL_RESOLUTION = 1,
  106. } ohmd_int_value;
  107. /** A collection of data information types used for setting information with ohmd_set_data(). */
  108. typedef enum {
  109. /** void* (set): Set void* data for use in the internal drivers. */
  110. OHMD_DRIVER_DATA = 0,
  111. /**
  112. * ohmd_device_properties* (set):
  113. * Set the device properties based on the ohmd_device_properties struct for use in the internal drivers.
  114. *
  115. * This can be used to fill in information about the device internally, such as Android, or for setting profiles.
  116. **/
  117. OHMD_DRIVER_PROPERTIES = 1,
  118. } ohmd_data_value;
  119. typedef enum {
  120. /** int[1] (set, default: 1): Set this to 0 to prevent OpenHMD from creating background threads to do automatic device updating.
  121. Note that you have to manually call ohmd_update(); at least once per frame if you disable the threads. */
  122. OHMD_IDS_AUTOMATIC_UPDATE = 0,
  123. } ohmd_int_settings;
  124. /** An opaque pointer to a context structure. */
  125. typedef struct ohmd_context ohmd_context;
  126. /** An opaque pointer to a structure representing a device, such as an HMD. */
  127. typedef struct ohmd_device ohmd_device;
  128. /** An opaque pointer to a structure representing arguments for a device. */
  129. typedef struct ohmd_device_settings ohmd_device_settings;
  130. /**
  131. * Create an OpenHMD context.
  132. *
  133. * @return a pointer to an allocated ohmd_context on success or NULL if it fails.
  134. **/
  135. OHMD_APIENTRYDLL ohmd_context* OHMD_APIENTRY ohmd_ctx_create(void);
  136. /**
  137. * Destroy an OpenHMD context.
  138. *
  139. * ohmd_ctx_destroy de-initializes and de-allocates an OpenHMD context allocated with ohmd_ctx_create.
  140. * All devices associated with the context are automatically closed.
  141. *
  142. * @param ctx The context to destroy.
  143. **/
  144. OHMD_APIENTRYDLL void OHMD_APIENTRY ohmd_ctx_destroy(ohmd_context* ctx);
  145. /**
  146. * Get the last error as a human readable string.
  147. *
  148. * If a function taking a context as an argument (ohmd_context "methods") returns non-successfully,
  149. * a human readable error message describing what went wrong can be retrieved with this function.
  150. *
  151. * @param ctx The context to retrieve the error message from.
  152. * @return a pointer to the error message.
  153. **/
  154. OHMD_APIENTRYDLL const char* OHMD_APIENTRY ohmd_ctx_get_error(ohmd_context* ctx);
  155. /**
  156. * Update a context.
  157. *
  158. * Performs tasks like pumping events from the device. The exact details are up to the driver
  159. * but try to call it quite frequently.
  160. * Once per frame in a "game loop" should be sufficient.
  161. * If OpenHMD is handled in a background thread, calling ohmd_ctx_update and then sleeping for 10-20 ms
  162. * is recommended.
  163. *
  164. * @param ctx The context that needs updating.
  165. **/
  166. OHMD_APIENTRYDLL void OHMD_APIENTRY ohmd_ctx_update(ohmd_context* ctx);
  167. /**
  168. * Probe for devices.
  169. *
  170. * Probes for and enumerates supported devices attached to the system.
  171. *
  172. * @param ctx A context with no currently open devices.
  173. * @return the number of devices found on the system.
  174. **/
  175. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_ctx_probe(ohmd_context* ctx);
  176. /**
  177. * Get device description from enumeration list index.
  178. *
  179. * Gets a human readable device description string from a zero indexed enumeration index
  180. * between 0 and (max - 1), where max is the number ohmd_ctx_probe returned
  181. * (i.e. if ohmd_ctx_probe returns 3, valid indices are 0, 1 and 2).
  182. * The function can return three types of data. The vendor name, the product name and
  183. * a driver specific path where the device is attached.
  184. *
  185. * ohmd_ctx_probe must be called before calling ohmd_list_gets.
  186. *
  187. * @param ctx A (probed) context.
  188. * @param index An index, between 0 and the value returned from ohmd_ctx_probe.
  189. * @param type The type of data to fetch. One of OHMD_VENDOR, OHMD_PRODUCT and OHMD_PATH.
  190. * @return a string with a human readable device name.
  191. **/
  192. OHMD_APIENTRYDLL const char* OHMD_APIENTRY ohmd_list_gets(ohmd_context* ctx, int index, ohmd_string_value type);
  193. /**
  194. * Open a device.
  195. *
  196. * Opens a device from a zero indexed enumeration index between 0 and (max - 1)
  197. * where max is the number ohmd_ctx_probe returned (i.e. if ohmd_ctx_probe returns 3,
  198. * valid indices are 0, 1 and 2).
  199. *
  200. * ohmd_ctx_probe must be called before calling ohmd_list_open_device.
  201. *
  202. * @param ctx A (probed) context.
  203. * @param index An index, between 0 and the value returned from ohmd_ctx_probe.
  204. * @return a pointer to an ohmd_device, which represents a hardware device, such as an HMD.
  205. **/
  206. OHMD_APIENTRYDLL ohmd_device* OHMD_APIENTRY ohmd_list_open_device(ohmd_context* ctx, int index);
  207. /**
  208. * Open a device with additional settings provided.
  209. *
  210. * Opens a device from a zero indexed enumeration index between 0 and (max - 1)
  211. * where max is the number ohmd_ctx_probe returned (i.e. if ohmd_ctx_probe returns 3,
  212. * valid indices are 0, 1 and 2).
  213. *
  214. * ohmd_ctx_probe must be called before calling ohmd_list_open_device.
  215. *
  216. * @param ctx A (probed) context.
  217. * @param index An index, between 0 and the value returned from ohmd_ctx_probe.
  218. * @param settings A pointer to a device settings struct.
  219. * @return a pointer to an ohmd_device, which represents a hardware device, such as an HMD.
  220. **/
  221. OHMD_APIENTRYDLL ohmd_device* OHMD_APIENTRY ohmd_list_open_device_s(ohmd_context* ctx, int index, ohmd_device_settings* settings);
  222. /**
  223. * Specify int settings in a device settings struct.
  224. *
  225. * @param settings The device settings struct to set values to.
  226. * @param key The specefic setting you wish to set.
  227. * @param value A pointer to an int or int array (containing the expected number of elements) with the value(s) you wish to set.
  228. **/
  229. OHMD_APIENTRYDLL void OHMD_APIENTRY ohmd_device_settings_seti(ohmd_device_settings* settings, ohmd_int_settings key, const int* val);
  230. /**
  231. * Create a device settings instance.
  232. *
  233. * @param ctx A pointer to a valid ohmd_context.
  234. * @return a pointer to an allocated ohmd_context on success or NULL if it fails.
  235. **/
  236. OHMD_APIENTRYDLL ohmd_device_settings* OHMD_APIENTRY ohmd_device_settings_create(ohmd_context* ctx);
  237. /**
  238. * Destroy a device settings instance.
  239. *
  240. * @param ctx The device settings instance to destroy.
  241. **/
  242. OHMD_APIENTRYDLL void OHMD_APIENTRY ohmd_device_settings_destroy(ohmd_device_settings* settings);
  243. /**
  244. * Close a device.
  245. *
  246. * Closes a device opened by ohmd_list_open_device. Note that ohmd_ctx_destroy automatically closes any open devices
  247. * associated with the context being destroyed.
  248. *
  249. * @param device The open device.
  250. * @return 0 on success, <0 on failure.
  251. **/
  252. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_close_device(ohmd_device* device);
  253. /**
  254. * Get a floating point value from a device.
  255. *
  256. *
  257. * @param device An open device to retrieve the value from.
  258. * @param type What type of value to retrieve, see ohmd_float_value section for more information.
  259. * @param[out] out A pointer to a float, or float array where the retrieved value should be written.
  260. * @return 0 on success, <0 on failure.
  261. **/
  262. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_device_getf(ohmd_device* device, ohmd_float_value type, float* out);
  263. /**
  264. * Set a floating point value for a device.
  265. *
  266. * @param device An open device to set the value in.
  267. * @param type What type of value to set, see ohmd_float_value section for more information.
  268. * @param in A pointer to a float, or float array where the new value is stored.
  269. * @return 0 on success, <0 on failure.
  270. **/
  271. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_device_setf(ohmd_device* device, ohmd_float_value type, const float* in);
  272. /**
  273. * Get an integer value from a device.
  274. *
  275. * @param device An open device to retrieve the value from.
  276. * @param type What type of value to retrieve, ohmd_int_value section for more information.
  277. * @param[out] out A pointer to an integer, or integer array where the retrieved value should be written.
  278. * @return 0 on success, <0 on failure.
  279. **/
  280. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_device_geti(ohmd_device* device, ohmd_int_value type, int* out);
  281. /**
  282. * Set an integer value for a device.
  283. *
  284. * @param device An open device to set the value in.
  285. * @param type What type of value to set, see ohmd_float_value section for more information.
  286. * @param in A pointer to a int, or int array where the new value is stored.
  287. * @return 0 on success, <0 on failure.
  288. **/
  289. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_device_seti(ohmd_device* device, ohmd_int_value type, const int* in);
  290. /**
  291. * Set an void* data value for a device.
  292. *
  293. * @param device An open device to set the value in.
  294. * @param type What type of value to set, see ohmd_float_value section for more information.
  295. * @param in A pointer to the void* casted object.
  296. * @return 0 on success, <0 on failure.
  297. **/
  298. OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_device_set_data(ohmd_device* device, ohmd_data_value type, const void* in);
  299. #ifdef __cplusplus
  300. }
  301. #endif
  302. #endif