104d68de55ed407c786bd295fc0819602714ad7f
[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 if (Sec[i]==pp.user.SpindownTimeoutAc)
193 {
194 SendDlgItemMessage(hwndDlg, IDC_DISKACLIST,
195 CB_SETCURSEL,
196 i-2,
197 (LPARAM)0);
198 }
199 if (Sec[i]==pp.user.SpindownTimeoutDc) // IdleTimeoutDc)
200 {
201 SendDlgItemMessage(hwndDlg, IDC_DISKDCLIST,
202 CB_SETCURSEL,
203 i-2,
204 (LPARAM)0);
205 }
206 if (Sec[i]==pp.user.IdleTimeoutAc)
207 {
208 SendDlgItemMessage(hwndDlg, IDC_STANDBYACLIST,
209 CB_SETCURSEL,
210 i,
211 (LPARAM)0);
212 }
213 if (Sec[i]==pp.user.IdleTimeoutDc)
214 {
215 SendDlgItemMessage(hwndDlg, IDC_STANDBYDCLIST,
216 CB_SETCURSEL,
217 i,
218 (LPARAM)0);
219 }
220
221 if (Sec[i]==pp.mach.DozeS4TimeoutAc)
222 {
223 SendDlgItemMessage(hwndDlg, IDC_HYBERNATEACLIST,
224 CB_SETCURSEL,
225 i,
226 (LPARAM)0);
227 }
228 if (Sec[i]==pp.mach.DozeS4TimeoutDc)
229 {
230 SendDlgItemMessage(hwndDlg, IDC_HYBERNATEDCLIST,
231 CB_SETCURSEL,
232 i,
233 (LPARAM)0);
234 }
235 }
236 }
237
238
239 BOOLEAN CALLBACK
240 callback_EnumPwrScheme(UINT uiIndex, DWORD dwName, LPTSTR sName, DWORD dwDesc,
241 LPWSTR sDesc, PPOWER_POLICY pp,LPARAM lParam)
242 {
243 int index;
244
245 UNREFERENCED_PARAMETER(lParam);
246 UNREFERENCED_PARAMETER(sDesc);
247 UNREFERENCED_PARAMETER(dwDesc);
248 UNREFERENCED_PARAMETER(dwName);
249
250 if (ValidatePowerPolicies(0,pp))
251 {
252 if (guiIndex >= MAX_POWER_POLICY)
253 {
254 // FIXME:
255 // Implement store power policy dynamically
256 return FALSE;
257 }
258
259
260 memcpy(&gPP[guiIndex], pp, sizeof(POWER_POLICY));
261 guiIndex++;
262
263 index = (int) SendMessage(hList,
264 CB_ADDSTRING,
265 0,
266 (LPARAM)sName);
267 if (index == CB_ERR)
268 return FALSE;
269
270 SendMessage(hList,
271 CB_SETITEMDATA,
272 index,
273 (LPARAM)uiIndex);
274
275 if (aps == uiIndex)
276 {
277 SendMessage(hList,
278 CB_SELECTSTRING,
279 TRUE,
280 (LPARAM)sName);
281 LoadConfig(GetParent(hList));
282 }
283 }
284 return TRUE;
285 }
286
287
288 static VOID
289 Pos_InitPage(HWND hwndDlg)
290 {
291 int ifrom=0,i=0,imin=0;
292 HWND hwnd = NULL;
293 TCHAR szName[MAX_PATH];
294 LRESULT index;
295
296 for(i=1;i<9;i++)
297 {
298 switch(i)
299 {
300 case 1:
301 hwnd=GetDlgItem(hwndDlg, IDC_MONITORACLIST);
302 imin=IDS_TIMEOUT1;
303 break;
304 case 2:
305 hwnd=GetDlgItem(hwndDlg, IDC_STANDBYACLIST);
306 imin=IDS_TIMEOUT1;
307 break;
308 case 3:
309 hwnd=GetDlgItem(hwndDlg, IDC_DISKACLIST);
310 imin=IDS_TIMEOUT3;
311 break;
312 case 4:
313 hwnd=GetDlgItem(hwndDlg, IDC_HYBERNATEACLIST);
314 imin=IDS_TIMEOUT3;
315 break;
316 case 5:
317 hwnd=GetDlgItem(hwndDlg, IDC_MONITORDCLIST);
318 imin=IDS_TIMEOUT1;
319 break;
320 case 6:
321 hwnd=GetDlgItem(hwndDlg, IDC_STANDBYDCLIST);
322 imin=IDS_TIMEOUT1;
323 break;
324 case 7:
325 hwnd=GetDlgItem(hwndDlg, IDC_DISKDCLIST);
326 imin=IDS_TIMEOUT3;
327 break;
328 case 8:
329 hwnd=GetDlgItem(hwndDlg, IDC_HYBERNATEDCLIST);
330 imin=IDS_TIMEOUT3;
331 break;
332 default:
333 return;
334 }
335 for (ifrom=imin;ifrom<(IDS_TIMEOUT15+1);ifrom++)
336 {
337 if (LoadString(hApplet, ifrom, szName, MAX_PATH))
338 {
339 index = SendMessage(hwnd,
340 CB_ADDSTRING,
341 0,
342 (LPARAM)szName);
343
344 if (index == CB_ERR)
345 return;
346
347 SendMessage(hwnd,
348 CB_SETITEMDATA,
349 index,
350 (LPARAM)Sec[ifrom-IDS_TIMEOUT16]);
351 }
352 }
353 if (LoadString(hApplet, IDS_TIMEOUT16, szName, MAX_PATH))
354 {
355 index = SendMessage(hwnd,
356 CB_ADDSTRING,
357 0,
358 (LPARAM)szName);
359 if (index == CB_ERR)
360 return;
361
362 SendMessage(hwnd,
363 CB_SETITEMDATA,
364 index,
365 (LPARAM)Sec[0]);
366 }
367 }
368 }
369
370
371 static VOID
372 Pos_SaveData(HWND hwndDlg)
373 {
374 INT iCurSel=0,tmp=0;
375
376 iCurSel = (INT) SendDlgItemMessage(hwndDlg, IDC_ENERGYLIST,
377 CB_GETCURSEL,
378 0,
379 0);
380 if (iCurSel == CB_ERR)
381 return;
382
383 tmp = (INT) SendDlgItemMessage(hwndDlg, IDC_MONITORDCLIST,
384 CB_GETCURSEL,
385 0,
386 (LPARAM)0);
387 if (tmp > 0 && tmp < 16)
388 {
389 gPP[iCurSel].user.VideoTimeoutAc = Sec[tmp];
390 }
391 tmp = (INT) SendDlgItemMessage(hwndDlg, IDC_MONITORDCLIST,
392 CB_GETCURSEL,
393 0,
394 (LPARAM)0);
395 if (tmp > 0 && tmp < 16)
396 {
397 gPP[iCurSel].user.VideoTimeoutDc = Sec[tmp];
398 }
399 tmp = (INT) SendDlgItemMessage(hwndDlg, IDC_DISKACLIST,
400 CB_GETCURSEL,
401 0,
402 (LPARAM)0);
403 if (tmp > 0 && tmp < 16)
404 {
405 gPP[iCurSel].user.SpindownTimeoutAc = Sec[tmp+2];
406 }
407 tmp = (INT) SendDlgItemMessage(hwndDlg, IDC_DISKDCLIST,
408 CB_GETCURSEL,
409 0,
410 (LPARAM)0);
411 if (tmp > 0 && tmp < 16)
412 {
413 gPP[iCurSel].user.SpindownTimeoutDc = Sec[tmp+2];
414 }
415 tmp = (INT) SendDlgItemMessage(hwndDlg, IDC_STANDBYACLIST,
416 CB_GETCURSEL,
417 0,
418 (LPARAM)0);
419 if (tmp > 0 && tmp < 16)
420 {
421 gPP[iCurSel].user.IdleTimeoutAc = Sec[tmp];
422 }
423 tmp = (INT) SendDlgItemMessage(hwndDlg, IDC_STANDBYDCLIST,
424 CB_GETCURSEL,
425 0,
426 (LPARAM)0);
427 if (tmp > 0 && tmp < 16)
428 {
429 gPP[iCurSel].user.IdleTimeoutDc = Sec[tmp];
430 }
431 tmp = (INT) SendDlgItemMessage(hwndDlg, IDC_HYBERNATEACLIST,
432 CB_GETCURSEL,
433 0,
434 (LPARAM)0);
435 if (tmp > 0 && tmp < 16)
436 {
437 gPP[iCurSel].mach.DozeS4TimeoutAc = Sec[tmp];
438 }
439 tmp = (INT) SendDlgItemMessage(hwndDlg, IDC_HYBERNATEDCLIST,
440 CB_GETCURSEL,
441 0,
442 (LPARAM)0);
443 if (tmp > 0 && tmp < 16)
444 {
445 gPP[iCurSel].mach.DozeS4TimeoutDc = Sec[tmp];
446 }
447
448 SetActivePwrScheme(iCurSel,NULL,&gPP[iCurSel]);
449 LoadConfig(hwndDlg);
450 }
451
452
453 static BOOLEAN
454 DelScheme(HWND hwnd)
455 {
456 INT iCurSel;
457 HWND hList;
458 TCHAR szBuf[1024], szBufT[1024];
459 UINT DelScheme;
460
461 hList = GetDlgItem(hwnd, IDC_ENERGYLIST);
462
463 iCurSel = SendMessage(hList, CB_GETCURSEL, 0, 0);
464 if (iCurSel == CB_ERR) return FALSE;
465
466 SendMessage(hList, CB_SETCURSEL, iCurSel, 0);
467
468 DelScheme = SendMessage(hList, CB_GETITEMDATA, (WPARAM)iCurSel, 0);
469 if (DelScheme == (UINT)CB_ERR) return FALSE;
470
471 LoadString(hApplet, IDS_DEL_SCHEME_TITLE, szBufT, sizeof(szBufT) / sizeof(TCHAR));
472 LoadString(hApplet, IDS_DEL_SCHEME, szBuf, sizeof(szBuf) / sizeof(TCHAR));
473
474 if (MessageBox(hwnd, szBuf, szBufT, MB_YESNO | MB_ICONQUESTION) == IDYES)
475 {
476 UINT Current;
477
478 if (GetActivePwrScheme(&Current))
479 {
480 SendMessage(hList, CB_SETCURSEL, (WPARAM)0, 0);
481 SendMessage(hList, CB_DELETESTRING, (WPARAM)iCurSel, 0);
482 if (Current == DelScheme) Pos_SaveData(hwnd);
483 }
484
485 if (DeletePwrScheme(DelScheme) != 0) return TRUE;
486 }
487
488 return FALSE;
489 }
490
491
492 static BOOLEAN
493 CreateEnergyList(HWND hwnd)
494 {
495 BOOLEAN retval;
496 POWER_POLICY pp;
497 SYSTEM_POWER_CAPABILITIES spc;
498
499 hList = hwnd;
500
501 if (!GetActivePwrScheme(&aps))
502 return FALSE;
503
504 if (!ReadGlobalPwrPolicy(&gGPP))
505 return FALSE;
506
507 if (!ReadPwrScheme(aps,&pp))
508 return FALSE;
509
510 if (!ValidatePowerPolicies(&gGPP,0))
511 return FALSE;
512
513 /*
514 if (!SetActivePwrScheme(aps,&gGPP,&pp))
515 return FALSE;
516 */
517
518 if (!GetPwrCapabilities(&spc))
519 return FALSE;
520
521 if (CanUserWritePwrScheme())
522 {
523 // TODO:
524 // Enable write / delete powerscheme button
525 }
526
527 Pos_InitPage(GetParent(hwnd));
528
529 if (!GetActivePwrScheme(&aps))
530 return FALSE;
531
532 retval = EnumPwrSchemes(callback_EnumPwrScheme, aps);
533
534 if(SendMessage(hwnd, CB_GETCOUNT, 0, 0) > 0)
535 {
536 EnableWindow(GetDlgItem(hwndDialog, IDC_DELETE_BTN),TRUE);
537 EnableWindow(GetDlgItem(hwndDialog, IDC_SAVEAS_BTN),TRUE);
538 }
539
540 return retval;
541 }
542
543
544 /* Property page dialog callback */
545 INT_PTR CALLBACK
546 PowerSchemesDlgProc(
547 HWND hwndDlg,
548 UINT uMsg,
549 WPARAM wParam,
550 LPARAM lParam
551 )
552 {
553 switch(uMsg)
554 {
555 case WM_INITDIALOG:
556 hPos = hwndDlg;
557 hwndDialog = hwndDlg;
558 if (!Pos_InitData())
559 {
560 // TODO:
561 // Initialization failed
562 // Handle error
563 MessageBox(hwndDlg,_T("Pos_InitData failed\n"), NULL, MB_OK);
564
565 }
566 if (!CreateEnergyList(GetDlgItem(hwndDlg, IDC_ENERGYLIST)))
567 {
568 // TODO:
569 // Initialization failed
570 // Handle error
571 MessageBox(hwndDlg,_T("CreateEnergyList failed\n"), NULL, MB_OK);
572 }
573 return TRUE;
574 case WM_COMMAND:
575 switch(LOWORD(wParam))
576 {
577 case IDC_ENERGYLIST:
578 if (HIWORD(wParam) == CBN_SELCHANGE)
579 {
580 LoadConfig(hwndDlg);
581 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
582 }
583 break;
584 case IDC_DELETE_BTN:
585 {
586 DelScheme(hwndDlg);
587 }
588 break;
589 case IDC_SAVEAS_BTN:
590 {
591
592 }
593 break;
594 case IDC_MONITORACLIST:
595 case IDC_MONITORDCLIST:
596 case IDC_DISKACLIST:
597 case IDC_DISKDCLIST:
598 case IDC_STANDBYACLIST:
599 case IDC_STANDBYDCLIST:
600 case IDC_HYBERNATEACLIST:
601 case IDC_HYBERNATEDCLIST:
602 if (HIWORD(wParam) == CBN_SELCHANGE)
603 {
604 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
605 }
606 break;
607 }
608 break;
609 case WM_NOTIFY:
610 {
611 LPNMHDR lpnm = (LPNMHDR)lParam;
612 if (lpnm->code == (UINT)PSN_APPLY)
613 {
614 Pos_SaveData(hwndDlg);
615 }
616 return TRUE;
617 }
618 break;
619 }
620 return FALSE;
621 }