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