src/video/x11/SDL_x11gl.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 12 Sep 2009 06:25:36 +0000
branchSDL-1.2
changeset 4183 39e748f251c6
parent 4159 a1b03ba2fcd0
child 4268 d48035d857d3
permissions -rw-r--r--
Patched X11 code to compile on Mac OS X 10.6 SDK.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@4159
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
#include "SDL_x11video.h"
slouken@1361
    25
#include "../../events/SDL_events_c.h"
slouken@0
    26
#include "SDL_x11dga_c.h"
slouken@0
    27
#include "SDL_x11gl_c.h"
slouken@0
    28
slouken@1402
    29
#if defined(__IRIX__)
slouken@862
    30
/* IRIX doesn't have a GL library versioning system */
slouken@862
    31
#define DEFAULT_OPENGL	"libGL.so"
slouken@1402
    32
#elif defined(__MACOSX__)
icculus@1191
    33
#define DEFAULT_OPENGL	"/usr/X11R6/lib/libGL.1.dylib"
slouken@1402
    34
#elif defined(__QNXNTO__)
slouken@1402
    35
#define DEFAULT_OPENGL	"libGL.so.3"
slouken@4023
    36
#elif defined(__OpenBSD__)
slouken@4023
    37
#define DEFAULT_OPENGL	"libGL.so.4.0"
slouken@862
    38
#else
slouken@0
    39
#define DEFAULT_OPENGL	"libGL.so.1"
slouken@862
    40
#endif
slouken@0
    41
slouken@667
    42
#ifndef GLX_ARB_multisample
slouken@667
    43
#define GLX_ARB_multisample
slouken@667
    44
#define GLX_SAMPLE_BUFFERS_ARB             100000
slouken@667
    45
#define GLX_SAMPLES_ARB                    100001
slouken@667
    46
#endif
slouken@667
    47
icculus@4183
    48
/* GLX_EXT_visual_rating stuff that might not be in the system headers... */
icculus@4183
    49
#ifndef GLX_VISUAL_CAVEAT_EXT
slouken@1737
    50
#define GLX_VISUAL_CAVEAT_EXT              0x20
icculus@4183
    51
#endif
icculus@4183
    52
#ifndef GLX_NONE_EXT
slouken@1841
    53
#define GLX_NONE_EXT                       0x8000
icculus@4183
    54
#endif
icculus@4183
    55
#ifndef GLX_SLOW_VISUAL_EXT
slouken@1737
    56
#define GLX_SLOW_VISUAL_EXT                0x8001
icculus@4183
    57
#endif
icculus@4183
    58
#ifndef GLX_NON_CONFORMANT_VISUAL_EXT
slouken@1737
    59
#define GLX_NON_CONFORMANT_VISUAL_EXT      0x800D
slouken@1737
    60
#endif
slouken@1737
    61
icculus@4183
    62
slouken@1737
    63
#if SDL_VIDEO_OPENGL_GLX
slouken@1737
    64
static int glXExtensionSupported(_THIS, const char *extension)
slouken@1737
    65
{
slouken@1737
    66
	const char *extensions;
slouken@1737
    67
	const char *start;
slouken@1737
    68
	const char *where, *terminator;
slouken@1737
    69
slouken@1737
    70
	/* Extension names should not have spaces. */
slouken@1737
    71
	where = SDL_strchr(extension, ' ');
slouken@1737
    72
	if ( where || *extension == '\0' ) {
slouken@1737
    73
	      return 0;
slouken@1737
    74
	}
slouken@1737
    75
slouken@1737
    76
	extensions = this->gl_data->glXQueryExtensionsString(GFX_Display,SDL_Screen);
slouken@1737
    77
	/* It takes a bit of care to be fool-proof about parsing the
slouken@1737
    78
	 * OpenGL extensions string. Don't be fooled by sub-strings, etc.
slouken@1737
    79
	 */
slouken@1737
    80
	
slouken@1737
    81
	start = extensions;
slouken@1737
    82
	
slouken@1737
    83
	for (;;) {
slouken@1737
    84
		where = SDL_strstr(start, extension);
slouken@1737
    85
		if (!where) break;
slouken@1737
    86
		
slouken@1737
    87
		terminator = where + strlen(extension);
slouken@1737
    88
		if (where == start || *(where - 1) == ' ')
slouken@1737
    89
	        if (*terminator == ' ' || *terminator == '\0') return 1;
slouken@1737
    90
						  
slouken@1737
    91
		start = terminator;
slouken@1737
    92
	}
slouken@1737
    93
	return 0;
slouken@1737
    94
}
slouken@1737
    95
#endif /* SDL_VIDEO_OPENGL_GLX */
slouken@1737
    96
slouken@0
    97
XVisualInfo *X11_GL_GetVisual(_THIS)
slouken@0
    98
{
slouken@1361
    99
#if SDL_VIDEO_OPENGL_GLX
slouken@0
   100
	/* 64 seems nice. */
slouken@0
   101
	int attribs[64];
slouken@0
   102
	int i;
slouken@0
   103
slouken@0
   104
	/* load the gl driver from a default path */
slouken@0
   105
	if ( ! this->gl_config.driver_loaded ) {
slouken@0
   106
	        /* no driver has been loaded, use default (ourselves) */
slouken@0
   107
	        if ( X11_GL_LoadLibrary(this, NULL) < 0 ) {
slouken@0
   108
		        return NULL;
slouken@0
   109
		}
slouken@0
   110
	}
slouken@0
   111
slouken@0
   112
	/* See if we already have a window which we must use */
slouken@0
   113
	if ( SDL_windowid ) {
slouken@0
   114
		XWindowAttributes a;
slouken@0
   115
		XVisualInfo vi_in;
slouken@0
   116
		int out_count;
slouken@0
   117
icculus@1575
   118
		XGetWindowAttributes(SDL_Display, SDL_Window, &a);
slouken@0
   119
		vi_in.screen = SDL_Screen;
icculus@1575
   120
		vi_in.visualid = XVisualIDFromVisual(a.visual);
icculus@1575
   121
		glx_visualinfo = XGetVisualInfo(SDL_Display,
slouken@0
   122
	                     VisualScreenMask|VisualIDMask, &vi_in, &out_count);
slouken@0
   123
		return glx_visualinfo;
slouken@0
   124
	}
slouken@0
   125
slouken@0
   126
        /* Setup our GLX attributes according to the gl_config. */
slouken@450
   127
	i = 0;
slouken@450
   128
	attribs[i++] = GLX_RGBA;
slouken@0
   129
	attribs[i++] = GLX_RED_SIZE;
slouken@0
   130
	attribs[i++] = this->gl_config.red_size;
slouken@0
   131
	attribs[i++] = GLX_GREEN_SIZE;
slouken@0
   132
	attribs[i++] = this->gl_config.green_size;
slouken@0
   133
	attribs[i++] = GLX_BLUE_SIZE;
slouken@0
   134
	attribs[i++] = this->gl_config.blue_size;
slouken@0
   135
slouken@0
   136
	if( this->gl_config.alpha_size ) {
slouken@0
   137
		attribs[i++] = GLX_ALPHA_SIZE;
slouken@0
   138
		attribs[i++] = this->gl_config.alpha_size;
slouken@0
   139
	}
slouken@0
   140
slouken@0
   141
	if( this->gl_config.buffer_size ) {
slouken@450
   142
		attribs[i++] = GLX_BUFFER_SIZE;
slouken@450
   143
		attribs[i++] = this->gl_config.buffer_size;
slouken@0
   144
	}
slouken@0
   145
slouken@0
   146
	if( this->gl_config.double_buffer ) {
slouken@0
   147
		attribs[i++] = GLX_DOUBLEBUFFER;
slouken@0
   148
	}
slouken@0
   149
slouken@0
   150
	attribs[i++] = GLX_DEPTH_SIZE;
slouken@0
   151
	attribs[i++] = this->gl_config.depth_size;
slouken@0
   152
slouken@0
   153
	if( this->gl_config.stencil_size ) {
slouken@0
   154
		attribs[i++] = GLX_STENCIL_SIZE;
slouken@0
   155
		attribs[i++] = this->gl_config.stencil_size;
slouken@0
   156
	}
slouken@0
   157
slouken@0
   158
	if( this->gl_config.accum_red_size ) {
slouken@450
   159
		attribs[i++] = GLX_ACCUM_RED_SIZE;
slouken@0
   160
		attribs[i++] = this->gl_config.accum_red_size;
slouken@0
   161
	}
slouken@0
   162
slouken@0
   163
	if( this->gl_config.accum_green_size ) {
slouken@450
   164
		attribs[i++] = GLX_ACCUM_GREEN_SIZE;
slouken@0
   165
		attribs[i++] = this->gl_config.accum_green_size;
slouken@0
   166
	}
slouken@0
   167
slouken@0
   168
	if( this->gl_config.accum_blue_size ) {
slouken@450
   169
		attribs[i++] = GLX_ACCUM_BLUE_SIZE;
slouken@0
   170
		attribs[i++] = this->gl_config.accum_blue_size;
slouken@0
   171
	}
slouken@0
   172
slouken@0
   173
	if( this->gl_config.accum_alpha_size ) {
slouken@450
   174
		attribs[i++] = GLX_ACCUM_ALPHA_SIZE;
slouken@0
   175
		attribs[i++] = this->gl_config.accum_alpha_size;
slouken@0
   176
	}
slouken@0
   177
slouken@450
   178
	if( this->gl_config.stereo ) {
slouken@450
   179
		attribs[i++] = GLX_STEREO;
slouken@450
   180
	}
slouken@655
   181
	
slouken@656
   182
	if( this->gl_config.multisamplebuffers ) {
slouken@655
   183
		attribs[i++] = GLX_SAMPLE_BUFFERS_ARB;
slouken@656
   184
		attribs[i++] = this->gl_config.multisamplebuffers;
slouken@655
   185
	}
slouken@655
   186
	
slouken@656
   187
	if( this->gl_config.multisamplesamples ) {
slouken@655
   188
		attribs[i++] = GLX_SAMPLES_ARB;
slouken@656
   189
		attribs[i++] = this->gl_config.multisamplesamples;
slouken@655
   190
	}
slouken@450
   191
slouken@1737
   192
	if( this->gl_config.accelerated >= 0 &&
slouken@1737
   193
	    glXExtensionSupported(this, "GLX_EXT_visual_rating") ) {
slouken@1737
   194
		attribs[i++] = GLX_VISUAL_CAVEAT_EXT;
slouken@1841
   195
		attribs[i++] = GLX_NONE_EXT;
slouken@1737
   196
	}
slouken@1737
   197
slouken@0
   198
#ifdef GLX_DIRECT_COLOR /* Try for a DirectColor visual for gamma support */
slouken@1336
   199
	if ( !SDL_getenv("SDL_VIDEO_X11_NODIRECTCOLOR") ) {
slouken@1060
   200
		attribs[i++] = GLX_X_VISUAL_TYPE;
slouken@1060
   201
		attribs[i++] = GLX_DIRECT_COLOR;
slouken@1060
   202
	}
slouken@0
   203
#endif
slouken@0
   204
	attribs[i++] = None;
slouken@0
   205
slouken@0
   206
 	glx_visualinfo = this->gl_data->glXChooseVisual(GFX_Display, 
slouken@0
   207
						  SDL_Screen, attribs);
slouken@0
   208
#ifdef GLX_DIRECT_COLOR
slouken@1336
   209
	if( !glx_visualinfo && !SDL_getenv("SDL_VIDEO_X11_NODIRECTCOLOR") ) { /* No DirectColor visual?  Try again.. */
slouken@0
   210
		attribs[i-3] = None;
slouken@0
   211
 		glx_visualinfo = this->gl_data->glXChooseVisual(GFX_Display, 
slouken@0
   212
						  SDL_Screen, attribs);
slouken@0
   213
	}
slouken@0
   214
#endif
slouken@0
   215
	if( !glx_visualinfo ) {
slouken@0
   216
		SDL_SetError( "Couldn't find matching GLX visual");
slouken@0
   217
		return NULL;
slouken@0
   218
	}
slouken@1315
   219
/*
slouken@1315
   220
	printf("Found GLX visual 0x%x\n", glx_visualinfo->visualid);
slouken@1315
   221
*/
slouken@0
   222
	return glx_visualinfo;
slouken@0
   223
#else
slouken@0
   224
	SDL_SetError("X11 driver not configured with OpenGL");
slouken@0
   225
	return NULL;
slouken@0
   226
#endif
slouken@0
   227
}
slouken@0
   228
slouken@0
   229
int X11_GL_CreateWindow(_THIS, int w, int h)
slouken@0
   230
{
slouken@0
   231
	int retval;
slouken@1361
   232
#if SDL_VIDEO_OPENGL_GLX
slouken@0
   233
	XSetWindowAttributes attributes;
slouken@0
   234
	unsigned long mask;
slouken@0
   235
	unsigned long black;
slouken@0
   236
slouken@0
   237
	black = (glx_visualinfo->visual == DefaultVisual(SDL_Display,
slouken@0
   238
						 	SDL_Screen))
slouken@0
   239
	       	? BlackPixel(SDL_Display, SDL_Screen) : 0;
slouken@0
   240
	attributes.background_pixel = black;
slouken@0
   241
	attributes.border_pixel = black;
slouken@0
   242
	attributes.colormap = SDL_XColorMap;
slouken@0
   243
	mask = CWBackPixel | CWBorderPixel | CWColormap;
slouken@0
   244
icculus@1575
   245
	SDL_Window = XCreateWindow(SDL_Display, WMwindow,
slouken@0
   246
			0, 0, w, h, 0, glx_visualinfo->depth,
slouken@0
   247
			InputOutput, glx_visualinfo->visual,
slouken@0
   248
			mask, &attributes);
slouken@0
   249
	if ( !SDL_Window ) {
slouken@0
   250
		SDL_SetError("Could not create window");
slouken@0
   251
		return -1;
slouken@0
   252
	}
slouken@0
   253
	retval = 0;
slouken@0
   254
#else
slouken@0
   255
	SDL_SetError("X11 driver not configured with OpenGL");
slouken@0
   256
	retval = -1;
slouken@0
   257
#endif
slouken@0
   258
	return(retval);
slouken@0
   259
}
slouken@0
   260
slouken@0
   261
int X11_GL_CreateContext(_THIS)
slouken@0
   262
{
slouken@0
   263
	int retval;
slouken@1361
   264
#if SDL_VIDEO_OPENGL_GLX
slouken@1736
   265
slouken@0
   266
	/* We do this to create a clean separation between X and GLX errors. */
icculus@1575
   267
	XSync( SDL_Display, False );
slouken@0
   268
	glx_context = this->gl_data->glXCreateContext(GFX_Display, 
slouken@0
   269
				     glx_visualinfo, NULL, True);
icculus@1575
   270
	XSync( GFX_Display, False );
slouken@0
   271
slouken@1736
   272
	if ( glx_context == NULL ) {
slouken@0
   273
		SDL_SetError("Could not create GL context");
slouken@1736
   274
		return(-1);
slouken@0
   275
	}
slouken@1736
   276
	if ( X11_GL_MakeCurrent(this) < 0 ) {
slouken@1736
   277
		return(-1);
slouken@1736
   278
	}
slouken@1736
   279
	gl_active = 1;
slouken@0
   280
icculus@3940
   281
	if ( !glXExtensionSupported(this, "GLX_SGI_swap_control") ) {
slouken@1736
   282
		this->gl_data->glXSwapIntervalSGI = NULL;
slouken@1736
   283
	}
slouken@1737
   284
	if ( !glXExtensionSupported(this, "GLX_MESA_swap_control") ) {
slouken@1736
   285
		this->gl_data->glXSwapIntervalMESA = NULL;
slouken@1736
   286
		this->gl_data->glXGetSwapIntervalMESA = NULL;
slouken@1736
   287
	}
slouken@1736
   288
	if ( this->gl_config.swap_control >= 0 ) {
slouken@1736
   289
		if ( this->gl_data->glXSwapIntervalMESA ) {
slouken@1736
   290
			this->gl_data->glXSwapIntervalMESA(this->gl_config.swap_control);
slouken@1736
   291
		} else if ( this->gl_data->glXSwapIntervalSGI ) {
slouken@1736
   292
			this->gl_data->glXSwapIntervalSGI(this->gl_config.swap_control);
slouken@1736
   293
		}
slouken@1736
   294
	}
slouken@0
   295
#else
slouken@0
   296
	SDL_SetError("X11 driver not configured with OpenGL");
slouken@0
   297
#endif
slouken@0
   298
	if ( gl_active ) {
slouken@0
   299
		retval = 0;
slouken@0
   300
	} else {
slouken@0
   301
		retval = -1;
slouken@0
   302
	}
slouken@0
   303
	return(retval);
slouken@0
   304
}
slouken@0
   305
