1 | /* $Id: display-drm.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * X11 guest client - VMSVGA emulation resize event pass-through to drm guest
|
---|
4 | * driver.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2016-2022 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/err.h>
|
---|
47 | #include <iprt/string.h>
|
---|
48 | #include <iprt/initterm.h>
|
---|
49 | #include <iprt/message.h>
|
---|
50 | #include <iprt/thread.h>
|
---|
51 | #include <iprt/asm.h>
|
---|
52 | #include <unistd.h>
|
---|
53 | #include <stdio.h>
|
---|
54 | #include <limits.h>
|
---|
55 | #include <signal.h>
|
---|
56 |
|
---|
57 | #ifdef RT_OS_LINUX
|
---|
58 | # include <sys/ioctl.h>
|
---|
59 | #else /* Solaris and BSDs, in case they ever adopt the DRM driver. */
|
---|
60 | # include <sys/ioccom.h>
|
---|
61 | #endif
|
---|
62 |
|
---|
63 | /** Ioctl command to query vmwgfx version information. */
|
---|
64 | #define DRM_IOCTL_VERSION _IOWR('d', 0x00, struct DRMVERSION)
|
---|
65 | /** Ioctl command to set new screen layout. */
|
---|
66 | #define DRM_IOCTL_VMW_UPDATE_LAYOUT _IOW('d', 0x40 + 20, struct DRMVMWUPDATELAYOUT)
|
---|
67 | /** A driver name which identifies VMWare driver. */
|
---|
68 | #define DRM_DRIVER_NAME "vmwgfx"
|
---|
69 | /** VMWare driver compatible version number. On previous versions resizing does not seem work. */
|
---|
70 | #define DRM_DRIVER_VERSION_MAJOR_MIN (2)
|
---|
71 | #define DRM_DRIVER_VERSION_MINOR_MIN (10)
|
---|
72 |
|
---|
73 | /** VMWare char device driver minor numbers range. */
|
---|
74 | #define VMW_CONTROL_DEVICE_MINOR_START (64)
|
---|
75 | #define VMW_RENDER_DEVICE_MINOR_START (128)
|
---|
76 | #define VMW_RENDER_DEVICE_MINOR_END (192)
|
---|
77 |
|
---|
78 | /** Maximum number of supported screens. DRM and X11 both limit this to 32. */
|
---|
79 | /** @todo if this ever changes, dynamically allocate resizeable arrays in the
|
---|
80 | * context structure. */
|
---|
81 | #define VMW_MAX_HEADS (32)
|
---|
82 |
|
---|
83 | /** Name of DRM resize thread. */
|
---|
84 | #define DRM_RESIZE_THREAD_NAME "DrmResizeThread"
|
---|
85 |
|
---|
86 | /* Time in milliseconds to wait for host events. */
|
---|
87 | #define DRM_HOST_EVENT_RX_TIMEOUT_MS (500)
|
---|
88 |
|
---|
89 | /** DRM version structure. */
|
---|
90 | struct DRMVERSION
|
---|
91 | {
|
---|
92 | int cMajor;
|
---|
93 | int cMinor;
|
---|
94 | int cPatchLevel;
|
---|
95 | size_t cbName;
|
---|
96 | char *pszName;
|
---|
97 | size_t cbDate;
|
---|
98 | char *pszDate;
|
---|
99 | size_t cbDescription;
|
---|
100 | char *pszDescription;
|
---|
101 | };
|
---|
102 | AssertCompileSize(struct DRMVERSION, 8 + 7 * sizeof(void *));
|
---|
103 |
|
---|
104 | /** Rectangle structure for geometry of a single screen. */
|
---|
105 | struct DRMVMWRECT
|
---|
106 | {
|
---|
107 | int32_t x;
|
---|
108 | int32_t y;
|
---|
109 | uint32_t w;
|
---|
110 | uint32_t h;
|
---|
111 | };
|
---|
112 | AssertCompileSize(struct DRMVMWRECT, 16);
|
---|
113 |
|
---|
114 | /** Preferred screen layout information for DRM_VMW_UPDATE_LAYOUT IoCtl. The
|
---|
115 | * rects argument is a cast pointer to an array of drm_vmw_rect. */
|
---|
116 | struct DRMVMWUPDATELAYOUT
|
---|
117 | {
|
---|
118 | uint32_t cOutputs;
|
---|
119 | uint32_t u32Pad;
|
---|
120 | uint64_t ptrRects;
|
---|
121 | };
|
---|
122 | AssertCompileSize(struct DRMVMWUPDATELAYOUT, 16);
|
---|
123 |
|
---|
124 | /** These two parameters are mostly unused. Defined here in order to satisfy linking requirements. */
|
---|
125 | unsigned g_cRespawn = 0;
|
---|
126 | unsigned g_cVerbosity = 0;
|
---|
127 |
|
---|
128 | /** Path to the PID file. */
|
---|
129 | static const char g_szPidFile[RTPATH_MAX] = "/var/run/VBoxDRMClient";
|
---|
130 |
|
---|
131 | /** Global flag which is triggered when service requested to shutdown. */
|
---|
132 | static bool volatile g_fShutdown;
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Attempts to open DRM device by given path and check if it is
|
---|
136 | * compatible for screen resize.
|
---|
137 | *
|
---|
138 | * @return DRM device handle on success or NIL_RTFILE otherwise.
|
---|
139 | * @param szPathPattern Path name pattern to the DRM device.
|
---|
140 | * @param uInstance Driver / device instance.
|
---|
141 | */
|
---|
142 | static RTFILE drmTryDevice(const char *szPathPattern, uint8_t uInstance)
|
---|
143 | {
|
---|
144 | int rc = VERR_NOT_FOUND;
|
---|
145 | char szPath[PATH_MAX];
|
---|
146 | struct DRMVERSION vmwgfxVersion;
|
---|
147 | RTFILE hDevice = NIL_RTFILE;
|
---|
148 |
|
---|
149 | RT_ZERO(szPath);
|
---|
150 | RT_ZERO(vmwgfxVersion);
|
---|
151 |
|
---|
152 | rc = RTStrPrintf(szPath, sizeof(szPath), szPathPattern, uInstance);
|
---|
153 | if (RT_SUCCESS(rc))
|
---|
154 | {
|
---|
155 | rc = RTFileOpen(&hDevice, szPath, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
156 | if (RT_SUCCESS(rc))
|
---|
157 | {
|
---|
158 | char szVmwgfxDriverName[sizeof(DRM_DRIVER_NAME)];
|
---|
159 | RT_ZERO(szVmwgfxDriverName);
|
---|
160 |
|
---|
161 | vmwgfxVersion.cbName = sizeof(szVmwgfxDriverName);
|
---|
162 | vmwgfxVersion.pszName = szVmwgfxDriverName;
|
---|
163 |
|
---|
164 | /* Query driver version information and check if it can be used for screen resizing. */
|
---|
165 | rc = RTFileIoCtl(hDevice, DRM_IOCTL_VERSION, &vmwgfxVersion, sizeof(vmwgfxVersion), NULL);
|
---|
166 | if ( RT_SUCCESS(rc)
|
---|
167 | && strncmp(szVmwgfxDriverName, DRM_DRIVER_NAME, sizeof(DRM_DRIVER_NAME) - 1) == 0
|
---|
168 | && ( vmwgfxVersion.cMajor >= DRM_DRIVER_VERSION_MAJOR_MIN
|
---|
169 | || ( vmwgfxVersion.cMajor == DRM_DRIVER_VERSION_MAJOR_MIN
|
---|
170 | && vmwgfxVersion.cMinor >= DRM_DRIVER_VERSION_MINOR_MIN)))
|
---|
171 | {
|
---|
172 | VBClLogInfo("VBoxDRMClient: found compatible device: %s\n", szPath);
|
---|
173 | }
|
---|
174 | else
|
---|
175 | {
|
---|
176 | RTFileClose(hDevice);
|
---|
177 | hDevice = NIL_RTFILE;
|
---|
178 | rc = VERR_NOT_FOUND;
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 | else
|
---|
183 | {
|
---|
184 | VBClLogError("VBoxDRMClient: unable to construct path to DRM device: %Rrc\n", rc);
|
---|
185 | }
|
---|
186 |
|
---|
187 | return RT_SUCCESS(rc) ? hDevice : NIL_RTFILE;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Attempts to find and open DRM device to be used for screen resize.
|
---|
192 | *
|
---|
193 | * @return DRM device handle on success or NIL_RTFILE otherwise.
|
---|
194 | */
|
---|
195 | static RTFILE drmOpenVmwgfx(void)
|
---|
196 | {
|
---|
197 | /* Control devices for drm graphics driver control devices go from
|
---|
198 | * controlD64 to controlD127. Render node devices go from renderD128
|
---|
199 | * to renderD192. The driver takes resize hints via the control device
|
---|
200 | * on pre-4.10 (???) kernels and on the render device on newer ones.
|
---|
201 | * At first, try to find control device and render one if not found.
|
---|
202 | */
|
---|
203 | uint8_t i;
|
---|
204 | RTFILE hDevice = NIL_RTFILE;
|
---|
205 |
|
---|
206 | /* Lookup control device. */
|
---|
207 | for (i = VMW_CONTROL_DEVICE_MINOR_START; i < VMW_RENDER_DEVICE_MINOR_START; i++)
|
---|
208 | {
|
---|
209 | hDevice = drmTryDevice("/dev/dri/controlD%u", i);
|
---|
210 | if (hDevice != NIL_RTFILE)
|
---|
211 | return hDevice;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /* Lookup render device. */
|
---|
215 | for (i = VMW_RENDER_DEVICE_MINOR_START; i <= VMW_RENDER_DEVICE_MINOR_END; i++)
|
---|
216 | {
|
---|
217 | hDevice = drmTryDevice("/dev/dri/renderD%u", i);
|
---|
218 | if (hDevice != NIL_RTFILE)
|
---|
219 | return hDevice;
|
---|
220 | }
|
---|
221 |
|
---|
222 | VBClLogError("VBoxDRMClient: unable to find DRM device\n");
|
---|
223 |
|
---|
224 | return hDevice;
|
---|
225 | }
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * This function converts input monitors layout array passed from DevVMM
|
---|
229 | * into monitors layout array to be passed to DRM stack.
|
---|
230 | *
|
---|
231 | * @return VINF_SUCCESS on success, IPRT error code otherwise.
|
---|
232 | * @param aDisplaysIn Input displays array.
|
---|
233 | * @param cDisplaysIn Number of elements in input displays array.
|
---|
234 | * @param aDisplaysOut Output displays array.
|
---|
235 | * @param cDisplaysOutMax Number of elements in output displays array.
|
---|
236 | * @param pcActualDisplays Number of displays to report to DRM stack (number of enabled displays).
|
---|
237 | */
|
---|
238 | static int drmValidateLayout(VMMDevDisplayDef *aDisplaysIn, uint32_t cDisplaysIn,
|
---|
239 | struct DRMVMWRECT *aDisplaysOut, uint32_t cDisplaysOutMax, uint32_t *pcActualDisplays)
|
---|
240 | {
|
---|
241 | /* This array is a cache of what was received from DevVMM so far.
|
---|
242 | * DevVMM may send to us partial information bout scree layout. This
|
---|
243 | * cache remembers entire picture. */
|
---|
244 | static struct VMMDevDisplayDef aVmMonitorsCache[VMW_MAX_HEADS];
|
---|
245 | /* Number of valid (enabled) displays in output array. */
|
---|
246 | uint32_t cDisplaysOut = 0;
|
---|
247 | /* Flag indicates that current layout cache is consistent and can be passed to DRM stack. */
|
---|
248 | bool fValid = true;
|
---|
249 |
|
---|
250 | /* Make sure input array fits cache size. */
|
---|
251 | if (cDisplaysIn > VMW_MAX_HEADS)
|
---|
252 | {
|
---|
253 | VBClLogError("VBoxDRMClient: unable to validate screen layout: input (%u) array does not fit to cache size (%u)\n",
|
---|
254 | cDisplaysIn, VMW_MAX_HEADS);
|
---|
255 | return VERR_INVALID_PARAMETER;
|
---|
256 | }
|
---|
257 |
|
---|
258 | /* Make sure there is enough space in output array. */
|
---|
259 | if (cDisplaysIn > cDisplaysOutMax)
|
---|
260 | {
|
---|
261 | VBClLogError("VBoxDRMClient: unable to validate screen layout: input array (%u) is bigger than output one (%u)\n",
|
---|
262 | cDisplaysIn, cDisplaysOut);
|
---|
263 | return VERR_INVALID_PARAMETER;
|
---|
264 | }
|
---|
265 |
|
---|
266 | /* Make sure input and output arrays are of non-zero size. */
|
---|
267 | if (!(cDisplaysIn > 0 && cDisplaysOutMax > 0))
|
---|
268 | {
|
---|
269 | VBClLogError("VBoxDRMClient: unable to validate screen layout: invalid size of either input (%u) or output display array\n",
|
---|
270 | cDisplaysIn, cDisplaysOutMax);
|
---|
271 | return VERR_INVALID_PARAMETER;
|
---|
272 | }
|
---|
273 |
|
---|
274 | /* Update cache. */
|
---|
275 | for (uint32_t i = 0; i < cDisplaysIn; i++)
|
---|
276 | {
|
---|
277 | uint32_t idDisplay = aDisplaysIn[i].idDisplay;
|
---|
278 | if (idDisplay < VMW_MAX_HEADS)
|
---|
279 | {
|
---|
280 | aVmMonitorsCache[idDisplay].idDisplay = idDisplay;
|
---|
281 | aVmMonitorsCache[idDisplay].fDisplayFlags = aDisplaysIn[i].fDisplayFlags;
|
---|
282 | aVmMonitorsCache[idDisplay].cBitsPerPixel = aDisplaysIn[i].cBitsPerPixel;
|
---|
283 | aVmMonitorsCache[idDisplay].cx = aDisplaysIn[i].cx;
|
---|
284 | aVmMonitorsCache[idDisplay].cy = aDisplaysIn[i].cy;
|
---|
285 | aVmMonitorsCache[idDisplay].xOrigin = aDisplaysIn[i].xOrigin;
|
---|
286 | aVmMonitorsCache[idDisplay].yOrigin = aDisplaysIn[i].yOrigin;
|
---|
287 | }
|
---|
288 | else
|
---|
289 | {
|
---|
290 | VBClLogError("VBoxDRMClient: received display ID (0x%x, position %u) is invalid\n", idDisplay, i);
|
---|
291 | /* If monitor configuration cannot be placed into cache, consider entire cache is invalid. */
|
---|
292 | fValid = false;
|
---|
293 | }
|
---|
294 | }
|
---|
295 |
|
---|
296 | /* Now, go though complete cache and check if it is valid. */
|
---|
297 | for (uint32_t i = 0; i < VMW_MAX_HEADS; i++)
|
---|
298 | {
|
---|
299 | if (i == 0)
|
---|
300 | {
|
---|
301 | if (aVmMonitorsCache[i].fDisplayFlags & VMMDEV_DISPLAY_DISABLED)
|
---|
302 | {
|
---|
303 | VBClLogError("VBoxDRMClient: unable to validate screen layout: first monitor is not allowed to be disabled");
|
---|
304 | fValid = false;
|
---|
305 | }
|
---|
306 | else
|
---|
307 | cDisplaysOut++;
|
---|
308 | }
|
---|
309 | else
|
---|
310 | {
|
---|
311 | /* Check if there is no hole in between monitors (i.e., if current monitor is enabled, but privious one does not). */
|
---|
312 | if ( !(aVmMonitorsCache[i].fDisplayFlags & VMMDEV_DISPLAY_DISABLED)
|
---|
313 | && aVmMonitorsCache[i - 1].fDisplayFlags & VMMDEV_DISPLAY_DISABLED)
|
---|
314 | {
|
---|
315 | VBClLogError("VBoxDRMClient: unable to validate screen layout: there is a hole in displays layout config, "
|
---|
316 | "monitor (%u) is ENABLED while (%u) does not\n", i, i - 1);
|
---|
317 | fValid = false;
|
---|
318 | }
|
---|
319 | else
|
---|
320 | {
|
---|
321 | /* Align displays next to each other (if needed) and check if there is no holes in between monitors. */
|
---|
322 | if (!(aVmMonitorsCache[i].fDisplayFlags & VMMDEV_DISPLAY_ORIGIN))
|
---|
323 | {
|
---|
324 | aVmMonitorsCache[i].xOrigin = aVmMonitorsCache[i - 1].xOrigin + aVmMonitorsCache[i - 1].cx;
|
---|
325 | aVmMonitorsCache[i].yOrigin = aVmMonitorsCache[i - 1].yOrigin;
|
---|
326 | }
|
---|
327 |
|
---|
328 | /* Only count enabled monitors. */
|
---|
329 | if (!(aVmMonitorsCache[i].fDisplayFlags & VMMDEV_DISPLAY_DISABLED))
|
---|
330 | cDisplaysOut++;
|
---|
331 | }
|
---|
332 | }
|
---|
333 | }
|
---|
334 |
|
---|
335 | /* Copy out layout data. */
|
---|
336 | if (fValid)
|
---|
337 | {
|
---|
338 | for (uint32_t i = 0; i < cDisplaysOut; i++)
|
---|
339 | {
|
---|
340 | aDisplaysOut[i].x = aVmMonitorsCache[i].xOrigin;
|
---|
341 | aDisplaysOut[i].y = aVmMonitorsCache[i].yOrigin;
|
---|
342 | aDisplaysOut[i].w = aVmMonitorsCache[i].cx;
|
---|
343 | aDisplaysOut[i].h = aVmMonitorsCache[i].cy;
|
---|
344 |
|
---|
345 | VBClLogInfo("VBoxDRMClient: update monitor %u parameters: %dx%d, (%d, %d)\n",
|
---|
346 | i,
|
---|
347 | aDisplaysOut[i].w, aDisplaysOut[i].h,
|
---|
348 | aDisplaysOut[i].x, aDisplaysOut[i].y);
|
---|
349 |
|
---|
350 | }
|
---|
351 |
|
---|
352 | *pcActualDisplays = cDisplaysOut;
|
---|
353 | }
|
---|
354 |
|
---|
355 | return (fValid && cDisplaysOut > 0) ? VINF_SUCCESS : VERR_INVALID_PARAMETER;
|
---|
356 | }
|
---|
357 |
|
---|
358 | /**
|
---|
359 | * This function sends screen layout data to DRM stack.
|
---|
360 | *
|
---|
361 | * @return VINF_SUCCESS on success, IPRT error code otherwise.
|
---|
362 | * @param hDevice Handle to opened DRM device.
|
---|
363 | * @param paRects Array of screen configuration data.
|
---|
364 | * @param cRects Number of elements in screen configuration array.
|
---|
365 | */
|
---|
366 | static int drmSendHints(RTFILE hDevice, struct DRMVMWRECT *paRects, uint32_t cRects)
|
---|
367 | {
|
---|
368 | int rc = 0;
|
---|
369 | uid_t curuid;
|
---|
370 |
|
---|
371 | /* Store real user id. */
|
---|
372 | curuid = getuid();
|
---|
373 |
|
---|
374 | /* Chenge effective user id. */
|
---|
375 | if (setreuid(0, 0) == 0)
|
---|
376 | {
|
---|
377 | struct DRMVMWUPDATELAYOUT ioctlLayout;
|
---|
378 |
|
---|
379 | RT_ZERO(ioctlLayout);
|
---|
380 | ioctlLayout.cOutputs = cRects;
|
---|
381 | ioctlLayout.ptrRects = (uint64_t)paRects;
|
---|
382 |
|
---|
383 | rc = RTFileIoCtl(hDevice, DRM_IOCTL_VMW_UPDATE_LAYOUT,
|
---|
384 | &ioctlLayout, sizeof(ioctlLayout), NULL);
|
---|
385 |
|
---|
386 | if (setreuid(curuid, 0) != 0)
|
---|
387 | {
|
---|
388 | VBClLogError("VBoxDRMClient: reset of setreuid failed after drm ioctl");
|
---|
389 | rc = VERR_ACCESS_DENIED;
|
---|
390 | }
|
---|
391 | }
|
---|
392 | else
|
---|
393 | {
|
---|
394 | VBClLogError("VBoxDRMClient: setreuid failed during drm ioctl\n");
|
---|
395 | rc = VERR_ACCESS_DENIED;
|
---|
396 | }
|
---|
397 |
|
---|
398 | return rc;
|
---|
399 | }
|
---|
400 |
|
---|
401 | /**
|
---|
402 | * This function converts vmwgfx monitors layout data into an array of monitors offsets
|
---|
403 | * and sends it back to the host in order to ensure that host and guest have the same
|
---|
404 | * monitors layout representation.
|
---|
405 | *
|
---|
406 | * @return IPRT status code.
|
---|
407 | * @param cDisplays Number of displays (elements in pDisplays).
|
---|
408 | * @param pDisplays Displays parameters as it was sent to vmwgfx driver.
|
---|
409 | */
|
---|
410 | static int drmSendMonitorPositions(uint32_t cDisplays, struct DRMVMWRECT *pDisplays)
|
---|
411 | {
|
---|
412 | static RTPOINT aPositions[VMW_MAX_HEADS];
|
---|
413 |
|
---|
414 | if (!pDisplays || !cDisplays || cDisplays > VMW_MAX_HEADS)
|
---|
415 | {
|
---|
416 | return VERR_INVALID_PARAMETER;
|
---|
417 | }
|
---|
418 |
|
---|
419 | /* Prepare monitor offsets list to be sent to the host. */
|
---|
420 | for (uint32_t i = 0; i < cDisplays; i++)
|
---|
421 | {
|
---|
422 | aPositions[i].x = pDisplays[i].x;
|
---|
423 | aPositions[i].y = pDisplays[i].y;
|
---|
424 | }
|
---|
425 |
|
---|
426 | return VbglR3SeamlessSendMonitorPositions(cDisplays, aPositions);
|
---|
427 | }
|
---|
428 |
|
---|
429 | /** Worker thread for resize task. */
|
---|
430 | static DECLCALLBACK(int) drmResizeWorker(RTTHREAD ThreadSelf, void *pvUser)
|
---|
431 | {
|
---|
432 | int rc;
|
---|
433 | RTFILE hDevice = (RTFILE)pvUser;
|
---|
434 |
|
---|
435 | RT_NOREF1(ThreadSelf);
|
---|
436 |
|
---|
437 | AssertReturn(hDevice, VERR_INVALID_PARAMETER);
|
---|
438 |
|
---|
439 | for (;;)
|
---|
440 | {
|
---|
441 | /* Do not acknowledge the first event we query for to pick up old events,
|
---|
442 | * e.g. from before a guest reboot. */
|
---|
443 | bool fAck = false;
|
---|
444 |
|
---|
445 | uint32_t events;
|
---|
446 |
|
---|
447 | VMMDevDisplayDef aDisplaysIn[VMW_MAX_HEADS];
|
---|
448 | uint32_t cDisplaysIn = 0;
|
---|
449 |
|
---|
450 | struct DRMVMWRECT aDisplaysOut[VMW_MAX_HEADS];
|
---|
451 | uint32_t cDisplaysOut = 0;
|
---|
452 |
|
---|
453 | RT_ZERO(aDisplaysIn);
|
---|
454 | RT_ZERO(aDisplaysOut);
|
---|
455 |
|
---|
456 | /* Query the first size without waiting. This lets us e.g. pick up
|
---|
457 | * the last event before a guest reboot when we start again after. */
|
---|
458 | rc = VbglR3GetDisplayChangeRequestMulti(VMW_MAX_HEADS, &cDisplaysIn, aDisplaysIn, fAck);
|
---|
459 | fAck = true;
|
---|
460 | if (RT_FAILURE(rc))
|
---|
461 | {
|
---|
462 | VBClLogError("Failed to get display change request, rc=%Rrc\n", rc);
|
---|
463 | }
|
---|
464 | else
|
---|
465 | {
|
---|
466 | /* Validate displays layout and push it to DRM stack if valid. */
|
---|
467 | rc = drmValidateLayout(aDisplaysIn, cDisplaysIn, aDisplaysOut, sizeof(aDisplaysOut), &cDisplaysOut);
|
---|
468 | if (RT_SUCCESS(rc))
|
---|
469 | {
|
---|
470 | rc = drmSendHints(hDevice, aDisplaysOut, cDisplaysOut);
|
---|
471 | VBClLogInfo("VBoxDRMClient: push screen layout data of %u display(s) to DRM stack has %s (%Rrc)\n",
|
---|
472 | cDisplaysOut, RT_SUCCESS(rc) ? "succeeded" : "failed", rc);
|
---|
473 | /* In addition, notify host that configuration was successfully applied to the guest vmwgfx driver. */
|
---|
474 | if (RT_SUCCESS(rc))
|
---|
475 | {
|
---|
476 | rc = drmSendMonitorPositions(cDisplaysOut, aDisplaysOut);
|
---|
477 | if (RT_FAILURE(rc))
|
---|
478 | {
|
---|
479 | VBClLogError("VBoxDRMClient: cannot send host notification: %Rrc\n", rc);
|
---|
480 | }
|
---|
481 | }
|
---|
482 | }
|
---|
483 | else
|
---|
484 | {
|
---|
485 | VBClLogError("VBoxDRMClient: displays layout is invalid, will not notify guest driver, rc=%Rrc\n", rc);
|
---|
486 | }
|
---|
487 | }
|
---|
488 |
|
---|
489 | do
|
---|
490 | {
|
---|
491 | rc = VbglR3WaitEvent(VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST, DRM_HOST_EVENT_RX_TIMEOUT_MS, &events);
|
---|
492 | } while (rc == VERR_TIMEOUT && !ASMAtomicReadBool(&g_fShutdown));
|
---|
493 |
|
---|
494 | if (ASMAtomicReadBool(&g_fShutdown))
|
---|
495 | {
|
---|
496 | VBClLogInfo("VBoxDRMClient: exitting resize thread: shutdown requested\n");
|
---|
497 | break;
|
---|
498 | }
|
---|
499 | else if (RT_FAILURE(rc))
|
---|
500 | {
|
---|
501 | VBClLogFatalError("Failure waiting for event, rc=%Rrc\n", rc);
|
---|
502 | }
|
---|
503 | }
|
---|
504 |
|
---|
505 | return 0;
|
---|
506 | }
|
---|
507 |
|
---|
508 | static void drmRequestShutdown(int sig)
|
---|
509 | {
|
---|
510 | RT_NOREF1(sig);
|
---|
511 |
|
---|
512 | ASMAtomicWriteBool(&g_fShutdown, true);
|
---|
513 | }
|
---|
514 |
|
---|
515 | int main(int argc, char *argv[])
|
---|
516 | {
|
---|
517 | RTFILE hDevice = NIL_RTFILE;
|
---|
518 | RTFILE hPidFile;
|
---|
519 |
|
---|
520 | RTTHREAD drmResizeThread;
|
---|
521 | int rcDrmResizeThread = 0;
|
---|
522 |
|
---|
523 | int rc = RTR3InitExe(argc, &argv, 0);
|
---|
524 | if (RT_FAILURE(rc))
|
---|
525 | return RTMsgInitFailure(rc);
|
---|
526 |
|
---|
527 | rc = VbglR3InitUser();
|
---|
528 | if (RT_FAILURE(rc))
|
---|
529 | VBClLogFatalError("VBoxDRMClient: VbglR3InitUser failed: %Rrc", rc);
|
---|
530 |
|
---|
531 | rc = VBClLogCreate("");
|
---|
532 | if (RT_FAILURE(rc))
|
---|
533 | VBClLogFatalError("VBoxDRMClient: failed to setup logging, rc=%Rrc\n", rc);
|
---|
534 |
|
---|
535 | PRTLOGGER pReleaseLog = RTLogRelGetDefaultInstance();
|
---|
536 | if (pReleaseLog)
|
---|
537 | {
|
---|
538 | rc = RTLogDestinations(pReleaseLog, "stdout");
|
---|
539 | if (RT_FAILURE(rc))
|
---|
540 | VBClLogFatalError("VBoxDRMClient: failed to redirert error output, rc=%Rrc", rc);
|
---|
541 | }
|
---|
542 | else
|
---|
543 | {
|
---|
544 | VBClLogFatalError("VBoxDRMClient: failed to get logger instance");
|
---|
545 | }
|
---|
546 |
|
---|
547 | /* Check PID file before attempting to initialize anything. */
|
---|
548 | rc = VbglR3PidFile(g_szPidFile, &hPidFile);
|
---|
549 | if (rc == VERR_FILE_LOCK_VIOLATION)
|
---|
550 | {
|
---|
551 | VBClLogInfo("VBoxDRMClient: already running, exiting\n");
|
---|
552 | return RTEXITCODE_SUCCESS;
|
---|
553 | }
|
---|
554 | else if (RT_FAILURE(rc))
|
---|
555 | {
|
---|
556 | VBClLogError("VBoxDRMClient: unable to lock PID file (%Rrc), exiting\n", rc);
|
---|
557 | return RTEXITCODE_FAILURE;
|
---|
558 | }
|
---|
559 |
|
---|
560 | hDevice = drmOpenVmwgfx();
|
---|
561 | if (hDevice == NIL_RTFILE)
|
---|
562 | return RTEXITCODE_FAILURE;
|
---|
563 |
|
---|
564 | rc = VbglR3CtlFilterMask(VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST, 0);
|
---|
565 | if (RT_FAILURE(rc))
|
---|
566 | {
|
---|
567 | VBClLogFatalError("Failed to request display change events, rc=%Rrc\n", rc);
|
---|
568 | return RTEXITCODE_FAILURE;
|
---|
569 | }
|
---|
570 | rc = VbglR3AcquireGuestCaps(VMMDEV_GUEST_SUPPORTS_GRAPHICS, 0, false);
|
---|
571 | if (rc == VERR_RESOURCE_BUSY) /* Someone else has already acquired it. */
|
---|
572 | {
|
---|
573 | return RTEXITCODE_FAILURE;
|
---|
574 | }
|
---|
575 | if (RT_FAILURE(rc))
|
---|
576 | {
|
---|
577 | VBClLogFatalError("Failed to register resizing support, rc=%Rrc\n", rc);
|
---|
578 | return RTEXITCODE_FAILURE;
|
---|
579 | }
|
---|
580 |
|
---|
581 | /* Setup signals: gracefully terminate on SIGINT, SIGTERM. */
|
---|
582 | if ( signal(SIGINT, drmRequestShutdown) == SIG_ERR
|
---|
583 | || signal(SIGTERM, drmRequestShutdown) == SIG_ERR)
|
---|
584 | {
|
---|
585 | VBClLogError("VBoxDRMClient: unable to setup signals\n");
|
---|
586 | return RTEXITCODE_FAILURE;
|
---|
587 | }
|
---|
588 |
|
---|
589 | /* Attempt to start DRM resize task. */
|
---|
590 | rc = RTThreadCreate(&drmResizeThread, drmResizeWorker, (void *)hDevice, 0,
|
---|
591 | RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, DRM_RESIZE_THREAD_NAME);
|
---|
592 | if (RT_SUCCESS(rc))
|
---|
593 | {
|
---|
594 | rc = RTThreadWait(drmResizeThread, RT_INDEFINITE_WAIT, &rcDrmResizeThread);
|
---|
595 | VBClLogInfo("VBoxDRMClient: %s thread exitted with status %Rrc\n", DRM_RESIZE_THREAD_NAME, rcDrmResizeThread);
|
---|
596 | rc |= rcDrmResizeThread;
|
---|
597 | }
|
---|
598 |
|
---|
599 | RTFileClose(hDevice);
|
---|
600 |
|
---|
601 | VBClLogInfo("VBoxDRMClient: releasing PID file lock\n");
|
---|
602 | VbglR3ClosePidFile(g_szPidFile, hPidFile);
|
---|
603 |
|
---|
604 | return rc == 0 ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
605 | }
|
---|