lovr/src/headset/headset.c

215 lines
4.9 KiB
C
Raw Normal View History

2016-11-19 09:28:01 +00:00
#include "headset/headset.h"
2016-12-01 07:03:58 +00:00
#include "event/event.h"
2016-08-11 04:17:14 +00:00
2017-10-22 23:44:26 +00:00
static HeadsetInterface* headset = NULL;
2018-02-23 09:28:42 +00:00
static bool initialized = false;
2018-03-05 04:43:42 +00:00
void lovrHeadsetInit(HeadsetDriver* drivers, int count, float offset) {
2018-02-23 09:28:42 +00:00
if (initialized) return;
initialized = true;
2018-02-24 04:01:13 +00:00
headset = NULL;
2018-02-23 09:28:42 +00:00
for (int i = 0; i < count; i++) {
HeadsetInterface* interface = NULL;
switch (drivers[i]) {
case DRIVER_FAKE: interface = &lovrHeadsetFakeDriver; break;
#ifndef EMSCRIPTEN
case DRIVER_OPENVR: interface = &lovrHeadsetOpenVRDriver; break;
#else
case DRIVER_WEBVR: interface = &lovrHeadsetWebVRDriver; break;
#endif
default: break;
}
2018-03-05 04:43:42 +00:00
if (interface && interface->init(offset)) {
headset = interface;
break;
}
}
}
void lovrHeadsetDestroy() {
2018-02-23 09:28:42 +00:00
if (!initialized) return;
initialized = false;
if (headset) {
headset->destroy();
2017-11-25 20:14:00 +00:00
headset = NULL;
}
}
const HeadsetDriver* lovrHeadsetGetDriver() {
if (!headset) {
lovrThrow("Headset is not initialized");
}
return &headset->driverType;
}
HeadsetType lovrHeadsetGetType() {
return headset ? headset->getType() : HEADSET_UNKNOWN;
}
HeadsetOrigin lovrHeadsetGetOriginType() {
return headset ? headset->getOriginType() : ORIGIN_HEAD;
}
bool lovrHeadsetIsMounted() {
return headset ? headset->isMounted() : false;
}
2017-10-31 08:14:09 +00:00
bool lovrHeadsetIsMirrored() {
return headset ? headset->isMirrored() : false;
}
2017-10-31 08:14:09 +00:00
void lovrHeadsetSetMirrored(bool mirror) {
if (headset) {
headset->setMirrored(mirror);
}
}
void lovrHeadsetGetDisplayDimensions(int* width, int* height) {
if (!headset) {
*width = *height = 0;
return;
}
headset->getDisplayDimensions(width, height);
}
2018-02-23 05:01:34 +00:00
void lovrHeadsetGetClipDistance(float* clipNear, float* clipFar) {
if (!headset) {
2018-02-23 05:01:34 +00:00
*clipNear = *clipFar = 0.f;
return;
}
2018-02-23 05:01:34 +00:00
headset->getClipDistance(clipNear, clipFar);
}
2018-02-23 05:01:34 +00:00
void lovrHeadsetSetClipDistance(float clipNear, float clipFar) {
if (headset) {
2018-02-23 05:01:34 +00:00
headset->setClipDistance(clipNear, clipFar);
}
}
float lovrHeadsetGetBoundsWidth() {
2017-11-25 20:14:00 +00:00
return headset ? headset->getBoundsWidth() : 0.f;
}
float lovrHeadsetGetBoundsDepth() {
2017-11-25 20:14:00 +00:00
return headset ? headset->getBoundsDepth() : 0.f;
}
void lovrHeadsetGetPose(float* x, float* y, float* z, float* angle, float* ax, float* ay, float* az) {
if (!headset) {
*x = *y = *z = *angle = *ax = *ay = *az = 0.f;
return;
}
headset->getPose(x, y, z, angle, ax, ay, az);
}
void lovrHeadsetGetEyePose(HeadsetEye eye, float* x, float* y, float* z, float* angle, float* ax, float* ay, float* az) {
if (!headset) {
*x = *y = *z = *angle = *ax = *ay = *az = 0.f;
return;
}
headset->getEyePose(eye, x, y, z, angle, ax, ay, az);
}
void lovrHeadsetGetVelocity(float* x, float* y, float* z) {
if (!headset) {
*x = *y = *z = 0.f;
return;
}
headset->getVelocity(x,y,z);
}
void lovrHeadsetGetAngularVelocity(float* x, float* y, float* z) {
if (!headset) {
*x = *y = *z = 0.f;
return;
}
headset->getAngularVelocity(x,y,z);
}
vec_controller_t* lovrHeadsetGetControllers() {
if (!headset) {
return NULL;
}
return headset->getControllers();
}
bool lovrHeadsetControllerIsConnected(Controller* controller) {
if (!headset || !controller) {
2017-10-31 08:14:09 +00:00
return false;
}
return headset->controllerIsConnected(controller);
}
ControllerHand lovrHeadsetControllerGetHand(Controller* controller) {
return headset ? headset->controllerGetHand(controller) : HAND_UNKNOWN;
}
void lovrHeadsetControllerGetPose(Controller* controller, float* x, float* y, float* z, float* angle, float* ax, float* ay, float* az) {
if (!headset || !controller) {
*x = *y = *z = *angle = *ax = *ay = *az = 0.f;
return;
}
headset->controllerGetPose(controller, x, y, z, angle, ax, ay, az);
}
float lovrHeadsetControllerGetAxis(Controller* controller, ControllerAxis axis) {
if (!headset || !controller) {
return 0.f;
}
return headset->controllerGetAxis(controller, axis);
}
2017-10-31 08:14:09 +00:00
bool lovrHeadsetControllerIsDown(Controller* controller, ControllerButton button) {
if (!headset || !controller) {
2017-10-31 08:14:09 +00:00
return false;
}
return headset->controllerIsDown(controller, button);
}
2017-10-31 08:14:09 +00:00
bool lovrHeadsetControllerIsTouched(Controller* controller, ControllerButton button) {
return (headset && controller) ? headset->controllerIsTouched(controller,button) : false;
}
void lovrHeadsetControllerVibrate(Controller* controller, float duration, float power) {
if (!headset || !controller) {
return;
}
headset->controllerVibrate(controller, duration, power);
}
2017-11-25 20:14:00 +00:00
ModelData* lovrHeadsetControllerNewModelData(Controller* controller) {
if (headset && controller) {
return headset->controllerNewModelData(controller);
} else {
return NULL;
}
}
2018-03-11 07:42:33 +00:00
void lovrHeadsetRenderTo(void (*callback)(void*), void* userdata) {
if (headset) {
headset->renderTo(callback, userdata);
}
}
void lovrHeadsetUpdate(float dt) {
2018-02-24 04:01:13 +00:00
if (headset && headset->update) {
headset->update(dt);
}
}