lovr/src/api/headset.c

246 lines
6.4 KiB
C
Raw Normal View History

2017-03-11 11:08:07 +00:00
#include "api/lovr.h"
2016-11-19 09:28:01 +00:00
#include "headset/headset.h"
2016-08-11 04:17:14 +00:00
2017-03-11 11:08:07 +00:00
map_int_t ControllerAxes;
map_int_t ControllerButtons;
map_int_t HeadsetEyes;
static void renderHelper(HeadsetEye eye, void* userdata) {
2016-08-11 04:17:14 +00:00
lua_State* L = (lua_State*)userdata;
2016-08-17 03:35:15 +00:00
luaL_checktype(L, 1, LUA_TFUNCTION);
lua_pushvalue(L, 1);
luax_pushenum(L, &HeadsetEyes, eye);
2016-08-11 04:17:14 +00:00
lua_call(L, 1, 0);
}
int l_lovrHeadsetInit(lua_State* L) {
lua_newtable(L);
luaL_register(L, NULL, lovrHeadset);
2017-01-21 03:46:45 +00:00
luax_registertype(L, "Controller", lovrController);
2016-10-03 14:40:20 +00:00
map_init(&HeadsetEyes);
map_set(&HeadsetEyes, "left", EYE_LEFT);
map_set(&HeadsetEyes, "right", EYE_RIGHT);
2016-11-15 00:20:56 +00:00
map_init(&ControllerAxes);
map_set(&ControllerAxes, "trigger", CONTROLLER_AXIS_TRIGGER);
map_set(&ControllerAxes, "touchx", CONTROLLER_AXIS_TOUCHPAD_X);
map_set(&ControllerAxes, "touchy", CONTROLLER_AXIS_TOUCHPAD_Y);
2016-11-15 03:57:23 +00:00
map_init(&ControllerButtons);
map_set(&ControllerButtons, "system", CONTROLLER_BUTTON_SYSTEM);
map_set(&ControllerButtons, "menu", CONTROLLER_BUTTON_MENU);
map_set(&ControllerButtons, "grip", CONTROLLER_BUTTON_GRIP);
map_set(&ControllerButtons, "touchpad", CONTROLLER_BUTTON_TOUCHPAD);
2016-08-11 04:17:14 +00:00
lovrHeadsetInit();
2016-10-03 14:40:20 +00:00
2016-08-11 04:17:14 +00:00
return 1;
}
2016-10-01 21:12:55 +00:00
int l_lovrHeadsetIsPresent(lua_State* L) {
lua_pushboolean(L, lovrHeadsetIsPresent());
return 1;
}
int l_lovrHeadsetGetType(lua_State* L) {
lua_pushstring(L, lovrHeadsetGetType());
return 1;
2016-08-11 04:17:14 +00:00
}
int l_lovrHeadsetIsMirrored(lua_State* L) {
lua_pushboolean(L, lovrHeadsetIsMirrored());
return 1;
}
int l_lovrHeadsetSetMirrored(lua_State* L) {
int mirror = lua_toboolean(L, 1);
lovrHeadsetSetMirrored(mirror);
return 0;
}
2016-10-24 23:03:29 +00:00
int l_lovrHeadsetGetDisplayWidth(lua_State* L) {
int width;
lovrHeadsetGetDisplayDimensions(&width, NULL);
lua_pushnumber(L, width);
return 1;
}
int l_lovrHeadsetGetDisplayHeight(lua_State* L) {
int height;
lovrHeadsetGetDisplayDimensions(NULL, &height);
lua_pushnumber(L, height);
return 1;
}
int l_lovrHeadsetGetDisplayDimensions(lua_State* L) {
int width, height;
lovrHeadsetGetDisplayDimensions(&width, &height);
lua_pushnumber(L, width);
lua_pushnumber(L, height);
return 2;
}
2016-09-27 06:48:09 +00:00
int l_lovrHeadsetGetClipDistance(lua_State* L) {
float near, far;
lovrHeadsetGetClipDistance(&near, &far);
lua_pushnumber(L, near);
lua_pushnumber(L, far);
return 2;
}
2016-10-01 21:12:55 +00:00
int l_lovrHeadsetSetClipDistance(lua_State* L) {
float near = luaL_checknumber(L, 1);
float far = luaL_checknumber(L, 2);
lovrHeadsetSetClipDistance(near, far);
return 0;
}
int l_lovrHeadsetGetBoundsWidth(lua_State* L) {
lua_pushnumber(L, lovrHeadsetGetBoundsWidth());
return 1;
}
int l_lovrHeadsetGetBoundsDepth(lua_State* L) {
lua_pushnumber(L, lovrHeadsetGetBoundsDepth());
return 1;
}
int l_lovrHeadsetGetBoundsDimensions(lua_State* L) {
lua_pushnumber(L, lovrHeadsetGetBoundsWidth());
lua_pushnumber(L, lovrHeadsetGetBoundsDepth());
2016-10-01 21:17:26 +00:00
return 2;
}
int l_lovrHeadsetGetBoundsGeometry(lua_State* L) {
float geometry[12];
lovrHeadsetGetBoundsGeometry(geometry);
lua_newtable(L);
for (int i = 0; i < 4; i++) {
lua_newtable(L);
for (int j = 0; j < 3; j++) {
lua_pushnumber(L, geometry[3 * i + j]);
lua_rawseti(L, -2, j + 1);
}
2016-11-24 05:01:46 +00:00
lua_rawseti(L, -2, i + 1);
}
return 1;
}
2016-10-01 22:11:45 +00:00
int l_lovrHeadsetIsBoundsVisible(lua_State* L) {
lua_pushboolean(L, lovrHeadsetIsBoundsVisible());
return 1;
}
int l_lovrHeadsetSetBoundsVisible(lua_State* L) {
char visible = lua_toboolean(L, 1);
lovrHeadsetSetBoundsVisible(visible);
return 0;
}
2016-10-01 21:12:55 +00:00
int l_lovrHeadsetGetPosition(lua_State* L) {
float x, y, z;
lovrHeadsetGetPosition(&x, &y, &z);
lua_pushnumber(L, x);
lua_pushnumber(L, y);
lua_pushnumber(L, z);
return 3;
}
int l_lovrHeadsetGetEyePosition(lua_State* L) {
float x, y, z;
2017-03-02 04:19:37 +00:00
HeadsetEye eye = *(HeadsetEye*) luax_optenum(L, 1, "left", &HeadsetEyes, "eye");
lovrHeadsetGetEyePosition(eye, &x, &y, &z);
lua_pushnumber(L, x);
lua_pushnumber(L, y);
lua_pushnumber(L, z);
return 3;
}
2016-09-17 02:43:43 +00:00
int l_lovrHeadsetGetOrientation(lua_State* L) {
2017-01-20 02:04:45 +00:00
float angle, x, y, z;
lovrHeadsetGetOrientation(&angle, &x, &y, &z);
lua_pushnumber(L, angle);
2016-09-17 02:43:43 +00:00
lua_pushnumber(L, x);
lua_pushnumber(L, y);
lua_pushnumber(L, z);
return 4;
2016-08-11 04:17:14 +00:00
}
2016-10-01 21:12:55 +00:00
int l_lovrHeadsetGetVelocity(lua_State* L) {
2016-09-17 02:43:43 +00:00
float x, y, z;
2016-10-01 21:12:55 +00:00
lovrHeadsetGetVelocity(&x, &y, &z);
2016-09-17 02:43:43 +00:00
lua_pushnumber(L, x);
lua_pushnumber(L, y);
lua_pushnumber(L, z);
return 3;
2016-08-11 04:17:14 +00:00
}
2016-10-01 21:12:55 +00:00
int l_lovrHeadsetGetAngularVelocity(lua_State* L) {
2016-09-17 02:43:43 +00:00
float x, y, z;
2016-10-01 21:12:55 +00:00
lovrHeadsetGetAngularVelocity(&x, &y, &z);
2016-09-17 02:43:43 +00:00
lua_pushnumber(L, x);
lua_pushnumber(L, y);
lua_pushnumber(L, z);
return 3;
2016-09-16 06:57:01 +00:00
}
2016-12-01 07:03:58 +00:00
int l_lovrHeadsetGetControllers(lua_State* L) {
vec_controller_t* controllers = lovrHeadsetGetControllers();
2016-12-02 01:06:27 +00:00
if (!controllers) {
lua_newtable(L);
return 1;
}
2016-12-01 07:03:58 +00:00
lua_newtable(L);
Controller* controller; int i;
vec_foreach(controllers, controller, i) {
luax_pushtype(L, Controller, controller);
lua_rawseti(L, -2, i + 1);
}
return 1;
}
int l_lovrHeadsetGetControllerCount(lua_State* L) {
vec_controller_t* controllers = lovrHeadsetGetControllers();
2016-12-02 01:06:27 +00:00
if (controllers) {
lua_pushnumber(L, controllers->length);
} else {
lua_pushnumber(L, 0);
}
2016-10-03 14:40:20 +00:00
return 1;
}
2016-08-11 04:17:14 +00:00
int l_lovrHeadsetRenderTo(lua_State* L) {
luaL_checktype(L, 1, LUA_TFUNCTION);
lovrHeadsetRenderTo(renderHelper, L);
return 0;
}
2017-03-11 11:08:07 +00:00
const luaL_Reg lovrHeadset[] = {
{ "isPresent", l_lovrHeadsetIsPresent },
{ "getType", l_lovrHeadsetGetType },
{ "isMirrored", l_lovrHeadsetIsMirrored },
{ "setMirrored", l_lovrHeadsetSetMirrored },
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 },
{ "isBoundsVisible", l_lovrHeadsetIsBoundsVisible },
{ "setBoundsVisible", l_lovrHeadsetSetBoundsVisible },
{ "getPosition", l_lovrHeadsetGetPosition },
{ "getEyePosition", l_lovrHeadsetGetEyePosition },
{ "getOrientation", l_lovrHeadsetGetOrientation },
{ "getVelocity", l_lovrHeadsetGetVelocity },
{ "getAngularVelocity", l_lovrHeadsetGetAngularVelocity },
{ "getControllers", l_lovrHeadsetGetControllers },
{ "getControllerCount", l_lovrHeadsetGetControllerCount },
{ "renderTo", l_lovrHeadsetRenderTo },
{ NULL, NULL }
};