1 // Copyright 2013, Fredrik Hultin.
2 // Copyright 2013, Jakob Bornecrantz.
3 // SPDX-License-Identifier: BSL-1.0
4 /*
5  * OpenHMD - Free and Open Source API and drivers for immersive technology.
6  */
7 
8 /* Internal interface */
9 
10 
11 #ifndef OPENHMDI_H
12 #define OPENHMDI_H
13 
14 #include <stdbool.h>
15 #include <stdint.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 
19 #include "openhmd.h"
20 #include "omath.h"
21 #include "platform.h"
22 
23 #define OHMD_MAX_DEVICES 16
24 
25 #define OHMD_MAX(_a, _b) ((_a) > (_b) ? (_a) : (_b))
26 #define OHMD_MIN(_a, _b) ((_a) < (_b) ? (_a) : (_b))
27 
28 #define OHMD_STRINGIFY(_what) #_what
29 
30 #define OHMD_VERSION_MAJOR 0
31 #define OHMD_VERSION_MINOR 3
32 #define OHMD_VERSION_PATCH 0
33 
34 typedef struct ohmd_driver ohmd_driver;
35 
36 typedef struct {
37 	char driver[OHMD_STR_SIZE];
38 	char vendor[OHMD_STR_SIZE];
39 	char product[OHMD_STR_SIZE];
40 	char path[OHMD_STR_SIZE];
41 	int revision;
42 	int id;
43 	ohmd_device_flags device_flags;
44 	ohmd_device_class device_class;
45 	ohmd_driver* driver_ptr;
46 } ohmd_device_desc;
47 
48 typedef struct {
49 	int num_devices;
50 	ohmd_device_desc devices[OHMD_MAX_DEVICES];
51 } ohmd_device_list;
52 
53 struct ohmd_driver {
54 	void (*get_device_list)(ohmd_driver* driver, ohmd_device_list* list);
55 	ohmd_device* (*open_device)(ohmd_driver* driver, ohmd_device_desc* desc);
56 	void (*destroy)(ohmd_driver* driver);
57 	ohmd_context* ctx;
58 };
59 
60 typedef struct {
61 		int hres;
62 		int vres;
63 		int control_count;
64 		int controls_hints[64];
65 		int controls_types[64];
66 
67 		float hsize;
68 		float vsize;
69 
70 		float lens_sep;
71 		float lens_vpos;
72 
73 		float fov;
74 		float ratio;
75 
76 		float ipd;
77 		float zfar;
78 		float znear;
79 
80 		int accel_only; //bool-like for setting acceleration only fallback (android driver)
81 
82 		mat4x4f proj_left; // adjusted projection matrix for left screen
83 		mat4x4f proj_right; // adjusted projection matrix for right screen
84 		float universal_distortion_k[4]; //PanoTools lens distiorion model [a,b,c,d]
85 		float universal_aberration_k[3]; //post-warp per channel scaling [r,g,b]
86 } ohmd_device_properties;
87 
88 struct ohmd_device_settings
89 {
90 	bool automatic_update;
91 };
92 
93 struct ohmd_device {
94 	ohmd_device_properties properties;
95 
96 	quatf rotation_correction;
97 	vec3f position_correction;
98 
99 	int (*getf)(ohmd_device* device, ohmd_float_value type, float* out);
100 	int (*setf)(ohmd_device* device, ohmd_float_value type, const float* in);
101 	int (*seti)(ohmd_device* device, ohmd_int_value type, const int* in);
102 	int (*set_data)(ohmd_device* device, ohmd_data_value type, const void* in);
103 
104 	void (*update)(ohmd_device* device);
105 	void (*close)(ohmd_device* device);
106 
107 	ohmd_context* ctx;
108 
109 	ohmd_device_settings settings;
110 
111 	int active_device_idx; // index into ohmd_device->active_devices[]
112 
113 	quatf rotation;
114 	vec3f position;
115 };
116 
117 
118 struct ohmd_context {
119 	ohmd_driver* drivers[16];
120 	int num_drivers;
121 
122 	ohmd_device_list list;
123 
124 	ohmd_device* active_devices[256];
125 	int num_active_devices;
126 
127 	ohmd_thread* update_thread;
128 	ohmd_mutex* update_mutex;
129 
130 	bool update_request_quit;
131 
132 	uint64_t monotonic_ticks_per_sec;
133 
134 	char error_msg[OHMD_STR_SIZE];
135 };
136 
137 // helper functions
138 void ohmd_monotonic_init(ohmd_context* ctx);
139 uint64_t ohmd_monotonic_get(ohmd_context* ctx);
140 uint64_t ohmd_monotonic_per_sec(ohmd_context* ctx);
141 uint64_t ohmd_monotonic_conv(uint64_t ticks, uint64_t srcTicksPerSecond, uint64_t dstTicksPerSecond);
142 void ohmd_set_default_device_properties(ohmd_device_properties* props);
143 void ohmd_calc_default_proj_matrices(ohmd_device_properties* props);
144 void ohmd_set_universal_distortion_k(ohmd_device_properties* props, float a, float b, float c, float d);
145 void ohmd_set_universal_aberration_k(ohmd_device_properties* props, float r, float g, float b);
146 
147 // drivers
148 ohmd_driver* ohmd_create_dummy_drv(ohmd_context* ctx);
149 ohmd_driver* ohmd_create_oculus_rift_drv(ohmd_context* ctx);
150 ohmd_driver* ohmd_create_deepoon_drv(ohmd_context* ctx);
151 ohmd_driver* ohmd_create_htc_vive_drv(ohmd_context* ctx);
152 ohmd_driver* ohmd_create_wmr_drv(ohmd_context* ctx);
153 ohmd_driver* ohmd_create_psvr_drv(ohmd_context* ctx);
154 ohmd_driver* ohmd_create_nolo_drv(ohmd_context* ctx);
155 ohmd_driver* ohmd_create_xgvr_drv(ohmd_context* ctx);
156 ohmd_driver* ohmd_create_vrtek_drv(ohmd_context* ctx);
157 ohmd_driver* ohmd_create_external_drv(ohmd_context* ctx);
158 ohmd_driver* ohmd_create_android_drv(ohmd_context* ctx);
159 
160 #include "log.h"
161 #include "omath.h"
162 #include "fusion.h"
163 
164 #endif
165