1 | /** @file
|
---|
2 | *
|
---|
3 | * Seamless mode:
|
---|
4 | * Linux guest.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
19 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
20 | * additional information or have any questions.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #ifndef __Additions_linux_seamless_x11_h
|
---|
24 | # define __Additions_linux_seamless_x11_h
|
---|
25 |
|
---|
26 | #include <VBox/log.h>
|
---|
27 |
|
---|
28 | #include "seamless-guest.h"
|
---|
29 |
|
---|
30 | #include <X11/Xlib.h>
|
---|
31 | #include <X11/extensions/shape.h>
|
---|
32 |
|
---|
33 | #include <map>
|
---|
34 | #include <vector>
|
---|
35 |
|
---|
36 | #define WM_TYPE_PROP "_NET_WM_WINDOW_TYPE"
|
---|
37 | #define WM_TYPE_DESKTOP_PROP "_NET_WM_WINDOW_TYPE_DESKTOP"
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Wrapper class around the VBoxGuestX11Pointer to provide reference semantics.
|
---|
41 | * See auto_ptr in the C++ <memory> header.
|
---|
42 | */
|
---|
43 | template <class T>
|
---|
44 | struct VBoxGuestX11PointerRef
|
---|
45 | {
|
---|
46 | T *mValue;
|
---|
47 |
|
---|
48 | VBoxGuestX11PointerRef(T* pValue) { mValue = pValue; }
|
---|
49 | };
|
---|
50 |
|
---|
51 | /** An auto pointer for pointers which have to be XFree'd. */
|
---|
52 | template <class T>
|
---|
53 | class VBoxGuestX11Pointer
|
---|
54 | {
|
---|
55 | private:
|
---|
56 | T *mValue;
|
---|
57 | public:
|
---|
58 | VBoxGuestX11Pointer(T *pValue = 0) { mValue = pValue; }
|
---|
59 | ~VBoxGuestX11Pointer() { if (0 != mValue) XFree(mValue); }
|
---|
60 |
|
---|
61 | /** release method to get the pointer's value and "reset" the pointer. */
|
---|
62 | T *release(void) { T *pTmp = mValue; mValue = 0; return pTmp; }
|
---|
63 |
|
---|
64 | /** reset the pointer value to zero or to another pointer. */
|
---|
65 | void reset(T* pValue = 0) { if (pValue != mValue) { XFree(mValue); mValue = pValue; } }
|
---|
66 |
|
---|
67 | /** Copy constructor */
|
---|
68 | VBoxGuestX11Pointer(VBoxGuestX11Pointer &orig) { mValue = orig.release(); }
|
---|
69 |
|
---|
70 | /** Copy from equivalent class */
|
---|
71 | template <class T1>
|
---|
72 | VBoxGuestX11Pointer(VBoxGuestX11Pointer<T1> &orig) { mValue = orig.release(); }
|
---|
73 |
|
---|
74 | /** Assignment operator. */
|
---|
75 | VBoxGuestX11Pointer& operator=(VBoxGuestX11Pointer &orig)
|
---|
76 | {
|
---|
77 | reset(orig.release());
|
---|
78 | return *this;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /** Assignment from equivalent class. */
|
---|
82 | template <class T1>
|
---|
83 | VBoxGuestX11Pointer& operator=(VBoxGuestX11Pointer<T1> &orig)
|
---|
84 | {
|
---|
85 | reset(orig.release);
|
---|
86 | return *this;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /** Assignment from a pointer. */
|
---|
90 | VBoxGuestX11Pointer& operator=(T *pValue)
|
---|
91 | {
|
---|
92 | if (0 != mValue)
|
---|
93 | {
|
---|
94 | XFree(mValue);
|
---|
95 | }
|
---|
96 | mValue = pValue;
|
---|
97 | return *this;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /** Dereference with * operator. */
|
---|
101 | T &operator*() { return *mValue; }
|
---|
102 |
|
---|
103 | /** Dereference with -> operator. */
|
---|
104 | T *operator->() { return mValue; }
|
---|
105 |
|
---|
106 | /** Accessing the value inside. */
|
---|
107 | T *get(void) { return mValue; }
|
---|
108 |
|
---|
109 | /** Convert a reference structure into an X11 pointer. */
|
---|
110 | VBoxGuestX11Pointer(VBoxGuestX11PointerRef<T> ref) { mValue = ref.mValue; }
|
---|
111 |
|
---|
112 | /** Assign from a reference structure into an X11 pointer. */
|
---|
113 | VBoxGuestX11Pointer& operator=(VBoxGuestX11PointerRef<T> ref)
|
---|
114 | {
|
---|
115 | if (ref.mValue != mValue)
|
---|
116 | {
|
---|
117 | XFree(mValue);
|
---|
118 | mValue = ref.mValue;
|
---|
119 | }
|
---|
120 | return *this;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /** Typecast an X11 pointer to a reference structure. */
|
---|
124 | template <class T1>
|
---|
125 | operator VBoxGuestX11PointerRef<T1>() { return VBoxGuestX11PointerRef<T1>(release()); }
|
---|
126 |
|
---|
127 | /** Typecast an X11 pointer to an X11 pointer around a different type. */
|
---|
128 | template <class T1>
|
---|
129 | operator VBoxGuestX11Pointer<T1>() { return VBoxGuestX11Pointer<T1>(release()); }
|
---|
130 | };
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Wrapper class around an X11 display pointer which takes care of closing the display
|
---|
134 | * when it is destroyed at the latest.
|
---|
135 | */
|
---|
136 | class VBoxGuestX11Display
|
---|
137 | {
|
---|
138 | private:
|
---|
139 | Display *mDisplay;
|
---|
140 | public:
|
---|
141 | VBoxGuestX11Display(void) { mDisplay = NULL; }
|
---|
142 | bool init(char *name = NULL)
|
---|
143 | {
|
---|
144 | LogFlowThisFunc(("\n"));
|
---|
145 | mDisplay = XOpenDisplay(name);
|
---|
146 | LogFlowThisFunc(("returning\n"));
|
---|
147 | return (mDisplay != NULL);
|
---|
148 | }
|
---|
149 | operator Display *() { return mDisplay; }
|
---|
150 | Display *get(void) { return mDisplay; }
|
---|
151 | bool isValid(void) { return (mDisplay != NULL); }
|
---|
152 | int close(void)
|
---|
153 | {
|
---|
154 | LogFlowThisFunc(("\n"));
|
---|
155 | int rc = XCloseDisplay(mDisplay);
|
---|
156 | mDisplay = NULL;
|
---|
157 | LogFlowThisFunc(("returning\n"));
|
---|
158 | return rc;
|
---|
159 | }
|
---|
160 | ~VBoxGuestX11Display()
|
---|
161 | {
|
---|
162 | if (mDisplay != NULL)
|
---|
163 | close();
|
---|
164 | }
|
---|
165 | };
|
---|
166 |
|
---|
167 | /** Structure containing information about a guest window's position and visible area.
|
---|
168 | Used inside of VBoxGuestWindowList. */
|
---|
169 | struct VBoxGuestWinInfo {
|
---|
170 | public:
|
---|
171 | /** Is the window currently mapped? */
|
---|
172 | bool mhasShape;
|
---|
173 | /** Co-ordinates in the guest screen. */
|
---|
174 | int mX, mY;
|
---|
175 | /** Window dimensions. */
|
---|
176 | int mWidth, mHeight;
|
---|
177 | /** Number of rectangles used to represent the visible area. */
|
---|
178 | int mcRects;
|
---|
179 | /** Rectangles representing the visible area. These must be allocated by XMalloc
|
---|
180 | and will be freed automatically if non-null when the class is destroyed. */
|
---|
181 | VBoxGuestX11Pointer<XRectangle> mapRects;
|
---|
182 | /** Constructor. */
|
---|
183 | VBoxGuestWinInfo(bool hasShape, int x, int y, int w, int h, int cRects,
|
---|
184 | VBoxGuestX11Pointer<XRectangle> rects)
|
---|
185 | : mapRects(rects)
|
---|
186 | {
|
---|
187 | mhasShape = hasShape, mX = x; mY = y; mWidth = w; mHeight = h; mcRects = cRects;
|
---|
188 | }
|
---|
189 |
|
---|
190 | private:
|
---|
191 | // We don't want a copy constructor or assignment operator
|
---|
192 | VBoxGuestWinInfo(const VBoxGuestWinInfo&);
|
---|
193 | VBoxGuestWinInfo& operator=(const VBoxGuestWinInfo&);
|
---|
194 | };
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * This class is just a wrapper around a map of structures containing information about
|
---|
198 | * the windows on the guest system. It has a function for adding a structure (see addWindow),
|
---|
199 | * for removing it by window handle (see removeWindow) and an iterator for
|
---|
200 | * going through the list.
|
---|
201 | */
|
---|
202 | class VBoxGuestWindowList
|
---|
203 | {
|
---|
204 | private:
|
---|
205 | // We don't want a copy constructor or an assignment operator
|
---|
206 | VBoxGuestWindowList(const VBoxGuestWindowList&);
|
---|
207 | VBoxGuestWindowList& operator=(const VBoxGuestWindowList&);
|
---|
208 |
|
---|
209 | // Private class members
|
---|
210 | std::map<Window, VBoxGuestWinInfo *> mWindows;
|
---|
211 |
|
---|
212 | public:
|
---|
213 | // Just proxy iterators to map::iterator
|
---|
214 | typedef std::map<Window, VBoxGuestWinInfo *>::const_iterator const_iterator;
|
---|
215 | typedef std::map<Window, VBoxGuestWinInfo *>::iterator iterator;
|
---|
216 |
|
---|
217 | // Constructor
|
---|
218 | VBoxGuestWindowList(void) {}
|
---|
219 | // Destructor
|
---|
220 | ~VBoxGuestWindowList()
|
---|
221 | {
|
---|
222 | /* We use post-increment in the operation to prevent the iterator from being invalidated. */
|
---|
223 | try
|
---|
224 | {
|
---|
225 | for (iterator it = begin(); it != end(); removeWindow(it++));
|
---|
226 | }
|
---|
227 | catch(...) {}
|
---|
228 | }
|
---|
229 |
|
---|
230 | // Standard operations
|
---|
231 | const_iterator begin() const { return mWindows.begin(); }
|
---|
232 | iterator begin() { return mWindows.begin(); }
|
---|
233 | const_iterator end() const { return mWindows.end(); }
|
---|
234 | iterator end() { return mWindows.end(); }
|
---|
235 | const_iterator find(Window win) const { return mWindows.find(win); }
|
---|
236 | iterator find(Window win) { return mWindows.find(win); }
|
---|
237 |
|
---|
238 | void addWindow(Window hWin, bool isMapped, int x, int y, int w, int h, int cRects,
|
---|
239 | VBoxGuestX11Pointer<XRectangle> rects)
|
---|
240 | {
|
---|
241 | LogFlowThisFunc(("\n"));
|
---|
242 | VBoxGuestWinInfo *pInfo = new VBoxGuestWinInfo(isMapped, x, y, w, h, cRects,
|
---|
243 | rects);
|
---|
244 | mWindows.insert(std::pair<Window, VBoxGuestWinInfo *>(hWin, pInfo));
|
---|
245 | LogFlowThisFunc(("returning\n"));
|
---|
246 | }
|
---|
247 |
|
---|
248 | void removeWindow(iterator it)
|
---|
249 | {
|
---|
250 | LogFlowThisFunc(("called\n"));
|
---|
251 | delete it->second;
|
---|
252 | mWindows.erase(it);
|
---|
253 | }
|
---|
254 |
|
---|
255 | void removeWindow(Window hWin)
|
---|
256 | {
|
---|
257 | LogFlowThisFunc(("called\n"));
|
---|
258 | removeWindow(find(hWin));
|
---|
259 | }
|
---|
260 | };
|
---|
261 |
|
---|
262 | class VBoxGuestSeamlessX11;
|
---|
263 |
|
---|
264 | class VBoxGuestSeamlessX11 : public VBoxGuestSeamlessGuest
|
---|
265 | {
|
---|
266 | private:
|
---|
267 | // We don't want a copy constructor or assignment operator
|
---|
268 | VBoxGuestSeamlessX11(const VBoxGuestSeamlessX11&);
|
---|
269 | VBoxGuestSeamlessX11& operator=(const VBoxGuestSeamlessX11&);
|
---|
270 |
|
---|
271 | // Private member variables
|
---|
272 | /** Pointer to the observer class. */
|
---|
273 | VBoxGuestSeamlessObserver *mObserver;
|
---|
274 | /** Our connection to the X11 display we are running on. */
|
---|
275 | VBoxGuestX11Display mDisplay;
|
---|
276 | /** Class to keep track of visible guest windows. */
|
---|
277 | VBoxGuestWindowList mGuestWindows;
|
---|
278 | /** Keeps track of the total number of rectangles needed for the visible area of all
|
---|
279 | guest windows on the last call to getRects. Used for pre-allocating space in
|
---|
280 | the vector of rectangles passed to the host. */
|
---|
281 | int mcRects;
|
---|
282 | /** Do we support the X shaped window extension? */
|
---|
283 | bool mSupportsShape;
|
---|
284 | /** Is seamles mode currently enabled? */
|
---|
285 | bool mEnabled;
|
---|
286 |
|
---|
287 | // Private methods
|
---|
288 |
|
---|
289 | // Methods to handle X11 events
|
---|
290 | void doConfigureEvent(const XConfigureEvent *event);
|
---|
291 | void doMapEvent(const XMapEvent *event);
|
---|
292 | void doUnmapEvent(const XUnmapEvent *event);
|
---|
293 | void doShapeEvent(const XShapeEvent *event);
|
---|
294 |
|
---|
295 | // Methods to manage guest window information
|
---|
296 | /**
|
---|
297 | * Store information about a desktop window and register for structure events on it.
|
---|
298 | * If it is mapped, go through the list of it's children and add information about
|
---|
299 | * mapped children to the tree of visible windows, making sure that those windows are
|
---|
300 | * not already in our list of desktop windows.
|
---|
301 | *
|
---|
302 | * @param hWin the window concerned - should be a "desktop" window
|
---|
303 | */
|
---|
304 | void monitorClientList(void);
|
---|
305 | void unmonitorClientList(void);
|
---|
306 | void rebuildWindowTree(void);
|
---|
307 | void addClients(const Window hRoot);
|
---|
308 | bool isVirtualRoot(Window hWin);
|
---|
309 | void addClientWindow(Window hWin);
|
---|
310 | void freeWindowTree(void);
|
---|
311 | void updateHostSeamlessInfo(void);
|
---|
312 |
|
---|
313 | public:
|
---|
314 | /**
|
---|
315 | * Initialise the guest and ensure that it is capable of handling seamless mode
|
---|
316 | * @param pObserver Observer class to connect host and guest interfaces
|
---|
317 | *
|
---|
318 | * @returns iprt status code
|
---|
319 | */
|
---|
320 | int init(VBoxGuestSeamlessObserver *pObserver);
|
---|
321 |
|
---|
322 | /**
|
---|
323 | * Shutdown seamless event monitoring.
|
---|
324 | */
|
---|
325 | void uninit(void)
|
---|
326 | {
|
---|
327 | if (0 != mObserver)
|
---|
328 | {
|
---|
329 | stop();
|
---|
330 | }
|
---|
331 | mObserver = 0;
|
---|
332 | }
|
---|
333 |
|
---|
334 | /**
|
---|
335 | * Initialise seamless event reporting in the guest.
|
---|
336 | *
|
---|
337 | * @returns IPRT status code
|
---|
338 | */
|
---|
339 | int start(void);
|
---|
340 | /** Stop reporting seamless events. */
|
---|
341 | void stop(void);
|
---|
342 | /** Get the current list of visible rectangles. */
|
---|
343 | std::auto_ptr<std::vector<RTRECT> > getRects(void);
|
---|
344 |
|
---|
345 | /** Process next event in the guest event queue - called by the event thread. */
|
---|
346 | void nextEvent(void);
|
---|
347 | /** Wake up the event thread if it is waiting for an event so that it can exit. */
|
---|
348 | bool interruptEvent(void);
|
---|
349 |
|
---|
350 | VBoxGuestSeamlessX11(void)
|
---|
351 | {
|
---|
352 | mObserver = 0; mcRects = 0; mEnabled = false; mSupportsShape = false;
|
---|
353 | }
|
---|
354 |
|
---|
355 | ~VBoxGuestSeamlessX11()
|
---|
356 | {
|
---|
357 | try
|
---|
358 | {
|
---|
359 | uninit();
|
---|
360 | }
|
---|
361 | catch(...) {}
|
---|
362 | }
|
---|
363 | };
|
---|
364 |
|
---|
365 | typedef VBoxGuestSeamlessX11 VBoxGuestSeamlessGuestImpl;
|
---|
366 |
|
---|
367 | #endif /* __Additions_linux_seamless_x11_h not defined */
|
---|