1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-29 12:18:27 +02:00

Add powersaver class

This commit is contained in:
Sergey Kukunin 2011-06-02 06:04:35 +03:00
parent b068aa04d4
commit 240286df11
6 changed files with 130 additions and 5 deletions

View File

@ -12,7 +12,7 @@ gmenu2x_SOURCES = asfont.cpp button.cpp cpu.cpp dirdialog.cpp filedialog.cpp \
textdialog.cpp textmanualdialog.cpp touchscreen.cpp translator.cpp \ textdialog.cpp textmanualdialog.cpp touchscreen.cpp translator.cpp \
utilities.cpp wallpaperdialog.cpp \ utilities.cpp wallpaperdialog.cpp \
browsedialog.cpp buttonbox.cpp dialog.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 \ noinst_HEADERS = asfont.h button.h cpu.h dirdialog.h FastDelegate.h \
filedialog.h filelister.h gmenu2x.h gp2x.h iconbutton.h imagedialog.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 \ surfacecollection.h surface.h textdialog.h textmanualdialog.h \
touchscreen.h translator.h utilities.h wallpaperdialog.h \ touchscreen.h translator.h utilities.h wallpaperdialog.h \
browsedialog.h buttonbox.h dialog.h \ browsedialog.h buttonbox.h dialog.h \
imageio.h imageio.h powersaver.h
AM_CFLAGS= @CFLAGS@ @SDL_CFLAGS@ AM_CFLAGS= @CFLAGS@ @SDL_CFLAGS@

View File

@ -194,6 +194,7 @@ void GMenu2X::gp2x_tvout_off() {
#endif #endif
} }
GMenu2X::GMenu2X() { GMenu2X::GMenu2X() {
//Detect firmware version and type //Detect firmware version and type
if (fileExists("/etc/open2x")) { if (fileExists("/etc/open2x")) {
@ -271,7 +272,7 @@ GMenu2X::GMenu2X() {
#endif #endif
//Screen //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()); ERROR("Could not initialize SDL: %s\n", SDL_GetError());
quit(); quit();
} }
@ -309,6 +310,7 @@ GMenu2X::GMenu2X() {
initBG(); initBG();
input.init(path+"input.conf"); input.init(path+"input.conf");
PowerSaver::getInstance()->setScreenTimeout( confInt["backlightTimeout"] );
setInputSpeed(); setInputSpeed();
initServices(); initServices();
setBacklight(confInt["backlight"]); setBacklight(confInt["backlight"]);
@ -319,6 +321,7 @@ GMenu2X::GMenu2X() {
readTmp(); readTmp();
if (lastSelectorElement>-1 && menu->selLinkApp()!=NULL && (!menu->selLinkApp()->getSelectorDir().empty() || !lastSelectorDir.empty())) if (lastSelectorElement>-1 && menu->selLinkApp()!=NULL && (!menu->selLinkApp()->getSelectorDir().empty() || !lastSelectorDir.empty()))
menu->selLinkApp()->selector(lastSelectorElement,lastSelectorDir); menu->selLinkApp()->selector(lastSelectorElement,lastSelectorDir);
} }
GMenu2X::~GMenu2X() { GMenu2X::~GMenu2X() {
@ -561,6 +564,7 @@ void GMenu2X::readConfig() {
evalIntConf( &confInt["maxClock"], 430, 30, 500 ); evalIntConf( &confInt["maxClock"], 430, 30, 500 );
evalIntConf( &confInt["menuClock"], 200, 30, 430 ); evalIntConf( &confInt["menuClock"], 200, 30, 430 );
evalIntConf( &confInt["globalVolume"], 67, 0,100 ); evalIntConf( &confInt["globalVolume"], 67, 0,100 );
evalIntConf( &confInt["backlightTimeout"], 15, 0,120 );
evalIntConf( &confInt["backlight"], 100, 5,100 ); evalIntConf( &confInt["backlight"], 100, 5,100 );
evalIntConf( &confInt["videoBpp"], 32,32,32 ); // 8,16 evalIntConf( &confInt["videoBpp"], 32,32,32 ); // 8,16
@ -816,7 +820,6 @@ void GMenu2X::main() {
if (!fileExists(CARD_ROOT)) if (!fileExists(CARD_ROOT))
CARD_ROOT = "/"; CARD_ROOT = "/";
while (!quit) { while (!quit) {
tickNow = SDL_GetTicks(); 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"])); 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 //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["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 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)); 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 (prevbacklight != confInt["backlight"]) setBacklight(confInt["backlight"]);
if (curMenuClock!=confInt["menuClock"]) setClock(confInt["menuClock"]); if (curMenuClock!=confInt["menuClock"]) setClock(confInt["menuClock"]);
if (curGlobalVolume!=confInt["globalVolume"]) setVolume(confInt["globalVolume"]); if (curGlobalVolume!=confInt["globalVolume"]) setVolume(confInt["globalVolume"]);
PowerSaver::getInstance()->setScreenTimeout( confInt["backlightTimeout"] );
if (lang == "English") lang = ""; if (lang == "English") lang = "";
if (lang != tr.lang()) { if (lang != tr.lang()) {
tr.setLang(lang); tr.setLang(lang);

View File

@ -30,6 +30,7 @@
#include "inputmanager.h" #include "inputmanager.h"
#include "asfont.h" #include "asfont.h"
#include "surface.h" #include "surface.h"
#include "powersaver.h"
#include <iostream> #include <iostream>
#include <string> #include <string>
@ -122,6 +123,7 @@ private:
usbnet, usbnet,
samba, samba,
web; web;
string ip, defaultgw, lastSelectorDir; string ip, defaultgw, lastSelectorDir;
int lastSelectorElement; int lastSelectorElement;
void readConfig(); void readConfig();

View File

@ -21,6 +21,7 @@
#include "debug.h" #include "debug.h"
#include "inputmanager.h" #include "inputmanager.h"
#include "utilities.h" #include "utilities.h"
#include "powersaver.h"
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
@ -179,6 +180,8 @@ bool InputManager::getEvent(bevent_t *bevent, bool wait) {
break; break;
} }
} }
if ( wait ) {
PowerSaver::getInstance()->resetScreenTimer();
}
return true; return true;
} }

90
src/powersaver.cpp Normal file
View File

@ -0,0 +1,90 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#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);
}
}

25
src/powersaver.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef POWERSAVER_H
#define POWERSAVER_H
#include <SDL.h>
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