1 | /* $Id: display-svga-x11.cpp 70169 2017-12-15 20:36:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * X11 guest client - VMSVGA emulation resize event pass-through to X.Org
|
---|
4 | * guest driver.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2017 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 | /*
|
---|
20 | * Known things to test when changing this code. All assume a guest with VMSVGA
|
---|
21 | * active and controlled by X11 or Wayland, and Guest Additions installed and
|
---|
22 | * running, unless otherwise stated.
|
---|
23 | * - On Linux 4.6 and later guests, VBoxClient --vmsvga should be running as
|
---|
24 | * root and not as the logged-in user. Dynamic resizing should work for all
|
---|
25 | * screens in any environment which handles kernel resize notifications,
|
---|
26 | * including at log-in screens. Test GNOME Shell Wayland and GNOME Shell
|
---|
27 | * under X.Org or Unity or KDE at the log-in screen and after log-in.
|
---|
28 | * - Linux 4.10 changed the user-kernel-ABI introduced in 4.6: test both.
|
---|
29 | * - On other guests (than Linux 4.6 or later) running X.Org Server 1.3 or
|
---|
30 | * later, VBoxClient --vmsvga should never be running as root, and should run
|
---|
31 | * (and dynamic resizing and screen enable/disable should work for all
|
---|
32 | * screens) whenever a user is logged in to a supported desktop environment.
|
---|
33 | * - On guests running X.Org Server 1.2 or older, VBoxClient --vmsvga should
|
---|
34 | * never run as root and should run whenever a user is logged in to a
|
---|
35 | * supported desktop environment. Dynamic resizing should work for the first
|
---|
36 | * screen, and enabling others should not be possible.
|
---|
37 | * - When VMSVGA is not enabled, VBoxClient --vmsvga should never stay running.
|
---|
38 | */
|
---|
39 |
|
---|
40 | #include "VBoxClient.h"
|
---|
41 |
|
---|
42 | #include <VBox/VBoxGuestLib.h>
|
---|
43 |
|
---|
44 | #include <iprt/assert.h>
|
---|
45 | #include <iprt/file.h>
|
---|
46 | #include <iprt/string.h>
|
---|
47 |
|
---|
48 | /** Maximum number of supported screens. DRM and X11 both limit this to 32. */
|
---|
49 | /** @todo if this ever changes, dynamically allocate resizeable arrays in the
|
---|
50 | * context structure. */
|
---|
51 | #define VMW_MAX_HEADS 32
|
---|
52 |
|
---|
53 | /* VMWare X.Org driver control parts definitions. */
|
---|
54 |
|
---|
55 | #include <X11/Xlibint.h>
|
---|
56 |
|
---|
57 | struct X11CONTEXT
|
---|
58 | {
|
---|
59 | Display *pDisplay;
|
---|
60 | int hRandRMajor;
|
---|
61 | int hVMWMajor;
|
---|
62 | };
|
---|
63 |
|
---|
64 | static void x11Connect(struct X11CONTEXT *pContext)
|
---|
65 | {
|
---|
66 | int dummy;
|
---|
67 |
|
---|
68 | if (pContext->pDisplay != NULL)
|
---|
69 | VBClFatalError(("%s called with bad argument\n", __func__));
|
---|
70 | pContext->pDisplay = XOpenDisplay(NULL);
|
---|
71 | if (pContext->pDisplay == NULL)
|
---|
72 | return;
|
---|
73 | if ( !XQueryExtension(pContext->pDisplay, "RANDR",
|
---|
74 | &pContext->hRandRMajor, &dummy, &dummy)
|
---|
75 | || !XQueryExtension(pContext->pDisplay, "VMWARE_CTRL",
|
---|
76 | &pContext->hVMWMajor, &dummy, &dummy))
|
---|
77 | {
|
---|
78 | XCloseDisplay(pContext->pDisplay);
|
---|
79 | pContext->pDisplay = NULL;
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | #define X11_VMW_TOPOLOGY_REQ 2
|
---|
84 | struct X11VMWRECT /* xXineramaScreenInfo in Xlib headers. */
|
---|
85 | {
|
---|
86 | int16_t x;
|
---|
87 | int16_t y;
|
---|
88 | uint16_t w;
|
---|
89 | uint16_t h;
|
---|
90 | };
|
---|
91 | AssertCompileSize(struct X11VMWRECT, 8);
|
---|
92 |
|
---|
93 | struct X11REQHEADER
|
---|
94 | {
|
---|
95 | uint8_t hMajor;
|
---|
96 | uint8_t idType;
|
---|
97 | uint16_t cd;
|
---|
98 | };
|
---|
99 |
|
---|
100 | struct X11VMWTOPOLOGYREQ
|
---|
101 | {
|
---|
102 | struct X11REQHEADER header;
|
---|
103 | uint32_t idX11Screen;
|
---|
104 | uint32_t cScreens;
|
---|
105 | uint32_t u32Pad;
|
---|
106 | struct X11VMWRECT aRects[1];
|
---|
107 | };
|
---|
108 | AssertCompileSize(struct X11VMWTOPOLOGYREQ, 24);
|
---|
109 |
|
---|
110 | #define X11_VMW_TOPOLOGY_REPLY_SIZE 32
|
---|
111 |
|
---|
112 | #define X11_VMW_RESOLUTION_REQUEST 1
|
---|
113 | struct X11VMWRESOLUTIONREQ
|
---|
114 | {
|
---|
115 | struct X11REQHEADER header;
|
---|
116 | uint32_t idX11Screen;
|
---|
117 | uint32_t w;
|
---|
118 | uint32_t h;
|
---|
119 | };
|
---|
120 | AssertCompileSize(struct X11VMWRESOLUTIONREQ, 16);
|
---|
121 |
|
---|
122 | #define X11_VMW_RESOLUTION_REPLY_SIZE 32
|
---|
123 |
|
---|
124 | #define X11_RANDR_GET_SCREEN_REQUEST 5
|
---|
125 | struct X11RANDRGETSCREENREQ
|
---|
126 | {
|
---|
127 | struct X11REQHEADER header;
|
---|
128 | uint32_t hWindow;
|
---|
129 | };
|
---|
130 | AssertCompileSize(struct X11RANDRGETSCREENREQ, 8);
|
---|
131 |
|
---|
132 | #define X11_RANDR_GET_SCREEN_REPLY_SIZE 32
|
---|
133 |
|
---|
134 | /* This was a macro in old Xlib versions and a function in newer ones; the
|
---|
135 | * display members touched by the macro were declared as ABI for compatibility
|
---|
136 | * reasons. To simplify building with different generations, we duplicate the
|
---|
137 | * code. */
|
---|
138 | static void x11GetRequest(struct X11CONTEXT *pContext, uint8_t hMajor,
|
---|
139 | uint8_t idType, size_t cb, struct X11REQHEADER **ppReq)
|
---|
140 | {
|
---|
141 | if (pContext->pDisplay->bufptr + cb > pContext->pDisplay->bufmax)
|
---|
142 | _XFlush(pContext->pDisplay);
|
---|
143 | if (pContext->pDisplay->bufptr + cb > pContext->pDisplay->bufmax)
|
---|
144 | VBClFatalError(("%s display buffer overflow.\n", __func__));
|
---|
145 | if (cb % 4 != 0)
|
---|
146 | VBClFatalError(("%s bad parameter.\n", __func__));
|
---|
147 | pContext->pDisplay->last_req = pContext->pDisplay->bufptr;
|
---|
148 | *ppReq = (struct X11REQHEADER *)pContext->pDisplay->bufptr;
|
---|
149 | (*ppReq)->hMajor = hMajor;
|
---|
150 | (*ppReq)->idType = idType;
|
---|
151 | (*ppReq)->cd = cb / 4;
|
---|
152 | pContext->pDisplay->bufptr += cb;
|
---|
153 | pContext->pDisplay->request++;
|
---|
154 | }
|
---|
155 |
|
---|
156 | static void x11SendHints(struct X11CONTEXT *pContext, struct X11VMWRECT *pRects,
|
---|
157 | unsigned cRects)
|
---|
158 | {
|
---|
159 | unsigned i;
|
---|
160 | struct X11VMWTOPOLOGYREQ *pReqTopology;
|
---|
161 | uint8_t repTopology[X11_VMW_TOPOLOGY_REPLY_SIZE];
|
---|
162 | struct X11VMWRESOLUTIONREQ *pReqResolution;
|
---|
163 | uint8_t repResolution[X11_VMW_RESOLUTION_REPLY_SIZE];
|
---|
164 |
|
---|
165 | if (!VALID_PTR(pContext->pDisplay))
|
---|
166 | VBClFatalError(("%s bad display argument.\n", __func__));
|
---|
167 | if (cRects == 0)
|
---|
168 | return;
|
---|
169 | /* Try a topology (multiple screen) request. */
|
---|
170 | x11GetRequest(pContext, pContext->hVMWMajor, X11_VMW_TOPOLOGY_REQ,
|
---|
171 | sizeof(struct X11VMWTOPOLOGYREQ)
|
---|
172 | + sizeof(struct X11VMWRECT) * (cRects - 1),
|
---|
173 | (struct X11REQHEADER **)&pReqTopology);
|
---|
174 | pReqTopology->idX11Screen = DefaultScreen(pContext->pDisplay);
|
---|
175 | pReqTopology->cScreens = cRects;
|
---|
176 | for (i = 0; i < cRects; ++i)
|
---|
177 | pReqTopology->aRects[i] = pRects[i];
|
---|
178 | _XSend(pContext->pDisplay, NULL, 0);
|
---|
179 | if (_XReply(pContext->pDisplay, (xReply *)&repTopology, 0, xTrue))
|
---|
180 | return;
|
---|
181 | /* That failed, so try the old single screen set resolution. We prefer
|
---|
182 | * simpler code to negligeably improved efficiency, so we just always try
|
---|
183 | * both requests instead of doing version checks or caching. */
|
---|
184 | x11GetRequest(pContext, pContext->hVMWMajor, X11_VMW_RESOLUTION_REQUEST,
|
---|
185 | sizeof(struct X11VMWTOPOLOGYREQ),
|
---|
186 | (struct X11REQHEADER **)&pReqResolution);
|
---|
187 | pReqResolution->idX11Screen = DefaultScreen(pContext->pDisplay);
|
---|
188 | pReqResolution->w = pRects[0].w;
|
---|
189 | pReqResolution->h = pRects[0].h;
|
---|
190 | if (_XReply(pContext->pDisplay, (xReply *)&repResolution, 0, xTrue))
|
---|
191 | return;
|
---|
192 | /* What now? */
|
---|
193 | VBClFatalError(("%s failed to set resolution\n", __func__));
|
---|
194 | }
|
---|
195 |
|
---|
196 | /** Call RRGetScreenInfo to wake up the server to the new modes. */
|
---|
197 | static void x11GetScreenInfo(struct X11CONTEXT *pContext)
|
---|
198 | {
|
---|
199 | struct X11RANDRGETSCREENREQ *pReqGetScreen;
|
---|
200 | uint8_t repGetScreen[X11_RANDR_GET_SCREEN_REPLY_SIZE];
|
---|
201 |
|
---|
202 | if (!VALID_PTR(pContext->pDisplay))
|
---|
203 | VBClFatalError(("%s bad display argument.\n", __func__));
|
---|
204 | x11GetRequest(pContext, pContext->hRandRMajor, X11_RANDR_GET_SCREEN_REQUEST,
|
---|
205 | sizeof(struct X11RANDRGETSCREENREQ),
|
---|
206 | (struct X11REQHEADER **)&pReqGetScreen);
|
---|
207 | pReqGetScreen->hWindow = DefaultRootWindow(pContext->pDisplay);
|
---|
208 | _XSend(pContext->pDisplay, NULL, 0);
|
---|
209 | if (!_XReply(pContext->pDisplay, (xReply *)&repGetScreen, 0, xTrue))
|
---|
210 | VBClFatalError(("%s failed to set resolution\n", __func__));
|
---|
211 | }
|
---|
212 |
|
---|
213 | static const char *getPidFilePath()
|
---|
214 | {
|
---|
215 | return ".vboxclient-display-svga-x11.pid";
|
---|
216 | }
|
---|
217 |
|
---|
218 | static int run(struct VBCLSERVICE **ppInterface, bool fDaemonised)
|
---|
219 | {
|
---|
220 | (void)ppInterface;
|
---|
221 | (void)fDaemonised;
|
---|
222 | struct X11CONTEXT x11Context = { NULL };
|
---|
223 | unsigned i;
|
---|
224 | int rc;
|
---|
225 | uint32_t acx[VMW_MAX_HEADS] = { 0 };
|
---|
226 | uint32_t acy[VMW_MAX_HEADS] = { 0 };
|
---|
227 | uint32_t adx[VMW_MAX_HEADS] = { 0 };
|
---|
228 | uint32_t ady[VMW_MAX_HEADS] = { 0 };
|
---|
229 | uint32_t afEnabled[VMW_MAX_HEADS] = { false };
|
---|
230 | struct X11VMWRECT aRects[VMW_MAX_HEADS];
|
---|
231 | unsigned cHeads;
|
---|
232 |
|
---|
233 | x11Connect(&x11Context);
|
---|
234 | if (x11Context.pDisplay == NULL)
|
---|
235 | return VINF_SUCCESS;
|
---|
236 | /* Initialise the guest library. */
|
---|
237 | rc = VbglR3InitUser();
|
---|
238 | if (RT_FAILURE(rc))
|
---|
239 | VBClFatalError(("Failed to connect to the VirtualBox kernel service, rc=%Rrc\n", rc));
|
---|
240 | rc = VbglR3CtlFilterMask(VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST, 0);
|
---|
241 | if (RT_FAILURE(rc))
|
---|
242 | VBClFatalError(("Failed to request display change events, rc=%Rrc\n", rc));
|
---|
243 | rc = VbglR3AcquireGuestCaps(VMMDEV_GUEST_SUPPORTS_GRAPHICS, 0, false);
|
---|
244 | if (rc == VERR_RESOURCE_BUSY) /* Someone else has already acquired it. */
|
---|
245 | return VINF_SUCCESS;
|
---|
246 | if (RT_FAILURE(rc))
|
---|
247 | VBClFatalError(("Failed to register resizing support, rc=%Rrc\n", rc));
|
---|
248 | for (;;)
|
---|
249 | {
|
---|
250 | uint32_t events;
|
---|
251 |
|
---|
252 | rc = VbglR3WaitEvent(VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST, RT_INDEFINITE_WAIT, &events);
|
---|
253 | if (RT_FAILURE(rc))
|
---|
254 | VBClFatalError(("Failure waiting for event, rc=%Rrc\n", rc));
|
---|
255 | while (rc != VERR_TIMEOUT)
|
---|
256 | {
|
---|
257 | uint32_t cx, cy, cBits, dx, dy, idx;
|
---|
258 | bool fEnabled, fChangeOrigin;
|
---|
259 |
|
---|
260 | rc = VbglR3GetDisplayChangeRequest(&cx, &cy, &cBits, &idx, &dx, &dy, &fEnabled, &fChangeOrigin, true);
|
---|
261 | if (RT_FAILURE(rc))
|
---|
262 | VBClFatalError(("Failed to get display change request, rc=%Rrc\n", rc));
|
---|
263 | if (idx < VMW_MAX_HEADS)
|
---|
264 | {
|
---|
265 | acx[idx] = cx;
|
---|
266 | acy[idx] = cy;
|
---|
267 | if (fChangeOrigin)
|
---|
268 | adx[idx] = dx < INT32_MAX ? dx : 0;
|
---|
269 | if (fChangeOrigin)
|
---|
270 | ady[idx] = dy < INT32_MAX ? dy : 0;
|
---|
271 | afEnabled[idx] = fEnabled;
|
---|
272 | }
|
---|
273 | rc = VbglR3WaitEvent(VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST, 0, &events);
|
---|
274 | if (RT_FAILURE(rc) && rc != VERR_TIMEOUT && rc != VERR_INTERRUPTED)
|
---|
275 | VBClFatalError(("Failure waiting for event, rc=%Rrc\n", rc));
|
---|
276 | }
|
---|
277 | for (i = 0, cHeads = 0; i < VMW_MAX_HEADS; ++i)
|
---|
278 | {
|
---|
279 | if (afEnabled[i])
|
---|
280 | {
|
---|
281 | aRects[cHeads].x = (int16_t)adx[i];
|
---|
282 | aRects[cHeads].y = (int16_t)ady[i];
|
---|
283 | aRects[cHeads].w = (uint16_t)acx[i];
|
---|
284 | aRects[cHeads].h = (uint16_t)acy[i];
|
---|
285 | ++cHeads;
|
---|
286 | }
|
---|
287 | }
|
---|
288 | x11SendHints(&x11Context, aRects, cHeads);
|
---|
289 | x11GetScreenInfo(&x11Context);
|
---|
290 | }
|
---|
291 | }
|
---|
292 |
|
---|
293 | static struct VBCLSERVICE interface =
|
---|
294 | {
|
---|
295 | getPidFilePath,
|
---|
296 | VBClServiceDefaultHandler, /* Init */
|
---|
297 | run,
|
---|
298 | VBClServiceDefaultCleanup
|
---|
299 | }, *pInterface = &interface;
|
---|
300 |
|
---|
301 | struct VBCLSERVICE **VBClDisplaySVGAX11Service()
|
---|
302 | {
|
---|
303 | return &pInterface;
|
---|
304 | }
|
---|