-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpccComputerPower.h
174 lines (139 loc) · 6.15 KB
/
cpccComputerPower.h
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* *****************************************
* File: core.cpccOS.h
* Purpose: Portable (cross-platform), light-weight, OS functions
* *****************************************
* Library: Cross Platform C++ Classes (cpcc)
* Copyright: 2015 StarMessage software.
* License: Free for opensource projects.
* Commercial license exists for closed source projects.
* Web: http://www.StarMessageSoftware.com/cpcclibrary
* Download: https://github.com/starmessage/cpcc
* email: sales -at- starmessage.info
* *****************************************
*/
#pragma once
#ifdef _WIN32
#include <windows.h>
#endif
#ifdef __APPLE__
#import <IOKit/pwr_mgt/IOPMLib.h>
// you need to link the frameworks CoreFoundation, IOKit
#endif
class cpccComputerPower
{
private:
#ifdef __APPLE__
IOPMAssertionID preventSleepAssertionID;
#endif
public:
cpccComputerPower()
#ifdef __APPLE__
: preventSleepAssertionID(0)
#endif
{ }
const bool preventIdleSystemSleep(const char *textualReason)
{
#ifdef _WIN32
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa373208%28v=vs.85%29.aspx
return SetThreadExecutionState(
// ES_AWAYMODE_REQUIRED | // This value must be specified with ES_CONTINUOUS.
ES_CONTINUOUS | // Informs the system that the state being set should remain in effect
// until the next call that uses ES_CONTINUOUS and one of the other state flags is cleared.
// Calling SetThreadExecutionState without ES_CONTINUOUS simply resets the idle timer;
// to keep the display or system in the working state, the thread must call SetThreadExecutionState periodically.
// ES_DISPLAY_REQUIRED | // Forces the display to be on by resetting the display idle timer.
ES_SYSTEM_REQUIRED // Forces the system to be in the working state by resetting the system idle timer.
) != NULL;
#elif __APPLE__
// IOPMAssertionCreateWithName is new API available in Mac OS X 10.6 Snow Leopard.
// IOPMAssertionCreateWithName allows an application to return a return a brief string to the user explaining why that application is preventing sleep.
// https://developer.apple.com/library/mac/qa/qa1340/_index.html
// kIOPMAssertionTypePreventUserIdleDisplaySleep (only available on 10.7 or later)
// kIOPMAssertionTypeNoDisplaySleep - prevents display sleep AND idle sleep,
// kIOPMAssertionTypeNoIdleSleep - prevents idle sleep of the MAC (the screen can turn off)
// NOTE: IOPMAssertionCreateWithName limits the string to 128 characters.
CFStringRef reasonForActivity;
reasonForActivity = CFStringCreateWithCString(NULL, textualReason, kCFStringEncodingASCII);
IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoIdleSleep,
kIOPMAssertionLevelOn,
reasonForActivity,
&preventSleepAssertionID);
CFRelease(reasonForActivity);
return (success == kIOReturnSuccess);
#else
#error #8724: Unknown platform for cpccOS
return false;
#endif
}
const bool preventMonitorSleep(const char *textualReason)
{
#ifdef _WIN32
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa373208%28v=vs.85%29.aspx
return SetThreadExecutionState(
// ES_AWAYMODE_REQUIRED | // This value must be specified with ES_CONTINUOUS.
ES_CONTINUOUS | // Informs the system that the state being set should remain in effect
// until the next call that uses ES_CONTINUOUS and one of the other state flags is cleared.
// Calling SetThreadExecutionState without ES_CONTINUOUS simply resets the idle timer;
// to keep the display or system in the working state, the thread must call SetThreadExecutionState periodically.
ES_DISPLAY_REQUIRED | // Forces the display to be on by resetting the display idle timer.
ES_SYSTEM_REQUIRED // Forces the system to be in the working state by resetting the system idle timer.
) != NULL;
#elif __APPLE__
// IOPMAssertionCreateWithName is new API available in Mac OS X 10.6 Snow Leopard.
// IOPMAssertionCreateWithName allows an application to return a return a brief string to the user explaining why that application is preventing sleep.
// https://developer.apple.com/library/mac/qa/qa1340/_index.html
// kIOPMAssertionTypePreventUserIdleDisplaySleep (only available on 10.7 or later)
// kIOPMAssertionTypeNoDisplaySleep - prevents display sleep AND idle sleep,
// kIOPMAssertionTypeNoIdleSleep - prevents idle sleep of the MAC (the screen can turn off)
// NOTE: IOPMAssertionCreateWithName limits the string to 128 characters.
CFStringRef reasonForActivity;
reasonForActivity = CFStringCreateWithCString(NULL, textualReason, kCFStringEncodingASCII);
IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep,
kIOPMAssertionLevelOn,
reasonForActivity,
&preventSleepAssertionID);
CFRelease(reasonForActivity);
return (success == kIOReturnSuccess);
#else
#error #8724: Unknown platform for cpccOS
return false;
#endif
}
const bool restorePowerSettings(void)
{
#ifdef _WIN32
SetThreadExecutionState(ES_CONTINUOUS);
return true;
#elif __APPLE__
if (preventSleepAssertionID == 0) return
false;
IOReturn success = IOPMAssertionRelease(preventSleepAssertionID);
preventSleepAssertionID = 0;
return (success == kIOReturnSuccess);
#else
#error #8724: Unknown platform for cpccOS
return false;
#endif
}
/*
void cpccOS::keepAwakeTrigger(void)
{
#ifdef _WIN32
#elif __APPLE__
create an NSTimer that fires a function with this
UpdateSystemActivity(OverallAct);
// removed document:
// https://developer.apple.com/library/mac/qa/qa1160/_index.html
// 'UpdateSystemActivity' is deprecated: first deprecated in OS X 10.8
// all these parameters stop the screensaver from running
// UpdateSystemActivity(OverallAct);
// UpdateSystemActivity(IdleActivity);
// UpdateSystemActivity(HDActivity);
// UpdateSystemActivity(UsrActivity);
UpdateSystemActivity(NetActivity);
#else
#error #8724f: Unknown platform for cpccOS
#endif
}
*/
};