slouken@0
   306
void X11_GL_Shutdown(_THIS)
slouken@0
   307
{
slouken@1361
   308
#if SDL_VIDEO_OPENGL_GLX
slouken@0
   309
	/* Clean up OpenGL */
slouken@0
   310
	if( glx_context ) {
slouken@0
   311
		this->gl_data->glXMakeCurrent(GFX_Display, None, NULL);
slouken@0
   312
slouken@0
   313
		if (glx_context != NULL)
slouken@0
   314
			this->gl_data->glXDestroyContext(GFX_Display, glx_context);
slouken@0
   315
slouken@0
   316
		glx_context = NULL;
slouken@0
   317
	}
slouken@0
   318
	gl_active = 0;
slouken@1361
   319
#endif /* SDL_VIDEO_OPENGL_GLX */
slouken@0
   320
}
slouken@0
   321
slouken@1361
   322
#if SDL_VIDEO_OPENGL_GLX
slouken@0
   323
slouken@0
   324
/* Make the current context active */
slouken@0
   325
int X11_GL_MakeCurrent(_THIS)
slouken@0
   326
{
slouken@0
   327
	int retval;
slouken@638
   328
	
slouken@0
   329
	retval = 0;
slouken@0
   330
	if ( ! this->gl_data->glXMakeCurrent(GFX_Display,
slouken@0
   331
	                                     SDL_Window, glx_context) ) {
slouken@0
   332
		SDL_SetError("Unable to make GL context current");
slouken@0
   333
		retval = -1;
slouken@0
   334
	}
icculus@1575
   335
	XSync( GFX_Display, False );
slouken@0
   336
slouken@0
   337
	/* More Voodoo X server workarounds... Grr... */
slouken@0
   338
	SDL_Lock_EventThread();
slouken@0
   339
	X11_CheckDGAMouse(this);
slouken@0
   340
	SDL_Unlock_EventThread();
slouken@0
   341
slouken@0
   342
	return(retval);
slouken@0
   343
}
slouken@0
   344
slouken@0
   345
/* Get attribute data from glX. */
slouken@0
   346
