Root/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
91

Archive Download this file



interactive