1 | /* $Id: display.cpp 21923 2009-07-31 20:01:47Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * X11 guest client - display management.
|
---|
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 | #include <X11/cursorfont.h>
|
---|
31 |
|
---|
32 | #include <iprt/assert.h>
|
---|
33 | #include <iprt/err.h>
|
---|
34 | #include <iprt/thread.h>
|
---|
35 | #include <VBox/log.h>
|
---|
36 | #include <VBox/VMMDev.h>
|
---|
37 | #include <VBox/VBoxGuestLib.h>
|
---|
38 |
|
---|
39 | #include "VBoxClient.h"
|
---|
40 |
|
---|
41 | static int initDisplay()
|
---|
42 | {
|
---|
43 | int rc = VINF_SUCCESS;
|
---|
44 | int rcSystem, rcErrno;
|
---|
45 | uint32_t fMouseFeatures = 0;
|
---|
46 |
|
---|
47 | LogFlowFunc(("\n"));
|
---|
48 | rcSystem = system("VBoxRandR --test");
|
---|
49 | if (-1 == rcSystem)
|
---|
50 | {
|
---|
51 | rcErrno = errno;
|
---|
52 | rc = RTErrConvertFromErrno(rcErrno);
|
---|
53 | }
|
---|
54 | if (RT_SUCCESS(rc))
|
---|
55 | {
|
---|
56 | if (0 != WEXITSTATUS(rcSystem))
|
---|
57 | rc = VERR_NOT_SUPPORTED;
|
---|
58 | }
|
---|
59 | if (RT_SUCCESS(rc))
|
---|
60 | rc = VbglR3CtlFilterMask(VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST, 0);
|
---|
61 | /* Enable support for switching between hardware and software cursors */
|
---|
62 | rc = VbglR3CtlFilterMask(VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED, 0);
|
---|
63 | if (RT_SUCCESS(rc))
|
---|
64 | {
|
---|
65 | rc = VbglR3GetMouseStatus(&fMouseFeatures, NULL, NULL);
|
---|
66 | if (RT_SUCCESS(rc))
|
---|
67 | VbglR3SetMouseStatus( fMouseFeatures
|
---|
68 | & ~VMMDEV_MOUSE_GUEST_NEEDS_HOST_CURSOR);
|
---|
69 | }
|
---|
70 | LogFlowFunc(("returning %Rrc\n", rc));
|
---|
71 | return rc;
|
---|
72 | }
|
---|
73 |
|
---|
74 | void cleanupDisplay(void)
|
---|
75 | {
|
---|
76 | uint32_t fMouseFeatures = 0;
|
---|
77 | LogFlowFunc(("\n"));
|
---|
78 | VbglR3CtlFilterMask(0, VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST
|
---|
79 | | VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED);
|
---|
80 | int rc = VbglR3GetMouseStatus(&fMouseFeatures, NULL, NULL);
|
---|
81 | if (RT_SUCCESS(rc))
|
---|
82 | VbglR3SetMouseStatus( fMouseFeatures
|
---|
83 | | VMMDEV_MOUSE_GUEST_NEEDS_HOST_CURSOR);
|
---|
84 | LogFlowFunc(("returning\n"));
|
---|
85 | }
|
---|
86 |
|
---|
87 | /** This thread just runs a dummy X11 event loop to be sure that we get
|
---|
88 | * terminated should the X server exit. */
|
---|
89 | static int x11ConnectionMonitor(RTTHREAD, void *)
|
---|
90 | {
|
---|
91 | XEvent ev;
|
---|
92 | Display *pDisplay = XOpenDisplay(NULL);
|
---|
93 | while (true)
|
---|
94 | XNextEvent(pDisplay, &ev);
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Display change request monitor thread function.
|
---|
99 | * Before entering the loop, we re-read the last request
|
---|
100 | * received, and if the first one received inside the
|
---|
101 | * loop is identical we ignore it, because it is probably
|
---|
102 | * stale.
|
---|
103 | */
|
---|
104 | int runDisplay()
|
---|
105 | {
|
---|
106 | LogFlowFunc(("\n"));
|
---|
107 | uint32_t cx0 = 0, cy0 = 0, cBits0 = 0, iDisplay0 = 0;
|
---|
108 | Display *pDisplay = XOpenDisplay(NULL);
|
---|
109 | if (pDisplay == NULL)
|
---|
110 | return VERR_NOT_FOUND;
|
---|
111 | Cursor hClockCursor = XCreateFontCursor(pDisplay, XC_watch);
|
---|
112 | Cursor hArrowCursor = XCreateFontCursor(pDisplay, XC_left_ptr);
|
---|
113 | int rc = RTThreadCreate(NULL, x11ConnectionMonitor, NULL, 0,
|
---|
114 | RTTHREADTYPE_INFREQUENT_POLLER, 0, "X11 monitor");
|
---|
115 | if (RT_FAILURE(rc))
|
---|
116 | return rc;
|
---|
117 | VbglR3GetDisplayChangeRequest(&cx0, &cy0, &cBits0, &iDisplay0, false);
|
---|
118 | while (true)
|
---|
119 | {
|
---|
120 | uint32_t fEvents = 0, cx = 0, cy = 0, cBits = 0, iDisplay = 0;
|
---|
121 | rc = VbglR3WaitEvent( VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST
|
---|
122 | | VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED,
|
---|
123 | RT_INDEFINITE_WAIT, &fEvents);
|
---|
124 | if (RT_SUCCESS(rc) && (fEvents & VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST))
|
---|
125 | {
|
---|
126 | int rc2 = VbglR3GetDisplayChangeRequest(&cx, &cy, &cBits,
|
---|
127 | &iDisplay, true);
|
---|
128 | /* Ignore the request if it is stale */
|
---|
129 | if ((cx != cx0) || (cy != cy0) || RT_FAILURE(rc2))
|
---|
130 | {
|
---|
131 | /* If we are not stopping, sleep for a bit to avoid using up
|
---|
132 | too much CPU while retrying. */
|
---|
133 | if (RT_FAILURE(rc2))
|
---|
134 | RTThreadYield();
|
---|
135 | else
|
---|
136 | {
|
---|
137 | system("VBoxRandR");
|
---|
138 | cx0 = cx;
|
---|
139 | cy0 = cy;
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|
143 | if ( RT_SUCCESS(rc)
|
---|
144 | && (fEvents & VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED))
|
---|
145 | {
|
---|
146 | XGrabPointer(pDisplay,
|
---|
147 | DefaultRootWindow(pDisplay), true, 0, GrabModeAsync,
|
---|
148 | GrabModeAsync, None, hClockCursor, CurrentTime);
|
---|
149 | XFlush(pDisplay);
|
---|
150 | XGrabPointer(pDisplay,
|
---|
151 | DefaultRootWindow(pDisplay), true, 0, GrabModeAsync,
|
---|
152 | GrabModeAsync, None, hArrowCursor, CurrentTime);
|
---|
153 | XFlush(pDisplay);
|
---|
154 | XUngrabPointer(pDisplay, CurrentTime);
|
---|
155 | XFlush(pDisplay);
|
---|
156 | }
|
---|
157 | }
|
---|
158 | LogFlowFunc(("returning VINF_SUCCESS\n"));
|
---|
159 | return VINF_SUCCESS;
|
---|
160 | }
|
---|
161 |
|
---|
162 | class DisplayService : public VBoxClient::Service
|
---|
163 | {
|
---|
164 | public:
|
---|
165 | virtual const char *getPidFilePath()
|
---|
166 | {
|
---|
167 | return ".vboxclient-display.pid";
|
---|
168 | }
|
---|
169 | virtual int run()
|
---|
170 | {
|
---|
171 | int rc = initDisplay();
|
---|
172 | if (RT_SUCCESS(rc))
|
---|
173 | rc = runDisplay();
|
---|
174 | return rc;
|
---|
175 | }
|
---|
176 | virtual void cleanup()
|
---|
177 | {
|
---|
178 | cleanupDisplay();
|
---|
179 | }
|
---|
180 | };
|
---|
181 |
|
---|
182 | VBoxClient::Service *VBoxClient::GetDisplayService()
|
---|
183 | {
|
---|
184 | return new DisplayService;
|
---|
185 | }
|
---|