From 43ea78c6bbc304ab4ea8002dc356a09ff323c2cf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jaakko=20Ker=C3=A4nen?= jaakko.keranen@iki.fi
Date: Sun, 13 Oct 2024 13:02:46 +0300
Subject: [PATCH 1/1] TUI: Always use idle sleep to reduce CPU usage
When nothing is happening (no input or animations), sleep
instead of polling events and running the refresh loop.
CMakeLists.txt | 5 +++++
src/app.c | 9 +++++----
2 files changed, 10 insertions(+), 4 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index a223c9c0..36c7e0ea 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -593,6 +593,11 @@ if (ENABLE_TUI)
target_link_libraries (tuiapp PUBLIC ${SEALCURSES_LDFLAGS})
endif ()
set_common_app_properties (tuiapp)
# SEALCurses does not have a waiting event getter, so ease the CPU load by sleeping
# when nothing is happening.
target_compile_definitions (tuiapp LAGRANGE_ENABLE_IDLE_SLEEP=1)
endif ()
diff --git a/src/app.c b/src/app.c
index 21b1f803..8174524a 100644
--- a/src/app.c
+++ b/src/app.c
@@ -88,9 +88,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
#if defined (iPlatformMsys)
#endif
-#if defined (iPlatformTerminal)
-# undef LAGRANGE_ENABLE_IDLE_SLEEP
-#endif
#if defined (LAGRANGE_ENABLE_X11_XLIB)
#endif
@@ -1463,6 +1460,9 @@ static void init_App_(iApp *d, int argc, char **argv) {
d->isIdling = iFalse;
d->lastEventTime = 0;
d->sleepTimer = SDL_AddTimer(1000, checkAsleep_App_, d);
+#if defined (iPlatformTerminal)
d->idleSleepDelayMs = 1000 / 60;
+#else
SDL_DisplayMode dispMode;
SDL_GetWindowDisplayMode(d->window->win, &dispMode);
if (dispMode.refresh_rate) {
@@ -1471,6 +1471,7 @@ static void init_App_(iApp *d, int argc, char **argv) {
else {
d->idleSleepDelayMs = 1000 / 60;
}
+#endif
d->idleSleepDelayMs *= 0.9f;
}
#endif
@@ -1927,7 +1928,7 @@ static iBool nextEvent_App_(iApp *d, enum iAppEventMode eventMode, SDL_Event *ev
/* SDL regression circa 2.0.18? SDL_PollEvent() doesn't always return
events posted immediately beforehand. Waiting with a very short timeout
seems to work better. */
-#if defined (iPlatformLinux) && SDL_VERSION_ATLEAST(2, 0, 18)
+#if !defined (iPlatformTerminal) && defined (iPlatformLinux) && SDL_VERSION_ATLEAST(2, 0, 18)
return SDL_WaitEventTimeout(event, 1);
#else
return SDL_PollEvent(event);
--
2.25.1
text/plain
This content has been proxied by September (3851b).