VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/VBoxClient/display.cpp@ 33914

Last change on this file since 33914 was 33304, checked in by vboxsync, 14 years ago

Additions/x11/VBoxClient: get rid of VMMDEV_MOUSE_HOST_RECHECKS_NEEDS_HOST_CURSOR hack in VBoxClient

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.5 KB
Line 
1/* $Id: display.cpp 33304 2010-10-21 12:18:54Z vboxsync $ */
2/** @file
3 * X11 guest client - display management.
4 */
5
6/*
7 * Copyright (C) 2006-2007 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/** @todo this should probably be replaced by something IPRT */
19/* For system() and WEXITSTATUS() */
20#include <stdlib.h>
21#include <sys/types.h>
22#include <sys/wait.h>
23#include <errno.h>
24
25#include <X11/Xlib.h>
26#include <X11/cursorfont.h>
27#include <X11/extensions/Xrandr.h>
28
29#include <iprt/assert.h>
30#include <iprt/err.h>
31#include <iprt/thread.h>
32#include <VBox/log.h>
33#include <VBox/VMMDev.h>
34#include <VBox/VBoxGuestLib.h>
35
36#include "VBoxClient.h"
37
38static int initDisplay(Display *pDisplay)
39{
40 int rc = VINF_SUCCESS;
41 uint32_t fMouseFeatures = 0;
42
43 LogRelFlowFunc(("testing dynamic resizing\n"));
44 int iDummy;
45 if (!XRRQueryExtension(pDisplay, &iDummy, &iDummy))
46 rc = VERR_NOT_SUPPORTED;
47 if (RT_SUCCESS(rc))
48 rc = VbglR3CtlFilterMask(VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST, 0);
49 else
50 VbglR3CtlFilterMask(0, VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST);
51 /* Log and ignore the return value, as there is not much we can do with
52 * it. */
53 LogRelFlowFunc(("dynamic resizing: result %Rrc\n", rc));
54 /* Enable support for switching between hardware and software cursors */
55 LogRelFlowFunc(("enabling relative mouse re-capturing support\n"));
56 rc = VbglR3GetMouseStatus(&fMouseFeatures, NULL, NULL);
57 if (RT_SUCCESS(rc))
58 {
59 rc = VbglR3CtlFilterMask(VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED,
60 0);
61 if (RT_SUCCESS(rc))
62 rc = VbglR3SetMouseStatus
63 ( fMouseFeatures
64 & ~VMMDEV_MOUSE_GUEST_NEEDS_HOST_CURSOR);
65 }
66 if (RT_FAILURE(rc))
67 {
68 VbglR3CtlFilterMask(0, VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED);
69 VbglR3SetMouseStatus( fMouseFeatures
70 | VMMDEV_MOUSE_GUEST_NEEDS_HOST_CURSOR);
71 }
72 LogRelFlowFunc(("mouse re-capturing support: result %Rrc\n", rc));
73 return VINF_SUCCESS;
74}
75
76void cleanupDisplay(void)
77{
78 uint32_t fMouseFeatures = 0;
79 LogRelFlowFunc(("\n"));
80 VbglR3CtlFilterMask(0, VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST
81 | VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED);
82 int rc = VbglR3GetMouseStatus(&fMouseFeatures, NULL, NULL);
83 if (RT_SUCCESS(rc))
84 VbglR3SetMouseStatus( fMouseFeatures
85 | VMMDEV_MOUSE_GUEST_NEEDS_HOST_CURSOR);
86 LogRelFlowFunc(("returning\n"));
87}
88
89/** This thread just runs a dummy X11 event loop to be sure that we get
90 * terminated should the X server exit. */
91static int x11ConnectionMonitor(RTTHREAD, void *)
92{
93 XEvent ev;
94 Display *pDisplay = XOpenDisplay(NULL);
95 while (true)
96 XNextEvent(pDisplay, &ev);
97 return 0;
98}
99
100/**
101 * This method first resets the current resolution using RandR to wake up
102 * the graphics driver, then sets the resolution requested if it is among
103 * those offered by the driver.
104 */
105static void setSize(Display *pDisplay, uint32_t cx, uint32_t cy)
106{
107 XRRScreenConfiguration *pConfig;
108 XRRScreenSize *pSizes;
109 int cSizes;
110 pConfig = XRRGetScreenInfo(pDisplay, DefaultRootWindow(pDisplay));
111 /* Reset the current mode */
112 LogRelFlowFunc(("Setting size %ux%u\n", cx, cy));
113 if (pConfig)
114 {
115 pSizes = XRRConfigSizes(pConfig, &cSizes);
116 unsigned uDist = UINT32_MAX;
117 int iMode = -1;
118 for (int i = 0; i < cSizes; ++i)
119 {
120#define VBCL_SQUARE(x) (x) * (x)
121 unsigned uThisDist = VBCL_SQUARE(pSizes[i].width - cx)
122 + VBCL_SQUARE(pSizes[i].height - cy);
123 LogRelFlowFunc(("Found size %dx%d, distance %u\n", pSizes[i].width,
124 pSizes[i].height, uThisDist));
125#undef VBCL_SQUARE
126 if (uThisDist < uDist)
127 {
128 uDist = uThisDist;
129 iMode = i;
130 }
131 }
132 if (iMode >= 0)
133 {
134 Time config_timestamp = 0;
135 XRRConfigTimes(pConfig, &config_timestamp);
136 LogRelFlowFunc(("Setting new size %d\n", iMode));
137 XRRSetScreenConfig(pDisplay, pConfig,
138 DefaultRootWindow(pDisplay), iMode,
139 RR_Rotate_0, config_timestamp);
140 }
141 XRRFreeScreenConfigInfo(pConfig);
142 }
143}
144
145/**
146 * Display change request monitor thread function.
147 * Before entering the loop, we re-read the last request
148 * received, and if the first one received inside the
149 * loop is identical we ignore it, because it is probably
150 * stale.
151 */
152static int runDisplay(Display *pDisplay)
153{
154 LogRelFlowFunc(("\n"));
155 Cursor hClockCursor = XCreateFontCursor(pDisplay, XC_watch);
156 Cursor hArrowCursor = XCreateFontCursor(pDisplay, XC_left_ptr);
157 int rc = RTThreadCreate(NULL, x11ConnectionMonitor, NULL, 0,
158 RTTHREADTYPE_INFREQUENT_POLLER, 0, "X11 monitor");
159 if (RT_FAILURE(rc))
160 return rc;
161 while (true)
162 {
163 uint32_t fEvents = 0, cx = 0, cy = 0, cBits = 0, iDisplay = 0;
164 rc = VbglR3WaitEvent( VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST
165 | VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED,
166 RT_INDEFINITE_WAIT, &fEvents);
167 /* Jiggle the mouse pointer to wake up the driver. */
168 XGrabPointer(pDisplay,
169 DefaultRootWindow(pDisplay), true, 0, GrabModeAsync,
170 GrabModeAsync, None, hClockCursor, CurrentTime);
171 XFlush(pDisplay);
172 XGrabPointer(pDisplay,
173 DefaultRootWindow(pDisplay), true, 0, GrabModeAsync,
174 GrabModeAsync, None, hArrowCursor, CurrentTime);
175 XFlush(pDisplay);
176 XUngrabPointer(pDisplay, CurrentTime);
177 XFlush(pDisplay);
178 /* And if it is a size hint, set the new size now that the video
179 * driver has had a chance to update its list. */
180 if (RT_SUCCESS(rc) && (fEvents & VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST))
181 {
182 int rc2 = VbglR3GetDisplayChangeRequest(&cx, &cy, &cBits,
183 &iDisplay, true);
184 /* If we are not stopping, sleep for a bit to avoid using up
185 too much CPU while retrying. */
186 if (RT_FAILURE(rc2))
187 RTThreadYield();
188 else
189 setSize(pDisplay, cx, cy);
190 }
191 }
192 LogRelFlowFunc(("returning VINF_SUCCESS\n"));
193 return VINF_SUCCESS;
194}
195
196class DisplayService : public VBoxClient::Service
197{
198public:
199 virtual const char *getPidFilePath()
200 {
201 return ".vboxclient-display.pid";
202 }
203 virtual int run(bool fDaemonised /* = false */)
204 {
205 Display *pDisplay = XOpenDisplay(NULL);
206 if (!pDisplay)
207 return VERR_NOT_FOUND;
208 int rc = initDisplay(pDisplay);
209 if (RT_SUCCESS(rc))
210 rc = runDisplay(pDisplay);
211 XCloseDisplay(pDisplay);
212 return rc;
213 }
214 virtual void cleanup()
215 {
216 cleanupDisplay();
217 }
218};
219
220VBoxClient::Service *VBoxClient::GetDisplayService()
221{
222 return new DisplayService;
223}
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