1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-17 20:14:03 +02:00

Various cleanups in PowerSaver

Use nullptr instead of NULL, evaluate pointer as bool where possible.
Declare the timer callback as a friend function so the methods it uses
can stay private.
Initialize screenState to false; was uninitialized which means that
an initially blanked screen might not be unblanked.
Force screen enable in constructor instead of when timeout is set.
Add removeScreenTimer method.
Include C++-ified versions of the C headers.
This commit is contained in:
Maarten ter Huurne 2014-08-14 09:16:13 +02:00
parent 6a2a0969f3
commit 8d662c95f6
2 changed files with 36 additions and 32 deletions

View File

@ -1,13 +1,14 @@
#include "powersaver.h" #include "powersaver.h"
#include "debug.h" #include "debug.h"
#include <errno.h> #include <cassert>
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
PowerSaver *PowerSaver::instance = NULL; PowerSaver *PowerSaver::instance = nullptr;
Uint32 screenTimerCallback(Uint32 timeout, void *d) { Uint32 screenTimerCallback(Uint32 timeout, void *d) {
unsigned int * old_ticks = (unsigned int *) d; unsigned int * old_ticks = (unsigned int *) d;
@ -20,29 +21,32 @@ Uint32 screenTimerCallback(Uint32 timeout, void *d) {
} }
DEBUG("Disable Backlight Event\n"); DEBUG("Disable Backlight Event\n");
PowerSaver::getInstance()->disableScreen(); PowerSaver::instance->disableScreen();
return 0; return 0;
} }
PowerSaver *PowerSaver::getInstance() { PowerSaver *PowerSaver::getInstance() {
if (instance == NULL) { if (!instance) {
instance = new PowerSaver(); instance = new PowerSaver();
} }
return instance; return instance;
} }
bool PowerSaver::isRunning() { bool PowerSaver::isRunning() {
return instance != NULL; return !!instance;
} }
PowerSaver::PowerSaver() { PowerSaver::PowerSaver()
setScreenTimeout(0); : screenState(false)
screenTimer = NULL; , screenTimeout(0)
, screenTimer(nullptr)
{
enableScreen();
} }
PowerSaver::~PowerSaver() { PowerSaver::~PowerSaver() {
SDL_RemoveTimer(screenTimer); removeScreenTimer();
instance = NULL; instance = nullptr;
} }
void PowerSaver::setScreenTimeout(unsigned int seconds) { void PowerSaver::setScreenTimeout(unsigned int seconds) {
@ -51,31 +55,30 @@ void PowerSaver::setScreenTimeout(unsigned int seconds) {
} }
void PowerSaver::resetScreenTimer() { void PowerSaver::resetScreenTimer() {
if (screenTimer != NULL) { removeScreenTimer();
SDL_RemoveTimer(screenTimer); enableScreen();
} if (screenTimeout != 0) {
addScreenTimer();
addScreenTimer();
// If display is off, turn on it.
if (!screenState) {
enableScreen();
} }
} }
void PowerSaver::addScreenTimer() { void PowerSaver::addScreenTimer() {
// If timeout is zero, don't set timeout. assert(!screenTimer);
if (screenTimeout == 0) {
screenTimer = NULL;
return;
}
timeout_startms = SDL_GetTicks(); timeout_startms = SDL_GetTicks();
screenTimer = SDL_AddTimer(screenTimeout * 1000, screenTimerCallback, &timeout_startms); screenTimer = SDL_AddTimer(
if (screenTimer == NULL) { screenTimeout * 1000, screenTimerCallback, &timeout_startms);
if (!screenTimer) {
ERROR("Could not initialize SDLTimer: %s\n", SDL_GetError()); ERROR("Could not initialize SDLTimer: %s\n", SDL_GetError());
} }
} }
void PowerSaver::removeScreenTimer() {
if (screenTimer) {
SDL_RemoveTimer(screenTimer);
screenTimer = nullptr;
}
}
#define SCREEN_BLANK_PATH "/sys/class/graphics/fb0/blank" #define SCREEN_BLANK_PATH "/sys/class/graphics/fb0/blank"
void PowerSaver::setScreenBlanking(bool state) { void PowerSaver::setScreenBlanking(bool state) {
const char *path = SCREEN_BLANK_PATH; const char *path = SCREEN_BLANK_PATH;

View File

@ -9,22 +9,23 @@ public:
static bool isRunning(); static bool isRunning();
~PowerSaver(); ~PowerSaver();
void resetScreenTimer(); void resetScreenTimer();
void enableScreen();
void disableScreen();
void setScreenTimeout(unsigned int seconds); void setScreenTimeout(unsigned int seconds);
private: private:
PowerSaver(); PowerSaver();
void addScreenTimer(); void addScreenTimer();
void removeScreenTimer();
void setScreenBlanking(bool state); void setScreenBlanking(bool state);
void enableScreen();
void disableScreen();
static PowerSaver *instance; static PowerSaver *instance;
bool screenState; bool screenState;
unsigned int screenTimeout; unsigned int screenTimeout;
unsigned int timeout_startms; unsigned int timeout_startms;
SDL_TimerID screenTimer; SDL_TimerID screenTimer;
friend Uint32 screenTimerCallback(Uint32 timeout, void *d);
}; };
#endif #endif