1/* $Id: setmode.c,v 1.2 2020/10/22 20:47:23 thorpej Exp $ */
2/** @file
3 * Linux Additions X11 graphics driver, mode setting
4 */
5
6/*
7 * Copyright (C) 2006-2017 Oracle Corporation
8 *
9 * This code is based on:
10 *
11 * X11 VESA driver
12 *
13 * Copyright (c) 2000 by Conectiva S.A. (http://www.conectiva.com)
14 *
15 * Permission is hereby granted, free of charge, to any person obtaining a
16 * copy of this software and associated documentation files (the "Software"),
17 * to deal in the Software without restriction, including without limitation
18 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
19 * and/or sell copies of the Software, and to permit persons to whom the
20 * Software is furnished to do so, subject to the following conditions:
21 *
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
28 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
29 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
30 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
31 * USE OR OTHER DEALINGS IN THE SOFTWARE.
32 *
33 * Except as contained in this notice, the name of Conectiva Linux shall
34 * not be used in advertising or otherwise to promote the sale, use or other
35 * dealings in this Software without prior written authorization from
36 * Conectiva Linux.
37 *
38 * Authors: Paulo César Pereira de Andrade <pcpa@conectiva.com.br>
39 *          Michael Thayer <michael.thayer@oracle.com>
40 */
41
42#ifdef XORG_7X
43/* We include <unistd.h> for Solaris below, and the ANSI C emulation layer
44 * interferes with that. */
45# define _XF86_ANSIC_H
46# define XF86_LIBC_H
47# include <string.h>
48#endif
49#include "vboxvideo_drv.h"
50#include "xf86.h"
51
52/* VGA hardware functions for setting and restoring text mode */
53#include "vgaHW.h"
54
55#ifdef RT_OS_SOLARIS
56# include <sys/vuid_event.h>
57# include <sys/msio.h>
58# include <errno.h>
59# include <fcntl.h>
60# include <unistd.h>
61#endif
62
63/** Clear the virtual framebuffer in VRAM.  Optionally also clear up to the
64 * size of a new framebuffer.  Framebuffer sizes larger than available VRAM
65 * be treated as zero and passed over. */
66void vbvxClearVRAM(ScrnInfoPtr pScrn, size_t cbOldSize, size_t cbNewSize)
67{
68    VBOXPtr pVBox = VBOXGetRec(pScrn);
69
70    /* Assume 32BPP - this is just a sanity test. */
71    AssertMsg(   cbOldSize / 4 <= VBOX_VIDEO_MAX_VIRTUAL * VBOX_VIDEO_MAX_VIRTUAL
72               && cbNewSize / 4 <= VBOX_VIDEO_MAX_VIRTUAL * VBOX_VIDEO_MAX_VIRTUAL,
73               ("cbOldSize=%llu cbNewSize=%llu, max=%u.\n", (unsigned long long)cbOldSize, (unsigned long long)cbNewSize,
74                VBOX_VIDEO_MAX_VIRTUAL * VBOX_VIDEO_MAX_VIRTUAL));
75    if (cbOldSize > (size_t)pVBox->cbFBMax)
76        cbOldSize = pVBox->cbFBMax;
77    if (cbNewSize > (size_t)pVBox->cbFBMax)
78        cbNewSize = pVBox->cbFBMax;
79    memset(pVBox->base, 0, max(cbOldSize, cbNewSize));
80}
81
82/** Set a graphics mode.  Poke any required values into registers, do an HGSMI
83 * mode set and tell the host we support advanced graphics functions.
84 */
85void vbvxSetMode(ScrnInfoPtr pScrn, unsigned cDisplay, unsigned cWidth, unsigned cHeight, int x, int y, Bool fEnabled,
86                 Bool fConnected, struct vbvxFrameBuffer *pFrameBuffer)
87{
88    VBOXPtr pVBox = VBOXGetRec(pScrn);
89    uint32_t offStart;
90    uint16_t fFlags;
91    int rc;
92    Bool fEnabledAndVisible = fEnabled && x + cWidth <= pFrameBuffer->cWidth && y + cHeight <= pFrameBuffer->cHeight;
93    /* Recent host code has a flag to blank the screen; older code needs BPP set to zero. */
94    uint32_t cBPP = fEnabledAndVisible || pVBox->fHostHasScreenBlankingFlag ? pFrameBuffer->cBPP : 0;
95
96    TRACE_LOG("cDisplay=%u, cWidth=%u, cHeight=%u, x=%d, y=%d, fEnabled=%d, fConnected=%d, pFrameBuffer: { x0=%d, y0=%d, cWidth=%u, cHeight=%u, cBPP=%u }\n",
97              cDisplay, cWidth, cHeight, x, y, fEnabled, fConnected, pFrameBuffer->x0, pFrameBuffer->y0, pFrameBuffer->cWidth,
98              pFrameBuffer->cHeight, pFrameBuffer->cBPP);
99    AssertMsg(cWidth != 0 && cHeight != 0, ("cWidth = 0 or cHeight = 0\n"));
100    offStart = (y * pFrameBuffer->cWidth + x) * pFrameBuffer->cBPP / 8;
101    if (cDisplay == 0 && fEnabled)
102        VBoxVideoSetModeRegisters(cWidth, cHeight, pFrameBuffer->cWidth, pFrameBuffer->cBPP, 0, x, y);
103    fFlags = VBVA_SCREEN_F_ACTIVE;
104    fFlags |= (fConnected ? 0 : VBVA_SCREEN_F_DISABLED);
105    fFlags |= (!fEnabledAndVisible && pVBox->fHostHasScreenBlankingFlag ? VBVA_SCREEN_F_BLANK : 0);
106    VBoxHGSMIProcessDisplayInfo(&pVBox->guestCtx, cDisplay, x - pFrameBuffer->x0, y - pFrameBuffer->y0, offStart,
107                                pFrameBuffer->cWidth * pFrameBuffer->cBPP / 8, cWidth, cHeight, cBPP, fFlags);
108    rc = VBoxHGSMIUpdateInputMapping(&pVBox->guestCtx, 0 - pFrameBuffer->x0, 0 - pFrameBuffer->y0, pFrameBuffer->cWidth,
109                                     pFrameBuffer->cHeight);
110    if (RT_FAILURE(rc))
111        FatalError("Failed to update the input mapping.\n");
112}
113
114/** Tell the virtual mouse device about the new virtual desktop size. */
115void vbvxSetSolarisMouseRange(int width, int height)
116{
117#ifdef RT_OS_SOLARIS
118    int rc;
119    int hMouse = open("/dev/mouse", O_RDWR);
120
121    if (hMouse >= 0)
122    {
123        do {
124            Ms_screen_resolution Res = { height, width };
125            rc = ioctl(hMouse, MSIOSRESOLUTION, &Res);
126        } while ((rc != 0) && (errno == EINTR));
127        close(hMouse);
128    }
129#else
130    (void)width; (void)height;
131#endif
132}
133