]> git.cworth.org Git - glenv/blob - glenv.c
6d27ad4bf402b6d380d619bb9730b50443714c76
[glenv] / glenv.c
1 /* Copyright © 2013, Intel Corporation
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdbool.h>
25 #include <limits.h>
26 #include <getopt.h>
27
28 #include <glaze.h>
29
30 typedef struct options {
31         bool query;
32         char *vendor;
33         char *renderer;
34         char *version;
35         char *shading_language_version;
36         char *extensions;
37         char *extensions_whitelist;
38         char *extensions_blacklist;
39 } options_t;
40
41 static void
42 export_options (options_t *options)
43 {
44         if (options->query) {
45                 glaze_set_first_gl_call_callback ("glenv_query");
46         }
47
48         if (options->vendor)
49                 setenv ("GLENV_GL_VENDOR", options->vendor, 1);
50         else
51                 unsetenv ("GLENV_GL_VENDOR");
52
53         if (options->renderer)
54                 setenv ("GLENV_GL_RENDERER", options->renderer, 1);
55         else
56                 unsetenv ("GLENV_GL_RENDERER");
57
58         if (options->version)
59                 setenv ("GLENV_GL_VERSION", options->version, 1);
60         else
61                 unsetenv ("GLENV_GL_VERSION");
62
63         if (options->shading_language_version)
64                 setenv ("GLENV_GL_SHADING_LANGUAGE_VERSION",
65                         options->shading_language_version, 1);
66         else
67                 unsetenv ("GLENV_GL_SHADING_LANGUAGE_VERSION");
68
69         if (options->extensions)
70                 setenv ("GLENV_EXTENSIONS", options->extensions, 1);
71         else
72                 unsetenv ("GLENV_EXTENSIONS");
73
74         if (options->extensions_whitelist)
75                 setenv ("GLENV_EXTENSIONS_WHITELIST", options->extensions_whitelist, 1);
76         else
77                 unsetenv ("GLENV_EXTENSIONS_WHITELIST");
78
79         if (options->extensions_blacklist)
80                 setenv ("GLENV_EXTENSIONS_BLACKLIST", options->extensions_blacklist, 1);
81         else
82                 unsetenv ("GLENV_EXTENSIONS_BLACKLIST");
83 }
84
85 static void
86 usage (void)
87 {
88         printf ("Usage: glenv [OPTIONS...] <program> [program args...]\n"
89                 "\n"
90                 "Execute <program> with alternate OpenGL environment.\n"
91                 "\n"
92                 "Options:\n"
93                 "       -h, --help                      Show this help message.\n"
94                 "       -q, --query                     Query and report current environment\n"
95                 "                                       then terminate program.\n"
96                 "       --vendor=STR                    Set GL_VENDOR to STR.\n"
97                 "       --renderer=STR                  Set GL_RENDERER to STR.\n"
98                 "       --version=STR                   Set GL_VERSION to STR.\n"
99                 "       --shading-language-version=STR  Set GL_SHADING_LANGUAGE_VERSION to STR.\n"
100                 "       --extensions=STR                Set GL_EXTENSIONS to STR.\n"
101                 "       --extensions-whitelist=STR      Remove from GL_EXTENSIONS all names\n"
102                 "                                       not appearing in STR (space-separated).\n"
103                 "       --extensions-blacklist=STR      Remove from GL_EXTENSIONS any names\n"
104                 "                                       appearing in STR (space-separated).\n");
105 }
106
107 enum {
108         VENDOR_OPT = CHAR_MAX + 1,
109         RENDERER_OPT,
110         VERSION_OPT,
111         SHADING_LANGUAGE_VERSION_OPT,
112         EXTENSIONS_OPT,
113         EXTENSIONS_WHITELIST_OPT,
114         EXTENSIONS_BLACKLIST_OPT
115 };
116
117 int
118 main (int argc, char *argv[])
119 {
120         int opt;
121         options_t options = {
122                 .query = false,
123                 .vendor = NULL,
124                 .renderer = NULL,
125                 .version = NULL,
126                 .shading_language_version = NULL,
127                 .extensions = NULL,
128                 .extensions_whitelist = NULL,
129                 .extensions_blacklist = NULL
130         };
131
132         const char *short_options="hq";
133         const struct option long_options[] = {
134                 {"help", no_argument, 0, 'h'},
135                 {"query", no_argument, 0, 'q'},
136                 {"vendor", required_argument, 0, VENDOR_OPT},
137                 {"renderer", required_argument, 0, RENDERER_OPT},
138                 {"version", required_argument, 0, VERSION_OPT},
139                 {"shading-language-version", required_argument, 0, SHADING_LANGUAGE_VERSION_OPT},
140                 {"extensions", required_argument, 0, EXTENSIONS_OPT},
141                 {"extensions-whitelist", required_argument, 0, EXTENSIONS_WHITELIST_OPT},
142                 {"extensions-blacklist", required_argument, 0, EXTENSIONS_BLACKLIST_OPT},
143                 {0, 0, 0, 0}
144         };
145
146         while (1)
147         {
148                 opt = getopt_long (argc, argv, short_options, long_options, NULL);
149                 if (opt == -1)
150                         break;
151
152                 switch (opt) {
153                 case 'h':
154                         usage ();
155                         return 0;
156                 case '?':
157                         exit (1);
158                         break;
159                 case 'q':
160                         options.query = true;
161                         break;
162                 case VENDOR_OPT:
163                         options.vendor = optarg;
164                         break;
165                 case RENDERER_OPT:
166                         options.renderer = optarg;
167                         break;
168                 case VERSION_OPT:
169                         options.version = optarg;
170                         break;
171                 case SHADING_LANGUAGE_VERSION_OPT:
172                         options.shading_language_version = optarg;
173                         break;
174                 case EXTENSIONS_OPT:
175                         options.extensions = optarg;
176                         break;
177                 case EXTENSIONS_WHITELIST_OPT:
178                         options.extensions_whitelist = optarg;
179                         break;
180                 case EXTENSIONS_BLACKLIST_OPT:
181                         options.extensions_blacklist = optarg;
182                         break;
183                 default:
184                         fprintf (stderr, "Internal error: "
185                                  "unexpected getopt value: %d\n", opt);
186                         exit (1);
187                 }
188         }
189
190         if (optind >= argc) {
191                 fprintf (stderr, "Error: No program name provided, "
192                          "see (glenv --help)\n");
193                 exit (1);
194         }
195
196         export_options (&options);
197
198         glaze_execute (argc - optind, &argv[optind], "libglenv.so");
199
200         /* If glaze_execute returns then something went wrong. */
201         return 1;
202 }