glx_context.c 21.2 KB
Newer Older
Camilla Berglund's avatar
Camilla Berglund committed
1
//========================================================================
Camilla Berglund's avatar
Camilla Berglund committed
2
// GLFW 3.3 GLX - www.glfw.org
Camilla Berglund's avatar
Camilla Berglund committed
3
4
//------------------------------------------------------------------------
// Copyright (c) 2002-2006 Marcus Geelnard
Camilla Berglund's avatar
Camilla Berglund committed
5
// Copyright (c) 2006-2016 Camilla Berglund <elmindreda@glfw.org>
Camilla Berglund's avatar
Camilla Berglund committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
//    claim that you wrote the original software. If you use this software
//    in a product, an acknowledgment in the product documentation would
//    be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
//    be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
//    distribution.
//
//========================================================================

#include "internal.h"

Camilla Berglund's avatar
Camilla Berglund committed
30
#include <string.h>
31
#include <stdlib.h>
32
#include <assert.h>
Camilla Berglund's avatar
Camilla Berglund committed
33

34
35
36
37
38
#ifndef GLXBadProfileARB
 #define GLXBadProfileARB 13
#endif


39
40
// Returns the specified attribute of the specified GLXFBConfig
//
41
static int getGLXFBConfigAttrib(GLXFBConfig fbconfig, int attrib)
42
43
{
    int value;
44
    glXGetFBConfigAttrib(_glfw.x11.display, fbconfig, attrib, &value);
45
46
47
    return value;
}

48
// Return the GLXFBConfig most closely matching the specified hints
49
//
50
static GLFWbool chooseGLXFBConfig(const _GLFWfbconfig* desired, GLXFBConfig* result)
51
52
53
54
55
56
{
    GLXFBConfig* nativeConfigs;
    _GLFWfbconfig* usableConfigs;
    const _GLFWfbconfig* closest;
    int i, nativeCount, usableCount;
    const char* vendor;
57
    GLFWbool trustWindowBit = GLFW_TRUE;
58

Camilla Berglund's avatar
Camilla Berglund committed
59
60
    // HACK: This is a (hopefully temporary) workaround for Chromium
    //       (VirtualBox GL) not setting the window bit on any GLXFBConfigs
61
    vendor = glXGetClientString(_glfw.x11.display, GLX_VENDOR);
62
    if (strcmp(vendor, "Chromium") == 0)
63
        trustWindowBit = GLFW_FALSE;
64

65
66
    nativeConfigs =
        glXGetFBConfigs(_glfw.x11.display, _glfw.x11.screen, &nativeCount);
67
68
    if (!nativeCount)
    {
69
        _glfwInputError(GLFW_API_UNAVAILABLE, "GLX: No GLXFBConfigs returned");
70
        return GLFW_FALSE;
71
72
    }

73
    usableConfigs = calloc(nativeCount, sizeof(_GLFWfbconfig));
74
75
76
77
78
79
80
    usableCount = 0;

    for (i = 0;  i < nativeCount;  i++)
    {
        const GLXFBConfig n = nativeConfigs[i];
        _GLFWfbconfig* u = usableConfigs + usableCount;

Camilla Berglund's avatar
Camilla Berglund committed
81
        // Only consider RGBA GLXFBConfigs
82
        if (!(getGLXFBConfigAttrib(n, GLX_RENDER_TYPE) & GLX_RGBA_BIT))
83
84
            continue;

Camilla Berglund's avatar
Camilla Berglund committed
85
        // Only consider window GLXFBConfigs
86
        if (!(getGLXFBConfigAttrib(n, GLX_DRAWABLE_TYPE) & GLX_WINDOW_BIT))
87
88
89
90
91
        {
            if (trustWindowBit)
                continue;
        }

92
93
94
        u->redBits = getGLXFBConfigAttrib(n, GLX_RED_SIZE);
        u->greenBits = getGLXFBConfigAttrib(n, GLX_GREEN_SIZE);
        u->blueBits = getGLXFBConfigAttrib(n, GLX_BLUE_SIZE);
95

96
97
98
        u->alphaBits = getGLXFBConfigAttrib(n, GLX_ALPHA_SIZE);
        u->depthBits = getGLXFBConfigAttrib(n, GLX_DEPTH_SIZE);
        u->stencilBits = getGLXFBConfigAttrib(n, GLX_STENCIL_SIZE);
99

100
101
102
103
        u->accumRedBits = getGLXFBConfigAttrib(n, GLX_ACCUM_RED_SIZE);
        u->accumGreenBits = getGLXFBConfigAttrib(n, GLX_ACCUM_GREEN_SIZE);
        u->accumBlueBits = getGLXFBConfigAttrib(n, GLX_ACCUM_BLUE_SIZE);
        u->accumAlphaBits = getGLXFBConfigAttrib(n, GLX_ACCUM_ALPHA_SIZE);
104

105
        u->auxBuffers = getGLXFBConfigAttrib(n, GLX_AUX_BUFFERS);
Camilla Berglund's avatar
Camilla Berglund committed
106

107
        if (getGLXFBConfigAttrib(n, GLX_STEREO))
108
            u->stereo = GLFW_TRUE;
109
        if (getGLXFBConfigAttrib(n, GLX_DOUBLEBUFFER))
110
            u->doublebuffer = GLFW_TRUE;
111
112

        if (_glfw.glx.ARB_multisample)
113
            u->samples = getGLXFBConfigAttrib(n, GLX_SAMPLES);
114

115
        if (_glfw.glx.ARB_framebuffer_sRGB || _glfw.glx.EXT_framebuffer_sRGB)
116
            u->sRGB = getGLXFBConfigAttrib(n, GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB);
117

Camilla Berglund's avatar
Cleanup    
Camilla Berglund committed
118
        u->handle = (uintptr_t) n;
119
120
121
122
123
        usableCount++;
    }

    closest = _glfwChooseFBConfig(desired, usableConfigs, usableCount);
    if (closest)
Camilla Berglund's avatar
Cleanup    
Camilla Berglund committed
124
        *result = (GLXFBConfig) closest->handle;
125
126
127
128

    XFree(nativeConfigs);
    free(usableConfigs);

Camilla Berglund's avatar
Cleanup    
Camilla Berglund committed
129
    return closest != NULL;
130
131
}

