src/video/x11/SDL_x11opengl.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 09 Feb 2009 05:32:12 +0000
changeset 3057 089a77aebb7d
parent 3013 8cc00819c8d6
child 3100 7dc982143c06
permissions -rw-r--r--
Added test program for SDL_CreateWindowFrom()
Make sure OpenGL library is loaded before working with OpenGL windows,
even those created with SDL_CreateWindowFrom()
slouken@1952
     1
/*
slouken@1952
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@1952
     4
slouken@1952
     5
    This library is free software; you can redistribute it and/or
slouken@1952
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1952
     7
    License as published by the Free Software Foundation; either
slouken@1952
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1952
     9
slouken@1952
    10
    This library is distributed in the hope that it will be useful,
slouken@1952
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1952
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1952
    13
    Lesser General Public License for more details.
slouken@1952
    14
slouken@1952
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1952
    16
    License along with _this library; if not, write to the Free Software
slouken@1952
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1952
    18
slouken@1952
    19
    Sam Lantinga
slouken@1952
    20
    slouken@libsdl.org
slouken@1952
    21
*/
slouken@1952
    22
#include "SDL_config.h"
slouken@1952
    23
slouken@1952
    24
#include "SDL_x11video.h"
slouken@1952
    25
slouken@1952
    26
/* GLX implementation of SDL OpenGL support */
slouken@1952
    27
slouken@1952
    28
#if SDL_VIDEO_OPENGL_GLX
slouken@1952
    29
#include "SDL_loadso.h"
slouken@1952
    30
slouken@1952
    31
#if defined(__IRIX__)
slouken@1952
    32
/* IRIX doesn't have a GL library versioning system */
slouken@1952
    33
#define DEFAULT_OPENGL	"libGL.so"
slouken@1952
    34
#elif defined(__MACOSX__)
slouken@1952
    35
#define DEFAULT_OPENGL	"/usr/X11R6/lib/libGL.1.dylib"
slouken@1952
    36
#elif defined(__QNXNTO__)
slouken@1952
    37
#define DEFAULT_OPENGL	"libGL.so.3"
slouken@1952
    38
#else
slouken@1952
    39
#define DEFAULT_OPENGL	"libGL.so.1"
slouken@1952
    40
#endif
slouken@1952
    41
slouken@1952
    42
#ifndef GLX_ARB_multisample
slouken@1952
    43
#define GLX_ARB_multisample
slouken@1952
    44
#define GLX_SAMPLE_BUFFERS_ARB             100000
slouken@1952
    45
#define GLX_SAMPLES_ARB                    100001
slouken@1952
    46
#endif
slouken@1952
    47
slouken@1952
    48
#ifndef GLX_EXT_visual_rating
slouken@1952
    49
#define GLX_EXT_visual_rating
slouken@1952
    50
#define GLX_VISUAL_CAVEAT_EXT              0x20
slouken@1952
    51
#define GLX_NONE_EXT                       0x8000
slouken@1952
    52
#define GLX_SLOW_VISUAL_EXT                0x8001
slouken@1952
    53
#define GLX_NON_CONFORMANT_VISUAL_EXT      0x800D
slouken@1952
    54
#endif
slouken@1952
    55
slouken@1952
    56
#define OPENGL_REQUIRS_DLOPEN
slouken@1952
    57
#if defined(OPENGL_REQUIRS_DLOPEN) && defined(SDL_LOADSO_DLOPEN)
slouken@1952
    58
#include <dlfcn.h>
slouken@1952
    59
#define GL_LoadObject(X)	dlopen(X, (RTLD_NOW|RTLD_GLOBAL))
slouken@1952
    60
#define GL_LoadFunction		dlsym
slouken@1952
    61
#define GL_UnloadObject		dlclose
slouken@1952
    62
#else
slouken@1952
    63
#define GL_LoadObject	SDL_LoadObject
slouken@1952
    64
#define GL_LoadFunction	SDL_LoadFunction
slouken@1952
    65
#define GL_UnloadObject	SDL_UnloadObject
slouken@1952
    66
#endif
slouken@1952
    67
slouken@3057
    68
static void X11_GL_InitExtensions(_THIS);
bob@2322
    69
slouken@1952
    70
int
slouken@1952
    71
X11_GL_LoadLibrary(_THIS, const char *path)
slouken@1952
    72
{
slouken@1952
    73
    void *handle;
slouken@1952
    74
slouken@3057
    75
    /* Load the OpenGL library */
slouken@3057
    76
    if (path == NULL) {
slouken@3057
    77
        path = SDL_getenv("SDL_OPENGL_LIBRARY");
slouken@1952
    78
    }
slouken@3057
    79
    if (path == NULL) {
slouken@3057
    80
        path = DEFAULT_OPENGL;
slouken@3057
    81
    }
slouken@3057
    82
    _this->gl_config.dll_handle = SDL_LoadObject(path);
slouken@3057
    83
    if (!_this->gl_config.dll_handle) {
slouken@3057
    84
        return -1;
slouken@3057
    85
    }
slouken@3057
    86
    SDL_strlcpy(_this->gl_config.driver_path, path,
slouken@3057
    87
                SDL_arraysize(_this->gl_config.driver_path));
slouken@2244
    88
slouken@3057
    89
    /* Allocate OpenGL memory */
slouken@3057
    90
    _this->gl_data =
slouken@3057
    91
        (struct SDL_GLDriverData *) SDL_calloc(1,
slouken@3057
    92
                                               sizeof(struct
slouken@3057
    93
                                                      SDL_GLDriverData));
slouken@3057
    94
    if (!_this->gl_data) {
slouken@3057
    95
        SDL_OutOfMemory();
slouken@3057
    96
        return -1;
slouken@3057
    97
    }
slouken@3057
    98
slouken@3057
    99
    /* Load function pointers */
slouken@3057
   100
    handle = _this->gl_config.dll_handle;
slouken@1952
   101
    _this->gl_data->glXGetProcAddress =
slouken@1952
   102
        (void *(*)(const GLubyte *)) GL_LoadFunction(handle,
slouken@1952
   103
                                                     "glXGetProcAddressARB");
slouken@1952
   104
    _this->gl_data->glXChooseVisual =
slouken@1952
   105
        (XVisualInfo * (*)(Display *, int, int *)) GL_LoadFunction(handle,
slouken@1952
   106
                                                                   "glXChooseVisual");
slouken@1952
   107
    _this->gl_data->glXCreateContext =
slouken@1952
   108
        (GLXContext(*)(Display *, XVisualInfo *, GLXContext, int))
slouken@1952
   109
        GL_LoadFunction(handle, "glXCreateContext");
slouken@1952
   110
    _this->gl_data->glXDestroyContext =
slouken@1952
   111
        (void (*)(Display *, GLXContext)) GL_LoadFunction(handle,
slouken@1952
   112
                                                          "glXDestroyContext");
slouken@1952
   113
    _this->gl_data->glXMakeCurrent =
slouken@1952
   114
        (int (*)(Display *, GLXDrawable, GLXContext)) GL_LoadFunction(handle,
slouken@1952
   115
                                                                      "glXMakeCurrent");
slouken@1952
   116
    _this->gl_data->glXSwapBuffers =
slouken@1952
   117
        (void (*)(Display *, GLXDrawable)) GL_LoadFunction(handle,
slouken@1952
   118
                                                           "glXSwapBuffers");
slouken@1952
   119
slouken@1952
   120
    if (!_this->gl_data->glXChooseVisual ||
slouken@1952
   121
        !_this->gl_data->glXCreateContext ||
slouken@1952
   122
        !_this->gl_data->glXDestroyContext ||
slouken@1956
   123
        !_this->gl_data->glXMakeCurrent || !_this->gl_data->glXSwapBuffers) {
slouken@1952
   124
        SDL_SetError("Could not retrieve OpenGL functions");
slouken@1952
   125
        return -1;
slouken@1952
   126
    }
slouken@1952
   127
slouken@3057
   128
    /* Initialize extensions */
slouken@3057
   129
    X11_GL_InitExtensions(_this);
slouken@3057
   130
slouken@1952
   131
    return 0;
slouken@1952
   132
}
slouken@1952
   133
slouken@1952
   134
