diff --git a/src/api/event.c b/src/api/event.c index 83d17009..391af2e2 100644 --- a/src/api/event.c +++ b/src/api/event.c @@ -19,6 +19,12 @@ static int nextEvent(lua_State* L) { return 2; } + case EVENT_FOCUS: { + lua_pushstring(L, "focus"); + lua_pushboolean(L, event->data.focus.isFocused); + return 2; + }; + case EVENT_CONTROLLER_ADDED: { lua_pushstring(L, "controlleradded"); luax_pushtype(L, Controller, event->data.controlleradded.controller); @@ -90,6 +96,10 @@ int l_lovrEventPush(lua_State* L) { data.quit.exitCode = luaL_optint(L, 2, 0); break; + case EVENT_FOCUS: + data.focus.isFocused = lua_toboolean(L, 2); + break; + case EVENT_CONTROLLER_ADDED: data.controlleradded.controller = luax_checktype(L, 2, Controller); break; diff --git a/src/event/event.h b/src/event/event.h index 0844f68c..d9bd914a 100644 --- a/src/event/event.h +++ b/src/event/event.h @@ -5,6 +5,7 @@ typedef enum { EVENT_QUIT, + EVENT_FOCUS, EVENT_CONTROLLER_ADDED, EVENT_CONTROLLER_REMOVED, EVENT_CONTROLLER_PRESSED, @@ -15,6 +16,10 @@ typedef struct { int exitCode; } QuitEvent; +typedef struct { + int isFocused; +} FocusEvent; + typedef struct { Controller* controller; } ControllerAddedEvent; @@ -35,6 +40,7 @@ typedef struct { typedef union { QuitEvent quit; + FocusEvent focus; ControllerAddedEvent controlleradded; ControllerRemovedEvent controllerremoved; ControllerPressedEvent controllerpressed; diff --git a/src/headset/openvr.c b/src/headset/openvr.c index 09e974e1..a9512f19 100644 --- a/src/headset/openvr.c +++ b/src/headset/openvr.c @@ -154,6 +154,14 @@ void lovrHeadsetPoll() { } break; } + + case EVREventType_VREvent_InputFocusCaptured: + case EVREventType_VREvent_InputFocusReleased: + int isFocused = vrEvent.eventType == EVREventType_VREvent_InputFocusReleased; + EventData data = { .focus = { isFocused } }; + Event event = { .type = EVENT_FOCUS, .data = data }; + lovrEventPush(event); + break; } } } diff --git a/src/lovr.c b/src/lovr.c index d6694ac7..ab56d00d 100644 --- a/src/lovr.c +++ b/src/lovr.c @@ -113,6 +113,9 @@ void lovrInit(lua_State* L, int argc, char** argv) { "lovr.handlers = setmetatable({ " " quit = function() end, " + " focus = function(f) " + " if lovr.focus then lovr.focus(f) end " + " end, " " controlleradded = function(c) " " if lovr.controlleradded then lovr.controlleradded(c) end " " end, "