132
// Create the OpenGL context using legacy API
133
//
134
135
136
static GLXContext createLegacyContextGLX(_GLFWwindow* window,
                                         GLXFBConfig fbconfig,
                                         GLXContext share)
137
{
138
139
140
141
142
    return glXCreateNewContext(_glfw.x11.display,
                               fbconfig,
                               GLX_RGBA_TYPE,
                               share,
                               True);
143
144
}

145
static void makeContextCurrentGLX(_GLFWwindow* window)
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
{
    if (window)
    {
        if (!glXMakeCurrent(_glfw.x11.display,
                            window->context.glx.window,
                            window->context.glx.handle))
        {
            _glfwInputError(GLFW_PLATFORM_ERROR,
                            "GLX: Failed to make context current");
            return;
        }
    }
    else
    {
        if (!glXMakeCurrent(_glfw.x11.display, None, NULL))
        {
            _glfwInputError(GLFW_PLATFORM_ERROR,
                            "GLX: Failed to clear current context");
            return;
        }
    }

    _glfwPlatformSetCurrentContext(window);
}

171
static void swapBuffersGLX(_GLFWwindow* window)
172
173
174
175
{
    glXSwapBuffers(_glfw.x11.display, window->context.glx.window);
}

176
static void swapIntervalGLX(int interval)
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
{
    _GLFWwindow* window = _glfwPlatformGetCurrentContext();

    if (_glfw.glx.EXT_swap_control)
    {
        _glfw.glx.SwapIntervalEXT(_glfw.x11.display,
                                  window->context.glx.window,
                                  interval);
    }
    else if (_glfw.glx.MESA_swap_control)
        _glfw.glx.SwapIntervalMESA(interval);
    else if (_glfw.glx.SGI_swap_control)
    {
        if (interval > 0)
            _glfw.glx.SwapIntervalSGI(interval);
    }
}

195
static int extensionSupportedGLX(const char* extension)
196
197
198
199
200
201
202
203
204
205
206
207
{
    const char* extensions =
        glXQueryExtensionsString(_glfw.x11.display, _glfw.x11.screen);
    if (extensions)
    {
        if (_glfwStringInExtensionString(extension, extensions))
            return GLFW_TRUE;
    }

    return GLFW_FALSE;
}

