mirror of https://github.com/bjornbytes/lovr.git
rm joystick mappings;
This commit is contained in:
parent
489acf5802
commit
9ae177e8c2
|
@ -10,8 +10,6 @@ LIBS += -l osvrClientKit
|
|||
LIBS += -framework OpenGL
|
||||
LIBS += -pagezero_size 10000 -image_base 100000000 # OSX magic for LuaJIT
|
||||
|
||||
: foreach vendor/map/*.c |> clang -c %f -o %o $(CFLAGS) |> obj/%B.o
|
||||
|
||||
: foreach event/*.c |> clang -c %f -o %o $(CFLAGS) |> obj/%B.o
|
||||
: foreach graphics/*.c |> clang -c %f -o %o $(CFLAGS) |> obj/%B.o
|
||||
: foreach joystick/*.c |> clang -c %f -o %o $(CFLAGS) |> obj/%B.o
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
#include "joystick.h"
|
||||
#include "../glfw.h"
|
||||
#include "../osvr.h"
|
||||
#include "../vendor/map/map.h"
|
||||
|
||||
extern OSVR_ClientContext ctx;
|
||||
|
||||
|
@ -18,8 +17,36 @@ Joystick* luax_checkjoystick(lua_State* L, int index) {
|
|||
return *(Joystick**) luaL_checkudata(L, index, "Joystick");
|
||||
}
|
||||
|
||||
static unsigned char lovrJoystickGetButtonState(Joystick* joystick, int buttonIndex) {
|
||||
if (joystick->isTracked) {
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_ButtonState state;
|
||||
osvrGetButtonState(*joystick->osvrButtonInterfaces[buttonIndex], ×tamp, &state);
|
||||
return state > 0;
|
||||
} else {
|
||||
int buttonCount;
|
||||
const unsigned char* buttons = glfwGetJoystickButtons(joystick->glfwIndex, &buttonCount);
|
||||
return buttons[buttonIndex];
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static float lovrJoystickGetAxisState(Joystick* joystick, int axisIndex) {
|
||||
if (joystick->isTracked) {
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_AnalogState state;
|
||||
osvrGetAnalogState(*joystick->osvrAxisInterfaces[axisIndex], ×tamp, &state);
|
||||
return (float)state;
|
||||
} else {
|
||||
int axisCount;
|
||||
const float* axes = glfwGetJoystickAxes(joystick->glfwIndex, &axisCount);
|
||||
return axes[axisIndex];
|
||||
}
|
||||
}
|
||||
|
||||
void lovrJoystickDestroy(Joystick* joystick) {
|
||||
if (joystick->type == JOYSTICK_TYPE_OSVR) {
|
||||
if (joystick->isTracked) {
|
||||
osvrClientFreeInterface(ctx, *joystick->osvrTrackerInterface);
|
||||
int i;
|
||||
|
||||
|
@ -33,211 +60,10 @@ void lovrJoystickDestroy(Joystick* joystick) {
|
|||
}
|
||||
}
|
||||
|
||||
int lovrJoystickMapAxis(Joystick* joystick, const char* key) {
|
||||
int* axisIndex = NULL;
|
||||
|
||||
if ((axisIndex = map_get(&joystick->axisMapping, key)) != NULL) {
|
||||
return *axisIndex;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int lovrJoystickMapButton(Joystick* joystick, const char* key) {
|
||||
int* buttonIndex = NULL;
|
||||
|
||||
if ((buttonIndex = map_get(&joystick->buttonMapping, key)) != NULL) {
|
||||
return *buttonIndex;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
unsigned char lovrJoystickGetButtonState(Joystick* joystick, int buttonIndex) {
|
||||
if (joystick->type == JOYSTICK_TYPE_OSVR) {
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_ButtonState state;
|
||||
osvrGetButtonState(*joystick->osvrButtonInterfaces[buttonIndex], ×tamp, &state);
|
||||
return state > 0;
|
||||
} else if (joystick->type == JOYSTICK_TYPE_GLFW) {
|
||||
int buttonCount;
|
||||
const unsigned char* buttons = glfwGetJoystickButtons(joystick->glfwIndex, &buttonCount);
|
||||
return buttons[buttonIndex];
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
float lovrJoystickGetAxisState(Joystick* joystick, int axisIndex) {
|
||||
if (joystick->type == JOYSTICK_TYPE_OSVR) {
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_AnalogState state;
|
||||
osvrGetAnalogState(*joystick->osvrAxisInterfaces[axisIndex], ×tamp, &state);
|
||||
return (float)state;
|
||||
} else if (joystick->type == JOYSTICK_TYPE_GLFW) {
|
||||
int axisCount;
|
||||
const float* axes = glfwGetJoystickAxes(joystick->glfwIndex, &axisCount);
|
||||
return axes[axisIndex];
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lovrJoystickIsGamepad(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
lua_pushboolean(L, joystick->type == JOYSTICK_TYPE_GLFW);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lovrJoystickIsTracked(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
lua_pushboolean(L, joystick->type == JOYSTICK_TYPE_OSVR);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lovrJoystickIsDown(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
const char* buttonName = luaL_checkstring(L, 2);
|
||||
|
||||
int buttonIndex = lovrJoystickMapButton(joystick, buttonName);
|
||||
|
||||
if (buttonIndex == -1) {
|
||||
return luaL_error(L, "Unknown button '%s'\n", buttonName);
|
||||
}
|
||||
|
||||
lua_pushboolean(L, lovrJoystickGetButtonState(joystick, buttonIndex));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lovrJoystickGetAxis(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
const char* axisName = luaL_checkstring(L, 2);
|
||||
|
||||
int axisIndex = lovrJoystickMapAxis(joystick, axisName);
|
||||
|
||||
if (axisIndex == -1) {
|
||||
return luaL_error(L, "Unknown axis '%s'\n", axisName);
|
||||
}
|
||||
|
||||
lua_pushnumber(L, lovrJoystickGetAxisState(joystick, axisIndex));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lovrJoystickGetPosition(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
|
||||
if (joystick->type != JOYSTICK_TYPE_OSVR) {
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
return 3;
|
||||
}
|
||||
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_PositionState state;
|
||||
osvrGetPositionState(*joystick->osvrTrackerInterface, ×tamp, &state);
|
||||
lua_pushnumber(L, osvrVec3GetX(&state));
|
||||
lua_pushnumber(L, osvrVec3GetY(&state));
|
||||
lua_pushnumber(L, osvrVec3GetZ(&state));
|
||||
return 3;
|
||||
}
|
||||
|
||||
int lovrJoystickGetOrientation(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
|
||||
if (joystick->type != JOYSTICK_TYPE_OSVR) {
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
return 4;
|
||||
}
|
||||
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_OrientationState state;
|
||||
osvrGetOrientationState(*joystick->osvrTrackerInterface, ×tamp, &state);
|
||||
lua_pushnumber(L, osvrQuatGetW(&state));
|
||||
lua_pushnumber(L, osvrQuatGetX(&state));
|
||||
lua_pushnumber(L, osvrQuatGetY(&state));
|
||||
lua_pushnumber(L, osvrQuatGetZ(&state));
|
||||
return 4;
|
||||
}
|
||||
|
||||
int lovrJoystickGetLinearVelocity(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
|
||||
if (joystick->type != JOYSTICK_TYPE_OSVR) {
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
return 3;
|
||||
}
|
||||
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_LinearVelocityState state;
|
||||
osvrGetLinearVelocityState(*joystick->osvrTrackerInterface, ×tamp, &state);
|
||||
lua_pushnumber(L, osvrVec3GetX(&state));
|
||||
lua_pushnumber(L, osvrVec3GetY(&state));
|
||||
lua_pushnumber(L, osvrVec3GetZ(&state));
|
||||
return 3;
|
||||
}
|
||||
|
||||
int lovrJoystickGetAngularVelocity(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
|
||||
if (joystick->type != JOYSTICK_TYPE_OSVR) {
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
return 5;
|
||||
}
|
||||
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_AngularVelocityState state;
|
||||
osvrGetAngularVelocityState(*joystick->osvrTrackerInterface, ×tamp, &state);
|
||||
lua_pushnumber(L, osvrQuatGetW(&state.incrementalRotation));
|
||||
lua_pushnumber(L, osvrQuatGetX(&state.incrementalRotation));
|
||||
lua_pushnumber(L, osvrQuatGetY(&state.incrementalRotation));
|
||||
lua_pushnumber(L, osvrQuatGetZ(&state.incrementalRotation));
|
||||
lua_pushnumber(L, state.dt);
|
||||
return 5;
|
||||
}
|
||||
|
||||
int lovrJoystickGetLinearAcceleration(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
|
||||
if (joystick->type != JOYSTICK_TYPE_OSVR) {
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
return 3;
|
||||
}
|
||||
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_LinearAccelerationState state;
|
||||
osvrGetLinearAccelerationState(*joystick->osvrTrackerInterface, ×tamp, &state);
|
||||
lua_pushnumber(L, osvrVec3GetX(&state));
|
||||
lua_pushnumber(L, osvrVec3GetY(&state));
|
||||
lua_pushnumber(L, osvrVec3GetZ(&state));
|
||||
return 3;
|
||||
}
|
||||
|
||||
int lovrJoystickGetAngularAcceleration(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
|
||||
if (joystick->type != JOYSTICK_TYPE_OSVR) {
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
return 5;
|
||||
}
|
||||
|
||||
if (joystick->isTracked) {
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_AngularAccelerationState state;
|
||||
osvrGetAngularAccelerationState(*joystick->osvrTrackerInterface, ×tamp, &state);
|
||||
|
@ -246,120 +72,214 @@ int lovrJoystickGetAngularAcceleration(lua_State* L) {
|
|||
lua_pushnumber(L, osvrQuatGetY(&state.incrementalRotation));
|
||||
lua_pushnumber(L, osvrQuatGetZ(&state.incrementalRotation));
|
||||
lua_pushnumber(L, state.dt);
|
||||
} else {
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
}
|
||||
|
||||
return 5;
|
||||
}
|
||||
|
||||
int lovrJoystickGetMapping(lua_State* L) {
|
||||
int lovrJoystickGetAngularVelocity(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
const char* key;
|
||||
map_iter_t iterator;
|
||||
|
||||
lua_newtable(L);
|
||||
|
||||
lua_newtable(L);
|
||||
iterator = map_iter(&joystick->axisMapping);
|
||||
while ((key = map_next(&joystick->axisMapping, &iterator)) != NULL) {
|
||||
lua_pushstring(L, key);
|
||||
lua_pushnumber(L, *map_get(&joystick->axisMapping, key));
|
||||
lua_settable(L, -3);
|
||||
if (joystick->isTracked) {
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_AngularVelocityState state;
|
||||
osvrGetAngularVelocityState(*joystick->osvrTrackerInterface, ×tamp, &state);
|
||||
lua_pushnumber(L, osvrQuatGetW(&state.incrementalRotation));
|
||||
lua_pushnumber(L, osvrQuatGetX(&state.incrementalRotation));
|
||||
lua_pushnumber(L, osvrQuatGetY(&state.incrementalRotation));
|
||||
lua_pushnumber(L, osvrQuatGetZ(&state.incrementalRotation));
|
||||
lua_pushnumber(L, state.dt);
|
||||
} else {
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
}
|
||||
lua_setfield(L, -2, "axis");
|
||||
|
||||
lua_newtable(L);
|
||||
iterator = map_iter(&joystick->buttonMapping);
|
||||
while ((key = map_next(&joystick->buttonMapping, &iterator)) != NULL) {
|
||||
lua_pushstring(L, key);
|
||||
lua_pushnumber(L, *map_get(&joystick->buttonMapping, key));
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
lua_setfield(L, -2, "button");
|
||||
|
||||
return 1;
|
||||
return 5;
|
||||
}
|
||||
|
||||
int lovrJoystickSetMapping(lua_State* L) {
|
||||
int lovrJoystickGetAxes(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
luaL_checktype(L, 2, LUA_TTABLE);
|
||||
int axisCount = 0;
|
||||
|
||||
// make a map
|
||||
|
||||
lua_pushnil(L);
|
||||
while (lua_next(L, 2) != 0) {
|
||||
const char* key = luaL_checkstring(L, -2);
|
||||
int index = luaL_checkint(L, -1);
|
||||
|
||||
// map stuff
|
||||
|
||||
lua_pop(L, 1);
|
||||
if (joystick->isTracked) {
|
||||
OSVR_ClientInterface** axes = joystick->osvrAxisInterfaces;
|
||||
for (; axisCount < sizeof(axes) && axes[axisCount] != NULL; axisCount++) {
|
||||
lua_pushnumber(L, lovrJoystickGetAxisState(joystick, axisCount));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lovrJoystickGetRawAxes(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
|
||||
lua_newtable(L);
|
||||
|
||||
if (joystick->type == JOYSTICK_TYPE_OSVR) {
|
||||
for (int i = 0; i < sizeof(joystick->osvrAxisInterfaces); i++) {
|
||||
lua_pushnumber(L, lovrJoystickGetAxisState(joystick, i));
|
||||
lua_rawseti(L, -2, i + 1);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int axisCount;
|
||||
} else {
|
||||
const float* axes = glfwGetJoystickAxes(joystick->glfwIndex, &axisCount);
|
||||
|
||||
for (int i = 0; i < axisCount; i++) {
|
||||
lua_pushnumber(L, axes[i]);
|
||||
lua_rawseti(L, -2, i + 1);
|
||||
}
|
||||
}
|
||||
|
||||
return axisCount;
|
||||
}
|
||||
|
||||
int lovrJoystickGetAxis(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
int axisIndex = luaL_checkint(L, 2);
|
||||
lua_pushnumber(L, lovrJoystickGetAxisState(joystick, axisIndex));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lovrJoystickGetAxisCount(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
int axisCount = 0;
|
||||
|
||||
if (joystick->isTracked) {
|
||||
OSVR_ClientInterface** axes = joystick->osvrAxisInterfaces;
|
||||
for (; axisCount < sizeof(axes) && axes[axisCount] != NULL; axisCount++);
|
||||
} else {
|
||||
glfwGetJoystickAxes(joystick->glfwIndex, &axisCount);
|
||||
}
|
||||
|
||||
lua_pushinteger(L, axisCount);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lovrJoystickGetButtonCount(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
int buttonCount = 0;
|
||||
|
||||
if (joystick->isTracked) {
|
||||
OSVR_ClientInterface** buttons = joystick->osvrButtonInterfaces;
|
||||
for (; buttonCount < sizeof(buttons) && buttons[buttonCount] != NULL; buttonCount++);
|
||||
} else {
|
||||
glfwGetJoystickButtons(joystick->glfwIndex, &buttonCount);
|
||||
}
|
||||
|
||||
lua_pushinteger(L, buttonCount);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lovrJoystickGetLinearAcceleration(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
|
||||
if (joystick->isTracked) {
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_LinearAccelerationState state;
|
||||
osvrGetLinearAccelerationState(*joystick->osvrTrackerInterface, ×tamp, &state);
|
||||
lua_pushnumber(L, osvrVec3GetX(&state));
|
||||
lua_pushnumber(L, osvrVec3GetY(&state));
|
||||
lua_pushnumber(L, osvrVec3GetZ(&state));
|
||||
} else {
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
}
|
||||
|
||||
return 3;
|
||||
}
|
||||
|
||||
int lovrJoystickGetLinearVelocity(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
|
||||
if (joystick->isTracked) {
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_LinearVelocityState state;
|
||||
osvrGetLinearVelocityState(*joystick->osvrTrackerInterface, ×tamp, &state);
|
||||
lua_pushnumber(L, osvrVec3GetX(&state));
|
||||
lua_pushnumber(L, osvrVec3GetY(&state));
|
||||
lua_pushnumber(L, osvrVec3GetZ(&state));
|
||||
} else {
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
}
|
||||
|
||||
return 3;
|
||||
}
|
||||
|
||||
int lovrJoystickGetName(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
|
||||
if (joystick->isTracked) {
|
||||
lua_pushstring(L, "Tracked controller");
|
||||
} else {
|
||||
lua_pushstring(L, glfwGetJoystickName(joystick->glfwIndex));
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lovrJoystickGetRawButtons(lua_State* L) {
|
||||
int lovrJoystickGetOrientation(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
|
||||
lua_newtable(L);
|
||||
|
||||
if (joystick->type == JOYSTICK_TYPE_OSVR) {
|
||||
for (int i = 0; i < sizeof(joystick->osvrButtonInterfaces); i++) {
|
||||
lua_pushboolean(L, lovrJoystickGetButtonState(joystick, i));
|
||||
lua_rawseti(L, -2, i + 1);
|
||||
if (joystick->isTracked) {
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_OrientationState state;
|
||||
osvrGetOrientationState(*joystick->osvrTrackerInterface, ×tamp, &state);
|
||||
lua_pushnumber(L, osvrQuatGetW(&state));
|
||||
lua_pushnumber(L, osvrQuatGetX(&state));
|
||||
lua_pushnumber(L, osvrQuatGetY(&state));
|
||||
lua_pushnumber(L, osvrQuatGetZ(&state));
|
||||
} else {
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
}
|
||||
|
||||
return 4;
|
||||
}
|
||||
|
||||
int lovrJoystickGetPosition(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
|
||||
if (joystick->isTracked) {
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_PositionState state;
|
||||
osvrGetPositionState(*joystick->osvrTrackerInterface, ×tamp, &state);
|
||||
lua_pushnumber(L, osvrVec3GetX(&state));
|
||||
lua_pushnumber(L, osvrVec3GetY(&state));
|
||||
lua_pushnumber(L, osvrVec3GetZ(&state));
|
||||
} else {
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushnumber(L, 0);
|
||||
}
|
||||
|
||||
return 3;
|
||||
}
|
||||
|
||||
int lovrJoystickIsDown(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
int buttonIndex = luaL_checkint(L, 2);
|
||||
lua_pushboolean(L, lovrJoystickGetButtonState(joystick, buttonIndex));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int buttonCount;
|
||||
const unsigned char* buttons = glfwGetJoystickButtons(joystick->glfwIndex, &buttonCount);
|
||||
|
||||
for (int i = 0; i < buttonCount; i++) {
|
||||
lua_pushboolean(L, buttons[i]);
|
||||
lua_rawseti(L, -2, i + 1);
|
||||
}
|
||||
}
|
||||
|
||||
int lovrJoystickIsTracked(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
lua_pushboolean(L, joystick->isTracked);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const luaL_Reg lovrJoystick[] = {
|
||||
{ "isGamepad", lovrJoystickIsGamepad },
|
||||
{ "isTracked", lovrJoystickIsTracked },
|
||||
{ "isDown", lovrJoystickIsDown },
|
||||
{ "getAxis", lovrJoystickGetAxis },
|
||||
{ "getPosition", lovrJoystickGetPosition },
|
||||
{ "getOrientation", lovrJoystickGetOrientation },
|
||||
{ "getLinearVelocity", lovrJoystickGetLinearVelocity },
|
||||
{ "getAngularVelocity", lovrJoystickGetAngularVelocity },
|
||||
{ "getLinearAcceleration", lovrJoystickGetLinearAcceleration },
|
||||
{ "getAngularAcceleration", lovrJoystickGetAngularAcceleration },
|
||||
{ "getMapping", lovrJoystickGetMapping },
|
||||
{ "setMapping", lovrJoystickSetMapping },
|
||||
{ "getRawAxes", lovrJoystickGetRawAxes },
|
||||
{ "getRawButtons", lovrJoystickGetRawButtons },
|
||||
{ "getAngularVelocity", lovrJoystickGetAngularVelocity },
|
||||
{ "getAxes", lovrJoystickGetAxes },
|
||||
{ "getAxis", lovrJoystickGetAxis },
|
||||
{ "getAxisCount", lovrJoystickGetAxisCount },
|
||||
{ "getButtonCount", lovrJoystickGetButtonCount },
|
||||
{ "getLinearAcceleration", lovrJoystickGetLinearAcceleration },
|
||||
{ "getLinearVelocity", lovrJoystickGetLinearVelocity },
|
||||
{ "getName", lovrJoystickGetName },
|
||||
{ "getOrientation", lovrJoystickGetOrientation },
|
||||
{ "getPosition", lovrJoystickGetPosition },
|
||||
{ "isDown", lovrJoystickIsDown },
|
||||
{ "isTracked", lovrJoystickIsTracked },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
|
|
@ -1,20 +1,10 @@
|
|||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
#include "../vendor/map/map.h"
|
||||
#include "../osvr.h"
|
||||
|
||||
typedef enum {
|
||||
JOYSTICK_TYPE_GLFW,
|
||||
JOYSTICK_TYPE_OSVR
|
||||
} JoystickType;
|
||||
|
||||
typedef map_int_t JoystickMapping;
|
||||
|
||||
typedef struct {
|
||||
JoystickType type;
|
||||
JoystickMapping axisMapping;
|
||||
JoystickMapping buttonMapping;
|
||||
int isTracked;
|
||||
int glfwIndex;
|
||||
OSVR_ClientInterface* osvrTrackerInterface;
|
||||
OSVR_ClientInterface* osvrAxisInterfaces[4];
|
||||
|
@ -25,22 +15,18 @@ void luax_pushjoystick(lua_State* L, Joystick* joystick);
|
|||
Joystick* luax_checkjoystick(lua_State* L, int index);
|
||||
|
||||
void lovrJoystickDestroy(Joystick* joystick);
|
||||
int lovrJoystickMapAxis(Joystick* joystick, const char* key);
|
||||
int lovrJoystickMapButton(Joystick* joystick, const char* key);
|
||||
unsigned char lovrJoystickGetButtonState(Joystick* joystick, int buttonIndex);
|
||||
|
||||
int lovrJoystickIsGamepad(lua_State* L);
|
||||
int lovrJoystickIsTracked(lua_State* L);
|
||||
int lovrJoystickIsDown(lua_State* L);
|
||||
int lovrJoystickGetAxis(lua_State* L);
|
||||
int lovrJoystickGetPosition(lua_State* L);
|
||||
int lovrJoystickGetOrientation(lua_State* L);
|
||||
int lovrJoystickGetLinearVelocity(lua_State* L);
|
||||
int lovrJoystickGetAngularVelocity(lua_State* L);
|
||||
int lovrJoystickGetLinearAcceleration(lua_State* L);
|
||||
int lovrJoystickGetAngularAcceleration(lua_State* L);
|
||||
int lovrJoystickGetMapping(lua_State* L);
|
||||
int lovrJoystickSetMapping(lua_State* L);
|
||||
int lovrJoystickGetRawAxes(lua_State* L);
|
||||
int lovrJoystickGetRawButtons(lua_State* L);
|
||||
int lovrJoystickGetAngularVelocity(lua_State* L);
|
||||
int lovrJoystickGetAxes(lua_State* L);
|
||||
int lovrJoystickGetAxis(lua_State* L);
|
||||
int lovrJoystickGetAxisCount(lua_State* L);
|
||||
int lovrJoystickGetButtonCount(lua_State* L);
|
||||
int lovrJoystickGetLinearAcceleration(lua_State* L);
|
||||
int lovrJoystickGetLinearVelocity(lua_State* L);
|
||||
int lovrJoystickGetName(lua_State* L);
|
||||
int lovrJoystickGetOrientation(lua_State* L);
|
||||
int lovrJoystickGetPosition(lua_State* L);
|
||||
int lovrJoystickIsDown(lua_State* L);
|
||||
int lovrJoystickIsTracked(lua_State* L);
|
||||
extern const luaL_Reg lovrJoystick[];
|
||||
|
|
|
@ -14,16 +14,15 @@ static JoystickState joystickState;
|
|||
|
||||
int lovrJoysticksGetJoystickCount(lua_State* L) {
|
||||
lua_pushnumber(L, joystickState.count);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lovrJoysticksGetJoysticks(lua_State* L) {
|
||||
lua_newtable(L);
|
||||
|
||||
for (int i = 0; joystickState.list[i] != NULL && i < 32; i++) {
|
||||
for (int i = 0; joystickState.list[i] != NULL && i < sizeof(joystickState.list); i++) {
|
||||
luax_pushjoystick(L, joystickState.list[i]);
|
||||
lua_rawseti(L, -2, i);
|
||||
lua_rawseti(L, -2, i + 1);
|
||||
}
|
||||
|
||||
return 1;
|
||||
|
@ -56,12 +55,9 @@ int lovrInitJoysticks(lua_State* L) {
|
|||
for (i = GLFW_JOYSTICK_1; i <= GLFW_JOYSTICK_LAST; i++) {
|
||||
if (glfwJoystickPresent(i)) {
|
||||
Joystick* joystick = malloc(sizeof(Joystick));
|
||||
joystick->type = JOYSTICK_TYPE_GLFW;
|
||||
joystick->isTracked = 0;
|
||||
joystick->glfwIndex = i;
|
||||
|
||||
map_init(&joystick->axisMapping);
|
||||
map_init(&joystick->buttonMapping);
|
||||
|
||||
joystickState.list[count++] = joystick;
|
||||
}
|
||||
}
|
||||
|
@ -79,7 +75,7 @@ int lovrInitJoysticks(lua_State* L) {
|
|||
|
||||
if (trackerInterface != NULL) {
|
||||
Joystick* joystick = malloc(sizeof(Joystick));
|
||||
joystick->type = JOYSTICK_TYPE_OSVR;
|
||||
joystick->isTracked = 1;
|
||||
joystick->glfwIndex = -1;
|
||||
joystick->osvrTrackerInterface = trackerInterface;
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include <lualib.h>
|
||||
|
||||
int lovrJoysticksGetJoystickCount(lua_State* L);
|
||||
int lovrJoysticksGetJoysticks(lua_State* L);
|
||||
|
||||
extern const luaL_Reg lovrJoysticks[];
|
||||
int lovrInitJoysticks(lua_State* L);
|
||||
|
|
|
@ -38,7 +38,7 @@ void lovrInit(lua_State* L) {
|
|||
"end "
|
||||
|
||||
"if not success and err then "
|
||||
" error(err, -1) "
|
||||
" print('Could not run conf.lua') "
|
||||
"end "
|
||||
|
||||
"local modules = { 'event', 'graphics', 'joystick', 'timer' } "
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
Copyright (c) 2014 rxi
|
||||
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
|
@ -1,193 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2014 rxi
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the MIT license. See LICENSE for details.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "map.h"
|
||||
|
||||
struct map_node_t {
|
||||
unsigned hash;
|
||||
void *value;
|
||||
map_node_t *next;
|
||||
/* char key[]; */
|
||||
/* char value[]; */
|
||||
};
|
||||
|
||||
|
||||
static unsigned map_hash(const char *str) {
|
||||
unsigned hash = 5381;
|
||||
while (*str) {
|
||||
hash = ((hash << 5) + hash) ^ *str++;
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
|
||||
static map_node_t *map_newnode(const char *key, void *value, int vsize) {
|
||||
map_node_t *node;
|
||||
int ksize = strlen(key) + 1;
|
||||
int voffset = ksize + ((sizeof(void*) - ksize) % sizeof(void*));
|
||||
node = malloc(sizeof(*node) + voffset + vsize);
|
||||
if (!node) return NULL;
|
||||
memcpy(node + 1, key, ksize);
|
||||
node->hash = map_hash(key);
|
||||
node->value = ((char*) (node + 1)) + voffset;
|
||||
memcpy(node->value, value, vsize);
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
static int map_bucketidx(map_base_t *m, unsigned hash) {
|
||||
/* If the implementation is changed to allow a non-power-of-2 bucket count,
|
||||
* the line below should be changed to use mod instead of AND */
|
||||
return hash & (m->nbuckets - 1);
|
||||
}
|
||||
|
||||
|
||||
static void map_addnode(map_base_t *m, map_node_t *node) {
|
||||
int n = map_bucketidx(m, node->hash);
|
||||
node->next = m->buckets[n];
|
||||
m->buckets[n] = node;
|
||||
}
|
||||
|
||||
|
||||
static int map_resize(map_base_t *m, int nbuckets) {
|
||||
map_node_t *nodes, *node, *next;
|
||||
map_node_t **buckets;
|
||||
int i;
|
||||
/* Chain all nodes together */
|
||||
nodes = NULL;
|
||||
i = m->nbuckets;
|
||||
while (i--) {
|
||||
node = (m->buckets)[i];
|
||||
while (node) {
|
||||
next = node->next;
|
||||
node->next = nodes;
|
||||
nodes = node;
|
||||
node = next;
|
||||
}
|
||||
}
|
||||
/* Reset buckets */
|
||||
buckets = realloc(m->buckets, sizeof(*m->buckets) * nbuckets);
|
||||
if (buckets != NULL) {
|
||||
m->buckets = buckets;
|
||||
m->nbuckets = nbuckets;
|
||||
}
|
||||
if (m->buckets) {
|
||||
memset(m->buckets, 0, sizeof(*m->buckets) * m->nbuckets);
|
||||
/* Re-add nodes to buckets */
|
||||
node = nodes;
|
||||
while (node) {
|
||||
next = node->next;
|
||||
map_addnode(m, node);
|
||||
node = next;
|
||||
}
|
||||
}
|
||||
/* Return error code if realloc() failed */
|
||||
return (buckets == NULL) ? -1 : 0;
|
||||
}
|
||||
|
||||
|
||||
static map_node_t **map_getref(map_base_t *m, const char *key) {
|
||||
unsigned hash = map_hash(key);
|
||||
map_node_t **next;
|
||||
if (m->nbuckets > 0) {
|
||||
next = &m->buckets[map_bucketidx(m, hash)];
|
||||
while (*next) {
|
||||
if ((*next)->hash == hash && !strcmp((char*) (*next + 1), key)) {
|
||||
return next;
|
||||
}
|
||||
next = &(*next)->next;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
void map_deinit_(map_base_t *m) {
|
||||
map_node_t *next, *node;
|
||||
int i;
|
||||
i = m->nbuckets;
|
||||
while (i--) {
|
||||
node = m->buckets[i];
|
||||
while (node) {
|
||||
next = node->next;
|
||||
free(node);
|
||||
node = next;
|
||||
}
|
||||
}
|
||||
free(m->buckets);
|
||||
}
|
||||
|
||||
|
||||
void *map_get_(map_base_t *m, const char *key) {
|
||||
map_node_t **next = map_getref(m, key);
|
||||
return next ? (*next)->value : NULL;
|
||||
}
|
||||
|
||||
|
||||
int map_set_(map_base_t *m, const char *key, void *value, int vsize) {
|
||||
int n, err;
|
||||
map_node_t **next, *node;
|
||||
/* Find & replace existing node */
|
||||
next = map_getref(m, key);
|
||||
if (next) {
|
||||
memcpy((*next)->value, value, vsize);
|
||||
return 0;
|
||||
}
|
||||
/* Add new node */
|
||||
node = map_newnode(key, value, vsize);
|
||||
if (node == NULL) goto fail;
|
||||
if (m->nnodes >= m->nbuckets) {
|
||||
n = (m->nbuckets > 0) ? (m->nbuckets << 1) : 1;
|
||||
err = map_resize(m, n);
|
||||
if (err) goto fail;
|
||||
}
|
||||
map_addnode(m, node);
|
||||
m->nnodes++;
|
||||
return 0;
|
||||
fail:
|
||||
if (node) free(node);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
void map_remove_(map_base_t *m, const char *key) {
|
||||
map_node_t *node;
|
||||
map_node_t **next = map_getref(m, key);
|
||||
if (next) {
|
||||
node = *next;
|
||||
*next = (*next)->next;
|
||||
free(node);
|
||||
m->nnodes--;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
map_iter_t map_iter_(void) {
|
||||
map_iter_t iter;
|
||||
iter.bucketidx = -1;
|
||||
iter.node = NULL;
|
||||
return iter;
|
||||
}
|
||||
|
||||
|
||||
const char *map_next_(map_base_t *m, map_iter_t *iter) {
|
||||
if (iter->node) {
|
||||
iter->node = iter->node->next;
|
||||
if (iter->node == NULL) goto nextBucket;
|
||||
} else {
|
||||
nextBucket:
|
||||
do {
|
||||
if (++iter->bucketidx >= m->nbuckets) {
|
||||
return NULL;
|
||||
}
|
||||
iter->node = m->buckets[iter->bucketidx];
|
||||
} while (iter->node == NULL);
|
||||
}
|
||||
return (char*) (iter->node + 1);
|
||||
}
|
|
@ -1,77 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2014 rxi
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the MIT license. See LICENSE for details.
|
||||
*/
|
||||
|
||||
#ifndef MAP_H
|
||||
#define MAP_H
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define MAP_VERSION "0.1.0"
|
||||
|
||||
struct map_node_t;
|
||||
typedef struct map_node_t map_node_t;
|
||||
|
||||
typedef struct {
|
||||
map_node_t **buckets;
|
||||
unsigned nbuckets, nnodes;
|
||||
} map_base_t;
|
||||
|
||||
typedef struct {
|
||||
unsigned bucketidx;
|
||||
map_node_t *node;
|
||||
} map_iter_t;
|
||||
|
||||
|
||||
#define map_t(T)\
|
||||
struct { map_base_t base; T *ref; T tmp; }
|
||||
|
||||
|
||||
#define map_init(m)\
|
||||
memset(m, 0, sizeof(*(m)))
|
||||
|
||||
|
||||
#define map_deinit(m)\
|
||||
map_deinit_(&(m)->base)
|
||||
|
||||
|
||||
#define map_get(m, key)\
|
||||
( (m)->ref = map_get_(&(m)->base, key) )
|
||||
|
||||
|
||||
#define map_set(m, key, value)\
|
||||
( (m)->tmp = (value),\
|
||||
map_set_(&(m)->base, key, &(m)->tmp, sizeof((m)->tmp)) )
|
||||
|
||||
|
||||
#define map_remove(m, key)\
|
||||
map_remove_(&(m)->base, key)
|
||||
|
||||
|
||||
#define map_iter(m)\
|
||||
map_iter_()
|
||||
|
||||
|
||||
#define map_next(m, iter)\
|
||||
map_next_(&(m)->base, iter)
|
||||
|
||||
|
||||
void map_deinit_(map_base_t *m);
|
||||
void *map_get_(map_base_t *m, const char *key);
|
||||
int map_set_(map_base_t *m, const char *key, void *value, int vsize);
|
||||
void map_remove_(map_base_t *m, const char *key);
|
||||
map_iter_t map_iter_(void);
|
||||
const char *map_next_(map_base_t *m, map_iter_t *iter);
|
||||
|
||||
|
||||
typedef map_t(void*) map_void_t;
|
||||
typedef map_t(char*) map_str_t;
|
||||
typedef map_t(int) map_int_t;
|
||||
typedef map_t(char) map_char_t;
|
||||
typedef map_t(float) map_float_t;
|
||||
typedef map_t(double) map_double_t;
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue