1 | /* $Id: getmode.c 55393 2015-04-22 20:01:10Z 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, 1024, 768);
|
---|
135 | pMode = vboxAddEmptyScreenMode(pScrn);
|
---|
136 | vboxFillDisplayMode(pScrn, pMode, NULL, 1024, 768);
|
---|
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 | DisplayModePtr pMode;
|
---|
155 | unsigned i;
|
---|
156 |
|
---|
157 | for (i = 0; i < pVBox->cScreens; ++i)
|
---|
158 | {
|
---|
159 | pVBox->pScreens[i].aPreferredSize.cx = 1024;
|
---|
160 | pVBox->pScreens[i].aPreferredSize.cy = 768;
|
---|
161 | pVBox->pScreens[i].afConnected = true;
|
---|
162 | }
|
---|
163 | /* Set up the first mode correctly to match the requested initial mode. */
|
---|
164 | pScrn->modes->HDisplay = pVBox->pScreens[0].aPreferredSize.cx;
|
---|
165 | pScrn->modes->VDisplay = pVBox->pScreens[0].aPreferredSize.cy;
|
---|
166 | }
|
---|
167 |
|
---|
168 | static bool useHardwareCursor(uint32_t fCursorCapabilities)
|
---|
169 | {
|
---|
170 | if ( !(fCursorCapabilities & VMMDEV_MOUSE_HOST_CANNOT_HWPOINTER)
|
---|
171 | && (fCursorCapabilities & VMMDEV_MOUSE_HOST_WANTS_ABSOLUTE))
|
---|
172 | return true;
|
---|
173 | return false;
|
---|
174 | }
|
---|
175 |
|
---|
176 | static void compareAndMaybeSetUseHardwareCursor(VBOXPtr pVBox, uint32_t fCursorCapabilities, bool *pfChanged, bool fSet)
|
---|
177 | {
|
---|
178 | if (pVBox->fUseHardwareCursor != useHardwareCursor(fCursorCapabilities))
|
---|
179 | *pfChanged = true;
|
---|
180 | if (fSet)
|
---|
181 | pVBox->fUseHardwareCursor = useHardwareCursor(fCursorCapabilities);
|
---|
182 | }
|
---|
183 |
|
---|
184 | #define SIZE_HINTS_PROPERTY "VBOX_SIZE_HINTS"
|
---|
185 | #define SIZE_HINTS_MISMATCH_PROPERTY "VBOX_SIZE_HINTS_MISMATCH"
|
---|
186 | #define MOUSE_CAPABILITIES_PROPERTY "VBOX_MOUSE_CAPABILITIES"
|
---|
187 |
|
---|
188 | #define COMPARE_AND_MAYBE_SET(pDest, src, pfChanged, fSet) \
|
---|
189 | do { \
|
---|
190 | if (*(pDest) != (src)) \
|
---|
191 | { \
|
---|
192 | if (fSet) \
|
---|
193 | *(pDest) = (src); \
|
---|
194 | *(pfChanged) = true; \
|
---|
195 | } \
|
---|
196 | } while(0)
|
---|
197 |
|
---|
198 | /** Read in information about the most recent size hints and cursor
|
---|
199 | * capabilities requested for the guest screens from a root window property set
|
---|
200 | * by an X11 client. Information obtained via HGSMI takes priority. */
|
---|
201 | void vbvxReadSizesAndCursorIntegrationFromProperties(ScrnInfoPtr pScrn, bool *pfNeedUpdate)
|
---|
202 | {
|
---|
203 | VBOXPtr pVBox = VBOXGetRec(pScrn);
|
---|
204 | size_t cPropertyElements, cDummy;
|
---|
205 | int32_t *paModeHints, *pfCursorCapabilities;
|
---|
206 | int rc;
|
---|
207 | unsigned i;
|
---|
208 | bool fChanged;
|
---|
209 | bool fNeedUpdate = false;
|
---|
210 | int32_t fSizeMismatch = false;
|
---|
211 |
|
---|
212 | if (vbvxGetIntegerPropery(pScrn, SIZE_HINTS_PROPERTY, &cPropertyElements, &paModeHints) != VINF_SUCCESS)
|
---|
213 | paModeHints = NULL;
|
---|
214 | if (paModeHints != NULL)
|
---|
215 | for (i = 0; i < cPropertyElements / 2 && i < pVBox->cScreens; ++i)
|
---|
216 | {
|
---|
217 | VBVAMODEHINT *pVBVAModeHint = &pVBox->paVBVAModeHints[i];
|
---|
218 | int32_t iSizeHint = paModeHints[i * 2];
|
---|
219 | int32_t iLocation = paModeHints[i * 2 + 1];
|
---|
220 | bool fNoHGSMI = !pVBox->fHaveHGSMIModeHints || pVBVAModeHint->magic != VBVAMODEHINT_MAGIC;
|
---|
221 |
|
---|
222 | fChanged = false;
|
---|
223 | if (iSizeHint != 0)
|
---|
224 | {
|
---|
225 | if (iSizeHint == -1)
|
---|
226 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].afConnected, false, &fChanged, fNoHGSMI);
|
---|
227 | else
|
---|
228 | {
|
---|
229 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredSize.cx, (iSizeHint >> 16) & 0x8fff, &fChanged, fNoHGSMI);
|
---|
230 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredSize.cy, iSizeHint & 0x8fff, &fChanged, fNoHGSMI);
|
---|
231 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].afConnected, true, &fChanged, fNoHGSMI);
|
---|
232 | }
|
---|
233 | if (iLocation == -1)
|
---|
234 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].afHaveLocation, false, &fChanged, fNoHGSMI);
|
---|
235 | else
|
---|
236 | {
|
---|
237 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredLocation.x, (iLocation >> 16) & 0x8fff, &fChanged,
|
---|
238 | fNoHGSMI);
|
---|
239 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredLocation.y, iLocation & 0x8fff, &fChanged, fNoHGSMI);
|
---|
240 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].afHaveLocation, true, &fChanged, fNoHGSMI);
|
---|
241 | }
|
---|
242 | if (fChanged && fNoHGSMI)
|
---|
243 | fNeedUpdate = true;
|
---|
244 | if (fChanged && !fNoHGSMI)
|
---|
245 | fSizeMismatch = true;
|
---|
246 | }
|
---|
247 | }
|
---|
248 | fChanged = false;
|
---|
249 | if ( vbvxGetIntegerPropery(pScrn, MOUSE_CAPABILITIES_PROPERTY, &cDummy, &pfCursorCapabilities) == VINF_SUCCESS
|
---|
250 | && cDummy == 1)
|
---|
251 | compareAndMaybeSetUseHardwareCursor(pVBox, *pfCursorCapabilities, &fChanged, !pVBox->fHaveHGSMIModeHints);
|
---|
252 | if (fChanged && !pVBox->fHaveHGSMIModeHints)
|
---|
253 | fNeedUpdate = true;
|
---|
254 | if (fChanged && pVBox->fHaveHGSMIModeHints)
|
---|
255 | fSizeMismatch = true;
|
---|
256 | vbvxSetIntegerPropery(pScrn, SIZE_HINTS_MISMATCH_PROPERTY, 1, &fSizeMismatch, false);
|
---|
257 | if (pfNeedUpdate != NULL && fNeedUpdate)
|
---|
258 | *pfNeedUpdate = true;
|
---|
259 | }
|
---|
260 |
|
---|
261 | /** Read in information about the most recent size hints and cursor
|
---|
262 | * capabilities requested for the guest screens from HGSMI. */
|
---|
263 | void vbvxReadSizesAndCursorIntegrationFromHGSMI(ScrnInfoPtr pScrn, bool *pfNeedUpdate)
|
---|
264 | {
|
---|
265 | VBOXPtr pVBox = VBOXGetRec(pScrn);
|
---|
266 | int rc;
|
---|
267 | unsigned i;
|
---|
268 | bool fChanged = false;
|
---|
269 | uint32_t fCursorCapabilities;
|
---|
270 |
|
---|
271 | if (!pVBox->fHaveHGSMIModeHints)
|
---|
272 | return;
|
---|
273 | rc = VBoxHGSMIGetModeHints(&pVBox->guestCtx, pVBox->cScreens, pVBox->paVBVAModeHints);
|
---|
274 | VBVXASSERT(rc == VINF_SUCCESS, ("VBoxHGSMIGetModeHints failed, rc=%d.\n", rc));
|
---|
275 | for (i = 0; i < pVBox->cScreens; ++i)
|
---|
276 | if (pVBox->paVBVAModeHints[i].magic == VBVAMODEHINT_MAGIC)
|
---|
277 | {
|
---|
278 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredSize.cx, pVBox->paVBVAModeHints[i].cx & 0x8fff, &fChanged, true);
|
---|
279 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredSize.cy, pVBox->paVBVAModeHints[i].cy & 0x8fff, &fChanged, true);
|
---|
280 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].afConnected, RT_BOOL(pVBox->paVBVAModeHints[i].fEnabled), &fChanged, true);
|
---|
281 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredLocation.x, (int32_t)pVBox->paVBVAModeHints[i].dx & 0x8fff, &fChanged,
|
---|
282 | true);
|
---|
283 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredLocation.y, (int32_t)pVBox->paVBVAModeHints[i].dy & 0x8fff, &fChanged,
|
---|
284 | true);
|
---|
285 | if (pVBox->paVBVAModeHints[i].dx != ~(uint32_t)0 && pVBox->paVBVAModeHints[i].dy != ~(uint32_t)0)
|
---|
286 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].afHaveLocation, true, &fChanged, true);
|
---|
287 | else
|
---|
288 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].afHaveLocation, false, &fChanged, true);
|
---|
289 | }
|
---|
290 | rc = VBoxQueryConfHGSMI(&pVBox->guestCtx, VBOX_VBVA_CONF32_CURSOR_CAPABILITIES, &fCursorCapabilities);
|
---|
291 | VBVXASSERT(rc == VINF_SUCCESS, ("Getting VBOX_VBVA_CONF32_CURSOR_CAPABILITIES failed, rc=%d.\n", rc));
|
---|
292 | compareAndMaybeSetUseHardwareCursor(pVBox, fCursorCapabilities, &fChanged, true);
|
---|
293 | if (pfNeedUpdate != NULL && fChanged)
|
---|
294 | *pfNeedUpdate = true;
|
---|
295 | }
|
---|
296 |
|
---|
297 | #undef COMPARE_AND_MAYBE_SET
|
---|
298 |
|
---|
299 | #ifdef VBOXVIDEO_13
|
---|
300 | # ifdef RT_OS_LINUX
|
---|
301 | /** We have this for two purposes: one is to ensure that the X server is woken
|
---|
302 | * up when we get a video ACPI event. Two is to grab ACPI video events to
|
---|
303 | * prevent gnome-settings-daemon from seeing them, as older versions ignored
|
---|
304 | * the time stamp and handled them at the wrong time. */
|
---|
305 | static void acpiEventHandler(int fd, void *pvData)
|
---|
306 | {
|
---|
307 | ScreenPtr pScreen = (ScreenPtr)pvData;
|
---|
308 | VBOXPtr pVBox = VBOXGetRec(xf86Screens[pScreen->myNum]);
|
---|
309 | struct input_event event;
|
---|
310 | ssize_t rc;
|
---|
311 |
|
---|
312 | do
|
---|
313 | rc = read(fd, &event, sizeof(event));
|
---|
314 | while (rc > 0 || (rc == -1 && errno == EINTR));
|
---|
315 | /* Why do they return EAGAIN instead of zero bytes read like everyone else does? */
|
---|
316 | VBVXASSERT(rc != -1 || errno == EAGAIN, ("Reading ACPI input event failed.\n"));
|
---|
317 | }
|
---|
318 |
|
---|
319 | void vbvxSetUpLinuxACPI(ScreenPtr pScreen)
|
---|
320 | {
|
---|
321 | VBOXPtr pVBox = VBOXGetRec(xf86Screens[pScreen->myNum]);
|
---|
322 | struct dirent *pDirent;
|
---|
323 | DIR *pDir;
|
---|
324 | int fd = -1;
|
---|
325 |
|
---|
326 | if (pVBox->fdACPIDevices != -1 || pVBox->hACPIEventHandler != NULL)
|
---|
327 | FatalError("ACPI input file descriptor not initialised correctly.\n");
|
---|
328 | pDir = opendir("/dev/input");
|
---|
329 | if (pDir == NULL)
|
---|
330 | return;
|
---|
331 | for (pDirent = readdir(pDir); pDirent != NULL; pDirent = readdir(pDir))
|
---|
332 | {
|
---|
333 | if (strncmp(pDirent->d_name, "event", sizeof("event") - 1) == 0)
|
---|
334 | {
|
---|
335 | #define BITS_PER_BLOCK (sizeof(unsigned long) * 8)
|
---|
336 | char szFile[64] = "/dev/input/";
|
---|
337 | char szDevice[64] = "";
|
---|
338 | unsigned long afKeys[KEY_MAX / BITS_PER_BLOCK];
|
---|
339 |
|
---|
340 | strncat(szFile, pDirent->d_name, sizeof(szFile) - sizeof("/dev/input/"));
|
---|
341 | if (fd != -1)
|
---|
342 | close(fd);
|
---|
343 | fd = open(szFile, O_RDONLY | O_NONBLOCK);
|
---|
344 | if ( fd == -1
|
---|
345 | || ioctl(fd, EVIOCGNAME(sizeof(szDevice)), szDevice) == -1
|
---|
346 | || strcmp(szDevice, "Video Bus") != 0)
|
---|
347 | continue;
|
---|
348 | if ( ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(afKeys)), afKeys) == -1
|
---|
349 | || (( afKeys[KEY_SWITCHVIDEOMODE / BITS_PER_BLOCK]
|
---|
350 | >> KEY_SWITCHVIDEOMODE % BITS_PER_BLOCK) & 1) == 0)
|
---|
351 | break;
|
---|
352 | if (ioctl(fd, EVIOCGRAB, (void *)1) != 0)
|
---|
353 | break;
|
---|
354 | pVBox->hACPIEventHandler
|
---|
355 | = xf86AddGeneralHandler(fd, acpiEventHandler, pScreen);
|
---|
356 | if (pVBox->hACPIEventHandler == NULL)
|
---|
357 | break;
|
---|
358 | pVBox->fdACPIDevices = fd;
|
---|
359 | fd = -1;
|
---|
360 | break;
|
---|
361 | #undef BITS_PER_BLOCK
|
---|
362 | }
|
---|
363 | }
|
---|
364 | if (fd != -1)
|
---|
365 | close(fd);
|
---|
366 | closedir(pDir);
|
---|
367 | }
|
---|
368 |
|
---|
369 | void vbvxCleanUpLinuxACPI(ScreenPtr pScreen)
|
---|
370 | {
|
---|
371 | VBOXPtr pVBox = VBOXGetRec(xf86Screens[pScreen->myNum]);
|
---|
372 | if (pVBox->fdACPIDevices != -1)
|
---|
373 | close(pVBox->fdACPIDevices);
|
---|
374 | pVBox->fdACPIDevices = -1;
|
---|
375 | xf86RemoveGeneralHandler(pVBox->hACPIEventHandler);
|
---|
376 | pVBox->hACPIEventHandler = NULL;
|
---|
377 | }
|
---|
378 | # endif /* RT_OS_LINUX */
|
---|
379 | #endif /* VBOXVIDEO_13 */
|
---|