VirtualBox

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

Last change on this file since 10797 was 10797, checked in by vboxsync, 16 years ago

Guest properties: initial commit of new interface

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