VirtualBox

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

Last change on this file since 60014 was 58308, checked in by vboxsync, 9 years ago

VBoxTray.cpp: removed malloc.h.

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