src/video/x11/SDL_x11gl.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 08 Dec 2005 14:21:17 +0000
changeset 1196 b81f54c3963f
parent 1191 2bd4cec0de63
child 1246 ca51a76a7328
permissions -rw-r--r--
Fixed compile warnings with gcc 4
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@769
     3
    Copyright (C) 1997-2004 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@862
    37
#if defined(sgi)
slouken@862
    38
/* IRIX doesn't have a GL library versioning system */
slouken@862
    39
#define DEFAULT_OPENGL	"libGL.so"
icculus@1191
    40
#elif defined(MACOSX)
icculus@1191
    41
#define DEFAULT_OPENGL	"/usr/X11R6/lib/libGL.1.dylib"
slouken@862
    42
#else
slouken@0
    43
#define DEFAULT_OPENGL	"libGL.so.1"
slouken@862
    44
#endif
slouken@0
    45
slouken@667
    46
#ifndef GLX_ARB_multisample
slouken@667
    47
#define GLX_ARB_multisample
slouken@667
    48
#define GLX_SAMPLE_BUFFERS_ARB             100000
slouken@667
    49
#define GLX_SAMPLES_ARB                    100001
slouken@667
    50
#endif
slouken@667
    51
slouken@0
    52
/* return the preferred visual to use for openGL graphics */
slouken@0
    53