208
static GLFWglproc getProcAddressGLX(const char* procname)
209
210
211
212
213
214
215
216
217
218
219
{
    if (_glfw.glx.GetProcAddress)
        return _glfw.glx.GetProcAddress((const GLubyte*) procname);
    else if (_glfw.glx.GetProcAddressARB)
        return _glfw.glx.GetProcAddressARB((const GLubyte*) procname);
    else
        return dlsym(_glfw.glx.handle, procname);
}

// Destroy the OpenGL context
//
220
static void destroyContextGLX(_GLFWwindow* window)
221
222
223
224
225
226
227
228
229
230
231
232
233
234
{
    if (window->context.glx.window)
    {
        glXDestroyWindow(_glfw.x11.display, window->context.glx.window);
        window->context.glx.window = None;
    }

    if (window->context.glx.handle)
    {
        glXDestroyContext(_glfw.x11.display, window->context.glx.handle);
        window->context.glx.handle = NULL;
    }
}

235

236
237
238
239
240
//////////////////////////////////////////////////////////////////////////
//////                       GLFW internal API                      //////
//////////////////////////////////////////////////////////////////////////

// Initialize GLX
241
//
Camilla Berglund's avatar
Camilla Berglund committed
242
GLFWbool _glfwInitGLX(void)
243
{
244
245
246
    int i;
    const char* sonames[] =
    {
247
#if defined(__CYGWIN__)
248
        "libGL-1.so",
249
#else
250
251
        "libGL.so.1",
        "libGL.so",
252
#endif
253
254
255
        NULL
    };

256
257
258
    if (_glfw.glx.handle)
        return GLFW_TRUE;

259
260
261
262
263
264
265
    for (i = 0;  sonames[i];  i++)
    {
        _glfw.glx.handle = dlopen(sonames[i], RTLD_LAZY | RTLD_GLOBAL);
        if (_glfw.glx.handle)
            break;
    }

266
    if (!_glfw.glx.handle)
267
    {
268
        _glfwInputError(GLFW_API_UNAVAILABLE, "GLX: Failed to load GLX");
269
        return GLFW_FALSE;
270
    }
271

272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
    _glfw.glx.GetFBConfigs =
        dlsym(_glfw.glx.handle, "glXGetFBConfigs");
    _glfw.glx.GetFBConfigAttrib =
        dlsym(_glfw.glx.handle, "glXGetFBConfigAttrib");
    _glfw.glx.GetClientString =
        dlsym(_glfw.glx.handle, "glXGetClientString");
    _glfw.glx.QueryExtension =
        dlsym(_glfw.glx.handle, "glXQueryExtension");
    _glfw.glx.QueryVersion =
        dlsym(_glfw.glx.handle, "glXQueryVersion");
    _glfw.glx.DestroyContext =
        dlsym(_glfw.glx.handle, "glXDestroyContext");
    _glfw.glx.MakeCurrent =
        dlsym(_glfw.glx.handle, "glXMakeCurrent");
    _glfw.glx.SwapBuffers =
        dlsym(_glfw.glx.handle, "glXSwapBuffers");
    _glfw.glx.QueryExtensionsString =
        dlsym(_glfw.glx.handle, "glXQueryExtensionsString");
    _glfw.glx.CreateNewContext =
        dlsym(_glfw.glx.handle, "glXCreateNewContext");
292
293
294
295
    _glfw.glx.CreateWindow =
        dlsym(_glfw.glx.handle, "glXCreateWindow");
    _glfw.glx.DestroyWindow =
        dlsym(_glfw.glx.handle, "glXDestroyWindow");
296
297
298
299
    _glfw.glx.GetProcAddress =
        dlsym(_glfw.glx.handle, "glXGetProcAddress");
    _glfw.glx.GetProcAddressARB =
        dlsym(_glfw.glx.handle, "glXGetProcAddressARB");
300
301
    _glfw.glx.GetVisualFromFBConfig =
        dlsym(_glfw.glx.handle, "glXGetVisualFromFBConfig");
302

303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
    if (!_glfw.glx.GetFBConfigs ||
        !_glfw.glx.GetFBConfigAttrib ||
        !_glfw.glx.GetClientString ||
        !_glfw.glx.QueryExtension ||
        !_glfw.glx.QueryVersion ||
        !_glfw.glx.DestroyContext ||
        !_glfw.glx.MakeCurrent ||
        !_glfw.glx.SwapBuffers ||
        !_glfw.glx.QueryExtensionsString ||
        !_glfw.glx.CreateNewContext ||
        !_glfw.glx.CreateWindow ||
        !_glfw.glx.DestroyWindow ||
        !_glfw.glx.GetProcAddress ||
        !_glfw.glx.GetProcAddressARB ||
        !_glfw.glx.GetVisualFromFBConfig)
    {
        _glfwInputError(GLFW_PLATFORM_ERROR,
                        "GLX: Failed to load required entry points");
        return GLFW_FALSE;
    }

324
325
326
    if (!glXQueryExtension(_glfw.x11.display,
                           &_glfw.glx.errorBase,
                           &_glfw.glx.eventBase))
327
    {
Camilla Berglund's avatar
Camilla Berglund committed
328
        _glfwInputError(GLFW_API_UNAVAILABLE, "GLX: GLX extension not found");
329
        return GLFW_FALSE;
330
331
    }

332
    if (!glXQueryVersion(_glfw.x11.display, &_glfw.glx.major, &_glfw.glx.minor))
333
    {
334
335
        _glfwInputError(GLFW_API_UNAVAILABLE,
                        "GLX: Failed to query GLX version");
336
        return GLFW_FALSE;
337
    }
338

339
    if (_glfw.glx.major == 1 && _glfw.glx.minor < 3)
Camilla Berglund's avatar
Camilla Berglund committed
340
341
342
    {
        _glfwInputError(GLFW_API_UNAVAILABLE,
                        "GLX: GLX version 1.3 is required");
343
        return GLFW_FALSE;
Camilla Berglund's avatar
Camilla Berglund committed
344
345
    }

346
    if (extensionSupportedGLX("GLX_EXT_swap_control"))
347
    {
348
        _glfw.glx.SwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)
349
            getProcAddressGLX("glXSwapIntervalEXT");
350
351

        if (_glfw.glx.SwapIntervalEXT)
352
            _glfw.glx.EXT_swap_control = GLFW_TRUE;
353
354
    }

