src/video/x11/SDL_x11opengl.c
author Bob Pendleton <bob@pendleton.com>
Fri, 09 Jan 2009 20:43:30 +0000
changeset 3011 8f4ed5ec2b06
parent 2963 ee331407574f
child 3013 8cc00819c8d6
permissions -rw-r--r--
I ran a global "make indent" it modified the following files.
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
bob@2322
    68
static int X11_GL_InitializeMemory(_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@1952
    75
    if (_this->gl_config.driver_loaded) {
slouken@2723
    76
        /* do not return without reinitializing the function hooks */
slouken@1952
    77
        if (path) {
slouken@1952
    78
            SDL_SetError("OpenGL library already loaded");
slouken@2723
    79
        }
slouken@2723
    80
        handle = _this->gl_config.dll_handle;
slouken@2723
    81
    } else {
slouken@2723
    82
        if (path == NULL) {
slouken@2723
    83
            path = SDL_getenv("SDL_OPENGL_LIBRARY");
slouken@2723
    84
        }
slouken@2723
    85
        if (path == NULL) {
slouken@2723
    86
            path = DEFAULT_OPENGL;
slouken@2723
    87
        }
slouken@2723
    88
        handle = GL_LoadObject(path);
slouken@2723
    89
        if (!handle) {
slouken@1952
    90
            return -1;
slouken@1952
    91
        }
slouken@2723
    92
        _this->gl_config.dll_handle = handle;
slouken@2723
    93
        SDL_strlcpy(_this->gl_config.driver_path, path,
slouken@2723
    94
                    SDL_arraysize(_this->gl_config.driver_path));
slouken@1952
    95
    }
bob@2322
    96
    X11_GL_InitializeMemory(_this);
slouken@2244
    97
slouken@1952
    98
    /* Load new function pointers */
slouken@1952
    99
    _this->gl_data->glXGetProcAddress =
slouken@1952
   100
        (void *(*)(const GLubyte *)) GL_LoadFunction(handle,
slouken@1952
   101
                                                     "glXGetProcAddressARB");
slouken@1952
   102
    _this->gl_data->glXChooseVisual =
slouken@1952
   103
        (XVisualInfo * (*)(Display *, int, int *)) GL_LoadFunction(handle,
slouken@1952
   104
                                                                   "glXChooseVisual");
slouken@1952
   105
    _this->gl_data->glXCreateContext =
slouken@1952
   106
        (GLXContext(*)(Display *, XVisualInfo *, GLXContext, int))
slouken@1952
   107
        GL_LoadFunction(handle, "glXCreateContext");
slouken@1952
   108
    _this->gl_data->glXDestroyContext =
slouken@1952
   109
        (void (*)(Display *, GLXContext)) GL_LoadFunction(handle,
slouken@1952
   110
                                                          "glXDestroyContext");
slouken@1952
   111
    _this->gl_data->glXMakeCurrent =
slouken@1952
   112
        (int (*)(Display *, GLXDrawable, GLXContext)) GL_LoadFunction(handle,
slouken@1952
   113
                                                                      "glXMakeCurrent");
slouken@1952
   114
    _this->gl_data->glXSwapBuffers =
slouken@1952
   115
        (void (*)(Display *, GLXDrawable)) GL_LoadFunction(handle,
slouken@1952
   116
                                                           "glXSwapBuffers");
slouken@1952
   117
slouken@1952
   118
    if (!_this->gl_data->glXChooseVisual ||
slouken@1952
   119
        !_this->gl_data->glXCreateContext ||
slouken@1952
   120
        !_this->gl_data->glXDestroyContext ||
slouken@1956
   121
        !_this->gl_data->glXMakeCurrent || !_this->gl_data->glXSwapBuffers) {
slouken@1952
   122
        SDL_SetError("Could not retrieve OpenGL functions");
slouken@1952
   123
        return -1;
slouken@1952
   124
    }
slouken@1952
   125
slouken@2723
   126
    ++_this->gl_config.driver_loaded;
slouken@1952
   127
    return 0;
slouken@1952
   128
}
slouken@1952
   129
slouken@1952
   130
void *
slouken@1952
   131
X11_GL_GetProcAddress(_THIS, const char *proc)
slouken@1952
   132
{
slouken@1952
   133
    void *handle;
slouken@1952
   134
slouken@1952
   135
    handle = _this->gl_config.dll_handle;
slouken@1952
   136
    if (_this->gl_data->glXGetProcAddress) {
slouken@1952
   137
        return _this->gl_data->glXGetProcAddress((const GLubyte *) proc);
slouken@1952
   138
    }
slouken@1952
   139
    return GL_LoadFunction(handle, proc);
slouken@1952
   140
}
slouken@1952
   141
slouken@1952
   142
static void
slouken@1952
   143
X11_GL_UnloadLibrary(_THIS)
slouken@1952
   144
{
slouken@1952
   145
    if (_this->gl_config.driver_loaded > 0) {
slouken@1952
   146
        if (--_this->gl_config.driver_loaded > 0) {
slouken@1952
   147
            return;
slouken@1952
   148
        }
slouken@1952
   149
        GL_UnloadObject(_this->gl_config.dll_handle);
slouken@1952
   150
        _this->gl_config.dll_handle = NULL;
slouken@1952
   151
    }
slouken@1952
   152
}
slouken@1952
   153
slouken@1952
   154
static SDL_bool
slouken@1952
   155
HasExtension(const char *extension, const char *extensions)
slouken@1952
   156
{
slouken@1952
   157
    const char *start;
slouken@1952
   158
    const char *where, *terminator;
slouken@1952
   159
slouken@1952
   160
    /* Extension names should not have spaces. */
slouken@1952
   161
    where = SDL_strchr(extension, ' ');
slouken@1952
   162
    if (where || *extension == '\0')
slouken@1952
   163
        return SDL_FALSE;
slouken@1952
   164
slouken@1952
   165
    if (!extensions)
slouken@1952
   166
        return SDL_FALSE;
slouken@1952
   167
slouken@1952
   168
    /* It takes a bit of care to be fool-proof about parsing the
slouken@1952
   169
     * OpenGL extensions string. Don't be fooled by sub-strings,
slouken@1952
   170
     * etc. */
slouken@1952
   171
slouken@1952
   172
    start = extensions;
slouken@1952
   173
slouken@1952
   174
    for (;;) {
slouken@1952
   175
        where = SDL_strstr(start, extension);
slouken@1952
   176
        if (!where)
slouken@1952
   177
            break;
slouken@1952
   178
slouken@1952
   179
        terminator = where + SDL_strlen(extension);
slouken@1952
   180
        if (where == start || *(where - 1) == ' ')
slouken@1952
   181
            if (*terminator == ' ' || *terminator == '\0')
slouken@1952
   182
                return SDL_TRUE;
slouken@1952
   183
slouken@1952
   184
        start = terminator;
slouken@1952
   185
    }
slouken@1952
   186
    return SDL_FALSE;
slouken@1952
   187
}
slouken@1952
   188
slouken@1952
   189
static void
slouken@1952
   190
