gui: simplify blanktimer
- get rid of separate thread, check timer in rendering thread instead
- use an enum for the blanking state instead of magic integers
- move #ifdefs for TW_NO_SCREEN_TIMEOUT inside blanktimer class
- move some #includes and enum TOUCH_STATE to pages.hpp
Change-Id: Id4b104e3680dc5db41d8ba85e32d722cf4086299
diff --git a/data.cpp b/data.cpp
index 3259a21..d82d285 100644
--- a/data.cpp
+++ b/data.cpp
@@ -73,9 +73,6 @@
string DataManager::mBackingFile;
int DataManager::mInitialized = 0;
-#ifndef TW_NO_SCREEN_TIMEOUT
-extern blanktimer blankTimer;
-#endif
extern bool datamedia;
pthread_mutex_t DataManager::m_valuesLock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
diff --git a/gui/action.cpp b/gui/action.cpp
index 9534399..a341e4a 100644
--- a/gui/action.cpp
+++ b/gui/action.cpp
@@ -42,9 +42,7 @@
#include "../adb_install.h"
#include "../fuse_sideload.h"
-#ifndef TW_NO_SCREEN_TIMEOUT
#include "blanktimer.hpp"
-#endif
extern "C" {
#include "../twcommon.h"
#include "../minuitwrp/minui.h"
@@ -59,10 +57,6 @@
#include "rapidxml.hpp"
#include "objects.hpp"
-#ifndef TW_NO_SCREEN_TIMEOUT
-extern blanktimer blankTimer;
-#endif
-
void curtainClose(void);
GUIAction::mapFunc GUIAction::mf;
@@ -446,9 +440,7 @@
}
DataManager::SetValue("tw_operation_state", 1);
DataManager::SetValue(TW_ACTION_BUSY, 0);
-#ifndef TW_NO_SCREEN_TIMEOUT
blankTimer.resetTimerAndUnblank();
-#endif
time(&Stop);
if ((int) difftime(Stop, Start) > 10)
DataManager::Vibrate("tw_action_vibrate");
diff --git a/gui/blanktimer.cpp b/gui/blanktimer.cpp
index 371d3f1..95b6c47 100644
--- a/gui/blanktimer.cpp
+++ b/gui/blanktimer.cpp
@@ -17,97 +17,66 @@
*/
using namespace std;
-#include "rapidxml.hpp"
-using namespace rapidxml;
-extern "C" {
-#include "../minuitwrp/minui.h"
-}
#include <string>
-#include <vector>
-#include <map>
-#include "resources.hpp"
#include <pthread.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
-#include <pixelflinger/pixelflinger.h>
-#include <linux/kd.h>
-#include <linux/fb.h>
-#include <sstream>
#include "pages.hpp"
#include "blanktimer.hpp"
-#include "objects.hpp"
#include "../data.hpp"
extern "C" {
+#include "../minuitwrp/minui.h"
#include "../twcommon.h"
}
#include "../twrp-functions.hpp"
#include "../variables.h"
blanktimer::blanktimer(void) {
- setTime(0);
- setConBlank(0);
+ pthread_mutex_init(&mutex, NULL);
+ setTime(0); // no timeout
+ state = kOn;
orig_brightness = getBrightness();
- screenoff = false;
}
-bool blanktimer::IsScreenOff() {
- return screenoff;
+bool blanktimer::isScreenOff() {
+ return state >= kOff;
}
void blanktimer::setTime(int newtime) {
+ pthread_mutex_lock(&mutex);
sleepTimer = newtime;
-}
-
-int blanktimer::setTimerThread(void) {
- pthread_t thread;
- ThreadPtr blankptr = &blanktimer::setClockTimer;
- PThreadPtr p = *(PThreadPtr*)&blankptr;
- pthread_create(&thread, NULL, p, this);
- return 0;
-}
-
-void blanktimer::setConBlank(int blank) {
- pthread_mutex_lock(&conblankmutex);
- conblank = blank;
- pthread_mutex_unlock(&conblankmutex);
+ pthread_mutex_unlock(&mutex);
}
void blanktimer::setTimer(void) {
- pthread_mutex_lock(&timermutex);
clock_gettime(CLOCK_MONOTONIC, &btimer);
- pthread_mutex_unlock(&timermutex);
}
-timespec blanktimer::getTimer(void) {
- return btimer;
-}
-
-int blanktimer::setClockTimer(void) {
+void blanktimer::checkForTimeout() {
+#ifndef TW_NO_SCREEN_TIMEOUT
+ pthread_mutex_lock(&mutex);
timespec curTime, diff;
- for(;;) {
- usleep(1000000);
- clock_gettime(CLOCK_MONOTONIC, &curTime);
- diff = TWFunc::timespec_diff(btimer, curTime);
- if (sleepTimer > 2 && diff.tv_sec > (sleepTimer - 2) && conblank == 0) {
- orig_brightness = getBrightness();
- setConBlank(1);
- TWFunc::Set_Brightness("5");
- }
- if (sleepTimer && diff.tv_sec > sleepTimer && conblank < 2) {
- setConBlank(2);
- TWFunc::Set_Brightness("0");
- screenoff = true;
- TWFunc::check_and_run_script("/sbin/postscreenblank.sh", "blank");
- PageManager::ChangeOverlay("lock");
- }
-#ifndef TW_NO_SCREEN_BLANK
- if (conblank == 2 && gr_fb_blank(1) >= 0) {
- setConBlank(3);
- }
-#endif
+ clock_gettime(CLOCK_MONOTONIC, &curTime);
+ diff = TWFunc::timespec_diff(btimer, curTime);
+ if (sleepTimer > 2 && diff.tv_sec > (sleepTimer - 2) && state == kOn) {
+ orig_brightness = getBrightness();
+ state = kDim;
+ TWFunc::Set_Brightness("5");
}
- return -1; //shouldn't get here
+ if (sleepTimer && diff.tv_sec > sleepTimer && state < kOff) {
+ state = kOff;
+ TWFunc::Set_Brightness("0");
+ TWFunc::check_and_run_script("/sbin/postscreenblank.sh", "blank");
+ PageManager::ChangeOverlay("lock");
+ }
+#ifndef TW_NO_SCREEN_BLANK
+ if (state == kOff && gr_fb_blank(1) >= 0) {
+ state = kBlanked;
+ }
+#endif
+ pthread_mutex_unlock(&mutex);
+#endif
}
string blanktimer::getBrightness(void) {
@@ -125,25 +94,30 @@
}
void blanktimer::resetTimerAndUnblank(void) {
+#ifndef TW_NO_SCREEN_TIMEOUT
+ pthread_mutex_lock(&mutex);
setTimer();
- switch (conblank) {
- case 3:
+ switch (state) {
+ case kBlanked:
#ifndef TW_NO_SCREEN_BLANK
if (gr_fb_blank(0) < 0) {
LOGINFO("blanktimer::resetTimerAndUnblank failed to gr_fb_blank(0)\n");
break;
}
#endif
+ // TODO: this is asymmetric with postscreenblank.sh - shouldn't it be under the next case label?
TWFunc::check_and_run_script("/sbin/postscreenunblank.sh", "unblank");
// No break here, we want to keep going
- case 2:
+ case kOff:
gui_forceRender();
- screenoff = false;
// No break here, we want to keep going
- case 1:
+ case kDim:
if (orig_brightness != "/nobrightness")
TWFunc::Set_Brightness(orig_brightness);
- setConBlank(0);
+ state = kOn;
+ case kOn:
break;
}
+ pthread_mutex_unlock(&mutex);
+#endif
}
diff --git a/gui/blanktimer.hpp b/gui/blanktimer.hpp
index c8159f6..5e61790 100644
--- a/gui/blanktimer.hpp
+++ b/gui/blanktimer.hpp
@@ -19,7 +19,6 @@
#ifndef __BLANKTIMER_HEADER_HPP
#define __BLANKTIMER_HEADER_HPP
-#include <pthread.h>
#include <sys/time.h>
using namespace std;
@@ -27,32 +26,29 @@
class blanktimer
{
public:
- blanktimer(void);
+ blanktimer();
- int setTimerThread(void);
- void resetTimerAndUnblank(void);
+ // set timeout in seconds
void setTime(int newtime);
- bool IsScreenOff();
+
+ // call this in regular intervals
+ void checkForTimeout();
+
+ // call this when an input event is received or when an operation is finished
+ void resetTimerAndUnblank();
+
+ bool isScreenOff();
private:
- typedef int (blanktimer::*ThreadPtr)(void);
- typedef void* (*PThreadPtr)(void*);
-
- void setConBlank(int blank);
void setTimer(void);
- timespec getTimer(void);
string getBrightness(void);
- int setBrightness(int brightness);
- int setBlankTimer(void);
- int setClockTimer(void);
- pthread_mutex_t conblankmutex;
- pthread_mutex_t timermutex;
- int conblank;
+ pthread_mutex_t mutex;
+ enum State { kOn = 0, kDim = 1, kOff = 2, kBlanked = 3 };
+ State state;
timespec btimer;
- unsigned long long sleepTimer;
+ long sleepTimer;
string orig_brightness;
- bool screenoff;
};
extern blanktimer blankTimer;
diff --git a/gui/gui.cpp b/gui/gui.cpp
index db6e23f..edf2d76 100644
--- a/gui/gui.cpp
+++ b/gui/gui.cpp
@@ -50,9 +50,7 @@
#include "../twrp-functions.hpp"
#include "../openrecoveryscript.hpp"
#include "../orscmd/orscmd.h"
-#ifndef TW_NO_SCREEN_TIMEOUT
#include "blanktimer.hpp"
-#endif
// Enable to print render time of each frame to the log file
//#define PRINT_RENDER_TIME 1
@@ -71,9 +69,7 @@
static int gNoAnimation = 1;
static int gGuiInputRunning = 0;
static int gCmdLineRunning = 0;
-#ifndef TW_NO_SCREEN_TIMEOUT
blanktimer blankTimer;
-#endif
// Needed by pages.cpp too
int gGuiRunning = 0;
@@ -178,24 +174,23 @@
static void * input_thread(void *cookie)
{
-
int drag = 0;
static int touch_and_hold = 0, dontwait = 0;
static int touch_repeat = 0, key_repeat = 0;
static int x = 0, y = 0;
- static int lshift = 0, rshift = 0;
static struct timeval touchStart;
- string seconds;
HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
MouseCursor *cursor = PageManager::GetMouseCursor();
#ifndef TW_NO_SCREEN_TIMEOUT
- //start screen timeout threads
- blankTimer.setTimerThread();
- DataManager::GetValue("tw_screen_timeout_secs", seconds);
- blankTimer.setTime(atoi(seconds.c_str()));
+ {
+ string seconds;
+ DataManager::GetValue("tw_screen_timeout_secs", seconds);
+ blankTimer.setTime(atoi(seconds.c_str()));
+ blankTimer.resetTimerAndUnblank();
+ }
#else
- LOGINFO("Skipping screen timeout threads: TW_NO_SCREEN_TIMEOUT is set\n");
+ LOGINFO("Skipping screen timeout: TW_NO_SCREEN_TIMEOUT is set\n");
#endif
for (;;)
@@ -225,9 +220,7 @@
LOGERR("TOUCH_HOLD: %d,%d\n", x, y);
#endif
PageManager::NotifyTouch(TOUCH_HOLD, x, y);
-#ifndef TW_NO_SCREEN_TIMEOUT
blankTimer.resetTimerAndUnblank();
-#endif
}
else if (touch_repeat && mtime > 100)
{
@@ -236,9 +229,7 @@
#endif
gettimeofday(&touchStart, NULL);
PageManager::NotifyTouch(TOUCH_REPEAT, x, y);
-#ifndef TW_NO_SCREEN_TIMEOUT
blankTimer.resetTimerAndUnblank();
-#endif
}
else if (key_repeat == 1 && mtime > 500)
{
@@ -248,9 +239,7 @@
gettimeofday(&touchStart, NULL);
key_repeat = 2;
kb->KeyRepeat();
-#ifndef TW_NO_SCREEN_TIMEOUT
blankTimer.resetTimerAndUnblank();
-#endif
}
else if (key_repeat == 2 && mtime > 100)
@@ -260,9 +249,7 @@
#endif
gettimeofday(&touchStart, NULL);
kb->KeyRepeat();
-#ifndef TW_NO_SCREEN_TIMEOUT
blankTimer.resetTimerAndUnblank();
-#endif
}
}
else if (ev.type == EV_ABS)
@@ -279,9 +266,7 @@
LOGERR("TOUCH_RELEASE: %d,%d\n", x, y);
#endif
PageManager::NotifyTouch(TOUCH_RELEASE, x, y);
-#ifndef TW_NO_SCREEN_TIMEOUT
blankTimer.resetTimerAndUnblank();
-#endif
touch_and_hold = 0;
touch_repeat = 0;
if (!key_repeat)
@@ -306,9 +291,7 @@
key_repeat = 0;
gettimeofday(&touchStart, NULL);
}
-#ifndef TW_NO_SCREEN_TIMEOUT
blankTimer.resetTimerAndUnblank();
-#endif
}
else
{
@@ -320,9 +303,7 @@
if (PageManager::NotifyTouch(TOUCH_DRAG, x, y) > 0)
state = 1;
key_repeat = 0;
-#ifndef TW_NO_SCREEN_TIMEOUT
blankTimer.resetTimerAndUnblank();
-#endif
}
}
}
@@ -384,17 +365,13 @@
touch_repeat = 0;
dontwait = 1;
gettimeofday(&touchStart, NULL);
-#ifndef TW_NO_SCREEN_TIMEOUT
blankTimer.resetTimerAndUnblank();
-#endif
} else {
key_repeat = 0;
touch_and_hold = 0;
touch_repeat = 0;
dontwait = 0;
-#ifndef TW_NO_SCREEN_TIMEOUT
blankTimer.resetTimerAndUnblank();
-#endif
}
} else {
// This is a key release
@@ -403,9 +380,7 @@
touch_and_hold = 0;
touch_repeat = 0;
dontwait = 0;
-#ifndef TW_NO_SCREEN_TIMEOUT
blankTimer.resetTimerAndUnblank();
-#endif
}
}
else if(ev.type == EV_REL)
@@ -615,6 +590,7 @@
flip();
}
+ blankTimer.checkForTimeout();
if (DataManager::GetIntValue("tw_gui_done") != 0)
break;
}
@@ -665,6 +641,7 @@
PageManager::Render();
flip();
}
+ blankTimer.checkForTimeout();
if (DataManager::GetIntValue("tw_page_done") != 0)
{
gui_changePage("main");
@@ -750,8 +727,6 @@
extern "C" int gui_init(void)
{
- int fd;
-
gr_init();
if (res_create_surface("/res/images/curtain.jpg", &gCurtain))
diff --git a/gui/pages.cpp b/gui/pages.cpp
index a1a6346..aae85ae 100644
--- a/gui/pages.cpp
+++ b/gui/pages.cpp
@@ -45,14 +45,9 @@
#include "rapidxml.hpp"
#include "objects.hpp"
-#ifndef TW_NO_SCREEN_TIMEOUT
#include "blanktimer.hpp"
-#endif
extern int gGuiRunning;
-#ifndef TW_NO_SCREEN_TIMEOUT
-extern blanktimer blankTimer;
-#endif
std::map<std::string, PageSet*> PageManager::mPageSets;
PageSet* PageManager::mCurrentSet;
@@ -1197,10 +1192,8 @@
int PageManager::Update(void)
{
-#ifndef TW_NO_SCREEN_TIMEOUT
- if(blankTimer.IsScreenOff())
+ if(blankTimer.isScreenOff())
return 0;
-#endif
int res = (mCurrentSet ? mCurrentSet->Update() : -1);
diff --git a/gui/pages.hpp b/gui/pages.hpp
index f1179f5..2ce3e13 100644
--- a/gui/pages.hpp
+++ b/gui/pages.hpp
@@ -4,6 +4,18 @@
#define _PAGES_HEADER_HPP
#include "../minzip/Zip.h"
+#include <vector>
+#include <map>
+#include "rapidxml.hpp"
+using namespace rapidxml;
+
+enum TOUCH_STATE {
+ TOUCH_START = 0,
+ TOUCH_DRAG = 1,
+ TOUCH_RELEASE = 2,
+ TOUCH_HOLD = 3,
+ TOUCH_REPEAT = 4
+};
typedef struct {
unsigned char red;
diff --git a/gui/resources.hpp b/gui/resources.hpp
index f359c55..1a86d54 100644
--- a/gui/resources.hpp
+++ b/gui/resources.hpp
@@ -23,14 +23,6 @@
static int ExtractResource(ZipArchive* pZip, std::string folderName, std::string fileName, std::string fileExtn, std::string destFile);
};
-typedef enum {
- TOUCH_START = 0,
- TOUCH_DRAG = 1,
- TOUCH_RELEASE = 2,
- TOUCH_HOLD = 3,
- TOUCH_REPEAT = 4
-} TOUCH_STATE;
-
class FontResource : public Resource
{
public: