src/video/x11/SDL_x11gl.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
child 1689 109bdfe06b31
permissions -rw-r--r--
more tweaking indent options
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@1668
   414
            return this->gl_data->glXGetSwapIntervalMESA();
slouken@1662
   415
        } else {
slouken@1662
   416
            return (-1) /*(this->gl_config.swap_control > 0) */ ;
slouken@1662
   417
        }
slouken@1662
   418
        break;
slouken@1662
   419
    default:
slouken@1662
   420
        return (-1);
slouken@1662
   421
    }
slouken@0
   422
slouken@1662
   423
    retval =
slouken@1668
   424
        this->gl_data->glXGetConfig(GFX_Display, glx_visualinfo, glx_attrib,
slouken@1668
   425
                                    value);
slouken@0
   426
slouken@1662
   427
    return retval;
slouken@0
   428
}
slouken@0
   429
slouken@1662
   430
void
slouken@1668
   431
X11_GL_SwapBuffers(_THIS)
slouken@0
   432
{
slouken@1668
   433
    this->gl_data->glXSwapBuffers(GFX_Display, SDL_Window);
slouken@0
   434
}
slouken@0
   435
slouken@1361
   436
#endif /* SDL_VIDEO_OPENGL_GLX */
slouken@0
   437
slouken@1662
   438
void
slouken@1668
   439
X11_GL_UnloadLibrary(_THIS)
slouken@0
   440
{
slouken@1361
   441
#if SDL_VIDEO_OPENGL_GLX
slouken@1662
   442
    if (this->gl_config.driver_loaded) {
icculus@1191
   443
slouken@1668
   444
        SDL_UnloadObject(this->gl_config.dll_handle);
slouken@0
   445
slouken@1662
   446
        this->gl_data->glXGetProcAddress = NULL;
slouken@1662
   447
        this->gl_data->glXChooseVisual = NULL;
slouken@1662
   448
        this->gl_data->glXCreateContext = NULL;
slouken@1662
   449
        this->gl_data->glXDestroyContext = NULL;
slouken@1662
   450
        this->gl_data->glXMakeCurrent = NULL;
slouken@1662
   451
        this->gl_data->glXSwapBuffers = NULL;
slouken@1662
   452
        this->gl_data->glXSwapIntervalSGI = NULL;
slouken@1662
   453
        this->gl_data->glXSwapIntervalMESA = NULL;
slouken@1662
   454
        this->gl_data->glXGetSwapIntervalMESA = NULL;
slouken@0
   455
slouken@1662
   456
        this->gl_config.dll_handle = NULL;
slouken@1662
   457
        this->gl_config.driver_loaded = 0;
slouken@1662
   458
    }
slouken@0
   459
#endif
slouken@0
   460
}
slouken@0
   461
slouken@1361
   462
#if SDL_VIDEO_OPENGL_GLX
icculus@1182
   463
slouken@0
   464
/* Passing a NULL path means load pointers from the application */
slouken@1662
   465
int
slouken@1668
   466
