blob: 17cc0b3d19ef4ea12cf5490ca9efc8af9cda2acd [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 }
bigbiff bigbiff87940362013-03-09 09:58:50 -050098#ifndef TW_NO_SCREEN_BLANK
Dees_Troy70237dc2013-02-28 21:31:48 +000099 if (conblank == 2 && gr_fb_blank(1) >= 0) {
100 setConBlank(3);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500101 }
bigbiff bigbiff87940362013-03-09 09:58:50 -0500102#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500103 }
Dees_Troy70237dc2013-02-28 21:31:48 +0000104 return -1; //shouldn't get here
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500105}
106
107int 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;
Dees_Troya13d74f2013-03-24 08:54:55 -0500112 int result = atoi(results.c_str());
113 if (result == 0) {
114 int tw_brightness;
115 DataManager::GetValue("tw_brightness", tw_brightness);
116 if (tw_brightness) {
117 result = tw_brightness;
118 } else {
119 result = 255;
120 }
121 }
122 return result;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500123
124}
125
126int blanktimer::setBrightness(int brightness) {
127 string brightness_path = EXPAND(TW_BRIGHTNESS_PATH);
128 string bstring;
129 char buff[100];
130 sprintf(buff, "%d", brightness);
131 bstring = buff;
132 if ((TWFunc::write_file(brightness_path, bstring)) != 0)
133 return -1;
134 return 0;
135}
136
137void blanktimer::resetTimerAndUnblank(void) {
138 setTimer();
Dees_Troy70237dc2013-02-28 21:31:48 +0000139 switch (conblank) {
140 case 3:
bigbiff bigbiff87940362013-03-09 09:58:50 -0500141#ifndef TW_NO_SCREEN_BLANK
Dees_Troy70237dc2013-02-28 21:31:48 +0000142 if (gr_fb_blank(0) < 0) {
143 LOGI("blanktimer::resetTimerAndUnblank failed to gr_fb_blank(0)\n");
144 break;
145 }
bigbiff bigbiff87940362013-03-09 09:58:50 -0500146#endif
Dees_Troy70237dc2013-02-28 21:31:48 +0000147 // No break here, we want to keep going
148 case 2:
149 gui_forceRender();
150 // No break here, we want to keep going
151 case 1:
152 setBrightness(orig_brightness);
153 setConBlank(0);
154 break;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500155 }
156}