VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibVideo.cpp@ 28736

Last change on this file since 28736 was 27687, checked in by vboxsync, 15 years ago

Additons/common/VBoxGuestLibR3: make saving and restoring video modes work with XFree86 and clean up the code

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 10.2 KB
Line 
1/* $Id: VBoxGuestR3LibVideo.cpp 27687 2010-03-24 22:14:20Z vboxsync $ */
2/** @file
3 * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Video.
4 */
5
6/*
7 * Copyright (C) 2007-2009 Sun Microsystems, Inc.
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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include <iprt/assert.h>
36#include <iprt/mem.h>
37#include <iprt/string.h>
38#include <VBox/log.h>
39#include <VBox/HostServices/GuestPropertySvc.h> /* For Save and RetrieveVideoMode */
40
41#include "VBGLR3Internal.h"
42
43#define VIDEO_PROP_PREFIX "/VirtualBox/GuestAdd/Vbgl/Video/"
44
45/**
46 * Enable or disable video acceleration.
47 *
48 * @returns VBox status code.
49 *
50 * @param fEnable Pass zero to disable, any other value to enable.
51 */
52VBGLR3DECL(int) VbglR3VideoAccelEnable(bool fEnable)
53{
54 VMMDevVideoAccelEnable Req;
55 vmmdevInitRequest(&Req.header, VMMDevReq_VideoAccelEnable);
56 Req.u32Enable = fEnable;
57 Req.cbRingBuffer = VBVA_RING_BUFFER_SIZE;
58 Req.fu32Status = 0;
59 return vbglR3GRPerform(&Req.header);
60}
61
62
63/**
64 * Flush the video buffer.
65 *
66 * @returns VBox status code.
67 */
68VBGLR3DECL(int) VbglR3VideoAccelFlush(void)
69{
70 VMMDevVideoAccelFlush Req;
71 vmmdevInitRequest(&Req.header, VMMDevReq_VideoAccelFlush);
72 return vbglR3GRPerform(&Req.header);
73}
74
75
76/**
77 * Send mouse pointer shape information to the host.
78 *
79 * @returns VBox status code.
80 *
81 * @param fFlags Mouse pointer flags.
82 * @param xHot X coordinate of hot spot.
83 * @param yHot Y coordinate of hot spot.
84 * @param cx Pointer width.
85 * @param cy Pointer height.
86 * @param pvImg Pointer to the image data (can be NULL).
87 * @param cbImg Size of the image data pointed to by pvImg.
88 */
89VBGLR3DECL(int) VbglR3SetPointerShape(uint32_t fFlags, uint32_t xHot, uint32_t yHot, uint32_t cx, uint32_t cy, const void *pvImg, size_t cbImg)
90{
91 VMMDevReqMousePointer *pReq;
92 int rc = vbglR3GRAlloc((VMMDevRequestHeader **)&pReq, RT_OFFSETOF(VMMDevReqMousePointer, pointerData) + cbImg, VMMDevReq_SetPointerShape);
93 if (RT_SUCCESS(rc))
94 {
95 pReq->fFlags = fFlags;
96 pReq->xHot = xHot;
97 pReq->yHot = yHot;
98 pReq->width = cx;
99 pReq->height = cy;
100 if (pvImg)
101 memcpy(pReq->pointerData, pvImg, cbImg);
102
103 rc = vbglR3GRPerform(&pReq->header);
104 vbglR3GRFree(&pReq->header);
105 if (RT_SUCCESS(rc))
106 rc = pReq->header.rc;
107 }
108 return rc;
109}
110
111
112/**
113 * Send mouse pointer shape information to the host.
114 * This version of the function accepts a request for clients that
115 * already allocate and manipulate the request structure directly.
116 *
117 * @returns VBox status code.
118 *
119 * @param pReq Pointer to the VMMDevReqMousePointer structure.
120 */
121VBGLR3DECL(int) VbglR3SetPointerShapeReq(VMMDevReqMousePointer *pReq)
122{
123 int rc = vbglR3GRPerform(&pReq->header);
124 if (RT_SUCCESS(rc))
125 rc = pReq->header.rc;
126 return rc;
127}
128
129
130/**
131 * Query the last display change request sent from the host to the guest.
132 *
133 * @returns iprt status value
134 * @param pcx Where to store the horizontal pixel resolution
135 * requested (a value of zero means do not change).
136 * @param pcy Where to store the vertical pixel resolution
137 * requested (a value of zero means do not change).
138 * @param pcBits Where to store the bits per pixel requested (a value
139 * of zero means do not change).
140 * @param iDisplay Where to store the display number the request was for
141 * - 0 for the primary display, 1 for the first
142 * secondary display, etc.
143 * @param fAck whether or not to acknowlege the newest request sent by
144 * the host. If this is set, the function will return the
145 * most recent host request, otherwise it will return the
146 * last request to be acknowleged.
147 *
148 */
149VBGLR3DECL(int) VbglR3GetDisplayChangeRequest(uint32_t *pcx, uint32_t *pcy, uint32_t *pcBits, uint32_t *piDisplay, bool fAck)
150{
151 VMMDevDisplayChangeRequest2 Req;
152
153 AssertPtrReturn(pcx, VERR_INVALID_PARAMETER);
154 AssertPtrReturn(pcy, VERR_INVALID_PARAMETER);
155 AssertPtrReturn(pcBits, VERR_INVALID_PARAMETER);
156 AssertPtrReturn(piDisplay, VERR_INVALID_PARAMETER);
157 RT_ZERO(Req);
158 vmmdevInitRequest(&Req.header, VMMDevReq_GetDisplayChangeRequest2);
159 if (fAck)
160 Req.eventAck = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST;
161 int rc = vbglR3GRPerform(&Req.header);
162 if (RT_SUCCESS(rc))
163 rc = Req.header.rc;
164 if (RT_SUCCESS(rc))
165 {
166 *pcx = Req.xres;
167 *pcy = Req.yres;
168 *pcBits = Req.bpp;
169 *piDisplay = Req.display;
170 }
171 return rc;
172}
173
174
175/**
176 * Query the host as to whether it likes a specific video mode.
177 *
178 * @returns the result of the query
179 * @param cx the width of the mode being queried
180 * @param cy the height of the mode being queried
181 * @param cBits the bpp of the mode being queried
182 */
183VBGLR3DECL(bool) VbglR3HostLikesVideoMode(uint32_t cx, uint32_t cy, uint32_t cBits)
184{
185 bool fRc = true; /* If for some reason we can't contact the host then
186 * we like everything. */
187 int rc;
188 VMMDevVideoModeSupportedRequest req;
189
190 vmmdevInitRequest(&req.header, VMMDevReq_VideoModeSupported);
191 req.width = cx;
192 req.height = cy;
193 req.bpp = cBits;
194 req.fSupported = true;
195 rc = vbglR3GRPerform(&req.header);
196 if (RT_SUCCESS(rc) && RT_SUCCESS(req.header.rc))
197 fRc = req.fSupported;
198 return fRc;
199}
200
201/**
202 * Save video mode parameters to the registry.
203 *
204 * @returns iprt status value
205 * @param pszName the name to save the mode parameters under
206 * @param cx mode width
207 * @param cy mode height
208 * @param cBits bits per pixel for the mode
209 */
210VBGLR3DECL(int) VbglR3SaveVideoMode(const char *pszName, uint32_t cx, uint32_t cy, uint32_t cBits)
211{
212#if defined(VBOX_WITH_GUEST_PROPS)
213 using namespace guestProp;
214
215 char szModeName[MAX_NAME_LEN];
216 char szModeParms[MAX_VALUE_LEN];
217 uint32_t u32ClientId = 0;
218 RTStrPrintf(szModeName, sizeof(szModeName), VIDEO_PROP_PREFIX"%s", pszName);
219 RTStrPrintf(szModeParms, sizeof(szModeParms), "%dx%dx%d", cx, cy, cBits);
220 int rc = VbglR3GuestPropConnect(&u32ClientId);
221 if (RT_SUCCESS(rc))
222 rc = VbglR3GuestPropWriteValue(u32ClientId, szModeName, szModeParms);
223 if (u32ClientId != 0)
224 VbglR3GuestPropDisconnect(u32ClientId); /* Return value ignored, because what can we do anyway? */
225 return rc;
226#else /* !VBOX_WITH_GUEST_PROPS */
227 return VERR_NOT_IMPLEMENTED;
228#endif /* !VBOX_WITH_GUEST_PROPS */
229}
230
231
232/**
233 * Retrieve video mode parameters from the guest property store.
234 *
235 * @returns iprt status value
236 * @param pszName the name under which the mode parameters are saved
237 * @param pcx where to store the mode width
238 * @param pcy where to store the mode height
239 * @param pcBits where to store the bits per pixel for the mode
240 */
241VBGLR3DECL(int) VbglR3RetrieveVideoMode(const char *pszName, uint32_t *pcx, uint32_t *pcy, uint32_t *pcBits)
242{
243#if defined(VBOX_WITH_GUEST_PROPS)
244 using namespace guestProp;
245
246/*
247 * First we retreive the video mode which is saved as a string in the
248 * guest property store.
249 */
250 /* The buffer for VbglR3GuestPropReadValue. If this is too small then
251 * something is wrong with the data stored in the property. */
252 char szModeParms[1024];
253 uint32_t u32ClientId = 0;
254 uint32_t cx, cy, cBits;
255
256 int rc = VbglR3GuestPropConnect(&u32ClientId);
257 if (RT_SUCCESS(rc))
258 {
259 char szModeName[MAX_NAME_LEN];
260 RTStrPrintf(szModeName, sizeof(szModeName), VIDEO_PROP_PREFIX"%s", pszName);
261 /** @todo add a VbglR3GuestPropReadValueF/FV that does the RTStrPrintf for you. */
262 rc = VbglR3GuestPropReadValue(u32ClientId, szModeName, szModeParms,
263 sizeof(szModeParms), NULL);
264 }
265
266/*
267 * Now we convert the string returned to numeric values.
268 */
269 char *pszNext;
270 if (RT_SUCCESS(rc))
271 /* Extract the width from the string */
272 rc = RTStrToUInt32Ex(szModeParms, &pszNext, 10, &cx);
273 if ((rc != VWRN_TRAILING_CHARS) || (*pszNext != 'x'))
274 rc = VERR_PARSE_ERROR;
275 if (RT_SUCCESS(rc))
276 {
277 /* Extract the height from the string */
278 ++pszNext;
279 rc = RTStrToUInt32Ex(pszNext, &pszNext, 10, &cy);
280 }
281 if ((rc != VWRN_TRAILING_CHARS) || (*pszNext != 'x'))
282 rc = VERR_PARSE_ERROR;
283 if (RT_SUCCESS(rc))
284 {
285 /* Extract the bpp from the string */
286 ++pszNext;
287 rc = RTStrToUInt32Full(pszNext, 10, &cBits);
288 }
289 if (rc != VINF_SUCCESS)
290 rc = VERR_PARSE_ERROR;
291
292/*
293 * And clean up and return the values if we successfully obtained them.
294 */
295 if (u32ClientId != 0)
296 VbglR3GuestPropDisconnect(u32ClientId); /* Return value ignored, because what can we do anyway? */
297 if (RT_SUCCESS(rc))
298 {
299 *pcx = cx;
300 *pcy = cy;
301 *pcBits = cBits;
302 }
303 return rc;
304#else /* !VBOX_WITH_GUEST_PROPS */
305 return VERR_NOT_IMPLEMENTED;
306#endif /* !VBOX_WITH_GUEST_PROPS */
307}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette