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" |
| 40 | extern "C" { |
| 41 | #include "../common.h" |
| 42 | #include "../recovery_ui.h" |
| 43 | } |
| 44 | #include "../twrp-functions.hpp" |
| 45 | #include "../variables.h" |
| 46 | |
| 47 | blanktimer::blanktimer(void) { |
| 48 | blanked = 0; |
| 49 | sleepTimer = 60; |
| 50 | orig_brightness = getBrightness(); |
| 51 | } |
| 52 | |
Dees_Troy | 2f9117a | 2013-02-17 19:52:09 -0600 | [diff] [blame] | 53 | void blanktimer::setTime(int newtime) { |
| 54 | sleepTimer = newtime; |
| 55 | } |
| 56 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 57 | int blanktimer::setTimerThread(void) { |
| 58 | pthread_t thread; |
| 59 | ThreadPtr blankptr = &blanktimer::setClockTimer; |
| 60 | PThreadPtr p = *(PThreadPtr*)&blankptr; |
| 61 | pthread_create(&thread, NULL, p, this); |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | void blanktimer::setBlank(int blank) { |
| 66 | pthread_mutex_lock(&blankmutex); |
| 67 | conblank = blank; |
| 68 | pthread_mutex_unlock(&blankmutex); |
| 69 | } |
| 70 | |
| 71 | int blanktimer::getBlank(void) { |
| 72 | return conblank; |
| 73 | } |
| 74 | |
| 75 | void blanktimer::setTimer(void) { |
| 76 | pthread_mutex_lock(&timermutex); |
| 77 | clock_gettime(CLOCK_MONOTONIC, &btimer); |
| 78 | pthread_mutex_unlock(&timermutex); |
| 79 | } |
| 80 | |
| 81 | timespec blanktimer::getTimer(void) { |
| 82 | return btimer; |
| 83 | } |
| 84 | |
| 85 | int blanktimer::setClockTimer(void) { |
| 86 | timespec curTime, diff; |
| 87 | while(1) { |
| 88 | usleep(1000); |
| 89 | clock_gettime(CLOCK_MONOTONIC, &curTime); |
| 90 | diff = TWFunc::timespec_diff(btimer, curTime); |
Dees_Troy | 2f9117a | 2013-02-17 19:52:09 -0600 | [diff] [blame] | 91 | if (sleepTimer && diff.tv_sec > sleepTimer && conblank != 1) { |
| 92 | orig_brightness = getBrightness(); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 93 | setBlank(1); |
Dees_Troy | 2f9117a | 2013-02-17 19:52:09 -0600 | [diff] [blame] | 94 | PageManager::ChangeOverlay("lock"); |
| 95 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 96 | if (conblank == 1 && blanked != 1) { |
| 97 | blanked = 1; |
| 98 | gr_fb_blank(conblank); |
| 99 | setBrightness(0); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | return -1; |
| 103 | } |
| 104 | |
| 105 | int blanktimer::getBrightness(void) { |
| 106 | string results; |
| 107 | string brightness_path = EXPAND(TW_BRIGHTNESS_PATH); |
| 108 | if ((TWFunc::read_file(brightness_path, results)) != 0) |
| 109 | return -1; |
| 110 | return atoi(results.c_str()); |
| 111 | |
| 112 | } |
| 113 | |
| 114 | int blanktimer::setBrightness(int brightness) { |
| 115 | string brightness_path = EXPAND(TW_BRIGHTNESS_PATH); |
| 116 | string bstring; |
| 117 | char buff[100]; |
| 118 | sprintf(buff, "%d", brightness); |
| 119 | bstring = buff; |
| 120 | if ((TWFunc::write_file(brightness_path, bstring)) != 0) |
| 121 | return -1; |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | void blanktimer::resetTimerAndUnblank(void) { |
| 126 | setTimer(); |
| 127 | if (blanked) { |
| 128 | setBrightness(orig_brightness); |
| 129 | blanked = 0; |
| 130 | setBlank(0); |
| 131 | gr_fb_blank(conblank); |
| 132 | gui_forceRender(); |
| 133 | } |
| 134 | } |