int X11_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
slouken@0
   347
{
icculus@4092
   348
	int retval = -1;
icculus@4092
   349
	int unsupported = 0;
slouken@0
   350
	int glx_attrib = None;
slouken@0
   351
slouken@0
   352
	switch( attrib ) {
slouken@0
   353
	    case SDL_GL_RED_SIZE:
slouken@0
   354
		glx_attrib = GLX_RED_SIZE;
slouken@0
   355
		break;
slouken@0
   356
	    case SDL_GL_GREEN_SIZE:
slouken@0
   357
		glx_attrib = GLX_GREEN_SIZE;
slouken@0
   358
		break;
slouken@0
   359
	    case SDL_GL_BLUE_SIZE:
slouken@0
   360
		glx_attrib = GLX_BLUE_SIZE;
slouken@0
   361
		break;
slouken@0
   362
	    case SDL_GL_ALPHA_SIZE:
slouken@0
   363
		glx_attrib = GLX_ALPHA_SIZE;
slouken@0
   364
		break;
slouken@0
   365
	    case SDL_GL_DOUBLEBUFFER:
slouken@0
   366
		glx_attrib = GLX_DOUBLEBUFFER;
slouken@0
   367
		break;
slouken@0
   368
	    case SDL_GL_BUFFER_SIZE:
slouken@0
   369
		glx_attrib = GLX_BUFFER_SIZE;
slouken@0
   370
		break;
slouken@0
   371
	    case SDL_GL_DEPTH_SIZE:
slouken@0
   372
		glx_attrib = GLX_DEPTH_SIZE;
slouken@0
   373
		break;
slouken@0
   374
	    case SDL_GL_STENCIL_SIZE:
slouken@0
   375
		glx_attrib = GLX_STENCIL_SIZE;
slouken@0
   376
		break;
slouken@0
   377
	    case SDL_GL_ACCUM_RED_SIZE:
slouken@0
   378
		glx_attrib = GLX_ACCUM_RED_SIZE;
slouken@0
   379
		break;
slouken@0
   380
	    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@0
   381
		glx_attrib = GLX_ACCUM_GREEN_SIZE;
slouken@0
   382
		break;
slouken@0
   383
	    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@0
   384
		glx_attrib = GLX_ACCUM_BLUE_SIZE;
slouken@0
   385
		break;
slouken@0
   386
	    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@0
   387
		glx_attrib = GLX_ACCUM_ALPHA_SIZE;
slouken@0
   388
		break;
slouken@450
   389
	    case SDL_GL_STEREO:
slouken@450
   390
		glx_attrib = GLX_STEREO;
slouken@450
   391
		break;
slouken@656
   392
 	    case SDL_GL_MULTISAMPLEBUFFERS:
slouken@655
   393
 		glx_attrib = GLX_SAMPLE_BUFFERS_ARB;
slouken@655
   394
 		break;
slouken@656
   395
 	    case SDL_GL_MULTISAMPLESAMPLES:
slouken@655
   396
 		glx_attrib = GLX_SAMPLES_ARB;
slouken@655
   397
 		break;
slouken@1737
   398
 	    case SDL_GL_ACCELERATED_VISUAL:
slouken@1737
   399
		if ( glXExtensionSupported(this, "GLX_EXT_visual_rating") ) {
slouken@1737
   400
			glx_attrib = GLX_VISUAL_CAVEAT_EXT;
slouken@1737
   401
			retval = this->gl_data->glXGetConfig(GFX_Display, glx_visualinfo, glx_attrib, value);
slouken@1737
   402
			if ( *value == GLX_SLOW_VISUAL_EXT ) {
slouken@1737
   403
				*value = SDL_FALSE;
slouken@1737
   404
			} else {
slouken@1737
   405
				*value = SDL_TRUE;
slouken@1737
   406
			}
slouken@1737
   407
			return retval;
slouken@1737
   408
		} else {
icculus@4092
   409
			unsupported = 1;
slouken@1737
   410
		}
slouken@1737
   411
		break;
slouken@1736
   412
	    case SDL_GL_SWAP_CONTROL:
slouken@1736
   413
		if ( this->gl_data->glXGetSwapIntervalMESA ) {
slouken@1880
   414
			*value = this->gl_data->glXGetSwapIntervalMESA();
slouken@1880
   415
			return(0);
slouken@1736
   416
		} else {
icculus@4092
   417
			unsupported = 1;
slouken@1736
   418
		}
slouken@1736
   419
		break;
slouken@0
   420
	    default:
icculus@4092
   421
			unsupported = 1;
icculus@4092
   422
			break;
slouken@0
   423
	}
slouken@0
   424
icculus@4092
   425
	if (unsupported) {
icculus@4092
   426
		SDL_SetError("OpenGL attribute is unsupported on this system");
icculus@4092
   427
	} else {
icculus@4092
   428
		retval = this->gl_data->glXGetConfig(GFX_Display, glx_visualinfo, glx_attrib, value);
icculus@4092
   429
	}
slouken@0
   430
	return retval;
slouken@0
   431
}
slouken@0
   432
slouken@0
   433
void X11_GL_SwapBuffers(_THIS)
slouken@0
   434
{
slouken@0
   435
	this->gl_data->glXSwapBuffers(GFX_Display, SDL_Window);
slouken@0
   436
}
slouken@0
   437
slouken@1361
   438
#endif /* SDL_VIDEO_OPENGL_GLX */
slouken@0
   439
slouken@1861
   440
#define OPENGL_REQUIRS_DLOPEN
slouken@1861
   441
