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 | |
| 19 | using namespace std; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 20 | #include <string> |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 21 | #include <pthread.h> |
| 22 | #include <sys/time.h> |
| 23 | #include <time.h> |
| 24 | #include <unistd.h> |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 25 | #include "pages.hpp" |
| 26 | #include "blanktimer.hpp" |
bigbiff bigbiff | f8e2f37 | 2013-02-27 20:50:43 -0500 | [diff] [blame] | 27 | #include "../data.hpp" |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 28 | extern "C" { |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 29 | #include "../minuitwrp/minui.h" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 30 | #include "../twcommon.h" |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 31 | } |
| 32 | #include "../twrp-functions.hpp" |
| 33 | #include "../variables.h" |
| 34 | |
| 35 | blanktimer::blanktimer(void) { |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 36 | pthread_mutex_init(&mutex, NULL); |
| 37 | setTime(0); // no timeout |
| 38 | state = kOn; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 39 | orig_brightness = getBrightness(); |
gordon1337 | 0d9133d | 2013-06-08 14:17:07 +0200 | [diff] [blame] | 40 | } |
| 41 | |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 42 | bool blanktimer::isScreenOff() { |
| 43 | return state >= kOff; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 44 | } |
| 45 | |
Dees_Troy | 2f9117a | 2013-02-17 19:52:09 -0600 | [diff] [blame] | 46 | void blanktimer::setTime(int newtime) { |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 47 | pthread_mutex_lock(&mutex); |
Dees_Troy | 2f9117a | 2013-02-17 19:52:09 -0600 | [diff] [blame] | 48 | sleepTimer = newtime; |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 49 | pthread_mutex_unlock(&mutex); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void blanktimer::setTimer(void) { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 53 | clock_gettime(CLOCK_MONOTONIC, &btimer); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 54 | } |
| 55 | |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 56 | void blanktimer::checkForTimeout() { |
| 57 | #ifndef TW_NO_SCREEN_TIMEOUT |
| 58 | pthread_mutex_lock(&mutex); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 59 | timespec curTime, diff; |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 60 | 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 bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 66 | } |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 67 | 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 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; |
Ethan Yonker | fc96650 | 2014-04-16 13:30:45 -0500 | [diff] [blame] | 84 | string brightness_path; |
| 85 | DataManager::GetValue("tw_brightness_file", brightness_path); |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 86 | if (brightness_path == "/nobrightness") |
| 87 | return brightness_path; |
| 88 | DataManager::GetValue("tw_brightness", result); |
| 89 | if (result == "") { |
| 90 | result = "255"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 91 | } |
| 92 | return result; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 93 | |
| 94 | } |
| 95 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 96 | void blanktimer::resetTimerAndUnblank(void) { |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 97 | #ifndef TW_NO_SCREEN_TIMEOUT |
| 98 | pthread_mutex_lock(&mutex); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 99 | setTimer(); |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 100 | switch (state) { |
| 101 | case kBlanked: |
bigbiff bigbiff | 8794036 | 2013-03-09 09:58:50 -0500 | [diff] [blame] | 102 | #ifndef TW_NO_SCREEN_BLANK |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 103 | if (gr_fb_blank(0) < 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 104 | LOGINFO("blanktimer::resetTimerAndUnblank failed to gr_fb_blank(0)\n"); |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 105 | break; |
| 106 | } |
bigbiff bigbiff | 8794036 | 2013-03-09 09:58:50 -0500 | [diff] [blame] | 107 | #endif |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 108 | // TODO: this is asymmetric with postscreenblank.sh - shouldn't it be under the next case label? |
Dees_Troy | 1c8d4fb | 2013-07-12 15:05:29 +0000 | [diff] [blame] | 109 | TWFunc::check_and_run_script("/sbin/postscreenunblank.sh", "unblank"); |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 110 | // No break here, we want to keep going |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 111 | case kOff: |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 112 | gui_forceRender(); |
| 113 | // No break here, we want to keep going |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 114 | case kDim: |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 115 | if (orig_brightness != "/nobrightness") |
| 116 | TWFunc::Set_Brightness(orig_brightness); |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 117 | state = kOn; |
| 118 | case kOn: |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 119 | break; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 120 | } |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 121 | pthread_mutex_unlock(&mutex); |
| 122 | #endif |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 123 | } |