1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-12-27 06:47:23 +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
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();

View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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() {

View File

@ -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);