VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/vboxvideo/getmode.c@ 67954

Last change on this file since 67954 was 64185, checked in by vboxsync, 8 years ago

bugref:8614: Additions/common/VBoxVideo: make the code more self-contained: break the VBoxVideo.h dependency on VMMDev.h by moving VBVA and mouse cursor flags to VBoxVideo.h. Now there is a dependency the other way, but I do not think that is such a problem.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 10.7 KB
Line 
1/* $Id: getmode.c 64185 2016-10-10 08:40:56Z vboxsync $ */
2/** @file
3 * VirtualBox X11 Additions graphics driver dynamic video mode functions.
4 */
5
6/*
7 * Copyright (C) 2006-2016 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 */
55static 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 */
98static 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 */
125void vboxAddModes(ScrnInfoPtr pScrn)
126{
127 unsigned cx = 0;
128 unsigned cy = 0;
129 unsigned i;
130 DisplayModePtr pMode;
131
132 /* Add two dynamic mode entries. When we receive a new size hint we will
133 * update whichever of these is not current. */
134 pMode = vboxAddEmptyScreenMode(pScrn);
135 vboxFillDisplayMode(pScrn, pMode, NULL, 800, 600);
136 pMode = vboxAddEmptyScreenMode(pScrn);
137 vboxFillDisplayMode(pScrn, pMode, NULL, 800, 600);
138 /* Add any modes specified by the user. We assume here that the mode names
139 * reflect the mode sizes. */
140 for (i = 0; pScrn->display->modes && pScrn->display->modes[i]; i++)
141 {
142 if (sscanf(pScrn->display->modes[i], "%ux%u", &cx, &cy) == 2)
143 {
144 pMode = vboxAddEmptyScreenMode(pScrn);
145 vboxFillDisplayMode(pScrn, pMode, pScrn->display->modes[i], cx, cy);
146 }
147 }
148}
149
150/** Set the initial values for the guest screen size hints to standard values
151 * in case nothing else is available. */
152void VBoxInitialiseSizeHints(ScrnInfoPtr pScrn)
153{
154 VBOXPtr pVBox = VBOXGetRec(pScrn);
155 unsigned i;
156
157 for (i = 0; i < pVBox->cScreens; ++i)
158 {
159 pVBox->pScreens[i].aPreferredSize.cx = 800;
160 pVBox->pScreens[i].aPreferredSize.cy = 600;
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
168static bool useHardwareCursor(uint32_t fCursorCapabilities)
169{
170 if (fCursorCapabilities & VBOX_VBVA_CURSOR_CAPABILITY_HARDWARE)
171 return true;
172 return false;
173}
174
175static 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 COMPARE_AND_MAYBE_SET(pDest, src, pfChanged, fSet) \
184do { \
185 if (*(pDest) != (src)) \
186 { \
187 if (fSet) \
188 *(pDest) = (src); \
189 *(pfChanged) = true; \
190 } \
191} while(0)
192
193/** Read in information about the most recent size hints and cursor
194 * capabilities requested for the guest screens from HGSMI. */
195void vbvxReadSizesAndCursorIntegrationFromHGSMI(ScrnInfoPtr pScrn, bool *pfNeedUpdate)
196{
197 VBOXPtr pVBox = VBOXGetRec(pScrn);
198 int rc;
199 unsigned i;
200 bool fChanged = false;
201 uint32_t fCursorCapabilities;
202
203 if (!pVBox->fHaveHGSMIModeHints)
204 return;
205 rc = VBoxHGSMIGetModeHints(&pVBox->guestCtx, pVBox->cScreens, pVBox->paVBVAModeHints);
206 VBVXASSERT(rc == VINF_SUCCESS, ("VBoxHGSMIGetModeHints failed, rc=%d.\n", rc));
207 for (i = 0; i < pVBox->cScreens; ++i)
208 if (pVBox->paVBVAModeHints[i].magic == VBVAMODEHINT_MAGIC)
209 {
210 COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredSize.cx, pVBox->paVBVAModeHints[i].cx & 0x8fff, &fChanged, true);
211 COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredSize.cy, pVBox->paVBVAModeHints[i].cy & 0x8fff, &fChanged, true);
212 COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].afConnected, RT_BOOL(pVBox->paVBVAModeHints[i].fEnabled), &fChanged, true);
213 COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredLocation.x, (int32_t)pVBox->paVBVAModeHints[i].dx & 0x8fff, &fChanged,
214 true);
215 COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredLocation.y, (int32_t)pVBox->paVBVAModeHints[i].dy & 0x8fff, &fChanged,
216 true);
217 if (pVBox->paVBVAModeHints[i].dx != ~(uint32_t)0 && pVBox->paVBVAModeHints[i].dy != ~(uint32_t)0)
218 COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].afHaveLocation, true, &fChanged, true);
219 else
220 COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].afHaveLocation, false, &fChanged, true);
221 }
222 rc = VBoxQueryConfHGSMI(&pVBox->guestCtx, VBOX_VBVA_CONF32_CURSOR_CAPABILITIES, &fCursorCapabilities);
223 VBVXASSERT(rc == VINF_SUCCESS, ("Getting VBOX_VBVA_CONF32_CURSOR_CAPABILITIES failed, rc=%d.\n", rc));
224 compareAndMaybeSetUseHardwareCursor(pVBox, fCursorCapabilities, &fChanged, true);
225 if (pfNeedUpdate != NULL && fChanged)
226 *pfNeedUpdate = true;
227}
228
229#undef COMPARE_AND_MAYBE_SET
230
231#ifdef VBOXVIDEO_13
232# ifdef RT_OS_LINUX
233/** We have this for two purposes: one is to ensure that the X server is woken
234 * up when we get a video ACPI event. Two is to grab ACPI video events to
235 * prevent gnome-settings-daemon from seeing them, as older versions ignored
236 * the time stamp and handled them at the wrong time. */
237static void acpiEventHandler(int fd, void *pvData)
238{
239 struct input_event event;
240 ssize_t rc;
241 RT_NOREF(pvData);
242
243 do
244 rc = read(fd, &event, sizeof(event));
245 while (rc > 0 || (rc == -1 && errno == EINTR));
246 /* Why do they return EAGAIN instead of zero bytes read like everyone else does? */
247 VBVXASSERT(rc != -1 || errno == EAGAIN, ("Reading ACPI input event failed.\n"));
248}
249
250void vbvxSetUpLinuxACPI(ScreenPtr pScreen)
251{
252 VBOXPtr pVBox = VBOXGetRec(xf86Screens[pScreen->myNum]);
253 struct dirent *pDirent;
254 DIR *pDir;
255 int fd = -1;
256
257 if (pVBox->fdACPIDevices != -1 || pVBox->hACPIEventHandler != NULL)
258 FatalError("ACPI input file descriptor not initialised correctly.\n");
259 pDir = opendir("/dev/input");
260 if (pDir == NULL)
261 return;
262 for (pDirent = readdir(pDir); pDirent != NULL; pDirent = readdir(pDir))
263 {
264 if (strncmp(pDirent->d_name, "event", sizeof("event") - 1) == 0)
265 {
266#define BITS_PER_BLOCK (sizeof(unsigned long) * 8)
267 char szFile[64] = "/dev/input/";
268 char szDevice[64] = "";
269 unsigned long afKeys[KEY_MAX / BITS_PER_BLOCK];
270
271 strncat(szFile, pDirent->d_name, sizeof(szFile) - sizeof("/dev/input/"));
272 if (fd != -1)
273 close(fd);
274 fd = open(szFile, O_RDONLY | O_NONBLOCK);
275 if ( fd == -1
276 || ioctl(fd, EVIOCGNAME(sizeof(szDevice)), szDevice) == -1
277 || strcmp(szDevice, "Video Bus") != 0)
278 continue;
279 if ( ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(afKeys)), afKeys) == -1
280 || (( afKeys[KEY_SWITCHVIDEOMODE / BITS_PER_BLOCK]
281 >> KEY_SWITCHVIDEOMODE % BITS_PER_BLOCK) & 1) == 0)
282 break;
283 if (ioctl(fd, EVIOCGRAB, (void *)1) != 0)
284 break;
285 pVBox->hACPIEventHandler
286 = xf86AddGeneralHandler(fd, acpiEventHandler, pScreen);
287 if (pVBox->hACPIEventHandler == NULL)
288 break;
289 pVBox->fdACPIDevices = fd;
290 fd = -1;
291 break;
292#undef BITS_PER_BLOCK
293 }
294 }
295 if (fd != -1)
296 close(fd);
297 closedir(pDir);
298}
299
300void vbvxCleanUpLinuxACPI(ScreenPtr pScreen)
301{
302 VBOXPtr pVBox = VBOXGetRec(xf86Screens[pScreen->myNum]);
303 if (pVBox->fdACPIDevices != -1)
304 close(pVBox->fdACPIDevices);
305 pVBox->fdACPIDevices = -1;
306 xf86RemoveGeneralHandler(pVBox->hACPIEventHandler);
307 pVBox->hACPIEventHandler = NULL;
308}
309# endif /* RT_OS_LINUX */
310#endif /* VBOXVIDEO_13 */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette