src/video/x11/SDL_x11gl.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 01 Jul 2003 16:35:54 +0000
changeset 643 564716cfb502
parent 638 b0108e9dea53
child 646 0f2029a70548
permissions -rw-r--r--
Removed direct dependency on OpenGL (call current_video->glGetString() instead
of glGetString() directly)...otherwise we'd have to explicitly link to a
libGL. --ryan.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@297
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 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@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
#include <stdlib.h>	/* For getenv() prototype */
slouken@0
    29
#include <string.h>
slouken@0
    30
slouken@0
    31
#include "SDL_events_c.h"
slouken@0
    32
#include "SDL_error.h"
slouken@0
    33
#include "SDL_x11video.h"
slouken@0
    34
#include "SDL_x11dga_c.h"
slouken@0
    35
#include "SDL_x11gl_c.h"
slouken@0
    36
slouken@0
    37
#define DEFAULT_OPENGL	"libGL.so.1"
slouken@0
    38
slouken@0
    39
/* return the preferred visual to use for openGL graphics */
slouken@0
    40
XVisualInfo *X11_GL_GetVisual(_THIS)
slouken@0
    41
{
slouken@0
    42
#ifdef HAVE_OPENGL
slouken@0
    43
	/* 64 seems nice. */
slouken@0
    44
	int attribs[64];
slouken@0
    45
	int i;
slouken@0
    46
slouken@0
    47
	/* load the gl driver from a default path */
slouken@0
    48
	if ( ! this->gl_config.driver_loaded ) {
slouken@0
    49
	        /* no driver has been loaded, use default (ourselves) */
slouken@0
    50
	        if ( X11_GL_LoadLibrary(this, NULL) < 0 ) {
slouken@0
    51
		        return NULL;
slouken@0
    52
		}
slouken@0
    53
	}
slouken@0
    54
slouken@0
    55
	/* See if we already have a window which we must use */
slouken@0
    56
	if ( SDL_windowid ) {
slouken@0
    57
		XWindowAttributes a;
slouken@0
    58
		XVisualInfo vi_in;
slouken@0
    59
		int out_count;
slouken@0
    60
slouken@0
    61
		XGetWindowAttributes(SDL_Display, SDL_Window, &a);
slouken@0
    62
		vi_in.screen = SDL_Screen;
slouken@0
    63
		vi_in.visualid = XVisualIDFromVisual(a.visual);
slouken@0
    64
		glx_visualinfo = XGetVisualInfo(SDL_Display,
slouken@0
    65
	                     VisualScreenMask|VisualIDMask, &vi_in, &out_count);
slouken@0
    66
		return glx_visualinfo;
slouken@0
    67
	}
slouken@0
    68
slouken@0
    69
        /* Setup our GLX attributes according to the gl_config. */
slouken@450
    70
	i = 0;
slouken@450
    71
	attribs[i++] = GLX_RGBA;
slouken@0
    72
	attribs[i++] = GLX_RED_SIZE;
slouken@0
    73
	attribs[i++] = this->gl_config.red_size;
slouken@0
    74
	attribs[i++] = GLX_GREEN_SIZE;
slouken@0
    75
	attribs[i++] = this->gl_config.green_size;
slouken@0
    76
	attribs[i++] = GLX_BLUE_SIZE;
slouken@0
    77
	attribs[i++] = this->gl_config.blue_size;
slouken@0
    78
slouken@0
    79
	if( this->gl_config.alpha_size ) {
slouken@0
    80
		attribs[i++] = GLX_ALPHA_SIZE;
slouken@0
    81
		attribs[i++] = this->gl_config.alpha_size;
slouken@0
    82
	}
slouken@0
    83
slouken@0
    84
	if( this->gl_config.buffer_size ) {
slouken@450
    85
		attribs[i++] = GLX_BUFFER_SIZE;
slouken@450
    86
		attribs[i++] = this->gl_config.buffer_size;
slouken@0
    87
	}
slouken@0
    88
slouken@0
    89
	if( this->gl_config.double_buffer ) {
slouken@0
    90
		attribs[i++] = GLX_DOUBLEBUFFER;
slouken@0
    91
	}
slouken@0
    92
slouken@0
    93
	attribs[i++] = GLX_DEPTH_SIZE;
slouken@0
    94
	attribs[i++] = this->gl_config.depth_size;
slouken@0
    95
slouken@0
    96
	if( this->gl_config.stencil_size ) {
slouken@0
    97
		attribs[i++] = GLX_STENCIL_SIZE;
slouken@0
    98
		attribs[i++] = this->gl_config.stencil_size;
slouken@0
    99
	}
slouken@0
   100
slouken@0
   101
	if( this->gl_config.accum_red_size ) {
slouken@450
   102
		attribs[i++] = GLX_ACCUM_RED_SIZE;
slouken@0
   103
		attribs[i++] = this->gl_config.accum_red_size;
slouken@0
   104
	}
slouken@0
   105
slouken@0
   106
	if( this->gl_config.accum_green_size ) {
slouken@450
   107
		attribs[i++] = GLX_ACCUM_GREEN_SIZE;
slouken@0
   108
		attribs[i++] = this->gl_config.accum_green_size;
slouken@0
   109
	}
slouken@0
   110
slouken@0
   111
	if( this->gl_config.accum_blue_size ) {
slouken@450
   112
		attribs[i++] = GLX_ACCUM_BLUE_SIZE;
slouken@0
   113
		attribs[i++] = this->gl_config.accum_blue_size;
slouken@0
   114
	}
slouken@0
   115
slouken@0
   116
	if( this->gl_config.accum_alpha_size ) {
slouken@450
   117
		attribs[i++] = GLX_ACCUM_ALPHA_SIZE;
slouken@0
   118
		attribs[i++] = this->gl_config.accum_alpha_size;
slouken@0
   119
	}
slouken@0
   120
slouken@450
   121
	if( this->gl_config.stereo ) {
slouken@450
   122
		attribs[i++] = GLX_STEREO;
slouken@450
   123
		attribs[i++] = this->gl_config.stereo;
slouken@450
   124
	}
slouken@450
   125
slouken@0
   126
#ifdef GLX_DIRECT_COLOR /* Try for a DirectColor visual for gamma support */
slouken@0
   127
	attribs[i++] = GLX_X_VISUAL_TYPE;
slouken@0
   128
	attribs[i++] = GLX_DIRECT_COLOR;
slouken@0
   129
#endif
slouken@0
   130
	attribs[i++] = None;
slouken@0
   131
slouken@0
   132
 	glx_visualinfo = this->gl_data->glXChooseVisual(GFX_Display, 
slouken@0
   133
						  SDL_Screen, attribs);
slouken@0
   134
#ifdef GLX_DIRECT_COLOR
slouken@0
   135
	if( !glx_visualinfo ) { /* No DirectColor visual?  Try again.. */
slouken@0
   136
		attribs[i-3] = None;
slouken@0
   137
 		glx_visualinfo = this->gl_data->glXChooseVisual(GFX_Display, 
slouken@0
   138
						  SDL_Screen, attribs);
slouken@0
   139
	}
slouken@0
   140
#endif
slouken@0
   141
	if( !glx_visualinfo ) {
slouken@0
   142
		SDL_SetError( "Couldn't find matching GLX visual");
slouken@0
   143
		return NULL;
slouken@0
   144
	}
slouken@0
   145
	return glx_visualinfo;
slouken@0
   146
#else
slouken@0
   147
	SDL_SetError("X11 driver not configured with OpenGL");
slouken@0
   148
	return NULL;
slouken@0
   149
#endif
slouken@0
   150
}
slouken@0
   151
