blob: d7a6ffffc6b487c85980847342d1b2e897c01705 [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" {
42#include "../common.h"
43#include "../recovery_ui.h"
44}
45#include "../twrp-functions.hpp"
46#include "../variables.h"
47
48blanktimer::blanktimer(void) {
Dees_Troy70237dc2013-02-28 21:31:48 +000049 setTime(0);
50 setConBlank(0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050051 orig_brightness = getBrightness();
52}
53
Dees_Troy2f9117a2013-02-17 19:52:09 -060054void blanktimer::setTime(int newtime) {
55 sleepTimer = newtime;
56}
57
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050058int 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_Troy70237dc2013-02-28 21:31:48 +000066void blanktimer::setConBlank(int blank) {
67 pthread_mutex_lock(&conblankmutex);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050068 conblank = blank;
Dees_Troy70237dc2013-02-28 21:31:48 +000069 pthread_mutex_unlock(&conblankmutex);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050070}
71
72void blanktimer::setTimer(void) {
73 pthread_mutex_lock(&timermutex);
74 clock_gettime(CLOCK_MONOTONIC, &btimer);
75 pthread_mutex_unlock(&timermutex);
76}
77
78timespec blanktimer::getTimer(void) {
79 return btimer;
80}
81
82int blanktimer::setClockTimer(void) {
83 timespec curTime, diff;
Dees_Troy70237dc2013-02-28 21:31:48 +000084 for(;;) {
85 usleep(1000000);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050086 clock_gettime(CLOCK_MONOTONIC, &curTime);
87 diff = TWFunc::timespec_diff(btimer, curTime);
Dees_Troy70237dc2013-02-28 21:31:48 +000088 if (sleepTimer > 2 && diff.tv_sec > (sleepTimer - 2) && conblank == 0) {
Dees_Troy2f9117a2013-02-17 19:52:09 -060089 orig_brightness = getBrightness();
Dees_Troy70237dc2013-02-28 21:31:48 +000090 setConBlank(1);
91 setBrightness(5);
92 }
93 if (sleepTimer && diff.tv_sec > sleepTimer && conblank < 2) {
94 setConBlank(2);
95 setBrightness(0);
Dees_Troy2f9117a2013-02-17 19:52:09 -060096 PageManager::ChangeOverlay("lock");
97 }
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 }
101 }
Dees_Troy70237dc2013-02-28 21:31:48 +0000102 return -1; //shouldn't get here
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500103}
104
105int 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
114int 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
125void blanktimer::resetTimerAndUnblank(void) {
126 setTimer();
Dees_Troy70237dc2013-02-28 21:31:48 +0000127 switch (conblank) {
128 case 3:
129 if (gr_fb_blank(0) < 0) {
130 LOGI("blanktimer::resetTimerAndUnblank failed to gr_fb_blank(0)\n");
131 break;
132 }
133 // No break here, we want to keep going
134 case 2:
135 gui_forceRender();
136 // No break here, we want to keep going
137 case 1:
138 setBrightness(orig_brightness);
139 setConBlank(0);
140 break;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500141 }
142}