X11_GL_InitExtensions(_THIS)
slouken@1952
   191
{
slouken@1952
   192
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@1952
   193
    int screen = ((SDL_DisplayData *) SDL_CurrentDisplay.driverdata)->screen;
slouken@1952
   194
    XVisualInfo *vinfo;
slouken@1952
   195
    XSetWindowAttributes xattr;
slouken@1952
   196
    Window w;
slouken@1952
   197
    GLXContext context;
slouken@1952
   198
    const char *(*glXQueryExtensionsStringFunc) (Display *, int);
slouken@1952
   199
    const char *extensions;
slouken@1952
   200
slouken@1952
   201
    vinfo = X11_GL_GetVisual(_this, display, screen);
slouken@1952
   202
    if (!vinfo) {
slouken@1952
   203
        return;
slouken@1952
   204
    }
slouken@1952
   205
    xattr.background_pixel = 0;
slouken@1952
   206
    xattr.border_pixel = 0;
slouken@1952
   207
    xattr.colormap =
slouken@1952
   208
        XCreateColormap(display, RootWindow(display, screen), vinfo->visual,
slouken@1952
   209
                        AllocNone);
slouken@1952
   210
    w = XCreateWindow(display, RootWindow(display, screen), 0, 0, 32, 32, 0,
slouken@1952
   211
                      vinfo->depth, InputOutput, vinfo->visual,
slouken@1952
   212
                      (CWBackPixel | CWBorderPixel | CWColormap), &xattr);
slouken@1952
   213
    context = _this->gl_data->glXCreateContext(display, vinfo, NULL, True);
slouken@1952
   214
    if (context) {
slouken@1952
   215
        _this->gl_data->glXMakeCurrent(display, w, context);
slouken@1952
   216
    }
slouken@1952
   217
    XFree(vinfo);
slouken@1952
   218
slouken@1952
   219
    glXQueryExtensionsStringFunc =
slouken@1952
   220
        (const char *(*)(Display *, int)) X11_GL_GetProcAddress(_this,
slouken@1952
   221
                                                                "glXQueryExtensionsString");
slouken@1952
   222
    if (glXQueryExtensionsStringFunc) {
slouken@1952
   223
        extensions = glXQueryExtensionsStringFunc(display, screen);
slouken@1952
   224
    } else {
slouken@1952
   225
        extensions = NULL;
slouken@1952
   226
    }
slouken@1952
   227
slouken@1952
   228
    /* Check for SGI_swap_control */
icculus@2105
   229
    if (HasExtension("GLX_SGI_swap_control", extensions)) {
slouken@1952
   230
        _this->gl_data->glXSwapIntervalSGI =
slouken@1952
   231
            (int (*)(int)) X11_GL_GetProcAddress(_this, "glXSwapIntervalSGI");
slouken@1952
   232
    }
slouken@1952
   233
slouken@1952
   234
    /* Check for GLX_MESA_swap_control */
slouken@1952
   235
    if (HasExtension("GLX_MESA_swap_control", extensions)) {
slouken@1952
   236
        _this->gl_data->glXSwapIntervalMESA =
slouken@1952
   237
            (GLint(*)(unsigned)) X11_GL_GetProcAddress(_this,
slouken@1952
   238
                                                       "glXSwapIntervalMESA");
slouken@1952
   239
        _this->gl_data->glXGetSwapIntervalMESA =
slouken@1952
   240
            (GLint(*)(void)) X11_GL_GetProcAddress(_this,
slouken@1952
   241
                                                   "glXGetSwapIntervalMESA");
slouken@1952
   242
    }
slouken@1952
   243
slouken@1952
   244
    /* Check for GLX_EXT_visual_rating */
slouken@1952
   245
    if (HasExtension("GLX_EXT_visual_rating", extensions)) {
slouken@1952
   246
        _this->gl_data->HAS_GLX_EXT_visual_rating = SDL_TRUE;
slouken@1952
   247
    }
slouken@1952
   248
slouken@1952
   249
    if (context) {
slouken@1952
   250
        _this->gl_data->glXMakeCurrent(display, None, NULL);
slouken@1952
   251
        _this->gl_data->glXDestroyContext(display, context);
slouken@1952
   252
    }
slouken@1952
   253
    XDestroyWindow(display, w);
bob@2323
   254
    X11_PumpEvents(_this);
slouken@1952
   255
}
slouken@1952
   256
bob@2322
   257
static int
bob@2322
   258
