blob: 06208e2732ade14ca0833430bfb441c0e15cc712 [file] [log] [blame]
bigbiff bigbiff8a68c312013-02-10 14:28:30 -05001/*
2 Copyright 2012 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19using namespace std;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050020#include <string>
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050021#include <pthread.h>
22#include <sys/time.h>
23#include <time.h>
24#include <unistd.h>
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050025#include "pages.hpp"
26#include "blanktimer.hpp"
bigbiff bigbifff8e2f372013-02-27 20:50:43 -050027#include "../data.hpp"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050028extern "C" {
thatfb759d42015-01-11 12:16:53 +010029#include "../minuitwrp/minui.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000030#include "../twcommon.h"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050031}
32#include "../twrp-functions.hpp"
33#include "../variables.h"
34
35blanktimer::blanktimer(void) {
thatfb759d42015-01-11 12:16:53 +010036 pthread_mutex_init(&mutex, NULL);
37 setTime(0); // no timeout
38 state = kOn;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050039 orig_brightness = getBrightness();
gordon13370d9133d2013-06-08 14:17:07 +020040}
41
thatfb759d42015-01-11 12:16:53 +010042bool blanktimer::isScreenOff() {
43 return state >= kOff;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050044}
45
Dees_Troy2f9117a2013-02-17 19:52:09 -060046void blanktimer::setTime(int newtime) {
thatfb759d42015-01-11 12:16:53 +010047 pthread_mutex_lock(&mutex);
Dees_Troy2f9117a2013-02-17 19:52:09 -060048 sleepTimer = newtime;
thatfb759d42015-01-11 12:16:53 +010049 pthread_mutex_unlock(&mutex);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050050}
51
52void blanktimer::setTimer(void) {
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050053 clock_gettime(CLOCK_MONOTONIC, &btimer);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050054}
55
thatfb759d42015-01-11 12:16:53 +010056void blanktimer::checkForTimeout() {
57#ifndef TW_NO_SCREEN_TIMEOUT
58 pthread_mutex_lock(&mutex);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050059 timespec curTime, diff;
thatfb759d42015-01-11 12:16:53 +010060 clock_gettime(CLOCK_MONOTONIC, &curTime);
61 diff = TWFunc::timespec_diff(btimer, curTime);
62 if (sleepTimer > 2 && diff.tv_sec > (sleepTimer - 2) && state == kOn) {
63 orig_brightness = getBrightness();
64 state = kDim;
65 TWFunc::Set_Brightness("5");
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050066 }
thatfb759d42015-01-11 12:16:53 +010067 if (sleepTimer && diff.tv_sec > sleepTimer && state < kOff) {
68 state = kOff;
69 TWFunc::Set_Brightness("0");
70 TWFunc::check_and_run_script("/sbin/postscreenblank.sh", "blank");
71 PageManager::ChangeOverlay("lock");
72 }
73#ifndef TW_NO_SCREEN_BLANK
74 if (state == kOff && gr_fb_blank(1) >= 0) {
75 state = kBlanked;
76 }
77#endif
78 pthread_mutex_unlock(&mutex);
79#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050080}
81
xNUTxe85f02d2014-07-18 01:30:58 +020082string blanktimer::getBrightness(void) {
83 string result;
Tatsuyuki Ishi548a1752015-12-28 10:08:58 +090084
85 if (DataManager::GetIntValue("tw_has_brightnesss_file")) {
86 DataManager::GetValue("tw_brightness", result);
87 if (result.empty())
88 result = "255";
Dees_Troya13d74f2013-03-24 08:54:55 -050089 }
90 return result;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050091}
92
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050093void blanktimer::resetTimerAndUnblank(void) {
thatfb759d42015-01-11 12:16:53 +010094#ifndef TW_NO_SCREEN_TIMEOUT
95 pthread_mutex_lock(&mutex);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050096 setTimer();
thatfb759d42015-01-11 12:16:53 +010097 switch (state) {
98 case kBlanked:
bigbiff bigbiff87940362013-03-09 09:58:50 -050099#ifndef TW_NO_SCREEN_BLANK
Dees_Troy70237dc2013-02-28 21:31:48 +0000100 if (gr_fb_blank(0) < 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000101 LOGINFO("blanktimer::resetTimerAndUnblank failed to gr_fb_blank(0)\n");
Dees_Troy70237dc2013-02-28 21:31:48 +0000102 break;
103 }
bigbiff bigbiff87940362013-03-09 09:58:50 -0500104#endif
thatfb759d42015-01-11 12:16:53 +0100105 // TODO: this is asymmetric with postscreenblank.sh - shouldn't it be under the next case label?
Dees_Troy1c8d4fb2013-07-12 15:05:29 +0000106 TWFunc::check_and_run_script("/sbin/postscreenunblank.sh", "unblank");
Dees_Troy70237dc2013-02-28 21:31:48 +0000107 // No break here, we want to keep going
thatfb759d42015-01-11 12:16:53 +0100108 case kOff:
Dees_Troy70237dc2013-02-28 21:31:48 +0000109 gui_forceRender();
110 // No break here, we want to keep going
thatfb759d42015-01-11 12:16:53 +0100111 case kDim:
Tatsuyuki Ishi548a1752015-12-28 10:08:58 +0900112 if (!orig_brightness.empty())
xNUTxe85f02d2014-07-18 01:30:58 +0200113 TWFunc::Set_Brightness(orig_brightness);
thatfb759d42015-01-11 12:16:53 +0100114 state = kOn;
115 case kOn:
Dees_Troy70237dc2013-02-28 21:31:48 +0000116 break;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500117 }
thatfb759d42015-01-11 12:16:53 +0100118 pthread_mutex_unlock(&mutex);
119#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500120}