1 | /** @file
|
---|
2 | *
|
---|
3 | * Seamless mode:
|
---|
4 | * Linux guest.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2011 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 | #ifndef __Additions_linux_seamless_x11_h
|
---|
20 | # define __Additions_linux_seamless_x11_h
|
---|
21 |
|
---|
22 | #include <VBox/log.h>
|
---|
23 | #include <iprt/avl.h>
|
---|
24 |
|
---|
25 | #include <X11/Xlib.h>
|
---|
26 | #include <X11/Xutil.h>
|
---|
27 | #include <X11/extensions/shape.h>
|
---|
28 |
|
---|
29 | #define WM_TYPE_PROP "_NET_WM_WINDOW_TYPE"
|
---|
30 | #define WM_TYPE_DESKTOP_PROP "_NET_WM_WINDOW_TYPE_DESKTOP"
|
---|
31 |
|
---|
32 | /* This is defined wrong in my X11 header files! */
|
---|
33 | #define VBoxShapeNotify 64
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Small virtual class which provides the interface for notifying the host of
|
---|
37 | * changes to the X11 window configuration, mainly split out from
|
---|
38 | * @a VBoxGuestSeamlessHost to simplify the unit test.
|
---|
39 | */
|
---|
40 | class SeamlessHostProxy
|
---|
41 | {
|
---|
42 | public:
|
---|
43 | virtual void sendRegionUpdate(RTRECT *pRects, size_t cRects) = 0;
|
---|
44 | virtual ~SeamlessHostProxy() {}
|
---|
45 | };
|
---|
46 |
|
---|
47 | /** Structure containing information about a guest window's position and visible area.
|
---|
48 | Used inside of VBoxGuestWindowList. */
|
---|
49 | struct VBoxGuestWinInfo {
|
---|
50 | public:
|
---|
51 | /** Header structure for insertion into an AVL tree */
|
---|
52 | AVLU32NODECORE Core;
|
---|
53 | /** Is the window currently mapped? */
|
---|
54 | bool mhasShape;
|
---|
55 | /** Co-ordinates in the guest screen. */
|
---|
56 | int mX, mY;
|
---|
57 | /** Window dimensions. */
|
---|
58 | int mWidth, mHeight;
|
---|
59 | /** Number of rectangles used to represent the visible area. */
|
---|
60 | int mcRects;
|
---|
61 | /** Rectangles representing the visible area. These must be allocated
|
---|
62 | * by XMalloc and will be freed automatically if non-null when the class
|
---|
63 | * is destroyed. */
|
---|
64 | XRectangle *mpRects;
|
---|
65 | /** Constructor. */
|
---|
66 | VBoxGuestWinInfo(bool hasShape, int x, int y, int w, int h, int cRects,
|
---|
67 | XRectangle *pRects)
|
---|
68 | : mhasShape(hasShape), mX(x), mY(y), mWidth(w), mHeight(h),
|
---|
69 | mcRects(cRects), mpRects(pRects) {}
|
---|
70 |
|
---|
71 | /** Destructor */
|
---|
72 | ~VBoxGuestWinInfo()
|
---|
73 | {
|
---|
74 | if (mpRects)
|
---|
75 | XFree(mpRects);
|
---|
76 | }
|
---|
77 |
|
---|
78 | private:
|
---|
79 | // We don't want a copy constructor or assignment operator
|
---|
80 | VBoxGuestWinInfo(const VBoxGuestWinInfo&);
|
---|
81 | VBoxGuestWinInfo& operator=(const VBoxGuestWinInfo&);
|
---|
82 | };
|
---|
83 |
|
---|
84 | /** Callback type used for "DoWithAll" calls */
|
---|
85 | typedef DECLCALLBACK(int) VBOXGUESTWINCALLBACK(VBoxGuestWinInfo *, void *);
|
---|
86 | /** Pointer to VBOXGUESTWINCALLBACK */
|
---|
87 | typedef VBOXGUESTWINCALLBACK *PVBOXGUESTWINCALLBACK;
|
---|
88 |
|
---|
89 | DECLCALLBACK(int) inline VBoxGuestWinCleanup(VBoxGuestWinInfo *pInfo, void *)
|
---|
90 | {
|
---|
91 | delete pInfo;
|
---|
92 | return VINF_SUCCESS;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * This class is just a wrapper around a map of structures containing
|
---|
97 | * information about the windows on the guest system. It has a function for
|
---|
98 | * adding a structure (see addWindow) and one for removing it by window
|
---|
99 | * handle (see removeWindow).
|
---|
100 | */
|
---|
101 | class VBoxGuestWindowList
|
---|
102 | {
|
---|
103 | private:
|
---|
104 | // We don't want a copy constructor or an assignment operator
|
---|
105 | VBoxGuestWindowList(const VBoxGuestWindowList&);
|
---|
106 | VBoxGuestWindowList& operator=(const VBoxGuestWindowList&);
|
---|
107 |
|
---|
108 | // Private class members
|
---|
109 | AVLU32TREE mWindows;
|
---|
110 |
|
---|
111 | public:
|
---|
112 | // Constructor
|
---|
113 | VBoxGuestWindowList(void) : mWindows(NULL) {}
|
---|
114 | // Destructor
|
---|
115 | ~VBoxGuestWindowList()
|
---|
116 | {
|
---|
117 | /** @todo having this inside the container class hard codes that the
|
---|
118 | * elements have to be allocated with the "new" operator, and
|
---|
119 | * I don't see a need to require this. */
|
---|
120 | doWithAll(VBoxGuestWinCleanup, NULL);
|
---|
121 | }
|
---|
122 |
|
---|
123 | // Standard operations
|
---|
124 | VBoxGuestWinInfo *find(Window hWin)
|
---|
125 | {
|
---|
126 | return (VBoxGuestWinInfo *)RTAvlU32Get(&mWindows, hWin);
|
---|
127 | }
|
---|
128 |
|
---|
129 | void detachAll(PVBOXGUESTWINCALLBACK pCallback, void *pvParam)
|
---|
130 | {
|
---|
131 | RTAvlU32Destroy(&mWindows, (PAVLU32CALLBACK)pCallback, pvParam);
|
---|
132 | }
|
---|
133 |
|
---|
134 | int doWithAll(PVBOXGUESTWINCALLBACK pCallback, void *pvParam)
|
---|
135 | {
|
---|
136 | return RTAvlU32DoWithAll(&mWindows, 1, (PAVLU32CALLBACK)pCallback,
|
---|
137 | pvParam);
|
---|
138 | }
|
---|
139 |
|
---|
140 | bool addWindow(Window hWin, bool isMapped, int x, int y, int w, int h, int cRects,
|
---|
141 | XRectangle *pRects)
|
---|
142 | {
|
---|
143 | LogRelFlowFunc(("\n"));
|
---|
144 | VBoxGuestWinInfo *pInfo = new VBoxGuestWinInfo(isMapped, x, y, w, h, cRects,
|
---|
145 | pRects);
|
---|
146 | pInfo->Core.Key = hWin;
|
---|
147 | LogRelFlowFunc(("returning\n"));
|
---|
148 | return RTAvlU32Insert(&mWindows, &pInfo->Core);
|
---|
149 | }
|
---|
150 |
|
---|
151 | VBoxGuestWinInfo *removeWindow(Window hWin)
|
---|
152 | {
|
---|
153 | LogRelFlowFunc(("called\n"));
|
---|
154 | return (VBoxGuestWinInfo *)RTAvlU32Remove(&mWindows, hWin);
|
---|
155 | }
|
---|
156 | };
|
---|
157 |
|
---|
158 | class SeamlessX11
|
---|
159 | {
|
---|
160 | private:
|
---|
161 | // We don't want a copy constructor or assignment operator
|
---|
162 | SeamlessX11(const SeamlessX11&);
|
---|
163 | SeamlessX11& operator=(const SeamlessX11&);
|
---|
164 |
|
---|
165 | // Private member variables
|
---|
166 | /** Pointer to the host class. */
|
---|
167 | SeamlessHostProxy *mHost;
|
---|
168 | /** Our connection to the X11 display we are running on. */
|
---|
169 | Display *mDisplay;
|
---|
170 | /** Class to keep track of visible guest windows. */
|
---|
171 | VBoxGuestWindowList mGuestWindows;
|
---|
172 | /** The current set of seamless rectangles. */
|
---|
173 | RTRECT *mpRects;
|
---|
174 | /** The current number of seamless rectangles. */
|
---|
175 | int mcRects;
|
---|
176 | /** Do we support the X shaped window extension? */
|
---|
177 | bool mSupportsShape;
|
---|
178 | /** Is seamless mode currently enabled? */
|
---|
179 | bool mEnabled;
|
---|
180 | /** Have there been changes since the last time we sent a notification? */
|
---|
181 | bool mChanged;
|
---|
182 |
|
---|
183 | // Private methods
|
---|
184 |
|
---|
185 | // Methods to manage guest window information
|
---|
186 | /**
|
---|
187 | * Store information about a desktop window and register for structure events on it.
|
---|
188 | * If it is mapped, go through the list of it's children and add information about
|
---|
189 | * mapped children to the tree of visible windows, making sure that those windows are
|
---|
190 | * not already in our list of desktop windows.
|
---|
191 | *
|
---|
192 | * @param hWin the window concerned - should be a "desktop" window
|
---|
193 | */
|
---|
194 | void monitorClientList(void);
|
---|
195 | void unmonitorClientList(void);
|
---|
196 | void rebuildWindowTree(void);
|
---|
197 | void addClients(const Window hRoot);
|
---|
198 | bool isVirtualRoot(Window hWin);
|
---|
199 | void addClientWindow(Window hWin);
|
---|
200 | void freeWindowTree(void);
|
---|
201 | void updateHostSeamlessInfo(void);
|
---|
202 | int updateRects(void);
|
---|
203 |
|
---|
204 | public:
|
---|
205 | /**
|
---|
206 | * Initialise the guest and ensure that it is capable of handling seamless mode
|
---|
207 | * @param pHost Host interface class to notify of window configuration
|
---|
208 | * changes.
|
---|
209 | *
|
---|
210 | * @returns iprt status code
|
---|
211 | */
|
---|
212 | int init(SeamlessHostProxy *pHost);
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Shutdown seamless event monitoring.
|
---|
216 | */
|
---|
217 | void uninit(void)
|
---|
218 | {
|
---|
219 | if (mHost)
|
---|
220 | stop();
|
---|
221 | mHost = NULL;
|
---|
222 | if (mDisplay)
|
---|
223 | XCloseDisplay(mDisplay);
|
---|
224 | mDisplay = NULL;
|
---|
225 | }
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Initialise seamless event reporting in the guest.
|
---|
229 | *
|
---|
230 | * @returns IPRT status code
|
---|
231 | */
|
---|
232 | int start(void);
|
---|
233 | /** Stop reporting seamless events. */
|
---|
234 | void stop(void);
|
---|
235 | /** Get the current list of visible rectangles. */
|
---|
236 | RTRECT *getRects(void);
|
---|
237 | /** Get the number of visible rectangles in the current list */
|
---|
238 | size_t getRectCount(void);
|
---|
239 |
|
---|
240 | /** Process next event in the guest event queue - called by the event thread. */
|
---|
241 | void nextConfigurationEvent(void);
|
---|
242 | /** Wake up the event thread if it is waiting for an event so that it can exit. */
|
---|
243 | bool interruptEventWait(void);
|
---|
244 |
|
---|
245 | /* Methods to handle X11 events. These are public so that the unit test
|
---|
246 | * can call them. */
|
---|
247 | void doConfigureEvent(Window hWin);
|
---|
248 | void doMapEvent(Window hWin);
|
---|
249 | void doUnmapEvent(Window hWin);
|
---|
250 | void doShapeEvent(Window hWin);
|
---|
251 |
|
---|
252 | SeamlessX11(void)
|
---|
253 | : mHost(0), mDisplay(NULL), mpRects(NULL), mcRects(0),
|
---|
254 | mSupportsShape(false), mEnabled(false), mChanged(false) {}
|
---|
255 |
|
---|
256 | ~SeamlessX11()
|
---|
257 | {
|
---|
258 | uninit();
|
---|
259 | }
|
---|
260 | };
|
---|
261 |
|
---|
262 | #endif /* __Additions_linux_seamless_x11_h not defined */
|
---|