XVisualInfo *X11_GL_GetVisual(_THIS)
slouken@0
    54
{
icculus@1191
    55
#ifdef HAVE_OPENGL_X11
slouken@0
    56
	/* 64 seems nice. */
slouken@0
    57
	int attribs[64];
slouken@0
    58
	int i;
slouken@0
    59
slouken@0
    60
	/* load the gl driver from a default path */
slouken@0
    61
	if ( ! this->gl_config.driver_loaded ) {
slouken@0
    62
	        /* no driver has been loaded, use default (ourselves) */
slouken@0
    63
	        if ( X11_GL_LoadLibrary(this, NULL) < 0 ) {
slouken@0
    64
		        return NULL;
slouken@0
    65
		}
slouken@0
    66
	}
slouken@0
    67
slouken@0
    68
	/* See if we already have a window which we must use */
slouken@0
    69
	if ( SDL_windowid ) {
slouken@0
    70
		XWindowAttributes a;
slouken@0
    71
		XVisualInfo vi_in;
slouken@0
    72
		int out_count;
slouken@0
    73
icculus@1168
    74
		pXGetWindowAttributes(SDL_Display, SDL_Window, &a);
slouken@0
    75
		vi_in.screen = SDL_Screen;
icculus@1168
    76
		vi_in.visualid = pXVisualIDFromVisual(a.visual);
icculus@1168
    77
		glx_visualinfo = pXGetVisualInfo(SDL_Display,
slouken@0
    78
	                     VisualScreenMask|VisualIDMask, &vi_in, &out_count);
slouken@0
    79
		return glx_visualinfo;
slouken@0
    80
	}
slouken@0
    81
slouken@0
    82
        /* Setup our GLX attributes according to the gl_config. */
slouken@450
    83
	i = 0;
slouken@450
    84
	attribs[i++] = GLX_RGBA;
slouken@0
    85
	attribs[i++] = GLX_RED_SIZE;
slouken@0
    86
	attribs[i++] = this->gl_config.red_size;
slouken@0
    87
	attribs[i++] = GLX_GREEN_SIZE;
slouken@0
    88
	attribs[i++] = this->gl_config.green_size;
slouken@0
    89
	attribs[i++] = GLX_BLUE_SIZE;
slouken@0
    90
	attribs[i++] = this->gl_config.blue_size;
slouken@0
    91
slouken@0
    92
	if( this->gl_config.alpha_size ) {
slouken@0
    93
		attribs[i++] = GLX_ALPHA_SIZE;
slouken@0
    94
		attribs[i++] = this->gl_config.alpha_size;
slouken@0
    95
	}
slouken@0
    96
slouken@0
    97
	if( this->gl_config.buffer_size ) {
slouken@450
    98
		attribs[i++] = GLX_BUFFER_SIZE;
slouken@450
    99
		attribs[i++] = this->gl_config.buffer_size;
slouken@0
   100
	}
slouken@0
   101
slouken@0
   102
	if( this->gl_config.double_buffer ) {
slouken@0
   103
		attribs[i++] = GLX_DOUBLEBUFFER;
slouken@0
   104
	}
slouken@0
   105
slouken@0
   106
	attribs[i++] = GLX_DEPTH_SIZE;
slouken@0
   107
	attribs[i++] = this->gl_config.depth_size;
slouken@0
   108
slouken@0
   109
	if( this->gl_config.stencil_size ) {
slouken@0
   110
		attribs[i++] = GLX_STENCIL_SIZE;
slouken@0
   111
		attribs[i++] = this->gl_config.stencil_size;
slouken@0
   112
	}
slouken@0
   113
slouken@0
   114
	if( this->gl_config.accum_red_size ) {
slouken@450
   115
		attribs[i++] = GLX_ACCUM_RED_SIZE;
slouken@0
   116
		attribs[i++] = this->gl_config.accum_red_size;
slouken@0
   117
	}
slouken@0
   118
slouken@0
   119
	if( this->gl_config.accum_green_size ) {
slouken@450
   120
		attribs[i++] = GLX_ACCUM_GREEN_SIZE;
slouken@0
   121
		attribs[i++] = this->gl_config.accum_green_size;
slouken@0
   122
	}
slouken@0
   123
slouken@0
   124
	if( this->gl_config.accum_blue_size ) {
slouken@450
   125
		attribs[i++] = GLX_ACCUM_BLUE_SIZE;
slouken@0
   126
		attribs[i++] = this->gl_config.accum_blue_size;
slouken@0
   127
	}
slouken@0
   128
slouken@0
   129
	if( this->gl_config.accum_alpha_size ) {
slouken@450
   130
		attribs[i++] = GLX_ACCUM_ALPHA_SIZE;
slouken@0
   131
		attribs[i++] = this->gl_config.accum_alpha_size;
slouken@0
   132
	}
slouken@0
   133
slouken@450
   134
	if( this->gl_config.stereo ) {
slouken@450
   135
		attribs[i++] = GLX_STEREO;
slouken@450
   136
		attribs[i++] = this->gl_config.stereo;
slouken@450
   137
	}
slouken@655
   138
	
slouken@656
   139
	if( this->gl_config.multisamplebuffers ) {
slouken@655
   140
		attribs[i++] = GLX_SAMPLE_BUFFERS_ARB;
slouken@656
   141
		attribs[i++] = this->gl_config.multisamplebuffers;
slouken@655
   142
	}
slouken@655
   143
	
slouken@656
   144
	if( this->gl_config.multisamplesamples ) {
slouken@655
   145
		attribs[i++] = GLX_SAMPLES_ARB;
slouken@656
   146
		attribs[i++] = this->gl_config.multisamplesamples;
slouken@655
   147
	}
slouken@450
   148
slouken@0
   149
#ifdef GLX_DIRECT_COLOR /* Try for a DirectColor visual for gamma support */
slouken@1060
   150
	if ( !getenv("SDL_VIDEO_X11_NODIRECTCOLOR") ) {
slouken@1060
   151
		attribs[i++] = GLX_X_VISUAL_TYPE;
slouken@1060
   152
		attribs[i++] = GLX_DIRECT_COLOR;
slouken@1060
   153
	}
slouken@0
   154
#endif
slouken@0
   155
	attribs[i++] = None;
slouken@0
   156
slouken@0
   157
 	glx_visualinfo = this->gl_data->glXChooseVisual(GFX_Display, 
slouken@0
   158
						  SDL_Screen, attribs);
slouken@0
   159
#ifdef GLX_DIRECT_COLOR
slouken@1060
   160
	if( !glx_visualinfo && !getenv("SDL_VIDEO_X11_NODIRECTCOLOR") ) { /* No DirectColor visual?  Try again.. */
slouken@0
   161
		attribs[i-3] = None;
slouken@0
   162
 		glx_visualinfo = this->gl_data->glXChooseVisual(GFX_Display, 
slouken@0
   163
						  SDL_Screen, attribs);
slouken@0
   164
	}
slouken@0
   165
#endif
slouken@0
   166
	if( !glx_visualinfo ) {
slouken@0
   167
		SDL_SetError( "Couldn't find matching GLX visual");
slouken@0
   168
		return NULL;
slouken@0
   169
	}
slouken@0
   170
	return glx_visualinfo;
slouken@0
   171
#else
slouken@0
   172
	SDL_SetError("X11 driver not configured with OpenGL");
slouken@0
   173
	return NULL;
slouken@0
   174
#endif
slouken@0
   175
}
slouken@0
   176
slouken@0
   177
int X11_GL_CreateWindow(_THIS, int w, int h)
slouken@0
   178
{
slouken@0
   179
	int retval;
icculus@1191
   180
#ifdef HAVE_OPENGL_X11
slouken@0
   181
	XSetWindowAttributes attributes;
slouken@0
   182
	unsigned long mask;
slouken@0
   183
	unsigned long black;
slouken@0
   184
slouken@0
   185
	black = (glx_visualinfo->visual == DefaultVisual(SDL_Display,
slouken@0
   186
						 	SDL_Screen))
slouken@0
   187
	       	? BlackPixel(SDL_Display, SDL_Screen) : 0;
slouken@0
   188
	attributes.background_pixel = black;
slouken@0
   189
	attributes.border_pixel = black;
slouken@0
   190
	attributes.colormap = SDL_XColorMap;
slouken@0
   191
	mask = CWBackPixel | CWBorderPixel | CWColormap;
slouken@0
   192
icculus@1168
   193
	SDL_Window = pXCreateWindow(SDL_Display, WMwindow,
slouken@0
   194
			0, 0, w, h, 0, glx_visualinfo->depth,
slouken@0
   195
			InputOutput, glx_visualinfo->visual,
slouken@0
   196
			mask, &attributes);
slouken@0
   197
	if ( !SDL_Window ) {
slouken@0
   198
		SDL_SetError("Could not create window");
slouken@0
   199
		return -1;
slouken@0
   200
	}
slouken@0
   201
	retval = 0;
slouken@0
   202
#else
slouken@0
   203
	SDL_SetError("X11 driver not configured with OpenGL");
slouken@0
   204
	retval = -1;
slouken@0
   205
#endif
slouken@0
   206
	return(retval);
slouken@0
   207
}
slouken@0
   208
slouken@0
   209
int X11_GL_CreateContext(_THIS)
slouken@0
   210
{
slouken@0
   211
	int retval;
icculus@1191
   212
#ifdef HAVE_OPENGL_X11
slouken@0
   213
	/* We do this to create a clean separation between X and GLX errors. */
icculus@1168
   214
	pXSync( SDL_Display, False );
slouken@0
   215
	glx_context = this->gl_data->glXCreateContext(GFX_Display, 
slouken@0
   216
				     glx_visualinfo, NULL, True);
icculus@1168
   217
	pXSync( GFX_Display, False );
slouken@0
   218
slouken@0
   219
	if (glx_context == NULL) {
slouken@0
   220
		SDL_SetError("Could not create GL context");
slouken@0
   221
		return -1;
slouken@0
   222
	}
slouken@0
   223
slouken@0
   224
	gl_active = 1;
slouken@0
   225
#else
slouken@0
   226
	SDL_SetError("X11 driver not configured with OpenGL");
slouken@0
   227
#endif
slouken@0
   228
	if ( gl_active ) {
slouken@0
   229
		retval = 0;
slouken@0
   230
	} else {
slouken@0
   231
		retval = -1;
slouken@0
   232
	}
slouken@0
   233
	return(retval);
slouken@0
   234
}
slouken@0
   235
slouken@0
   236
void X11_GL_Shutdown(_THIS)
slouken@0
   237
{
icculus@1191
   238
#ifdef HAVE_OPENGL_X11
slouken@0
   239
	/* Clean up OpenGL */
slouken@0
   240
	if( glx_context ) {
slouken@0
   241
		this->gl_data->glXMakeCurrent(GFX_Display, None, NULL);
slouken@0
   242
slouken@0
   243
		if (glx_context != NULL)
slouken@0
   244
			this->gl_data->glXDestroyContext(GFX_Display, glx_context);
slouken@0
   245
slouken@0
   246
		if( this->gl_data->glXReleaseBuffersMESA ) {
slouken@0
   247
		    this->gl_data->glXReleaseBuffersMESA(GFX_Display,SDL_Window);
slouken@0
   248
		}
slouken@0
   249
		glx_context = NULL;
slouken@0
   250
	}
slouken@0
   251
	gl_active = 0;
icculus@1191
   252
#endif /* HAVE_OPENGL_X11 */
slouken@0
   253
}
slouken@0
   254
