2017-12-10 20:40:37 +00:00
|
|
|
#include "api.h"
|
2016-11-19 09:28:01 +00:00
|
|
|
#include "event/event.h"
|
2016-11-29 06:59:27 +00:00
|
|
|
|
2017-03-11 11:08:07 +00:00
|
|
|
map_int_t EventTypes;
|
|
|
|
|
2017-01-18 06:11:17 +00:00
|
|
|
static int pollRef;
|
|
|
|
|
2016-11-29 06:59:27 +00:00
|
|
|
static int nextEvent(lua_State* L) {
|
2017-05-17 06:03:42 +00:00
|
|
|
Event event;
|
2016-11-29 06:59:27 +00:00
|
|
|
|
2017-05-17 06:03:42 +00:00
|
|
|
if (!lovrEventPoll(&event)) {
|
2016-11-29 06:59:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-17 06:03:42 +00:00
|
|
|
switch (event.type) {
|
2017-03-12 02:46:07 +00:00
|
|
|
case EVENT_QUIT: {
|
2016-11-29 06:59:27 +00:00
|
|
|
lua_pushstring(L, "quit");
|
2018-01-26 02:32:16 +00:00
|
|
|
if (event.data.quit.restart)
|
|
|
|
lua_pushliteral(L, "restart");
|
|
|
|
else
|
|
|
|
lua_pushnumber(L, event.data.quit.exitCode);
|
2016-11-29 06:59:27 +00:00
|
|
|
return 2;
|
2017-03-12 02:46:07 +00:00
|
|
|
}
|
2016-11-29 06:59:27 +00:00
|
|
|
|
2017-03-12 03:01:01 +00:00
|
|
|
case EVENT_FOCUS: {
|
|
|
|
lua_pushstring(L, "focus");
|
2017-05-17 06:03:42 +00:00
|
|
|
lua_pushboolean(L, event.data.focus.isFocused);
|
2017-03-12 03:01:01 +00:00
|
|
|
return 2;
|
|
|
|
};
|
|
|
|
|
2016-12-01 07:03:58 +00:00
|
|
|
case EVENT_CONTROLLER_ADDED: {
|
|
|
|
lua_pushstring(L, "controlleradded");
|
2017-05-17 06:03:42 +00:00
|
|
|
luax_pushtype(L, Controller, event.data.controlleradded.controller);
|
|
|
|
lovrRelease(&event.data.controlleradded.controller->ref);
|
2016-12-01 07:03:58 +00:00
|
|
|
return 2;
|
2017-03-12 02:46:07 +00:00
|
|
|
}
|
2016-12-01 07:03:58 +00:00
|
|
|
|
|
|
|
case EVENT_CONTROLLER_REMOVED: {
|
|
|
|
lua_pushstring(L, "controllerremoved");
|
2017-05-17 06:03:42 +00:00
|
|
|
luax_pushtype(L, Controller, event.data.controllerremoved.controller);
|
|
|
|
lovrRelease(&event.data.controlleradded.controller->ref);
|
2016-12-01 07:03:58 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2017-03-12 02:46:07 +00:00
|
|
|
case EVENT_CONTROLLER_PRESSED: {
|
|
|
|
lua_pushstring(L, "controllerpressed");
|
2017-05-17 06:03:42 +00:00
|
|
|
luax_pushtype(L, Controller, event.data.controllerpressed.controller);
|
|
|
|
luax_pushenum(L, &ControllerButtons, event.data.controllerpressed.button);
|
2017-03-12 02:46:07 +00:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
case EVENT_CONTROLLER_RELEASED: {
|
|
|
|
lua_pushstring(L, "controllerreleased");
|
2017-05-17 06:03:42 +00:00
|
|
|
luax_pushtype(L, Controller, event.data.controllerreleased.controller);
|
|
|
|
luax_pushenum(L, &ControllerButtons, event.data.controllerreleased.button);
|
2017-03-12 02:46:07 +00:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2016-11-29 06:59:27 +00:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2016-08-10 06:28:17 +00:00
|
|
|
|
|
|
|
int l_lovrEventInit(lua_State* L) {
|
|
|
|
lua_newtable(L);
|
|
|
|
luaL_register(L, NULL, lovrEvent);
|
2017-01-18 06:11:17 +00:00
|
|
|
|
|
|
|
// Store nextEvent in the registry to avoid creating a closure every time we poll for events.
|
|
|
|
lua_pushcfunction(L, nextEvent);
|
|
|
|
pollRef = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
|
2016-11-29 06:59:27 +00:00
|
|
|
map_init(&EventTypes);
|
|
|
|
map_set(&EventTypes, "quit", EVENT_QUIT);
|
|
|
|
lovrEventInit();
|
2016-08-10 06:28:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-11-29 06:59:27 +00:00
|
|
|
int l_lovrEventClear(lua_State* L) {
|
|
|
|
lovrEventClear();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-10 06:28:17 +00:00
|
|
|
int l_lovrEventPoll(lua_State* L) {
|
2017-01-18 06:11:17 +00:00
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, pollRef);
|
2016-11-29 06:59:27 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int l_lovrEventPump(lua_State* L) {
|
|
|
|
lovrEventPump();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int l_lovrEventPush(lua_State* L) {
|
|
|
|
EventType type = *(EventType*) luax_checkenum(L, 1, &EventTypes, "event type");
|
|
|
|
EventData data;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case EVENT_QUIT:
|
|
|
|
data.quit.exitCode = luaL_optint(L, 2, 0);
|
|
|
|
break;
|
2016-12-01 07:03:58 +00:00
|
|
|
|
2017-03-12 03:01:01 +00:00
|
|
|
case EVENT_FOCUS:
|
|
|
|
data.focus.isFocused = lua_toboolean(L, 2);
|
|
|
|
break;
|
|
|
|
|
2016-12-01 07:03:58 +00:00
|
|
|
case EVENT_CONTROLLER_ADDED:
|
|
|
|
data.controlleradded.controller = luax_checktype(L, 2, Controller);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EVENT_CONTROLLER_REMOVED:
|
|
|
|
data.controllerremoved.controller = luax_checktype(L, 2, Controller);
|
|
|
|
break;
|
2017-03-12 02:46:07 +00:00
|
|
|
|
|
|
|
case EVENT_CONTROLLER_PRESSED:
|
|
|
|
data.controllerpressed.controller = luax_checktype(L, 2, Controller);
|
|
|
|
data.controllerpressed.button = *(ControllerButton*) luax_checkenum(L, 3, &ControllerButtons, "button");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EVENT_CONTROLLER_RELEASED:
|
|
|
|
data.controllerreleased.controller = luax_checktype(L, 2, Controller);
|
|
|
|
data.controllerreleased.button = *(ControllerButton*) luax_checkenum(L, 3, &ControllerButtons, "button");
|
|
|
|
break;
|
2016-11-29 06:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Event event = { .type = type, .data = data };
|
|
|
|
lovrEventPush(event);
|
2016-08-10 06:28:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int l_lovrEventQuit(lua_State* L) {
|
2018-01-26 02:32:16 +00:00
|
|
|
EventData data;
|
|
|
|
|
|
|
|
int argType = lua_type(L, 1);
|
|
|
|
if (argType == LUA_TSTRING && 0 == strcmp("restart", lua_tostring(L, 1))) {
|
|
|
|
data.quit.restart = true;
|
|
|
|
data.quit.exitCode = 0;
|
|
|
|
} else if (argType == LUA_TNUMBER || lua_isnoneornil(L, 1)) {
|
|
|
|
data.quit.restart = false;
|
|
|
|
data.quit.exitCode = luaL_optint(L, 1, 0);
|
|
|
|
} else {
|
|
|
|
return luaL_argerror (L, 1, "number, nil or the exact string 'restart' expected");
|
|
|
|
}
|
|
|
|
|
|
|
|
EventType type = EVENT_QUIT;
|
|
|
|
Event event = { .type = type, .data = data };
|
|
|
|
lovrEventPush(event);
|
2016-08-10 06:28:17 +00:00
|
|
|
}
|
2017-03-11 11:08:07 +00:00
|
|
|
|
|
|
|
const luaL_Reg lovrEvent[] = {
|
|
|
|
{ "clear", l_lovrEventClear },
|
|
|
|
{ "poll", l_lovrEventPoll },
|
|
|
|
{ "pump", l_lovrEventPump },
|
|
|
|
{ "push", l_lovrEventPush },
|
|
|
|
{ "quit", l_lovrEventQuit },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|