Leave game mode on "deinitialization" if we entered it, per e-mail from John Tsiombik...
[freeglut] / src / freeglut_state.c
index 2b683e1..9e3a627 100644 (file)
 static int fghGetConfig( int attribute )
 {
   int returnValue = 0;
+  int result;  /*  Not checked  */
 
   if( fgStructure.CurrentWindow )
-      glXGetConfig( fgDisplay.Display, fgStructure.CurrentWindow->Window.VisualInfo,
-                    attribute, &returnValue );
+      result = glXGetFBConfigAttrib( fgDisplay.Display,
+                                     *(fgStructure.CurrentWindow->Window.FBConfig),
+                                     attribute,
+                                     &returnValue );
 
   return returnValue;
 }
@@ -113,6 +116,14 @@ void FGAPIENTRY glutSetOption( GLenum eWhat, int value )
             fgStructure.CurrentWindow->State.Cursor = value;
         break;
 
+    case GLUT_AUX:
+      fgState.AuxiliaryBufferNumber = value;
+      break;
+
+    case GLUT_MULTISAMPLE:
+      fgState.SampleNumber = value;
+      break;
+
     default:
         fgWarning( "glutSetOption(): missing enum handle %d", eWhat );
         break;
@@ -168,6 +179,10 @@ int FGAPIENTRY glutGet( GLenum eWhat )
     case GLUT_INIT_WINDOW_HEIGHT:   return fgState.Size.Use ?
                                            fgState.Size.Y : -1     ;
     case GLUT_INIT_DISPLAY_MODE:    return fgState.DisplayMode     ;
+    case GLUT_INIT_MAJOR_VERSION:   return fgState.MajorVersion    ;
+    case GLUT_INIT_MINOR_VERSION:   return fgState.MinorVersion    ;
+    case GLUT_INIT_FLAGS:           return fgState.ContextFlags    ;
+    case GLUT_INIT_PROFILE:         return fgState.ContextProfile  ;
 
 #if TARGET_HOST_POSIX_X11
     /*
@@ -213,7 +228,20 @@ int FGAPIENTRY glutGet( GLenum eWhat )
              */
             return 0;
         }
