Index: trunk/src/VBox/Additions/common/VBoxGuestLib/Makefile.kmk
===================================================================
--- trunk/src/VBox/Additions/common/VBoxGuestLib/Makefile.kmk	(revision 6531)
+++ trunk/src/VBox/Additions/common/VBoxGuestLib/Makefile.kmk	(revision 6538)
@@ -48,5 +48,6 @@
   endif
    LIBRARIES += \
-  	VBoxGuestR3LibLinux
+  	VBoxGuestR3LibLinux \
+  	VBoxGuestR3LibLinuxXFree86
  endif
  ifdef VBOX_WITH_OS2_ADDITIONS
@@ -92,5 +93,5 @@
 
 #
-# VBoxGuestR3LibBase
+# VBoxGuestR3Lib
 #
 VBoxGuestR3Lib_TEMPLATE    = VBOXGUESTR3LIB
@@ -107,4 +108,15 @@
 	VBoxGuestR3LibVideo.cpp
 
+#
+# VBoxGuestR3LibXFree86 - a reduced version of the guest library which uses
+# the X server runtime instead of iprt, for use with old servers where the
+# C library is not available.
+#
+VBoxGuestR3LibXFree86_SOURCES    = \
+	VBoxGuestR3Lib.cpp \
+	VBoxGuestR3LibGR.cpp \
+	VBoxGuestR3LibMouse.cpp \
+	VBoxGuestR3LibMisc.cpp \
+	VBoxGuestR3LibVideo.cpp
 
 
@@ -154,9 +166,22 @@
 
 #
-# VBoxGuestR3LibBaseLinux
+# VBoxGuestR3LibLinux
 #
 VBoxGuestR3LibLinux_TEMPLATE = VBOXLNX32GUESTR3LIB
 VBoxGuestR3LibLinux_SOURCES  = $(VBoxGuestR3Lib_SOURCES)
 VBoxGuestR3LibLinux_DEFS     = VBOX_HGCM
+
+
+#
+# VBoxGuestR3LibLinuxXFree86
+#
+VBoxGuestR3LibLinuxXFree86_TEMPLATE = VBOXLNX32GUESTR3LIB
+VBoxGuestR3LibLinuxXFree86_SOURCES  = $(VBoxGuestR3LibXFree86_SOURCES)
+VBoxGuestR3LibLinuxXFree86_DEFS     = VBOX_HGCM VBOX_VBGLR3_XFREE86
+VBoxGuestR3LibLinuxXFree86_INCS     = \
+        ../../x11/x11include/4.2/programs/Xserver/hw/xfree86/common/ \
+        ../../x11/x11include/4.2/programs/Xserver/hw/xfree86/os-support \
+        ../../x11/x11include/4.2/programs/Xserver/include \
+        ../../x11/x11include/4.2/exports/include/X11
 
 
Index: trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3Lib.cpp
===================================================================
--- trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3Lib.cpp	(revision 6531)
+++ trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3Lib.cpp	(revision 6538)
@@ -34,8 +34,27 @@
 #include <iprt/asm.h>
 #include <iprt/string.h>
-#include <iprt/file.h>
-#include <iprt/assert.h>
-#include <iprt/mem.h>
-#include <iprt/alloca.h>
+#ifdef VBOX_VBGLR3_XFREE86
+/* Definitions for X server library functions such as xf86open and mappings to C functions. */
+/* Make the headers C++-compatible */
+# define class xf86_vbox_class
+# define bool xf86_vbox_bool
+# define private xf86_vbox_private
+# define new xf86_vbox_new
+extern "C"
+{
+# include "xf86.h"
+# include "xf86_OSproc.h"
+# include "xf86Resources.h"
+# include "xf86_ansic.h"
+}
+# undef class
+# undef bool
+# undef private
+# undef new
+#else
+# include <iprt/file.h>
+# include <iprt/assert.h>
+# include <iprt/thread.h>
+#endif
 #include <VBox/VBoxGuest.h>
 #include "VBGLR3Internal.h"
@@ -46,11 +65,38 @@
 *******************************************************************************/
 /** The VBoxGuest device handle. */
+#ifdef VBOX_VBGLR3_XFREE86
+static int g_File = -1;
+#else
 static RTFILE g_File = NIL_RTFILE;
