VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/win/HostPowerWin.cpp@ 61888

Last change on this file since 61888 was 60052, checked in by vboxsync, 9 years ago

warning

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
Line 
1/* $Id: HostPowerWin.cpp 60052 2016-03-15 21:44:49Z vboxsync $ */
2/** @file
3 *
4 * VirtualBox interface to host's power notification service
5 */
6
7/*
8 * Copyright (C) 2006-2013 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19
20/*********************************************************************************************************************************
21* Header Files *
22*********************************************************************************************************************************/
23#include <windows.h>
24/* Some SDK versions lack the extern "C" and thus cause linking failures.
25 * This workaround isn't pretty, but there are not many options. */
26extern "C" {
27#include <PowrProf.h>
28}
29
30#include <VBox/com/ptr.h>
31#include "HostPower.h"
32#include "Logging.h"
33
34static WCHAR gachWindowClassName[] = L"VBoxPowerNotifyClass";
35
36HostPowerServiceWin::HostPowerServiceWin(VirtualBox *aVirtualBox) : HostPowerService(aVirtualBox), mThread(NIL_RTTHREAD)
37{
38 mHwnd = 0;
39
40 int rc = RTThreadCreate(&mThread, HostPowerServiceWin::NotificationThread, this, 65536,
41 RTTHREADTYPE_GUI, RTTHREADFLAGS_WAITABLE, "MainPower");
42
43 if (RT_FAILURE(rc))
44 {
45 Log(("HostPowerServiceWin::HostPowerServiceWin: RTThreadCreate failed with %Rrc\n", rc));
46 return;
47 }
48}
49
50HostPowerServiceWin::~HostPowerServiceWin()
51{
52 if (mHwnd)
53 {
54 Log(("HostPowerServiceWin::!HostPowerServiceWin: destroy window %x\n", mHwnd));
55
56 /* Is this allowed from another thread? */
57 SetWindowLongPtr(mHwnd, 0, 0);
58 /* Poke the thread out of the event loop and wait for it to clean up. */
59 PostMessage(mHwnd, WM_QUIT, 0, 0);
60 RTThreadWait(mThread, 5000, NULL);
61 mThread = NIL_RTTHREAD;
62 }
63}
64
65
66
67DECLCALLBACK(int) HostPowerServiceWin::NotificationThread(RTTHREAD ThreadSelf, void *pInstance)
68{
69 HostPowerServiceWin *pPowerObj = (HostPowerServiceWin *)pInstance;
70 HWND hwnd = 0;
71
72 /* Create a window and make it a power event notification handler. */
73 int rc = VINF_SUCCESS;
74
75 HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
76
77 /* Register the Window Class. */
78 WNDCLASS wc;
79
80 wc.style = CS_NOCLOSE;
81 wc.lpfnWndProc = HostPowerServiceWin::WndProc;
82 wc.cbClsExtra = 0;
83 wc.cbWndExtra = sizeof(void *);
84 wc.hInstance = hInstance;
85 wc.hIcon = NULL;
86 wc.hCursor = NULL;
87 wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
88 wc.lpszMenuName = NULL;
89 wc.lpszClassName = gachWindowClassName;
90
91 ATOM atomWindowClass = RegisterClass(&wc);
92
93 if (atomWindowClass == 0)
94 {
95 rc = VERR_NOT_SUPPORTED;
96 Log(("HostPowerServiceWin::NotificationThread: RegisterClassA failed with %x\n", GetLastError()));
97 }
98 else
99 {
100 /* Create the window. */
101 hwnd = pPowerObj->mHwnd = CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
102 gachWindowClassName, gachWindowClassName,
103 WS_POPUPWINDOW,
104 -200, -200, 100, 100, NULL, NULL, hInstance, NULL);
105
106 if (hwnd == NULL)
107 {
108 Log(("HostPowerServiceWin::NotificationThread: CreateWindowExA failed with %x\n", GetLastError()));
109 rc = VERR_NOT_SUPPORTED;
110 }
111 else
112 {
113 SetWindowLongPtr(hwnd, 0, (LONG_PTR)pPowerObj);
114 SetWindowPos(hwnd, HWND_TOPMOST, -200, -200, 0, 0,
115 SWP_NOACTIVATE | SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_NOSIZE);
116
117 MSG msg;
118 BOOL fRet;
119 while ((fRet = GetMessage(&msg, NULL, 0, 0)) != 0)
120 {
121 if (fRet != -1)
122 {
123 TranslateMessage(&msg);
124 DispatchMessage(&msg);
125 }
126 else
127 {
128 // handle the error and possibly exit
129 break;
130 }
131 }
132 }
133 }
134
135 Log(("HostPowerServiceWin::NotificationThread: exit thread\n"));
136 if (hwnd)
137 DestroyWindow(hwnd);
138
139 if (atomWindowClass != 0)
140 {
141 UnregisterClass(gachWindowClassName, hInstance);
142 atomWindowClass = 0;
143 }
144
145 return 0;
146}
147
148LRESULT CALLBACK HostPowerServiceWin::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
149{
150 switch (msg)
151 {
152 case WM_POWERBROADCAST:
153 {
154 HostPowerServiceWin *pPowerObj;
155
156 pPowerObj = (HostPowerServiceWin *)GetWindowLongPtr(hwnd, 0);
157 if (pPowerObj)
158 {
159 switch(wParam)
160 {
161 case PBT_APMSUSPEND:
162 pPowerObj->notify(Reason_HostSuspend);
163 break;
164
165 case PBT_APMRESUMEAUTOMATIC:
166 pPowerObj->notify(Reason_HostResume);
167 break;
168
169 case PBT_APMPOWERSTATUSCHANGE:
170 {
171 SYSTEM_POWER_STATUS SystemPowerStatus;
172
173 Log(("PBT_APMPOWERSTATUSCHANGE\n"));
174 if (GetSystemPowerStatus(&SystemPowerStatus) == TRUE)
175 {
176 Log(("PBT_APMPOWERSTATUSCHANGE ACLineStatus=%d BatteryFlag=%d\n", SystemPowerStatus.ACLineStatus,
177 SystemPowerStatus.BatteryFlag));
178
179 if (SystemPowerStatus.ACLineStatus == 0) /* offline */
180 {
181 if (SystemPowerStatus.BatteryFlag == 2 /* low > 33% */)
182 {
183 LONG rc;
184 SYSTEM_BATTERY_STATE BatteryState;
185
186 rc = CallNtPowerInformation(SystemBatteryState, NULL, 0, (PVOID)&BatteryState,
187 sizeof(BatteryState));
188#ifdef LOG_ENABLED
189 if (rc == 0 /* STATUS_SUCCESS */)
190 Log(("CallNtPowerInformation claims %d seconds of power left\n",
191 BatteryState.EstimatedTime));
192#endif
193 if ( rc == 0 /* STATUS_SUCCESS */
194 && BatteryState.EstimatedTime < 60*5)
195 {
196 pPowerObj->notify(Reason_HostBatteryLow);
197 }
198 }
199 /* If the machine has less than 5% battery left (and is not connected
200 * to the AC), then we should save the state. */
201 else if (SystemPowerStatus.BatteryFlag == 4 /* critical battery status; less than 5% */)
202 {
203 pPowerObj->notify(Reason_HostBatteryLow);
204 }
205 }
206 }
207 break;
208 }
209 default:
210 return DefWindowProc(hwnd, msg, wParam, lParam);
211 }
212 }
213 return TRUE;
214 }
215
216 default:
217 return DefWindowProc(hwnd, msg, wParam, lParam);
218 }
219}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette