src/video/x11/SDL_x11opengl.c
author Bob Pendleton <bob@pendleton.com>
Fri, 07 Mar 2008 23:57:15 +0000
changeset 2327 7b53a8401195
parent 2324 3202e4826c57
child 2698 e1da92da346c
child 2723 911db724ea24
permissions -rw-r--r--
In testdyngl.c the event type was being anded (&) with SDL_KEYDOWN and if the result was none zero the program was quiting. This is very weird because it was
working earlier this week.

I added some more trace code to SDL_x11events.c

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