blob: 61356d340737b4c35cf8d628cd067efa2c24f8c8 [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
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050019#include <string>
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050020#include <pthread.h>
21#include <sys/time.h>
22#include <time.h>
23#include <unistd.h>
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050024#include "pages.hpp"
25#include "blanktimer.hpp"
bigbiff bigbifff8e2f372013-02-27 20:50:43 -050026#include "../data.hpp"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050027extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000028#include "../twcommon.h"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050029}
bigbiffd81833a2021-01-17 11:06:57 -050030#include "minuitwrp/minui.h"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050031#include "../twrp-functions.hpp"
32#include "../variables.h"
33
34blanktimer::blanktimer(void) {
thatfb759d42015-01-11 12:16:53 +010035 pthread_mutex_init(&mutex, NULL);
36 setTime(0); // no timeout
37 state = kOn;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050038 orig_brightness = getBrightness();
gordon13370d9133d2013-06-08 14:17:07 +020039}
40
thatfb759d42015-01-11 12:16:53 +010041bool blanktimer::isScreenOff() {
42 return state >= kOff;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050043}
44
Dees_Troy2f9117a2013-02-17 19:52:09 -060045void blanktimer::setTime(int newtime) {
thatfb759d42015-01-11 12:16:53 +010046 pthread_mutex_lock(&mutex);
Dees_Troy2f9117a2013-02-17 19:52:09 -060047 sleepTimer = newtime;
thatfb759d42015-01-11 12:16:53 +010048 pthread_mutex_unlock(&mutex);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050049}
50
51void blanktimer::setTimer(void) {
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050052 clock_gettime(CLOCK_MONOTONIC, &btimer);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050053}
54
thatfb759d42015-01-11 12:16:53 +010055void blanktimer::checkForTimeout() {
56#ifndef TW_NO_SCREEN_TIMEOUT
57 pthread_mutex_lock(&mutex);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050058 timespec curTime, diff;
thatfb759d42015-01-11 12:16:53 +010059 clock_gettime(CLOCK_MONOTONIC, &curTime);
60 diff = TWFunc::timespec_diff(btimer, curTime);
61 if (sleepTimer > 2 && diff.tv_sec > (sleepTimer - 2) && state == kOn) {
62 orig_brightness = getBrightness();
63 state = kDim;
64 TWFunc::Set_Brightness("5");
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050065 }
thatfb759d42015-01-11 12:16:53 +010066 if (sleepTimer && diff.tv_sec > sleepTimer && state < kOff) {
67 state = kOff;
68 TWFunc::Set_Brightness("0");
bigbiffad58e1b2020-07-06 20:24:34 -040069 TWFunc::check_and_run_script("/system/bin/postscreenblank.sh", "blank");
thatfb759d42015-01-11 12:16:53 +010070 PageManager::ChangeOverlay("lock");
71 }
72#ifndef TW_NO_SCREEN_BLANK
Ethan Yonkerfbb43532015-12-28 21:54:50 +010073 if (state == kOff) {
74 gr_fb_blank(true);
thatfb759d42015-01-11 12:16:53 +010075 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
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100100 gr_fb_blank(false);
bigbiff bigbiff87940362013-03-09 09:58:50 -0500101#endif
thatfb759d42015-01-11 12:16:53 +0100102 // TODO: this is asymmetric with postscreenblank.sh - shouldn't it be under the next case label?
bigbiffad58e1b2020-07-06 20:24:34 -0400103 TWFunc::check_and_run_script("/system/bin/postscreenunblank.sh", "unblank");
Dees_Troy70237dc2013-02-28 21:31:48 +0000104 // No break here, we want to keep going
thatfb759d42015-01-11 12:16:53 +0100105 case kOff:
Dees_Troy70237dc2013-02-28 21:31:48 +0000106 gui_forceRender();
107 // No break here, we want to keep going
thatfb759d42015-01-11 12:16:53 +0100108 case kDim:
Tatsuyuki Ishi548a1752015-12-28 10:08:58 +0900109 if (!orig_brightness.empty())
xNUTxe85f02d2014-07-18 01:30:58 +0200110 TWFunc::Set_Brightness(orig_brightness);
thatfb759d42015-01-11 12:16:53 +0100111 state = kOn;
112 case kOn:
Dees_Troy70237dc2013-02-28 21:31:48 +0000113 break;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500114 }
thatfb759d42015-01-11 12:16:53 +0100115 pthread_mutex_unlock(&mutex);
116#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500117}
Matt Mower9472ba12016-01-20 18:12:47 -0600118
119void blanktimer::blank(void) {
120/* 1) No need for timer handling since checkForTimeout() verifies
121 * state of screen before performing screen-off
122 * 2) Assume screen-off causes issues for devices that set
123 * TW_NO_SCREEN_TIMEOUT and do not blank screen here either
124 */
125
126#ifndef TW_NO_SCREEN_TIMEOUT
127 pthread_mutex_lock(&mutex);
128 if (state == kOn) {
129 orig_brightness = getBrightness();
130 state = kOff;
131 TWFunc::Set_Brightness("0");
bigbiffad58e1b2020-07-06 20:24:34 -0400132 TWFunc::check_and_run_script("/system/bin/postscreenblank.sh", "blank");
Matt Mower9472ba12016-01-20 18:12:47 -0600133 }
134#ifndef TW_NO_SCREEN_BLANK
135 if (state == kOff) {
136 gr_fb_blank(true);
137 state = kBlanked;
138 }
139#endif
140 pthread_mutex_unlock(&mutex);
141#endif
142}
143
144void blanktimer::toggleBlank(void) {
145 if (state == kOn) {
146 blank();
147 PageManager::ChangeOverlay("lock");
148 } else {
149 resetTimerAndUnblank();
150 }
151}