blob: 383b1572426b31beba41681ca15011753449cdfa [file] [log] [blame]
bigbiff bigbiff8a68c312013-02-10 14:28:30 -05001/*
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
19using namespace std;
20#include "rapidxml.hpp"
21using namespace rapidxml;
22extern "C" {
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050023#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 Troyb7ae0982013-09-10 20:47:35 +000039#include "objects.hpp"
bigbiff bigbifff8e2f372013-02-27 20:50:43 -050040#include "../data.hpp"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050041extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000042#include "../twcommon.h"
Dees Troyb7ae0982013-09-10 20:47:35 +000043#ifdef HAVE_SELINUX
44#include "../minzip/Zip.h"
45#else
46#include "../minzipold/Zip.h"
47#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050048}
49#include "../twrp-functions.hpp"
50#include "../variables.h"
51
52blanktimer::blanktimer(void) {
Dees_Troy70237dc2013-02-28 21:31:48 +000053 setTime(0);
54 setConBlank(0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050055 orig_brightness = getBrightness();
gordon13370d9133d2013-06-08 14:17:07 +020056 screenoff = false;
57}
58
59bool blanktimer::IsScreenOff() {
60 return screenoff;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050061}
62
Dees_Troy2f9117a2013-02-17 19:52:09 -060063void blanktimer::setTime(int newtime) {
64 sleepTimer = newtime;
65}
66
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050067int 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_Troy70237dc2013-02-28 21:31:48 +000075void blanktimer::setConBlank(int blank) {
76 pthread_mutex_lock(&conblankmutex);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050077 conblank = blank;
Dees_Troy70237dc2013-02-28 21:31:48 +000078 pthread_mutex_unlock(&conblankmutex);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050079}
80
81void blanktimer::setTimer(void) {
82 pthread_mutex_lock(&timermutex);
83 clock_gettime(CLOCK_MONOTONIC, &btimer);
84 pthread_mutex_unlock(&timermutex);
85}
86
87timespec blanktimer::getTimer(void) {
88 return btimer;
89}
90
91int blanktimer::setClockTimer(void) {
92 timespec curTime, diff;
Dees_Troy70237dc2013-02-28 21:31:48 +000093 for(;;) {
94 usleep(1000000);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050095 clock_gettime(CLOCK_MONOTONIC, &curTime);
96 diff = TWFunc::timespec_diff(btimer, curTime);
Dees_Troy70237dc2013-02-28 21:31:48 +000097 if (sleepTimer > 2 && diff.tv_sec > (sleepTimer - 2) && conblank == 0) {
Dees_Troy2f9117a2013-02-17 19:52:09 -060098 orig_brightness = getBrightness();
Dees_Troy70237dc2013-02-28 21:31:48 +000099 setConBlank(1);
100 setBrightness(5);
101 }
102 if (sleepTimer && diff.tv_sec > sleepTimer && conblank < 2) {
103 setConBlank(2);
104 setBrightness(0);
gordon13370d9133d2013-06-08 14:17:07 +0200105 screenoff = true;
Dees_Troy1c8d4fb2013-07-12 15:05:29 +0000106 TWFunc::check_and_run_script("/sbin/postscreenblank.sh", "blank");
Dees_Troy2f9117a2013-02-17 19:52:09 -0600107 PageManager::ChangeOverlay("lock");
108 }
bigbiff bigbiff87940362013-03-09 09:58:50 -0500109#ifndef TW_NO_SCREEN_BLANK
Dees_Troy70237dc2013-02-28 21:31:48 +0000110 if (conblank == 2 && gr_fb_blank(1) >= 0) {
111 setConBlank(3);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500112 }
bigbiff bigbiff87940362013-03-09 09:58:50 -0500113#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500114 }
Dees_Troy70237dc2013-02-28 21:31:48 +0000115 return -1; //shouldn't get here
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500116}
117
118int blanktimer::getBrightness(void) {
119 string results;
Ethan Yonkerfc966502014-04-16 13:30:45 -0500120 string brightness_path;
121 DataManager::GetValue("tw_brightness_file", brightness_path);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500122 if ((TWFunc::read_file(brightness_path, results)) != 0)
123 return -1;
Dees_Troya13d74f2013-03-24 08:54:55 -0500124 int result = atoi(results.c_str());
125 if (result == 0) {
126 int tw_brightness;
127 DataManager::GetValue("tw_brightness", tw_brightness);
128 if (tw_brightness) {
129 result = tw_brightness;
130 } else {
131 result = 255;
132 }
133 }
134 return result;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500135
136}
137
138int blanktimer::setBrightness(int brightness) {
Ethan Yonkerfc966502014-04-16 13:30:45 -0500139 string brightness_path;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500140 string bstring;
141 char buff[100];
Ethan Yonkerfc966502014-04-16 13:30:45 -0500142 DataManager::GetValue("tw_brightness_file", brightness_path);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500143 sprintf(buff, "%d", brightness);
144 bstring = buff;
145 if ((TWFunc::write_file(brightness_path, bstring)) != 0)
146 return -1;
Matt Mowerb53907e2013-12-17 13:45:51 -0600147 gui_forceRender();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500148 return 0;
149}
150
151void blanktimer::resetTimerAndUnblank(void) {
152 setTimer();
Dees_Troy70237dc2013-02-28 21:31:48 +0000153 switch (conblank) {
154 case 3:
bigbiff bigbiff87940362013-03-09 09:58:50 -0500155#ifndef TW_NO_SCREEN_BLANK
Dees_Troy70237dc2013-02-28 21:31:48 +0000156 if (gr_fb_blank(0) < 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000157 LOGINFO("blanktimer::resetTimerAndUnblank failed to gr_fb_blank(0)\n");
Dees_Troy70237dc2013-02-28 21:31:48 +0000158 break;
159 }
bigbiff bigbiff87940362013-03-09 09:58:50 -0500160#endif
Dees_Troy1c8d4fb2013-07-12 15:05:29 +0000161 TWFunc::check_and_run_script("/sbin/postscreenunblank.sh", "unblank");
Dees_Troy70237dc2013-02-28 21:31:48 +0000162 // No break here, we want to keep going
163 case 2:
164 gui_forceRender();
gordon13370d9133d2013-06-08 14:17:07 +0200165 screenoff = false;
Dees_Troy70237dc2013-02-28 21:31:48 +0000166 // No break here, we want to keep going
167 case 1:
168 setBrightness(orig_brightness);
169 setConBlank(0);
170 break;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500171 }
172}