X11_GL_InitializeMemory(_THIS)
slouken@1952
   259
{
slouken@1952
   260
    if (_this->gl_data) {
slouken@1952
   261
        return 0;
slouken@1952
   262
    }
slouken@1952
   263
slouken@1952
   264
    _this->gl_data =
slouken@1952
   265
        (struct SDL_GLDriverData *) SDL_calloc(1,
slouken@1952
   266
                                               sizeof(struct
slouken@1952
   267
                                                      SDL_GLDriverData));
slouken@1952
   268
    if (!_this->gl_data) {
slouken@1952
   269
        SDL_OutOfMemory();
slouken@1952
   270
        return -1;
slouken@1952
   271
    }
bob@2322
   272
    _this->gl_data->initialized = 0;
slouken@1952
   273
slouken@1952
   274
    return 0;
slouken@1952
   275
}
slouken@1952
   276
bob@2322
   277
int
bob@2322
   278
X11_GL_Initialize(_THIS)
bob@2322
   279
{
bob@2322
   280
bob@2324
   281
    if (X11_GL_InitializeMemory(_this) < 0) {
bob@2324
   282
        return -1;
bob@2324
   283
    }
bob@2324
   284
    ++_this->gl_data->initialized;
bob@2322
   285
bob@2324
   286
    if (X11_GL_LoadLibrary(_this, NULL) < 0) {
bob@2324
   287
        return -1;
bob@2324
   288
    }
bob@2322
   289
bob@2324
   290
    /* Initialize extensions */
bob@2324
   291
    X11_GL_InitExtensions(_this);
bob@2322
   292
bob@2324
   293
    return 0;
bob@2322
   294
}
bob@2322
   295
slouken@1952
   296
void
slouken@1952
   297
X11_GL_Shutdown(_THIS)
slouken@1952
   298
{
slouken@1952
   299
    if (!_this->gl_data || (--_this->gl_data->initialized > 0)) {
slouken@1952
   300
        return;
slouken@1952
   301
    }
slouken@1952
   302
slouken@1954
   303
    /* Don't actually unload the library, since it may have registered
slouken@1954
   304
     * X11 shutdown hooks, per the notes at:
slouken@1954
   305
     * http://dri.sourceforge.net/doc/DRIuserguide.html
slouken@1954
   306
     * //X11_GL_UnloadLibrary(_this);
slouken@1954
   307
     */
slouken@1952
   308
slouken@1952
   309
    SDL_free(_this->gl_data);
slouken@1952
   310
    _this->gl_data = NULL;
slouken@1952
   311
}
slouken@1952
   312
slouken@1952
   313
XVisualInfo *
slouken@1952
   314
