1 | /* $Id: getmode.c 56228 2015-06-03 16:54:04Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox X11 Additions graphics driver dynamic video mode functions.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2014 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 | #include "vboxvideo.h"
|
---|
19 | #include <VBox/VMMDev.h>
|
---|
20 |
|
---|
21 | #define NEED_XF86_TYPES
|
---|
22 | #include <iprt/string.h>
|
---|
23 |
|
---|
24 | #include "xf86.h"
|
---|
25 |
|
---|
26 | #ifdef XORG_7X
|
---|
27 | # include <stdio.h>
|
---|
28 | # include <stdlib.h>
|
---|
29 | #endif
|
---|
30 |
|
---|
31 | #ifdef VBOXVIDEO_13
|
---|
32 | # ifdef RT_OS_LINUX
|
---|
33 | # include <linux/input.h>
|
---|
34 | # ifndef EVIOCGRAB
|
---|
35 | # define EVIOCGRAB _IOW('E', 0x90, int)
|
---|
36 | # endif
|
---|
37 | # ifndef KEY_SWITCHVIDEOMODE
|
---|
38 | # define KEY_SWITCHVIDEOMODE 227
|
---|
39 | # endif
|
---|
40 | # include <dirent.h>
|
---|
41 | # include <errno.h>
|
---|
42 | # include <fcntl.h>
|
---|
43 | # include <unistd.h>
|
---|
44 | # endif /* RT_OS_LINUX */
|
---|
45 | #endif /* VBOXVIDEO_13 */
|
---|
46 |
|
---|
47 | /**************************************************************************
|
---|
48 | * Main functions *
|
---|
49 | **************************************************************************/
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Fills a display mode M with a built-in mode of name pszName and dimensions
|
---|
53 | * cx and cy.
|
---|
54 | */
|
---|
55 | static void vboxFillDisplayMode(ScrnInfoPtr pScrn, DisplayModePtr m,
|
---|
56 | const char *pszName, unsigned cx, unsigned cy)
|
---|
57 | {
|
---|
58 | VBOXPtr pVBox = pScrn->driverPrivate;
|
---|
59 | char szName[256];
|
---|
60 | DisplayModePtr pPrev = m->prev;
|
---|
61 | DisplayModePtr pNext = m->next;
|
---|
62 |
|
---|
63 | if (!pszName)
|
---|
64 | {
|
---|
65 | sprintf(szName, "%ux%u", cx, cy);
|
---|
66 | pszName = szName;
|
---|
67 | }
|
---|
68 | TRACE_LOG("pszName=%s, cx=%u, cy=%u\n", pszName, cx, cy);
|
---|
69 | if (m->name)
|
---|
70 | free((void*)m->name);
|
---|
71 | memset(m, '\0', sizeof(*m));
|
---|
72 | m->prev = pPrev;
|
---|
73 | m->next = pNext;
|
---|
74 | m->status = MODE_OK;
|
---|
75 | m->type = M_T_BUILTIN;
|
---|
76 | /* Older versions of VBox only support screen widths which are a multiple
|
---|
77 | * of 8 */
|
---|
78 | if (pVBox->fAnyX)
|
---|
79 | m->HDisplay = cx;
|
---|
80 | else
|
---|
81 | m->HDisplay = cx & ~7;
|
---|
82 | m->HSyncStart = m->HDisplay + 2;
|
---|
83 | m->HSyncEnd = m->HDisplay + 4;
|
---|
84 | m->HTotal = m->HDisplay + 6;
|
---|
85 | m->VDisplay = cy;
|
---|
86 | m->VSyncStart = m->VDisplay + 2;
|
---|
87 | m->VSyncEnd = m->VDisplay + 4;
|
---|
88 | m->VTotal = m->VDisplay + 6;
|
---|
89 | m->Clock = m->HTotal * m->VTotal * 60 / 1000; /* kHz */
|
---|
90 | m->name = xnfstrdup(pszName);
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Allocates an empty display mode and links it into the doubly linked list of
|
---|
95 | * modes pointed to by pScrn->modes. Returns a pointer to the newly allocated
|
---|
96 | * memory.
|
---|
97 | */
|
---|
98 | static DisplayModePtr vboxAddEmptyScreenMode(ScrnInfoPtr pScrn)
|
---|
99 | {
|
---|
100 | DisplayModePtr pMode = xnfcalloc(sizeof(DisplayModeRec), 1);
|
---|
101 |
|
---|
102 | TRACE_ENTRY();
|
---|
103 | if (!pScrn->modes)
|
---|
104 | {
|
---|
105 | pScrn->modes = pMode;
|
---|
106 | pMode->next = pMode;
|
---|
107 | pMode->prev = pMode;
|
---|
108 | }
|
---|
109 | else
|
---|
110 | {
|
---|
111 | pMode->next = pScrn->modes;
|
---|
112 | pMode->prev = pScrn->modes->prev;
|
---|
113 | pMode->next->prev = pMode;
|
---|
114 | pMode->prev->next = pMode;
|
---|
115 | }
|
---|
116 | return pMode;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Create display mode entries in the screen information structure for each
|
---|
121 | * of the graphics modes that we wish to support, that is:
|
---|
122 | * - A dynamic mode in first place which will be updated by the RandR code.
|
---|
123 | * - Any modes that the user requested in xorg.conf/XFree86Config.
|
---|
124 | */
|
---|
125 | void vboxAddModes(ScrnInfoPtr pScrn)
|
---|
126 | {
|
---|
127 | unsigned cx = 0, cy = 0, cIndex = 0;
|
---|
128 | unsigned i;
|
---|
129 | DisplayModePtr pMode;
|
---|
130 |
|
---|
131 | /* Add two dynamic mode entries. When we receive a new size hint we will
|
---|
132 | * update whichever of these is not current. */
|
---|
133 | pMode = vboxAddEmptyScreenMode(pScrn);
|
---|
134 | vboxFillDisplayMode(pScrn, pMode, NULL, 800, 600);
|
---|
135 | pMode = vboxAddEmptyScreenMode(pScrn);
|
---|
136 | vboxFillDisplayMode(pScrn, pMode, NULL, 800, 600);
|
---|
137 | /* Add any modes specified by the user. We assume here that the mode names
|
---|
138 | * reflect the mode sizes. */
|
---|
139 | for (i = 0; pScrn->display->modes && pScrn->display->modes[i]; i++)
|
---|
140 | {
|
---|
141 | if (sscanf(pScrn->display->modes[i], "%ux%u", &cx, &cy) == 2)
|
---|
142 | {
|
---|
143 | pMode = vboxAddEmptyScreenMode(pScrn);
|
---|
144 | vboxFillDisplayMode(pScrn, pMode, pScrn->display->modes[i], cx, cy);
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | /** Set the initial values for the guest screen size hints to standard values
|
---|
150 | * in case nothing else is available. */
|
---|
151 | void VBoxInitialiseSizeHints(ScrnInfoPtr pScrn)
|
---|
152 | {
|
---|
153 | VBOXPtr pVBox = VBOXGetRec(pScrn);
|
---|
154 | unsigned i;
|
---|
155 |
|
---|
156 | for (i = 0; i < pVBox->cScreens; ++i)
|
---|
157 | {
|
---|
158 | pVBox->pScreens[i].aPreferredSize.cx = 800;
|
---|
159 | pVBox->pScreens[i].aPreferredSize.cy = 600;
|
---|
160 | pVBox->pScreens[i].afConnected = true;
|
---|
161 | }
|
---|
162 | /* Set up the first mode correctly to match the requested initial mode. */
|
---|
163 | pScrn->modes->HDisplay = pVBox->pScreens[0].aPreferredSize.cx;
|
---|
164 | pScrn->modes->VDisplay = pVBox->pScreens[0].aPreferredSize.cy;
|
---|
165 | }
|
---|
166 |
|
---|
167 | static bool useHardwareCursor(uint32_t fCursorCapabilities)
|
---|
168 | {
|
---|
169 | if ( !(fCursorCapabilities & VMMDEV_MOUSE_HOST_CANNOT_HWPOINTER)
|
---|
170 | && (fCursorCapabilities & VMMDEV_MOUSE_HOST_WANTS_ABSOLUTE))
|
---|
171 | return true;
|
---|
172 | return false;
|
---|
173 | }
|
---|
174 |
|
---|
175 | static void compareAndMaybeSetUseHardwareCursor(VBOXPtr pVBox, uint32_t fCursorCapabilities, bool *pfChanged, bool fSet)
|
---|
176 | {
|
---|
177 | if (pVBox->fUseHardwareCursor != useHardwareCursor(fCursorCapabilities))
|
---|
178 | *pfChanged = true;
|
---|
179 | if (fSet)
|
---|
180 | pVBox->fUseHardwareCursor = useHardwareCursor(fCursorCapabilities);
|
---|
181 | }
|
---|
182 |
|
---|
183 | #define SIZE_HINTS_PROPERTY "VBOX_SIZE_HINTS"
|
---|
184 | #define SIZE_HINTS_MISMATCH_PROPERTY "VBOX_SIZE_HINTS_MISMATCH"
|
---|
185 | #define MOUSE_CAPABILITIES_PROPERTY "VBOX_MOUSE_CAPABILITIES"
|
---|
186 |
|
---|
187 | #define COMPARE_AND_MAYBE_SET(pDest, src, pfChanged, fSet) \
|
---|
188 | do { \
|
---|
189 | if (*(pDest) != (src)) \
|
---|
190 | { \
|
---|
191 | if (fSet) \
|
---|
192 | *(pDest) = (src); \
|
---|
193 | *(pfChanged) = true; \
|
---|
194 | } \
|
---|
195 | } while(0)
|
---|
196 |
|
---|
197 | /** Read in information about the most recent size hints and cursor
|
---|
198 | * capabilities requested for the guest screens from a root window property set
|
---|
199 | * by an X11 client. Information obtained via HGSMI takes priority. */
|
---|
200 | void vbvxReadSizesAndCursorIntegrationFromProperties(ScrnInfoPtr pScrn, bool *pfNeedUpdate)
|
---|
201 | {
|
---|
202 | VBOXPtr pVBox = VBOXGetRec(pScrn);
|
---|
203 | size_t cPropertyElements, cDummy;
|
---|
204 | int32_t *paModeHints, *pfCursorCapabilities;
|
---|
205 | unsigned i;
|
---|
206 | bool fChanged;
|
---|
207 | bool fNeedUpdate = false;
|
---|
208 | int32_t fSizeMismatch = false;
|
---|
209 |
|
---|
210 | if (vbvxGetIntegerPropery(pScrn, SIZE_HINTS_PROPERTY, &cPropertyElements, &paModeHints) != VINF_SUCCESS)
|
---|
211 | paModeHints = NULL;
|
---|
212 | if (paModeHints != NULL)
|
---|
213 | for (i = 0; i < cPropertyElements / 2 && i < pVBox->cScreens; ++i)
|
---|
214 | {
|
---|
215 | VBVAMODEHINT *pVBVAModeHint = &pVBox->paVBVAModeHints[i];
|
---|
216 | int32_t iSizeHint = paModeHints[i * 2];
|
---|
217 | int32_t iLocation = paModeHints[i * 2 + 1];
|
---|
218 | bool fNoHGSMI = !pVBox->fHaveHGSMIModeHints || pVBVAModeHint->magic != VBVAMODEHINT_MAGIC;
|
---|
219 |
|
---|
220 | fChanged = false;
|
---|
221 | if (iSizeHint != 0)
|
---|
222 | {
|
---|
223 | if (iSizeHint == -1)
|
---|
224 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].afConnected, false, &fChanged, fNoHGSMI);
|
---|
225 | else
|
---|
226 | {
|
---|
227 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredSize.cx, (iSizeHint >> 16) & 0x8fff, &fChanged, fNoHGSMI);
|
---|
228 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredSize.cy, iSizeHint & 0x8fff, &fChanged, fNoHGSMI);
|
---|
229 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].afConnected, true, &fChanged, fNoHGSMI);
|
---|
230 | }
|
---|
231 | if (iLocation == -1)
|
---|
232 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].afHaveLocation, false, &fChanged, fNoHGSMI);
|
---|
233 | else
|
---|
234 | {
|
---|
235 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredLocation.x, (iLocation >> 16) & 0x8fff, &fChanged,
|
---|
236 | fNoHGSMI);
|
---|
237 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredLocation.y, iLocation & 0x8fff, &fChanged, fNoHGSMI);
|
---|
238 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].afHaveLocation, true, &fChanged, fNoHGSMI);
|
---|
239 | }
|
---|
240 | if (fChanged && fNoHGSMI)
|
---|
241 | fNeedUpdate = true;
|
---|
242 | if (fChanged && !fNoHGSMI)
|
---|
243 | fSizeMismatch = true;
|
---|
244 | }
|
---|
245 | }
|
---|
246 | fChanged = false;
|
---|
247 | if ( vbvxGetIntegerPropery(pScrn, MOUSE_CAPABILITIES_PROPERTY, &cDummy, &pfCursorCapabilities) == VINF_SUCCESS
|
---|
248 | && cDummy == 1)
|
---|
249 | compareAndMaybeSetUseHardwareCursor(pVBox, *pfCursorCapabilities, &fChanged, !pVBox->fHaveHGSMIModeHints);
|
---|
250 | if (fChanged && !pVBox->fHaveHGSMIModeHints)
|
---|
251 | fNeedUpdate = true;
|
---|
252 | if (fChanged && pVBox->fHaveHGSMIModeHints)
|
---|
253 | fSizeMismatch = true;
|
---|
254 | vbvxSetIntegerPropery(pScrn, SIZE_HINTS_MISMATCH_PROPERTY, 1, &fSizeMismatch, false);
|
---|
255 | if (pfNeedUpdate != NULL && fNeedUpdate)
|
---|
256 | *pfNeedUpdate = true;
|
---|
257 | }
|
---|
258 |
|
---|
259 | /** Read in information about the most recent size hints and cursor
|
---|
260 | * capabilities requested for the guest screens from HGSMI. */
|
---|
261 | void vbvxReadSizesAndCursorIntegrationFromHGSMI(ScrnInfoPtr pScrn, bool *pfNeedUpdate)
|
---|
262 | {
|
---|
263 | VBOXPtr pVBox = VBOXGetRec(pScrn);
|
---|
264 | int rc;
|
---|
265 | unsigned i;
|
---|
266 | bool fChanged = false;
|
---|
267 | uint32_t fCursorCapabilities;
|
---|
268 |
|
---|
269 | if (!pVBox->fHaveHGSMIModeHints)
|
---|
270 | return;
|
---|
271 | rc = VBoxHGSMIGetModeHints(&pVBox->guestCtx, pVBox->cScreens, pVBox->paVBVAModeHints);
|
---|
272 | VBVXASSERT(rc == VINF_SUCCESS, ("VBoxHGSMIGetModeHints failed, rc=%d.\n", rc));
|
---|
273 | for (i = 0; i < pVBox->cScreens; ++i)
|
---|
274 | if (pVBox->paVBVAModeHints[i].magic == VBVAMODEHINT_MAGIC)
|
---|
275 | {
|
---|
276 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredSize.cx, pVBox->paVBVAModeHints[i].cx & 0x8fff, &fChanged, true);
|
---|
277 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredSize.cy, pVBox->paVBVAModeHints[i].cy & 0x8fff, &fChanged, true);
|
---|
278 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].afConnected, RT_BOOL(pVBox->paVBVAModeHints[i].fEnabled), &fChanged, true);
|
---|
279 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredLocation.x, (int32_t)pVBox->paVBVAModeHints[i].dx & 0x8fff, &fChanged,
|
---|
280 | true);
|
---|
281 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredLocation.y, (int32_t)pVBox->paVBVAModeHints[i].dy & 0x8fff, &fChanged,
|
---|
282 | true);
|
---|
283 | if (pVBox->paVBVAModeHints[i].dx != ~(uint32_t)0 && pVBox->paVBVAModeHints[i].dy != ~(uint32_t)0)
|
---|
284 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].afHaveLocation, true, &fChanged, true);
|
---|
285 | else
|
---|
286 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].afHaveLocation, false, &fChanged, true);
|
---|
287 | }
|
---|
288 | rc = VBoxQueryConfHGSMI(&pVBox->guestCtx, VBOX_VBVA_CONF32_CURSOR_CAPABILITIES, &fCursorCapabilities);
|
---|
289 | VBVXASSERT(rc == VINF_SUCCESS, ("Getting VBOX_VBVA_CONF32_CURSOR_CAPABILITIES failed, rc=%d.\n", rc));
|
---|
290 | compareAndMaybeSetUseHardwareCursor(pVBox, fCursorCapabilities, &fChanged, true);
|
---|
291 | if (pfNeedUpdate != NULL && fChanged)
|
---|
292 | *pfNeedUpdate = true;
|
---|
293 | }
|
---|
294 |
|
---|
295 | #undef COMPARE_AND_MAYBE_SET
|
---|
296 |
|
---|
297 | #ifdef VBOXVIDEO_13
|
---|
298 | # ifdef RT_OS_LINUX
|
---|
299 | /** We have this for two purposes: one is to ensure that the X server is woken
|
---|
300 | * up when we get a video ACPI event. Two is to grab ACPI video events to
|
---|
301 | * prevent gnome-settings-daemon from seeing them, as older versions ignored
|
---|
302 | * the time stamp and handled them at the wrong time. */
|
---|
303 | static void acpiEventHandler(int fd, void *pvData)
|
---|
304 | {
|
---|
305 | ScreenPtr pScreen = (ScreenPtr)pvData;
|
---|
306 | VBOXPtr pVBox = VBOXGetRec(xf86Screens[pScreen->myNum]);
|
---|
307 | struct input_event event;
|
---|
308 | ssize_t rc;
|
---|
309 |
|
---|
310 | do
|
---|
311 | rc = read(fd, &event, sizeof(event));
|
---|
312 | while (rc > 0 || (rc == -1 && errno == EINTR));
|
---|
313 | /* Why do they return EAGAIN instead of zero bytes read like everyone else does? */
|
---|
314 | VBVXASSERT(rc != -1 || errno == EAGAIN, ("Reading ACPI input event failed.\n"));
|
---|
315 | }
|
---|
316 |
|
---|
317 | void vbvxSetUpLinuxACPI(ScreenPtr pScreen)
|
---|
318 | {
|
---|
319 | VBOXPtr pVBox = VBOXGetRec(xf86Screens[pScreen->myNum]);
|
---|
320 | struct dirent *pDirent;
|
---|
321 | DIR *pDir;
|
---|
322 | int fd = -1;
|
---|
323 |
|
---|
324 | if (pVBox->fdACPIDevices != -1 || pVBox->hACPIEventHandler != NULL)
|
---|
325 | FatalError("ACPI input file descriptor not initialised correctly.\n");
|
---|
326 | pDir = opendir("/dev/input");
|
---|
327 | if (pDir == NULL)
|
---|
328 | return;
|
---|
329 | for (pDirent = readdir(pDir); pDirent != NULL; pDirent = readdir(pDir))
|
---|
330 | {
|
---|
331 | if (strncmp(pDirent->d_name, "event", sizeof("event") - 1) == 0)
|
---|
332 | {
|
---|
333 | #define BITS_PER_BLOCK (sizeof(unsigned long) * 8)
|
---|
334 | char szFile[64] = "/dev/input/";
|
---|
335 | char szDevice[64] = "";
|
---|
336 | unsigned long afKeys[KEY_MAX / BITS_PER_BLOCK];
|
---|
337 |
|
---|
338 | strncat(szFile, pDirent->d_name, sizeof(szFile) - sizeof("/dev/input/"));
|
---|
339 | if (fd != -1)
|
---|
340 | close(fd);
|
---|
341 | fd = open(szFile, O_RDONLY | O_NONBLOCK);
|
---|
342 | if ( fd == -1
|
---|
343 | || ioctl(fd, EVIOCGNAME(sizeof(szDevice)), szDevice) == -1
|
---|
344 | || strcmp(szDevice, "Video Bus") != 0)
|
---|
345 | continue;
|
---|
346 | if ( ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(afKeys)), afKeys) == -1
|
---|
347 | || (( afKeys[KEY_SWITCHVIDEOMODE / BITS_PER_BLOCK]
|
---|
348 | >> KEY_SWITCHVIDEOMODE % BITS_PER_BLOCK) & 1) == 0)
|
---|
349 | break;
|
---|
350 | if (ioctl(fd, EVIOCGRAB, (void *)1) != 0)
|
---|
351 | break;
|
---|
352 | pVBox->hACPIEventHandler
|
---|
353 | = xf86AddGeneralHandler(fd, acpiEventHandler, pScreen);
|
---|
354 | if (pVBox->hACPIEventHandler == NULL)
|
---|
355 | break;
|
---|
356 | pVBox->fdACPIDevices = fd;
|
---|
357 | fd = -1;
|
---|
358 | break;
|
---|
359 | #undef BITS_PER_BLOCK
|
---|
360 | }
|
---|
361 | }
|
---|
362 | if (fd != -1)
|
---|
363 | close(fd);
|
---|
364 | closedir(pDir);
|
---|
365 | }
|
---|
366 |
|
---|
367 | void vbvxCleanUpLinuxACPI(ScreenPtr pScreen)
|
---|
368 | {
|
---|
369 | VBOXPtr pVBox = VBOXGetRec(xf86Screens[pScreen->myNum]);
|
---|
370 | if (pVBox->fdACPIDevices != -1)
|
---|
371 | close(pVBox->fdACPIDevices);
|
---|
372 | pVBox->fdACPIDevices = -1;
|
---|
373 | xf86RemoveGeneralHandler(pVBox->hACPIEventHandler);
|
---|
374 | pVBox->hACPIEventHandler = NULL;
|
---|
375 | }
|
---|
376 | # endif /* RT_OS_LINUX */
|
---|
377 | #endif /* VBOXVIDEO_13 */
|
---|