Skip to content

Commit 26c92c9

Browse files
committed
screensaver: Adds a screensaver.
1 parent c936b6d commit 26c92c9

File tree

8 files changed

+107
-1
lines changed

8 files changed

+107
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ add_executable(${PROJECT_NAME}
3131
Includes/menu.cpp
3232
Includes/networkManager.cpp
3333
Includes/renderer.cpp
34+
Includes/screensaver.cpp
3435
Includes/settingsMenu.cpp
3536
Includes/sntpClient.cpp
3637
Includes/subAppRouter.cpp

Includes/config.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,29 @@ void from_json(nlohmann::json const& j, homescreenConfig& o) {
211211
}
212212
}
213213

214+
void to_json(nlohmann::json& j, screensaverConfig const& o) {
215+
j = nlohmann::json{ { "enabled", o.getEnabled() },
216+
{ "timeout_millis", o.getTimeoutMillis() } };
217+
}
218+
219+
void from_json(nlohmann::json const& j, screensaverConfig& o) {
220+
if (j.contains("enabled") && j["enabled"].is_boolean()) {
221+
o.setEnabled(j["enabled"]);
222+
}
223+
if (j.contains("timeout_millis")) {
224+
o.setTimeoutMillis(j["timeout_millis"]);
225+
}
226+
}
227+
214228
void to_json(nlohmann::json& j, Settings const& o) {
215229
j = nlohmann::json{ { "ftp", nlohmann::json(o.ftp) },
216230
{ "mount", nlohmann::json(o.mount) },
217231
#ifdef NXDK
218232
{ "network", nlohmann::json(o.net) },
219233
#endif
220234
{ "logging", nlohmann::json(o.logging) },
221-
{ "homescreenConfig", nlohmann::json(o.homescreen) } };
235+
{ "homescreen", nlohmann::json(o.homescreen) },
236+
{ "screensaver", nlohmann::json(o.screensaver) } };
222237
}
223238