#if defined(OPENGL_REQUIRS_DLOPEN) && defined(SDL_LOADSO_DLOPEN)
slouken@1861
   442
#include <dlfcn.h>
slouken@1861
   443
#define GL_LoadObject(X)	dlopen(X, (RTLD_NOW|RTLD_GLOBAL))
slouken@1861
   444
#define GL_LoadFunction		dlsym
slouken@1861
   445
#define GL_UnloadObject		dlclose
slouken@1861
   446
#else
slouken@1861
   447
#define GL_LoadObject	SDL_LoadObject
slouken@1861
   448
#define GL_LoadFunction	SDL_LoadFunction
slouken@1861
   449
#define GL_UnloadObject	SDL_UnloadObject
slouken@1861
   450
#endif
slouken@1861
   451
slouken@0
   452
void X11_GL_UnloadLibrary(_THIS)
slouken@0
   453
{
slouken@1361
   454
#if SDL_VIDEO_OPENGL_GLX
slouken@0
   455
	if ( this->gl_config.driver_loaded ) {
icculus@1191
   456
slouken@1861
   457
		GL_UnloadObject(this->gl_config.dll_handle);
slouken@0
   458
slouken@0
   459
		this->gl_data->glXGetProcAddress = NULL;
slouken@0
   460
		this->gl_data->glXChooseVisual = NULL;
slouken@0
   461
		this->gl_data->glXCreateContext = NULL;
slouken@0
   462
		this->gl_data->glXDestroyContext = NULL;
slouken@0
   463
		this->gl_data->glXMakeCurrent = NULL;
slouken@0
   464
		this->gl_data->glXSwapBuffers = NULL;
slouken@1736
   465
		this->gl_data->glXSwapIntervalSGI = NULL;
slouken@1736
   466
		this->gl_data->glXSwapIntervalMESA = NULL;
slouken@1736
   467
		this->gl_data->glXGetSwapIntervalMESA = NULL;
slouken@0
   468
slouken@0
   469
		this->gl_config.dll_handle = NULL;
slouken@0
   470
		this->gl_config.driver_loaded = 0;
slouken@0
   471
	}
slouken@0
   472
#endif
slouken@0
   473
}
slouken@0
   474
slouken@1361
   475
#if SDL_VIDEO_OPENGL_GLX
icculus@1182
   476
slouken@0
   477
/* Passing a NULL path means load pointers from the application */
slouken@0
   478
