Index: /trunk/src/VBox/Additions/common/crOpenGL/context.c
===================================================================
--- /trunk/src/VBox/Additions/common/crOpenGL/context.c	(revision 30473)
+++ /trunk/src/VBox/Additions/common/crOpenGL/context.c	(revision 30474)
@@ -152,5 +152,5 @@
 
 GLboolean
-stubIsWindowVisible( const WindowInfo *win )
+stubIsWindowVisible(WindowInfo *win)
 {
 #if defined(WINDOWS)
@@ -159,14 +159,16 @@
     return GL_TRUE;
 #elif defined(GLX)
-    if (win->dpy) {
-    XWindowAttributes attr;
-    XLOCK(win->dpy);
-    XGetWindowAttributes(win->dpy, win->drawable, &attr);
-    XUNLOCK(win->dpy);
-    return (attr.map_state != IsUnmapped);
+    Display *dpy = stubGetWindowDisplay(win);
+    if (dpy) 
+    {
+        XWindowAttributes attr;
+        XLOCK(dpy);
+        XGetWindowAttributes(dpy, win->drawable, &attr);
+        XUNLOCK(dpy);
+        return (attr.map_state != IsUnmapped);
     }
     else {
-    /* probably created by crWindowCreate() */
-    return win->mapped;
+        /* probably created by crWindowCreate() */
+        return win->mapped;
     }
 #endif
@@ -461,5 +463,5 @@
 
 static void
-GetCursorPosition( const WindowInfo *window, int pos[2] )
+GetCursorPosition(WindowInfo *window, int pos[2])
 {
     RECT rect;
@@ -569,25 +571,25 @@
 
 void
-stubGetWindowGeometry( const WindowInfo *window, int *x, int *y,
-                                             unsigned int *w, unsigned int *h )
+stubGetWindowGeometry(WindowInfo *window, int *x, int *y, unsigned int *w, unsigned int *h)
 {
     Window root, child;
     unsigned int border, depth;
+    Display *dpy;
+
+    dpy = stubGetWindowDisplay(window);
 
     //@todo: Performing those checks is expensive operation, especially for simple apps with high FPS.
     //       Disabling those tripples glxgears fps, thus using xevens instead of per frame polling is much more preffered.
     //@todo: Check similiar on windows guests, though doubtfull as there're no XSync like calls on windows.
-    if (window && window->dpy)
+    if (window && dpy)
     {
-        XLOCK(window->dpy);
+        XLOCK(dpy);
     }
 
     if (!window
-        || !window->dpy
+        || !dpy
         || !window->drawable
-        || !XGetGeometry(window->dpy, window->drawable, &root,
-                         x, y, w, h, &border, &depth)
-        || !XTranslateCoordinates(window->dpy, window->drawable, root,
-                                  0, 0, x, y, &child)) 
+        || !XGetGeometry(dpy, window->drawable, &root, x, y, w, h, &border, &depth)
+        || !XTranslateCoordinates(dpy, window->drawable, root, 0, 0, x, y, &child)) 
     {
         crWarning("Failed to get windows geometry for %p, try xwininfo", window);
@@ -596,7 +598,7 @@
     }
 
-    if (window && window->dpy)
+    if (window && dpy)
     {
-        XUNLOCK(window->dpy);
+        XUNLOCK(dpy);
     }
 }
@@ -650,5 +652,5 @@
  */
 static void
-GetCursorPosition( const WindowInfo *window, int pos[2] )
+GetCursorPosition(WindowInfo *window, int pos[2] )
 {
     int rootX, rootY;
@@ -1078,5 +1080,5 @@
 
 void
-stubSwapBuffers( const WindowInfo *window, GLint flags )
+stubSwapBuffers(WindowInfo *window, GLint flags)
 {
     if (!window)
Index: /trunk/src/VBox/Additions/common/crOpenGL/glx.c
===================================================================
--- /trunk/src/VBox/Additions/common/crOpenGL/glx.c	(revision 30473)
+++ /trunk/src/VBox/Additions/common/crOpenGL/glx.c	(revision 30474)
@@ -1034,5 +1034,5 @@
 DECLEXPORT(void) VBOXGLXTAG(glXSwapBuffers)( Display *dpy, GLXDrawable drawable )
 {
-    const WindowInfo *window = stubGetWindowInfo(dpy, drawable);
+    WindowInfo *window = stubGetWindowInfo(dpy, drawable);
     stubSwapBuffers( window, 0 );
 }
Index: /trunk/src/VBox/Additions/common/crOpenGL/load.c
===================================================================
--- /trunk/src/VBox/Additions/common/crOpenGL/load.c	(revision 30473)
+++ /trunk/src/VBox/Additions/common/crOpenGL/load.c	(revision 30474)
@@ -143,12 +143,15 @@
     int x, y;
     unsigned int border, depth, w, h;
-
-    XLOCK(pWindow->dpy);
-    if (!XGetGeometry(pWindow->dpy, pWindow->drawable, &root, &x, &y, &w, &h, &border, &depth))
-    {
-        XUNLOCK(pWindow->dpy);
+    Display *dpy;
+
+    dpy = stubGetWindowDisplay(pWindow);
+
+    XLOCK(dpy);
+    if (!XGetGeometry(dpy, pWindow->drawable, &root, &x, &y, &w, &h, &border, &depth))
+    {
+        XUNLOCK(dpy);
         return false;
     }
-    XUNLOCK(pWindow->dpy);
+    XUNLOCK(dpy);
 #endif
 
@@ -782,5 +785,5 @@
             crNetFreeConnection(ns.conn);
         }
-#ifdef CR_NEWWINTRACK
+#if 0 && defined(CR_NEWWINTRACK)
         {
             Status st = XInitThreads();
Index: /trunk/src/VBox/Additions/common/crOpenGL/stub.c
===================================================================
--- /trunk/src/VBox/Additions/common/crOpenGL/stub.c	(revision 30473)
+++ /trunk/src/VBox/Additions/common/crOpenGL/stub.c	(revision 30474)
@@ -9,4 +9,5 @@
 #include "cr_mem.h" 
 #include "stub.h"
+#include <iprt/thread.h>
 
 #ifdef GLX
@@ -28,4 +29,31 @@
 }
 
+Display* stubGetWindowDisplay(WindowInfo *pWindow)
+{
+#ifdef CR_NEWWINTRACK
+    if (RTThreadNativeSelf()==RTThreadGetNative(stub.hSyncThread))
+    {
+        if (pWindow && pWindow->dpy && !pWindow->syncDpy)
+        {
+            crDebug("going to XOpenDisplay(%s)", pWindow->dpyName);
+            pWindow->syncDpy = XOpenDisplay(pWindow->dpyName);
+            if (!pWindow->syncDpy)
+            {
+                crWarning("Failed to open display %s", pWindow->dpyName);
+            }
+            return pWindow->syncDpy;
+        }
+        else
+        {
+            return pWindow ? pWindow->syncDpy:NULL;
+        }
+    }
+    else
+#endif
+    {
+        return pWindow ? pWindow->dpy:NULL;
+    }
+}
+
 /**
  * Returns -1 on error
@@ -81,5 +109,5 @@
 void APIENTRY crSwapBuffers( GLint window, GLint flags )
 {
-    const WindowInfo *winInfo = (const WindowInfo *)
+    WindowInfo *winInfo = (WindowInfo *)
         crHashtableSearch(stub.windowTable, (unsigned int) window);
     if (winInfo)
@@ -100,6 +128,10 @@
     WindowInfo *winInfo = (WindowInfo *)
         crHashtableSearch(stub.windowTable, (unsigned int) window);
-    if (winInfo && winInfo->type == CHROMIUM && stub.spu) {
+    if (winInfo && winInfo->type == CHROMIUM && stub.spu)
+    {
         stub.spu->dispatch_table.WindowDestroy( winInfo->spuWindow );
+#ifdef CR_NEWWINTRACK
+        crLockMutex(&stub.mutex);
+#endif
 #ifdef WINDOWS
         if (winInfo->hVisibleRegion != INVALID_HANDLE_VALUE)
@@ -112,7 +144,15 @@
             XFree(winInfo->pVisibleRegions);
         }
+# ifdef CR_NEWWINTRACK
+        if (winInfo->syncDpy)
+        {
+            XCloseDisplay(winInfo->syncDpy);
+        }
+# endif
+#endif
+#ifdef CR_NEWWINTRACK
+        crUnlockMutex(&stub.mutex);
 #endif
         crForcedFlush();
-
         crHashtableDelete(stub.windowTable, window, crFree);
     }
@@ -446,4 +486,5 @@
     XRectangle *pXRects;
     GLint* pGLRects;
+    Display *dpy;
 
     if (bExtensionsChecked || stubCheckXExtensions(pWindow))
@@ -456,13 +497,15 @@
         return GL_FALSE;
     }
+
+    dpy = stubGetWindowDisplay(pWindow);
 
     /*@todo see comment regarding size/position updates and XSync, same applies to those functions but
     * it seems there's no way to get even based updates for this. Or I've failed to find the appropriate extension.
     */
-    XLOCK(pWindow->dpy);
-    xreg = XCompositeCreateRegionFromBorderClip(pWindow->dpy, pWindow->drawable);
-    pXRects = XFixesFetchRegion(pWindow->dpy, xreg, &cRects);
-    XFixesDestroyRegion(pWindow->dpy, xreg);
-    XUNLOCK(pWindow->dpy);
+    XLOCK(dpy);
+    xreg = XCompositeCreateRegionFromBorderClip(dpy, pWindow->drawable);
+    pXRects = XFixesFetchRegion(dpy, xreg, &cRects);
+    XFixesDestroyRegion(dpy, xreg);
+    XUNLOCK(dpy);
 
     /* @todo For some odd reason *first* run of compiz on freshly booted VM gives us 0 cRects all the time.
Index: /trunk/src/VBox/Additions/common/crOpenGL/stub.h
===================================================================
--- /trunk/src/VBox/Additions/common/crOpenGL/stub.h	(revision 30473)
+++ /trunk/src/VBox/Additions/common/crOpenGL/stub.h	(revision 30474)
@@ -46,6 +46,6 @@
 #endif
 
-#ifdef WINDOWS
-#define CR_NEWWINTRACK
+#if defined(WINDOWS) || defined(Linux)
+# define CR_NEWWINTRACK
 #endif
 
@@ -54,5 +54,5 @@
 #endif
 
-#if defined(CR_NEWWINTRACK) && !defined(WINDOWS)
+#if 0 && defined(CR_NEWWINTRACK) && !defined(WINDOWS)
 #define XLOCK(dpy) XLockDisplay(dpy)
 #define XUNLOCK(dpy) XUnlockDisplay(dpy)
@@ -166,4 +166,7 @@
 #elif defined(GLX)
     Display *dpy;
+# ifdef CR_NEWWINTRACK
+    Display *syncDpy;
+# endif
     GLXDrawable drawable;
     XRectangle *pVisibleRegions;
@@ -279,4 +282,5 @@
 extern WindowInfo *stubGetWindowInfo( Display *dpy, GLXDrawable drawable );
 extern void stubUseXFont( Display *dpy, Font font, int first, int count, int listbase );
+extern Display* stubGetWindowDisplay(WindowInfo *pWindow);
 
 #endif
@@ -287,8 +291,8 @@
 extern GLboolean stubMakeCurrent( WindowInfo *window, ContextInfo *context );
 extern GLint stubNewWindow( const char *dpyName, GLint visBits );
-extern void stubSwapBuffers( const WindowInfo *window, GLint flags );
-extern void stubGetWindowGeometry( const WindowInfo *win, int *x, int *y, unsigned int *w, unsigned int *h );
+extern void stubSwapBuffers(WindowInfo *window, GLint flags);
+extern void stubGetWindowGeometry(WindowInfo *win, int *x, int *y, unsigned int *w, unsigned int *h);
 extern GLboolean stubUpdateWindowGeometry(WindowInfo *pWindow, GLboolean bForceUpdate);
-extern GLboolean stubIsWindowVisible( const WindowInfo *win );
+extern GLboolean stubIsWindowVisible(WindowInfo *win);
 extern bool stubInit(void);
 
