src/video/x11/SDL_x11opengl.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 15 Aug 2007 03:50:49 +0000
changeset 2244 5234868559fa
parent 2243 5523312f311e
child 2320 166400aa33d7
permissions -rw-r--r--
indent
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
slouken@1952
    68
int
slouken@1952
    69
X11_GL_LoadLibrary(_THIS, const char *path)
slouken@1952
    70
{
slouken@1952
    71
    void *handle;
slouken@1952
    72
slouken@1952
    73
    if (_this->gl_config.driver_loaded) {
slouken@1952
    74
        if (path) {
slouken@1952
    75
            SDL_SetError("OpenGL library already loaded");
slouken@1952
    76
            return -1;
slouken@1952
    77
        } else {
slouken@1952
    78
            ++_this->gl_config.driver_loaded;
slouken@1952
    79
            return 0;
slouken@1952
    80
        }
slouken@1952
    81
    }
slouken@1952
    82
    if (path == NULL) {
slouken@1952
    83
        path = SDL_getenv("SDL_OPENGL_LIBRARY");
slouken@1952
    84
    }
slouken@1952
    85
    if (path == NULL) {
slouken@1952
    86
        path = DEFAULT_OPENGL;
slouken@1952
    87
    }
slouken@1952
    88
    handle = GL_LoadObject(path);
slouken@1952
    89
    if (!handle) {
slouken@1952
    90
        return -1;
slouken@1952
    91
    }
slouken@2243
    92
    // LoadLibrary may be called before WindowCreate!
slouken@2243
    93
    X11_GL_Initialize(_this);
slouken@2244
    94
slouken@1952
    95
    /* Load new function pointers */
slouken@1952
    96
    _this->gl_data->glXGetProcAddress =
slouken@1952
    97
        (void *(*)(const GLubyte *)) GL_LoadFunction(handle,
slouken@1952
    98
                                                     "glXGetProcAddressARB");
slouken@1952
    99
    _this->gl_data->glXChooseVisual =
slouken@1952
   100
        (XVisualInfo * (*)(Display *, int, int *)) GL_LoadFunction(handle,
slouken@1952
   101
                                                                   "glXChooseVisual");
slouken@1952
   102
    _this->gl_data->glXCreateContext =
slouken@1952
   103
        (GLXContext(*)(Display *, XVisualInfo *, GLXContext, int))
slouken@1952
   104
        GL_LoadFunction(handle, "glXCreateContext");
slouken@1952
   105
    _this->gl_data->glXDestroyContext =
slouken@1952
   106
        (void (*)(Display *, GLXContext)) GL_LoadFunction(handle,
slouken@1952
   107
                                                          "glXDestroyContext");
slouken@1952
   108
    _this->gl_data->glXMakeCurrent =
slouken@1952
   109
        (int (*)(Display *, GLXDrawable, GLXContext)) GL_LoadFunction(handle,
slouken@1952
   110
                                                                      "glXMakeCurrent");
slouken@1952
   111
    _this->gl_data->glXSwapBuffers =
slouken@1952
   112
        (void (*)(Display *, GLXDrawable)) GL_LoadFunction(handle,
slouken@1952
   113
                                                           "glXSwapBuffers");
slouken@1952
   114
slouken@1952
   115
    if (!_this->gl_data->glXChooseVisual ||
slouken@1952
   116
        !_this->gl_data->glXCreateContext ||
slouken@1952
   117
        !_this->gl_data->glXDestroyContext ||
slouken@1956
   118
        !_this->gl_data->glXMakeCurrent || !_this->gl_data->glXSwapBuffers) {
slouken@1952
   119
        SDL_SetError("Could not retrieve OpenGL functions");
slouken@1952
   120
        return -1;
slouken@1952
   121
    }
slouken@1952
   122
slouken@1952
   123
    _this->gl_config.dll_handle = handle;
slouken@1952
   124
    SDL_strlcpy(_this->gl_config.driver_path, path,
slouken@1952
   125
                SDL_arraysize(_this->gl_config.driver_path));
slouken@1952
   126
    _this->gl_config.driver_loaded = 1;
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);
slouken@1952
   254
    X11_PumpEvents(_this);
slouken@1952
   255
}
slouken@1952
   256
slouken@1952
   257
int
slouken@1952
   258