355
    if (extensionSupportedGLX("GLX_SGI_swap_control"))
356
    {
357
        _glfw.glx.SwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)
358
            getProcAddressGLX("glXSwapIntervalSGI");
359
360

        if (_glfw.glx.SwapIntervalSGI)
361
            _glfw.glx.SGI_swap_control = GLFW_TRUE;
362
363
    }

364
    if (extensionSupportedGLX("GLX_MESA_swap_control"))
365
    {
366
        _glfw.glx.SwapIntervalMESA = (PFNGLXSWAPINTERVALMESAPROC)
367
            getProcAddressGLX("glXSwapIntervalMESA");
368

369
        if (_glfw.glx.SwapIntervalMESA)
370
            _glfw.glx.MESA_swap_control = GLFW_TRUE;
371
    }
372

373
    if (extensionSupportedGLX("GLX_ARB_multisample"))
374
        _glfw.glx.ARB_multisample = GLFW_TRUE;
375

376
    if (extensionSupportedGLX("GLX_ARB_framebuffer_sRGB"))
377
        _glfw.glx.ARB_framebuffer_sRGB = GLFW_TRUE;
378

379
    if (extensionSupportedGLX("GLX_EXT_framebuffer_sRGB"))
380
        _glfw.glx.EXT_framebuffer_sRGB = GLFW_TRUE;
381

382
    if (extensionSupportedGLX("GLX_ARB_create_context"))
383
384
    {
        _glfw.glx.CreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)
385
            getProcAddressGLX("glXCreateContextAttribsARB");
386

387
        if (_glfw.glx.CreateContextAttribsARB)
388
            _glfw.glx.ARB_create_context = GLFW_TRUE;
389
390
    }

391
    if (extensionSupportedGLX("GLX_ARB_create_context_robustness"))
