]> git.cworth.org Git - glenv/blob - glenv.c
824b8b8bfaf697a3131added5ca149c2a1fbbaf3
[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                 setenv ("GLENV_QUERY", "1", 1);
46         else
47                 unsetenv ("GLENV_QUERY");
48
49         if (options->vendor)
50                 setenv ("GLENV_GL_VENDOR", options->vendor, 1);
51         else
52                 unsetenv ("GLENV_GL_VENDOR");
53
54         if (options->renderer)
55                 setenv ("GLENV_GL_RENDERER", options->renderer, 1);
56         else
57                 unsetenv ("GLENV_GL_RENDERER");
58
59         if (options->version)
60                 setenv ("GLENV_GL_VERSION", options->version, 1);
61         else
62                 unsetenv ("GLENV_GL_VERSION");
63
64         if (options->shading_language_version)
65                 setenv ("GLENV_GL_SHADING_LANGUAGE_VERSION",
66                         options->shading_language_version, 1);
67         else
68                 unsetenv ("GLENV_GL_SHADING_LANGUAGE_VERSION");
69
70         if (options->extensions)
71                 setenv ("GLENV_EXTENSIONS", options->extensions, 1);
72         else
73                 unsetenv ("GLENV_EXTENSIONS");
74
75         if (options->extensions_whitelist)
76                 setenv ("GLENV_EXTENSIONS_WHITELIST", options->extensions_whitelist, 1);
77         else
78                 unsetenv ("GLENV_EXTENSIONS_WHITELIST");
79
80         if (options->extensions_blacklist)
81                 setenv ("GLENV_EXTENSIONS_BLACKLIST", options->extensions_blacklist, 1);
82         else
83                 unsetenv ("GLENV_EXTENSIONS_BLACKLIST");
84 }
85
86 static void
87 usage (void)
88 {
89         printf ("Usage: glenv [OPTIONS...] <program> [program args...]\n"
90                 "\n"
91                 "Execute <program> with alternate OpenGL environment.\n"
92                 "\n"
93                 "Options:\n"
94                 "       -h, --help                      Show this help message.\n"
95                 "       -q, --query                     Query and report current environment\n"
96                 "                                       then terminate program.\n"
97                 "       --vendor=STR                    Set GL_VENDOR to STR.\n"
98                 "       --renderer=STR                  Set GL_RENDERER to STR.\n"
99                 "       --version=STR                   Set GL_VERSION to STR.\n"
100                 "       --shading-language-version=STR  Set GL_SHADING_LANGUAGE_VERSION to STR.\n"
101                 "       --extensions=STR                Set GL_EXTENSIONS to STR.\n"
102                 "       --extensions-whitelist=STR      Remove from GL_EXTENSIONS all names\n"
103                 "                                       not appearing in STR (space-separated).\n"
104                 "       --extensions-blacklist=STR      Remove from GL_EXTENSIONS any names\n"
105                 "                                       appearing in STR (space-separated).\n");
106 }
107
108 enum {
109         VENDOR_OPT = CHAR_MAX + 1,
110         RENDERER_OPT,
111         VERSION_OPT,
112         SHADING_LANGUAGE_VERSION_OPT,
113         EXTENSIONS_OPT,
114         EXTENSIONS_WHITELIST_OPT,
115         EXTENSIONS_BLACKLIST_OPT
116 };
117
118 int
119 main (int argc, char *argv[])
120 {
121         int opt;
122         options_t options = {
123                 .query = false,
124                 .vendor = NULL,
125                 .renderer = NULL,
126                 .version = NULL,
127                 .shading_language_version = NULL,
128                 .extensions = NULL,
129                 .extensions_whitelist = NULL,
130                 .extensions_blacklist = NULL
131         };
132
133         const char *short_options="hq";
134         const struct option long_options[] = {
135                 {"help", no_argument, 0, 'h'},
136                 {"query", no_argument, 0, 'q'},
137                 {"vendor", required_argument, 0, VENDOR_OPT},
138                 {"renderer", required_argument, 0, RENDERER_OPT},
139                 {"version", required_argument, 0, VERSION_OPT},
140                 {"shading-language-version", required_argument, 0, SHADING_LANGUAGE_VERSION_OPT},
141                 {"extensions", required_argument, 0, EXTENSIONS_OPT},
142                 {"extensions-whitelist", required_argument, 0, EXTENSIONS_WHITELIST_OPT},
143                 {"extensions-blacklist", required_argument, 0, EXTENSIONS_BLACKLIST_OPT},
144                 {0, 0, 0, 0}
145         };
146
147         while (1)
148         {
149                 opt = getopt_long (argc, argv, short_options, long_options, NULL);
150                 if (opt == -1)
151                         break;
152
153                 switch (opt) {
154                 case 'h':
155                         usage ();
156                         return 0;
157                 case '?':
158                         exit (1);
159                         break;
160                 case 'q':
161                         options.query = true;
162                         break;
163                 case VENDOR_OPT:
164                         options.vendor = optarg;
165                         break;
166                 case RENDERER_OPT:
167                         options.renderer = optarg;
168                         break;
169                 case VERSION_OPT:
170                         options.version = optarg;
171                         break;
172                 case SHADING_LANGUAGE_VERSION_OPT:
173                         options.shading_language_version = optarg;
174                         break;
175                 case EXTENSIONS_OPT:
176                         options.extensions = optarg;
177                         break;
178                 case EXTENSIONS_WHITELIST_OPT:
179                         options.extensions_whitelist = optarg;
180                         break;
181                 case EXTENSIONS_BLACKLIST_OPT:
182                         options.extensions_blacklist = optarg;
183                         break;
184                 default:
185                         fprintf (stderr, "Internal error: "
186                                  "unexpected getopt value: %d\n", opt);
187                         exit (1);
188                 }
189         }
190
191         if (optind >= argc) {
192                 fprintf (stderr, "Error: No program name provided, "
193                          "see (glenv --help)\n");
194                 exit (1);
195         }
196
197         export_options (&options);
198
199         glaze_execute (argc - optind, &argv[optind], "libglenv.so");
200
201         /* If glaze_execute returns then something went wrong. */
202         return 1;
203 }