icculus@1191
   255
#ifdef HAVE_OPENGL_X11
slouken@0
   256
slouken@655
   257
static int ExtensionSupported(const char *extension)
slouken@638
   258
{
slouken@638
   259
	const GLubyte *extensions = NULL;
slouken@638
   260
	const GLubyte *start;
slouken@638
   261
	GLubyte *where, *terminator;
slouken@638
   262
slouken@638
   263
	/* Extension names should not have spaces. */
slouken@638
   264
	where = (GLubyte *) strchr(extension, ' ');
slouken@638
   265
	if (where || *extension == '\0')
slouken@638
   266
	      return 0;
slouken@638
   267
	
icculus@643
   268
	extensions = current_video->glGetString(GL_EXTENSIONS);
slouken@638
   269
	/* It takes a bit of care to be fool-proof about parsing the
slouken@638
   270
	 *      OpenGL extensions string. Don't be fooled by sub-strings,
slouken@638
   271
	 *           etc. */
slouken@638
   272
	
slouken@638
   273
	start = extensions;
slouken@638
   274
	
slouken@638
   275
	for (;;)
slouken@638
   276
	{
slouken@638
   277
		where = (GLubyte *) strstr((const char *) start, extension);
slouken@638
   278
		if (!where) break;
slouken@638
   279
		
slouken@638
   280
		terminator = where + strlen(extension);
slouken@638
   281
		if (where == start || *(where - 1) == ' ')
slouken@638
   282
	        if (*terminator == ' ' || *terminator == '\0') return 1;
slouken@638
   283
						  
slouken@638
   284
		start = terminator;
slouken@638
   285
	}
slouken@638
   286
	
slouken@638
   287
	return 0;
slouken@638
   288
}
slouken@638
   289
slouken@0
   290
/* Make the current context active */
slouken@0
   291
int X11_GL_MakeCurrent(_THIS)
slouken@0
   292
{
slouken@0
   293
	int retval;
slouken@638
   294
	
slouken@0
   295
	retval = 0;
slouken@0
   296
	if ( ! this->gl_data->glXMakeCurrent(GFX_Display,
slouken@0
   297
	                                     SDL_Window, glx_context) ) {
slouken@0
   298
		SDL_SetError("Unable to make GL context current");
slouken@0
   299
		retval = -1;
slouken@0
   300
	}
icculus@1168
   301
	pXSync( GFX_Display, False );
slouken@0
   302
slouken@638
   303
	/* 
slouken@638
   304
	 * The context is now current, check for glXReleaseBuffersMESA() 
slouken@638
   305
	 * extension. If extension is _not_ supported, destroy the pointer 
slouken@638
   306
	 * (to make sure it will not be called in X11_GL_Shutdown() ).
slouken@638
   307
	 * 
slouken@638
   308
	 * DRI/Mesa drivers include glXReleaseBuffersMESA() in the libGL.so, 
slouken@638
   309
	 * but there's no need to call it (is is only needed for some old 
slouken@638
   310
	 * non-DRI drivers).
slouken@638
   311
	 * 
slouken@638
   312
	 * When using for example glew (http://glew.sf.net), dlsym() for
slouken@638
   313
	 * glXReleaseBuffersMESA() returns the pointer from the glew library
slouken@638
   314
	 * (namespace conflict).
slouken@638
   315
	 *
slouken@638
   316
	 * The glXReleaseBuffersMESA() pointer in the glew is NULL, if the 
slouken@638
   317
	 * driver doesn't support this extension. So blindly calling it will
slouken@638
   318
	 * cause segfault with DRI/Mesa drivers!
slouken@638
   319
	 * 
slouken@638
   320
	 */
slouken@638
   321
	
slouken@655
   322
	if ( ! ExtensionSupported("glXReleaseBuffersMESA") ) {
slouken@655
   323
		this->gl_data->glXReleaseBuffersMESA = NULL;
slouken@655
   324
	}
slouken@655
   325
slouken@0
   326
	/* More Voodoo X server workarounds... Grr... */
slouken@0
   327
	SDL_Lock_EventThread();
slouken@0
   328
	X11_CheckDGAMouse(this);
slouken@0
   329
	SDL_Unlock_EventThread();
slouken@0
   330
slouken@0
   331
	return(retval);
slouken@0
   332
}
slouken@0
   333
