VBoxGuestR3LibVideo.cpp revision 39a628c9e979cb2355caa57eb099b13cb922783c
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync/* $Id$ */
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync/** @file
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Video.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync */
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync/*
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * Copyright (C) 2007 Sun Microsystems, Inc.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync *
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * available from http://www.virtualbox.org. This file is free software;
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * you can redistribute it and/or modify it under the terms of the GNU
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * General Public License (GPL) as published by the Free Software
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync *
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * additional information or have any questions.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync */
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync/*******************************************************************************
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync* Header Files *
c312e1b81dffe42e0fb766020fb8defaeade05d6vboxsync*******************************************************************************/
c312e1b81dffe42e0fb766020fb8defaeade05d6vboxsync#include <iprt/string.h>
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync#include <iprt/mem.h>
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync#include <iprt/assert.h>
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync#include <VBox/log.h>
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync#include <VBox/HostServices/GuestPropertySvc.h> /* For Save and RetrieveVideoMode */
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync#include "VBGLR3Internal.h"
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync#define VIDEO_PROP_PREFIX "/VirtualBox/GuestAdd/Vbgl/Video/"
50f0e2e83362e100d306a411980d555d46aa00a8vboxsync
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync/**
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * Enable or disable video acceleration.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync *
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * @returns VBox status code.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync *
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * @param fEnable Pass zero to disable, any other value to enable.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync */
b0dfb334954c0552bb583967a3077ec88fd00471vboxsyncVBGLR3DECL(int) VbglR3VideoAccelEnable(bool fEnable)
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync{
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync VMMDevVideoAccelEnable Req;
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync vmmdevInitRequest(&Req.header, VMMDevReq_VideoAccelEnable);
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync Req.u32Enable = fEnable;
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync Req.cbRingBuffer = VBVA_RING_BUFFER_SIZE;
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync Req.fu32Status = 0;
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync return vbglR3GRPerform(&Req.header);
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync}
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync/**
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * Flush the video buffer.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync *
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * @returns VBox status code.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync */
b0dfb334954c0552bb583967a3077ec88fd00471vboxsyncVBGLR3DECL(int) VbglR3VideoAccelFlush(void)
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync{
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync VMMDevVideoAccelFlush Req;
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync vmmdevInitRequest(&Req.header, VMMDevReq_VideoAccelFlush);
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync return vbglR3GRPerform(&Req.header);
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync}
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync
50f0e2e83362e100d306a411980d555d46aa00a8vboxsync/**
50f0e2e83362e100d306a411980d555d46aa00a8vboxsync * Send mouse pointer shape information to the host.
50f0e2e83362e100d306a411980d555d46aa00a8vboxsync *
54828795a553ed0731f308ebda81675ad2c39d58vboxsync * @returns VBox status code.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync *
54828795a553ed0731f308ebda81675ad2c39d58vboxsync * @param fFlags Mouse pointer flags.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * @param xHot X coordinate of hot spot.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * @param yHot Y coordinate of hot spot.
54828795a553ed0731f308ebda81675ad2c39d58vboxsync * @param cx Pointer width.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * @param cy Pointer height.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * @param pvImg Pointer to the image data (can be NULL).
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * @param cbImg Size of the image data pointed to by pvImg.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync */
b0dfb334954c0552bb583967a3077ec88fd00471vboxsyncVBGLR3DECL(int) VbglR3SetPointerShape(uint32_t fFlags, uint32_t xHot, uint32_t yHot, uint32_t cx, uint32_t cy, const void *pvImg, size_t cbImg)
50f0e2e83362e100d306a411980d555d46aa00a8vboxsync{
50f0e2e83362e100d306a411980d555d46aa00a8vboxsync VMMDevReqMousePointer *pReq;
50f0e2e83362e100d306a411980d555d46aa00a8vboxsync int rc = vbglR3GRAlloc((VMMDevRequestHeader **)&pReq, RT_OFFSETOF(VMMDevReqMousePointer, pointerData) + cbImg, VMMDevReq_SetPointerShape);
54828795a553ed0731f308ebda81675ad2c39d58vboxsync if (RT_SUCCESS(rc))
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync {
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync pReq->fFlags = fFlags;
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync pReq->xHot = xHot;
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync pReq->yHot = yHot;
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync pReq->width = cx;
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync pReq->height = cy;
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync if (pvImg)
50f0e2e83362e100d306a411980d555d46aa00a8vboxsync memcpy(pReq->pointerData, pvImg, cbImg);
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync rc = vbglR3GRPerform(&pReq->header);
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync vbglR3GRFree(&pReq->header);
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync if (RT_SUCCESS(rc))
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync rc = pReq->header.rc;
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync }
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync return rc;
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync}
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync/**
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync * Send mouse pointer shape information to the host.
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync * This version of the function accepts a request for clients that
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync * already allocate and manipulate the request structure directly.
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync *
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync * @returns VBox status code.
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync *
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync * @param pReq Pointer to the VMMDevReqMousePointer structure.
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync */
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsyncVBGLR3DECL(int) VbglR3SetPointerShapeReq(VMMDevReqMousePointer *pReq)
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync{
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync int rc = vbglR3GRPerform(&pReq->header);
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync if (RT_SUCCESS(rc))
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync rc = pReq->header.rc;
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync return rc;
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync}
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync/**
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync * Query the last display change request.
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync *
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync * @returns iprt status value
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync * @param pcx Where to store the horizontal pixel resolution (0 = do not change).
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync * @param pcy Where to store the vertical pixel resolution (0 = do not change).
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync * @param pcBits Where to store the bits per pixel (0 = do not change).
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync * @param iDisplay Where to store the display number the request was for - 0 for the
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync * primary display, 1 for the first secondary, etc.
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync */
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsyncVBGLR3DECL(int) VbglR3GetLastDisplayChangeRequest(uint32_t *pcx, uint32_t *pcy, uint32_t *pcBits, uint32_t *piDisplay)
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync{
VMMDevDisplayChangeRequest2 Req = { { 0 } };
#ifndef VBOX_VBGLR3_XFREE86
AssertPtrReturn(pcx, VERR_INVALID_PARAMETER);
AssertPtrReturn(pcy, VERR_INVALID_PARAMETER);
AssertPtrReturn(pcBits, VERR_INVALID_PARAMETER);
AssertPtrReturn(piDisplay, VERR_INVALID_PARAMETER);
#endif
vmmdevInitRequest(&Req.header, VMMDevReq_GetDisplayChangeRequest2);
int rc = vbglR3GRPerform(&Req.header);
if (RT_SUCCESS(rc))
rc = Req.header.rc;
if (RT_SUCCESS(rc))
{
*pcx = Req.xres;
*pcy = Req.yres;
*pcBits = Req.bpp;
*piDisplay = Req.display;
}
return rc;
}
/**
* Wait for a display change request event from the host. These events must have been
* activated previously using VbglR3CtlFilterMask.
*
* @returns IPRT status value
* @param pcx On success, where to return the requested display width.
* 0 means no change.
* @param pcy On success, where to return the requested display height.
* 0 means no change.
* @param pcBits On success, where to return the requested bits per pixel.
* 0 means no change.
* @param piDisplay On success, where to return the index of the display to be changed.
*/
VBGLR3DECL(int) VbglR3DisplayChangeWaitEvent(uint32_t *pcx, uint32_t *pcy, uint32_t *pcBits, uint32_t *piDisplay)
{
VBoxGuestWaitEventInfo waitEvent;
int rc;
#ifndef VBOX_VBGLR3_XFREE86
AssertPtrReturn(pcx, VERR_INVALID_PARAMETER);
AssertPtrReturn(pcy, VERR_INVALID_PARAMETER);
AssertPtrReturn(pcBits, VERR_INVALID_PARAMETER);
AssertPtrReturn(piDisplay, VERR_INVALID_PARAMETER);
#endif
waitEvent.u32TimeoutIn = RT_INDEFINITE_WAIT;
waitEvent.u32EventMaskIn = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST;
waitEvent.u32Result = VBOXGUEST_WAITEVENT_ERROR;
waitEvent.u32EventFlagsOut = 0;
rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_WAITEVENT, &waitEvent, sizeof(waitEvent));
if (RT_SUCCESS(rc))
{
/* did we get the right event? */
if (waitEvent.u32EventFlagsOut & VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST)
{
VMMDevDisplayChangeRequest2 Req = { { 0 } };
vmmdevInitRequest(&Req.header, VMMDevReq_GetDisplayChangeRequest2);
Req.eventAck = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST;
int rc = vbglR3GRPerform(&Req.header);
if (RT_SUCCESS(rc))
rc = Req.header.rc;
if (RT_SUCCESS(rc))
{
*pcx = Req.xres;
*pcy = Req.yres;
*pcBits = Req.bpp;
*piDisplay = Req.display;
}
}
else
rc = VERR_TRY_AGAIN;
}
return rc;
}
/**
* Query the host as to whether it likes a specific video mode.
*
* @returns the result of the query
* @param cx the width of the mode being queried
* @param cy the height of the mode being queried
* @param cBits the bpp of the mode being queried
*/
VBGLR3DECL(bool) VbglR3HostLikesVideoMode(uint32_t cx, uint32_t cy, uint32_t cBits)
{
bool fRc = true; /* If for some reason we can't contact the host then
* we like everything. */
int rc;
VMMDevVideoModeSupportedRequest req;
vmmdevInitRequest(&req.header, VMMDevReq_VideoModeSupported);
req.width = cx;
req.height = cy;
req.bpp = cBits;
req.fSupported = true;
rc = vbglR3GRPerform(&req.header);
if (RT_SUCCESS(rc) && RT_SUCCESS(req.header.rc))
fRc = req.fSupported;
return fRc;
}
/**
* Save video mode parameters to the registry.
*
* @returns iprt status value
* @param pszName the name to save the mode parameters under
* @param cx mode width
* @param cy mode height
* @param cBits bits per pixel for the mode
*/
VBGLR3DECL(int) VbglR3SaveVideoMode(const char *pszName, uint32_t cx, uint32_t cy, uint32_t cBits)
{
#ifdef VBOX_WITH_GUEST_PROPS
using namespace guestProp;
char szModeName[MAX_NAME_LEN];
char szModeParms[MAX_VALUE_LEN];
uint32_t u32ClientId = 0;
RTStrPrintf(szModeName, sizeof(szModeName), VIDEO_PROP_PREFIX"%s", pszName);
RTStrPrintf(szModeParms, sizeof(szModeParms), "%dx%dx%d", cx, cy, cBits);
int rc = VbglR3GuestPropConnect(&u32ClientId);
if (RT_SUCCESS(rc))
rc = VbglR3GuestPropWriteValue(u32ClientId, szModeName, szModeParms);
if (u32ClientId != 0)
VbglR3GuestPropDisconnect(u32ClientId); /* Return value ignored, because what can we do anyway? */
return rc;
#else /* VBOX_WITH_GUEST_PROPS not defined */
return VERR_NOT_IMPLEMENTED;
#endif /* VBOX_WITH_GUEST_PROPS not defined */
}
/**
* Retrieve video mode parameters from the guest property store.
*
* @returns iprt status value
* @param pszName the name under which the mode parameters are saved
* @param pcx where to store the mode width
* @param pcy where to store the mode height
* @param pcBits where to store the bits per pixel for the mode
*/
VBGLR3DECL(int) VbglR3RetrieveVideoMode(const char *pszName, uint32_t *pcx, uint32_t *pcy, uint32_t *pcBits)
{
#ifdef VBOX_WITH_GUEST_PROPS
using namespace guestProp;
/*
* First we retreive the video mode which is saved as a string in the
* guest property store.
*/
/* The buffer for VbglR3GuestPropReadValue. If this is too small then
* something is wrong with the data stored in the property. */
char szModeParms[1024];
uint32_t u32ClientId = 0;
uint32_t cx, cy, cBits;
int rc = VbglR3GuestPropConnect(&u32ClientId);
if (RT_SUCCESS(rc))
{
char szModeName[MAX_NAME_LEN];
RTStrPrintf(szModeName, sizeof(szModeName), VIDEO_PROP_PREFIX"%s", pszName);
/** @todo add a VbglR3GuestPropReadValueF/FV that does the RTStrPrintf for you. */
rc = VbglR3GuestPropReadValue(u32ClientId, szModeName, szModeParms,
sizeof(szModeParms), NULL);
}
/*
* Now we convert the string returned to numeric values.
*/
char *pszNext;
if (RT_SUCCESS(rc))
/* Extract the width from the string */
rc = RTStrToUInt32Ex(szModeParms, &pszNext, 10, &cx);
if ((rc != VWRN_TRAILING_CHARS) || (*pszNext != 'x'))
rc = VERR_PARSE_ERROR;
if (RT_SUCCESS(rc))
{
/* Extract the height from the string */
++pszNext;
rc = RTStrToUInt32Ex(pszNext, &pszNext, 10, &cy);
}
if ((rc != VWRN_TRAILING_CHARS) || (*pszNext != 'x'))
rc = VERR_PARSE_ERROR;
if (RT_SUCCESS(rc))
{
/* Extract the bpp from the string */
++pszNext;
rc = RTStrToUInt32Full(pszNext, 10, &cBits);
}
if (rc != VINF_SUCCESS)
rc = VERR_PARSE_ERROR;
/*
* And clean up and return the values if we successfully obtained them.
*/
if (u32ClientId != 0)
VbglR3GuestPropDisconnect(u32ClientId); /* Return value ignored, because what can we do anyway? */
if (RT_SUCCESS(rc))
{
*pcx = cx;
*pcy = cy;
*pcBits = cBits;
}
return rc;
#else /* VBOX_WITH_GUEST_PROPS not defined */
return VERR_NOT_IMPLEMENTED;
#endif /* VBOX_WITH_GUEST_PROPS not defined */
}