void *
slouken@1952
   135
X11_GL_GetProcAddress(_THIS, const char *proc)
slouken@1952
   136
{
slouken@1952
   137
    void *handle;
slouken@1952
   138
slouken@1952
   139
    handle = _this->gl_config.dll_handle;
slouken@1952
   140
    if (_this->gl_data->glXGetProcAddress) {
slouken@1952
   141
        return _this->gl_data->glXGetProcAddress((const GLubyte *) proc);
slouken@1952
   142
    }
slouken@1952
   143
    return GL_LoadFunction(handle, proc);
slouken@1952
   144
}
slouken@1952
   145
slouken@3057
   146
void
slouken@1952
   147
X11_GL_UnloadLibrary(_THIS)
slouken@1952
   148
{
slouken@3057
   149
    /* Don't actually unload the library, since it may have registered
slouken@3057
   150
     * X11 shutdown hooks, per the notes at:
slouken@3057
   151
     * http://dri.sourceforge.net/doc/DRIuserguide.html
slouken@3057
   152
     */
slouken@3057
   153
#if 0
slouken@3057
   154
    GL_UnloadObject(_this->gl_config.dll_handle);
slouken@3057
   155
    _this->gl_config.dll_handle = NULL;
slouken@3057
   156
#endif
slouken@3057
   157
slouken@3057
   158
    /* Free OpenGL memory */
slouken@3057
   159
    SDL_free(_this->gl_data);
slouken@3057
   160
    _this->gl_data = NULL;
slouken@1952
   161
}
slouken@1952
   162
slouken@1952
   163
static SDL_bool
slouken@1952
   164
HasExtension(const char *extension, const char *extensions)
slouken@1952
   165
{
slouken@1952
   166
    const char *start;
slouken@1952
   167
    const char *where, *terminator;
slouken@1952
   168
slouken@1952
   169
    /* Extension names should not have spaces. */
slouken@1952
   170
    where = SDL_strchr(extension, ' ');
slouken@1952
   171
    if (where || *extension == '\0')
slouken@1952
   172
        return SDL_FALSE;
slouken@1952
   173
slouken@1952
   174
    if (!extensions)
slouken@1952
   175
        return SDL_FALSE;
slouken@1952
   176
slouken@1952
   177
    /* It takes a bit of care to be fool-proof about parsing the
slouken@1952
   178
     * OpenGL extensions string. Don't be fooled by sub-strings,
slouken@1952
   179
     * etc. */
slouken@1952
   180
slouken@1952
   181
    start = extensions;
slouken@1952
   182
slouken@1952
   183
    for (;;) {
slouken@1952
   184
        where = SDL_strstr(start, extension);
slouken@1952
   185
        if (!where)
slouken@1952
   186
            break;
slouken@1952
   187
slouken@1952
   188
        terminator = where + SDL_strlen(extension);
slouken@1952
   189
        if (where == start || *(where - 1) == ' ')
slouken@1952
   190
            if (*terminator == ' ' || *terminator == '\0')
slouken@1952
   191
                return SDL_TRUE;
slouken@1952
   192
slouken@1952
   193
        start = terminator;
slouken@1952
   194
    }
slouken@1952
   195
    return SDL_FALSE;
slouken@1952
   196
}
slouken@1952
   197
slouken@1952
   198
static void
slouken@1952
   199
X11_GL_InitExtensions(_THIS)
slouken@1952
   200
{
slouken@1952
   201
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@1952
   202
    int screen = ((SDL_DisplayData *) SDL_CurrentDisplay.driverdata)->screen;
slouken@1952
   203
    XVisualInfo *vinfo;
slouken@1952
   204
    XSetWindowAttributes xattr;
slouken@1952
   205
    Window w;
slouken@1952
   206
    GLXContext context;
slouken@1952
   207
    const char *(*glXQueryExtensionsStringFunc) (Display *, int);
slouken@1952
   208
    const char *extensions;
slouken@1952
   209
slouken@1952
   210
    vinfo = X11_GL_GetVisual(_this, display, screen);
slouken@1952
   211
    if (!vinfo) {
slouken@1952
   212
        return;
slouken@1952
   213
    }
slouken@1952
   214
    xattr.background_pixel = 0;
slouken@1952
   215
    xattr.border_pixel = 0;
slouken@1952
   216
    xattr.colormap =
slouken@1952
   217
        XCreateColormap(display, RootWindow(display, screen), vinfo->visual,
slouken@1952
   218
                        AllocNone);
slouken@1952
   219
    w = XCreateWindow(display, RootWindow(display, screen), 0, 0, 32, 32, 0,
slouken@1952
   220
                      vinfo->depth, InputOutput, vinfo->visual,
slouken@1952
   221
                      (CWBackPixel | CWBorderPixel | CWColormap), &xattr);
slouken@1952
   222
    context = _this->gl_data->glXCreateContext(display, vinfo, NULL, True);
slouken@1952
   223
    if (context) {
slouken@1952
   224
        _this->gl_data->glXMakeCurrent(display, w, context);
slouken@1952
   225
    }
slouken@1952
   226
    XFree(vinfo);
slouken@1952
   227
slouken@1952
   228
    glXQueryExtensionsStringFunc =
slouken@1952
   229
        (const char *(*)(Display *, int)) X11_GL_GetProcAddress(_this,
slouken@1952
   230
                                                                "glXQueryExtensionsString");
slouken@1952
   231
    if (glXQueryExtensionsStringFunc) {
slouken@1952
   232
        extensions = glXQueryExtensionsStringFunc(display, screen);
slouken@1952
   233
    } else {
slouken@1952
   234
        extensions = NULL;
slouken@1952
   235
    }
slouken@1952
   236
slouken@1952
   237
    /* Check for SGI_swap_control */
icculus@2105
   238
    if (HasExtension("GLX_SGI_swap_control", extensions)) {
slouken@1952
   239
        _this->gl_data->glXSwapIntervalSGI =
slouken@1952
   240
            (int (*)(int)) X11_GL_GetProcAddress(_this, "glXSwapIntervalSGI");
slouken@1952
   241
    }
slouken@1952
   242
slouken@1952
   243
    /* Check for GLX_MESA_swap_control */
slouken@1952
   244
    if (HasExtension("GLX_MESA_swap_control", extensions)) {
slouken@1952
   245
        _this->gl_data->glXSwapIntervalMESA =
slouken@1952
   246
            (GLint(*)(unsigned)) X11_GL_GetProcAddress(_this,
slouken@1952
   247
                                                       "glXSwapIntervalMESA");
slouken@1952
   248
        _this->gl_data->glXGetSwapIntervalMESA =
slouken@1952
   249
            (GLint(*)(void)) X11_GL_GetProcAddress(_this,
slouken@1952
   250
                                                   "glXGetSwapIntervalMESA");
slouken@1952
   251
    }
slouken@1952
   252
slouken@1952
   253
    /* Check for GLX_EXT_visual_rating */
slouken@1952
   254
    if (HasExtension("GLX_EXT_visual_rating", extensions)) {
slouken@1952
   255
        _this->gl_data->HAS_GLX_EXT_visual_rating = SDL_TRUE;
slouken@1952
   256
    }
slouken@1952
   257
slouken@1952
   258
    if (context) {
slouken@1952
   259
        _this->gl_data->glXMakeCurrent(display, None, NULL);
slouken@1952
   260
        _this->gl_data->glXDestroyContext(display, context);
slouken@1952
   261
    }
slouken@1952
   262
    XDestroyWindow(display, w);
bob@2323
   263
    X11_PumpEvents(_this);
slouken@1952
   264
}
slouken@1952
   265
slouken@1952
   266
XVisualInfo *
slouken@1952
   267
