Date:2011-06-02 05:04:35 (12 years 9 months ago)
Author:Sergey Kukunin
Commit:240286df11ccc463edce8907a47b717caf5def16
Message:Add powersaver class

Files: src/Makefile.am (2 diffs)
src/gmenu2x.cpp (8 diffs)
src/gmenu2x.h (2 diffs)
src/inputmanager.cpp (2 diffs)
src/powersaver.cpp (1 diff)
src/powersaver.h (1 diff)

Change Details

src/Makefile.am
1212    textdialog.cpp textmanualdialog.cpp touchscreen.cpp translator.cpp \
1313    utilities.cpp wallpaperdialog.cpp \
1414    browsedialog.cpp buttonbox.cpp dialog.cpp \
15    imageio.cpp
15    imageio.cpp powersaver.cpp
1616
1717noinst_HEADERS = asfont.h button.h cpu.h dirdialog.h FastDelegate.h \
1818    filedialog.h filelister.h gmenu2x.h gp2x.h iconbutton.h imagedialog.h \
...... 
2525    surfacecollection.h surface.h textdialog.h textmanualdialog.h \
2626    touchscreen.h translator.h utilities.h wallpaperdialog.h \
2727    browsedialog.h buttonbox.h dialog.h \
28    imageio.h
28    imageio.h powersaver.h
2929
3030AM_CFLAGS= @CFLAGS@ @SDL_CFLAGS@
3131
src/gmenu2x.cpp
194194#endif
195195}
196196
197
197198GMenu2X::GMenu2X() {
198199    //Detect firmware version and type
199200    if (fileExists("/etc/open2x")) {
...... 
271272#endif
272273
273274    //Screen
274    if( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_JOYSTICK)<0 ) {
275    if( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_JOYSTICK|SDL_INIT_TIMER)<0 ) {
275276        ERROR("Could not initialize SDL: %s\n", SDL_GetError());
276277        quit();
277278    }
...... 
309310
310311    initBG();
311312    input.init(path+"input.conf");
313    PowerSaver::getInstance()->setScreenTimeout( confInt["backlightTimeout"] );
312314    setInputSpeed();
313315    initServices();
314316    setBacklight(confInt["backlight"]);
...... 
319321    readTmp();
320322    if (lastSelectorElement>-1 && menu->selLinkApp()!=NULL && (!menu->selLinkApp()->getSelectorDir().empty() || !lastSelectorDir.empty()))
321323        menu->selLinkApp()->selector(lastSelectorElement,lastSelectorDir);
324
322325}
323326
324327GMenu2X::~GMenu2X() {
...... 
561564    evalIntConf( &confInt["maxClock"], 430, 30, 500 );
562565    evalIntConf( &confInt["menuClock"], 200, 30, 430 );
563566    evalIntConf( &confInt["globalVolume"], 67, 0,100 );
567    evalIntConf( &confInt["backlightTimeout"], 15, 0,120 );
564568    evalIntConf( &confInt["backlight"], 100, 5,100 );
565569    evalIntConf( &confInt["videoBpp"], 32,32,32 ); // 8,16
566570
...... 
816820    if (!fileExists(CARD_ROOT))
817821        CARD_ROOT = "/";
818822
819
820823    while (!quit) {
821824        tickNow = SDL_GetTicks();
822825
...... 
11121115    sd.addSetting(new MenuSettingBool(this,tr["Output logs"],tr["Logs the output of the links. Use the Log Viewer to read them."],&confInt["outputLogs"]));
11131116    //G
11141117    sd.addSetting(new MenuSettingInt(this,tr["Lcd Backlight"],tr["Set dingoo's Lcd Backlight value (default: 100)"],&confInt["backlight"],5,100));
1118    sd.addSetting(new MenuSettingInt(this,tr["Screen Timeout"],tr["Set screen's backlight timeout in seconds"],&confInt["backlightTimeout"],0,120));
11151119// sd.addSetting(new MenuSettingMultiString(this,tr["Tv-Out encoding"],tr["Encoding of the tv-out signal"],&confStr["tvoutEncoding"],&encodings));
11161120    sd.addSetting(new MenuSettingBool(this,tr["Show root"],tr["Show root folder in the file selection dialogs"],&showRootFolder));
11171121
...... 
11201124        if (prevbacklight != confInt["backlight"]) setBacklight(confInt["backlight"]);
11211125        if (curMenuClock!=confInt["menuClock"]) setClock(confInt["menuClock"]);
11221126        if (curGlobalVolume!=confInt["globalVolume"]) setVolume(confInt["globalVolume"]);
1127        PowerSaver::getInstance()->setScreenTimeout( confInt["backlightTimeout"] );
11231128        if (lang == "English") lang = "";
11241129        if (lang != tr.lang()) {
11251130            tr.setLang(lang);
src/gmenu2x.h
3030#include "inputmanager.h"
3131#include "asfont.h"
3232#include "surface.h"
33#include "powersaver.h"
3334
3435#include <iostream>
3536#include <string>
...... 
122123        usbnet,
123124        samba,
124125        web;
126
125127    string ip, defaultgw, lastSelectorDir;
126128    int lastSelectorElement;
127129    void readConfig();
src/inputmanager.cpp
2121#include "debug.h"
2222#include "inputmanager.h"
2323#include "utilities.h"
24#include "powersaver.h"
2425
2526#include <iostream>
2627#include <fstream>
...... 
179180              break;
180181          }
181182    }
182
183    if ( wait ) {
184        PowerSaver::getInstance()->resetScreenTimer();
185    }
183186    return true;
184187}
src/powersaver.cpp
1#include <errno.h>
2#include <fcntl.h>
3#include <stdio.h>
4#include <string.h>
5#include <unistd.h>
6
7#include "powersaver.h"
8#include "debug.h"
9PowerSaver* PowerSaver::instance = NULL;
10Uint32 screenTimerCallback(Uint32 interval, void *param)
11{
12    DEBUG("Disable Backlight Event\n");
13    PowerSaver::getInstance()->disableScreen();
14    return 0;
15}
16
17PowerSaver* PowerSaver::getInstance() {
18    if ( instance == NULL ) {
19        instance = new PowerSaver();
20    }
21    return instance;
22}
23
24PowerSaver::PowerSaver( ) {
25    setScreenTimeout(0);
26    screenTimer = NULL;
27}
28
29PowerSaver::~PowerSaver() {
30    SDL_RemoveTimer(screenTimer);
31}
32
33void PowerSaver::setScreenTimeout( unsigned int seconds ) {
34    screenTimeout = seconds;
35    resetScreenTimer();
36}
37
38void PowerSaver::resetScreenTimer() {
39    if ( screenTimer != NULL ) {
40        SDL_RemoveTimer(screenTimer);
41    }
42    addScreenTimer();
43    //If display is off, turn on it
44    if ( !screenState ) {
45        enableScreen();
46    }
47}
48
49void PowerSaver::addScreenTimer() {
50    //if timeout is zero, don't set timeout
51    if ( screenTimeout == 0 ) {
52        screenTimer = NULL;
53        return;
54    }
55    screenTimer = SDL_AddTimer(screenTimeout*1000, screenTimerCallback,NULL);
56    if ( screenTimer == NULL ) {
57        ERROR("Could not initialize SDLTimer: %s\n", SDL_GetError());
58    }
59}
60
61#define SCREEN_BLANK_PATH "/sys/class/graphics/fb0/blank"
62void PowerSaver::setScreenBlanking( bool state ) {
63    const char* path = SCREEN_BLANK_PATH;
64    const char* blank = state ? "0" : "1";
65
66    int fd = open(path, O_RDWR);
67    if (fd == -1) {
68        WARNING("Failed to open '%s': %s\n", path, strerror(errno));
69    } else {
70        ssize_t written = write(fd, blank, strlen(blank));
71        if (written == -1) {
72            WARNING("Error writing '%s': %s\n", path, strerror(errno));
73        }
74        close(fd);
75    }
76    screenState = state;
77}
78
79void PowerSaver::enableScreen() {
80    if ( !screenState ) {
81        setScreenBlanking(true);
82    }
83}
84void PowerSaver::disableScreen() {
85    if ( screenState ) {
86        setScreenBlanking(false);
87    }
88}
89
90
src/powersaver.h
1#ifndef POWERSAVER_H
2#define POWERSAVER_H
3#include <SDL.h>
4class PowerSaver {
5
6    public:
7        static PowerSaver* getInstance();
8        ~PowerSaver();
9        void addScreenTimer();
10        void resetScreenTimer();
11
12        void enableScreen();
13        void disableScreen();
14
15        void setScreenTimeout( unsigned int seconds );
16    private:
17        PowerSaver( );
18        static PowerSaver* instance;
19        bool screenState;
20        unsigned int screenTimeout;
21        SDL_TimerID screenTimer;
22
23        void setScreenBlanking( bool state );
24};
25#endif

Archive Download the corresponding diff file



interactive