VirtualBox

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

Last change on this file since 60190 was 60190, checked in by vboxsync, 9 years ago

bugref:8087:8087: Additions/x11: support non-root X server: revert r106142: we still need to keep old, buggy versions of gnome-settings-daemon from seeing ACPI events when possible to prevent them from doing bad things.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 10.8 KB
Line 
1/* $Id: getmode.c 60190 2016-03-24 20:06:24Z 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 */
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, 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. */
151void 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
167static 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
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 ScreenPtr pScreen = (ScreenPtr)pvData;
240 VBOXPtr pVBox = VBOXGetRec(xf86Screens[pScreen->myNum]);
241 struct input_event event;
242 ssize_t rc;
243
244 do
245 rc = read(fd, &event, sizeof(event));
246 while (rc > 0 || (rc == -1 && errno == EINTR));
247 /* Why do they return EAGAIN instead of zero bytes read like everyone else does? */
248 VBVXASSERT(rc != -1 || errno == EAGAIN, ("Reading ACPI input event failed.\n"));
249}
250
251void vbvxSetUpLinuxACPI(ScreenPtr pScreen)
252{
253 VBOXPtr pVBox = VBOXGetRec(xf86Screens[pScreen->myNum]);
254 struct dirent *pDirent;
255 DIR *pDir;
256 int fd = -1;
257
258 if (pVBox->fdACPIDevices != -1 || pVBox->hACPIEventHandler != NULL)
259 FatalError("ACPI input file descriptor not initialised correctly.\n");
260 pDir = opendir("/dev/input");
261 if (pDir == NULL)
262 return;
263 for (pDirent = readdir(pDir); pDirent != NULL; pDirent = readdir(pDir))
264 {
265 if (strncmp(pDirent->d_name, "event", sizeof("event") - 1) == 0)
266 {
267#define BITS_PER_BLOCK (sizeof(unsigned long) * 8)
268 char szFile[64] = "/dev/input/";
269 char szDevice[64] = "";
270 unsigned long afKeys[KEY_MAX / BITS_PER_BLOCK];
271
272 strncat(szFile, pDirent->d_name, sizeof(szFile) - sizeof("/dev/input/"));
273 if (fd != -1)
274 close(fd);
275 fd = open(szFile, O_RDONLY | O_NONBLOCK);
276 if ( fd == -1
277 || ioctl(fd, EVIOCGNAME(sizeof(szDevice)), szDevice) == -1
278 || strcmp(szDevice, "Video Bus") != 0)
279 continue;
280 if ( ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(afKeys)), afKeys) == -1
281 || (( afKeys[KEY_SWITCHVIDEOMODE / BITS_PER_BLOCK]
282 >> KEY_SWITCHVIDEOMODE % BITS_PER_BLOCK) & 1) == 0)
283 break;
284 if (ioctl(fd, EVIOCGRAB, (void *)1) != 0)
285 break;
286 pVBox->hACPIEventHandler
287 = xf86AddGeneralHandler(fd, acpiEventHandler, pScreen);
288 if (pVBox->hACPIEventHandler == NULL)
289 break;
290 pVBox->fdACPIDevices = fd;
291 fd = -1;
292 break;
293#undef BITS_PER_BLOCK
294 }
295 }
296 if (fd != -1)
297 close(fd);
298 closedir(pDir);
299}
300
301void vbvxCleanUpLinuxACPI(ScreenPtr pScreen)
302{
303 VBOXPtr pVBox = VBOXGetRec(xf86Screens[pScreen->myNum]);
304 if (pVBox->fdACPIDevices != -1)
305 close(pVBox->fdACPIDevices);
306 pVBox->fdACPIDevices = -1;
307 xf86RemoveGeneralHandler(pVBox->hACPIEventHandler);
308 pVBox->hACPIEventHandler = NULL;
309}
310# endif /* RT_OS_LINUX */
311#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