From c1689e41fbe8538a7b50da9c7cbf1b1f98da23d9 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Thu, 14 Aug 2014 09:47:05 +0200 Subject: [PATCH] Dismantled Singleton pattern of PowerSaver The instance-on-demand didn't really work, since we needed explicit control over this object's destruction to ensure the timer is stopped when launching an application. And trying to combine getInstance() with explicit external delete was just ugly. --- src/gmenu2x.cpp | 16 ++++------------ src/gmenu2x.h | 2 ++ src/inputmanager.cpp | 7 ++++--- src/inputmanager.h | 4 +++- src/powersaver.cpp | 13 ++----------- src/powersaver.h | 4 +--- 6 files changed, 16 insertions(+), 30 deletions(-) diff --git a/src/gmenu2x.cpp b/src/gmenu2x.cpp index c31c9a2..2363f2e 100644 --- a/src/gmenu2x.cpp +++ b/src/gmenu2x.cpp @@ -215,6 +215,7 @@ void GMenu2X::initCPULimits() { #endif GMenu2X::GMenu2X() + : input(powerSaver) { usbnet = samba = inet = web = false; useSelectionPng = false; @@ -283,18 +284,14 @@ GMenu2X::GMenu2X() exit(EXIT_FAILURE); } - if (confInt["backlightTimeout"] > 0) - PowerSaver::getInstance()->setScreenTimeout( confInt["backlightTimeout"] ); + powerSaver.setScreenTimeout(confInt["backlightTimeout"]); + #ifdef ENABLE_CPUFREQ setClock(confInt["menuClock"]); #endif } GMenu2X::~GMenu2X() { - if (PowerSaver::isRunning()) { - delete PowerSaver::getInstance(); - } - fflush(NULL); sc.clear(); @@ -704,12 +701,7 @@ void GMenu2X::showSettings() { if (curMenuClock != confInt["menuClock"]) setClock(confInt["menuClock"]); #endif - if (confInt["backlightTimeout"] == 0) { - if (PowerSaver::isRunning()) - delete PowerSaver::getInstance(); - } else { - PowerSaver::getInstance()->setScreenTimeout( confInt["backlightTimeout"] ); - } + powerSaver.setScreenTimeout(confInt["backlightTimeout"]); input.repeatRateChanged(); diff --git a/src/gmenu2x.h b/src/gmenu2x.h index a90459d..ce6b25b 100644 --- a/src/gmenu2x.h +++ b/src/gmenu2x.h @@ -26,6 +26,7 @@ #include "translator.h" #include "touchscreen.h" #include "inputmanager.h" +#include "powersaver.h" #include "surface.h" #include "utilities.h" @@ -144,6 +145,7 @@ public: return std::make_pair(top, resY - top - bottom); } + PowerSaver powerSaver; InputManager input; //Configuration hashes diff --git a/src/inputmanager.cpp b/src/inputmanager.cpp index 62d1488..14cd25f 100644 --- a/src/inputmanager.cpp +++ b/src/inputmanager.cpp @@ -57,7 +57,8 @@ bool InputManager::init(GMenu2X *gmenu2x, Menu *menu) { return true; } -InputManager::InputManager() +InputManager::InputManager(PowerSaver& powerSaver) + : powerSaver(powerSaver) { #ifndef SDL_JOYSTICK_DISABLED int i; @@ -301,8 +302,8 @@ bool InputManager::getButton(Button *button, bool wait) { if (i == BUTTON_TYPE_SIZE) return false; - if (wait && PowerSaver::isRunning()) { - PowerSaver::getInstance()->resetScreenTimer(); + if (wait) { + powerSaver.resetScreenTimer(); } return true; diff --git a/src/inputmanager.h b/src/inputmanager.h index 65e525b..c11d691 100644 --- a/src/inputmanager.h +++ b/src/inputmanager.h @@ -29,6 +29,7 @@ class GMenu2X; class Menu; +class PowerSaver; class InputManager; enum EventCode { @@ -61,7 +62,7 @@ public: }; #define BUTTON_TYPE_SIZE 10 - InputManager(); + InputManager(PowerSaver& powerSaver); ~InputManager(); bool init(GMenu2X *gmenu2x, Menu *menu); @@ -81,6 +82,7 @@ private: GMenu2X *gmenu2x; Menu *menu; + PowerSaver& powerSaver; ButtonMapEntry buttonMap[BUTTON_TYPE_SIZE]; #ifndef SDL_JOYSTICK_DISABLED diff --git a/src/powersaver.cpp b/src/powersaver.cpp index 02d6464..ed503ef 100644 --- a/src/powersaver.cpp +++ b/src/powersaver.cpp @@ -25,23 +25,14 @@ Uint32 screenTimerCallback(Uint32 timeout, void *d) { return 0; } -PowerSaver *PowerSaver::getInstance() { - if (!instance) { - instance = new PowerSaver(); - } - return instance; -} - -bool PowerSaver::isRunning() { - return !!instance; -} - PowerSaver::PowerSaver() : screenState(false) , screenTimeout(0) , screenTimer(nullptr) { enableScreen(); + assert(!instance); + instance = this; } PowerSaver::~PowerSaver() { diff --git a/src/powersaver.h b/src/powersaver.h index 9b10150..797d9bc 100644 --- a/src/powersaver.h +++ b/src/powersaver.h @@ -5,14 +5,12 @@ class PowerSaver { public: - static PowerSaver *getInstance(); - static bool isRunning(); + PowerSaver(); ~PowerSaver(); void resetScreenTimer(); void setScreenTimeout(unsigned int seconds); private: - PowerSaver(); void addScreenTimer(); void removeScreenTimer(); void setScreenBlanking(bool state);