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