VirtualBox

source: vbox/trunk/src/VBox/GuestHost/OpenGL/spu_loader/spuinit.c@ 19454

Last change on this file since 19454 was 15532, checked in by vboxsync, 16 years ago

crOpenGL: export to OSE

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.7 KB
Line 
1/* Copyright (c) 2001, Stanford University
2 * All rights reserved
3 *
4 * See the file LICENSE.txt for information on redistributing this software.
5 */
6
7#include "cr_spu.h"
8#include "cr_error.h"
9#include "cr_string.h"
10#include <stdio.h>
11
12/**
13 * \mainpage spu_loader
14 *
15 * \section Spu_loaderIntroduction Introduction
16 *
17 * Chromium consists of all the top-level files in the cr
18 * directory. The spu_loader module basically takes care of API dispatch,
19 * and OpenGL state management.
20 *
21 */
22void crSPUInitDispatchTable( SPUDispatchTable *table )
23{
24 table->copyList = NULL;
25 table->copy_of = NULL;
26 table->mark = 0;
27 table->server = NULL;
28}
29
30static int validate_int( const char *response,
31 const char *min,
32 const char *max )
33{
34 int i, imin, imax;
35 if (sscanf(response, "%d", &i) != 1)
36 return 0;
37 if (min && sscanf(min, "%d", &imin) == 1 && imin > i)
38 return 0;
39 if (max && sscanf(max, "%d", &imax) == 1 && imax < i)
40 return 0;
41 return 1;
42}
43
44static int validate_float( const char *response,
45 const char *min,
46 const char *max )
47{
48 float f, fmin, fmax;
49 if (sscanf(response, "%f", &f) != 1)
50 return 0;
51 if (min && sscanf(min, "%f", &fmin) == 1 && fmin > f)
52 return 0;
53 if (max && sscanf(max, "%f", &fmax) == 1 && fmax < f)
54 return 0;
55 return 1;
56}
57
58static int validate_one_option( const SPUOptions *opt,
59 const char *response,
60 const char *min,
61 const char *max )
62{
63 switch (opt->type) {
64 case CR_BOOL:
65 return validate_int( response, "0", "1" );
66 case CR_INT:
67 return validate_int( response, min, max );
68 case CR_FLOAT:
69 return validate_float( response, min, max );
70 case CR_ENUM:
71 /* Make sure response string is present in the min string.
72 * For enums, the min string is a comma-separted list of valid values.
73 */
74 CRASSERT(opt->numValues == 1); /* an enum limitation for now */
75 {
76 const char *p = crStrstr(min, response);
77 if (!p)
78 return 0; /* invalid value! */
79 if (p[-1] != '\'')
80 return 0; /* right substring */
81 if (p[crStrlen(response)] != '\'')
82 return 0; /* left substring */
83 return 1;
84 }
85 default:
86 return 0;
87 }
88}
89
90
91/**
92 * Make sure the response matches the opt's parameters (right number
93 * and type of values, etc.)
94 * Return 1 if OK, 0 if error.
95 */
96static int validate_option( const SPUOptions *opt, const char *response )
97{
98 const char *min = opt->min;
99 const char *max = opt->max;
100 int i = 0;
101 int retval;
102
103 if (opt->type == CR_STRING)
104 return 1;
105
106 CRASSERT(opt->numValues > 0);
107
108 /* skip leading [ for multi-value options */
109 if (opt->numValues > 1) {
110 /* multi-valued options must be enclosed in brackets */
111 if (*response != '[')
112 return 0;
113 response++; /* skip [ */
114 /* make sure min and max are bracketed as well */
115 if (min) {
116 CRASSERT(*min == '['); /* error in <foo>spu_config.c code!!! */
117 min++;
118 }
119 if (max) {
120 CRASSERT(*max == '['); /* error in <foo>spu_config.c code!!! */
121 max++;
122 }
123 }
124
125 for (;;)
126 {
127 if (!validate_one_option( opt, response, min, max ))
128 {
129 retval = 0;
130 break;
131 }
132 if (++i == opt->numValues)
133 {
134 retval = 1; /* all done! */
135 break;
136 }
137 /* advance pointers to next item */
138 if (min)
139 {
140 while (*min != ' ' && *min)
141 min++;
142 while (*min == ' ')
143 min++;
144 }
145 if (max)
146 {
147 while (*max != ' ' && *max)
148 max++;
149 while (*max == ' ')
150 max++;
151 }
152 if (response)
153 {
154 while (*response != ' ' && *response)
155 response++;
156 while (*response == ' ')
157 response++;
158 }
159 }
160
161 return retval;
162}
163
164/** Use the default values for all the options:
165 */
166void crSPUSetDefaultParams( void *spu, SPUOptions *options )
167{
168 int i;
169
170 for (i = 0 ; options[i].option ; i++)
171 {
172 SPUOptions *opt = &options[i];
173 opt->cb( spu, opt->deflt );
174 }
175}
176
177
178/**
179 * Find the index of the given enum value in the SPUOption's list of
180 * possible enum values.
181 * Return the enum index, or -1 if not found.
182 */
183int crSPUGetEnumIndex( const SPUOptions *options, const char *optName, const char *value )
184{
185 const SPUOptions *opt;
186 const int valueLen = crStrlen(value);
187
188 /* first, find the right option */
189 for (opt = options; opt->option; opt++) {
190 if (crStrcmp(opt->option, optName) == 0) {
191 char **values;
192 int i;
193
194 CRASSERT(opt->type == CR_ENUM);
195
196 /* break into array of strings */
197 /* min string should be of form "'enum1', 'enum2', 'enum3', etc" */
198 values = crStrSplit(opt->min, ",");
199
200 /* search the array */
201 for (i = 0; values[i]; i++) {
202 /* find leading quote */
203 const char *e = crStrchr(values[i], '\'');
204 CRASSERT(e);
205 if (e) {
206 /* test for match */
207 if (crStrncmp(value, e + 1, valueLen) == 0 && e[valueLen + 1] == '\'') {
208 crFreeStrings(values);
209 return i;
210 }
211 }
212 }
213
214 /* enum value not found! */
215 crFreeStrings(values);
216 return -1;
217 }
218 }
219
220 return -1;
221}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette