1 | /** @file
|
---|
2 | *
|
---|
3 | * VirtualBox interface to host's power notification service
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | /*******************************************************************************
|
---|
23 | * Header Files *
|
---|
24 | *******************************************************************************/
|
---|
25 | #include <windows.h>
|
---|
26 |
|
---|
27 | #include <VBox/com/ptr.h>
|
---|
28 | #include "HostPower.h"
|
---|
29 | #include "Logging.h"
|
---|
30 |
|
---|
31 | static char gachWindowClassName[] = "VBoxPowerNotifyClass";
|
---|
32 |
|
---|
33 | HostPowerServiceWin::HostPowerServiceWin(VirtualBox *aVirtualBox) : HostPowerService(aVirtualBox)
|
---|
34 | {
|
---|
35 | mHwnd = 0;
|
---|
36 |
|
---|
37 | #if 0
|
---|
38 | int rc = RTThreadCreate (&mThread, HostPowerServiceWin::NotificationThread, this, 65536,
|
---|
39 | RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "MainPower");
|
---|
40 |
|
---|
41 | if (RT_FAILURE (rc))
|
---|
42 | {
|
---|
43 | Log(("HostPowerServiceWin::HostPowerServiceWin: RTThreadCreate failed with %Rrc\n", rc));
|
---|
44 | return;
|
---|
45 | }
|
---|
46 | #endif
|
---|
47 | }
|
---|
48 |
|
---|
49 | HostPowerServiceWin::~HostPowerServiceWin()
|
---|
50 | {
|
---|
51 | if (mHwnd)
|
---|
52 | {
|
---|
53 | Log(("HostPowerServiceWin::!HostPowerServiceWin: destroy window %x\n", mHwnd));
|
---|
54 |
|
---|
55 | /* Is this allowed from another thread? */
|
---|
56 | SetWindowLongPtr(mHwnd, 0, 0);
|
---|
57 | PostMessage(mHwnd, WM_CLOSE, 0, 0);
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 |
|
---|
63 | DECLCALLBACK(int) HostPowerServiceWin::NotificationThread (RTTHREAD ThreadSelf, void *pInstance)
|
---|
64 | {
|
---|
65 | HostPowerServiceWin *pPowerObj = (HostPowerServiceWin *)pInstance;
|
---|
66 | HWND hwnd = 0;
|
---|
67 |
|
---|
68 | /* Create a window and make it a power event notification handler. */
|
---|
69 | int rc = VINF_SUCCESS;
|
---|
70 |
|
---|
71 | HINSTANCE hInstance = (HINSTANCE)GetModuleHandle (NULL);
|
---|
72 |
|
---|
73 | /* Register the Window Class. */
|
---|
74 | WNDCLASSA wc;
|
---|
75 |
|
---|
76 | wc.style = CS_NOCLOSE;
|
---|
77 | wc.lpfnWndProc = HostPowerServiceWin::WndProc;
|
---|
78 | wc.cbClsExtra = 0;
|
---|
79 | wc.cbWndExtra = sizeof(void *);
|
---|
80 | wc.hInstance = hInstance;
|
---|
81 | wc.hIcon = NULL;
|
---|
82 | wc.hCursor = NULL;
|
---|
83 | wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
|
---|
84 | wc.lpszMenuName = NULL;
|
---|
85 | wc.lpszClassName = gachWindowClassName;
|
---|
86 |
|
---|
87 | ATOM atomWindowClass = RegisterClassA(&wc);
|
---|
88 |
|
---|
89 | if (atomWindowClass == 0)
|
---|
90 | {
|
---|
91 | rc = VERR_NOT_SUPPORTED;
|
---|
92 | Log(("HostPowerServiceWin::NotificationThread: RegisterClassA failed with %x\n", GetLastError()));
|
---|
93 | }
|
---|
94 | else
|
---|
95 | {
|
---|
96 | /* Create the window. */
|
---|
97 | hwnd = pPowerObj->mHwnd = CreateWindowExA (WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
|
---|
98 | gachWindowClassName, gachWindowClassName,
|
---|
99 | WS_POPUPWINDOW,
|
---|
100 | -200, -200, 100, 100, NULL, NULL, hInstance, NULL);
|
---|
101 |
|
---|
102 | if (hwnd == NULL)
|
---|
103 | {
|
---|
104 | Log(("HostPowerServiceWin::NotificationThread: CreateWindowExA failed with %x\n", GetLastError()));
|
---|
105 | rc = VERR_NOT_SUPPORTED;
|
---|
106 | }
|
---|
107 | else
|
---|
108 | {
|
---|
109 | SetWindowLongPtr(hwnd, 0, (LONG_PTR)pPowerObj);
|
---|
110 | SetWindowPos(hwnd, HWND_TOPMOST, -200, -200, 0, 0,
|
---|
111 | SWP_NOACTIVATE | SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_NOSIZE);
|
---|
112 |
|
---|
113 | MSG msg;
|
---|
114 | while (GetMessage(&msg, NULL, 0, 0))
|
---|
115 | {
|
---|
116 | TranslateMessage(&msg);
|
---|
117 | DispatchMessage(&msg);
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | Log(("HostPowerServiceWin::NotificationThread: exit thread\n"));
|
---|
123 | if (hwnd)
|
---|
124 | {
|
---|
125 | DestroyWindow (hwnd);
|
---|
126 | }
|
---|
127 |
|
---|
128 | if (atomWindowClass != 0)
|
---|
129 | {
|
---|
130 | UnregisterClassA (gachWindowClassName, hInstance);
|
---|
131 | atomWindowClass = 0;
|
---|
132 | }
|
---|
133 |
|
---|
134 | return 0;
|
---|
135 | }
|
---|
136 |
|
---|
137 | LRESULT 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 | switch(wParam)
|
---|
147 | {
|
---|
148 | case PBT_APMSUSPEND:
|
---|
149 | pPowerObj->notify(HostPowerEvent_Suspend);
|
---|
150 | break;
|
---|
151 |
|
---|
152 | case PBT_APMRESUMEAUTOMATIC:
|
---|
153 | pPowerObj->notify(HostPowerEvent_Resume);
|
---|
154 | break;
|
---|
155 |
|
---|
156 | case PBT_APMPOWERSTATUSCHANGE:
|
---|
157 | {
|
---|
158 | SYSTEM_POWER_STATUS SystemPowerStatus;
|
---|
159 |
|
---|
160 | if (GetSystemPowerStatus(&SystemPowerStatus) == TRUE)
|
---|
161 | {
|
---|
162 | if ( SystemPowerStatus.BatteryFlag != 255 /* unknown */
|
---|
163 | && (SystemPowerStatus.BatteryFlag & 4 /* critical battery status; less than 5% */))
|
---|
164 | {
|
---|
165 | pPowerObj->notify(HostPowerEvent_BatteryLow);
|
---|
166 | }
|
---|
167 | }
|
---|
168 | break;
|
---|
169 | }
|
---|
170 |
|
---|
171 | }
|
---|
172 | return DefWindowProc (hwnd, msg, wParam, lParam);
|
---|
173 | }
|
---|
174 |
|
---|
175 | default:
|
---|
176 | return DefWindowProc (hwnd, msg, wParam, lParam);
|
---|
177 | }
|
---|
178 | }
|
---|