slouken@0
   152
int X11_GL_CreateWindow(_THIS, int w, int h)
slouken@0
   153
{
slouken@0
   154
	int retval;
slouken@0
   155
#ifdef HAVE_OPENGL
slouken@0
   156
	XSetWindowAttributes attributes;
slouken@0
   157
	unsigned long mask;
slouken@0
   158
	unsigned long black;
slouken@0
   159
slouken@0
   160
	black = (glx_visualinfo->visual == DefaultVisual(SDL_Display,
slouken@0
   161
						 	SDL_Screen))
slouken@0
   162
	       	? BlackPixel(SDL_Display, SDL_Screen) : 0;
slouken@0
   163
	attributes.background_pixel = black;
slouken@0
   164
	attributes.border_pixel = black;
slouken@0
   165
	attributes.colormap = SDL_XColorMap;
slouken@0
   166
	mask = CWBackPixel | CWBorderPixel | CWColormap;
slouken@0
   167
slouken@0
   168
	SDL_Window = XCreateWindow(SDL_Display, WMwindow,
slouken@0
   169
			0, 0, w, h, 0, glx_visualinfo->depth,
slouken@0
   170
			InputOutput, glx_visualinfo->visual,
slouken@0
   171
			mask, &attributes);
slouken@0
   172
	if ( !SDL_Window ) {
slouken@0
   173
		SDL_SetError("Could not create window");
slouken@0
   174
		return -1;
slouken@0
   175
	}
slouken@0
   176
	retval = 0;
slouken@0
   177
#else
slouken@0
   178
	SDL_SetError("X11 driver not configured with OpenGL");
slouken@0
   179
	retval = -1;
slouken@0
   180
#endif
slouken@0
   181
	return(retval);
slouken@0
   182
}
slouken@0
   183
