-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerManager.cs
More file actions
184 lines (148 loc) · 5.77 KB
/
PowerManager.cs
File metadata and controls
184 lines (148 loc) · 5.77 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
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
175
176
177
178
179
180
181
182
183
184
using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace PlanSwitcher {
public class PowerPlan {
public readonly string name;
public Guid guid;
public PowerPlan(string name, Guid guid) {
this.name = name;
this.guid = guid;
}
}
public interface IPowerManager {
/// <returns>
/// All supported power plans.
/// </returns>
List<PowerPlan> GetPlans();
bool IsCharging();
PowerPlan GetCurrentPlan();
/// <returns>Battery charge value in percent,
/// i.e. values in a 0..100 range</returns>
int GetChargeValue();
void SetActive(PowerPlan plan);
/// <summary>
/// Opens Power Options section of the Control Panel.
/// </summary>
void OpenControlPanel();
}
public class PowerManagerProvider {
public static IPowerManager CreatePowerManager(UICallback uiCallback) {
return new PowerManager(uiCallback);
}
}
class PowerManager : IPowerManager {
/// <summary>
/// Indicates that almost no power savings measures will be used.
/// </summary>
private readonly PowerPlan MaximumPerformance;
/// <summary>
/// Indicates that fairly aggressive power savings measures will be used.
/// </summary>
private readonly PowerPlan Balanced;
/// <summary>
/// Indicates that very aggressive power savings measures will be used to help
/// stretch battery life.
/// </summary>
private readonly PowerPlan PowerSourceOptimized;
private UICallback uiCallback;
public PowerManager(UICallback uiCallback) {
this.uiCallback = uiCallback;
// See GUID values in WinNT.h.
MaximumPerformance = NewPlan("8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c");
Balanced = NewPlan("381b4222-f694-41f0-9685-ff5bb260df2e");
PowerSourceOptimized = NewPlan("a1841308-3541-4fab-bc81-f71556f20b4a");
// Add handler for power mode state changing.
Microsoft.Win32.SystemEvents.PowerModeChanged += new Microsoft.Win32.PowerModeChangedEventHandler(PowerModeChangedHandler);
}
private PowerPlan NewPlan(string guidString) {
Guid guid = new Guid(guidString);
return new PowerPlan(GetPowerPlanName(guid), guid);
}
public void SetActive(PowerPlan plan) {
PowerSetActiveScheme(IntPtr.Zero, ref plan.guid);
uiCallback.UpdateBatteryState();
//Logger.Instance().Info("Switched to " + plan.name);
}
/// <returns>
/// All supported power plans.
/// </returns>
public List<PowerPlan> GetPlans() {
return new List<PowerPlan>(new PowerPlan[] {
MaximumPerformance,
Balanced,
PowerSourceOptimized
});
}
public bool IsCharging() {
PowerStatus pwrStatus = SystemInformation.PowerStatus;
return pwrStatus.PowerLineStatus == PowerLineStatus.Online;
}
public int GetChargeValue() {
PowerStatus pwrStatus = SystemInformation.PowerStatus;
return (int)(pwrStatus.BatteryLifePercent * 100);
}
private Guid GetActiveGuid() {
Guid ActiveScheme = Guid.Empty;
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));
if (PowerGetActiveScheme((IntPtr)null, out ptr) == 0) {
ActiveScheme = (Guid)Marshal.PtrToStructure(ptr, typeof(Guid));
if (ptr != null) {
Marshal.FreeHGlobal(ptr);
}
}
return ActiveScheme;
}
public PowerPlan GetCurrentPlan() {
Guid guid = GetActiveGuid();
return GetPlans().Find(p => (p.guid == guid));
}
private void PowerModeChangedHandler(object sender, EventArgs e) {
if (SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Online) {
SetActive(MaximumPerformance);
//Logger.Instance().Info("Power connected");
}
else if (SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Offline) {
SetActive(PowerSourceOptimized);
//Logger.Instance().Info("Power disconnected");
}
else {
//Logger.Instance().Warn("Power state changed to an unknown value");
}
}
private static string GetPowerPlanName(Guid guid) {
string name = string.Empty;
IntPtr lpszName = (IntPtr)null;
uint dwSize = 0;
PowerReadFriendlyName((IntPtr)null, ref guid, (IntPtr)null, (IntPtr)null, lpszName, ref dwSize);
if (dwSize > 0) {
lpszName = Marshal.AllocHGlobal((int)dwSize);
if (0 == PowerReadFriendlyName((IntPtr)null, ref guid, (IntPtr)null, (IntPtr)null, lpszName, ref dwSize)) {
name = Marshal.PtrToStringUni(lpszName);
}
if (lpszName != IntPtr.Zero)
Marshal.FreeHGlobal(lpszName);
}
return name;
}
/// <summary>
/// Opens Power Options section of the Control Panel.
/// </summary>
public void OpenControlPanel() {
var root = Environment.GetEnvironmentVariable("SystemRoot");
Process.Start(root + "\\system32\\control.exe", "/name Microsoft.PowerOptions");
}
#region DLL imports
[DllImport("kernel32.dll")]
private static extern int GetSystemDefaultLCID();
[DllImport("powrprof.dll", EntryPoint = "PowerSetActiveScheme")]
public static extern uint PowerSetActiveScheme(IntPtr UserPowerKey, ref Guid ActivePolicyGuid);
[DllImport("powrprof.dll", EntryPoint = "PowerGetActiveScheme")]
public static extern uint PowerGetActiveScheme(IntPtr UserPowerKey, out IntPtr ActivePolicyGuid);
[DllImport("powrprof.dll", EntryPoint = "PowerReadFriendlyName")]
public static extern uint PowerReadFriendlyName(IntPtr RootPowerKey, ref Guid SchemeGuid, IntPtr SubGroupOfPowerSettingsGuid, IntPtr PowerSettingGuid, IntPtr Buffer, ref uint BufferSize);
#endregion
}
}