1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-26 04:58:27 +02:00

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.
This commit is contained in:
Maarten ter Huurne 2014-08-14 09:47:05 +02:00
parent 8d662c95f6
commit c1689e41fb
6 changed files with 16 additions and 30 deletions

View File

@ -215,6 +215,7 @@ void GMenu2X::initCPULimits() {
#endif #endif
GMenu2X::GMenu2X() GMenu2X::GMenu2X()
: input(powerSaver)
{ {
usbnet = samba = inet = web = false; usbnet = samba = inet = web = false;
useSelectionPng = false; useSelectionPng = false;
@ -283,18 +284,14 @@ GMenu2X::GMenu2X()
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (confInt["backlightTimeout"] > 0) powerSaver.setScreenTimeout(confInt["backlightTimeout"]);
PowerSaver::getInstance()->setScreenTimeout( confInt["backlightTimeout"] );
#ifdef ENABLE_CPUFREQ #ifdef ENABLE_CPUFREQ
setClock(confInt["menuClock"]); setClock(confInt["menuClock"]);
#endif #endif
} }
GMenu2X::~GMenu2X() { GMenu2X::~GMenu2X() {
if (PowerSaver::isRunning()) {
delete PowerSaver::getInstance();
}
fflush(NULL); fflush(NULL);
sc.clear(); sc.clear();
@ -704,12 +701,7 @@ void GMenu2X::showSettings() {
if (curMenuClock != confInt["menuClock"]) setClock(confInt["menuClock"]); if (curMenuClock != confInt["menuClock"]) setClock(confInt["menuClock"]);
#endif #endif
if (confInt["backlightTimeout"] == 0) { powerSaver.setScreenTimeout(confInt["backlightTimeout"]);
if (PowerSaver::isRunning())
delete PowerSaver::getInstance();
} else {
PowerSaver::getInstance()->setScreenTimeout( confInt["backlightTimeout"] );
}
input.repeatRateChanged(); input.repeatRateChanged();

View File

@ -26,6 +26,7 @@
#include "translator.h" #include "translator.h"
#include "touchscreen.h" #include "touchscreen.h"
#include "inputmanager.h" #include "inputmanager.h"
#include "powersaver.h"
#include "surface.h" #include "surface.h"
#include "utilities.h" #include "utilities.h"
@ -144,6 +145,7 @@ public:
return std::make_pair(top, resY - top - bottom); return std::make_pair(top, resY - top - bottom);
} }
PowerSaver powerSaver;
InputManager input; InputManager input;
//Configuration hashes //Configuration hashes

View File

@ -57,7 +57,8 @@ bool InputManager::init(GMenu2X *gmenu2x, Menu *menu) {
return true; return true;
} }
InputManager::InputManager() InputManager::InputManager(PowerSaver& powerSaver)
: powerSaver(powerSaver)
{ {
#ifndef SDL_JOYSTICK_DISABLED #ifndef SDL_JOYSTICK_DISABLED
int i; int i;
@ -301,8 +302,8 @@ bool InputManager::getButton(Button *button, bool wait) {
if (i == BUTTON_TYPE_SIZE) if (i == BUTTON_TYPE_SIZE)
return false; return false;
if (wait && PowerSaver::isRunning()) { if (wait) {
PowerSaver::getInstance()->resetScreenTimer(); powerSaver.resetScreenTimer();
} }
return true; return true;

View File

@ -29,6 +29,7 @@
class GMenu2X; class GMenu2X;
class Menu; class Menu;
class PowerSaver;
class InputManager; class InputManager;
enum EventCode { enum EventCode {
@ -61,7 +62,7 @@ public:
}; };
#define BUTTON_TYPE_SIZE 10 #define BUTTON_TYPE_SIZE 10
InputManager(); InputManager(PowerSaver& powerSaver);
~InputManager(); ~InputManager();
bool init(GMenu2X *gmenu2x, Menu *menu); bool init(GMenu2X *gmenu2x, Menu *menu);
@ -81,6 +82,7 @@ private:
GMenu2X *gmenu2x; GMenu2X *gmenu2x;
Menu *menu; Menu *menu;
PowerSaver& powerSaver;
ButtonMapEntry buttonMap[BUTTON_TYPE_SIZE]; ButtonMapEntry buttonMap[BUTTON_TYPE_SIZE];
#ifndef SDL_JOYSTICK_DISABLED #ifndef SDL_JOYSTICK_DISABLED

View File

@ -25,23 +25,14 @@ Uint32 screenTimerCallback(Uint32 timeout, void *d) {
return 0; return 0;
} }
PowerSaver *PowerSaver::getInstance() {
if (!instance) {
instance = new PowerSaver();
}
return instance;
}
bool PowerSaver::isRunning() {
return !!instance;
}
PowerSaver::PowerSaver() PowerSaver::PowerSaver()
: screenState(false) : screenState(false)
, screenTimeout(0) , screenTimeout(0)
, screenTimer(nullptr) , screenTimer(nullptr)
{ {
enableScreen(); enableScreen();
assert(!instance);
instance = this;
} }
PowerSaver::~PowerSaver() { PowerSaver::~PowerSaver() {

View File

@ -5,14 +5,12 @@
class PowerSaver { class PowerSaver {
public: public:
static PowerSaver *getInstance(); PowerSaver();
static bool isRunning();
~PowerSaver(); ~PowerSaver();
void resetScreenTimer(); void resetScreenTimer();
void setScreenTimeout(unsigned int seconds); void setScreenTimeout(unsigned int seconds);
private: private:
PowerSaver();
void addScreenTimer(); void addScreenTimer();
void removeScreenTimer(); void removeScreenTimer();
void setScreenBlanking(bool state); void setScreenBlanking(bool state);