X11_GL_Initialize(_THIS)
slouken@1952
   259
{
slouken@1952
   260
    if (_this->gl_data) {
slouken@1952
   261
        ++_this->gl_data->initialized;
slouken@1952
   262
        return 0;
slouken@1952
   263
    }
slouken@1952
   264
slouken@1952
   265
    _this->gl_data =
slouken@1952
   266
        (struct SDL_GLDriverData *) SDL_calloc(1,
slouken@1952
   267
                                               sizeof(struct
slouken@1952
   268
                                                      SDL_GLDriverData));
slouken@1952
   269
    if (!_this->gl_data) {
slouken@1952
   270
        SDL_OutOfMemory();
slouken@1952
   271
        return -1;
slouken@1952
   272
    }
slouken@1952
   273
    _this->gl_data->initialized = 1;
slouken@1952
   274
slouken@1952
   275
    if (X11_GL_LoadLibrary(_this, NULL) < 0) {
slouken@1952
   276
        return -1;
slouken@1952
   277
    }
slouken@1952
   278
slouken@1952
   279
    /* Initialize extensions */
slouken@1952
   280
    X11_GL_InitExtensions(_this);
slouken@1952
   281
slouken@1952
   282
    return 0;
slouken@1952
   283
}
slouken@1952
   284
slouken@1952
   285
void
slouken@1952
   286
X11_GL_Shutdown(_THIS)
slouken@1952
   287
{
slouken@1952
   288
    if (!_this->gl_data || (--_this->gl_data->initialized > 0)) {
slouken@1952
   289
        return;
slouken@1952
   290
    }
slouken@1952
   291
slouken@1954
   292
    /* Don't actually unload the library, since it may have registered
slouken@1954
   293
     * X11 shutdown hooks, per the notes at:
slouken@1954
   294
     * http://dri.sourceforge.net/doc/DRIuserguide.html
slouken@1954
   295
     * //X11_GL_UnloadLibrary(_this);
slouken@1954
   296
     */
slouken@1952
   297
slouken@1952
   298
    SDL_free(_this->gl_data);
slouken@1952
   299
    _this->gl_data = NULL;
slouken@1952
   300
}
slouken@1952
   301
slouken@1952
   302
XVisualInfo *
slouken@1952
   303
