From 0116dc2e0785edbe372b0fd1a27864e996405659 Mon Sep 17 00:00:00 2001 From: Romain Goyet Date: Fri, 4 Sep 2020 21:17:36 -0400 Subject: [PATCH] [ion/simulator] Add a runtime "--headless" option We probably can ditch the static headless version --- ion/src/simulator/shared/keyboard.cpp | 7 ++++++- ion/src/simulator/shared/main.cpp | 22 +++++++++++++++------- ion/src/simulator/shared/window.cpp | 16 ++++++++++++---- ion/src/simulator/shared/window.h | 2 ++ 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/ion/src/simulator/shared/keyboard.cpp b/ion/src/simulator/shared/keyboard.cpp index f99fa1961..9a3849f4e 100644 --- a/ion/src/simulator/shared/keyboard.cpp +++ b/ion/src/simulator/shared/keyboard.cpp @@ -39,6 +39,11 @@ namespace Ion { namespace Keyboard { State scan() { + State state = sKeyboardState; + + if (Simulator::Window::isHeadless()) { + return state; + } // We need to tell SDL to get new state from the host OS SDL_PumpEvents(); @@ -48,7 +53,6 @@ State scan() { // Grab this opportunity to refresh the display if needed Simulator::Window::refresh(); - State state = sKeyboardState; #if !EPSILON_SDL_SCREEN_ONLY // Register a key for the mouse, if any Key k = Simulator::Layout::getHighlightedKey(); @@ -65,6 +69,7 @@ State scan() { state.setKey(pair.key()); } } + return state; } diff --git a/ion/src/simulator/shared/main.cpp b/ion/src/simulator/shared/main.cpp index a6c56bf0d..2bf0bd553 100644 --- a/ion/src/simulator/shared/main.cpp +++ b/ion/src/simulator/shared/main.cpp @@ -1,6 +1,7 @@ #include "haptics.h" #include "journal.h" #include "platform.h" +#include "random.h" #include "telemetry.h" #include "window.h" #include @@ -85,19 +86,26 @@ int main(int argc, char * argv[]) { } #endif + bool headless = args.popFlag("--headless"); + using namespace Ion::Simulator; - Journal::init(); + Random::init(); + if (!headless) { + Journal::init(); #if EPSILON_TELEMETRY - Telemetry::init(); + Telemetry::init(); #endif - Window::init(); - Haptics::init(); + Window::init(); + Haptics::init(); + } ion_main(args.argc(), args.argv()); - Haptics::shutdown(); - Window::quit(); + if (!headless) { + Haptics::shutdown(); + Window::quit(); #if EPSILON_TELEMETRY - Telemetry::shutdown(); + Telemetry::shutdown(); #endif + } return 0; } diff --git a/ion/src/simulator/shared/window.cpp b/ion/src/simulator/shared/window.cpp index ccfc8854b..7b25e05ab 100644 --- a/ion/src/simulator/shared/window.cpp +++ b/ion/src/simulator/shared/window.cpp @@ -2,7 +2,6 @@ #include "display.h" #include "layout.h" #include "platform.h" -#include "random.h" #include #include @@ -19,14 +18,16 @@ static bool sNeedsRefresh = false; static SDL_Rect sScreenRect; #endif +bool isHeadless() { + return sWindow == nullptr; +} + void init() { if (SDL_Init(SDL_INIT_VIDEO) != 0) { SDL_Log("Could not init video"); return; } - Random::init(); - sWindow = SDL_CreateWindow( "Epsilon", SDL_WINDOWPOS_CENTERED, @@ -67,6 +68,9 @@ void init() { } void relayout() { + if (isHeadless()) { + return; + } int windowWidth = 0; int windowHeight = 0; SDL_GetWindowSize(sWindow, &windowWidth, &windowHeight); @@ -89,7 +93,7 @@ void setNeedsRefresh() { } void refresh() { - if (!sNeedsRefresh) { + if (!sNeedsRefresh || isHeadless()) { return; } sNeedsRefresh = false; @@ -112,11 +116,15 @@ void refresh() { } void quit() { + if (isHeadless()) { + return; + } #if !EPSILON_SDL_SCREEN_ONLY Layout::quit(); #endif Display::quit(); SDL_DestroyWindow(sWindow); + sWindow = nullptr; SDL_Quit(); } diff --git a/ion/src/simulator/shared/window.h b/ion/src/simulator/shared/window.h index 224ad96a4..f2e221b85 100644 --- a/ion/src/simulator/shared/window.h +++ b/ion/src/simulator/shared/window.h @@ -8,6 +8,8 @@ namespace Window { void init(); void quit(); +bool isHeadless(); + void setNeedsRefresh(); void refresh(); void relayout();