VirtualBox

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

Last change on this file since 62499 was 62485, checked in by vboxsync, 9 years ago

(C) 2016

  • 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 62485 2016-07-22 18:36:43Z vboxsync $ */
2/** @file
3 *
4 * VirtualBox interface to host's power notification service
5 */
6
7/*
8 * Copyright (C) 2006-2016 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 /* Poke the thread out of the event loop and wait for it to clean up. */
57 PostMessage(mHwnd, WM_CLOSE, 0, 0);
58 RTThreadWait(mThread, 5000, NULL);
59 mThread = NIL_RTTHREAD;
60 }
61}
62
63
64
65DECLCALLBACK(int) HostPowerServiceWin::NotificationThread(RTTHREAD ThreadSelf, void *pInstance)
66{
67 HostPowerServiceWin *pPowerObj = (HostPowerServiceWin *)pInstance;
68 HWND hwnd = 0;
69
70 /* Create a window and make it a power event notification handler. */
71 int rc = VINF_SUCCESS;
72
73 HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
74
75 /* Register the Window Class. */
76 WNDCLASS wc;
77
78 wc.style = CS_NOCLOSE;
79 wc.lpfnWndProc = HostPowerServiceWin::WndProc;
80 wc.cbClsExtra = 0;
81 wc.cbWndExtra = sizeof(void *);
82 wc.hInstance = hInstance;
83 wc.hIcon = NULL;
84 wc.hCursor = NULL;
85 wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
86 wc.lpszMenuName = NULL;
87 wc.lpszClassName = gachWindowClassName;
88
89 ATOM atomWindowClass = RegisterClass(&wc);
90
91 if (atomWindowClass == 0)
92 {
93 rc = VERR_NOT_SUPPORTED;
94 Log(("HostPowerServiceWin::NotificationThread: RegisterClassA failed with %x\n", GetLastError()));
95 }
96 else
97 {
98 /* Create the window. */
99 hwnd = pPowerObj->mHwnd = CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
100 gachWindowClassName, gachWindowClassName,
101 WS_POPUPWINDOW,
102 -200, -200, 100, 100, NULL, NULL, hInstance, NULL);
103
104 if (hwnd == NULL)
105 {
106 Log(("HostPowerServiceWin::NotificationThread: CreateWindowExA failed with %x\n", GetLastError()));
107 rc = VERR_NOT_SUPPORTED;
108 }
109 else
110 {
111 SetWindowLongPtr(hwnd, 0, (LONG_PTR)pPowerObj);
112 SetWindowPos(hwnd, HWND_TOPMOST, -200, -200, 0, 0,
113 SWP_NOACTIVATE | SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_NOSIZE);
114
115 MSG msg;
116 BOOL fRet;
117 while ((fRet = GetMessage(&msg, NULL, 0, 0)) > 0)
118 {
119 TranslateMessage(&msg);
120 DispatchMessage(&msg);
121 }
122 /*
123 * Window procedure can return error,
124 * but this is exceptional situation
125 * that should be identified in testing
126 */
127 Assert(fRet >= 0);
128 }
129 }
130
131 Log(("HostPowerServiceWin::NotificationThread: exit thread\n"));
132
133 if (atomWindowClass != 0)
134 {
135 UnregisterClass(gachWindowClassName, hInstance);
136 atomWindowClass = 0;
137 }
138
139 return 0;
140}
141
142LRESULT CALLBACK HostPowerServiceWin::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
143{
144 switch (msg)
145 {
146 case WM_POWERBROADCAST:
147 {
148 HostPowerServiceWin *pPowerObj;
149
150 pPowerObj = (HostPowerServiceWin *)GetWindowLongPtr(hwnd, 0);
151 if (pPowerObj)
152 {
153 switch(wParam)
154 {
155 case PBT_APMSUSPEND:
156 pPowerObj->notify(Reason_HostSuspend);
157 break;
158
159 case PBT_APMRESUMEAUTOMATIC:
160 pPowerObj->notify(Reason_HostResume);
161 break;
162
163 case PBT_APMPOWERSTATUSCHANGE:
164 {
165 SYSTEM_POWER_STATUS SystemPowerStatus;
166
167 Log(("PBT_APMPOWERSTATUSCHANGE\n"));
168 if (GetSystemPowerStatus(&SystemPowerStatus) == TRUE)
169 {
170 Log(("PBT_APMPOWERSTATUSCHANGE ACLineStatus=%d BatteryFlag=%d\n", SystemPowerStatus.ACLineStatus,
171 SystemPowerStatus.BatteryFlag));
172
173 if (SystemPowerStatus.ACLineStatus == 0) /* offline */
174 {
175 if (SystemPowerStatus.BatteryFlag == 2 /* low > 33% */)
176 {
177 LONG rc;
178 SYSTEM_BATTERY_STATE BatteryState;
179
180 rc = CallNtPowerInformation(SystemBatteryState, NULL, 0, (PVOID)&BatteryState,
181 sizeof(BatteryState));
182#ifdef LOG_ENABLED
183 if (rc == 0 /* STATUS_SUCCESS */)
184 Log(("CallNtPowerInformation claims %d seconds of power left\n",
185 BatteryState.EstimatedTime));
186#endif
187 if ( rc == 0 /* STATUS_SUCCESS */
188 && BatteryState.EstimatedTime < 60*5)
189 {
190 pPowerObj->notify(Reason_HostBatteryLow);
191 }
192 }
193 /* If the machine has less than 5% battery left (and is not connected
194 * to the AC), then we should save the state. */
195 else if (SystemPowerStatus.BatteryFlag == 4 /* critical battery status; less than 5% */)
196 {
197 pPowerObj->notify(Reason_HostBatteryLow);
198 }
199 }
200 }
201 break;
202 }
203 default:
204 return DefWindowProc(hwnd, msg, wParam, lParam);
205 }
206 }
207 return TRUE;
208 }
209
210 case WM_DESTROY:
211 {
212 /* moved here. it can't work across theads */
213 SetWindowLongPtr(hwnd, 0, 0);
214 PostQuitMessage(0);
215 return 0;
216 }
217
218 default:
219 return DefWindowProc(hwnd, msg, wParam, lParam);
220 }
221}
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