VirtualBox

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

Last change on this file since 48955 was 46775, checked in by vboxsync, 11 years ago

Main/Host(HostPower)+Session+Console: convert HostPower code to signal pause/resume/savestate through internal methods, conveying information why the method was called, preparing for VM/PDM passing this information to devices and drivers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.1 KB
Line 
1/** @file
2 *
3 * VirtualBox interface to host's power notification service
4 */
5
6/*
7 * Copyright (C) 2006-2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/*******************************************************************************
19* Header Files *
20*******************************************************************************/
21#include <windows.h>
22/* Some SDK versions lack the extern "C" and thus cause linking failures.
23 * This workaround isn't pretty, but there are not many options. */
24extern "C" {
25#include <PowrProf.h>
26}
27
28#include <VBox/com/ptr.h>
29#include "HostPower.h"
30#include "Logging.h"
31
32static WCHAR gachWindowClassName[] = L"VBoxPowerNotifyClass";
33
34HostPowerServiceWin::HostPowerServiceWin(VirtualBox *aVirtualBox) : HostPowerService(aVirtualBox), mThread(NIL_RTTHREAD)
35{
36 mHwnd = 0;
37
38 int rc = RTThreadCreate(&mThread, HostPowerServiceWin::NotificationThread, this, 65536,
39 RTTHREADTYPE_GUI, RTTHREADFLAGS_WAITABLE, "MainPower");
40
41 if (RT_FAILURE(rc))
42 {
43 Log(("HostPowerServiceWin::HostPowerServiceWin: RTThreadCreate failed with %Rrc\n", rc));
44 return;
45 }
46}
47
48HostPowerServiceWin::~HostPowerServiceWin()
49{
50 if (mHwnd)
51 {
52 Log(("HostPowerServiceWin::!HostPowerServiceWin: destroy window %x\n", mHwnd));
53
54 /* Is this allowed from another thread? */
55 SetWindowLongPtr(mHwnd, 0, 0);
56 /* Send the quit message and wait for it be processed. */
57 SendMessage(mHwnd, WM_QUIT, 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 while (GetMessage(&msg, NULL, 0, 0))
117 {
118 TranslateMessage(&msg);
119 DispatchMessage(&msg);
120 }
121 }
122 }
123
124 Log(("HostPowerServiceWin::NotificationThread: exit thread\n"));
125 if (hwnd)
126 DestroyWindow(hwnd);
127
128 if (atomWindowClass != 0)
129 {
130 UnregisterClass(gachWindowClassName, hInstance);
131 atomWindowClass = 0;
132 }
133
134 return 0;
135}
136
137LRESULT CALLBACK HostPowerServiceWin::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
138{
139 switch (msg)
140 {
141 case WM_POWERBROADCAST:
142 {
143 HostPowerServiceWin *pPowerObj;
144
145 pPowerObj = (HostPowerServiceWin *)GetWindowLongPtr(hwnd, 0);
146 if (pPowerObj)
147 {
148 switch(wParam)
149 {
150 case PBT_APMSUSPEND:
151 pPowerObj->notify(Reason_HostSuspend);
152 break;
153
154 case PBT_APMRESUMEAUTOMATIC:
155 pPowerObj->notify(Reason_HostResume);
156 break;
157
158 case PBT_APMPOWERSTATUSCHANGE:
159 {
160 SYSTEM_POWER_STATUS SystemPowerStatus;
161
162 Log(("PBT_APMPOWERSTATUSCHANGE\n"));
163 if (GetSystemPowerStatus(&SystemPowerStatus) == TRUE)
164 {
165 Log(("PBT_APMPOWERSTATUSCHANGE ACLineStatus=%d BatteryFlag=%d\n", SystemPowerStatus.ACLineStatus, SystemPowerStatus.BatteryFlag));
166
167 if (SystemPowerStatus.ACLineStatus == 0) /* offline */
168 {
169 if (SystemPowerStatus.BatteryFlag == 2 /* low > 33% */)
170 {
171 LONG rc;
172 SYSTEM_BATTERY_STATE BatteryState;
173
174 rc = CallNtPowerInformation(SystemBatteryState, NULL, 0, (PVOID)&BatteryState, sizeof(BatteryState));
175#ifdef LOG_ENABLED
176 if (rc == 0 /* STATUS_SUCCESS */)
177 Log(("CallNtPowerInformation claims %d seconds of power left\n", BatteryState.EstimatedTime));
178#endif
179 if ( rc == 0 /* STATUS_SUCCESS */
180 && BatteryState.EstimatedTime < 60*5)
181 {
182 pPowerObj->notify(Reason_HostBatteryLow);
183 }
184 }
185 else
186 /* If the machine has less than 5% battery left (and is not connected to the AC), then we should save the state. */
187 if (SystemPowerStatus.BatteryFlag == 4 /* critical battery status; less than 5% */)
188 {
189 pPowerObj->notify(Reason_HostBatteryLow);
190 }
191 }
192 }
193 break;
194 }
195 default:
196 return DefWindowProc(hwnd, msg, wParam, lParam);
197 }
198 }
199 return TRUE;
200 }
201
202 default:
203 return DefWindowProc(hwnd, msg, wParam, lParam);
204 }
205}
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