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; |
| 20 | #include "rapidxml.hpp" |
| 21 | using namespace rapidxml; |
| 22 | extern "C" { |
| 23 | #include "../minzip/Zip.h" |
| 24 | #include "../minuitwrp/minui.h" |
| 25 | } |
| 26 | #include <string> |
| 27 | #include <vector> |
| 28 | #include <map> |
| 29 | #include "resources.hpp" |
| 30 | #include <pthread.h> |
| 31 | #include <sys/time.h> |
| 32 | #include <time.h> |
| 33 | #include <unistd.h> |
| 34 | #include <pixelflinger/pixelflinger.h> |
| 35 | #include <linux/kd.h> |
| 36 | #include <linux/fb.h> |
| 37 | #include <sstream> |
| 38 | #include "pages.hpp" |
| 39 | #include "blanktimer.hpp" |
bigbiff bigbiff | f8e2f37 | 2013-02-27 20:50:43 -0500 | [diff] [blame] | 40 | #include "../data.hpp" |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 41 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 42 | #include "../twcommon.h" |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 43 | } |
| 44 | #include "../twrp-functions.hpp" |
| 45 | #include "../variables.h" |
| 46 | |
| 47 | blanktimer::blanktimer(void) { |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 48 | setTime(0); |
| 49 | setConBlank(0); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 50 | orig_brightness = getBrightness(); |
gordon1337 | 0d9133d | 2013-06-08 14:17:07 +0200 | [diff] [blame] | 51 | screenoff = false; |
| 52 | } |
| 53 | |
| 54 | bool blanktimer::IsScreenOff() { |
| 55 | return screenoff; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 56 | } |
| 57 | |
Dees_Troy | 2f9117a | 2013-02-17 19:52:09 -0600 | [diff] [blame] | 58 | void blanktimer::setTime(int newtime) { |
| 59 | sleepTimer = newtime; |
| 60 | } |
| 61 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 62 | int blanktimer::setTimerThread(void) { |
| 63 | pthread_t thread; |
| 64 | ThreadPtr blankptr = &blanktimer::setClockTimer; |
| 65 | PThreadPtr p = *(PThreadPtr*)&blankptr; |
| 66 | pthread_create(&thread, NULL, p, this); |
| 67 | return 0; |
| 68 | } |
| 69 | |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 70 | void blanktimer::setConBlank(int blank) { |
| 71 | pthread_mutex_lock(&conblankmutex); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 72 | conblank = blank; |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 73 | pthread_mutex_unlock(&conblankmutex); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void blanktimer::setTimer(void) { |
| 77 | pthread_mutex_lock(&timermutex); |
| 78 | clock_gettime(CLOCK_MONOTONIC, &btimer); |
| 79 | pthread_mutex_unlock(&timermutex); |
| 80 | } |
| 81 | |
| 82 | timespec blanktimer::getTimer(void) { |
| 83 | return btimer; |
| 84 | } |
| 85 | |
| 86 | int blanktimer::setClockTimer(void) { |
| 87 | timespec curTime, diff; |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 88 | for(;;) { |
| 89 | usleep(1000000); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 90 | clock_gettime(CLOCK_MONOTONIC, &curTime); |
| 91 | diff = TWFunc::timespec_diff(btimer, curTime); |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 92 | if (sleepTimer > 2 && diff.tv_sec > (sleepTimer - 2) && conblank == 0) { |
Dees_Troy | 2f9117a | 2013-02-17 19:52:09 -0600 | [diff] [blame] | 93 | orig_brightness = getBrightness(); |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 94 | setConBlank(1); |
| 95 | setBrightness(5); |
| 96 | } |
| 97 | if (sleepTimer && diff.tv_sec > sleepTimer && conblank < 2) { |
| 98 | setConBlank(2); |
| 99 | setBrightness(0); |
gordon1337 | 0d9133d | 2013-06-08 14:17:07 +0200 | [diff] [blame] | 100 | screenoff = true; |
Dees_Troy | 1c8d4fb | 2013-07-12 15:05:29 +0000 | [diff] [blame] | 101 | TWFunc::check_and_run_script("/sbin/postscreenblank.sh", "blank"); |
Dees_Troy | 2f9117a | 2013-02-17 19:52:09 -0600 | [diff] [blame] | 102 | PageManager::ChangeOverlay("lock"); |
| 103 | } |
bigbiff bigbiff | 8794036 | 2013-03-09 09:58:50 -0500 | [diff] [blame] | 104 | #ifndef TW_NO_SCREEN_BLANK |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 105 | if (conblank == 2 && gr_fb_blank(1) >= 0) { |
| 106 | setConBlank(3); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 107 | } |
bigbiff bigbiff | 8794036 | 2013-03-09 09:58:50 -0500 | [diff] [blame] | 108 | #endif |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 109 | } |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 110 | return -1; //shouldn't get here |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | int blanktimer::getBrightness(void) { |
| 114 | string results; |
| 115 | string brightness_path = EXPAND(TW_BRIGHTNESS_PATH); |
| 116 | if ((TWFunc::read_file(brightness_path, results)) != 0) |
| 117 | return -1; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 118 | int result = atoi(results.c_str()); |
| 119 | if (result == 0) { |
| 120 | int tw_brightness; |
| 121 | DataManager::GetValue("tw_brightness", tw_brightness); |
| 122 | if (tw_brightness) { |
| 123 | result = tw_brightness; |
| 124 | } else { |
| 125 | result = 255; |
| 126 | } |
| 127 | } |
| 128 | return result; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 129 | |
| 130 | } |
| 131 | |
| 132 | int blanktimer::setBrightness(int brightness) { |
| 133 | string brightness_path = EXPAND(TW_BRIGHTNESS_PATH); |
| 134 | string bstring; |
| 135 | char buff[100]; |
| 136 | sprintf(buff, "%d", brightness); |
| 137 | bstring = buff; |
| 138 | if ((TWFunc::write_file(brightness_path, bstring)) != 0) |
| 139 | return -1; |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | void blanktimer::resetTimerAndUnblank(void) { |
| 144 | setTimer(); |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 145 | switch (conblank) { |
| 146 | case 3: |
bigbiff bigbiff | 8794036 | 2013-03-09 09:58:50 -0500 | [diff] [blame] | 147 | #ifndef TW_NO_SCREEN_BLANK |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 148 | if (gr_fb_blank(0) < 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 149 | LOGINFO("blanktimer::resetTimerAndUnblank failed to gr_fb_blank(0)\n"); |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 150 | break; |
| 151 | } |
bigbiff bigbiff | 8794036 | 2013-03-09 09:58:50 -0500 | [diff] [blame] | 152 | #endif |
Dees_Troy | 1c8d4fb | 2013-07-12 15:05:29 +0000 | [diff] [blame] | 153 | TWFunc::check_and_run_script("/sbin/postscreenunblank.sh", "unblank"); |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 154 | // No break here, we want to keep going |
| 155 | case 2: |
| 156 | gui_forceRender(); |
gordon1337 | 0d9133d | 2013-06-08 14:17:07 +0200 | [diff] [blame] | 157 | screenoff = false; |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 158 | // No break here, we want to keep going |
| 159 | case 1: |
| 160 | setBrightness(orig_brightness); |
| 161 | setConBlank(0); |
| 162 | break; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 163 | } |
| 164 | } |