X11_GL_LoadLibrary(_THIS, const char *path)
slouken@0
   467
{
slouken@1662
   468
    void *handle = NULL;
slouken@0
   469
slouken@1662
   470
    if (gl_active) {
slouken@1668
   471
        SDL_SetError("OpenGL context already created");
slouken@1662
   472
        return -1;
slouken@1662
   473
    }
icculus@1182
   474
slouken@1662
   475
    if (path == NULL) {
slouken@1668
   476
        path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
slouken@1662
   477
        if (path == NULL) {
slouken@1662
   478
            path = DEFAULT_OPENGL;
slouken@1662
   479
        }
slouken@1662
   480
    }
icculus@1191
   481
slouken@1668
   482
    handle = SDL_LoadObject(path);
slouken@1662
   483
    if (handle == NULL) {
slouken@1662
   484
        /* SDL_LoadObject() will call SDL_SetError() for us. */
slouken@1662
   485
        return -1;
slouken@1662
   486
    }
slouken@0
   487
slouken@1662
   488
    /* Unload the old driver and reset the pointers */
slouken@1668
   489
    X11_GL_UnloadLibrary(this);
slouken@0
   490
slouken@1662
   491
    /* Load new function pointers */
slouken@1662
   492
    this->gl_data->glXGetProcAddress =
slouken@1668
   493
        (void *(*)(const GLubyte *)) SDL_LoadFunction(handle,
slouken@1668
   494
                                                      "glXGetProcAddressARB");
slouken@1662
   495
    this->gl_data->glXChooseVisual =
slouken@1668
   496
        (XVisualInfo * (*)(Display *, int, int *)) SDL_LoadFunction(handle,
slouken@1668
   497
                                                                    "glXChooseVisual");
slouken@1662
   498
    this->gl_data->glXCreateContext =
slouken@1668
   499
        (GLXContext(*)(Display *, XVisualInfo *, GLXContext, int))
slouken@1668
   500
        SDL_LoadFunction(handle, "glXCreateContext");
slouken@1662
   501
    this->gl_data->glXDestroyContext =
slouken@1668
   502
        (void (*)(Display *, GLXContext)) SDL_LoadFunction(handle,
slouken@1668
   503
                                                           "glXDestroyContext");
slouken@1662
   504
    this->gl_data->glXMakeCurrent =
slouken@1662
   505
        (int (*)(Display *, GLXDrawable, GLXContext))
slouken@1668
   506
        SDL_LoadFunction(handle, "glXMakeCurrent");
slouken@1662
   507
    this->gl_data->glXSwapBuffers =
slouken@1668
   508
        (void (*)(Display *, GLXDrawable)) SDL_LoadFunction(handle,
slouken@1668
   509
                                                            "glXSwapBuffers");
slouken@1662
   510
    this->gl_data->glXGetConfig =
slouken@1662
   511
        (int (*)(Display *, XVisualInfo *, int, int *))
slouken@1668
   512
        SDL_LoadFunction(handle, "glXGetConfig");
slouken@1662
   513
    this->gl_data->glXQueryExtensionsString =
slouken@1668
   514
        (const char *(*)(Display *, int)) SDL_LoadFunction(handle,
slouken@1668
   515
                                                           "glXQueryExtensionsString");
slouken@1662
   516
    this->gl_data->glXSwapIntervalSGI =
slouken@1668
   517
        (int (*)(int)) SDL_LoadFunction(handle, "glXSwapIntervalSGI");
slouken@1662
   518
    this->gl_data->glXSwapIntervalMESA =
slouken@1668
   519
        (GLint(*)(unsigned)) SDL_LoadFunction(handle, "glXSwapIntervalMESA");
slouken@1662
   520
    this->gl_data->glXGetSwapIntervalMESA =
slouken@1668
   521
        (GLint(*)(void)) SDL_LoadFunction(handle, "glXGetSwapIntervalMESA");
slouken@1315
   522
slouken@1662
   523
    if ((this->gl_data->glXChooseVisual == NULL) ||
slouken@1662
   524
        (this->gl_data->glXCreateContext == NULL) ||
slouken@1662
   525
        (this->gl_data->glXDestroyContext == NULL) ||
slouken@1662
   526
        (this->gl_data->glXMakeCurrent == NULL) ||
slouken@1662
   527
        (this->gl_data->glXSwapBuffers == NULL) ||
slouken@1662
   528
        (this->gl_data->glXGetConfig == NULL) ||
slouken@1662
   529
        (this->gl_data->glXQueryExtensionsString == NULL)) {
slouken@1668
   530
        SDL_SetError("Could not retrieve OpenGL functions");
slouken@1662
   531
        return -1;
slouken@1662
   532
    }
slouken@0
   533
slouken@1662
   534
    this->gl_config.dll_handle = handle;
slouken@1662
   535
    this->gl_config.driver_loaded = 1;
slouken@1662
   536
    if (path) {
slouken@1668
   537
        SDL_strlcpy(this->gl_config.driver_path, path,
slouken@1668
   538
                    SDL_arraysize(this->gl_config.driver_path));
slouken@1662
   539
    } else {
slouken@1662
   540
        *this->gl_config.driver_path = '\0';
slouken@1662
   541
    }
slouken@1662
   542
    return 0;
slouken@0
   543
}
slouken@0
   544
slouken@1662
   545
void *
slouken@1668
   546
X11_GL_GetProcAddress(_THIS, const char *proc)
slouken@0
   547
{
slouken@1662
   548
    void *handle;
slouken@1662
   549
slouken@1662
   550
    handle = this->gl_config.dll_handle;
slouken@1662
   551
    if (this->gl_data->glXGetProcAddress) {
slouken@1668
   552
        return this->gl_data->glXGetProcAddress((const GLubyte *) proc);
slouken@1662
   553
    }
slouken@1668
   554
    return SDL_LoadFunction(handle, proc);
slouken@0
   555
}
slouken@0
   556
slouken@1361
   557
#endif /* SDL_VIDEO_OPENGL_GLX */
slouken@1662
   558
/* vi: set ts=4 sw=4 expandtab: */