slouken@0
   334
/* Get attribute data from glX. */
slouken@0
   335
int X11_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
slouken@0
   336
{
slouken@0
   337
	int retval;
slouken@0
   338
	int glx_attrib = None;
slouken@0
   339
slouken@0
   340
	switch( attrib ) {
slouken@0
   341
	    case SDL_GL_RED_SIZE:
slouken@0
   342
		glx_attrib = GLX_RED_SIZE;
slouken@0
   343
		break;
slouken@0
   344
	    case SDL_GL_GREEN_SIZE:
slouken@0
   345
		glx_attrib = GLX_GREEN_SIZE;
slouken@0
   346
		break;
slouken@0
   347
	    case SDL_GL_BLUE_SIZE:
slouken@0
   348
		glx_attrib = GLX_BLUE_SIZE;
slouken@0
   349
		break;
slouken@0
   350
	    case SDL_GL_ALPHA_SIZE:
slouken@0
   351
		glx_attrib = GLX_ALPHA_SIZE;
slouken@0
   352
		break;
slouken@0
   353
	    case SDL_GL_DOUBLEBUFFER:
slouken@0
   354
		glx_attrib = GLX_DOUBLEBUFFER;
slouken@0
   355
		break;
slouken@0
   356
	    case SDL_GL_BUFFER_SIZE:
slouken@0
   357
		glx_attrib = GLX_BUFFER_SIZE;
slouken@0
   358
		break;
slouken@0
   359
	    case SDL_GL_DEPTH_SIZE:
slouken@0
   360
		glx_attrib = GLX_DEPTH_SIZE;
slouken@0
   361
		break;
slouken@0
   362
	    case SDL_GL_STENCIL_SIZE:
slouken@0
   363
		glx_attrib = GLX_STENCIL_SIZE;
slouken@0
   364
		break;
slouken@0
   365
	    case SDL_GL_ACCUM_RED_SIZE:
slouken@0
   366
		glx_attrib = GLX_ACCUM_RED_SIZE;
slouken@0
   367
		break;
slouken@0
   368
	    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@0
   369
		glx_attrib = GLX_ACCUM_GREEN_SIZE;
slouken@0
   370
		break;
slouken@0
   371
	    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@0
   372
		glx_attrib = GLX_ACCUM_BLUE_SIZE;
slouken@0
   373
		break;
slouken@0
   374
	    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@0
   375
		glx_attrib = GLX_ACCUM_ALPHA_SIZE;
slouken@0
   376
		break;
slouken@450
   377
	    case SDL_GL_STEREO:
slouken@450
   378
		glx_attrib = GLX_STEREO;
slouken@450
   379
		break;
slouken@656
   380
 	    case SDL_GL_MULTISAMPLEBUFFERS:
slouken@655
   381
 		glx_attrib = GLX_SAMPLE_BUFFERS_ARB;
slouken@655
   382
 		break;
slouken@656
   383
 	    case SDL_GL_MULTISAMPLESAMPLES:
slouken@655
   384
 		glx_attrib = GLX_SAMPLES_ARB;
slouken@655
   385
 		break;
slouken@0
   386
	    default:
slouken@0
   387
		return(-1);
slouken@0
   388
	}
slouken@0
   389
slouken@0
   390
	retval = this->gl_data->glXGetConfig(GFX_Display, glx_visualinfo, glx_attrib, value);
slouken@0
   391
slouken@0
   392
	return retval;
slouken@0
   393
}
slouken@0
   394
slouken@0
   395
void X11_GL_SwapBuffers(_THIS)
slouken@0
   396
{
slouken@0
   397
	this->gl_data->glXSwapBuffers(GFX_Display, SDL_Window);
slouken@0
   398
}
slouken@0
   399
icculus@1191
   400
#endif /* HAVE_OPENGL_X11 */
slouken@0
   401
slouken@0
   402
