blob: 7085557e865c027c2689cc04380a052cb4f09071 [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;
112 return atoi(results.c_str());
113
114}
115
116int blanktimer::setBrightness(int brightness) {
117 string brightness_path = EXPAND(TW_BRIGHTNESS_PATH);
118 string bstring;
119 char buff[100];
120 sprintf(buff, "%d", brightness);
121 bstring = buff;
122 if ((TWFunc::write_file(brightness_path, bstring)) != 0)
123 return -1;
124 return 0;
125}
126
127void blanktimer::resetTimerAndUnblank(void) {
128 setTimer();
Dees_Troy70237dc2013-02-28 21:31:48 +0000129 switch (conblank) {
130 case 3:
bigbiff bigbiff87940362013-03-09 09:58:50 -0500131#ifndef TW_NO_SCREEN_BLANK
Dees_Troy70237dc2013-02-28 21:31:48 +0000132 if (gr_fb_blank(0) < 0) {
133 LOGI("blanktimer::resetTimerAndUnblank failed to gr_fb_blank(0)\n");
134 break;
135 }
bigbiff bigbiff87940362013-03-09 09:58:50 -0500136#endif
Dees_Troy70237dc2013-02-28 21:31:48 +0000137 // No break here, we want to keep going
138 case 2:
139 gui_forceRender();
140 // No break here, we want to keep going
141 case 1:
142 setBrightness(orig_brightness);
143 setConBlank(0);
144 break;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500145 }
146}