Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fb759d45 authored by that's avatar that
Browse files

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
parent f66324ff
Loading
Loading
Loading
Loading
+0 −3
Original line number Diff line number Diff line
@@ -73,9 +73,6 @@ map<string, string> 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;
+0 −8
Original line number Diff line number Diff line
@@ -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");
+40 −66
Original line number Diff line number Diff line
@@ -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) {
	if (sleepTimer > 2 && diff.tv_sec > (sleepTimer - 2) && state == kOn) {
		orig_brightness = getBrightness();
			setConBlank(1);
		state = kDim;
		TWFunc::Set_Brightness("5");
	}
		if (sleepTimer && diff.tv_sec > sleepTimer && conblank < 2) {
			setConBlank(2);
	if (sleepTimer && diff.tv_sec > sleepTimer && state < kOff) {
		state = kOff;
		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);
	if (state == kOff && gr_fb_blank(1) >= 0) {
		state = kBlanked;
	}
#endif
	}
	return -1; //shouldn't get here
	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
}
+14 −18
Original line number Diff line number Diff line
@@ -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 @@ 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;
+9 −34
Original line number Diff line number Diff line
@@ -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();
	{
		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))
Loading