X11_GL_GetVisual(_THIS, Display * display, int screen)
slouken@1952
   268
{
slouken@1952
   269
    XVisualInfo *vinfo;
slouken@1952
   270
slouken@1952
   271
    /* 64 seems nice. */
slouken@1952
   272
    int attribs[64];
slouken@1952
   273
    int i;
slouken@1952
   274
slouken@1952
   275
    /* Setup our GLX attributes according to the gl_config. */
slouken@1952
   276
    i = 0;
slouken@1952
   277
    attribs[i++] = GLX_RGBA;
slouken@1952
   278
    attribs[i++] = GLX_RED_SIZE;
slouken@1952
   279
    attribs[i++] = _this->gl_config.red_size;
slouken@1952
   280
    attribs[i++] = GLX_GREEN_SIZE;
slouken@1952
   281
    attribs[i++] = _this->gl_config.green_size;
slouken@1952
   282
    attribs[i++] = GLX_BLUE_SIZE;
slouken@1952
   283
    attribs[i++] = _this->gl_config.blue_size;
slouken@1952
   284
slouken@1952
   285
    if (_this->gl_config.alpha_size) {
slouken@1952
   286
        attribs[i++] = GLX_ALPHA_SIZE;
slouken@1952
   287
        attribs[i++] = _this->gl_config.alpha_size;
slouken@1952
   288
    }
slouken@1952
   289
slouken@1952
   290
    if (_this->gl_config.buffer_size) {
slouken@1952
   291
        attribs[i++] = GLX_BUFFER_SIZE;
slouken@1952
   292
        attribs[i++] = _this->gl_config.buffer_size;
slouken@1952
   293
    }
slouken@1952
   294
slouken@1952
   295
    if (_this->gl_config.double_buffer) {
slouken@1952
   296
        attribs[i++] = GLX_DOUBLEBUFFER;
slouken@1952
   297
    }
slouken@1952
   298
slouken@1952
   299
    attribs[i++] = GLX_DEPTH_SIZE;
slouken@1952
   300
    attribs[i++] = _this->gl_config.depth_size;
slouken@1952
   301
slouken@1952
   302
    if (_this->gl_config.stencil_size) {
slouken@1952
   303
        attribs[i++] = GLX_STENCIL_SIZE;
slouken@1952
   304
        attribs[i++] = _this->gl_config.stencil_size;
slouken@1952
   305
    }
slouken@1952
   306
slouken@1952
   307
    if (_this->gl_config.accum_red_size) {
slouken@1952
   308
        attribs[i++] = GLX_ACCUM_RED_SIZE;
slouken@1952
   309
        attribs[i++] = _this->gl_config.accum_red_size;
slouken@1952
   310
    }
slouken@1952
   311
slouken@1952
   312
    if (_this->gl_config.accum_green_size) {
slouken@1952
   313
        attribs[i++] = GLX_ACCUM_GREEN_SIZE;
slouken@1952
   314
        attribs[i++] = _this->gl_config.accum_green_size;
slouken@1952
   315
    }
slouken@1952
   316
slouken@1952
   317
    if (_this->gl_config.accum_blue_size) {
slouken@1952
   318
        attribs[i++] = GLX_ACCUM_BLUE_SIZE;
slouken@1952
   319
        attribs[i++] = _this->gl_config.accum_blue_size;
slouken@1952
   320
    }
slouken@1952
   321
slouken@1952
   322
    if (_this->gl_config.accum_alpha_size) {
slouken@1952
   323
        attribs[i++] = GLX_ACCUM_ALPHA_SIZE;
slouken@1952
   324
        attribs[i++] = _this->gl_config.accum_alpha_size;
slouken@1952
   325
    }
slouken@1952
   326
slouken@1952
   327
    if (_this->gl_config.stereo) {
slouken@1952
   328
        attribs[i++] = GLX_STEREO;
slouken@1952
   329
    }
slouken@1952
   330
slouken@1952
   331
    if (_this->gl_config.multisamplebuffers) {
slouken@1952
   332
        attribs[i++] = GLX_SAMPLE_BUFFERS_ARB;
slouken@1952
   333
        attribs[i++] = _this->gl_config.multisamplebuffers;
slouken@1952
   334
    }
slouken@1952
   335
slouken@1952
   336
    if (_this->gl_config.multisamplesamples) {
slouken@1952
   337
        attribs[i++] = GLX_SAMPLES_ARB;
slouken@1952
   338
        attribs[i++] = _this->gl_config.multisamplesamples;
slouken@1952
   339
    }
slouken@1952
   340
slouken@1952
   341
    if (_this->gl_config.accelerated >= 0
slouken@1952
   342
        && _this->gl_data->HAS_GLX_EXT_visual_rating) {
slouken@1952
   343
        attribs[i++] = GLX_VISUAL_CAVEAT_EXT;
slouken@1952
   344
        attribs[i++] = GLX_NONE_EXT;
slouken@1952
   345
    }
slouken@1952
   346
#ifdef GLX_DIRECT_COLOR         /* Try for a DirectColor visual for gamma support */
slouken@2963
   347
    if (X11_UseDirectColorVisuals()) {
slouken@1952
   348
        attribs[i++] = GLX_X_VISUAL_TYPE;
slouken@1952
   349
        attribs[i++] = GLX_DIRECT_COLOR;
slouken@1952
   350
    }
slouken@1952
   351
#endif
slouken@1952
   352
    attribs[i++] = None;
slouken@1952
   353
slouken@1952
   354
    vinfo = _this->gl_data->glXChooseVisual(display, screen, attribs);
slouken@1952
   355
#ifdef GLX_DIRECT_COLOR
slouken@2963
   356
    if (!vinfo && X11_UseDirectColorVisuals()) {        /* No DirectColor visual?  Try again.. */
slouken@1952
   357
        attribs[i - 3] = None;
slouken@1952
   358
        vinfo = _this->gl_data->glXChooseVisual(display, screen, attribs);
slouken@1952
   359
    }
slouken@1952
   360
#endif
slouken@1952
   361
    if (!vinfo) {
slouken@1952
   362
        SDL_SetError("Couldn't find matching GLX visual");
slouken@1952
   363
    }
slouken@1952
   364
    return vinfo;
slouken@1952
   365
}
slouken@1952
   366