X11_GL_GetVisual(_THIS, Display * display, int screen)
slouken@1952
   315
{
slouken@1952
   316
    XVisualInfo *vinfo;
slouken@1952
   317
slouken@1952
   318
    /* 64 seems nice. */
slouken@1952
   319
    int attribs[64];
slouken@1952
   320
    int i;
slouken@1952
   321
slouken@1952
   322
    /* Setup our GLX attributes according to the gl_config. */
slouken@1952
   323
    i = 0;
slouken@1952
   324
    attribs[i++] = GLX_RGBA;
slouken@1952
   325
    attribs[i++] = GLX_RED_SIZE;
slouken@1952
   326
    attribs[i++] = _this->gl_config.red_size;
slouken@1952
   327
    attribs[i++] = GLX_GREEN_SIZE;
slouken@1952
   328
    attribs[i++] = _this->gl_config.green_size;
slouken@1952
   329
    attribs[i++] = GLX_BLUE_SIZE;
slouken@1952
   330
    attribs[i++] = _this->gl_config.blue_size;
slouken@1952
   331
slouken@1952
   332
    if (_this->gl_config.alpha_size) {
slouken@1952
   333
        attribs[i++] = GLX_ALPHA_SIZE;
slouken@1952
   334
        attribs[i++] = _this->gl_config.alpha_size;
slouken@1952
   335
    }
slouken@1952
   336
slouken@1952
   337
    if (_this->gl_config.buffer_size) {
slouken@1952
   338
        attribs[i++] = GLX_BUFFER_SIZE;
slouken@1952
   339
        attribs[i++] = _this->gl_config.buffer_size;
slouken@1952
   340
    }
slouken@1952
   341
slouken@1952
   342
    if (_this->gl_config.double_buffer) {
slouken@1952
   343
        attribs[i++] = GLX_DOUBLEBUFFER;
slouken@1952
   344
    }
slouken@1952
   345
slouken@1952
   346
    attribs[i++] = GLX_DEPTH_SIZE;
slouken@1952
   347
    attribs[i++] = _this->gl_config.depth_size;
slouken@1952
   348
slouken@1952
   349
    if (_this->gl_config.stencil_size) {
slouken@1952
   350
        attribs[i++] = GLX_STENCIL_SIZE;
slouken@1952
   351
        attribs[i++] = _this->gl_config.stencil_size;
slouken@1952
   352
    }
slouken@1952
   353
slouken@1952
   354
    if (_this->gl_config.accum_red_size) {
slouken@1952
   355
        attribs[i++] = GLX_ACCUM_RED_SIZE;
slouken@1952
   356
        attribs[i++] = _this->gl_config.accum_red_size;
slouken@1952
   357
    }
slouken@1952
   358
slouken@1952
   359
    if (_this->gl_config.accum_green_size) {
slouken@1952
   360
        attribs[i++] = GLX_ACCUM_GREEN_SIZE;
slouken@1952
   361
        attribs[i++] = _this->gl_config.accum_green_size;
slouken@1952
   362
    }
slouken@1952
   363
slouken@1952
   364
    if (_this->gl_config.accum_blue_size) {
slouken@1952
   365
        attribs[i++] = GLX_ACCUM_BLUE_SIZE;
slouken@1952
   366
        attribs[i++] = _this->gl_config.accum_blue_size;
slouken@1952
   367
    }
slouken@1952
   368
slouken@1952
   369
    if (_this->gl_config.accum_alpha_size) {
slouken@1952
   370
        attribs[i++] = GLX_ACCUM_ALPHA_SIZE;
slouken@1952
   371
        attribs[i++] = _this->gl_config.accum_alpha_size;
slouken@1952
   372
    }
slouken@1952
   373
slouken@1952
   374
    if (_this->gl_config.stereo) {
slouken@1952
   375
        attribs[i++] = GLX_STEREO;
slouken@1952
   376
    }
slouken@1952
   377
slouken@1952
   378
    if (_this->gl_config.multisamplebuffers) {
slouken@1952
   379
        attribs[i++] = GLX_SAMPLE_BUFFERS_ARB;
slouken@1952
   380
        attribs[i++] = _this->gl_config.multisamplebuffers;
slouken@1952
   381
    }
slouken@1952
   382
slouken@1952
   383
    if (_this->gl_config.multisamplesamples) {
slouken@1952
   384
        attribs[i++] = GLX_SAMPLES_ARB;
slouken@1952
   385
        attribs[i++] = _this->gl_config.multisamplesamples;
slouken@1952
   386
    }
slouken@1952
   387
slouken@1952
   388
    if (_this->gl_config.accelerated >= 0
slouken@1952
   389
        && _this->gl_data->HAS_GLX_EXT_visual_rating) {
slouken@1952
   390
        attribs[i++] = GLX_VISUAL_CAVEAT_EXT;
slouken@1952
   391
        attribs[i++] = GLX_NONE_EXT;
slouken@1952
   392
    }
slouken@1952
   393
#ifdef GLX_DIRECT_COLOR         /* Try for a DirectColor visual for gamma support */
slouken@2963
   394
    if (X11_UseDirectColorVisuals()) {
slouken@1952
   395
        attribs[i++] = GLX_X_VISUAL_TYPE;
slouken@1952
   396
        attribs[i++] = GLX_DIRECT_COLOR;
slouken@1952
   397
    }
slouken@1952
   398
#endif
slouken@1952
   399
    attribs[i++] = None;
slouken@1952
   400
slouken@1952
   401
    vinfo = _this->gl_data->glXChooseVisual(display, screen, attribs);
slouken@1952
   402
#ifdef GLX_DIRECT_COLOR
slouken@2963
   403
    if (!vinfo && X11_UseDirectColorVisuals()) {        /* No DirectColor visual?  Try again.. */
slouken@1952
   404
        attribs[i - 3] = None;
slouken@1952
   405
        vinfo = _this->gl_data->glXChooseVisual(display, screen, attribs);
slouken@1952
   406
    }
slouken@1952
   407
#endif
slouken@1952
   408
    if (!vinfo) {
slouken@1952
   409
        SDL_SetError("Couldn't find matching GLX visual");
slouken@1952
   410
    }
slouken@1952
   411
    return vinfo;
slouken@1952
   412
}
slouken@1952
   413
