This repository was archived by the owner on Oct 2, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpowersaveblocker.cpp
More file actions
62 lines (53 loc) · 1.36 KB
/
powersaveblocker.cpp
File metadata and controls
62 lines (53 loc) · 1.36 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
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright (C) 2020 Raspberry Pi (Trading) Limited
*/
#include "powersaveblocker.h"
#include <QDebug>
#include <string>
PowerSaveBlocker::PowerSaveBlocker(QObject *parent) : QObject(parent)
{
}
PowerSaveBlocker::~PowerSaveBlocker()
{
if (_stayingAwake)
{
removeBlock();
}
}
void PowerSaveBlocker::applyBlock(const QString &reason) const
{
if (_stayingAwake)
{
return;
}
#ifdef Q_OS_WIN
REASON_CONTEXT rc;
std::wstring wreason = reason.toStdWString();
rc.Version = POWER_REQUEST_CONTEXT_VERSION;
rc.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING;
rc.Reason.SimpleReasonString = (wchar_t *)wreason.c_str();
_powerRequest = PowerCreateRequest(&rc);
if (_powerRequest == INVALID_HANDLE_VALUE)
{
qDebug() << "Error creating power request:" << GetLastError();
return;
}
_stayingAwake = PowerSetRequest(_powerRequest, PowerRequestDisplayRequired);
if (!_stayingAwake)
{
qDebug() << "Error running PowerSetRequest():" << GetLastError();
}
#endif
}
void PowerSaveBlocker::removeBlock() const
{
if (!_stayingAwake)
{
return;
}
#ifdef Q_OS_WIN
PowerClearRequest(_powerRequest, PowerRequestDisplayRequired);
CloseHandle(_powerRequest);
#endif
}