1 | /** @file
|
---|
2 | * VBoxService - Guest Additions Service
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License as published by the Free Software Foundation,
|
---|
12 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
13 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
14 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * If you received this file as part of a commercial VirtualBox
|
---|
17 | * distribution, then only the terms of your commercial VirtualBox
|
---|
18 | * license agreement apply instead of the previous paragraph.
|
---|
19 | *
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include "VBoxService.h"
|
---|
23 | #include "VBoxSeamless.h"
|
---|
24 | #include "resource.h"
|
---|
25 | #include <malloc.h>
|
---|
26 |
|
---|
27 | #include "helpers.h"
|
---|
28 |
|
---|
29 | /* global variables */
|
---|
30 | HANDLE gVBoxDriver;
|
---|
31 | HANDLE gStopSem;
|
---|
32 | SERVICE_STATUS gVBoxServiceStatus;
|
---|
33 | SERVICE_STATUS_HANDLE gVBoxServiceStatusHandle;
|
---|
34 | HINSTANCE gInstance;
|
---|
35 | HWND gToolWindow;
|
---|
36 |
|
---|
37 |
|
---|
38 | /* prototypes */
|
---|
39 | VOID DisplayChangeThread(void *dummy);
|
---|
40 | LRESULT CALLBACK VBoxToolWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
|
---|
41 |
|
---|
42 |
|
---|
43 | #ifdef DEBUG
|
---|
44 | /**
|
---|
45 | * Helper function to send a message to WinDbg
|
---|
46 | *
|
---|
47 | * @param String message string
|
---|
48 | */
|
---|
49 | void WriteLog(char *String, ...)
|
---|
50 | {
|
---|
51 | DWORD cbReturned;
|
---|
52 | CHAR Buffer[1024];
|
---|
53 | VMMDevReqLogString *pReq = (VMMDevReqLogString *)Buffer;
|
---|
54 |
|
---|
55 | va_list va;
|
---|
56 |
|
---|
57 | va_start(va, String);
|
---|
58 |
|
---|
59 | vmmdevInitRequest(&pReq->header, VMMDevReq_LogString);
|
---|
60 | vsprintf(pReq->szString, String, va);
|
---|
61 | OutputDebugStringA(pReq->szString);
|
---|
62 | pReq->header.size += strlen(pReq->szString);
|
---|
63 |
|
---|
64 | DeviceIoControl(gVBoxDriver, IOCTL_VBOXGUEST_VMMREQUEST, pReq, pReq->header.size,
|
---|
65 | pReq, pReq->header.size, &cbReturned, NULL);
|
---|
66 |
|
---|
67 | va_end (va);
|
---|
68 | return;
|
---|
69 | }
|
---|
70 | #endif
|
---|
71 |
|
---|
72 |
|
---|
73 |
|
---|
74 | /* The shared clipboard service prototypes. */
|
---|
75 | int VBoxClipboardInit (const VBOXSERVICEENV *pEnv, void **ppInstance, bool *pfStartThread);
|
---|
76 | unsigned __stdcall VBoxClipboardThread (void *pInstance);
|
---|
77 | void VBoxClipboardDestroy (const VBOXSERVICEENV *pEnv, void *pInstance);
|
---|
78 |
|
---|
79 | /* The service table. */
|
---|
80 | static VBOXSERVICEINFO vboxServiceTable[] =
|
---|
81 | {
|
---|
82 | {
|
---|
83 | "Shared Clipboard",
|
---|
84 | VBoxClipboardInit,
|
---|
85 | VBoxClipboardThread,
|
---|
86 | VBoxClipboardDestroy
|
---|
87 | },
|
---|
88 | {
|
---|
89 | "Seamless Windows",
|
---|
90 | VBoxSeamlessInit,
|
---|
91 | VBoxSeamlessThread,
|
---|
92 | VBoxSeamlessDestroy
|
---|
93 | },
|
---|
94 | {
|
---|
95 | NULL
|
---|
96 | }
|
---|
97 | };
|
---|
98 |
|
---|
99 | static int vboxStartServices (VBOXSERVICEENV *pEnv, VBOXSERVICEINFO *pTable)
|
---|
100 | {
|
---|
101 | pEnv->hStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
---|
102 |
|
---|
103 | if (!pEnv->hStopEvent)
|
---|
104 | {
|
---|
105 | /* Could not create event. */
|
---|
106 | return VERR_NOT_SUPPORTED;
|
---|
107 | }
|
---|
108 |
|
---|
109 | while (pTable->pszName)
|
---|
110 | {
|
---|
111 | dprintf(("Starting %s...\n", pTable->pszName));
|
---|
112 |
|
---|
113 | int rc = VINF_SUCCESS;
|
---|
114 |
|
---|
115 | bool fStartThread = false;
|
---|
116 |
|
---|
117 | pTable->hThread = (HANDLE)0;
|
---|
118 | pTable->pInstance = NULL;
|
---|
119 | pTable->fStarted = false;
|
---|
120 |
|
---|
121 | if (pTable->pfnInit)
|
---|
122 | {
|
---|
123 | rc = pTable->pfnInit (pEnv, &pTable->pInstance, &fStartThread);
|
---|
124 | }
|
---|
125 |
|
---|
126 | if (VBOX_FAILURE (rc))
|
---|
127 | {
|
---|
128 | dprintf(("Failed to initialize rc = %Vrc.\n", rc));
|
---|
129 | }
|
---|
130 | else
|
---|
131 | {
|
---|
132 | if (pTable->pfnThread && fStartThread)
|
---|
133 | {
|
---|
134 | unsigned threadid;
|
---|
135 |
|
---|
136 | pTable->hThread = (HANDLE)_beginthreadex (NULL, /* security */
|
---|
137 | 0, /* stacksize */
|
---|
138 | pTable->pfnThread,
|
---|
139 | pTable->pInstance,
|
---|
140 | 0, /* initflag */
|
---|
141 | &threadid);
|
---|
142 |
|
---|
143 | if (pTable->hThread == (HANDLE)(0))
|
---|
144 | {
|
---|
145 | rc = VERR_NOT_SUPPORTED;
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | if (VBOX_FAILURE (rc))
|
---|
150 | {
|
---|
151 | dprintf(("Failed to start the thread.\n"));
|
---|
152 |
|
---|
153 | if (pTable->pfnDestroy)
|
---|
154 | {
|
---|
155 | pTable->pfnDestroy (pEnv, pTable->pInstance);
|
---|
156 | }
|
---|
157 | }
|
---|
158 | else
|
---|
159 | {
|
---|
160 | pTable->fStarted = true;
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | /* Advance to next table element. */
|
---|
165 | pTable++;
|
---|
166 | }
|
---|
167 |
|
---|
168 | return VINF_SUCCESS;
|
---|
169 | }
|
---|
170 |
|
---|
171 | static void vboxStopServices (VBOXSERVICEENV *pEnv, VBOXSERVICEINFO *pTable)
|
---|
172 | {
|
---|
173 | if (!pEnv->hStopEvent)
|
---|
174 | {
|
---|
175 | return;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /* Signal to all threads. */
|
---|
179 | SetEvent(pEnv->hStopEvent);
|
---|
180 |
|
---|
181 | while (pTable->pszName)
|
---|
182 | {
|
---|
183 | if (pTable->fStarted)
|
---|
184 | {
|
---|
185 | if (pTable->pfnThread)
|
---|
186 | {
|
---|
187 | /* There is a thread, wait for termination. */
|
---|
188 | WaitForSingleObject(pTable->hThread, INFINITE);
|
---|
189 |
|
---|
190 | CloseHandle (pTable->hThread);
|
---|
191 | pTable->hThread = 0;
|
---|
192 | }
|
---|
193 |
|
---|
194 | if (pTable->pfnDestroy)
|
---|
195 | {
|
---|
196 | pTable->pfnDestroy (pEnv, pTable->pInstance);
|
---|
197 | }
|
---|
198 |
|
---|
199 | pTable->fStarted = false;
|
---|
200 | }
|
---|
201 |
|
---|
202 | /* Advance to next table element. */
|
---|
203 | pTable++;
|
---|
204 | }
|
---|
205 |
|
---|
206 | CloseHandle (pEnv->hStopEvent);
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | void WINAPI VBoxServiceStart(void)
|
---|
211 | {
|
---|
212 | dprintf(("VBoxService: Start\n"));
|
---|
213 |
|
---|
214 | VBOXSERVICEENV svcEnv;
|
---|
215 |
|
---|
216 | DWORD status = NO_ERROR;
|
---|
217 |
|
---|
218 | /* open VBox guest driver */
|
---|
219 | gVBoxDriver = CreateFile(VBOXGUEST_DEVICE_NAME,
|
---|
220 | GENERIC_READ | GENERIC_WRITE,
|
---|
221 | FILE_SHARE_READ | FILE_SHARE_WRITE,
|
---|
222 | NULL,
|
---|
223 | OPEN_EXISTING,
|
---|
224 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
|
---|
225 | NULL);
|
---|
226 | if (gVBoxDriver == INVALID_HANDLE_VALUE)
|
---|
227 | {
|
---|
228 | dprintf(("VBoxService: could not open VBox Guest Additions driver! rc = %d\n", GetLastError()));
|
---|
229 | status = ERROR_GEN_FAILURE;
|
---|
230 | }
|
---|
231 |
|
---|
232 | dprintf(("VBoxService: Driver h %p, st %p\n", gVBoxDriver, status));
|
---|
233 |
|
---|
234 | if (status == NO_ERROR)
|
---|
235 | {
|
---|
236 | /* create a custom window class */
|
---|
237 | WNDCLASS windowClass = {0};
|
---|
238 | windowClass.style = CS_NOCLOSE;
|
---|
239 | windowClass.lpfnWndProc = (WNDPROC)VBoxToolWndProc;
|
---|
240 | windowClass.hInstance = gInstance;
|
---|
241 | windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
|
---|
242 | windowClass.lpszClassName = "VirtualBoxTool";
|
---|
243 | if (!RegisterClass(&windowClass))
|
---|
244 | status = GetLastError();
|
---|
245 | }
|
---|
246 |
|
---|
247 | dprintf(("VBoxService: Class st %p\n", status));
|
---|
248 |
|
---|
249 | if (status == NO_ERROR)
|
---|
250 | {
|
---|
251 | /* create our window */
|
---|
252 | gToolWindow = CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
|
---|
253 | "VirtualBoxTool", "VirtualBoxTool",
|
---|
254 | WS_POPUPWINDOW,
|
---|
255 | -200, -200, 100, 100, NULL, NULL, gInstance, NULL);
|
---|
256 | if (!gToolWindow)
|
---|
257 | status = GetLastError();
|
---|
258 | else
|
---|
259 | {
|
---|
260 | /* move the window beneach the mouse pointer so that we get access to it */
|
---|
261 | POINT mousePos;
|
---|
262 | GetCursorPos(&mousePos);
|
---|
263 | SetWindowPos(gToolWindow, HWND_TOPMOST, mousePos.x - 10, mousePos.y - 10, 0, 0,
|
---|
264 | SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_NOSIZE);
|
---|
265 | /* change the mouse pointer so that we can go for a hardware shape */
|
---|
266 | SetCursor(LoadCursor(NULL, IDC_APPSTARTING));
|
---|
267 | SetCursor(LoadCursor(NULL, IDC_ARROW));
|
---|
268 | /* move back our tool window */
|
---|
269 | SetWindowPos(gToolWindow, HWND_TOPMOST, -200, -200, 0, 0,
|
---|
270 | SWP_NOACTIVATE | SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_NOSIZE);
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | dprintf(("VBoxService: Window h %p, st %p\n", gToolWindow, status));
|
---|
275 |
|
---|
276 | if (status == NO_ERROR)
|
---|
277 | {
|
---|
278 | gStopSem = CreateEvent(NULL, TRUE, FALSE, NULL);
|
---|
279 | if (gStopSem == NULL)
|
---|
280 | {
|
---|
281 | dprintf(("VBoxService: CreateEvent failed: rc = %d\n", GetLastError()));
|
---|
282 | return;
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 | /*
|
---|
287 | * Start services listed in the vboxServiceTable.
|
---|
288 | */
|
---|
289 | svcEnv.hInstance = gInstance;
|
---|
290 | svcEnv.hDriver = gVBoxDriver;
|
---|
291 |
|
---|
292 | if (status == NO_ERROR)
|
---|
293 | {
|
---|
294 | int rc = vboxStartServices (&svcEnv, vboxServiceTable);
|
---|
295 |
|
---|
296 | if (VBOX_FAILURE (rc))
|
---|
297 | {
|
---|
298 | status = ERROR_GEN_FAILURE;
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 | /* create display change thread */
|
---|
303 | HANDLE hDisplayChangeThread;
|
---|
304 | if (status == NO_ERROR)
|
---|
305 | {
|
---|
306 | hDisplayChangeThread = (HANDLE)_beginthread(DisplayChangeThread, 0, NULL);
|
---|
307 | if ((int)hDisplayChangeThread == -1L)
|
---|
308 | status = ERROR_GEN_FAILURE;
|
---|
309 | }
|
---|
310 |
|
---|
311 | dprintf(("VBoxService: hDisplayChangeThread h %p, st %p\n", hDisplayChangeThread, status));
|
---|
312 |
|
---|
313 | /* terminate service if something went wrong */
|
---|
314 | if (status != NO_ERROR)
|
---|
315 | {
|
---|
316 | vboxStopServices (&svcEnv, vboxServiceTable);
|
---|
317 | return;
|
---|
318 | }
|
---|
319 |
|
---|
320 | BOOL fTrayIconCreated = false;
|
---|
321 |
|
---|
322 | /* prepare the system tray icon */
|
---|
323 | NOTIFYICONDATA ndata;
|
---|
324 | memset (&ndata, 0, sizeof (ndata));
|
---|
325 | ndata.cbSize = NOTIFYICONDATA_V1_SIZE; // sizeof(NOTIFYICONDATA);
|
---|
326 | ndata.hWnd = gToolWindow;
|
---|
327 | ndata.uID = 2000;
|
---|
328 | ndata.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
|
---|
329 | ndata.uCallbackMessage = WM_USER;
|
---|
330 | ndata.hIcon = LoadIcon(gInstance, MAKEINTRESOURCE(IDI_VIRTUALBOX));
|
---|
331 | sprintf(ndata.szTip, "innotek VirtualBox Guest Additions %d.%d.%d", VBOX_VERSION_MAJOR, VBOX_VERSION_MINOR, VBOX_VERSION_BUILD);
|
---|
332 |
|
---|
333 | dprintf(("VBoxService: ndata.hWnd %08X, ndata.hIcon = %p\n", ndata.hWnd, ndata.hIcon));
|
---|
334 |
|
---|
335 | /*
|
---|
336 | * Main execution loop
|
---|
337 | * Wait for the stop semaphore to be posted or a window event to arrive
|
---|
338 | */
|
---|
339 | while(true)
|
---|
340 | {
|
---|
341 | DWORD waitResult = MsgWaitForMultipleObjectsEx(1, &gStopSem, 500, QS_ALLINPUT, 0);
|
---|
342 | if (waitResult == WAIT_OBJECT_0)
|
---|
343 | {
|
---|
344 | dprintf(("VBoxService: exit\n"));
|
---|
345 | /* exit */
|
---|
346 | break;
|
---|
347 | }
|
---|
348 | else if (waitResult == WAIT_OBJECT_0 + 1)
|
---|
349 | {
|
---|
350 | /* a window message, handle it */
|
---|
351 | MSG msg;
|
---|
352 | while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
|
---|
353 | {
|
---|
354 | dprintf(("VBoxService: msg %p\n", msg.message));
|
---|
355 | if (msg.message == WM_QUIT)
|
---|
356 | {
|
---|
357 | dprintf(("VBoxService: WM_QUIT!\n"));
|
---|
358 | SetEvent(gStopSem);
|
---|
359 | continue;
|
---|
360 | }
|
---|
361 | TranslateMessage(&msg);
|
---|
362 | DispatchMessage(&msg);
|
---|
363 | }
|
---|
364 | }
|
---|
365 | else /* timeout */
|
---|
366 | {
|
---|
367 | #ifndef DEBUG_sandervl
|
---|
368 | dprintf(("VBoxService: timed out\n"));
|
---|
369 | #endif
|
---|
370 | /* we might have to repeat this operation because the shell might not be loaded yet */
|
---|
371 | if (!fTrayIconCreated)
|
---|
372 | {
|
---|
373 | fTrayIconCreated = Shell_NotifyIcon(NIM_ADD, &ndata);
|
---|
374 | dprintf(("VBoxService: fTrayIconCreated = %d, err %08X\n", fTrayIconCreated, GetLastError ()));
|
---|
375 | }
|
---|
376 | }
|
---|
377 | }
|
---|
378 |
|
---|
379 | dprintf(("VBoxService: returned from main loop, exiting...\n"));
|
---|
380 |
|
---|
381 | /* remove the system tray icon */
|
---|
382 | Shell_NotifyIcon(NIM_DELETE, &ndata);
|
---|
383 |
|
---|
384 | dprintf(("VBoxService: waiting for display change thread...\n"));
|
---|
385 |
|
---|
386 | /* wait for the display change thread to terminate */
|
---|
387 | WaitForSingleObject(hDisplayChangeThread, INFINITE);
|
---|
388 |
|
---|
389 | vboxStopServices (&svcEnv, vboxServiceTable);
|
---|
390 |
|
---|
391 | dprintf(("VBoxService: destroying tool window...\n"));
|
---|
392 |
|
---|
393 | /* destroy the tool window */
|
---|
394 | DestroyWindow(gToolWindow);
|
---|
395 |
|
---|
396 | UnregisterClass("VirtualBoxTool", gInstance);
|
---|
397 |
|
---|
398 | CloseHandle(gVBoxDriver);
|
---|
399 | CloseHandle(gStopSem);
|
---|
400 |
|
---|
401 | dprintf(("VBoxService: leaving service main function\n"));
|
---|
402 |
|
---|
403 | return;
|
---|
404 | }
|
---|
405 |
|
---|
406 |
|
---|
407 | /**
|
---|
408 | * Main function
|
---|
409 | */
|
---|
410 | int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
|
---|
411 | {
|
---|
412 | dprintf(("VBoxService: WinMain\n"));
|
---|
413 | gInstance = hInstance;
|
---|
414 | VBoxServiceStart ();
|
---|
415 | return 0;
|
---|
416 | }
|
---|
417 |
|
---|
418 | static bool isVBoxDisplayDriverActive (void)
|
---|
419 | {
|
---|
420 | bool result = false;
|
---|
421 |
|
---|
422 | DISPLAY_DEVICE dispDevice;
|
---|
423 |
|
---|
424 | FillMemory(&dispDevice, sizeof(DISPLAY_DEVICE), 0);
|
---|
425 |
|
---|
426 | dispDevice.cb = sizeof(DISPLAY_DEVICE);
|
---|
427 |
|
---|
428 | INT devNum = 0;
|
---|
429 |
|
---|
430 | while (EnumDisplayDevices(NULL,
|
---|
431 | devNum,
|
---|
432 | &dispDevice,
|
---|
433 | 0))
|
---|
434 | {
|
---|
435 | DDCLOG(("DevNum:%d\nName:%s\nString:%s\nID:%s\nKey:%s\nFlags=%08X\n\n",
|
---|
436 | devNum,
|
---|
437 | &dispDevice.DeviceName[0],
|
---|
438 | &dispDevice.DeviceString[0],
|
---|
439 | &dispDevice.DeviceID[0],
|
---|
440 | &dispDevice.DeviceKey[0],
|
---|
441 | dispDevice.StateFlags));
|
---|
442 |
|
---|
443 | if (dispDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
|
---|
444 | {
|
---|
445 | DDCLOG(("Primary device.\n"));
|
---|
446 |
|
---|
447 | if (strcmp(&dispDevice.DeviceString[0], "VirtualBox Graphics Adapter") == 0)
|
---|
448 | {
|
---|
449 | DDCLOG(("VBox display driver is active.\n"));
|
---|
450 | result = true;
|
---|
451 | }
|
---|
452 |
|
---|
453 | break;
|
---|
454 | }
|
---|
455 |
|
---|
456 | FillMemory(&dispDevice, sizeof(DISPLAY_DEVICE), 0);
|
---|
457 |
|
---|
458 | dispDevice.cb = sizeof(DISPLAY_DEVICE);
|
---|
459 |
|
---|
460 | devNum++;
|
---|
461 | }
|
---|
462 |
|
---|
463 | return result;
|
---|
464 | }
|
---|
465 |
|
---|
466 | /* ChangeDisplaySettingsEx does not exist in NT. ResizeDisplayDevice uses the function. */
|
---|
467 | typedef LONG WINAPI defChangeDisplaySettingsEx(LPCTSTR lpszDeviceName, LPDEVMODE lpDevMode, HWND hwnd, DWORD dwflags, LPVOID lParam);
|
---|
468 | static defChangeDisplaySettingsEx *pChangeDisplaySettingsEx = NULL;
|
---|
469 |
|
---|
470 | /* Returns TRUE to try again. */
|
---|
471 | static BOOL ResizeDisplayDevice(ULONG Id, DWORD Width, DWORD Height, DWORD BitsPerPixel)
|
---|
472 | {
|
---|
473 | BOOL fModeReset = (Width == 0 && Height == 0 && BitsPerPixel == 0);
|
---|
474 |
|
---|
475 | DISPLAY_DEVICE DisplayDevice;
|
---|
476 |
|
---|
477 | ZeroMemory(&DisplayDevice, sizeof(DisplayDevice));
|
---|
478 | DisplayDevice.cb = sizeof(DisplayDevice);
|
---|
479 |
|
---|
480 | /* Find out how many display devices the system has */
|
---|
481 | DWORD NumDevices = 0;
|
---|
482 | DWORD i = 0;
|
---|
483 | while (EnumDisplayDevices (NULL, i, &DisplayDevice, 0))
|
---|
484 | {
|
---|
485 | DDCLOG(("[%d] %s\n", i, DisplayDevice.DeviceName));
|
---|
486 |
|
---|
487 | if (DisplayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
|
---|
488 | {
|
---|
489 | DDCLOG(("Found primary device. err %d\n", GetLastError ()));
|
---|
490 | NumDevices++;
|
---|
491 | }
|
---|
492 | else if (!(DisplayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER))
|
---|
493 | {
|
---|
494 |
|
---|
495 | DDCLOG(("Found secondary device. err %d\n", GetLastError ()));
|
---|
496 | NumDevices++;
|
---|
497 | }
|
---|
498 |
|
---|
499 | ZeroMemory(&DisplayDevice, sizeof(DisplayDevice));
|
---|
500 | DisplayDevice.cb = sizeof(DisplayDevice);
|
---|
501 | i++;
|
---|
502 | }
|
---|
503 |
|
---|
504 | DDCLOG(("Found total %d devices. err %d\n", NumDevices, GetLastError ()));
|
---|
505 |
|
---|
506 | if (NumDevices == 0 || Id >= NumDevices)
|
---|
507 | {
|
---|
508 | DDCLOG(("Requested identifier %d is invalid. err %d\n", Id, GetLastError ()));
|
---|
509 | return FALSE;
|
---|
510 | }
|
---|
511 |
|
---|
512 | DISPLAY_DEVICE *paDisplayDevices = (DISPLAY_DEVICE *)alloca (sizeof (DISPLAY_DEVICE) * NumDevices);
|
---|
513 | DEVMODE *paDeviceModes = (DEVMODE *)alloca (sizeof (DEVMODE) * NumDevices);
|
---|
514 | RECTL *paRects = (RECTL *)alloca (sizeof (RECTL) * NumDevices);
|
---|
515 |
|
---|
516 | /* Fetch information about current devices and modes. */
|
---|
517 | DWORD DevNum = 0;
|
---|
518 | DWORD DevPrimaryNum = 0;
|
---|
519 |
|
---|
520 | ZeroMemory(&DisplayDevice, sizeof(DISPLAY_DEVICE));
|
---|
521 | DisplayDevice.cb = sizeof(DISPLAY_DEVICE);
|
---|
522 |
|
---|
523 | i = 0;
|
---|
524 | while (EnumDisplayDevices (NULL, i, &DisplayDevice, 0))
|
---|
525 | {
|
---|
526 | DDCLOG(("[%d(%d)] %s\n", i, DevNum, DisplayDevice.DeviceName));
|
---|
527 |
|
---|
528 | BOOL bFetchDevice = FALSE;
|
---|
529 |
|
---|
530 | if (DisplayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
|
---|
531 | {
|
---|
532 | DDCLOG(("Found primary device. err %d\n", GetLastError ()));
|
---|
533 | DevPrimaryNum = DevNum;
|
---|
534 | bFetchDevice = TRUE;
|
---|
535 | }
|
---|
536 | else if (!(DisplayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER))
|
---|
537 | {
|
---|
538 |
|
---|
539 | DDCLOG(("Found secondary device. err %d\n", GetLastError ()));
|
---|
540 | bFetchDevice = TRUE;
|
---|
541 | }
|
---|
542 |
|
---|
543 | if (bFetchDevice)
|
---|
544 | {
|
---|
545 | if (DevNum >= NumDevices)
|
---|
546 | {
|
---|
547 | DDCLOG(("%d >= %d\n", NumDevices, DevNum));
|
---|
548 | return FALSE;
|
---|
549 | }
|
---|
550 |
|
---|
551 | paDisplayDevices[DevNum] = DisplayDevice;
|
---|
552 |
|
---|
553 | ZeroMemory(&paDeviceModes[DevNum], sizeof(DEVMODE));
|
---|
554 | paDeviceModes[DevNum].dmSize = sizeof(DEVMODE);
|
---|
555 | if (!EnumDisplaySettings((LPSTR)DisplayDevice.DeviceName,
|
---|
556 | ENUM_REGISTRY_SETTINGS, &paDeviceModes[DevNum]))
|
---|
557 | {
|
---|
558 | DDCLOG(("EnumDisplaySettings err %d\n", GetLastError ()));
|
---|
559 | return FALSE;
|
---|
560 | }
|
---|
561 |
|
---|
562 | DDCLOG(("%dx%d at %d,%d\n",
|
---|
563 | paDeviceModes[DevNum].dmPelsWidth,
|
---|
564 | paDeviceModes[DevNum].dmPelsHeight,
|
---|
565 | paDeviceModes[DevNum].dmPosition.x,
|
---|
566 | paDeviceModes[DevNum].dmPosition.y));
|
---|
567 |
|
---|
568 | paRects[DevNum].left = paDeviceModes[DevNum].dmPosition.x;
|
---|
569 | paRects[DevNum].top = paDeviceModes[DevNum].dmPosition.y;
|
---|
570 | paRects[DevNum].right = paDeviceModes[DevNum].dmPosition.x + paDeviceModes[DevNum].dmPelsWidth;
|
---|
571 | paRects[DevNum].bottom = paDeviceModes[DevNum].dmPosition.y + paDeviceModes[DevNum].dmPelsHeight;
|
---|
572 | DevNum++;
|
---|
573 | }
|
---|
574 |
|
---|
575 | ZeroMemory(&DisplayDevice, sizeof(DISPLAY_DEVICE));
|
---|
576 | DisplayDevice.cb = sizeof(DISPLAY_DEVICE);
|
---|
577 | i++;
|
---|
578 | }
|
---|
579 |
|
---|
580 | if (Width == 0)
|
---|
581 | {
|
---|
582 | Width = paRects[Id].right - paRects[Id].left;
|
---|
583 | }
|
---|
584 |
|
---|
585 | if (Height == 0)
|
---|
586 | {
|
---|
587 | Height = paRects[Id].bottom - paRects[Id].top;
|
---|
588 | }
|
---|
589 |
|
---|
590 | /* Check whether a mode reset or a change is requested. */
|
---|
591 | if ( !fModeReset
|
---|
592 | && paRects[Id].right - paRects[Id].left == Width
|
---|
593 | && paRects[Id].bottom - paRects[Id].top == Height
|
---|
594 | && paDeviceModes[Id].dmBitsPerPel == BitsPerPixel)
|
---|
595 | {
|
---|
596 | DDCLOG(("VBoxService: already at desired resolution.\n"));
|
---|
597 | return FALSE;
|
---|
598 | }
|
---|
599 |
|
---|
600 | resizeRect(paRects, NumDevices, DevPrimaryNum, Id, Width, Height);
|
---|
601 | #ifdef DDCLOG
|
---|
602 | for (i = 0; i < NumDevices; i++)
|
---|
603 | {
|
---|
604 | DDCLOG(("[%d]: %d,%d %dx%d\n",
|
---|
605 | i, paRects[i].left, paRects[i].top,
|
---|
606 | paRects[i].right - paRects[i].left,
|
---|
607 | paRects[i].bottom - paRects[i].top));
|
---|
608 | }
|
---|
609 | #endif /* DDCLOG */
|
---|
610 |
|
---|
611 | /* Without this, Windows will not ask the miniport for its
|
---|
612 | * mode table but uses an internal cache instead.
|
---|
613 | */
|
---|
614 | DEVMODE tempDevMode;
|
---|
615 | ZeroMemory (&tempDevMode, sizeof (tempDevMode));
|
---|
616 | tempDevMode.dmSize = sizeof(DEVMODE);
|
---|
617 | EnumDisplaySettings(NULL, 0xffffff, &tempDevMode);
|
---|
618 |
|
---|
619 | /* Assign the new rectangles to displays. */
|
---|
620 | for (i = 0; i < NumDevices; i++)
|
---|
621 | {
|
---|
622 | paDeviceModes[i].dmPosition.x = paRects[i].left;
|
---|
623 | paDeviceModes[i].dmPosition.y = paRects[i].top;
|
---|
624 | paDeviceModes[i].dmPelsWidth = paRects[i].right - paRects[i].left;
|
---|
625 | paDeviceModes[i].dmPelsHeight = paRects[i].bottom - paRects[i].top;
|
---|
626 |
|
---|
627 | paDeviceModes[i].dmFields = DM_POSITION | DM_PELSHEIGHT | DM_PELSWIDTH;
|
---|
628 |
|
---|
629 | if ( i == Id
|
---|
630 | && BitsPerPixel != 0)
|
---|
631 | {
|
---|
632 | paDeviceModes[i].dmFields |= DM_BITSPERPEL;
|
---|
633 | paDeviceModes[i].dmBitsPerPel = BitsPerPixel;
|
---|
634 | }
|
---|
635 |
|
---|
636 | pChangeDisplaySettingsEx((LPSTR)paDisplayDevices[i].DeviceName,
|
---|
637 | &paDeviceModes[i], NULL, CDS_NORESET | CDS_UPDATEREGISTRY, NULL);
|
---|
638 | DDCLOG(("ChangeDisplaySettings position err %d\n", GetLastError ()));
|
---|
639 | }
|
---|
640 |
|
---|
641 | /* A second call to ChangeDisplaySettings updates the monitor. */
|
---|
642 | LONG status = ChangeDisplaySettings(NULL, 0);
|
---|
643 | DDCLOG(("ChangeDisplaySettings update status %d\n", status));
|
---|
644 | if (status == DISP_CHANGE_SUCCESSFUL || status == DISP_CHANGE_BADMODE)
|
---|
645 | {
|
---|
646 | /* Successfully set new video mode or our driver can not set the requested mode. Stop trying. */
|
---|
647 | return FALSE;
|
---|
648 | }
|
---|
649 |
|
---|
650 | /* Retry the request. */
|
---|
651 | return TRUE;
|
---|
652 | }
|
---|
653 |
|
---|
654 | /**
|
---|
655 | * Thread function to wait for and process display change
|
---|
656 | * requests
|
---|
657 | */
|
---|
658 | VOID DisplayChangeThread(void *dummy)
|
---|
659 | {
|
---|
660 | bool fTerminate = false;
|
---|
661 | VBoxGuestFilterMaskInfo maskInfo;
|
---|
662 | DWORD cbReturned;
|
---|
663 |
|
---|
664 | HMODULE hUser = GetModuleHandle("USER32");
|
---|
665 |
|
---|
666 | if (hUser)
|
---|
667 | {
|
---|
668 | pChangeDisplaySettingsEx = (defChangeDisplaySettingsEx *)GetProcAddress(hUser, "ChangeDisplaySettingsExA");
|
---|
669 | DDCLOG(("VBoxService: pChangeDisplaySettingsEx = %p\n", pChangeDisplaySettingsEx));
|
---|
670 | }
|
---|
671 |
|
---|
672 | maskInfo.u32OrMask = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST;
|
---|
673 | maskInfo.u32NotMask = 0;
|
---|
674 | if (DeviceIoControl (gVBoxDriver, IOCTL_VBOXGUEST_CTL_FILTER_MASK, &maskInfo, sizeof (maskInfo), NULL, 0, &cbReturned, NULL))
|
---|
675 | {
|
---|
676 | DDCLOG(("VBoxService: DeviceIOControl(CtlMask - or) succeeded\n"));
|
---|
677 | }
|
---|
678 | else
|
---|
679 | {
|
---|
680 | dprintf(("VBoxService: DeviceIOControl(CtlMask) failed, DisplayChangeThread exited\n"));
|
---|
681 | return;
|
---|
682 | }
|
---|
683 |
|
---|
684 | do
|
---|
685 | {
|
---|
686 | /* wait for a display change event */
|
---|
687 | VBoxGuestWaitEventInfo waitEvent;
|
---|
688 | waitEvent.u32TimeoutIn = 100;
|
---|
689 | waitEvent.u32EventMaskIn = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST;
|
---|
690 | if (DeviceIoControl(gVBoxDriver, IOCTL_VBOXGUEST_WAITEVENT, &waitEvent, sizeof(waitEvent), &waitEvent, sizeof(waitEvent), &cbReturned, NULL))
|
---|
691 | {
|
---|
692 | DDCLOG(("VBoxService: DeviceIOControl succeded\n"));
|
---|
693 |
|
---|
694 | /* are we supposed to stop? */
|
---|
695 | if (WaitForSingleObject(gStopSem, 0) == WAIT_OBJECT_0)
|
---|
696 | break;
|
---|
697 |
|
---|
698 | DDCLOG(("VBoxService: checking event\n"));
|
---|
699 |
|
---|
700 | /* did we get the right event? */
|
---|
701 | if (waitEvent.u32EventFlagsOut & VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST)
|
---|
702 | {
|
---|
703 | DDCLOG(("VBoxService: going to get display change information.\n"));
|
---|
704 |
|
---|
705 | /* We got at least one event. Read the requested resolution
|
---|
706 | * and try to set it until success. New events will not be seen
|
---|
707 | * but a new resolution will be read in this poll loop.
|
---|
708 | */
|
---|
709 | for (;;)
|
---|
710 | {
|
---|
711 | /* get the display change request */
|
---|
712 | VMMDevDisplayChangeRequest2 displayChangeRequest = {0};
|
---|
713 | displayChangeRequest.header.size = sizeof(VMMDevDisplayChangeRequest2);
|
---|
714 | displayChangeRequest.header.version = VMMDEV_REQUEST_HEADER_VERSION;
|
---|
715 | displayChangeRequest.header.requestType = VMMDevReq_GetDisplayChangeRequest2;
|
---|
716 | displayChangeRequest.eventAck = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST;
|
---|
717 | BOOL fDisplayChangeQueried = DeviceIoControl(gVBoxDriver, IOCTL_VBOXGUEST_VMMREQUEST, &displayChangeRequest, sizeof(VMMDevDisplayChangeRequest2),
|
---|
718 | &displayChangeRequest, sizeof(VMMDevDisplayChangeRequest2), &cbReturned, NULL);
|
---|
719 | if (!fDisplayChangeQueried)
|
---|
720 | {
|
---|
721 | /* Try the old version of the request for old VBox hosts. */
|
---|
722 | displayChangeRequest.header.size = sizeof(VMMDevDisplayChangeRequest);
|
---|
723 | displayChangeRequest.header.version = VMMDEV_REQUEST_HEADER_VERSION;
|
---|
724 | displayChangeRequest.header.requestType = VMMDevReq_GetDisplayChangeRequest;
|
---|
725 | displayChangeRequest.eventAck = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST;
|
---|
726 | fDisplayChangeQueried = DeviceIoControl(gVBoxDriver, IOCTL_VBOXGUEST_VMMREQUEST, &displayChangeRequest, sizeof(VMMDevDisplayChangeRequest),
|
---|
727 | &displayChangeRequest, sizeof(VMMDevDisplayChangeRequest), &cbReturned, NULL);
|
---|
728 | displayChangeRequest.display = 0;
|
---|
729 | }
|
---|
730 |
|
---|
731 | if (fDisplayChangeQueried)
|
---|
732 | {
|
---|
733 | DDCLOG(("VBoxService: VMMDevReq_GetDisplayChangeRequest2: %dx%dx%d at %d\n", displayChangeRequest.xres, displayChangeRequest.yres, displayChangeRequest.bpp, displayChangeRequest.display));
|
---|
734 |
|
---|
735 | /* Horizontal resolution must be a multiple of 8, round down. */
|
---|
736 | displayChangeRequest.xres &= 0xfff8;
|
---|
737 |
|
---|
738 | /*
|
---|
739 | * Only try to change video mode if the active display driver is VBox additions.
|
---|
740 | */
|
---|
741 | if (isVBoxDisplayDriverActive ())
|
---|
742 | {
|
---|
743 | if (pChangeDisplaySettingsEx != 0)
|
---|
744 | {
|
---|
745 | /* W2K or later. */
|
---|
746 | if (!ResizeDisplayDevice(displayChangeRequest.display,
|
---|
747 | displayChangeRequest.xres,
|
---|
748 | displayChangeRequest.yres,
|
---|
749 | displayChangeRequest.bpp))
|
---|
750 | {
|
---|
751 | break;
|
---|
752 | }
|
---|
753 | }
|
---|
754 | else
|
---|
755 | {
|
---|
756 | /* Single monitor NT. */
|
---|
757 | DEVMODE devMode;
|
---|
758 | memset (&devMode, 0, sizeof (devMode));
|
---|
759 | devMode.dmSize = sizeof(DEVMODE);
|
---|
760 |
|
---|
761 | /* get the current screen setup */
|
---|
762 | if (EnumDisplaySettings(NULL, ENUM_REGISTRY_SETTINGS, &devMode))
|
---|
763 | {
|
---|
764 | dprintf(("VBoxService: Current mode: %dx%dx%d at %d,%d\n", devMode.dmPelsWidth, devMode.dmPelsHeight, devMode.dmBitsPerPel, devMode.dmPosition.x, devMode.dmPosition.y));
|
---|
765 |
|
---|
766 | /* Check whether a mode reset or a change is requested. */
|
---|
767 | if (displayChangeRequest.xres || displayChangeRequest.yres || displayChangeRequest.bpp)
|
---|
768 | {
|
---|
769 | /* A change is requested.
|
---|
770 | * Set values which are not to be changed to the current values.
|
---|
771 | */
|
---|
772 | if (!displayChangeRequest.xres)
|
---|
773 | displayChangeRequest.xres = devMode.dmPelsWidth;
|
---|
774 | if (!displayChangeRequest.yres)
|
---|
775 | displayChangeRequest.yres = devMode.dmPelsHeight;
|
---|
776 | if (!displayChangeRequest.bpp)
|
---|
777 | displayChangeRequest.bpp = devMode.dmBitsPerPel;
|
---|
778 | }
|
---|
779 | else
|
---|
780 | {
|
---|
781 | /* All zero values means a forced mode reset. Do nothing. */
|
---|
782 | }
|
---|
783 |
|
---|
784 | /* Verify that the mode is indeed changed. */
|
---|
785 | if ( devMode.dmPelsWidth == displayChangeRequest.xres
|
---|
786 | && devMode.dmPelsHeight == displayChangeRequest.yres
|
---|
787 | && devMode.dmBitsPerPel == displayChangeRequest.bpp)
|
---|
788 | {
|
---|
789 | dprintf(("VBoxService: already at desired resolution.\n"));
|
---|
790 | break;
|
---|
791 | }
|
---|
792 |
|
---|
793 | // without this, Windows will not ask the miniport for its
|
---|
794 | // mode table but uses an internal cache instead
|
---|
795 | DEVMODE tempDevMode = {0};
|
---|
796 | tempDevMode.dmSize = sizeof(DEVMODE);
|
---|
797 | EnumDisplaySettings(NULL, 0xffffff, &tempDevMode);
|
---|
798 |
|
---|
799 | /* adjust the values that are supposed to change */
|
---|
800 | if (displayChangeRequest.xres)
|
---|
801 | devMode.dmPelsWidth = displayChangeRequest.xres;
|
---|
802 | if (displayChangeRequest.yres)
|
---|
803 | devMode.dmPelsHeight = displayChangeRequest.yres;
|
---|
804 | if (displayChangeRequest.bpp)
|
---|
805 | devMode.dmBitsPerPel = displayChangeRequest.bpp;
|
---|
806 |
|
---|
807 | DDCLOG(("VBoxService: setting the new mode %dx%dx%d\n", devMode.dmPelsWidth, devMode.dmPelsHeight, devMode.dmBitsPerPel));
|
---|
808 |
|
---|
809 | /* set the new mode */
|
---|
810 | LONG status = ChangeDisplaySettings(&devMode, CDS_UPDATEREGISTRY);
|
---|
811 | if (status != DISP_CHANGE_SUCCESSFUL)
|
---|
812 | {
|
---|
813 | dprintf(("VBoxService: error from ChangeDisplaySettings: %d\n", status));
|
---|
814 |
|
---|
815 | if (status == DISP_CHANGE_BADMODE)
|
---|
816 | {
|
---|
817 | /* Our driver can not set the requested mode. Stop trying. */
|
---|
818 | break;
|
---|
819 | }
|
---|
820 | }
|
---|
821 | else
|
---|
822 | {
|
---|
823 | /* Successfully set new video mode. */
|
---|
824 | break;
|
---|
825 | }
|
---|
826 | }
|
---|
827 | else
|
---|
828 | {
|
---|
829 | dprintf(("VBoxService: error from EnumDisplaySettings: %d\n", GetLastError ()));
|
---|
830 | break;
|
---|
831 | }
|
---|
832 | }
|
---|
833 | }
|
---|
834 | else
|
---|
835 | {
|
---|
836 | dprintf(("VBoxService: vboxDisplayDriver is not active.\n"));
|
---|
837 | }
|
---|
838 |
|
---|
839 | /* Retry the change a bit later. */
|
---|
840 | /* are we supposed to stop? */
|
---|
841 | if (WaitForSingleObject(gStopSem, 1000) == WAIT_OBJECT_0)
|
---|
842 | {
|
---|
843 | fTerminate = true;
|
---|
844 | break;
|
---|
845 | }
|
---|
846 | }
|
---|
847 | else
|
---|
848 | {
|
---|
849 | dprintf(("VBoxService: error from DeviceIoControl IOCTL_VBOXGUEST_VMMREQUEST\n"));
|
---|
850 | /* sleep a bit to not eat too much CPU while retrying */
|
---|
851 | /* are we supposed to stop? */
|
---|
852 | if (WaitForSingleObject(gStopSem, 50) == WAIT_OBJECT_0)
|
---|
853 | {
|
---|
854 | fTerminate = true;
|
---|
855 | break;
|
---|
856 | }
|
---|
857 | }
|
---|
858 | }
|
---|
859 | }
|
---|
860 | } else
|
---|
861 | {
|
---|
862 | dprintf(("VBoxService: error 0 from DeviceIoControl IOCTL_VBOXGUEST_WAITEVENT\n"));
|
---|
863 | /* sleep a bit to not eat too much CPU in case the above call always fails */
|
---|
864 | if (WaitForSingleObject(gStopSem, 10) == WAIT_OBJECT_0)
|
---|
865 | {
|
---|
866 | fTerminate = true;
|
---|
867 | break;
|
---|
868 | }
|
---|
869 | }
|
---|
870 | } while (!fTerminate);
|
---|
871 |
|
---|
872 | maskInfo.u32OrMask = 0;
|
---|
873 | maskInfo.u32NotMask = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST;
|
---|
874 | if (DeviceIoControl (gVBoxDriver, IOCTL_VBOXGUEST_CTL_FILTER_MASK, &maskInfo, sizeof (maskInfo), NULL, 0, &cbReturned, NULL))
|
---|
875 | {
|
---|
876 | DDCLOG(("VBoxService: DeviceIOControl(CtlMask - not) succeeded\n"));
|
---|
877 | }
|
---|
878 | else
|
---|
879 | {
|
---|
880 | dprintf(("VBoxService: DeviceIOControl(CtlMask) failed\n"));
|
---|
881 | }
|
---|
882 |
|
---|
883 | dprintf(("VBoxService: finished display change request thread\n"));
|
---|
884 | }
|
---|
885 |
|
---|
886 | /**
|
---|
887 | * Window procedure for our tool window
|
---|
888 | */
|
---|
889 | LRESULT CALLBACK VBoxToolWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
|
---|
890 | {
|
---|
891 | switch (msg)
|
---|
892 | {
|
---|
893 | case WM_CLOSE:
|
---|
894 | break;
|
---|
895 |
|
---|
896 | case WM_DESTROY:
|
---|
897 | break;
|
---|
898 |
|
---|
899 | case WM_VBOX_INSTALL_SEAMLESS_HOOK:
|
---|
900 |
|
---|
901 | break;
|
---|
902 |
|
---|
903 | case WM_VBOX_REMOVE_SEAMLESS_HOOK:
|
---|
904 | break;
|
---|
905 |
|
---|
906 | default:
|
---|
907 | return DefWindowProc(hwnd, msg, wParam, lParam);
|
---|
908 | }
|
---|
909 | return 0;
|
---|
910 | }
|
---|
911 |
|
---|