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
|
|
|
|
2018-07-05 03:11:52 +00:00
|
|
|
const char* EventTypes[] = {
|
|
|
|
[EVENT_QUIT] = "quit",
|
|
|
|
[EVENT_FOCUS] = "focus",
|
|
|
|
[EVENT_MOUNT] = "mount",
|
|
|
|
[EVENT_THREAD_ERROR] = "threaderror",
|
|
|
|
[EVENT_CONTROLLER_ADDED] = "controlleradded",
|
|
|
|
[EVENT_CONTROLLER_REMOVED] = "controllerremoved",
|
|
|
|
[EVENT_CONTROLLER_PRESSED] = "controllerpressed",
|
|
|
|
[EVENT_CONTROLLER_RELEASED] = "controllerreleased",
|
|
|
|
};
|
2017-03-11 11:08:07 +00:00
|
|
|
|
2018-04-02 05:43:54 +00:00
|
|
|
static _Thread_local int pollRef;
|
2017-01-18 06:11:17 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-07-05 03:11:52 +00:00
|
|
|
lua_pushstring(L, EventTypes[event.type]);
|
2018-02-26 00:40:48 +00:00
|
|
|
|
2017-05-17 06:03:42 +00:00
|
|
|
switch (event.type) {
|
2018-02-26 00:40:48 +00:00
|
|
|
case EVENT_QUIT:
|
|
|
|
if (event.data.quit.restart) {
|
|
|
|
lua_pushstring(L, "restart");
|
|
|
|
} else {
|
2018-01-26 02:32:16 +00:00
|
|
|
lua_pushnumber(L, event.data.quit.exitCode);
|
2018-02-26 00:40:48 +00:00
|
|
|
}
|
2016-11-29 06:59:27 +00:00
|
|
|
return 2;
|
|
|
|
|
2018-02-26 00:40:48 +00:00
|
|
|
case EVENT_FOCUS:
|
|
|
|
case EVENT_MOUNT:
|
2018-07-26 20:40:15 +00:00
|
|
|
lua_pushboolean(L, event.data.boolean.value);
|
2018-02-26 00:40:48 +00:00
|
|
|
return 2;
|
|
|
|
|
2018-04-15 04:36:24 +00:00
|
|
|
#ifndef EMSCRIPTEN
|
2018-02-26 00:40:48 +00:00
|
|
|
case EVENT_THREAD_ERROR:
|
2018-07-26 20:40:15 +00:00
|
|
|
luax_pushobject(L, event.data.thread.thread);
|
|
|
|
lua_pushstring(L, event.data.thread.error);
|
|
|
|
free((void*) event.data.thread.error);
|
2018-02-17 17:49:24 +00:00
|
|
|
return 3;
|
2018-04-15 04:36:24 +00:00
|
|
|
#endif
|
2018-02-17 17:49:24 +00:00
|
|
|
|
2018-02-26 00:40:48 +00:00
|
|
|
case EVENT_CONTROLLER_ADDED:
|
|
|
|
case EVENT_CONTROLLER_REMOVED:
|
2018-07-26 20:40:15 +00:00
|
|
|
luax_pushobject(L, event.data.controller.controller);
|
|
|
|
lovrRelease(event.data.controller.controller);
|
2016-12-01 07:03:58 +00:00
|
|
|
return 2;
|
|
|
|
|
2018-02-26 00:40:48 +00:00
|
|
|
case EVENT_CONTROLLER_PRESSED:
|
|
|
|
case EVENT_CONTROLLER_RELEASED:
|
2018-07-26 20:40:15 +00:00
|
|
|
luax_pushobject(L, event.data.controller.controller);
|
|
|
|
lua_pushstring(L, ControllerButtons[event.data.controller.button]);
|
2017-03-12 02:46:07 +00:00
|
|
|
return 3;
|
|
|
|
|
2016-11-29 06:59:27 +00:00
|
|
|
default:
|
2018-02-26 00:40:48 +00:00
|
|
|
return 1;
|
2016-11-29 06:59:27 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
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) {
|
2018-07-05 03:11:52 +00:00
|
|
|
EventType type = luaL_checkoption(L, 1, NULL, EventTypes);
|
2016-11-29 06:59:27 +00:00
|
|
|
EventData data;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case EVENT_QUIT:
|
2018-02-26 00:40:48 +00:00
|
|
|
if (lua_type(L, 2) == LUA_TSTRING) {
|
|
|
|
data.quit.restart = lua_toboolean(L, 2);
|
|
|
|
} else {
|
|
|
|
data.quit.exitCode = luaL_optint(L, 2, 0);
|
|
|
|
}
|
2016-11-29 06:59:27 +00:00
|
|
|
break;
|
2016-12-01 07:03:58 +00:00
|
|
|
|
2017-03-12 03:01:01 +00:00
|
|
|
case EVENT_FOCUS:
|
2018-02-26 00:40:48 +00:00
|
|
|
case EVENT_MOUNT:
|
2018-07-26 20:40:15 +00:00
|
|
|
data.boolean.value = lua_toboolean(L, 2);
|
2017-03-12 03:01:01 +00:00
|
|
|
break;
|
|
|
|
|
2018-04-26 05:12:49 +00:00
|
|
|
#ifdef EMSCRIPTEN
|
|
|
|
case EVENT_THREAD_ERROR:
|
|
|
|
break;
|
|
|
|
#else
|
2018-02-17 17:49:24 +00:00
|
|
|
case EVENT_THREAD_ERROR:
|
2018-07-26 20:40:15 +00:00
|
|
|
data.thread.thread = luax_checktype(L, 2, Thread);
|
|
|
|
data.thread.error = luaL_checkstring(L, 3);
|
2018-02-17 17:49:24 +00:00
|
|
|
break;
|
2018-04-15 04:36:24 +00:00
|
|
|
#endif
|
2018-02-17 17:49:24 +00:00
|
|
|
|
2016-12-01 07:03:58 +00:00
|
|
|
case EVENT_CONTROLLER_ADDED:
|
|
|
|
case EVENT_CONTROLLER_REMOVED:
|
2018-07-26 20:40:15 +00:00
|
|
|
data.controller.controller = luax_checktype(L, 2, Controller);
|
2016-12-01 07:03:58 +00:00
|
|
|
break;
|
2017-03-12 02:46:07 +00:00
|
|
|
|
|
|
|
case EVENT_CONTROLLER_PRESSED:
|
|
|
|
case EVENT_CONTROLLER_RELEASED:
|
2018-07-26 20:40:15 +00:00
|
|
|
data.controller.controller = luax_checktype(L, 2, Controller);
|
|
|
|
data.controller.button = luaL_checkoption(L, 3, NULL, ControllerButtons);
|
2017-03-12 02:46:07 +00:00
|
|
|
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);
|
2018-01-27 03:01:20 +00:00
|
|
|
return 0;
|
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 }
|
|
|
|
};
|