From 240286df11ccc463edce8907a47b717caf5def16 Mon Sep 17 00:00:00 2001 From: Sergey Kukunin Date: Thu, 2 Jun 2011 06:04:35 +0300 Subject: [PATCH] Add powersaver class --- src/Makefile.am | 4 +- src/gmenu2x.cpp | 9 ++++- src/gmenu2x.h | 2 + src/inputmanager.cpp | 5 ++- src/powersaver.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++++ src/powersaver.h | 25 ++++++++++++ 6 files changed, 130 insertions(+), 5 deletions(-) create mode 100644 src/powersaver.cpp create mode 100644 src/powersaver.h diff --git a/src/Makefile.am b/src/Makefile.am index ee16c15..16e0ac5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -12,7 +12,7 @@ gmenu2x_SOURCES = asfont.cpp button.cpp cpu.cpp dirdialog.cpp filedialog.cpp \ textdialog.cpp textmanualdialog.cpp touchscreen.cpp translator.cpp \ utilities.cpp wallpaperdialog.cpp \ browsedialog.cpp buttonbox.cpp dialog.cpp \ - imageio.cpp + imageio.cpp powersaver.cpp noinst_HEADERS = asfont.h button.h cpu.h dirdialog.h FastDelegate.h \ filedialog.h filelister.h gmenu2x.h gp2x.h iconbutton.h imagedialog.h \ @@ -25,7 +25,7 @@ noinst_HEADERS = asfont.h button.h cpu.h dirdialog.h FastDelegate.h \ surfacecollection.h surface.h textdialog.h textmanualdialog.h \ touchscreen.h translator.h utilities.h wallpaperdialog.h \ browsedialog.h buttonbox.h dialog.h \ - imageio.h + imageio.h powersaver.h AM_CFLAGS= @CFLAGS@ @SDL_CFLAGS@ diff --git a/src/gmenu2x.cpp b/src/gmenu2x.cpp index afa07f5..fa1b1ed 100644 --- a/src/gmenu2x.cpp +++ b/src/gmenu2x.cpp @@ -194,6 +194,7 @@ void GMenu2X::gp2x_tvout_off() { #endif } + GMenu2X::GMenu2X() { //Detect firmware version and type if (fileExists("/etc/open2x")) { @@ -271,7 +272,7 @@ GMenu2X::GMenu2X() { #endif //Screen - if( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_JOYSTICK)<0 ) { + if( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_JOYSTICK|SDL_INIT_TIMER)<0 ) { ERROR("Could not initialize SDL: %s\n", SDL_GetError()); quit(); } @@ -309,6 +310,7 @@ GMenu2X::GMenu2X() { initBG(); input.init(path+"input.conf"); + PowerSaver::getInstance()->setScreenTimeout( confInt["backlightTimeout"] ); setInputSpeed(); initServices(); setBacklight(confInt["backlight"]); @@ -319,6 +321,7 @@ GMenu2X::GMenu2X() { readTmp(); if (lastSelectorElement>-1 && menu->selLinkApp()!=NULL && (!menu->selLinkApp()->getSelectorDir().empty() || !lastSelectorDir.empty())) menu->selLinkApp()->selector(lastSelectorElement,lastSelectorDir); + } GMenu2X::~GMenu2X() { @@ -561,6 +564,7 @@ void GMenu2X::readConfig() { evalIntConf( &confInt["maxClock"], 430, 30, 500 ); evalIntConf( &confInt["menuClock"], 200, 30, 430 ); evalIntConf( &confInt["globalVolume"], 67, 0,100 ); + evalIntConf( &confInt["backlightTimeout"], 15, 0,120 ); evalIntConf( &confInt["backlight"], 100, 5,100 ); evalIntConf( &confInt["videoBpp"], 32,32,32 ); // 8,16 @@ -816,7 +820,6 @@ void GMenu2X::main() { if (!fileExists(CARD_ROOT)) CARD_ROOT = "/"; - while (!quit) { tickNow = SDL_GetTicks(); @@ -1112,6 +1115,7 @@ void GMenu2X::options() { sd.addSetting(new MenuSettingBool(this,tr["Output logs"],tr["Logs the output of the links. Use the Log Viewer to read them."],&confInt["outputLogs"])); //G sd.addSetting(new MenuSettingInt(this,tr["Lcd Backlight"],tr["Set dingoo's Lcd Backlight value (default: 100)"],&confInt["backlight"],5,100)); + sd.addSetting(new MenuSettingInt(this,tr["Screen Timeout"],tr["Set screen's backlight timeout in seconds"],&confInt["backlightTimeout"],0,120)); // sd.addSetting(new MenuSettingMultiString(this,tr["Tv-Out encoding"],tr["Encoding of the tv-out signal"],&confStr["tvoutEncoding"],&encodings)); sd.addSetting(new MenuSettingBool(this,tr["Show root"],tr["Show root folder in the file selection dialogs"],&showRootFolder)); @@ -1120,6 +1124,7 @@ void GMenu2X::options() { if (prevbacklight != confInt["backlight"]) setBacklight(confInt["backlight"]); if (curMenuClock!=confInt["menuClock"]) setClock(confInt["menuClock"]); if (curGlobalVolume!=confInt["globalVolume"]) setVolume(confInt["globalVolume"]); + PowerSaver::getInstance()->setScreenTimeout( confInt["backlightTimeout"] ); if (lang == "English") lang = ""; if (lang != tr.lang()) { tr.setLang(lang); diff --git a/src/gmenu2x.h b/src/gmenu2x.h index cf13c43..278facf 100644 --- a/src/gmenu2x.h +++ b/src/gmenu2x.h @@ -30,6 +30,7 @@ #include "inputmanager.h" #include "asfont.h" #include "surface.h" +#include "powersaver.h" #include #include @@ -122,6 +123,7 @@ private: usbnet, samba, web; + string ip, defaultgw, lastSelectorDir; int lastSelectorElement; void readConfig(); diff --git a/src/inputmanager.cpp b/src/inputmanager.cpp index a5c5a63..abc2e82 100644 --- a/src/inputmanager.cpp +++ b/src/inputmanager.cpp @@ -21,6 +21,7 @@ #include "debug.h" #include "inputmanager.h" #include "utilities.h" +#include "powersaver.h" #include #include @@ -179,6 +180,8 @@ bool InputManager::getEvent(bevent_t *bevent, bool wait) { break; } } - + if ( wait ) { + PowerSaver::getInstance()->resetScreenTimer(); + } return true; } diff --git a/src/powersaver.cpp b/src/powersaver.cpp new file mode 100644 index 0000000..914555a --- /dev/null +++ b/src/powersaver.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +#include +#include + +#include "powersaver.h" +#include "debug.h" +PowerSaver* PowerSaver::instance = NULL; +Uint32 screenTimerCallback(Uint32 interval, void *param) +{ + DEBUG("Disable Backlight Event\n"); + PowerSaver::getInstance()->disableScreen(); + return 0; +} + +PowerSaver* PowerSaver::getInstance() { + if ( instance == NULL ) { + instance = new PowerSaver(); + } + return instance; +} + +PowerSaver::PowerSaver( ) { + setScreenTimeout(0); + screenTimer = NULL; +} + +PowerSaver::~PowerSaver() { + SDL_RemoveTimer(screenTimer); +} + +void PowerSaver::setScreenTimeout( unsigned int seconds ) { + screenTimeout = seconds; + resetScreenTimer(); +} + +void PowerSaver::resetScreenTimer() { + if ( screenTimer != NULL ) { + SDL_RemoveTimer(screenTimer); + } + addScreenTimer(); + //If display is off, turn on it + if ( !screenState ) { + enableScreen(); + } +} + +void PowerSaver::addScreenTimer() { + //if timeout is zero, don't set timeout + if ( screenTimeout == 0 ) { + screenTimer = NULL; + return; + } + screenTimer = SDL_AddTimer(screenTimeout*1000, screenTimerCallback,NULL); + if ( screenTimer == NULL ) { + ERROR("Could not initialize SDLTimer: %s\n", SDL_GetError()); + } +} + +#define SCREEN_BLANK_PATH "/sys/class/graphics/fb0/blank" +void PowerSaver::setScreenBlanking( bool state ) { + const char* path = SCREEN_BLANK_PATH; + const char* blank = state ? "0" : "1"; + + int fd = open(path, O_RDWR); + if (fd == -1) { + WARNING("Failed to open '%s': %s\n", path, strerror(errno)); + } else { + ssize_t written = write(fd, blank, strlen(blank)); + if (written == -1) { + WARNING("Error writing '%s': %s\n", path, strerror(errno)); + } + close(fd); + } + screenState = state; +} + +void PowerSaver::enableScreen() { + if ( !screenState ) { + setScreenBlanking(true); + } +} +void PowerSaver::disableScreen() { + if ( screenState ) { + setScreenBlanking(false); + } +} + + diff --git a/src/powersaver.h b/src/powersaver.h new file mode 100644 index 0000000..e464853 --- /dev/null +++ b/src/powersaver.h @@ -0,0 +1,25 @@ +#ifndef POWERSAVER_H +#define POWERSAVER_H +#include +class PowerSaver { + + public: + static PowerSaver* getInstance(); + ~PowerSaver(); + void addScreenTimer(); + void resetScreenTimer(); + + void enableScreen(); + void disableScreen(); + + void setScreenTimeout( unsigned int seconds ); + private: + PowerSaver( ); + static PowerSaver* instance; + bool screenState; + unsigned int screenTimeout; + SDL_TimerID screenTimer; + + void setScreenBlanking( bool state ); +}; +#endif