-
Notifications
You must be signed in to change notification settings - Fork 0
/
lockstatewatcher.cpp
105 lines (95 loc) · 3.39 KB
/
lockstatewatcher.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
104
105
#include "lockstatewatcher.h"
#include <QDebug>
#include <QDateTime>
#include <algorithm>
#include <WtsApi32.h>
#include "logger.h"
LockStateWatcher::LockStateWatcher(const Settings &settings, QWidget *parent)
: QWidget(parent),
settings_(settings),
lock_state_buffer_{ false, false, false, false, false},
buffer_for_lock{ false, false, true, true, true},
buffer_for_unlock{ true, true, false, false, false}
{
lock_timer_.invalidate();
}
bool LockStateWatcher::isSessionLocked()
{
// taken from https://stackoverflow.com/questions/29326685/c-check-if-computer-is-locked/43055326#43055326
typedef BOOL( PASCAL * WTSQuerySessionInformation )( HANDLE hServer, DWORD SessionId, WTS_INFO_CLASS WTSInfoClass, LPTSTR* ppBuffer, DWORD* pBytesReturned );
typedef void ( PASCAL * WTSFreeMemory )( PVOID pMemory );
WTSINFOEXW * pInfo = nullptr;
WTS_INFO_CLASS wtsic = WTSSessionInfoEx;
bool bRet = false;
LPTSTR ppBuffer = nullptr;
DWORD dwBytesReturned = 0;
LONG dwFlags = 0;
WTSQuerySessionInformation pWTSQuerySessionInformation = nullptr;
WTSFreeMemory pWTSFreeMemory = nullptr;
HMODULE hLib = LoadLibraryW(L"wtsapi32.dll");
if (!hLib) {
return false;
}
pWTSQuerySessionInformation = reinterpret_cast<WTSQuerySessionInformation>(GetProcAddress(hLib, "WTSQuerySessionInformationW"));
if (pWTSQuerySessionInformation) {
pWTSFreeMemory = reinterpret_cast<WTSFreeMemory>(GetProcAddress(hLib, "WTSFreeMemory"));
if (pWTSFreeMemory != nullptr) {
DWORD dwSessionID = WTSGetActiveConsoleSessionId();
if (pWTSQuerySessionInformation( WTS_CURRENT_SERVER_HANDLE, dwSessionID, wtsic, &ppBuffer, &dwBytesReturned)) {
if (dwBytesReturned > 0) {
pInfo = reinterpret_cast<WTSINFOEXW*>(ppBuffer);
if (pInfo->Level == 1) {
dwFlags = pInfo->Data.WTSInfoExLevel1.SessionFlags;
}
if (dwFlags == WTS_SESSIONSTATE_LOCK) {
bRet = true;
}
}
pWTSFreeMemory(ppBuffer);
ppBuffer = nullptr;
}
}
}
if (hLib != nullptr) {
FreeLibrary(hLib);
}
return bRet;
}
LockEvent LockStateWatcher::determineLockEvent(bool session_locked)
{
lock_state_buffer_.push_back(session_locked);
lock_state_buffer_.pop_front();
if (lock_state_buffer_ == buffer_for_lock)
return LockEvent::Lock;
else if (lock_state_buffer_ == buffer_for_unlock)
return LockEvent::Unlock;
else
return LockEvent::None;
}
void LockStateWatcher::update()
{
const LockEvent lock_event = determineLockEvent(isSessionLocked());
if (lock_event == LockEvent::Lock) {
if (settings_.logToFile())
Logger::Log("[LOCK] >> Lock determined");
lock_timer_.start();
}
else if (lock_event == LockEvent::Unlock) {
if (settings_.logToFile() && lock_timer_.isValid())
Logger::Log("[LOCK] Current Lock Duration = " + QString::number(lock_timer_.elapsed()) + "ms");
lock_timer_.invalidate();
if (settings_.logToFile())
Logger::Log("[LOCK] Unlock determined <<");
if (settings_.isAutopauseEnabled())
emit desktopLockEvent(LockEvent::Unlock);
}
if (lock_timer_.isValid() && (lock_timer_.elapsed() >= settings_.getBackpauseMsec())) {
if (settings_.logToFile() && lock_timer_.isValid())
Logger::Log("[LOCK] Current Lock Duration = " + QString::number(lock_timer_.elapsed()) + "ms");
lock_timer_.invalidate();
if (settings_.logToFile())
Logger::Log("[LOCK] Ongoing Lock is long enough to be counted as a Pause");
if (settings_.isAutopauseEnabled())
emit desktopLockEvent(LockEvent::LongOngoingLock);
}
}