ReactOS  0.4.13-dev-73-gcfe54aa
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 
14 HWND hAdv = 0;
15 
19 
20 
21 static
22 VOID
24 {
25  HWND hwndTaskBar;
26 
27  hwndTaskBar = FindWindowW(L"SystemTray_Main", NULL);
28  if (hwndTaskBar == NULL)
29  return;
30 
31  SendMessageW(hwndTaskBar, WM_USER + 220, 1, bEnabled);
32 }
33 
34 static
35 BOOL
37 {
38  HWND hwndTaskBar;
39 
40  hwndTaskBar = FindWindowW(L"SystemTray_Main", NULL);
41  if (hwndTaskBar == NULL)
42  return FALSE;
43 
44  return (BOOL)SendMessageW(hwndTaskBar, WM_USER + 221, 1, 0);
45 }
46 
47 static VOID
48 AddItem(HWND hDlgCtrl, INT ResourceId, LPARAM lParam, POWER_ACTION * lpAction)
49 {
50  TCHAR szBuffer[MAX_PATH];
51  LRESULT Index;
52  if (LoadString(hApplet, ResourceId, szBuffer, MAX_PATH) < MAX_PATH)
53  {
54  Index = SendMessage(hDlgCtrl, CB_ADDSTRING, 0, (LPARAM)szBuffer);
55  if (Index != CB_ERR)
56  {
58  lpAction[Index] = (POWER_ACTION)lParam;
59  }
60  }
61 }
62 
63 static INT
64 FindActionIndex(POWER_ACTION * lpAction, DWORD dwActionSize, POWER_ACTION poAction)
65 {
66  INT Index;
67 
68  for (Index = 0; Index < (INT)dwActionSize; Index++)
69  {
70  if (lpAction[Index] == poAction)
71  return Index;
72  }
73 
74  return -1;
75 }
76 
77 static BOOLEAN
79 {
81 
83  {
84  if (sbs.BatteryPresent)
85  {
86  if (sbs.AcOnLine)
87  {
88  return FALSE;
89  }
90  return TRUE;
91  }
92  }
93 
94  return FALSE;
95 }
96 
99 {
100  POWER_ACTION poAction = PowerActionNone;
101  /*
102 
103  TCHAR szBuffer[MAX_PATH];
104 
105  // Note: Windows XP SP2+ does not return the PowerAction code
106  // for PowerActionWarmEject + PowerActionShutdown but sets it
107  // to PowerActionNone and sets the Flags & EventCode
108 
109 
110  _stprintf(szBuffer, L"Action: %x EventCode %x Flags %x",Policy->Action, Policy->EventCode, Policy->Flags);
111  MessageBoxW(NULL, szBuffer, NULL, MB_OK);
112 
113  */
114 
115  if (Policy->Action == PowerActionNone)
116  {
118  {
119  if (Policy->EventCode == POWER_FORCE_TRIGGER_RESET)
120  {
121  poAction = PowerActionNone;
122  }
123  else if (Policy->EventCode == POWER_USER_NOTIFY_BUTTON)
124  {
125  poAction = PowerActionWarmEject;
126  }
127  else if (Policy->EventCode == POWER_USER_NOTIFY_SHUTDOWN)
128  {
129  poAction = PowerActionShutdown;
130  }
131  }
132  }
133  else
134  {
135  poAction = Policy->Action;
136  }
137 
138  return poAction;
139 }
140 
141 VOID
143  POWER_ACTION *lpAction,
144  DWORD dwActionSize,
145  POWER_ACTION_POLICY *Policy)
146 {
147  int poActionIndex;
148  POWER_ACTION poAction;
149 
150  poAction = GetPowerActionFromPolicy(Policy);
151  poActionIndex = FindActionIndex(lpAction, dwActionSize, poAction);
152 
153  if (poActionIndex < 0)
154  {
155  return;
156  }
157 
158  SendMessage(hDlgCtrl, CB_SETCURSEL, (WPARAM)poActionIndex, (LPARAM)0);
159 }
160 
161 BOOLEAN
163  OUT POWER_ACTION_POLICY *Policy)
164 {
165  LRESULT Index;
166  LRESULT ItemData;
167 
168  Index = SendMessage(hDlgCtrl, CB_GETCURSEL, 0, 0);
169  if (Index == CB_ERR)
170  return FALSE;
171 
172  ItemData = SendMessage(hDlgCtrl, CB_GETITEMDATA, (WPARAM)Index, 0);
173  if (ItemData == CB_ERR)
174  return FALSE;
175 
176  switch(ItemData)
177  {
178  case PowerActionNone:
179  Policy->Action = PowerActionNone;
180  Policy->EventCode = POWER_FORCE_TRIGGER_RESET;
181  break;
183  Policy->Action = PowerActionNone;
184  Policy->EventCode = POWER_USER_NOTIFY_BUTTON;
185  break;
186  case PowerActionShutdown:
187  Policy->Action = PowerActionNone;
188  Policy->EventCode = POWER_USER_NOTIFY_SHUTDOWN;
189  break;
190  case PowerActionSleep:
192  Policy->Action = (POWER_ACTION)ItemData;
193  Policy->EventCode = 0;
194  break;
195  default:
196  return FALSE;
197  }
199 
200  return TRUE;
201 }
202 
203 
204 //-------------------------------------------------------------------
205 
206 VOID
208 {
209  TCHAR szAction[MAX_PATH];
210 
211  if (!IsBatteryUsed())
212  {
213 #if 0
214  /* experimental code */
215 // ShowCurrentPowerButtonAcAction(hList2,
218  sizeof(g_SystemBatteries) / sizeof(POWER_ACTION),
219  &gGPP.user.LidCloseAc);
220 #else
222  {
225  TRUE,
226  (LPARAM)szAction);
227  }
228 #endif
231  sizeof(g_PowerButton) / sizeof(POWER_ACTION),
233 
234 #if 0
235  /* experimental code */
238  sizeof(g_SleepButton) / sizeof(POWER_ACTION),
240 #else
242  {
245  TRUE,
246  (LPARAM)szAction);
247  }
248 #endif
249  }
250  else
251  {
252 #if 0
253 
256  sizeof(g_SleepButton) / sizeof(POWER_ACTION),
257  &gGPP.user.LidCloseDc);
258 
261  sizeof(g_SleepButton) / sizeof(POWER_ACTION),
263 
266  sizeof(g_SleepButton) / sizeof(POWER_ACTION),
268 #else
270  {
273  TRUE,
274  (LPARAM)szAction);
275  }
277  {
280  TRUE,
281  (LPARAM)szAction);
282  }
284  {
287  TRUE,
288  (LPARAM)szAction);
289  }
290 #endif
291  }
292 }
293 
294 VOID
296 {
297  HWND hList1;
298  HWND hList2;
299  HWND hList3;
300 
301  BOOLEAN bSuspend = FALSE;
302  BOOLEAN bHibernate;
304 
306 
309  else
311 
321 
322  GetPwrCapabilities(&spc);
323 
324  if (spc.SystemS1 || spc.SystemS2 || spc.SystemS3)
325  bSuspend=TRUE;
326 
327  bHibernate = spc.HiberFilePresent;
328  bShutdown = spc.SystemS5;
329 
330  hList1 = GetDlgItem(hAdv, IDC_LIDCLOSE);
331  SendMessage(hList1, CB_RESETCONTENT, 0, 0);
332 
334  if (spc.SystemBatteriesPresent)
335  {
337 
338  if (bSuspend)
339  {
341  }
342 
343  if (bHibernate)
344  {
346  }
347  }
348  else
349  {
352  ShowWindow(hList1, FALSE);
353  }
354 
355  hList2 = GetDlgItem(hAdv, IDC_POWERBUTTON);
356  SendMessage(hList2, CB_RESETCONTENT, 0, 0);
357 
358  memset(g_PowerButton, 0x0, sizeof(g_PowerButton));
359  if (spc.PowerButtonPresent)
360  {
363 
364  if (bSuspend)
365  {
367  }
368 
369  if (bHibernate)
370  {
372 
373  }
374  if (bShutdown)
375  {
377  }
378  }
379  else
380  {
382  ShowWindow(hList2, FALSE);
383  }
384 
385  hList3 = GetDlgItem(hAdv, IDC_SLEEPBUTTON);
386  SendMessage(hList3, CB_RESETCONTENT, 0, 0);
387  memset(g_SleepButton, 0x0, sizeof(g_SleepButton));
388 
389  if (spc.SleepButtonPresent)
390  {
393 
394  if (bSuspend)
395  {
397  }
398 
399  if (bHibernate)
400  {
402  }
403 
404  if (bShutdown)
405  {
407  }
408  }
409  else
410  {
412  ShowWindow(hList3, FALSE);
413  }
414 
416  {
418  }
419 }
420 
421 static VOID
423 {
424  BOOL bSystrayBatteryMeter;
425  BOOL bPasswordLogon;
426  BOOL bVideoDimDisplay;
427 
428  bSystrayBatteryMeter =
430 
431  bPasswordLogon =
433 
434  bVideoDimDisplay =
436 
437  if (bSystrayBatteryMeter)
438  {
440  {
442  }
443  }
444  else
445  {
447  {
449  }
450  }
451 
452  if (bPasswordLogon)
453  {
455  {
457  }
458  }
459  else
460  {
462  {
464  }
465  }
466 
467  if (bVideoDimDisplay)
468  {
470  {
472  }
473  }
474  else
475  {
477  {
479  }
480  }
481 
482  if (!IsBatteryUsed())
483  {
485 #if 0
488 #endif
489  }
490  else
491  {
492 #if 0
496 #endif
497  }
498 
499  if (!WriteGlobalPwrPolicy(&gGPP))
500  {
501  MessageBox(hwndDlg, L"WriteGlobalPwrPolicy failed", NULL, MB_OK);
502  }
503 
504  SetSystrayPowerIconState(!bSystrayBatteryMeter);
505 
506  Adv_InitDialog();
507 }
508 
509 /* Property page dialog callback */
512  UINT uMsg,
513  WPARAM wParam,
514  LPARAM lParam)
515 {
516  switch(uMsg)
517  {
518  case WM_INITDIALOG:
519  hAdv = hwndDlg;
520  Adv_InitDialog();
521  return TRUE;
522  break;
523  case WM_COMMAND:
524  switch(LOWORD(wParam))
525  {
527  case IDC_PASSWORDLOGON:
528  case IDC_VIDEODIMDISPLAY:
529  if (HIWORD(wParam) == BN_CLICKED)
530  {
531  PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
532  }
533  break;
534  case IDC_LIDCLOSE:
535  case IDC_POWERBUTTON:
536  case IDC_SLEEPBUTTON:
537  if (HIWORD(wParam) == CBN_SELCHANGE)
538  {
539  PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
540  }
541  break;
542  }
543  break;
544  case WM_NOTIFY:
545  {
546  LPNMHDR lpnm = (LPNMHDR)lParam;
547  if (lpnm->code == (UINT)PSN_APPLY)
548  {
549  Adv_SaveData(hwndDlg);
550  }
551  return TRUE;
552  }
553  }
554  return FALSE;
555 }
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
VOID ShowCurrentPowerActionPolicies(HWND hwndDlg)
Definition: advanced.c:207
static VOID AddItem(HWND hDlgCtrl, INT ResourceId, LPARAM lParam, POWER_ACTION *lpAction)
Definition: advanced.c:48
#define CB_GETITEMDATA
Definition: winuser.h:1911
#define IDC_POWERBUTTON
Definition: resource.h:98
#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:96
UINT_PTR WPARAM
Definition: windef.h:207
#define SendDlgItemMessage
Definition: winuser.h:5708
#define PSN_APPLY
Definition: prsht.h:117
BOOL WINAPI ShowWindow(_In_ HWND, _In_ int)
int32_t INT_PTR
Definition: typedefs.h:62
static VOID Adv_SaveData(HWND hwndDlg)
Definition: advanced.c:422
static INT FindActionIndex(POWER_ACTION *lpAction, DWORD dwActionSize, POWER_ACTION poAction)
Definition: advanced.c:64
int32_t INT
Definition: typedefs.h:56
#define IDC_SPOWERBUTTON
Definition: resource.h:97
#define CB_GETCURSEL
Definition: winuser.h:1904
static BOOL GetSystrayPowerIconState(VOID)
Definition: advanced.c:36
WPARAM wParam
Definition: combotst.c:138
#define IDC_SYSTRAYBATTERYMETER
Definition: resource.h:92
struct tagNMHDR * LPNMHDR
#define IDC_SSLEEPBUTTON
Definition: resource.h:99
UINT code
Definition: winuser.h:3112
#define EnableVideoDimDisplay
Definition: powrprof.h:19
POWER_ACTION GetPowerActionFromPolicy(POWER_ACTION_POLICY *Policy)
Definition: advanced.c:98
INT_PTR CALLBACK AdvancedDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: advanced.c:225
VOID Adv_InitDialog(VOID)
Definition: advanced.c:295
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_ADDSTRING
Definition: winuser.h:1897
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
HWND hAdv
Definition: advanced.c:14
#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:16
#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:94
#define IDS_PowerActionShutdown
Definition: resource.h:25
static const WCHAR L[]
Definition: oid.c:1250
#define CB_SELECTSTRING
Definition: winuser.h:1921
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:24
#define IDS_PowerActionSleep
Definition: resource.h:23
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:18
static BOOLEAN IsBatteryUsed(VOID)
Definition: advanced.c:78
#define IDC_SLIDCLOSE
Definition: resource.h:95
LRESULT WINAPI SendMessageW(_In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
BOOLEAN SaveCurrentPowerActionPolicy(IN HWND hDlgCtrl, OUT POWER_ACTION_POLICY *Policy)
Definition: advanced.c:162
unsigned int UINT
Definition: ndis.h:50
static VOID SetSystrayPowerIconState(BOOL bEnabled)
Definition: advanced.c:23
#define MB_OK
Definition: winuser.h:784
#define CBN_SELCHANGE
Definition: winuser.h:1940
VOID ShowCurrentPowerActionPolicy(HWND hDlgCtrl, POWER_ACTION *lpAction, DWORD dwActionSize, POWER_ACTION_POLICY *Policy)
Definition: advanced.c:142
#define BST_CHECKED
Definition: winuser.h:197
#define IDC_PASSWORDLOGON
Definition: resource.h:93
#define IDS_PowerActionWarmEject
Definition: resource.h:28
#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
LONG_PTR LRESULT
Definition: windef.h:209
static POWER_ACTION g_PowerButton[5]
Definition: advanced.c:17
return STATUS_SUCCESS
Definition: btrfs.c:2725
#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:21
#define IDC_SLEEPBUTTON
Definition: resource.h:100