VirtualBox

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

Last change on this file since 63942 was 63221, checked in by vboxsync, 8 years ago

GA/X11: warnings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 10.8 KB
Line 
1/* $Id: getmode.c 63221 2016-08-09 16:13:14Z 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 & VMMDEV_MOUSE_HOST_CANNOT_HWPOINTER)
171 && (fCursorCapabilities & VMMDEV_MOUSE_HOST_WANTS_ABSOLUTE))
172 return true;
173 return false;
174}
175
176static 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 COMPARE_AND_MAYBE_SET(pDest, src, pfChanged, fSet) \
185do { \
186 if (*(pDest) != (src)) \
187 { \
188 if (fSet) \
189 *(pDest) = (src); \
190 *(pfChanged) = true; \
191 } \
192} while(0)
193
194/** Read in information about the most recent size hints and cursor
195 * capabilities requested for the guest screens from HGSMI. */
196void vbvxReadSizesAndCursorIntegrationFromHGSMI(ScrnInfoPtr pScrn, bool *pfNeedUpdate)
197{
198 VBOXPtr pVBox = VBOXGetRec(pScrn);
199 int rc;
200 unsigned i;
201 bool fChanged = false;
202 uint32_t fCursorCapabilities;
203
204 if (!pVBox->fHaveHGSMIModeHints)
205 return;
206 rc = VBoxHGSMIGetModeHints(&pVBox->guestCtx, pVBox->cScreens, pVBox->paVBVAModeHints);
207 VBVXASSERT(rc == VINF_SUCCESS, ("VBoxHGSMIGetModeHints failed, rc=%d.\n", rc));
208 for (i = 0; i < pVBox->cScreens; ++i)
209 if (pVBox->paVBVAModeHints[i].magic == VBVAMODEHINT_MAGIC)
210 {
211 COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredSize.cx, pVBox->paVBVAModeHints[i].cx & 0x8fff, &fChanged, true);
212 COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredSize.cy, pVBox->paVBVAModeHints[i].cy & 0x8fff, &fChanged, true);
213 COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].afConnected, RT_BOOL(pVBox->paVBVAModeHints[i].fEnabled), &fChanged, true);
214 COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredLocation.x, (int32_t)pVBox->paVBVAModeHints[i].dx & 0x8fff, &fChanged,
215 true);
216 COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].aPreferredLocation.y, (int32_t)pVBox->paVBVAModeHints[i].dy & 0x8fff, &fChanged,
217 true);
218 if (pVBox->paVBVAModeHints[i].dx != ~(uint32_t)0 && pVBox->paVBVAModeHints[i].dy != ~(uint32_t)0)
219 COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].afHaveLocation, true, &fChanged, true);
220 else
221 COMPARE_AND_MAYBE_SET(&pVBox->pScreens[i].afHaveLocation, false, &fChanged, true);
222 }
223 rc = VBoxQueryConfHGSMI(&pVBox->guestCtx, VBOX_VBVA_CONF32_CURSOR_CAPABILITIES, &fCursorCapabilities);
224 VBVXASSERT(rc == VINF_SUCCESS, ("Getting VBOX_VBVA_CONF32_CURSOR_CAPABILITIES failed, rc=%d.\n", rc));
225 compareAndMaybeSetUseHardwareCursor(pVBox, fCursorCapabilities, &fChanged, true);
226 if (pfNeedUpdate != NULL && fChanged)
227 *pfNeedUpdate = true;
228}
229
230#undef COMPARE_AND_MAYBE_SET
231
232#ifdef VBOXVIDEO_13
233# ifdef RT_OS_LINUX
234/** We have this for two purposes: one is to ensure that the X server is woken
235 * up when we get a video ACPI event. Two is to grab ACPI video events to
236 * prevent gnome-settings-daemon from seeing them, as older versions ignored
237 * the time stamp and handled them at the wrong time. */
238static void acpiEventHandler(int fd, void *pvData)
239{
240 struct input_event event;
241 ssize_t rc;
242 RT_NOREF(pvData);
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