seamless-x11.cpp revision e42f96d376f5a18f4c61212e47eadae813bb2a2b
/** @file
*
* Seamless mode:
* Linux guest.
*/
/*
* Copyright (C) 2006-2007 innotek GmbH
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*****************************************************************************
* Header files *
*****************************************************************************/
#include <iprt/log.h>
#include <iprt/err.h>
#include <iprt/assert.h>
#include <VBox/VBoxGuest.h>
#include "seamless-guest.h"
#include <X11/Xatom.h>
/*****************************************************************************
* Static functions *
*****************************************************************************/
static VBoxGuestX11Pointer<unsigned char> XXGetProperty (Display *aDpy, Window aWnd,
Atom aPropType, const char *aPropName,
unsigned long *nItems)
{
Atom propNameAtom = XInternAtom (aDpy, aPropName,
True /* only_if_exists */);
if (propNameAtom == None)
{
return VBoxGuestX11Pointer<unsigned char>(0);
}
Atom actTypeAtom = None;
int actFmt = 0;
unsigned long nBytesAfter = 0;
unsigned char *propVal = 0;
int rc = XGetWindowProperty (aDpy, aWnd, propNameAtom,
0, LONG_MAX, False /* delete */,
aPropType, &actTypeAtom, &actFmt,
nItems, &nBytesAfter, &propVal);
if (rc != Success)
return VBoxGuestX11Pointer<unsigned char>(0);
return VBoxGuestX11Pointer<unsigned char>(propVal);
}
/**
* Initialise the guest and ensure that it is capable of handling seamless mode
*
* @returns true if it can handle seamless, false otherwise
*/
int VBoxGuestSeamlessX11::init(VBoxGuestSeamlessObserver *pObserver)
{
int rc = VINF_SUCCESS;
/** Dummy value for XXGetProperty */
unsigned long nItems;
if (0 != mObserver) /* Assertion */
{
LogRel(("VBoxService: ERROR: attempt to initialise seamless guest object twice!\n"));
return VERR_INTERNAL_ERROR;
}
if (!mDisplay.init())
{
LogRel(("VBoxService: seamless guest object failed to acquire a connection to the display.\n"));
return VERR_ACCESS_DENIED;
}
if (0 == XXGetProperty(mDisplay, DefaultRootWindow(mDisplay.get()), XA_WINDOW,
NET_CLIENT_LIST, &nItems).get())
{
LogRel(("VBoxService: _NET_CLIENT_LIST property not supported by guest window manager. Seamless mode will not be enabled.\n"));
}
mObserver = pObserver;
return rc;
}
/**
* Read information about currently visible windows in the guest and subscribe to X11
* events about changes to this information.
*
* @note This class does not contain its own event thread, so an external thread must
* call nextEvent() for as long as events are wished.
* @todo This function should switch the guest to fullscreen mode.
*/
int VBoxGuestSeamlessX11::start(void)
{
int rc = VINF_SUCCESS;
/** Dummy values for XShapeQueryExtension */
int error, event;
mSupportsShape = XShapeQueryExtension(mDisplay, &event, &error);
mEnabled = true;
monitorClientList();
rebuildWindowTree();
return rc;
}
/** Stop reporting seamless events to the host. Free information about guest windows
and stop requesting updates. */
void VBoxGuestSeamlessX11::stop(void)
{
mEnabled = false;
unmonitorClientList();
freeWindowTree();
}
void VBoxGuestSeamlessX11::monitorClientList(void)
{
XSelectInput(mDisplay, DefaultRootWindow(mDisplay.get()), PropertyChangeMask);
}
void VBoxGuestSeamlessX11::unmonitorClientList(void)
{
XSelectInput(mDisplay, DefaultRootWindow(mDisplay.get()), 0);
}
void VBoxGuestSeamlessX11::rebuildWindowTree(void)
{
VBoxGuestX11Pointer<unsigned char> clientListRaw;
VBoxGuestX11Pointer<Window> clientList;
unsigned long nItems;
freeWindowTree();
clientListRaw = XXGetProperty(mDisplay, DefaultRootWindow(mDisplay.get()), XA_WINDOW,
NET_CLIENT_LIST, &nItems);
clientList = reinterpret_cast<Window *>(clientListRaw.release());
for (unsigned i = 0; i < nItems; ++i)
{
VBoxGuestX11Pointer<unsigned char> windowTypeRaw;
VBoxGuestX11Pointer<Atom> windowType;
unsigned long ulCount;
windowTypeRaw = XXGetProperty(mDisplay, clientList.get()[i], XA_ATOM,
WM_TYPE_PROP, &ulCount);
windowType = reinterpret_cast<Atom *>(windowTypeRaw.release());
if ( (ulCount != 0)
&& (*windowType != XInternAtom(mDisplay, WM_TYPE_DESKTOP_PROP, True)))
{
addClientWindow(clientList.get()[i]);
}
}
}
void VBoxGuestSeamlessX11::addClientWindow(const Window hWin)
{
XWindowAttributes winAttrib;
VBoxGuestX11Pointer<XRectangle> rects;
int cRects = 0, iOrdering;
int x, y;
/** Dummy value for XTranslateCoordinates */
Window dummyWin;
if (!XGetWindowAttributes(mDisplay, hWin, &winAttrib))
{
LogRelFunc(("VBoxService: Failed to get the window attributes for window %d\n", hWin));
return;
}
if (mSupportsShape)
{
XShapeSelectInput(mDisplay, hWin, ShapeNotify);
rects = XShapeGetRectangles(mDisplay, hWin, ShapeClip, &cRects, &iOrdering);
if (0 == rects.get())
{
cRects = 0;
}
}
XSelectInput(mDisplay, hWin, StructureNotifyMask);
XTranslateCoordinates(mDisplay, hWin, DefaultRootWindow(mDisplay.get()), winAttrib.x,
winAttrib.y, &x, &y, &dummyWin);
mGuestWindows.addWindow(hWin, winAttrib.map_state != IsUnmapped, x, y,
winAttrib.width, winAttrib.height, cRects, rects);
}
/**
* Free all information in the tree of visible windows
*/
void VBoxGuestSeamlessX11::freeWindowTree(void)
{
/* We use post-increment in the operation to prevent the iterator from being invalidated. */
for (VBoxGuestWindowList::iterator it = mGuestWindows.begin(); it != mGuestWindows.end();
mGuestWindows.removeWindow(it++))
{
XSelectInput(mDisplay, it->first, 0);
XShapeSelectInput(mDisplay, it->first, 0);
}
}
/**
* Waits for a position or shape-related event from guest windows
*
* @note Called from the guest event thread.
*/
void VBoxGuestSeamlessX11::nextEvent(void)
{
XEvent event;
/* Start by sending information about the current window setup to the host. We do this
here because we want to send all such information from a single thread. */
mObserver->notify();
XNextEvent(mDisplay, &event);
switch (event.type)
{
case ConfigureNotify:
doConfigureEvent(&event.xconfigure);
break;
case MapNotify:
doMapEvent(&event.xmap);
break;
case PropertyNotify:
doPropertyEvent(&event.xproperty);
break;
case ShapeNotify:
doShapeEvent(reinterpret_cast<XShapeEvent *>(&event));
break;
case UnmapNotify:
doUnmapEvent(&event.xunmap);
break;
default:
break;
}
}
/**
* Handle a configuration event in the seamless event thread by setting the new position.
*
* @param event the X11 event structure
*/
void VBoxGuestSeamlessX11::doConfigureEvent(const XConfigureEvent *event)
{
VBoxGuestWindowList::iterator iter;
iter = mGuestWindows.find(event->window);
if (iter != mGuestWindows.end())
{
iter->second->mX = event->x;
iter->second->mY = event->y;
iter->second->mWidth = event->width;
iter->second->mHeight = event->height;
}
}
/**
* Handle a map event in the seamless event thread.
*
* @param event the X11 event structure
*/
void VBoxGuestSeamlessX11::doMapEvent(const XMapEvent *event)
{
VBoxGuestWindowList::iterator iter;
iter = mGuestWindows.find(event->window);
if (iter != mGuestWindows.end())
{
iter->second->mMapped = true;
}
}
/**
* If the list of client windows changes, rebuild the list.
*
* @param event the X11 event structure
*/
void VBoxGuestSeamlessX11::doPropertyEvent(const XPropertyEvent *event)
{
if (XInternAtom(mDisplay, NET_CLIENT_LIST, true) == event->atom)
{
rebuildWindowTree();
}
}
/**
* Handle a window shape change event in the seamless event thread.
*
* @param event the X11 event structure
*/
void VBoxGuestSeamlessX11::doShapeEvent(const XShapeEvent *event)
{
VBoxGuestWindowList::iterator iter;
VBoxGuestX11Pointer<XRectangle> rects;
int cRects, iOrdering;
iter = mGuestWindows.find(event->window);
if (iter != mGuestWindows.end())
{
rects = XShapeGetRectangles(mDisplay, iter->first, ShapeClip, &cRects, &iOrdering);
if (0 == rects.get())
{
cRects = 0;
}
iter->second->mapRects = rects;
iter->second->mcRects = cRects;
}
}
/**
* Handle an unmap event in the seamless event thread.
*
* @param event the X11 event structure
*/
void VBoxGuestSeamlessX11::doUnmapEvent(const XUnmapEvent *event)
{
VBoxGuestWindowList::iterator iter;
iter = mGuestWindows.find(event->window);
if (iter != mGuestWindows.end())
{
iter->second->mMapped = true;
}
}
/**
* Sends an updated list of visible rectangles to the host
*/
std::auto_ptr<std::vector<RTRECT> > VBoxGuestSeamlessX11::getRects(void)
{
unsigned cRects = 0;
std::auto_ptr<std::vector<RTRECT> > apRects(new std::vector<RTRECT>);
if (0 != mcRects)
{
apRects.get()->reserve(mcRects * 2);
}
for (VBoxGuestWindowList::iterator it = mGuestWindows.begin();
it != mGuestWindows.end(); ++it)
{
if (it->second->mMapped)
{
if (it->second->mcRects > 0)
{
for (int i = 0; i < it->second->mcRects; ++i)
{
RTRECT rect;
rect.xLeft = it->second->mX
+ it->second->mapRects.get()[i].x;
rect.yBottom = it->second->mY
+ it->second->mapRects.get()[i].y
+ it->second->mapRects.get()[i].height;
rect.xRight = it->second->mX
+ it->second->mapRects.get()[i].x
+ it->second->mapRects.get()[i].width;
rect.yTop = it->second->mY
+ it->second->mapRects.get()[i].y;
apRects.get()->push_back(rect);
}
cRects += it->second->mcRects;
}
else
{
RTRECT rect;
rect.xLeft = it->second->mX;
rect.yBottom = it->second->mY
+ it->second->mHeight;
rect.xRight = it->second->mX
+ it->second->mWidth;
rect.yTop = it->second->mY;
apRects.get()->push_back(rect);
++cRects;
}
}
}
mcRects = cRects;
return apRects;
}
/**
* Send a client event to wake up the X11 seamless event loop prior to stopping it.
*
* @note This function should only be called from the host event thread.
*/
bool VBoxGuestSeamlessX11::interruptEvent(void)
{
/* Message contents set to zero. */
XClientMessageEvent clientMessage = { ClientMessage, 0, 0, 0, 0, 0, 8 };
if (0 != XSendEvent(mDisplay, DefaultRootWindow(mDisplay.get()), false, PropertyChangeMask,
reinterpret_cast<XEvent *>(&clientMessage)))
{
XFlush(mDisplay);
return true;
}
return false;
}