392
        _glfw.glx.ARB_create_context_robustness = GLFW_TRUE;
393

394
    if (extensionSupportedGLX("GLX_ARB_create_context_profile"))
395
        _glfw.glx.ARB_create_context_profile = GLFW_TRUE;
396

397
    if (extensionSupportedGLX("GLX_EXT_create_context_es2_profile"))
398
        _glfw.glx.EXT_create_context_es2_profile = GLFW_TRUE;
399

400
    if (extensionSupportedGLX("GLX_ARB_context_flush_control"))
401
        _glfw.glx.ARB_context_flush_control = GLFW_TRUE;
402

403
    return GLFW_TRUE;
404
405
}

406
// Terminate GLX
407
//
408
void _glfwTerminateGLX(void)
409
{
410
411
    // NOTE: This function must not call any X11 functions, as it is called
    //       after XCloseDisplay (see _glfwPlatformTerminate for details)
412

413
    if (_glfw.glx.handle)
414
    {
415
416
        dlclose(_glfw.glx.handle);
        _glfw.glx.handle = NULL;
417
418
419
    }
}

Camilla Berglund's avatar
Camilla Berglund committed
420
#define setGLXattrib(attribName, attribValue) \
421
{ \
422
    attribs[index++] = attribName; \
423
    attribs[index++] = attribValue; \
siavash's avatar
siavash committed
424
    assert((size_t) index < sizeof(attribs) / sizeof(attribs[0])); \
425
}
426

427
// Create the OpenGL or OpenGL ES context
428
//
Camilla Berglund's avatar
Camilla Berglund committed
429
430
431
GLFWbool _glfwCreateContextGLX(_GLFWwindow* window,
                               const _GLFWctxconfig* ctxconfig,
                               const _GLFWfbconfig* fbconfig)
432
433
{
    int attribs[40];
434
    GLXFBConfig native = NULL;
435
436
    GLXContext share = NULL;

437
    if (ctxconfig->share)
438
        share = ctxconfig->share->context.glx.handle;
439

440
    if (!chooseGLXFBConfig(fbconfig, &native))
441
    {
442
        _glfwInputError(GLFW_FORMAT_UNAVAILABLE,
443
                        "GLX: Failed to find a suitable GLXFBConfig");
444
        return GLFW_FALSE;
445
446
    }

447
    if (ctxconfig->client == GLFW_OPENGL_ES_API)
448
    {
449
450
451
        if (!_glfw.glx.ARB_create_context ||
            !_glfw.glx.ARB_create_context_profile ||
            !_glfw.glx.EXT_create_context_es2_profile)
452
        {
453
            _glfwInputError(GLFW_API_UNAVAILABLE,
Camilla Berglund's avatar
Camilla Berglund committed
454
                            "GLX: OpenGL ES requested but GLX_EXT_create_context_es2_profile is unavailable");
455
            return GLFW_FALSE;
456
        }
Camilla Berglund's avatar
Camilla Berglund committed
457
    }
458

459
    if (ctxconfig->forward)
Camilla Berglund's avatar
Camilla Berglund committed
460
    {
461
        if (!_glfw.glx.ARB_create_context)
462
        {
463
            _glfwInputError(GLFW_VERSION_UNAVAILABLE,
Camilla Berglund's avatar
Camilla Berglund committed
464
                            "GLX: Forward compatibility requested but GLX_ARB_create_context_profile is unavailable");
465
            return GLFW_FALSE;
466
        }
Camilla Berglund's avatar
Camilla Berglund committed
467
    }
468

469
    if (ctxconfig->profile)
Camilla Berglund's avatar
Camilla Berglund committed
470
    {
471
472
        if (!_glfw.glx.ARB_create_context ||
            !_glfw.glx.ARB_create_context_profile)
473
        {
474
            _glfwInputError(GLFW_VERSION_UNAVAILABLE,
Camilla Berglund's avatar
Camilla Berglund committed
475
                            "GLX: An OpenGL profile requested but GLX_ARB_create_context_profile is unavailable");
476
            return GLFW_FALSE;
Camilla Berglund's avatar
Camilla Berglund committed
477
478
479
        }
    }

480
    _glfwGrabErrorHandlerX11();
481

482
    if (_glfw.glx.ARB_create_context)
Camilla Berglund's avatar
Camilla Berglund committed
483
    {
484
        int index = 0, mask = 0, flags = 0;
485

486
        if (ctxconfig->client == GLFW_OPENGL_API)
Camilla Berglund's avatar
Camilla Berglund committed
487
        {
488
            if (ctxconfig->forward)
489
490
                flags |= GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;

491
492
493
494
            if (ctxconfig->profile == GLFW_OPENGL_CORE_PROFILE)
                mask |= GLX_CONTEXT_CORE_PROFILE_BIT_ARB;
            else if (ctxconfig->profile == GLFW_OPENGL_COMPAT_PROFILE)
                mask |= GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB;
495
        }
Camilla Berglund's avatar
Camilla Berglund committed
496
497
        else
            mask |= GLX_CONTEXT_ES2_PROFILE_BIT_EXT;
498

499
500
        if (ctxconfig->debug)
            flags |= GLX_CONTEXT_DEBUG_BIT_ARB;
501
502
        if (ctxconfig->noerror)
            flags |= GL_CONTEXT_FLAG_NO_ERROR_BIT_KHR;
503

504
        if (ctxconfig->robustness)
505
        {
506
            if (_glfw.glx.ARB_create_context_robustness)
507
            {
508
                if (ctxconfig->robustness == GLFW_NO_RESET_NOTIFICATION)
509
510
511
512
                {
                    setGLXattrib(GLX_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB,
                                 GLX_NO_RESET_NOTIFICATION_ARB);
                }
513
                else if (ctxconfig->robustness == GLFW_LOSE_CONTEXT_ON_RESET)
514
515
516
517
                {
                    setGLXattrib(GLX_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB,
                                 GLX_LOSE_CONTEXT_ON_RESET_ARB);
                }
518

Camilla Berglund's avatar
Camilla Berglund committed
519
520
                flags |= GLX_CONTEXT_ROBUST_ACCESS_BIT_ARB;
            }
521
522
        }

523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
        if (ctxconfig->release)
        {
            if (_glfw.glx.ARB_context_flush_control)
            {
                if (ctxconfig->release == GLFW_RELEASE_BEHAVIOR_NONE)
                {
                    setGLXattrib(GLX_CONTEXT_RELEASE_BEHAVIOR_ARB,
                                 GLX_CONTEXT_RELEASE_BEHAVIOR_NONE_ARB);
                }
                else if (ctxconfig->release == GLFW_RELEASE_BEHAVIOR_FLUSH)
                {
                    setGLXattrib(GLX_CONTEXT_RELEASE_BEHAVIOR_ARB,
                                 GLX_CONTEXT_RELEASE_BEHAVIOR_FLUSH_ARB);
                }
            }
        }

Camilla Berglund's avatar
Camilla Berglund committed
540
541
542
        // NOTE: Only request an explicitly versioned context when necessary, as
        //       explicitly requesting version 1.0 does not always return the
        //       highest version supported by the driver
543
        if (ctxconfig->major != 1 || ctxconfig->minor != 0)
544
        {
545
546
            setGLXattrib(GLX_CONTEXT_MAJOR_VERSION_ARB, ctxconfig->major);
            setGLXattrib(GLX_CONTEXT_MINOR_VERSION_ARB, ctxconfig->minor);
Camilla Berglund's avatar
Camilla Berglund committed
547
        }
548

Camilla Berglund's avatar
Camilla Berglund committed
549
550
        if (mask)
            setGLXattrib(GLX_CONTEXT_PROFILE_MASK_ARB, mask);
551

Camilla Berglund's avatar
Camilla Berglund committed
552
553
554
555
        if (flags)
            setGLXattrib(GLX_CONTEXT_FLAGS_ARB, flags);

        setGLXattrib(None, None);
556

557
        window->context.glx.handle =
558
            _glfw.glx.CreateContextAttribsARB(_glfw.x11.display,
559
                                              native,
560
561
562
                                              share,
                                              True,
                                              attribs);
563

Camilla Berglund's avatar
Camilla Berglund committed
564
565
566
567
        // HACK: This is a fallback for broken versions of the Mesa
        //       implementation of GLX_ARB_create_context_profile that fail
        //       default 1.0 context creation with a GLXBadProfileARB error in
        //       violation of the extension spec
568
        if (!window->context.glx.handle)
569
        {
570
            if (_glfw.x11.errorCode == _glfw.glx.errorBase + GLXBadProfileARB &&
571
                ctxconfig->client == GLFW_OPENGL_API &&
572
                ctxconfig->profile == GLFW_OPENGL_ANY_PROFILE &&
573
                ctxconfig->forward == GLFW_FALSE)
574
            {
575
                window->context.glx.handle =
576
                    createLegacyContextGLX(window, native, share);
577
            }
578
579
        }
    }
580
    else
581
582
583
584
    {
        window->context.glx.handle =
            createLegacyContextGLX(window, native, share);
    }
585

586
    _glfwReleaseErrorHandlerX11();
587

588
    if (!window->context.glx.handle)
589
    {
590
        _glfwInputErrorX11(GLFW_VERSION_UNAVAILABLE, "GLX: Failed to create context");
591
        return GLFW_FALSE;
592
593
    }

594
595
    window->context.glx.window =
        glXCreateWindow(_glfw.x11.display, native, window->x11.handle, NULL);
596
    if (!window->context.glx.window)
597
598
599
600
601
    {
        _glfwInputError(GLFW_PLATFORM_ERROR, "GLX: Failed to create window");
        return GLFW_FALSE;
    }

602
603
604
605
606
607
    window->context.makeCurrent = makeContextCurrentGLX;
    window->context.swapBuffers = swapBuffersGLX;
    window->context.swapInterval = swapIntervalGLX;
    window->context.extensionSupported = extensionSupportedGLX;
    window->context.getProcAddress = getProcAddressGLX;
    window->context.destroy = destroyContextGLX;
608

609
    return GLFW_TRUE;
610
611
612
613
}

