ReactOS  0.4.14-dev-991-g696cdc6
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 138 of file power.cpp.

139 {
140  ShellExecuteW(NULL, NULL, L"powercfg.cpl", NULL, NULL, SW_SHOWNORMAL);
141 }
smooth NULL
Definition: ftsmooth.c:416
static const WCHAR L[]
Definition: oid.c:1250
#define SW_SHOWNORMAL
Definition: winuser.h:764
HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpVerb, LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
Definition: shlexec.cpp:2250

Referenced by _ShowContextMenu(), and Power_Message().

◆ _ShowContextMenu()

static void _ShowContextMenu ( CSysTray pSysTray)
static

Definition at line 143 of file power.cpp.

144 {
146  HMENU hPopup = CreatePopupMenu();
147  AppendMenuW(hPopup, MF_STRING, IDS_PWR_PROPERTIES, strOpen);
149 
150  SetForegroundWindow(pSysTray->GetHWnd());
152  POINT pt;
153  GetCursorPos(&pt);
154 
155  DWORD id = TrackPopupMenuEx(hPopup, flags,
156  pt.x, pt.y,
157  pSysTray->GetHWnd(), NULL);
158 
159  switch (id)
160  {
161  case IDS_PWR_PROPERTIES:
162  _RunPower();
163  break;
164  }
165  DestroyMenu(hPopup);
166 }
#define pt(x, y)
Definition: drawing.c:79
#define TPM_RETURNCMD
Definition: winuser.h:2362
BOOL WINAPI TrackPopupMenuEx(_In_ HMENU, _In_ UINT, _In_ int, _In_ int, _In_ HWND, _In_opt_ LPTPMPARAMS)
#define MF_STRING
Definition: winuser.h:138
#define IDS_PWR_PROPERTIES
Definition: resource.h:5
BOOL WINAPI GetCursorPos(_Out_ LPPOINT)
Definition: cursoricon.c:2635
BOOL WINAPI AppendMenuW(_In_ HMENU, _In_ UINT, _In_ UINT_PTR, _In_opt_ LPCWSTR)
#define TPM_BOTTOMALIGN
Definition: winuser.h:2360
smooth NULL
Definition: ftsmooth.c:416
const char * LPCSTR
Definition: xmlstorage.h:183
unsigned long DWORD
Definition: ntddk_ex.h:95
#define TPM_NONOTIFY
Definition: winuser.h:2361
GLbitfield flags
Definition: glext.h:7161
BOOL WINAPI DestroyMenu(_In_ HMENU)
HWND GetHWnd()
Definition: csystray.h:53
BOOL WINAPI SetMenuDefaultItem(_In_ HMENU, _In_ UINT, _In_ UINT)
HMENU WINAPI CreatePopupMenu(void)
Definition: menu.c:846
BOOL WINAPI SetForegroundWindow(_In_ HWND)
static const CLSID *static CLSID *static const GUID VARIANT VARIANT *static IServiceProvider DWORD *static HMENU
Definition: ordinal.c:60
#define TPM_RIGHTALIGN
Definition: winuser.h:2353
static void _RunPower()
Definition: power.cpp:138

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  {
95  index = Quantize(PowerStatus.BatteryLifePercent);
98  }
99  else if (((PowerStatus.BatteryFlag & BATTERY_FLAG_NO_BATTERY) == 0) &&
100  ((PowerStatus.BatteryFlag & BATTERY_FLAG_CHARGING) == 0))
101  {
102  index = Quantize(PowerStatus.BatteryLifePercent);
105  }
106  else
107  {
109  g_strTooltip.LoadStringW(IDS_PWR_AC);
110  }
111 
112  return hBatIcon;
113 }
#define MAKEINTRESOURCE
Definition: winuser.h:591
static HICON
Definition: imagelist.c:84
void __cdecl Format(UINT nFormatID,...)
Definition: cstringt.h:688
BOOL WINAPI GetSystemPowerStatus(IN LPSYSTEM_POWER_STATUS PowerStatus)
Definition: power.c:27
#define BATTERY_FLAG_NO_BATTERY
Definition: winbase.h:500
#define IDS_PWR_CHARGING
Definition: resource.h:9
#define BATTERY_FLAG_UNKNOWN
Definition: winbase.h:501
static UINT Quantize(BYTE p)
Definition: power.cpp:51
GLuint index
Definition: glext.h:6031
#define IDS_PWR_UNKNOWN_REMAINING
Definition: resource.h:10
#define AC_LINE_UNKNOWN
Definition: winbase.h:495
int bc_icons[5]
Definition: power.cpp:20
#define IDI_BATTCAP_ERR
Definition: resource.h:53
int br_icons[5]
Definition: power.cpp:19
unsigned int UINT
Definition: ndis.h:50
#define BATTERY_FLAG_CHARGING
Definition: winbase.h:499
#define IDI_POWER_AC
Definition: resource.h:54
CString g_strTooltip
Definition: power.cpp:29
#define IDS_PWR_PERCENT_REMAINING
Definition: resource.h:8
static HINSTANCE hinst
Definition: edit.c:551
#define LoadIcon
Definition: winuser.h:5788
#define IDS_PWR_AC
Definition: resource.h:11

