bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 1 | /* |
| 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 bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 19 | #include <string> |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 20 | #include <pthread.h> |
| 21 | #include <sys/time.h> |
| 22 | #include <time.h> |
| 23 | #include <unistd.h> |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 24 | #include "pages.hpp" |
| 25 | #include "blanktimer.hpp" |
bigbiff bigbiff | f8e2f37 | 2013-02-27 20:50:43 -0500 | [diff] [blame] | 26 | #include "../data.hpp" |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 27 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 28 | #include "../twcommon.h" |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 29 | } |
bigbiff | d81833a | 2021-01-17 11:06:57 -0500 | [diff] [blame] | 30 | #include "minuitwrp/minui.h" |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 31 | #include "../twrp-functions.hpp" |
| 32 | #include "../variables.h" |
| 33 | |
| 34 | blanktimer::blanktimer(void) { |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 35 | pthread_mutex_init(&mutex, NULL); |
| 36 | setTime(0); // no timeout |
| 37 | state = kOn; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 38 | orig_brightness = getBrightness(); |
gordon1337 | 0d9133d | 2013-06-08 14:17:07 +0200 | [diff] [blame] | 39 | } |
| 40 | |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 41 | bool blanktimer::isScreenOff() { |
| 42 | return state >= kOff; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 43 | } |
| 44 | |
Dees_Troy | 2f9117a | 2013-02-17 19:52:09 -0600 | [diff] [blame] | 45 | void blanktimer::setTime(int newtime) { |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 46 | pthread_mutex_lock(&mutex); |
Dees_Troy | 2f9117a | 2013-02-17 19:52:09 -0600 | [diff] [blame] | 47 | sleepTimer = newtime; |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 48 | pthread_mutex_unlock(&mutex); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void blanktimer::setTimer(void) { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 52 | clock_gettime(CLOCK_MONOTONIC, &btimer); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 53 | } |
| 54 | |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 55 | void blanktimer::checkForTimeout() { |
| 56 | #ifndef TW_NO_SCREEN_TIMEOUT |
| 57 | pthread_mutex_lock(&mutex); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 58 | timespec curTime, diff; |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 59 | 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 bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 65 | } |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 66 | if (sleepTimer && diff.tv_sec > sleepTimer && state < kOff) { |
| 67 | state = kOff; |
| 68 | TWFunc::Set_Brightness("0"); |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 69 | TWFunc::check_and_run_script("/system/bin/postscreenblank.sh", "blank"); |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 70 | PageManager::ChangeOverlay("lock"); |
| 71 | } |
| 72 | #ifndef TW_NO_SCREEN_BLANK |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 73 | if (state == kOff) { |
| 74 | gr_fb_blank(true); |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 75 | state = kBlanked; |
| 76 | } |
| 77 | #endif |
| 78 | pthread_mutex_unlock(&mutex); |
| 79 | #endif |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 80 | } |
| 81 | |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 82 | string blanktimer::getBrightness(void) { |
| 83 | string result; |
Tatsuyuki Ishi | 548a175 | 2015-12-28 10:08:58 +0900 | [diff] [blame] | 84 | |
| 85 | if (DataManager::GetIntValue("tw_has_brightnesss_file")) { |
| 86 | DataManager::GetValue("tw_brightness", result); |
| 87 | if (result.empty()) |
| 88 | result = "255"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 89 | } |
| 90 | return result; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 91 | } |
| 92 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 93 | void blanktimer::resetTimerAndUnblank(void) { |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 94 | #ifndef TW_NO_SCREEN_TIMEOUT |
| 95 | pthread_mutex_lock(&mutex); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 96 | setTimer(); |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 97 | switch (state) { |
| 98 | case kBlanked: |
bigbiff bigbiff | 8794036 | 2013-03-09 09:58:50 -0500 | [diff] [blame] | 99 | #ifndef TW_NO_SCREEN_BLANK |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 100 | gr_fb_blank(false); |
bigbiff bigbiff | 8794036 | 2013-03-09 09:58:50 -0500 | [diff] [blame] | 101 | #endif |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 102 | // TODO: this is asymmetric with postscreenblank.sh - shouldn't it be under the next case label? |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 103 | TWFunc::check_and_run_script("/system/bin/postscreenunblank.sh", "unblank"); |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 104 | // No break here, we want to keep going |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 105 | case kOff: |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 106 | gui_forceRender(); |
| 107 | // No break here, we want to keep going |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 108 | case kDim: |
Tatsuyuki Ishi | 548a175 | 2015-12-28 10:08:58 +0900 | [diff] [blame] | 109 | if (!orig_brightness.empty()) |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 110 | TWFunc::Set_Brightness(orig_brightness); |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 111 | state = kOn; |
| 112 | case kOn: |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 113 | break; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 114 | } |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 115 | pthread_mutex_unlock(&mutex); |
| 116 | #endif |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 117 | } |
Matt Mower | 9472ba1 | 2016-01-20 18:12:47 -0600 | [diff] [blame] | 118 | |
| 119 | void 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"); |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 132 | TWFunc::check_and_run_script("/system/bin/postscreenblank.sh", "blank"); |
Matt Mower | 9472ba1 | 2016-01-20 18:12:47 -0600 | [diff] [blame] | 133 | } |
| 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 | |
| 144 | void blanktimer::toggleBlank(void) { |
| 145 | if (state == kOn) { |
| 146 | blank(); |
| 147 | PageManager::ChangeOverlay("lock"); |
| 148 | } else { |
| 149 | resetTimerAndUnblank(); |
| 150 | } |
| 151 | } |