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