1 | /* $Id: getmode.c 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox X11 Additions graphics driver dynamic video mode functions.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * Permission is hereby granted, free of charge, to any person
|
---|
10 | * obtaining a copy of this software and associated documentation
|
---|
11 | * files (the "Software"), to deal in the Software without
|
---|
12 | * restriction, including without limitation the rights to use,
|
---|
13 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
14 | * copies of the Software, and to permit persons to whom the
|
---|
15 | * Software is furnished to do so, subject to the following
|
---|
16 | * conditions:
|
---|
17 | *
|
---|
18 | * The above copyright notice and this permission notice shall be
|
---|
19 | * included in all copies or substantial portions of the Software.
|
---|
20 | *
|
---|
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
28 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
29 | */
|
---|
30 |
|
---|
31 | #include "vboxvideo.h"
|
---|
32 |
|
---|
33 | #define NEED_XF86_TYPES
|
---|
34 | #include "xf86.h"
|
---|
35 |
|
---|
36 | #ifdef XORG_7X
|
---|
37 | # include <stdio.h>
|
---|
38 | # include <stdlib.h>
|
---|
39 | # include <string.h>
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #ifdef VBOXVIDEO_13
|
---|
43 | # ifdef RT_OS_LINUX
|
---|
44 | # include <linux/input.h>
|
---|
45 | # ifndef EVIOCGRAB
|
---|
46 | # define EVIOCGRAB _IOW('E', 0x90, int)
|
---|
47 | # endif
|
---|
48 | # ifndef KEY_SWITCHVIDEOMODE
|
---|
49 | # define KEY_SWITCHVIDEOMODE 227
|
---|
50 | # endif
|
---|
51 | # include <dirent.h>
|
---|
52 | # include <errno.h>
|
---|
53 | # include <fcntl.h>
|
---|
54 | # include <unistd.h>
|
---|
55 | # endif /* RT_OS_LINUX */
|
---|
56 | #endif /* VBOXVIDEO_13 */
|
---|
57 |
|
---|
58 | /**************************************************************************
|
---|
59 | * Main functions *
|
---|
60 | **************************************************************************/
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Fills a display mode M with a built-in mode of name pszName and dimensions
|
---|
64 | * cx and cy.
|
---|
65 | */
|
---|
66 | static void vboxFillDisplayMode(ScrnInfoPtr pScrn, DisplayModePtr m,
|
---|
67 | const char *pszName, unsigned cx, unsigned cy)
|
---|
68 | {
|
---|
69 | VBOXPtr pVBox = pScrn->driverPrivate;
|
---|
70 | char szName[256];
|
---|
71 | DisplayModePtr pPrev = m->prev;
|
---|
72 | DisplayModePtr pNext = m->next;
|
---|
73 |
|
---|
74 | if (!pszName)
|
---|
75 | {
|
---|
76 | sprintf(szName, "%ux%u", cx, cy);
|
---|
77 | pszName = szName;
|
---|
78 | }
|
---|
79 | TRACE_LOG("pszName=%s, cx=%u, cy=%u\n", pszName, cx, cy);
|
---|
80 | if (m->name)
|
---|
81 | free((void*)m->name);
|
---|
82 | memset(m, '\0', sizeof(*m));
|
---|
83 | m->prev = pPrev;
|
---|
84 | m->next = pNext;
|
---|
85 | m->status = MODE_OK;
|
---|
86 | m->type = M_T_BUILTIN;
|
---|
87 | /* Older versions of VBox only support screen widths which are a multiple
|
---|
88 | * of 8 */
|
---|
89 | if (pVBox->fAnyX)
|
---|
90 | m->HDisplay = cx;
|
---|
91 | else
|
---|
92 | m->HDisplay = cx & ~7;
|
---|
93 | m->HSyncStart = m->HDisplay + 2;
|
---|
94 | m->HSyncEnd = m->HDisplay + 4;
|
---|
95 | m->HTotal = m->HDisplay + 6;
|
---|
96 | m->VDisplay = cy;
|
---|
97 | m->VSyncStart = m->VDisplay + 2;
|
---|
98 | m->VSyncEnd = m->VDisplay + 4;
|
---|
99 | m->VTotal = m->VDisplay + 6;
|
---|
100 | m->Clock = m->HTotal * m->VTotal * 60 / 1000; /* kHz */
|
---|
101 | m->name = xnfstrdup(pszName);
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Allocates an empty display mode and links it into the doubly linked list of
|
---|
106 | * modes pointed to by pScrn->modes. Returns a pointer to the newly allocated
|
---|
107 | * memory.
|
---|
108 | */
|
---|
109 | static DisplayModePtr vboxAddEmptyScreenMode(ScrnInfoPtr pScrn)
|
---|
110 | {
|
---|
111 | DisplayModePtr pMode = xnfcalloc(sizeof(DisplayModeRec), 1);
|
---|
112 |
|
---|
113 | TRACE_ENTRY();
|
---|
114 | if (!pScrn->modes)
|
---|
115 | {
|
---|
116 | pScrn->modes = pMode;
|
---|
117 | pMode->next = pMode;
|
---|
118 | pMode->prev = pMode;
|
---|
119 | }
|
---|
120 | else
|
---|
121 | {
|
---|
122 | pMode->next = pScrn->modes;
|
---|
123 | pMode->prev = pScrn->modes->prev;
|
---|
124 | pMode->next->prev = pMode;
|
---|
125 | pMode->prev->next = pMode;
|
---|
126 | }
|
---|
127 | return pMode;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Create display mode entries in the screen information structure for each
|
---|
132 | * of the graphics modes that we wish to support, that is:
|
---|
133 | * - A dynamic mode in first place which will be updated by the RandR code.
|
---|
134 | * - Any modes that the user requested in xorg.conf/XFree86Config.
|
---|
135 | */
|
---|
136 | void vboxAddModes(ScrnInfoPtr pScrn)
|
---|
137 | {
|
---|
138 | unsigned cx = 0;
|
---|
139 | unsigned cy = 0;
|
---|
140 | unsigned i;
|
---|
141 | DisplayModePtr pMode;
|
---|
142 |
|
---|
143 | /* Add two dynamic mode entries. When we receive a new size hint we will
|
---|
144 | * update whichever of these is not current. */
|
---|
145 | pMode = vboxAddEmptyScreenMode(pScrn);
|
---|
146 | vboxFillDisplayMode(pScrn, pMode, NULL, 800, 600);
|
---|
147 | pMode = vboxAddEmptyScreenMode(pScrn);
|
---|
148 | vboxFillDisplayMode(pScrn, pMode, NULL, 800, 600);
|
---|
149 | /* Add any modes specified by the user. We assume here that the mode names
|
---|
150 | * reflect the mode sizes. */
|
---|
151 | for (i = 0; pScrn->display->modes && pScrn->display->modes[i]; i++)
|
---|
152 | {
|
---|
153 | if (sscanf(pScrn->display->modes[i], "%ux%u", &cx, &cy) == 2)
|
---|
154 | {
|
---|
155 | pMode = vboxAddEmptyScreenMode(pScrn);
|
---|
156 | vboxFillDisplayMode(pScrn, pMode, pScrn->display->modes[i], cx, cy);
|
---|
157 | }
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | /** Set the initial values for the guest screen size hints to standard values
|
---|
162 | * in case nothing else is available. */
|
---|
163 | void VBoxInitialiseSizeHints(ScrnInfoPtr pScrn)
|
---|
164 | {
|
---|
165 | VBOXPtr pVBox = VBOXGetRec(pScrn);
|
---|
166 | unsigned i;
|
---|
167 |
|
---|
168 | for (i = 0; i < pVBox->cScreens; ++i)
|
---|
169 | {
|
---|
170 | pVBox->pScreens[i].aPreferredSize.cx = 800;
|
---|
171 | pVBox->pScreens[i].aPreferredSize.cy = 600;
|
---|
172 | pVBox->pScreens[i].afConnected = true;
|
---|
173 | }
|
---|
174 | /* Set up the first mode correctly to match the requested initial mode. */
|
---|
175 | pScrn->modes->HDisplay = pVBox->pScreens[0].aPreferredSize.cx;
|
---|
176 | pScrn->modes->VDisplay = pVBox->pScreens[0].aPreferredSize.cy;
|
---|
177 | }
|
---|
178 |
|
---|
179 | static Bool useHardwareCursor(uint32_t fCursorCapabilities)
|
---|
180 | {
|
---|
181 | if (fCursorCapabilities & VBOX_VBVA_CURSOR_CAPABILITY_HARDWARE)
|
---|
182 | return true;
|
---|
183 | return false;
|
---|
184 | }
|
---|
185 |
|
---|
186 | static void compareAndMaybeSetUseHardwareCursor(VBOXPtr pVBox, uint32_t fCursorCapabilities, Bool *pfChanged, Bool fSet)
|
---|
187 | {
|
---|
188 | if (pVBox->fUseHardwareCursor != useHardwareCursor(fCursorCapabilities))
|
---|
189 | *pfChanged = true;
|
---|
190 | if (fSet)
|
---|
191 | pVBox->fUseHardwareCursor = useHardwareCursor(fCursorCapabilities);
|
---|
192 | }
|
---|
193 |
|
---|
194 | #define COMPARE_AND_MAYBE_SET(pDest, src, pfChanged, fSet) \
|
---|
195 | do { \
|
---|
196 | if (*(pDest) != (src)) \
|
---|
197 | { \
|
---|
198 | if (fSet) \
|
---|
199 | *(pDest) = (src); \
|
---|
200 | *(pfChanged) = true; \
|
---|
201 | } \
|
---|
202 | } while(0)
|
---|
203 |
|
---|
204 | /** Read in information about the most recent size hints and cursor
|
---|
205 | * capabilities requested for the guest screens from HGSMI. */
|
---|
206 | void vbvxReadSizesAndCursorIntegrationFromHGSMI(ScrnInfoPtr pScrn, Bool *pfNeedUpdate)
|
---|
207 | {
|
---|
208 | VBOXPtr pVBox = VBOXGetRec(pScrn);
|
---|
209 | int rc;
|
---|
210 | unsigned i;
|
---|
211 | Bool fChanged = false;
|
---|
212 | uint32_t fCursorCapabilities;
|
---|
213 |
|
---|
214 | if (!pVBox->fHaveHGSMIModeHints)
|
---|
215 | return;
|
---|
216 | rc = VBoxHGSMIGetModeHints(&pVBox->guestCtx, pVBox->cScreens, pVBox->paVBVAModeHints);
|
---|
217 | AssertMsg(rc == VINF_SUCCESS, ("VBoxHGSMIGetModeHints failed, rc=%d.\n", rc));
|
---|
218 | for (i = 0; i < pVBox->cScreens; ++i)
|
---|
219 | if (pVBox->paVBVAModeHints[i].magic == VBVAMODEHINT_MAGIC)
|
---|
220 | {
|
---|
221 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredSize.cx, pVBox->paVBVAModeHints[i].cx & 0x8fff, &fChanged, true);
|
---|
222 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredSize.cy, pVBox->paVBVAModeHints[i].cy & 0x8fff, &fChanged, true);
|
---|
223 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].afConnected, RT_BOOL(pVBox->paVBVAModeHints[i].fEnabled), &fChanged, true);
|
---|
224 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredLocation.x, (int32_t)pVBox->paVBVAModeHints[i].dx & 0x8fff, &fChanged,
|
---|
225 | true);
|
---|
226 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredLocation.y, (int32_t)pVBox->paVBVAModeHints[i].dy & 0x8fff, &fChanged,
|
---|
227 | true);
|
---|
228 | if (pVBox->paVBVAModeHints[i].dx != ~(uint32_t)0 && pVBox->paVBVAModeHints[i].dy != ~(uint32_t)0)
|
---|
229 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].afHaveLocation, true, &fChanged, true);
|
---|
230 | else
|
---|
231 | COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].afHaveLocation, false, &fChanged, true);
|
---|
232 | }
|
---|
233 | rc = VBoxQueryConfHGSMI(&pVBox->guestCtx, VBOX_VBVA_CONF32_CURSOR_CAPABILITIES, &fCursorCapabilities);
|
---|
234 | AssertMsg(rc == VINF_SUCCESS, ("Getting VBOX_VBVA_CONF32_CURSOR_CAPABILITIES failed, rc=%d.\n", rc));
|
---|
235 | compareAndMaybeSetUseHardwareCursor(pVBox, fCursorCapabilities, &fChanged, true);
|
---|
236 | if (pfNeedUpdate != NULL && fChanged)
|
---|
237 | *pfNeedUpdate = true;
|
---|
238 | }
|
---|
239 |
|
---|
240 | #undef COMPARE_AND_MAYBE_SET
|
---|
241 |
|
---|
242 | #ifdef VBOXVIDEO_13
|
---|
243 | # ifdef RT_OS_LINUX
|
---|
244 | /** We have this for two purposes: one is to ensure that the X server is woken
|
---|
245 | * up when we get a video ACPI event. Two is to grab ACPI video events to
|
---|
246 | * prevent gnome-settings-daemon from seeing them, as older versions ignored
|
---|
247 | * the time stamp and handled them at the wrong time. */
|
---|
248 | static void acpiEventHandler(int fd, void *pvData)
|
---|
249 | {
|
---|
250 | struct input_event event;
|
---|
251 | ssize_t rc;
|
---|
252 | RT_NOREF(pvData);
|
---|
253 |
|
---|
254 | do
|
---|
255 | rc = read(fd, &event, sizeof(event));
|
---|
256 | while (rc > 0 || (rc == -1 && errno == EINTR));
|
---|
257 | /* Why do they return EAGAIN instead of zero bytes read like everyone else does? */
|
---|
258 | AssertMsg(rc != -1 || errno == EAGAIN, ("Reading ACPI input event failed.\n"));
|
---|
259 | }
|
---|
260 |
|
---|
261 | void vbvxSetUpLinuxACPI(ScreenPtr pScreen)
|
---|
262 | {
|
---|
263 | static const char s_szDevInput[] = "/dev/input/";
|
---|
264 | struct dirent *pDirent;
|
---|
265 | char szFile[sizeof(s_szDevInput) + sizeof(pDirent->d_name) + 16];
|
---|
266 | VBOXPtr pVBox = VBOXGetRec(xf86Screens[pScreen->myNum]);
|
---|
267 | DIR *pDir;
|
---|
268 | int fd = -1;
|
---|
269 |
|
---|
270 | if (pVBox->fdACPIDevices != -1 || pVBox->hACPIEventHandler != NULL)
|
---|
271 | FatalError("ACPI input file descriptor not initialised correctly.\n");
|
---|
272 | pDir = opendir("/dev/input");
|
---|
273 | if (pDir == NULL)
|
---|
274 | return;
|
---|
275 | memcpy(szFile, s_szDevInput, sizeof(s_szDevInput));
|
---|
276 | for (pDirent = readdir(pDir); pDirent != NULL; pDirent = readdir(pDir))
|
---|
277 | {
|
---|
278 | if (strncmp(pDirent->d_name, "event", sizeof("event") - 1) == 0)
|
---|
279 | {
|
---|
280 | #define BITS_PER_BLOCK (sizeof(unsigned long) * 8)
|
---|
281 | char szDevice[64] = "";
|
---|
282 | unsigned long afKeys[KEY_MAX / BITS_PER_BLOCK];
|
---|
283 | size_t const cchName = strlen(pDirent->d_name);
|
---|
284 | if (cchName + sizeof(s_szDevInput) > sizeof(szFile))
|
---|
285 | continue;
|
---|
286 | memcpy(&szFile[sizeof(s_szDevInput) - 1], pDirent->d_name, cchName + 1);
|
---|
287 | if (fd != -1)
|
---|
288 | close(fd);
|
---|
289 | fd = open(szFile, O_RDONLY | O_NONBLOCK);
|
---|
290 | if ( fd == -1
|
---|
291 | || ioctl(fd, EVIOCGNAME(sizeof(szDevice)), szDevice) == -1
|
---|
292 | || strcmp(szDevice, "Video Bus") != 0)
|
---|
293 | continue;
|
---|
294 | if ( ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(afKeys)), afKeys) == -1
|
---|
295 | || (( afKeys[KEY_SWITCHVIDEOMODE / BITS_PER_BLOCK]
|
---|
296 | >> KEY_SWITCHVIDEOMODE % BITS_PER_BLOCK) & 1) == 0)
|
---|
297 | break;
|
---|
298 | if (ioctl(fd, EVIOCGRAB, (void *)1) != 0)
|
---|
299 | break;
|
---|
300 | pVBox->hACPIEventHandler
|
---|
301 | = xf86AddGeneralHandler(fd, acpiEventHandler, pScreen);
|
---|
302 | if (pVBox->hACPIEventHandler == NULL)
|
---|
303 | break;
|
---|
304 | pVBox->fdACPIDevices = fd;
|
---|
305 | fd = -1;
|
---|
306 | break;
|
---|
307 | #undef BITS_PER_BLOCK
|
---|
308 | }
|
---|
309 | }
|
---|
310 | if (fd != -1)
|
---|
311 | close(fd);
|
---|
312 | closedir(pDir);
|
---|
313 | }
|
---|
314 |
|
---|
315 | void vbvxCleanUpLinuxACPI(ScreenPtr pScreen)
|
---|
316 | {
|
---|
317 | VBOXPtr pVBox = VBOXGetRec(xf86Screens[pScreen->myNum]);
|
---|
318 | if (pVBox->fdACPIDevices != -1)
|
---|
319 | close(pVBox->fdACPIDevices);
|
---|
320 | pVBox->fdACPIDevices = -1;
|
---|
321 | xf86RemoveGeneralHandler(pVBox->hACPIEventHandler);
|
---|
322 | pVBox->hACPIEventHandler = NULL;
|
---|
323 | }
|
---|
324 | # endif /* RT_OS_LINUX */
|
---|
325 | #endif /* VBOXVIDEO_13 */
|
---|