-
+#endif
+/**
+ * A counter of the number of times the library has been initialised, for use with
+ * X.org drivers, where the library may be shared by multiple independant modules
+ * inside a single process space.
+ */
+static uint32_t g_cInits = 0;
 
 VBGLR3DECL(int) VbglR3Init(void)
 {
-    if (g_File != NIL_RTFILE)
+    uint32_t cInits = ASMAtomicIncU32(&g_cInits);
+#ifdef VBOX_VBGLR3_XFREE86
+    if (1 != cInits)
         return VINF_SUCCESS;
+    if (-1 != g_File)
+        return VERR_INTERNAL_ERROR;
+#else
+    Assert(cInits > 0);
+    if (1 > cInits)
+    {
+        /* This will not work when the library is shared inside a multi-threaded
+           process.  Hopefully no-one will try that, as we can't use the threads
+           APIs here. */
+        if (NIL_RTFILE == g_File)
+            return VERR_INTERNAL_ERROR;
+        return VINF_SUCCESS;
+    }
+    if (NIL_RTFILE != g_File)
+        return VERR_INTERNAL_ERROR;
+#endif
 
 #if defined(RT_OS_OS2)
@@ -105,5 +151,12 @@
     g_File = hf;
 
-     /* PORTME */
+#elif defined(VBOX_VBGLR3_XFREE86)
+    int File = open(VBOXGUEST_DEVICE_NAME, O_RDWR);
+    if (-1 == File)
+    {
+        return VERR_UNRESOLVED_ERROR;
+    }
+    g_File = File;
+
 #else
     /* the default implemenation. (linux, solaris) */
@@ -122,11 +175,29 @@
 VBGLR3DECL(void) VbglR3Term(void)
 {
+    uint32_t cInits = ASMAtomicDecU32(&g_cInits);
+    if (cInits > 0)
+        return;
+#ifndef VBOX_VBGLR3_XFREE86
+    AssertReturnVoid(0 == cInits);
     RTFILE File = g_File;
     g_File = NIL_RTFILE;
-    if (File == NIL_RTFILE)
+    AssertReturnVoid(NIL_RTFILE != File);
+#else
+    int File = g_File;
+    g_File = -1;
+    if (-1 == File)
         return;
+#endif
 #if defined(RT_OS_OS2)
     APIRET rc = DosClose(File);
     AssertMsg(!rc, ("%ld\n", rc));
+#elif defined(VBOX_VBGLR3_XFREE86)
+    /* if (-1 == close(File))
+    {
+        int iErr = errno;
+        AssertRC(RTErrConvertFromErrno(iErr));
+    } */
+    close(File);  /* iprt is not available here. */
+    File = -1;
 #else
     int rc = RTFileClose(File);
@@ -171,9 +242,21 @@
     int rc = ioctl((int)g_File, iFunction, &Hdr);
     if (rc == -1)
+    {
         rc = errno;
-    return rc;
+        return RTErrConvertFromErrno(rc);
+    }
+    return VINF_SUCCESS;
 
 #else
     /* Default implementation - PORTME: Do not use this without testings that error passing works! */
+# ifdef VBOX_VBGLR3_XFREE86
+    int rc = ioctl(g_File, (int) iFunction, pvData);
+    if (rc == -1)
+    {
+        rc = errno;
+        return RTErrConvertFromErrno(rc);
+    }
+    return VINF_SUCCESS;
+# else
     int rc2 = VERR_INTERNAL_ERROR;
     int rc = RTFileIoCtl(g_File, (int)iFunction, pvData, cbData, &rc2);
@@ -181,5 +264,6 @@
         rc = rc2;
     return rc;
-#endif
-}
-
+# endif
+#endif
+}
+
Index: trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibGR.cpp
===================================================================
--- trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibGR.cpp	(revision 6531)
+++ trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibGR.cpp	(revision 6538)
@@ -20,7 +20,27 @@
 *   Header Files                                                               *
 *******************************************************************************/
-#include <iprt/mem.h>
-#include <iprt/assert.h>
-#include <iprt/string.h>
+#ifdef VBOX_VBGLR3_XFREE86
+/* For the definitions of standard X server library functions like xalloc. */
+/* Make the headers C++-compatible */
+# define class xf86_vbox_class
+# define bool xf86_vbox_bool
+# define private xf86_vbox_private
+# define new xf86_vbox_new
+extern "C"
+{
+# include "xf86.h"
+# include "xf86_OSproc.h"
+# include "xf86Resources.h"
+# include "xf86_ansic.h"
+}
+# undef class
+# undef bool
+# undef private
+# undef new
+#else
+# include <iprt/mem.h>
+# include <iprt/assert.h>
+# include <iprt/string.h>
+#endif
 #include <iprt/err.h>
 #include <VBox/VBoxGuest.h>
@@ -32,4 +52,7 @@
     VMMDevRequestHeader *pReq;
 
+#ifdef VBOX_VBGLR3_XFREE86
+    pReq = (VMMDevRequestHeader *)xalloc(cb);
+#else
     AssertPtrReturn(ppReq, VERR_INVALID_PARAMETER);
     AssertMsgReturn(cb >= sizeof(VMMDevRequestHeader), ("%#x vs %#zx\n", cb, sizeof(VMMDevRequestHeader)),
@@ -37,4 +60,5 @@
 
     pReq = (VMMDevRequestHeader *)RTMemTmpAlloc(cb);
+#endif
     if (RT_UNLIKELY(!pReq))
         return VERR_NO_MEMORY;
@@ -61,5 +85,9 @@
 void vbglR3GRFree(VMMDevRequestHeader *pReq)
 {
+#ifdef VBOX_VBGLR3_XFREE86
+    xfree(pReq);
+#else
     RTMemTmpFree(pReq);
+#endif
 }
 
Index: trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibMisc.cpp
===================================================================
--- trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibMisc.cpp	(revision 6531)
+++ trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibMisc.cpp	(revision 6538)
@@ -20,5 +20,7 @@
 *   Header Files                                                               *
 *******************************************************************************/
-#include <iprt/assert.h>
+#ifndef VBOX_VBGLR3_XFREE86
+# include <iprt/assert.h>
+#endif
 #include "VBGLR3Internal.h"
 