void X11_GL_UnloadLibrary(_THIS)
slouken@0
   403
{
icculus@1191
   404
#ifdef HAVE_OPENGL_X11
slouken@0
   405
	if ( this->gl_config.driver_loaded ) {
icculus@1191
   406
icculus@1191
   407
		/* !!! FIXME: Can we just use SDL_UnloadObject() everywhere? */
icculus@1191
   408
		#ifdef USE_DLOPEN
slouken@0
   409
		dlclose(this->gl_config.dll_handle);
icculus@1191
   410
		#else
icculus@1191
   411
		SDL_UnloadObject(this->gl_config.dll_handle);
icculus@1191
   412
		#endif
slouken@0
   413
slouken@0
   414
		this->gl_data->glXGetProcAddress = NULL;
slouken@0
   415
		this->gl_data->glXChooseVisual = NULL;
slouken@0
   416
		this->gl_data->glXCreateContext = NULL;
slouken@0
   417
		this->gl_data->glXDestroyContext = NULL;
slouken@0
   418
		this->gl_data->glXMakeCurrent = NULL;
slouken@0
   419
		this->gl_data->glXSwapBuffers = NULL;
slouken@0
   420
slouken@0
   421
		this->gl_config.dll_handle = NULL;
slouken@0
   422
		this->gl_config.driver_loaded = 0;
slouken@0
   423
	}
slouken@0
   424
#endif
slouken@0
   425
}
slouken@0
   426
icculus@1191
   427
#ifdef HAVE_OPENGL_X11
slouken@0
   428
icculus@1191
   429
static void *do_dlsym(void *handle, const char *name)
icculus@1191
   430
{
icculus@1191
   431
	/* !!! FIXME: Can we just use SDL_LoadFunction() everywhere? */
icculus@1191
   432
#ifdef USE_DLOPEN
icculus@1191
   433
	return dlsym(handle, name);
icculus@1191
   434
#else
icculus@1191
   435
	return SDL_LoadFunction(handle, name);
icculus@1191
   436
#endif
icculus@1191
   437
}
icculus@1191
   438
icculus@1191
   439
#if defined(__OpenBSD__) && !defined(__ELF__)
icculus@1191
   440
#define do_dlsym(x,y) do_dlsym(x, "_" y)
icculus@1191
   441
#endif
icculus@1182
   442
slouken@0
   443
/* Passing a NULL path means load pointers from the application */
slouken@0
   444
