-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPowerUtils.cpp
More file actions
144 lines (121 loc) · 3.76 KB
/
PowerUtils.cpp
File metadata and controls
144 lines (121 loc) · 3.76 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
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
#include "Base.h"
#include "PowerUtils.h"
#ifdef _WIN32
#include "PlatformUtils.h"
#include <dxgi.h>
#elif __APPLE__
#include "Object.h"
#include <IOKit/ps/IOPowerSources.h>
#include <IOKit/pwr_mgt/IOPM.h>
#endif
NS_BEGIN
//#################################################################################################
EPowerSource GetCurrentPowerSource(void)
{
EPowerSource eSource = EPS_Unknown;
#ifdef _WIN32
SYSTEM_POWER_STATUS sps = {0};
GetSystemPowerStatus(&sps);
if(sps.ACLineStatus == AC_LINE_ONLINE)
eSource = EPS_AC;
else if(sps.ACLineStatus != AC_LINE_UNKNOWN)
eSource = EPS_Battery;
#elif __APPLE__
CCFObject cfPower = IOPSCopyPowerSourcesInfo();
if(cfPower)
{
CCFObject cfValue(IOPSGetProvidingPowerSourceType(cfPower), false);
if(cfValue)
{
CStr strSource = cfValue.GetString();
if(strSource == kIOPMACPowerKey)
eSource = EPS_AC;
else if(strSource == kIOPMBatteryPowerKey || strSource == kIOPMUPSPowerKey)
eSource = EPS_Battery;
}
}
#elif __linux__
// TODO
#endif
return eSource;
}
//#################################################################################################
bool IsBatterySaverActive(void)
{
bool bBatterySaverActive = false;
#ifdef _WIN32
SYSTEM_POWER_STATUS sps = {0};
GetSystemPowerStatus(&sps);
if(sps.SystemStatusFlag)
bBatterySaverActive = true;
#elif __APPLE__
IOPSLowBatteryWarningLevel eLevel = IOPSGetBatteryWarningLevel();
if(eLevel == kIOPSLowBatteryWarningEarly || eLevel == kIOPSLowBatteryWarningFinal)
bBatterySaverActive = true;
#elif __linux__
// TODO
#endif
return bBatterySaverActive;
}
#ifdef _WIN32
//#################################################################################################
bool HasActiveDisplay(void)
{ // Just because the laptop lid is closed does not mean the user is not interactive, there could be an external display
#ifdef _DEBUG
DWORD dwSessionId;
if(ProcessIdToSessionId(GetCurrentProcessId(), &dwSessionId))
Assert(dwSessionId); // This function does not work when called from a service running in session 0
#endif
bool bHasActiveDisplay = false;
using PFNCREATEDXGIFACTORY = HRESULT(WINAPI*)(const IID&, PVOID*);
HMODULE hDxgi = SystemLoadLibrary(_N("dxgi.dll"));
if(hDxgi)
{
PFNCREATEDXGIFACTORY pfnCreateDXGIFactory = (PFNCREATEDXGIFACTORY)GetProcAddress(hDxgi, "CreateDXGIFactory");
if(pfnCreateDXGIFactory)
{
IDXGIFactory *pFactory = nullptr;
if(SUCCEEDED(pfnCreateDXGIFactory(__uuidof(IDXGIFactory), (PVOID*)&pFactory)))
{
DXGI_OUTPUT_DESC od;
IDXGIAdapter *pAdapter = nullptr;
UINT nAdapterIndex = 0;
while(!bHasActiveDisplay && SUCCEEDED(pFactory->EnumAdapters(nAdapterIndex++, &pAdapter)))
{
IDXGIOutput *pOutput = nullptr;
UINT nOutputIndex = 0;
while(SUCCEEDED(pAdapter->EnumOutputs(nOutputIndex++, &pOutput)))
{
if(SUCCEEDED(pOutput->GetDesc(&od)) && od.AttachedToDesktop)
{
bHasActiveDisplay = true;
pOutput->Release();
break;
}
pOutput->Release();
}
pAdapter->Release();
}
pFactory->Release();
}
}
FreeLibrary(hDxgi);
}
return bHasActiveDisplay;
}
#elif __APPLE__
//#################################################################################################
ELidState GetLidState(void)
{
ELidState eState = ELS_Unknown;
CIOObject ioReg = IORegistryEntryFromPath(kIOMasterPortDefault, "IOService:/AppleACPIPlatformExpert/IOPMrootDomain");
if(ioReg)
{
CCFObject cfProp = IORegistryEntryCreateCFProperty(ioReg, CFSTR(kAppleClamshellStateKey), kCFAllocatorDefault, 0);
if(cfProp)
eState = (cfProp.GetBoolean()) ? ELS_Closed : ELS_Open;
}
return eState;
}
#endif
NS_END