openhmd.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 Implemenation */
  8. #include "openhmdi.h"
  9. #include <stdlib.h>
  10. #include <string.h>
  11. OHMD_APIENTRY ohmd_context* ohmd_ctx_create()
  12. {
  13. ohmd_context* ctx = calloc(1, sizeof(ohmd_context));
  14. if(!ctx){
  15. LOGE("could not allocate RAM for context");
  16. return NULL;
  17. }
  18. ctx->drivers[ctx->num_drivers++] = ohmd_create_oculus_rift_drv(ctx);
  19. return ctx;
  20. }
  21. OHMD_APIENTRY void ohmd_ctx_destroy(ohmd_context* ctx)
  22. {
  23. for(int i = 0; i < ctx->num_active_devices; i++){
  24. ctx->active_devices[i]->close(ctx->active_devices[i]);
  25. }
  26. for(int i = 0; i < ctx->num_drivers; i++){
  27. ctx->drivers[i]->destroy(ctx->drivers[i]);
  28. }
  29. free(ctx);
  30. }
  31. OHMD_APIENTRY void ohmd_ctx_update(ohmd_context* ctx)
  32. {
  33. for(int i = 0; i < ctx->num_active_devices; i++)
  34. ctx->active_devices[i]->update(ctx->active_devices[i]);
  35. }
  36. OHMD_APIENTRY const char* ohmd_ctx_get_error(ohmd_context* ctx)
  37. {
  38. return ctx->error_msg;
  39. }
  40. OHMD_APIENTRY int ohmd_ctx_probe(ohmd_context* ctx)
  41. {
  42. memset(&ctx->list, 0, sizeof(ohmd_device_list));
  43. for(int i = 0; i < ctx->num_drivers; i++){
  44. ctx->drivers[i]->get_device_list(ctx->drivers[i], &ctx->list);
  45. }
  46. return ctx->list.num_devices;
  47. }
  48. OHMD_APIENTRY const char* ohmd_list_gets(ohmd_context* ctx, int index, ohmd_string_value type)
  49. {
  50. if(index >= ctx->list.num_devices)
  51. return NULL;
  52. switch(type){
  53. case OHMD_VENDOR:
  54. return ctx->list.devices[index].vendor;
  55. case OHMD_PRODUCT:
  56. return ctx->list.devices[index].product;
  57. case OHMD_PATH:
  58. return ctx->list.devices[index].path;
  59. default:
  60. return NULL;
  61. }
  62. }
  63. OHMD_APIENTRY ohmd_device* ohmd_list_open_device(ohmd_context* ctx, int index)
  64. {
  65. if(index >= 0 && index < ctx->list.num_devices){
  66. ohmd_device_desc* desc = &ctx->list.devices[index];
  67. ohmd_driver* driver = (ohmd_driver*)desc->driver_ptr;
  68. ohmd_device* device = driver->open_device(driver, desc);
  69. if (device == NULL)
  70. return NULL;
  71. ctx->active_devices[ctx->num_active_devices++] = device;
  72. return device;
  73. }
  74. ohmd_set_error(ctx, "no device with index: %d", index);
  75. return NULL;
  76. }
  77. OHMD_APIENTRY int ohmd_device_getf(ohmd_device* device, ohmd_float_value type, float* out)
  78. {
  79. return device->getf(device, type, out);
  80. }
  81. void* ohmd_allocfn(ohmd_context* ctx, char* e_msg, size_t size)
  82. {
  83. void* ret = calloc(1, size);
  84. if(!ret)
  85. ohmd_set_error(ctx, "%s", e_msg);
  86. return ret;
  87. }