1 | /** @file
|
---|
2 | *
|
---|
3 | * Guest client: display auto-resize.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-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 | /** @todo this should probably be replaced by something IPRT */
|
---|
23 | /* For system() and WEXITSTATUS() */
|
---|
24 | #include <stdlib.h>
|
---|
25 | #include <sys/types.h>
|
---|
26 | #include <sys/wait.h>
|
---|
27 | #include <errno.h>
|
---|
28 |
|
---|
29 | #include <X11/Xlib.h>
|
---|
30 |
|
---|
31 | #include <iprt/assert.h>
|
---|
32 | #include <iprt/err.h>
|
---|
33 | #include <iprt/thread.h>
|
---|
34 | #include <VBox/log.h>
|
---|
35 | #include <VBox/VBoxGuest.h>
|
---|
36 | #include <VBox/VBoxGuestLib.h>
|
---|
37 |
|
---|
38 | #include "VBoxClient.h"
|
---|
39 |
|
---|
40 | static int initAutoResize()
|
---|
41 | {
|
---|
42 | int rc = VINF_SUCCESS;
|
---|
43 | int rcSystem, rcErrno;
|
---|
44 |
|
---|
45 | LogFlowFunc(("\n"));
|
---|
46 | rcSystem = system("VBoxRandR --test");
|
---|
47 | if (-1 == rcSystem)
|
---|
48 | {
|
---|
49 | rcErrno = errno;
|
---|
50 | rc = RTErrConvertFromErrno(rcErrno);
|
---|
51 | }
|
---|
52 | if (RT_SUCCESS(rc))
|
---|
53 | {
|
---|
54 | if (0 != WEXITSTATUS(rcSystem))
|
---|
55 | rc = VERR_NOT_SUPPORTED;
|
---|
56 | }
|
---|
57 | if (RT_SUCCESS(rc))
|
---|
58 | rc = VbglR3CtlFilterMask(VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST, 0);
|
---|
59 | LogFlowFunc(("returning %Rrc\n", rc));
|
---|
60 | return rc;
|
---|
61 | }
|
---|
62 |
|
---|
63 | void cleanupAutoResize(void)
|
---|
64 | {
|
---|
65 | LogFlowFunc(("\n"));
|
---|
66 | VbglR3CtlFilterMask(0, VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST);
|
---|
67 | LogFlowFunc(("returning\n"));
|
---|
68 | }
|
---|
69 |
|
---|
70 | /** This thread just runs a dummy X11 event loop to be sure that we get
|
---|
71 | * terminated should the X server exit. */
|
---|
72 | static int x11ConnectionMonitor(RTTHREAD, void *)
|
---|
73 | {
|
---|
74 | XEvent ev;
|
---|
75 | Display *pDisplay = XOpenDisplay(NULL);
|
---|
76 | while (true)
|
---|
77 | XNextEvent(pDisplay, &ev);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Display change request monitor thread function.
|
---|
82 | * Before entering the loop, we re-read the last request
|
---|
83 | * received, and if the first one received inside the
|
---|
84 | * loop is identical we ignore it, because it is probably
|
---|
85 | * stale.
|
---|
86 | */
|
---|
87 | int runAutoResize()
|
---|
88 | {
|
---|
89 | LogFlowFunc(("\n"));
|
---|
90 | uint32_t cx0 = 0, cy0 = 0, cBits0 = 0, iDisplay0 = 0;
|
---|
91 | int rc = RTThreadCreate(NULL, x11ConnectionMonitor, NULL, 0,
|
---|
92 | RTTHREADTYPE_INFREQUENT_POLLER, 0, "X11 monitor");
|
---|
93 | if (RT_FAILURE(rc))
|
---|
94 | return rc;
|
---|
95 | VbglR3GetLastDisplayChangeRequest(&cx0, &cy0, &cBits0, &iDisplay0);
|
---|
96 | while (true)
|
---|
97 | {
|
---|
98 | uint32_t cx = 0, cy = 0, cBits = 0, iDisplay = 0;
|
---|
99 | rc = VbglR3DisplayChangeWaitEvent(&cx, &cy, &cBits, &iDisplay);
|
---|
100 | /* Ignore the request if it is stale */
|
---|
101 | if ((cx != cx0) || (cy != cy0) || RT_FAILURE(rc))
|
---|
102 | {
|
---|
103 | /* If we are not stopping, sleep for a bit to avoid using up too
|
---|
104 | much CPU while retrying. */
|
---|
105 | if (RT_FAILURE(rc))
|
---|
106 | RTThreadYield();
|
---|
107 | else
|
---|
108 | system("VBoxRandR");
|
---|
109 | }
|
---|
110 | /* We do not want to ignore any further requests. */
|
---|
111 | cx0 = 0;
|
---|
112 | cy0 = 0;
|
---|
113 | }
|
---|
114 | LogFlowFunc(("returning VINF_SUCCESS\n"));
|
---|
115 | return VINF_SUCCESS;
|
---|
116 | }
|
---|
117 |
|
---|
118 | class AutoResizeService : public VBoxClient::Service
|
---|
119 | {
|
---|
120 | public:
|
---|
121 | virtual const char *getPidFilePath()
|
---|
122 | {
|
---|
123 | return ".vboxclient-autoresize.pid";
|
---|
124 | }
|
---|
125 | virtual int run()
|
---|
126 | {
|
---|
127 | int rc = initAutoResize();
|
---|
128 | if (RT_SUCCESS(rc))
|
---|
129 | rc = runAutoResize();
|
---|
130 | return rc;
|
---|
131 | }
|
---|
132 | virtual void cleanup()
|
---|
133 | {
|
---|
134 | cleanupAutoResize();
|
---|
135 | }
|
---|
136 | };
|
---|
137 |
|
---|
138 | VBoxClient::Service *VBoxClient::GetAutoResizeService()
|
---|
139 | {
|
---|
140 | return new AutoResizeService;
|
---|
141 | }
|
---|