VirtualBox

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

Last change on this file since 55875 was 55875, checked in by vboxsync, 10 years ago

Additions/x11/vboxvideo: adjust the mouse range properly on older X servers.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 53.5 KB
Line 
1/* $Id: vboxvideo.c 55875 2015-05-15 16:12:38Z vboxsync $ */
2/** @file
3 *
4 * Linux Additions X11 graphics driver
5 */
6
7/*
8 * Copyright (C) 2006-2013 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 * --------------------------------------------------------------------
18 *
19 * This code is based on the X.Org VESA driver with the following copyrights:
20 *
21 * Copyright (c) 2000 by Conectiva S.A. (http://www.conectiva.com)
22 * Copyright 2008 Red Hat, Inc.
23 * Copyright 2012 Red Hat, Inc.
24 *
25 * and the following permission notice (not all original sourse files include
26 * the last paragraph):
27 *
28 * Permission is hereby granted, free of charge, to any person obtaining a
29 * copy of this software and associated documentation files (the "Software"),
30 * to deal in the Software without restriction, including without limitation
31 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
32 * and/or sell copies of the Software, and to permit persons to whom the
33 * Software is furnished to do so, subject to the following conditions:
34 *
35 * The above copyright notice and this permission notice shall be included in
36 * all copies or substantial portions of the Software.
37 *
38 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
39 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
40 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
41 * CONECTIVA LINUX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
42 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
43 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
44 * SOFTWARE.
45 *
46 * Except as contained in this notice, the name of Conectiva Linux shall
47 * not be used in advertising or otherwise to promote the sale, use or other
48 * dealings in this Software without prior written authorization from
49 * Conectiva Linux.
50 *
51 * Authors: Paulo César Pereira de Andrade <[email protected]>
52 * David Dawes <[email protected]>
53 * Adam Jackson <[email protected]>
54 * Dave Airlie <[email protected]>
55 */
56
57#include "vboxvideo.h"
58#include <VBox/VBoxGuest.h>
59#include <VBox/Hardware/VBoxVideoVBE.h>
60#include "version-generated.h"
61#include "product-generated.h"
62#include "revision-generated.h"
63
64/* Basic definitions and functions needed by all drivers. */
65#include "xf86.h"
66/* For video memory mapping. */
67#include "xf86_OSproc.h"
68#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) < 6
69/* PCI resources. */
70# include "xf86Resources.h"
71#endif
72/* Generic server linear frame-buffer APIs. */
73#include "fb.h"
74/* Colormap and visual handling. */
75#include "micmap.h"
76#include "xf86cmap.h"
77/* ShadowFB support */
78#include "shadowfb.h"
79/* VGA hardware functions for setting and restoring text mode */
80#include "vgaHW.h"
81#ifdef VBOX_DRI
82# include "xf86drm.h"
83# include "xf86drmMode.h"
84#endif
85#ifdef VBOXVIDEO_13
86/* X.org 1.3+ mode setting */
87# define _HAVE_STRING_ARCH_strsep /* bits/string2.h, __strsep_1c. */
88# include "xf86Crtc.h"
89# include "xf86Modes.h"
90/* For xf86RandR12GetOriginalVirtualSize(). */
91# include "xf86RandR12.h"
92#endif
93/* For setting the root window property. */
94#include "property.h"
95#include "X11/Xatom.h"
96
97#ifdef XORG_7X
98# include <stdlib.h>
99# include <string.h>
100#endif
101
102/* This was accepted upstream in X.Org Server 1.16 which bumped the video
103 * driver ABI to 17. */
104#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) < 17
105# define SET_HAVE_VT_PROPERTY
106#endif
107
108/* Mandatory functions */
109
110static const OptionInfoRec * VBOXAvailableOptions(int chipid, int busid);
111static void VBOXIdentify(int flags);
112#ifndef PCIACCESS
113static Bool VBOXProbe(DriverPtr drv, int flags);
114#else
115static Bool VBOXPciProbe(DriverPtr drv, int entity_num,
116 struct pci_device *dev, intptr_t match_data);
117#endif
118static Bool VBOXPreInit(ScrnInfoPtr pScrn, int flags);
119static Bool VBOXScreenInit(ScreenPtr pScreen, int argc, char **argv);
120static Bool VBOXEnterVT(ScrnInfoPtr pScrn);
121static void VBOXLeaveVT(ScrnInfoPtr pScrn);
122static Bool VBOXCloseScreen(ScreenPtr pScreen);
123static Bool VBOXSaveScreen(ScreenPtr pScreen, int mode);
124static Bool VBOXSwitchMode(ScrnInfoPtr pScrn, DisplayModePtr pMode);
125static void VBOXAdjustFrame(ScrnInfoPtr pScrn, int x, int y);
126static void VBOXFreeScreen(ScrnInfoPtr pScrn);
127static void VBOXDisplayPowerManagementSet(ScrnInfoPtr pScrn, int mode,
128 int flags);
129
130/* locally used functions */
131static Bool VBOXMapVidMem(ScrnInfoPtr pScrn);
132static void VBOXUnmapVidMem(ScrnInfoPtr pScrn);
133static void VBOXSaveMode(ScrnInfoPtr pScrn);
134static void VBOXRestoreMode(ScrnInfoPtr pScrn);
135static void setSizesAndCursorIntegration(ScrnInfoPtr pScrn, bool fScreenInitTime);
136
137#ifndef XF86_SCRN_INTERFACE
138# define xf86ScreenToScrn(pScreen) xf86Screens[(pScreen)->myNum]
139# define xf86ScrnToScreen(pScrn) screenInfo.screens[(pScrn)->scrnIndex]
140#endif
141
142static inline void VBOXSetRec(ScrnInfoPtr pScrn)
143{
144 if (!pScrn->driverPrivate)
145 {
146 VBOXPtr pVBox = (VBOXPtr)xnfcalloc(sizeof(VBOXRec), 1);
147 pScrn->driverPrivate = pVBox;
148#if defined(VBOXVIDEO_13) && defined(RT_OS_LINUX)
149 pVBox->fdACPIDevices = -1;
150#endif
151 }
152}
153
154enum GenericTypes
155{
156 CHIP_VBOX_GENERIC
157};
158
159#ifdef PCIACCESS
160static const struct pci_id_match vbox_device_match[] = {
161 {
162 VBOX_VENDORID, VBOX_DEVICEID, PCI_MATCH_ANY, PCI_MATCH_ANY,
163 0, 0, 0
164 },
165
166 { 0, 0, 0 },
167};
168#endif
169
170/* Supported chipsets */
171static SymTabRec VBOXChipsets[] =
172{
173 {VBOX_DEVICEID, "vbox"},
174 {-1, NULL}
175};
176
177static PciChipsets VBOXPCIchipsets[] = {
178 { VBOX_DEVICEID, VBOX_DEVICEID, RES_SHARED_VGA },
179 { -1, -1, RES_UNDEFINED },
180};
181
182/*
183 * This contains the functions needed by the server after loading the
184 * driver module. It must be supplied, and gets added the driver list by
185 * the Module Setup function in the dynamic case. In the static case a
186 * reference to this is compiled in, and this requires that the name of
187 * this DriverRec be an upper-case version of the driver name.
188 */
189
190#ifdef XORG_7X
191_X_EXPORT
192#endif
193DriverRec VBOXVIDEO = {
194 VBOX_VERSION,
195 VBOX_DRIVER_NAME,
196 VBOXIdentify,
197#ifdef PCIACCESS
198 NULL,
199#else
200 VBOXProbe,
201#endif
202 VBOXAvailableOptions,
203 NULL,
204 0,
205#ifdef XORG_7X
206 NULL,
207#endif
208#ifdef PCIACCESS
209 vbox_device_match,
210 VBOXPciProbe
211#endif
212};
213
214/* No options for now */
215static const OptionInfoRec VBOXOptions[] = {
216 { -1, NULL, OPTV_NONE, {0}, FALSE }
217};
218
219#ifndef XORG_7X
220/*
221 * List of symbols from other modules that this module references. This
222 * list is used to tell the loader that it is OK for symbols here to be
223 * unresolved providing that it hasn't been told that they haven't been
224 * told that they are essential via a call to xf86LoaderReqSymbols() or
225 * xf86LoaderReqSymLists(). The purpose is this is to avoid warnings about
226 * unresolved symbols that are not required.
227 */
228static const char *fbSymbols[] = {
229 "fbPictureInit",
230 "fbScreenInit",
231 NULL
232};
233
234static const char *shadowfbSymbols[] = {
235 "ShadowFBInit2",
236 NULL
237};
238
239static const char *ramdacSymbols[] = {
240 "xf86DestroyCursorInfoRec",
241 "xf86InitCursor",
242 "xf86CreateCursorInfoRec",
243 NULL
244};
245
246static const char *vgahwSymbols[] = {
247 "vgaHWFreeHWRec",
248 "vgaHWGetHWRec",
249 "vgaHWGetIOBase",
250 "vgaHWGetIndex",
251 "vgaHWRestore",
252 "vgaHWSave",
253 "vgaHWSetStdFuncs",
254 NULL
255};
256#endif /* !XORG_7X */
257
258/** Resize the virtual framebuffer. */
259static Bool adjustScreenPixmap(ScrnInfoPtr pScrn, int width, int height)
260{
261 ScreenPtr pScreen = xf86ScrnToScreen(pScrn);
262 VBOXPtr pVBox = VBOXGetRec(pScrn);
263 int adjustedWidth = pScrn->bitsPerPixel == 16 ? (width + 1) & ~1 : width;
264 int cbLine = adjustedWidth * pScrn->bitsPerPixel / 8;
265 PixmapPtr pPixmap;
266 int rc;
267
268 TRACE_LOG("width=%d, height=%d\n", width, height);
269 VBVXASSERT(width >= 0 && height >= 0, ("Invalid negative width (%d) or height (%d)\n", width, height));
270 if (pScreen == NULL) /* Not yet initialised. */
271 return TRUE;
272 pPixmap = pScreen->GetScreenPixmap(pScreen);
273 VBVXASSERT(pPixmap != NULL, ("Failed to get the screen pixmap.\n"));
274 TRACE_LOG("pPixmap=%p adjustedWidth=%d height=%d pScrn->depth=%d pScrn->bitsPerPixel=%d cbLine=%d pVBox->base=%p pPixmap->drawable.width=%d pPixmap->drawable.height=%d\n",
275 pPixmap, adjustedWidth, height, pScrn->depth, pScrn->bitsPerPixel, cbLine, pVBox->base, pPixmap->drawable.width,
276 pPixmap->drawable.height);
277 if ( adjustedWidth != pPixmap->drawable.width
278 || height != pPixmap->drawable.height)
279 {
280 if ( adjustedWidth > VBOX_VIDEO_MAX_VIRTUAL || height > VBOX_VIDEO_MAX_VIRTUAL
281 || (unsigned)cbLine * (unsigned)height >= pVBox->cbFBMax)
282 {
283 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
284 "Virtual framebuffer %dx%d too large. For information, video memory: %u Kb.\n",
285 adjustedWidth, height, (unsigned) pVBox->cbFBMax / 1024);
286 return FALSE;
287 }
288 vbvxClearVRAM(pScrn, ((size_t)pScrn->virtualX) * pScrn->virtualY * (pScrn->bitsPerPixel / 8),
289 ((size_t)adjustedWidth) * height * (pScrn->bitsPerPixel / 8));
290 pScreen->ModifyPixmapHeader(pPixmap, adjustedWidth, height, pScrn->depth, pScrn->bitsPerPixel, cbLine, pVBox->base);
291 }
292 pScrn->displayWidth = pScrn->virtualX = adjustedWidth;
293 pScrn->virtualY = height;
294#ifdef VBOX_DRI_OLD
295 if (pVBox->useDRI)
296 VBOXDRIUpdateStride(pScrn, pVBox);
297#endif
298 return TRUE;
299}
300
301/** Set a video mode to the hardware, RandR 1.1 version. Since we no longer do
302 * virtual frame buffers, adjust the screen pixmap dimensions to match. */
303static void setModeRandR11(ScrnInfoPtr pScrn, DisplayModePtr pMode, bool fLimitedContext)
304{
305 VBOXPtr pVBox = VBOXGetRec(pScrn);
306 struct vbvxFrameBuffer frameBuffer = { 0, 0, pMode->HDisplay, pMode->VDisplay, pScrn->bitsPerPixel};
307
308 pVBox->pScreens[0].aScreenLocation.cx = pMode->HDisplay;
309 pVBox->pScreens[0].aScreenLocation.cy = pMode->VDisplay;
310 if (fLimitedContext)
311 {
312 pScrn->displayWidth = pScrn->virtualX = pMode->HDisplay;
313 pScrn->virtualY = pMode->VDisplay;
314 }
315 else
316 {
317 xf86ScrnToScreen(pScrn)->width = pMode->HDisplay;
318 xf86ScrnToScreen(pScrn)->height = pMode->VDisplay;
319 adjustScreenPixmap(pScrn, pMode->HDisplay, pMode->VDisplay);
320 }
321 if (pMode->HDisplay != 0 && pMode->VDisplay != 0)
322 vbvxSetMode(pScrn, 0, pMode->HDisplay, pMode->VDisplay, 0, 0, true, true, &frameBuffer);
323 pScrn->currentMode = pMode;
324}
325
326#ifdef VBOXVIDEO_13
327/* X.org 1.3+ mode-setting support ******************************************/
328
329/** Set a video mode to the hardware, RandR 1.2 version. If this is the first
330 * screen, re-set the current mode for all others (the offset for the first
331 * screen is always treated as zero by the hardware, so all other screens need
332 * to be changed to compensate for any changes!). The mode to set is taken
333 * from the X.Org Crtc structure. */
334static void setModeRandR12(ScrnInfoPtr pScrn, unsigned cScreen)
335{
336 VBOXPtr pVBox = VBOXGetRec(pScrn);
337 unsigned i;
338 struct vbvxFrameBuffer frameBuffer = { pVBox->pScreens[0].paCrtcs->x, pVBox->pScreens[0].paCrtcs->y, pScrn->virtualX,
339 pScrn->virtualY, pScrn->bitsPerPixel };
340 unsigned cFirst = cScreen;
341 unsigned cLast = cScreen != 0 ? cScreen + 1 : pVBox->cScreens;
342 int originalX, originalY;
343
344 /* Check that this code cannot trigger the resizing bug in X.Org Server 1.3.
345 * See the work-around in PreInit. */
346 xf86RandR12GetOriginalVirtualSize(pScrn, &originalX, &originalY);
347 VBVXASSERT(originalX == VBOX_VIDEO_MAX_VIRTUAL && originalY == VBOX_VIDEO_MAX_VIRTUAL, ("OriginalSize=%dx%d",
348 originalX, originalY));
349 for (i = cFirst; i < cLast; ++i)
350 if (pVBox->pScreens[i].paCrtcs->mode.HDisplay != 0 && pVBox->pScreens[i].paCrtcs->mode.VDisplay != 0)
351 vbvxSetMode(pScrn, i, pVBox->pScreens[i].paCrtcs->mode.HDisplay, pVBox->pScreens[i].paCrtcs->mode.VDisplay,
352 pVBox->pScreens[i].paCrtcs->x, pVBox->pScreens[i].paCrtcs->y, pVBox->pScreens[i].fPowerOn,
353 pVBox->pScreens[i].paOutputs->status == XF86OutputStatusConnected, &frameBuffer);
354}
355
356/** Wrapper around setModeRandR12() to avoid exposing non-obvious semantics.
357 */
358static void setAllModesRandR12(ScrnInfoPtr pScrn)
359{
360 setModeRandR12(pScrn, 0);
361}
362
363/* For descriptions of these functions and structures, see
364 hw/xfree86/modes/xf86Crtc.h and hw/xfree86/modes/xf86Modes.h in the
365 X.Org source tree. */
366
367static Bool vbox_config_resize(ScrnInfoPtr pScrn, int cw, int ch)
368{
369 VBOXPtr pVBox = VBOXGetRec(pScrn);
370 Bool rc;
371 unsigned i;
372
373 TRACE_LOG("width=%d, height=%d\n", cw, ch);
374 /* Don't fiddle with the hardware if we are switched
375 * to a virtual terminal. */
376 if (!pScrn->vtSema) {
377 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
378 "We do not own the active VT, exiting.\n");
379 return TRUE;
380 }
381 rc = adjustScreenPixmap(pScrn, cw, ch);
382 /* Power-on all screens (the server expects this) and set the new pitch to them. */
383 for (i = 0; i < pVBox->cScreens; ++i)
384 pVBox->pScreens[i].fPowerOn = true;
385 setAllModesRandR12(pScrn);
386 vbvxSetSolarisMouseRange(cw, ch);
387 return rc;
388}
389
390static const xf86CrtcConfigFuncsRec VBOXCrtcConfigFuncs = {
391 vbox_config_resize
392};
393
394static void
395vbox_crtc_dpms(xf86CrtcPtr crtc, int mode)
396{
397 ScrnInfoPtr pScrn = crtc->scrn;
398 VBOXPtr pVBox = VBOXGetRec(pScrn);
399 unsigned cDisplay = (uintptr_t)crtc->driver_private;
400
401 TRACE_LOG("mode=%d\n", mode);
402 pVBox->pScreens[cDisplay].fPowerOn = (mode != DPMSModeOff);
403 setModeRandR12(pScrn, cDisplay);
404}
405
406static Bool
407vbox_crtc_lock (xf86CrtcPtr crtc)
408{ (void) crtc; return FALSE; }
409
410
411/* We use this function to check whether the X server owns the active virtual
412 * terminal before attempting a mode switch, since the RandR extension isn't
413 * very dilligent here, which can mean crashes if we are unlucky. This is
414 * not the way it the function is intended - it is meant for reporting modes
415 * which the hardware can't handle. I hope that this won't confuse any clients
416 * connecting to us. */
417static Bool
418vbox_crtc_mode_fixup (xf86CrtcPtr crtc, DisplayModePtr mode,
419 DisplayModePtr adjusted_mode)
420{ (void) crtc; (void) mode; (void) adjusted_mode; return TRUE; }
421
422static void
423vbox_crtc_stub (xf86CrtcPtr crtc)
424{ (void) crtc; }
425
426static void
427vbox_crtc_mode_set (xf86CrtcPtr crtc, DisplayModePtr mode,
428 DisplayModePtr adjusted_mode, int x, int y)
429{
430 (void) mode;
431 VBOXPtr pVBox = VBOXGetRec(crtc->scrn);
432 unsigned cDisplay = (uintptr_t)crtc->driver_private;
433
434 TRACE_LOG("name=%s, HDisplay=%d, VDisplay=%d, x=%d, y=%d\n", adjusted_mode->name,
435 adjusted_mode->HDisplay, adjusted_mode->VDisplay, x, y);
436 pVBox->pScreens[cDisplay].fPowerOn = true;
437 pVBox->pScreens[cDisplay].aScreenLocation.cx = adjusted_mode->HDisplay;
438 pVBox->pScreens[cDisplay].aScreenLocation.cy = adjusted_mode->VDisplay;
439 pVBox->pScreens[cDisplay].aScreenLocation.x = x;
440 pVBox->pScreens[cDisplay].aScreenLocation.y = y;
441 /* Don't fiddle with the hardware if we are switched
442 * to a virtual terminal. */
443 if (!crtc->scrn->vtSema)
444 {
445 xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
446 "We do not own the active VT, exiting.\n");
447 return;
448 }
449 setModeRandR12(crtc->scrn, cDisplay);
450}
451
452static void
453vbox_crtc_gamma_set (xf86CrtcPtr crtc, CARD16 *red,
454 CARD16 *green, CARD16 *blue, int size)
455{ (void) crtc; (void) red; (void) green; (void) blue; (void) size; }
456
457static void *
458vbox_crtc_shadow_allocate (xf86CrtcPtr crtc, int width, int height)
459{ (void) crtc; (void) width; (void) height; return NULL; }
460
461static const xf86CrtcFuncsRec VBOXCrtcFuncs = {
462 .dpms = vbox_crtc_dpms,
463 .save = NULL, /* These two are never called by the server. */
464 .restore = NULL,
465 .lock = vbox_crtc_lock,
466 .unlock = NULL, /* This will not be invoked if lock returns FALSE. */
467 .mode_fixup = vbox_crtc_mode_fixup,
468 .prepare = vbox_crtc_stub,
469 .mode_set = vbox_crtc_mode_set,
470 .commit = vbox_crtc_stub,
471 .gamma_set = vbox_crtc_gamma_set,
472 .shadow_allocate = vbox_crtc_shadow_allocate,
473 .shadow_create = NULL, /* These two should not be invoked if allocate
474 returns NULL. */
475 .shadow_destroy = NULL,
476 .set_cursor_colors = NULL, /* We are still using the old cursor API. */
477 .set_cursor_position = NULL,
478 .show_cursor = NULL,
479 .hide_cursor = NULL,
480 .load_cursor_argb = NULL,
481 .destroy = vbox_crtc_stub
482};
483
484static void
485vbox_output_stub (xf86OutputPtr output)
486{ (void) output; }
487
488static void
489vbox_output_dpms (xf86OutputPtr output, int mode)
490{
491 (void)output; (void)mode;
492}
493
494static int
495vbox_output_mode_valid (xf86OutputPtr output, DisplayModePtr mode)
496{
497 return MODE_OK;
498}
499
500static Bool
501vbox_output_mode_fixup (xf86OutputPtr output, DisplayModePtr mode,
502 DisplayModePtr adjusted_mode)
503{ (void) output; (void) mode; (void) adjusted_mode; return TRUE; }
504
505static void
506vbox_output_mode_set (xf86OutputPtr output, DisplayModePtr mode,
507 DisplayModePtr adjusted_mode)
508{ (void) output; (void) mode; (void) adjusted_mode; }
509
510/* A virtual monitor is always connected. */
511static xf86OutputStatus
512vbox_output_detect (xf86OutputPtr output)
513{
514 ScrnInfoPtr pScrn = output->scrn;
515 VBOXPtr pVBox = VBOXGetRec(pScrn);
516 uint32_t iScreen = (uintptr_t)output->driver_private;
517 return pVBox->pScreens[iScreen].afConnected
518 ? XF86OutputStatusConnected : XF86OutputStatusDisconnected;
519}
520
521static DisplayModePtr vbox_output_add_mode(VBOXPtr pVBox, DisplayModePtr *pModes, const char *pszName, int x, int y,
522 Bool isPreferred, Bool isUserDef)
523{
524 TRACE_LOG("pszName=%s, x=%d, y=%d\n", pszName ? pszName : "(null)", x, y);
525 DisplayModePtr pMode = xnfcalloc(1, sizeof(DisplayModeRec));
526 int cRefresh = 60;
527
528 pMode->status = MODE_OK;
529 /* We don't ask the host whether it likes user defined modes,
530 * as we assume that the user really wanted that mode. */
531 pMode->type = isUserDef ? M_T_USERDEF : M_T_BUILTIN;
532 if (isPreferred)
533 pMode->type |= M_T_PREFERRED;
534 /* Older versions of VBox only support screen widths which are a multiple
535 * of 8 */
536 if (pVBox->fAnyX)
537 pMode->HDisplay = x;
538 else
539 pMode->HDisplay = x & ~7;
540 pMode->HSyncStart = pMode->HDisplay + 2;
541 pMode->HSyncEnd = pMode->HDisplay + 4;
542 pMode->HTotal = pMode->HDisplay + 6;
543 pMode->VDisplay = y;
544 pMode->VSyncStart = pMode->VDisplay + 2;
545 pMode->VSyncEnd = pMode->VDisplay + 4;
546 pMode->VTotal = pMode->VDisplay + 6;
547 pMode->Clock = pMode->HTotal * pMode->VTotal * cRefresh / 1000; /* kHz */
548 if (NULL == pszName) {
549 xf86SetModeDefaultName(pMode);
550 } else {
551 pMode->name = xnfstrdup(pszName);
552 }
553 *pModes = xf86ModesAdd(*pModes, pMode);
554 return pMode;
555}
556
557static DisplayModePtr
558vbox_output_get_modes (xf86OutputPtr output)
559{
560 unsigned i, cIndex = 0;
561 DisplayModePtr pModes = NULL, pMode;
562 ScrnInfoPtr pScrn = output->scrn;
563 VBOXPtr pVBox = VBOXGetRec(pScrn);
564
565 TRACE_ENTRY();
566 uint32_t x, y, iScreen;
567 iScreen = (uintptr_t)output->driver_private;
568 pMode = vbox_output_add_mode(pVBox, &pModes, NULL,
569 RT_CLAMP(pVBox->pScreens[iScreen].aPreferredSize.cx, VBOX_VIDEO_MIN_SIZE, VBOX_VIDEO_MAX_VIRTUAL),
570 RT_CLAMP(pVBox->pScreens[iScreen].aPreferredSize.cy, VBOX_VIDEO_MIN_SIZE, VBOX_VIDEO_MAX_VIRTUAL),
571 TRUE, FALSE);
572 TRACE_EXIT();
573 return pModes;
574}
575
576static const xf86OutputFuncsRec VBOXOutputFuncs = {
577 .create_resources = vbox_output_stub,
578 .dpms = vbox_output_dpms,
579 .save = NULL, /* These two are never called by the server. */
580 .restore = NULL,
581 .mode_valid = vbox_output_mode_valid,
582 .mode_fixup = vbox_output_mode_fixup,
583 .prepare = vbox_output_stub,
584 .commit = vbox_output_stub,
585 .mode_set = vbox_output_mode_set,
586 .detect = vbox_output_detect,
587 .get_modes = vbox_output_get_modes,
588#ifdef RANDR_12_INTERFACE
589 .set_property = NULL,
590#endif
591 .destroy = vbox_output_stub
592};
593#endif /* VBOXVIDEO_13 */
594
595/* Module loader interface */
596static MODULESETUPPROTO(vboxSetup);
597
598static XF86ModuleVersionInfo vboxVersionRec =
599{
600 VBOX_DRIVER_NAME,
601 VBOX_VENDOR,
602 MODINFOSTRING1,
603 MODINFOSTRING2,
604#ifdef XORG_7X
605 XORG_VERSION_CURRENT,
606#else
607 XF86_VERSION_CURRENT,
608#endif
609 1, /* Module major version. Xorg-specific */
610 0, /* Module minor version. Xorg-specific */
611 1, /* Module patchlevel. Xorg-specific */
612 ABI_CLASS_VIDEODRV, /* This is a video driver */
613 ABI_VIDEODRV_VERSION,
614 MOD_CLASS_VIDEODRV,
615 {0, 0, 0, 0}
616};
617
618/*
619 * This data is accessed by the loader. The name must be the module name
620 * followed by "ModuleData".
621 */
622#ifdef XORG_7X
623_X_EXPORT
624#endif
625XF86ModuleData vboxvideoModuleData = { &vboxVersionRec, vboxSetup, NULL };
626
627static pointer
628vboxSetup(pointer Module, pointer Options, int *ErrorMajor, int *ErrorMinor)
629{
630 static Bool Initialised = FALSE;
631
632 if (!Initialised)
633 {
634 Initialised = TRUE;
635#ifdef PCIACCESS
636 xf86AddDriver(&VBOXVIDEO, Module, HaveDriverFuncs);
637#else
638 xf86AddDriver(&VBOXVIDEO, Module, 0);
639#endif
640#ifndef XORG_7X
641 LoaderRefSymLists(fbSymbols,
642 shadowfbSymbols,
643 ramdacSymbols,
644 vgahwSymbols,
645 NULL);
646#endif
647 xf86Msg(X_CONFIG, "Load address of symbol \"VBOXVIDEO\" is %p\n",
648 (void *)&VBOXVIDEO);
649 return (pointer)TRUE;
650 }
651
652 if (ErrorMajor)
653 *ErrorMajor = LDR_ONCEONLY;
654 return (NULL);
655}
656
657
658static const OptionInfoRec *
659VBOXAvailableOptions(int chipid, int busid)
660{
661 return (VBOXOptions);
662}
663
664static void
665VBOXIdentify(int flags)
666{
667 xf86PrintChipsets(VBOX_NAME, "guest driver for VirtualBox", VBOXChipsets);
668}
669
670#ifndef XF86_SCRN_INTERFACE
671# define SCRNINDEXAPI(pfn) pfn ## Index
672static Bool VBOXScreenInitIndex(int scrnIndex, ScreenPtr pScreen, int argc,
673 char **argv)
674{ return VBOXScreenInit(pScreen, argc, argv); }
675
676static Bool VBOXEnterVTIndex(int scrnIndex, int flags)
677{ (void) flags; return VBOXEnterVT(xf86Screens[scrnIndex]); }
678
679static void VBOXLeaveVTIndex(int scrnIndex, int flags)
680{ (void) flags; VBOXLeaveVT(xf86Screens[scrnIndex]); }
681
682static Bool VBOXCloseScreenIndex(int scrnIndex, ScreenPtr pScreen)
683{ (void) scrnIndex; return VBOXCloseScreen(pScreen); }
684
685static Bool VBOXSwitchModeIndex(int scrnIndex, DisplayModePtr pMode, int flags)
686{ (void) flags; return VBOXSwitchMode(xf86Screens[scrnIndex], pMode); }
687
688static void VBOXAdjustFrameIndex(int scrnIndex, int x, int y, int flags)
689{ (void) flags; VBOXAdjustFrame(xf86Screens[scrnIndex], x, y); }
690
691static void VBOXFreeScreenIndex(int scrnIndex, int flags)
692{ (void) flags; VBOXFreeScreen(xf86Screens[scrnIndex]); }
693# else
694# define SCRNINDEXAPI(pfn) pfn
695#endif /* XF86_SCRN_INTERFACE */
696
697static void setScreenFunctions(ScrnInfoPtr pScrn, xf86ProbeProc pfnProbe)
698{
699 pScrn->driverVersion = VBOX_VERSION;
700 pScrn->driverName = VBOX_DRIVER_NAME;
701 pScrn->name = VBOX_NAME;
702 pScrn->Probe = pfnProbe;
703 pScrn->PreInit = VBOXPreInit;
704 pScrn->ScreenInit = SCRNINDEXAPI(VBOXScreenInit);
705 pScrn->SwitchMode = SCRNINDEXAPI(VBOXSwitchMode);
706 pScrn->AdjustFrame = SCRNINDEXAPI(VBOXAdjustFrame);
707 pScrn->EnterVT = SCRNINDEXAPI(VBOXEnterVT);
708 pScrn->LeaveVT = SCRNINDEXAPI(VBOXLeaveVT);
709 pScrn->FreeScreen = SCRNINDEXAPI(VBOXFreeScreen);
710}
711
712/*
713 * One of these functions is called once, at the start of the first server
714 * generation to do a minimal probe for supported hardware.
715 */
716
717#ifdef PCIACCESS
718static Bool
719VBOXPciProbe(DriverPtr drv, int entity_num, struct pci_device *dev,
720 intptr_t match_data)
721{
722 ScrnInfoPtr pScrn;
723
724 TRACE_ENTRY();
725 pScrn = xf86ConfigPciEntity(NULL, 0, entity_num, VBOXPCIchipsets,
726 NULL, NULL, NULL, NULL, NULL);
727 if (pScrn != NULL) {
728 VBOXPtr pVBox;
729
730 VBOXSetRec(pScrn);
731 pVBox = VBOXGetRec(pScrn);
732 if (!pVBox)
733 return FALSE;
734 setScreenFunctions(pScrn, NULL);
735 pVBox->pciInfo = dev;
736 }
737
738 TRACE_LOG("returning %s\n", BOOL_STR(pScrn != NULL));
739 return (pScrn != NULL);
740}
741#endif
742
743#ifndef PCIACCESS
744static Bool
745VBOXProbe(DriverPtr drv, int flags)
746{
747 Bool foundScreen = FALSE;
748 int numDevSections;
749 GDevPtr *devSections;
750
751 /*
752 * Find the config file Device sections that match this
753 * driver, and return if there are none.
754 */
755 if ((numDevSections = xf86MatchDevice(VBOX_NAME,
756 &devSections)) <= 0)
757 return (FALSE);
758
759 /* PCI BUS */
760 if (xf86GetPciVideoInfo())
761 {
762 int numUsed;
763 int *usedChips;
764 int i;
765 numUsed = xf86MatchPciInstances(VBOX_NAME, VBOX_VENDORID,
766 VBOXChipsets, VBOXPCIchipsets,
767 devSections, numDevSections,
768 drv, &usedChips);
769 if (numUsed > 0)
770 {
771 if (flags & PROBE_DETECT)
772 foundScreen = TRUE;
773 else
774 for (i = 0; i < numUsed; i++)
775 {
776 ScrnInfoPtr pScrn = NULL;
777 /* Allocate a ScrnInfoRec */
778 if ((pScrn = xf86ConfigPciEntity(pScrn,0,usedChips[i],
779 VBOXPCIchipsets,NULL,
780 NULL,NULL,NULL,NULL)))
781 {
782 setScreenFunctions(pScrn, VBOXProbe);
783 foundScreen = TRUE;
784 }
785 }
786 free(usedChips);
787 }
788 }
789 free(devSections);
790 return (foundScreen);
791}
792#endif
793
794
795/*
796 * QUOTE from the XFree86 DESIGN document:
797 *
798 * The purpose of this function is to find out all the information
799 * required to determine if the configuration is usable, and to initialise
800 * those parts of the ScrnInfoRec that can be set once at the beginning of
801 * the first server generation.
802 *
803 * (...)
804 *
805 * This includes probing for video memory, clocks, ramdac, and all other
806 * HW info that is needed. It includes determining the depth/bpp/visual
807 * and related info. It includes validating and determining the set of
808 * video modes that will be used (and anything that is required to
809 * determine that).
810 *
811 * This information should be determined in the least intrusive way
812 * possible. The state of the HW must remain unchanged by this function.
813 * Although video memory (including MMIO) may be mapped within this
814 * function, it must be unmapped before returning.
815 *
816 * END QUOTE
817 */
818
819static Bool
820VBOXPreInit(ScrnInfoPtr pScrn, int flags)
821{
822 VBOXPtr pVBox;
823 Gamma gzeros = {0.0, 0.0, 0.0};
824 rgb rzeros = {0, 0, 0};
825 unsigned DispiId;
826
827 TRACE_ENTRY();
828 /* Are we really starting the server, or is this just a dummy run? */
829 if (flags & PROBE_DETECT)
830 return (FALSE);
831
832 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "VirtualBox guest additions video driver version " VBOX_VERSION_STRING "r%d\n",
833 VBOX_SVN_REV);
834
835 /* Get our private data from the ScrnInfoRec structure. */
836 VBOXSetRec(pScrn);
837 pVBox = VBOXGetRec(pScrn);
838 if (!pVBox)
839 return FALSE;
840
841 /* Entity information seems to mean bus information. */
842 pVBox->pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
843
844 /* The ramdac module is needed for the hardware cursor. */
845 if (!xf86LoadSubModule(pScrn, "ramdac"))
846 return FALSE;
847
848 /* The framebuffer module. */
849 if (!xf86LoadSubModule(pScrn, "fb"))
850 return (FALSE);
851
852 if (!xf86LoadSubModule(pScrn, "shadowfb"))
853 return FALSE;
854
855 if (!xf86LoadSubModule(pScrn, "vgahw"))
856 return FALSE;
857
858#ifdef VBOX_DRI_OLD
859 /* Load the dri module. */
860 if (!xf86LoadSubModule(pScrn, "dri"))
861 return FALSE;
862#else
863# ifdef VBOX_DRI
864 /* Load the dri module. */
865 if (!xf86LoadSubModule(pScrn, "dri2"))
866 return FALSE;
867# endif
868#endif
869
870#ifndef PCIACCESS
871 if (pVBox->pEnt->location.type != BUS_PCI)
872 return FALSE;
873
874 pVBox->pciInfo = xf86GetPciInfoForEntity(pVBox->pEnt->index);
875 pVBox->pciTag = pciTag(pVBox->pciInfo->bus,
876 pVBox->pciInfo->device,
877 pVBox->pciInfo->func);
878#endif
879
880 /* Set up our ScrnInfoRec structure to describe our virtual
881 capabilities to X. */
882
883 pScrn->chipset = "vbox";
884 /** @note needed during colourmap initialisation */
885 pScrn->rgbBits = 8;
886
887 /* Let's create a nice, capable virtual monitor. */
888 pScrn->monitor = pScrn->confScreen->monitor;
889 pScrn->monitor->DDC = NULL;
890 pScrn->monitor->nHsync = 1;
891 pScrn->monitor->hsync[0].lo = 1;
892 pScrn->monitor->hsync[0].hi = 10000;
893 pScrn->monitor->nVrefresh = 1;
894 pScrn->monitor->vrefresh[0].lo = 1;
895 pScrn->monitor->vrefresh[0].hi = 100;
896
897 pScrn->progClock = TRUE;
898
899 /* Using the PCI information caused problems with non-powers-of-two
900 sized video RAM configurations */
901 pVBox->cbFBMax = VBoxVideoGetVRAMSize();
902 pScrn->videoRam = pVBox->cbFBMax / 1024;
903
904 /* Check if the chip restricts horizontal resolution or not. */
905 pVBox->fAnyX = VBoxVideoAnyWidthAllowed();
906
907 /* Set up clock information that will support all modes we need. */
908 pScrn->clockRanges = xnfcalloc(sizeof(ClockRange), 1);
909 pScrn->clockRanges->minClock = 1000;
910 pScrn->clockRanges->maxClock = 1000000000;
911 pScrn->clockRanges->clockIndex = -1;
912 pScrn->clockRanges->ClockMulFactor = 1;
913 pScrn->clockRanges->ClockDivFactor = 1;
914
915 if (!xf86SetDepthBpp(pScrn, 24, 0, 0, Support32bppFb))
916 return FALSE;
917 /* We only support 16 and 24 bits depth (i.e. 16 and 32bpp) */
918 if (pScrn->bitsPerPixel != 32 && pScrn->bitsPerPixel != 16)
919 {
920 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
921 "The VBox additions only support 16 and 32bpp graphics modes\n");
922 return FALSE;
923 }
924 xf86PrintDepthBpp(pScrn);
925 vboxAddModes(pScrn);
926
927#ifdef VBOXVIDEO_13
928 /* Work around a bug in the original X server modesetting code, which took
929 * the first valid values set to these two as maxima over the server
930 * lifetime. This bug was introduced on Feb 15 2007 and was fixed in commit
931 * fa877d7f three months later, so it was present in X.Org Server 1.3. */
932 pScrn->virtualX = VBOX_VIDEO_MAX_VIRTUAL;
933 pScrn->virtualY = VBOX_VIDEO_MAX_VIRTUAL;
934#else
935 /* We don't validate with xf86ValidateModes and xf86PruneModes as we
936 * already know what we like and what we don't. */
937
938 pScrn->currentMode = pScrn->modes;
939
940 /* Set the right virtual resolution. */
941 pScrn->virtualX = pScrn->bitsPerPixel == 16 ? (pScrn->currentMode->HDisplay + 1) & ~1 : pScrn->currentMode->HDisplay;
942 pScrn->virtualY = pScrn->currentMode->VDisplay;
943
944#endif /* !VBOXVIDEO_13 */
945
946 /* Needed before we initialise DRI. */
947 pScrn->displayWidth = pScrn->virtualX;
948
949 xf86PrintModes(pScrn);
950
951 /* VGA hardware initialisation */
952 if (!vgaHWGetHWRec(pScrn))
953 return FALSE;
954 /* Must be called before any VGA registers are saved or restored */
955 vgaHWSetStdFuncs(VGAHWPTR(pScrn));
956 vgaHWGetIOBase(VGAHWPTR(pScrn));
957
958 /* Colour weight - we always call this, since we are always in
959 truecolour. */
960 if (!xf86SetWeight(pScrn, rzeros, rzeros))
961 return (FALSE);
962
963 /* visual init */
964 if (!xf86SetDefaultVisual(pScrn, -1))
965 return (FALSE);
966
967 xf86SetGamma(pScrn, gzeros);
968
969 /* Set the DPI. Perhaps we should read this from the host? */
970 xf86SetDpi(pScrn, 96, 96);
971
972 if (pScrn->memPhysBase == 0) {
973#ifdef PCIACCESS
974 pScrn->memPhysBase = pVBox->pciInfo->regions[0].base_addr;
975#else
976 pScrn->memPhysBase = pVBox->pciInfo->memBase[0];
977#endif
978 pScrn->fbOffset = 0;
979 }
980
981 TRACE_EXIT();
982 return (TRUE);
983}
984
985/**
986 * Dummy function for setting the colour palette, which we actually never
987 * touch. However, the server still requires us to provide this.
988 */
989static void
990vboxLoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
991 LOCO *colors, VisualPtr pVisual)
992{
993 (void)pScrn; (void) numColors; (void) indices; (void) colors;
994 (void)pVisual;
995}
996
997#define HAS_VT_ATOM_NAME "XFree86_has_VT"
998#define VBOXVIDEO_DRIVER_ATOM_NAME "VBOXVIDEO_DRIVER_IN_USE"
999/* The memory storing the initial value of the XFree86_has_VT root window
1000 * property. This has to remain available until server start-up, so we just
1001 * use a global. */
1002static CARD32 InitialPropertyValue = 1;
1003
1004/** Initialise a flag property on the root window to say whether the server VT
1005 * is currently the active one as some clients need to know this. */
1006static void initialiseProperties(ScrnInfoPtr pScrn)
1007{
1008 Atom atom = -1;
1009 CARD32 *PropertyValue = &InitialPropertyValue;
1010#ifdef SET_HAVE_VT_PROPERTY
1011 atom = MakeAtom(HAS_VT_ATOM_NAME, sizeof(HAS_VT_ATOM_NAME) - 1, TRUE);
1012 if (xf86RegisterRootWindowProperty(pScrn->scrnIndex, atom, XA_INTEGER,
1013 32, 1, PropertyValue) != Success)
1014 FatalError("vboxvideo: failed to register VT property\n");
1015#endif /* SET_HAVE_VT_PROPERTY */
1016 atom = MakeAtom(VBOXVIDEO_DRIVER_ATOM_NAME,
1017 sizeof(VBOXVIDEO_DRIVER_ATOM_NAME) - 1, TRUE);
1018 if (xf86RegisterRootWindowProperty(pScrn->scrnIndex, atom, XA_INTEGER,
1019 32, 1, PropertyValue) != Success)
1020 FatalError("vboxvideo: failed to register driver in use property\n");
1021}
1022
1023#ifdef SET_HAVE_VT_PROPERTY
1024/** Update a flag property on the root window to say whether the server VT
1025 * is currently the active one as some clients need to know this. */
1026static void updateHasVTProperty(ScrnInfoPtr pScrn, Bool hasVT)
1027{
1028 Atom property_name;
1029 int32_t value = hasVT ? 1 : 0;
1030 int i;
1031
1032 property_name = MakeAtom(HAS_VT_ATOM_NAME, sizeof(HAS_VT_ATOM_NAME) - 1,
1033 FALSE);
1034 if (property_name == BAD_RESOURCE)
1035 FatalError("Failed to retrieve \"HAS_VT\" atom\n");
1036 if (ROOT_WINDOW(pScrn) == NULL)
1037 return;
1038 ChangeWindowProperty(ROOT_WINDOW(pScrn), property_name, XA_INTEGER, 32,
1039 PropModeReplace, 1, &value, TRUE);
1040}
1041#endif /* SET_HAVE_VT_PROPERTY */
1042
1043#ifdef VBOXVIDEO_13
1044
1045static void setVirtualSizeRandR12(ScrnInfoPtr pScrn, bool fLimitedContext)
1046{
1047 VBOXPtr pVBox = VBOXGetRec(pScrn);
1048 unsigned i;
1049 unsigned cx = 0;
1050 unsigned cy = 0;
1051
1052 for (i = 0; i < pVBox->cScreens; ++i)
1053 {
1054 if ( pVBox->fHaveHGSMIModeHints && pVBox->pScreens[i].afHaveLocation)
1055 {
1056 pVBox->pScreens[i].paCrtcs->x = pVBox->pScreens[i].aPreferredLocation.x;
1057 pVBox->pScreens[i].paCrtcs->y = pVBox->pScreens[i].aPreferredLocation.y;
1058 }
1059 if ( pVBox->pScreens[i].paOutputs->status == XF86OutputStatusConnected
1060 && pVBox->pScreens[i].paCrtcs->x + pVBox->pScreens[i].aPreferredSize.cx < VBOX_VIDEO_MAX_VIRTUAL
1061 && pVBox->pScreens[i].paCrtcs->y + pVBox->pScreens[i].aPreferredSize.cy < VBOX_VIDEO_MAX_VIRTUAL)
1062 {
1063 cx = max(cx, pVBox->pScreens[i].paCrtcs->x + pVBox->pScreens[i].aPreferredSize.cx);
1064 cy = max(cy, pVBox->pScreens[i].paCrtcs->y + pVBox->pScreens[i].aPreferredSize.cy);
1065 }
1066 }
1067 if (cx != 0 && cy != 0)
1068 {
1069 /* Do not set the virtual resolution in limited context as that can
1070 * cause problems setting up RandR 1.2 which needs it set to the
1071 * maximum size at this point. */
1072 if (!fLimitedContext)
1073 {
1074 TRACE_LOG("cx=%u, cy=%u\n", cx, cy);
1075 xf86ScrnToScreen(pScrn)->width = cx;
1076 xf86ScrnToScreen(pScrn)->height = cy;
1077 adjustScreenPixmap(pScrn, cx, cy);
1078 vbvxSetSolarisMouseRange(cx, cy);
1079 }
1080 }
1081}
1082
1083static void setScreenSizesRandR12(ScrnInfoPtr pScrn, bool fLimitedContext)
1084{
1085 VBOXPtr pVBox = VBOXGetRec(pScrn);
1086 unsigned i;
1087
1088 for (i = 0; i < pVBox->cScreens; ++i)
1089 {
1090 if (!pVBox->pScreens[i].afConnected)
1091 continue;
1092 /* The Crtc can get "unset" if the screen was disconnected previously.
1093 * I couldn't find an API to re-set it which did not have side-effects.
1094 */
1095 pVBox->pScreens[i].paOutputs->crtc = pVBox->pScreens[i].paCrtcs;
1096 xf86CrtcSetMode(pVBox->pScreens[i].paCrtcs, pVBox->pScreens[i].paOutputs->probed_modes, RR_Rotate_0,
1097 pVBox->pScreens[i].paCrtcs->x, pVBox->pScreens[i].paCrtcs->y);
1098 if (!fLimitedContext)
1099 RRCrtcNotify(pVBox->pScreens[i].paCrtcs->randr_crtc, pVBox->pScreens[i].paOutputs->randr_output->modes[0],
1100 pVBox->pScreens[i].paCrtcs->x, pVBox->pScreens[i].paCrtcs->y, RR_Rotate_0,
1101#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) >= 5
1102 NULL,
1103#endif
1104 1, &pVBox->pScreens[i].paOutputs->randr_output);
1105 }
1106}
1107
1108static void setSizesRandR12(ScrnInfoPtr pScrn, bool fLimitedContext)
1109{
1110 VBOXPtr pVBox = VBOXGetRec(pScrn);
1111
1112 if (!fLimitedContext)
1113 {
1114# if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) >= 5
1115 RRGetInfo(xf86ScrnToScreen(pScrn), TRUE);
1116# else
1117 RRGetInfo(xf86ScrnToScreen(pScrn));
1118# endif
1119 }
1120 setVirtualSizeRandR12(pScrn, fLimitedContext);
1121 setScreenSizesRandR12(pScrn, fLimitedContext);
1122 if (!fLimitedContext)
1123 {
1124 /* We use RRScreenSizeSet() here and not RRScreenSizeNotify() because
1125 * the first also pushes the virtual screen size to the input driver.
1126 * We were doing this manually by setting screenInfo.width and height
1127 * and calling xf86UpdateDesktopDimensions() where appropriate, but this
1128 * failed on Ubuntu 12.04.0 due to a problematic X server back-port. */
1129 RRScreenSizeSet(xf86ScrnToScreen(pScrn), xf86ScrnToScreen(pScrn)->width, xf86ScrnToScreen(pScrn)->height,
1130 xf86ScrnToScreen(pScrn)->mmWidth, xf86ScrnToScreen(pScrn)->mmHeight);
1131 RRTellChanged(xf86ScrnToScreen(pScrn));
1132 }
1133}
1134
1135#else
1136
1137static void setSizesRandR11(ScrnInfoPtr pScrn, bool fLimitedContext)
1138{
1139 VBOXPtr pVBox = VBOXGetRec(pScrn);
1140 DisplayModePtr pNewMode;
1141
1142 pNewMode = pScrn->modes != pScrn->currentMode ? pScrn->modes : pScrn->modes->next;
1143 pNewMode->HDisplay = RT_CLAMP(pVBox->pScreens[0].aPreferredSize.cx, VBOX_VIDEO_MIN_SIZE, VBOX_VIDEO_MAX_VIRTUAL);
1144 pNewMode->VDisplay = RT_CLAMP(pVBox->pScreens[0].aPreferredSize.cy, VBOX_VIDEO_MIN_SIZE, VBOX_VIDEO_MAX_VIRTUAL);
1145 setModeRandR11(pScrn, pNewMode, fLimitedContext);
1146}
1147
1148#endif
1149
1150static void setSizesAndCursorIntegration(ScrnInfoPtr pScrn, bool fScreenInitTime)
1151{
1152 VBOXPtr pVBox = VBOXGetRec(pScrn);
1153
1154 TRACE_LOG("fScreenInitTime=%d\n", (int)fScreenInitTime);
1155#ifdef VBOXVIDEO_13
1156 setSizesRandR12(pScrn, fScreenInitTime);
1157#else
1158 setSizesRandR11(pScrn, fScreenInitTime);
1159#endif
1160 if (pScrn->vtSema)
1161 vbvxReprobeCursor(pScrn);
1162}
1163
1164/* We update the size hints from the X11 property set by VBoxClient every time
1165 * that the X server goes to sleep (to catch the property change request).
1166 * Although this is far more often than necessary it should not have real-life
1167 * performance consequences and allows us to simplify the code quite a bit. */
1168static void updateSizeHintsBlockHandler(pointer pData, OSTimePtr pTimeout, pointer pReadmask)
1169{
1170 ScrnInfoPtr pScrn = (ScrnInfoPtr)pData;
1171 VBOXPtr pVBox = VBOXGetRec(pScrn);
1172 bool fNeedUpdate = false;
1173
1174 (void)pTimeout;
1175 (void)pReadmask;
1176 if (!pScrn->vtSema)
1177 return;
1178 vbvxReadSizesAndCursorIntegrationFromHGSMI(pScrn, &fNeedUpdate);
1179 if (ROOT_WINDOW(pScrn) != NULL)
1180 vbvxReadSizesAndCursorIntegrationFromProperties(pScrn, &fNeedUpdate);
1181 if (fNeedUpdate)
1182 setSizesAndCursorIntegration(pScrn, false);
1183}
1184
1185/*
1186 * QUOTE from the XFree86 DESIGN document:
1187 *
1188 * This is called at the start of each server generation.
1189 *
1190 * (...)
1191 *
1192 * Decide which operations need to be placed under resource access
1193 * control. (...) Map any video memory or other memory regions. (...)
1194 * Save the video card state. (...) Initialise the initial video
1195 * mode.
1196 *
1197 * End QUOTE.
1198 */
1199static Bool VBOXScreenInit(ScreenPtr pScreen, int argc, char **argv)
1200{
1201 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1202 VBOXPtr pVBox = VBOXGetRec(pScrn);
1203 VisualPtr visual;
1204 unsigned flags;
1205
1206 TRACE_ENTRY();
1207
1208 if (!VBOXMapVidMem(pScrn))
1209 return (FALSE);
1210
1211 /* save current video state */
1212 VBOXSaveMode(pScrn);
1213
1214 /* mi layer - reset the visual list (?)*/
1215 miClearVisualTypes();
1216 if (!miSetVisualTypes(pScrn->depth, TrueColorMask,
1217 pScrn->rgbBits, TrueColor))
1218 return (FALSE);
1219 if (!miSetPixmapDepths())
1220 return (FALSE);
1221
1222#ifdef VBOX_DRI
1223 pVBox->useDRI = VBOXDRIScreenInit(pScrn, pScreen, pVBox);
1224# ifndef VBOX_DRI_OLD /* DRI2 */
1225 if (pVBox->drmFD >= 0)
1226 /* Tell the kernel driver, if present, that we are taking over. */
1227 drmIoctl(pVBox->drmFD, VBOXVIDEO_IOCTL_DISABLE_HGSMI, NULL);
1228# endif
1229#endif
1230
1231 if (!fbScreenInit(pScreen, pVBox->base,
1232 pScrn->virtualX, pScrn->virtualY,
1233 pScrn->xDpi, pScrn->yDpi,
1234 pScrn->displayWidth, pScrn->bitsPerPixel))
1235 return (FALSE);
1236
1237 /* Fixup RGB ordering */
1238 /** @note the X server uses this even in true colour. */
1239 visual = pScreen->visuals + pScreen->numVisuals;
1240 while (--visual >= pScreen->visuals) {
1241 if ((visual->class | DynamicClass) == DirectColor) {
1242 visual->offsetRed = pScrn->offset.red;
1243 visual->offsetGreen = pScrn->offset.green;
1244 visual->offsetBlue = pScrn->offset.blue;
1245 visual->redMask = pScrn->mask.red;
1246 visual->greenMask = pScrn->mask.green;
1247 visual->blueMask = pScrn->mask.blue;
1248 }
1249 }
1250
1251 /* must be after RGB ordering fixed */
1252 fbPictureInit(pScreen, 0, 0);
1253
1254 xf86SetBlackWhitePixels(pScreen);
1255 pScrn->vtSema = TRUE;
1256
1257#if defined(VBOXVIDEO_13) && defined(RT_OS_LINUX)
1258 vbvxSetUpLinuxACPI(pScreen);
1259#endif
1260
1261 if (!VBoxHGSMIIsSupported())
1262 {
1263 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Graphics device too old to support.\n");
1264 return FALSE;
1265 }
1266 vbvxSetUpHGSMIHeapInGuest(pVBox, pScrn->videoRam * 1024);
1267 pVBox->cScreens = VBoxHGSMIGetMonitorCount(&pVBox->guestCtx);
1268 pVBox->pScreens = xnfcalloc(pVBox->cScreens, sizeof(*pVBox->pScreens));
1269 pVBox->paVBVAModeHints = xnfcalloc(pVBox->cScreens, sizeof(*pVBox->paVBVAModeHints));
1270 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Requested monitor count: %u\n", pVBox->cScreens);
1271 vboxEnableVbva(pScrn);
1272 /* Set up the dirty rectangle handler. It will be added into a function
1273 * chain and gets removed when the screen is cleaned up. */
1274 if (ShadowFBInit2(pScreen, NULL, vbvxHandleDirtyRect) != TRUE)
1275 return FALSE;
1276 VBoxInitialiseSizeHints(pScrn);
1277 /* Get any screen size hints from HGSMI. Do not yet try to access X11
1278 * properties, as they are not yet set up, and nor are the clients that
1279 * might have set them. */
1280 vbvxReadSizesAndCursorIntegrationFromHGSMI(pScrn, NULL);
1281
1282#ifdef VBOXVIDEO_13
1283 /* Initialise CRTC and output configuration for use with randr1.2. */
1284 xf86CrtcConfigInit(pScrn, &VBOXCrtcConfigFuncs);
1285
1286 {
1287 uint32_t i;
1288
1289 for (i = 0; i < pVBox->cScreens; ++i)
1290 {
1291 char szOutput[256];
1292
1293 /* Setup our virtual CRTCs. */
1294 pVBox->pScreens[i].paCrtcs = xf86CrtcCreate(pScrn, &VBOXCrtcFuncs);
1295 pVBox->pScreens[i].paCrtcs->driver_private = (void *)(uintptr_t)i;
1296
1297 /* Set up our virtual outputs. */
1298 snprintf(szOutput, sizeof(szOutput), "VGA-%u", i);
1299 pVBox->pScreens[i].paOutputs
1300 = xf86OutputCreate(pScrn, &VBOXOutputFuncs, szOutput);
1301
1302 /* We are not interested in the monitor section in the
1303 * configuration file. */
1304 xf86OutputUseScreenMonitor(pVBox->pScreens[i].paOutputs, FALSE);
1305 pVBox->pScreens[i].paOutputs->possible_crtcs = 1 << i;
1306 pVBox->pScreens[i].paOutputs->possible_clones = 0;
1307 pVBox->pScreens[i].paOutputs->driver_private = (void *)(uintptr_t)i;
1308 TRACE_LOG("Created crtc (%p) and output %s (%p)\n",
1309 (void *)pVBox->pScreens[i].paCrtcs, szOutput,
1310 (void *)pVBox->pScreens[i].paOutputs);
1311 }
1312 }
1313
1314 /* Set a sane minimum and maximum mode size to match what the hardware
1315 * supports. */
1316 xf86CrtcSetSizeRange(pScrn, VBOX_VIDEO_MIN_SIZE, VBOX_VIDEO_MIN_SIZE, VBOX_VIDEO_MAX_VIRTUAL, VBOX_VIDEO_MAX_VIRTUAL);
1317
1318 /* Now create our initial CRTC/output configuration. */
1319 if (!xf86InitialConfiguration(pScrn, TRUE)) {
1320 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Initial CRTC configuration failed!\n");
1321 return (FALSE);
1322 }
1323
1324 /* Initialise randr 1.2 mode-setting functions. */
1325 if (!xf86CrtcScreenInit(pScreen)) {
1326 return FALSE;
1327 }
1328
1329#endif
1330 /* set first video mode */
1331 setSizesAndCursorIntegration(pScrn, true);
1332
1333 /* Register block and wake-up handlers for getting new screen size hints. */
1334 RegisterBlockAndWakeupHandlers(updateSizeHintsBlockHandler, (WakeupHandlerProcPtr)NoopDDA, (pointer)pScrn);
1335
1336 /* software cursor */
1337 miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
1338
1339 /* colourmap code */
1340 if (!miCreateDefColormap(pScreen))
1341 return (FALSE);
1342
1343 if(!xf86HandleColormaps(pScreen, 256, 8, vboxLoadPalette, NULL, 0))
1344 return (FALSE);
1345
1346 pVBox->CloseScreen = pScreen->CloseScreen;
1347 pScreen->CloseScreen = SCRNINDEXAPI(VBOXCloseScreen);
1348#ifdef VBOXVIDEO_13
1349 pScreen->SaveScreen = xf86SaveScreen;
1350#else
1351 pScreen->SaveScreen = VBOXSaveScreen;
1352#endif
1353
1354#ifdef VBOXVIDEO_13
1355 xf86DPMSInit(pScreen, xf86DPMSSet, 0);
1356#else
1357 /* We probably do want to support power management - even if we just use
1358 a dummy function. */
1359 xf86DPMSInit(pScreen, VBOXDisplayPowerManagementSet, 0);
1360#endif
1361
1362 /* Report any unused options (only for the first generation) */
1363 if (serverGeneration == 1)
1364 xf86ShowUnusedOptions(pScrn->scrnIndex, pScrn->options);
1365
1366 if (vbvxCursorInit(pScreen) != TRUE)
1367 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1368 "Unable to start the VirtualBox mouse pointer integration with the host system.\n");
1369
1370#ifdef VBOX_DRI_OLD
1371 if (pVBox->useDRI)
1372 pVBox->useDRI = VBOXDRIFinishScreenInit(pScreen);
1373#endif
1374
1375 initialiseProperties(pScrn);
1376
1377 return (TRUE);
1378}
1379
1380static Bool VBOXEnterVT(ScrnInfoPtr pScrn)
1381{
1382 VBOXPtr pVBox = VBOXGetRec(pScrn);
1383
1384 TRACE_ENTRY();
1385#ifdef VBOX_DRI_OLD
1386 if (pVBox->useDRI)
1387 DRIUnlock(xf86ScrnToScreen(pScrn));
1388#elif defined(VBOX_DRI) /* DRI2 */
1389 if (pVBox->drmFD >= 0)
1390 {
1391 /* Tell the kernel driver, if present, that we are taking over. */
1392 drmSetMaster(pVBox->drmFD);
1393 }
1394#endif
1395 vbvxSetUpHGSMIHeapInGuest(pVBox, pScrn->videoRam * 1024);
1396 vboxEnableVbva(pScrn);
1397 /* Re-set video mode */
1398 vbvxReadSizesAndCursorIntegrationFromHGSMI(pScrn, NULL);
1399 vbvxReadSizesAndCursorIntegrationFromProperties(pScrn, NULL);
1400 /* This prevents a crash in CentOS 3. I was unable to debug it to
1401 * satisfaction, partly due to the lack of symbols. My guess is that
1402 * pScrn->ModifyPixmapHeader() expects certain things to be set up when
1403 * it sees pScrn->vtSema set to true which are not quite done at this
1404 * point of the VT switch. */
1405 pScrn->vtSema = FALSE;
1406 setSizesAndCursorIntegration(pScrn, false);
1407 pScrn->vtSema = TRUE;
1408#ifdef SET_HAVE_VT_PROPERTY
1409 updateHasVTProperty(pScrn, TRUE);
1410#endif
1411 return TRUE;
1412}
1413
1414static void VBOXLeaveVT(ScrnInfoPtr pScrn)
1415{
1416 VBOXPtr pVBox = VBOXGetRec(pScrn);
1417#ifdef VBOXVIDEO_13
1418 unsigned i;
1419#endif
1420
1421 TRACE_ENTRY();
1422#ifdef VBOXVIDEO_13
1423 for (i = 0; i < pVBox->cScreens; ++i)
1424 vbox_crtc_dpms(pVBox->pScreens[i].paCrtcs, DPMSModeOff);
1425#endif
1426 vboxDisableVbva(pScrn);
1427 vbvxClearVRAM(pScrn, ((size_t)pScrn->virtualX) * pScrn->virtualY * (pScrn->bitsPerPixel / 8), 0);
1428#ifdef VBOX_DRI_OLD
1429 if (pVBox->useDRI)
1430 DRILock(xf86ScrnToScreen(pScrn), 0);
1431#elif defined(VBOX_DRI) /* DRI2 */
1432 if (pVBox->drmFD >= 0)
1433 drmDropMaster(pVBox->drmFD);
1434#endif
1435 VBOXRestoreMode(pScrn);
1436#ifdef SET_HAVE_VT_PROPERTY
1437 updateHasVTProperty(pScrn, FALSE);
1438#endif
1439 TRACE_EXIT();
1440}
1441
1442static Bool VBOXCloseScreen(ScreenPtr pScreen)
1443{
1444 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1445 VBOXPtr pVBox = VBOXGetRec(pScrn);
1446#if defined(VBOX_DRI) && !defined(VBOX_DRI_OLD) /* DRI2 */
1447 BOOL fRestore = TRUE;
1448#endif
1449 if (pScrn->vtSema)
1450 {
1451#ifdef VBOXVIDEO_13
1452 unsigned i;
1453
1454 for (i = 0; i < pVBox->cScreens; ++i)
1455 vbox_crtc_dpms(pVBox->pScreens[i].paCrtcs, DPMSModeOff);
1456#endif
1457 vboxDisableVbva(pScrn);
1458 vbvxClearVRAM(pScrn, ((size_t)pScrn->virtualX) * pScrn->virtualY * (pScrn->bitsPerPixel / 8), 0);
1459 }
1460#ifdef VBOX_DRI
1461# ifndef VBOX_DRI_OLD /* DRI2 */
1462 if ( pVBox->drmFD >= 0
1463 /* Tell the kernel driver, if present, that we are going away. */
1464 && drmIoctl(pVBox->drmFD, VBOXVIDEO_IOCTL_ENABLE_HGSMI, NULL) >= 0)
1465 fRestore = false;
1466# endif
1467 if (pVBox->useDRI)
1468 VBOXDRICloseScreen(pScreen, pVBox);
1469 pVBox->useDRI = false;
1470#endif
1471#if defined(VBOX_DRI) && !defined(VBOX_DRI_OLD) /* DRI2 */
1472 if (fRestore)
1473#endif
1474 if (pScrn->vtSema)
1475 VBOXRestoreMode(pScrn);
1476 if (pScrn->vtSema)
1477 VBOXUnmapVidMem(pScrn);
1478 pScrn->vtSema = FALSE;
1479
1480 vbvxCursorTerm(pVBox);
1481
1482 pScreen->CloseScreen = pVBox->CloseScreen;
1483#if defined(VBOXVIDEO_13) && defined(RT_OS_LINUX)
1484 vbvxCleanUpLinuxACPI(pScreen);
1485#endif
1486#ifndef XF86_SCRN_INTERFACE
1487 return pScreen->CloseScreen(pScreen->myNum, pScreen);
1488#else
1489 return pScreen->CloseScreen(pScreen);
1490#endif
1491}
1492
1493static Bool VBOXSwitchMode(ScrnInfoPtr pScrn, DisplayModePtr pMode)
1494{
1495 VBOXPtr pVBox;
1496 Bool rc = TRUE;
1497
1498 TRACE_LOG("HDisplay=%d, VDisplay=%d\n", pMode->HDisplay, pMode->VDisplay);
1499 if (!pScrn->vtSema)
1500 {
1501 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1502 "We do not own the active VT, exiting.\n");
1503 return TRUE;
1504 }
1505#ifdef VBOXVIDEO_13
1506 rc = xf86SetSingleMode(pScrn, pMode, RR_Rotate_0);
1507#else
1508 setModeRandR11(pScrn, pMode, false);
1509#endif
1510 TRACE_LOG("returning %s\n", rc ? "TRUE" : "FALSE");
1511 return rc;
1512}
1513
1514static void VBOXAdjustFrame(ScrnInfoPtr pScrn, int x, int y)
1515{ (void)pScrn; (void)x; (void)y; }
1516
1517static void VBOXFreeScreen(ScrnInfoPtr pScrn)
1518{
1519 /* Destroy the VGA hardware record */
1520 vgaHWFreeHWRec(pScrn);
1521 /* And our private record */
1522 free(pScrn->driverPrivate);
1523 pScrn->driverPrivate = NULL;
1524}
1525
1526static Bool
1527VBOXMapVidMem(ScrnInfoPtr pScrn)
1528{
1529 VBOXPtr pVBox = VBOXGetRec(pScrn);
1530 Bool rc = TRUE;
1531
1532 TRACE_ENTRY();
1533 if (!pVBox->base)
1534 {
1535#ifdef PCIACCESS
1536 (void) pci_device_map_range(pVBox->pciInfo,
1537 pScrn->memPhysBase,
1538 pScrn->videoRam * 1024,
1539 PCI_DEV_MAP_FLAG_WRITABLE,
1540 & pVBox->base);
1541#else
1542 pVBox->base = xf86MapPciMem(pScrn->scrnIndex,
1543 VIDMEM_FRAMEBUFFER,
1544 pVBox->pciTag, pScrn->memPhysBase,
1545 (unsigned) pScrn->videoRam * 1024);
1546#endif
1547 if (!pVBox->base)
1548 rc = FALSE;
1549 }
1550 TRACE_LOG("returning %s\n", rc ? "TRUE" : "FALSE");
1551 return rc;
1552}
1553
1554static void
1555VBOXUnmapVidMem(ScrnInfoPtr pScrn)
1556{
1557 VBOXPtr pVBox = VBOXGetRec(pScrn);
1558
1559 TRACE_ENTRY();
1560 if (pVBox->base == NULL)
1561 return;
1562
1563#ifdef PCIACCESS
1564 (void) pci_device_unmap_range(pVBox->pciInfo,
1565 pVBox->base,
1566 pScrn->videoRam * 1024);
1567#else
1568 xf86UnMapVidMem(pScrn->scrnIndex, pVBox->base,
1569 (unsigned) pScrn->videoRam * 1024);
1570#endif
1571 pVBox->base = NULL;
1572 TRACE_EXIT();
1573}
1574
1575static Bool
1576VBOXSaveScreen(ScreenPtr pScreen, int mode)
1577{
1578 (void)pScreen; (void)mode;
1579 return TRUE;
1580}
1581
1582void
1583VBOXSaveMode(ScrnInfoPtr pScrn)
1584{
1585 VBOXPtr pVBox = VBOXGetRec(pScrn);
1586 vgaRegPtr vgaReg;
1587
1588 TRACE_ENTRY();
1589 vgaReg = &VGAHWPTR(pScrn)->SavedReg;
1590 vgaHWSave(pScrn, vgaReg, VGA_SR_ALL);
1591 pVBox->fSavedVBEMode = VBoxVideoGetModeRegisters(&pVBox->cSavedWidth,
1592 &pVBox->cSavedHeight,
1593 &pVBox->cSavedPitch,
1594 &pVBox->cSavedBPP,
1595 &pVBox->fSavedFlags);
1596}
1597
1598void
1599VBOXRestoreMode(ScrnInfoPtr pScrn)
1600{
1601 VBOXPtr pVBox = VBOXGetRec(pScrn);
1602 vgaRegPtr vgaReg;
1603#ifdef VBOX_DRI
1604 drmModeResPtr pRes;
1605#endif
1606
1607 TRACE_ENTRY();
1608#ifdef VBOX_DRI
1609 /* Do not try to re-set the VGA state if a mode-setting driver is loaded. */
1610 if ( pVBox->drmFD >= 0
1611 && LoaderSymbol("drmModeGetResources") != NULL
1612 && (pRes = drmModeGetResources(pVBox->drmFD)) != NULL)
1613 {
1614 drmModeFreeResources(pRes);
1615 return;
1616 }
1617#endif
1618 vgaReg = &VGAHWPTR(pScrn)->SavedReg;
1619 vgaHWRestore(pScrn, vgaReg, VGA_SR_ALL);
1620 if (pVBox->fSavedVBEMode)
1621 VBoxVideoSetModeRegisters(pVBox->cSavedWidth, pVBox->cSavedHeight,
1622 pVBox->cSavedPitch, pVBox->cSavedBPP,
1623 pVBox->fSavedFlags, 0, 0);
1624 else
1625 VBoxVideoDisableVBE();
1626}
1627
1628static void
1629VBOXDisplayPowerManagementSet(ScrnInfoPtr pScrn, int mode,
1630 int flags)
1631{
1632 (void)pScrn; (void)mode; (void) flags;
1633}
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