1b8e80941Smrg#include <string.h>
2b8e80941Smrg#include <stdlib.h>
3b8e80941Smrg#include <X11/Xlib.h>
4b8e80941Smrg
5b8e80941Smrg#include "glvnd/libglxabi.h"
6b8e80941Smrg
7b8e80941Smrg#include "glxglvnd.h"
8b8e80941Smrg
9b8e80941Smrgstatic Bool __glXGLVNDIsScreenSupported(Display *dpy, int screen)
10b8e80941Smrg{
11b8e80941Smrg    /* TODO: Think of a better heuristic... */
12b8e80941Smrg    return True;
13b8e80941Smrg}
14b8e80941Smrg
15b8e80941Smrgstatic void *__glXGLVNDGetProcAddress(const GLubyte *procName)
16b8e80941Smrg{
17b8e80941Smrg    return glXGetProcAddressARB(procName);
18b8e80941Smrg}
19b8e80941Smrg
20b8e80941Smrgstatic int
21b8e80941Smrgcompare(const void *l, const void *r)
22b8e80941Smrg{
23b8e80941Smrg    const char *s = *(const char **)r;
24b8e80941Smrg    return strcmp(l, s);
25b8e80941Smrg}
26b8e80941Smrg
27b8e80941Smrgstatic unsigned FindGLXFunction(const GLubyte *name)
28b8e80941Smrg{
29b8e80941Smrg    const char **match;
30b8e80941Smrg
31b8e80941Smrg    match = bsearch(name, __glXDispatchTableStrings, DI_FUNCTION_COUNT,
32b8e80941Smrg                    sizeof(const char *), compare);
33b8e80941Smrg
34b8e80941Smrg    if (match == NULL)
35b8e80941Smrg        return DI_FUNCTION_COUNT;
36b8e80941Smrg
37b8e80941Smrg    return match - __glXDispatchTableStrings;
38b8e80941Smrg}
39b8e80941Smrg
40b8e80941Smrgstatic void *__glXGLVNDGetDispatchAddress(const GLubyte *procName)
41b8e80941Smrg{
42b8e80941Smrg    unsigned internalIndex = FindGLXFunction(procName);
43b8e80941Smrg
44b8e80941Smrg    return (void*)__glXDispatchFunctions[internalIndex];
45b8e80941Smrg}
46b8e80941Smrg
47b8e80941Smrgstatic void __glXGLVNDSetDispatchIndex(const GLubyte *procName, int index)
48b8e80941Smrg{
49b8e80941Smrg    unsigned internalIndex = FindGLXFunction(procName);
50b8e80941Smrg
51b8e80941Smrg    if (internalIndex == DI_FUNCTION_COUNT)
52b8e80941Smrg        return; /* unknown or static dispatch */
53b8e80941Smrg
54b8e80941Smrg    __glXDispatchTableIndices[internalIndex] = index;
55b8e80941Smrg}
56b8e80941Smrg
57b8e80941Smrg_X_EXPORT Bool __glx_Main(uint32_t version, const __GLXapiExports *exports,
58b8e80941Smrg                          __GLXvendorInfo *vendor, __GLXapiImports *imports)
59b8e80941Smrg{
60b8e80941Smrg    static Bool initDone = False;
61b8e80941Smrg
62b8e80941Smrg    if (GLX_VENDOR_ABI_GET_MAJOR_VERSION(version) !=
63b8e80941Smrg        GLX_VENDOR_ABI_MAJOR_VERSION ||
64b8e80941Smrg        GLX_VENDOR_ABI_GET_MINOR_VERSION(version) <
65b8e80941Smrg        GLX_VENDOR_ABI_MINOR_VERSION)
66b8e80941Smrg        return False;
67b8e80941Smrg
68b8e80941Smrg    if (!initDone) {
69b8e80941Smrg        initDone = True;
70b8e80941Smrg        __glXGLVNDAPIExports = exports;
71b8e80941Smrg
72b8e80941Smrg        imports->isScreenSupported = __glXGLVNDIsScreenSupported;
73b8e80941Smrg        imports->getProcAddress = __glXGLVNDGetProcAddress;
74b8e80941Smrg        imports->getDispatchAddress = __glXGLVNDGetDispatchAddress;
75b8e80941Smrg        imports->setDispatchIndex = __glXGLVNDSetDispatchIndex;
76b8e80941Smrg        imports->notifyError = NULL;
77b8e80941Smrg        imports->isPatchSupported = NULL;
78b8e80941Smrg        imports->initiatePatch = NULL;
79b8e80941Smrg    }
80b8e80941Smrg
81b8e80941Smrg    return True;
82b8e80941Smrg}
83