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