VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxControl/VBoxControl.cpp@ 13298

Last change on this file since 13298 was 13292, checked in by vboxsync, 17 years ago

Additions/VBoxControl: cleanup an error handler

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 38.8 KB
Line 
1/** $Id: VBoxControl.cpp 13292 2008-10-15 16:07:24Z vboxsync $ */
2/** @file
3 * VBoxControl - Guest Additions Command Line Management Interface
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/*******************************************************************************
25* Header Files *
26*******************************************************************************/
27#include <iprt/mem.h>
28#include <iprt/string.h>
29#include <iprt/stream.h>
30#include <iprt/path.h>
31#include <iprt/initterm.h>
32#include <iprt/autores.h>
33#include <VBox/log.h>
34#include <VBox/VBoxGuest.h>
35#include <VBox/version.h>
36#ifdef RT_OS_WINDOWS
37# include <windows.h>
38# include <malloc.h> /* for alloca */
39#endif
40#ifdef VBOX_WITH_GUEST_PROPS
41# include <VBox/HostServices/GuestPropertySvc.h>
42#endif
43#include "VBoxControl.h"
44
45/*******************************************************************************
46* Global Variables *
47*******************************************************************************/
48/** The program name (derived from argv[0]). */
49char const *g_pszProgName = "";
50/** The current verbosity level. */
51int g_cVerbosity = 0;
52
53
54/**
55 * Displays the program usage message.
56 *
57 * @param u64Which
58 *
59 * @{
60 */
61
62/** Helper function */
63static void doUsage(char const *line, char const *name = "", char const *command = "")
64{
65 RTPrintf("%s %-*s%s", name, 32 - strlen(name), command, line);
66}
67
68/** Enumerate the different parts of the usage we might want to print out */
69enum g_eUsage
70{
71#ifdef RT_OS_WINDOWS
72 GET_VIDEO_ACCEL,
73 SET_VIDEO_ACCEL,
74 LIST_CUST_MODES,
75 ADD_CUST_MODE,
76 REMOVE_CUST_MODE,
77 SET_VIDEO_MODE,
78#endif
79#ifdef VBOX_WITH_GUEST_PROPS
80 GUEST_PROP,
81#endif
82 USAGE_ALL = UINT32_MAX
83};
84
85static void usage(g_eUsage eWhich = USAGE_ALL)
86{
87 RTPrintf("Usage:\n\n");
88 RTPrintf("%s [-v|-version] print version number and exit\n", g_pszProgName);
89 RTPrintf("%s -nologo ... suppress the logo\n\n", g_pszProgName);
90
91#ifdef RT_OS_WINDOWS
92 if ((GET_VIDEO_ACCEL == eWhich) || (USAGE_ALL == eWhich))
93 doUsage("\n", g_pszProgName, "getvideoacceleration");
94 if ((SET_VIDEO_ACCEL == eWhich) || (USAGE_ALL == eWhich))
95 doUsage("<on|off>\n", g_pszProgName, "setvideoacceleration");
96 if ((LIST_CUST_MODES == eWhich) || (USAGE_ALL == eWhich))
97 doUsage("\n", g_pszProgName, "listcustommodes");
98 if ((ADD_CUST_MODE == eWhich) || (USAGE_ALL == eWhich))
99 doUsage("<width> <height> <bpp>\n", g_pszProgName, "addcustommode");
100 if ((REMOVE_CUST_MODE == eWhich) || (USAGE_ALL == eWhich))
101 doUsage("<width> <height> <bpp>\n", g_pszProgName, "removecustommode");
102 if ((SET_VIDEO_MODE == eWhich) || (USAGE_ALL == eWhich))
103 doUsage("<width> <height> <bpp> <screen>\n", g_pszProgName, "setvideomode");
104#endif
105#ifdef VBOX_WITH_GUEST_PROPS
106 if ((GUEST_PROP == eWhich) || (USAGE_ALL == eWhich))
107 {
108 doUsage("get <property> [-verbose]\n", g_pszProgName, "guestproperty");
109 doUsage("set <property> [<value> [-flags <flags>]]\n", g_pszProgName, "guestproperty");
110 doUsage("enumerate [-patterns <patterns>]\n", g_pszProgName, "guestproperty");
111 }
112#endif
113}
114/** @} */
115
116/**
117 * Displays an error message.
118 *
119 * @param pszFormat The message text.
120 * @param ... Format arguments.
121 */
122static void VBoxControlError(const char *pszFormat, ...)
123{
124 // RTStrmPrintf(g_pStdErr, "%s: error: ", g_pszProgName);
125
126 va_list va;
127 va_start(va, pszFormat);
128 RTStrmPrintfV(g_pStdErr, pszFormat, va);
129 va_end(va);
130}
131
132#ifdef RT_OS_WINDOWS
133
134LONG (WINAPI * gpfnChangeDisplaySettingsEx)(LPCTSTR lpszDeviceName, LPDEVMODE lpDevMode, HWND hwnd, DWORD dwflags, LPVOID lParam);
135
136static unsigned nextAdjacentRectXP (RECTL *paRects, unsigned nRects, unsigned iRect)
137{
138 unsigned i;
139 for (i = 0; i < nRects; i++)
140 {
141 if (paRects[iRect].right == paRects[i].left)
142 {
143 return i;
144 }
145 }
146 return ~0;
147}
148
149static unsigned nextAdjacentRectXN (RECTL *paRects, unsigned nRects, unsigned iRect)
150{
151 unsigned i;
152 for (i = 0; i < nRects; i++)
153 {
154 if (paRects[iRect].left == paRects[i].right)
155 {
156 return i;
157 }
158 }
159 return ~0;
160}
161
162static unsigned nextAdjacentRectYP (RECTL *paRects, unsigned nRects, unsigned iRect)
163{
164 unsigned i;
165 for (i = 0; i < nRects; i++)
166 {
167 if (paRects[iRect].bottom == paRects[i].top)
168 {
169 return i;
170 }
171 }
172 return ~0;
173}
174
175unsigned nextAdjacentRectYN (RECTL *paRects, unsigned nRects, unsigned iRect)
176{
177 unsigned i;
178 for (i = 0; i < nRects; i++)
179 {
180 if (paRects[iRect].top == paRects[i].bottom)
181 {
182 return i;
183 }
184 }
185 return ~0;
186}
187
188void resizeRect(RECTL *paRects, unsigned nRects, unsigned iPrimary, unsigned iResized, int NewWidth, int NewHeight)
189{
190 RECTL *paNewRects = (RECTL *)alloca (sizeof (RECTL) * nRects);
191 memcpy (paNewRects, paRects, sizeof (RECTL) * nRects);
192 paNewRects[iResized].right += NewWidth - (paNewRects[iResized].right - paNewRects[iResized].left);
193 paNewRects[iResized].bottom += NewHeight - (paNewRects[iResized].bottom - paNewRects[iResized].top);
194
195 /* Verify all pairs of originally adjacent rectangles for all 4 directions.
196 * If the pair has a "good" delta (that is the first rectangle intersects the second)
197 * at a direction and the second rectangle is not primary one (which can not be moved),
198 * move the second rectangle to make it adjacent to the first one.
199 */
200
201 /* X positive. */
202 unsigned iRect;
203 for (iRect = 0; iRect < nRects; iRect++)
204 {
205 /* Find the next adjacent original rect in x positive direction. */
206 unsigned iNextRect = nextAdjacentRectXP (paRects, nRects, iRect);
207 Log(("next %d -> %d\n", iRect, iNextRect));
208
209 if (iNextRect == ~0 || iNextRect == iPrimary)
210 {
211 continue;
212 }
213
214 /* Check whether there is an X intesection between these adjacent rects in the new rectangles
215 * and fix the intersection if delta is "good".
216 */
217 int delta = paNewRects[iRect].right - paNewRects[iNextRect].left;
218
219 if (delta > 0)
220 {
221 Log(("XP intersection right %d left %d, diff %d\n",
222 paNewRects[iRect].right, paNewRects[iNextRect].left,
223 delta));
224
225 paNewRects[iNextRect].left += delta;
226 paNewRects[iNextRect].right += delta;
227 }
228 }
229
230 /* X negative. */
231 for (iRect = 0; iRect < nRects; iRect++)
232 {
233 /* Find the next adjacent original rect in x negative direction. */
234 unsigned iNextRect = nextAdjacentRectXN (paRects, nRects, iRect);
235 Log(("next %d -> %d\n", iRect, iNextRect));
236
237 if (iNextRect == ~0 || iNextRect == iPrimary)
238 {
239 continue;
240 }
241
242 /* Check whether there is an X intesection between these adjacent rects in the new rectangles
243 * and fix the intersection if delta is "good".
244 */
245 int delta = paNewRects[iRect].left - paNewRects[iNextRect].right;
246
247 if (delta < 0)
248 {
249 Log(("XN intersection left %d right %d, diff %d\n",
250 paNewRects[iRect].left, paNewRects[iNextRect].right,
251 delta));
252
253 paNewRects[iNextRect].left += delta;
254 paNewRects[iNextRect].right += delta;
255 }
256 }
257
258 /* Y positive (in the computer sence, top->down). */
259 for (iRect = 0; iRect < nRects; iRect++)
260 {
261 /* Find the next adjacent original rect in y positive direction. */
262 unsigned iNextRect = nextAdjacentRectYP (paRects, nRects, iRect);
263 Log(("next %d -> %d\n", iRect, iNextRect));
264
265 if (iNextRect == ~0 || iNextRect == iPrimary)
266 {
267 continue;
268 }
269
270 /* Check whether there is an Y intesection between these adjacent rects in the new rectangles
271 * and fix the intersection if delta is "good".
272 */
273 int delta = paNewRects[iRect].bottom - paNewRects[iNextRect].top;
274
275 if (delta > 0)
276 {
277 Log(("YP intersection bottom %d top %d, diff %d\n",
278 paNewRects[iRect].bottom, paNewRects[iNextRect].top,
279 delta));
280
281 paNewRects[iNextRect].top += delta;
282 paNewRects[iNextRect].bottom += delta;
283 }
284 }
285
286 /* Y negative (in the computer sence, down->top). */
287 for (iRect = 0; iRect < nRects; iRect++)
288 {
289 /* Find the next adjacent original rect in x negative direction. */
290 unsigned iNextRect = nextAdjacentRectYN (paRects, nRects, iRect);
291 Log(("next %d -> %d\n", iRect, iNextRect));
292
293 if (iNextRect == ~0 || iNextRect == iPrimary)
294 {
295 continue;
296 }
297
298 /* Check whether there is an Y intesection between these adjacent rects in the new rectangles
299 * and fix the intersection if delta is "good".
300 */
301 int delta = paNewRects[iRect].top - paNewRects[iNextRect].bottom;
302
303 if (delta < 0)
304 {
305 Log(("YN intersection top %d bottom %d, diff %d\n",
306 paNewRects[iRect].top, paNewRects[iNextRect].bottom,
307 delta));
308
309 paNewRects[iNextRect].top += delta;
310 paNewRects[iNextRect].bottom += delta;
311 }
312 }
313
314 memcpy (paRects, paNewRects, sizeof (RECTL) * nRects);
315 return;
316}
317
318/* Returns TRUE to try again. */
319static BOOL ResizeDisplayDevice(ULONG Id, DWORD Width, DWORD Height, DWORD BitsPerPixel)
320{
321 BOOL fModeReset = (Width == 0 && Height == 0 && BitsPerPixel == 0);
322
323 DISPLAY_DEVICE DisplayDevice;
324
325 ZeroMemory(&DisplayDevice, sizeof(DisplayDevice));
326 DisplayDevice.cb = sizeof(DisplayDevice);
327
328 /* Find out how many display devices the system has */
329 DWORD NumDevices = 0;
330 DWORD i = 0;
331 while (EnumDisplayDevices (NULL, i, &DisplayDevice, 0))
332 {
333 Log(("[%d] %s\n", i, DisplayDevice.DeviceName));
334
335 if (DisplayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
336 {
337 Log(("Found primary device. err %d\n", GetLastError ()));
338 NumDevices++;
339 }
340 else if (!(DisplayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER))
341 {
342
343 Log(("Found secondary device. err %d\n", GetLastError ()));
344 NumDevices++;
345 }
346
347 ZeroMemory(&DisplayDevice, sizeof(DisplayDevice));
348 DisplayDevice.cb = sizeof(DisplayDevice);
349 i++;
350 }
351
352 Log(("Found total %d devices. err %d\n", NumDevices, GetLastError ()));
353
354 if (NumDevices == 0 || Id >= NumDevices)
355 {
356 Log(("Requested identifier %d is invalid. err %d\n", Id, GetLastError ()));
357 return FALSE;
358 }
359
360 DISPLAY_DEVICE *paDisplayDevices = (DISPLAY_DEVICE *)alloca (sizeof (DISPLAY_DEVICE) * NumDevices);
361 DEVMODE *paDeviceModes = (DEVMODE *)alloca (sizeof (DEVMODE) * NumDevices);
362 RECTL *paRects = (RECTL *)alloca (sizeof (RECTL) * NumDevices);
363
364 /* Fetch information about current devices and modes. */
365 DWORD DevNum = 0;
366 DWORD DevPrimaryNum = 0;
367
368 ZeroMemory(&DisplayDevice, sizeof(DISPLAY_DEVICE));
369 DisplayDevice.cb = sizeof(DISPLAY_DEVICE);
370
371 i = 0;
372 while (EnumDisplayDevices (NULL, i, &DisplayDevice, 0))
373 {
374 Log(("[%d(%d)] %s\n", i, DevNum, DisplayDevice.DeviceName));
375
376 BOOL bFetchDevice = FALSE;
377
378 if (DisplayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
379 {
380 Log(("Found primary device. err %d\n", GetLastError ()));
381 DevPrimaryNum = DevNum;
382 bFetchDevice = TRUE;
383 }
384 else if (!(DisplayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER))
385 {
386
387 Log(("Found secondary device. err %d\n", GetLastError ()));
388 bFetchDevice = TRUE;
389 }
390
391 if (bFetchDevice)
392 {
393 if (DevNum >= NumDevices)
394 {
395 Log(("%d >= %d\n", NumDevices, DevNum));
396 return FALSE;
397 }
398
399 paDisplayDevices[DevNum] = DisplayDevice;
400
401 ZeroMemory(&paDeviceModes[DevNum], sizeof(DEVMODE));
402 paDeviceModes[DevNum].dmSize = sizeof(DEVMODE);
403 if (!EnumDisplaySettings((LPSTR)DisplayDevice.DeviceName,
404 ENUM_REGISTRY_SETTINGS, &paDeviceModes[DevNum]))
405 {
406 Log(("EnumDisplaySettings err %d\n", GetLastError ()));
407 return FALSE;
408 }
409
410 Log(("%dx%d at %d,%d\n",
411 paDeviceModes[DevNum].dmPelsWidth,
412 paDeviceModes[DevNum].dmPelsHeight,
413 paDeviceModes[DevNum].dmPosition.x,
414 paDeviceModes[DevNum].dmPosition.y));
415
416 paRects[DevNum].left = paDeviceModes[DevNum].dmPosition.x;
417 paRects[DevNum].top = paDeviceModes[DevNum].dmPosition.y;
418 paRects[DevNum].right = paDeviceModes[DevNum].dmPosition.x + paDeviceModes[DevNum].dmPelsWidth;
419 paRects[DevNum].bottom = paDeviceModes[DevNum].dmPosition.y + paDeviceModes[DevNum].dmPelsHeight;
420 DevNum++;
421 }
422
423 ZeroMemory(&DisplayDevice, sizeof(DISPLAY_DEVICE));
424 DisplayDevice.cb = sizeof(DISPLAY_DEVICE);
425 i++;
426 }
427
428 if (Width == 0)
429 {
430 Width = paRects[Id].right - paRects[Id].left;
431 }
432
433 if (Height == 0)
434 {
435 Height = paRects[Id].bottom - paRects[Id].top;
436 }
437
438 /* Check whether a mode reset or a change is requested. */
439 if ( !fModeReset
440 && paRects[Id].right - paRects[Id].left == Width
441 && paRects[Id].bottom - paRects[Id].top == Height
442 && paDeviceModes[Id].dmBitsPerPel == BitsPerPixel)
443 {
444 Log(("VBoxDisplayThread : already at desired resolution.\n"));
445 return FALSE;
446 }
447
448 resizeRect(paRects, NumDevices, DevPrimaryNum, Id, Width, Height);
449#ifdef Log
450 for (i = 0; i < NumDevices; i++)
451 {
452 Log(("[%d]: %d,%d %dx%d\n",
453 i, paRects[i].left, paRects[i].top,
454 paRects[i].right - paRects[i].left,
455 paRects[i].bottom - paRects[i].top));
456 }
457#endif /* Log */
458
459 /* Without this, Windows will not ask the miniport for its
460 * mode table but uses an internal cache instead.
461 */
462 DEVMODE tempDevMode;
463 ZeroMemory (&tempDevMode, sizeof (tempDevMode));
464 tempDevMode.dmSize = sizeof(DEVMODE);
465 EnumDisplaySettings(NULL, 0xffffff, &tempDevMode);
466
467 /* Assign the new rectangles to displays. */
468 for (i = 0; i < NumDevices; i++)
469 {
470 paDeviceModes[i].dmPosition.x = paRects[i].left;
471 paDeviceModes[i].dmPosition.y = paRects[i].top;
472 paDeviceModes[i].dmPelsWidth = paRects[i].right - paRects[i].left;
473 paDeviceModes[i].dmPelsHeight = paRects[i].bottom - paRects[i].top;
474
475 paDeviceModes[i].dmFields = DM_POSITION | DM_PELSHEIGHT | DM_PELSWIDTH;
476
477 if ( i == Id
478 && BitsPerPixel != 0)
479 {
480 paDeviceModes[i].dmFields |= DM_BITSPERPEL;
481 paDeviceModes[i].dmBitsPerPel = BitsPerPixel;
482 }
483 Log(("calling pfnChangeDisplaySettingsEx %x\n", gpfnChangeDisplaySettingsEx));
484 gpfnChangeDisplaySettingsEx((LPSTR)paDisplayDevices[i].DeviceName,
485 &paDeviceModes[i], NULL, CDS_NORESET | CDS_UPDATEREGISTRY, NULL);
486 Log(("ChangeDisplaySettings position err %d\n", GetLastError ()));
487 }
488
489 /* A second call to ChangeDisplaySettings updates the monitor. */
490 LONG status = ChangeDisplaySettings(NULL, 0);
491 Log(("ChangeDisplaySettings update status %d\n", status));
492 if (status == DISP_CHANGE_SUCCESSFUL || status == DISP_CHANGE_BADMODE)
493 {
494 /* Successfully set new video mode or our driver can not set the requested mode. Stop trying. */
495 return FALSE;
496 }
497
498 /* Retry the request. */
499 return TRUE;
500}
501
502int handleSetVideoMode(int argc, char *argv[])
503{
504 if (argc != 3 && argc != 4)
505 {
506 usage(SET_VIDEO_MODE);
507 return 1;
508 }
509
510 DWORD xres = atoi(argv[0]);
511 DWORD yres = atoi(argv[1]);
512 DWORD bpp = atoi(argv[2]);
513 DWORD scr = 0;
514
515 if (argc == 4)
516 {
517 scr = atoi(argv[3]);
518 }
519
520 HMODULE hUser = GetModuleHandle("USER32");
521
522 if (hUser)
523 {
524 *(uintptr_t *)&gpfnChangeDisplaySettingsEx = (uintptr_t)GetProcAddress(hUser, "ChangeDisplaySettingsExA");
525 Log(("VBoxService: pChangeDisplaySettingsEx = %p\n", gpfnChangeDisplaySettingsEx));
526
527 if (gpfnChangeDisplaySettingsEx)
528 {
529 /* The screen index is 0 based in the ResizeDisplayDevice call. */
530 scr = scr > 0? scr - 1: 0;
531
532 /* Horizontal resolution must be a multiple of 8, round down. */
533 xres &= ~0x7;
534
535 RTPrintf("Setting resolution of display %d to %dx%dx%d ...", scr, xres, yres, bpp);
536 ResizeDisplayDevice(scr, xres, yres, bpp);
537 RTPrintf("done.\n");
538 }
539 else VBoxControlError("Error retrieving API for display change!");
540 }
541 else VBoxControlError("Error retrieving handle to user32.dll!");
542
543 return 0;
544}
545
546HKEY getVideoKey(bool writable)
547{
548 HKEY hkeyDeviceMap = 0;
549 HKEY hkeyVideo = 0;
550 LONG status;
551
552 status = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\VIDEO", 0, KEY_READ, &hkeyDeviceMap);
553 if ((status != ERROR_SUCCESS) || !hkeyDeviceMap)
554 {
555 VBoxControlError("Error opening video device map registry key!\n");
556 return 0;
557 }
558 char szVideoLocation[256];
559 DWORD dwKeyType;
560 szVideoLocation[0] = 0;
561 DWORD len = sizeof(szVideoLocation);
562 status = RegQueryValueExA(hkeyDeviceMap, "\\Device\\Video0", NULL, &dwKeyType, (LPBYTE)szVideoLocation, &len);
563 /*
564 * This value will start with a weird value: \REGISTRY\Machine
565 * Make sure this is true.
566 */
567 if ( (status == ERROR_SUCCESS)
568 && (dwKeyType == REG_SZ)
569 && (_strnicmp(szVideoLocation, "\\REGISTRY\\Machine", 17) == 0))
570 {
571 /* open that branch */
572 status = RegOpenKeyExA(HKEY_LOCAL_MACHINE, &szVideoLocation[18], 0, KEY_READ | (writable ? KEY_WRITE : 0), &hkeyVideo);
573 }
574 else
575 {
576 VBoxControlError("Error opening registry key '%s'\n", &szVideoLocation[18]);
577 }
578 RegCloseKey(hkeyDeviceMap);
579 return hkeyVideo;
580}
581
582int handleGetVideoAcceleration(int argc, char *argv[])
583{
584 ULONG status;
585 HKEY hkeyVideo = getVideoKey(false);
586
587 if (hkeyVideo)
588 {
589 /* query the actual value */
590 DWORD fAcceleration = 1;
591 DWORD len = sizeof(fAcceleration);
592 DWORD dwKeyType;
593 status = RegQueryValueExA(hkeyVideo, "EnableVideoAccel", NULL, &dwKeyType, (LPBYTE)&fAcceleration, &len);
594 if (status != ERROR_SUCCESS)
595 RTPrintf("Video acceleration: default\n");
596 else
597 RTPrintf("Video acceleration: %s\n", fAcceleration ? "on" : "off");
598 RegCloseKey(hkeyVideo);
599 }
600 return 0;
601}
602
603int handleSetVideoAcceleration(int argc, char *argv[])
604{
605 ULONG status;
606 HKEY hkeyVideo;
607
608 /* must have exactly one argument: the new offset */
609 if ( (argc != 1)
610 || ( strcmp(argv[0], "on")
611 && strcmp(argv[0], "off")))
612 {
613 usage(SET_VIDEO_ACCEL);
614 return 1;
615 }
616
617 hkeyVideo = getVideoKey(true);
618
619 if (hkeyVideo)
620 {
621 int fAccel = 0;
622 if (!strcmp(argv[0], "on"))
623 fAccel = 1;
624 /* set a new value */
625 status = RegSetValueExA(hkeyVideo, "EnableVideoAccel", 0, REG_DWORD, (LPBYTE)&fAccel, sizeof(fAccel));
626 if (status != ERROR_SUCCESS)
627 {
628 VBoxControlError("Error %d writing video acceleration status!\n", status);
629 }
630 RegCloseKey(hkeyVideo);
631 }
632 return 0;
633}
634
635#define MAX_CUSTOM_MODES 128
636
637/* the table of custom modes */
638struct
639{
640 DWORD xres;
641 DWORD yres;
642 DWORD bpp;
643} customModes[MAX_CUSTOM_MODES] = {0};
644
645void getCustomModes(HKEY hkeyVideo)
646{
647 ULONG status;
648 int curMode = 0;
649
650 /* null out the table */
651 memset(customModes, 0, sizeof(customModes));
652
653 do
654 {
655 char valueName[20];
656 DWORD xres, yres, bpp = 0;
657 DWORD dwType;
658 DWORD dwLen = sizeof(DWORD);
659
660 RTStrPrintf(valueName, sizeof(valueName), "CustomMode%dWidth", curMode);
661 status = RegQueryValueExA(hkeyVideo, valueName, NULL, &dwType, (LPBYTE)&xres, &dwLen);
662 if (status != ERROR_SUCCESS)
663 break;
664 RTStrPrintf(valueName, sizeof(valueName), "CustomMode%dHeight", curMode);
665 status = RegQueryValueExA(hkeyVideo, valueName, NULL, &dwType, (LPBYTE)&yres, &dwLen);
666 if (status != ERROR_SUCCESS)
667 break;
668 RTStrPrintf(valueName, sizeof(valueName), "CustomMode%dBPP", curMode);
669 status = RegQueryValueExA(hkeyVideo, valueName, NULL, &dwType, (LPBYTE)&bpp, &dwLen);
670 if (status != ERROR_SUCCESS)
671 break;
672
673 /* check if the mode is OK */
674 if ( (xres > (1 << 16))
675 && (yres > (1 << 16))
676 && ( (bpp != 16)
677 || (bpp != 24)
678 || (bpp != 32)))
679 break;
680
681 /* add mode to table */
682 customModes[curMode].xres = xres;
683 customModes[curMode].yres = yres;
684 customModes[curMode].bpp = bpp;
685
686 ++curMode;
687
688 if (curMode >= MAX_CUSTOM_MODES)
689 break;
690 } while(1);
691}
692
693void writeCustomModes(HKEY hkeyVideo)
694{
695 ULONG status;
696 int tableIndex = 0;
697 int modeIndex = 0;
698
699 /* first remove all values */
700 for (int i = 0; i < MAX_CUSTOM_MODES; i++)
701 {
702 char valueName[20];
703 RTStrPrintf(valueName, sizeof(valueName), "CustomMode%dWidth", i);
704 RegDeleteValueA(hkeyVideo, valueName);
705 RTStrPrintf(valueName, sizeof(valueName), "CustomMode%dHeight", i);
706 RegDeleteValueA(hkeyVideo, valueName);
707 RTStrPrintf(valueName, sizeof(valueName), "CustomMode%dBPP", i);
708 RegDeleteValueA(hkeyVideo, valueName);
709 }
710
711 do
712 {
713 if (tableIndex >= MAX_CUSTOM_MODES)
714 break;
715
716 /* is the table entry present? */
717 if ( (!customModes[tableIndex].xres)
718 || (!customModes[tableIndex].yres)
719 || (!customModes[tableIndex].bpp))
720 {
721 tableIndex++;
722 continue;
723 }
724
725 RTPrintf("writing mode %d (%dx%dx%d)\n", modeIndex, customModes[tableIndex].xres, customModes[tableIndex].yres, customModes[tableIndex].bpp);
726 char valueName[20];
727 RTStrPrintf(valueName, sizeof(valueName), "CustomMode%dWidth", modeIndex);
728 status = RegSetValueExA(hkeyVideo, valueName, 0, REG_DWORD, (LPBYTE)&customModes[tableIndex].xres,
729 sizeof(customModes[tableIndex].xres));
730 RTStrPrintf(valueName, sizeof(valueName), "CustomMode%dHeight", modeIndex);
731 RegSetValueExA(hkeyVideo, valueName, 0, REG_DWORD, (LPBYTE)&customModes[tableIndex].yres,
732 sizeof(customModes[tableIndex].yres));
733 RTStrPrintf(valueName, sizeof(valueName), "CustomMode%dBPP", modeIndex);
734 RegSetValueExA(hkeyVideo, valueName, 0, REG_DWORD, (LPBYTE)&customModes[tableIndex].bpp,
735 sizeof(customModes[tableIndex].bpp));
736
737 modeIndex++;
738 tableIndex++;
739
740 } while(1);
741
742}
743
744int handleListCustomModes(int argc, char *argv[])
745{
746 if (argc != 0)
747 {
748 usage(LIST_CUST_MODES);
749 return 1;
750 }
751
752 HKEY hkeyVideo = getVideoKey(false);
753
754 if (hkeyVideo)
755 {
756 getCustomModes(hkeyVideo);
757 for (int i = 0; i < (sizeof(customModes) / sizeof(customModes[0])); i++)
758 {
759 if ( !customModes[i].xres
760 || !customModes[i].yres
761 || !customModes[i].bpp)
762 continue;
763
764 RTPrintf("Mode: %d x %d x %d\n",
765 customModes[i].xres, customModes[i].yres, customModes[i].bpp);
766 }
767 RegCloseKey(hkeyVideo);
768 }
769 return 0;
770}
771
772int handleAddCustomMode(int argc, char *argv[])
773{
774 if (argc != 3)
775 {
776 usage(ADD_CUST_MODE);
777 return 1;
778 }
779
780 DWORD xres = atoi(argv[0]);
781 DWORD yres = atoi(argv[1]);
782 DWORD bpp = atoi(argv[2]);
783
784 /** @todo better check including xres mod 8 = 0! */
785 if ( (xres > (1 << 16))
786 && (yres > (1 << 16))
787 && ( (bpp != 16)
788 || (bpp != 24)
789 || (bpp != 32)))
790 {
791 VBoxControlError("Error: invalid mode specified!\n");
792 return 1;
793 }
794
795 HKEY hkeyVideo = getVideoKey(true);
796
797 if (hkeyVideo)
798 {
799 int i;
800 int fModeExists = 0;
801 getCustomModes(hkeyVideo);
802 for (i = 0; i < MAX_CUSTOM_MODES; i++)
803 {
804 /* mode exists? */
805 if ( customModes[i].xres == xres
806 && customModes[i].yres == yres
807 && customModes[i].bpp == bpp
808 )
809 {
810 fModeExists = 1;
811 }
812 }
813 if (!fModeExists)
814 {
815 for (i = 0; i < MAX_CUSTOM_MODES; i++)
816 {
817 /* item free? */
818 if (!customModes[i].xres)
819 {
820 customModes[i].xres = xres;
821 customModes[i].yres = yres;
822 customModes[i].bpp = bpp;
823 break;
824 }
825 }
826 writeCustomModes(hkeyVideo);
827 }
828 RegCloseKey(hkeyVideo);
829 }
830 return 0;
831}
832
833int handleRemoveCustomMode(int argc, char *argv[])
834{
835 if (argc != 3)
836 {
837 usage(REMOVE_CUST_MODE);
838 return 1;
839 }
840
841 DWORD xres = atoi(argv[0]);
842 DWORD yres = atoi(argv[1]);
843 DWORD bpp = atoi(argv[2]);
844
845 HKEY hkeyVideo = getVideoKey(true);
846
847 if (hkeyVideo)
848 {
849 getCustomModes(hkeyVideo);
850 for (int i = 0; i < MAX_CUSTOM_MODES; i++)
851 {
852 /* correct item? */
853 if ( (customModes[i].xres == xres)
854 && (customModes[i].yres == yres)
855 && (customModes[i].bpp == bpp))
856 {
857 RTPrintf("found mode at index %d\n", i);
858 memset(&customModes[i], 0, sizeof(customModes[i]));
859 break;
860 }
861 }
862 writeCustomModes(hkeyVideo);
863 RegCloseKey(hkeyVideo);
864 }
865
866 return 0;
867}
868
869#endif /* RT_OS_WINDOWS */
870
871#ifdef VBOX_WITH_GUEST_PROPS
872/**
873 * Retrieves a value from the guest property store.
874 * This is accessed through the "VBoxGuestPropSvc" HGCM service.
875 *
876 * @returns 0 on success, 1 on failure
877 * @note see the command line API description for parameters
878 */
879int getGuestProperty(int argc, char **argv)
880{
881 using namespace guestProp;
882
883 bool verbose = false;
884 if ((2 == argc) && (0 == strcmp(argv[1], "-verbose")))
885 verbose = true;
886 else if (argc != 1)
887 {
888 usage(GUEST_PROP);
889 return 1;
890 }
891
892 uint32_t u32ClientId = 0;
893 int rc = VINF_SUCCESS;
894
895 rc = VbglR3GuestPropConnect(&u32ClientId);
896 if (!RT_SUCCESS(rc))
897 VBoxControlError("Failed to connect to the guest property service, error %Rrc\n", rc);
898
899/*
900 * Here we actually retrieve the value from the host.
901 */
902 const char *pszName = argv[0];
903 char *pszValue = NULL;
904 uint64_t u64Timestamp = 0;
905 char *pszFlags = NULL;
906 /* The buffer for storing the data and its initial size. We leave a bit
907 * of space here in case the maximum values are raised. */
908 void *pvBuf = NULL;
909 uint32_t cbBuf = MAX_VALUE_LEN + MAX_FLAGS_LEN + 1024;
910 if (RT_SUCCESS(rc))
911 {
912 /* Because there is a race condition between our reading the size of a
913 * property and the guest updating it, we loop a few times here and
914 * hope. Actually this should never go wrong, as we are generous
915 * enough with buffer space. */
916 bool finish = false;
917 for (unsigned i = 0; (i < 10) && !finish; ++i)
918 {
919 void *pvTmpBuf = RTMemRealloc(pvBuf, cbBuf);
920 if (NULL == pvTmpBuf)
921 {
922 rc = VERR_NO_MEMORY;
923 VBoxControlError("Out of memory\n");
924 }
925 else
926 {
927 pvBuf = pvTmpBuf;
928 rc = VbglR3GuestPropRead(u32ClientId, pszName, pvBuf, cbBuf,
929 &pszValue, &u64Timestamp, &pszFlags,
930 &cbBuf);
931 }
932 if (VERR_BUFFER_OVERFLOW == rc)
933 /* Leave a bit of extra space to be safe */
934 cbBuf += 1024;
935 else
936 finish = true;
937 }
938 if (VERR_TOO_MUCH_DATA == rc)
939 VBoxControlError("Temporarily unable to retrieve the property\n");
940 else if (!RT_SUCCESS(rc) && (rc != VERR_NOT_FOUND))
941 VBoxControlError("Failed to retrieve the property value, error %Rrc\n", rc);
942 }
943/*
944 * And display it on the guest console.
945 */
946 if (VERR_NOT_FOUND == rc)
947 RTPrintf("No value set!\n");
948 else if (RT_SUCCESS(rc))
949 {
950 RTPrintf("Value: %S\n", pszValue);
951 if (verbose)
952 {
953 RTPrintf("Timestamp: %lld ns\n", u64Timestamp);
954 RTPrintf("Flags: %S\n", pszFlags);
955 }
956 }
957
958 if (u32ClientId != 0)
959 VbglR3GuestPropDisconnect(u32ClientId);
960 RTMemFree(pvBuf);
961 return RT_SUCCESS(rc) ? 0 : 1;
962}
963
964
965/**
966 * Writes a value to the guest property store.
967 * This is accessed through the "VBoxGuestPropSvc" HGCM service.
968 *
969 * @returns 0 on success, 1 on failure
970 * @note see the command line API description for parameters
971 */
972static int setGuestProperty(int argc, char *argv[])
973{
974/*
975 * Check the syntax. We can deduce the correct syntax from the number of
976 * arguments.
977 */
978 bool usageOK = true;
979 const char *pszName = NULL;
980 const char *pszValue = NULL;
981 const char *pszFlags = NULL;
982 if (2 == argc)
983 {
984 pszValue = argv[1];
985 }
986 else if (3 == argc)
987 usageOK = false;
988 else if (4 == argc)
989 {
990 pszValue = argv[1];
991 if (strcmp(argv[2], "-flags") != 0)
992 usageOK = false;
993 pszFlags = argv[3];
994 }
995 else if (argc != 1)
996 usageOK = false;
997 if (!usageOK)
998 {
999 usage(GUEST_PROP);
1000 return 1;
1001 }
1002 /* This is always needed. */
1003 pszName = argv[0];
1004
1005/*
1006 * Do the actual setting.
1007 */
1008 uint32_t u32ClientId = 0;
1009 int rc = VINF_SUCCESS;
1010 rc = VbglR3GuestPropConnect(&u32ClientId);
1011 if (!RT_SUCCESS(rc))
1012 VBoxControlError("Failed to connect to the guest property service, error %Rrc\n", rc);
1013 if (RT_SUCCESS(rc))
1014 {
1015 if (pszFlags != NULL)
1016 rc = VbglR3GuestPropWrite(u32ClientId, pszName, pszValue, pszFlags);
1017 else
1018 rc = VbglR3GuestPropWriteValue(u32ClientId, pszName, pszValue);
1019 if (!RT_SUCCESS(rc))
1020 VBoxControlError("Failed to store the property value, error %Rrc\n", rc);
1021 }
1022
1023 if (u32ClientId != 0)
1024 VbglR3GuestPropDisconnect(u32ClientId);
1025 return RT_SUCCESS(rc) ? 0 : 1;
1026}
1027
1028
1029/**
1030 * Enumerates the properties in the guest property store.
1031 * This is accessed through the "VBoxGuestPropSvc" HGCM service.
1032 *
1033 * @returns 0 on success, 1 on failure
1034 * @note see the command line API description for parameters
1035 */
1036static int enumGuestProperty(int argc, char *argv[])
1037{
1038 /*
1039 * Check the syntax. We can deduce the correct syntax from the number of
1040 * arguments.
1041 */
1042 char const * const *papszPatterns = NULL;
1043 size_t cPatterns = 0;
1044 if ( argc > 1
1045 && !strcmp(argv[0], "-patterns"))
1046 {
1047 papszPatterns = (char const * const *)&argv[1];
1048 cPatterns = argc - 1;
1049 }
1050 else if (argc != 0)
1051 {
1052 usage(GUEST_PROP);
1053 return 1;
1054 }
1055
1056 /*
1057 * Do the actual enumeration.
1058 */
1059 uint32_t u32ClientId = 0;
1060 int rc = VbglR3GuestPropConnect(&u32ClientId);
1061 if (RT_SUCCESS(rc))
1062 {
1063 PVBGLR3GUESTPROPENUM pHandle;
1064 const char *pszName, *pszValue, *pszFlags;
1065 uint64_t u64Timestamp;
1066
1067 rc = VbglR3GuestPropEnum(u32ClientId, papszPatterns, cPatterns, &pHandle,
1068 &pszName, &pszValue, &u64Timestamp, &pszFlags);
1069 if (RT_SUCCESS(rc))
1070 {
1071 while (RT_SUCCESS(rc) && pszName)
1072 {
1073 RTPrintf("Name: %s, value: %s, timestamp: %lld, flags: %s\n",
1074 pszName, pszValue, u64Timestamp, pszFlags);
1075
1076 rc = VbglR3GuestPropEnumNext(pHandle, &pszName, &pszValue, &u64Timestamp, &pszFlags);
1077 if (RT_FAILURE(rc))
1078 VBoxControlError("Error while enumerating guest properties: %Rrc\n", rc);
1079 }
1080
1081 VbglR3GuestPropEnumFree(pHandle);
1082 }
1083 else if (VERR_NOT_FOUND == rc)
1084 RTPrintf("No properties found.\n");
1085 else
1086 VBoxControlError("Failed to enumerate the guest properties! Error: %Rrc\n", rc);
1087 VbglR3GuestPropDisconnect(u32ClientId);
1088 }
1089 else
1090 VBoxControlError("Failed to connect to the guest property service! Error: %Rrc\n", rc);
1091 return RT_SUCCESS(rc) ? 0 : 1;
1092}
1093
1094
1095/**
1096 * Access the guest property store through the "VBoxGuestPropSvc" HGCM
1097 * service.
1098 *
1099 * @returns 0 on success, 1 on failure
1100 * @note see the command line API description for parameters
1101 */
1102static int handleGuestProperty(int argc, char *argv[])
1103{
1104 if (0 == argc)
1105 {
1106 usage(GUEST_PROP);
1107 return 1;
1108 }
1109 if (0 == strcmp(argv[0], "get"))
1110 return getGuestProperty(argc - 1, argv + 1);
1111 else if (0 == strcmp(argv[0], "set"))
1112 return setGuestProperty(argc - 1, argv + 1);
1113 else if (0 == strcmp(argv[0], "enumerate"))
1114 return enumGuestProperty(argc - 1, argv + 1);
1115 /* else */
1116 usage(GUEST_PROP);
1117 return 1;
1118}
1119
1120#endif
1121
1122/** command handler type */
1123typedef DECLCALLBACK(int) FNHANDLER(int argc, char *argv[]);
1124typedef FNHANDLER *PFNHANDLER;
1125
1126/** The table of all registered command handlers. */
1127struct COMMANDHANDLER
1128{
1129 const char *command;
1130 PFNHANDLER handler;
1131} g_commandHandlers[] =
1132{
1133#ifdef RT_OS_WINDOWS
1134 { "getvideoacceleration", handleGetVideoAcceleration },
1135 { "setvideoacceleration", handleSetVideoAcceleration },
1136 { "listcustommodes", handleListCustomModes },
1137 { "addcustommode", handleAddCustomMode },
1138 { "removecustommode", handleRemoveCustomMode },
1139 { "setvideomode", handleSetVideoMode },
1140#endif
1141#ifdef VBOX_WITH_GUEST_PROPS
1142 { "guestproperty", handleGuestProperty },
1143#endif
1144 { NULL, NULL } /* terminator */
1145};
1146
1147/** Main function */
1148int main(int argc, char **argv)
1149{
1150 /** The application's global return code */
1151 int rc = 0;
1152 /** An IPRT return code for local use */
1153 int rrc = VINF_SUCCESS;
1154 /** The index of the command line argument we are currently processing */
1155 int iArg = 1;
1156 /** Should we show the logo text? */
1157 bool showlogo = true;
1158 /** Should we print the usage after the logo? For the -help switch. */
1159 bool dohelp = false;
1160 /** Will we be executing a command or just printing information? */
1161 bool onlyinfo = false;
1162
1163/*
1164 * Start by handling command line switches
1165 */
1166
1167 /** Are we finished with handling switches? */
1168 bool done = false;
1169 while (!done && (iArg < argc))
1170 {
1171 if ( (0 == strcmp(argv[iArg], "-v"))
1172 || (0 == strcmp(argv[iArg], "--version"))
1173 || (0 == strcmp(argv[iArg], "-version"))
1174 || (0 == strcmp(argv[iArg], "getversion"))
1175 )
1176 {
1177 /* Print version number, and do nothing else. */
1178 RTPrintf("%sr%d\n", VBOX_VERSION_STRING, VBoxSVNRev ());
1179 onlyinfo = true;
1180 showlogo = false;
1181 done = true;
1182 }
1183 else if (0 == strcmp(argv[iArg], "-nologo"))
1184 showlogo = false;
1185 else if (0 == strcmp(argv[iArg], "-help"))
1186 {
1187 onlyinfo = true;
1188 dohelp = true;
1189 done = true;
1190 }
1191 else
1192 /* We have found an argument which isn't a switch. Exit to the
1193 * command processing bit. */
1194 done = true;
1195 if (!done)
1196 ++iArg;
1197 }
1198
1199/*
1200 * Find the application name, show our logo if the user hasn't suppressed it,
1201 * and show the usage if the user asked us to
1202 */
1203
1204 g_pszProgName = RTPathFilename(argv[0]);
1205 if (showlogo)
1206 RTPrintf("VirtualBox Guest Additions Command Line Management Interface Version "
1207 VBOX_VERSION_STRING "\n"
1208 "(C) 2008 Sun Microsystems, Inc.\n"
1209 "All rights reserved\n\n");
1210 if (dohelp)
1211 usage();
1212
1213/*
1214 * Do global initialisation for the programme if we will be handling a command
1215 */
1216
1217 if (!onlyinfo)
1218 {
1219 rrc = RTR3Init(); /** @todo r=bird: This ALWAYS goes first, the only exception is when you have to parse args to figure out which to call! */
1220 if (!RT_SUCCESS(rrc))
1221 {
1222 VBoxControlError("Failed to initialise the VirtualBox runtime - error %Rrc\n", rrc);
1223 rc = 1;
1224 }
1225 if (0 == rc)
1226 {
1227 if (!RT_SUCCESS(VbglR3Init()))
1228 {
1229 VBoxControlError("Could not contact the host system. Make sure that you are running this\n"
1230 "application inside a VirtualBox guest system, and that you have sufficient\n"
1231 "user permissions.\n");
1232 rc = 1;
1233 }
1234 }
1235 }
1236
1237/*
1238 * Now look for an actual command in the argument list and handle it.
1239 */
1240
1241 if (!onlyinfo && (0 == rc))
1242 {
1243 /*
1244 * The input is in the guest OS'es codepage (NT guarantees ACP).
1245 * For VBox we use UTF-8. For simplicity, just convert the argv[] array
1246 * here.
1247 */
1248 for (int i = iArg; i < argc; i++)
1249 {
1250 char *converted;
1251 RTStrCurrentCPToUtf8(&converted, argv[i]);
1252 argv[i] = converted;
1253 }
1254
1255 if (argc > iArg)
1256 {
1257 /** Is next parameter a known command? */
1258 bool found = false;
1259 /** And if so, what is its position in the table? */
1260 unsigned index = 0;
1261 while ( index < RT_ELEMENTS(g_commandHandlers)
1262 && !found
1263 && (g_commandHandlers[index].command != NULL))
1264 {
1265 if (0 == strcmp(argv[iArg], g_commandHandlers[index].command))
1266 found = true;
1267 else
1268 ++index;
1269 }
1270 if (found)
1271 rc = g_commandHandlers[index].handler(argc - iArg - 1, argv + iArg + 1);
1272 else
1273 {
1274 rc = 1;
1275 usage();
1276 }
1277 }
1278 else
1279 {
1280 /* The user didn't specify a command. */
1281 rc = 1;
1282 usage();
1283 }
1284
1285 /*
1286 * Free converted argument vector
1287 */
1288 for (int i = iArg; i < argc; i++)
1289 RTStrFree(argv[i]);
1290
1291 }
1292
1293/*
1294 * And exit, returning the status
1295 */
1296
1297 return rc;
1298}
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