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
39 #include "main/imports.h"
41 #include "xmlconfig.h"
43 #undef GET_PROGRAM_NAME
45 #if (defined(__GNU_LIBRARY__) || defined(__GLIBC__)) && !defined(__UCLIBC__)
46 # if !defined(__GLIBC__) || (__GLIBC__ < 2)
47 /* These aren't declared in any libc5 header */
48 extern char *program_invocation_name
, *program_invocation_short_name
;
50 # define GET_PROGRAM_NAME() program_invocation_short_name
51 #elif defined(__CYGWIN__)
52 # define GET_PROGRAM_NAME() program_invocation_short_name
53 #elif defined(__FreeBSD__) && (__FreeBSD__ >= 2)
54 # include <osreldate.h>
55 # if (__FreeBSD_version >= 440000)
57 # define GET_PROGRAM_NAME() getprogname()
59 #elif defined(__NetBSD__) && defined(__NetBSD_Version) && (__NetBSD_Version >= 106000100)
61 # define GET_PROGRAM_NAME() getprogname()
62 #elif defined(__APPLE__)
64 # define GET_PROGRAM_NAME() getprogname()
66 /* Solaris has getexecname() which returns the full path - return just
67 the basename to match BSD getprogname() */
71 static const char *__getProgramName () {
72 static const char *progname
;
74 if (progname
== NULL
) {
75 const char *e
= getexecname();
77 /* Have to make a copy since getexecname can return a readonly
78 string, but basename expects to be able to modify its arg. */
81 progname
= basename(n
);
88 # define GET_PROGRAM_NAME() __getProgramName()
91 #if !defined(GET_PROGRAM_NAME)
92 # if defined(__OpenBSD__) || defined(NetBSD) || defined(__UCLIBC__) || defined(ANDROID)
93 /* This is a hack. It's said to work on OpenBSD, NetBSD and GNU.
94 * Rogelio M.Serrano Jr. reported it's also working with UCLIBC. It's
95 * used as a last resort, if there is no documented facility available. */
96 static const char *__getProgramName () {
97 extern const char *__progname
;
98 char * arg
= strrchr(__progname
, '/');
104 # define GET_PROGRAM_NAME() __getProgramName()
106 # define GET_PROGRAM_NAME() ""
107 # warning "Per application configuration won't work with your OS version."
111 /** \brief Find an option in an option cache with the name as key */
112 static uint32_t findOption (const driOptionCache
*cache
, const char *name
) {
113 uint32_t len
= strlen (name
);
114 uint32_t size
= 1 << cache
->tableSize
, mask
= size
- 1;
118 /* compute a hash from the variable length name */
119 for (i
= 0, shift
= 0; i
< len
; ++i
, shift
= (shift
+8) & 31)
120 hash
+= (uint32_t)name
[i
] << shift
;
122 hash
= (hash
>> (16-cache
->tableSize
/2)) & mask
;
124 /* this is just the starting point of the linear search for the option */
125 for (i
= 0; i
< size
; ++i
, hash
= (hash
+1) & mask
) {
126 /* if we hit an empty entry then the option is not defined (yet) */
127 if (cache
->info
[hash
].name
== 0)
129 else if (!strcmp (name
, cache
->info
[hash
].name
))
132 /* this assertion fails if the hash table is full */
138 /** \brief Like strdup but using malloc and with error checking. */
139 #define XSTRDUP(dest,source) do { \
140 uint32_t len = strlen (source); \
141 if (!(dest = malloc(len+1))) { \
142 fprintf (stderr, "%s: %d: out of memory.\n", __FILE__, __LINE__); \
145 memcpy (dest, source, len+1); \
148 static int compare (const void *a
, const void *b
) {
149 return strcmp (*(char *const*)a
, *(char *const*)b
);
151 /** \brief Binary search in a string array. */
152 static uint32_t bsearchStr (const XML_Char
*name
,
153 const XML_Char
*elems
[], uint32_t count
) {
154 const XML_Char
**found
;
155 found
= bsearch (&name
, elems
, count
, sizeof (XML_Char
*), compare
);
157 return found
- elems
;
162 /** \brief Locale-independent integer parser.
164 * Works similar to strtol. Leading space is NOT skipped. The input
165 * number may have an optional sign. Radix is specified by base. If
166 * base is 0 then decimal is assumed unless the input number is
167 * prefixed by 0x or 0X for hexadecimal or 0 for octal. After
168 * returning tail points to the first character that is not part of
169 * the integer number. If no number was found then tail points to the
170 * start of the input string. */
171 static int strToI (const XML_Char
*string
, const XML_Char
**tail
, int base
) {
172 int radix
= base
== 0 ? 10 : base
;
175 bool numberFound
= false;
176 const XML_Char
*start
= string
;
178 assert (radix
>= 2 && radix
<= 36);
180 if (*string
== '-') {
183 } else if (*string
== '+')
185 if (base
== 0 && *string
== '0') {
187 if (*(string
+1) == 'x' || *(string
+1) == 'X') {
198 if (*string
>= '0' && *string
< '0' + radix
)
199 digit
= *string
- '0';
201 if (*string
>= '0' && *string
<= '9')
202 digit
= *string
- '0';
203 else if (*string
>= 'a' && *string
< 'a' + radix
- 10)
204 digit
= *string
- 'a' + 10;
205 else if (*string
>= 'A' && *string
< 'A' + radix
- 10)
206 digit
= *string
- 'A' + 10;
210 result
= radix
*result
+ digit
;
215 *tail
= numberFound
? string
: start
;
216 return sign
* result
;
219 /** \brief Locale-independent floating-point parser.
221 * Works similar to strtod. Leading space is NOT skipped. The input
222 * number may have an optional sign. '.' is interpreted as decimal
223 * point and may occur at most once. Optionally the number may end in
224 * [eE]<exponent>, where <exponent> is an integer as recognized by
225 * strToI. In that case the result is number * 10^exponent. After
226 * returning tail points to the first character that is not part of
227 * the floating point number. If no number was found then tail points
228 * to the start of the input string.
230 * Uses two passes for maximum accuracy. */
231 static float strToF (const XML_Char
*string
, const XML_Char
**tail
) {
232 int nDigits
= 0, pointPos
, exponent
;
233 float sign
= 1.0f
, result
= 0.0f
, scale
;
234 const XML_Char
*start
= string
, *numStart
;
237 if (*string
== '-') {
240 } else if (*string
== '+')
243 /* first pass: determine position of decimal point, number of
244 * digits, exponent and the end of the number. */
246 while (*string
>= '0' && *string
<= '9') {
251 if (*string
== '.') {
253 while (*string
>= '0' && *string
<= '9') {
259 /* no digits, no number */
264 if (*string
== 'e' || *string
== 'E') {
265 const XML_Char
*expTail
;
266 exponent
= strToI (string
+1, &expTail
, 10);
267 if (expTail
== string
+1)
275 /* scale of the first digit */
276 scale
= sign
* (float)pow (10.0, (double)(pointPos
-1 + exponent
));
278 /* second pass: parse digits */
280 if (*string
!= '.') {
281 assert (*string
>= '0' && *string
<= '9');
282 result
+= scale
* (float)(*string
- '0');
287 } while (nDigits
> 0);
292 /** \brief Parse a value of a given type. */
293 static unsigned char parseValue (driOptionValue
*v
, driOptionType type
,
294 const XML_Char
*string
) {
295 const XML_Char
*tail
= NULL
;
296 /* skip leading white-space */
297 string
+= strspn (string
, " \f\n\r\t\v");
300 if (!strcmp (string
, "false")) {
303 } else if (!strcmp (string
, "true")) {
310 case DRI_ENUM
: /* enum is just a special integer */
312 v
->_int
= strToI (string
, &tail
, 0);
315 v
->_float
= strToF (string
, &tail
);
319 v
->_string
= strndup(string
, STRING_CONF_MAXLEN
);
324 return false; /* empty string (or containing only white-space) */
325 /* skip trailing white space */
327 tail
+= strspn (tail
, " \f\n\r\t\v");
329 return false; /* something left over that is not part of value */
334 /** \brief Parse a list of ranges of type info->type. */
335 static unsigned char parseRanges (driOptionInfo
*info
, const XML_Char
*string
) {
336 XML_Char
*cp
, *range
;
338 driOptionRange
*ranges
;
340 XSTRDUP (cp
, string
);
341 /* pass 1: determine the number of ranges (number of commas + 1) */
343 for (nRanges
= 1; *range
; ++range
)
347 if ((ranges
= malloc(nRanges
*sizeof(driOptionRange
))) == NULL
) {
348 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
352 /* pass 2: parse all ranges into preallocated array */
354 for (i
= 0; i
< nRanges
; ++i
) {
357 end
= strchr (range
, ',');
360 sep
= strchr (range
, ':');
361 if (sep
) { /* non-empty interval */
363 if (!parseValue (&ranges
[i
].start
, info
->type
, range
) ||
364 !parseValue (&ranges
[i
].end
, info
->type
, sep
+1))
366 if (info
->type
== DRI_INT
&&
367 ranges
[i
].start
._int
> ranges
[i
].end
._int
)
369 if (info
->type
== DRI_FLOAT
&&
370 ranges
[i
].start
._float
> ranges
[i
].end
._float
)
372 } else { /* empty interval */
373 if (!parseValue (&ranges
[i
].start
, info
->type
, range
))
375 ranges
[i
].end
= ranges
[i
].start
;
387 assert (range
== NULL
);
389 info
->nRanges
= nRanges
;
390 info
->ranges
= ranges
;
394 /** \brief Check if a value is in one of info->ranges. */
395 static bool checkValue (const driOptionValue
*v
, const driOptionInfo
*info
) {
397 assert (info
->type
!= DRI_BOOL
); /* should be caught by the parser */
398 if (info
->nRanges
== 0)
400 switch (info
->type
) {
401 case DRI_ENUM
: /* enum is just a special integer */
403 for (i
= 0; i
< info
->nRanges
; ++i
)
404 if (v
->_int
>= info
->ranges
[i
].start
._int
&&
405 v
->_int
<= info
->ranges
[i
].end
._int
)
409 for (i
= 0; i
< info
->nRanges
; ++i
)
410 if (v
->_float
>= info
->ranges
[i
].start
._float
&&
411 v
->_float
<= info
->ranges
[i
].end
._float
)
417 assert (0); /* should never happen */
423 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
426 * Is called from the drivers.
428 * \param f \c printf like format string.
431 __driUtilMessage(const char *f
, ...)
434 const char *libgl_debug
;
436 libgl_debug
=getenv("LIBGL_DEBUG");
437 if (libgl_debug
&& !strstr(libgl_debug
, "quiet")) {
438 fprintf(stderr
, "libGL: ");
440 vfprintf(stderr
, f
, args
);
442 fprintf(stderr
, "\n");
446 /** \brief Output a warning message. */
447 #define XML_WARNING1(msg) do {\
448 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
449 (int) XML_GetCurrentLineNumber(data->parser), \
450 (int) XML_GetCurrentColumnNumber(data->parser)); \
452 #define XML_WARNING(msg,args...) do { \
453 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
454 (int) XML_GetCurrentLineNumber(data->parser), \
455 (int) XML_GetCurrentColumnNumber(data->parser), \
458 /** \brief Output an error message. */
459 #define XML_ERROR1(msg) do { \
460 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
461 (int) XML_GetCurrentLineNumber(data->parser), \
462 (int) XML_GetCurrentColumnNumber(data->parser)); \
464 #define XML_ERROR(msg,args...) do { \
465 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
466 (int) XML_GetCurrentLineNumber(data->parser), \
467 (int) XML_GetCurrentColumnNumber(data->parser), \
470 /** \brief Output a fatal error message and abort. */
471 #define XML_FATAL1(msg) do { \
472 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
474 (int) XML_GetCurrentLineNumber(data->parser), \
475 (int) XML_GetCurrentColumnNumber(data->parser)); \
478 #define XML_FATAL(msg,args...) do { \
479 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
481 (int) XML_GetCurrentLineNumber(data->parser), \
482 (int) XML_GetCurrentColumnNumber(data->parser), \
487 /** \brief Parser context for __driConfigOptions. */
491 driOptionCache
*cache
;
500 /** \brief Elements in __driConfigOptions. */
502 OI_DESCRIPTION
= 0, OI_DRIINFO
, OI_ENUM
, OI_OPTION
, OI_SECTION
, OI_COUNT
504 static const XML_Char
*OptInfoElems
[] = {
505 "description", "driinfo", "enum", "option", "section"
508 /** \brief Parse attributes of an enum element.
510 * We're not actually interested in the data. Just make sure this is ok
511 * for external configuration tools.
513 static void parseEnumAttr (struct OptInfoData
*data
, const XML_Char
**attr
) {
515 const XML_Char
*value
= NULL
, *text
= NULL
;
517 uint32_t opt
= data
->curOption
;
518 for (i
= 0; attr
[i
]; i
+= 2) {
519 if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
520 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
521 else XML_FATAL("illegal enum attribute: %s.", attr
[i
]);
523 if (!value
) XML_FATAL1 ("value attribute missing in enum.");
524 if (!text
) XML_FATAL1 ("text attribute missing in enum.");
525 if (!parseValue (&v
, data
->cache
->info
[opt
].type
, value
))
526 XML_FATAL ("illegal enum value: %s.", value
);
527 if (!checkValue (&v
, &data
->cache
->info
[opt
]))
528 XML_FATAL ("enum value out of valid range: %s.", value
);
531 /** \brief Parse attributes of a description element.
533 * We're not actually interested in the data. Just make sure this is ok
534 * for external configuration tools.
536 static void parseDescAttr (struct OptInfoData
*data
, const XML_Char
**attr
) {
538 const XML_Char
*lang
= NULL
, *text
= NULL
;
539 for (i
= 0; attr
[i
]; i
+= 2) {
540 if (!strcmp (attr
[i
], "lang")) lang
= attr
[i
+1];
541 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
542 else XML_FATAL("illegal description attribute: %s.", attr
[i
]);
544 if (!lang
) XML_FATAL1 ("lang attribute missing in description.");
545 if (!text
) XML_FATAL1 ("text attribute missing in description.");
548 /** \brief Parse attributes of an option element. */
549 static void parseOptInfoAttr (struct OptInfoData
*data
, const XML_Char
**attr
) {
550 enum OptAttr
{OA_DEFAULT
= 0, OA_NAME
, OA_TYPE
, OA_VALID
, OA_COUNT
};
551 static const XML_Char
*optAttr
[] = {"default", "name", "type", "valid"};
552 const XML_Char
*attrVal
[OA_COUNT
] = {NULL
, NULL
, NULL
, NULL
};
553 const char *defaultVal
;
554 driOptionCache
*cache
= data
->cache
;
556 for (i
= 0; attr
[i
]; i
+= 2) {
557 uint32_t attrName
= bsearchStr (attr
[i
], optAttr
, OA_COUNT
);
558 if (attrName
>= OA_COUNT
)
559 XML_FATAL ("illegal option attribute: %s", attr
[i
]);
560 attrVal
[attrName
] = attr
[i
+1];
562 if (!attrVal
[OA_NAME
]) XML_FATAL1 ("name attribute missing in option.");
563 if (!attrVal
[OA_TYPE
]) XML_FATAL1 ("type attribute missing in option.");
564 if (!attrVal
[OA_DEFAULT
]) XML_FATAL1 ("default attribute missing in option.");
566 opt
= findOption (cache
, attrVal
[OA_NAME
]);
567 if (cache
->info
[opt
].name
)
568 XML_FATAL ("option %s redefined.", attrVal
[OA_NAME
]);
569 data
->curOption
= opt
;
571 XSTRDUP (cache
->info
[opt
].name
, attrVal
[OA_NAME
]);
573 if (!strcmp (attrVal
[OA_TYPE
], "bool"))
574 cache
->info
[opt
].type
= DRI_BOOL
;
575 else if (!strcmp (attrVal
[OA_TYPE
], "enum"))
576 cache
->info
[opt
].type
= DRI_ENUM
;
577 else if (!strcmp (attrVal
[OA_TYPE
], "int"))
578 cache
->info
[opt
].type
= DRI_INT
;
579 else if (!strcmp (attrVal
[OA_TYPE
], "float"))
580 cache
->info
[opt
].type
= DRI_FLOAT
;
581 else if (!strcmp (attrVal
[OA_TYPE
], "string"))
582 cache
->info
[opt
].type
= DRI_STRING
;
584 XML_FATAL ("illegal type in option: %s.", attrVal
[OA_TYPE
]);
586 defaultVal
= getenv (cache
->info
[opt
].name
);
587 if (defaultVal
!= NULL
) {
588 /* don't use XML_WARNING, we want the user to see this! */
590 "ATTENTION: default value of option %s overridden by environment.\n",
591 cache
->info
[opt
].name
);
593 defaultVal
= attrVal
[OA_DEFAULT
];
594 if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, defaultVal
))
595 XML_FATAL ("illegal default value for %s: %s.", cache
->info
[opt
].name
, defaultVal
);
597 if (attrVal
[OA_VALID
]) {
598 if (cache
->info
[opt
].type
== DRI_BOOL
)
599 XML_FATAL1 ("boolean option with valid attribute.");
600 if (!parseRanges (&cache
->info
[opt
], attrVal
[OA_VALID
]))
601 XML_FATAL ("illegal valid attribute: %s.", attrVal
[OA_VALID
]);
602 if (!checkValue (&cache
->values
[opt
], &cache
->info
[opt
]))
603 XML_FATAL ("default value out of valid range '%s': %s.",
604 attrVal
[OA_VALID
], defaultVal
);
605 } else if (cache
->info
[opt
].type
== DRI_ENUM
) {
606 XML_FATAL1 ("valid attribute missing in option (mandatory for enums).");
608 cache
->info
[opt
].nRanges
= 0;
609 cache
->info
[opt
].ranges
= NULL
;
613 /** \brief Handler for start element events. */
614 static void optInfoStartElem (void *userData
, const XML_Char
*name
,
615 const XML_Char
**attr
) {
616 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
617 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
621 XML_FATAL1 ("nested <driinfo> elements.");
623 XML_FATAL1 ("attributes specified on <driinfo> element.");
624 data
->inDriInfo
= true;
627 if (!data
->inDriInfo
)
628 XML_FATAL1 ("<section> must be inside <driinfo>.");
630 XML_FATAL1 ("nested <section> elements.");
632 XML_FATAL1 ("attributes specified on <section> element.");
633 data
->inSection
= true;
636 if (!data
->inSection
&& !data
->inOption
)
637 XML_FATAL1 ("<description> must be inside <description> or <option.");
639 XML_FATAL1 ("nested <description> elements.");
641 parseDescAttr (data
, attr
);
644 if (!data
->inSection
)
645 XML_FATAL1 ("<option> must be inside <section>.");
647 XML_FATAL1 ("<option> nested in <description> element.");
649 XML_FATAL1 ("nested <option> elements.");
650 data
->inOption
= true;
651 parseOptInfoAttr (data
, attr
);
654 if (!(data
->inOption
&& data
->inDesc
))
655 XML_FATAL1 ("<enum> must be inside <option> and <description>.");
657 XML_FATAL1 ("nested <enum> elements.");
659 parseEnumAttr (data
, attr
);
662 XML_FATAL ("unknown element: %s.", name
);
666 /** \brief Handler for end element events. */
667 static void optInfoEndElem (void *userData
, const XML_Char
*name
) {
668 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
669 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
672 data
->inDriInfo
= false;
675 data
->inSection
= false;
678 data
->inDesc
= false;
681 data
->inOption
= false;
684 data
->inEnum
= false;
687 assert (0); /* should have been caught by StartElem */
691 void driParseOptionInfo (driOptionCache
*info
, const char *configOptions
) {
694 struct OptInfoData userData
;
695 struct OptInfoData
*data
= &userData
;
697 /* Make the hash table big enough to fit more than the maximum number of
698 * config options we've ever seen in a driver.
701 info
->info
= calloc(1 << info
->tableSize
, sizeof (driOptionInfo
));
702 info
->values
= calloc(1 << info
->tableSize
, sizeof (driOptionValue
));
703 if (info
->info
== NULL
|| info
->values
== NULL
) {
704 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
708 p
= XML_ParserCreate ("UTF-8"); /* always UTF-8 */
709 XML_SetElementHandler (p
, optInfoStartElem
, optInfoEndElem
);
710 XML_SetUserData (p
, data
);
712 userData
.name
= "__driConfigOptions";
714 userData
.cache
= info
;
715 userData
.inDriInfo
= false;
716 userData
.inSection
= false;
717 userData
.inDesc
= false;
718 userData
.inOption
= false;
719 userData
.inEnum
= false;
720 userData
.curOption
= -1;
722 status
= XML_Parse (p
, configOptions
, strlen (configOptions
), 1);
724 XML_FATAL ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
729 /** \brief Parser context for configuration files. */
733 driOptionCache
*cache
;
735 const char *driverName
, *execName
;
736 uint32_t ignoringDevice
;
737 uint32_t ignoringApp
;
744 /** \brief Elements in configuration files. */
746 OC_APPLICATION
= 0, OC_DEVICE
, OC_DRICONF
, OC_OPTION
, OC_COUNT
748 static const XML_Char
*OptConfElems
[] = {
749 "application", "device", "driconf", "option"
752 /** \brief Parse attributes of a device element. */
753 static void parseDeviceAttr (struct OptConfData
*data
, const XML_Char
**attr
) {
755 const XML_Char
*driver
= NULL
, *screen
= NULL
;
756 for (i
= 0; attr
[i
]; i
+= 2) {
757 if (!strcmp (attr
[i
], "driver")) driver
= attr
[i
+1];
758 else if (!strcmp (attr
[i
], "screen")) screen
= attr
[i
+1];
759 else XML_WARNING("unknown device attribute: %s.", attr
[i
]);
761 if (driver
&& strcmp (driver
, data
->driverName
))
762 data
->ignoringDevice
= data
->inDevice
;
764 driOptionValue screenNum
;
765 if (!parseValue (&screenNum
, DRI_INT
, screen
))
766 XML_WARNING("illegal screen number: %s.", screen
);
767 else if (screenNum
._int
!= data
->screenNum
)
768 data
->ignoringDevice
= data
->inDevice
;
772 /** \brief Parse attributes of an application element. */
773 static void parseAppAttr (struct OptConfData
*data
, const XML_Char
**attr
) {
775 const XML_Char
*exec
= NULL
;
776 for (i
= 0; attr
[i
]; i
+= 2) {
777 if (!strcmp (attr
[i
], "name")) /* not needed here */;
778 else if (!strcmp (attr
[i
], "executable")) exec
= attr
[i
+1];
779 else XML_WARNING("unknown application attribute: %s.", attr
[i
]);
781 if (exec
&& strcmp (exec
, data
->execName
))
782 data
->ignoringApp
= data
->inApp
;
785 /** \brief Parse attributes of an option element. */
786 static void parseOptConfAttr (struct OptConfData
*data
, const XML_Char
**attr
) {
788 const XML_Char
*name
= NULL
, *value
= NULL
;
789 for (i
= 0; attr
[i
]; i
+= 2) {
790 if (!strcmp (attr
[i
], "name")) name
= attr
[i
+1];
791 else if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
792 else XML_WARNING("unknown option attribute: %s.", attr
[i
]);
794 if (!name
) XML_WARNING1 ("name attribute missing in option.");
795 if (!value
) XML_WARNING1 ("value attribute missing in option.");
797 driOptionCache
*cache
= data
->cache
;
798 uint32_t opt
= findOption (cache
, name
);
799 if (cache
->info
[opt
].name
== NULL
)
800 /* don't use XML_WARNING, drirc defines options for all drivers,
801 * but not all drivers support them */
803 else if (getenv (cache
->info
[opt
].name
))
804 /* don't use XML_WARNING, we want the user to see this! */
805 fprintf (stderr
, "ATTENTION: option value of option %s ignored.\n",
806 cache
->info
[opt
].name
);
807 else if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, value
))
808 XML_WARNING ("illegal option value: %s.", value
);
812 /** \brief Handler for start element events. */
813 static void optConfStartElem (void *userData
, const XML_Char
*name
,
814 const XML_Char
**attr
) {
815 struct OptConfData
*data
= (struct OptConfData
*)userData
;
816 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
820 XML_WARNING1 ("nested <driconf> elements.");
822 XML_WARNING1 ("attributes specified on <driconf> element.");
826 if (!data
->inDriConf
)
827 XML_WARNING1 ("<device> should be inside <driconf>.");
829 XML_WARNING1 ("nested <device> elements.");
831 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
832 parseDeviceAttr (data
, attr
);
836 XML_WARNING1 ("<application> should be inside <device>.");
838 XML_WARNING1 ("nested <application> elements.");
840 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
841 parseAppAttr (data
, attr
);
845 XML_WARNING1 ("<option> should be inside <application>.");
847 XML_WARNING1 ("nested <option> elements.");
849 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
850 parseOptConfAttr (data
, attr
);
853 XML_WARNING ("unknown element: %s.", name
);
857 /** \brief Handler for end element events. */
858 static void optConfEndElem (void *userData
, const XML_Char
*name
) {
859 struct OptConfData
*data
= (struct OptConfData
*)userData
;
860 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
866 if (data
->inDevice
-- == data
->ignoringDevice
)
867 data
->ignoringDevice
= 0;
870 if (data
->inApp
-- == data
->ignoringApp
)
871 data
->ignoringApp
= 0;
877 /* unknown element, warning was produced on start tag */;
881 /** \brief Initialize an option cache based on info */
882 static void initOptionCache (driOptionCache
*cache
, const driOptionCache
*info
) {
883 GLuint i
, size
= 1 << info
->tableSize
;
884 cache
->info
= info
->info
;
885 cache
->tableSize
= info
->tableSize
;
886 cache
->values
= malloc((1<<info
->tableSize
) * sizeof (driOptionValue
));
887 if (cache
->values
== NULL
) {
888 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
891 memcpy (cache
->values
, info
->values
,
892 (1<<info
->tableSize
) * sizeof (driOptionValue
));
893 for (i
= 0; i
< size
; ++i
) {
894 if (cache
->info
[i
].type
== DRI_STRING
)
895 XSTRDUP(cache
->values
[i
]._string
, info
->values
[i
]._string
);
899 /** \brief Parse the named configuration file */
900 static void parseOneConfigFile (XML_Parser p
) {
901 #define BUF_SIZE 0x1000
902 struct OptConfData
*data
= (struct OptConfData
*)XML_GetUserData (p
);
906 if ((fd
= open (data
->name
, O_RDONLY
)) == -1) {
907 __driUtilMessage ("Can't open configuration file %s: %s.",
908 data
->name
, strerror (errno
));
914 void *buffer
= XML_GetBuffer (p
, BUF_SIZE
);
916 __driUtilMessage ("Can't allocate parser buffer.");
919 bytesRead
= read (fd
, buffer
, BUF_SIZE
);
920 if (bytesRead
== -1) {
921 __driUtilMessage ("Error reading from configuration file %s: %s.",
922 data
->name
, strerror (errno
));
925 status
= XML_ParseBuffer (p
, bytesRead
, bytesRead
== 0);
927 XML_ERROR ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
938 void driParseConfigFiles (driOptionCache
*cache
, const driOptionCache
*info
,
939 int screenNum
, const char *driverName
) {
940 char *filenames
[2] = {"/etc/drirc", NULL
};
943 struct OptConfData userData
;
945 initOptionCache (cache
, info
);
947 userData
.cache
= cache
;
948 userData
.screenNum
= screenNum
;
949 userData
.driverName
= driverName
;
950 userData
.execName
= GET_PROGRAM_NAME();
952 if ((home
= getenv ("HOME"))) {
953 uint32_t len
= strlen (home
);
954 filenames
[1] = malloc(len
+ 7+1);
955 if (filenames
[1] == NULL
)
956 __driUtilMessage ("Can't allocate memory for %s/.drirc.", home
);
958 memcpy (filenames
[1], home
, len
);
959 memcpy (filenames
[1] + len
, "/.drirc", 7+1);
963 for (i
= 0; i
< 2; ++i
) {
965 if (filenames
[i
] == NULL
)
968 p
= XML_ParserCreate (NULL
); /* use encoding specified by file */
969 XML_SetElementHandler (p
, optConfStartElem
, optConfEndElem
);
970 XML_SetUserData (p
, &userData
);
972 userData
.name
= filenames
[i
];
973 userData
.ignoringDevice
= 0;
974 userData
.ignoringApp
= 0;
975 userData
.inDriConf
= 0;
976 userData
.inDevice
= 0;
978 userData
.inOption
= 0;
980 parseOneConfigFile (p
);
987 void driDestroyOptionInfo (driOptionCache
*info
) {
988 driDestroyOptionCache (info
);
990 uint32_t i
, size
= 1 << info
->tableSize
;
991 for (i
= 0; i
< size
; ++i
) {
992 if (info
->info
[i
].name
) {
993 free(info
->info
[i
].name
);
994 free(info
->info
[i
].ranges
);
1001 void driDestroyOptionCache (driOptionCache
*cache
) {
1003 GLuint i
, size
= 1 << cache
->tableSize
;
1004 for (i
= 0; i
< size
; ++i
) {
1005 if (cache
->info
[i
].type
== DRI_STRING
)
1006 free(cache
->values
[i
]._string
);
1009 free(cache
->values
);
1012 unsigned char driCheckOption (const driOptionCache
*cache
, const char *name
,
1013 driOptionType type
) {
1014 uint32_t i
= findOption (cache
, name
);
1015 return cache
->info
[i
].name
!= NULL
&& cache
->info
[i
].type
== type
;
1018 unsigned char driQueryOptionb (const driOptionCache
*cache
, const char *name
) {
1019 uint32_t i
= findOption (cache
, name
);
1020 /* make sure the option is defined and has the correct type */
1021 assert (cache
->info
[i
].name
!= NULL
);
1022 assert (cache
->info
[i
].type
== DRI_BOOL
);
1023 return cache
->values
[i
]._bool
;
1026 int driQueryOptioni (const driOptionCache
*cache
, const char *name
) {
1027 uint32_t i
= findOption (cache
, name
);
1028 /* make sure the option is defined and has the correct type */
1029 assert (cache
->info
[i
].name
!= NULL
);
1030 assert (cache
->info
[i
].type
== DRI_INT
|| cache
->info
[i
].type
== DRI_ENUM
);
1031 return cache
->values
[i
]._int
;
1034 float driQueryOptionf (const driOptionCache
*cache
, const char *name
) {
1035 uint32_t i
= findOption (cache
, name
);
1036 /* make sure the option is defined and has the correct type */
1037 assert (cache
->info
[i
].name
!= NULL
);
1038 assert (cache
->info
[i
].type
== DRI_FLOAT
);
1039 return cache
->values
[i
]._float
;
1042 char *driQueryOptionstr (const driOptionCache
*cache
, const char *name
) {
1043 GLuint i
= findOption (cache
, name
);
1044 /* make sure the option is defined and has the correct type */
1045 assert (cache
->info
[i
].name
!= NULL
);
1046 assert (cache
->info
[i
].type
== DRI_STRING
);
1047 return cache
->values
[i
]._string
;