VirtualBox

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

Last change on this file since 55772 was 54852, checked in by vboxsync, 10 years ago

Additions/common/VBoxGuestLib: when trying to save video mode hints to guest properties, correctly pass back VINF_PERMISSION_DENIED (read-only guest properties).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 18.3 KB
Line 
1/* $Id: VBoxGuestR3LibVideo.cpp 54852 2015-03-19 19:11:50Z vboxsync $ */
2/** @file
3 * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Video.
4 */
5
6/*
7 * Copyright (C) 2007-2012 Oracle Corporation
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
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include "VBGLR3Internal.h"
32
33#include <VBox/log.h>
34#include <VBox/HostServices/GuestPropertySvc.h> /* For Save and RetrieveVideoMode */
35#include <iprt/assert.h>
36#if !defined(VBOX_VBGLR3_XFREE86) && !defined(VBOX_VBGLR3_XORG)
37# include <iprt/mem.h>
38#endif
39#include <iprt/string.h>
40
41#include <stdio.h>
42
43#ifdef VBOX_VBGLR3_XFREE86
44/* Rather than try to resolve all the header file conflicts, I will just
45 prototype what we need here. */
46extern "C" void* xf86memcpy(void*,const void*,xf86size_t);
47# undef memcpy
48# define memcpy xf86memcpy
49extern "C" void* xf86memset(const void*,int,xf86size_t);
50# undef memset
51# define memset xf86memset
52#endif /* VBOX_VBGLR3_XFREE86 */
53
54#define VIDEO_PROP_PREFIX "/VirtualBox/GuestAdd/Vbgl/Video/"
55
56/**
57 * Enable or disable video acceleration.
58 *
59 * @returns VBox status code.
60 *
61 * @param fEnable Pass zero to disable, any other value to enable.
62 */
63VBGLR3DECL(int) VbglR3VideoAccelEnable(bool fEnable)
64{
65 VMMDevVideoAccelEnable Req;
66 vmmdevInitRequest(&Req.header, VMMDevReq_VideoAccelEnable);
67 Req.u32Enable = fEnable;
68 Req.cbRingBuffer = VBVA_RING_BUFFER_SIZE;
69 Req.fu32Status = 0;
70 return vbglR3GRPerform(&Req.header);
71}
72
73
74/**
75 * Flush the video buffer.
76 *
77 * @returns VBox status code.
78 */
79VBGLR3DECL(int) VbglR3VideoAccelFlush(void)
80{
81 VMMDevVideoAccelFlush Req;
82 vmmdevInitRequest(&Req.header, VMMDevReq_VideoAccelFlush);
83 return vbglR3GRPerform(&Req.header);
84}
85
86
87/**
88 * Send mouse pointer shape information to the host.
89 *
90 * @returns VBox status code.
91 *
92 * @param fFlags Mouse pointer flags.
93 * @param xHot X coordinate of hot spot.
94 * @param yHot Y coordinate of hot spot.
95 * @param cx Pointer width.
96 * @param cy Pointer height.
97 * @param pvImg Pointer to the image data (can be NULL).
98 * @param cbImg Size of the image data pointed to by pvImg.
99 */
100VBGLR3DECL(int) VbglR3SetPointerShape(uint32_t fFlags, uint32_t xHot, uint32_t yHot, uint32_t cx, uint32_t cy, const void *pvImg, size_t cbImg)
101{
102 VMMDevReqMousePointer *pReq;
103 size_t cbReq = vmmdevGetMousePointerReqSize(cx, cy);
104 AssertReturn( !pvImg
105 || cbReq == RT_OFFSETOF(VMMDevReqMousePointer, pointerData) + cbImg,
106 VERR_INVALID_PARAMETER);
107 int rc = vbglR3GRAlloc((VMMDevRequestHeader **)&pReq, cbReq,
108 VMMDevReq_SetPointerShape);
109 if (RT_SUCCESS(rc))
110 {
111 pReq->fFlags = fFlags;
112 pReq->xHot = xHot;
113 pReq->yHot = yHot;
114 pReq->width = cx;
115 pReq->height = cy;
116 if (pvImg)
117 memcpy(pReq->pointerData, pvImg, cbImg);
118
119 rc = vbglR3GRPerform(&pReq->header);
120 if (RT_SUCCESS(rc))
121 rc = pReq->header.rc;
122 vbglR3GRFree(&pReq->header);
123 }
124 return rc;
125}
126
127
128/**
129 * Send mouse pointer shape information to the host.
130 * This version of the function accepts a request for clients that
131 * already allocate and manipulate the request structure directly.
132 *
133 * @returns VBox status code.
134 *
135 * @param pReq Pointer to the VMMDevReqMousePointer structure.
136 */
137VBGLR3DECL(int) VbglR3SetPointerShapeReq(VMMDevReqMousePointer *pReq)
138{
139 int rc = vbglR3GRPerform(&pReq->header);
140 if (RT_SUCCESS(rc))
141 rc = pReq->header.rc;
142 return rc;
143}
144
145
146/**
147 * Query the last display change request sent from the host to the guest.
148 *
149 * @returns iprt status value
150 * @param pcx Where to store the horizontal pixel resolution
151 * @param pcy Where to store the vertical pixel resolution
152 * requested (a value of zero means do not change).
153 * @param pcBits Where to store the bits per pixel requested (a value
154 * of zero means do not change).
155 * @param iDisplay Where to store the display number the request was for
156 * - 0 for the primary display, 1 for the first
157 * secondary display, etc.
158 * @param fAck whether or not to acknowledge the newest request sent by
159 * the host. If this is set, the function will return the
160 * most recent host request, otherwise it will return the
161 * last request to be acknowledged.
162 *
163 */
164static int getDisplayChangeRequest2(uint32_t *pcx, uint32_t *pcy,
165 uint32_t *pcBits, uint32_t *piDisplay,
166 bool fAck)
167{
168 VMMDevDisplayChangeRequest2 Req;
169
170 AssertPtrReturn(pcx, VERR_INVALID_PARAMETER);
171 AssertPtrReturn(pcy, VERR_INVALID_PARAMETER);
172 AssertPtrReturn(pcBits, VERR_INVALID_PARAMETER);
173 AssertPtrReturn(piDisplay, VERR_INVALID_PARAMETER);
174 RT_ZERO(Req);
175 vmmdevInitRequest(&Req.header, VMMDevReq_GetDisplayChangeRequest2);
176 if (fAck)
177 Req.eventAck = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST;
178 int rc = vbglR3GRPerform(&Req.header);
179 if (RT_SUCCESS(rc))
180 rc = Req.header.rc;
181 if (RT_SUCCESS(rc))
182 {
183 *pcx = Req.xres;
184 *pcy = Req.yres;
185 *pcBits = Req.bpp;
186 *piDisplay = Req.display;
187 }
188 return rc;
189}
190
191
192/**
193 * Query the last display change request sent from the host to the guest.
194 *
195 * @returns iprt status value
196 * @param pcx Where to store the horizontal pixel resolution
197 * requested (a value of zero means do not change).
198 * @param pcy Where to store the vertical pixel resolution
199 * requested (a value of zero means do not change).
200 * @param pcBits Where to store the bits per pixel requested (a value
201 * of zero means do not change).
202 * @param piDisplay Where to store the display number the request was for
203 * - 0 for the primary display, 1 for the first
204 * secondary display, etc.
205 * @param fAck whether or not to acknowledge the newest request sent by
206 * the host. If this is set, the function will return the
207 * most recent host request, otherwise it will return the
208 * last request to be acknowledged.
209 *
210 * @param pdx New horizontal position of the secondary monitor.
211 * Optional.
212 * @param pdy New vertical position of the secondary monitor.
213 * Optional.
214 * param pfEnabled Secondary monitor is enabled or not.
215 * Optional.
216 * param pfChangeOrigin Whether the mode hint retrieved included information
217 * about origin/display offset inside the frame-buffer.
218 * Optional.
219 *
220 */
221VBGLR3DECL(int) VbglR3GetDisplayChangeRequest(uint32_t *pcx, uint32_t *pcy,
222 uint32_t *pcBits,
223 uint32_t *piDisplay,
224 uint32_t *pdx, uint32_t *pdy,
225 bool *pfEnabled,
226 bool *pfChangeOrigin,
227 bool fAck)
228{
229 VMMDevDisplayChangeRequestEx Req;
230 int rc = VINF_SUCCESS;
231 AssertPtrReturn(pcx, VERR_INVALID_PARAMETER);
232 AssertPtrReturn(pcy, VERR_INVALID_PARAMETER);
233 AssertPtrReturn(pcBits, VERR_INVALID_PARAMETER);
234 AssertPtrNullReturn(pdx, VERR_INVALID_PARAMETER);
235 AssertPtrNullReturn(pdy, VERR_INVALID_PARAMETER);
236 AssertPtrReturn(piDisplay, VERR_INVALID_PARAMETER);
237 AssertPtrNullReturn(pfEnabled, VERR_INVALID_PARAMETER);
238 AssertPtrNullReturn(pfChangeOrigin, VERR_INVALID_PARAMETER);
239 RT_ZERO(Req);
240 rc = vmmdevInitRequest(&Req.header, VMMDevReq_GetDisplayChangeRequestEx);
241 AssertRCReturn(rc, rc);
242 if (fAck)
243 Req.eventAck = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST;
244 rc = vbglR3GRPerform(&Req.header);
245 if (RT_SUCCESS(rc))
246 rc = Req.header.rc;
247 if (RT_SUCCESS(rc))
248 {
249 *pcx = Req.xres;
250 *pcy = Req.yres;
251 *pcBits = Req.bpp;
252 *piDisplay = Req.display;
253 if (pdx)
254 *pdx = Req.cxOrigin;
255 if (pdy)
256 *pdy = Req.cyOrigin;
257 if (pfEnabled)
258 *pfEnabled = Req.fEnabled;
259 if (pfChangeOrigin)
260 *pfChangeOrigin = Req.fChangeOrigin;
261 }
262 /* NEEDS TESTING: test below with current Additions on VBox 4.1 or older. */
263 /** @todo Can we find some standard grep-able string for "NEEDS TESTING"? */
264 if (rc == VERR_NOT_IMPLEMENTED) /* Fall back to the old API. */
265 {
266 if (pfEnabled)
267 *pfEnabled = true;
268 if (pfChangeOrigin)
269 *pfChangeOrigin = false;
270 return getDisplayChangeRequest2(pcx, pcy, pcBits, piDisplay, fAck);
271 }
272 return rc;
273}
274
275
276/**
277 * Query the host as to whether it likes a specific video mode.
278 *
279 * @returns the result of the query
280 * @param cx the width of the mode being queried
281 * @param cy the height of the mode being queried
282 * @param cBits the bpp of the mode being queried
283 */
284VBGLR3DECL(bool) VbglR3HostLikesVideoMode(uint32_t cx, uint32_t cy, uint32_t cBits)
285{
286 bool fRc = true; /* If for some reason we can't contact the host then
287 * we like everything. */
288 int rc;
289 VMMDevVideoModeSupportedRequest req;
290
291 vmmdevInitRequest(&req.header, VMMDevReq_VideoModeSupported);
292 req.width = cx;
293 req.height = cy;
294 req.bpp = cBits;
295 req.fSupported = true;
296 rc = vbglR3GRPerform(&req.header);
297 if (RT_SUCCESS(rc) && RT_SUCCESS(req.header.rc))
298 fRc = req.fSupported;
299 return fRc;
300}
301
302/**
303 * Get the highest screen number for which there is a saved video mode or "0"
304 * if there are no saved modes.
305 *
306 * @returns iprt status value
307 * @returns VERR_NOT_SUPPORTED if the guest property service is not available.
308 * @param pcScreen where to store the virtual screen number
309 */
310VBGLR3DECL(int) VbglR3VideoModeGetHighestSavedScreen(unsigned *pcScreen)
311{
312#if defined(VBOX_WITH_GUEST_PROPS)
313 using namespace guestProp;
314
315 int rc, rc2 = VERR_UNRESOLVED_ERROR;
316 uint32_t u32ClientId = 0;
317 const char *pszPattern = VIDEO_PROP_PREFIX"*";
318 PVBGLR3GUESTPROPENUM pHandle = NULL;
319 const char *pszName;
320 unsigned cHighestScreen = 0;
321
322 AssertPtrReturn(pcScreen, VERR_INVALID_POINTER);
323 rc = VbglR3GuestPropConnect(&u32ClientId);
324 if (RT_SUCCESS(rc))
325 rc = VbglR3GuestPropEnum(u32ClientId, &pszPattern, 1, &pHandle,
326 &pszName, NULL, NULL, NULL);
327 if (u32ClientId != 0)
328 rc2 = VbglR3GuestPropDisconnect(u32ClientId);
329 if (RT_SUCCESS(rc))
330 rc = rc2;
331 while (pszName != NULL && RT_SUCCESS(rc))
332 {
333 uint32_t cScreen;
334
335 rc = RTStrToUInt32Full(pszName + sizeof(VIDEO_PROP_PREFIX) - 1, 10,
336 &cScreen);
337 if (RT_SUCCESS(rc)) /* There may be similar properties with text. */
338 cHighestScreen = RT_MAX(cHighestScreen, cScreen);
339 rc = VbglR3GuestPropEnumNext(pHandle, &pszName, NULL, NULL, NULL);
340 }
341 VbglR3GuestPropEnumFree(pHandle);
342 if (RT_SUCCESS(rc))
343 *pcScreen = cHighestScreen;
344 return rc;
345#else /* !VBOX_WITH_GUEST_PROPS */
346 return VERR_NOT_SUPPORTED;
347#endif /* !VBOX_WITH_GUEST_PROPS */
348}
349
350/**
351 * Save video mode parameters to the guest property store.
352 *
353 * @returns iprt status value
354 * @param cScreen virtual screen number
355 * @param cx mode width
356 * @param cy mode height
357 * @param cBits bits per pixel for the mode
358 * @param x virtual screen X offset
359 * @param y virtual screen Y offset
360 * @param fEnabled is this virtual screen enabled?
361 */
362VBGLR3DECL(int) VbglR3SaveVideoMode(unsigned cScreen, unsigned cx, unsigned cy,
363 unsigned cBits, unsigned x, unsigned y,
364 bool fEnabled)
365{
366#if defined(VBOX_WITH_GUEST_PROPS)
367 using namespace guestProp;
368
369 char szModeName[MAX_NAME_LEN];
370 char szModeParms[MAX_VALUE_LEN];
371 uint32_t u32ClientId = 0;
372 unsigned cx2, cy2, cBits2, x2, y2, cHighestScreen, cHighestScreen2;
373 bool fEnabled2;
374 int rc, rc2 = VERR_UNRESOLVED_ERROR;
375
376 rc = VbglR3VideoModeGetHighestSavedScreen(&cHighestScreen);
377 RTStrPrintf(szModeName, sizeof(szModeName), VIDEO_PROP_PREFIX"%u", cScreen);
378 RTStrPrintf(szModeParms, sizeof(szModeParms), "%ux%ux%u,%ux%u,%u", cx, cy,
379 cBits, x, y, (unsigned) fEnabled);
380 if (RT_SUCCESS(rc))
381 rc = VbglR3GuestPropConnect(&u32ClientId);
382 if (RT_SUCCESS(rc))
383 {
384 rc = VbglR3GuestPropWriteValue(u32ClientId, szModeName, szModeParms);
385 /* Write out the mode using the legacy name too, in case the user
386 * re-installs older Additions. */
387 if (cScreen == 0)
388 {
389 RTStrPrintf(szModeParms, sizeof(szModeParms), "%ux%ux%u", cx, cy,
390 cBits);
391 VbglR3GuestPropWriteValue(u32ClientId, VIDEO_PROP_PREFIX"SavedMode",
392 szModeParms);
393 }
394 }
395 if (u32ClientId != 0)
396 rc2 = VbglR3GuestPropDisconnect(u32ClientId);
397 if (rc == VINF_PERMISSION_DENIED)
398 return rc;
399 if (RT_SUCCESS(rc))
400 rc = rc2;
401 /* Sanity check 1. We do not try to make allowance for someone else
402 * changing saved settings at the same time as us. */
403 if (RT_SUCCESS(rc))
404 {
405 rc = VbglR3RetrieveVideoMode(cScreen, &cx2, &cy2, &cBits2, &x2, &y2,
406 &fEnabled2);
407 if ( RT_SUCCESS(rc)
408 && ( cx != cx2 || cy != cy2 || cBits != cBits2
409 || x != x2 || y != y2 || fEnabled != fEnabled2))
410 rc = VERR_WRITE_ERROR;
411 }
412 /* Sanity check 2. Same comment. */
413 if (RT_SUCCESS(rc))
414 rc = VbglR3VideoModeGetHighestSavedScreen(&cHighestScreen2);
415 if (RT_SUCCESS(rc))
416 if (cHighestScreen2 != RT_MAX(cHighestScreen, cScreen))
417 rc = VERR_INTERNAL_ERROR;
418 return rc;
419#else /* !VBOX_WITH_GUEST_PROPS */
420 return VERR_NOT_SUPPORTED;
421#endif /* !VBOX_WITH_GUEST_PROPS */
422}
423
424
425/**
426 * Retrieve video mode parameters from the guest property store.
427 *
428 * @returns iprt status value
429 * @param cScreen the virtual screen number
430 * @param pcx where to store the mode width
431 * @param pcy where to store the mode height
432 * @param pcBits where to store the bits per pixel for the mode
433 * @param px where to store the virtual screen X offset
434 * @param py where to store the virtual screen Y offset
435 * @param pfEnabled where to store whether this virtual screen is enabled
436 */
437VBGLR3DECL(int) VbglR3RetrieveVideoMode(unsigned cScreen,
438 unsigned *pcx, unsigned *pcy,
439 unsigned *pcBits,
440 unsigned *px, unsigned *py,
441 bool *pfEnabled)
442{
443#if defined(VBOX_WITH_GUEST_PROPS)
444 using namespace guestProp;
445
446/*
447 * First we retrieve the video mode which is saved as a string in the
448 * guest property store.
449 */
450 /* The buffer for VbglR3GuestPropReadValue. If this is too small then
451 * something is wrong with the data stored in the property. */
452 char szModeName[MAX_NAME_LEN];
453 char szModeParms[1024];
454 uint32_t u32ClientId = 0;
455 int cMatches;
456 unsigned cx, cy, cBits;
457 unsigned x = 0;
458 unsigned y = 0;
459 unsigned fEnabled = 1;
460 int rc;
461 int rc2 = VERR_UNRESOLVED_ERROR;
462
463 /** @todo add a VbglR3GuestPropReadValueF/FV that does the RTStrPrintf for you. */
464 RTStrPrintf(szModeName, sizeof(szModeName), VIDEO_PROP_PREFIX"%u", cScreen);
465 rc = VbglR3GuestPropConnect(&u32ClientId);
466 if (RT_SUCCESS(rc))
467 {
468 rc = VbglR3GuestPropReadValue(u32ClientId, szModeName, szModeParms,
469 sizeof(szModeParms), NULL);
470 /* Try legacy single screen name. */
471 if (rc == VERR_NOT_FOUND && cScreen == 0)
472 rc = VbglR3GuestPropReadValue(u32ClientId,
473 VIDEO_PROP_PREFIX"SavedMode",
474 szModeParms, sizeof(szModeParms),
475 NULL);
476 }
477 if (u32ClientId != 0)
478 rc2 = VbglR3GuestPropDisconnect(u32ClientId);
479 if (RT_SUCCESS(rc))
480 rc = rc2;
481
482/*
483 * Now we convert the string returned to numeric values.
484 */
485 if (RT_SUCCESS(rc))
486 {
487 char c1, c2;
488 cMatches = sscanf(szModeParms, "%ux%ux%u%c%ux%u,%u%c", &cx, &cy, &cBits,
489 &c1, &x, &y, &fEnabled, &c2);
490 if ((cMatches == 7 && c1 == ',') || cMatches == 3)
491 rc = VINF_SUCCESS;
492 else if (cMatches < 0)
493 rc = VERR_READ_ERROR;
494 else
495 rc = VERR_PARSE_ERROR;
496 }
497/*
498 * And clean up and return the values if we successfully obtained them.
499 */
500 if (RT_SUCCESS(rc))
501 {
502 if (pcx)
503 *pcx = cx;
504 if (pcy)
505 *pcy = cy;
506 if (pcBits)
507 *pcBits = cBits;
508 if (px)
509 *px = x;
510 if (py)
511 *py = y;
512 if (pfEnabled)
513 *pfEnabled = RT_BOOL(fEnabled);
514 }
515 return rc;
516#else /* !VBOX_WITH_GUEST_PROPS */
517 return VERR_NOT_SUPPORTED;
518#endif /* !VBOX_WITH_GUEST_PROPS */
519}
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