openhmd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. /* Main Lib Implementation */
  8. #include "openhmdi.h"
  9. #include "shaders.h"
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #define DIGITAL_INPUT_EVENT_QUEUE_SIZE 1024
  14. // Running automatic updates at 1000 Hz
  15. #define AUTOMATIC_UPDATE_SLEEP (1.0 / 1000.0)
  16. ohmd_context* OHMD_APIENTRY ohmd_ctx_create(void)
  17. {
  18. ohmd_context* ctx = calloc(1, sizeof(ohmd_context));
  19. if(!ctx){
  20. LOGE("could not allocate RAM for context");
  21. return NULL;
  22. }
  23. ohmd_monotonic_init(ctx);
  24. #if DRIVER_OCULUS_RIFT
  25. ctx->drivers[ctx->num_drivers++] = ohmd_create_oculus_rift_drv(ctx);
  26. #endif
  27. #if DRIVER_DEEPOON
  28. ctx->drivers[ctx->num_drivers++] = ohmd_create_deepoon_drv(ctx);
  29. #endif
  30. #if DRIVER_HTC_VIVE
  31. ctx->drivers[ctx->num_drivers++] = ohmd_create_htc_vive_drv(ctx);
  32. #endif
  33. #if DRIVER_PSVR
  34. ctx->drivers[ctx->num_drivers++] = ohmd_create_psvr_drv(ctx);
  35. #endif
  36. #if DRIVER_NOLO
  37. ctx->drivers[ctx->num_drivers++] = ohmd_create_nolo_drv(ctx);
  38. #endif
  39. #if DRIVER_EXTERNAL
  40. ctx->drivers[ctx->num_drivers++] = ohmd_create_external_drv(ctx);
  41. #endif
  42. #if DRIVER_ANDROID
  43. ctx->drivers[ctx->num_drivers++] = ohmd_create_android_drv(ctx);
  44. #endif
  45. // add dummy driver last to make it the lowest priority
  46. ctx->drivers[ctx->num_drivers++] = ohmd_create_dummy_drv(ctx);
  47. ctx->update_request_quit = false;
  48. return ctx;
  49. }
  50. void OHMD_APIENTRY ohmd_ctx_destroy(ohmd_context* ctx)
  51. {
  52. ctx->update_request_quit = true;
  53. for(int i = 0; i < ctx->num_active_devices; i++){
  54. ctx->active_devices[i]->close(ctx->active_devices[i]);
  55. }
  56. for(int i = 0; i < ctx->num_drivers; i++){
  57. ctx->drivers[i]->destroy(ctx->drivers[i]);
  58. }
  59. if(ctx->update_thread){
  60. ohmd_destroy_thread(ctx->update_thread);
  61. ohmd_destroy_mutex(ctx->update_mutex);
  62. }
  63. free(ctx);
  64. }
  65. void OHMD_APIENTRY ohmd_ctx_update(ohmd_context* ctx)
  66. {
  67. for(int i = 0; i < ctx->num_active_devices; i++){
  68. ohmd_device* dev = ctx->active_devices[i];
  69. if(!dev->settings.automatic_update && dev->update)
  70. dev->update(dev);
  71. ohmd_lock_mutex(ctx->update_mutex);
  72. dev->getf(dev, OHMD_POSITION_VECTOR, (float*)&dev->position);
  73. dev->getf(dev, OHMD_ROTATION_QUAT, (float*)&dev->rotation);
  74. ohmd_unlock_mutex(ctx->update_mutex);
  75. }
  76. }
  77. const char* OHMD_APIENTRY ohmd_ctx_get_error(ohmd_context* ctx)
  78. {
  79. return ctx->error_msg;
  80. }
  81. int OHMD_APIENTRY ohmd_ctx_probe(ohmd_context* ctx)
  82. {
  83. memset(&ctx->list, 0, sizeof(ohmd_device_list));
  84. for(int i = 0; i < ctx->num_drivers; i++){
  85. ctx->drivers[i]->get_device_list(ctx->drivers[i], &ctx->list);
  86. }
  87. return ctx->list.num_devices;
  88. }
  89. int OHMD_APIENTRY ohmd_gets(ohmd_string_description type, const char ** out)
  90. {
  91. switch(type){
  92. case OHMD_GLSL_DISTORTION_VERT_SRC:
  93. *out = distortion_vert;
  94. return OHMD_S_OK;
  95. case OHMD_GLSL_DISTORTION_FRAG_SRC:
  96. *out = distortion_frag;
  97. return OHMD_S_OK;
  98. default:
  99. return OHMD_S_UNSUPPORTED;
  100. }
  101. }
  102. const char* OHMD_APIENTRY ohmd_list_gets(ohmd_context* ctx, int index, ohmd_string_value type)
  103. {
  104. if(index >= ctx->list.num_devices)
  105. return NULL;
  106. switch(type){
  107. case OHMD_VENDOR:
  108. return ctx->list.devices[index].vendor;
  109. case OHMD_PRODUCT:
  110. return ctx->list.devices[index].product;
  111. case OHMD_PATH:
  112. return ctx->list.devices[index].path;
  113. default:
  114. return NULL;
  115. }
  116. }
  117. static unsigned int ohmd_update_thread(void* arg)
  118. {
  119. ohmd_context* ctx = (ohmd_context*)arg;
  120. while(!ctx->update_request_quit)
  121. {
  122. ohmd_lock_mutex(ctx->update_mutex);
  123. for(int i = 0; i < ctx->num_active_devices; i++){
  124. if(ctx->active_devices[i]->settings.automatic_update && ctx->active_devices[i]->update)
  125. ctx->active_devices[i]->update(ctx->active_devices[i]);
  126. }
  127. ohmd_unlock_mutex(ctx->update_mutex);
  128. ohmd_sleep(AUTOMATIC_UPDATE_SLEEP);
  129. }
  130. return 0;
  131. }
  132. static void ohmd_set_up_update_thread(ohmd_context* ctx)
  133. {
  134. if(!ctx->update_thread){
  135. ctx->update_mutex = ohmd_create_mutex(ctx);
  136. ctx->update_thread = ohmd_create_thread(ctx, ohmd_update_thread, ctx);
  137. }
  138. }
  139. ohmd_device* OHMD_APIENTRY ohmd_list_open_device_s(ohmd_context* ctx, int index, ohmd_device_settings* settings)
  140. {
  141. ohmd_lock_mutex(ctx->update_mutex);
  142. if(index >= 0 && index < ctx->list.num_devices){
  143. ohmd_device_desc* desc = &ctx->list.devices[index];
  144. ohmd_driver* driver = (ohmd_driver*)desc->driver_ptr;
  145. ohmd_device* device = driver->open_device(driver, desc);
  146. if (device == NULL) {
  147. ohmd_unlock_mutex(ctx->update_mutex);
  148. return NULL;
  149. }
  150. device->rotation_correction.w = 1;
  151. device->settings = *settings;
  152. device->ctx = ctx;
  153. device->active_device_idx = ctx->num_active_devices;
  154. ctx->active_devices[ctx->num_active_devices++] = device;
  155. if(device->properties.digital_button_count > 0)
  156. device->digital_input_event_queue = ohmdq_create(ctx, sizeof(ohmd_digital_input_event), DIGITAL_INPUT_EVENT_QUEUE_SIZE);
  157. ohmd_unlock_mutex(ctx->update_mutex);
  158. if(device->settings.automatic_update)
  159. ohmd_set_up_update_thread(ctx);
  160. return device;
  161. }
  162. ohmd_unlock_mutex(ctx->update_mutex);
  163. ohmd_set_error(ctx, "no device with index: %d", index);
  164. return NULL;
  165. }
  166. ohmd_device* OHMD_APIENTRY ohmd_list_open_device(ohmd_context* ctx, int index)
  167. {
  168. ohmd_device_settings settings;
  169. settings.automatic_update = true;
  170. return ohmd_list_open_device_s(ctx, index, &settings);
  171. }
  172. int OHMD_APIENTRY ohmd_close_device(ohmd_device* device)
  173. {
  174. ohmd_lock_mutex(device->ctx->update_mutex);
  175. ohmd_context* ctx = device->ctx;
  176. int idx = device->active_device_idx;
  177. ohmdq* dinq = device->digital_input_event_queue;
  178. memmove(ctx->active_devices + idx, ctx->active_devices + idx + 1,
  179. sizeof(ohmd_device*) * (ctx->num_active_devices - idx - 1));
  180. device->close(device);
  181. if(dinq)
  182. ohmdq_destroy(dinq);
  183. ctx->num_active_devices--;
  184. for(int i = idx; i < ctx->num_active_devices; i++)
  185. ctx->active_devices[i]->active_device_idx--;
  186. ohmd_unlock_mutex(ctx->update_mutex);
  187. return OHMD_S_OK;
  188. }
  189. static int ohmd_device_getf_unp(ohmd_device* device, ohmd_float_value type, float* out)
  190. {
  191. switch(type){
  192. case OHMD_LEFT_EYE_GL_MODELVIEW_MATRIX: {
  193. vec3f point = {{0, 0, 0}};
  194. quatf rot = device->rotation;
  195. quatf tmp = device->rotation_correction;
  196. oquatf_mult_me(&tmp, &rot);
  197. rot = tmp;
  198. mat4x4f orient, world_shift, result;
  199. omat4x4f_init_look_at(&orient, &rot, &point);
  200. omat4x4f_init_translate(&world_shift, -device->position.x +(device->properties.ipd / 2.0f), -device->position.y, -device->position.z);
  201. omat4x4f_mult(&world_shift, &orient, &result);
  202. omat4x4f_transpose(&result, (mat4x4f*)out);
  203. return OHMD_S_OK;
  204. }
  205. case OHMD_RIGHT_EYE_GL_MODELVIEW_MATRIX: {
  206. vec3f point = {{0, 0, 0}};
  207. quatf rot = device->rotation;
  208. oquatf_mult_me(&rot, &device->rotation_correction);
  209. mat4x4f orient, world_shift, result;
  210. omat4x4f_init_look_at(&orient, &rot, &point);
  211. omat4x4f_init_translate(&world_shift, -device->position.x + -(device->properties.ipd / 2.0f), -device->position.y, -device->position.z);
  212. omat4x4f_mult(&world_shift, &orient, &result);
  213. omat4x4f_transpose(&result, (mat4x4f*)out);
  214. return OHMD_S_OK;
  215. }
  216. case OHMD_LEFT_EYE_GL_PROJECTION_MATRIX:
  217. omat4x4f_transpose(&device->properties.proj_left, (mat4x4f*)out);
  218. return OHMD_S_OK;
  219. case OHMD_RIGHT_EYE_GL_PROJECTION_MATRIX:
  220. omat4x4f_transpose(&device->properties.proj_right, (mat4x4f*)out);
  221. return OHMD_S_OK;
  222. case OHMD_SCREEN_HORIZONTAL_SIZE:
  223. *out = device->properties.hsize;
  224. return OHMD_S_OK;
  225. case OHMD_SCREEN_VERTICAL_SIZE:
  226. *out = device->properties.vsize;
  227. return OHMD_S_OK;
  228. case OHMD_LENS_HORIZONTAL_SEPARATION:
  229. *out = device->properties.lens_sep;
  230. return OHMD_S_OK;
  231. case OHMD_LENS_VERTICAL_POSITION:
  232. *out = device->properties.lens_vpos;
  233. return OHMD_S_OK;
  234. case OHMD_RIGHT_EYE_FOV:
  235. case OHMD_LEFT_EYE_FOV:
  236. *out = device->properties.fov;
  237. return OHMD_S_OK;
  238. case OHMD_RIGHT_EYE_ASPECT_RATIO:
  239. case OHMD_LEFT_EYE_ASPECT_RATIO:
  240. *out = device->properties.ratio;
  241. return OHMD_S_OK;
  242. case OHMD_EYE_IPD:
  243. *out = device->properties.ipd;
  244. return OHMD_S_OK;
  245. case OHMD_PROJECTION_ZFAR:
  246. *out = device->properties.zfar;
  247. return OHMD_S_OK;
  248. case OHMD_PROJECTION_ZNEAR:
  249. *out = device->properties.znear;
  250. return OHMD_S_OK;
  251. case OHMD_ROTATION_QUAT:
  252. {
  253. *(quatf*)out = device->rotation;
  254. oquatf_mult_me((quatf*)out, &device->rotation_correction);
  255. quatf tmp = device->rotation_correction;
  256. oquatf_mult_me(&tmp, (quatf*)out);
  257. *(quatf*)out = tmp;
  258. return OHMD_S_OK;
  259. }
  260. case OHMD_POSITION_VECTOR:
  261. {
  262. *(vec3f*)out = device->position;
  263. for(int i = 0; i < 3; i++)
  264. out[i] += device->position_correction.arr[i];
  265. return OHMD_S_OK;
  266. }
  267. case OHMD_UNIVERSAL_DISTORTION_K: {
  268. for (int i = 0; i < 4; i++) {
  269. out[i] = device->properties.universal_distortion_k[i];
  270. }
  271. return OHMD_S_OK;
  272. }
  273. case OHMD_UNIVERSAL_ABERRATION_K: {
  274. for (int i = 0; i < 3; i++) {
  275. out[i] = device->properties.universal_aberration_k[i];
  276. }
  277. return OHMD_S_OK;
  278. }
  279. default:
  280. return device->getf(device, type, out);
  281. }
  282. }
  283. int OHMD_APIENTRY ohmd_device_getf(ohmd_device* device, ohmd_float_value type, float* out)
  284. {
  285. ohmd_lock_mutex(device->ctx->update_mutex);
  286. int ret = ohmd_device_getf_unp(device, type, out);
  287. ohmd_unlock_mutex(device->ctx->update_mutex);
  288. return ret;
  289. }
  290. int ohmd_device_setf_unp(ohmd_device* device, ohmd_float_value type, const float* in)
  291. {
  292. switch(type){
  293. case OHMD_EYE_IPD:
  294. device->properties.ipd = *in;
  295. return OHMD_S_OK;
  296. case OHMD_PROJECTION_ZFAR:
  297. device->properties.zfar = *in;
  298. return OHMD_S_OK;
  299. case OHMD_PROJECTION_ZNEAR:
  300. device->properties.znear = *in;
  301. return OHMD_S_OK;
  302. case OHMD_ROTATION_QUAT:
  303. {
  304. // adjust rotation correction
  305. quatf q;
  306. int ret = device->getf(device, OHMD_ROTATION_QUAT, (float*)&q);
  307. if(ret != 0){
  308. return ret;
  309. }
  310. oquatf_diff(&q, (quatf*)in, &device->rotation_correction);
  311. return OHMD_S_OK;
  312. }
  313. case OHMD_POSITION_VECTOR:
  314. {
  315. // adjust position correction
  316. vec3f v;
  317. int ret = device->getf(device, OHMD_POSITION_VECTOR, (float*)&v);
  318. if(ret != 0){
  319. return ret;
  320. }
  321. for(int i = 0; i < 3; i++)
  322. device->position_correction.arr[i] = in[i] - v.arr[i];
  323. return OHMD_S_OK;
  324. }
  325. case OHMD_EXTERNAL_SENSOR_FUSION:
  326. {
  327. if(device->setf == NULL)
  328. return OHMD_S_UNSUPPORTED;
  329. return device->setf(device, type, in);
  330. }
  331. default:
  332. return OHMD_S_INVALID_PARAMETER;
  333. }
  334. }
  335. int OHMD_APIENTRY ohmd_device_setf(ohmd_device* device, ohmd_float_value type, const float* in)
  336. {
  337. ohmd_lock_mutex(device->ctx->update_mutex);
  338. int ret = ohmd_device_setf_unp(device, type, in);
  339. ohmd_unlock_mutex(device->ctx->update_mutex);
  340. return ret;
  341. }
  342. int OHMD_APIENTRY ohmd_device_geti(ohmd_device* device, ohmd_int_value type, int* out)
  343. {
  344. ohmdq* dinq = device->digital_input_event_queue;
  345. switch(type){
  346. case OHMD_SCREEN_HORIZONTAL_RESOLUTION:
  347. *out = device->properties.hres;
  348. return OHMD_S_OK;
  349. case OHMD_SCREEN_VERTICAL_RESOLUTION:
  350. *out = device->properties.vres;
  351. return OHMD_S_OK;
  352. case OHMD_BUTTON_EVENT_COUNT:
  353. *out = dinq ? (int)ohmdq_get_size(dinq) : 0;
  354. return OHMD_S_OK;
  355. case OHMD_BUTTON_EVENT_OVERFLOW:
  356. *out = dinq ? (ohmdq_get_size(dinq) == ohmdq_get_max(dinq)) : 0;
  357. return OHMD_S_OK;
  358. case OHMD_BUTTON_COUNT:
  359. *out = device->properties.digital_button_count;
  360. return OHMD_S_OK;
  361. case OHMD_BUTTON_POP_EVENT: {
  362. ohmd_digital_input_event event;
  363. if(!ohmdq_pop(dinq, &event)){
  364. return OHMD_S_INVALID_OPERATION;
  365. }
  366. out[0] = event.idx;
  367. out[1] = event.state;
  368. return OHMD_S_OK;
  369. }
  370. default:
  371. return OHMD_S_INVALID_PARAMETER;
  372. }
  373. }
  374. int OHMD_APIENTRY ohmd_device_seti(ohmd_device* device, ohmd_int_value type, const int* in)
  375. {
  376. switch(type){
  377. default:
  378. return OHMD_S_INVALID_PARAMETER;
  379. }
  380. }
  381. int ohmd_device_set_data_unp(ohmd_device* device, ohmd_data_value type, const void* in)
  382. {
  383. switch(type){
  384. case OHMD_DRIVER_DATA:
  385. device->set_data(device, OHMD_DRIVER_DATA, in);
  386. return OHMD_S_OK;
  387. case OHMD_DRIVER_PROPERTIES:
  388. device->set_data(device, OHMD_DRIVER_PROPERTIES, in);
  389. return OHMD_S_OK;
  390. default:
  391. return OHMD_S_INVALID_PARAMETER;
  392. }
  393. }
  394. int OHMD_APIENTRY ohmd_device_set_data(ohmd_device* device, ohmd_data_value type, const void* in)
  395. {
  396. ohmd_lock_mutex(device->ctx->update_mutex);
  397. int ret = ohmd_device_set_data_unp(device, type, in);
  398. ohmd_unlock_mutex(device->ctx->update_mutex);
  399. return ret;
  400. }
  401. ohmd_status OHMD_APIENTRY ohmd_device_settings_seti(ohmd_device_settings* settings, ohmd_int_settings key, const int* val)
  402. {
  403. switch(key){
  404. case OHMD_IDS_AUTOMATIC_UPDATE:
  405. settings->automatic_update = val[0] == 0 ? false : true;
  406. return OHMD_S_OK;
  407. default:
  408. return OHMD_S_INVALID_PARAMETER;
  409. }
  410. }
  411. ohmd_device_settings* OHMD_APIENTRY ohmd_device_settings_create(ohmd_context* ctx)
  412. {
  413. return ohmd_alloc(ctx, sizeof(ohmd_device_settings));
  414. }
  415. void OHMD_APIENTRY ohmd_device_settings_destroy(ohmd_device_settings* settings)
  416. {
  417. free(settings);
  418. }
  419. void* ohmd_allocfn(ohmd_context* ctx, const char* e_msg, size_t size)
  420. {
  421. void* ret = calloc(1, size);
  422. if(!ret)
  423. ohmd_set_error(ctx, "%s", e_msg);
  424. return ret;
  425. }
  426. void ohmd_set_default_device_properties(ohmd_device_properties* props)
  427. {
  428. props->ipd = 0.061f;
  429. props->znear = 0.1f;
  430. props->zfar = 1000.0f;
  431. ohmd_set_universal_distortion_k(props, 0, 0, 0, 1);
  432. ohmd_set_universal_aberration_k(props, 1.0, 1.0, 1.0);
  433. }
  434. void ohmd_calc_default_proj_matrices(ohmd_device_properties* props)
  435. {
  436. mat4x4f proj_base; // base projection matrix
  437. // Calculate where the lens is on each screen,
  438. // and with the given value offset the projection matrix.
  439. float screen_center = props->hsize / 4.0f;
  440. float lens_shift = screen_center - props->lens_sep / 2.0f;
  441. // XXX: on CV1, props->hsize > props->lens_sep / 2.0,
  442. // I am not sure about the implications, but just taking the absolute
  443. // value of the offset seems to work.
  444. float proj_offset = fabs(4.0f * lens_shift / props->hsize);
  445. // Setup the base projection matrix. Each eye mostly have the
  446. // same projection matrix with the exception of the offset.
  447. omat4x4f_init_perspective(&proj_base, props->fov, props->ratio, props->znear, props->zfar);
  448. // Setup the two adjusted projection matrices. Each is setup to deal
  449. // with the fact that the lens is not in the center of the screen.
  450. // These matrices only change of the hardware changes, so static.
  451. mat4x4f translate;
  452. omat4x4f_init_translate(&translate, proj_offset, 0, 0);
  453. omat4x4f_mult(&translate, &proj_base, &props->proj_left);
  454. omat4x4f_init_translate(&translate, -proj_offset, 0, 0);
  455. omat4x4f_mult(&translate, &proj_base, &props->proj_right);
  456. }
  457. void ohmd_set_universal_distortion_k(ohmd_device_properties* props, float a, float b, float c, float d)
  458. {
  459. props->universal_distortion_k[0] = a;
  460. props->universal_distortion_k[1] = b;
  461. props->universal_distortion_k[2] = c;
  462. props->universal_distortion_k[3] = d;
  463. }
  464. void ohmd_set_universal_aberration_k(ohmd_device_properties* props, float r, float g, float b)
  465. {
  466. props->universal_aberration_k[0] = r;
  467. props->universal_aberration_k[1] = g;
  468. props->universal_aberration_k[2] = b;
  469. }
  470. uint64_t ohmd_monotonic_per_sec(ohmd_context* ctx)
  471. {
  472. return ctx->monotonic_ticks_per_sec;
  473. }
  474. /*
  475. * Grabbed from druntime, good thing it's BOOST v1.0 as well.
  476. */
  477. uint64_t ohmd_monotonic_conv(uint64_t ticks, uint64_t srcTicksPerSecond, uint64_t dstTicksPerSecond)
  478. {
  479. // This would be more straightforward with floating point arithmetic,
  480. // but we avoid it here in order to avoid the rounding errors that that
  481. // introduces. Also, by splitting out the units in this way, we're able
  482. // to deal with much larger values before running into problems with
  483. // integer overflow.
  484. return ticks / srcTicksPerSecond * dstTicksPerSecond +
  485. ticks % srcTicksPerSecond * dstTicksPerSecond / srcTicksPerSecond;
  486. }