[POWERCFG] Fix coding style and indentation. No code changes!
[reactos.git] / dll / cpl / powercfg / powershemes.c
1 /*
2 * PROJECT: ReactOS Power Configuration Applet
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/cpl/powercfg/powershemes.c
5 * PURPOSE: powerschemes tab of applet
6 * PROGRAMMERS: Alexander Wurzinger (Lohnegrim at gmx dot net)
7 * Johannes Anderwald (johannes.anderwald@reactos.org)
8 * Martin Rottensteiner
9 * Dmitry Chapyshev (lentind@yandex.ru)
10 */
11
12 #include "powercfg.h"
13
14 UINT Sec[]=
15 {
16 60,
17 120,
18 180,
19 300,
20 600,
21 900,
22 1200,
23 1500,
24 1800,
25 2700,
26 3600,
27 7200,
28 10800,
29 14400,
30 18000,
31 0
32 };
33
34 HWND hList = 0;
35 HWND hPos = 0;
36
37 unsigned aps = 0;
38
39 #define MAX_POWER_POLICY 20
40
41 POWER_POLICY gPP[MAX_POWER_POLICY];
42 UINT guiIndex = 0;
43 HWND hwndDialog;
44
45
46 BOOLEAN
47 Pos_InitData(VOID)
48 {
49 SYSTEM_POWER_CAPABILITIES spc;
50 /*
51 RECT rectCtl, rectDlg, rectCtl2;
52 LONG movetop = 0;
53 LONG moveright = 0;
54
55 if (GetWindowRect(hPos,&rectDlg))
56 {
57 if (GetWindowRect(GetDlgItem(hPos, IDC_SAT),&rectCtl2))
58 {
59 if (GetWindowRect(GetDlgItem(hPos, IDC_MONITOR),&rectCtl))
60 {
61 movetop=rectCtl.top - rectCtl2.top;
62 MoveWindow(GetDlgItem(hPos, IDC_MONITOR),rectCtl.left-rectDlg.left,rectCtl.top-rectDlg.top-movetop,rectCtl.right-rectCtl.left,rectCtl.bottom-rectCtl.top,FALSE);
63 if (GetWindowRect(GetDlgItem(hPos, IDC_DISK),&rectCtl))
64 {
65 MoveWindow(GetDlgItem(hPos, IDC_DISK),rectCtl.left-rectDlg.left,rectCtl.top-rectDlg.top-movetop,rectCtl.right-rectCtl.left,rectCtl.bottom-rectCtl.top,FALSE);
66 }
67 if (GetWindowRect(GetDlgItem(hPos, IDC_STANDBY),&rectCtl))
68 {
69 MoveWindow(GetDlgItem(hPos, IDC_STANDBY),rectCtl.left-rectDlg.left,rectCtl.top-rectDlg.top-movetop,rectCtl.right-rectCtl.left,rectCtl.bottom-rectCtl.top,FALSE);
70 }
71 if (GetWindowRect(GetDlgItem(hPos, IDC_HYBERNATE),&rectCtl))
72 {
73 MoveWindow(GetDlgItem(hPos, IDC_HYBERNATE),rectCtl.left-rectDlg.left,rectCtl.top-rectDlg.top-movetop,rectCtl.right-rectCtl.left,rectCtl.bottom-rectCtl.top,FALSE);
74 }
75 if (GetWindowRect(GetDlgItem(hPos, IDC_MONITORDCLIST),&rectCtl2))
76 {
77 movetop=movetop-8;
78 if (GetWindowRect(GetDlgItem(hPos, IDC_MONITORACLIST),&rectCtl))
79 {
80 moveright=rectCtl.right - rectCtl2.right;
81 MoveWindow(GetDlgItem(hPos, IDC_MONITORACLIST),rectCtl.left-rectDlg.left,rectCtl.top-rectDlg.top-movetop,rectCtl.right-rectCtl.left-moveright,rectCtl.bottom-rectCtl.top,FALSE);
82 if (GetWindowRect(GetDlgItem(hPos, IDC_DISKACLIST),&rectCtl))
83 {
84 MoveWindow(GetDlgItem(hPos, IDC_DISKACLIST),rectCtl.left-rectDlg.left,rectCtl.top-rectDlg.top-movetop,rectCtl.right-rectCtl.left-moveright,rectCtl.bottom-rectCtl.top,FALSE);
85 }
86 if (GetWindowRect(GetDlgItem(hPos, IDC_STANDBYACLIST),&rectCtl))
87 {
88 MoveWindow(GetDlgItem(hPos, IDC_STANDBYACLIST),rectCtl.left-rectDlg.left,rectCtl.top-rectDlg.top-movetop,rectCtl.right-rectCtl.left-moveright,rectCtl.bottom-rectCtl.top,FALSE);
89 }
90 if (GetWindowRect(GetDlgItem(hPos, IDC_HYBERNATEACLIST),&rectCtl))
91 {
92 MoveWindow(GetDlgItem(hPos, IDC_HYBERNATEACLIST),rectCtl.left-rectDlg.left,rectCtl.top-rectDlg.top-movetop,rectCtl.right-rectCtl.left-moveright,rectCtl.bottom-rectCtl.top,FALSE);
93 }
94 }
95 if (GetWindowRect(GetDlgItem(hPos, IDC_GRPDETAIL),&rectCtl))
96 {
97 MoveWindow(GetDlgItem(hPos, IDC_GRPDETAIL),rectCtl.left-rectDlg.left,rectCtl.top-rectDlg.top,rectCtl.right-rectCtl.left,rectCtl.bottom-rectCtl.top,FALSE);
98 }
99 }
100 }
101 }
102 }
103 }
104 */
105
106 if (!GetPwrCapabilities(&spc))
107 {
108 return FALSE;
109 }
110
111 if (!spc.SystemBatteriesPresent)
112 {
113 ShowWindow(GetDlgItem(hPos, IDC_SAT), FALSE);
114 ShowWindow(GetDlgItem(hPos, IDC_IAC), FALSE);
115 ShowWindow(GetDlgItem(hPos, IDC_SAC), FALSE);
116 ShowWindow(GetDlgItem(hPos, IDC_IDC), FALSE);
117 ShowWindow(GetDlgItem(hPos, IDC_SDC), FALSE);
118 ShowWindow(GetDlgItem(hPos, IDC_MONITORDCLIST), FALSE);
119 ShowWindow(GetDlgItem(hPos, IDC_DISKDCLIST), FALSE);
120 ShowWindow(GetDlgItem(hPos, IDC_STANDBYDCLIST), FALSE);
121 ShowWindow(GetDlgItem(hPos, IDC_HYBERNATEDCLIST), FALSE);
122 ShowWindow(GetDlgItem(hPos, IDC_HYBERNATEACLIST), spc.HiberFilePresent);
123
124 }
125 else
126 {
127 ShowWindow(GetDlgItem(hPos, IDC_HYBERNATEDCLIST), spc.HiberFilePresent);
128 ShowWindow(GetDlgItem(hPos, IDC_HYBERNATEACLIST), FALSE);
129 }
130
131
132 if (!(spc.SystemS1 ||spc.SystemS2 || spc.SystemS3))
133 {
134 ShowWindow(GetDlgItem(hPos, IDC_STANDBYACLIST), TRUE);
135 ShowWindow(GetDlgItem(hPos, IDC_STANDBYDCLIST), TRUE);
136 ShowWindow(GetDlgItem(hPos, IDC_STANDBY), TRUE);
137 }
138
139 ShowWindow(GetDlgItem(hPos, IDC_HYBERNATE), spc.HiberFilePresent);
140
141 return TRUE;
142 }
143
144
145 static VOID
146 LoadConfig(HWND hwndDlg)
147 {
148 INT i = 0, iCurSel = 0;
149 UINT uiIndex;
150 TCHAR szProfile[MAX_PATH];
151 TCHAR szTemp[MAX_PATH];
152 TCHAR szConfig[MAX_PATH];
153 POWER_POLICY pp;
154
155 iCurSel = (INT)SendDlgItemMessage(hwndDlg, IDC_ENERGYLIST,
156 CB_GETCURSEL,
157 0,
158 0);
159 if (iCurSel == CB_ERR)
160 return;
161
162 memcpy(&pp, &gPP[iCurSel], sizeof(POWER_POLICY));
163
164 uiIndex = (UINT)SendDlgItemMessage(hwndDlg, IDC_ENERGYLIST, CB_GETCURSEL, 0, 0);
165 if (uiIndex != (UINT)CB_ERR)
166 {
167 SendDlgItemMessage(hwndDlg, IDC_ENERGYLIST, CB_GETLBTEXT, uiIndex, (LPARAM)szProfile);
168 if (LoadString(hApplet, IDS_CONFIG1, szTemp, MAX_PATH))
169 {
170 _stprintf(szConfig,szTemp,szProfile);
171 SetWindowText(GetDlgItem(hwndDlg, IDC_GRPDETAIL), szConfig);
172 }
173 }
174
175 for (i = 0; i < 16; i++)
176 {
177 if (Sec[i] == pp.user.VideoTimeoutAc)
178 {
179 SendDlgItemMessage(hwndDlg, IDC_MONITORACLIST,
180 CB_SETCURSEL,
181 i,
182 (LPARAM)0);
183 }
184
185 if (Sec[i] == pp.user.VideoTimeoutDc)
186 {
187 SendDlgItemMessage(hwndDlg, IDC_MONITORDCLIST,
188 CB_SETCURSEL,
189 i,
190 (LPARAM)0);
191 }
192
193 if (Sec[i] == pp.user.SpindownTimeoutAc)
194 {
195 SendDlgItemMessage(hwndDlg, IDC_DISKACLIST,
196 CB_SETCURSEL,
197 i - 2,
198 (LPARAM)0);
199 }
200
201 if (Sec[i] == pp.user.SpindownTimeoutDc) // IdleTimeoutDc)
202 {
203 SendDlgItemMessage(hwndDlg, IDC_DISKDCLIST,
204 CB_SETCURSEL,
205 i - 2,
206 (LPARAM)0);
207 }
208
209 if (Sec[i] == pp.user.IdleTimeoutAc)
210 {
211 SendDlgItemMessage(hwndDlg, IDC_STANDBYACLIST,
212 CB_SETCURSEL,
213 i,
214 (LPARAM)0);
215 }
216
217 if (Sec[i] == pp.user.IdleTimeoutDc)
218 {
219 SendDlgItemMessage(hwndDlg, IDC_STANDBYDCLIST,
220 CB_SETCURSEL,
221 i,
222 (LPARAM)0);
223 }
224
225 if (Sec[i] == pp.mach.DozeS4TimeoutAc)
226 {
227 SendDlgItemMessage(hwndDlg, IDC_HYBERNATEACLIST,
228 CB_SETCURSEL,
229 i,
230 (LPARAM)0);
231 }
232
233 if (Sec[i] == pp.mach.DozeS4TimeoutDc)
234 {
235 SendDlgItemMessage(hwndDlg, IDC_HYBERNATEDCLIST,
236 CB_SETCURSEL,
237 i,
238 (LPARAM)0);
239 }
240 }
241 }
242
243
244 BOOLEAN CALLBACK
245 callback_EnumPwrScheme(UINT uiIndex, DWORD dwName, LPTSTR sName, DWORD dwDesc,
246 LPWSTR sDesc, PPOWER_POLICY pp, LPARAM lParam)
247 {
248 int index;
249
250 UNREFERENCED_PARAMETER(lParam);
251 UNREFERENCED_PARAMETER(sDesc);
252 UNREFERENCED_PARAMETER(dwDesc);
253 UNREFERENCED_PARAMETER(dwName);
254
255 if (ValidatePowerPolicies(0, pp))
256 {
257 if (guiIndex >= MAX_POWER_POLICY)
258 {
259 // FIXME:
260 // Implement store power policy dynamically
261 return FALSE;
262 }
263
264
265 memcpy(&gPP[guiIndex], pp, sizeof(POWER_POLICY));
266 guiIndex++;
267
268 index = (int)SendMessage(hList,
269 CB_ADDSTRING,
270 0,
271 (LPARAM)sName);
272 if (index == CB_ERR)
273 return FALSE;
274
275 SendMessage(hList,
276 CB_SETITEMDATA,
277 index,
278 (LPARAM)uiIndex);
279
280 if (aps == uiIndex)
281 {
282 SendMessage(hList,
283 CB_SELECTSTRING,
284 TRUE,
285 (LPARAM)sName);
286 LoadConfig(GetParent(hList));
287 }
288 }
289
290 return TRUE;
291 }
292
293
294 static VOID
295 Pos_InitPage(HWND hwndDlg)
296 {
297 int ifrom = 0, i = 0, imin = 0;
298 HWND hwnd = NULL;
299 TCHAR szName[MAX_PATH];
300 LRESULT index;
301
302 for (i = 1; i < 9; i++)
303 {
304 switch (i)
305 {
306 case 1:
307 hwnd = GetDlgItem(hwndDlg, IDC_MONITORACLIST);
308 imin = IDS_TIMEOUT1;
309 break;
310
311 case 2:
312 hwnd = GetDlgItem(hwndDlg, IDC_STANDBYACLIST);
313 imin = IDS_TIMEOUT1;
314 break;
315
316 case 3:
317 hwnd = GetDlgItem(hwndDlg, IDC_DISKACLIST);
318 imin = IDS_TIMEOUT3;
319 break;
320
321 case 4:
322 hwnd = GetDlgItem(hwndDlg, IDC_HYBERNATEACLIST);
323 imin = IDS_TIMEOUT3;
324 break;
325
326 case 5:
327 hwnd = GetDlgItem(hwndDlg, IDC_MONITORDCLIST);
328 imin = IDS_TIMEOUT1;
329 break;
330
331 case 6:
332 hwnd = GetDlgItem(hwndDlg, IDC_STANDBYDCLIST);
333 imin = IDS_TIMEOUT1;
334 break;
335
336 case 7:
337 hwnd = GetDlgItem(hwndDlg, IDC_DISKDCLIST);
338 imin = IDS_TIMEOUT3;
339 break;
340
341 case 8:
342 hwnd = GetDlgItem(hwndDlg, IDC_HYBERNATEDCLIST);
343 imin = IDS_TIMEOUT3;
344 break;
345
346 default:
347 return;
348 }
349
350 for (ifrom = imin; ifrom < (IDS_TIMEOUT15 + 1); ifrom++)
351 {
352 if (LoadString(hApplet, ifrom, szName, MAX_PATH))
353 {
354 index = SendMessage(hwnd,
355 CB_ADDSTRING,
356 0,
357 (LPARAM)szName);
358 if (index == CB_ERR)
359 return;
360
361 SendMessage(hwnd,
362 CB_SETITEMDATA,
363 index,
364 (LPARAM)Sec[ifrom - IDS_TIMEOUT16]);
365 }
366 }
367
368 if (LoadString(hApplet, IDS_TIMEOUT16, szName, MAX_PATH))
369 {
370 index = SendMessage(hwnd,
371 CB_ADDSTRING,
372 0,
373 (LPARAM)szName);
374 if (index == CB_ERR)
375 return;
376
377 SendMessage(hwnd,
378 CB_SETITEMDATA,
379 index,
380 (LPARAM)Sec[0]);
381 }
382 }
383 }
384
385
386 static VOID
387 Pos_SaveData(HWND hwndDlg)
388 {
389 INT iCurSel = 0, tmp = 0;
390
391 iCurSel = (INT)SendDlgItemMessage(hwndDlg, IDC_ENERGYLIST,
392 CB_GETCURSEL,
393 0,
394 0);
395 if (iCurSel == CB_ERR)
396 return;
397
398 tmp = (INT)SendDlgItemMessage(hwndDlg, IDC_MONITORDCLIST,
399 CB_GETCURSEL,
400 0,
401 (LPARAM)0);
402 if (tmp > 0 && tmp < 16)
403 {
404 gPP[iCurSel].user.VideoTimeoutAc = Sec[tmp];
405 }
406
407 tmp = (INT)SendDlgItemMessage(hwndDlg, IDC_MONITORDCLIST,
408 CB_GETCURSEL,
409 0,
410 (LPARAM)0);
411 if (tmp > 0 && tmp < 16)
412 {
413 gPP[iCurSel].user.VideoTimeoutDc = Sec[tmp];
414 }
415
416 tmp = (INT)SendDlgItemMessage(hwndDlg, IDC_DISKACLIST,
417 CB_GETCURSEL,
418 0,
419 (LPARAM)0);
420 if (tmp > 0 && tmp < 16)
421 {
422 gPP[iCurSel].user.SpindownTimeoutAc = Sec[tmp + 2];
423 }
424
425 tmp = (INT)SendDlgItemMessage(hwndDlg, IDC_DISKDCLIST,
426 CB_GETCURSEL,
427 0,
428 (LPARAM)0);
429 if (tmp > 0 && tmp < 16)
430 {
431 gPP[iCurSel].user.SpindownTimeoutDc = Sec[tmp + 2];
432 }
433
434 tmp = (INT)SendDlgItemMessage(hwndDlg, IDC_STANDBYACLIST,
435 CB_GETCURSEL,
436 0,
437 (LPARAM)0);
438 if (tmp > 0 && tmp < 16)
439 {
440 gPP[iCurSel].user.IdleTimeoutAc = Sec[tmp];
441 }
442
443 tmp = (INT)SendDlgItemMessage(hwndDlg, IDC_STANDBYDCLIST,
444 CB_GETCURSEL,
445 0,
446 (LPARAM)0);
447 if (tmp > 0 && tmp < 16)
448 {
449 gPP[iCurSel].user.IdleTimeoutDc = Sec[tmp];
450 }
451
452 tmp = (INT)SendDlgItemMessage(hwndDlg, IDC_HYBERNATEACLIST,
453 CB_GETCURSEL,
454 0,
455 (LPARAM)0);
456 if (tmp > 0 && tmp < 16)
457 {
458 gPP[iCurSel].mach.DozeS4TimeoutAc = Sec[tmp];
459 }
460
461 tmp = (INT)SendDlgItemMessage(hwndDlg, IDC_HYBERNATEDCLIST,
462 CB_GETCURSEL,
463 0,
464 (LPARAM)0);
465 if (tmp > 0 && tmp < 16)
466 {
467 gPP[iCurSel].mach.DozeS4TimeoutDc = Sec[tmp];
468 }
469
470 SetActivePwrScheme(iCurSel, NULL, &gPP[iCurSel]);
471 LoadConfig(hwndDlg);
472 }
473
474
475 static BOOLEAN
476 DelScheme(HWND hwnd)
477 {
478 INT iCurSel;
479 HWND hList;
480 TCHAR szBuf[1024], szBufT[1024];
481 UINT DelScheme;
482
483 hList = GetDlgItem(hwnd, IDC_ENERGYLIST);
484
485 iCurSel = SendMessage(hList, CB_GETCURSEL, 0, 0);
486 if (iCurSel == CB_ERR)
487 return FALSE;
488
489 SendMessage(hList, CB_SETCURSEL, iCurSel, 0);
490
491 DelScheme = SendMessage(hList, CB_GETITEMDATA, (WPARAM)iCurSel, 0);
492 if (DelScheme == (UINT)CB_ERR)
493 return FALSE;
494
495 LoadString(hApplet, IDS_DEL_SCHEME_TITLE, szBufT, sizeof(szBufT) / sizeof(TCHAR));
496 LoadString(hApplet, IDS_DEL_SCHEME, szBuf, sizeof(szBuf) / sizeof(TCHAR));
497
498 if (MessageBox(hwnd, szBuf, szBufT, MB_YESNO | MB_ICONQUESTION) == IDYES)
499 {
500 UINT Current;
501
502 if (GetActivePwrScheme(&Current))
503 {
504 SendMessage(hList, CB_SETCURSEL, (WPARAM)0, 0);
505 SendMessage(hList, CB_DELETESTRING, (WPARAM)iCurSel, 0);
506 if (Current == DelScheme)
507 Pos_SaveData(hwnd);
508 }
509
510 if (DeletePwrScheme(DelScheme) != 0)
511 return TRUE;
512 }
513
514 return FALSE;
515 }
516
517
518 static BOOLEAN
519 CreateEnergyList(HWND hwnd)
520 {
521 BOOLEAN retval;
522 POWER_POLICY pp;
523 SYSTEM_POWER_CAPABILITIES spc;
524
525 hList = hwnd;
526
527 if (!GetActivePwrScheme(&aps))
528 return FALSE;
529
530 if (!ReadGlobalPwrPolicy(&gGPP))
531 return FALSE;
532
533 if (!ReadPwrScheme(aps, &pp))
534 return FALSE;
535
536 if (!ValidatePowerPolicies(&gGPP, 0))
537 return FALSE;
538
539 /*
540 if (!SetActivePwrScheme(aps, &gGPP, &pp))
541 return FALSE;
542 */
543
544 if (!GetPwrCapabilities(&spc))
545 return FALSE;
546
547 if (CanUserWritePwrScheme())
548 {
549 // TODO:
550 // Enable write / delete powerscheme button
551 }
552
553 Pos_InitPage(GetParent(hwnd));
554
555 if (!GetActivePwrScheme(&aps))
556 return FALSE;
557
558 retval = EnumPwrSchemes(callback_EnumPwrScheme, aps);
559
560 if (SendMessage(hwnd, CB_GETCOUNT, 0, 0) > 0)
561 {
562 EnableWindow(GetDlgItem(hwndDialog, IDC_DELETE_BTN), TRUE);
563 EnableWindow(GetDlgItem(hwndDialog, IDC_SAVEAS_BTN), TRUE);
564 }
565
566 return retval;
567 }
568
569
570 /* Property page dialog callback */
571 INT_PTR CALLBACK
572 PowerSchemesDlgProc(
573 HWND hwndDlg,
574 UINT uMsg,
575 WPARAM wParam,
576 LPARAM lParam)
577 {
578 switch(uMsg)
579 {
580 case WM_INITDIALOG:
581 hPos = hwndDlg;
582 hwndDialog = hwndDlg;
583 if (!Pos_InitData())
584 {
585 // TODO:
586 // Initialization failed
587 // Handle error
588 MessageBox(hwndDlg,_T("Pos_InitData failed\n"), NULL, MB_OK);
589 }
590
591 if (!CreateEnergyList(GetDlgItem(hwndDlg, IDC_ENERGYLIST)))
592 {
593 // TODO:
594 // Initialization failed
595 // Handle error
596 MessageBox(hwndDlg,_T("CreateEnergyList failed\n"), NULL, MB_OK);
597 }
598 return TRUE;
599
600 case WM_COMMAND:
601 switch(LOWORD(wParam))
602 {
603 case IDC_ENERGYLIST:
604 if (HIWORD(wParam) == CBN_SELCHANGE)
605 {
606 LoadConfig(hwndDlg);
607 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
608 }
609 break;
610
611 case IDC_DELETE_BTN:
612 DelScheme(hwndDlg);
613 break;
614
615 case IDC_SAVEAS_BTN:
616 break;
617
618 case IDC_MONITORACLIST:
619 case IDC_MONITORDCLIST:
620 case IDC_DISKACLIST:
621 case IDC_DISKDCLIST:
622 case IDC_STANDBYACLIST:
623 case IDC_STANDBYDCLIST:
624 case IDC_HYBERNATEACLIST:
625 case IDC_HYBERNATEDCLIST:
626 if (HIWORD(wParam) == CBN_SELCHANGE)
627 {
628 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
629 }
630 break;
631 }
632 break;
633
634 case WM_NOTIFY:
635 {
636 LPNMHDR lpnm = (LPNMHDR)lParam;
637 if (lpnm->code == (UINT)PSN_APPLY)
638 {
639 Pos_SaveData(hwndDlg);
640 }
641 return TRUE;
642 }
643 break;
644 }
645
646 return FALSE;
647 }