VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxTray.cpp@ 28800

Last change on this file since 28800 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 32.0 KB
Line 
1/** @file
2 * VBoxTray - Guest Additions Tray Application
3 */
4
5/*
6 * Copyright (C) 2006-2009 Oracle Corporation
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 (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 */
16
17#include "VBoxTray.h"
18#include "VBoxSeamless.h"
19#include "VBoxClipboard.h"
20#include "VBoxDisplay.h"
21#include "VBoxRestore.h"
22#include "VBoxVRDP.h"
23#include "VBoxHostVersion.h"
24#include <VBoxHook.h>
25#include "resource.h"
26#include <malloc.h>
27#include <VBoxGuestInternal.h>
28
29#include "helpers.h"
30#include <sddl.h>
31
32#include <iprt/buildconfig.h>
33
34/* global variables */
35HANDLE gVBoxDriver;
36HANDLE gStopSem;
37HANDLE ghSeamlessNotifyEvent = 0;
38SERVICE_STATUS gVBoxServiceStatus;
39SERVICE_STATUS_HANDLE gVBoxServiceStatusHandle;
40HINSTANCE gInstance;
41HWND gToolWindow;
42
43/* Prototypes */
44VOID DisplayChangeThread(void *dummy);
45LRESULT CALLBACK VBoxToolWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
46
47/* The service table. */
48static VBOXSERVICEINFO vboxServiceTable[] =
49{
50 {
51 "Display",
52 VBoxDisplayInit,
53 VBoxDisplayThread,
54 VBoxDisplayDestroy,
55 },
56 {
57 "Shared Clipboard",
58 VBoxClipboardInit,
59 VBoxClipboardThread,
60 VBoxClipboardDestroy
61 },
62 {
63 "Seamless Windows",
64 VBoxSeamlessInit,
65 VBoxSeamlessThread,
66 VBoxSeamlessDestroy
67 },
68#ifdef VBOX_WITH_VRDP_SESSION_HANDLING
69 {
70 "Restore",
71 VBoxRestoreInit,
72 VBoxRestoreThread,
73 VBoxRestoreDestroy,
74 },
75#endif
76 {
77 "VRDP",
78 VBoxVRDPInit,
79 VBoxVRDPThread,
80 VBoxVRDPDestroy,
81 },
82 {
83 NULL
84 }
85};
86
87static int vboxStartServices (VBOXSERVICEENV *pEnv, VBOXSERVICEINFO *pTable)
88{
89 Log(("VBoxTray: Starting services...\n"));
90
91 pEnv->hStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
92
93 if (!pEnv->hStopEvent)
94 {
95 /* Could not create event. */
96 return VERR_NOT_SUPPORTED;
97 }
98
99 while (pTable->pszName)
100 {
101 Log(("Starting %s...\n", pTable->pszName));
102
103 int rc = VINF_SUCCESS;
104
105 bool fStartThread = false;
106
107 pTable->hThread = (HANDLE)0;
108 pTable->pInstance = NULL;
109 pTable->fStarted = false;
110
111 if (pTable->pfnInit)
112 {
113 rc = pTable->pfnInit (pEnv, &pTable->pInstance, &fStartThread);
114 }
115
116 if (RT_FAILURE (rc))
117 {
118 Log(("Failed to initialize rc = %Rrc.\n", rc));
119 }
120 else
121 {
122 if (pTable->pfnThread && fStartThread)
123 {
124 unsigned threadid;
125
126 pTable->hThread = (HANDLE)_beginthreadex (NULL, /* security */
127 0, /* stacksize */
128 pTable->pfnThread,
129 pTable->pInstance,
130 0, /* initflag */
131 &threadid);
132
133 if (pTable->hThread == (HANDLE)(0))
134 {
135 rc = VERR_NOT_SUPPORTED;
136 }
137 }
138
139 if (RT_FAILURE (rc))
140 {
141 Log(("Failed to start the thread.\n"));
142
143 if (pTable->pfnDestroy)
144 {
145 pTable->pfnDestroy (pEnv, pTable->pInstance);
146 }
147 }
148 else
149 {
150 pTable->fStarted = true;
151 }
152 }
153
154 /* Advance to next table element. */
155 pTable++;
156 }
157
158 return VINF_SUCCESS;
159}
160
161static void vboxStopServices (VBOXSERVICEENV *pEnv, VBOXSERVICEINFO *pTable)
162{
163 if (!pEnv->hStopEvent)
164 {
165 return;
166 }
167
168 /* Signal to all threads. */
169 SetEvent(pEnv->hStopEvent);
170
171 while (pTable->pszName)
172 {
173 if (pTable->fStarted)
174 {
175 if (pTable->pfnThread)
176 {
177 /* There is a thread, wait for termination. */
178 WaitForSingleObject(pTable->hThread, INFINITE);
179
180 CloseHandle (pTable->hThread);
181 pTable->hThread = 0;
182 }
183
184 if (pTable->pfnDestroy)
185 {
186 pTable->pfnDestroy (pEnv, pTable->pInstance);
187 }
188
189 pTable->fStarted = false;
190 }
191
192 /* Advance to next table element. */
193 pTable++;
194 }
195
196 CloseHandle (pEnv->hStopEvent);
197}
198
199
200/** Attempt to force Windows to reload the cursor image by attaching to the
201 * thread of the window currently under the mouse, hiding the cursor and
202 * showing it again. This could fail to work in any number of ways (no
203 * window under the cursor, the cursor has moved to a different window while
204 * we are processing), but we just accept this, as the cursor will be reloaded
205 * at some point anyway. */
206void VBoxServiceReloadCursor(void)
207{
208 LogFlowFunc(("\n"));
209 POINT mousePos;
210 HWND hWin;
211 DWORD hThread, hCurrentThread;
212
213 GetCursorPos(&mousePos);
214 hWin = WindowFromPoint(mousePos);
215 if (hWin)
216 {
217 hThread = GetWindowThreadProcessId(hWin, NULL);
218 hCurrentThread = GetCurrentThreadId();
219 if (hCurrentThread != hThread)
220 AttachThreadInput(hCurrentThread, hThread, TRUE);
221 }
222 ShowCursor(false);
223 ShowCursor(true);
224 if (hWin && (hCurrentThread != hThread))
225 AttachThreadInput(hCurrentThread, hThread, FALSE);
226 LogFlowFunc(("exiting\n"));
227}
228
229
230void WINAPI VBoxServiceStart(void)
231{
232 Log(("VBoxTray: Leaving service main function"));
233
234 VBOXSERVICEENV svcEnv;
235
236 DWORD status = NO_ERROR;
237
238 /* open VBox guest driver */
239 gVBoxDriver = CreateFile(VBOXGUEST_DEVICE_NAME,
240 GENERIC_READ | GENERIC_WRITE,
241 FILE_SHARE_READ | FILE_SHARE_WRITE,
242 NULL,
243 OPEN_EXISTING,
244 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
245 NULL);
246 if (gVBoxDriver == INVALID_HANDLE_VALUE)
247 {
248 LogRel(("VBoxTray: Could not open VirtualBox Guest Additions driver! Please install / start it first! rc = %d\n", GetLastError()));
249 status = ERROR_GEN_FAILURE;
250 }
251
252 Log(("VBoxTray: Driver Handle = %p, Status = %p\n", gVBoxDriver, status));
253
254 if (status == NO_ERROR)
255 {
256 /* create a custom window class */
257 WNDCLASS windowClass = {0};
258 windowClass.style = CS_NOCLOSE;
259 windowClass.lpfnWndProc = (WNDPROC)VBoxToolWndProc;
260 windowClass.hInstance = gInstance;
261 windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
262 windowClass.lpszClassName = "VirtualBoxTool";
263 if (!RegisterClass(&windowClass))
264 status = GetLastError();
265 }
266
267 Log(("VBoxTray: Class st %p\n", status));
268
269 if (status == NO_ERROR)
270 {
271 /* create our window */
272 gToolWindow = CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
273 "VirtualBoxTool", "VirtualBoxTool",
274 WS_POPUPWINDOW,
275 -200, -200, 100, 100, NULL, NULL, gInstance, NULL);
276 if (!gToolWindow)
277 status = GetLastError();
278 else
279 VBoxServiceReloadCursor();
280 }
281
282 Log(("VBoxTray: Window Handle = %p, Status = %p\n", gToolWindow, status));
283
284 OSVERSIONINFO info;
285 DWORD dwMajorVersion = 5; /* default XP */
286 info.dwOSVersionInfoSize = sizeof(info);
287 if (GetVersionEx(&info))
288 {
289 Log(("VBoxTray: Windows version major %d minor %d\n", info.dwMajorVersion, info.dwMinorVersion));
290 dwMajorVersion = info.dwMajorVersion;
291 }
292
293 if (status == NO_ERROR)
294 {
295 gStopSem = CreateEvent(NULL, TRUE, FALSE, NULL);
296 if (gStopSem == NULL)
297 {
298 Log(("VBoxTray: CreateEvent for Stopping failed: rc = %d\n", GetLastError()));
299 return;
300 }
301
302 /* We need to setup a security descriptor to allow other processes modify access to the seamless notification event semaphore */
303 SECURITY_ATTRIBUTES SecAttr;
304 char secDesc[SECURITY_DESCRIPTOR_MIN_LENGTH];
305 BOOL ret;
306
307 SecAttr.nLength = sizeof(SecAttr);
308 SecAttr.bInheritHandle = FALSE;
309 SecAttr.lpSecurityDescriptor = &secDesc;
310 InitializeSecurityDescriptor(SecAttr.lpSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
311 ret = SetSecurityDescriptorDacl(SecAttr.lpSecurityDescriptor, TRUE, 0, FALSE);
312 if (!ret)
313 Log(("VBoxTray: SetSecurityDescriptorDacl failed with %d\n", GetLastError()));
314
315 /* For Vista and up we need to change the integrity of the security descriptor too */
316 if (dwMajorVersion >= 6)
317 {
318 HMODULE hModule;
319
320 BOOL (WINAPI * pfnConvertStringSecurityDescriptorToSecurityDescriptorA)(LPCSTR StringSecurityDescriptor, DWORD StringSDRevision, PSECURITY_DESCRIPTOR *SecurityDescriptor, PULONG SecurityDescriptorSize);
321
322 hModule = LoadLibrary("ADVAPI32.DLL");
323 if (hModule)
324 {
325 PSECURITY_DESCRIPTOR pSD;
326 PACL pSacl = NULL;
327 BOOL fSaclPresent = FALSE;
328 BOOL fSaclDefaulted = FALSE;
329
330 *(uintptr_t *)&pfnConvertStringSecurityDescriptorToSecurityDescriptorA = (uintptr_t)GetProcAddress(hModule, "ConvertStringSecurityDescriptorToSecurityDescriptorA");
331
332 Log(("VBoxTray: pfnConvertStringSecurityDescriptorToSecurityDescriptorA = %x\n", pfnConvertStringSecurityDescriptorToSecurityDescriptorA));
333 if (pfnConvertStringSecurityDescriptorToSecurityDescriptorA)
334 {
335 ret = pfnConvertStringSecurityDescriptorToSecurityDescriptorA("S:(ML;;NW;;;LW)", /* this means "low integrity" */
336 SDDL_REVISION_1, &pSD, NULL);
337 if (!ret)
338 Log(("VBoxTray: ConvertStringSecurityDescriptorToSecurityDescriptorA failed with %d\n", GetLastError()));
339
340 ret = GetSecurityDescriptorSacl(pSD, &fSaclPresent, &pSacl, &fSaclDefaulted);
341 if (!ret)
342 Log(("VBoxTray: GetSecurityDescriptorSacl failed with %d\n", GetLastError()));
343
344 ret = SetSecurityDescriptorSacl(SecAttr.lpSecurityDescriptor, TRUE, pSacl, FALSE);
345 if (!ret)
346 Log(("VBoxTray: SetSecurityDescriptorSacl failed with %d\n", GetLastError()));
347 }
348 }
349 }
350
351 if (dwMajorVersion >= 5) /* Only for W2K and up ... */
352 {
353 ghSeamlessNotifyEvent = CreateEvent(&SecAttr, FALSE, FALSE, VBOXHOOK_GLOBAL_EVENT_NAME);
354 if (ghSeamlessNotifyEvent == NULL)
355 {
356 Log(("VBoxTray: CreateEvent for Seamless failed: rc = %d\n", GetLastError()));
357 return;
358 }
359 }
360 }
361
362 /*
363 * Start services listed in the vboxServiceTable.
364 */
365 svcEnv.hInstance = gInstance;
366 svcEnv.hDriver = gVBoxDriver;
367
368 /* initializes disp-if to default (XPDM) mode */
369 status = VBoxDispIfInit(&svcEnv.dispIf);
370#ifdef VBOXWDDM
371 /* for now the display mode will be adjusted to WDDM mode if needed
372 * on display service initialization when it detects the display driver type */
373#endif
374
375 if (status == NO_ERROR)
376 {
377 int rc = vboxStartServices (&svcEnv, vboxServiceTable);
378
379 if (RT_FAILURE (rc))
380 {
381 status = ERROR_GEN_FAILURE;
382 }
383 }
384
385 /* terminate service if something went wrong */
386 if (status != NO_ERROR)
387 {
388 vboxStopServices (&svcEnv, vboxServiceTable);
389 return;
390 }
391
392 BOOL fTrayIconCreated = false;
393
394 /* prepare the system tray icon */
395 NOTIFYICONDATA ndata;
396 RT_ZERO (ndata);
397 ndata.cbSize = NOTIFYICONDATA_V1_SIZE; // sizeof(NOTIFYICONDATA);
398 ndata.hWnd = gToolWindow;
399 ndata.uID = ID_TRAYICON;
400 ndata.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
401 ndata.uCallbackMessage = WM_VBOX_TRAY;
402 ndata.hIcon = LoadIcon(gInstance, MAKEINTRESOURCE(IDI_VIRTUALBOX));
403 sprintf(ndata.szTip, "%s Guest Additions %d.%d.%dr%d", VBOX_PRODUCT, VBOX_VERSION_MAJOR, VBOX_VERSION_MINOR, VBOX_VERSION_BUILD, VBOX_SVN_REV);
404 Log(("VBoxTray: ndata.hWnd %08X, ndata.hIcon = %p\n", ndata.hWnd, ndata.hIcon));
405
406 /* Boost thread priority to make sure we wake up early for seamless window notifications (not sure if it actually makes any difference though) */
407 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
408
409 /*
410 * Main execution loop
411 * Wait for the stop semaphore to be posted or a window event to arrive
412 */
413
414 DWORD dwEventCount = 2;
415 HANDLE hWaitEvent[2] = {gStopSem, ghSeamlessNotifyEvent};
416
417 if (0 == ghSeamlessNotifyEvent) /* If seamless mode is not active / supported, reduce event array count */
418 dwEventCount = 1;
419
420 Log(("VBoxTray: Number of events to wait in main loop: %ld\n", dwEventCount));
421
422 while(true)
423 {
424 DWORD waitResult = MsgWaitForMultipleObjectsEx(dwEventCount, hWaitEvent, 500, QS_ALLINPUT, 0);
425 waitResult = waitResult - WAIT_OBJECT_0;
426
427 Log(("VBoxTray: Wait result = %ld.\n", waitResult));
428
429 if (waitResult == 0)
430 {
431 Log(("VBoxTray: Event 'Exit' triggered.\n"));
432 /* exit */
433 break;
434 }
435 else if ((waitResult == 1) && (ghSeamlessNotifyEvent!=0)) /* Only jump in, if seamless is active! */
436 {
437 Log(("VBoxTray: Event 'Seamless' triggered.\n"));
438
439 /* seamless window notification */
440 VBoxSeamlessCheckWindows();
441 }
442 else
443 {
444 /* timeout or a window message, handle it */
445 MSG msg;
446 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
447 {
448 Log(("VBoxTray: msg %p\n", msg.message));
449 if (msg.message == WM_QUIT)
450 {
451 Log(("VBoxTray: WM_QUIT!\n"));
452 SetEvent(gStopSem);
453 continue;
454 }
455 TranslateMessage(&msg);
456 DispatchMessage(&msg);
457 }
458 /* we might have to repeat this operation because the shell might not be loaded yet */
459 if (!fTrayIconCreated)
460 {
461 fTrayIconCreated = Shell_NotifyIcon(NIM_ADD, &ndata);
462 Log(("VBoxTray: fTrayIconCreated = %d, err %08X\n", fTrayIconCreated, GetLastError ()));
463
464 /* We're ready to create the tooltip balloon. */
465 if (fTrayIconCreated && dwMajorVersion >= 5)
466 {
467 /* Check in 10 seconds (@todo make seconds configurable) ... */
468 SetTimer(gToolWindow,
469 WM_VBOX_CHECK_HOSTVERSION,
470 10000, /* 10 seconds */
471 NULL /* no timerproc */);
472 }
473 }
474 }
475 }
476
477 Log(("VBoxTray: Returned from main loop, exiting ...\n"));
478
479 /* remove the system tray icon and refresh system tray */
480 Shell_NotifyIcon(NIM_DELETE, &ndata);
481 HWND hTrayWnd = FindWindow("Shell_TrayWnd", NULL); /* We assume we only have one tray atm */
482 HWND hTrayNotifyWnd = FindWindowEx(hTrayWnd, 0, "TrayNotifyWnd", NULL);
483 if (hTrayNotifyWnd)
484 SendMessage(hTrayNotifyWnd, WM_PAINT, 0, NULL);
485
486 Log(("VBoxTray: waiting for display change thread ...\n"));
487
488 vboxStopServices (&svcEnv, vboxServiceTable);
489
490 Log(("VBoxTray: Destroying tool window ...\n"));
491
492 /* destroy the tool window */
493 DestroyWindow(gToolWindow);
494
495 UnregisterClass("VirtualBoxTool", gInstance);
496
497 CloseHandle(gVBoxDriver);
498 CloseHandle(gStopSem);
499 CloseHandle(ghSeamlessNotifyEvent);
500
501 Log(("VBoxTray: Leaving service main function\n"));
502
503 return;
504}
505
506
507/**
508 * Main function
509 */
510int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
511{
512 /* Do not use a global namespace ("Global\\") for mutex name here, will blow up NT4 compatibility! */
513 HANDLE hMutexAppRunning = CreateMutex(NULL, FALSE, "VBoxTray");
514 if ( (hMutexAppRunning != NULL)
515 && (GetLastError() == ERROR_ALREADY_EXISTS))
516 {
517 /* Close the mutex for this application instance. */
518 CloseHandle (hMutexAppRunning);
519 hMutexAppRunning = NULL;
520 return 0;
521 }
522
523 int rc = RTR3Init();
524 if (RT_FAILURE(rc))
525 return rc;
526
527 rc = VbglR3Init();
528 if (RT_FAILURE(rc))
529 return rc;
530
531 LogRel(("VBoxTray: %s r%s started.\n", RTBldCfgVersion(), RTBldCfgRevisionStr()));
532
533 gInstance = hInstance;
534 VBoxServiceStart();
535
536 LogRel(("VBoxTray: Ended.\n"));
537
538 /* Release instance mutex. */
539 if (hMutexAppRunning != NULL) {
540 CloseHandle(hMutexAppRunning);
541 hMutexAppRunning = NULL;
542 }
543
544 VbglR3Term();
545 return 0;
546}
547
548/**
549 * Window procedure for our tool window
550 */
551LRESULT CALLBACK VBoxToolWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
552{
553 switch (msg)
554 {
555 case WM_CLOSE:
556 break;
557
558 case WM_DESTROY:
559 KillTimer(gToolWindow, WM_VBOX_CHECK_HOSTVERSION);
560 break;
561
562 case WM_TIMER:
563 switch (wParam)
564 {
565 case WM_VBOX_CHECK_HOSTVERSION:
566 if (RT_SUCCESS(VBoxCheckHostVersion()))
567 {
568 /* After successful run we don't need to check again. */
569 KillTimer(gToolWindow, WM_VBOX_CHECK_HOSTVERSION);
570 }
571 return 0;
572
573 default:
574 break;
575 }
576
577 break;
578
579 case WM_VBOX_TRAY:
580 switch (lParam)
581 {
582 case WM_LBUTTONDBLCLK:
583 break;
584
585 case WM_RBUTTONDOWN:
586 break;
587 }
588 break;
589
590 case WM_VBOX_INSTALL_SEAMLESS_HOOK:
591 VBoxSeamlessInstallHook();
592 break;
593
594 case WM_VBOX_REMOVE_SEAMLESS_HOOK:
595 VBoxSeamlessRemoveHook();
596 break;
597
598 case WM_VBOX_SEAMLESS_UPDATE:
599 VBoxSeamlessCheckWindows();
600 break;
601
602 case WM_VBOX_RESTORED:
603 VBoxRestoreSession();
604 break;
605
606 case WM_VBOX_CHECK_VRDP:
607 VBoxRestoreCheckVRDP();
608 break;
609
610 default:
611 return DefWindowProc(hwnd, msg, wParam, lParam);
612 }
613 return 0;
614}
615
616/* display driver interface abstraction for XPDM & WDDM
617 * with WDDM we can not use ExtEscape to communicate with our driver
618 * because we do not have XPDM display driver any more, i.e. escape requests are handled by cdd
619 * that knows nothing about us */
620DWORD VBoxDispIfInit(PVBOXDISPIF pIf)
621{
622 pIf->enmMode = VBOXDISPIF_MODE_XPDM;
623 return NO_ERROR;
624}
625
626DWORD VBoxDispIfTerm(PVBOXDISPIF pIf)
627{
628 pIf->enmMode = VBOXDISPIF_MODE_UNKNOWN;
629 return NO_ERROR;
630}
631
632static DWORD vboxDispIfEscapeXPDM(PCVBOXDISPIF pIf, PVBOXDISPIFESCAPE pEscape, int cbData)
633{
634 HDC hdc = GetDC(HWND_DESKTOP);
635 VOID *pvData = cbData ? VBOXDISPIFESCAPE_DATA(pEscape, VOID) : NULL;
636 int iRet = ExtEscape(hdc, pEscape->escapeCode, cbData, (LPCSTR)pvData, 0, NULL);
637 ReleaseDC(HWND_DESKTOP, hdc);
638 if (iRet > 0)
639 return VINF_SUCCESS;
640 else if (iRet == 0)
641 return ERROR_NOT_SUPPORTED;
642 /* else */
643 return ERROR_GEN_FAILURE;
644}
645
646#ifdef VBOXWDDM
647static DWORD vboxDispIfSwitchToWDDM(PVBOXDISPIF pIf)
648{
649 DWORD err = NO_ERROR;
650 OSVERSIONINFO OSinfo;
651 OSinfo.dwOSVersionInfoSize = sizeof (OSinfo);
652 GetVersionEx (&OSinfo);
653 if (OSinfo.dwMajorVersion >= 6)
654 {
655 /* this is vista and up */
656 Log((__FUNCTION__": this is vista and up\n"));
657 HMODULE hGdi32 = GetModuleHandle("gdi32");
658 if (hGdi32 != NULL)
659 {
660 bool bSupported = true;
661 pIf->modeData.wddm.pfnD3DKMTOpenAdapterFromHdc = (PFND3DKMT_OPENADAPTERFROMHDC)GetProcAddress(hGdi32, "D3DKMTOpenAdapterFromHdc");
662 Log((__FUNCTION__"pfnD3DKMTOpenAdapterFromHdc = %p\n", pIf->modeData.wddm.pfnD3DKMTOpenAdapterFromHdc));
663 bSupported &= !!(pIf->modeData.wddm.pfnD3DKMTOpenAdapterFromHdc);
664
665 pIf->modeData.wddm.pfnD3DKMTOpenAdapterFromGdiDisplayName = (PFND3DKMT_OPENADAPTERFROMGDIDISPLAYNAME)GetProcAddress(hGdi32, "D3DKMTOpenAdapterFromGdiDisplayName");
666 Log((__FUNCTION__": pfnD3DKMTOpenAdapterFromGdiDisplayName = %p\n", pIf->modeData.wddm.pfnD3DKMTOpenAdapterFromGdiDisplayName));
667 bSupported &= !!(pIf->modeData.wddm.pfnD3DKMTOpenAdapterFromGdiDisplayName);
668
669 pIf->modeData.wddm.pfnD3DKMTCloseAdapter = (PFND3DKMT_CLOSEADAPTER)GetProcAddress(hGdi32, "D3DKMTCloseAdapter");
670 Log((__FUNCTION__": pfnD3DKMTCloseAdapter = %p\n", pIf->modeData.wddm.pfnD3DKMTCloseAdapter));
671 bSupported &= !!(pIf->modeData.wddm.pfnD3DKMTCloseAdapter);
672
673 pIf->modeData.wddm.pfnD3DKMTEscape = (PFND3DKMT_ESCAPE)GetProcAddress(hGdi32, "D3DKMTEscape");
674 Log((__FUNCTION__": pfnD3DKMTEscape = %p\n", pIf->modeData.wddm.pfnD3DKMTEscape));
675 bSupported &= !!(pIf->modeData.wddm.pfnD3DKMTCloseAdapter);
676
677 pIf->modeData.wddm.pfnD3DKMTInvalidateActiveVidPn = (PFND3DKMT_INVALIDATEACTIVEVIDPN)GetProcAddress(hGdi32, "D3DKMTInvalidateActiveVidPn");
678 Log((__FUNCTION__": pfnD3DKMTInvalidateActiveVidPn = %p\n", pIf->modeData.wddm.pfnD3DKMTInvalidateActiveVidPn));
679 bSupported &= !!(pIf->modeData.wddm.pfnD3DKMTInvalidateActiveVidPn);
680
681 if (!bSupported)
682 {
683 Log((__FUNCTION__": one of pfnD3DKMT function pointers failed to initialize\n"));
684 err = ERROR_NOT_SUPPORTED;
685 }
686 }
687 else
688 {
689 Log((__FUNCTION__": GetModuleHandle(gdi32) failed, err(%d)\n", GetLastError()));
690 err = ERROR_NOT_SUPPORTED;
691 }
692 }
693 else
694 {
695 Log((__FUNCTION__": can not switch to VBOXDISPIF_MODE_WDDM, because os is not Vista or upper\n"));
696 err = ERROR_NOT_SUPPORTED;
697 }
698
699 return err;
700}
701
702typedef DECLCALLBACK(BOOLEAN) FNVBOXDISPIFWDDM_ADAPTEROP(PCVBOXDISPIF pIf, D3DKMT_HANDLE hAdapter, LPCWSTR pDevName, PVOID pContext);
703typedef FNVBOXDISPIFWDDM_ADAPTEROP *PFNVBOXDISPIFWDDM_ADAPTEROP;
704static DWORD vboxDispIfWDDMAdapterOp(PCVBOXDISPIF pIf, LPCWSTR pDevName, PFNVBOXDISPIFWDDM_ADAPTEROP pfnOp, PVOID pContext)
705{
706 D3DKMT_OPENADAPTERFROMGDIDISPLAYNAME OpenAdapterData = {0};
707 wcsncpy(OpenAdapterData.DeviceName, pDevName, RT_ELEMENTS(OpenAdapterData.DeviceName) - 1 /* the last one is always \0 */);
708 DWORD err = ERROR_GEN_FAILURE;
709 NTSTATUS Status = pIf->modeData.wddm.pfnD3DKMTOpenAdapterFromGdiDisplayName(&OpenAdapterData);
710 if (!Status)
711 {
712 BOOLEAN bCloseAdapter = pfnOp(pIf, OpenAdapterData.hAdapter, OpenAdapterData.DeviceName, pContext);
713
714 if (bCloseAdapter)
715 {
716 D3DKMT_CLOSEADAPTER ClosaAdapterData = {0};
717 ClosaAdapterData.hAdapter = OpenAdapterData.hAdapter;
718 Status = pIf->modeData.wddm.pfnD3DKMTCloseAdapter(&ClosaAdapterData);
719 if (Status)
720 {
721 Log((__FUNCTION__": pfnD3DKMTCloseAdapter failed, Status (0x%x)\n", Status));
722 /* ignore */
723 Status = 0;
724 }
725 }
726 }
727 else
728 Log((__FUNCTION__": pfnD3DKMTOpenAdapterFromGdiDisplayName failed, Status (0x%x)\n", Status));
729
730 return err;
731}
732
733typedef struct
734{
735 NTSTATUS Status;
736 PVBOXDISPIFESCAPE pEscape;
737 int cbData;
738} VBOXDISPIFWDDM_ESCAPEOP_CONTEXT, *PVBOXDISPIFWDDM_ESCAPEOP_CONTEXT;
739
740DECLCALLBACK(BOOLEAN) vboxDispIfEscapeWDDMOp(PCVBOXDISPIF pIf, D3DKMT_HANDLE hAdapter, LPCWSTR pDevName, PVOID pContext)
741{
742 PVBOXDISPIFWDDM_ESCAPEOP_CONTEXT pCtx = (PVBOXDISPIFWDDM_ESCAPEOP_CONTEXT)pContext;
743
744 D3DKMT_ESCAPE EscapeData = {0};
745 EscapeData.hAdapter = hAdapter;
746 //EscapeData.hDevice = NULL;
747 EscapeData.Type = D3DKMT_ESCAPE_DRIVERPRIVATE;
748 EscapeData.Flags.HardwareAccess = 1;
749 EscapeData.pPrivateDriverData = pCtx->pEscape;
750 EscapeData.PrivateDriverDataSize = VBOXDISPIFESCAPE_SIZE(pCtx->cbData);
751 //EscapeData.hContext = NULL;
752
753 pCtx->Status = pIf->modeData.wddm.pfnD3DKMTEscape(&EscapeData);
754
755 return TRUE;
756}
757
758static DWORD vboxDispIfEscapeWDDM(PCVBOXDISPIF pIf, PVBOXDISPIFESCAPE pEscape, int cbData)
759{
760 VBOXDISPIFWDDM_ESCAPEOP_CONTEXT Ctx = {0};
761 Ctx.pEscape = pEscape;
762 Ctx.cbData = cbData;
763 DWORD err = vboxDispIfWDDMAdapterOp(pIf, L"\\\\.\\DISPLAY1", vboxDispIfEscapeWDDMOp, &Ctx);
764 if (err == NO_ERROR)
765 {
766 if (!Ctx.Status)
767 err = NO_ERROR;
768 else
769 {
770 if (Ctx.Status == 0xC00000BBL) /* not supported */
771 err = ERROR_NOT_SUPPORTED;
772 else
773 err = ERROR_GEN_FAILURE;
774 Log((__FUNCTION__": pfnD3DKMTEscape failed, Status (0x%x)\n", Ctx.Status));
775 }
776 }
777 else
778 Log((__FUNCTION__": vboxDispIfWDDMAdapterOp failed, err (%d)\n", err));
779
780 return err;
781}
782
783typedef struct
784{
785 NTSTATUS Status;
786 VBOXWDDM_RECOMMENDVIDPN_SCREEN_INFO Info;
787} VBOXDISPIFWDDM_RESIZEOP_CONTEXT, *PVBOXDISPIFWDDM_RESIZEOP_CONTEXT;
788
789DECLCALLBACK(BOOLEAN) vboxDispIfResizeWDDMOp(PCVBOXDISPIF pIf, D3DKMT_HANDLE hAdapter, LPCWSTR pDevName, PVOID pContext)
790{
791 PVBOXDISPIFWDDM_RESIZEOP_CONTEXT pCtx = (PVBOXDISPIFWDDM_RESIZEOP_CONTEXT)pContext;
792
793 D3DKMT_INVALIDATEACTIVEVIDPN IAVidPnData = {0};
794 uint32_t cbData = VBOXWDDM_RECOMMENDVIDPN_SIZE(1);
795 PVBOXWDDM_RECOMMENDVIDPN pData = (PVBOXWDDM_RECOMMENDVIDPN)malloc(cbData);
796 if (pData)
797 {
798 memset(pData, 0, cbData);
799 pData->cScreenInfos = 1;
800 memcpy(&pData->aScreenInfos[0], &pCtx->Info, sizeof (VBOXWDDM_RECOMMENDVIDPN_SCREEN_INFO));
801
802 IAVidPnData.hAdapter = hAdapter;
803 IAVidPnData.pPrivateDriverData = pData;
804 IAVidPnData.PrivateDriverDataSize = cbData;
805
806 pCtx->Status = pIf->modeData.wddm.pfnD3DKMTInvalidateActiveVidPn(&IAVidPnData);
807 if (pCtx->Status)
808 Log((__FUNCTION__": pfnD3DKMTInvalidateActiveVidPn failed, Status (0x%x)\n", pCtx->Status));
809
810 free(pData);
811 }
812 else
813 {
814 Log((__FUNCTION__": malloc failed\n"));
815 pCtx->Status = -1;
816 }
817
818 return TRUE;
819}
820
821static DWORD vboxDispIfResizeWDDM(PCVBOXDISPIF const pIf, ULONG Id, DWORD Width, DWORD Height, DWORD BitsPerPixel)
822{
823 VBOXDISPIFWDDM_RESIZEOP_CONTEXT Ctx = {0};
824 Ctx.Info.Id = Id;
825 Ctx.Info.Width = Width;
826 Ctx.Info.Height = Height;
827 Ctx.Info.BitsPerPixel = BitsPerPixel;
828 DWORD err = vboxDispIfWDDMAdapterOp(pIf, L"\\\\.\\DISPLAY1", vboxDispIfResizeWDDMOp, &Ctx);
829 if (err == NO_ERROR)
830 {
831 if (!Ctx.Status)
832 err = NO_ERROR;
833 else
834 {
835 if (Ctx.Status == 0xC00000BBL) /* not supported */
836 err = ERROR_NOT_SUPPORTED;
837 else
838 err = ERROR_GEN_FAILURE;
839 Log((__FUNCTION__": vboxDispIfResizeWDDMOp failed, Status (0x%x)\n", Ctx.Status));
840 }
841 }
842 else
843 Log((__FUNCTION__": vboxDispIfWDDMAdapterOp failed, err (%d)\n", err));
844
845 return err;
846}
847#endif
848
849DWORD VBoxDispIfEscape(PCVBOXDISPIF pIf, PVBOXDISPIFESCAPE pEscape, int cbData)
850{
851 switch (pIf->enmMode)
852 {
853 case VBOXDISPIF_MODE_XPDM_NT4:
854 case VBOXDISPIF_MODE_XPDM:
855 return vboxDispIfEscapeXPDM(pIf, pEscape, cbData);
856#ifdef VBOXWDDM
857 case VBOXDISPIF_MODE_WDDM:
858 return vboxDispIfEscapeWDDM(pIf, pEscape, cbData);
859#endif
860 default:
861 Log((__FUNCTION__": unknown mode (%d)\n", pIf->enmMode));
862 return ERROR_INVALID_PARAMETER;
863 }
864}
865
866static DWORD vboxDispIfResizeXPDM(PCVBOXDISPIF const pIf, ULONG Id, DWORD Width, DWORD Height, DWORD BitsPerPixel)
867{
868 return ERROR_NOT_SUPPORTED;
869}
870
871DWORD VBoxDispIfResize(PCVBOXDISPIF const pIf, ULONG Id, DWORD Width, DWORD Height, DWORD BitsPerPixel)
872{
873 switch (pIf->enmMode)
874 {
875 case VBOXDISPIF_MODE_XPDM_NT4:
876 return ERROR_NOT_SUPPORTED;
877 case VBOXDISPIF_MODE_XPDM:
878 return vboxDispIfResizeXPDM(pIf, Id, Width, Height, BitsPerPixel);
879#ifdef VBOXWDDM
880 case VBOXDISPIF_MODE_WDDM:
881 return vboxDispIfResizeWDDM(pIf, Id, Width, Height, BitsPerPixel);
882#endif
883 default:
884 Log((__FUNCTION__": unknown mode (%d)\n", pIf->enmMode));
885 return ERROR_INVALID_PARAMETER;
886 }
887}
888
889static DWORD vboxDispIfSwitchToXPDM_NT4(PVBOXDISPIF pIf)
890{
891 return NO_ERROR;
892}
893
894static DWORD vboxDispIfSwitchToXPDM(PVBOXDISPIF pIf)
895{
896 DWORD err = NO_ERROR;
897 AssertBreakpoint();
898 OSVERSIONINFO OSinfo;
899 OSinfo.dwOSVersionInfoSize = sizeof (OSinfo);
900 GetVersionEx (&OSinfo);
901 if (OSinfo.dwMajorVersion >= 5)
902 {
903 HMODULE hUser = GetModuleHandle("USER32");
904 if (NULL != hUser)
905 {
906 bool bSupported = true;
907 *(uintptr_t *)&pIf->modeData.xpdm.pfnChangeDisplaySettingsEx = (uintptr_t)GetProcAddress(hUser, "ChangeDisplaySettingsExA");
908 Log((__FUNCTION__": pfnChangeDisplaySettingsEx = %p\n", pIf->modeData.xpdm.pfnChangeDisplaySettingsEx));
909 bSupported &= !!(pIf->modeData.xpdm.pfnChangeDisplaySettingsEx);
910
911 if (!bSupported)
912 {
913 Log((__FUNCTION__": pfnChangeDisplaySettingsEx function pointer failed to initialize\n"));
914 err = ERROR_NOT_SUPPORTED;
915 }
916 }
917 else
918 {
919 Log((__FUNCTION__": failed to get USER32 handle, err (%d)\n", GetLastError()));
920 err = ERROR_NOT_SUPPORTED;
921 }
922 }
923 else
924 {
925 Log((__FUNCTION__": can not switch to VBOXDISPIF_MODE_XPDM, because os is not >= w2k\n"));
926 err = ERROR_NOT_SUPPORTED;
927 }
928
929 return err;
930}
931
932DWORD VBoxDispIfSwitchMode(PVBOXDISPIF pIf, VBOXDISPIF_MODE enmMode, VBOXDISPIF_MODE *penmOldMode)
933{
934 /* @todo: may need to addd synchronization in case we want to change modes dynamically
935 * i.e. currently the mode is supposed to be initialized once on service initialization */
936 if (penmOldMode)
937 *penmOldMode = pIf->enmMode;
938
939 if (enmMode == pIf->enmMode)
940 return VINF_ALREADY_INITIALIZED;
941
942 DWORD err = NO_ERROR;
943 switch (enmMode)
944 {
945 case VBOXDISPIF_MODE_XPDM_NT4:
946 Log((__FUNCTION__": request to switch to VBOXDISPIF_MODE_XPDM_NT4\n"));
947 err = vboxDispIfSwitchToXPDM_NT4(pIf);
948 if (err == NO_ERROR)
949 {
950 Log((__FUNCTION__": successfully switched to XPDM_NT4 mode\n"));
951 pIf->enmMode = VBOXDISPIF_MODE_XPDM_NT4;
952 }
953 else
954 Log((__FUNCTION__": failed to switch to XPDM_NT4 mode, err (%d)\n", err));
955 break;
956 case VBOXDISPIF_MODE_XPDM:
957 Log((__FUNCTION__": request to switch to VBOXDISPIF_MODE_XPDM\n"));
958 err = vboxDispIfSwitchToXPDM(pIf);
959 if (err == NO_ERROR)
960 {
961 Log((__FUNCTION__": successfully switched to XPDM mode\n"));
962 pIf->enmMode = VBOXDISPIF_MODE_XPDM;
963 }
964 else
965 Log((__FUNCTION__": failed to switch to XPDM mode, err (%d)\n", err));
966 break;
967#ifdef VBOXWDDM
968 case VBOXDISPIF_MODE_WDDM:
969 {
970 Log((__FUNCTION__": request to switch to VBOXDISPIF_MODE_WDDM\n"));
971 err = vboxDispIfSwitchToWDDM(pIf);
972 if (err == NO_ERROR)
973 {
974 Log((__FUNCTION__": successfully switched to WDDM mode\n"));
975 pIf->enmMode = VBOXDISPIF_MODE_WDDM;
976 }
977 else
978 Log((__FUNCTION__": failed to switch to WDDM mode, err (%d)\n", err));
979 break;
980 }
981#endif
982 default:
983 err = ERROR_INVALID_PARAMETER;
984 break;
985 }
986 return err;
987}
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