2 * XML DRI client-side driver configuration
3 * Copyright (C) 2003 Felix Kuehling
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * FELIX KUEHLING, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
21 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * \brief Driver-independent client-side part of the XML configuration
27 * \author Felix Kuehling
36 #include "main/imports.h"
38 #include "xmlconfig.h"
40 #undef GET_PROGRAM_NAME
42 #if (defined(__GNU_LIBRARY__) || defined(__GLIBC__)) && !defined(__UCLIBC__)
43 # if !defined(__GLIBC__) || (__GLIBC__ < 2)
44 /* These aren't declared in any libc5 header */
45 extern char *program_invocation_name
, *program_invocation_short_name
;
47 # define GET_PROGRAM_NAME() program_invocation_short_name
48 #elif defined(__CYGWIN__)
49 # define GET_PROGRAM_NAME() program_invocation_short_name
50 #elif defined(__FreeBSD__) && (__FreeBSD__ >= 2)
51 # include <osreldate.h>
52 # if (__FreeBSD_version >= 440000)
54 # define GET_PROGRAM_NAME() getprogname()
56 #elif defined(__NetBSD__) && defined(__NetBSD_Version) && (__NetBSD_Version >= 106000100)
58 # define GET_PROGRAM_NAME() getprogname()
59 #elif defined(__APPLE__)
61 # define GET_PROGRAM_NAME() getprogname()
63 /* Solaris has getexecname() which returns the full path - return just
64 the basename to match BSD getprogname() */
68 static const char *__getProgramName () {
69 static const char *progname
;
71 if (progname
== NULL
) {
72 const char *e
= getexecname();
74 /* Have to make a copy since getexecname can return a readonly
75 string, but basename expects to be able to modify its arg. */
78 progname
= basename(n
);
85 # define GET_PROGRAM_NAME() __getProgramName()
88 #if !defined(GET_PROGRAM_NAME)
89 # if defined(__OpenBSD__) || defined(NetBSD) || defined(__UCLIBC__) || defined(ANDROID)
90 /* This is a hack. It's said to work on OpenBSD, NetBSD and GNU.
91 * Rogelio M.Serrano Jr. reported it's also working with UCLIBC. It's
92 * used as a last resort, if there is no documented facility available. */
93 static const char *__getProgramName () {
94 extern const char *__progname
;
95 char * arg
= strrchr(__progname
, '/');
101 # define GET_PROGRAM_NAME() __getProgramName()
103 # define GET_PROGRAM_NAME() ""
104 # warning "Per application configuration won't work with your OS version."
108 /** \brief Find an option in an option cache with the name as key */
109 static uint32_t findOption (const driOptionCache
*cache
, const char *name
) {
110 uint32_t len
= strlen (name
);
111 uint32_t size
= 1 << cache
->tableSize
, mask
= size
- 1;
115 /* compute a hash from the variable length name */
116 for (i
= 0, shift
= 0; i
< len
; ++i
, shift
= (shift
+8) & 31)
117 hash
+= (uint32_t)name
[i
] << shift
;
119 hash
= (hash
>> (16-cache
->tableSize
/2)) & mask
;
121 /* this is just the starting point of the linear search for the option */
122 for (i
= 0; i
< size
; ++i
, hash
= (hash
+1) & mask
) {
123 /* if we hit an empty entry then the option is not defined (yet) */
124 if (cache
->info
[hash
].name
== 0)
126 else if (!strcmp (name
, cache
->info
[hash
].name
))
129 /* this assertion fails if the hash table is full */
135 /** \brief Like strdup but using malloc and with error checking. */
136 #define XSTRDUP(dest,source) do { \
137 uint32_t len = strlen (source); \
138 if (!(dest = malloc(len+1))) { \
139 fprintf (stderr, "%s: %d: out of memory.\n", __FILE__, __LINE__); \
142 memcpy (dest, source, len+1); \
145 static int compare (const void *a
, const void *b
) {
146 return strcmp (*(char *const*)a
, *(char *const*)b
);
148 /** \brief Binary search in a string array. */
149 static uint32_t bsearchStr (const XML_Char
*name
,
150 const XML_Char
*elems
[], uint32_t count
) {
151 const XML_Char
**found
;
152 found
= bsearch (&name
, elems
, count
, sizeof (XML_Char
*), compare
);
154 return found
- elems
;
159 /** \brief Locale-independent integer parser.
161 * Works similar to strtol. Leading space is NOT skipped. The input
162 * number may have an optional sign. Radix is specified by base. If
163 * base is 0 then decimal is assumed unless the input number is
164 * prefixed by 0x or 0X for hexadecimal or 0 for octal. After
165 * returning tail points to the first character that is not part of
166 * the integer number. If no number was found then tail points to the
167 * start of the input string. */
168 static int strToI (const XML_Char
*string
, const XML_Char
**tail
, int base
) {
169 int radix
= base
== 0 ? 10 : base
;
172 bool numberFound
= false;
173 const XML_Char
*start
= string
;
175 assert (radix
>= 2 && radix
<= 36);
177 if (*string
== '-') {
180 } else if (*string
== '+')
182 if (base
== 0 && *string
== '0') {
184 if (*(string
+1) == 'x' || *(string
+1) == 'X') {
195 if (*string
>= '0' && *string
< '0' + radix
)
196 digit
= *string
- '0';
198 if (*string
>= '0' && *string
<= '9')
199 digit
= *string
- '0';
200 else if (*string
>= 'a' && *string
< 'a' + radix
- 10)
201 digit
= *string
- 'a' + 10;
202 else if (*string
>= 'A' && *string
< 'A' + radix
- 10)
203 digit
= *string
- 'A' + 10;
207 result
= radix
*result
+ digit
;
212 *tail
= numberFound
? string
: start
;
213 return sign
* result
;
216 /** \brief Locale-independent floating-point parser.
218 * Works similar to strtod. Leading space is NOT skipped. The input
219 * number may have an optional sign. '.' is interpreted as decimal
220 * point and may occur at most once. Optionally the number may end in
221 * [eE]<exponent>, where <exponent> is an integer as recognized by
222 * strToI. In that case the result is number * 10^exponent. After
223 * returning tail points to the first character that is not part of
224 * the floating point number. If no number was found then tail points
225 * to the start of the input string.
227 * Uses two passes for maximum accuracy. */
228 static float strToF (const XML_Char
*string
, const XML_Char
**tail
) {
229 int nDigits
= 0, pointPos
, exponent
;
230 float sign
= 1.0f
, result
= 0.0f
, scale
;
231 const XML_Char
*start
= string
, *numStart
;
234 if (*string
== '-') {
237 } else if (*string
== '+')
240 /* first pass: determine position of decimal point, number of
241 * digits, exponent and the end of the number. */
243 while (*string
>= '0' && *string
<= '9') {
248 if (*string
== '.') {
250 while (*string
>= '0' && *string
<= '9') {
256 /* no digits, no number */
261 if (*string
== 'e' || *string
== 'E') {
262 const XML_Char
*expTail
;
263 exponent
= strToI (string
+1, &expTail
, 10);
264 if (expTail
== string
+1)
272 /* scale of the first digit */
273 scale
= sign
* (float)pow (10.0, (double)(pointPos
-1 + exponent
));
275 /* second pass: parse digits */
277 if (*string
!= '.') {
278 assert (*string
>= '0' && *string
<= '9');
279 result
+= scale
* (float)(*string
- '0');
284 } while (nDigits
> 0);
289 /** \brief Parse a value of a given type. */
290 static unsigned char parseValue (driOptionValue
*v
, driOptionType type
,
291 const XML_Char
*string
) {
292 const XML_Char
*tail
= NULL
;
293 /* skip leading white-space */
294 string
+= strspn (string
, " \f\n\r\t\v");
297 if (!strcmp (string
, "false")) {
300 } else if (!strcmp (string
, "true")) {
307 case DRI_ENUM
: /* enum is just a special integer */
309 v
->_int
= strToI (string
, &tail
, 0);
312 v
->_float
= strToF (string
, &tail
);
317 v
->_string
= strndup(string
, STRING_CONF_MAXLEN
);
322 return false; /* empty string (or containing only white-space) */
323 /* skip trailing white space */
325 tail
+= strspn (tail
, " \f\n\r\t\v");
327 return false; /* something left over that is not part of value */
332 /** \brief Parse a list of ranges of type info->type. */
333 static unsigned char parseRanges (driOptionInfo
*info
, const XML_Char
*string
) {
334 XML_Char
*cp
, *range
;
336 driOptionRange
*ranges
;
338 XSTRDUP (cp
, string
);
339 /* pass 1: determine the number of ranges (number of commas + 1) */
341 for (nRanges
= 1; *range
; ++range
)
345 if ((ranges
= malloc(nRanges
*sizeof(driOptionRange
))) == NULL
) {
346 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
350 /* pass 2: parse all ranges into preallocated array */
352 for (i
= 0; i
< nRanges
; ++i
) {
355 end
= strchr (range
, ',');
358 sep
= strchr (range
, ':');
359 if (sep
) { /* non-empty interval */
361 if (!parseValue (&ranges
[i
].start
, info
->type
, range
) ||
362 !parseValue (&ranges
[i
].end
, info
->type
, sep
+1))
364 if (info
->type
== DRI_INT
&&
365 ranges
[i
].start
._int
> ranges
[i
].end
._int
)
367 if (info
->type
== DRI_FLOAT
&&
368 ranges
[i
].start
._float
> ranges
[i
].end
._float
)
370 } else { /* empty interval */
371 if (!parseValue (&ranges
[i
].start
, info
->type
, range
))
373 ranges
[i
].end
= ranges
[i
].start
;
385 assert (range
== NULL
);
387 info
->nRanges
= nRanges
;
388 info
->ranges
= ranges
;
392 /** \brief Check if a value is in one of info->ranges. */
393 static bool checkValue (const driOptionValue
*v
, const driOptionInfo
*info
) {
395 assert (info
->type
!= DRI_BOOL
); /* should be caught by the parser */
396 if (info
->nRanges
== 0)
398 switch (info
->type
) {
399 case DRI_ENUM
: /* enum is just a special integer */
401 for (i
= 0; i
< info
->nRanges
; ++i
)
402 if (v
->_int
>= info
->ranges
[i
].start
._int
&&
403 v
->_int
<= info
->ranges
[i
].end
._int
)
407 for (i
= 0; i
< info
->nRanges
; ++i
)
408 if (v
->_float
>= info
->ranges
[i
].start
._float
&&
409 v
->_float
<= info
->ranges
[i
].end
._float
)
415 assert (0); /* should never happen */
421 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
424 * Is called from the drivers.
426 * \param f \c printf like format string.
429 __driUtilMessage(const char *f
, ...)
433 if (getenv("LIBGL_DEBUG")) {
434 fprintf(stderr
, "libGL: ");
436 vfprintf(stderr
, f
, args
);
438 fprintf(stderr
, "\n");
442 /** \brief Output a warning message. */
443 #define XML_WARNING1(msg) do {\
444 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
445 (int) XML_GetCurrentLineNumber(data->parser), \
446 (int) XML_GetCurrentColumnNumber(data->parser)); \
448 #define XML_WARNING(msg,args...) do { \
449 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
450 (int) XML_GetCurrentLineNumber(data->parser), \
451 (int) XML_GetCurrentColumnNumber(data->parser), \
454 /** \brief Output an error message. */
455 #define XML_ERROR1(msg) do { \
456 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
457 (int) XML_GetCurrentLineNumber(data->parser), \
458 (int) XML_GetCurrentColumnNumber(data->parser)); \
460 #define XML_ERROR(msg,args...) do { \
461 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
462 (int) XML_GetCurrentLineNumber(data->parser), \
463 (int) XML_GetCurrentColumnNumber(data->parser), \
466 /** \brief Output a fatal error message and abort. */
467 #define XML_FATAL1(msg) do { \
468 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
470 (int) XML_GetCurrentLineNumber(data->parser), \
471 (int) XML_GetCurrentColumnNumber(data->parser)); \
474 #define XML_FATAL(msg,args...) do { \
475 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
477 (int) XML_GetCurrentLineNumber(data->parser), \
478 (int) XML_GetCurrentColumnNumber(data->parser), \
483 /** \brief Parser context for __driConfigOptions. */
487 driOptionCache
*cache
;
496 /** \brief Elements in __driConfigOptions. */
498 OI_DESCRIPTION
= 0, OI_DRIINFO
, OI_ENUM
, OI_OPTION
, OI_SECTION
, OI_COUNT
500 static const XML_Char
*OptInfoElems
[] = {
501 "description", "driinfo", "enum", "option", "section"
504 /** \brief Parse attributes of an enum element.
506 * We're not actually interested in the data. Just make sure this is ok
507 * for external configuration tools.
509 static void parseEnumAttr (struct OptInfoData
*data
, const XML_Char
**attr
) {
511 const XML_Char
*value
= NULL
, *text
= NULL
;
513 uint32_t opt
= data
->curOption
;
514 for (i
= 0; attr
[i
]; i
+= 2) {
515 if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
516 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
517 else XML_FATAL("illegal enum attribute: %s.", attr
[i
]);
519 if (!value
) XML_FATAL1 ("value attribute missing in enum.");
520 if (!text
) XML_FATAL1 ("text attribute missing in enum.");
521 if (!parseValue (&v
, data
->cache
->info
[opt
].type
, value
))
522 XML_FATAL ("illegal enum value: %s.", value
);
523 if (!checkValue (&v
, &data
->cache
->info
[opt
]))
524 XML_FATAL ("enum value out of valid range: %s.", value
);
527 /** \brief Parse attributes of a description element.
529 * We're not actually interested in the data. Just make sure this is ok
530 * for external configuration tools.
532 static void parseDescAttr (struct OptInfoData
*data
, const XML_Char
**attr
) {
534 const XML_Char
*lang
= NULL
, *text
= NULL
;
535 for (i
= 0; attr
[i
]; i
+= 2) {
536 if (!strcmp (attr
[i
], "lang")) lang
= attr
[i
+1];
537 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
538 else XML_FATAL("illegal description attribute: %s.", attr
[i
]);
540 if (!lang
) XML_FATAL1 ("lang attribute missing in description.");
541 if (!text
) XML_FATAL1 ("text attribute missing in description.");
544 /** \brief Parse attributes of an option element. */
545 static void parseOptInfoAttr (struct OptInfoData
*data
, const XML_Char
**attr
) {
546 enum OptAttr
{OA_DEFAULT
= 0, OA_NAME
, OA_TYPE
, OA_VALID
, OA_COUNT
};
547 static const XML_Char
*optAttr
[] = {"default", "name", "type", "valid"};
548 const XML_Char
*attrVal
[OA_COUNT
] = {NULL
, NULL
, NULL
, NULL
};
549 const char *defaultVal
;
550 driOptionCache
*cache
= data
->cache
;
552 for (i
= 0; attr
[i
]; i
+= 2) {
553 uint32_t attrName
= bsearchStr (attr
[i
], optAttr
, OA_COUNT
);
554 if (attrName
>= OA_COUNT
)
555 XML_FATAL ("illegal option attribute: %s", attr
[i
]);
556 attrVal
[attrName
] = attr
[i
+1];
558 if (!attrVal
[OA_NAME
]) XML_FATAL1 ("name attribute missing in option.");
559 if (!attrVal
[OA_TYPE
]) XML_FATAL1 ("type attribute missing in option.");
560 if (!attrVal
[OA_DEFAULT
]) XML_FATAL1 ("default attribute missing in option.");
562 opt
= findOption (cache
, attrVal
[OA_NAME
]);
563 if (cache
->info
[opt
].name
)
564 XML_FATAL ("option %s redefined.", attrVal
[OA_NAME
]);
565 data
->curOption
= opt
;
567 XSTRDUP (cache
->info
[opt
].name
, attrVal
[OA_NAME
]);
569 if (!strcmp (attrVal
[OA_TYPE
], "bool"))
570 cache
->info
[opt
].type
= DRI_BOOL
;
571 else if (!strcmp (attrVal
[OA_TYPE
], "enum"))
572 cache
->info
[opt
].type
= DRI_ENUM
;
573 else if (!strcmp (attrVal
[OA_TYPE
], "int"))
574 cache
->info
[opt
].type
= DRI_INT
;
575 else if (!strcmp (attrVal
[OA_TYPE
], "float"))
576 cache
->info
[opt
].type
= DRI_FLOAT
;
577 else if (!strcmp (attrVal
[OA_TYPE
], "string"))
578 cache
->info
[opt
].type
= DRI_STRING
;
580 XML_FATAL ("illegal type in option: %s.", attrVal
[OA_TYPE
]);
582 defaultVal
= getenv (cache
->info
[opt
].name
);
583 if (defaultVal
!= NULL
) {
584 /* don't use XML_WARNING, we want the user to see this! */
586 "ATTENTION: default value of option %s overridden by environment.\n",
587 cache
->info
[opt
].name
);
589 defaultVal
= attrVal
[OA_DEFAULT
];
590 if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, defaultVal
))
591 XML_FATAL ("illegal default value for %s: %s.", cache
->info
[opt
].name
, defaultVal
);
593 if (attrVal
[OA_VALID
]) {
594 if (cache
->info
[opt
].type
== DRI_BOOL
)
595 XML_FATAL1 ("boolean option with valid attribute.");
596 if (!parseRanges (&cache
->info
[opt
], attrVal
[OA_VALID
]))
597 XML_FATAL ("illegal valid attribute: %s.", attrVal
[OA_VALID
]);
598 if (!checkValue (&cache
->values
[opt
], &cache
->info
[opt
]))
599 XML_FATAL ("default value out of valid range '%s': %s.",
600 attrVal
[OA_VALID
], defaultVal
);
601 } else if (cache
->info
[opt
].type
== DRI_ENUM
) {
602 XML_FATAL1 ("valid attribute missing in option (mandatory for enums).");
604 cache
->info
[opt
].nRanges
= 0;
605 cache
->info
[opt
].ranges
= NULL
;
609 /** \brief Handler for start element events. */
610 static void optInfoStartElem (void *userData
, const XML_Char
*name
,
611 const XML_Char
**attr
) {
612 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
613 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
617 XML_FATAL1 ("nested <driinfo> elements.");
619 XML_FATAL1 ("attributes specified on <driinfo> element.");
620 data
->inDriInfo
= true;
623 if (!data
->inDriInfo
)
624 XML_FATAL1 ("<section> must be inside <driinfo>.");
626 XML_FATAL1 ("nested <section> elements.");
628 XML_FATAL1 ("attributes specified on <section> element.");
629 data
->inSection
= true;
632 if (!data
->inSection
&& !data
->inOption
)
633 XML_FATAL1 ("<description> must be inside <description> or <option.");
635 XML_FATAL1 ("nested <description> elements.");
637 parseDescAttr (data
, attr
);
640 if (!data
->inSection
)
641 XML_FATAL1 ("<option> must be inside <section>.");
643 XML_FATAL1 ("<option> nested in <description> element.");
645 XML_FATAL1 ("nested <option> elements.");
646 data
->inOption
= true;
647 parseOptInfoAttr (data
, attr
);
650 if (!(data
->inOption
&& data
->inDesc
))
651 XML_FATAL1 ("<enum> must be inside <option> and <description>.");
653 XML_FATAL1 ("nested <enum> elements.");
655 parseEnumAttr (data
, attr
);
658 XML_FATAL ("unknown element: %s.", name
);
662 /** \brief Handler for end element events. */
663 static void optInfoEndElem (void *userData
, const XML_Char
*name
) {
664 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
665 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
668 data
->inDriInfo
= false;
671 data
->inSection
= false;
674 data
->inDesc
= false;
677 data
->inOption
= false;
680 data
->inEnum
= false;
683 assert (0); /* should have been caught by StartElem */
687 void driParseOptionInfo (driOptionCache
*info
, const char *configOptions
) {
690 struct OptInfoData userData
;
691 struct OptInfoData
*data
= &userData
;
693 /* Make the hash table big enough to fit more than the maximum number of
694 * config options we've ever seen in a driver.
697 info
->info
= calloc(1 << info
->tableSize
, sizeof (driOptionInfo
));
698 info
->values
= calloc(1 << info
->tableSize
, sizeof (driOptionValue
));
699 if (info
->info
== NULL
|| info
->values
== NULL
) {
700 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
704 p
= XML_ParserCreate ("UTF-8"); /* always UTF-8 */
705 XML_SetElementHandler (p
, optInfoStartElem
, optInfoEndElem
);
706 XML_SetUserData (p
, data
);
708 userData
.name
= "__driConfigOptions";
710 userData
.cache
= info
;
711 userData
.inDriInfo
= false;
712 userData
.inSection
= false;
713 userData
.inDesc
= false;
714 userData
.inOption
= false;
715 userData
.inEnum
= false;
716 userData
.curOption
= -1;
718 status
= XML_Parse (p
, configOptions
, strlen (configOptions
), 1);
720 XML_FATAL ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
725 /** \brief Parser context for configuration files. */
729 driOptionCache
*cache
;
731 const char *driverName
, *execName
;
732 uint32_t ignoringDevice
;
733 uint32_t ignoringApp
;
740 /** \brief Elements in configuration files. */
742 OC_APPLICATION
= 0, OC_DEVICE
, OC_DRICONF
, OC_OPTION
, OC_COUNT
744 static const XML_Char
*OptConfElems
[] = {
745 "application", "device", "driconf", "option"
748 /** \brief Parse attributes of a device element. */
749 static void parseDeviceAttr (struct OptConfData
*data
, const XML_Char
**attr
) {
751 const XML_Char
*driver
= NULL
, *screen
= NULL
;
752 for (i
= 0; attr
[i
]; i
+= 2) {
753 if (!strcmp (attr
[i
], "driver")) driver
= attr
[i
+1];
754 else if (!strcmp (attr
[i
], "screen")) screen
= attr
[i
+1];
755 else XML_WARNING("unknown device attribute: %s.", attr
[i
]);
757 if (driver
&& strcmp (driver
, data
->driverName
))
758 data
->ignoringDevice
= data
->inDevice
;
760 driOptionValue screenNum
;
761 if (!parseValue (&screenNum
, DRI_INT
, screen
))
762 XML_WARNING("illegal screen number: %s.", screen
);
763 else if (screenNum
._int
!= data
->screenNum
)
764 data
->ignoringDevice
= data
->inDevice
;
768 /** \brief Parse attributes of an application element. */
769 static void parseAppAttr (struct OptConfData
*data
, const XML_Char
**attr
) {
771 const XML_Char
*exec
= NULL
;
772 for (i
= 0; attr
[i
]; i
+= 2) {
773 if (!strcmp (attr
[i
], "name")) /* not needed here */;
774 else if (!strcmp (attr
[i
], "executable")) exec
= attr
[i
+1];
775 else XML_WARNING("unknown application attribute: %s.", attr
[i
]);
777 if (exec
&& strcmp (exec
, data
->execName
))
778 data
->ignoringApp
= data
->inApp
;
781 /** \brief Parse attributes of an option element. */
782 static void parseOptConfAttr (struct OptConfData
*data
, const XML_Char
**attr
) {
784 const XML_Char
*name
= NULL
, *value
= NULL
;
785 for (i
= 0; attr
[i
]; i
+= 2) {
786 if (!strcmp (attr
[i
], "name")) name
= attr
[i
+1];
787 else if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
788 else XML_WARNING("unknown option attribute: %s.", attr
[i
]);
790 if (!name
) XML_WARNING1 ("name attribute missing in option.");
791 if (!value
) XML_WARNING1 ("value attribute missing in option.");
793 driOptionCache
*cache
= data
->cache
;
794 uint32_t opt
= findOption (cache
, name
);
795 if (cache
->info
[opt
].name
== NULL
)
796 /* don't use XML_WARNING, drirc defines options for all drivers,
797 * but not all drivers support them */
799 else if (getenv (cache
->info
[opt
].name
))
800 /* don't use XML_WARNING, we want the user to see this! */
801 fprintf (stderr
, "ATTENTION: option value of option %s ignored.\n",
802 cache
->info
[opt
].name
);
803 else if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, value
))
804 XML_WARNING ("illegal option value: %s.", value
);
808 /** \brief Handler for start element events. */
809 static void optConfStartElem (void *userData
, const XML_Char
*name
,
810 const XML_Char
**attr
) {
811 struct OptConfData
*data
= (struct OptConfData
*)userData
;
812 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
816 XML_WARNING1 ("nested <driconf> elements.");
818 XML_WARNING1 ("attributes specified on <driconf> element.");
822 if (!data
->inDriConf
)
823 XML_WARNING1 ("<device> should be inside <driconf>.");
825 XML_WARNING1 ("nested <device> elements.");
827 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
828 parseDeviceAttr (data
, attr
);
832 XML_WARNING1 ("<application> should be inside <device>.");
834 XML_WARNING1 ("nested <application> elements.");
836 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
837 parseAppAttr (data
, attr
);
841 XML_WARNING1 ("<option> should be inside <application>.");
843 XML_WARNING1 ("nested <option> elements.");
845 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
846 parseOptConfAttr (data
, attr
);
849 XML_WARNING ("unknown element: %s.", name
);
853 /** \brief Handler for end element events. */
854 static void optConfEndElem (void *userData
, const XML_Char
*name
) {
855 struct OptConfData
*data
= (struct OptConfData
*)userData
;
856 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
862 if (data
->inDevice
-- == data
->ignoringDevice
)
863 data
->ignoringDevice
= 0;
866 if (data
->inApp
-- == data
->ignoringApp
)
867 data
->ignoringApp
= 0;
873 /* unknown element, warning was produced on start tag */;
877 /** \brief Initialize an option cache based on info */
878 static void initOptionCache (driOptionCache
*cache
, const driOptionCache
*info
) {
879 GLuint i
, size
= 1 << info
->tableSize
;
880 cache
->info
= info
->info
;
881 cache
->tableSize
= info
->tableSize
;
882 cache
->values
= malloc((1<<info
->tableSize
) * sizeof (driOptionValue
));
883 if (cache
->values
== NULL
) {
884 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
887 memcpy (cache
->values
, info
->values
,
888 (1<<info
->tableSize
) * sizeof (driOptionValue
));
889 for (i
= 0; i
< size
; ++i
) {
890 if (cache
->info
[i
].type
== DRI_STRING
)
891 XSTRDUP(cache
->values
[i
]._string
, info
->values
[i
]._string
);
895 /** \brief Parse the named configuration file */
896 static void parseOneConfigFile (XML_Parser p
) {
897 #define BUF_SIZE 0x1000
898 struct OptConfData
*data
= (struct OptConfData
*)XML_GetUserData (p
);
902 if ((fd
= open (data
->name
, O_RDONLY
)) == -1) {
903 __driUtilMessage ("Can't open configuration file %s: %s.",
904 data
->name
, strerror (errno
));
910 void *buffer
= XML_GetBuffer (p
, BUF_SIZE
);
912 __driUtilMessage ("Can't allocate parser buffer.");
915 bytesRead
= read (fd
, buffer
, BUF_SIZE
);
916 if (bytesRead
== -1) {
917 __driUtilMessage ("Error reading from configuration file %s: %s.",
918 data
->name
, strerror (errno
));
921 status
= XML_ParseBuffer (p
, bytesRead
, bytesRead
== 0);
923 XML_ERROR ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
934 void driParseConfigFiles (driOptionCache
*cache
, const driOptionCache
*info
,
935 int screenNum
, const char *driverName
) {
936 char *filenames
[2] = {"/etc/drirc", NULL
};
939 struct OptConfData userData
;
941 initOptionCache (cache
, info
);
943 userData
.cache
= cache
;
944 userData
.screenNum
= screenNum
;
945 userData
.driverName
= driverName
;
946 userData
.execName
= GET_PROGRAM_NAME();
948 if ((home
= getenv ("HOME"))) {
949 uint32_t len
= strlen (home
);
950 filenames
[1] = malloc(len
+ 7+1);
951 if (filenames
[1] == NULL
)
952 __driUtilMessage ("Can't allocate memory for %s/.drirc.", home
);
954 memcpy (filenames
[1], home
, len
);
955 memcpy (filenames
[1] + len
, "/.drirc", 7+1);
959 for (i
= 0; i
< 2; ++i
) {
961 if (filenames
[i
] == NULL
)
964 p
= XML_ParserCreate (NULL
); /* use encoding specified by file */
965 XML_SetElementHandler (p
, optConfStartElem
, optConfEndElem
);
966 XML_SetUserData (p
, &userData
);
968 userData
.name
= filenames
[i
];
969 userData
.ignoringDevice
= 0;
970 userData
.ignoringApp
= 0;
971 userData
.inDriConf
= 0;
972 userData
.inDevice
= 0;
974 userData
.inOption
= 0;
976 parseOneConfigFile (p
);
983 void driDestroyOptionInfo (driOptionCache
*info
) {
984 driDestroyOptionCache (info
);
986 uint32_t i
, size
= 1 << info
->tableSize
;
987 for (i
= 0; i
< size
; ++i
) {
988 if (info
->info
[i
].name
) {
989 free(info
->info
[i
].name
);
990 free(info
->info
[i
].ranges
);
997 void driDestroyOptionCache (driOptionCache
*cache
) {
999 GLuint i
, size
= 1 << cache
->tableSize
;
1000 for (i
= 0; i
< size
; ++i
) {
1001 if (cache
->info
[i
].type
== DRI_STRING
)
1002 free(cache
->values
[i
]._string
);
1005 free(cache
->values
);
1008 unsigned char driCheckOption (const driOptionCache
*cache
, const char *name
,
1009 driOptionType type
) {
1010 uint32_t i
= findOption (cache
, name
);
1011 return cache
->info
[i
].name
!= NULL
&& cache
->info
[i
].type
== type
;
1014 unsigned char driQueryOptionb (const driOptionCache
*cache
, const char *name
) {
1015 uint32_t i
= findOption (cache
, name
);
1016 /* make sure the option is defined and has the correct type */
1017 assert (cache
->info
[i
].name
!= NULL
);
1018 assert (cache
->info
[i
].type
== DRI_BOOL
);
1019 return cache
->values
[i
]._bool
;
1022 int driQueryOptioni (const driOptionCache
*cache
, const char *name
) {
1023 uint32_t i
= findOption (cache
, name
);
1024 /* make sure the option is defined and has the correct type */
1025 assert (cache
->info
[i
].name
!= NULL
);
1026 assert (cache
->info
[i
].type
== DRI_INT
|| cache
->info
[i
].type
== DRI_ENUM
);
1027 return cache
->values
[i
]._int
;
1030 float driQueryOptionf (const driOptionCache
*cache
, const char *name
) {
1031 uint32_t i
= findOption (cache
, name
);
1032 /* make sure the option is defined and has the correct type */
1033 assert (cache
->info
[i
].name
!= NULL
);
1034 assert (cache
->info
[i
].type
== DRI_FLOAT
);
1035 return cache
->values
[i
]._float
;
1038 char *driQueryOptionstr (const driOptionCache
*cache
, const char *name
) {
1039 GLuint i
= findOption (cache
, name
);
1040 /* make sure the option is defined and has the correct type */
1041 assert (cache
->info
[i
].name
!= NULL
);
1042 assert (cache
->info
[i
].type
== DRI_STRING
);
1043 return cache
->values
[i
]._string
;