slouken@0
   184
int X11_GL_CreateContext(_THIS)
slouken@0
   185
{
slouken@0
   186
	int retval;
slouken@0
   187
#ifdef HAVE_OPENGL
slouken@0
   188
	/* We do this to create a clean separation between X and GLX errors. */
slouken@0
   189
	XSync( SDL_Display, False );
slouken@0
   190
	glx_context = this->gl_data->glXCreateContext(GFX_Display, 
slouken@0
   191
				     glx_visualinfo, NULL, True);
slouken@0
   192
	XSync( GFX_Display, False );
slouken@0
   193
slouken@0
   194
	if (glx_context == NULL) {
slouken@0
   195
		SDL_SetError("Could not create GL context");
slouken@0
   196
		return -1;
slouken@0
   197
	}
slouken@0
   198
slouken@0
   199
	gl_active = 1;
slouken@0
   200
#else
slouken@0
   201
	SDL_SetError("X11 driver not configured with OpenGL");
slouken@0
   202
#endif
slouken@0
   203
	if ( gl_active ) {
slouken@0
   204
		retval = 0;
slouken@0
   205
	} else {
slouken@0
   206
		retval = -1;
slouken@0
   207
	}
slouken@0
   208
	return(retval);
slouken@0
   209
}
slouken@0
   210
slouken@0
   211
void X11_GL_Shutdown(_THIS)
slouken@0
   212
{
slouken@0
   213
#ifdef HAVE_OPENGL
slouken@0
   214
	/* Clean up OpenGL */
slouken@0
   215
	if( glx_context ) {
slouken@0
   216
		this->gl_data->glXMakeCurrent(GFX_Display, None, NULL);
slouken@0
   217
slouken@0
   218
		if (glx_context != NULL)
slouken@0
   219
			this->gl_data->glXDestroyContext(GFX_Display, glx_context);
slouken@0
   220
slouken@0
   221
		if( this->gl_data->glXReleaseBuffersMESA ) {
slouken@0
   222
		    this->gl_data->glXReleaseBuffersMESA(GFX_Display,SDL_Window);
slouken@0
   223
		}
slouken@0
   224
		glx_context = NULL;
slouken@0
   225
	}
slouken@0
   226
	gl_active = 0;
slouken@0
   227
#endif /* HAVE_OPENGL */
slouken@0
   228
}
slouken@0
   229
slouken@0
   230
#ifdef HAVE_OPENGL
slouken@0
   231
slouken@638
   232
static int ExtensionSupported(const char *extension, const char *all_extensions)
slouken@638
   233
{
slouken@638
   234
	const GLubyte *extensions = NULL;
slouken@638
   235
	const GLubyte *start;
slouken@638
   236
	GLubyte *where, *terminator;
slouken@638
   237
slouken@638
   238
	/* Extension names should not have spaces. */
slouken@638
   239
	where = (GLubyte *) strchr(extension, ' ');
slouken@638
   240
	if (where || *extension == '\0')
slouken@638
   241
	      return 0;
slouken@638
   242
	
icculus@643
   243
	extensions = current_video->glGetString(GL_EXTENSIONS);
slouken@638
   244
	/* It takes a bit of care to be fool-proof about parsing the
slouken@638
   245
	 *      OpenGL extensions string. Don't be fooled by sub-strings,
slouken@638
   246
	 *           etc. */
slouken@638
   247
	
slouken@638
   248
	start = extensions;
slouken@638
   249
	
slouken@638
   250
	for (;;)
slouken@638
   251
	{
slouken@638
   252
		where = (GLubyte *) strstr((const char *) start, extension);
slouken@638
   253
		if (!where) break;
slouken@638
   254
		
slouken@638
   255
		terminator = where + strlen(extension);
slouken@638
   256
		if (where == start || *(where - 1) == ' ')
slouken@638
   257
	        if (*terminator == ' ' || *terminator == '\0') return 1;
slouken@638
   258
						  
slouken@638
   259
		start = terminator;
slouken@638
   260
	}
slouken@638
   261
	
slouken@638
   262
	return 0;
slouken@638
   263
}
slouken@638
   264
slouken@0
   265
/* Make the current context active */
slouken@0
   266
int X11_GL_MakeCurrent(_THIS)
slouken@0
   267
{
slouken@0
   268
	int retval;
slouken@638
   269
	const char *glx_extensions;
slouken@638
   270
	
slouken@0
   271
	retval = 0;
slouken@0
   272
	if ( ! this->gl_data->glXMakeCurrent(GFX_Display,
slouken@0
   273
	                                     SDL_Window, glx_context) ) {
slouken@0
   274
		SDL_SetError("Unable to make GL context current");
slouken@0
   275
		retval = -1;
slouken@0
   276
	}
slouken@0
   277
	XSync( GFX_Display, False );
slouken@0
   278
slouken@638
   279
	
slouken@638
   280
	/* 
slouken@638
   281
	 * The context is now current, check for glXReleaseBuffersMESA() 
slouken@638
   282
	 * extension. If extension is _not_ supported, destroy the pointer 
slouken@638
   283
	 * (to make sure it will not be called in X11_GL_Shutdown() ).
slouken@638
   284
	 * 
slouken@638
   285
	 * DRI/Mesa drivers include glXReleaseBuffersMESA() in the libGL.so, 
slouken@638
   286
	 * but there's no need to call it (is is only needed for some old 
slouken@638
   287
	 * non-DRI drivers).
slouken@638
   288
	 * 
slouken@638
   289
	 * When using for example glew (http://glew.sf.net), dlsym() for
slouken@638
   290
	 * glXReleaseBuffersMESA() returns the pointer from the glew library
slouken@638
   291
	 * (namespace conflict).
slouken@638
   292
	 *
slouken@638
   293
	 * The glXReleaseBuffersMESA() pointer in the glew is NULL, if the 
slouken@638
   294
	 * driver doesn't support this extension. So blindly calling it will
slouken@638
   295
	 * cause segfault with DRI/Mesa drivers!
slouken@638
   296
	 * 
slouken@638
   297
	 */
slouken@638
   298
	
slouken@638
   299
	glx_extensions = this->gl_data->glXQueryExtensionsString(GFX_Display, SDL_Screen);
slouken@638
   300
	if (!ExtensionSupported("glXReleaseBuffersMESA", glx_extensions)) this->gl_data->glXReleaseBuffersMESA = NULL;
slouken@638
   301
	
slouken@638
   302
	
slouken@0
   303
	/* More Voodoo X server workarounds... Grr... */
slouken@0
   304
	SDL_Lock_EventThread();
slouken@0
   305
	X11_CheckDGAMouse(this);
slouken@0
   306
	SDL_Unlock_EventThread();
slouken@0
   307
slouken@0
   308
	return(retval);
slouken@0
   309
}
slouken@0
   310
slouken@0
   311
/* Get attribute data from glX. */
slouken@0
   312
int X11_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
slouken@0
   313
{
slouken@0
   314
	int retval;
slouken@0
   315
	int glx_attrib = None;
slouken@0
   316
slouken@0
   317
	switch( attrib ) {
slouken@0
   318
	    case SDL_GL_RED_SIZE:
slouken@0
   319
		glx_attrib = GLX_RED_SIZE;
slouken@0
   320
		break;
slouken@0
   321
	    case SDL_GL_GREEN_SIZE:
slouken@0
   322
		glx_attrib = GLX_GREEN_SIZE;
slouken@0
   323
		break;
slouken@0
   324
	    case SDL_GL_BLUE_SIZE:
slouken@0
   325
		glx_attrib = GLX_BLUE_SIZE;
slouken@0
   326
		break;
slouken@0
   327
	    case SDL_GL_ALPHA_SIZE:
slouken@0
   328
		glx_attrib = GLX_ALPHA_SIZE;
slouken@0
   329
		break;
slouken@0
   330
	    case SDL_GL_DOUBLEBUFFER:
slouken@0
   331
		glx_attrib = GLX_DOUBLEBUFFER;
slouken@0
   332
		break;
slouken@0
   333
	    case SDL_GL_BUFFER_SIZE:
slouken@0
   334
		glx_attrib = GLX_BUFFER_SIZE;
slouken@0
   335
		break;
slouken@0
   336
	    case SDL_GL_DEPTH_SIZE:
slouken@0
   337
		glx_attrib = GLX_DEPTH_SIZE;
slouken@0
   338
		break;
slouken@0
   339
	    case SDL_GL_STENCIL_SIZE:
slouken@0
   340
		glx_attrib = GLX_STENCIL_SIZE;
slouken@0
   341
		break;
slouken@0
   342
	    case SDL_GL_ACCUM_RED_SIZE:
slouken@0
   343
		glx_attrib = GLX_ACCUM_RED_SIZE;
slouken@0
   344
		break;
slouken@0
   345
	    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@0
   346
		glx_attrib = GLX_ACCUM_GREEN_SIZE;
slouken@0
   347
		break;
slouken@0
   348
	    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@0
   349
		glx_attrib = GLX_ACCUM_BLUE_SIZE;
slouken@0
   350
		break;
slouken@0
   351
	    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@0
   352
		glx_attrib = GLX_ACCUM_ALPHA_SIZE;
slouken@0
   353
		break;
slouken@450
   354
	    case SDL_GL_STEREO:
slouken@450
   355
		glx_attrib = GLX_STEREO;
slouken@450
   356
		break;
slouken@0
   357
	    default:
slouken@0
   358
		return(-1);
slouken@0
   359
	}
slouken@0
   360
slouken@0
   361
	retval = this->gl_data->glXGetConfig(GFX_Display, glx_visualinfo, glx_attrib, value);
slouken@0
   362
slouken@0
   363
	return retval;
slouken@0
   364
}
slouken@0
   365
slouken@0
   366
void X11_GL_SwapBuffers(_THIS)
slouken@0
   367
{
slouken@0
   368
	this->gl_data->glXSwapBuffers(GFX_Display, SDL_Window);
slouken@0
   369
}
slouken@0
   370
