ReactOS 0.4.16-dev-1946-g52006dd
advanced.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Power Configuration Applet
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: Advanced tab
5 * COPYRIGHT: Copyright 2006 Alexander Wurzinger <lohnegrim@gmx.net>
6 * Copyright 2006 Johannes Anderwald <johannes.anderwald@reactos.org>
7 * Copyright 2006 Martin Rottensteiner <2005only@pianonote.at>
8 * Copyright 2019 Eric Kohl <eric.kohl@reactos.org>
9 */
10
11#include "powercfg.h"
12
16
17
18static
19VOID
21{
22 HWND hwndTaskBar;
23
24 hwndTaskBar = FindWindowW(L"SystemTray_Main", NULL);
25 if (hwndTaskBar == NULL)
26 return;
27
28 SendMessageW(hwndTaskBar, WM_USER + 220, 1, bEnabled);
29}
30
31static
32BOOL
34{
35 HWND hwndTaskBar;
36
37 hwndTaskBar = FindWindowW(L"SystemTray_Main", NULL);
38 if (hwndTaskBar == NULL)
39 return FALSE;
40
41 return (BOOL)SendMessageW(hwndTaskBar, WM_USER + 221, 1, 0);
42}
43
44static VOID
46{
47 TCHAR szBuffer[MAX_PATH];
49
50 if (LoadString(hApplet, ResourceId, szBuffer, _countof(szBuffer)) < _countof(szBuffer))
51 {
52 Index = SendMessage(hDlgCtrl, CB_INSERTSTRING, -1, (LPARAM)szBuffer);
53 if (Index != CB_ERR)
54 {
56 lpAction[Index] = (POWER_ACTION)lParam;
57 }
58 }
59}
60
61static INT
62FindActionIndex(POWER_ACTION * lpAction, DWORD dwActionSize, POWER_ACTION poAction)
63{
64 INT Index;
65
66 for (Index = 0; Index < (INT)dwActionSize; Index++)
67 {
68 if (lpAction[Index] == poAction)
69 return Index;
70 }
71
72 return -1;
73}
74
75static BOOLEAN
77{
79
80 if (CallNtPowerInformation(SystemBatteryState, NULL, 0, &sbs, sizeof(sbs)) == STATUS_SUCCESS)
81 return (sbs.BatteryPresent && !sbs.AcOnLine);
82 return FALSE;
83}
84
89 BOOL bIsLid)
90{
92 /*
93
94 TCHAR szBuffer[MAX_PATH];
95
96 // Note: Windows XP SP2+ does not return the PowerAction code
97 // for PowerActionWarmEject + PowerActionShutdown but sets it
98 // to PowerActionNone and sets the Flags & EventCode
99
100 _stprintf(szBuffer, L"Action: %x EventCode %x Flags %x",Policy->Action, Policy->EventCode, Policy->Flags);
101 MessageBoxW(NULL, szBuffer, NULL, MB_OK);
102
103 */
104
105 if (Policy->Action == PowerActionNone)
106 {
108 {
109 if (Policy->EventCode == POWER_FORCE_TRIGGER_RESET)
110 poAction = PowerActionNone;
111 else if (Policy->EventCode == POWER_USER_NOTIFY_BUTTON)
112 poAction = PowerActionWarmEject;
113 else if (Policy->EventCode == POWER_USER_NOTIFY_SHUTDOWN)
114 poAction = PowerActionShutdown;
115 }
116 }
117 else
118 {
119 poAction = Policy->Action;
120 if ((poAction == PowerActionHibernate) && !IS_PWR_HIBERNATE_ALLOWED(spc))
121 poAction = PowerActionSleep;
122 if ((poAction == PowerActionSleep) && !IS_PWR_SUSPEND_ALLOWED(spc))
123 {
124 if (bIsLid)
125 poAction = PowerActionNone;
126 else
127 poAction = PowerActionShutdown;
128 }
129 }
130
131 return poAction;
132}
133
134VOID
136 POWER_ACTION *lpAction,
137 DWORD dwActionSize,
140 BOOL bIsLid)
141{
142 int poActionIndex;
143 POWER_ACTION poAction;
144
145 poAction = GetPowerActionFromPolicy(Policy, spc, bIsLid);
146 poActionIndex = FindActionIndex(lpAction, dwActionSize, poAction);
147
148 if (poActionIndex < 0)
149 {
150 return;
151 }
152
153 SendMessage(hDlgCtrl, CB_SETCURSEL, (WPARAM)poActionIndex, (LPARAM)0);
154}
155
159{
161 LRESULT ItemData;
162
163 Index = SendMessage(hDlgCtrl, CB_GETCURSEL, 0, 0);
164 if (Index == CB_ERR)
165 return FALSE;
166
167 ItemData = SendMessage(hDlgCtrl, CB_GETITEMDATA, (WPARAM)Index, 0);
168 if (ItemData == CB_ERR)
169 return FALSE;
170
171 switch(ItemData)
172 {
173 case PowerActionNone:
174 Policy->Action = PowerActionNone;
176 break;
177
179 Policy->Action = PowerActionNone;
180 Policy->EventCode = POWER_USER_NOTIFY_BUTTON;
181 break;
182
184 Policy->Action = PowerActionNone;
186 break;
187
188 case PowerActionSleep:
190 Policy->Action = (POWER_ACTION)ItemData;
191 Policy->EventCode = 0;
192 break;
193
194 default:
195 return FALSE;
196 }
197
199
200 return TRUE;
201}
202
203
204//-------------------------------------------------------------------
205
206VOID
208 HWND hwndDlg,
210{
211 if (!IsBatteryUsed())
212 {
215 sizeof(g_SystemBatteries) / sizeof(POWER_ACTION),
217 spc,
218 TRUE);
219
222 sizeof(g_PowerButton) / sizeof(POWER_ACTION),
224 spc,
225 FALSE);
226
229 sizeof(g_SleepButton) / sizeof(POWER_ACTION),
231 spc,
232 FALSE);
233 }
234 else
235 {
238 sizeof(g_SystemBatteries) / sizeof(POWER_ACTION),
240 spc,
241 TRUE);
242
245 sizeof(g_PowerButton) / sizeof(POWER_ACTION),
247 spc,
248 FALSE);
249
252 sizeof(g_SleepButton) / sizeof(POWER_ACTION),
254 spc,
255 FALSE);
256 }
257}
258
259VOID
261 HWND hwndDlg)
262{
263 HWND hList1, hList2, hList3;
264 BOOLEAN bSuspend, bHibernate, bShutdown;
265 BOOL bEnabled;
267
268 bEnabled = GetSystrayPowerIconState();
269
270 if (bEnabled)
272 else
273 gGPP.user.GlobalFlags &= ~EnableSysTrayBatteryMeter;
274
275 CheckDlgButton(hwndDlg,
277 bEnabled ? BST_CHECKED : BST_UNCHECKED);
278 CheckDlgButton(hwndDlg,
281 CheckDlgButton(hwndDlg,
284
285 GetPwrCapabilities(&spc);
286
287 bSuspend = IS_PWR_SUSPEND_ALLOWED(&spc);
288 bHibernate = IS_PWR_HIBERNATE_ALLOWED(&spc);
290
291 hList1 = GetDlgItem(hwndDlg, IDC_LIDCLOSE);
292 SendMessage(hList1, CB_RESETCONTENT, 0, 0);
293
296 {
298
299 if (bSuspend)
301
302 if (bHibernate)
304 }
305 else
306 {
309 ShowWindow(hList1, SW_HIDE);
310 }
311
312 hList2 = GetDlgItem(hwndDlg, IDC_POWERBUTTON);
313 SendMessage(hList2, CB_RESETCONTENT, 0, 0);
314
316 if (spc.PowerButtonPresent)
317 {
320
321 if (bSuspend)
323
324 if (bHibernate)
326
327 if (bShutdown)
329 }
330 else
331 {
333 ShowWindow(hList2, SW_HIDE);
334 }
335
336 hList3 = GetDlgItem(hwndDlg, IDC_SLEEPBUTTON);
337 SendMessage(hList3, CB_RESETCONTENT, 0, 0);
338
340 if (spc.SleepButtonPresent)
341 {
344
345 if (bSuspend)
347
348 if (bHibernate)
350
351 if (bShutdown)
353 }
354 else
355 {
357 ShowWindow(hList3, SW_HIDE);
358 }
359
361 ShowCurrentPowerActionPolicies(hwndDlg, &spc);
362}
363
364static VOID
366{
367 BOOL bSystrayBatteryMeter;
368 BOOL bPasswordLogon;
369 BOOL bVideoDimDisplay;
370
371 bSystrayBatteryMeter =
373
374 bPasswordLogon =
376
377 bVideoDimDisplay =
379
380 if (bSystrayBatteryMeter)
381 {
383 {
385 }
386 }
387 else
388 {
390 {
392 }
393 }
394
395 if (bPasswordLogon)
396 {
398 {
400 }
401 }
402 else
403 {
405 {
407 }
408 }
409
410 if (bVideoDimDisplay)
411 {
413 {
415 }
416 }
417 else
418 {
420 {
422 }
423 }
424
425 if (!IsBatteryUsed())
426 {
427#if 0
429#endif
432 }
433 else
434 {
435#if 0
439#endif
440 }
441
443 {
444 MessageBox(hwndDlg, L"WriteGlobalPwrPolicy failed", NULL, MB_OK);
445 }
446
447 SetSystrayPowerIconState(bSystrayBatteryMeter);
448
449// Adv_InitDialog(hwndDlg);
450}
451
452/* Property page dialog callback */
455 UINT uMsg,
458{
459 switch (uMsg)
460 {
461 case WM_INITDIALOG:
462 Adv_InitDialog(hwndDlg);
463 return TRUE;
464
465 case WM_COMMAND:
466 switch (LOWORD(wParam))
467 {
471 if (HIWORD(wParam) == BN_CLICKED)
472 {
473 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
474 }
475 break;
476
477 case IDC_LIDCLOSE:
478 case IDC_POWERBUTTON:
479 case IDC_SLEEPBUTTON:
481 {
482 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
483 }
484 break;
485 }
486 break;
487
488 case WM_NOTIFY:
489 switch (((LPNMHDR)lParam)->code)
490 {
491 case PSN_APPLY:
492 Adv_SaveData(hwndDlg);
493 return TRUE;
494
495 case PSN_SETACTIVE:
496 Adv_InitDialog(hwndDlg);
497 return TRUE;
498 }
499 break;
500 }
501
502 return FALSE;
503}
INT ResourceId
Definition: LoadImageGCC.c:72
unsigned char BOOLEAN
INT_PTR CALLBACK AdvancedDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: advanced.c:225
WPARAM wParam
Definition: combotst.c:138
LPARAM lParam
Definition: combotst.c:139
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
HINSTANCE hApplet
Definition: access.c:17
static POWER_ACTION g_PowerButton[5]
Definition: advanced.c:14
POWER_ACTION GetPowerActionFromPolicy(POWER_ACTION_POLICY *Policy, PSYSTEM_POWER_CAPABILITIES spc, BOOL bIsLid)
Definition: advanced.c:86
VOID Adv_InitDialog(HWND hwndDlg)
Definition: advanced.c:260
VOID ShowCurrentPowerActionPolicy(HWND hDlgCtrl, POWER_ACTION *lpAction, DWORD dwActionSize, POWER_ACTION_POLICY *Policy, PSYSTEM_POWER_CAPABILITIES spc, BOOL bIsLid)
Definition: advanced.c:135
static VOID Adv_SaveData(HWND hwndDlg)
Definition: advanced.c:365
BOOLEAN SaveCurrentPowerActionPolicy(IN HWND hDlgCtrl, OUT POWER_ACTION_POLICY *Policy)
Definition: advanced.c:157
static POWER_ACTION g_SleepButton[5]
Definition: advanced.c:15
static BOOLEAN IsBatteryUsed(VOID)
Definition: advanced.c:76
static INT FindActionIndex(POWER_ACTION *lpAction, DWORD dwActionSize, POWER_ACTION poAction)
Definition: advanced.c:62
VOID ShowCurrentPowerActionPolicies(HWND hwndDlg, PSYSTEM_POWER_CAPABILITIES spc)
Definition: advanced.c:207
static VOID SetSystrayPowerIconState(BOOL bEnabled)
Definition: advanced.c:20
static POWER_ACTION g_SystemBatteries[3]
Definition: advanced.c:13
static BOOL GetSystrayPowerIconState(VOID)
Definition: advanced.c:33
#define IDC_SLIDCLOSE
Definition: resource.h:141
#define IDS_PowerActionWarmEject
Definition: resource.h:70
#define IDS_PowerActionHibernate
Definition: resource.h:66
#define IDC_VIDEODIMDISPLAY
Definition: resource.h:140
#define IDC_SPOWERBUTTON
Definition: resource.h:143
#define IDC_SLEEPBUTTON
Definition: resource.h:146
#define IDS_PowerActionNone1
Definition: resource.h:63
#define IDS_PowerActionShutdown
Definition: resource.h:67
#define IDC_PASSWORDLOGON
Definition: resource.h:139
#define IDC_POWERBUTTON
Definition: resource.h:144
#define IDS_PowerActionSleep
Definition: resource.h:65
#define IDC_SYSTRAYBATTERYMETER
Definition: resource.h:138
#define IDC_SSLEEPBUTTON
Definition: resource.h:145
#define IDC_LIDCLOSE
Definition: resource.h:142
#define MAX_PATH
Definition: compat.h:34
#define CALLBACK
Definition: compat.h:35
#define L(x)
Definition: resources.c:13
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
#define ZeroMemory
Definition: minwinbase.h:31
LONG_PTR LPARAM
Definition: minwindef.h:175
LONG_PTR LRESULT
Definition: minwindef.h:176
UINT_PTR WPARAM
Definition: minwindef.h:174
unsigned int UINT
Definition: ndis.h:50
#define IS_PWR_SUSPEND_ALLOWED(PowerCaps)
Definition: potypes.h:63
#define IS_PWR_HIBERNATE_ALLOWED(PowerCaps)
Definition: potypes.h:70
#define IS_PWR_POWEROFF_ALLOWED(PowerCaps)
Definition: potypes.h:77
#define POWER_ACTION_UI_ALLOWED
Definition: ntpoapi.h:416
#define POWER_USER_NOTIFY_BUTTON
Definition: ntpoapi.h:427
#define POWER_FORCE_TRIGGER_RESET
Definition: ntpoapi.h:429
#define POWER_ACTION_QUERY_ALLOWED
Definition: ntpoapi.h:415
POWER_ACTION
Definition: ntpoapi.h:122
@ PowerActionNone
Definition: ntpoapi.h:123
@ PowerActionHibernate
Definition: ntpoapi.h:126
@ PowerActionShutdown
Definition: ntpoapi.h:127
@ PowerActionWarmEject
Definition: ntpoapi.h:130
@ PowerActionSleep
Definition: ntpoapi.h:125
#define POWER_USER_NOTIFY_SHUTDOWN
Definition: ntpoapi.h:428
@ SystemBatteryState
Definition: ntpoapi.h:72
#define LOWORD(l)
Definition: pedump.c:82
#define INT
Definition: polytest.cpp:20
GLOBAL_POWER_POLICY gGPP
Definition: powercfg.c:16
BOOLEAN WINAPI GetPwrCapabilities(PSYSTEM_POWER_CAPABILITIES lpSystemPowerCapabilities)
Definition: powrprof.c:582
NTSTATUS WINAPI CallNtPowerInformation(POWER_INFORMATION_LEVEL InformationLevel, PVOID lpInputBuffer, ULONG nInputBufferSize, PVOID lpOutputBuffer, ULONG nOutputBufferSize)
Definition: powrprof.c:283
BOOLEAN WINAPI WriteGlobalPwrPolicy(PGLOBAL_POWER_POLICY pGlobalPowerPolicy)
Definition: powrprof.c:1026
BOOLEAN WINAPI ReadGlobalPwrPolicy(PGLOBAL_POWER_POLICY pGlobalPowerPolicy)
Definition: powrprof.c:857
#define EnableVideoDimDisplay
Definition: powrprof.h:36
#define EnablePasswordLogon
Definition: powrprof.h:34
#define EnableSysTrayBatteryMeter
Definition: powrprof.h:32
#define PropSheet_Changed(d, w)
Definition: prsht.h:344
#define PSN_APPLY
Definition: prsht.h:117
#define PSN_SETACTIVE
Definition: prsht.h:115
#define WM_NOTIFY
Definition: richedit.h:61
#define STATUS_SUCCESS
Definition: shellext.h:65
#define _countof(array)
Definition: sndvol32.h:70
BOOLEAN BatteryPresent
Definition: ntpoapi.h:386
BOOLEAN SystemBatteriesPresent
Definition: ntpoapi.h:374
GLOBAL_USER_POWER_POLICY user
Definition: powrprof.h:60
POWER_ACTION_POLICY PowerButtonDc
Definition: powrprof.h:49
POWER_ACTION_POLICY LidCloseDc
Definition: powrprof.h:53
POWER_ACTION_POLICY SleepButtonDc
Definition: powrprof.h:51
POWER_ACTION_POLICY PowerButtonAc
Definition: powrprof.h:48
POWER_ACTION_POLICY SleepButtonAc
Definition: powrprof.h:50
POWER_ACTION_POLICY LidCloseAc
Definition: powrprof.h:52
Definition: inflate.c:139
volatile BOOL bShutdown
Definition: tcpsvcs.c:16
int32_t INT_PTR
Definition: typedefs.h:64
int32_t INT
Definition: typedefs.h:58
#define IN
Definition: typedefs.h:39
#define HIWORD(l)
Definition: typedefs.h:247
#define OUT
Definition: typedefs.h:40
#define AddItem
Definition: userenv.h:209
_In_ WDFCOLLECTION _In_ ULONG Index
_In_ WDFINTERRUPT _In_ WDF_INTERRUPT_POLICY Policy
Definition: wdfinterrupt.h:653
#define CB_SETITEMDATA
Definition: winuser.h:1995
#define SW_HIDE
Definition: winuser.h:779
BOOL WINAPI ShowWindow(_In_ HWND, _In_ int)
BOOL WINAPI CheckDlgButton(_In_ HWND, _In_ int, _In_ UINT)
#define BST_UNCHECKED
Definition: winuser.h:199
#define WM_COMMAND
Definition: winuser.h:1768
#define CB_ERR
Definition: winuser.h:2471
#define CB_SETCURSEL
Definition: winuser.h:1990
#define CB_RESETCONTENT
Definition: winuser.h:1988
#define WM_INITDIALOG
Definition: winuser.h:1767
HWND WINAPI GetDlgItem(_In_opt_ HWND, _In_ int)
#define CBN_SELCHANGE
Definition: winuser.h:2008
UINT WINAPI IsDlgButtonChecked(_In_ HWND, _In_ int)
#define CB_GETITEMDATA
Definition: winuser.h:1979
#define SendMessage
Definition: winuser.h:5954
#define MB_OK
Definition: winuser.h:801
HWND WINAPI GetParent(_In_ HWND)
#define LoadString
Definition: winuser.h:5930
#define MessageBox
Definition: winuser.h:5933
#define WM_USER
Definition: winuser.h:1923
#define BN_CLICKED
Definition: winuser.h:1954
HWND WINAPI FindWindowW(_In_opt_ LPCWSTR, _In_opt_ LPCWSTR)
#define CB_INSERTSTRING
Definition: winuser.h:1986
#define CB_GETCURSEL
Definition: winuser.h:1972
LRESULT WINAPI SendMessageW(_In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
#define BST_CHECKED
Definition: winuser.h:197
char TCHAR
Definition: xmlstorage.h:189