slouken@1952
   414
SDL_GLContext
slouken@1952
   415
X11_GL_CreateContext(_THIS, SDL_Window * window)
slouken@1952
   416
{
slouken@1952
   417
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1952
   418
    Display *display = data->videodata->display;
slouken@1952
   419
    int screen =
bob@3011
   420
        ((SDL_DisplayData *) SDL_GetDisplayFromWindow(window)->driverdata)->
bob@3011
   421
        screen;
slouken@1952
   422
    XWindowAttributes xattr;
slouken@1952
   423
    XVisualInfo v, *vinfo;
slouken@1952
   424
    int n;
slouken@1952
   425
    GLXContext context = NULL;
slouken@1952
   426
slouken@1955
   427
    /* We do this to create a clean separation between X and GLX errors. */
slouken@1952
   428
    XSync(display, False);
slouken@1952
   429
    XGetWindowAttributes(display, data->window, &xattr);
slouken@1952
   430
    v.screen = screen;
slouken@1952
   431
    v.visualid = XVisualIDFromVisual(xattr.visual);
slouken@1952
   432
    vinfo = XGetVisualInfo(display, VisualScreenMask | VisualIDMask, &v, &n);
slouken@1952
   433
    if (vinfo) {
slouken@1952
   434
        context =
slouken@1952
   435
            _this->gl_data->glXCreateContext(display, vinfo, NULL, True);
slouken@1952
   436
        XFree(vinfo);
slouken@1952
   437
    }
slouken@1952
   438
    XSync(display, False);
slouken@1952
   439
slouken@1952
   440
    if (!context) {
slouken@1952
   441
        SDL_SetError("Could not create GL context");
slouken@2178
   442
        return NULL;
slouken@1952
   443
    }
slouken@2178
   444
slouken@2178
   445
    if (X11_GL_MakeCurrent(_this, window, context) < 0) {
slouken@2178
   446
        X11_GL_DeleteContext(_this, context);
slouken@2178
   447
        return NULL;
slouken@2178
   448
    }
slouken@2178
   449
slouken@2178
   450
    return context;
slouken@1952
   451
}
slouken@1952
   452
slouken@1952
   453
int
slouken@1952
   454
X11_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
slouken@1952
   455
{
slouken@1952
   456
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@1952
   457
    Window drawable =
slouken@1952
   458
        (window ? ((SDL_WindowData *) window->driverdata)->window : None);
slouken@1952
   459
    GLXContext glx_context = (GLXContext) context;
slouken@1952
   460
    int status;
slouken@1952
   461
slouken@1952
   462
    status = 0;
slouken@1952
   463
    if (!_this->gl_data->glXMakeCurrent(display, drawable, glx_context)) {
slouken@1952
   464
        SDL_SetError("Unable to make GL context current");
slouken@1952
   465
        status = -1;
slouken@1952
   466
    }
slouken@1952
   467
    XSync(display, False);
slouken@1952
   468
slouken@1952
   469
    return (status);
slouken@1952
   470
}
slouken@1952
   471
