ReactOS  0.4.13-dev-455-g28ed234
advanced.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Power Configuration Applet
3  * LICENSE: GPL - See COPYING in the top level directory
4  * FILE: dll/cpl/powercfg/advanced.c
5  * PURPOSE: advanced 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 
17 
18 
19 static
20 VOID
22 {
23  HWND hwndTaskBar;
24 
25  hwndTaskBar = FindWindowW(L"SystemTray_Main", NULL);
26  if (hwndTaskBar == NULL)
27  return;
28 
29  SendMessageW(hwndTaskBar, WM_USER + 220, 1, bEnabled);
30 }
31 
32 static
33 BOOL
35 {
36  HWND hwndTaskBar;
37 
38  hwndTaskBar = FindWindowW(L"SystemTray_Main", NULL);
39  if (hwndTaskBar == NULL)
40  return FALSE;
41 
42  return (BOOL)SendMessageW(hwndTaskBar, WM_USER + 221, 1, 0);
43 }
44 
45 static VOID
46 AddItem(HWND hDlgCtrl, INT ResourceId, LPARAM lParam, POWER_ACTION * lpAction)
47 {
48  TCHAR szBuffer[MAX_PATH];
49  LRESULT Index;
50 
51  if (LoadString(hApplet, ResourceId, szBuffer, MAX_PATH) < MAX_PATH)
52  {
53  Index = SendMessage(hDlgCtrl, CB_INSERTSTRING, -1, (LPARAM)szBuffer);
54  if (Index != CB_ERR)
55  {
57  lpAction[Index] = (POWER_ACTION)lParam;
58  }
59  }
60 }
61 
62 static INT
63 FindActionIndex(POWER_ACTION * lpAction, DWORD dwActionSize, POWER_ACTION poAction)
64 {
65  INT Index;
66 
67  for (Index = 0; Index < (INT)dwActionSize; Index++)
68  {
69  if (lpAction[Index] == poAction)
70  return Index;
71  }
72 
73  return -1;
74 }
75 
76 static BOOLEAN
78 {
80 
82  {
83  if (sbs.BatteryPresent)
84  {
85  if (sbs.AcOnLine)
86  {
87  return FALSE;
88  }
89  return TRUE;
90  }
91  }
92 
93  return FALSE;
94 }
95 
98  POWER_ACTION_POLICY *Policy,
100  BOOL bIsLid)
101 {
102  POWER_ACTION poAction = PowerActionNone;
103  /*
104 
105  TCHAR szBuffer[MAX_PATH];
106 
107  // Note: Windows XP SP2+ does not return the PowerAction code
108  // for PowerActionWarmEject + PowerActionShutdown but sets it
109  // to PowerActionNone and sets the Flags & EventCode
110 
111 
112  _stprintf(szBuffer, L"Action: %x EventCode %x Flags %x",Policy->Action, Policy->EventCode, Policy->Flags);
113  MessageBoxW(NULL, szBuffer, NULL, MB_OK);
114 
115  */
116 
117  if (Policy->Action == PowerActionNone)
118  {
120  {
121  if (Policy->EventCode == POWER_FORCE_TRIGGER_RESET)
122  {
123  poAction = PowerActionNone;
124  }
125  else if (Policy->EventCode == POWER_USER_NOTIFY_BUTTON)
126  {
127  poAction = PowerActionWarmEject;
128  }
129  else if (Policy->EventCode == POWER_USER_NOTIFY_SHUTDOWN)
130  {
131  poAction = PowerActionShutdown;
132  }
133  }
134  }
135  else
136  {
137  poAction = Policy->Action;
138  if ((poAction == PowerActionHibernate) && !(spc->SystemS4 && spc->HiberFilePresent))
139  poAction = PowerActionSleep;
140  if ((poAction == PowerActionSleep) && !(spc->SystemS1 || spc->SystemS2 || spc->SystemS3))
141  {
142  if (bIsLid)
143  poAction = PowerActionNone;
144  else
145  poAction = PowerActionShutdown;
146  }
147  }
148 
149  return poAction;
150 }
151 
152 VOID
154  POWER_ACTION *lpAction,
155  DWORD dwActionSize,
156  POWER_ACTION_POLICY *Policy,
158  BOOL bIsLid)
159 {
160  int poActionIndex;
161  POWER_ACTION poAction;
162 
163  poAction = GetPowerActionFromPolicy(Policy, spc, bIsLid);
164  poActionIndex = FindActionIndex(lpAction, dwActionSize, poAction);
165 
166  if (poActionIndex < 0)
167  {
168  return;
169  }
170 
171  SendMessage(hDlgCtrl, CB_SETCURSEL, (WPARAM)poActionIndex, (LPARAM)0);
172 }
173 
174 BOOLEAN
176  OUT POWER_ACTION_POLICY *Policy)
177 {
178  LRESULT Index;
179  LRESULT ItemData;
180 
181  Index = SendMessage(hDlgCtrl, CB_GETCURSEL, 0, 0);
182  if (Index == CB_ERR)
183  return FALSE;
184 
185  ItemData = SendMessage(hDlgCtrl, CB_GETITEMDATA, (WPARAM)Index, 0);
186  if (ItemData == CB_ERR)
187  return FALSE;
188 
189  switch(ItemData)
190  {
191  case PowerActionNone:
192  Policy->Action = PowerActionNone;
193  Policy->EventCode = POWER_FORCE_TRIGGER_RESET;
194  break;
195 
197  Policy->Action = PowerActionNone;
198  Policy->EventCode = POWER_USER_NOTIFY_BUTTON;
199  break;
200 
201  case PowerActionShutdown:
202  Policy->Action = PowerActionNone;
203  Policy->EventCode = POWER_USER_NOTIFY_SHUTDOWN;
204  break;
205 
206  case PowerActionSleep:
208  Policy->Action = (POWER_ACTION)ItemData;
209  Policy->EventCode = 0;
210  break;
211 
212  default:
213  return FALSE;
214  }
215 
217 
218  return TRUE;
219 }
220 
221 
222 //-------------------------------------------------------------------
223 
224 VOID
226  HWND hwndDlg,
228 {
229  if (!IsBatteryUsed())
230  {
233  sizeof(g_SystemBatteries) / sizeof(POWER_ACTION),
235  spc,
236  TRUE);
237 
240  sizeof(g_PowerButton) / sizeof(POWER_ACTION),
242  spc,
243  FALSE);
244 
247  sizeof(g_SleepButton) / sizeof(POWER_ACTION),
249  spc,
250  FALSE);
251  }
252  else
253  {
256  sizeof(g_SystemBatteries) / sizeof(POWER_ACTION),
258  spc,
259  TRUE);
260 
263  sizeof(g_PowerButton) / sizeof(POWER_ACTION),
265  spc,
266  FALSE);
267 
270  sizeof(g_SleepButton) / sizeof(POWER_ACTION),
272  spc,
273  FALSE);
274  }
275 }
276 
277 VOID
279  HWND hwndDlg)
280 {
281  HWND hList1;
282  HWND hList2;
283  HWND hList3;
284 
285  BOOLEAN bSuspend = FALSE;
286  BOOLEAN bHibernate;
288  BOOL bEnabled;
289 
291 
292  bEnabled = GetSystrayPowerIconState();
293 
294  if (bEnabled)
296  else
298 
299  CheckDlgButton(hwndDlg,
301  bEnabled ? BST_CHECKED : BST_UNCHECKED);
302  CheckDlgButton(hwndDlg,
305  CheckDlgButton(hwndDlg,
308 
309  GetPwrCapabilities(&spc);
310 
311  if (spc.SystemS1 || spc.SystemS2 || spc.SystemS3)
312  bSuspend=TRUE;
313 
314  bHibernate = spc.HiberFilePresent;
315  bShutdown = spc.SystemS5;
316 
317  hList1 = GetDlgItem(hwndDlg, IDC_LIDCLOSE);
318  SendMessage(hList1, CB_RESETCONTENT, 0, 0);
319 
321  if (spc.SystemBatteriesPresent)
322  {
324 
325  if (bSuspend)
326  {
328  }
329 
330  if (bHibernate)
331  {
333  }
334  }
335  else
336  {
339  ShowWindow(hList1, FALSE);
340  }
341 
342  hList2 = GetDlgItem(hwndDlg, IDC_POWERBUTTON);
343  SendMessage(hList2, CB_RESETCONTENT, 0, 0);
344 
345  memset(g_PowerButton, 0x0, sizeof(g_PowerButton));
346  if (spc.PowerButtonPresent)
347  {
350 
351  if (bSuspend)
352  {
354  }
355 
356  if (bHibernate)
357  {
359  }
360 
361  if (bShutdown)
362  {
364  }
365  }
366  else
367  {
369  ShowWindow(hList2, FALSE);
370  }
371 
372  hList3 = GetDlgItem(hwndDlg, IDC_SLEEPBUTTON);
373  SendMessage(hList3, CB_RESETCONTENT, 0, 0);
374  memset(g_SleepButton, 0x0, sizeof(g_SleepButton));
375 
376  if (spc.SleepButtonPresent)
377  {
380 
381  if (bSuspend)
382  {
384  }
385 
386  if (bHibernate)
387  {
389  }
390 
391  if (bShutdown)
392  {
394  }
395  }
396  else
397  {
399  ShowWindow(hList3, FALSE);
400  }
401 
403  {
404  ShowCurrentPowerActionPolicies(hwndDlg, &spc);
405  }
406 }
407 
408 static VOID
410 {
411  BOOL bSystrayBatteryMeter;
412  BOOL bPasswordLogon;
413  BOOL bVideoDimDisplay;
414 
415  bSystrayBatteryMeter =
417 
418  bPasswordLogon =
420 
421  bVideoDimDisplay =
423 
424  if (bSystrayBatteryMeter)
425  {
427  {
429  }
430  }
431  else
432  {
434  {
436  }
437  }
438 
439  if (bPasswordLogon)
440  {
442  {
444  }
445  }
446  else
447  {
449  {
451  }
452  }
453 
454  if (bVideoDimDisplay)
455  {
457  {
459  }
460  }
461  else
462  {
464  {
466  }
467  }
468 
469  if (!IsBatteryUsed())
470  {
471 #if 0
473 #endif
476  }
477  else
478  {
479 #if 0
483 #endif
484  }
485 
486  if (!WriteGlobalPwrPolicy(&gGPP))
487  {
488  MessageBox(hwndDlg, L"WriteGlobalPwrPolicy failed", NULL, MB_OK);
489  }
490 
491  SetSystrayPowerIconState(bSystrayBatteryMeter);
492 
493 // Adv_InitDialog(hwndDlg);
494 }
495 
496 /* Property page dialog callback */
499  UINT uMsg,
500  WPARAM wParam,
501  LPARAM lParam)
502 {
503  switch (uMsg)
504  {
505  case WM_INITDIALOG:
506  Adv_InitDialog(hwndDlg);
507  return TRUE;
508 
509  case WM_COMMAND:
510  switch (LOWORD(wParam))
511  {
513  case IDC_PASSWORDLOGON:
514  case IDC_VIDEODIMDISPLAY:
515  if (HIWORD(wParam) == BN_CLICKED)
516  {
517  PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
518  }
519  break;
520 
521  case IDC_LIDCLOSE:
522  case IDC_POWERBUTTON:
523  case IDC_SLEEPBUTTON:
524  if (HIWORD(wParam) == CBN_SELCHANGE)
525  {
526  PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
527  }
528  break;
529  }
530  break;
531 
532  case WM_NOTIFY:
533  switch (((LPNMHDR)lParam)->code)
534  {
535  case PSN_APPLY:
536  Adv_SaveData(hwndDlg);
537  return TRUE;
538 
539  case PSN_SETACTIVE:
540  Adv_InitDialog(hwndDlg);
541  return TRUE;
542  }
543  break;
544  }
545 
546  return FALSE;
547 }
volatile BOOL bShutdown
Definition: tcpsvcs.c:16
#define CB_SETITEMDATA
Definition: winuser.h:1927
BOOL WINAPI CheckDlgButton(_In_ HWND, _In_ int, _In_ UINT)
#define IN
Definition: typedefs.h:38
#define TRUE
Definition: types.h:120
POWER_ACTION_POLICY SleepButtonDc
Definition: powrprof.h:35
#define POWER_USER_NOTIFY_SHUTDOWN
Definition: ntpoapi.h:427
GLint x0
Definition: linetemp.h:95
GLOBAL_USER_POWER_POLICY user
Definition: powrprof.h:42
static VOID AddItem(HWND hDlgCtrl, INT ResourceId, LPARAM lParam, POWER_ACTION *lpAction)
Definition: advanced.c:46
#define CB_GETITEMDATA
Definition: winuser.h:1911
#define IDC_POWERBUTTON
Definition: resource.h:100
#define POWER_FORCE_TRIGGER_RESET
Definition: ntpoapi.h:428
#define INT
Definition: polytest.cpp:20
#define CALLBACK
Definition: compat.h:27
POWER_ACTION_POLICY PowerButtonDc
Definition: powrprof.h:33
BOOLEAN SystemBatteriesPresent
Definition: ntpoapi.h:374
BOOLEAN WINAPI GetPwrCapabilities(PSYSTEM_POWER_CAPABILITIES lpSystemPowerCapabilities)
Definition: powrprof.c:358
HINSTANCE hApplet
Definition: access.c:17
#define IDC_LIDCLOSE
Definition: resource.h:98
UINT_PTR WPARAM
Definition: windef.h:207
#define PSN_APPLY
Definition: prsht.h:117
BOOL WINAPI ShowWindow(_In_ HWND, _In_ int)
int32_t INT_PTR
Definition: typedefs.h:62
#define PSN_SETACTIVE
Definition: prsht.h:115
static VOID Adv_SaveData(HWND hwndDlg)
Definition: advanced.c:409
static INT FindActionIndex(POWER_ACTION *lpAction, DWORD dwActionSize, POWER_ACTION poAction)
Definition: advanced.c:63
int32_t INT
Definition: typedefs.h:56
#define IDC_SPOWERBUTTON
Definition: resource.h:99
#define CB_GETCURSEL
Definition: winuser.h:1904
static BOOL GetSystrayPowerIconState(VOID)
Definition: advanced.c:34
WPARAM wParam
Definition: combotst.c:138
#define IDC_SYSTRAYBATTERYMETER
Definition: resource.h:94
#define IDC_SSLEEPBUTTON
Definition: resource.h:101
#define EnableVideoDimDisplay
Definition: powrprof.h:19
INT_PTR CALLBACK AdvancedDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: advanced.c:225
unsigned int BOOL
Definition: ntddk_ex.h:94
NTSTATUS WINAPI CallNtPowerInformation(POWER_INFORMATION_LEVEL InformationLevel, PVOID lpInputBuffer, ULONG nInputBufferSize, PVOID lpOutputBuffer, ULONG nOutputBufferSize)
Definition: powrprof.c:59
#define CB_INSERTSTRING
Definition: winuser.h:1918
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
POWER_ACTION Action
Definition: ntpoapi.h:408
LONG_PTR LPARAM
Definition: windef.h:208
#define CB_RESETCONTENT
Definition: winuser.h:1920
UINT WINAPI IsDlgButtonChecked(_In_ HWND, _In_ int)
#define CB_ERR
Definition: winuser.h:2389
POWER_ACTION_POLICY LidCloseAc
Definition: powrprof.h:36
GLOBAL_POWER_POLICY gGPP
Definition: powercfg.c:23
char TCHAR
Definition: xmlstorage.h:189
#define POWER_ACTION_QUERY_ALLOWED
Definition: ntpoapi.h:414
#define POWER_ACTION_UI_ALLOWED
Definition: ntpoapi.h:415
HWND WINAPI GetDlgItem(_In_opt_ HWND, _In_ int)
HWND WINAPI FindWindowW(_In_opt_ LPCWSTR, _In_opt_ LPCWSTR)
#define PropSheet_Changed(d, w)
Definition: prsht.h:326
#define POWER_USER_NOTIFY_BUTTON
Definition: ntpoapi.h:426
static POWER_ACTION g_SystemBatteries[3]
Definition: advanced.c:14
#define MAX_PATH
Definition: compat.h:26
static const UCHAR Index[8]
Definition: usbohci.c:18
#define BN_CLICKED
Definition: winuser.h:1886
unsigned long DWORD
Definition: ntddk_ex.h:95
POWER_ACTION_POLICY LidCloseDc
Definition: powrprof.h:37
#define EnablePasswordLogon
Definition: powrprof.h:16
#define IDC_VIDEODIMDISPLAY
Definition: resource.h:96
#define IDS_PowerActionShutdown
Definition: resource.h:27
static const WCHAR L[]
Definition: oid.c:1250
VOID ShowCurrentPowerActionPolicies(HWND hwndDlg, PSYSTEM_POWER_CAPABILITIES spc)
Definition: advanced.c:225
POWER_ACTION
Definition: ntpoapi.h:122
#define MessageBox
Definition: winuser.h:5688
#define CB_SETCURSEL
Definition: winuser.h:1922
#define WM_COMMAND
Definition: winuser.h:1716
#define BST_UNCHECKED
Definition: winuser.h:199
#define WM_USER
Definition: winuser.h:1856
#define IDS_PowerActionHibernate
Definition: resource.h:26
#define IDS_PowerActionSleep
Definition: resource.h:25
HWND WINAPI GetParent(_In_ HWND)
BOOLEAN WINAPI ReadGlobalPwrPolicy(PGLOBAL_POWER_POLICY pGlobalPowerPolicy)
Definition: powrprof.c:521
static POWER_ACTION g_SleepButton[5]
Definition: advanced.c:16
static BOOLEAN IsBatteryUsed(VOID)
Definition: advanced.c:77
#define IDC_SLIDCLOSE
Definition: resource.h:97
LRESULT WINAPI SendMessageW(_In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
BOOLEAN SaveCurrentPowerActionPolicy(IN HWND hDlgCtrl, OUT POWER_ACTION_POLICY *Policy)
Definition: advanced.c:175
unsigned int UINT
Definition: ndis.h:50
static VOID SetSystrayPowerIconState(BOOL bEnabled)
Definition: advanced.c:21
#define MB_OK
Definition: winuser.h:784
#define CBN_SELCHANGE
Definition: winuser.h:1940
#define BST_CHECKED
Definition: winuser.h:197
#define IDC_PASSWORDLOGON
Definition: resource.h:95
#define IDS_PowerActionWarmEject
Definition: resource.h:30
#define SendMessage
Definition: winuser.h:5709
#define OUT
Definition: typedefs.h:39
BOOLEAN WINAPI WriteGlobalPwrPolicy(PGLOBAL_POWER_POLICY pGlobalPowerPolicy)
Definition: powrprof.c:690
#define HIWORD(l)
Definition: typedefs.h:246
unsigned int ULONG
Definition: retypes.h:1
#define EnableSysTrayBatteryMeter
Definition: powrprof.h:17
POWER_ACTION_POLICY PowerButtonAc
Definition: powrprof.h:32
POWER_ACTION GetPowerActionFromPolicy(POWER_ACTION_POLICY *Policy, PSYSTEM_POWER_CAPABILITIES spc, BOOL bIsLid)
Definition: advanced.c:97
VOID Adv_InitDialog(HWND hwndDlg)
Definition: advanced.c:278
LONG_PTR LRESULT
Definition: windef.h:209
VOID ShowCurrentPowerActionPolicy(HWND hDlgCtrl, POWER_ACTION *lpAction, DWORD dwActionSize, POWER_ACTION_POLICY *Policy, PSYSTEM_POWER_CAPABILITIES spc, BOOL bIsLid)
Definition: advanced.c:153
static POWER_ACTION g_PowerButton[5]
Definition: advanced.c:15
return STATUS_SUCCESS
Definition: btrfs.c:2777
#define memset(x, y, z)
Definition: compat.h:39
#define WM_INITDIALOG
Definition: winuser.h:1715
LPARAM lParam
Definition: combotst.c:139
#define LOWORD(l)
Definition: pedump.c:82
BOOLEAN BatteryPresent
Definition: ntpoapi.h:386
POWER_ACTION_POLICY SleepButtonAc
Definition: powrprof.h:34
#define LoadString
Definition: winuser.h:5685
#define WM_NOTIFY
Definition: richedit.h:61
#define IDS_PowerActionNone1
Definition: resource.h:23
#define IDC_SLEEPBUTTON
Definition: resource.h:102