Referenced by Power_Init(), and Power_Update().

◆ Power_Init()

HRESULT STDMETHODCALLTYPE Power_Init ( _In_ CSysTray pSysTray)

Definition at line 115 of file power.cpp.

116 {
117  TRACE("Power_Init\n");
119 
120  return pSysTray->NotifyIcon(NIM_ADD, ID_ICON_POWER, g_hIconBattery, g_strTooltip);
121 }
static HICON g_hIconBattery
Definition: power.cpp:30
#define TRACE(s)
Definition: solgame.cpp:4
#define ID_ICON_POWER
Definition: precomp.h:35
static HICON DynamicLoadIcon(HINSTANCE hinst)
Definition: power.cpp:77
#define NIM_ADD
Definition: shellapi.h:91
CString g_strTooltip
Definition: power.cpp:29
HINSTANCE g_hInstance
Definition: MainWindow.cpp:18

Referenced by Power_Message().

◆ Power_Message()

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

Definition at line 230 of file power.cpp.

231 {
232  TRACE("Power_Message uMsg=%d, wParam=%x, lParam=%x\n", uMsg, wParam, lParam);
233 
234  switch (uMsg)
235  {
236  case WM_USER + 220:
237  TRACE("Power_Message: WM_USER+220\n");
238  if (wParam == POWER_SERVICE_FLAG)
239  {
240  if (lParam)
241  {
242  pSysTray->EnableService(POWER_SERVICE_FLAG, TRUE);
243  return Power_Init(pSysTray);
244  }
245  else
246  {
247  pSysTray->EnableService(POWER_SERVICE_FLAG, FALSE);
248  return Power_Shutdown(pSysTray);
249  }
250  }
251  return S_FALSE;
252 
253  case WM_USER + 221:
254  TRACE("Power_Message: WM_USER+221\n");
255  if (wParam == POWER_SERVICE_FLAG)
256  {
257  lResult = (LRESULT)pSysTray->IsServiceEnabled(POWER_SERVICE_FLAG);
258  return S_OK;
259  }
260  return S_FALSE;
261 
262  case WM_TIMER:
263  if (wParam == POWER_TIMER_ID)
264  {
265  KillTimer(pSysTray->GetHWnd(), POWER_TIMER_ID);
266  ShowPowerSchemesPopupMenu(pSysTray);
267  }
268  break;
269 
270  case ID_ICON_POWER:
271  Power_Update(pSysTray);
272 
273  switch (lParam)
274  {
275  case WM_LBUTTONDOWN:
276  SetTimer(pSysTray->GetHWnd(), POWER_TIMER_ID, GetDoubleClickTime(), NULL);
277  break;
278 
279  case WM_LBUTTONUP:
280  break;
281 
282  case WM_LBUTTONDBLCLK:
283  KillTimer(pSysTray->GetHWnd(), POWER_TIMER_ID);
284  _RunPower();
285  break;
286 
287  case WM_RBUTTONDOWN:
288  break;
289 
290  case WM_RBUTTONUP:
291  _ShowContextMenu(pSysTray);
292  break;
293 
294  case WM_RBUTTONDBLCLK:
295  break;
296 
297  case WM_MOUSEMOVE:
298  break;
299  }
300  return S_OK;
301 
302  default:
303  TRACE("Power_Message received for unknown ID %d, ignoring.\n");
304  return S_FALSE;
305  }
306 
307  return S_FALSE;
308 }
#define POWER_SERVICE_FLAG
Definition: precomp.h:37
HRESULT STDMETHODCALLTYPE Power_Init(_In_ CSysTray *pSysTray)
Definition: power.cpp:115
#define TRUE
Definition: types.h:120
#define WM_LBUTTONDOWN
Definition: winuser.h:1758
HRESULT STDMETHODCALLTYPE Power_Update(_In_ CSysTray *pSysTray)
Definition: power.cpp:123
static void _ShowContextMenu(CSysTray *pSysTray)
Definition: power.cpp:143
WPARAM wParam
Definition: combotst.c:138
#define S_FALSE
Definition: winerror.h:2357
smooth NULL
Definition: ftsmooth.c:416
UINT_PTR WINAPI SetTimer(_In_opt_ HWND, _In_ UINT_PTR, _In_ UINT, _In_opt_ TIMERPROC)
#define WM_RBUTTONDOWN
Definition: winuser.h:1761
#define TRACE(s)
Definition: solgame.cpp:4
#define ID_ICON_POWER
Definition: precomp.h:35
#define WM_TIMER
Definition: winuser.h:1724
#define WM_RBUTTONUP
Definition: winuser.h:1762
BOOL WINAPI KillTimer(_In_opt_ HWND, _In_ UINT_PTR)
#define WM_LBUTTONDBLCLK
Definition: winuser.h:1760
#define LRESULT
Definition: ole.h:14
static VOID ShowPowerSchemesPopupMenu(CSysTray *pSysTray)
Definition: power.cpp:195
#define WM_USER
Definition: winuser.h:1877
#define S_OK
Definition: intsafe.h:59
#define POWER_TIMER_ID
Definition: precomp.h:77
#define WM_LBUTTONUP
Definition: winuser.h:1759
#define WM_MOUSEMOVE
Definition: winuser.h:1757
UINT WINAPI GetDoubleClickTime(void)
Definition: ntwrapper.h:314
#define WM_RBUTTONDBLCLK
Definition: winuser.h:1763
HRESULT STDMETHODCALLTYPE Power_Shutdown(_In_ CSysTray *pSysTray)
Definition: power.cpp:131
LPARAM lParam
Definition: combotst.c:139
static void _RunPower()
Definition: power.cpp:138

