-
Notifications
You must be signed in to change notification settings - Fork 2
/
customeventfilter.cpp
103 lines (92 loc) · 2.53 KB
/
customeventfilter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "customeventfilter.h"
#include <QEvent>
#include <QFile>
#include <QDebug>
const QString filename = "/sys/class/backlight/rpi_backlight/brightness";
QFile file(filename);
CustomEventFilter::CustomEventFilter(QObject *parent) :
QObject(parent)
{
DEBUG = false;
TIMEOUT = 60000;
MIN_BRIGHTNESS = 12;
MAX_BRIGHTNESS = 200;
isSleeping = false;
lastBrightness = MAX_BRIGHTNESS;
inactivityTimer.setInterval(TIMEOUT);
connect(&inactivityTimer,SIGNAL(timeout()),this,SLOT(goToSleep()));
inactivityTimer.start();
}
bool CustomEventFilter::eventFilter(QObject *obj, QEvent *ev)
{
if(ev->type() == QEvent::KeyPress || ev->type() == QEvent::MouseMove){
isSleeping = false;
inactivityTimer.stop();
inactivityTimer.start();
this->adjustBrightness(MAX_BRIGHTNESS);
}
return QObject::eventFilter(obj, ev);
}
void CustomEventFilter::goToSleep()
{
isSleeping = true;
this->adjustBrightness(MIN_BRIGHTNESS);
}
void CustomEventFilter::adjustBrightness(int brightness)
{
if(isSleeping == true){
if(brightness != lastBrightness){
lastBrightness = MIN_BRIGHTNESS;
this->commitBrightness(MIN_BRIGHTNESS);
this->debug(QString("Sleep."));
}
}else if(isSleeping == false){
if(brightness != lastBrightness){
lastBrightness = MAX_BRIGHTNESS;
this->commitBrightness(MAX_BRIGHTNESS);
this->debug(QString("Wake up."));
}
}
}
void CustomEventFilter::commitBrightness(int brightness)
{
if (file.open(QIODevice::ReadWrite))
{
QTextStream stream( &file );
stream << brightness << endl;
}else{
this->debug(QString("Unable to open file."));
}
file.close();
}
void CustomEventFilter::setTimeOut(int microseconds)
{
this->TIMEOUT = microseconds;
this->inactivityTimer.setInterval(this->TIMEOUT);
}
void CustomEventFilter::setMinBrightness(int brightness)
{
if(brightness < 0 && brightness > 255){
this->debug("Brightness out or range (0-255.");
return;
}
this->MIN_BRIGHTNESS = brightness;
}
void CustomEventFilter::setMaxBrightness(int brightness)
{
if(brightness < 0 && brightness > 255){
this->debug("Brightness out or range (0-255.");
return;
}
this->MAX_BRIGHTNESS = brightness;
}
void CustomEventFilter::setDebug(bool status)
{
this->DEBUG = status;
}
void CustomEventFilter::debug(QString message)
{
if(DEBUG){
qDebug() << message;
}
}