+ numAuxBuffers = fghNumberOfAuxBuffersRequested();
+ if ( numAuxBuffers > 0 ) {
+ ATTRIB_VAL( GLX_AUX_BUFFERS, numAuxBuffers );
+ }
+
+ if( fgState.DisplayMode & GLUT_SRGB ) {
+ ATTRIB_VAL( GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB, True );
+ }
+
+ if (fgState.DisplayMode & GLUT_MULTISAMPLE) {
+ ATTRIB_VAL(GLX_SAMPLE_BUFFERS, 1);
+ ATTRIB_VAL(GLX_SAMPLES, fgState.SampleNumber);
+ }
+
+ /* Push a terminator at the end of the list */
+ ATTRIB( None );
+
+ {
+ GLXFBConfig * fbconfigArray; /* Array of FBConfigs */
+ GLXFBConfig * fbconfig; /* The FBConfig we want */
+ int fbconfigArraySize; /* Number of FBConfigs in the array */
+
+
+ /* Get all FBConfigs that match "attributes". */
+ fbconfigArray = glXChooseFBConfig( fgDisplay.Display,
+ fgDisplay.Screen,
+ attributes,
+ &fbconfigArraySize );
+
+ if (fbconfigArray != NULL)
+ {
+ int result; /* Returned by glXGetFBConfigAttrib, not checked. */
+
+
+ if( wantIndexedMode )
+ {
+ /*
+ * In index mode, we want the largest buffer size, i.e. visual
+ * depth. Here, FBConfigs are sorted by increasing buffer size
+ * first, so FBConfigs with the largest size come last.
+ */
+
+ int bufferSizeMin, bufferSizeMax;
+
+ /* Get bufferSizeMin. */
+ result =
+ glXGetFBConfigAttrib( fgDisplay.Display,
+ fbconfigArray[0],
+ GLX_BUFFER_SIZE,
+ &bufferSizeMin );
+ /* Get bufferSizeMax. */
+ result =
+ glXGetFBConfigAttrib( fgDisplay.Display,
+ fbconfigArray[fbconfigArraySize - 1],
+ GLX_BUFFER_SIZE,
+ &bufferSizeMax );
+
+ if (bufferSizeMax > bufferSizeMin)
+ {
+ /*
+ * Free and reallocate fbconfigArray, keeping only FBConfigs
+ * with the largest buffer size.
+ */
+ XFree(fbconfigArray);
+
+ /* Add buffer size token at the end of the list. */
+ where--;
+ ATTRIB_VAL( GLX_BUFFER_SIZE, bufferSizeMax );
+ ATTRIB( None );
+
+ fbconfigArray = glXChooseFBConfig( fgDisplay.Display,
+ fgDisplay.Screen,
+ attributes,
+ &fbconfigArraySize );
+ }
+ }
+
+ /*
+ * We now have an array of FBConfigs, the first one being the "best"
+ * one. So we should return only this FBConfig:
+ *
+ * int fbconfigXID;
+ *
+ * - pick the XID of the FBConfig we want
+ * result = glXGetFBConfigAttrib( fgDisplay.Display,
+ * fbconfigArray[0],
+ * GLX_FBCONFIG_ID,
+ * &fbconfigXID );
+ *
+ * - free the array
+ * XFree(fbconfigArray);
+ *
+ * - reset "attributes" with the XID
+ * where = 0;
+ * ATTRIB_VAL( GLX_FBCONFIG_ID, fbconfigXID );
+ * ATTRIB( None );
+ *
+ * - get our FBConfig only
+ * fbconfig = glXChooseFBConfig( fgDisplay.Display,
+ * fgDisplay.Screen,
+ * attributes,
+ * &fbconfigArraySize );
+ *
+ * However, for some configurations (for instance multisampling with
+ * Mesa 6.5.2 and ATI drivers), this does not work:
+ * glXChooseFBConfig returns NULL, whereas fbconfigXID is a valid
+ * XID. Further investigation is needed.
+ *
+ * So, for now, we return the whole array of FBConfigs. This should
+ * not produce any side effects elsewhere.
+ */
+ fbconfig = fbconfigArray;
+ }
+ else
+ {
+ fbconfig = NULL;
+ }
+
+ if (numcfgs)
+ *numcfgs = fbconfigArraySize;
+
+ return fbconfig;
+ }
+}
+
+
+static void fghFillContextAttributes( int *attributes ) {
+ int where = 0, contextFlags, contextProfile;
+
+ if ( !fghIsLegacyContextVersionRequested() ) {
+ ATTRIB_VAL( GLX_CONTEXT_MAJOR_VERSION_ARB, fgState.MajorVersion );
+ ATTRIB_VAL( GLX_CONTEXT_MINOR_VERSION_ARB, fgState.MinorVersion );
+ }
+
+ contextFlags =
+ fghMapBit( fgState.ContextFlags, GLUT_DEBUG, GLX_CONTEXT_DEBUG_BIT_ARB ) |
+ fghMapBit( fgState.ContextFlags, GLUT_FORWARD_COMPATIBLE, GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB );
+ if ( contextFlags != 0 ) {
+ ATTRIB_VAL( GLX_CONTEXT_FLAGS_ARB, contextFlags );
+ }
+
+ contextProfile =
+ fghMapBit( fgState.ContextProfile, GLUT_CORE_PROFILE, GLX_CONTEXT_CORE_PROFILE_BIT_ARB ) |
+ fghMapBit( fgState.ContextProfile, GLUT_COMPATIBILITY_PROFILE, GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB );
+ if ( contextProfile != 0 ) {
+ ATTRIB_VAL( GLX_CONTEXT_PROFILE_MASK_ARB, contextProfile );
+ }
+
+ ATTRIB( 0 );
+}
+
+typedef GLXContext (*CreateContextAttribsProc)(Display *dpy, GLXFBConfig config,
+ GLXContext share_list, Bool direct,
+ const int *attrib_list);
+
+static GLXContext fghCreateNewContext( SFG_Window* window )
+{
+ /* for color model calculation */
+ int menu = ( window->IsMenu && !fgStructure.MenuContext );
+ int index_mode = ( fgState.DisplayMode & GLUT_INDEX );
+
+ /* "classic" context creation */
+ Display *dpy = fgDisplay.Display;
+ GLXFBConfig config = *(window->Window.FBConfig);
+ int render_type = ( !menu && index_mode ) ? GLX_COLOR_INDEX_TYPE : GLX_RGBA_TYPE;
+ GLXContext share_list = NULL;
+ Bool direct = ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT );
+ GLXContext context;
+
+ /* new context creation */
+ int attributes[9];
+ CreateContextAttribsProc createContextAttribs;
+
+ /* If nothing fancy has been required, simply use the old context creation GLX API entry */
+ if ( fghIsLegacyContextRequested() )
+ {
+ context = glXCreateNewContext( dpy, config, render_type, share_list, direct );
+ if ( context == NULL ) {
+ fghContextCreationError();
+ }
+ return context;
+ }
+
+ /* color index mode is not available anymore with OpenGL 3.0 */
+ if ( render_type == GLX_COLOR_INDEX_TYPE ) {
+ fgWarning( "color index mode is deprecated, using RGBA mode" );
+ }
+
+ fghFillContextAttributes( attributes );
+
+ createContextAttribs = (CreateContextAttribsProc) fghGetProcAddress( "glXCreateContextAttribsARB" );
+ if ( createContextAttribs == NULL ) {
+ fgError( "glXCreateContextAttribsARB not found" );
+ }
+
+ context = createContextAttribs( dpy, config, share_list, direct, attributes );
+ if ( context == NULL ) {
+ fghContextCreationError();
+ }
+ return context;
+}
+
+
+#define _NET_WM_STATE_TOGGLE 2
+static int fghResizeFullscrToggle(void)
+{
+ XWindowAttributes attributes;
+
+ if(glutGet(GLUT_FULL_SCREEN)) {
+ /* restore original window size */
+ SFG_Window *win = fgStructure.CurrentWindow;
+ fgStructure.CurrentWindow->State.NeedToResize = GL_TRUE;
+ fgStructure.CurrentWindow->State.Width = win->State.OldWidth;
+ fgStructure.CurrentWindow->State.Height = win->State.OldHeight;
+
+ } else {
+ /* resize the window to cover the entire screen */
+ XGetWindowAttributes(fgDisplay.Display,
+ fgStructure.CurrentWindow->Window.Handle,
+ &attributes);
+
+ /*
+ * The "x" and "y" members of "attributes" are the window's coordinates
+ * relative to its parent, i.e. to the decoration window.
+ */
+ XMoveResizeWindow(fgDisplay.Display,
+ fgStructure.CurrentWindow->Window.Handle,
+ -attributes.x,
+ -attributes.y,
+ fgDisplay.ScreenWidth,
+ fgDisplay.ScreenHeight);
+ }
+ return 0;
+}
+
+static int fghEwmhFullscrToggle(void)
+{
+ XEvent xev;
+ long evmask = SubstructureRedirectMask | SubstructureNotifyMask;
+
+ if(!fgDisplay.State || !fgDisplay.StateFullScreen) {
+ return -1;
+ }
+
+ xev.type = ClientMessage;
+ xev.xclient.window = fgStructure.CurrentWindow->Window.Handle;
+ xev.xclient.message_type = fgDisplay.State;
+ xev.xclient.format = 32;
+ xev.xclient.data.l[0] = _NET_WM_STATE_TOGGLE;
+ xev.xclient.data.l[1] = fgDisplay.StateFullScreen;
+ xev.xclient.data.l[2] = 0; /* no second property to toggle */
+ xev.xclient.data.l[3] = 1; /* source indication: application */
+ xev.xclient.data.l[4] = 0; /* unused */
+
+ if(!XSendEvent(fgDisplay.Display, fgDisplay.RootWindow, 0, evmask, &xev)) {
+ return -1;
+ }
+ return 0;
+}
+
+static int fghToggleFullscreen(void)
+{
+ /* first try the EWMH (_NET_WM_STATE) method ... */
+ if(fghEwmhFullscrToggle() != -1) {
+ return 0;
+ }
+
+ /* fall back to resizing the window */
+ if(fghResizeFullscrToggle() != -1) {
+ return 0;
+ }
+ return -1;
+}
+
+
+#endif /* TARGET_HOST_POSIX_X11 */
+
+
+#if TARGET_HOST_MS_WINDOWS