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" { |
| 42 | #include "../common.h" |
| 43 | #include "../recovery_ui.h" |
| 44 | } |
| 45 | #include "../twrp-functions.hpp" |
| 46 | #include "../variables.h" |
| 47 | |
| 48 | blanktimer::blanktimer(void) { |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 49 | setTime(0); |
| 50 | setConBlank(0); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 51 | orig_brightness = getBrightness(); |
| 52 | } |
| 53 | |
Dees_Troy | 2f9117a | 2013-02-17 19:52:09 -0600 | [diff] [blame] | 54 | void blanktimer::setTime(int newtime) { |
| 55 | sleepTimer = newtime; |
| 56 | } |
| 57 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 58 | int blanktimer::setTimerThread(void) { |
| 59 | pthread_t thread; |
| 60 | ThreadPtr blankptr = &blanktimer::setClockTimer; |
| 61 | PThreadPtr p = *(PThreadPtr*)&blankptr; |
| 62 | pthread_create(&thread, NULL, p, this); |
| 63 | return 0; |
| 64 | } |
| 65 | |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 66 | void blanktimer::setConBlank(int blank) { |
| 67 | pthread_mutex_lock(&conblankmutex); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 68 | conblank = blank; |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 69 | pthread_mutex_unlock(&conblankmutex); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void blanktimer::setTimer(void) { |
| 73 | pthread_mutex_lock(&timermutex); |
| 74 | clock_gettime(CLOCK_MONOTONIC, &btimer); |
| 75 | pthread_mutex_unlock(&timermutex); |
| 76 | } |
| 77 | |
| 78 | timespec blanktimer::getTimer(void) { |
| 79 | return btimer; |
| 80 | } |
| 81 | |
| 82 | int blanktimer::setClockTimer(void) { |
| 83 | timespec curTime, diff; |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 84 | for(;;) { |
| 85 | usleep(1000000); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 86 | clock_gettime(CLOCK_MONOTONIC, &curTime); |
| 87 | diff = TWFunc::timespec_diff(btimer, curTime); |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 88 | if (sleepTimer > 2 && diff.tv_sec > (sleepTimer - 2) && conblank == 0) { |
Dees_Troy | 2f9117a | 2013-02-17 19:52:09 -0600 | [diff] [blame] | 89 | orig_brightness = getBrightness(); |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 90 | setConBlank(1); |
| 91 | setBrightness(5); |
| 92 | } |
| 93 | if (sleepTimer && diff.tv_sec > sleepTimer && conblank < 2) { |
| 94 | setConBlank(2); |
| 95 | setBrightness(0); |
Dees_Troy | 2f9117a | 2013-02-17 19:52:09 -0600 | [diff] [blame] | 96 | PageManager::ChangeOverlay("lock"); |
| 97 | } |
bigbiff bigbiff | 8794036 | 2013-03-09 09:58:50 -0500 | [diff] [blame] | 98 | #ifndef TW_NO_SCREEN_BLANK |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 99 | if (conblank == 2 && gr_fb_blank(1) >= 0) { |
| 100 | setConBlank(3); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 101 | } |
bigbiff bigbiff | 8794036 | 2013-03-09 09:58:50 -0500 | [diff] [blame] | 102 | #endif |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 103 | } |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 104 | return -1; //shouldn't get here |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | int blanktimer::getBrightness(void) { |
| 108 | string results; |
| 109 | string brightness_path = EXPAND(TW_BRIGHTNESS_PATH); |
| 110 | if ((TWFunc::read_file(brightness_path, results)) != 0) |
| 111 | return -1; |
| 112 | return atoi(results.c_str()); |
| 113 | |
| 114 | } |
| 115 | |
| 116 | int blanktimer::setBrightness(int brightness) { |
| 117 | string brightness_path = EXPAND(TW_BRIGHTNESS_PATH); |
| 118 | string bstring; |
| 119 | char buff[100]; |
| 120 | sprintf(buff, "%d", brightness); |
| 121 | bstring = buff; |
| 122 | if ((TWFunc::write_file(brightness_path, bstring)) != 0) |
| 123 | return -1; |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | void blanktimer::resetTimerAndUnblank(void) { |
| 128 | setTimer(); |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 129 | switch (conblank) { |
| 130 | case 3: |
bigbiff bigbiff | 8794036 | 2013-03-09 09:58:50 -0500 | [diff] [blame] | 131 | #ifndef TW_NO_SCREEN_BLANK |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 132 | if (gr_fb_blank(0) < 0) { |
| 133 | LOGI("blanktimer::resetTimerAndUnblank failed to gr_fb_blank(0)\n"); |
| 134 | break; |
| 135 | } |
bigbiff bigbiff | 8794036 | 2013-03-09 09:58:50 -0500 | [diff] [blame] | 136 | #endif |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 137 | // No break here, we want to keep going |
| 138 | case 2: |
| 139 | gui_forceRender(); |
| 140 | // No break here, we want to keep going |
| 141 | case 1: |
| 142 | setBrightness(orig_brightness); |
| 143 | setConBlank(0); |
| 144 | break; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 145 | } |
| 146 | } |