Created
November 11, 2016 03:17
-
-
Save tycho/a6ec49d03f9e9d03fac3bc1b8b1fef98 to your computer and use it in GitHub Desktop.
glGetString override
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Build with: gcc -s -O2 -shared -fPIC -o glgetstring.{so,c} -ldl | |
| */ | |
| /* for RTLD_NEXT */ | |
| #ifndef _GNU_SOURCE | |
| #define _GNU_SOURCE | |
| #endif | |
| #include <dlfcn.h> | |
| #include <GL/gl.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| typedef struct _glstring_t { | |
| GLenum type; | |
| const char *environ_name; | |
| const char *environ; | |
| } glstring_t, *pglstring_t; | |
| #define DECLARE_GLSTRING(x) { x, "OVERRIDE_" #x, NULL } | |
| glstring_t table[] = { | |
| DECLARE_GLSTRING(GL_VENDOR), | |
| DECLARE_GLSTRING(GL_RENDERER), | |
| DECLARE_GLSTRING(GL_VERSION), | |
| DECLARE_GLSTRING(GL_EXTENSIONS), | |
| { 0 }, | |
| }; | |
| typedef const GLubyte *GLAPIENTRY (*glGetString_t)(GLenum); | |
| const GLubyte *GLAPIENTRY glGetString( GLenum type ) | |
| { | |
| static glGetString_t next = NULL; | |
| pglstring_t string; | |
| if (!next) | |
| { | |
| /* Fill in defaults since this is the first call. */ | |
| next = dlsym(RTLD_NEXT, "glGetString"); | |
| for (string = table; string && string->environ_name; string++) { | |
| string->environ = getenv(string->environ_name); | |
| } | |
| } | |
| /* Find a match in the override table. */ | |
| for (string = table; string && string->environ_name; string++) | |
| { | |
| if (string->type != type) | |
| continue; | |
| /* Override specified in environment. */ | |
| if (string->environ) | |
| return string->environ; | |
| /* No override, fall back to API call */ | |
| break; | |
| } | |
| /* If we get here, it's a type that wasn't in the table, but may be defined | |
| * in the API. | |
| */ | |
| return next(type); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment