lovr/src/api/l_headset.c

651 lines
17 KiB
C
Raw Normal View History

2017-12-10 20:40:37 +00:00
#include "api.h"
2016-11-19 09:28:01 +00:00
#include "headset/headset.h"
2019-04-03 04:28:41 +00:00
#include "data/modelData.h"
#include "graphics/model.h"
2019-01-25 20:08:45 +00:00
#include "graphics/texture.h"
2019-05-20 09:47:33 +00:00
#include "core/maf.h"
2016-08-11 04:17:14 +00:00
#if defined(EMSCRIPTEN) || defined(LOVR_USE_OCULUS_MOBILE)
#define LOVR_HEADSET_HELPER_USES_REGISTRY
#endif
2018-07-05 03:11:52 +00:00
const char* HeadsetDrivers[] = {
2019-03-07 07:02:03 +00:00
[DRIVER_DESKTOP] = "desktop",
2019-04-09 19:38:55 +00:00
[DRIVER_LEAP_MOTION] = "leap",
[DRIVER_OCULUS] = "oculus",
[DRIVER_OCULUS_MOBILE] = "oculusmobile",
2018-07-05 03:11:52 +00:00
[DRIVER_OPENVR] = "openvr",
2019-04-30 23:21:30 +00:00
[DRIVER_OPENXR] = "openxr",
2018-07-05 03:11:52 +00:00
[DRIVER_WEBVR] = "webvr",
NULL
};
const char* HeadsetOrigins[] = {
[ORIGIN_HEAD] = "head",
[ORIGIN_FLOOR] = "floor",
NULL
};
const char* Devices[] = {
[DEVICE_HEAD] = "head",
[DEVICE_HAND] = "hand",
[DEVICE_HAND_LEFT] = "hand/left",
[DEVICE_HAND_RIGHT] = "hand/right",
[DEVICE_EYE_LEFT] = "eye/left",
[DEVICE_EYE_RIGHT] = "eye/left",
[DEVICE_TRACKER_1] = "tracker/1",
[DEVICE_TRACKER_2] = "tracker/2",
[DEVICE_TRACKER_3] = "tracker/3",
[DEVICE_TRACKER_4] = "tracker/4",
NULL
};
const char* DeviceButtons[] = {
[BUTTON_TRIGGER] = "trigger",
[BUTTON_THUMBSTICK] = "thumbstick",
2019-05-08 23:57:51 +00:00
[BUTTON_TOUCHPAD] = "touchpad",
[BUTTON_GRIP] = "grip",
[BUTTON_MENU] = "menu",
[BUTTON_A] = "a",
[BUTTON_B] = "b",
[BUTTON_X] = "x",
[BUTTON_Y] = "y",
[BUTTON_PROXIMITY] = "proximity",
NULL
};
const char* DeviceAxes[] = {
[AXIS_TRIGGER] = "trigger",
2019-05-13 05:04:41 +00:00
[AXIS_THUMBSTICK] = "thumbstick",
[AXIS_TOUCHPAD] = "touchpad",
[AXIS_PINCH] = "pinch",
[AXIS_GRIP] = "grip",
NULL
};
const char* DeviceBones[] = {
[BONE_THUMB] = "thumb",
[BONE_INDEX] = "index",
[BONE_MIDDLE] = "middle",
[BONE_RING] = "ring",
[BONE_PINKY] = "pinky",
[BONE_THUMB_NULL] = "thumb/null",
[BONE_THUMB_1] = "thumb/1",
[BONE_THUMB_2] = "thumb/2",
[BONE_THUMB_3] = "thumb/3",
[BONE_INDEX_1] = "index/1",
[BONE_INDEX_2] = "index/2",
[BONE_INDEX_3] = "index/3",
[BONE_INDEX_4] = "index/4",
[BONE_MIDDLE_1] = "middle/1",
[BONE_MIDDLE_2] = "middle/2",
[BONE_MIDDLE_3] = "middle/3",
[BONE_MIDDLE_4] = "middle/4",
[BONE_RING_1] = "ring/1",
[BONE_RING_2] = "ring/2",
[BONE_RING_3] = "ring/3",
[BONE_RING_4] = "ring/4",
[BONE_PINKY_1] = "pinky/1",
[BONE_PINKY_2] = "pinky/2",
[BONE_PINKY_3] = "pinky/3",
[BONE_PINKY_4] = "pinky/4",
NULL
};
2017-04-16 23:56:49 +00:00
typedef struct {
lua_State* L;
int ref;
} HeadsetRenderData;
static HeadsetRenderData headsetRenderData;
2018-03-11 07:42:33 +00:00
static void renderHelper(void* userdata) {
2017-04-16 23:56:49 +00:00
HeadsetRenderData* renderData = userdata;
lua_State* L = renderData->L;
#ifdef LOVR_HEADSET_HELPER_USES_REGISTRY
luax_geterror(L);
if (lua_isnil(L, -1)) {
lua_pushcfunction(L, luax_getstack);
lua_rawgeti(L, LUA_REGISTRYINDEX, renderData->ref);
if (lua_pcall(L, 0, 0, -2)) {
luax_seterror(L);
}
lua_pop(L, 1); // pop luax_getstack
}
lua_pop(L, 1);
#else
2018-03-11 07:42:33 +00:00
lua_call(L, 0, 0);
#endif
}
static Device luax_optdevice(lua_State* L, int index) {
const char* str = luaL_optstring(L, 1, "head");
if (!strcmp(str, "left")) {
return DEVICE_HAND_LEFT;
} else if (!strcmp(str, "right")) {
return DEVICE_HAND_RIGHT;
}
return luaL_checkoption(L, 1, "head", Devices);
}
2018-09-27 01:27:38 +00:00
static int l_lovrHeadsetGetDriver(lua_State* L) {
if (lua_gettop(L) == 0) {
lua_pushstring(L, HeadsetDrivers[lovrHeadsetDriver->driverType]);
return 1;
} else {
Device device = luax_optdevice(L, 1);
FOREACH_TRACKING_DRIVER(driver) {
if (driver->getPose(device, NULL, NULL)) {
lua_pushstring(L, HeadsetDrivers[driver->driverType]);
return 1;
}
}
}
return 0;
}
2019-04-06 22:09:20 +00:00
static int l_lovrHeadsetGetName(lua_State* L) {
2019-04-19 04:03:38 +00:00
char name[256];
if (lovrHeadsetDriver->getName(name, sizeof(name))) {
lua_pushstring(L, name);
} else {
lua_pushnil(L);
}
2016-10-01 21:12:55 +00:00
return 1;
2016-08-11 04:17:14 +00:00
}
2018-09-27 01:27:38 +00:00
static int l_lovrHeadsetGetOriginType(lua_State* L) {
2018-07-05 03:11:52 +00:00
lua_pushstring(L, HeadsetOrigins[lovrHeadsetDriver->getOriginType()]);
2017-08-04 05:21:49 +00:00
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrHeadsetGetDisplayWidth(lua_State* L) {
uint32_t width, height;
lovrHeadsetDriver->getDisplayDimensions(&width, &height);
lua_pushinteger(L, width);
2016-10-24 23:03:29 +00:00
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrHeadsetGetDisplayHeight(lua_State* L) {
uint32_t width, height;
lovrHeadsetDriver->getDisplayDimensions(&width, &height);
lua_pushinteger(L, height);
2016-10-24 23:03:29 +00:00
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrHeadsetGetDisplayDimensions(lua_State* L) {
uint32_t width, height;
2018-04-28 23:59:53 +00:00
lovrHeadsetDriver->getDisplayDimensions(&width, &height);
lua_pushinteger(L, width);
lua_pushinteger(L, height);
2016-10-24 23:03:29 +00:00
return 2;
}
2018-09-27 01:27:38 +00:00
static int l_lovrHeadsetGetClipDistance(lua_State* L) {
2018-02-23 05:01:34 +00:00
float clipNear, clipFar;
2018-04-28 23:59:53 +00:00
lovrHeadsetDriver->getClipDistance(&clipNear, &clipFar);
2018-02-23 05:01:34 +00:00
lua_pushnumber(L, clipNear);
lua_pushnumber(L, clipFar);
2016-09-27 06:48:09 +00:00
return 2;
}
2018-09-27 01:27:38 +00:00
static int l_lovrHeadsetSetClipDistance(lua_State* L) {
float clipNear = luax_checkfloat(L, 1);
float clipFar = luax_checkfloat(L, 2);
2018-04-28 23:59:53 +00:00
lovrHeadsetDriver->setClipDistance(clipNear, clipFar);
2016-10-01 21:12:55 +00:00
return 0;
}
2018-09-27 01:27:38 +00:00
static int l_lovrHeadsetGetBoundsWidth(lua_State* L) {
2018-04-29 21:38:08 +00:00
float width, depth;
lovrHeadsetDriver->getBoundsDimensions(&width, &depth);
lua_pushnumber(L, width);
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrHeadsetGetBoundsDepth(lua_State* L) {
2018-04-29 21:38:08 +00:00
float width, depth;
lovrHeadsetDriver->getBoundsDimensions(&width, &depth);
lua_pushnumber(L, depth);
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrHeadsetGetBoundsDimensions(lua_State* L) {
2018-04-29 21:38:08 +00:00
float width, depth;
lovrHeadsetDriver->getBoundsDimensions(&width, &depth);
lua_pushnumber(L, width);
lua_pushnumber(L, depth);
2016-10-01 21:17:26 +00:00
return 2;
}
static int l_lovrHeadsetGetBoundsGeometry(lua_State* L) {
int count;
const float* points = lovrHeadsetDriver->getBoundsGeometry(&count);
if (!points) {
lua_pushnil(L);
return 1;
}
if (lua_type(L, 1) == LUA_TTABLE) {
lua_settop(L, 1);
} else {
lua_settop(L, 0);
lua_createtable(L, count, 0);
}
for (int i = 0; i < count; i++) {
lua_pushnumber(L, points[i]);
lua_rawseti(L, 1, i + 1);
}
return 1;
}
int l_lovrHeadsetGetPose(lua_State* L) {
Device device = luax_optdevice(L, 1);
float position[3], orientation[4];
FOREACH_TRACKING_DRIVER(driver) {
if (driver->getPose(device, position, orientation)) {
float angle, ax, ay, az;
quat_getAngleAxis(orientation, &angle, &ax, &ay, &az);
lua_pushnumber(L, position[0]);
lua_pushnumber(L, position[1]);
lua_pushnumber(L, position[2]);
lua_pushnumber(L, angle);
lua_pushnumber(L, ax);
lua_pushnumber(L, ay);
lua_pushnumber(L, az);
return 7;
}
}
return 0;
2016-10-01 21:12:55 +00:00
}
int l_lovrHeadsetGetPosition(lua_State* L) {
Device device = luax_optdevice(L, 1);
float position[3], orientation[4];
FOREACH_TRACKING_DRIVER(driver) {
if (driver->getPose(device, position, orientation)) {
lua_pushnumber(L, position[0]);
lua_pushnumber(L, position[1]);
lua_pushnumber(L, position[2]);
return 3;
}
}
return 0;
}
int l_lovrHeadsetGetOrientation(lua_State* L) {
Device device = luax_optdevice(L, 1);
float position[3], orientation[4];
FOREACH_TRACKING_DRIVER(driver) {
if (driver->getPose(device, position, orientation)) {
float angle, ax, ay, az;
quat_getAngleAxis(orientation, &angle, &ax, &ay, &az);
lua_pushnumber(L, angle);
lua_pushnumber(L, ax);
lua_pushnumber(L, ay);
lua_pushnumber(L, az);
return 4;
}
}
return 0;
2016-08-11 04:17:14 +00:00
}
2019-04-19 02:23:59 +00:00
int l_lovrHeadsetGetDirection(lua_State* L) {
Device device = luax_optdevice(L, 1);
float position[3], orientation[4];
2019-04-19 02:23:59 +00:00
FOREACH_TRACKING_DRIVER(driver) {
if (driver->getPose(device, position, orientation)) {
2019-04-19 02:23:59 +00:00
float v[3] = { 0.f, 0.f, -1.f };
quat_rotate(orientation, v);
2019-04-19 02:23:59 +00:00
lua_pushnumber(L, v[0]);
lua_pushnumber(L, v[1]);
lua_pushnumber(L, v[2]);
return 3;
}
}
return 0;
}
int l_lovrHeadsetGetBonePose(lua_State* L) {
Device device = luax_optdevice(L, 1);
DeviceBone bone = luaL_checkoption(L, 2, NULL, DeviceBones);
float position[3], orientation[4];
FOREACH_TRACKING_DRIVER(driver) {
if (driver->getBonePose(device, bone, position, orientation)) {
float angle, ax, ay, az;
quat_getAngleAxis(orientation, &angle, &ax, &ay, &az);
lua_pushnumber(L, position[0]);
lua_pushnumber(L, position[1]);
lua_pushnumber(L, position[2]);
lua_pushnumber(L, angle);
lua_pushnumber(L, ax);
lua_pushnumber(L, ay);
lua_pushnumber(L, az);
return 7;
}
}
return 0;
}
int l_lovrHeadsetGetVelocity(lua_State* L) {
Device device = luax_optdevice(L, 1);
float velocity[3], angularVelocity[3];
FOREACH_TRACKING_DRIVER(driver) {
if (driver->getVelocity(device, velocity, angularVelocity)) {
lua_pushnumber(L, velocity[0]);
lua_pushnumber(L, velocity[1]);
lua_pushnumber(L, velocity[2]);
return 3;
}
}
return 0;
2016-08-11 04:17:14 +00:00
}
int l_lovrHeadsetGetAngularVelocity(lua_State* L) {
Device device = luax_optdevice(L, 1);
float velocity[3], angularVelocity[3];
FOREACH_TRACKING_DRIVER(driver) {
if (driver->getVelocity(device, velocity, angularVelocity)) {
lua_pushnumber(L, angularVelocity[0]);
lua_pushnumber(L, angularVelocity[1]);
lua_pushnumber(L, angularVelocity[2]);
return 3;
}
}
return 0;
2016-09-16 06:57:01 +00:00
}
int l_lovrHeadsetGetAcceleration(lua_State* L) {
Device device = luax_optdevice(L, 1);
float acceleration[3], angularAcceleration[3];
FOREACH_TRACKING_DRIVER(driver) {
if (driver->getAcceleration(device, acceleration, angularAcceleration)) {
lua_pushnumber(L, acceleration[0]);
lua_pushnumber(L, acceleration[1]);
lua_pushnumber(L, acceleration[2]);
return 3;
}
}
return 0;
}
int l_lovrHeadsetGetAngularAcceleration(lua_State* L) {
Device device = luax_optdevice(L, 1);
float acceleration[3], angularAcceleration[3];
FOREACH_TRACKING_DRIVER(driver) {
if (driver->getAcceleration(device, acceleration, angularAcceleration)) {
lua_pushnumber(L, angularAcceleration[0]);
lua_pushnumber(L, angularAcceleration[1]);
lua_pushnumber(L, angularAcceleration[2]);
return 3;
}
}
return 0;
}
int l_lovrHeadsetIsDown(lua_State* L) {
Device device = luax_optdevice(L, 1);
DeviceButton button = luaL_checkoption(L, 2, NULL, DeviceButtons);
bool down;
FOREACH_TRACKING_DRIVER(driver) {
if (driver->isDown(device, button, &down)) {
lua_pushboolean(L, down);
return 1;
}
}
return 0;
}
int l_lovrHeadsetIsTouched(lua_State* L) {
Device device = luax_optdevice(L, 1);
DeviceButton button = luaL_checkoption(L, 2, NULL, DeviceButtons);
bool touched;
FOREACH_TRACKING_DRIVER(driver) {
if (driver->isDown(device, button, &touched)) {
lua_pushboolean(L, touched);
return 1;
}
}
return 0;
}
int l_lovrHeadsetGetAxis(lua_State* L) {
Device device = luax_optdevice(L, 1);
DeviceAxis axis = luaL_checkoption(L, 2, NULL, DeviceAxes);
2019-05-13 05:04:41 +00:00
float value[3];
FOREACH_TRACKING_DRIVER(driver) {
2019-05-13 05:04:41 +00:00
if (driver->getAxis(device, axis, value)) {
switch (axis) {
case MAX_AXES:
case AXIS_TRIGGER:
case AXIS_PINCH:
case AXIS_GRIP:
lua_pushnumber(L, value[0]);
return 1;
case AXIS_THUMBSTICK:
case AXIS_TOUCHPAD:
lua_pushnumber(L, value[0]);
lua_pushnumber(L, value[1]);
return 2;
}
}
}
return 0;
}
int l_lovrHeadsetVibrate(lua_State* L) {
Device device = luax_optdevice(L, 1);
float strength = luax_optfloat(L, 2, 1.f);
float duration = luax_optfloat(L, 3, .5f);
float frequency = luax_optfloat(L, 4, 0.f);
FOREACH_TRACKING_DRIVER(driver) {
if (driver->vibrate(device, strength, duration, frequency)) {
lua_pushboolean(L, true);
return 1;
}
}
lua_pushboolean(L, false);
return 1;
}
2019-04-03 04:28:41 +00:00
int l_lovrHeadsetNewModel(lua_State* L) {
Device device = luax_optdevice(L, 1);
2019-04-03 04:28:41 +00:00
ModelData* modelData = NULL;
FOREACH_TRACKING_DRIVER(driver) {
if ((modelData = driver->newModelData(device)) != NULL) {
2019-04-03 04:28:41 +00:00
break;
}
}
if (modelData) {
Model* model = lovrModelCreate(modelData);
luax_pushobject(L, model);
lovrRelease(ModelData, modelData);
lovrRelease(Model, model);
return 1;
}
return 0;
}
2018-09-27 01:27:38 +00:00
static int l_lovrHeadsetRenderTo(lua_State* L) {
2017-04-16 23:56:49 +00:00
lua_settop(L, 1);
2016-08-11 04:17:14 +00:00
luaL_checktype(L, 1, LUA_TFUNCTION);
2017-04-21 04:17:25 +00:00
#ifdef LOVR_HEADSET_HELPER_USES_REGISTRY
2017-04-23 06:21:29 +00:00
if (headsetRenderData.ref != LUA_NOREF) {
2017-04-21 04:17:25 +00:00
luaL_unref(L, LUA_REGISTRYINDEX, headsetRenderData.ref);
}
2017-04-16 23:56:49 +00:00
headsetRenderData.ref = luaL_ref(L, LUA_REGISTRYINDEX);
lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD);
headsetRenderData.L = lua_tothread(L, -1);
lua_pop(L, 1);
#else
2017-04-16 23:56:49 +00:00
headsetRenderData.L = L;
#endif
2018-04-28 23:59:53 +00:00
lovrHeadsetDriver->renderTo(renderHelper, &headsetRenderData);
2016-08-11 04:17:14 +00:00
return 0;
}
2017-03-11 11:08:07 +00:00
2018-09-27 01:27:38 +00:00
static int l_lovrHeadsetUpdate(lua_State* L) {
2019-04-09 19:38:55 +00:00
float dt = luax_checkfloat(L, 1);
2018-05-01 22:10:51 +00:00
if (lovrHeadsetDriver->update) {
2019-04-09 19:38:55 +00:00
lovrHeadsetDriver->update(dt);
}
FOREACH_TRACKING_DRIVER(driver) {
2019-04-30 04:11:03 +00:00
if (driver->update && driver != lovrHeadsetDriver) {
driver->update(dt);
}
2018-05-01 22:10:51 +00:00
}
return 0;
}
2018-01-09 02:33:51 +00:00
2019-01-25 20:08:45 +00:00
static int l_lovrHeadsetGetMirrorTexture(lua_State* L) {
Texture *texture = NULL;
if (lovrHeadsetDriver->getMirrorTexture)
texture = lovrHeadsetDriver->getMirrorTexture();
2019-01-28 21:53:05 +00:00
luax_pushobject(L, texture);
2019-01-25 20:08:45 +00:00
return 1;
}
2019-05-08 11:06:50 +00:00
static int deviceIterator(lua_State* L) {
size_t index = lua_tointeger(L, lua_upvalueindex(1));
Device* devices = (Device*) lua_touserdata(L, lua_upvalueindex(2));
size_t count = lua_tointeger(L, lua_upvalueindex(3));
2019-05-12 21:35:15 +00:00
float position[3], orientation[4];
2019-05-08 11:06:50 +00:00
while (index < count) {
FOREACH_TRACKING_DRIVER(driver) {
2019-05-12 21:35:15 +00:00
if (driver->getPose(devices[index], position, orientation)) {
2019-05-08 11:06:50 +00:00
lua_pushstring(L, Devices[devices[index]]);
lua_pushinteger(L, ++index);
lua_replace(L, lua_upvalueindex(1));
return 1;
}
}
index++;
}
return 0;
}
static Device hands[] = {
DEVICE_HAND,
DEVICE_HAND_LEFT,
DEVICE_HAND_RIGHT
};
static Device trackers[] = {
DEVICE_TRACKER_1,
DEVICE_TRACKER_2,
DEVICE_TRACKER_3,
DEVICE_TRACKER_4
};
static int l_lovrHeadsetHands(lua_State* L) {
lua_pushinteger(L, 0);
lua_pushlightuserdata(L, hands);
lua_pushinteger(L, sizeof(hands) / sizeof(hands[0]));
lua_pushcclosure(L, deviceIterator, 3);
return 1;
}
static int l_lovrHeadsetTrackers(lua_State* L) {
lua_pushinteger(L, 0);
lua_pushlightuserdata(L, trackers);
lua_pushinteger(L, sizeof(trackers) / sizeof(trackers[0]));
lua_pushcclosure(L, deviceIterator, 3);
return 1;
}
2018-09-27 01:27:38 +00:00
static const luaL_Reg lovrHeadset[] = {
{ "getDriver", l_lovrHeadsetGetDriver },
2019-04-06 22:09:20 +00:00
{ "getName", l_lovrHeadsetGetName },
2017-08-07 06:56:22 +00:00
{ "getOriginType", l_lovrHeadsetGetOriginType },
2017-03-11 11:08:07 +00:00
{ "getDisplayWidth", l_lovrHeadsetGetDisplayWidth },
{ "getDisplayHeight", l_lovrHeadsetGetDisplayHeight },
{ "getDisplayDimensions", l_lovrHeadsetGetDisplayDimensions },
{ "getClipDistance", l_lovrHeadsetGetClipDistance },
{ "setClipDistance", l_lovrHeadsetSetClipDistance },
{ "getBoundsWidth", l_lovrHeadsetGetBoundsWidth },
{ "getBoundsDepth", l_lovrHeadsetGetBoundsDepth },
{ "getBoundsDimensions", l_lovrHeadsetGetBoundsDimensions },
{ "getBoundsGeometry", l_lovrHeadsetGetBoundsGeometry },
{ "getPose", l_lovrHeadsetGetPose },
2017-03-11 11:08:07 +00:00
{ "getPosition", l_lovrHeadsetGetPosition },
{ "getOrientation", l_lovrHeadsetGetOrientation },
2019-04-19 04:56:25 +00:00
{ "getDirection", l_lovrHeadsetGetDirection },
{ "getBonePose", l_lovrHeadsetGetBonePose },
2017-03-11 11:08:07 +00:00
{ "getVelocity", l_lovrHeadsetGetVelocity },
{ "getAngularVelocity", l_lovrHeadsetGetAngularVelocity },
{ "getAcceleration", l_lovrHeadsetGetAcceleration },
{ "getAngularAcceleration", l_lovrHeadsetGetAngularAcceleration },
{ "isDown", l_lovrHeadsetIsDown },
{ "isTouched", l_lovrHeadsetIsTouched },
{ "getAxis", l_lovrHeadsetGetAxis },
{ "vibrate", l_lovrHeadsetVibrate },
2019-04-03 04:28:41 +00:00
{ "newModel", l_lovrHeadsetNewModel },
2017-03-11 11:08:07 +00:00
{ "renderTo", l_lovrHeadsetRenderTo },
{ "update", l_lovrHeadsetUpdate },
2019-01-25 20:08:45 +00:00
{ "getMirrorTexture", l_lovrHeadsetGetMirrorTexture },
2019-05-08 11:06:50 +00:00
{ "hands", l_lovrHeadsetHands },
{ "trackers", l_lovrHeadsetTrackers },
2017-03-11 11:08:07 +00:00
{ NULL, NULL }
};
2018-09-27 01:27:38 +00:00
int luaopen_lovr_headset(lua_State* L) {
lua_newtable(L);
luaL_register(L, NULL, lovrHeadset);
luax_pushconf(L);
lua_getfield(L, -1, "headset");
vec_t(HeadsetDriver) drivers;
vec_init(&drivers);
2019-02-17 07:43:20 +00:00
float offset = 1.7f;
2018-09-27 01:27:38 +00:00
int msaa = 4;
if (lua_istable(L, -1)) {
// Drivers
lua_getfield(L, -1, "drivers");
2019-03-17 07:58:01 +00:00
int n = luax_len(L, -1);
2018-09-27 01:27:38 +00:00
for (int i = 0; i < n; i++) {
lua_rawgeti(L, -1, i + 1);
vec_push(&drivers, luaL_checkoption(L, -1, NULL, HeadsetDrivers));
lua_pop(L, 1);
}
lua_pop(L, 1);
// Offset
lua_getfield(L, -1, "offset");
offset = luax_optfloat(L, -1, 1.7f);
2018-09-27 01:27:38 +00:00
lua_pop(L, 1);
// MSAA
lua_getfield(L, -1, "msaa");
msaa = luaL_optinteger(L, -1, 4);
2018-09-27 01:27:38 +00:00
lua_pop(L, 1);
}
if (lovrHeadsetInit(drivers.data, drivers.length, offset, msaa)) {
luax_atexit(L, lovrHeadsetDestroy);
}
2018-09-27 01:27:38 +00:00
vec_deinit(&drivers);
lua_pop(L, 2);
headsetRenderData.ref = LUA_NOREF;
return 1;
}