int X11_GL_LoadLibrary(_THIS, const char* path) 
slouken@0
   479
{
icculus@1191
   480
	void* handle = NULL;
slouken@0
   481
icculus@1182
   482
	if ( gl_active ) {
icculus@1182
   483
		SDL_SetError("OpenGL context already created");
icculus@1182
   484
		return -1;
icculus@1182
   485
	}
icculus@1182
   486
icculus@1182
   487
	if ( path == NULL ) {
slouken@1336
   488
		path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
slouken@0
   489
		if ( path == NULL ) {
slouken@0
   490
			path = DEFAULT_OPENGL;
slouken@0
   491
		}
icculus@1191
   492
	}
icculus@1191
   493
slouken@1861
   494
	handle = GL_LoadObject(path);
slouken@1361
   495
	if ( handle == NULL ) {
patmandin@3905
   496
#if defined(OPENGL_REQUIRS_DLOPEN) && defined(SDL_LOADSO_DLOPEN)
patmandin@3905
   497
		SDL_SetError("Failed loading %s", path);
patmandin@3905
   498
#else
slouken@1361
   499
		/* SDL_LoadObject() will call SDL_SetError() for us. */
patmandin@3905
   500
#endif
slouken@1361
   501
		return -1;
slouken@0
   502
	}
slouken@0
   503
slouken@0
   504
	/* Unload the old driver and reset the pointers */
slouken@0
   505
	X11_GL_UnloadLibrary(this);
slouken@0
   506
slouken@0
   507
	/* Load new function pointers */
slouken@180
   508
	this->gl_data->glXGetProcAddress =
slouken@1861
   509
		(void *(*)(const GLubyte *)) GL_LoadFunction(handle, "glXGetProcAddressARB");
slouken@180
   510
	this->gl_data->glXChooseVisual =
slouken@1861
   511
		(XVisualInfo *(*)(Display *, int, int *)) GL_LoadFunction(handle, "glXChooseVisual");
slouken@180
   512
	this->gl_data->glXCreateContext =
slouken@1861
   513
		(GLXContext (*)(Display *, XVisualInfo *, GLXContext, int)) GL_LoadFunction(handle, "glXCreateContext");
slouken@180
   514
	this->gl_data->glXDestroyContext =
slouken@1861
   515
		(void (*)(Display *, GLXContext)) GL_LoadFunction(handle, "glXDestroyContext");
slouken@180
   516
	this->gl_data->glXMakeCurrent =
slouken@1861
   517
		(int (*)(Display *, GLXDrawable, GLXContext)) GL_LoadFunction(handle, "glXMakeCurrent");
slouken@180
   518
	this->gl_data->glXSwapBuffers =
slouken@1861
   519
		(void (*)(Display *, GLXDrawable)) GL_LoadFunction(handle, "glXSwapBuffers");
slouken@180
   520
	this->gl_data->glXGetConfig =
slouken@1861
   521
		(int (*)(Display *, XVisualInfo *, int, int *)) GL_LoadFunction(handle, "glXGetConfig");
slouken@638
   522
	this->gl_data->glXQueryExtensionsString =
slouken@1861
   523
		(const char *(*)(Display *, int)) GL_LoadFunction(handle, "glXQueryExtensionsString");
slouken@1736
   524
	this->gl_data->glXSwapIntervalSGI =
slouken@1861
   525
		(int (*)(int)) GL_LoadFunction(handle, "glXSwapIntervalSGI");
slouken@1736
   526
	this->gl_data->glXSwapIntervalMESA =
slouken@1861
   527
		(GLint (*)(unsigned)) GL_LoadFunction(handle, "glXSwapIntervalMESA");
slouken@1736
   528
	this->gl_data->glXGetSwapIntervalMESA =
slouken@1861
   529
		(GLint (*)(void)) GL_LoadFunction(handle, "glXGetSwapIntervalMESA");
slouken@1315
   530
slouken@0
   531
	if ( (this->gl_data->glXChooseVisual == NULL) || 
slouken@0
   532
	     (this->gl_data->glXCreateContext == NULL) ||
slouken@0
   533
	     (this->gl_data->glXDestroyContext == NULL) ||
slouken@0
   534
	     (this->gl_data->glXMakeCurrent == NULL) ||
slouken@0
   535
	     (this->gl_data->glXSwapBuffers == NULL) ||
slouken@638
   536
	     (this->gl_data->glXGetConfig == NULL) ||
slouken@638
   537
	     (this->gl_data->glXQueryExtensionsString == NULL)) {
slouken@0
   538
		SDL_SetError("Could not retrieve OpenGL functions");
slouken@0
   539
		return -1;
slouken@0
   540
	}
slouken@0
   541
slouken@0
   542
	this->gl_config.dll_handle = handle;
slouken@0
   543
	this->gl_config.driver_loaded = 1;
slouken@0
   544
	if ( path ) {
slouken@1379
   545
		SDL_strlcpy(this->gl_config.driver_path, path,
slouken@1379
   546
			SDL_arraysize(this->gl_config.driver_path));
slouken@0
   547
	} else {
slouken@1379
   548
		*this->gl_config.driver_path = '\0';
slouken@0
   549
	}
slouken@0
   550
	return 0;
slouken@0
   551
}
slouken@0
   552
slouken@0
   553
void *X11_GL_GetProcAddress(_THIS, const char* proc)
slouken@0
   554
{
slouken@0
   555
	void* handle;
slouken@0
   556
	
slouken@0
   557
	handle = this->gl_config.dll_handle;
slouken@0
   558
	if ( this->gl_data->glXGetProcAddress ) {
slouken@1196
   559
		return this->gl_data->glXGetProcAddress((const GLubyte *)proc);
slouken@0
   560
	}
slouken@1861
   561
	return GL_LoadFunction(handle, proc);
slouken@0
   562
}
slouken@0
   563
slouken@1361
   564
#endif /* SDL_VIDEO_OPENGL_GLX */