[SYSDM]
[reactos.git] / reactos / dll / cpl / powercfg / advanced.c
1 /* $Id$
2 *
3 * PROJECT: ReactOS Power Configuration Applet
4 * LICENSE: GPL - See COPYING in the top level directory
5 * FILE: dll/cpl/powercfg/advanced.c
6 * PURPOSE: advanced tab of applet
7 * PROGRAMMERS: Alexander Wurzinger (Lohnegrim at gmx dot net)
8 * Johannes Anderwald (johannes.anderwald@student.tugraz.at)
9 * Martin Rottensteiner
10 * Dmitry Chapyshev (lentind@yandex.ru)
11 */
12
13 //#ifndef NSTATUS
14 //typedef long NTSTATUS;
15 //#endif
16
17 #include "ntstatus.h"
18 #define WIN32_NO_STATUS
19 #include <windows.h>
20 #include <commctrl.h>
21 #include <cpl.h>
22 #include "resource.h"
23 #include "powercfg.h"
24
25 HWND hAdv = 0;
26
27 static POWER_ACTION g_SystemBatteries[3];
28 static POWER_ACTION g_PowerButton[5];
29 static POWER_ACTION g_SleepButton[5];
30
31
32 static VOID
33 AddItem(HWND hDlgCtrl, INT ResourceId, LPARAM lParam, POWER_ACTION * lpAction)
34 {
35 TCHAR szBuffer[MAX_PATH];
36 LRESULT Index;
37 if (LoadString(hApplet, ResourceId, szBuffer, MAX_PATH) < MAX_PATH)
38 {
39 Index = SendMessage(hDlgCtrl, CB_ADDSTRING, 0, (LPARAM)szBuffer);
40 if (Index != CB_ERR)
41 {
42 SendMessage(hDlgCtrl, CB_SETITEMDATA, (WPARAM)Index, lParam);
43 lpAction[Index] = (POWER_ACTION)lParam;
44 }
45 }
46 }
47
48 static INT
49 FindActionIndex(POWER_ACTION * lpAction, DWORD dwActionSize, POWER_ACTION poAction)
50 {
51 INT Index;
52
53 for (Index = 0; Index < (INT)dwActionSize; Index++)
54 {
55 if (lpAction[Index] == poAction)
56 return Index;
57 }
58
59 return -1;
60 }
61
62 static BOOLEAN
63 IsBatteryUsed(VOID)
64 {
65 SYSTEM_BATTERY_STATE sbs;
66
67 if (CallNtPowerInformation(SystemBatteryState,NULL, (ULONG)0, &sbs, sizeof(SYSTEM_BATTERY_STATE)) == STATUS_SUCCESS)
68 {
69 if (sbs.BatteryPresent)
70 {
71 if (sbs.AcOnLine)
72 {
73 return FALSE;
74 }
75 return TRUE;
76 }
77 }
78
79 return FALSE;
80 }
81
82 POWER_ACTION
83 GetPowerActionFromPolicy(POWER_ACTION_POLICY *Policy)
84 {
85 POWER_ACTION poAction = PowerActionNone;
86 /*
87
88 TCHAR szBuffer[MAX_PATH];
89
90 // Note: Windows XP SP2+ does not return the PowerAction code
91 // for PowerActionWarmEject + PowerActionShutdown but sets it
92 // to PowerActionNone and sets the Flags & EventCode
93
94
95 _stprintf(szBuffer, L"Action: %x EventCode %x Flags %x",Policy->Action, Policy->EventCode, Policy->Flags);
96 MessageBoxW(NULL, szBuffer, NULL, MB_OK);
97
98 */
99
100 if (Policy->Action == PowerActionNone)
101 {
102 if (Policy->Flags == (POWER_ACTION_UI_ALLOWED | POWER_ACTION_QUERY_ALLOWED))
103 {
104 if (Policy->EventCode == POWER_FORCE_TRIGGER_RESET)
105 {
106 poAction = PowerActionNone;
107 }
108 else if (Policy->EventCode == POWER_USER_NOTIFY_BUTTON)
109 {
110 poAction = PowerActionWarmEject;
111 }
112 else if (Policy->EventCode == POWER_USER_NOTIFY_SHUTDOWN)
113 {
114 poAction = PowerActionShutdown;
115 }
116 }
117 }
118 else
119 {
120 poAction = Policy->Action;
121 }
122
123 return poAction;
124 }
125
126 VOID
127 ShowCurrentPowerActionPolicy(HWND hDlgCtrl,
128 POWER_ACTION *lpAction,
129 DWORD dwActionSize,
130 POWER_ACTION_POLICY *Policy)
131 {
132 int poActionIndex;
133 POWER_ACTION poAction;
134
135 poAction = GetPowerActionFromPolicy(Policy);
136 poActionIndex = FindActionIndex(lpAction, dwActionSize, poAction);
137
138 if (poActionIndex < 0)
139 {
140 return;
141 }
142
143 SendMessage(hDlgCtrl, CB_SETCURSEL, (WPARAM)poActionIndex, (LPARAM)0);
144 }
145
146 BOOLEAN
147 SaveCurrentPowerActionPolicy(IN HWND hDlgCtrl,
148 OUT POWER_ACTION_POLICY *Policy)
149 {
150 LRESULT Index;
151 LRESULT ItemData;
152
153 Index = SendMessage(hDlgCtrl, CB_GETCURSEL, 0, 0);
154 if (Index == CB_ERR)
155 return FALSE;
156
157 ItemData = SendMessage(hDlgCtrl, CB_GETITEMDATA, (WPARAM)Index, 0);
158 if (ItemData == CB_ERR)
159 return FALSE;
160
161 switch(ItemData)
162 {
163 case PowerActionNone:
164 Policy->Action = PowerActionNone;
165 Policy->EventCode = POWER_FORCE_TRIGGER_RESET;
166 break;
167 case PowerActionWarmEject:
168 Policy->Action = PowerActionNone;
169 Policy->EventCode = POWER_USER_NOTIFY_BUTTON;
170 break;
171 case PowerActionShutdown:
172 Policy->Action = PowerActionNone;
173 Policy->EventCode = POWER_USER_NOTIFY_SHUTDOWN;
174 break;
175 case PowerActionSleep:
176 case PowerActionHibernate:
177 Policy->Action = (POWER_ACTION)ItemData;
178 Policy->EventCode = 0;
179 break;
180 default:
181 return FALSE;
182 }
183 Policy->Flags = (POWER_ACTION_UI_ALLOWED | POWER_ACTION_QUERY_ALLOWED);
184
185 return TRUE;
186 }
187
188
189
190
191 //-------------------------------------------------------------------
192
193 VOID
194 ShowCurrentPowerActionPolicies(HWND hwndDlg)
195 {
196 TCHAR szAction[MAX_PATH];
197
198 if (!IsBatteryUsed())
199 {
200 #if 0
201 /* expiremental code */
202 // ShowCurrentPowerButtonAcAction(hList2,
203 ShowCurrentPowerActionPolicy(GetDlgItem(hwndDlg, IDC_POWERBUTTON),
204 g_SystemBatteries,
205 sizeof(g_SystemBatteries) / sizeof(POWER_ACTION),
206 &gGPP.user.LidCloseAc);
207 #else
208 if (LoadString(hApplet, IDS_PowerActionNone1+gGPP.user.LidCloseAc.Action, szAction, MAX_PATH))
209 {
210 SendDlgItemMessage(hwndDlg, IDC_LIDCLOSE,
211 CB_SELECTSTRING,
212 TRUE,
213 (LPARAM)szAction);
214 }
215 #endif
216 ShowCurrentPowerActionPolicy(GetDlgItem(hwndDlg, IDC_POWERBUTTON),
217 g_PowerButton,
218 sizeof(g_PowerButton) / sizeof(POWER_ACTION),
219 &gGPP.user.PowerButtonAc);
220
221 #if 0
222 /* expiremental code */
223 ShowCurrentPowerActionPolicy(GetDlgItem(hwndDlg, IDC_SLEEPBUTTON),
224 g_SleepButton,
225 sizeof(g_SleepButton) / sizeof(POWER_ACTION),
226 &gGPP.user.SleepButtonAc);
227 #else
228 if (LoadString(hApplet, IDS_PowerActionNone1+gGPP.user.SleepButtonAc.Action, szAction, MAX_PATH))
229 {
230 SendDlgItemMessage(hwndDlg, IDC_SLEEPBUTTON,
231 CB_SELECTSTRING,
232 TRUE,
233 (LPARAM)szAction);
234 }
235 #endif
236 }
237 else
238 {
239 #if 0
240
241 ShowCurrentPowerActionPolicy(GetDlgItem(hwndDlg, IDC_LIDCLOSE),
242 g_SleepButton,
243 sizeof(g_SleepButton) / sizeof(POWER_ACTION),
244 &gGPP.user.LidCloseDc);
245
246 ShowCurrentPowerActionPolicy(GetDlgItem(hwndDlg, IDC_POWERBUTTON),
247 g_SleepButton,
248 sizeof(g_SleepButton) / sizeof(POWER_ACTION),
249 &gGPP.user.PowerButtonDc);
250
251 ShowCurrentPowerActionPolicy(GetDlgItem(hwndDlg, IDC_SLEEPBUTTON),
252 g_SleepButton,
253 sizeof(g_SleepButton) / sizeof(POWER_ACTION),
254 &gGPP.user.SleepButtonDc);
255 #else
256 if (LoadString(hApplet, IDS_PowerActionNone1+gGPP.user.LidCloseDc.Action, szAction, MAX_PATH))
257 {
258 SendDlgItemMessage(hwndDlg, IDC_LIDCLOSE,
259 CB_SELECTSTRING,
260 TRUE,
261 (LPARAM)szAction);
262 }
263 if (LoadString(hApplet, IDS_PowerActionNone1+gGPP.user.PowerButtonDc.Action, szAction, MAX_PATH))
264 {
265 SendDlgItemMessage(hwndDlg, IDC_POWERBUTTON,
266 CB_SELECTSTRING,
267 TRUE,
268 (LPARAM)szAction);
269 }
270 if (LoadString(hApplet, IDS_PowerActionNone1+gGPP.user.SleepButtonDc.Action, szAction, MAX_PATH))
271 {
272 SendDlgItemMessage(hwndDlg, IDC_SLEEPBUTTON,
273 CB_SELECTSTRING,
274 TRUE,
275 (LPARAM)szAction);
276 }
277 #endif
278 }
279 }
280
281 VOID
282 Adv_InitDialog(VOID)
283 {
284 HWND hList1;
285 HWND hList2;
286 HWND hList3;
287
288 BOOLEAN bSuspend = FALSE;
289 BOOLEAN bHibernate;
290 BOOLEAN bShutdown;
291
292 SYSTEM_POWER_CAPABILITIES spc;
293
294 CheckDlgButton(hAdv,
295 IDC_SYSTRAYBATTERYMETER,
296 gGPP.user.GlobalFlags & EnableSysTrayBatteryMeter ? BST_CHECKED : BST_UNCHECKED);
297 CheckDlgButton(hAdv,
298 IDC_PASSWORDLOGON,
299 gGPP.user.GlobalFlags & EnablePasswordLogon ? BST_CHECKED : BST_UNCHECKED);
300 CheckDlgButton(hAdv,
301 IDC_VIDEODIMDISPLAY,
302 gGPP.user.GlobalFlags & EnableVideoDimDisplay ? BST_CHECKED : BST_UNCHECKED);
303
304 GetPwrCapabilities(&spc);
305
306 if (spc.SystemS1 || spc.SystemS2 || spc.SystemS3)
307 bSuspend=TRUE;
308
309 bHibernate = spc.HiberFilePresent;
310 bShutdown = spc.SystemS5;
311
312 hList1 = GetDlgItem(hAdv, IDC_LIDCLOSE);
313 SendMessage(hList1, CB_RESETCONTENT, 0, 0);
314
315 memset(g_SystemBatteries, 0x0, sizeof(g_SystemBatteries));
316 if (spc.SystemBatteriesPresent)
317 {
318 AddItem(hList1, IDS_PowerActionNone1, (LPARAM)PowerActionNone, g_SystemBatteries);
319
320 if (bSuspend)
321 {
322 AddItem(hList1, IDS_PowerActionSleep, (LPARAM)PowerActionSleep, g_SystemBatteries);
323 }
324
325 if (bHibernate)
326 {
327 AddItem(hList1, IDS_PowerActionHibernate, (LPARAM)PowerActionHibernate, g_SystemBatteries);
328 }
329 }
330 else
331 {
332 ShowWindow(GetDlgItem(hAdv, IDC_VIDEODIMDISPLAY), FALSE);
333 ShowWindow(GetDlgItem(hAdv, IDC_SLIDCLOSE), FALSE);
334 ShowWindow(hList1, FALSE);
335 }
336
337 hList2 = GetDlgItem(hAdv, IDC_POWERBUTTON);
338 SendMessage(hList2, CB_RESETCONTENT, 0, 0);
339
340 memset(g_PowerButton, 0x0, sizeof(g_PowerButton));
341 if (spc.PowerButtonPresent)
342 {
343 AddItem(hList2, IDS_PowerActionNone1, (LPARAM)PowerActionNone, g_PowerButton);
344 AddItem(hList2, IDS_PowerActionWarmEject, (LPARAM)PowerActionWarmEject, g_PowerButton);
345
346 if (bSuspend)
347 {
348 AddItem(hList2, IDS_PowerActionSleep, (LPARAM)PowerActionSleep, g_PowerButton);
349 }
350
351 if (bHibernate)
352 {
353 AddItem(hList2, IDS_PowerActionHibernate, (LPARAM)PowerActionHibernate, g_PowerButton);
354
355 }
356 if (bShutdown)
357 {
358 AddItem(hList2, IDS_PowerActionShutdown, (LPARAM)PowerActionShutdown, g_PowerButton);
359 }
360 }
361 else
362 {
363 ShowWindow(GetDlgItem(hAdv, IDC_SPOWERBUTTON), FALSE);
364 ShowWindow(hList2, FALSE);
365 }
366
367 hList3=GetDlgItem(hAdv, IDC_SLEEPBUTTON);
368 SendMessage(hList3, CB_RESETCONTENT, 0, 0);
369 memset(g_SleepButton, 0x0, sizeof(g_SleepButton));
370
371 if (spc.SleepButtonPresent)
372 {
373 AddItem(hList3, IDS_PowerActionNone1, (LPARAM)PowerActionNone, g_SleepButton);
374 AddItem(hList3, IDS_PowerActionWarmEject, (LPARAM)PowerActionWarmEject, g_SleepButton);
375
376 if (bSuspend)
377 {
378 AddItem(hList3, IDS_PowerActionSleep, (LPARAM)PowerActionSleep, g_SleepButton);
379 }
380
381 if (bHibernate)
382 {
383 AddItem(hList3, IDS_PowerActionHibernate, (LPARAM)PowerActionHibernate, g_SleepButton);
384 }
385
386 if (bShutdown)
387 {
388 AddItem(hList3, IDS_PowerActionShutdown, (LPARAM)PowerActionShutdown, g_SleepButton);
389 }
390 }
391 else
392 {
393 ShowWindow(GetDlgItem(hAdv, IDC_SSLEEPBUTTON), FALSE);
394 ShowWindow(hList3, FALSE);
395 }
396
397 if (ReadGlobalPwrPolicy(&gGPP))
398 {
399 ShowCurrentPowerActionPolicies(hAdv);
400 }
401 }
402
403
404 static VOID
405 Adv_SaveData(HWND hwndDlg)
406 {
407 BOOL bSystrayBatteryMeter;
408 BOOL bPasswordLogon;
409 BOOL bVideoDimDisplay;
410
411 bSystrayBatteryMeter =
412 (IsDlgButtonChecked(hwndDlg, IDC_SYSTRAYBATTERYMETER) == BST_CHECKED);
413
414 bPasswordLogon =
415 (IsDlgButtonChecked(hwndDlg, IDC_PASSWORDLOGON) == BST_CHECKED);
416
417 bVideoDimDisplay =
418 (IsDlgButtonChecked(hwndDlg, IDC_VIDEODIMDISPLAY) == BST_CHECKED);
419
420 if (bSystrayBatteryMeter)
421 {
422 if (!(gGPP.user.GlobalFlags & EnableSysTrayBatteryMeter))
423 {
424 gGPP.user.GlobalFlags = gGPP.user.GlobalFlags + EnableSysTrayBatteryMeter;
425 }
426 }
427 else
428 {
429 if ((gGPP.user.GlobalFlags & EnableSysTrayBatteryMeter))
430 {
431 gGPP.user.GlobalFlags = gGPP.user.GlobalFlags - EnableSysTrayBatteryMeter;
432 }
433 }
434
435 if (bPasswordLogon)
436 {
437 if (!(gGPP.user.GlobalFlags & EnablePasswordLogon))
438 {
439 gGPP.user.GlobalFlags = gGPP.user.GlobalFlags + EnablePasswordLogon;
440 }
441 }
442 else
443 {
444 if ((gGPP.user.GlobalFlags & EnablePasswordLogon))
445 {
446 gGPP.user.GlobalFlags = gGPP.user.GlobalFlags - EnablePasswordLogon;
447 }
448 }
449
450 if (bVideoDimDisplay)
451 {
452 if (!(gGPP.user.GlobalFlags & EnableVideoDimDisplay))
453 {
454 gGPP.user.GlobalFlags = gGPP.user.GlobalFlags + EnableVideoDimDisplay;
455 }
456 }
457 else
458 {
459 if ((gGPP.user.GlobalFlags & EnableVideoDimDisplay))
460 {
461 gGPP.user.GlobalFlags = gGPP.user.GlobalFlags - EnableVideoDimDisplay;
462 }
463 }
464
465 if (!IsBatteryUsed())
466 {
467 SaveCurrentPowerActionPolicy(GetDlgItem(hwndDlg, IDC_POWERBUTTON), &gGPP.user.PowerButtonAc);
468 #if 0
469 SaveCurrentPowerActionPolicy(GetDlgItem(hwndDlg, IDC_LIDCLOSE), &gGPP.user.LidCloseAc);
470 SaveCurrentPowerActionPolicy(GetDlgItem(hwndDlg, IDC_SLEEPBUTTON), &gGPP.user.SleepButtonAc);
471 #endif
472 }
473 else
474 {
475 #if 0
476 SaveCurrentPowerActionPolicy(GetDlgItem(hwndDlg, IDC_POWERBUTTON), &gGPP.user.PowerButtonDc);
477 SaveCurrentPowerActionPolicy(GetDlgItem(hwndDlg, IDC_LIDCLOSE), &gGPP.user.LidCloseDc);
478 SaveCurrentPowerActionPolicy(GetDlgItem(hwndDlg, IDC_SLEEPBUTTON), &gGPP.user.SleepButtonDc);
479 #endif
480 }
481
482 if (!WriteGlobalPwrPolicy(&gGPP))
483 {
484 MessageBox(hwndDlg, L"WriteGlobalPwrPolicy failed", NULL, MB_OK);
485 }
486
487 Adv_InitDialog();
488 }
489
490 /* Property page dialog callback */
491 INT_PTR CALLBACK
492 AdvancedDlgProc(HWND hwndDlg,
493 UINT uMsg,
494 WPARAM wParam,
495 LPARAM lParam)
496 {
497 switch(uMsg)
498 {
499 case WM_INITDIALOG:
500 hAdv = hwndDlg;
501 Adv_InitDialog();
502 return TRUE;
503 break;
504 case WM_COMMAND:
505 switch(LOWORD(wParam))
506 {
507 case IDC_SYSTRAYBATTERYMETER:
508 case IDC_PASSWORDLOGON:
509 case IDC_VIDEODIMDISPLAY:
510 if (HIWORD(wParam) == BN_CLICKED)
511 {
512 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
513 }
514 break;
515 case IDC_LIDCLOSE:
516 case IDC_POWERBUTTON:
517 case IDC_SLEEPBUTTON:
518 if (HIWORD(wParam) == CBN_SELCHANGE)
519 {
520 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
521 }
522 break;
523 }
524 break;
525 case WM_NOTIFY:
526 {
527 LPNMHDR lpnm = (LPNMHDR)lParam;
528 if (lpnm->code == (UINT)PSN_APPLY)
529 {
530 Adv_SaveData(hwndDlg);
531 }
532 return TRUE;
533 }
534 }
535 return FALSE;
536 }