VirtualBox

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

Last change on this file since 58188 was 58188, checked in by vboxsync, 9 years ago

VBoxGuestLib: u32ClientI[Dd] -> idClient. Some cleanups.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 18.3 KB
Line 
1/* $Id: VBoxGuestR3LibVideo.cpp 58188 2015-10-12 13:56:16Z vboxsync $ */
2/** @file
3 * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Video.
4 */
5
6/*
7 * Copyright (C) 2007-2015 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
232 AssertPtrReturn(pcx, VERR_INVALID_PARAMETER);
233 AssertPtrReturn(pcy, VERR_INVALID_PARAMETER);
234 AssertPtrReturn(pcBits, VERR_INVALID_PARAMETER);
235 AssertPtrNullReturn(pdx, VERR_INVALID_PARAMETER);
236 AssertPtrNullReturn(pdy, VERR_INVALID_PARAMETER);
237 AssertPtrReturn(piDisplay, VERR_INVALID_PARAMETER);
238 AssertPtrNullReturn(pfEnabled, VERR_INVALID_PARAMETER);
239 AssertPtrNullReturn(pfChangeOrigin, VERR_INVALID_PARAMETER);
240
241 RT_ZERO(Req);
242 rc = vmmdevInitRequest(&Req.header, VMMDevReq_GetDisplayChangeRequestEx);
243 AssertRCReturn(rc, rc);
244 if (fAck)
245 Req.eventAck = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST;
246 rc = vbglR3GRPerform(&Req.header);
247 if (RT_SUCCESS(rc))
248 rc = Req.header.rc;
249 if (RT_SUCCESS(rc))
250 {
251 *pcx = Req.xres;
252 *pcy = Req.yres;
253 *pcBits = Req.bpp;
254 *piDisplay = Req.display;
255 if (pdx)
256 *pdx = Req.cxOrigin;
257 if (pdy)
258 *pdy = Req.cyOrigin;
259 if (pfEnabled)
260 *pfEnabled = Req.fEnabled;
261 if (pfChangeOrigin)
262 *pfChangeOrigin = Req.fChangeOrigin;
263 return VINF_SUCCESS;
264 }
265
266 /* NEEDS TESTING: test below with current Additions on VBox 4.1 or older. */
267 /** @todo Can we find some standard grep-able string for "NEEDS TESTING"? */
268 if (rc == VERR_NOT_IMPLEMENTED) /* Fall back to the old API. */
269 {
270 if (pfEnabled)
271 *pfEnabled = true;
272 if (pfChangeOrigin)
273 *pfChangeOrigin = false;
274 return getDisplayChangeRequest2(pcx, pcy, pcBits, piDisplay, fAck);
275 }
276 return rc;
277}
278
279
280/**
281 * Query the host as to whether it likes a specific video mode.
282 *
283 * @returns the result of the query
284 * @param cx the width of the mode being queried
285 * @param cy the height of the mode being queried
286 * @param cBits the bpp of the mode being queried
287 */
288VBGLR3DECL(bool) VbglR3HostLikesVideoMode(uint32_t cx, uint32_t cy, uint32_t cBits)
289{
290 bool fRc = true; /* If for some reason we can't contact the host then
291 * we like everything. */
292 int rc;
293 VMMDevVideoModeSupportedRequest req;
294
295 vmmdevInitRequest(&req.header, VMMDevReq_VideoModeSupported);
296 req.width = cx;
297 req.height = cy;
298 req.bpp = cBits;
299 req.fSupported = true;
300 rc = vbglR3GRPerform(&req.header);
301 if (RT_SUCCESS(rc) && RT_SUCCESS(req.header.rc))
302 fRc = req.fSupported;
303 return fRc;
304}
305
306/**
307 * Get the highest screen number for which there is a saved video mode or "0"
308 * if there are no saved modes.
309 *
310 * @returns iprt status value
311 * @returns VERR_NOT_SUPPORTED if the guest property service is not available.
312 * @param pcScreen where to store the virtual screen number
313 */
314VBGLR3DECL(int) VbglR3VideoModeGetHighestSavedScreen(unsigned *pcScreen)
315{
316#if defined(VBOX_WITH_GUEST_PROPS)
317 using namespace guestProp;
318
319 int rc;
320 HGCMCLIENTID idClient = 0;
321 PVBGLR3GUESTPROPENUM pHandle = NULL;
322 const char *pszName = NULL;
323 unsigned cHighestScreen = 0;
324
325 /* Validate input. */
326 AssertPtrReturn(pcScreen, VERR_INVALID_POINTER);
327
328 /* Query the data. */
329 rc = VbglR3GuestPropConnect(&idClient);
330 if (RT_SUCCESS(rc))
331 {
332 const char *pszPattern = VIDEO_PROP_PREFIX"*";
333 rc = VbglR3GuestPropEnum(idClient, &pszPattern, 1, &pHandle, &pszName, NULL, NULL, NULL);
334 int rc2 = VbglR3GuestPropDisconnect(idClient);
335 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
336 rc = rc2;
337 }
338
339 /* Process the data. */
340 while (RT_SUCCESS(rc) && pszName != NULL)
341 {
342 uint32_t cScreen;
343
344 rc = RTStrToUInt32Full(pszName + sizeof(VIDEO_PROP_PREFIX) - 1, 10, &cScreen);
345 if (RT_SUCCESS(rc)) /* There may be similar properties with text. */
346 cHighestScreen = RT_MAX(cHighestScreen, cScreen);
347 rc = VbglR3GuestPropEnumNext(pHandle, &pszName, NULL, NULL, NULL);
348 }
349
350 VbglR3GuestPropEnumFree(pHandle);
351
352 /* Return result. */
353 if (RT_SUCCESS(rc))
354 *pcScreen = cHighestScreen;
355 return rc;
356#else /* !VBOX_WITH_GUEST_PROPS */
357 return VERR_NOT_SUPPORTED;
358#endif /* !VBOX_WITH_GUEST_PROPS */
359}
360
361/**
362 * Save video mode parameters to the guest property store.
363 *
364 * @returns iprt status value
365 * @param idScreen The virtual screen number.
366 * @param cx mode width
367 * @param cy mode height
368 * @param cBits bits per pixel for the mode
369 * @param x virtual screen X offset
370 * @param y virtual screen Y offset
371 * @param fEnabled is this virtual screen enabled?
372 */
373VBGLR3DECL(int) VbglR3SaveVideoMode(unsigned idScreen, unsigned cx, unsigned cy, unsigned cBits,
374 unsigned x, unsigned y, bool fEnabled)
375{
376#if defined(VBOX_WITH_GUEST_PROPS)
377 using namespace guestProp;
378
379 HGCMCLIENTID idClient = 0;
380 unsigned cx2, cy2, cBits2, x2, y2, cHighestScreen, cHighestScreen2;
381 bool fEnabled2;
382 int rc;
383 int rc2 = VERR_INTERNAL_ERROR;
384
385 rc = VbglR3VideoModeGetHighestSavedScreen(&cHighestScreen);
386 if (RT_SUCCESS(rc))
387 rc = VbglR3GuestPropConnect(&idClient);
388 if (RT_SUCCESS(rc))
389 {
390 char szModeName[MAX_NAME_LEN];
391 char szModeParms[MAX_VALUE_LEN];
392 RTStrPrintf(szModeName, sizeof(szModeName), VIDEO_PROP_PREFIX "%u", idScreen);
393 RTStrPrintf(szModeParms, sizeof(szModeParms), "%ux%ux%u,%ux%u,%u", cx, cy, cBits, x, y, (unsigned) fEnabled);
394
395 rc = VbglR3GuestPropWriteValue(idClient, szModeName, szModeParms);
396 /* Write out the mode using the legacy name too, in case the user
397 * re-installs older Additions. */
398 if (idScreen == 0)
399 {
400 RTStrPrintf(szModeParms, sizeof(szModeParms), "%ux%ux%u", cx, cy, cBits);
401 VbglR3GuestPropWriteValue(idClient, VIDEO_PROP_PREFIX "SavedMode", szModeParms);
402 }
403 }
404 if (idClient != 0)
405 rc2 = VbglR3GuestPropDisconnect(idClient);
406 if (rc == VINF_PERMISSION_DENIED)
407 return rc;
408 if (RT_SUCCESS(rc))
409 rc = rc2;
410 /* Sanity check 1. We do not try to make allowance for someone else
411 * changing saved settings at the same time as us. */
412 if (RT_SUCCESS(rc))
413 {
414 rc = VbglR3RetrieveVideoMode(idScreen, &cx2, &cy2, &cBits2, &x2, &y2, &fEnabled2);
415 if ( RT_SUCCESS(rc)
416 && (cx != cx2 || cy != cy2 || cBits != cBits2 || x != x2 || y != y2 || fEnabled != fEnabled2))
417 rc = VERR_WRITE_ERROR;
418 }
419 /* Sanity check 2. Same comment. */
420 if (RT_SUCCESS(rc))
421 rc = VbglR3VideoModeGetHighestSavedScreen(&cHighestScreen2);
422 if (RT_SUCCESS(rc))
423 if (cHighestScreen2 != RT_MAX(cHighestScreen, idScreen))
424 rc = VERR_INTERNAL_ERROR;
425 return rc;
426#else /* !VBOX_WITH_GUEST_PROPS */
427 return VERR_NOT_SUPPORTED;
428#endif /* !VBOX_WITH_GUEST_PROPS */
429}
430
431
432/**
433 * Retrieve video mode parameters from the guest property store.
434 *
435 * @returns iprt status value
436 * @param idScreen The virtual screen number.
437 * @param pcx where to store the mode width
438 * @param pcy where to store the mode height
439 * @param pcBits where to store the bits per pixel for the mode
440 * @param px where to store the virtual screen X offset
441 * @param py where to store the virtual screen Y offset
442 * @param pfEnabled where to store whether this virtual screen is enabled
443 */
444VBGLR3DECL(int) VbglR3RetrieveVideoMode(unsigned idScreen,
445 unsigned *pcx, unsigned *pcy,
446 unsigned *pcBits,
447 unsigned *px, unsigned *py,
448 bool *pfEnabled)
449{
450#if defined(VBOX_WITH_GUEST_PROPS)
451 using namespace guestProp;
452
453 /*
454 * First we retrieve the video mode which is saved as a string in the
455 * guest property store.
456 */
457 /* The buffer for VbglR3GuestPropReadValue. If this is too small then
458 * something is wrong with the data stored in the property. */
459 char szModeParms[1024];
460 HGCMCLIENTID idClient = 0;
461 int cMatches;
462 unsigned cx, cy, cBits;
463 unsigned x = 0;
464 unsigned y = 0;
465 unsigned fEnabled = 1;
466 int rc;
467 int rc2 = VERR_UNRESOLVED_ERROR;
468
469 rc = VbglR3GuestPropConnect(&idClient);
470 if (RT_SUCCESS(rc))
471 {
472 /** @todo add a VbglR3GuestPropReadValueF/FV that does the RTStrPrintf for you. */
473 char szModeName[MAX_NAME_LEN];
474 RTStrPrintf(szModeName, sizeof(szModeName), VIDEO_PROP_PREFIX "%u", idScreen);
475 rc = VbglR3GuestPropReadValue(idClient, szModeName, szModeParms, sizeof(szModeParms), NULL);
476 /* Try legacy single screen name. */
477 if (rc == VERR_NOT_FOUND && idScreen == 0)
478 rc = VbglR3GuestPropReadValue(idClient,
479 VIDEO_PROP_PREFIX"SavedMode",
480 szModeParms, sizeof(szModeParms),
481 NULL);
482 }
483 if (idClient != 0)
484 rc2 = VbglR3GuestPropDisconnect(idClient);
485 if (RT_SUCCESS(rc))
486 rc = rc2;
487
488 /*
489 * Now we convert the string returned to numeric values.
490 */
491 if (RT_SUCCESS(rc))
492 {
493 char c1, c2;
494 cMatches = sscanf(szModeParms, "%5ux%5ux%2u%c%5ux%5u,%1u%c", &cx, &cy, &cBits, &c1, &x, &y, &fEnabled, &c2);
495 if ((cMatches == 7 && c1 == ',') || cMatches == 3)
496 rc = VINF_SUCCESS;
497 else if (cMatches < 0)
498 rc = VERR_READ_ERROR;
499 else
500 rc = VERR_PARSE_ERROR;
501 }
502
503 /*
504 * And clean up and return the values if we successfully obtained them.
505 */
506 if (RT_SUCCESS(rc))
507 {
508 if (pcx)
509 *pcx = cx;
510 if (pcy)
511 *pcy = cy;
512 if (pcBits)
513 *pcBits = cBits;
514 if (px)
515 *px = x;
516 if (py)
517 *py = y;
518 if (pfEnabled)
519 *pfEnabled = RT_BOOL(fEnabled);
520 }
521 return rc;
522#else /* !VBOX_WITH_GUEST_PROPS */
523 return VERR_NOT_SUPPORTED;
524#endif /* !VBOX_WITH_GUEST_PROPS */
525}
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