bob@2327
   472
/* 
bob@2327
   473
   0 is a valid argument to glxSwapIntervalMESA and setting it to 0
bob@2327
   474
   with the MESA version of the extension will undo the effect of a
bob@2327
   475
   previous call with a value that is greater than zero (or at least
bob@2327
   476
   that is what the FM says. OTOH, 0 is an invalid argument to
bob@2327
   477
   glxSwapIntervalSGI and it returns an error if you call it with 0 as
bob@2327
   478
   an argument.
bob@2327
   479
*/
bob@2327
   480
bob@2327
   481
static int swapinterval = -1;
slouken@1952
   482
int
slouken@1952
   483
X11_GL_SetSwapInterval(_THIS, int interval)
slouken@1952
   484
{
slouken@1952
   485
    int status;
slouken@1952
   486
slouken@1952
   487
    if (_this->gl_data->glXSwapIntervalMESA) {
slouken@1952
   488
        status = _this->gl_data->glXSwapIntervalMESA(interval);
slouken@1952
   489
        if (status != 0) {
slouken@1952
   490
            SDL_SetError("glxSwapIntervalMESA failed");
slouken@1952
   491
            status = -1;
bob@2327
   492
        } else {
bob@2327
   493
            swapinterval = interval;
slouken@1952
   494
        }
slouken@1952
   495
    } else if (_this->gl_data->glXSwapIntervalSGI) {
slouken@1952
   496
        status = _this->gl_data->glXSwapIntervalSGI(interval);
slouken@1952
   497
        if (status != 0) {
slouken@1952
   498
            SDL_SetError("glxSwapIntervalSGI failed");
slouken@1952
   499
            status = -1;
bob@2327
   500
        } else {
bob@2327
   501
            swapinterval = interval;
slouken@1952
   502
        }
slouken@1952
   503
    } else {
slouken@1952
   504
        SDL_Unsupported();
slouken@1952
   505
        status = -1;
slouken@1952
   506
    }
slouken@1952
   507
    return status;
slouken@1952
   508
}
slouken@1952
   509
slouken@1952
   510
int
slouken@1952
   511
X11_GL_GetSwapInterval(_THIS)
slouken@1952
   512
{
slouken@1952
   513
    if (_this->gl_data->glXGetSwapIntervalMESA) {
slouken@1952
   514
        return _this->gl_data->glXGetSwapIntervalMESA();
slouken@1952
   515
    } else {
bob@2327
   516
        return swapinterval;
slouken@1952
   517
    }
slouken@1952
   518
}
slouken@1952
   519
slouken@1952
   520
void
slouken@1952
   521
X11_GL_SwapWindow(_THIS, SDL_Window * window)
slouken@1952
   522
{
slouken@1952
   523
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1952
   524
    Display *display = data->videodata->display;
slouken@1952
   525
slouken@1952
   526
    _this->gl_data->glXSwapBuffers(display, data->window);
slouken@1952
   527
}
slouken@1952
   528
slouken@1952
   529
void
slouken@1952
   530
X11_GL_DeleteContext(_THIS, SDL_GLContext context)
slouken@1952
   531
{
slouken@1952
   532
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@1952
   533
    GLXContext glx_context = (GLXContext) context;
slouken@1952
   534
slouken@1952
   535
    _this->gl_data->glXDestroyContext(display, glx_context);
bob@2324
   536
    XSync(display, False);
slouken@1952
   537
}
slouken@1952
   538
slouken@1952
   539
#endif /* SDL_VIDEO_OPENGL_GLX */
slouken@1952
   540
slouken@1952
   541
/* vi: set ts=4 sw=4 expandtab: */