VirtualBox

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

Last change on this file since 69500 was 69500, checked in by vboxsync, 7 years ago

*: scm --update-copyright-year

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