-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathidletimer.cpp
More file actions
90 lines (77 loc) · 2.11 KB
/
Copy pathidletimer.cpp
File metadata and controls
90 lines (77 loc) · 2.11 KB
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
#include "idletimer.h"
#ifdef AGGRESSIVE_MODE_SUPPORTED
// Qt
#include <QSettings>
#include <QDebug>
// Headers for getting time of idle
#if defined(Q_OS_WIN32)
#include <windows.h>
#elif defined(Q_OS_LINUX)
#include <ctime>
#include <cstdio>
#include <unistd.h>
#include <X11/extensions/scrnsaver.h>
#endif
#define IDLE_TIMER_DEFAULT 10 // sec
IdleTimer::IdleTimer(QObject* parent) : QObject(parent)
{
const QSettings settings;
m_interval = settings.value("idleTimer", IDLE_TIMER_DEFAULT).toInt();
m_timer = new CountdownTimer(TimerType::seconds, m_interval, this);
connect(m_timer, SIGNAL(timeout()), this, SLOT(stopTimer()));
connect(m_timer, SIGNAL(tick(int)), this, SLOT(sendTick(int)));
}
void IdleTimer::start()
{
m_timer->start();
emit tick(m_interval);
qDebug() << "IDLETIMER: Idletimer has started";
}
/* Returns the idle time in seconds */
int IdleTimer::getIdleTime()
{
#if defined(Q_OS_WIN32)
LASTINPUTINFO li;
li.cbSize = sizeof(LASTINPUTINFO);
GetLastInputInfo(&li);
DWORD te = GetTickCount();
int t = (te - li.dwTime) / 1000;
return t;
#elif defined(Q_OS_LINUX) // Cred to https://stackoverflow.com/a/4702411
static XScreenSaverInfo* mitInfo;
Display* display = XOpenDisplay(nullptr);
if (display == nullptr)
{
return -1;
}
const int screen = DefaultScreen(display);
mitInfo = XScreenSaverAllocInfo();
XScreenSaverQueryInfo(display, RootWindow(display, screen), mitInfo);
const time_t idleTime = (mitInfo->idle) / 1000;
XFree(mitInfo);
XCloseDisplay(display);
return static_cast<int>(idleTime);
#endif
}
/* Stops the timer */
void IdleTimer::stopTimer()
{
m_timer->stop();
qDebug() << "IDLETIMER: Idletimer has been stopped";
}
/* Sends sends tick from countdown timer
* or reset timer if the computer isn't left alone. */
void IdleTimer::sendTick(const int countDown)
{
const int timeLeft = m_interval - getIdleTime();
if (countDown < timeLeft)
{
m_timer->start();
emit tick(m_interval);
}
else
{
emit tick(countDown);
}
}
#endif // AGGRESSIVE_MODE_SUPPORTED