◆ Power_Shutdown()

HRESULT STDMETHODCALLTYPE Power_Shutdown ( _In_ CSysTray pSysTray)

Definition at line 131 of file power.cpp.

132 {
133  TRACE("Power_Shutdown\n");
134 
135  return pSysTray->NotifyIcon(NIM_DELETE, ID_ICON_POWER, NULL, NULL);
136 }
smooth NULL
Definition: ftsmooth.c:416
#define TRACE(s)
Definition: solgame.cpp:4
#define ID_ICON_POWER
Definition: precomp.h:35
#define NIM_DELETE
Definition: shellapi.h:93

Referenced by Power_Message().

◆ Power_Update()

HRESULT STDMETHODCALLTYPE Power_Update ( _In_ CSysTray pSysTray)

Definition at line 123 of file power.cpp.

124 {
125  TRACE("Power_Update\n");
127 
128  return pSysTray->NotifyIcon(NIM_MODIFY, ID_ICON_POWER, g_hIconBattery, g_strTooltip);
129 }
static HICON g_hIconBattery
Definition: power.cpp:30
#define TRACE(s)
Definition: solgame.cpp:4
#define ID_ICON_POWER
Definition: precomp.h:35
static HICON DynamicLoadIcon(HINSTANCE hinst)
Definition: power.cpp:77
CString g_strTooltip
Definition: power.cpp:29
HINSTANCE g_hInstance
Definition: MainWindow.cpp:18
#define NIM_MODIFY
Definition: shellapi.h:92

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 171 of file power.cpp.

179 {
180  PPWRSCHEMECONTEXT PowerSchemeContext = (PPWRSCHEMECONTEXT)lParam;
181 
182  if (AppendMenuW(PowerSchemeContext->hPopup, MF_STRING, uiIndex + 1, sName))
183  {
184  if (PowerSchemeContext->uiFirst == 0)
185  PowerSchemeContext->uiFirst = uiIndex + 1;
186 
187  PowerSchemeContext->uiLast = uiIndex + 1;
188  }
189 
190  return TRUE;
191 }
#define TRUE
Definition: types.h:120
struct _PWRSCHEMECONTEXT * PPWRSCHEMECONTEXT
#define MF_STRING
Definition: winuser.h:138
BOOL WINAPI AppendMenuW(_In_ HMENU, _In_ UINT, _In_ UINT_PTR, _In_opt_ LPCWSTR)
LPARAM lParam
Definition: combotst.c:139

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 195 of file power.cpp.

197 {
198  PWRSCHEMECONTEXT PowerSchemeContext = {NULL, 0, 0};
199  UINT uiActiveScheme;
200  DWORD id;
201  POINT pt;
202  PowerSchemeContext.hPopup = CreatePopupMenu();
203  EnumPwrSchemes(PowerSchemesEnumProc, (LPARAM)&PowerSchemeContext);
204 
205  if (GetActivePwrScheme(&uiActiveScheme))
206  {
207  CheckMenuRadioItem(PowerSchemeContext.hPopup,
208  PowerSchemeContext.uiFirst,
209  PowerSchemeContext.uiLast,
210  uiActiveScheme + 1,
211  MF_BYCOMMAND);
212  }
213 
214  SetForegroundWindow(pSysTray->GetHWnd());
215  GetCursorPos(&pt);
216 
217  id = TrackPopupMenuEx(PowerSchemeContext.hPopup,
219  pt.x,
220  pt.y,
221  pSysTray->GetHWnd(),
222  NULL);
223 
224  DestroyMenu(PowerSchemeContext.hPopup);
225 
226  if (id != 0)
227  SetActivePwrScheme(id - 1, NULL, NULL);
228 }
static BOOLEAN CALLBACK PowerSchemesEnumProc(UINT uiIndex, DWORD dwName, LPWSTR sName, DWORD dwDesc, LPWSTR sDesc, PPOWER_POLICY pp, LPARAM lParam)
Definition: power.cpp:171
#define MF_BYCOMMAND
Definition: winuser.h:202
#define pt(x, y)
Definition: drawing.c:79
BOOLEAN WINAPI SetActivePwrScheme(UINT uiID, PGLOBAL_POWER_POLICY lpGlobalPowerPolicy, PPOWER_POLICY lpPowerPolicy)
Definition: powrprof.c:642
BOOL WINAPI CheckMenuRadioItem(_In_ HMENU, _In_ UINT, _In_ UINT, _In_ UINT, _In_ UINT)
#define TPM_RETURNCMD
Definition: winuser.h:2362
BOOL WINAPI TrackPopupMenuEx(_In_ HMENU, _In_ UINT, _In_ int, _In_ int, _In_ HWND, _In_opt_ LPTPMPARAMS)
BOOL WINAPI GetCursorPos(_Out_ LPPOINT)
Definition: cursoricon.c:2635
#define TPM_BOTTOMALIGN
Definition: winuser.h:2360
smooth NULL
Definition: ftsmooth.c:416
LONG_PTR LPARAM
Definition: windef.h:208
BOOLEAN WINAPI EnumPwrSchemes(PWRSCHEMESENUMPROC lpfnPwrSchemesEnumProc, LPARAM lParam)
Definition: powrprof.c:219
BOOLEAN WINAPI GetActivePwrScheme(PUINT puiID)
Definition: powrprof.c:285
unsigned long DWORD
Definition: ntddk_ex.h:95
#define TPM_NONOTIFY
Definition: winuser.h:2361
BOOL WINAPI DestroyMenu(_In_ HMENU)
HWND GetHWnd()
Definition: csystray.h:53
unsigned int UINT
Definition: ndis.h:50
HMENU WINAPI CreatePopupMenu(void)
Definition: menu.c:846
GLenum GLuint id
Definition: glext.h:5579
BOOL WINAPI SetForegroundWindow(_In_ HWND)
#define TPM_RIGHTALIGN
Definition: winuser.h:2353

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().