blob: 700a9ad2729d2d8e98dc0cf81122d07bb1902d56 [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" {
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 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"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050043}
44#include "../twrp-functions.hpp"
45#include "../variables.h"
46
47blanktimer::blanktimer(void) {
Dees_Troy70237dc2013-02-28 21:31:48 +000048 setTime(0);
49 setConBlank(0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050050 orig_brightness = getBrightness();
51}
52
Dees_Troy2f9117a2013-02-17 19:52:09 -060053void blanktimer::setTime(int newtime) {
54 sleepTimer = newtime;
55}
56
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050057int 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
Dees_Troy70237dc2013-02-28 21:31:48 +000065void blanktimer::setConBlank(int blank) {
66 pthread_mutex_lock(&conblankmutex);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050067 conblank = blank;
Dees_Troy70237dc2013-02-28 21:31:48 +000068 pthread_mutex_unlock(&conblankmutex);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050069}
70
71void blanktimer::setTimer(void) {
72 pthread_mutex_lock(&timermutex);
73 clock_gettime(CLOCK_MONOTONIC, &btimer);
74 pthread_mutex_unlock(&timermutex);
75}
76
77timespec blanktimer::getTimer(void) {
78 return btimer;
79}
80
81int blanktimer::setClockTimer(void) {
82 timespec curTime, diff;
Dees_Troy70237dc2013-02-28 21:31:48 +000083 for(;;) {
84 usleep(1000000);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050085 clock_gettime(CLOCK_MONOTONIC, &curTime);
86 diff = TWFunc::timespec_diff(btimer, curTime);
Dees_Troy70237dc2013-02-28 21:31:48 +000087 if (sleepTimer > 2 && diff.tv_sec > (sleepTimer - 2) && conblank == 0) {
Dees_Troy2f9117a2013-02-17 19:52:09 -060088 orig_brightness = getBrightness();
Dees_Troy70237dc2013-02-28 21:31:48 +000089 setConBlank(1);
90 setBrightness(5);
91 }
92 if (sleepTimer && diff.tv_sec > sleepTimer && conblank < 2) {
93 setConBlank(2);
94 setBrightness(0);
Dees_Troy2f9117a2013-02-17 19:52:09 -060095 PageManager::ChangeOverlay("lock");
96 }
bigbiff bigbiff87940362013-03-09 09:58:50 -050097#ifndef TW_NO_SCREEN_BLANK
Dees_Troy70237dc2013-02-28 21:31:48 +000098 if (conblank == 2 && gr_fb_blank(1) >= 0) {
99 setConBlank(3);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500100 }
bigbiff bigbiff87940362013-03-09 09:58:50 -0500101#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500102 }
Dees_Troy70237dc2013-02-28 21:31:48 +0000103 return -1; //shouldn't get here
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500104}
105
106int blanktimer::getBrightness(void) {
107 string results;
108 string brightness_path = EXPAND(TW_BRIGHTNESS_PATH);
109 if ((TWFunc::read_file(brightness_path, results)) != 0)
110 return -1;
Dees_Troya13d74f2013-03-24 08:54:55 -0500111 int result = atoi(results.c_str());
112 if (result == 0) {
113 int tw_brightness;
114 DataManager::GetValue("tw_brightness", tw_brightness);
115 if (tw_brightness) {
116 result = tw_brightness;
117 } else {
118 result = 255;
119 }
120 }
121 return result;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500122
123}
124
125int blanktimer::setBrightness(int brightness) {
126 string brightness_path = EXPAND(TW_BRIGHTNESS_PATH);
127 string bstring;
128 char buff[100];
129 sprintf(buff, "%d", brightness);
130 bstring = buff;
131 if ((TWFunc::write_file(brightness_path, bstring)) != 0)
132 return -1;
133 return 0;
134}
135
136void blanktimer::resetTimerAndUnblank(void) {
137 setTimer();
Dees_Troy70237dc2013-02-28 21:31:48 +0000138 switch (conblank) {
139 case 3:
bigbiff bigbiff87940362013-03-09 09:58:50 -0500140#ifndef TW_NO_SCREEN_BLANK
Dees_Troy70237dc2013-02-28 21:31:48 +0000141 if (gr_fb_blank(0) < 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000142 LOGINFO("blanktimer::resetTimerAndUnblank failed to gr_fb_blank(0)\n");
Dees_Troy70237dc2013-02-28 21:31:48 +0000143 break;
144 }
bigbiff bigbiff87940362013-03-09 09:58:50 -0500145#endif
Dees_Troy70237dc2013-02-28 21:31:48 +0000146 // No break here, we want to keep going
147 case 2:
148 gui_forceRender();
149 // No break here, we want to keep going
150 case 1:
151 setBrightness(orig_brightness);
152 setConBlank(0);
153 break;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500154 }
155}