VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuest/lib/VBoxGuestR3LibVideo.cpp@ 85271

Last change on this file since 85271 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

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