1173a43bceae6e455ad512c2462b394a8a965407
[libgliar] / src / gliar.c
1 /*
2 libgliar - a library that can fake the OpenGL context info returned by
3 the glGet OpenGL calls
4
5 Copyright (C) 2013 Canonical Ltd
6
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program.        If not, see <http://www.gnu.org/licenses/>.
19
20 Author: Eleni Maria Stea <elene.mst@gmail.com>
21 */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <dlfcn.h>
28 #include <pwd.h>
29 #include <GL/gl.h>
30 #include "cfg.h"
31
32 static int done_init;
33
34 static const GLubyte* (*gl_get_string)(GLenum);
35 /*static const GLubyte* (*gl_get_stringi)(GLenum, GLuint);
36
37 static const void* (*gl_get_booleanv)(GLenum, GLboolean*);
38 static const void* (*gl_get_doublev)(GLenum, GLdouble*);
39 static const void* (*gl_get_floatv)(GLenum, GLfloat*);
40 static const void* (*gl_get_integerv)(GLenum, GLint*);
41 static const void* (*gl_get_integer64v)(GLenum, GLint64*);
42
43 static const void* (*gl_get_booleani_v)(GLenum, GLuint, GLboolean*);
44 static const void* (*gl_get_doublei_v)(GLenum, GLuint, GLdouble*);
45 static const void* (*gl_get_floati_v)(GLenum, GLuint, GLfloat*);
46 static const void* (*gl_get_integeri_v)(GLenum, GLuint, GLint*);
47 static const void* (*gl_get_integer64i_v)(GLenum, GLuint, GLint64*);*/
48
49 static struct cfgopt *cfglist;
50
51 static int init(void)
52 {
53         if(done_init) {
54                 return 0;
55         }
56
57         if(!(cfglist = gliar_load_cfg("gliar.conf"))) {
58                 struct passwd *pw;
59                 char *homedir, *path;
60
61                 if((pw = getpwuid(getuid()))) {
62                         homedir = pw->pw_dir;
63                 } else {
64                         homedir = getenv("HOME");
65                 }
66
67                 if(homedir) {
68                         path = alloca(strlen(homedir) + strlen(".gliar.conf") + 2);
69                         sprintf(path, "%s/.gliar.conf", homedir);
70
71                         cfglist = gliar_load_cfg(path);
72                 }
73         }
74
75         gl_get_string = dlsym(RTLD_NEXT, "glGetString");
76 /*      gl_get_stringi = dlsym(RTLD_NEXT, "glGetStringi");
77
78         gl_get_booleanv = dlsym(RTLD_NEXT, "glGetBooleanv");
79         gl_get_doublev = dlsym(RTLD_NEXT, "glGetDoublev");
80         gl_get_floatv = dlsym(RTLD_NEXT, "glGetFloatv");
81         gl_get_integerv = dlsym(RTLD_NEXT, "glGetIntegerv");
82         gl_get_integer64v = dlsym(RTLD_NEXT, "glGetInteger64v");
83
84         gl_get_booleani_v = dlsym(RTLD_NEXT, "glGetBooleani_v");
85         gl_get_doublei_v = dlsym(RTLD_NEXT, "glGetDoublei_v");
86         gl_get_floati_v = dlsym(RTLD_NEXT, "glGetFloati_v");
87         gl_get_integeri_v = dlsym(RTLD_NEXT, "glGetIntegeri_v");
88         gl_get_integer64i_v = dlsym(RTLD_NEXT, "glGetInteger64i_v");*/
89
90         done_init = 1;
91         return 0;
92 }
93
94 const GLubyte *glGetString(GLenum name)
95 {
96         const char *key;
97         const struct cfgopt *option;
98
99         init();
100
101         switch(name) {
102         case GL_VENDOR:
103                 key = "vendor";
104                 break;
105
106         case GL_VERSION:
107                 key = "version";
108                 break;
109
110         case GL_EXTENSIONS:
111                 key = "extensions";
112                 break;
113
114         case GL_RENDERER:
115                 key = "renderer";
116                 break;
117
118         case GL_SHADING_LANGUAGE_VERSION:
119                 key = "sl version";
120                 break;
121
122         default:
123                 key = 0;
124         }
125
126         if(key && (option = gliar_find_opt(cfglist, key))) {
127                 return (const GLubyte*)option->conc_vals;
128         }
129
130         return gl_get_string(name);
131 }