VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/xclient/seamless-x11.h@ 6556

Last change on this file since 6556 was 6290, checked in by vboxsync, 17 years ago

Guest Additions: update for the Linux seamless additions

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette