19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_x11video.h"
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_events_c.h"
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_x11dga_c.h"
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_x11gl_c.h"
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(__IRIX__)
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* IRIX doesn't have a GL library versioning system */
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DEFAULT_OPENGL	"libGL.so"
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#elif defined(__MACOSX__)
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DEFAULT_OPENGL	"/usr/X11R6/lib/libGL.1.dylib"
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#elif defined(__QNXNTO__)
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DEFAULT_OPENGL	"libGL.so.3"
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#elif defined(__OpenBSD__)
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DEFAULT_OPENGL	"libGL.so.4.0"
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DEFAULT_OPENGL	"libGL.so.1"
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef GLX_ARB_multisample
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define GLX_ARB_multisample
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define GLX_SAMPLE_BUFFERS_ARB             100000
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define GLX_SAMPLES_ARB                    100001
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* GLX_EXT_visual_rating stuff that might not be in the system headers... */
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef GLX_VISUAL_CAVEAT_EXT
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define GLX_VISUAL_CAVEAT_EXT              0x20
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef GLX_NONE_EXT
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define GLX_NONE_EXT                       0x8000
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef GLX_SLOW_VISUAL_EXT
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define GLX_SLOW_VISUAL_EXT                0x8001
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef GLX_NON_CONFORMANT_VISUAL_EXT
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define GLX_NON_CONFORMANT_VISUAL_EXT      0x800D
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_OPENGL_GLX
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int glXExtensionSupported(_THIS, const char *extension)
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const char *extensions;
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const char *start;
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const char *where, *terminator;
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Extension names should not have spaces. */
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	where = SDL_strchr(extension, ' ');
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( where || *extension == '\0' ) {
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	      return 0;
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	extensions = this->gl_data->glXQueryExtensionsString(GFX_Display,SDL_Screen);
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* It takes a bit of care to be fool-proof about parsing the
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * OpenGL extensions string. Don't be fooled by sub-strings, etc.
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* http://bugs.debian.org/537487 */
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (extensions == NULL) {
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	      return 0;
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	start = extensions;
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (;;) {
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		where = SDL_strstr(start, extension);
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (!where) break;
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		terminator = where + strlen(extension);
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (where == start || *(where - 1) == ' ')
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        if (*terminator == ' ' || *terminator == '\0') return 1;
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		start = terminator;
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_VIDEO_OPENGL_GLX */
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallXVisualInfo *X11_GL_GetVisual(_THIS)
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_OPENGL_GLX
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* 64 seems nice. */
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int attribs[64];
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* load the gl driver from a default path */
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! this->gl_config.driver_loaded ) {
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        /* no driver has been loaded, use default (ourselves) */
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        if ( X11_GL_LoadLibrary(this, NULL) < 0 ) {
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		        return NULL;
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* See if we already have a window which we must use */
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_windowid ) {
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		XWindowAttributes a;
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		XVisualInfo vi_in;
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int out_count;
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		XGetWindowAttributes(SDL_Display, SDL_Window, &a);
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		vi_in.screen = SDL_Screen;
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		vi_in.visualid = XVisualIDFromVisual(a.visual);
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		glx_visualinfo = XGetVisualInfo(SDL_Display,
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	                     VisualScreenMask|VisualIDMask, &vi_in, &out_count);
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return glx_visualinfo;
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* Setup our GLX attributes according to the gl_config. */
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	i = 0;
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	attribs[i++] = GLX_RGBA;
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	attribs[i++] = GLX_RED_SIZE;
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	attribs[i++] = this->gl_config.red_size;
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	attribs[i++] = GLX_GREEN_SIZE;
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	attribs[i++] = this->gl_config.green_size;
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	attribs[i++] = GLX_BLUE_SIZE;
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	attribs[i++] = this->gl_config.blue_size;
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( this->gl_config.alpha_size ) {
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attribs[i++] = GLX_ALPHA_SIZE;
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attribs[i++] = this->gl_config.alpha_size;
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( this->gl_config.double_buffer ) {
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attribs[i++] = GLX_DOUBLEBUFFER;
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	attribs[i++] = GLX_DEPTH_SIZE;
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	attribs[i++] = this->gl_config.depth_size;
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( this->gl_config.stencil_size ) {
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attribs[i++] = GLX_STENCIL_SIZE;
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attribs[i++] = this->gl_config.stencil_size;
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( this->gl_config.accum_red_size ) {
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attribs[i++] = GLX_ACCUM_RED_SIZE;
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attribs[i++] = this->gl_config.accum_red_size;
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( this->gl_config.accum_green_size ) {
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attribs[i++] = GLX_ACCUM_GREEN_SIZE;
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attribs[i++] = this->gl_config.accum_green_size;
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( this->gl_config.accum_blue_size ) {
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attribs[i++] = GLX_ACCUM_BLUE_SIZE;
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attribs[i++] = this->gl_config.accum_blue_size;
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( this->gl_config.accum_alpha_size ) {
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attribs[i++] = GLX_ACCUM_ALPHA_SIZE;
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attribs[i++] = this->gl_config.accum_alpha_size;
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( this->gl_config.stereo ) {
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attribs[i++] = GLX_STEREO;
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( this->gl_config.multisamplebuffers ) {
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attribs[i++] = GLX_SAMPLE_BUFFERS_ARB;
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attribs[i++] = this->gl_config.multisamplebuffers;
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( this->gl_config.multisamplesamples ) {
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attribs[i++] = GLX_SAMPLES_ARB;
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attribs[i++] = this->gl_config.multisamplesamples;
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( this->gl_config.accelerated >= 0 &&
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    glXExtensionSupported(this, "GLX_EXT_visual_rating") ) {
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attribs[i++] = GLX_VISUAL_CAVEAT_EXT;
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attribs[i++] = GLX_NONE_EXT;
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef GLX_DIRECT_COLOR /* Try for a DirectColor visual for gamma support */
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !SDL_getenv("SDL_VIDEO_X11_NODIRECTCOLOR") ) {
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attribs[i++] = GLX_X_VISUAL_TYPE;
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attribs[i++] = GLX_DIRECT_COLOR;
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	attribs[i++] = None;
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	glx_visualinfo = this->gl_data->glXChooseVisual(GFX_Display,
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						  SDL_Screen, attribs);
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef GLX_DIRECT_COLOR
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( !glx_visualinfo && !SDL_getenv("SDL_VIDEO_X11_NODIRECTCOLOR") ) { /* No DirectColor visual?  Try again.. */
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		attribs[i-3] = None;
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 		glx_visualinfo = this->gl_data->glXChooseVisual(GFX_Display,
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						  SDL_Screen, attribs);
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( !glx_visualinfo ) {
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError( "Couldn't find matching GLX visual");
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return NULL;
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("Found GLX visual 0x%x\n", glx_visualinfo->visualid);
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return glx_visualinfo;
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SetError("X11 driver not configured with OpenGL");
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return NULL;
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint X11_GL_CreateWindow(_THIS, int w, int h)
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_OPENGL_GLX
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	XSetWindowAttributes attributes;
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	unsigned long mask;
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	unsigned long black;
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	black = (glx_visualinfo->visual == DefaultVisual(SDL_Display,
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						 	SDL_Screen))
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	       	? BlackPixel(SDL_Display, SDL_Screen) : 0;
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	attributes.background_pixel = black;
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	attributes.border_pixel = black;
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	attributes.colormap = SDL_XColorMap;
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mask = CWBackPixel | CWBorderPixel | CWColormap;
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Window = XCreateWindow(SDL_Display, WMwindow,
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			0, 0, w, h, 0, glx_visualinfo->depth,
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			InputOutput, glx_visualinfo->visual,
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mask, &attributes);
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !SDL_Window ) {
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Could not create window");
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	retval = 0;
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SetError("X11 driver not configured with OpenGL");
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	retval = -1;
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(retval);
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint X11_GL_CreateContext(_THIS)
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_OPENGL_GLX
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We do this to create a clean separation between X and GLX errors. */
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	XSync( SDL_Display, False );
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	glx_context = this->gl_data->glXCreateContext(GFX_Display,
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				     glx_visualinfo, NULL, True);
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	XSync( GFX_Display, False );
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( glx_context == NULL ) {
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Could not create GL context");
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( X11_GL_MakeCurrent(this) < 0 ) {
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	gl_active = 1;
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !glXExtensionSupported(this, "GLX_SGI_swap_control") ) {
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->gl_data->glXSwapIntervalSGI = NULL;
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !glXExtensionSupported(this, "GLX_MESA_swap_control") ) {
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->gl_data->glXSwapIntervalMESA = NULL;
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !glXExtensionSupported(this, "GLX_EXT_swap_control") ) {
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->gl_data->glXSwapIntervalEXT = NULL;
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this->gl_config.swap_control >= 0 ) {
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int rc = -1;
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( this->gl_data->glXSwapIntervalEXT ) {
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			rc = this->gl_data->glXSwapIntervalEXT(GFX_Display, SDL_Window,
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					this->gl_config.swap_control);
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else if ( this->gl_data->glXSwapIntervalMESA ) {
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			rc = this->gl_data->glXSwapIntervalMESA(this->gl_config.swap_control);
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else if ( this->gl_data->glXSwapIntervalSGI ) {
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			rc = this->gl_data->glXSwapIntervalSGI(this->gl_config.swap_control);
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (rc == 0) {
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			this->gl_data->swap_interval = this->gl_config.swap_control;
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SetError("X11 driver not configured with OpenGL");
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( gl_active ) {
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = 0;
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = -1;
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(retval);
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid X11_GL_Shutdown(_THIS)
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_OPENGL_GLX
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Clean up OpenGL */
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if( glx_context ) {
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->gl_data->glXMakeCurrent(GFX_Display, None, NULL);
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (glx_context != NULL)
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			this->gl_data->glXDestroyContext(GFX_Display, glx_context);
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		glx_context = NULL;
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	gl_active = 0;
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_VIDEO_OPENGL_GLX */
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_OPENGL_GLX
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Make the current context active */
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint X11_GL_MakeCurrent(_THIS)
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	retval = 0;
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! this->gl_data->glXMakeCurrent(GFX_Display,
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	                                     SDL_Window, glx_context) ) {
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Unable to make GL context current");
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = -1;
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	XSync( GFX_Display, False );
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* More Voodoo X server workarounds... Grr... */
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Lock_EventThread();
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	X11_CheckDGAMouse(this);
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Unlock_EventThread();
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(retval);
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Get attribute data from glX. */
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint X11_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval = -1;
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int unsupported = 0;
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int glx_attrib = None;
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch( attrib ) {
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case SDL_GL_RED_SIZE:
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		glx_attrib = GLX_RED_SIZE;
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case SDL_GL_GREEN_SIZE:
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		glx_attrib = GLX_GREEN_SIZE;
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case SDL_GL_BLUE_SIZE:
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		glx_attrib = GLX_BLUE_SIZE;
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case SDL_GL_ALPHA_SIZE:
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		glx_attrib = GLX_ALPHA_SIZE;
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case SDL_GL_DOUBLEBUFFER:
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		glx_attrib = GLX_DOUBLEBUFFER;
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case SDL_GL_BUFFER_SIZE:
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		glx_attrib = GLX_BUFFER_SIZE;
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case SDL_GL_DEPTH_SIZE:
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		glx_attrib = GLX_DEPTH_SIZE;
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case SDL_GL_STENCIL_SIZE:
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		glx_attrib = GLX_STENCIL_SIZE;
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case SDL_GL_ACCUM_RED_SIZE:
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		glx_attrib = GLX_ACCUM_RED_SIZE;
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case SDL_GL_ACCUM_GREEN_SIZE:
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		glx_attrib = GLX_ACCUM_GREEN_SIZE;
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case SDL_GL_ACCUM_BLUE_SIZE:
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		glx_attrib = GLX_ACCUM_BLUE_SIZE;
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case SDL_GL_ACCUM_ALPHA_SIZE:
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		glx_attrib = GLX_ACCUM_ALPHA_SIZE;
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case SDL_GL_STEREO:
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		glx_attrib = GLX_STEREO;
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	    case SDL_GL_MULTISAMPLEBUFFERS:
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 		glx_attrib = GLX_SAMPLE_BUFFERS_ARB;
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 		break;
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	    case SDL_GL_MULTISAMPLESAMPLES:
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 		glx_attrib = GLX_SAMPLES_ARB;
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 		break;
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	    case SDL_GL_ACCELERATED_VISUAL:
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( glXExtensionSupported(this, "GLX_EXT_visual_rating") ) {
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			glx_attrib = GLX_VISUAL_CAVEAT_EXT;
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			retval = this->gl_data->glXGetConfig(GFX_Display, glx_visualinfo, glx_attrib, value);
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( *value == GLX_SLOW_VISUAL_EXT ) {
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*value = SDL_FALSE;
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else {
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*value = SDL_TRUE;
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return retval;
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			unsupported = 1;
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case SDL_GL_SWAP_CONTROL:
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ( this->gl_data->glXSwapIntervalEXT ) ||
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     ( this->gl_data->glXSwapIntervalMESA ) ||
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     ( this->gl_data->glXSwapIntervalSGI ) ) {
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*value = this->gl_data->swap_interval;
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return 0;
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			unsupported = 1;
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    default:
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			unsupported = 1;
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (unsupported) {
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("OpenGL attribute is unsupported on this system");
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = this->gl_data->glXGetConfig(GFX_Display, glx_visualinfo, glx_attrib, value);
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return retval;
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid X11_GL_SwapBuffers(_THIS)
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->gl_data->glXSwapBuffers(GFX_Display, SDL_Window);
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_VIDEO_OPENGL_GLX */
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define OPENGL_REQUIRS_DLOPEN
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(OPENGL_REQUIRS_DLOPEN) && defined(SDL_LOADSO_DLOPEN)
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <dlfcn.h>
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define GL_LoadObject(X)	dlopen(X, (RTLD_NOW|RTLD_GLOBAL))
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define GL_LoadFunction		dlsym
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define GL_UnloadObject		dlclose
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define GL_LoadObject	SDL_LoadObject
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define GL_LoadFunction	SDL_LoadFunction
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define GL_UnloadObject	SDL_UnloadObject
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid X11_GL_UnloadLibrary(_THIS)
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_OPENGL_GLX
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this->gl_config.driver_loaded ) {
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		GL_UnloadObject(this->gl_config.dll_handle);
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->gl_data->glXGetProcAddress = NULL;
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->gl_data->glXChooseVisual = NULL;
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->gl_data->glXCreateContext = NULL;
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->gl_data->glXDestroyContext = NULL;
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->gl_data->glXMakeCurrent = NULL;
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->gl_data->glXSwapBuffers = NULL;
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->gl_data->glXSwapIntervalSGI = NULL;
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->gl_data->glXSwapIntervalMESA = NULL;
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->gl_data->glXSwapIntervalEXT = NULL;
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->gl_config.dll_handle = NULL;
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->gl_config.driver_loaded = 0;
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_OPENGL_GLX
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Passing a NULL path means load pointers from the application */
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint X11_GL_LoadLibrary(_THIS, const char* path)
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	void* handle = NULL;
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( gl_active ) {
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("OpenGL context already created");
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( path == NULL ) {
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( path == NULL ) {
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			path = DEFAULT_OPENGL;
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	handle = GL_LoadObject(path);
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( handle == NULL ) {
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(OPENGL_REQUIRS_DLOPEN) && defined(SDL_LOADSO_DLOPEN)
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Failed loading %s", path);
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* SDL_LoadObject() will call SDL_SetError() for us. */
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Unload the old driver and reset the pointers */
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	X11_GL_UnloadLibrary(this);
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Save the handle for X11_GL_GetProcAddress() */
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->gl_config.dll_handle = handle;
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Load new function pointers */
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->gl_data->glXGetProcAddress =
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void *(*)(const GLubyte *)) GL_LoadFunction(handle, "glXGetProcAddressARB");
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->gl_data->glXChooseVisual =
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(XVisualInfo *(*)(Display *, int, int *)) X11_GL_GetProcAddress(this, "glXChooseVisual");
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->gl_data->glXCreateContext =
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(GLXContext (*)(Display *, XVisualInfo *, GLXContext, int)) X11_GL_GetProcAddress(this, "glXCreateContext");
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->gl_data->glXDestroyContext =
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void (*)(Display *, GLXContext)) X11_GL_GetProcAddress(this, "glXDestroyContext");
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->gl_data->glXMakeCurrent =
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(int (*)(Display *, GLXDrawable, GLXContext)) X11_GL_GetProcAddress(this, "glXMakeCurrent");
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->gl_data->glXSwapBuffers =
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(void (*)(Display *, GLXDrawable)) X11_GL_GetProcAddress(this, "glXSwapBuffers");
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->gl_data->glXGetConfig =
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(int (*)(Display *, XVisualInfo *, int, int *)) X11_GL_GetProcAddress(this, "glXGetConfig");
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->gl_data->glXQueryExtensionsString =
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(const char *(*)(Display *, int)) X11_GL_GetProcAddress(this, "glXQueryExtensionsString");
5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->gl_data->glXSwapIntervalSGI =
5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(int (*)(int)) X11_GL_GetProcAddress(this, "glXSwapIntervalSGI");
5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->gl_data->glXSwapIntervalMESA =
5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(GLint (*)(unsigned)) X11_GL_GetProcAddress(this, "glXSwapIntervalMESA");
5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->gl_data->glXSwapIntervalEXT =
5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(int (*)(Display*,GLXDrawable,int)) X11_GL_GetProcAddress(this, "glXSwapIntervalEXT");
5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (this->gl_data->glXChooseVisual == NULL) ||
5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     (this->gl_data->glXCreateContext == NULL) ||
5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     (this->gl_data->glXDestroyContext == NULL) ||
5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     (this->gl_data->glXMakeCurrent == NULL) ||
5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     (this->gl_data->glXSwapBuffers == NULL) ||
5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     (this->gl_data->glXGetConfig == NULL) ||
5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     (this->gl_data->glXQueryExtensionsString == NULL)) {
5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		GL_UnloadObject(this->gl_config.dll_handle);
5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->gl_config.dll_handle = NULL;
5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Could not retrieve OpenGL functions");
5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->gl_config.driver_loaded = 1;
5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( path ) {
5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_strlcpy(this->gl_config.driver_path, path,
5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_arraysize(this->gl_config.driver_path));
5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*this->gl_config.driver_path = '\0';
5659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
5679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid *X11_GL_GetProcAddress(_THIS, const char* proc)
5709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this->gl_data->glXGetProcAddress ) {
5729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return this->gl_data->glXGetProcAddress((const GLubyte *)proc);
5739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return GL_LoadFunction(this->gl_config.dll_handle, proc);
5759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_VIDEO_OPENGL_GLX */
578