Add restart cookie; lovr.event.restart; arg.restart;

This commit is contained in:
bjorn 2020-02-26 14:40:40 -08:00
parent 69dd0d4674
commit 90b605f012
5 changed files with 36 additions and 8 deletions

View File

@ -22,6 +22,7 @@ void luax_checkvariant(lua_State* L, int index, Variant* variant) {
int type = lua_type(L, index);
switch (type) {
case LUA_TNIL:
case LUA_TNONE:
variant->type = TYPE_NIL;
break;
@ -99,10 +100,12 @@ static int nextEvent(lua_State* L) {
case EVENT_QUIT:
if (event.data.quit.restart) {
lua_pushliteral(L, "restart");
luax_pushvariant(L, &event.data.quit.cookie);
return 3;
} else {
lua_pushnumber(L, event.data.quit.exitCode);
return 2;
}
return 2;
case EVENT_FOCUS:
lua_pushboolean(L, event.data.boolean.value);
@ -173,9 +176,10 @@ static int l_lovrEventQuit(lua_State* L) {
EventData data;
int argType = lua_type(L, 1);
if (argType == LUA_TSTRING && 0 == strcmp("restart", lua_tostring(L, 1))) {
if (argType == LUA_TSTRING && !strcmp("restart", lua_tostring(L, 1))) {
data.quit.restart = true;
data.quit.exitCode = 0;
luax_checkvariant(L, 2, &data.quit.cookie);
} else if (argType == LUA_TNUMBER || lua_isnoneornil(L, 1)) {
data.quit.restart = false;
data.quit.exitCode = luaL_optint(L, 1, 0);
@ -189,12 +193,24 @@ static int l_lovrEventQuit(lua_State* L) {
return 0;
}
static int l_lovrEventRestart(lua_State* L) {
EventData data;
data.quit.exitCode = 0;
data.quit.restart = true;
luax_checkvariant(L, 1, &data.quit.cookie);
EventType type = EVENT_QUIT;
Event event = { .type = type, .data = data };
lovrEventPush(event);
return 0;
}
static const luaL_Reg lovrEvent[] = {
{ "clear", l_lovrEventClear },
{ "poll", l_lovrEventPoll },
{ "pump", l_lovrEventPump },
{ "push", l_lovrEventPush },
{ "quit", l_lovrEventQuit },
{ "restart", l_lovrEventRestart },
{ NULL, NULL }
};

View File

@ -1,5 +1,6 @@
#include "resources/boot.lua.h"
#include "api/api.h"
#include "event/event.h"
#include "core/os.h"
#include "core/util.h"
#include <stdbool.h>
@ -62,6 +63,8 @@ int main(int argc, char** argv) {
int status;
bool restart;
Variant cookie;
cookie.type = TYPE_NIL;
do {
lovrPlatformSetTime(0.);
@ -88,6 +91,9 @@ int main(int argc, char** argv) {
lua_pushliteral(L, "lovr");
lua_setfield(L, -2, "exe");
luax_pushvariant(L, &cookie);
lua_setfield(L, -2, "restart");
typedef enum { // What flag is being searched for?
ARGFLAG_NONE, // Not processing a flag
ARGFLAG_ROOT
@ -157,8 +163,13 @@ int main(int argc, char** argv) {
lovrPlatformSleep(0.);
}
restart = lua_type(T, -1) == LUA_TSTRING && !strcmp(lua_tostring(T, -1), "restart");
status = lua_tonumber(T, -1);
restart = lua_type(T, 1) == LUA_TSTRING && !strcmp(lua_tostring(T, 1), "restart");
status = lua_tonumber(T, 1);
luax_checkvariant(T, 2, &cookie);
if (cookie.type == TYPE_OBJECT) {
cookie.type = TYPE_NIL;
memset(&cookie.value, 0, sizeof(cookie.value));
}
lua_close(L);
} while (restart);

View File

@ -43,6 +43,7 @@ typedef struct Variant {
typedef struct {
bool restart;
int exitCode;
Variant cookie;
} QuitEvent;
typedef struct {

View File

@ -162,7 +162,7 @@ static void gammaCorrect(Color* color) {
}
static void onCloseWindow(void) {
lovrEventPush((Event) { .type = EVENT_QUIT, .data.quit = { false, 0 } });
lovrEventPush((Event) { .type = EVENT_QUIT, .data.quit = { .exitCode = 0 } });
}
static void onResizeWindow(int width, int height) {

View File

@ -150,7 +150,7 @@ function lovr.run()
lovr.event.pump()
for name, a, b, c, d in lovr.event.poll() do
if name == 'quit' and (not lovr.quit or not lovr.quit()) then
return a or 0
return a or 0, b
end
if lovr.handlers[name] then lovr.handlers[name](a, b, c, d) end
end
@ -284,8 +284,8 @@ return function()
return 1
end
local ok, result = xpcall(continuation, onerror)
if result and ok then return result -- Result is value returned by function. Return it.
local ok, result, extra = xpcall(continuation, onerror)
if result and ok then return result, extra -- Result is value returned by function. Return it.
elseif not ok then continuation = result end -- Result is value returned by error handler. Make it the new error handler.
local externerror = coroutine.yield() -- Return control to C code