1 | /* $Id: seamless-x11.h 83045 2020-02-11 12:31:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Seamless mode - X11 guests.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.alldomusa.eu.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 |
|
---|
18 | #ifndef GA_INCLUDED_SRC_x11_VBoxClient_seamless_x11_h
|
---|
19 | #define GA_INCLUDED_SRC_x11_VBoxClient_seamless_x11_h
|
---|
20 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
21 | # pragma once
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | #include <VBox/log.h>
|
---|
25 | #include <iprt/avl.h>
|
---|
26 | #ifdef RT_NEED_NEW_AND_DELETE
|
---|
27 | # include <iprt/mem.h>
|
---|
28 | # include <new>
|
---|
29 | #endif
|
---|
30 |
|
---|
31 | #include <X11/Xlib.h>
|
---|
32 | #include <X11/Xutil.h>
|
---|
33 | #include <X11/Xlibint.h>
|
---|
34 | #include <X11/extensions/shape.h>
|
---|
35 | #include <X11/extensions/Xrandr.h>
|
---|
36 |
|
---|
37 | #define WM_TYPE_PROP "_NET_WM_WINDOW_TYPE"
|
---|
38 | #define WM_TYPE_DESKTOP_PROP "_NET_WM_WINDOW_TYPE_DESKTOP"
|
---|
39 |
|
---|
40 | /* This is defined wrong in my X11 header files! */
|
---|
41 | #define VBoxShapeNotify 64
|
---|
42 |
|
---|
43 | /** Following are stolen from randr.h Define XrandR stuff of they are not there (some ancient build box maybe). */
|
---|
44 |
|
---|
45 | /* Event selection bits */
|
---|
46 | #ifndef RRScreenChangeNotifyMask
|
---|
47 | #define RRScreenChangeNotifyMask (1L << 0)
|
---|
48 | #endif
|
---|
49 | /* V1.2 additions */
|
---|
50 | #ifndef RRCrtcChangeNotifyMask
|
---|
51 | #define RRCrtcChangeNotifyMask (1L << 1)
|
---|
52 | #endif
|
---|
53 | #ifndef RROutputChangeNotifyMask
|
---|
54 | #define RROutputChangeNotifyMask (1L << 2)
|
---|
55 | #endif
|
---|
56 |
|
---|
57 | #ifndef RROutputPropertyNotifyMask
|
---|
58 | #define RROutputPropertyNotifyMask (1L << 3)
|
---|
59 | #endif
|
---|
60 |
|
---|
61 | /* V1.4 additions */
|
---|
62 | #ifndef RRProviderChangeNotifyMask
|
---|
63 | #define RRProviderChangeNotifyMask (1L << 4)
|
---|
64 | #endif
|
---|
65 |
|
---|
66 | #ifndef RRProviderPropertyNotifyMask
|
---|
67 | #define RRProviderPropertyNotifyMask (1L << 5)
|
---|
68 | #endif
|
---|
69 |
|
---|
70 | #ifndef RRResourceChangeNotifyMask
|
---|
71 | #define RRResourceChangeNotifyMask (1L << 6)
|
---|
72 | #endif
|
---|
73 |
|
---|
74 | #ifndef RRScreenChangeNotify
|
---|
75 | /* Event codes */
|
---|
76 | #define RRScreenChangeNotify 0
|
---|
77 | #endif
|
---|
78 | #ifndef RRNotify
|
---|
79 | /* V1.2 additions */
|
---|
80 | #define RRNotify 1
|
---|
81 | #endif
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Callback which provides the interface for notifying the host of changes to
|
---|
85 | * the X11 window configuration, mainly split out from @a VBoxGuestSeamlessHost
|
---|
86 | * to simplify the unit test.
|
---|
87 | */
|
---|
88 | typedef void FNSENDREGIONUPDATE(RTRECT *pRects, size_t cRects);
|
---|
89 | typedef FNSENDREGIONUPDATE *PFNSENDREGIONUPDATE;
|
---|
90 |
|
---|
91 | /** Structure containing information about a guest window's position and visible area.
|
---|
92 | Used inside of VBoxGuestWindowList. */
|
---|
93 | struct VBoxGuestWinInfo {
|
---|
94 | public:
|
---|
95 | /** Header structure for insertion into an AVL tree */
|
---|
96 | AVLU32NODECORE Core;
|
---|
97 | /** Is the window currently mapped? */
|
---|
98 | bool mhasShape;
|
---|
99 | /** Co-ordinates in the guest screen. */
|
---|
100 | int mX, mY;
|
---|
101 | /** Window dimensions. */
|
---|
102 | int mWidth, mHeight;
|
---|
103 | /** Number of rectangles used to represent the visible area. */
|
---|
104 | int mcRects;
|
---|
105 | /** Rectangles representing the visible area. These must be allocated
|
---|
106 | * by XMalloc and will be freed automatically if non-null when the class
|
---|
107 | * is destroyed. */
|
---|
108 | XRectangle *mpRects;
|
---|
109 | /** Constructor. */
|
---|
110 | VBoxGuestWinInfo(bool hasShape, int x, int y, int w, int h, int cRects,
|
---|
111 | XRectangle *pRects)
|
---|
112 | : mhasShape(hasShape), mX(x), mY(y), mWidth(w), mHeight(h),
|
---|
113 | mcRects(cRects), mpRects(pRects) {}
|
---|
114 |
|
---|
115 | /** Destructor */
|
---|
116 | ~VBoxGuestWinInfo()
|
---|
117 | {
|
---|
118 | if (mpRects)
|
---|
119 | XFree(mpRects);
|
---|
120 | }
|
---|
121 | #ifdef RT_NEED_NEW_AND_DELETE
|
---|
122 | RTMEM_IMPLEMENT_NEW_AND_DELETE();
|
---|
123 | #endif
|
---|
124 |
|
---|
125 | private:
|
---|
126 | // We don't want a copy constructor or assignment operator
|
---|
127 | VBoxGuestWinInfo(const VBoxGuestWinInfo&);
|
---|
128 | VBoxGuestWinInfo& operator=(const VBoxGuestWinInfo&);
|
---|
129 | };
|
---|
130 |
|
---|
131 | /** Callback type used for "DoWithAll" calls */
|
---|
132 | typedef DECLCALLBACK(int) VBOXGUESTWINCALLBACK(VBoxGuestWinInfo *, void *);
|
---|
133 | /** Pointer to VBOXGUESTWINCALLBACK */
|
---|
134 | typedef VBOXGUESTWINCALLBACK *PVBOXGUESTWINCALLBACK;
|
---|
135 |
|
---|
136 | DECLCALLBACK(int) inline VBoxGuestWinCleanup(VBoxGuestWinInfo *pInfo, void *)
|
---|
137 | {
|
---|
138 | delete pInfo;
|
---|
139 | return VINF_SUCCESS;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * This class is just a wrapper around a map of structures containing
|
---|
144 | * information about the windows on the guest system. It has a function for
|
---|
145 | * adding a structure (see addWindow) and one for removing it by window
|
---|
146 | * handle (see removeWindow).
|
---|
147 | */
|
---|
148 | class VBoxGuestWindowList
|
---|
149 | {
|
---|
150 | private:
|
---|
151 | // We don't want a copy constructor or an assignment operator
|
---|
152 | VBoxGuestWindowList(const VBoxGuestWindowList&);
|
---|
153 | VBoxGuestWindowList& operator=(const VBoxGuestWindowList&);
|
---|
154 |
|
---|
155 | // Private class members
|
---|
156 | AVLU32TREE mWindows;
|
---|
157 |
|
---|
158 | public:
|
---|
159 | // Constructor
|
---|
160 | VBoxGuestWindowList(void) : mWindows(NULL) {}
|
---|
161 | // Destructor
|
---|
162 | ~VBoxGuestWindowList()
|
---|
163 | {
|
---|
164 | /** @todo having this inside the container class hard codes that the
|
---|
165 | * elements have to be allocated with the "new" operator, and
|
---|
166 | * I don't see a need to require this. */
|
---|
167 | doWithAll(VBoxGuestWinCleanup, NULL);
|
---|
168 | }
|
---|
169 |
|
---|
170 | #ifdef RT_NEED_NEW_AND_DELETE
|
---|
171 | RTMEM_IMPLEMENT_NEW_AND_DELETE();
|
---|
172 | #endif
|
---|
173 |
|
---|
174 | // Standard operations
|
---|
175 | VBoxGuestWinInfo *find(Window hWin)
|
---|
176 | {
|
---|
177 | return (VBoxGuestWinInfo *)RTAvlU32Get(&mWindows, hWin);
|
---|
178 | }
|
---|
179 |
|
---|
180 | void detachAll(PVBOXGUESTWINCALLBACK pCallback, void *pvParam)
|
---|
181 | {
|
---|
182 | RTAvlU32Destroy(&mWindows, (PAVLU32CALLBACK)pCallback, pvParam);
|
---|
183 | }
|
---|
184 |
|
---|
185 | int doWithAll(PVBOXGUESTWINCALLBACK pCallback, void *pvParam)
|
---|
186 | {
|
---|
187 | return RTAvlU32DoWithAll(&mWindows, 1, (PAVLU32CALLBACK)pCallback,
|
---|
188 | pvParam);
|
---|
189 | }
|
---|
190 |
|
---|
191 | bool addWindow(Window hWin, bool isMapped, int x, int y, int w, int h, int cRects,
|
---|
192 | XRectangle *pRects)
|
---|
193 | {
|
---|
194 | LogRelFlowFunc(("hWin=%lu, isMapped=%RTbool, x=%d, y=%d, w=%d, h=%d, cRects=%d\n",
|
---|
195 | (unsigned long) hWin, isMapped, x, y, w, h, cRects));
|
---|
196 | VBoxGuestWinInfo *pInfo = new VBoxGuestWinInfo(isMapped, x, y, w, h, cRects,
|
---|
197 | pRects);
|
---|
198 | pInfo->Core.Key = hWin;
|
---|
199 | LogRelFlowFuncLeave();
|
---|
200 | return RTAvlU32Insert(&mWindows, &pInfo->Core);
|
---|
201 | }
|
---|
202 |
|
---|
203 | VBoxGuestWinInfo *removeWindow(Window hWin)
|
---|
204 | {
|
---|
205 | LogRelFlowFuncEnter();
|
---|
206 | return (VBoxGuestWinInfo *)RTAvlU32Remove(&mWindows, hWin);
|
---|
207 | }
|
---|
208 | };
|
---|
209 |
|
---|
210 | class SeamlessX11
|
---|
211 | {
|
---|
212 | private:
|
---|
213 | // We don't want a copy constructor or assignment operator
|
---|
214 | SeamlessX11(const SeamlessX11&);
|
---|
215 | SeamlessX11& operator=(const SeamlessX11&);
|
---|
216 |
|
---|
217 | // Private member variables
|
---|
218 | /** Pointer to the host callback. */
|
---|
219 | PFNSENDREGIONUPDATE mHostCallback;
|
---|
220 | /** Our connection to the X11 display we are running on. */
|
---|
221 | Display *mDisplay;
|
---|
222 | /** Class to keep track of visible guest windows. */
|
---|
223 | VBoxGuestWindowList mGuestWindows;
|
---|
224 | /** The current set of seamless rectangles. */
|
---|
225 | RTRECT *mpRects;
|
---|
226 | /** The current number of seamless rectangles. */
|
---|
227 | int mcRects;
|
---|
228 | /** Do we support the X shaped window extension? */
|
---|
229 | bool mSupportsShape;
|
---|
230 | /** Is seamless mode currently enabled? */
|
---|
231 | bool mEnabled;
|
---|
232 | /** Have there been changes since the last time we sent a notification? */
|
---|
233 | bool mChanged;
|
---|
234 | /** Event mask we use to select randr related events. */
|
---|
235 | int mRandRMask;
|
---|
236 | int mRandREventBase;
|
---|
237 | int mRandRErrorBase;
|
---|
238 | bool mRandRAvailable;
|
---|
239 |
|
---|
240 | // Private methods
|
---|
241 |
|
---|
242 | // Methods to manage guest window information
|
---|
243 | /**
|
---|
244 | * Store information about a desktop window and register for structure events on it.
|
---|
245 | * If it is mapped, go through the list of it's children and add information about
|
---|
246 | * mapped children to the tree of visible windows, making sure that those windows are
|
---|
247 | * not already in our list of desktop windows.
|
---|
248 | *
|
---|
249 | * @param hWin the window concerned - should be a "desktop" window
|
---|
250 | */
|
---|
251 | void monitorClientList(void);
|
---|
252 | void unmonitorClientList(void);
|
---|
253 | void monitorRandREvents(XEvent *pEvent);
|
---|
254 | void rebuildWindowTree(void);
|
---|
255 | void addClients(const Window hRoot);
|
---|
256 | bool isVirtualRoot(Window hWin);
|
---|
257 | void addClientWindow(Window hWin);
|
---|
258 | void freeWindowTree(void);
|
---|
259 | void updateHostSeamlessInfo(void);
|
---|
260 | int updateRects(void);
|
---|
261 |
|
---|
262 | public:
|
---|
263 | /**
|
---|
264 | * Initialise the guest and ensure that it is capable of handling seamless mode
|
---|
265 | * @param pHostCallback Host interface callback to notify of window configuration
|
---|
266 | * changes.
|
---|
267 | *
|
---|
268 | * @returns iprt status code
|
---|
269 | */
|
---|
270 | int init(PFNSENDREGIONUPDATE pHostCallback);
|
---|
271 |
|
---|
272 | /**
|
---|
273 | * Shutdown seamless event monitoring.
|
---|
274 | */
|
---|
275 | void uninit(void)
|
---|
276 | {
|
---|
277 | if (mHostCallback)
|
---|
278 | stop();
|
---|
279 | mHostCallback = NULL;
|
---|
280 | if (mDisplay)
|
---|
281 | XCloseDisplay(mDisplay);
|
---|
282 | mDisplay = NULL;
|
---|
283 | }
|
---|
284 |
|
---|
285 | /**
|
---|
286 | * Initialise seamless event reporting in the guest.
|
---|
287 | *
|
---|
288 | * @returns IPRT status code
|
---|
289 | */
|
---|
290 | int start(void);
|
---|
291 | /** Stop reporting seamless events. */
|
---|
292 | void stop(void);
|
---|
293 | /** Get the current list of visible rectangles. */
|
---|
294 | RTRECT *getRects(void);
|
---|
295 | /** Get the number of visible rectangles in the current list */
|
---|
296 | size_t getRectCount(void);
|
---|
297 |
|
---|
298 | /** Process next event in the guest event queue - called by the event thread. */
|
---|
299 | void nextConfigurationEvent(void);
|
---|
300 | /** Wake up the event thread if it is waiting for an event so that it can exit. */
|
---|
301 | bool interruptEventWait(void);
|
---|
302 |
|
---|
303 | /* Methods to handle X11 events. These are public so that the unit test
|
---|
304 | * can call them. */
|
---|
305 | void doConfigureEvent(Window hWin);
|
---|
306 | void doShapeEvent(Window hWin);
|
---|
307 |
|
---|
308 | SeamlessX11(void)
|
---|
309 | : mHostCallback(NULL), mDisplay(NULL), mpRects(NULL), mcRects(0),
|
---|
310 | mSupportsShape(false), mEnabled(false), mChanged(false) {}
|
---|
311 |
|
---|
312 | ~SeamlessX11()
|
---|
313 | {
|
---|
314 | uninit();
|
---|
315 | }
|
---|
316 |
|
---|
317 | #ifdef RT_NEED_NEW_AND_DELETE
|
---|
318 | RTMEM_IMPLEMENT_NEW_AND_DELETE();
|
---|
319 | #endif
|
---|
320 | };
|
---|
321 |
|
---|
322 | #endif /* !GA_INCLUDED_SRC_x11_VBoxClient_seamless_x11_h */
|
---|