X11_GL_GetVisual(_THIS, Display * display, int screen)
slouken@1952
   304
{
slouken@1952
   305
    XVisualInfo *vinfo;
slouken@1952
   306
slouken@1952
   307
    /* 64 seems nice. */
slouken@1952
   308
    int attribs[64];
slouken@1952
   309
    int i;
slouken@1952
   310
slouken@1952
   311
    /* Setup our GLX attributes according to the gl_config. */
slouken@1952
   312
    i = 0;
slouken@1952
   313
    attribs[i++] = GLX_RGBA;
slouken@1952
   314
    attribs[i++] = GLX_RED_SIZE;
slouken@1952
   315
    attribs[i++] = _this->gl_config.red_size;
slouken@1952
   316
    attribs[i++] = GLX_GREEN_SIZE;
slouken@1952
   317
    attribs[i++] = _this->gl_config.green_size;
slouken@1952
   318
    attribs[i++] = GLX_BLUE_SIZE;
slouken@1952
   319
    attribs[i++] = _this->gl_config.blue_size;
slouken@1952
   320
slouken@1952
   321
    if (_this->gl_config.alpha_size) {
slouken@1952
   322
        attribs[i++] = GLX_ALPHA_SIZE;
slouken@1952
   323
        attribs[i++] = _this->gl_config.alpha_size;
slouken@1952
   324
    }
slouken@1952
   325
slouken@1952
   326
    if (_this->gl_config.buffer_size) {
slouken@1952
   327
        attribs[i++] = GLX_BUFFER_SIZE;
slouken@1952
   328
        attribs[i++] = _this->gl_config.buffer_size;
slouken@1952
   329
    }
slouken@1952
   330
slouken@1952
   331
    if (_this->gl_config.double_buffer) {
slouken@1952
   332
        attribs[i++] = GLX_DOUBLEBUFFER;
slouken@1952
   333
    }
slouken@1952
   334
slouken@1952
   335
    attribs[i++] = GLX_DEPTH_SIZE;
slouken@1952
   336
    attribs[i++] = _this->gl_config.depth_size;
slouken@1952
   337
slouken@1952
   338
    if (_this->gl_config.stencil_size) {
slouken@1952
   339
        attribs[i++] = GLX_STENCIL_SIZE;
slouken@1952
   340
        attribs[i++] = _this->gl_config.stencil_size;
slouken@1952
   341
    }
slouken@1952
   342
slouken@1952
   343
    if (_this->gl_config.accum_red_size) {
slouken@1952
   344
        attribs[i++] = GLX_ACCUM_RED_SIZE;
slouken@1952
   345
        attribs[i++] = _this->gl_config.accum_red_size;
slouken@1952
   346
    }
slouken@1952
   347
slouken@1952
   348
    if (_this->gl_config.accum_green_size) {
slouken@1952
   349
        attribs[i++] = GLX_ACCUM_GREEN_SIZE;
slouken@1952
   350
        attribs[i++] = _this->gl_config.accum_green_size;
slouken@1952
   351
    }
slouken@1952
   352
slouken@1952
   353
    if (_this->gl_config.accum_blue_size) {
slouken@1952
   354
        attribs[i++] = GLX_ACCUM_BLUE_SIZE;
slouken@1952
   355
        attribs[i++] = _this->gl_config.accum_blue_size;
slouken@1952
   356
    }
slouken@1952
   357
slouken@1952
   358
    if (_this->gl_config.accum_alpha_size) {
slouken@1952
   359
        attribs[i++] = GLX_ACCUM_ALPHA_SIZE;
slouken@1952
   360
        attribs[i++] = _this->gl_config.accum_alpha_size;
slouken@1952
   361
    }
slouken@1952
   362
slouken@1952
   363
    if (_this->gl_config.stereo) {
slouken@1952
   364
        attribs[i++] = GLX_STEREO;
slouken@1952
   365
    }
slouken@1952
   366
slouken@1952
   367
    if (_this->gl_config.multisamplebuffers) {
slouken@1952
   368
        attribs[i++] = GLX_SAMPLE_BUFFERS_ARB;
slouken@1952
   369
        attribs[i++] = _this->gl_config.multisamplebuffers;
slouken@1952
   370
    }
slouken@1952
   371
slouken@1952
   372
    if (_this->gl_config.multisamplesamples) {
slouken@1952
   373
        attribs[i++] = GLX_SAMPLES_ARB;
slouken@1952
   374
        attribs[i++] = _this->gl_config.multisamplesamples;
slouken@1952
   375
    }
slouken@1952
   376
slouken@1952
   377
    if (_this->gl_config.accelerated >= 0
slouken@1952
   378
        && _this->gl_data->HAS_GLX_EXT_visual_rating) {
slouken@1952
   379
        attribs[i++] = GLX_VISUAL_CAVEAT_EXT;
slouken@1952
   380
        attribs[i++] = GLX_NONE_EXT;
slouken@1952
   381
    }
slouken@1952
   382
#ifdef GLX_DIRECT_COLOR         /* Try for a DirectColor visual for gamma support */
slouken@1952
   383
    if (!SDL_getenv("SDL_VIDEO_X11_NODIRECTCOLOR")) {
slouken@1952
   384
        attribs[i++] = GLX_X_VISUAL_TYPE;
slouken@1952
   385
        attribs[i++] = GLX_DIRECT_COLOR;
slouken@1952
   386
    }
slouken@1952
   387
#endif
slouken@1952
   388
    attribs[i++] = None;
slouken@1952
   389
slouken@1952
   390
    vinfo = _this->gl_data->glXChooseVisual(display, screen, attribs);
slouken@1952
   391
#ifdef GLX_DIRECT_COLOR
slouken@1952
   392
    if (!vinfo && !SDL_getenv("SDL_VIDEO_X11_NODIRECTCOLOR")) { /* No DirectColor visual?  Try again.. */
slouken@1952
   393
        attribs[i - 3] = None;
slouken@1952
   394
        vinfo = _this->gl_data->glXChooseVisual(display, screen, attribs);
slouken@1952
   395
    }
slouken@1952
   396
#endif
slouken@1952
   397
    if (!vinfo) {
slouken@1952
   398
        SDL_SetError("Couldn't find matching GLX visual");
slouken@1952
   399
    }
slouken@1952
   400
    return vinfo;
slouken@1952
   401
}
slouken@1952
   402
slouken@1952
   403
SDL_GLContext
slouken@1952
   404
X11_GL_CreateContext(_THIS, SDL_Window * window)
slouken@1952
   405
{
slouken@1952
   406
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1952
   407
    Display *display = data->videodata->display;
slouken@1952
   408
    int screen =
slouken@1952
   409
        ((SDL_DisplayData *) SDL_GetDisplayFromWindow(window)->driverdata)->
slouken@1952
   410
        screen;
slouken@1952
   411
    XWindowAttributes xattr;
slouken@1952
   412
    XVisualInfo v, *vinfo;
slouken@1952
   413
    int n;
slouken@1952
   414
    GLXContext context = NULL;
slouken@1952
   415
slouken@1955
   416
    /* We do this to create a clean separation between X and GLX errors. */
slouken@1952
   417
    XSync(display, False);
slouken@1952
   418
    XGetWindowAttributes(display, data->window, &xattr);
slouken@1952
   419
    v.screen = screen;
slouken@1952
   420
    v.visualid = XVisualIDFromVisual(xattr.visual);
slouken@1952
   421
    vinfo = XGetVisualInfo(display, VisualScreenMask | VisualIDMask, &v, &n);
slouken@1952
   422
    if (vinfo) {
slouken@1952
   423
        context =
slouken@1952
   424
            _this->gl_data->glXCreateContext(display, vinfo, NULL, True);
slouken@1952
   425
        XFree(vinfo);
slouken@1952
   426
    }
slouken@1952
   427
    XSync(display, False);
slouken@1952
   428
slouken@1952
   429
    if (!context) {
slouken@1952
   430
        SDL_SetError("Could not create GL context");
slouken@2178
   431
        return NULL;
slouken@1952
   432
    }
slouken@2178
   433
slouken@2178
   434
    if (X11_GL_MakeCurrent(_this, window, context) < 0) {
slouken@2178
   435
        X11_GL_DeleteContext(_this, context);
slouken@2178
   436
        return NULL;
slouken@2178
   437
    }
slouken@2178
   438
slouken@2178
   439
    return context;
slouken@1952
   440
}
slouken@1952
   441
