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