VirtualBox

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

Last change on this file since 63103 was 63099, checked in by vboxsync, 8 years ago

GA/NT/VBoxTray: warnings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 64.6 KB
Line 
1/* $Id: VBoxTray.cpp 63099 2016-08-06 15:38:51Z vboxsync $ */
2/** @file
3 * VBoxTray - Guest Additions Tray Application
4 */
5
6/*
7 * Copyright (C) 2006-2016 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/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include <package-generated.h>
23#include "product-generated.h"
24
25#include "VBoxTray.h"
26#include "VBoxTrayMsg.h"
27#include "VBoxHelpers.h"
28#include "VBoxSeamless.h"
29#include "VBoxClipboard.h"
30#include "VBoxDisplay.h"
31#include "VBoxVRDP.h"
32#include "VBoxHostVersion.h"
33#include "VBoxSharedFolders.h"
34#ifdef VBOX_WITH_DRAG_AND_DROP
35# include "VBoxDnD.h"
36#endif
37#include "VBoxIPC.h"
38#include "VBoxLA.h"
39#include <VBoxHook.h>
40#include "resource.h"
41
42#include <sddl.h>
43
44#include <iprt/asm.h>
45#include <iprt/buildconfig.h>
46#include <iprt/ldr.h>
47#include <iprt/process.h>
48#include <iprt/system.h>
49#include <iprt/time.h>
50
51#ifdef DEBUG
52# define LOG_ENABLED
53# define LOG_GROUP LOG_GROUP_DEFAULT
54#endif
55#include <VBox/log.h>
56
57/* Default desktop state tracking */
58#include <Wtsapi32.h>
59
60/*
61 * St (session [state] tracking) functionality API
62 *
63 * !!!NOTE: this API is NOT thread-safe!!!
64 * it is supposed to be called & used from within the window message handler thread
65 * of the window passed to vboxStInit */
66static int vboxStInit(HWND hWnd);
67static void vboxStTerm(void);
68/* @returns true on "IsActiveConsole" state change */
69static BOOL vboxStHandleEvent(WPARAM EventID);
70static BOOL vboxStIsActiveConsole();
71static BOOL vboxStCheckTimer(WPARAM wEvent);
72
73/*
74 * Dt (desktop [state] tracking) functionality API
75 *
76 * !!!NOTE: this API is NOT thread-safe!!!
77 * */
78static int vboxDtInit();
79static void vboxDtTerm();
80/* @returns true on "IsInputDesktop" state change */
81static BOOL vboxDtHandleEvent();
82/* @returns true iff the application (VBoxTray) desktop is input */
83static BOOL vboxDtIsInputDesktop();
84static HANDLE vboxDtGetNotifyEvent();
85static BOOL vboxDtCheckTimer(WPARAM wParam);
86
87/* caps API */
88#define VBOXCAPS_ENTRY_IDX_SEAMLESS 0
89#define VBOXCAPS_ENTRY_IDX_GRAPHICS 1
90#define VBOXCAPS_ENTRY_IDX_COUNT 2
91
92typedef enum VBOXCAPS_ENTRY_FUNCSTATE
93{
94 /* the cap is unsupported */
95 VBOXCAPS_ENTRY_FUNCSTATE_UNSUPPORTED = 0,
96 /* the cap is supported */
97 VBOXCAPS_ENTRY_FUNCSTATE_SUPPORTED,
98 /* the cap functionality is started, it can be disabled however if its AcState is not ACQUIRED */
99 VBOXCAPS_ENTRY_FUNCSTATE_STARTED,
100} VBOXCAPS_ENTRY_FUNCSTATE;
101
102
103static void VBoxCapsEntryFuncStateSet(uint32_t iCup, VBOXCAPS_ENTRY_FUNCSTATE enmFuncState);
104static int VBoxCapsInit();
105static int VBoxCapsReleaseAll();
106static void VBoxCapsTerm();
107static BOOL VBoxCapsEntryIsAcquired(uint32_t iCap);
108static BOOL VBoxCapsEntryIsEnabled(uint32_t iCap);
109static BOOL VBoxCapsCheckTimer(WPARAM wParam);
110static int VBoxCapsEntryRelease(uint32_t iCap);
111static int VBoxCapsEntryAcquire(uint32_t iCap);
112static int VBoxCapsAcquireAllSupported();
113
114/* console-related caps API */
115static BOOL VBoxConsoleIsAllowed();
116static void VBoxConsoleEnable(BOOL fEnable);
117static void VBoxConsoleCapSetSupported(uint32_t iCap, BOOL fSupported);
118
119static void VBoxGrapicsSetSupported(BOOL fSupported);
120
121
122/*********************************************************************************************************************************
123* Internal Functions *
124*********************************************************************************************************************************/
125static int vboxTrayCreateTrayIcon(void);
126static LRESULT CALLBACK vboxToolWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
127
128/* Global message handler prototypes. */
129static int vboxTrayGlMsgTaskbarCreated(WPARAM lParam, LPARAM wParam);
130/*static int vboxTrayGlMsgShowBalloonMsg(WPARAM lParam, LPARAM wParam);*/
131
132static int VBoxAcquireGuestCaps(uint32_t fOr, uint32_t fNot, bool fCfg);
133
134
135/*********************************************************************************************************************************
136* Global Variables *
137*********************************************************************************************************************************/
138HANDLE g_hVBoxDriver;
139HANDLE g_hStopSem;
140HANDLE g_hSeamlessWtNotifyEvent = 0;
141HANDLE g_hSeamlessKmNotifyEvent = 0;
142HINSTANCE g_hInstance;
143HWND g_hwndToolWindow;
144NOTIFYICONDATA g_NotifyIconData;
145DWORD g_dwMajorVersion;
146
147uint32_t g_fGuestDisplaysChanged = 0;
148
149static PRTLOGGER g_pLoggerRelease = NULL;
150static uint32_t g_cHistory = 10; /* Enable log rotation, 10 files. */
151static uint32_t g_uHistoryFileTime = RT_SEC_1DAY; /* Max 1 day per file. */
152static uint64_t g_uHistoryFileSize = 100 * _1M; /* Max 100MB per file. */
153
154/**
155 * The details of the services that has been compiled in.
156 */
157static VBOXSERVICEINFO g_aServices[] =
158{
159 { &g_SvcDescDisplay, NIL_RTTHREAD, NULL, false, false, false, false, true },
160 { &g_SvcDescClipboard, NIL_RTTHREAD, NULL, false, false, false, false, true },
161 { &g_SvcDescSeamless, NIL_RTTHREAD, NULL, false, false, false, false, true },
162 { &g_SvcDescVRDP, NIL_RTTHREAD, NULL, false, false, false, false, true },
163 { &g_SvcDescIPC, NIL_RTTHREAD, NULL, false, false, false, false, true },
164 { &g_SvcDescLA, NIL_RTTHREAD, NULL, false, false, false, false, true },
165#ifdef VBOX_WITH_DRAG_AND_DROP
166 { &g_SvcDescDnD, NIL_RTTHREAD, NULL, false, false, false, false, true }
167#endif
168};
169
170/* The global message table. */
171static VBOXGLOBALMESSAGE g_vboxGlobalMessageTable[] =
172{
173 /* Windows specific stuff. */
174 {
175 "TaskbarCreated",
176 vboxTrayGlMsgTaskbarCreated
177 },
178
179 /* VBoxTray specific stuff. */
180 /** @todo Add new messages here! */
181
182 {
183 NULL
184 }
185};
186
187/**
188 * Gets called whenever the Windows main taskbar
189 * get (re-)created. Nice to install our tray icon.
190 *
191 * @return IPRT status code.
192 * @param wParam
193 * @param lParam
194 */
195static int vboxTrayGlMsgTaskbarCreated(WPARAM wParam, LPARAM lParam)
196{
197 RT_NOREF(wParam, lParam);
198 return vboxTrayCreateTrayIcon();
199}
200
201static int vboxTrayCreateTrayIcon(void)
202{
203 HICON hIcon = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_VIRTUALBOX));
204 if (hIcon == NULL)
205 {
206 DWORD dwErr = GetLastError();
207 LogFunc(("Could not load tray icon, error %08X\n", dwErr));
208 return RTErrConvertFromWin32(dwErr);
209 }
210
211 /* Prepare the system tray icon. */
212 RT_ZERO(g_NotifyIconData);
213 g_NotifyIconData.cbSize = NOTIFYICONDATA_V1_SIZE; // sizeof(NOTIFYICONDATA);
214 g_NotifyIconData.hWnd = g_hwndToolWindow;
215 g_NotifyIconData.uID = ID_TRAYICON;
216 g_NotifyIconData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
217 g_NotifyIconData.uCallbackMessage = WM_VBOXTRAY_TRAY_ICON;
218 g_NotifyIconData.hIcon = hIcon;
219
220 sprintf(g_NotifyIconData.szTip, "%s Guest Additions %d.%d.%dr%d",
221 VBOX_PRODUCT, VBOX_VERSION_MAJOR, VBOX_VERSION_MINOR, VBOX_VERSION_BUILD, VBOX_SVN_REV);
222
223 int rc = VINF_SUCCESS;
224 if (!Shell_NotifyIcon(NIM_ADD, &g_NotifyIconData))
225 {
226 DWORD dwErr = GetLastError();
227 LogFunc(("Could not create tray icon, error=%ld\n", dwErr));
228 rc = RTErrConvertFromWin32(dwErr);
229 RT_ZERO(g_NotifyIconData);
230 }
231
232 if (hIcon)
233 DestroyIcon(hIcon);
234 return rc;
235}
236
237static void vboxTrayRemoveTrayIcon(void)
238{
239 if (g_NotifyIconData.cbSize > 0)
240 {
241 /* Remove the system tray icon and refresh system tray. */
242 Shell_NotifyIcon(NIM_DELETE, &g_NotifyIconData);
243 HWND hTrayWnd = FindWindow("Shell_TrayWnd", NULL); /* We assume we only have one tray atm. */
244 if (hTrayWnd)
245 {
246 HWND hTrayNotifyWnd = FindWindowEx(hTrayWnd, 0, "TrayNotifyWnd", NULL);
247 if (hTrayNotifyWnd)
248 SendMessage(hTrayNotifyWnd, WM_PAINT, 0, NULL);
249 }
250 RT_ZERO(g_NotifyIconData);
251 }
252}
253
254/**
255 * The service thread.
256 *
257 * @returns Whatever the worker function returns.
258 * @param ThreadSelf My thread handle.
259 * @param pvUser The service index.
260 */
261static DECLCALLBACK(int) vboxTrayServiceThread(RTTHREAD ThreadSelf, void *pvUser)
262{
263 PVBOXSERVICEINFO pSvc = (PVBOXSERVICEINFO)pvUser;
264 AssertPtr(pSvc);
265
266#ifndef RT_OS_WINDOWS
267 /*
268 * Block all signals for this thread. Only the main thread will handle signals.
269 */
270 sigset_t signalMask;
271 sigfillset(&signalMask);
272 pthread_sigmask(SIG_BLOCK, &signalMask, NULL);
273#endif
274
275 int rc = pSvc->pDesc->pfnWorker(pSvc->pInstance, &pSvc->fShutdown);
276 ASMAtomicXchgBool(&pSvc->fShutdown, true);
277 RTThreadUserSignal(ThreadSelf);
278
279 LogFunc(("Worker for '%s' ended with %Rrc\n", pSvc->pDesc->pszName, rc));
280 return rc;
281}
282
283static int vboxTrayServicesStart(PVBOXSERVICEENV pEnv)
284{
285 AssertPtrReturn(pEnv, VERR_INVALID_POINTER);
286
287 LogRel(("Starting services ...\n"));
288
289 int rc = VINF_SUCCESS;
290
291 for (unsigned i = 0; i < RT_ELEMENTS(g_aServices); i++)
292 {
293 PVBOXSERVICEINFO pSvc = &g_aServices[i];
294 LogRel(("Starting service '%s' ...\n", pSvc->pDesc->pszName));
295
296 pSvc->hThread = NIL_RTTHREAD;
297 pSvc->pInstance = NULL;
298 pSvc->fStarted = false;
299 pSvc->fShutdown = false;
300
301 int rc2 = VINF_SUCCESS;
302
303 if (pSvc->pDesc->pfnInit)
304 rc2 = pSvc->pDesc->pfnInit(pEnv, &pSvc->pInstance);
305
306 if (RT_FAILURE(rc2))
307 {
308 LogRel(("Failed to initialize service '%s', rc=%Rrc\n", pSvc->pDesc->pszName, rc2));
309 if (rc2 == VERR_NOT_SUPPORTED)
310 {
311 LogRel(("Service '%s' is not supported on this system\n", pSvc->pDesc->pszName));
312 rc2 = VINF_SUCCESS;
313 }
314 /* Keep going. */
315 }
316 else
317 {
318 if (pSvc->pDesc->pfnWorker)
319 {
320 rc2 = RTThreadCreate(&pSvc->hThread, vboxTrayServiceThread, pSvc /* pvUser */,
321 0 /* Default stack size */, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, pSvc->pDesc->pszName);
322 if (RT_SUCCESS(rc2))
323 {
324 pSvc->fStarted = true;
325
326 RTThreadUserWait(pSvc->hThread, 30 * 1000 /* Timeout in ms */);
327 if (pSvc->fShutdown)
328 {
329 LogRel(("Service '%s' failed to start!\n", pSvc->pDesc->pszName));
330 rc = VERR_GENERAL_FAILURE;
331 }
332 else
333 LogRel(("Service '%s' started\n", pSvc->pDesc->pszName));
334 }
335 else
336 {
337 LogRel(("Failed to start thread for service '%s': %Rrc\n", rc2));
338 if (pSvc->pDesc->pfnDestroy)
339 pSvc->pDesc->pfnDestroy(pSvc->pInstance);
340 }
341 }
342 }
343
344 if (RT_SUCCESS(rc))
345 rc = rc2;
346 }
347
348 if (RT_SUCCESS(rc))
349 LogRel(("All services started\n"));
350 else
351 LogRel(("Services started, but some with errors\n"));
352
353 LogFlowFuncLeaveRC(rc);
354 return rc;
355}
356
357static int vboxTrayServicesStop(VBOXSERVICEENV *pEnv)
358{
359 AssertPtrReturn(pEnv, VERR_INVALID_POINTER);
360
361 LogRel2(("Stopping all services ...\n"));
362
363 /*
364 * Signal all the services.
365 */
366 for (unsigned i = 0; i < RT_ELEMENTS(g_aServices); i++)
367 ASMAtomicWriteBool(&g_aServices[i].fShutdown, true);
368
369 /*
370 * Do the pfnStop callback on all running services.
371 */
372 for (unsigned i = 0; i < RT_ELEMENTS(g_aServices); i++)
373 {
374 PVBOXSERVICEINFO pSvc = &g_aServices[i];
375 if ( pSvc->fStarted
376 && pSvc->pDesc->pfnStop)
377 {
378 LogRel2(("Calling stop function for service '%s' ...\n", pSvc->pDesc->pszName));
379 int rc2 = pSvc->pDesc->pfnStop(pSvc->pInstance);
380 if (RT_FAILURE(rc2))
381 LogRel(("Failed to stop service '%s': %Rrc\n", pSvc->pDesc->pszName, rc2));
382 }
383 }
384
385 LogRel2(("All stop functions for services called\n"));
386
387 int rc = VINF_SUCCESS;
388
389 /*
390 * Wait for all the service threads to complete.
391 */
392 for (unsigned i = 0; i < RT_ELEMENTS(g_aServices); i++)
393 {
394 PVBOXSERVICEINFO pSvc = &g_aServices[i];
395 if (!pSvc->fEnabled) /* Only stop services which were started before. */
396 continue;
397
398 if (pSvc->hThread != NIL_RTTHREAD)
399 {
400 LogRel2(("Waiting for service '%s' to stop ...\n", pSvc->pDesc->pszName));
401 int rc2 = VINF_SUCCESS;
402 for (int j = 0; j < 30; j++) /* Wait 30 seconds in total */
403 {
404 rc2 = RTThreadWait(pSvc->hThread, 1000 /* Wait 1 second */, NULL);
405 if (RT_SUCCESS(rc2))
406 break;
407 }
408 if (RT_FAILURE(rc2))
409 {
410 LogRel(("Service '%s' failed to stop (%Rrc)\n", pSvc->pDesc->pszName, rc2));
411 if (RT_SUCCESS(rc))
412 rc = rc2;
413 }
414 }
415
416 if (pSvc->pDesc->pfnDestroy)
417 {
418 LogRel2(("Terminating service '%s' ...\n", pSvc->pDesc->pszName));
419 pSvc->pDesc->pfnDestroy(pSvc->pInstance);
420 }
421 }
422
423 if (RT_SUCCESS(rc))
424 LogRel(("All services stopped\n"));
425
426 LogFlowFuncLeaveRC(rc);
427 return rc;
428}
429
430static int vboxTrayRegisterGlobalMessages(PVBOXGLOBALMESSAGE pTable)
431{
432 int rc = VINF_SUCCESS;
433 if (pTable == NULL) /* No table to register? Skip. */
434 return rc;
435 while ( pTable->pszName
436 && RT_SUCCESS(rc))
437 {
438 /* Register global accessible window messages. */
439 pTable->uMsgID = RegisterWindowMessage(TEXT(pTable->pszName));
440 if (!pTable->uMsgID)
441 {
442 DWORD dwErr = GetLastError();
443 Log(("Registering global message \"%s\" failed, error = %08X\n", dwErr));
444 rc = RTErrConvertFromWin32(dwErr);
445 }
446
447 /* Advance to next table element. */
448 pTable++;
449 }
450 return rc;
451}
452
453static bool vboxTrayHandleGlobalMessages(PVBOXGLOBALMESSAGE pTable, UINT uMsg,
454 WPARAM wParam, LPARAM lParam)
455{
456 if (pTable == NULL)
457 return false;
458 while (pTable && pTable->pszName)
459 {
460 if (pTable->uMsgID == uMsg)
461 {
462 if (pTable->pfnHandler)
463 pTable->pfnHandler(wParam, lParam);
464 return true;
465 }
466
467 /* Advance to next table element. */
468 pTable++;
469 }
470 return false;
471}
472
473static int vboxTrayOpenBaseDriver(void)
474{
475 /* Open VBox guest driver. */
476 DWORD dwErr = ERROR_SUCCESS;
477 g_hVBoxDriver = CreateFile(VBOXGUEST_DEVICE_NAME,
478 GENERIC_READ | GENERIC_WRITE,
479 FILE_SHARE_READ | FILE_SHARE_WRITE,
480 NULL,
481 OPEN_EXISTING,
482 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
483 NULL);
484 if (g_hVBoxDriver == INVALID_HANDLE_VALUE)
485 {
486 dwErr = GetLastError();
487 LogRel(("Could not open VirtualBox Guest Additions driver! Please install / start it first! Error = %08X\n", dwErr));
488 }
489 return RTErrConvertFromWin32(dwErr);
490}
491
492static void vboxTrayCloseBaseDriver(void)
493{
494 if (g_hVBoxDriver)
495 {
496 CloseHandle(g_hVBoxDriver);
497 g_hVBoxDriver = NULL;
498 }
499}
500
501/**
502 * Release logger callback.
503 *
504 * @return IPRT status code.
505 * @param pLoggerRelease
506 * @param enmPhase
507 * @param pfnLog
508 */
509static void vboxTrayLogHeaderFooter(PRTLOGGER pLoggerRelease, RTLOGPHASE enmPhase, PFNRTLOGPHASEMSG pfnLog)
510{
511 /* Some introductory information. */
512 static RTTIMESPEC s_TimeSpec;
513 char szTmp[256];
514 if (enmPhase == RTLOGPHASE_BEGIN)
515 RTTimeNow(&s_TimeSpec);
516 RTTimeSpecToString(&s_TimeSpec, szTmp, sizeof(szTmp));
517
518 switch (enmPhase)
519 {
520 case RTLOGPHASE_BEGIN:
521 {
522 pfnLog(pLoggerRelease,
523 "VBoxTray %s r%s %s (%s %s) release log\n"
524 "Log opened %s\n",
525 RTBldCfgVersion(), RTBldCfgRevisionStr(), VBOX_BUILD_TARGET,
526 __DATE__, __TIME__, szTmp);
527
528 int vrc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szTmp, sizeof(szTmp));
529 if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
530 pfnLog(pLoggerRelease, "OS Product: %s\n", szTmp);
531 vrc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szTmp, sizeof(szTmp));
532 if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
533 pfnLog(pLoggerRelease, "OS Release: %s\n", szTmp);
534 vrc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szTmp, sizeof(szTmp));
535 if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
536 pfnLog(pLoggerRelease, "OS Version: %s\n", szTmp);
537 if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
538 pfnLog(pLoggerRelease, "OS Service Pack: %s\n", szTmp);
539
540 /* the package type is interesting for Linux distributions */
541 char szExecName[RTPATH_MAX];
542 char *pszExecName = RTProcGetExecutablePath(szExecName, sizeof(szExecName));
543 pfnLog(pLoggerRelease,
544 "Executable: %s\n"
545 "Process ID: %u\n"
546 "Package type: %s"
547#ifdef VBOX_OSE
548 " (OSE)"
549#endif
550 "\n",
551 pszExecName ? pszExecName : "unknown",
552 RTProcSelf(),
553 VBOX_PACKAGE_STRING);
554 break;
555 }
556
557 case RTLOGPHASE_PREROTATE:
558 pfnLog(pLoggerRelease, "Log rotated - Log started %s\n", szTmp);
559 break;
560
561 case RTLOGPHASE_POSTROTATE:
562 pfnLog(pLoggerRelease, "Log continuation - Log started %s\n", szTmp);
563 break;
564
565 case RTLOGPHASE_END:
566 pfnLog(pLoggerRelease, "End of log file - Log started %s\n", szTmp);
567 break;
568
569 default:
570 /* nothing */;
571 }
572}
573
574/**
575 * Creates the default release logger outputting to the specified file.
576 * Pass NULL for disabled logging.
577 *
578 * @return IPRT status code.
579 * @param pszLogFile Filename for log output. Optional.
580 */
581static int vboxTrayLogCreate(const char *pszLogFile)
582{
583 /* Create release logger (stdout + file). */
584 static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
585 RTUINT fFlags = RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME_PROG;
586#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
587 fFlags |= RTLOGFLAGS_USECRLF;
588#endif
589 char szError[RTPATH_MAX + 128] = "";
590 int rc = RTLogCreateEx(&g_pLoggerRelease, fFlags,
591#ifdef DEBUG
592 "all.e.l.f",
593 "VBOXTRAY_LOG",
594#else
595 "all",
596 "VBOXTRAY_RELEASE_LOG",
597#endif
598 RT_ELEMENTS(s_apszGroups), s_apszGroups, RTLOGDEST_STDOUT,
599 vboxTrayLogHeaderFooter, g_cHistory, g_uHistoryFileSize, g_uHistoryFileTime,
600 szError, sizeof(szError), pszLogFile);
601 if (RT_SUCCESS(rc))
602 {
603#ifdef DEBUG
604 RTLogSetDefaultInstance(g_pLoggerRelease);
605#else
606 /* Register this logger as the release logger. */
607 RTLogRelSetDefaultInstance(g_pLoggerRelease);
608#endif
609 /* Explicitly flush the log in case of VBOXTRAY_RELEASE_LOG=buffered. */
610 RTLogFlush(g_pLoggerRelease);
611 }
612 else
613 MessageBox(GetDesktopWindow(),
614 szError, "VBoxTray - Logging Error", MB_OK | MB_ICONERROR);
615
616 return rc;
617}
618
619static void vboxTrayLogDestroy(void)
620{
621 RTLogDestroy(RTLogRelSetDefaultInstance(NULL));
622}
623
624static void vboxTrayDestroyToolWindow(void)
625{
626 if (g_hwndToolWindow)
627 {
628 Log(("Destroying tool window ...\n"));
629
630 /* Destroy the tool window. */
631 DestroyWindow(g_hwndToolWindow);
632 g_hwndToolWindow = NULL;
633
634 UnregisterClass("VBoxTrayToolWndClass", g_hInstance);
635 }
636}
637
638static int vboxTrayCreateToolWindow(void)
639{
640 DWORD dwErr = ERROR_SUCCESS;
641
642 /* Create a custom window class. */
643 WNDCLASSEX wc = { 0 };
644 wc.cbSize = sizeof(WNDCLASSEX);
645 wc.style = CS_NOCLOSE;
646 wc.lpfnWndProc = (WNDPROC)vboxToolWndProc;
647 wc.hInstance = g_hInstance;
648 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
649 wc.lpszClassName = "VBoxTrayToolWndClass";
650
651 if (!RegisterClassEx(&wc))
652 {
653 dwErr = GetLastError();
654 Log(("Registering invisible tool window failed, error = %08X\n", dwErr));
655 }
656 else
657 {
658 /*
659 * Create our (invisible) tool window.
660 * Note: The window name ("VBoxTrayToolWnd") and class ("VBoxTrayToolWndClass") is
661 * needed for posting globally registered messages to VBoxTray and must not be
662 * changed! Otherwise things get broken!
663 *
664 */
665 g_hwndToolWindow = CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
666 "VBoxTrayToolWndClass", "VBoxTrayToolWnd",
667 WS_POPUPWINDOW,
668 -200, -200, 100, 100, NULL, NULL, g_hInstance, NULL);
669 if (!g_hwndToolWindow)
670 {
671 dwErr = GetLastError();
672 Log(("Creating invisible tool window failed, error = %08X\n", dwErr));
673 }
674 else
675 {
676 /* Reload the cursor(s). */
677 hlpReloadCursor();
678
679 Log(("Invisible tool window handle = %p\n", g_hwndToolWindow));
680 }
681 }
682
683 if (dwErr != ERROR_SUCCESS)
684 vboxTrayDestroyToolWindow();
685 return RTErrConvertFromWin32(dwErr);
686}
687
688static int vboxTraySetupSeamless(void)
689{
690 OSVERSIONINFO info;
691 g_dwMajorVersion = 5; /* Default to Windows XP. */
692 info.dwOSVersionInfoSize = sizeof(info);
693 if (GetVersionEx(&info))
694 {
695 Log(("Windows version %ld.%ld\n", info.dwMajorVersion, info.dwMinorVersion));
696 g_dwMajorVersion = info.dwMajorVersion;
697 }
698
699 /* We need to setup a security descriptor to allow other processes modify access to the seamless notification event semaphore. */
700 SECURITY_ATTRIBUTES SecAttr;
701 DWORD dwErr = ERROR_SUCCESS;
702 char secDesc[SECURITY_DESCRIPTOR_MIN_LENGTH];
703 BOOL fRC;
704
705 SecAttr.nLength = sizeof(SecAttr);
706 SecAttr.bInheritHandle = FALSE;
707 SecAttr.lpSecurityDescriptor = &secDesc;
708 InitializeSecurityDescriptor(SecAttr.lpSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
709 fRC = SetSecurityDescriptorDacl(SecAttr.lpSecurityDescriptor, TRUE, 0, FALSE);
710 if (!fRC)
711 {
712 dwErr = GetLastError();
713 Log(("SetSecurityDescriptorDacl failed with last error = %08X\n", dwErr));
714 }
715 else
716 {
717 /* For Vista and up we need to change the integrity of the security descriptor, too. */
718 if (g_dwMajorVersion >= 6)
719 {
720 BOOL (WINAPI * pfnConvertStringSecurityDescriptorToSecurityDescriptorA)(LPCSTR StringSecurityDescriptor, DWORD StringSDRevision, PSECURITY_DESCRIPTOR *SecurityDescriptor, PULONG SecurityDescriptorSize);
721 *(void **)&pfnConvertStringSecurityDescriptorToSecurityDescriptorA =
722 RTLdrGetSystemSymbol("advapi32.dll", "ConvertStringSecurityDescriptorToSecurityDescriptorA");
723 Log(("pfnConvertStringSecurityDescriptorToSecurityDescriptorA = %x\n", pfnConvertStringSecurityDescriptorToSecurityDescriptorA));
724 if (pfnConvertStringSecurityDescriptorToSecurityDescriptorA)
725 {
726 PSECURITY_DESCRIPTOR pSD;
727 PACL pSacl = NULL;
728 BOOL fSaclPresent = FALSE;
729 BOOL fSaclDefaulted = FALSE;
730
731 fRC = pfnConvertStringSecurityDescriptorToSecurityDescriptorA("S:(ML;;NW;;;LW)", /* this means "low integrity" */
732 SDDL_REVISION_1, &pSD, NULL);
733 if (!fRC)
734 {
735 dwErr = GetLastError();
736 Log(("ConvertStringSecurityDescriptorToSecurityDescriptorA failed with last error = %08X\n", dwErr));
737 }
738 else
739 {
740 fRC = GetSecurityDescriptorSacl(pSD, &fSaclPresent, &pSacl, &fSaclDefaulted);
741 if (!fRC)
742 {
743 dwErr = GetLastError();
744 Log(("GetSecurityDescriptorSacl failed with last error = %08X\n", dwErr));
745 }
746 else
747 {
748 fRC = SetSecurityDescriptorSacl(SecAttr.lpSecurityDescriptor, TRUE, pSacl, FALSE);
749 if (!fRC)
750 {
751 dwErr = GetLastError();
752 Log(("SetSecurityDescriptorSacl failed with last error = %08X\n", dwErr));
753 }
754 }
755 }
756 }
757 }
758
759 if ( dwErr == ERROR_SUCCESS
760 && g_dwMajorVersion >= 5) /* Only for W2K and up ... */
761 {
762 g_hSeamlessWtNotifyEvent = CreateEvent(&SecAttr, FALSE, FALSE, VBOXHOOK_GLOBAL_WT_EVENT_NAME);
763 if (g_hSeamlessWtNotifyEvent == NULL)
764 {
765 dwErr = GetLastError();
766 Log(("CreateEvent for Seamless failed, last error = %08X\n", dwErr));
767 }
768
769 g_hSeamlessKmNotifyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
770 if (g_hSeamlessKmNotifyEvent == NULL)
771 {
772 dwErr = GetLastError();
773 Log(("CreateEvent for Seamless failed, last error = %08X\n", dwErr));
774 }
775 }
776 }
777 return RTErrConvertFromWin32(dwErr);
778}
779
780static void vboxTrayShutdownSeamless(void)
781{
782 if (g_hSeamlessWtNotifyEvent)
783 {
784 CloseHandle(g_hSeamlessWtNotifyEvent);
785 g_hSeamlessWtNotifyEvent = NULL;
786 }
787
788 if (g_hSeamlessKmNotifyEvent)
789 {
790 CloseHandle(g_hSeamlessKmNotifyEvent);
791 g_hSeamlessKmNotifyEvent = NULL;
792 }
793}
794
795static void VBoxTrayCheckDt()
796{
797 BOOL fOldAllowedState = VBoxConsoleIsAllowed();
798 if (vboxDtHandleEvent())
799 {
800 if (!VBoxConsoleIsAllowed() != !fOldAllowedState)
801 VBoxConsoleEnable(!fOldAllowedState);
802 }
803}
804
805static int vboxTrayServiceMain(void)
806{
807 int rc = VINF_SUCCESS;
808 LogFunc(("Entering vboxTrayServiceMain\n"));
809
810 g_hStopSem = CreateEvent(NULL, TRUE, FALSE, NULL);
811 if (g_hStopSem == NULL)
812 {
813 rc = RTErrConvertFromWin32(GetLastError());
814 LogFunc(("CreateEvent for stopping VBoxTray failed, rc=%Rrc\n", rc));
815 }
816 else
817 {
818 /*
819 * Start services listed in the vboxServiceTable.
820 */
821 VBOXSERVICEENV svcEnv;
822 svcEnv.hInstance = g_hInstance;
823 svcEnv.hDriver = g_hVBoxDriver;
824
825 /* Initializes disp-if to default (XPDM) mode. */
826 VBoxDispIfInit(&svcEnv.dispIf); /* Cannot fail atm. */
827 #ifdef VBOX_WITH_WDDM
828 /*
829 * For now the display mode will be adjusted to WDDM mode if needed
830 * on display service initialization when it detects the display driver type.
831 */
832 #endif
833
834 /* Finally start all the built-in services! */
835 rc = vboxTrayServicesStart(&svcEnv);
836 if (RT_FAILURE(rc))
837 {
838 /* Terminate service if something went wrong. */
839 vboxTrayServicesStop(&svcEnv);
840 }
841 else
842 {
843 rc = vboxTrayCreateTrayIcon();
844 if ( RT_SUCCESS(rc)
845 && g_dwMajorVersion >= 5) /* Only for W2K and up ... */
846 {
847 /* We're ready to create the tooltip balloon.
848 Check in 10 seconds (@todo make seconds configurable) ... */
849 SetTimer(g_hwndToolWindow,
850 TIMERID_VBOXTRAY_CHECK_HOSTVERSION,
851 10 * 1000, /* 10 seconds */
852 NULL /* No timerproc */);
853 }
854
855 if (RT_SUCCESS(rc))
856 {
857 /* Do the Shared Folders auto-mounting stuff. */
858 rc = VBoxSharedFoldersAutoMount();
859 if (RT_SUCCESS(rc))
860 {
861 /* Report the host that we're up and running! */
862 hlpReportStatus(VBoxGuestFacilityStatus_Active);
863 }
864 }
865
866 if (RT_SUCCESS(rc))
867 {
868 /* Boost thread priority to make sure we wake up early for seamless window notifications
869 * (not sure if it actually makes any difference though). */
870 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
871
872 /*
873 * Main execution loop
874 * Wait for the stop semaphore to be posted or a window event to arrive
875 */
876
877 HANDLE hWaitEvent[4] = {0};
878 DWORD dwEventCount = 0;
879
880 hWaitEvent[dwEventCount++] = g_hStopSem;
881
882 /* Check if seamless mode is not active and add seamless event to the list */
883 if (0 != g_hSeamlessWtNotifyEvent)
884 {
885 hWaitEvent[dwEventCount++] = g_hSeamlessWtNotifyEvent;
886 }
887
888 if (0 != g_hSeamlessKmNotifyEvent)
889 {
890 hWaitEvent[dwEventCount++] = g_hSeamlessKmNotifyEvent;
891 }
892
893 if (0 != vboxDtGetNotifyEvent())
894 {
895 hWaitEvent[dwEventCount++] = vboxDtGetNotifyEvent();
896 }
897
898 LogFlowFunc(("Number of events to wait in main loop: %ld\n", dwEventCount));
899 while (true)
900 {
901 DWORD waitResult = MsgWaitForMultipleObjectsEx(dwEventCount, hWaitEvent, 500, QS_ALLINPUT, 0);
902 waitResult = waitResult - WAIT_OBJECT_0;
903
904 /* Only enable for message debugging, lots of traffic! */
905 //Log(("Wait result = %ld\n", waitResult));
906
907 if (waitResult == 0)
908 {
909 LogFunc(("Event 'Exit' triggered\n"));
910 /* exit */
911 break;
912 }
913 else
914 {
915 BOOL fHandled = FALSE;
916 if (waitResult < RT_ELEMENTS(hWaitEvent))
917 {
918 if (hWaitEvent[waitResult])
919 {
920 if (hWaitEvent[waitResult] == g_hSeamlessWtNotifyEvent)
921 {
922 LogFunc(("Event 'Seamless' triggered\n"));
923
924 /* seamless window notification */
925 VBoxSeamlessCheckWindows(false);
926 fHandled = TRUE;
927 }
928 else if (hWaitEvent[waitResult] == g_hSeamlessKmNotifyEvent)
929 {
930 LogFunc(("Event 'Km Seamless' triggered\n"));
931
932 /* seamless window notification */
933 VBoxSeamlessCheckWindows(true);
934 fHandled = TRUE;
935 }
936 else if (hWaitEvent[waitResult] == vboxDtGetNotifyEvent())
937 {
938 LogFunc(("Event 'Dt' triggered\n"));
939 VBoxTrayCheckDt();
940 fHandled = TRUE;
941 }
942 }
943 }
944
945 if (!fHandled)
946 {
947 /* timeout or a window message, handle it */
948 MSG msg;
949 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
950 {
951#ifdef DEBUG_andy
952 LogFlowFunc(("PeekMessage %u\n", msg.message));
953#endif
954 if (msg.message == WM_QUIT)
955 {
956 LogFunc(("Terminating ...\n"));
957 SetEvent(g_hStopSem);
958 }
959 TranslateMessage(&msg);
960 DispatchMessage(&msg);
961 }
962 }
963 }
964 }
965 LogFunc(("Returned from main loop, exiting ...\n"));
966 }
967 LogFunc(("Waiting for services to stop ...\n"));
968 vboxTrayServicesStop(&svcEnv);
969 } /* Services started */
970 CloseHandle(g_hStopSem);
971 } /* Stop event created */
972
973 vboxTrayRemoveTrayIcon();
974
975 LogFunc(("Leaving with rc=%Rrc\n", rc));
976 return rc;
977}
978
979/**
980 * Main function
981 */
982int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
983{
984 RT_NOREF(hPrevInstance, lpCmdLine, nCmdShow);
985
986 /* Note: Do not use a global namespace ("Global\\") for mutex name here,
987 * will blow up NT4 compatibility! */
988 HANDLE hMutexAppRunning = CreateMutex(NULL, FALSE, "VBoxTray");
989 if ( hMutexAppRunning != NULL
990 && GetLastError() == ERROR_ALREADY_EXISTS)
991 {
992 /* VBoxTray already running? Bail out. */
993 CloseHandle (hMutexAppRunning);
994 hMutexAppRunning = NULL;
995 return 0;
996 }
997
998 LogRel(("%s r%s\n", RTBldCfgVersion(), RTBldCfgRevisionStr()));
999
1000 int rc = RTR3InitExeNoArguments(0);
1001 if (RT_SUCCESS(rc))
1002 rc = vboxTrayLogCreate(NULL /* pszLogFile */);
1003
1004 if (RT_SUCCESS(rc))
1005 {
1006 rc = VbglR3Init();
1007 if (RT_SUCCESS(rc))
1008 rc = vboxTrayOpenBaseDriver();
1009 }
1010
1011 if (RT_SUCCESS(rc))
1012 {
1013 /* Save instance handle. */
1014 g_hInstance = hInstance;
1015
1016 hlpReportStatus(VBoxGuestFacilityStatus_Init);
1017 rc = vboxTrayCreateToolWindow();
1018 if (RT_SUCCESS(rc))
1019 {
1020 VBoxCapsInit();
1021
1022 rc = vboxStInit(g_hwndToolWindow);
1023 if (!RT_SUCCESS(rc))
1024 {
1025 LogFlowFunc(("vboxStInit failed, rc=%Rrc\n", rc));
1026 /* ignore the St Init failure. this can happen for < XP win that do not support WTS API
1027 * in that case the session is treated as active connected to the physical console
1028 * (i.e. fallback to the old behavior that was before introduction of VBoxSt) */
1029 Assert(vboxStIsActiveConsole());
1030 }
1031
1032 rc = vboxDtInit();
1033 if (!RT_SUCCESS(rc))
1034 {
1035 LogFlowFunc(("vboxDtInit failed, rc=%Rrc\n", rc));
1036 /* ignore the Dt Init failure. this can happen for < XP win that do not support WTS API
1037 * in that case the session is treated as active connected to the physical console
1038 * (i.e. fallback to the old behavior that was before introduction of VBoxSt) */
1039 Assert(vboxDtIsInputDesktop());
1040 }
1041
1042 rc = VBoxAcquireGuestCaps(VMMDEV_GUEST_SUPPORTS_SEAMLESS | VMMDEV_GUEST_SUPPORTS_GRAPHICS, 0, true);
1043 if (!RT_SUCCESS(rc))
1044 LogFlowFunc(("VBoxAcquireGuestCaps failed with rc=%Rrc, ignoring ...\n", rc));
1045
1046 rc = vboxTraySetupSeamless(); /** @todo r=andy Do we really want to be this critical for the whole application? */
1047 if (RT_SUCCESS(rc))
1048 {
1049 rc = vboxTrayServiceMain();
1050 if (RT_SUCCESS(rc))
1051 hlpReportStatus(VBoxGuestFacilityStatus_Terminating);
1052 vboxTrayShutdownSeamless();
1053 }
1054
1055 /* it should be safe to call vboxDtTerm even if vboxStInit above failed */
1056 vboxDtTerm();
1057
1058 /* it should be safe to call vboxStTerm even if vboxStInit above failed */
1059 vboxStTerm();
1060
1061 VBoxCapsTerm();
1062
1063 vboxTrayDestroyToolWindow();
1064 }
1065 if (RT_SUCCESS(rc))
1066 hlpReportStatus(VBoxGuestFacilityStatus_Terminated);
1067 }
1068
1069 if (RT_FAILURE(rc))
1070 {
1071 LogRel(("Error while starting, rc=%Rrc\n", rc));
1072 hlpReportStatus(VBoxGuestFacilityStatus_Failed);
1073 }
1074 LogRel(("Ended\n"));
1075 vboxTrayCloseBaseDriver();
1076
1077 /* Release instance mutex. */
1078 if (hMutexAppRunning != NULL)
1079 {
1080 CloseHandle(hMutexAppRunning);
1081 hMutexAppRunning = NULL;
1082 }
1083
1084 VbglR3Term();
1085
1086 vboxTrayLogDestroy();
1087
1088 return RT_SUCCESS(rc) ? 0 : 1;
1089}
1090
1091/**
1092 * Window procedure for our main tool window.
1093 */
1094static LRESULT CALLBACK vboxToolWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1095{
1096 LogFlowFunc(("hWnd=%p, uMsg=%u\n", hWnd, uMsg));
1097
1098 switch (uMsg)
1099 {
1100 case WM_CREATE:
1101 {
1102 LogFunc(("Tool window created\n"));
1103
1104 int rc = vboxTrayRegisterGlobalMessages(&g_vboxGlobalMessageTable[0]);
1105 if (RT_FAILURE(rc))
1106 LogFunc(("Error registering global window messages, rc=%Rrc\n", rc));
1107 return 0;
1108 }
1109
1110 case WM_CLOSE:
1111 return 0;
1112
1113 case WM_DESTROY:
1114 {
1115 LogFunc(("Tool window destroyed\n"));
1116 KillTimer(g_hwndToolWindow, TIMERID_VBOXTRAY_CHECK_HOSTVERSION);
1117 return 0;
1118 }
1119
1120 case WM_TIMER:
1121 {
1122 if (VBoxCapsCheckTimer(wParam))
1123 return 0;
1124 if (vboxDtCheckTimer(wParam))
1125 return 0;
1126 if (vboxStCheckTimer(wParam))
1127 return 0;
1128
1129 switch (wParam)
1130 {
1131 case TIMERID_VBOXTRAY_CHECK_HOSTVERSION:
1132 if (RT_SUCCESS(VBoxCheckHostVersion()))
1133 {
1134 /* After successful run we don't need to check again. */
1135 KillTimer(g_hwndToolWindow, TIMERID_VBOXTRAY_CHECK_HOSTVERSION);
1136 }
1137 return 0;
1138
1139 default:
1140 break;
1141 }
1142
1143 break; /* Make sure other timers get processed the usual way! */
1144 }
1145
1146 case WM_VBOXTRAY_TRAY_ICON:
1147 {
1148 switch (lParam)
1149 {
1150 case WM_LBUTTONDBLCLK:
1151 break;
1152
1153 case WM_RBUTTONDOWN:
1154 break;
1155 }
1156 return 0;
1157 }
1158
1159 case WM_VBOX_SEAMLESS_ENABLE:
1160 {
1161 VBoxCapsEntryFuncStateSet(VBOXCAPS_ENTRY_IDX_SEAMLESS, VBOXCAPS_ENTRY_FUNCSTATE_STARTED);
1162 return 0;
1163 }
1164
1165 case WM_VBOX_SEAMLESS_DISABLE:
1166 {
1167 VBoxCapsEntryFuncStateSet(VBOXCAPS_ENTRY_IDX_SEAMLESS, VBOXCAPS_ENTRY_FUNCSTATE_SUPPORTED);
1168 return 0;
1169 }
1170
1171 case WM_DISPLAYCHANGE:
1172 ASMAtomicUoWriteU32(&g_fGuestDisplaysChanged, 1);
1173 // No break or return is intentional here.
1174 case WM_VBOX_SEAMLESS_UPDATE:
1175 {
1176 if (VBoxCapsEntryIsEnabled(VBOXCAPS_ENTRY_IDX_SEAMLESS))
1177 VBoxSeamlessCheckWindows(true);
1178 return 0;
1179 }
1180
1181 case WM_VBOX_GRAPHICS_SUPPORTED:
1182 {
1183 VBoxGrapicsSetSupported(TRUE);
1184 return 0;
1185 }
1186
1187 case WM_VBOX_GRAPHICS_UNSUPPORTED:
1188 {
1189 VBoxGrapicsSetSupported(FALSE);
1190 return 0;
1191 }
1192
1193 case WM_WTSSESSION_CHANGE:
1194 {
1195 BOOL fOldAllowedState = VBoxConsoleIsAllowed();
1196 if (vboxStHandleEvent(wParam))
1197 {
1198 if (!VBoxConsoleIsAllowed() != !fOldAllowedState)
1199 VBoxConsoleEnable(!fOldAllowedState);
1200 }
1201 return 0;
1202 }
1203
1204 default:
1205 {
1206 /* Handle all globally registered window messages. */
1207 if (vboxTrayHandleGlobalMessages(&g_vboxGlobalMessageTable[0], uMsg,
1208 wParam, lParam))
1209 {
1210 return 0; /* We handled the message. @todo Add return value!*/
1211 }
1212 break; /* We did not handle the message, dispatch to DefWndProc. */
1213 }
1214 }
1215
1216 /* Only if message was *not* handled by our switch above, dispatch to DefWindowProc. */
1217 return DefWindowProc(hWnd, uMsg, wParam, lParam);
1218}
1219
1220/* St (session [state] tracking) functionality API impl */
1221
1222typedef struct VBOXST
1223{
1224 HWND hWTSAPIWnd;
1225 RTLDRMOD hLdrModWTSAPI32;
1226 BOOL fIsConsole;
1227 WTS_CONNECTSTATE_CLASS enmConnectState;
1228 UINT_PTR idDelayedInitTimer;
1229 BOOL (WINAPI * pfnWTSRegisterSessionNotification)(HWND hWnd, DWORD dwFlags);
1230 BOOL (WINAPI * pfnWTSUnRegisterSessionNotification)(HWND hWnd);
1231 BOOL (WINAPI * pfnWTSQuerySessionInformationA)(HANDLE hServer, DWORD SessionId, WTS_INFO_CLASS WTSInfoClass, LPTSTR *ppBuffer, DWORD *pBytesReturned);
1232} VBOXST;
1233
1234static VBOXST gVBoxSt;
1235
1236static int vboxStCheckState()
1237{
1238 int rc = VINF_SUCCESS;
1239 WTS_CONNECTSTATE_CLASS *penmConnectState = NULL;
1240 USHORT *pProtocolType = NULL;
1241 DWORD cbBuf = 0;
1242 if (gVBoxSt.pfnWTSQuerySessionInformationA(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSConnectState,
1243 (LPTSTR *)&penmConnectState, &cbBuf))
1244 {
1245 if (gVBoxSt.pfnWTSQuerySessionInformationA(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSClientProtocolType,
1246 (LPTSTR *)&pProtocolType, &cbBuf))
1247 {
1248 gVBoxSt.fIsConsole = (*pProtocolType == 0);
1249 gVBoxSt.enmConnectState = *penmConnectState;
1250 return VINF_SUCCESS;
1251 }
1252
1253 DWORD dwErr = GetLastError();
1254 LogFlowFunc(("WTSQuerySessionInformationA WTSClientProtocolType failed, error = %08X\n", dwErr));
1255 rc = RTErrConvertFromWin32(dwErr);
1256 }
1257 else
1258 {
1259 DWORD dwErr = GetLastError();
1260 LogFlowFunc(("WTSQuerySessionInformationA WTSConnectState failed, error = %08X\n", dwErr));
1261 rc = RTErrConvertFromWin32(dwErr);
1262 }
1263
1264 /* failure branch, set to "console-active" state */
1265 gVBoxSt.fIsConsole = TRUE;
1266 gVBoxSt.enmConnectState = WTSActive;
1267
1268 return rc;
1269}
1270
1271static int vboxStInit(HWND hWnd)
1272{
1273 RT_ZERO(gVBoxSt);
1274 int rc = RTLdrLoadSystem("WTSAPI32.DLL", false /*fNoUnload*/, &gVBoxSt.hLdrModWTSAPI32);
1275 if (RT_SUCCESS(rc))
1276 {
1277 rc = RTLdrGetSymbol(gVBoxSt.hLdrModWTSAPI32, "WTSRegisterSessionNotification",
1278 (void **)&gVBoxSt.pfnWTSRegisterSessionNotification);
1279 if (RT_SUCCESS(rc))
1280 {
1281 rc = RTLdrGetSymbol(gVBoxSt.hLdrModWTSAPI32, "WTSUnRegisterSessionNotification",
1282 (void **)&gVBoxSt.pfnWTSUnRegisterSessionNotification);
1283 if (RT_SUCCESS(rc))
1284 {
1285 rc = RTLdrGetSymbol(gVBoxSt.hLdrModWTSAPI32, "WTSQuerySessionInformationA",
1286 (void **)&gVBoxSt.pfnWTSQuerySessionInformationA);
1287 if (RT_FAILURE(rc))
1288 LogFlowFunc(("WTSQuerySessionInformationA not found\n"));
1289 }
1290 else
1291 LogFlowFunc(("WTSUnRegisterSessionNotification not found\n"));
1292 }
1293 else
1294 LogFlowFunc(("WTSRegisterSessionNotification not found\n"));
1295 if (RT_SUCCESS(rc))
1296 {
1297 gVBoxSt.hWTSAPIWnd = hWnd;
1298 if (gVBoxSt.pfnWTSRegisterSessionNotification(gVBoxSt.hWTSAPIWnd, NOTIFY_FOR_THIS_SESSION))
1299 vboxStCheckState();
1300 else
1301 {
1302 DWORD dwErr = GetLastError();
1303 LogFlowFunc(("WTSRegisterSessionNotification failed, error = %08X\n", dwErr));
1304 if (dwErr == RPC_S_INVALID_BINDING)
1305 {
1306 gVBoxSt.idDelayedInitTimer = SetTimer(gVBoxSt.hWTSAPIWnd, TIMERID_VBOXTRAY_ST_DELAYED_INIT_TIMER,
1307 2000, (TIMERPROC)NULL);
1308 gVBoxSt.fIsConsole = TRUE;
1309 gVBoxSt.enmConnectState = WTSActive;
1310 rc = VINF_SUCCESS;
1311 }
1312 else
1313 rc = RTErrConvertFromWin32(dwErr);
1314 }
1315
1316 if (RT_SUCCESS(rc))
1317 return VINF_SUCCESS;
1318 }
1319
1320 RTLdrClose(gVBoxSt.hLdrModWTSAPI32);
1321 }
1322 else
1323 LogFlowFunc(("WTSAPI32 load failed, rc = %Rrc\n", rc));
1324
1325 RT_ZERO(gVBoxSt);
1326 gVBoxSt.fIsConsole = TRUE;
1327 gVBoxSt.enmConnectState = WTSActive;
1328 return rc;
1329}
1330
1331static void vboxStTerm(void)
1332{
1333 if (!gVBoxSt.hWTSAPIWnd)
1334 {
1335 LogFlowFunc(("vboxStTerm called for non-initialized St\n"));
1336 return;
1337 }
1338
1339 if (gVBoxSt.idDelayedInitTimer)
1340 {
1341 /* notification is not registered, just kill timer */
1342 KillTimer(gVBoxSt.hWTSAPIWnd, gVBoxSt.idDelayedInitTimer);
1343 gVBoxSt.idDelayedInitTimer = 0;
1344 }
1345 else
1346 {
1347 if (!gVBoxSt.pfnWTSUnRegisterSessionNotification(gVBoxSt.hWTSAPIWnd))
1348 {
1349 LogFlowFunc(("WTSAPI32 load failed, error = %08X\n", GetLastError()));
1350 }
1351 }
1352
1353 RTLdrClose(gVBoxSt.hLdrModWTSAPI32);
1354 RT_ZERO(gVBoxSt);
1355}
1356
1357#define VBOXST_DBG_MAKECASE(_val) case _val: return #_val;
1358
1359static const char* vboxStDbgGetString(DWORD val)
1360{
1361 switch (val)
1362 {
1363 VBOXST_DBG_MAKECASE(WTS_CONSOLE_CONNECT);
1364 VBOXST_DBG_MAKECASE(WTS_CONSOLE_DISCONNECT);
1365 VBOXST_DBG_MAKECASE(WTS_REMOTE_CONNECT);
1366 VBOXST_DBG_MAKECASE(WTS_REMOTE_DISCONNECT);
1367 VBOXST_DBG_MAKECASE(WTS_SESSION_LOGON);
1368 VBOXST_DBG_MAKECASE(WTS_SESSION_LOGOFF);
1369 VBOXST_DBG_MAKECASE(WTS_SESSION_LOCK);
1370 VBOXST_DBG_MAKECASE(WTS_SESSION_UNLOCK);
1371 VBOXST_DBG_MAKECASE(WTS_SESSION_REMOTE_CONTROL);
1372 default:
1373 LogFlowFunc(("invalid WTS state %d\n", val));
1374 return "Unknown";
1375 }
1376}
1377
1378static BOOL vboxStCheckTimer(WPARAM wEvent)
1379{
1380 if (wEvent != gVBoxSt.idDelayedInitTimer)
1381 return FALSE;
1382
1383 if (gVBoxSt.pfnWTSRegisterSessionNotification(gVBoxSt.hWTSAPIWnd, NOTIFY_FOR_THIS_SESSION))
1384 {
1385 KillTimer(gVBoxSt.hWTSAPIWnd, gVBoxSt.idDelayedInitTimer);
1386 gVBoxSt.idDelayedInitTimer = 0;
1387 vboxStCheckState();
1388 }
1389 else
1390 {
1391 LogFlowFunc(("timer WTSRegisterSessionNotification failed, error = %08X\n", GetLastError()));
1392 Assert(gVBoxSt.fIsConsole == TRUE);
1393 Assert(gVBoxSt.enmConnectState == WTSActive);
1394 }
1395
1396 return TRUE;
1397}
1398
1399
1400static BOOL vboxStHandleEvent(WPARAM wEvent)
1401{
1402 RT_NOREF(wEvent);
1403 LogFlowFunc(("WTS Event: %s\n", vboxStDbgGetString(wEvent)));
1404 BOOL fOldIsActiveConsole = vboxStIsActiveConsole();
1405
1406 vboxStCheckState();
1407
1408 return !vboxStIsActiveConsole() != !fOldIsActiveConsole;
1409}
1410
1411static BOOL vboxStIsActiveConsole(void)
1412{
1413 return (gVBoxSt.enmConnectState == WTSActive && gVBoxSt.fIsConsole);
1414}
1415
1416/*
1417 * Dt (desktop [state] tracking) functionality API impl
1418 *
1419 * !!!NOTE: this API is NOT thread-safe!!!
1420 * */
1421
1422typedef struct VBOXDT
1423{
1424 HANDLE hNotifyEvent;
1425 BOOL fIsInputDesktop;
1426 UINT_PTR idTimer;
1427 RTLDRMOD hLdrModHook;
1428 BOOL (* pfnVBoxHookInstallActiveDesktopTracker)(HMODULE hDll);
1429 BOOL (* pfnVBoxHookRemoveActiveDesktopTracker)();
1430 HDESK (WINAPI * pfnGetThreadDesktop)(DWORD dwThreadId);
1431 HDESK (WINAPI * pfnOpenInputDesktop)(DWORD dwFlags, BOOL fInherit, ACCESS_MASK dwDesiredAccess);
1432 BOOL (WINAPI * pfnCloseDesktop)(HDESK hDesktop);
1433} VBOXDT;
1434
1435static VBOXDT gVBoxDt;
1436
1437static BOOL vboxDtCalculateIsInputDesktop()
1438{
1439 BOOL fIsInputDt = FALSE;
1440 HDESK hInput = gVBoxDt.pfnOpenInputDesktop(0, FALSE, DESKTOP_CREATEWINDOW);
1441 if (hInput)
1442 {
1443// DWORD dwThreadId = GetCurrentThreadId();
1444// HDESK hThreadDt = gVBoxDt.pfnGetThreadDesktop(dwThreadId);
1445// if (hThreadDt)
1446// {
1447 fIsInputDt = TRUE;
1448// }
1449// else
1450// {
1451// DWORD dwErr = GetLastError();
1452// LogFlowFunc(("pfnGetThreadDesktop for Seamless failed, last error = %08X\n", dwErr));
1453// }
1454
1455 gVBoxDt.pfnCloseDesktop(hInput);
1456 }
1457 else
1458 {
1459// DWORD dwErr = GetLastError();
1460// LogFlowFunc(("pfnOpenInputDesktop for Seamless failed, last error = %08X\n", dwErr));
1461 }
1462 return fIsInputDt;
1463}
1464
1465static BOOL vboxDtCheckTimer(WPARAM wParam)
1466{
1467 if (wParam != gVBoxDt.idTimer)
1468 return FALSE;
1469
1470 VBoxTrayCheckDt();
1471
1472 return TRUE;
1473}
1474
1475static int vboxDtInit()
1476{
1477 int rc = VINF_SUCCESS;
1478 OSVERSIONINFO info;
1479 g_dwMajorVersion = 5; /* Default to Windows XP. */
1480 info.dwOSVersionInfoSize = sizeof(info);
1481 if (GetVersionEx(&info))
1482 {
1483 LogRel(("Windows version %ld.%ld\n", info.dwMajorVersion, info.dwMinorVersion));
1484 g_dwMajorVersion = info.dwMajorVersion;
1485 }
1486
1487 RT_ZERO(gVBoxDt);
1488
1489 gVBoxDt.hNotifyEvent = CreateEvent(NULL, FALSE, FALSE, VBOXHOOK_GLOBAL_DT_EVENT_NAME);
1490 if (gVBoxDt.hNotifyEvent != NULL)
1491 {
1492 /* Load the hook dll and resolve the necessary entry points. */
1493 rc = RTLdrLoadAppPriv(VBOXHOOK_DLL_NAME, &gVBoxDt.hLdrModHook);
1494 if (RT_SUCCESS(rc))
1495 {
1496 rc = RTLdrGetSymbol(gVBoxDt.hLdrModHook, "VBoxHookInstallActiveDesktopTracker",
1497 (void **)&gVBoxDt.pfnVBoxHookInstallActiveDesktopTracker);
1498 if (RT_SUCCESS(rc))
1499 {
1500 rc = RTLdrGetSymbol(gVBoxDt.hLdrModHook, "VBoxHookRemoveActiveDesktopTracker",
1501 (void **)&gVBoxDt.pfnVBoxHookRemoveActiveDesktopTracker);
1502 if (RT_FAILURE(rc))
1503 LogFlowFunc(("VBoxHookRemoveActiveDesktopTracker not found\n"));
1504 }
1505 else
1506 LogFlowFunc(("VBoxHookInstallActiveDesktopTracker not found\n"));
1507 if (RT_SUCCESS(rc))
1508 {
1509 /* Try get the system APIs we need. */
1510 *(void **)&gVBoxDt.pfnGetThreadDesktop = RTLdrGetSystemSymbol("user32.dll", "GetThreadDesktop");
1511 if (!gVBoxDt.pfnGetThreadDesktop)
1512 {
1513 LogFlowFunc(("GetThreadDesktop not found\n"));
1514 rc = VERR_NOT_SUPPORTED;
1515 }
1516
1517 *(void **)&gVBoxDt.pfnOpenInputDesktop = RTLdrGetSystemSymbol("user32.dll", "OpenInputDesktop");
1518 if (!gVBoxDt.pfnOpenInputDesktop)
1519 {
1520 LogFlowFunc(("OpenInputDesktop not found\n"));
1521 rc = VERR_NOT_SUPPORTED;
1522 }
1523
1524 *(void **)&gVBoxDt.pfnCloseDesktop = RTLdrGetSystemSymbol("user32.dll", "CloseDesktop");
1525 if (!gVBoxDt.pfnCloseDesktop)
1526 {
1527 LogFlowFunc(("CloseDesktop not found\n"));
1528 rc = VERR_NOT_SUPPORTED;
1529 }
1530
1531 if (RT_SUCCESS(rc))
1532 {
1533 BOOL fRc = FALSE;
1534 /* For Vista and up we need to change the integrity of the security descriptor, too. */
1535 if (g_dwMajorVersion >= 6)
1536 {
1537 HMODULE hModHook = (HMODULE)RTLdrGetNativeHandle(gVBoxDt.hLdrModHook);
1538 Assert((uintptr_t)hModHook != ~(uintptr_t)0);
1539 fRc = gVBoxDt.pfnVBoxHookInstallActiveDesktopTracker(hModHook);
1540 if (!fRc)
1541 LogFlowFunc(("pfnVBoxHookInstallActiveDesktopTracker failed, last error = %08X\n", GetLastError()));
1542 }
1543
1544 if (!fRc)
1545 {
1546 gVBoxDt.idTimer = SetTimer(g_hwndToolWindow, TIMERID_VBOXTRAY_DT_TIMER, 500, (TIMERPROC)NULL);
1547 if (!gVBoxDt.idTimer)
1548 {
1549 DWORD dwErr = GetLastError();
1550 LogFlowFunc(("SetTimer error %08X\n", dwErr));
1551 rc = RTErrConvertFromWin32(dwErr);
1552 }
1553 }
1554
1555 if (RT_SUCCESS(rc))
1556 {
1557 gVBoxDt.fIsInputDesktop = vboxDtCalculateIsInputDesktop();
1558 return VINF_SUCCESS;
1559 }
1560 }
1561 }
1562
1563 RTLdrClose(gVBoxDt.hLdrModHook);
1564 }
1565 else
1566 {
1567 DWORD dwErr = GetLastError();
1568 LogFlowFunc(("CreateEvent for Seamless failed, last error = %08X\n", dwErr));
1569 rc = RTErrConvertFromWin32(dwErr);
1570 }
1571
1572 CloseHandle(gVBoxDt.hNotifyEvent);
1573 }
1574 else
1575 {
1576 DWORD dwErr = GetLastError();
1577 LogFlowFunc(("CreateEvent for Seamless failed, last error = %08X\n", dwErr));
1578 rc = RTErrConvertFromWin32(dwErr);
1579 }
1580
1581
1582 RT_ZERO(gVBoxDt);
1583 gVBoxDt.fIsInputDesktop = TRUE;
1584
1585 return rc;
1586}
1587
1588static void vboxDtTerm()
1589{
1590 if (!gVBoxDt.hLdrModHook)
1591 return;
1592
1593 gVBoxDt.pfnVBoxHookRemoveActiveDesktopTracker();
1594
1595 RTLdrClose(gVBoxDt.hLdrModHook);
1596 CloseHandle(gVBoxDt.hNotifyEvent);
1597
1598 RT_ZERO(gVBoxDt);
1599}
1600/* @returns true on "IsInputDesktop" state change */
1601static BOOL vboxDtHandleEvent()
1602{
1603 BOOL fIsInputDesktop = gVBoxDt.fIsInputDesktop;
1604 gVBoxDt.fIsInputDesktop = vboxDtCalculateIsInputDesktop();
1605 return !fIsInputDesktop != !gVBoxDt.fIsInputDesktop;
1606}
1607
1608static HANDLE vboxDtGetNotifyEvent()
1609{
1610 return gVBoxDt.hNotifyEvent;
1611}
1612
1613/* @returns true iff the application (VBoxTray) desktop is input */
1614static BOOL vboxDtIsInputDesktop()
1615{
1616 return gVBoxDt.fIsInputDesktop;
1617}
1618
1619/* we need to perform Acquire/Release using the file handled we use for rewuesting events from VBoxGuest
1620 * otherwise Acquisition mechanism will treat us as different client and will not propagate necessary requests
1621 * */
1622static int VBoxAcquireGuestCaps(uint32_t fOr, uint32_t fNot, bool fCfg)
1623{
1624 DWORD cbReturned = 0;
1625 VBoxGuestCapsAquire Info;
1626 Log(("VBoxAcquireGuestCaps or(0x%x), not(0x%x), cfx(%d)\n", fOr, fNot, fCfg));
1627 Info.enmFlags = fCfg ? VBOXGUESTCAPSACQUIRE_FLAGS_CONFIG_ACQUIRE_MODE : VBOXGUESTCAPSACQUIRE_FLAGS_NONE;
1628 Info.rc = VERR_NOT_IMPLEMENTED;
1629 Info.u32OrMask = fOr;
1630 Info.u32NotMask = fNot;
1631 if (!DeviceIoControl(g_hVBoxDriver, VBOXGUEST_IOCTL_GUEST_CAPS_ACQUIRE, &Info, sizeof(Info), &Info, sizeof(Info), &cbReturned, NULL))
1632 {
1633 DWORD LastErr = GetLastError();
1634 LogFlowFunc(("DeviceIoControl VBOXGUEST_IOCTL_GUEST_CAPS_ACQUIRE failed LastErr %d\n", LastErr));
1635 return RTErrConvertFromWin32(LastErr);
1636 }
1637
1638 int rc = Info.rc;
1639 if (!RT_SUCCESS(rc))
1640 {
1641 LogFlowFunc(("VBOXGUEST_IOCTL_GUEST_CAPS_ACQUIRE failed rc %d\n", rc));
1642 return rc;
1643 }
1644
1645 return rc;
1646}
1647
1648typedef enum VBOXCAPS_ENTRY_ACSTATE
1649{
1650 /* the given cap is released */
1651 VBOXCAPS_ENTRY_ACSTATE_RELEASED = 0,
1652 /* the given cap acquisition is in progress */
1653 VBOXCAPS_ENTRY_ACSTATE_ACQUIRING,
1654 /* the given cap is acquired */
1655 VBOXCAPS_ENTRY_ACSTATE_ACQUIRED
1656} VBOXCAPS_ENTRY_ACSTATE;
1657
1658
1659struct VBOXCAPS_ENTRY;
1660struct VBOXCAPS;
1661
1662typedef DECLCALLBACKPTR(void, PFNVBOXCAPS_ENTRY_ON_ENABLE)(struct VBOXCAPS *pConsole, struct VBOXCAPS_ENTRY *pCap, BOOL fEnabled);
1663
1664typedef struct VBOXCAPS_ENTRY
1665{
1666 uint32_t fCap;
1667 uint32_t iCap;
1668 VBOXCAPS_ENTRY_FUNCSTATE enmFuncState;
1669 VBOXCAPS_ENTRY_ACSTATE enmAcState;
1670 PFNVBOXCAPS_ENTRY_ON_ENABLE pfnOnEnable;
1671} VBOXCAPS_ENTRY;
1672
1673
1674typedef struct VBOXCAPS
1675{
1676 UINT_PTR idTimer;
1677 VBOXCAPS_ENTRY aCaps[VBOXCAPS_ENTRY_IDX_COUNT];
1678} VBOXCAPS;
1679
1680static VBOXCAPS gVBoxCaps;
1681
1682static DECLCALLBACK(void) vboxCapsOnEnableSeamless(struct VBOXCAPS *pConsole, struct VBOXCAPS_ENTRY *pCap, BOOL fEnabled)
1683{
1684 RT_NOREF(pConsole, pCap);
1685 if (fEnabled)
1686 {
1687 Log(("vboxCapsOnEnableSeamless: ENABLED\n"));
1688 Assert(pCap->enmAcState == VBOXCAPS_ENTRY_ACSTATE_ACQUIRED);
1689 Assert(pCap->enmFuncState == VBOXCAPS_ENTRY_FUNCSTATE_STARTED);
1690 VBoxSeamlessEnable();
1691 }
1692 else
1693 {
1694 Log(("vboxCapsOnEnableSeamless: DISABLED\n"));
1695 Assert(pCap->enmAcState != VBOXCAPS_ENTRY_ACSTATE_ACQUIRED || pCap->enmFuncState != VBOXCAPS_ENTRY_FUNCSTATE_STARTED);
1696 VBoxSeamlessDisable();
1697 }
1698}
1699
1700static void vboxCapsEntryAcStateSet(VBOXCAPS_ENTRY *pCap, VBOXCAPS_ENTRY_ACSTATE enmAcState)
1701{
1702 VBOXCAPS *pConsole = &gVBoxCaps;
1703
1704 Log(("vboxCapsEntryAcStateSet: new state enmAcState(%d); pCap: fCap(%d), iCap(%d), enmFuncState(%d), enmAcState(%d)\n",
1705 enmAcState, pCap->fCap, pCap->iCap, pCap->enmFuncState, pCap->enmAcState));
1706
1707 if (pCap->enmAcState == enmAcState)
1708 return;
1709
1710 VBOXCAPS_ENTRY_ACSTATE enmOldAcState = pCap->enmAcState;
1711 pCap->enmAcState = enmAcState;
1712
1713 if (enmAcState == VBOXCAPS_ENTRY_ACSTATE_ACQUIRED)
1714 {
1715 if (pCap->enmFuncState == VBOXCAPS_ENTRY_FUNCSTATE_STARTED)
1716 {
1717 if (pCap->pfnOnEnable)
1718 pCap->pfnOnEnable(pConsole, pCap, TRUE);
1719 }
1720 }
1721 else if (enmOldAcState == VBOXCAPS_ENTRY_ACSTATE_ACQUIRED && pCap->enmFuncState == VBOXCAPS_ENTRY_FUNCSTATE_STARTED)
1722 {
1723 if (pCap->pfnOnEnable)
1724 pCap->pfnOnEnable(pConsole, pCap, FALSE);
1725 }
1726}
1727
1728static void vboxCapsEntryFuncStateSet(VBOXCAPS_ENTRY *pCap, VBOXCAPS_ENTRY_FUNCSTATE enmFuncState)
1729{
1730 VBOXCAPS *pConsole = &gVBoxCaps;
1731
1732 Log(("vboxCapsEntryFuncStateSet: new state enmAcState(%d); pCap: fCap(%d), iCap(%d), enmFuncState(%d), enmAcState(%d)\n",
1733 enmFuncState, pCap->fCap, pCap->iCap, pCap->enmFuncState, pCap->enmAcState));
1734
1735 if (pCap->enmFuncState == enmFuncState)
1736 return;
1737
1738 VBOXCAPS_ENTRY_FUNCSTATE enmOldFuncState = pCap->enmFuncState;
1739
1740 pCap->enmFuncState = enmFuncState;
1741
1742 if (enmFuncState == VBOXCAPS_ENTRY_FUNCSTATE_STARTED)
1743 {
1744 Assert(enmOldFuncState == VBOXCAPS_ENTRY_FUNCSTATE_SUPPORTED);
1745 if (pCap->enmAcState == VBOXCAPS_ENTRY_ACSTATE_ACQUIRED)
1746 {
1747 if (pCap->pfnOnEnable)
1748 pCap->pfnOnEnable(pConsole, pCap, TRUE);
1749 }
1750 }
1751 else if (pCap->enmAcState == VBOXCAPS_ENTRY_ACSTATE_ACQUIRED && enmOldFuncState == VBOXCAPS_ENTRY_FUNCSTATE_STARTED)
1752 {
1753 if (pCap->pfnOnEnable)
1754 pCap->pfnOnEnable(pConsole, pCap, FALSE);
1755 }
1756}
1757
1758static void VBoxCapsEntryFuncStateSet(uint32_t iCup, VBOXCAPS_ENTRY_FUNCSTATE enmFuncState)
1759{
1760 VBOXCAPS *pConsole = &gVBoxCaps;
1761 VBOXCAPS_ENTRY *pCap = &pConsole->aCaps[iCup];
1762 vboxCapsEntryFuncStateSet(pCap, enmFuncState);
1763}
1764
1765static int VBoxCapsInit()
1766{
1767 VBOXCAPS *pConsole = &gVBoxCaps;
1768 memset(pConsole, 0, sizeof (*pConsole));
1769 pConsole->aCaps[VBOXCAPS_ENTRY_IDX_SEAMLESS].fCap = VMMDEV_GUEST_SUPPORTS_SEAMLESS;
1770 pConsole->aCaps[VBOXCAPS_ENTRY_IDX_SEAMLESS].iCap = VBOXCAPS_ENTRY_IDX_SEAMLESS;
1771 pConsole->aCaps[VBOXCAPS_ENTRY_IDX_SEAMLESS].pfnOnEnable = vboxCapsOnEnableSeamless;
1772 pConsole->aCaps[VBOXCAPS_ENTRY_IDX_GRAPHICS].fCap = VMMDEV_GUEST_SUPPORTS_GRAPHICS;
1773 pConsole->aCaps[VBOXCAPS_ENTRY_IDX_GRAPHICS].iCap = VBOXCAPS_ENTRY_IDX_GRAPHICS;
1774 return VINF_SUCCESS;
1775}
1776
1777static int VBoxCapsReleaseAll()
1778{
1779 VBOXCAPS *pConsole = &gVBoxCaps;
1780 Log(("VBoxCapsReleaseAll\n"));
1781 int rc = VBoxAcquireGuestCaps(0, VMMDEV_GUEST_SUPPORTS_SEAMLESS | VMMDEV_GUEST_SUPPORTS_GRAPHICS, false);
1782 if (!RT_SUCCESS(rc))
1783 {
1784 LogFlowFunc(("vboxCapsEntryReleaseAll VBoxAcquireGuestCaps failed rc %d\n", rc));
1785 return rc;
1786 }
1787
1788 if (pConsole->idTimer)
1789 {
1790 Log(("killing console timer\n"));
1791 KillTimer(g_hwndToolWindow, pConsole->idTimer);
1792 pConsole->idTimer = 0;
1793 }
1794
1795 for (int i = 0; i < RT_ELEMENTS(pConsole->aCaps); ++i)
1796 {
1797 vboxCapsEntryAcStateSet(&pConsole->aCaps[i], VBOXCAPS_ENTRY_ACSTATE_RELEASED);
1798 }
1799
1800 return rc;
1801}
1802
1803static void VBoxCapsTerm()
1804{
1805 VBOXCAPS *pConsole = &gVBoxCaps;
1806 VBoxCapsReleaseAll();
1807 memset(pConsole, 0, sizeof (*pConsole));
1808}
1809
1810static BOOL VBoxCapsEntryIsAcquired(uint32_t iCap)
1811{
1812 VBOXCAPS *pConsole = &gVBoxCaps;
1813 return pConsole->aCaps[iCap].enmAcState == VBOXCAPS_ENTRY_ACSTATE_ACQUIRED;
1814}
1815
1816static BOOL VBoxCapsEntryIsEnabled(uint32_t iCap)
1817{
1818 VBOXCAPS *pConsole = &gVBoxCaps;
1819 return pConsole->aCaps[iCap].enmAcState == VBOXCAPS_ENTRY_ACSTATE_ACQUIRED
1820 && pConsole->aCaps[iCap].enmFuncState == VBOXCAPS_ENTRY_FUNCSTATE_STARTED;
1821}
1822
1823static BOOL VBoxCapsCheckTimer(WPARAM wParam)
1824{
1825 VBOXCAPS *pConsole = &gVBoxCaps;
1826 if (wParam != pConsole->idTimer)
1827 return FALSE;
1828
1829 uint32_t u32AcquiredCaps = 0;
1830 BOOL fNeedNewTimer = FALSE;
1831
1832 for (int i = 0; i < RT_ELEMENTS(pConsole->aCaps); ++i)
1833 {
1834 VBOXCAPS_ENTRY *pCap = &pConsole->aCaps[i];
1835 if (pCap->enmAcState != VBOXCAPS_ENTRY_ACSTATE_ACQUIRING)
1836 continue;
1837
1838 int rc = VBoxAcquireGuestCaps(pCap->fCap, 0, false);
1839 if (RT_SUCCESS(rc))
1840 {
1841 vboxCapsEntryAcStateSet(&pConsole->aCaps[i], VBOXCAPS_ENTRY_ACSTATE_ACQUIRED);
1842 u32AcquiredCaps |= pCap->fCap;
1843 }
1844 else
1845 {
1846 Assert(rc == VERR_RESOURCE_BUSY);
1847 fNeedNewTimer = TRUE;
1848 }
1849 }
1850
1851 if (!fNeedNewTimer)
1852 {
1853 KillTimer(g_hwndToolWindow, pConsole->idTimer);
1854 /* cleanup timer data */
1855 pConsole->idTimer = 0;
1856 }
1857
1858 return TRUE;
1859}
1860
1861static int VBoxCapsEntryRelease(uint32_t iCap)
1862{
1863 VBOXCAPS *pConsole = &gVBoxCaps;
1864 VBOXCAPS_ENTRY *pCap = &pConsole->aCaps[iCap];
1865 if (pCap->enmAcState == VBOXCAPS_ENTRY_ACSTATE_RELEASED)
1866 {
1867 LogFlowFunc(("invalid cap[%d] state[%d] on release\n", iCap, pCap->enmAcState));
1868 return VERR_INVALID_STATE;
1869 }
1870
1871 if (pCap->enmAcState == VBOXCAPS_ENTRY_ACSTATE_ACQUIRED)
1872 {
1873 int rc = VBoxAcquireGuestCaps(0, pCap->fCap, false);
1874 AssertRC(rc);
1875 }
1876
1877 vboxCapsEntryAcStateSet(pCap, VBOXCAPS_ENTRY_ACSTATE_RELEASED);
1878
1879 return VINF_SUCCESS;
1880}
1881
1882static int VBoxCapsEntryAcquire(uint32_t iCap)
1883{
1884 VBOXCAPS *pConsole = &gVBoxCaps;
1885 Assert(VBoxConsoleIsAllowed());
1886 VBOXCAPS_ENTRY *pCap = &pConsole->aCaps[iCap];
1887 Log(("VBoxCapsEntryAcquire %d\n", iCap));
1888 if (pCap->enmAcState != VBOXCAPS_ENTRY_ACSTATE_RELEASED)
1889 {
1890 LogFlowFunc(("invalid cap[%d] state[%d] on acquire\n", iCap, pCap->enmAcState));
1891 return VERR_INVALID_STATE;
1892 }
1893
1894 vboxCapsEntryAcStateSet(pCap, VBOXCAPS_ENTRY_ACSTATE_ACQUIRING);
1895 int rc = VBoxAcquireGuestCaps(pCap->fCap, 0, false);
1896 if (RT_SUCCESS(rc))
1897 {
1898 vboxCapsEntryAcStateSet(pCap, VBOXCAPS_ENTRY_ACSTATE_ACQUIRED);
1899 return VINF_SUCCESS;
1900 }
1901
1902 if (rc != VERR_RESOURCE_BUSY)
1903 {
1904 LogFlowFunc(("vboxCapsEntryReleaseAll VBoxAcquireGuestCaps failed rc %d\n", rc));
1905 return rc;
1906 }
1907
1908 LogFlowFunc(("iCap %d is busy!\n", iCap));
1909
1910 /* the cap was busy, most likely it is still used by other VBoxTray instance running in another session,
1911 * queue the retry timer */
1912 if (!pConsole->idTimer)
1913 {
1914 pConsole->idTimer = SetTimer(g_hwndToolWindow, TIMERID_VBOXTRAY_CAPS_TIMER, 100, (TIMERPROC)NULL);
1915 if (!pConsole->idTimer)
1916 {
1917 DWORD dwErr = GetLastError();
1918 LogFlowFunc(("SetTimer error %08X\n", dwErr));
1919 return RTErrConvertFromWin32(dwErr);
1920 }
1921 }
1922
1923 return rc;
1924}
1925
1926static int VBoxCapsAcquireAllSupported()
1927{
1928 VBOXCAPS *pConsole = &gVBoxCaps;
1929 Log(("VBoxCapsAcquireAllSupported\n"));
1930 for (int i = 0; i < RT_ELEMENTS(pConsole->aCaps); ++i)
1931 {
1932 if (pConsole->aCaps[i].enmFuncState >= VBOXCAPS_ENTRY_FUNCSTATE_SUPPORTED)
1933 {
1934 Log(("VBoxCapsAcquireAllSupported acquiring cap %d, state %d\n", i, pConsole->aCaps[i].enmFuncState));
1935 VBoxCapsEntryAcquire(i);
1936 }
1937 else
1938 {
1939 LogFlowFunc(("VBoxCapsAcquireAllSupported: WARN: cap %d not supported, state %d\n", i, pConsole->aCaps[i].enmFuncState));
1940 }
1941 }
1942 return VINF_SUCCESS;
1943}
1944
1945static BOOL VBoxConsoleIsAllowed()
1946{
1947 return vboxDtIsInputDesktop() && vboxStIsActiveConsole();
1948}
1949
1950static void VBoxConsoleEnable(BOOL fEnable)
1951{
1952 if (fEnable)
1953 VBoxCapsAcquireAllSupported();
1954 else
1955 VBoxCapsReleaseAll();
1956}
1957
1958static void VBoxConsoleCapSetSupported(uint32_t iCap, BOOL fSupported)
1959{
1960 if (fSupported)
1961 {
1962 VBoxCapsEntryFuncStateSet(iCap, VBOXCAPS_ENTRY_FUNCSTATE_SUPPORTED);
1963
1964 if (VBoxConsoleIsAllowed())
1965 VBoxCapsEntryAcquire(iCap);
1966 }
1967 else
1968 {
1969 VBoxCapsEntryFuncStateSet(iCap, VBOXCAPS_ENTRY_FUNCSTATE_UNSUPPORTED);
1970
1971 VBoxCapsEntryRelease(iCap);
1972 }
1973}
1974
1975void VBoxSeamlessSetSupported(BOOL fSupported)
1976{
1977 VBoxConsoleCapSetSupported(VBOXCAPS_ENTRY_IDX_SEAMLESS, fSupported);
1978}
1979
1980static void VBoxGrapicsSetSupported(BOOL fSupported)
1981{
1982 VBoxConsoleCapSetSupported(VBOXCAPS_ENTRY_IDX_GRAPHICS, fSupported);
1983}
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