added renderer, shading language version
[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 static const GLubyte* (*gl_get_string)(GLenum);
34 static struct cfgopt *cfglist;
35
36 static int init(void)
37 {
38         if(done_init) {
39                 return 0;
40         }
41
42         if(!(cfglist = gliar_load_cfg("gliar.conf"))) {
43                 struct passwd *pw;
44                 char *homedir, *path;
45
46                 if((pw = getpwuid(getuid()))) {
47                         homedir = pw->pw_dir;
48                 } else {
49                         homedir = getenv("HOME");
50                 }
51
52                 if(homedir) {
53                         path = alloca(strlen(homedir) + strlen(".gliar.conf") + 2);
54                         sprintf(path, "%s/.gliar.conf", homedir);
55
56                         cfglist = gliar_load_cfg(path);
57                 }
58         }
59
60         gl_get_string = dlsym(RTLD_NEXT, "glGetString");
61
62         done_init = 1;
63         return 0;
64 }
65
66 const GLubyte *glGetString(GLenum name)
67 {
68         const char *key, *value;
69
70         init();
71
72         switch(name) {
73         case GL_VENDOR:
74                 key = "vendor";
75                 break;
76
77         case GL_VERSION:
78                 key = "version";
79                 break;
80
81         case GL_EXTENSIONS:
82                 key = "extensions";
83                 break;
84
85   case GL_RENDERER:
86     key = "renderer";
87     break;
88
89   case GL_SHADING_LANGUAGE_VERSION:
90     key = "sl version";
91     break;
92
93         default:
94                 key = 0;
95         }
96
97         if(key && (value = gliar_find_opt(cfglist, key))) {
98                 return value;
99         }
100
101         return gl_get_string(name);
102 }