ReactOS 0.4.16-dev-112-g52265ae
power.cpp File Reference
#include "precomp.h"
#include <devguid.h>
#include <winioctl.h>
#include <powrprof.h>
#include <windows.h>
#include <batclass.h>
Include dependency graph for power.cpp:

Go to the source code of this file.

Classes

struct  _PWRSCHEMECONTEXT
 

Typedefs

typedef struct _PWRSCHEMECONTEXT PWRSCHEMECONTEXT
 
typedef struct _PWRSCHEMECONTEXTPPWRSCHEMECONTEXT
 

Functions

static UINT Quantize (BYTE p)
 
static HICON DynamicLoadIcon (HINSTANCE hinst)
 
HRESULT STDMETHODCALLTYPE Power_Init (_In_ CSysTray *pSysTray)
 
HRESULT STDMETHODCALLTYPE Power_Update (_In_ CSysTray *pSysTray)
 
HRESULT STDMETHODCALLTYPE Power_Shutdown (_In_ CSysTray *pSysTray)
 
static void _RunPower ()
 
static void _ShowContextMenu (CSysTray *pSysTray)
 
static BOOLEAN CALLBACK PowerSchemesEnumProc (UINT uiIndex, DWORD dwName, LPWSTR sName, DWORD dwDesc, LPWSTR sDesc, PPOWER_POLICY pp, LPARAM lParam)
 
static VOID ShowPowerSchemesPopupMenu (CSysTray *pSysTray)
 
HRESULT STDMETHODCALLTYPE Power_Message (_In_ CSysTray *pSysTray, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT &lResult)
 

Variables

int br_icons [5] = { IDI_BATTCAP0, IDI_BATTCAP1, IDI_BATTCAP2, IDI_BATTCAP3, IDI_BATTCAP4 }
 
int bc_icons [5] = { IDI_BATTCHA0, IDI_BATTCHA1, IDI_BATTCHA2, IDI_BATTCHA3, IDI_BATTCHA4 }
 
CString g_strTooltip
 
static HICON g_hIconBattery = NULL
 

Typedef Documentation

◆ PPWRSCHEMECONTEXT

◆ PWRSCHEMECONTEXT

Function Documentation

◆ _RunPower()

static void _RunPower ( )
static

Definition at line 144 of file power.cpp.

145{
146 ShellExecuteW(NULL, NULL, L"powercfg.cpl", NULL, NULL, SW_SHOWNORMAL);
147}
#define NULL
Definition: types.h:112
#define L(x)
Definition: ntvdm.h:50
HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpVerb, LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
Definition: shlexec.cpp:2482
#define SW_SHOWNORMAL
Definition: winuser.h:773

Referenced by _ShowContextMenu(), and Power_Message().

◆ _ShowContextMenu()

static void _ShowContextMenu ( CSysTray pSysTray)
static

Definition at line 149 of file power.cpp.

150{
152 HMENU hPopup = CreatePopupMenu();
153 AppendMenuW(hPopup, MF_STRING, IDS_PWR_PROPERTIES, strOpen);
155
156 SetForegroundWindow(pSysTray->GetHWnd());
158 POINT pt;
160
161 DWORD id = TrackPopupMenuEx(hPopup, flags,
162 pt.x, pt.y,
163 pSysTray->GetHWnd(), NULL);
164
165 switch (id)
166 {
168 _RunPower();
169 break;
170 }
171 DestroyMenu(hPopup);
172}
HWND GetHWnd()
Definition: csystray.h:54
#define FALSE
Definition: types.h:117
static void _RunPower()
Definition: power.cpp:144
#define IDS_PWR_PROPERTIES
Definition: resource.h:5
#define pt(x, y)
Definition: drawing.c:79
unsigned long DWORD
Definition: ntddk_ex.h:95
GLbitfield flags
Definition: glext.h:7161
static const CLSID *static CLSID *static const GUID VARIANT VARIANT *static IServiceProvider DWORD *static HMENU
Definition: ordinal.c:63
HMENU WINAPI CreatePopupMenu(void)
Definition: menu.c:838
BOOL WINAPI SetMenuDefaultItem(_In_ HMENU, _In_ UINT, _In_ UINT)
#define TPM_BOTTOMALIGN
Definition: winuser.h:2388
#define MF_STRING
Definition: winuser.h:138
BOOL WINAPI SetForegroundWindow(_In_ HWND)
BOOL WINAPI GetCursorPos(_Out_ LPPOINT)
Definition: cursoricon.c:2722
#define TPM_RIGHTALIGN
Definition: winuser.h:2381
#define TPM_NONOTIFY
Definition: winuser.h:2389
BOOL WINAPI TrackPopupMenuEx(_In_ HMENU, _In_ UINT, _In_ int, _In_ int, _In_ HWND, _In_opt_ LPTPMPARAMS)
BOOL WINAPI DestroyMenu(_In_ HMENU)
#define TPM_RETURNCMD
Definition: winuser.h:2390
BOOL WINAPI AppendMenuW(_In_ HMENU, _In_ UINT, _In_ UINT_PTR, _In_opt_ LPCWSTR)
const char * LPCSTR
Definition: xmlstorage.h:183