slouken@1952
   367
SDL_GLContext
slouken@1952
   368
X11_GL_CreateContext(_THIS, SDL_Window * window)
slouken@1952
   369
{
slouken@1952
   370
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1952
   371
    Display *display = data->videodata->display;
slouken@1952
   372
    int screen =
slouken@3013
   373
        ((SDL_DisplayData *) SDL_GetDisplayFromWindow(window)->
slouken@3013
   374
         driverdata)->screen;
slouken@1952
   375
    XWindowAttributes xattr;
slouken@1952
   376
    XVisualInfo v, *vinfo;
slouken@1952
   377
    int n;
slouken@1952
   378
    GLXContext context = NULL;
slouken@1952
   379
slouken@1955
   380
    /* We do this to create a clean separation between X and GLX errors. */
slouken@1952
   381
    XSync(display, False);
slouken@1952
   382
    XGetWindowAttributes(display, data->window, &xattr);
slouken@1952
   383
    v.screen = screen;
slouken@1952
   384
    v.visualid = XVisualIDFromVisual(xattr.visual);
slouken@1952
   385
    vinfo = XGetVisualInfo(display, VisualScreenMask | VisualIDMask, &v, &n);
slouken@1952
   386
    if (vinfo) {
slouken@1952
   387
        context =
slouken@1952
   388
            _this->gl_data->glXCreateContext(display, vinfo, NULL, True);
slouken@1952
   389
        XFree(vinfo);
slouken@1952
   390
    }
slouken@1952
   391
    XSync(display, False);
slouken@1952
   392
slouken@1952
   393
    if (!context) {
slouken@1952
   394
        SDL_SetError("Could not create GL context");
slouken@2178
   395
        return NULL;
slouken@1952
   396
    }
slouken@2178
   397
slouken@2178
   398
    if (X11_GL_MakeCurrent(_this, window, context) < 0) {
slouken@2178
   399
        X11_GL_DeleteContext(_this, context);
slouken@2178
   400
        return NULL;
slouken@2178
   401
    }
slouken@2178
   402
slouken@2178
   403
    return context;
slouken@1952
   404
}
slouken@1952
   405
slouken@1952
   406
int
slouken@1952
   407