224239
void from_json(nlohmann::json const& j, Settings& o) {
@@ -242,6 +257,9 @@ void from_json(nlohmann::json const& j, Settings& o) {
242257
if (j.contains("homescreen")) {
243258
o.homescreen = j["homescreen"].get<homescreenConfig>();
244259
}
260+
if (j.contains("screensaver")) {
261+
o.screensaver = j["screensaver"].get<screensaverConfig>();
262+
}
245263
}
246264

247265
Config::Config() {

Includes/config.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,22 @@ class homescreenConfig {
131131
void to_json(nlohmann::json& j, homescreenConfig const& o);
132132
void from_json(nlohmann::json const& j, homescreenConfig& o);
133133

134+
class screensaverConfig {
135+
bool enabled{ true };
136+
unsigned int timeoutMillis{ 5 * 60 * 1000 };
137+
138+
public:
139+
screensaverConfig() = default;
140+
141+
bool getEnabled() const { return enabled; }
142+
unsigned int getTimeoutMillis() const { return timeoutMillis; }
143+
144+
void setEnabled(bool val) { enabled = val; }
145+
void setTimeoutMillis(unsigned int val) { timeoutMillis = val; }
146+
};
147+
void to_json(nlohmann::json& j, screensaverConfig const& o);
148+
void from_json(nlohmann::json const& j, screensaverConfig& o);
149+
134150
class Settings {
135151
public:
136152
Settings() = default;
@@ -142,6 +158,7 @@ class Settings {
142158
mountConfig mount;
143159
loggingConfig logging;
144160
homescreenConfig homescreen;
161+
screensaverConfig screensaver;
145162
};
146163
void to_json(nlohmann::json& j, Settings const& o);
147164
void from_json(nlohmann::json const& j, Settings& o);

Includes/screensaver.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "screensaver.h"
2+
#include "timing.h"
3+
4+
Screensaver::Screensaver(Renderer& r, bool enabled, unsigned int timeoutMillis) :
5+
renderer(r), timeoutMillis(timeoutMillis), isEnabled(enabled) {
6+
lastInputTime = std::chrono::steady_clock::now();
7+
}
8+
9+
void Screensaver::render() {
10+
if (!isEnabled) {
11+
return;
12+
}
13+
auto millisElapsed = millisSince(lastInputTime);
14+
if (millisElapsed >= timeoutMillis) {
15+
isActive = true;
16+
}
17+
18+
if (!isActive) {
19+
return;
20+
}
21+
renderer.setDrawColor(0, 0, 0, 200);
22+
23+
SDL_FRect screen = { 0, 0, static_cast<float>(renderer.getWidth()),
24+
static_cast<float>(renderer.getHeight()) };
25+
renderer.fillRectangle(screen);
26+
}
27+
28+
void Screensaver::notifyInput() {
29+
lastInputTime = std::chrono::steady_clock::now();
30+
isActive = false;
31+
}

Includes/screensaver.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef NEVOLUTIONX_SCREENSAVER_H
2+
#define NEVOLUTIONX_SCREENSAVER_H
3+
4+
#include <chrono>
5+
#include "renderer.h"
6+
7+
class Screensaver {
8+
public:
9+
Screensaver(Renderer& r, bool enable, unsigned int timeoutMillis);
10+
11+
void render();
12+
13+
// Checks to see if the screensaver should consume an input event and stop rendering.
14+
void notifyInput();
15+
16+
private:
17+
Renderer& renderer;
18+
unsigned int timeoutMillis;
19+
bool isEnabled{ true };
20+
bool isActive{ false };
21+
std::chrono::steady_clock::time_point lastInputTime;
22+
};
23+
24+
#endif // NEVOLUTIONX_SCREENSAVER_H

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ SRCS += \
1717
$(INCDIR)/networkManager.cpp \
1818
$(INCDIR)/networking.cpp \
1919
$(INCDIR)/renderer.cpp \
20+
$(INCDIR)/screensaver.cpp \
2021
$(INCDIR)/settingsMenu.cpp \
2122
$(INCDIR)/sntpClient.cpp \
2223
$(INCDIR)/subAppRouter.cpp \

main.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "menu.hpp"
1010
#include "networkManager.h"
1111
#include "renderer.h"
12+
#include "screensaver.h"
1213
#include "sntpClient.h"
1314
#include "subAppRouter.h"
1415
#include "subsystems.h"
@@ -119,6 +120,10 @@ int main(void) {
119120
ftpServer* ftpServerInstance = nullptr;
120121
std::shared_ptr<sntpClient> sntpClientInstance;
121122

123+
auto const& screensaverSettings = config.settings.screensaver;
124+
Screensaver screensaver(r, screensaverSettings.getEnabled(),
125+
screensaverSettings.getTimeoutMillis());
126+
122127
auto lastFrameStart = std::chrono::steady_clock::now();
123128
while (running) {
124129
auto frameStart = std::chrono::steady_clock::now();
@@ -161,6 +166,7 @@ int main(void) {
161166
}
162167

163168
InfoLog::renderOverlay(r, f);
169+
screensaver.render();
164170

165171
r.flip();
166172

@@ -174,10 +180,13 @@ int main(void) {
174180
SDL_GameControllerClose(controllers[event.cdevice.which]);
175181
controllers.erase(event.cdevice.which);
176182
} else if (event.type == SDL_CONTROLLERBUTTONDOWN) {
183+
screensaver.notifyInput();
177184
router.handleButtonDown(event.cbutton);
178185
} else if (event.type == SDL_CONTROLLERBUTTONUP) {
186+
screensaver.notifyInput();
179187
router.handleButtonUp(event.cbutton);
180188
} else if (event.type == SDL_CONTROLLERAXISMOTION) {
189+
screensaver.notifyInput();
181190
router.handleAxisMotion(event.caxis);
182191
}
183192
}

sampleconfig.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
{
3939
"show_fps": false,
4040
"show_ip": true
41+
},
42+
"screensaver":
43+
{
44+
"enabled": true,
45+
"timeout_millis": 300000
4146
}
4247
},
4348
"menu":

0 commit comments

Comments
 (0)