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