forked from mosoto/Caffeinated
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
284 lines (248 loc) · 9.21 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Caffeinated.Properties;
namespace Caffeinated {
public class Duration {
public int Minutes { get; set; }
public string Description {
get {
return Duration.ToDescription(this.Minutes);
}
}
public static string ToDescription(int time) {
if (time == -1) {
return "Auto (App list)";
}
if (time == 0) {
return "Indefinitely";
}
int mins = time % 60;
if (mins != 0) {
return String.Format("{0} minutes", mins);
}
else {
int hours = time / 60;
if (hours == 1) {
return "1 hour";
}
else {
return String.Format("{0} hours", hours);
}
}
}
}
internal static class NativeMethods {
[DllImport("kernel32.dll")]
public static extern uint SetThreadExecutionState(uint esFlags);
public const uint ES_CONTINUOUS = 0x80000000;
public const uint ES_SYSTEM_REQUIRED = 0x00000001;
public const uint ES_DISPLAY_REQUIRED = 0x00000002;
public const uint ES_AWAYMODE_REQUIRED = 0x00000040;
}
public class AppContext : ApplicationContext {
private NotifyIcon notifyIcon;
private IContainer components;
private Icon onIcon;
private Icon offIcon;
private Icon autoOnIcon;
private Icon autoOffIcon;
private Timer timer;
private Timer autoTimer;
private SettingsForm settingsForm = null;
private AboutForm aboutForm = null;
[STAThread]
static void Main() {
var context = new AppContext();
Application.Run(context);
}
public AppContext() {
this.components = new Container();
this.timer = new Timer(components);
timer.Tick += new EventHandler(timer_Tick);
this.autoTimer = new Timer(components);
autoTimer.Tick += new EventHandler(autoTimer_Tick);
autoTimer.Interval = 1 * 10 * 1000; // Check every 10 seconds.
var contextMenu = new ContextMenu();
var exitItem = new MenuItem("E&xit");
exitItem.Click += new EventHandler(this.exitItem_Click);
// we want the lower durations to be closer to the mouse. So,
var times = Settings.Default.RealDurations;
IEnumerable<int> sortedTimes = Enumerable.Empty<int>();
if ((new Taskbar()).Position == TaskbarPosition.Top) {
sortedTimes = times.OrderBy(i => i);
}
else {
sortedTimes = times.OrderByDescending(i => i);
}
var activateForItem = new MenuItem("&Stay awake for");
foreach (var time in sortedTimes) {
var item = new MenuItem(Duration.ToDescription(time));
item.Tag = time;
item.Click += new EventHandler(item_Click);
activateForItem.MenuItems.Add(item);
}
var settingsItem = new MenuItem("&Settings...");
settingsItem.Click += new EventHandler(settingsItem_Click);
var aboutItem = new MenuItem("&About...");
aboutItem.Click += new EventHandler(aboutItem_Click);
contextMenu.MenuItems.AddRange(
new MenuItem[] {
activateForItem,
new MenuItem("-"),
settingsItem,
aboutItem,
exitItem
}
);
this.offIcon = new Icon(
Properties.Resources.sleeping_icon_white,
SystemInformation.SmallIconSize
);
this.onIcon = new Icon(
Properties.Resources.surprised_icon_white,
SystemInformation.SmallIconSize
);
this.autoOffIcon = new Icon(
Properties.Resources.sleeping_icon_blue,
SystemInformation.SmallIconSize
);
this.autoOnIcon = new Icon(
Properties.Resources.surprised_icon_blue,
SystemInformation.SmallIconSize
);
this.notifyIcon = new NotifyIcon(this.components);
notifyIcon.ContextMenu = contextMenu;
// tooltip
notifyIcon.Text = "Caffeinated";
notifyIcon.Visible = true;
// Handle the DoubleClick event to activate the form.
notifyIcon.MouseClick += new MouseEventHandler(notifyIcon1_Click);
if (Settings.Default.ActivateAtLaunch) {
activate(Settings.Default.DefaultDuration);
}
else {
deactivate(false);
}
if (Settings.Default.ShowSettingsAtLaunch) {
showSettings();
}
}
void aboutItem_Click(object sender, EventArgs e) {
aboutForm = new AboutForm();
aboutForm.Show();
}
void settingsItem_Click(object sender, EventArgs e) {
showSettings();
}
void showSettings() {
settingsForm = new SettingsForm();
settingsForm.Show();
}
void timer_Tick(object sender, EventArgs e) {
deactivate(false);
}
void autoTimer_Tick(object sender, EventArgs e) {
if (isAutoAppRunning()) {
activate(-1);
}
else {
deactivate(true);
}
}
bool isAutoAppRunning() {
HashSet<String> appSet = new HashSet<String>(Settings.Default.AutoAppList.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));
Process[] processlist = Process.GetProcesses();
foreach (Process theprocess in processlist) {
if (appSet.Contains(theprocess.ProcessName)) return true;
}
return false;
}
void item_Click(object sender, EventArgs e) {
int time = (int)((MenuItem)sender).Tag;
if (time == -1) {
autoTimer_Tick(null, null);
autoTimer.Start();
return;
}
autoTimer.Stop();
this.activate(time);
}
void notifyIcon1_Click(object sender, MouseEventArgs e) {
if (e.Button != MouseButtons.Left) return;
if (this.isActive())
this.deactivate(false);
else
this.activate(Settings.Default.DefaultDuration);
}
void ShowError() {
MessageBox.Show(
"Call to SetThreadExecutionState failed.",
"Caffeinated",
MessageBoxButtons.OK
);
}
bool isActive() {
return (notifyIcon.Icon == onIcon) || (notifyIcon.Icon == autoOnIcon) || (notifyIcon.Icon == autoOffIcon);
}
void activate(int duration) {
uint sleepDisabled = NativeMethods.ES_CONTINUOUS |
NativeMethods.ES_SYSTEM_REQUIRED |
NativeMethods.ES_AWAYMODE_REQUIRED;
if (Settings.Default.KeepMonitorOn) {
sleepDisabled |= NativeMethods.ES_DISPLAY_REQUIRED;
}
uint previousState = NativeMethods.SetThreadExecutionState(sleepDisabled);
if (previousState == 0) {
ShowError();
ExitThread();
}
if (duration > 0) {
this.timer.Interval = duration * 60 * 1000;
this.timer.Start();
}
if (duration == -1) {
if (!autoTimer.Enabled)
autoTimer.Start();
this.notifyIcon.Icon = autoOnIcon;
this.notifyIcon.Text = "Caffeinated: (auto) sleep not allowed!";
}
else
{
this.notifyIcon.Icon = onIcon;
this.notifyIcon.Text = "Caffeinated: sleep not allowed!";
}
}
private void deactivate(bool auto)
{
timer.Stop();
uint result = NativeMethods.SetThreadExecutionState(NativeMethods.ES_CONTINUOUS);
if (result == 0) {
ShowError();
}
if (auto) {
this.notifyIcon.Icon = autoOffIcon;
this.notifyIcon.Text = "Caffeinated: (auto) sleep allowed";
}
else {
autoTimer.Stop();
this.notifyIcon.Icon = offIcon;
this.notifyIcon.Text = "Caffeinated: sleep allowed";
}
}
private void exitItem_Click(object Sender, EventArgs e) {
deactivate(false);
ExitThread();
}
protected override void Dispose(bool disposing) {
if (disposing && components != null)
components.Dispose();
base.Dispose(disposing);
}
}
}