From 6bb5586a7db9e12aa0d61dd51c5f779e9f451617 Mon Sep 17 00:00:00 2001 From: Erik Abair Date: Tue, 5 Oct 2021 14:32:11 -0700 Subject: [PATCH] screensaver: Adds a screensaver. --- CMakeLists.txt | 1 + Includes/config.hpp | 17 +++++++++++++++++ Includes/screensaver.hpp | 24 ++++++++++++++++++++++++ Makefile | 1 + Sources/config.cpp | 20 +++++++++++++++++++- Sources/screensaver.cpp | 31 +++++++++++++++++++++++++++++++ main.cpp | 9 +++++++++ sampleconfig.json | 5 +++++ 8 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 Includes/screensaver.hpp create mode 100644 Sources/screensaver.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a8da0e..24e191d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,6 +31,7 @@ add_executable(${PROJECT_NAME} Sources/menu.cpp Sources/networkManager.cpp Sources/renderer.cpp + Sources/screensaver.cpp Sources/settingsMenu.cpp Sources/sntpClient.cpp Sources/subAppRouter.cpp diff --git a/Includes/config.hpp b/Includes/config.hpp index c1b4cab..fe163d9 100644 --- a/Includes/config.hpp +++ b/Includes/config.hpp @@ -131,6 +131,22 @@ class homescreenConfig { void to_json(nlohmann::json& j, homescreenConfig const& o); void from_json(nlohmann::json const& j, homescreenConfig& o); +class screensaverConfig { + bool enabled{ true }; + unsigned int timeoutMillis{ 5 * 60 * 1000 }; + +public: + screensaverConfig() = default; + + bool getEnabled() const { return enabled; } + unsigned int getTimeoutMillis() const { return timeoutMillis; } + + void setEnabled(bool val) { enabled = val; } + void setTimeoutMillis(unsigned int val) { timeoutMillis = val; } +}; +void to_json(nlohmann::json& j, screensaverConfig const& o); +void from_json(nlohmann::json const& j, screensaverConfig& o); + class Settings { public: Settings() = default; @@ -142,6 +158,7 @@ class Settings { mountConfig mount; loggingConfig logging; homescreenConfig homescreen; + screensaverConfig screensaver; }; void to_json(nlohmann::json& j, Settings const& o); void from_json(nlohmann::json const& j, Settings& o); diff --git a/Includes/screensaver.hpp b/Includes/screensaver.hpp new file mode 100644 index 0000000..8b1fd21 --- /dev/null +++ b/Includes/screensaver.hpp @@ -0,0 +1,24 @@ +#ifndef NEVOLUTIONX_SCREENSAVER_H +#define NEVOLUTIONX_SCREENSAVER_H + +#include +#include "renderer.hpp" + +class Screensaver { +public: + Screensaver(Renderer& r, bool enable, unsigned int timeoutMillis); + + void render(); + + // Checks to see if the screensaver should consume an input event and stop rendering. + void notifyInput(); + +private: + Renderer& renderer; + unsigned int timeoutMillis; + bool isEnabled{ true }; + bool isActive{ false }; + std::chrono::steady_clock::time_point lastInputTime; +}; + +#endif // NEVOLUTIONX_SCREENSAVER_H diff --git a/Makefile b/Makefile index fa6c22f..34a937b 100644 --- a/Makefile +++ b/Makefile @@ -18,6 +18,7 @@ SRCS += \ $(SRCDIR)/networkManager.cpp \ $(SRCDIR)/networking.cpp \ $(SRCDIR)/renderer.cpp \ + $(SRCDIR)/screensaver.cpp \ $(SRCDIR)/settingsMenu.cpp \ $(SRCDIR)/sntpClient.cpp \ $(SRCDIR)/subAppRouter.cpp \ diff --git a/Sources/config.cpp b/Sources/config.cpp index b399f86..aea5092 100644 --- a/Sources/config.cpp +++ b/Sources/config.cpp @@ -211,6 +211,20 @@ void from_json(nlohmann::json const& j, homescreenConfig& o) { } } +void to_json(nlohmann::json& j, screensaverConfig const& o) { + j = nlohmann::json{ { "enabled", o.getEnabled() }, + { "timeout_millis", o.getTimeoutMillis() } }; +} + +void from_json(nlohmann::json const& j, screensaverConfig& o) { + if (j.contains("enabled") && j["enabled"].is_boolean()) { + o.setEnabled(j["enabled"]); + } + if (j.contains("timeout_millis")) { + o.setTimeoutMillis(j["timeout_millis"]); + } +} + void to_json(nlohmann::json& j, Settings const& o) { j = nlohmann::json{ { "ftp", nlohmann::json(o.ftp) }, { "mount", nlohmann::json(o.mount) }, @@ -218,7 +232,8 @@ void to_json(nlohmann::json& j, Settings const& o) { { "network", nlohmann::json(o.net) }, #endif { "logging", nlohmann::json(o.logging) }, - { "homescreenConfig", nlohmann::json(o.homescreen) } }; + { "homescreen", nlohmann::json(o.homescreen) }, + { "screensaver", nlohmann::json(o.screensaver) } }; } void from_json(nlohmann::json const& j, Settings& o) { @@ -242,6 +257,9 @@ void from_json(nlohmann::json const& j, Settings& o) { if (j.contains("homescreen")) { o.homescreen = j["homescreen"].get(); } + if (j.contains("screensaver")) { + o.screensaver = j["screensaver"].get(); + } } Config::Config() { diff --git a/Sources/screensaver.cpp b/Sources/screensaver.cpp new file mode 100644 index 0000000..8a0b4de --- /dev/null +++ b/Sources/screensaver.cpp @@ -0,0 +1,31 @@ +#include "screensaver.hpp" +#include "timing.hpp" + +Screensaver::Screensaver(Renderer& r, bool enabled, unsigned int timeoutMillis) : + renderer(r), timeoutMillis(timeoutMillis), isEnabled(enabled) { + lastInputTime = std::chrono::steady_clock::now(); +} + +void Screensaver::render() { + if (!isEnabled) { + return; + } + auto millisElapsed = millisSince(lastInputTime); + if (millisElapsed >= timeoutMillis) { + isActive = true; + } + + if (!isActive) { + return; + } + renderer.setDrawColor(0, 0, 0, 200); + + SDL_FRect screen = { 0, 0, static_cast(renderer.getWidth()), + static_cast(renderer.getHeight()) }; + renderer.fillRectangle(screen); +} + +void Screensaver::notifyInput() { + lastInputTime = std::chrono::steady_clock::now(); + isActive = false; +} diff --git a/main.cpp b/main.cpp index 945bd42..f4eb3cf 100644 --- a/main.cpp +++ b/main.cpp @@ -9,6 +9,7 @@ #include "menu.hpp" #include "networkManager.hpp" #include "renderer.hpp" +#include "screensaver.hpp" #include "sntpClient.hpp" #include "subAppRouter.hpp" #include "subsystems.hpp" @@ -119,6 +120,10 @@ int main(void) { ftpServer* ftpServerInstance = nullptr; std::shared_ptr sntpClientInstance; + auto const& screensaverSettings = config.settings.screensaver; + Screensaver screensaver(r, screensaverSettings.getEnabled(), + screensaverSettings.getTimeoutMillis()); + auto lastFrameStart = std::chrono::steady_clock::now(); while (running) { auto frameStart = std::chrono::steady_clock::now(); @@ -161,6 +166,7 @@ int main(void) { } InfoLog::renderOverlay(r, f); + screensaver.render(); r.flip(); @@ -174,10 +180,13 @@ int main(void) { SDL_GameControllerClose(controllers[event.cdevice.which]); controllers.erase(event.cdevice.which); } else if (event.type == SDL_CONTROLLERBUTTONDOWN) { + screensaver.notifyInput(); router.handleButtonDown(event.cbutton); } else if (event.type == SDL_CONTROLLERBUTTONUP) { + screensaver.notifyInput(); router.handleButtonUp(event.cbutton); } else if (event.type == SDL_CONTROLLERAXISMOTION) { + screensaver.notifyInput(); router.handleAxisMotion(event.caxis); } } diff --git a/sampleconfig.json b/sampleconfig.json index bbcb15a..8b42fb1 100644 --- a/sampleconfig.json +++ b/sampleconfig.json @@ -38,6 +38,11 @@ { "show_fps": false, "show_ip": true + }, + "screensaver": + { + "enabled": true, + "timeout_millis": 300000 } }, "menu":