dispatch_glx.c revision e52adb7b
1e52adb7bSmrg/*
2e52adb7bSmrg * Copyright © 2013 Intel Corporation
3e52adb7bSmrg *
4e52adb7bSmrg * Permission is hereby granted, free of charge, to any person obtaining a
5e52adb7bSmrg * copy of this software and associated documentation files (the "Software"),
6e52adb7bSmrg * to deal in the Software without restriction, including without limitation
7e52adb7bSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8e52adb7bSmrg * and/or sell copies of the Software, and to permit persons to whom the
9e52adb7bSmrg * Software is furnished to do so, subject to the following conditions:
10e52adb7bSmrg *
11e52adb7bSmrg * The above copyright notice and this permission notice (including the next
12e52adb7bSmrg * paragraph) shall be included in all copies or substantial portions of the
13e52adb7bSmrg * Software.
14e52adb7bSmrg *
15e52adb7bSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16e52adb7bSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17e52adb7bSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18e52adb7bSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19e52adb7bSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20e52adb7bSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21e52adb7bSmrg * IN THE SOFTWARE.
22e52adb7bSmrg */
23e52adb7bSmrg
24e52adb7bSmrg#include <assert.h>
25e52adb7bSmrg#include <string.h>
26e52adb7bSmrg#include <stdio.h>
27e52adb7bSmrg
28e52adb7bSmrg#include "dispatch_common.h"
29e52adb7bSmrg
30e52adb7bSmrg/**
31e52adb7bSmrg * If we can determine the GLX version from the current context, then
32e52adb7bSmrg * return that, otherwise return a version that will just send us on
33e52adb7bSmrg * to dlsym() or get_proc_address().
34e52adb7bSmrg */
35e52adb7bSmrgint
36e52adb7bSmrgepoxy_conservative_glx_version(void)
37e52adb7bSmrg{
38e52adb7bSmrg    Display *dpy = glXGetCurrentDisplay();
39e52adb7bSmrg    GLXContext ctx = glXGetCurrentContext();
40e52adb7bSmrg    int screen;
41e52adb7bSmrg
42e52adb7bSmrg    if (!dpy || !ctx)
43e52adb7bSmrg        return 14;
44e52adb7bSmrg
45e52adb7bSmrg    glXQueryContext(dpy, ctx, GLX_SCREEN, &screen);
46e52adb7bSmrg
47e52adb7bSmrg    return epoxy_glx_version(dpy, screen);
48e52adb7bSmrg}
49e52adb7bSmrg
50e52adb7bSmrgPUBLIC int
51e52adb7bSmrgepoxy_glx_version(Display *dpy, int screen)
52e52adb7bSmrg{
53e52adb7bSmrg    int server_major, server_minor;
54e52adb7bSmrg    int client_major, client_minor;
55e52adb7bSmrg    int server, client;
56e52adb7bSmrg    const char *version_string;
57e52adb7bSmrg    int ret;
58e52adb7bSmrg
59e52adb7bSmrg    version_string = glXQueryServerString(dpy, screen, GLX_VERSION);
60e52adb7bSmrg    ret = sscanf(version_string, "%d.%d", &server_major, &server_minor);
61e52adb7bSmrg    assert(ret == 2);
62e52adb7bSmrg    server = server_major * 10 + server_minor;
63e52adb7bSmrg
64e52adb7bSmrg    version_string = glXGetClientString(dpy, GLX_VERSION);
65e52adb7bSmrg    ret = sscanf(version_string, "%d.%d", &client_major, &client_minor);
66e52adb7bSmrg    assert(ret == 2);
67e52adb7bSmrg    client = client_major * 10 + client_minor;
68e52adb7bSmrg
69e52adb7bSmrg    if (client < server)
70e52adb7bSmrg        return client;
71e52adb7bSmrg    else
72e52adb7bSmrg        return server;
73e52adb7bSmrg}
74e52adb7bSmrg
75e52adb7bSmrg/**
76e52adb7bSmrg * If we can determine the GLX extension support from the current
77e52adb7bSmrg * context, then return that, otherwise give the answer that will just
78e52adb7bSmrg * send us on to get_proc_address().
79e52adb7bSmrg */
80e52adb7bSmrgbool
81e52adb7bSmrgepoxy_conservative_has_glx_extension(const char *ext)
82e52adb7bSmrg{
83e52adb7bSmrg    Display *dpy = glXGetCurrentDisplay();
84e52adb7bSmrg    GLXContext ctx = glXGetCurrentContext();
85e52adb7bSmrg    int screen;
86e52adb7bSmrg
87e52adb7bSmrg    if (!dpy || !ctx)
88e52adb7bSmrg        return true;
89e52adb7bSmrg
90e52adb7bSmrg    glXQueryContext(dpy, ctx, GLX_SCREEN, &screen);
91e52adb7bSmrg
92e52adb7bSmrg    return epoxy_has_glx_extension(dpy, screen, ext);
93e52adb7bSmrg}
94e52adb7bSmrg
95e52adb7bSmrgPUBLIC bool
96e52adb7bSmrgepoxy_has_glx_extension(Display *dpy, int screen, const char *ext)
97e52adb7bSmrg {
98e52adb7bSmrg    /* No, you can't just use glXGetClientString or
99e52adb7bSmrg     * glXGetServerString() here.  Those each tell you about one half
100e52adb7bSmrg     * of what's needed for an extension to be supported, and
101e52adb7bSmrg     * glXQueryExtensionsString() is what gives you the intersection
102e52adb7bSmrg     * of the two.
103e52adb7bSmrg     */
104e52adb7bSmrg    return epoxy_extension_in_string(glXQueryExtensionsString(dpy, screen), ext);
105e52adb7bSmrg}
106