-        return fgStructure.CurrentWindow->Window.VisualInfo->visual->map_entries;
+        else
+        {
+          const GLXFBConfig * fbconfig =
+                fgStructure.CurrentWindow->Window.FBConfig;
+
+          XVisualInfo * visualInfo =
+                glXGetVisualFromFBConfig( fgDisplay.Display, *fbconfig );
+
+          const int result = visualInfo->visual->map_entries;
+
+          XFree(visualInfo);
+
+          return result;
+        }
 
     /*
      * Those calls are somewhat similiar, as they use XGetWindowAttributes()
@@ -278,13 +306,23 @@ int FGAPIENTRY glutGet( GLenum eWhat )
     /* I do not know yet if there will be a fgChooseVisual() function for Win32 */
     case GLUT_DISPLAY_MODE_POSSIBLE:
     {
-        XVisualInfo* visualInfo = fgChooseVisual();
-        if ( visualInfo == NULL ) {
-            return 0;
-        } else {
-            XFree( visualInfo );
-            return 1;
+        /*  We should not have to call fgChooseFBConfig again here.  */
+        GLXFBConfig * fbconfig;
+        int isPossible;
+
+        fbconfig = fgChooseFBConfig(NULL);
+
+        if (fbconfig == NULL)
+        {
+            isPossible = 0;
+        }
+        else
+        {
+            isPossible = 1;
+            XFree(fbconfig);
         }
+
+        return isPossible;
     }
 
     /* This is system-dependant */
@@ -292,7 +330,7 @@ int FGAPIENTRY glutGet( GLenum eWhat )
         if( fgStructure.CurrentWindow == NULL )
             return 0;
 
-        return fgStructure.CurrentWindow->Window.VisualInfo->visualid;
+        return fghGetConfig( GLX_VISUAL_ID );
 
 #elif TARGET_HOST_MS_WINDOWS
 
@@ -302,16 +340,28 @@ int FGAPIENTRY glutGet( GLenum eWhat )
 
     /* Handle the OpenGL inquiries */
     case GLUT_WINDOW_RGBA:
+#if defined(_WIN32_WCE)
+      boolValue = (GLboolean)0;  /* WinCE doesn't support this feature */
+#else
       glGetBooleanv ( GL_RGBA_MODE, &boolValue );
       returnValue = boolValue ? 1 : 0;
+#endif
       return returnValue;
     case GLUT_WINDOW_DOUBLEBUFFER:
+#if defined(_WIN32_WCE)
+      boolValue = (GLboolean)0;  /* WinCE doesn't support this feature */
+#else
       glGetBooleanv ( GL_DOUBLEBUFFER, &boolValue );
       returnValue = boolValue ? 1 : 0;
+#endif
       return returnValue;
     case GLUT_WINDOW_STEREO:
+#if defined(_WIN32_WCE)
+      boolValue = (GLboolean)0;  /* WinCE doesn't support this feature */
+#else
       glGetBooleanv ( GL_STEREO, &boolValue );
       returnValue = boolValue ? 1 : 0;
+#endif
       return returnValue;
 
     case GLUT_WINDOW_RED_SIZE:
@@ -327,16 +377,32 @@ int FGAPIENTRY glutGet( GLenum eWhat )
       glGetIntegerv ( GL_ALPHA_BITS, &returnValue );
       return returnValue;
     case GLUT_WINDOW_ACCUM_RED_SIZE:
+#if defined(_WIN32_WCE)
+      returnValue = 0;  /* WinCE doesn't support this feature */
+#else
       glGetIntegerv ( GL_ACCUM_RED_BITS, &returnValue );
+#endif
       return returnValue;
     case GLUT_WINDOW_ACCUM_GREEN_SIZE:
+#if defined(_WIN32_WCE)
+      returnValue = 0;  /* WinCE doesn't support this feature */
+#else
       glGetIntegerv ( GL_ACCUM_GREEN_BITS, &returnValue );
+#endif
       return returnValue;
     case GLUT_WINDOW_ACCUM_BLUE_SIZE:
+#if defined(_WIN32_WCE)
+      returnValue = 0;  /* WinCE doesn't support this feature */
+#else
       glGetIntegerv ( GL_ACCUM_BLUE_BITS, &returnValue );
+#endif
       return returnValue;
     case GLUT_WINDOW_ACCUM_ALPHA_SIZE:
+#if defined(_WIN32_WCE)
+      returnValue = 0;  /* WinCE doesn't support this feature */
+#else
       glGetIntegerv ( GL_ACCUM_ALPHA_BITS, &returnValue );
+#endif
       return returnValue;
     case GLUT_WINDOW_DEPTH_SIZE:
       glGetIntegerv ( GL_DEPTH_BITS, &returnValue );
@@ -382,24 +448,11 @@ int FGAPIENTRY glutGet( GLenum eWhat )
 
         freeglut_return_val_if_fail( fgStructure.CurrentWindow != NULL, 0 );
 
-        /*
-         * We need to call GetWindowRect() first...
-         *  (this returns the pixel coordinates of the outside of the window)
-         */
+#if defined(_WIN32_WCE)
         GetWindowRect( fgStructure.CurrentWindow->Window.Handle, &winRect );
-
-        /* ...then we've got to correct the results we've just received... */
-
-#if !defined(_WIN32_WCE)
-        if ( ( fgStructure.GameModeWindow != fgStructure.CurrentWindow ) && ( fgStructure.CurrentWindow->Parent == NULL ) &&
-             ( ! fgStructure.CurrentWindow->IsMenu ) )
-        {
-          winRect.left   += GetSystemMetrics( SM_CXSIZEFRAME );
-          winRect.right  -= GetSystemMetrics( SM_CXSIZEFRAME );
-          winRect.top    += GetSystemMetrics( SM_CYSIZEFRAME ) + GetSystemMetrics( SM_CYCAPTION );
-          winRect.bottom -= GetSystemMetrics( SM_CYSIZEFRAME );
-        }
-#endif /* !defined(_WIN32_WCE) */
+#else
+        winRect = fghGetClientArea(fgStructure.CurrentWindow, FALSE);
+#endif /* defined(_WIN32_WCE) */
 
         switch( eWhat )
         {
@@ -412,17 +465,32 @@ int FGAPIENTRY glutGet( GLenum eWhat )
     break;
 
     case GLUT_WINDOW_BORDER_WIDTH :
-#if defined(_WIN32_WCE)
-        return 0;
-#else
-        return GetSystemMetrics( SM_CXSIZEFRAME );
-#endif /* !defined(_WIN32_WCE) */
-
     case GLUT_WINDOW_HEADER_HEIGHT :
 #if defined(_WIN32_WCE)
         return 0;
 #else
-        return GetSystemMetrics( SM_CYCAPTION );
+        {
+            DWORD windowStyle;
+
+            if (fgStructure.CurrentWindow && fgStructure.CurrentWindow->Window.Handle)
+                windowStyle = GetWindowLong(fgStructure.CurrentWindow->Window.Handle, GWL_STYLE);
+            else
+                /* If no window, return sizes for a default window with title bar and border */
+                windowStyle = WS_OVERLAPPEDWINDOW;
+            
+            switch( eWhat )
+            {
+            case GLUT_WINDOW_BORDER_WIDTH:
+                {
+                    int xBorderWidth, yBorderWidth;
+                    fghGetBorderWidth(windowStyle, &xBorderWidth, &yBorderWidth);
+                    return xBorderWidth;
+                }
+            case GLUT_WINDOW_HEADER_HEIGHT:
+                /* Need to query for WS_MAXIMIZEBOX to see if we have a title bar, the WS_CAPTION query is also true for a WS_DLGFRAME only... */
+                return (windowStyle & WS_MAXIMIZEBOX)? GetSystemMetrics( SM_CYCAPTION ) : 0;
+            }
+        }
 #endif /* defined(_WIN32_WCE) */
 
     case GLUT_DISPLAY_MODE_POSSIBLE:
@@ -476,7 +544,15 @@ int FGAPIENTRY glutGet( GLenum eWhat )
 
     case GLUT_DIRECT_RENDERING:
         return fgState.DirectContext;
-        break;
+
+    case GLUT_FULL_SCREEN:
+        return fgStructure.CurrentWindow->State.IsFullscreen;
+
+    case GLUT_AUX:
+      return fgState.AuxiliaryBufferNumber;
+
+    case GLUT_MULTISAMPLE:
+      return fgState.SampleNumber;
 
     default:
         fgWarning( "glutGet(): missing enum handle %d", eWhat );
@@ -577,10 +653,14 @@ int FGAPIENTRY glutDeviceGet( GLenum eWhat )
         return 0;
 
     case GLUT_HAS_SPACEBALL:
+        return fgHasSpaceball();
+
     case GLUT_HAS_TABLET:
         return 0;
 
     case GLUT_NUM_SPACEBALL_BUTTONS:
+        return fgSpaceballNumButtons();
+
     case GLUT_NUM_TABLET_BUTTONS:
         return 0;
 
@@ -694,4 +774,114 @@ int FGAPIENTRY glutLayerGet( GLenum eWhat )
     return -1;
 }
 
+int * FGAPIENTRY glutGetModeValues(GLenum eWhat, int * size)
+{
+  int * array;
+
+#if TARGET_HOST_POSIX_X11
+  int attributes[9];
+  GLXFBConfig * fbconfigArray;  /*  Array of FBConfigs  */
+  int fbconfigArraySize;        /*  Number of FBConfigs in the array  */
+  int attribute_name = 0;
+#endif
+
+  FREEGLUT_EXIT_IF_NOT_INITIALISED("glutGetModeValues");
+
+  array = NULL;
+  *size = 0;
+
+  switch (eWhat)
+    {
+#if TARGET_HOST_POSIX_X11
+    case GLUT_AUX:
+    case GLUT_MULTISAMPLE:
+
+      attributes[0] = GLX_BUFFER_SIZE;
+      attributes[1] = GLX_DONT_CARE;
+
+      switch (eWhat)
+        {
+        case GLUT_AUX:
+          /*
+            FBConfigs are now sorted by increasing number of auxiliary
+            buffers.  We want at least one buffer.
+          */
+          attributes[2] = GLX_AUX_BUFFERS;
+          attributes[3] = 1;
+          attributes[4] = None;
+
+          attribute_name = GLX_AUX_BUFFERS;
+
+          break;
+
+
+        case GLUT_MULTISAMPLE:
+          attributes[2] = GLX_AUX_BUFFERS;
+          attributes[3] = GLX_DONT_CARE;
+          attributes[4] = GLX_SAMPLE_BUFFERS;
+          attributes[5] = 1;
+          /*
+            FBConfigs are now sorted by increasing number of samples per
+            pixel.  We want at least one sample.
+          */
+          attributes[6] = GLX_SAMPLES;
+          attributes[7] = 1;
+          attributes[8] = None;
+
+          attribute_name = GLX_SAMPLES;
+
+          break;
+        }
+
+      fbconfigArray = glXChooseFBConfig(fgDisplay.Display,
+                                        fgDisplay.Screen,
+                                        attributes,
+                                        &fbconfigArraySize);
+
+      if (fbconfigArray != NULL)
+        {
+          int * temp_array;
+          int result;   /*  Returned by glXGetFBConfigAttrib. Not checked.  */
+          int previous_value;
+          int i;
+
+          temp_array = malloc(sizeof(int) * fbconfigArraySize);
+          previous_value = 0;
+
+          for (i = 0; i < fbconfigArraySize; i++)
+            {
+              int value;
+
+              result = glXGetFBConfigAttrib(fgDisplay.Display,
+                                            fbconfigArray[i],
+                                            attribute_name,
+                                            &value);
+              if (value > previous_value)
+                {
+                  temp_array[*size] = value;
+                  previous_value = value;
+                  (*size)++;
+                }
+            }
+
+          array = malloc(sizeof(int) * (*size));
+          for (i = 0; i < *size; i++)
+            {
+              array[i] = temp_array[i];
+            }
+
+          free(temp_array);
+          XFree(fbconfigArray);
+        }
+
+      break;
+#endif      
+
+    default:
+      break;
+    }
+
+  return array;
+}
+
 /*** END OF FILE ***/