slouken@0
   371
#endif /* HAVE_OPENGL */
slouken@0
   372
slouken@0
   373
void X11_GL_UnloadLibrary(_THIS)
slouken@0
   374
{
slouken@0
   375
#ifdef HAVE_OPENGL
slouken@0
   376
	if ( this->gl_config.driver_loaded ) {
slouken@0
   377
		dlclose(this->gl_config.dll_handle);
slouken@0
   378
slouken@0
   379
		this->gl_data->glXGetProcAddress = NULL;
slouken@0
   380
		this->gl_data->glXChooseVisual = NULL;
slouken@0
   381
		this->gl_data->glXCreateContext = NULL;
slouken@0
   382
		this->gl_data->glXDestroyContext = NULL;
slouken@0
   383
		this->gl_data->glXMakeCurrent = NULL;
slouken@0
   384
		this->gl_data->glXSwapBuffers = NULL;
slouken@0
   385
slouken@0
   386
		this->gl_config.dll_handle = NULL;
slouken@0
   387
		this->gl_config.driver_loaded = 0;
slouken@0
   388
	}
slouken@0
   389
#endif
slouken@0
   390
}
slouken@0
   391
slouken@0
   392
#ifdef HAVE_OPENGL
slouken@0
   393
slouken@0
   394
/* Passing a NULL path means load pointers from the application */
slouken@0
   395
