-sync command line option now causes X server to operate in synchronized/blocking...
[freeglut] / src / x11 / fg_init_x11.c
index 83fe287..3adf4e4 100644 (file)
@@ -29,7 +29,9 @@
 #define FREEGLUT_BUILDING_LIB
 #include <limits.h>  /* LONG_MAX */
 #include <GL/freeglut.h>
-#include "../fg_internal.h"
+#include "fg_internal.h"
+#include "fg_init.h"
+#include "egl/fg_init_egl.h"
 
 /* Return the atom associated with "name". */
 static Atom fghGetAtom(const char * name)
@@ -80,9 +82,9 @@ static int fghGetWindowProperty(Window window,
                               "fghGetWindowProperty");
 
   if (type_returned != type)
-    {
-      number_of_elements = 0;
-    }
+  {
+    number_of_elements = 0;
+  }
 
   return number_of_elements;
 }
@@ -111,30 +113,30 @@ static int fghNetWMSupported(void)
                                            XA_WINDOW,
                                            (unsigned char **) window_ptr_1);
   if (number_of_windows == 1)
-    {
-      Window ** window_ptr_2;
-
-      window_ptr_2 = malloc(sizeof(Window *));
+  {
+    Window ** window_ptr_2;
 
-      /* Check that the window has the same property set to the same value. */
-      number_of_windows = fghGetWindowProperty(**window_ptr_1,
-                                               wm_check,
-                                               XA_WINDOW,
-                                               (unsigned char **) window_ptr_2);
-      if ((number_of_windows == 1) && (**window_ptr_1 == **window_ptr_2))
-      {
-        /* NET WM compliant */
-        net_wm_supported = 1;
-      }
+    window_ptr_2 = malloc(sizeof(Window *));
 
-      XFree(*window_ptr_2);
-      free(window_ptr_2);
+    /* Check that the window has the same property set to the same value. */
+    number_of_windows = fghGetWindowProperty(**window_ptr_1,
+                                             wm_check,
+                                             XA_WINDOW,
+                                             (unsigned char **) window_ptr_2);
+    if ((number_of_windows == 1) && (**window_ptr_1 == **window_ptr_2))
+    {
+      /* NET WM compliant */
+      net_wm_supported = 1;
     }
 
-        XFree(*window_ptr_1);
-        free(window_ptr_1);
+    XFree(*window_ptr_2);
+    free(window_ptr_2);
+  }
+
+  XFree(*window_ptr_1);
+  free(window_ptr_1);
 
-        return net_wm_supported;
+  return net_wm_supported;
 }
 
 /*  Check if "hint" is present in "property" for "window". */
@@ -174,10 +176,25 @@ void fgPlatformInitialize( const char* displayName )
     if( fgDisplay.pDisplay.Display == NULL )
         fgError( "failed to open display '%s'", XDisplayName( displayName ) );
 
+    if ( fgState.XSyncSwitch )
+        XSynchronize(fgDisplay.pDisplay.Display, True);
+
+#ifdef EGL_VERSION_1_0
+    fghPlatformInitializeEGL();
+#else
     if( !glXQueryExtension( fgDisplay.pDisplay.Display, NULL, NULL ) )
         fgError( "OpenGL GLX extension not supported by display '%s'",
             XDisplayName( displayName ) );
 
+    /* This forces AMD Catalyst drivers to initialize and register a shutdown
+     * function, which must be done before our own call to atexit to prevent
+     * a crash if glutMainLoop is not called or is not exited cleanly.
+     * (see bug #206)
+     */
+    glXQueryExtensionsString( fgDisplay.pDisplay.Display,
+        DefaultScreen( fgDisplay.pDisplay.Display ));
+#endif
+
     fgDisplay.pDisplay.Screen = DefaultScreen( fgDisplay.pDisplay.Display );
     fgDisplay.pDisplay.RootWindow = RootWindow(
         fgDisplay.pDisplay.Display,
@@ -210,8 +227,12 @@ void fgPlatformInitialize( const char* displayName )
     /* Create the state and full screen atoms */
     fgDisplay.pDisplay.State           = None;
     fgDisplay.pDisplay.StateFullScreen = None;
+    fgDisplay.pDisplay.NetWMPid        = None;
+    fgDisplay.pDisplay.ClientMachine   = None;
 
-    if (fghNetWMSupported())
+    fgDisplay.pDisplay.NetWMSupported = fghNetWMSupported();
+
+    if (fgDisplay.pDisplay.NetWMSupported)
     {
       const Atom supported = fghGetAtom("_NET_SUPPORTED");
       const Atom state     = fghGetAtom("_NET_WM_STATE");
@@ -230,8 +251,14 @@ void fgPlatformInitialize( const char* displayName )
           fgDisplay.pDisplay.StateFullScreen = full_screen;
         }
       }
+
+      fgDisplay.pDisplay.NetWMPid = fghGetAtom("_NET_WM_PID");
+      fgDisplay.pDisplay.ClientMachine = fghGetAtom("WM_CLIENT_MACHINE");
     }
 
+    /* Get start time */
+    fgState.Time = fgSystemTime();
+    
 
     fgState.Initialised = GL_TRUE;
 
@@ -266,8 +293,10 @@ void fgPlatformCloseDisplay ( void )
 }
 
 
+#ifndef EGL_VERSION_1_0
 void fgPlatformDestroyContext ( SFG_PlatformDisplay pDisplay, SFG_WindowContextType MContext )
 {
     /* Note that the MVisualInfo is not owned by the MenuContext! */
     glXDestroyContext( pDisplay.Display, MContext );
 }
+#endif