added support for numerical values in option struct
[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, *value;
97
98         init();
99
100         switch(name) {
101         case GL_VENDOR:
102                 key = "vendor";
103                 break;
104
105         case GL_VERSION:
106                 key = "version";
107                 break;
108
109         case GL_EXTENSIONS:
110                 key = "extensions";
111                 break;
112
113   case GL_RENDERER:
114     key = "renderer";
115     break;
116
117   case GL_SHADING_LANGUAGE_VERSION:
118     key = "sl version";
119     break;
120
121         default:
122                 key = 0;
123         }
124
125         if(key && (value = gliar_find_opt(cfglist, key))) {
126                 return value;
127         }
128
129         return gl_get_string(name);
130 }