Referenced by Power_Message().

◆ DynamicLoadIcon()

static HICON DynamicLoadIcon ( HINSTANCE  hinst)
static

Definition at line 77 of file power.cpp.

78{
79 SYSTEM_POWER_STATUS PowerStatus;
80 HICON hBatIcon;
81 UINT index = -1;
82
83 if (!GetSystemPowerStatus(&PowerStatus) ||
84 PowerStatus.ACLineStatus == AC_LINE_UNKNOWN ||
85 PowerStatus.BatteryFlag == BATTERY_FLAG_UNKNOWN)
86 {
89 return hBatIcon;
90 }
91
92 if (((PowerStatus.BatteryFlag & BATTERY_FLAG_NO_BATTERY) == 0) &&
94 {
97 }
98 else if (((PowerStatus.BatteryFlag & BATTERY_FLAG_NO_BATTERY) == 0) &&
100 {
101 index = Quantize(PowerStatus.BatteryLifePercent);
104 }
105 else if (((PowerStatus.BatteryFlag & BATTERY_FLAG_NO_BATTERY) == 0) &&
106 ((PowerStatus.BatteryFlag & BATTERY_FLAG_CHARGING) == 0))
107 {
108 index = Quantize(PowerStatus.BatteryLifePercent);
111 }
112 else
113 {
115 g_strTooltip.LoadStringW(IDS_PWR_AC);
116 }
117
118 return hBatIcon;
119}
void __cdecl Format(UINT nFormatID,...)
Definition: cstringt.h:818
static UINT Quantize(BYTE p)
Definition: power.cpp:51
CString g_strTooltip
Definition: power.cpp:29
int br_icons[5]
Definition: power.cpp:19
int bc_icons[5]
Definition: power.cpp:20
#define IDS_PWR_AC
Definition: resource.h:11
#define IDI_POWER_AC
Definition: resource.h:54
#define IDS_PWR_CHARGING
Definition: resource.h:9
#define IDS_PWR_PERCENT_REMAINING
Definition: resource.h:8
#define IDI_BATTCAP_ERR
Definition: resource.h:53
#define IDS_PWR_UNKNOWN_REMAINING
Definition: resource.h:10
BOOL WINAPI GetSystemPowerStatus(IN LPSYSTEM_POWER_STATUS PowerStatus)
Definition: power.c:27
GLuint index
Definition: glext.h:6031
static HINSTANCE hinst
Definition: edit.c:551
static HICON
Definition: imagelist.c:80
unsigned int UINT
Definition: ndis.h:50
#define BATTERY_FLAG_CHARGING
Definition: winbase.h:518
#define BATTERY_FLAG_NO_BATTERY
Definition: winbase.h:519
#define BATTERY_FLAG_UNKNOWN
Definition: winbase.h:520
#define BATTERY_PERCENTAGE_UNKNOWN
Definition: winbase.h:521
#define AC_LINE_UNKNOWN
Definition: winbase.h:514
#define LoadIcon
Definition: winuser.h:5825
#define MAKEINTRESOURCE
Definition: winuser.h:591

Referenced by Power_Init(), and Power_Update().

◆ Power_Init()

HRESULT STDMETHODCALLTYPE Power_Init ( _In_ CSysTray pSysTray)

Definition at line 121 of file power.cpp.

122{
123 TRACE("Power_Init\n");
125
126 return pSysTray->NotifyIcon(NIM_ADD, ID_ICON_POWER, g_hIconBattery, g_strTooltip);
127}
HINSTANCE g_hInstance
Definition: MainWindow.cpp:18
static HICON g_hIconBattery
Definition: power.cpp:30
static HICON DynamicLoadIcon(HINSTANCE hinst)
Definition: power.cpp:77
#define ID_ICON_POWER
Definition: precomp.h:35
#define NIM_ADD
Definition: shellapi.h:94
#define TRACE(s)
Definition: solgame.cpp:4

Referenced by Power_Message().

◆ Power_Message()

HRESULT STDMETHODCALLTYPE Power_Message ( _In_ CSysTray pSysTray,
UINT  uMsg,
WPARAM  wParam,
LPARAM  lParam,
LRESULT lResult 
)

Definition at line 236 of file power.cpp.

237{
238 TRACE("Power_Message uMsg=%d, wParam=%x, lParam=%x\n", uMsg, wParam, lParam);
239
240 switch (uMsg)
241 {
242 case WM_USER + 220:
243 TRACE("Power_Message: WM_USER+220\n");
245 {
246 if (lParam)
247 {
248 pSysTray->EnableService(POWER_SERVICE_FLAG, TRUE);
249 return Power_Init(pSysTray);
250 }
251 else
252 {
253 pSysTray->EnableService(POWER_SERVICE_FLAG, FALSE);
254 return Power_Shutdown(pSysTray);
255 }
256 }
257 return S_FALSE;
258
259 case WM_USER + 221:
260 TRACE("Power_Message: WM_USER+221\n");
262 {
263 lResult = (LRESULT)pSysTray->IsServiceEnabled(POWER_SERVICE_FLAG);
264 return S_OK;
265 }
266 return S_FALSE;
267
268 case WM_TIMER:
269 if (wParam == POWER_TIMER_ID)
270 {
271 KillTimer(pSysTray->GetHWnd(), POWER_TIMER_ID);
273 }
274 break;
275
276 case ID_ICON_POWER:
277 Power_Update(pSysTray);
278
279 switch (lParam)
280 {
281 case WM_LBUTTONDOWN:
282 SetTimer(pSysTray->GetHWnd(), POWER_TIMER_ID, GetDoubleClickTime(), NULL);
283 break;
284
285 case WM_LBUTTONUP:
286 break;
287
288 case WM_LBUTTONDBLCLK:
289 KillTimer(pSysTray->GetHWnd(), POWER_TIMER_ID);
290 _RunPower();
291 break;
292
293 case WM_RBUTTONDOWN:
294 break;
295
296 case WM_RBUTTONUP:
297 _ShowContextMenu(pSysTray);
298 break;
299
300 case WM_RBUTTONDBLCLK:
301 break;
302
303 case WM_MOUSEMOVE:
304 break;
305 }
306 return S_OK;
307
308 default:
309 TRACE("Power_Message received for unknown ID %d, ignoring.\n");
310 return S_FALSE;
311 }
312
313 return S_FALSE;
314}
WPARAM wParam
Definition: combotst.c:138
LPARAM lParam
Definition: combotst.c:139
#define TRUE
Definition: types.h:120
HRESULT STDMETHODCALLTYPE Power_Init(_In_ CSysTray *pSysTray)
Definition: power.cpp:121
HRESULT STDMETHODCALLTYPE Power_Shutdown(_In_ CSysTray *pSysTray)
Definition: power.cpp:137
HRESULT STDMETHODCALLTYPE Power_Update(_In_ CSysTray *pSysTray)
Definition: power.cpp:129
static void _ShowContextMenu(CSysTray *pSysTray)
Definition: power.cpp:149
static VOID ShowPowerSchemesPopupMenu(CSysTray *pSysTray)
Definition: power.cpp:201
#define POWER_TIMER_ID
Definition: precomp.h:83
#define POWER_SERVICE_FLAG
Definition: precomp.h:38
#define S_OK
Definition: intsafe.h:52
#define LRESULT
Definition: ole.h:14
#define S_FALSE
Definition: winerror.h:2357
#define WM_LBUTTONDBLCLK
Definition: winuser.h:1781
#define WM_RBUTTONUP
Definition: winuser.h:1783
#define WM_RBUTTONDBLCLK
Definition: winuser.h:1784
#define WM_MOUSEMOVE
Definition: winuser.h:1778
#define WM_LBUTTONDOWN
Definition: winuser.h:1779
UINT WINAPI GetDoubleClickTime(void)
Definition: ntwrapper.h:314
UINT_PTR WINAPI SetTimer(_In_opt_ HWND, _In_ UINT_PTR, _In_ UINT, _In_opt_ TIMERPROC)
#define WM_RBUTTONDOWN
Definition: winuser.h:1782
#define WM_TIMER
Definition: winuser.h:1745
#define WM_LBUTTONUP
Definition: winuser.h:1780
#define WM_USER
Definition: winuser.h:1898
BOOL WINAPI KillTimer(_In_opt_ HWND, _In_ UINT_PTR)

◆ Power_Shutdown()

HRESULT STDMETHODCALLTYPE Power_Shutdown ( _In_ CSysTray pSysTray)

Definition at line 137 of file power.cpp.

138{
139 TRACE("Power_Shutdown\n");
140
141 return pSysTray->NotifyIcon(NIM_DELETE, ID_ICON_POWER, NULL, NULL);
142}
#define NIM_DELETE
Definition: shellapi.h:96

Referenced by Power_Message().

◆ Power_Update()

HRESULT STDMETHODCALLTYPE Power_Update ( _In_ CSysTray pSysTray)

Definition at line 129 of file power.cpp.

130{
131 TRACE("Power_Update\n");
133
134 return pSysTray->NotifyIcon(NIM_MODIFY, ID_ICON_POWER, g_hIconBattery, g_strTooltip);
135}
#define NIM_MODIFY
Definition: shellapi.h:95

Referenced by Power_Message().

◆ PowerSchemesEnumProc()

static BOOLEAN CALLBACK PowerSchemesEnumProc ( UINT  uiIndex,
DWORD  dwName,
LPWSTR  sName,
DWORD  dwDesc,
LPWSTR  sDesc,
PPOWER_POLICY  pp,
LPARAM  lParam 
)
static

Definition at line 177 of file power.cpp.

185{
186 PPWRSCHEMECONTEXT PowerSchemeContext = (PPWRSCHEMECONTEXT)lParam;
187
188 if (AppendMenuW(PowerSchemeContext->hPopup, MF_STRING, uiIndex + 1, sName))
189 {
190 if (PowerSchemeContext->uiFirst == 0)
191 PowerSchemeContext->uiFirst = uiIndex + 1;
192
193 PowerSchemeContext->uiLast = uiIndex + 1;
194 }
195
196 return TRUE;
197}
struct _PWRSCHEMECONTEXT * PPWRSCHEMECONTEXT

Referenced by ShowPowerSchemesPopupMenu().

◆ Quantize()

static UINT Quantize ( BYTE  p)
static

Definition at line 51 of file power.cpp.

52{
53 if (p <= 12)
54 return 0;
55 else if (p > 12 && p <= 37)
56 return 1;
57 else if (p > 37 && p <= 62)
58 return 2;
59 else if (p > 62 && p <= 87)
60 return 3;
61 else
62 return 4;
63}
GLfloat GLfloat p
Definition: glext.h:8902

Referenced by DynamicLoadIcon().

◆ ShowPowerSchemesPopupMenu()

static VOID ShowPowerSchemesPopupMenu ( CSysTray pSysTray)
static

Definition at line 201 of file power.cpp.

203{
204 PWRSCHEMECONTEXT PowerSchemeContext = {NULL, 0, 0};
205 UINT uiActiveScheme;
206 DWORD id;
207 POINT pt;
208 PowerSchemeContext.hPopup = CreatePopupMenu();
209 EnumPwrSchemes(PowerSchemesEnumProc, (LPARAM)&PowerSchemeContext);
210
211 if (GetActivePwrScheme(&uiActiveScheme))
212 {
213 CheckMenuRadioItem(PowerSchemeContext.hPopup,
214 PowerSchemeContext.uiFirst,
215 PowerSchemeContext.uiLast,
216 uiActiveScheme + 1,
218 }
219
220 SetForegroundWindow(pSysTray->GetHWnd());
222
223 id = TrackPopupMenuEx(PowerSchemeContext.hPopup,
225 pt.x,
226 pt.y,
227 pSysTray->GetHWnd(),
228 NULL);
229
230 DestroyMenu(PowerSchemeContext.hPopup);
231
232 if (id != 0)
233 SetActivePwrScheme(id - 1, NULL, NULL);
234}
static BOOLEAN CALLBACK PowerSchemesEnumProc(UINT uiIndex, DWORD dwName, LPWSTR sName, DWORD dwDesc, LPWSTR sDesc, PPOWER_POLICY pp, LPARAM lParam)
Definition: power.cpp:177
GLuint id
Definition: glext.h:5910
BOOLEAN WINAPI GetActivePwrScheme(PUINT puiID)
Definition: powrprof.c:285
BOOLEAN WINAPI SetActivePwrScheme(UINT uiID, PGLOBAL_POWER_POLICY lpGlobalPowerPolicy, PPOWER_POLICY lpPowerPolicy)
Definition: powrprof.c:642
BOOLEAN WINAPI EnumPwrSchemes(PWRSCHEMESENUMPROC lpfnPwrSchemesEnumProc, LPARAM lParam)
Definition: powrprof.c:219
LONG_PTR LPARAM
Definition: windef.h:208
#define MF_BYCOMMAND
Definition: winuser.h:202
BOOL WINAPI CheckMenuRadioItem(_In_ HMENU, _In_ UINT, _In_ UINT, _In_ UINT, _In_ UINT)

Referenced by Power_Message().

Variable Documentation

◆ bc_icons

Definition at line 20 of file power.cpp.

Referenced by DynamicLoadIcon().

◆ br_icons

Definition at line 19 of file power.cpp.

Referenced by DynamicLoadIcon().

◆ g_hIconBattery

HICON g_hIconBattery = NULL
static

Definition at line 30 of file power.cpp.

Referenced by Power_Init(), and Power_Update().

◆ g_strTooltip

CString g_strTooltip

Definition at line 29 of file power.cpp.

Referenced by DynamicLoadIcon(), Power_Init(), and Power_Update().