int X11_GL_LoadLibrary(_THIS, const char* path) 
slouken@0
   396
{
slouken@0
   397
	void* handle;
slouken@0
   398
	int dlopen_flags;
slouken@0
   399
slouken@0
   400
 	if ( gl_active ) {
slouken@0
   401
 		SDL_SetError("OpenGL context already created");
slouken@0
   402
 		return -1;
slouken@0
   403
 	}
slouken@0
   404
slouken@0
   405
#ifdef RTLD_GLOBAL
slouken@0
   406
	dlopen_flags = RTLD_LAZY | RTLD_GLOBAL;
slouken@0
   407
#else
slouken@0
   408
	dlopen_flags = RTLD_LAZY;
slouken@0
   409
#endif
slouken@0
   410
	handle = dlopen(path, dlopen_flags);
slouken@0
   411
	/* Catch the case where the application isn't linked with GL */
slouken@0
   412
	if ( (dlsym(handle, "glXChooseVisual") == NULL) && (path == NULL) ) {
slouken@0
   413
		dlclose(handle);
slouken@0
   414
		path = getenv("SDL_VIDEO_GL_DRIVER");
slouken@0
   415
		if ( path == NULL ) {
slouken@0
   416
			path = DEFAULT_OPENGL;
slouken@0
   417
		}
slouken@0
   418
		handle = dlopen(path, dlopen_flags);
slouken@0
   419
	}
slouken@0
   420
	if ( handle == NULL ) {
slouken@0
   421
		SDL_SetError("Could not load OpenGL library");
slouken@0
   422
		return -1;
slouken@0
   423
	}
slouken@0
   424
slouken@0
   425
	/* Unload the old driver and reset the pointers */
slouken@0
   426
	X11_GL_UnloadLibrary(this);
slouken@0
   427
slouken@0
   428
	/* Load new function pointers */
slouken@180
   429
	this->gl_data->glXGetProcAddress =
slouken@180
   430
		(void *(*)(const GLubyte *)) dlsym(handle, "glXGetProcAddressARB");
slouken@180
   431
	this->gl_data->glXChooseVisual =
slouken@180
   432
		(XVisualInfo *(*)(Display *, int, int *)) dlsym(handle, "glXChooseVisual");
slouken@180
   433
	this->gl_data->glXCreateContext =
slouken@180
   434
		(GLXContext (*)(Display *, XVisualInfo *, GLXContext, int)) dlsym(handle, "glXCreateContext");
slouken@180
   435
	this->gl_data->glXDestroyContext =
slouken@180
   436
		(void (*)(Display *, GLXContext)) dlsym(handle, "glXDestroyContext");
slouken@180
   437
	this->gl_data->glXMakeCurrent =
slouken@180
   438
		(int (*)(Display *, GLXDrawable, GLXContext)) dlsym(handle, "glXMakeCurrent");
slouken@180
   439
	this->gl_data->glXSwapBuffers =
slouken@180
   440
		(void (*)(Display *, GLXDrawable)) dlsym(handle, "glXSwapBuffers");
slouken@180
   441
	this->gl_data->glXGetConfig =
slouken@180
   442
		(int (*)(Display *, XVisualInfo *, int, int *)) dlsym(handle, "glXGetConfig");
slouken@638
   443
	this->gl_data->glXQueryExtensionsString =
slouken@638
   444
		(const char (*)(Display *, int)) dlsym(handle, "glXQueryExtensionsString");
slouken@638
   445
	
slouken@0
   446
	/* We don't compare below for this in case we're not using Mesa. */
slouken@180
   447
	this->gl_data->glXReleaseBuffersMESA =
slouken@180
   448
		(void (*)(Display *, GLXDrawable)) dlsym( handle, "glXReleaseBuffersMESA" );
slouken@638
   449
	
slouken@638
   450
	
slouken@0
   451
	if ( (this->gl_data->glXChooseVisual == NULL) || 
slouken@0
   452
	     (this->gl_data->glXCreateContext == NULL) ||
slouken@0
   453
	     (this->gl_data->glXDestroyContext == NULL) ||
slouken@0
   454
	     (this->gl_data->glXMakeCurrent == NULL) ||
slouken@0
   455
	     (this->gl_data->glXSwapBuffers == NULL) ||
slouken@638
   456
	     (this->gl_data->glXGetConfig == NULL) ||
slouken@638
   457
	     (this->gl_data->glXQueryExtensionsString == NULL)) {
slouken@0
   458
		SDL_SetError("Could not retrieve OpenGL functions");
slouken@0
   459
		return -1;
slouken@0
   460
	}
slouken@0
   461
slouken@0
   462
	this->gl_config.dll_handle = handle;
slouken@0
   463
	this->gl_config.driver_loaded = 1;
slouken@0
   464
	if ( path ) {
slouken@0
   465
		strncpy(this->gl_config.driver_path, path,
slouken@0
   466
			sizeof(this->gl_config.driver_path)-1);
slouken@0
   467
	} else {
slouken@0
   468
		strcpy(this->gl_config.driver_path, "");
slouken@0
   469
	}
slouken@0
   470
	return 0;
slouken@0
   471
}
slouken@0
   472
slouken@0
   473
void *X11_GL_GetProcAddress(_THIS, const char* proc)
slouken@0
   474
{
slouken@110
   475
	static char procname[1024];
slouken@0
   476
	void* handle;
slouken@110
   477
	void* retval;
slouken@0
   478
	
slouken@0
   479
	handle = this->gl_config.dll_handle;
slouken@0
   480
	if ( this->gl_data->glXGetProcAddress ) {
slouken@566
   481
		return this->gl_data->glXGetProcAddress(proc);
slouken@0
   482
	}
slouken@110
   483
#if defined(__OpenBSD__) && !defined(__ELF__)
slouken@110
   484
#undef dlsym(x,y);
slouken@110
   485
#endif
slouken@110
   486
	retval = dlsym(handle, proc);
slouken@110
   487
	if (!retval && strlen(proc) <= 1022) {
slouken@127
   488
		procname[0] = '_';
slouken@110
   489
		strcpy(procname + 1, proc);
slouken@110
   490
		retval = dlsym(handle, procname);
slouken@110
   491
	}
slouken@110
   492
	return retval;
slouken@0
   493
}
slouken@0
   494
slouken@0
   495
#endif /* HAVE_OPENGL */