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