-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
421 lines (361 loc) · 9.99 KB
/
main.c
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
* Simplified clone of the Caffeine application for Windows Vista and newer
*
* Original idea and Mac implementation by Lighthead Software
* http://lightheadsw.com/caffeine/
*
* Copyright (C) 2013 Patrick Schlangen <patrick@schlangen.me>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <Windows.h>
#include <tchar.h>
#include "resource.h"
/*
* Enable manifest generation for modern look&feel
*/
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
/*
* IDs for menu items and messagesw
*/
#define ID_TRAYICON 500
#define ID_MENU_EXIT 510
#define ID_MENU_TOGGLE 511
#define ID_MENU_PREFS 512
#define WM_SHELLNOTIFY (WM_USER+1)
/*
* Structure for configuration options
*/
struct CaffeinePrefs_t
{
BOOL bDisplayOn;
BOOL bNoStandby;
BOOL bAutoStart;
BOOL bAutoEnable;
};
/*
* Global variables
*/
HINSTANCE g_hInstance;
WCHAR g_szClassName[] = _T("CaffeineTrayIcon");
HWND g_hTrayIcon;
HMENU g_hTrayMenu;
NOTIFYICONDATA g_TrayIcon;
BOOL g_bIsEnabled = FALSE;
struct CaffeinePrefs_t g_cPrefs;
/*
* Core functionality (set thread execution state to prevent display-off etc)
*/
void CaffeineSet(BOOL enable)
{
EXECUTION_STATE esFlags = ES_CONTINUOUS;
if(enable)
{
if(g_cPrefs.bDisplayOn)
esFlags |= ES_DISPLAY_REQUIRED;
if(g_cPrefs.bNoStandby)
esFlags |= ES_SYSTEM_REQUIRED;
g_TrayIcon.hIcon = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ICON_ENABLED));
wcscpy_s(g_TrayIcon.szTip, sizeof(g_TrayIcon.szTip), _T("Caffeine (enabled)"));
CheckMenuItem(g_hTrayMenu, ID_MENU_TOGGLE, MF_BYCOMMAND | MF_CHECKED);
}
else
{
g_TrayIcon.hIcon = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ICON_DISABLED));
wcscpy_s(g_TrayIcon.szTip, sizeof(g_TrayIcon.szTip), _T("Caffeine (disabled)"));
CheckMenuItem(g_hTrayMenu, ID_MENU_TOGGLE, MF_BYCOMMAND | MF_UNCHECKED);
}
SetThreadExecutionState(esFlags);
Shell_NotifyIcon(NIM_MODIFY, &g_TrayIcon);
g_bIsEnabled = enable;
}
/*
* Toggle Caffeine state
*/
void CaffeineToggle()
{
CaffeineSet(!g_bIsEnabled);
}
/*
* Save preferences from g_cPrefs to registry
*/
void PrefsSave()
{
HKEY hk;
WCHAR strFileName[MAX_PATH];
if(RegCreateKeyEx(HKEY_CURRENT_USER, _T("Software\\Caffeine"), 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hk, NULL) == ERROR_SUCCESS)
{
RegSetValueEx(hk, _T("DisplayOn"), 0, REG_DWORD,
(BYTE *)&g_cPrefs.bDisplayOn, sizeof(g_cPrefs.bDisplayOn));
RegSetValueEx(hk, _T("NoStandby"), 0, REG_DWORD,
(BYTE *)&g_cPrefs.bNoStandby, sizeof(g_cPrefs.bNoStandby));
RegSetValueEx(hk, _T("AutoEnable"), 0, REG_DWORD,
(BYTE *)&g_cPrefs.bAutoEnable, sizeof(g_cPrefs.bAutoEnable));
RegSetValueEx(hk, _T("AutoStart"), 0, REG_DWORD,
(BYTE *)&g_cPrefs.bAutoEnable, sizeof(g_cPrefs.bAutoStart));
RegCloseKey(hk);
}
if(RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, KEY_ALL_ACCESS, &hk) == ERROR_SUCCESS)
{
if(g_cPrefs.bAutoStart)
{
GetModuleFileName(GetModuleHandle(NULL), strFileName, sizeof(strFileName));
RegSetValueEx(hk, _T("Caffeine"), 0, REG_SZ, (BYTE *)strFileName, wcslen(strFileName)*sizeof(WCHAR)+1);
}
else
{
RegDeleteValue(hk, _T("Caffeine"));
}
RegCloseKey(hk);
}
}
/*
* Load preferences from registry to g_cPrefs
*/
void PrefsLoad()
{
HKEY hk;
DWORD dwMaxLen;
g_cPrefs.bDisplayOn = TRUE;
g_cPrefs.bNoStandby = TRUE;
g_cPrefs.bAutoEnable = FALSE;
g_cPrefs.bAutoStart = FALSE;
if(RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Caffeine"), 0, KEY_QUERY_VALUE, &hk) == ERROR_SUCCESS)
{
dwMaxLen = sizeof(g_cPrefs.bDisplayOn);
RegQueryValueEx(hk, _T("DisplayOn"), NULL, NULL, (BYTE *)&g_cPrefs.bDisplayOn, &dwMaxLen);
dwMaxLen = sizeof(g_cPrefs.bNoStandby);
RegQueryValueEx(hk, _T("NoStandby"), NULL, NULL, (BYTE *)&g_cPrefs.bNoStandby, &dwMaxLen);
dwMaxLen = sizeof(g_cPrefs.bAutoEnable);
RegQueryValueEx(hk, _T("AutoEnable"), NULL, NULL, (BYTE *)&g_cPrefs.bAutoEnable, &dwMaxLen);
dwMaxLen = sizeof(g_cPrefs.bAutoStart);
RegQueryValueEx(hk, _T("AutoStart"), NULL, NULL, (BYTE *)&g_cPrefs.bAutoStart, &dwMaxLen);
RegCloseKey(hk);
}
}
/*
* Preferences dialog callback
*/
INT_PTR CALLBACK PrefsDlgProc(HWND hWndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
{
SendMessage(hWndDlg, WM_SETICON, ICON_SMALL,
(LPARAM)LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ICON_ENABLED)));
SendMessage(hWndDlg, WM_SETICON, ICON_BIG,
(LPARAM)LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ICON_ENABLED)));
SendDlgItemMessage(hWndDlg,
IDC_CHECK_DISPLAY_ON,
BM_SETCHECK,
g_cPrefs.bDisplayOn ? BST_CHECKED : BST_UNCHECKED,
0);
SendDlgItemMessage(hWndDlg,
IDC_CHECK_NO_STANDBY,
BM_SETCHECK,
g_cPrefs.bNoStandby ? BST_CHECKED : BST_UNCHECKED,
0);
SendDlgItemMessage(hWndDlg,
IDC_CHECK_AUTOSTART,
BM_SETCHECK,
g_cPrefs.bAutoStart ? BST_CHECKED : BST_UNCHECKED,
0);
SendDlgItemMessage(hWndDlg,
IDC_CHECK_AUTOENABLE,
BM_SETCHECK,
g_cPrefs.bAutoEnable ? BST_CHECKED : BST_UNCHECKED,
0);
}
break;
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case IDOK:
g_cPrefs.bDisplayOn = SendDlgItemMessage(hWndDlg, IDC_CHECK_DISPLAY_ON,
BM_GETCHECK, 0, 0) == BST_CHECKED;
g_cPrefs.bNoStandby = SendDlgItemMessage(hWndDlg, IDC_CHECK_NO_STANDBY,
BM_GETCHECK, 0, 0) == BST_CHECKED;
g_cPrefs.bAutoStart = SendDlgItemMessage(hWndDlg, IDC_CHECK_AUTOSTART,
BM_GETCHECK, 0, 0) == BST_CHECKED;
g_cPrefs.bAutoEnable = SendDlgItemMessage(hWndDlg, IDC_CHECK_AUTOENABLE,
BM_GETCHECK, 0, 0) == BST_CHECKED;
// save prefs to registry
PrefsSave();
// if currently enabled, commit configuration changes by disabling and re-enabling
if(g_bIsEnabled)
{
CaffeineSet(FALSE);
CaffeineSet(TRUE);
}
// close dialog
EndDialog(hWndDlg, IDOK);
break;
case IDCANCEL:
EndDialog(hWndDlg, IDCANCEL);
break;
};
}
break;
default:
return(FALSE);
};
return(TRUE);
}
/*
* Show preferences dialog
*/
void PrefsShow()
{
SetForegroundWindow(g_hTrayIcon);
DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_DIALOG_PREFS), g_hTrayIcon, PrefsDlgProc);
}
/*
* Tray icon callback
*/
LRESULT CALLBACK TrayIconWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
POINT cursorPos;
switch(uMsg)
{
case WM_CREATE:
{
g_hTrayMenu = CreatePopupMenu();
AppendMenu(g_hTrayMenu, MF_STRING | MF_UNCHECKED, ID_MENU_TOGGLE, _T("&Enable"));
AppendMenu(g_hTrayMenu, MF_SEPARATOR, 0, NULL);
AppendMenu(g_hTrayMenu, MF_STRING, ID_MENU_PREFS, _T("&Preferences"));
AppendMenu(g_hTrayMenu, MF_SEPARATOR, 0, NULL);
AppendMenu(g_hTrayMenu, MF_STRING, ID_MENU_EXIT, _T("E&xit"));
g_TrayIcon.cbSize = sizeof(g_TrayIcon);
g_TrayIcon.hWnd = hWnd;
g_TrayIcon.uID = ID_TRAYICON;
g_TrayIcon.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP;
g_TrayIcon.uCallbackMessage = WM_SHELLNOTIFY;
g_TrayIcon.hIcon = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ICON_DISABLED));
wcscpy_s(g_TrayIcon.szTip, sizeof(g_TrayIcon.szTip), _T("Caffeine (disabled)"));
Shell_NotifyIcon(NIM_ADD, &g_TrayIcon);
CaffeineSet(g_cPrefs.bAutoEnable);
return(0);
}
break;
case WM_CLOSE:
{
DestroyWindow(hWnd);
}
break;
case WM_DESTROY:
{
Shell_NotifyIcon(NIM_DELETE, &g_TrayIcon);
PostQuitMessage(0);
}
break;
case WM_SHELLNOTIFY:
{
if(wParam == ID_TRAYICON)
{
switch(lParam)
{
case WM_LBUTTONUP:
CaffeineToggle();
return(0);
case WM_RBUTTONUP:
GetCursorPos(&cursorPos);
SetForegroundWindow(hWnd);
TrackPopupMenu(g_hTrayMenu,
TPM_RIGHTALIGN,
cursorPos.x, cursorPos.y,
0, hWnd, NULL);
return(0);
};
}
}
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_MENU_EXIT:
DestroyWindow(hWnd);
return(0);
case ID_MENU_TOGGLE:
CaffeineToggle();
return(0);
case ID_MENU_PREFS:
PrefsShow();
return(0);
};
};
return(DefWindowProc(hWnd, uMsg, wParam, lParam));
}
/*
* Create tray icon
*/
BOOL TrayIconSetup()
{
WNDCLASSEX wc;
wc.cbSize = sizeof(wc);
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.lpfnWndProc = TrayIconWndProc;
wc.style = 0;
wc.hInstance = g_hInstance;
wc.hIcon = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ICON_ENABLED));
wc.hIconSm = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ICON_ENABLED));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = g_szClassName;
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszMenuName = NULL;
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, _T("Failed to register tray icon window class."), _T("Error"),
MB_ICONERROR | MB_OK);
return(FALSE);
}
g_hTrayIcon = CreateWindowEx(0,
g_szClassName,
_T("Caffeine"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
240, 120,
NULL, NULL,
g_hInstance,
NULL);
if(g_hTrayIcon == NULL)
{
MessageBox(NULL, _T("Failed to create tray icon window."), _T("Error"),
MB_ICONERROR | MB_OK);
return(FALSE);
}
return(TRUE);
}
/*
* Entry point
*/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG Msg;
g_hInstance = hInstance;
PrefsLoad();
if(!TrayIconSetup())
return(1);
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return((int)Msg.wParam);
}