#undef setGLXattrib

614
615
// Returns the Visual and depth of the chosen GLXFBConfig
//
616
617
618
GLFWbool _glfwChooseVisualGLX(const _GLFWctxconfig* ctxconfig,
                              const _GLFWfbconfig* fbconfig,
                              Visual** visual, int* depth)
619
620
621
622
{
    GLXFBConfig native;
    XVisualInfo* result;

623
    if (!chooseGLXFBConfig(fbconfig, &native))
624
625
626
627
628
629
    {
        _glfwInputError(GLFW_FORMAT_UNAVAILABLE,
                        "GLX: Failed to find a suitable GLXFBConfig");
        return GLFW_FALSE;
    }

630
    result = glXGetVisualFromFBConfig(_glfw.x11.display, native);
631
632
633
634
635
636
637
638
639
640
641
642
643
644
    if (!result)
    {
        _glfwInputError(GLFW_PLATFORM_ERROR,
                        "GLX: Failed to retrieve Visual for GLXFBConfig");
        return GLFW_FALSE;
    }

    *visual = result->visual;
    *depth = result->depth;

    XFree(result);
    return GLFW_TRUE;
}

645

646
647
648
649
650
651
652
//////////////////////////////////////////////////////////////////////////
//////                        GLFW native API                       //////
//////////////////////////////////////////////////////////////////////////

GLFWAPI GLXContext glfwGetGLXContext(GLFWwindow* handle)
{
    _GLFWwindow* window = (_GLFWwindow*) handle;
653
    _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
654

655
    if (window->context.client == GLFW_NO_API)
656
657
658
659
660
    {
        _glfwInputError(GLFW_NO_WINDOW_CONTEXT, NULL);
        return NULL;
    }

661
    return window->context.glx.handle;
662
663
}

Camilla Berglund's avatar
Camilla Berglund committed
664
665
666
667
668
GLFWAPI GLXWindow glfwGetGLXWindow(GLFWwindow* handle)
{
    _GLFWwindow* window = (_GLFWwindow*) handle;
    _GLFW_REQUIRE_INIT_OR_RETURN(None);

669
    if (window->context.client == GLFW_NO_API)
Camilla Berglund's avatar
Camilla Berglund committed
670
671
672
673
674
675
676
677
    {
        _glfwInputError(GLFW_NO_WINDOW_CONTEXT, NULL);
        return None;
    }

    return window->context.glx.window;
}