-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cs
More file actions
370 lines (307 loc) · 11.3 KB
/
Main.cs
File metadata and controls
370 lines (307 loc) · 11.3 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace IdlePowerSwitcher
{
public partial class Main : Form
{
//List variables
string highpoweroption;
string lowpoweroption;
uint lastInputuint;
string lastInputstring;
bool MinimizedSetting;
TimeSpan time;
TimeSpan currentIdleTime;
RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
[DllImport("user32.dll")]
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
internal struct LASTINPUTINFO
{
public uint cbSize;
public uint dwTime;
}
#region PowerSettingEnumeration
[DllImport("PowrProf.dll")]
public static extern UInt32 PowerEnumerate(IntPtr RootPowerKey, IntPtr SchemeGuid, IntPtr SubGroupOfPowerSettingGuid, UInt32 AcessFlags, UInt32 Index, ref Guid Buffer, ref UInt32 BufferSize);
[DllImport("PowrProf.dll")]
public static extern UInt32 PowerReadFriendlyName(IntPtr RootPowerKey, ref Guid SchemeGuid, IntPtr SubGroupOfPowerSettingGuid, IntPtr PowerSettingGuid, IntPtr Buffer, ref UInt32 BufferSize);
[DllImport("powrProf.dll")]
public static extern uint PowerGetActiveScheme(IntPtr UserRootPowerKey, ref IntPtr ActivePolicyGuid);
[DllImport("PowrProf.dll")]
public static extern uint PowerSetActiveScheme(IntPtr UserRootPowerKey, ref Guid SchemeGuid);
List<String> lsFriendly = new List<string>();
List<Guid> lsGuid = new List<Guid>();
public enum AccessFlags : uint
{
ACCESS_SCHEME = 16,
ACCESS_SUBGROUP = 17,
ACCESS_INDIVIDUAL_SETTING = 18
}
private static string ReadFriendlyName(Guid schemeGuid)
{
uint sizeName = 1024;
IntPtr pSizeName = Marshal.AllocHGlobal((int)sizeName);
string friendlyName;
try
{
PowerReadFriendlyName(IntPtr.Zero, ref schemeGuid, IntPtr.Zero, IntPtr.Zero, pSizeName, ref sizeName);
friendlyName = Marshal.PtrToStringUni(pSizeName);
}
finally
{
Marshal.FreeHGlobal(pSizeName);
}
return friendlyName;
}
public static IEnumerable<Guid> GetAll()
{
var schemeGuid = Guid.Empty;
uint sizeSchemeGuid = (uint)Marshal.SizeOf(typeof(Guid));
uint schemeIndex = 0;
while (PowerEnumerate(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, (uint)AccessFlags.ACCESS_SCHEME, schemeIndex, ref schemeGuid, ref sizeSchemeGuid) == 0)
{
yield return schemeGuid;
schemeIndex++;
}
}
public void SetActive(Guid powerSchemeId)
{
var schemeGuid = powerSchemeId;
Main.PowerSetActiveScheme(IntPtr.Zero, ref schemeGuid);
}
public Guid GetActive()
{
IntPtr pCurrentSchemeGuid = IntPtr.Zero;
Main.PowerGetActiveScheme(IntPtr.Zero, ref pCurrentSchemeGuid);
var currentSchemeGuid = (Guid)Marshal.PtrToStructure(pCurrentSchemeGuid, typeof(Guid));
return currentSchemeGuid;
}
#endregion
public static bool ScrLockActive()
{
return Control.IsKeyLocked(Keys.Scroll);
}
public Main()
{
InitializeComponent();
LoadSettings();
isStartupChecked();
populatePlans();
}
private void populatePlans()
{
//Get all current power plans
var guidPlans = GetAll();
foreach (Guid guidPlan in guidPlans)
{
//Add guid and friendly name into corresponding lists
lsGuid.Add(guidPlan);
lsFriendly.Add(ReadFriendlyName(guidPlan));
//Populate ComboBoxes
cbLowOption.Items.Add(ReadFriendlyName(guidPlan));
cbHighOption.Items.Add(ReadFriendlyName(guidPlan));
}
lblTest.Text = ReadFriendlyName(GetActive());
}
private void isStartupChecked()
{
//Check to see if program is set to run on startup
if (rk.GetValue("IdlePowerSwitcher") == null)
{
//Not set to run on start
chkStartup.Checked = false;
}
else
{
//Is set to run on start
chkStartup.Checked = true;
}
}
private void LoadSettings()
{
highpoweroption = Properties.Settings.Default.HighPowerOption;
lowpoweroption = Properties.Settings.Default.LowPowerOption;
sliderIdle.Value = Properties.Settings.Default.IdleTimeWait;
//Format label text for time.
time = TimeSpan.FromSeconds(sliderIdle.Value);
lblSelectedIdleTime.Text = time.ToString(@"mm\:ss");
cbHighOption.Text = highpoweroption;
cbLowOption.Text = lowpoweroption;
MinimizedSetting = Properties.Settings.Default.MinimizeOnStart;
//Load minimized setting... minimize on startup if box is checked.
if (MinimizedSetting) { chkMinimize.Checked = true; WindowState = FormWindowState.Minimized; ShowInTaskbar = false; }
}
static uint GetLastInputTime()
{
uint idleTime = 0;
LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
lastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo);
lastInputInfo.dwTime = 0;
uint envTicks = (uint)Environment.TickCount;
if (GetLastInputInfo(ref lastInputInfo))
{
uint lastInputTick = lastInputInfo.dwTime;
idleTime = envTicks - lastInputTick;
}
return ((idleTime > 0) ? (idleTime / 1000) : 0);
}
private void sliderIdle_Scroll(object sender, EventArgs e)
{
time = TimeSpan.FromSeconds(sliderIdle.Value);
lblSelectedIdleTime.Text = time.ToString(@"mm\:ss");
btnApply.Enabled = true;
}
private void btnApply_Click(object sender, EventArgs e)
{
//Set settings
Properties.Settings.Default.HighPowerOption = cbHighOption.Text;
Properties.Settings.Default.LowPowerOption = cbLowOption.Text;
Properties.Settings.Default.IdleTimeWait = sliderIdle.Value;
Properties.Settings.Default.MinimizeOnStart = chkMinimize.Checked;
Properties.Settings.Default.Save();
CheckRunOnStart();
btnApply.Enabled = false;
}
private void CheckRunOnStart()
{
if (chkStartup.Checked)
rk.SetValue("IdlePowerSwitcher", Application.ExecutablePath.ToString());
else
rk.DeleteValue("IdlePowerSwitcher", false);
}
private void SetHighPowerOption()
{
highpoweroption = cbHighOption.Text;
int a = lsFriendly.IndexOf(highpoweroption);
Guid guida = lsGuid[a];
//Set PowerOption
SetActive(guida);
//Update Text
lblTest.Text = ReadFriendlyName(GetActive());
}
private void SetLowPowerOption()
{
lowpoweroption = cbLowOption.Text;
int a = lsFriendly.IndexOf(lowpoweroption);
Guid guida = lsGuid[a];
//Set PowerOption
SetActive(guida);
//Update Text
lblTest.Text = ReadFriendlyName(GetActive());
}
private void timeScrollLock_Tick(object sender, EventArgs e)
{
if (ScrLockActive())
{
//Stop auto switch while scroll lock is enabled.
timerIdleTime.Enabled = false;
//Switch to high power automatically
SetHighPowerOption();
}
else
{
timerIdleTime.Enabled = true;
}
}
private void timerIdleTime_Tick(object sender, EventArgs e)
{
lastInputuint = GetLastInputTime();
lastInputstring = lastInputuint.ToString();
if (lastInputuint == 0)
{
try
{
SetHighPowerOption();
}
catch (Exception)
{ }
}
else if (lastInputuint >= (sliderIdle.Value))
{
try
{
SetLowPowerOption();
}
catch (Exception)
{ }
}
currentIdleTime = TimeSpan.FromSeconds(lastInputuint);
lblLastInput.Text = currentIdleTime.ToString(@"mm\:ss");
}
private void Main_Resize(object sender, EventArgs e)
{
if (FormWindowState.Minimized == WindowState)
{
notifyIcon.Visible = true;
notifyIcon.ShowBalloonTip(200);
Hide();
}
else if (FormWindowState.Normal == WindowState)
{
notifyIcon.Visible = false;
}
}
private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
openFromTaskbar();
}
private void notifyIcon_BalloonTipClicked(object sender, EventArgs e)
{
openFromTaskbar();
}
private void showToolStripMenuItem_Click(object sender, EventArgs e)
{
openFromTaskbar();
}
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void openFromTaskbar()
{
WindowState = FormWindowState.Minimized;
Show();
WindowState = FormWindowState.Normal;
ShowInTaskbar = true;
}
private void cbHighandLowOption_SelectedIndexChanged(object sender, EventArgs e)
{
btnApply.Enabled = true;
}
private void chkStartup_CheckedChanged(object sender, EventArgs e)
{
btnApply.Enabled = true;
}
private void chkMinimize_CheckedChanged(object sender, EventArgs e)
{
btnApply.Enabled = true;
}
private void llblChangePower_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("powercfg.cpl");
}
private void minimizeToSystemTrayToolStripMenuItem_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
}
private void closeToolStripMenuItem1_Click(object sender, EventArgs e)
{
Close();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutBox aboutBox1 = new AboutBox();
aboutBox1.Show();
}
}
}