display.cpp revision a5f07c4bc554dddeed320b485b68ccd6b3448be9
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * X11 guest client - display management.
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2006-2012 Oracle Corporation
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * available from http://www.virtualbox.org. This file is free software;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * you can redistribute it and/or modify it under the terms of the GNU
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * General Public License (GPL) as published by the Free Software
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/** @todo this should probably be replaced by something IPRT */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/* For system() and WEXITSTATUS() */
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync/* TESTING: Dynamic resizing and mouse integration toggling should work
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * correctly with a range of X servers (pre-1.3, 1.3 and later under Linux, 1.3
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * and later under Solaris) with Guest Additions installed. Switching to a
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * virtual terminal while a user session is in place should disable dynamic
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * resizing and cursor integration, switching back should re-enable them. */
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync/** Most recent information received for a particular screen. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync unsigned x;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync unsigned y;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/** Display magic number, start of a UUID. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/** State information needed for the service. The main VBoxClient code provides
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * the daemon logic needed by all services. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** The service interface. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Magic number for sanity checks. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Are we initialised yet? */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** The connection to the server. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Can we use version 1.2 or later of the RandR protocol here? */
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync /** The command argument to use for the xrandr binary. Currently only
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * used to support the non-standard location on some Solaris systems -
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * would it make sense to use absolute paths on all systems? */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** The number of screens we are currently aware of. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /** Array of information about different screens. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync/** Tell the VBoxGuest driver we no longer want any events and tell the host
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * we no longer support any capabilities. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = VbglR3SetGuestCaps(0, VMMDEV_GUEST_SUPPORTS_GRAPHICS);
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync VBClFatalError(("Failed to unset graphics capability, rc=%Rrc.\n", rc));
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync rc = VbglR3SetMouseStatus(VMMDEV_MOUSE_GUEST_NEEDS_HOST_CURSOR);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync VBClFatalError(("Failed to unset mouse status, rc=%Rrc.\n", rc));
0c4004948fca34f2db87e7b38013137e9472c306vboxsync rc = VbglR3CtlFilterMask(0, VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBClFatalError(("Failed to unset filter mask, rc=%Rrc.\n", rc));
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync/** Tell the VBoxGuest driver which events we want and tell the host which
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync * capabilities we support. */
6f76f373e9274aa29ec1485be3bfebafb670af0evboxsync int rc = VbglR3CtlFilterMask( VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBClFatalError(("Failed to set filter mask, rc=%Rrc.\n", rc));
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VbglR3SetGuestCaps(VMMDEV_GUEST_SUPPORTS_GRAPHICS, 0);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VBClFatalError(("Failed to set graphics capability, rc=%Rrc.\n", rc));
0a3cfc177eb2cca0805ca23cf5f98b0080b2ec21vboxsync VBClFatalError(("Failed to set mouse status, rc=%Rrc.\n", rc));
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync /* Initialise the guest library. */
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync VBClFatalError(("Failed to connect to the VirtualBox kernel service, rc=%Rrc\n", rc));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (WEXITSTATUS(status) != 0) /* Utility or extension not available. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VBClFatalError(("Failed to execute the xrandr utility.\n"));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync RTStrPrintf(szCommand, sizeof(szCommand), "%s --q12", pState->pcszXrandr);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsyncstatic void updateScreenInformation(struct DISPLAYSTATE *pState, unsigned cx, unsigned cy, unsigned cBPP, unsigned iDisplay,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync unsigned x, unsigned y, bool fEnabled, bool fUpdatePosition)
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync (struct screenInformation *)RTMemRealloc(pState->paScreenInformation,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync (iDisplay + 1) * sizeof(*pState->paScreenInformation));
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VBClFatalError(("Failed to re-allocate screen information.\n"));
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync for (i = pState->cScreensTracked; i < iDisplay + 1; ++i)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pState->paScreenInformation[iDisplay].fEnabled = fEnabled;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pState->paScreenInformation[iDisplay].fUpdateSize = true;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pState->paScreenInformation[iDisplay].fUpdatePosition = fUpdatePosition;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsyncstatic void updateSizeHintsProperty(struct DISPLAYSTATE *pState)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync long *paSizeHints = (long *)RTMemTmpAllocZ(pState->cScreensTracked * sizeof(long));
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync unsigned i;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VBClFatalError(("Failed to allocate size hint property memory.\n"));
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync && pState->paScreenInformation[i].cx != 0 && pState->paScreenInformation[i].cy != 0)
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync paSizeHints[i] = (pState->paScreenInformation[i].cx & 0x8fff) << 16 | (pState->paScreenInformation[i].cy & 0x8fff);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync else if (pState->paScreenInformation[i].cx != 0 && pState->paScreenInformation[i].cy != 0)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync XChangeProperty(pState->pDisplay, DefaultRootWindow(pState->pDisplay), XInternAtom(pState->pDisplay, "VBOX_SIZE_HINTS", 0),
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync XA_INTEGER, 32, PropModeReplace, (unsigned char *)paSizeHints, pState->cScreensTracked);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic void notifyXServer(struct DISPLAYSTATE *pState)
eb8371e8297aff06a1982468835ff62e1ab77b30vboxsync unsigned i;
eb8371e8297aff06a1982468835ff62e1ab77b30vboxsync /** @note The xrandr command can fail if something else accesses RandR at
eb8371e8297aff06a1982468835ff62e1ab77b30vboxsync * the same time. We just ignore failure for now and let the user try
eb8371e8297aff06a1982468835ff62e1ab77b30vboxsync * again as we do not know what someone else is doing. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if ( !pState->fHaveRandR12 && pState->paScreenInformation[0].fUpdateSize
65697a26b524640b83828b715160c798c43a0424vboxsync && pState->paScreenInformation[0].cx > 0 && pState->paScreenInformation[0].cy > 0)
f4aad55f8addd816ef005845842a2418bbdc3ea2vboxsync RTStrPrintf(szCommand, sizeof(szCommand), "%s -s %ux%u",
65697a26b524640b83828b715160c798c43a0424vboxsync pState->pcszXrandr, pState->paScreenInformation[0].cx, pState->paScreenInformation[0].cy);
65697a26b524640b83828b715160c798c43a0424vboxsync pState->paScreenInformation[0].fUpdateSize = false;
65697a26b524640b83828b715160c798c43a0424vboxsync else if (pState->fHaveRandR12 && fUpdateInformation)
eb8371e8297aff06a1982468835ff62e1ab77b30vboxsync RTStrPrintf(szCommand, sizeof(szCommand), "%s --output VGA-%u --preferred", pState->pcszXrandr, i);
eb8371e8297aff06a1982468835ff62e1ab77b30vboxsync if (pState->paScreenInformation[i].fUpdatePosition)
65697a26b524640b83828b715160c798c43a0424vboxsync RTStrPrintf(szCommand, sizeof(szCommand), "%s --output VGA-%u --auto --pos %ux%u",
65697a26b524640b83828b715160c798c43a0424vboxsync pState->pcszXrandr, i, pState->paScreenInformation[i].x, pState->paScreenInformation[i].y);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pState->paScreenInformation[i].fUpdateSize = pState->paScreenInformation[i].fUpdatePosition = false;
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync RTStrPrintf(szCommand, sizeof(szCommand), "%s", pState->pcszXrandr);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic void updateMouseCapabilities(struct DISPLAYSTATE *pState)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync unsigned i;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBClFatalError(("Failed to get mouse status, rc=%Rrc\n", rc));
f687f34bd232be13744edbc0cc5155fa5d4540edvboxsync XChangeProperty(pState->pDisplay, DefaultRootWindow(pState->pDisplay),
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync XInternAtom(pState->pDisplay, "VBOX_MOUSE_CAPABILITIES", 0), XA_INTEGER, 32, PropModeReplace,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * Display change request monitor thread function.
eb8371e8297aff06a1982468835ff62e1ab77b30vboxsync rc = VbglR3VideoModeGetHighestSavedScreen(&cScreensTracked);
64f58e4154eaa20c47782b429eeaff09070369bfvboxsync if (rc != VINF_SUCCESS && rc != VERR_NOT_SUPPORTED)
eb8371e8297aff06a1982468835ff62e1ab77b30vboxsync VBClFatalError(("Failed to get the number of saved screen modes, rc=%Rrc\n", rc));
eb8371e8297aff06a1982468835ff62e1ab77b30vboxsync /* Make sure that we have an entry for screen 1 at least. */
eb8371e8297aff06a1982468835ff62e1ab77b30vboxsync updateScreenInformation(pState, 1024, 768, 0, 1, 0, 0, true, false);
eb8371e8297aff06a1982468835ff62e1ab77b30vboxsync /* The "8" is for the sanity test below. */
eb8371e8297aff06a1982468835ff62e1ab77b30vboxsync for (i = 0; i < RT_MAX(cScreensTracked + 1, 8); ++i)
eb8371e8297aff06a1982468835ff62e1ab77b30vboxsync bool fEnabled = true;
eb8371e8297aff06a1982468835ff62e1ab77b30vboxsync rc = VbglR3RetrieveVideoMode(i, &cx, &cy, &cBPP, &x, &y,
eb8371e8297aff06a1982468835ff62e1ab77b30vboxsync /* Sanity test for VbglR3VideoModeGetHighestSavedScreen(). */
eb8371e8297aff06a1982468835ff62e1ab77b30vboxsync VBClFatalError(("Internal error retrieving the number of saved screen modes.\n"));
eb8371e8297aff06a1982468835ff62e1ab77b30vboxsync updateScreenInformation(pState, cx, cy, cBPP, i, x, y, fEnabled, true);
eb8371e8297aff06a1982468835ff62e1ab77b30vboxsync while (true)
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync rc = VbglR3WaitEvent( VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBClFatalError(("event wait failed, rc=%Rrc\n", rc));
65697a26b524640b83828b715160c798c43a0424vboxsync /* If it is a size hint, set the new size. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync uint32_t cx = 0, cy = 0, cBPP = 0, iDisplay = 0, x = 0, y = 0;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VbglR3GetDisplayChangeRequest(&cx, &cy, &cBPP, &iDisplay,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBClFatalError(("Failed to get display change request, rc=%Rrc\n",
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync LogRelFlowFunc(("Got size hint from host cx=%d, cy=%d, bpp=%d, iDisplay=%d, x=%d, y=%d fEnabled=%d\n",
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VBClFatalError(("Received a size hint for too high display number %u\n",
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync (unsigned) iDisplay));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync updateScreenInformation(pState, cx, cy, cBPP, iDisplay, x, y, fEnabled, fUpdatePosition);
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync VBClFatalError(("Failed to check seamless mode, rc=%Rrc\n", rc));
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync rc = VbglR3SaveVideoMode(iDisplay, cx, cy, cBPP, x, y,
f4aad55f8addd816ef005845842a2418bbdc3ea2vboxsync VBClFatalError(("Failed to save size hint, rc=%Rrc\n", rc));
f4aad55f8addd816ef005845842a2418bbdc3ea2vboxsyncstatic const char *getPidFilePath()
f4aad55f8addd816ef005845842a2418bbdc3ea2vboxsync return ".vboxclient-display.pid";
f4aad55f8addd816ef005845842a2418bbdc3ea2vboxsyncstatic struct DISPLAYSTATE *getStateFromInterface(struct VBCLSERVICE **ppInterface)
f4aad55f8addd816ef005845842a2418bbdc3ea2vboxsync struct DISPLAYSTATE *pSelf = (struct DISPLAYSTATE *)ppInterface;
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync struct DISPLAYSTATE *pSelf = getStateFromInterface(ppInterface);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsyncstatic int run(struct VBCLSERVICE **ppInterface, bool fDaemonised)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync struct DISPLAYSTATE *pSelf = getStateFromInterface(ppInterface);
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync VBClFatalError(("Failed to start the VT monitor thread: %Rrc\n", rc));
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync return VERR_INTERNAL_ERROR; /* "Should never reach here." */
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync struct DISPLAYSTATE *pSelf = getStateFromInterface(ppInterface);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync struct DISPLAYSTATE *pSelf = getStateFromInterface(ppInterface);
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsyncstatic void cleanup(struct VBCLSERVICE **ppInterface)
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync struct DISPLAYSTATE *pService = (struct DISPLAYSTATE *)RTMemAlloc(sizeof(*pService));