int X11_GL_LoadLibrary(_THIS, const char* path) 
slouken@0
   445
{
icculus@1191
   446
	void* handle = NULL;
slouken@0
   447
icculus@1182
   448
	if ( gl_active ) {
icculus@1182
   449
		SDL_SetError("OpenGL context already created");
icculus@1182
   450
		return -1;
icculus@1182
   451
	}
icculus@1182
   452
icculus@1182
   453
	if ( path == NULL ) {
slouken@0
   454
		path = getenv("SDL_VIDEO_GL_DRIVER");
slouken@0
   455
		if ( path == NULL ) {
slouken@0
   456
			path = DEFAULT_OPENGL;
slouken@0
   457
		}
icculus@1191
   458
	}
icculus@1191
   459
icculus@1191
   460
	/* !!! FIXME: Can we just use SDL_LoadObject() everywhere? */
icculus@1191
   461
	#ifdef USE_DLOPEN
icculus@1191
   462
	{
icculus@1191
   463
		#ifdef RTLD_GLOBAL
icculus@1191
   464
			int dlopen_flags = RTLD_LAZY | RTLD_GLOBAL;
icculus@1191
   465
		#else
icculus@1191
   466
			int dlopen_flags = RTLD_LAZY;
icculus@1191
   467
		#endif
slouken@0
   468
		handle = dlopen(path, dlopen_flags);
slouken@0
   469
	}
icculus@1191
   470
	#else
icculus@1191
   471
		handle = SDL_LoadObject(path);
icculus@1191
   472
	#endif
icculus@1191
   473
slouken@0
   474
	if ( handle == NULL ) {
slouken@0
   475
		SDL_SetError("Could not load OpenGL library");
slouken@0
   476
		return -1;
slouken@0
   477
	}
slouken@0
   478
slouken@0
   479
	/* Unload the old driver and reset the pointers */
slouken@0
   480
	X11_GL_UnloadLibrary(this);
slouken@0
   481
slouken@0
   482
	/* Load new function pointers */
slouken@180
   483
	this->gl_data->glXGetProcAddress =
icculus@1191
   484
		(void *(*)(const GLubyte *)) do_dlsym(handle, "glXGetProcAddressARB");
slouken@180
   485
	this->gl_data->glXChooseVisual =
icculus@1191
   486
		(XVisualInfo *(*)(Display *, int, int *)) do_dlsym(handle, "glXChooseVisual");
slouken@180
   487
	this->gl_data->glXCreateContext =
icculus@1191
   488
		(GLXContext (*)(Display *, XVisualInfo *, GLXContext, int)) do_dlsym(handle, "glXCreateContext");
slouken@180
   489
	this->gl_data->glXDestroyContext =
icculus@1191
   490
		(void (*)(Display *, GLXContext)) do_dlsym(handle, "glXDestroyContext");
slouken@180
   491
	this->gl_data->glXMakeCurrent =
icculus@1191
   492
		(int (*)(Display *, GLXDrawable, GLXContext)) do_dlsym(handle, "glXMakeCurrent");
slouken@180
   493
	this->gl_data->glXSwapBuffers =
icculus@1191
   494
		(void (*)(Display *, GLXDrawable)) do_dlsym(handle, "glXSwapBuffers");
slouken@180
   495
	this->gl_data->glXGetConfig =
icculus@1191
   496
		(int (*)(Display *, XVisualInfo *, int, int *)) do_dlsym(handle, "glXGetConfig");
slouken@638
   497
	this->gl_data->glXQueryExtensionsString =
icculus@1191
   498
		(const char *(*)(Display *, int)) do_dlsym(handle, "glXQueryExtensionsString");
slouken@638
   499
	
slouken@0
   500
	/* We don't compare below for this in case we're not using Mesa. */
slouken@180
   501
	this->gl_data->glXReleaseBuffersMESA =
icculus@1191
   502
		(void (*)(Display *, GLXDrawable)) do_dlsym( handle, "glXReleaseBuffersMESA" );
slouken@638
   503
	
slouken@638
   504
	
slouken@0
   505
	if ( (this->gl_data->glXChooseVisual == NULL) || 
slouken@0
   506
	     (this->gl_data->glXCreateContext == NULL) ||
slouken@0
   507
	     (this->gl_data->glXDestroyContext == NULL) ||
slouken@0
   508
	     (this->gl_data->glXMakeCurrent == NULL) ||
slouken@0
   509
	     (this->gl_data->glXSwapBuffers == NULL) ||
slouken@638
   510
	     (this->gl_data->glXGetConfig == NULL) ||
slouken@638
   511
	     (this->gl_data->glXQueryExtensionsString == NULL)) {
slouken@0
   512
		SDL_SetError("Could not retrieve OpenGL functions");
slouken@0
   513
		return -1;
slouken@0
   514
	}
slouken@0
   515
slouken@0
   516
	this->gl_config.dll_handle = handle;
slouken@0
   517
	this->gl_config.driver_loaded = 1;
slouken@0
   518
	if ( path ) {
slouken@0
   519
		strncpy(this->gl_config.driver_path, path,
slouken@0
   520
			sizeof(this->gl_config.driver_path)-1);
slouken@0
   521
	} else {
slouken@0
   522
		strcpy(this->gl_config.driver_path, "");
slouken@0
   523
	}
slouken@0
   524
	return 0;
slouken@0
   525
}
slouken@0
   526
slouken@0
   527
void *X11_GL_GetProcAddress(_THIS, const char* proc)
slouken@0
   528
{
slouken@110
   529
	static char procname[1024];
slouken@0
   530
	void* handle;
slouken@110
   531
	void* retval;
slouken@0
   532
	
slouken@0
   533
	handle = this->gl_config.dll_handle;
slouken@0
   534
	if ( this->gl_data->glXGetProcAddress ) {
slouken@1196
   535
		return this->gl_data->glXGetProcAddress((const GLubyte *)proc);
slouken@0
   536
	}
slouken@110
   537
#if defined(__OpenBSD__) && !defined(__ELF__)
icculus@1191
   538
#undef do_dlsym
slouken@110
   539
#endif
icculus@1191
   540
	retval = do_dlsym(handle, proc);
slouken@110
   541
	if (!retval && strlen(proc) <= 1022) {
slouken@127
   542
		procname[0] = '_';
slouken@110
   543
		strcpy(procname + 1, proc);
icculus@1191
   544
		retval = do_dlsym(handle, procname);
slouken@110
   545
	}
slouken@110
   546
	return retval;
slouken@0
   547
}
slouken@0
   548
icculus@1191
   549
#endif /* HAVE_OPENGL_X11 */