X11_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
slouken@1952
   408
{
slouken@1952
   409
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@1952
   410
    Window drawable =
slouken@1952
   411
        (window ? ((SDL_WindowData *) window->driverdata)->window : None);
slouken@1952
   412
    GLXContext glx_context = (GLXContext) context;
slouken@1952
   413
    int status;
slouken@1952
   414
slouken@1952
   415
    status = 0;
slouken@1952
   416
    if (!_this->gl_data->glXMakeCurrent(display, drawable, glx_context)) {
slouken@1952
   417
        SDL_SetError("Unable to make GL context current");
slouken@1952
   418
        status = -1;
slouken@1952
   419
    }
slouken@1952
   420
    XSync(display, False);
slouken@1952
   421
slouken@1952
   422
    return (status);
slouken@1952
   423
}
slouken@1952
   424
bob@2327
   425
/* 
bob@2327
   426
   0 is a valid argument to glxSwapIntervalMESA and setting it to 0
bob@2327
   427
   with the MESA version of the extension will undo the effect of a
bob@2327
   428
   previous call with a value that is greater than zero (or at least
bob@2327
   429
   that is what the FM says. OTOH, 0 is an invalid argument to
bob@2327
   430
   glxSwapIntervalSGI and it returns an error if you call it with 0 as
bob@2327
   431
   an argument.
bob@2327
   432
*/
bob@2327
   433
bob@2327
   434
static int swapinterval = -1;
slouken@1952
   435
int
slouken@1952
   436
X11_GL_SetSwapInterval(_THIS, int interval)
slouken@1952
   437
{
slouken@1952
   438
    int status;
slouken@1952
   439
slouken@1952
   440
    if (_this->gl_data->glXSwapIntervalMESA) {
slouken@1952
   441
        status = _this->gl_data->glXSwapIntervalMESA(interval);
slouken@1952
   442
        if (status != 0) {
slouken@1952
   443
            SDL_SetError("glxSwapIntervalMESA failed");
slouken@1952
   444
            status = -1;
bob@2327
   445
        } else {
bob@2327
   446
            swapinterval = interval;
slouken@1952
   447
        }
slouken@1952
   448
    } else if (_this->gl_data->glXSwapIntervalSGI) {
slouken@1952
   449
        status = _this->gl_data->glXSwapIntervalSGI(interval);
slouken@1952
   450
        if (status != 0) {
slouken@1952
   451
            SDL_SetError("glxSwapIntervalSGI failed");
slouken@1952
   452
            status = -1;
bob@2327
   453
        } else {
bob@2327
   454
            swapinterval = interval;
slouken@1952
   455
        }
slouken@1952
   456
    } else {
slouken@1952
   457
        SDL_Unsupported();
slouken@1952
   458
        status = -1;
slouken@1952
   459
    }
slouken@1952
   460
    return status;
slouken@1952
   461
}
slouken@1952
   462
slouken@1952
   463
int
slouken@1952
   464
X11_GL_GetSwapInterval(_THIS)
slouken@1952
   465
{
slouken@1952
   466
    if (_this->gl_data->glXGetSwapIntervalMESA) {
slouken@1952
   467
        return _this->gl_data->glXGetSwapIntervalMESA();
slouken@1952
   468
    } else {
bob@2327
   469
        return swapinterval;
slouken@1952
   470
    }
slouken@1952
   471
}
slouken@1952
   472
slouken@1952
   473
void
slouken@1952
   474
X11_GL_SwapWindow(_THIS, SDL_Window * window)
slouken@1952
   475
{
slouken@1952
   476
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1952
   477
    Display *display = data->videodata->display;
slouken@1952
   478
slouken@1952
   479
    _this->gl_data->glXSwapBuffers(display, data->window);
slouken@1952
   480
}
slouken@1952
   481
slouken@1952
   482
void
slouken@1952
   483
X11_GL_DeleteContext(_THIS, SDL_GLContext context)
slouken@1952
   484
{
slouken@1952
   485
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@1952
   486
    GLXContext glx_context = (GLXContext) context;
slouken@1952
   487
slouken@1952
   488
    _this->gl_data->glXDestroyContext(display, glx_context);
bob@2324
   489
    XSync(display, False);
slouken@1952
   490
}
slouken@1952
   491
slouken@1952
   492
#endif /* SDL_VIDEO_OPENGL_GLX */
slouken@1952
   493
slouken@1952
   494
/* vi: set ts=4 sw=4 expandtab: */