-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
377 lines (319 loc) · 13.4 KB
/
Program.cs
File metadata and controls
377 lines (319 loc) · 13.4 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
371
372
373
374
375
376
377
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Text.Json;
using System.IO;
using System.Threading.Tasks;
using System.Timers;
using Timer = System.Timers.Timer;
using System.Threading;
using System.Runtime.InteropServices;
using System.Linq;
using System.Reflection;
using System.Diagnostics;
namespace Sleeper
{
/*
* ------------------------------------------------------------------------------------------------------------------
* Sleeper by CapaciousCore licensed under MIT and...
* ------------------------------------------------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* CapaciousCore wrote this software. As long as you retain this notice you can do whatever you want with this stuff.
* If we meet some day, and you think this stuff is worth it, you can buy me a beer in return.
* ------------------------------------------------------------------------------------------------------------------
*/
static class Program
{
internal static Mutex Mutex;
[STAThread]
static void Main()
{
using (Mutex = new Mutex(false, @"Global\" + GetGuid()))
{
if (!Mutex.WaitOne(0, false))
{
MessageBox.Show("An application instance is already running", "Sleeper", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new TrayApplicationContext());
}
}
private static Guid GetGuid()
{
GuidAttribute GuidAttribute = (GuidAttribute)Assembly.GetExecutingAssembly()?.GetCustomAttributes(typeof(GuidAttribute), false).SingleOrDefault();
Guid.TryParse(GuidAttribute?.Value, out Guid GUID);
return GUID;
}
}
public class Configuration
{
public enum SelectionOperation { After, At };
public enum OperationTypes { Shutdown, Restart, Log_off, Hibernate, Stand_by };
public SelectionOperation SelectedOperation { set; get; }
public OperationTypes AfterOperation { set; get; }
public OperationTypes AtOperation { set; get; }
public ushort TimeAfterOperation { set; get; }
public ushort TimeAtOperation { set; get; }
public bool IsWarnEnabled { set; get; }
public bool IsEnabled { set; get; }
}
public class WarningConfiguration
{
public string Operation { set; get; }
public ushort RemainingSeconds { set; get; }
}
internal struct LastInputInfo
{
public uint Size;
public uint Time;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TokenPrivileges
{
public int Count;
public long Luid;
public int Attr;
}
public class TrayApplicationContext : ApplicationContext
{
private NotifyIcon TrayIcon;
private Configuration Config;
private Timer IdleTimer = new Timer(interval: 1000);
private Task Operation;
private CancellationTokenSource OperationCancellationTokenSource;
private CancellationTokenSource WarningCancellationTokenSource;
[DllImport("User32.dll")]
private static extern bool GetLastInputInfo(ref LastInputInfo lii);
[DllImport("Kernel32.dll")]
private static extern uint GetLastError();
[DllImport("user32.dll")]
private static extern int ExitWindowsEx(int uFlags, int dwReason);
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
private static extern bool OpenProcessToken(IntPtr ProcessHandle, int DesiredAccess, ref IntPtr TokenHandle);
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool LookupPrivilegeValue(string SystemName, string Name, ref long Luid);
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
private static extern bool AdjustTokenPrivileges(IntPtr TokenHandle, bool DisableAllPrivileges, ref TokenPrivileges NewState, int BufferLength, IntPtr PreviousState, IntPtr ReturnLength);
public TrayApplicationContext()
{
if (File.Exists("./Config.json"))
{
Config = JsonSerializer.Deserialize<Configuration>(File.ReadAllText("./Config.json"));
}
else
{
Config = new Configuration
{
SelectedOperation = Sleeper.Configuration.SelectionOperation.After,
AfterOperation = Sleeper.Configuration.OperationTypes.Shutdown,
AtOperation = Sleeper.Configuration.OperationTypes.Shutdown,
TimeAfterOperation = 0,
TimeAtOperation = 0,
IsWarnEnabled = false,
IsEnabled = false
};
}
IdleTimer.Elapsed += IdleTimerTick;
if (Config.IsEnabled)
{
SwitchTimer();
}
ContextMenu ContextMenu = new ContextMenu();
ContextMenu.MenuItems.Add("Configuration", Configuration);
ContextMenu.MenuItems.Add("-");
ContextMenu.MenuItems.Add("Exit", Exit);
TrayIcon = new NotifyIcon
{
Visible = true,
Text = "Sleeper",
Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath),
ContextMenu = ContextMenu
};
Application.ApplicationExit += OnApplicationExit;
}
private void Configuration(object sender, EventArgs e)
{
if (GetFormInstance<ConfigurationForm>() == null)
{
ConfigurationForm ConfigurationForm = new ConfigurationForm(ref Config);
ConfigurationForm.ChangeAction += SwitchTimer;
ConfigurationForm.Show();
}
}
private void Exit(object sender, EventArgs e)
{
TrayIcon.Visible = false;
Application.Exit();
}
private void SwitchTimer(bool Start = true)
{
if (Start)
{
if (Config.SelectedOperation == Sleeper.Configuration.SelectionOperation.After)
{
WarningCancellationTokenSource = OperationCancellationTokenSource = null;
IdleTimer.Start();
}
else
{
TimeSpan TimeAtOperation = TimeSpan.FromMinutes(Config.TimeAtOperation);
DateTime ScheduledTime = DateTime.Today + TimeAtOperation;
if (DateTime.Now > ScheduledTime)
{
ScheduledTime += TimeSpan.FromDays(1);
}
if (Config.IsWarnEnabled)
{
WarningCancellationTokenSource = new CancellationTokenSource();
if (ScheduledTime.AddMinutes(-1) > DateTime.Now)
{
Task.Delay(ScheduledTime.AddMinutes(-1) - DateTime.Now, WarningCancellationTokenSource.Token).ContinueWith(_ => ShowWarning());
}
else
{
Task.Run(() => ShowWarning((ushort)(ScheduledTime - DateTime.Now).TotalSeconds), WarningCancellationTokenSource.Token);
}
}
// Variant of using the TPL
OperationCancellationTokenSource = new CancellationTokenSource();
Operation = Task.Delay(ScheduledTime - DateTime.Now, OperationCancellationTokenSource.Token).ContinueWith(_ => ExecuteOperation());
}
}
else
{
if (Config.SelectedOperation == Sleeper.Configuration.SelectionOperation.After)
{
IdleTimer.Stop();
}
else
{
if (Config.IsWarnEnabled)
{
WarningCancellationTokenSource.Cancel();
}
OperationCancellationTokenSource.Cancel();
}
HideWarning();
}
}
private TForm GetFormInstance<TForm>() where TForm : Form
{
return Application.OpenForms.OfType<TForm>().FirstOrDefault();
}
private void IdleTimerTick(object sender, ElapsedEventArgs e)
{
var TimeAfterOperation = TimeSpan.FromMinutes(Config.TimeAfterOperation).TotalSeconds;
long IdleTime = GetIdleTime();
if (TimeAfterOperation == 0)
{
TimeAfterOperation = 60;
}
if (Config.IsWarnEnabled)
{
if (IdleTime >= TimeAfterOperation - 60)
{
ShowWarning((ushort)(60 - IdleTime));
}
else
{
HideWarning();
}
}
if (IdleTime >= TimeAfterOperation)
{
ExecuteOperation();
}
}
private static long GetIdleTime()
{
LastInputInfo LastInputInfo = new LastInputInfo();
LastInputInfo.Size = (uint)Marshal.SizeOf(LastInputInfo);
if (!GetLastInputInfo(ref LastInputInfo))
{
throw new Exception(GetLastError().ToString());
}
return (((Environment.TickCount & int.MaxValue) - (LastInputInfo.Time & uint.MaxValue)) & uint.MaxValue) / 1000;
}
private void ShowWarning(ushort RemainingSeconds = 60)
{
if (WarningCancellationTokenSource == null || !WarningCancellationTokenSource.IsCancellationRequested)
{
WarningForm WarningForm = GetFormInstance<WarningForm>();
if (WarningForm == null)
{
WarningConfiguration WarningConfiguration = new WarningConfiguration
{
Operation = Enum.GetName(typeof(Configuration.OperationTypes), (Config.SelectedOperation == Sleeper.Configuration.SelectionOperation.After ? Config.AfterOperation : Config.AtOperation)).ToLower().Replace('_', ' '),
RemainingSeconds = RemainingSeconds
};
WarningForm = new WarningForm(WarningConfiguration);
WarningForm.ShowDialog();
}
else
{
WarningForm.UpdateRemainingSeconds(RemainingSeconds);
}
}
}
private void HideWarning()
{
WarningForm WarningForm = GetFormInstance<WarningForm>();
if (WarningForm != null)
{
WarningForm.Invoke(new Action(() =>
{
WarningForm.Close();
}));
}
}
private void ExecuteOperation()
{
if (OperationCancellationTokenSource == null || !OperationCancellationTokenSource.IsCancellationRequested)
{
Configuration.OperationTypes Operation = (Config.SelectedOperation == Sleeper.Configuration.SelectionOperation.After ? Config.AfterOperation : Config.AtOperation);
switch (Operation)
{
case Sleeper.Configuration.OperationTypes.Shutdown:
GetPrivilege();
ExitWindowsEx(1, 0);
break;
case Sleeper.Configuration.OperationTypes.Restart:
GetPrivilege();
ExitWindowsEx(2, 0);
break;
case Sleeper.Configuration.OperationTypes.Log_off:
ExitWindowsEx(0, 0);
// ExitWindowsEx(4, 0);
break;
case Sleeper.Configuration.OperationTypes.Hibernate:
Application.SetSuspendState(PowerState.Hibernate, true, true);
break;
case Sleeper.Configuration.OperationTypes.Stand_by:
Application.SetSuspendState(PowerState.Suspend, true, true);
break;
}
Application.Exit();
}
}
private void GetPrivilege()
{
IntPtr TokenHandle = IntPtr.Zero;
TokenPrivileges TokenPrivileges = new TokenPrivileges
{
Count = 1,
Luid = 0,
Attr = 2 // SE_PRIVILEGE_ENABLED
};
OpenProcessToken(Process.GetCurrentProcess().Handle, 40, ref TokenHandle); // Desired access is TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY
LookupPrivilegeValue(null, "SeShutdownPrivilege", ref TokenPrivileges.Luid);
AdjustTokenPrivileges(TokenHandle, false, ref TokenPrivileges, 0, IntPtr.Zero, IntPtr.Zero);
}
private void OnApplicationExit(object sender, EventArgs e)
{
Program.Mutex.ReleaseMutex();
File.WriteAllText("./Config.json", JsonSerializer.Serialize(Config));
}
}
}