From fb759d45f5c95e3184ffc9c35b57a0a463a6d1ab Mon Sep 17 00:00:00 2001 From: that Date: Sun, 11 Jan 2015 12:16:53 +0100 Subject: 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 --- data.cpp | 3 -- gui/action.cpp | 8 ---- gui/blanktimer.cpp | 106 ++++++++++++++++++++--------------------------------- gui/blanktimer.hpp | 32 +++++++--------- gui/gui.cpp | 43 +++++----------------- gui/pages.cpp | 9 +---- gui/pages.hpp | 12 ++++++ gui/resources.hpp | 8 ---- 8 files changed, 76 insertions(+), 145 deletions(-) diff --git a/data.cpp b/data.cpp index 3259a2132..d82d285fe 100644 --- a/data.cpp +++ b/data.cpp @@ -73,9 +73,6 @@ map DataManager::mConstValues; 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 953439906..a341e4a62 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 @@ extern "C" { #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 @@ void GUIAction::operation_end(const int operation_status) } 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 371d3f126..95b6c47ff 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 -#include -#include -#include "resources.hpp" #include #include #include #include -#include -#include -#include -#include #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"); - } + 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"); + } + 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 (conblank == 2 && gr_fb_blank(1) >= 0) { - setConBlank(3); - } -#endif + if (state == kOff && gr_fb_blank(1) >= 0) { + state = kBlanked; } - return -1; //shouldn't get here +#endif + pthread_mutex_unlock(&mutex); +#endif } string blanktimer::getBrightness(void) { @@ -125,25 +94,30 @@ string blanktimer::getBrightness(void) { } 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 c8159f643..5e617901f 100644 --- a/gui/blanktimer.hpp +++ b/gui/blanktimer.hpp @@ -19,7 +19,6 @@ #ifndef __BLANKTIMER_HEADER_HPP #define __BLANKTIMER_HEADER_HPP -#include #include using namespace std; @@ -27,32 +26,29 @@ using namespace std; class blanktimer { public: - blanktimer(void); + blanktimer(); - int setTimerThread(void); - void resetTimerAndUnblank(void); + // set timeout in seconds void setTime(int newtime); - bool IsScreenOff(); -private: - typedef int (blanktimer::*ThreadPtr)(void); - typedef void* (*PThreadPtr)(void*); + // call this in regular intervals + void checkForTimeout(); + + // call this when an input event is received or when an operation is finished + void resetTimerAndUnblank(); - void setConBlank(int blank); + bool isScreenOff(); + +private: 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 db6e23f84..edf2d7671 100644 --- a/gui/gui.cpp +++ b/gui/gui.cpp @@ -50,9 +50,7 @@ extern "C" #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 @@ pthread_mutex_t gForceRendermutex; 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 @@ void curtainClose() 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 @@ static void * input_thread(void *cookie) 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 @@ static void * input_thread(void *cookie) #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 @@ static void * input_thread(void *cookie) 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 @@ static void * input_thread(void *cookie) #endif gettimeofday(&touchStart, NULL); kb->KeyRepeat(); -#ifndef TW_NO_SCREEN_TIMEOUT blankTimer.resetTimerAndUnblank(); -#endif } } else if (ev.type == EV_ABS) @@ -279,9 +266,7 @@ static void * input_thread(void *cookie) 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 @@ static void * input_thread(void *cookie) key_repeat = 0; gettimeofday(&touchStart, NULL); } -#ifndef TW_NO_SCREEN_TIMEOUT blankTimer.resetTimerAndUnblank(); -#endif } else { @@ -320,9 +303,7 @@ static void * input_thread(void *cookie) 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 @@ static void * input_thread(void *cookie) 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 @@ static void * input_thread(void *cookie) 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 @@ static int runPages(void) flip(); } + blankTimer.checkForTimeout(); if (DataManager::GetIntValue("tw_gui_done") != 0) break; } @@ -665,6 +641,7 @@ static int runPage(const char *page_name) PageManager::Render(); flip(); } + blankTimer.checkForTimeout(); if (DataManager::GetIntValue("tw_page_done") != 0) { gui_changePage("main"); @@ -750,8 +727,6 @@ std::string gui_parse_text(string inText) 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 a1a6346a4..aae85aec9 100644 --- a/gui/pages.cpp +++ b/gui/pages.cpp @@ -45,14 +45,9 @@ extern "C" { #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 PageManager::mPageSets; PageSet* PageManager::mCurrentSet; @@ -1197,10 +1192,8 @@ void PageManager::LoadCursorData(xml_node<>* node) 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 f1179f53e..2ce3e13a4 100644 --- a/gui/pages.hpp +++ b/gui/pages.hpp @@ -4,6 +4,18 @@ #define _PAGES_HEADER_HPP #include "../minzip/Zip.h" +#include +#include +#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 f359c55dd..1a86d544d 100644 --- a/gui/resources.hpp +++ b/gui/resources.hpp @@ -23,14 +23,6 @@ protected: 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: -- cgit v1.2.3