slouken@1952
   442
int
slouken@1952
   443
X11_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
slouken@1952
   444
{
slouken@1952
   445
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@1952
   446
    Window drawable =
slouken@1952
   447
        (window ? ((SDL_WindowData *) window->driverdata)->window : None);
slouken@1952
   448
    GLXContext glx_context = (GLXContext) context;
slouken@1952
   449
    int status;
slouken@1952
   450
slouken@1952
   451
    status = 0;
slouken@1952
   452
    if (!_this->gl_data->glXMakeCurrent(display, drawable, glx_context)) {
slouken@1952
   453
        SDL_SetError("Unable to make GL context current");
slouken@1952
   454
        status = -1;
slouken@1952
   455
    }
slouken@1952
   456
    XSync(display, False);
slouken@1952
   457
slouken@1952
   458
    return (status);
slouken@1952
   459
}
slouken@1952
   460
slouken@1952
   461
int
slouken@1952
   462
X11_GL_SetSwapInterval(_THIS, int interval)
slouken@1952
   463
{
slouken@1952
   464
    int status;
slouken@1952
   465
slouken@1952
   466
    if (_this->gl_data->glXSwapIntervalMESA) {
slouken@1952
   467
        status = _this->gl_data->glXSwapIntervalMESA(interval);
slouken@1952
   468
        if (status != 0) {
slouken@1952
   469
            SDL_SetError("glxSwapIntervalMESA failed");
slouken@1952
   470
            status = -1;
slouken@1952
   471
        }
slouken@1952
   472
    } else if (_this->gl_data->glXSwapIntervalSGI) {
slouken@1952
   473
        status = _this->gl_data->glXSwapIntervalSGI(interval);
slouken@1952
   474
        if (status != 0) {
slouken@1952
   475
            SDL_SetError("glxSwapIntervalSGI failed");
slouken@1952
   476
            status = -1;
slouken@1952
   477
        }
slouken@1952
   478
    } else {
slouken@1952
   479
        SDL_Unsupported();
slouken@1952
   480
        status = -1;
slouken@1952
   481
    }
slouken@1952
   482
    return status;
slouken@1952
   483
}
slouken@1952
   484
slouken@1952
   485
int
slouken@1952
   486
X11_GL_GetSwapInterval(_THIS)
slouken@1952
   487
{
slouken@1952
   488
    if (_this->gl_data->glXGetSwapIntervalMESA) {
slouken@1952
   489
        return _this->gl_data->glXGetSwapIntervalMESA();
slouken@1952
   490
    } else {
slouken@1952
   491
        SDL_Unsupported();
slouken@1952
   492
        return -1;
slouken@1952
   493
    }
slouken@1952
   494
}
slouken@1952
   495
slouken@1952
   496
void
slouken@1952
   497
X11_GL_SwapWindow(_THIS, SDL_Window * window)
slouken@1952
   498
{
slouken@1952
   499
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1952
   500
    Display *display = data->videodata->display;
slouken@1952
   501
slouken@1952
   502
    _this->gl_data->glXSwapBuffers(display, data->window);
slouken@1952
   503
}
slouken@1952
   504
slouken@1952
   505
void
slouken@1952
   506
X11_GL_DeleteContext(_THIS, SDL_GLContext context)
slouken@1952
   507
{
slouken@1952
   508
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@1952
   509
    GLXContext glx_context = (GLXContext) context;
slouken@1952
   510
slouken@1952
   511
    _this->gl_data->glXDestroyContext(display, glx_context);
slouken@1952
   512
}
slouken@1952
   513
slouken@1952
   514
#endif /* SDL_VIDEO_OPENGL_GLX */
slouken@1952
   515
slouken@1952
   516
/* vi: set ts=4 sw=4 expandtab: */