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(__DragonFly__)
64 # define GET_PROGRAM_NAME() getprogname()
65 #elif defined(__APPLE__)
67 # define GET_PROGRAM_NAME() getprogname()
69 /* Solaris has getexecname() which returns the full path - return just
70 the basename to match BSD getprogname() */
77 static const char *progname
;
79 if (progname
== NULL
) {
80 const char *e
= getexecname();
82 /* Have to make a copy since getexecname can return a readonly
83 string, but basename expects to be able to modify its arg. */
86 progname
= basename(n
);
93 # define GET_PROGRAM_NAME() __getProgramName()
96 #if !defined(GET_PROGRAM_NAME)
97 # if defined(__OpenBSD__) || defined(NetBSD) || defined(__UCLIBC__) || defined(ANDROID)
98 /* This is a hack. It's said to work on OpenBSD, NetBSD and GNU.
99 * Rogelio M.Serrano Jr. reported it's also working with UCLIBC. It's
100 * used as a last resort, if there is no documented facility available. */
104 extern const char *__progname
;
105 char * arg
= strrchr(__progname
, '/');
111 # define GET_PROGRAM_NAME() __getProgramName()
113 # define GET_PROGRAM_NAME() ""
114 # warning "Per application configuration won't work with your OS version."
118 /** \brief Find an option in an option cache with the name as key */
120 findOption(const driOptionCache
*cache
, const char *name
)
122 uint32_t len
= strlen (name
);
123 uint32_t size
= 1 << cache
->tableSize
, mask
= size
- 1;
127 /* compute a hash from the variable length name */
128 for (i
= 0, shift
= 0; i
< len
; ++i
, shift
= (shift
+8) & 31)
129 hash
+= (uint32_t)name
[i
] << shift
;
131 hash
= (hash
>> (16-cache
->tableSize
/2)) & mask
;
133 /* this is just the starting point of the linear search for the option */
134 for (i
= 0; i
< size
; ++i
, hash
= (hash
+1) & mask
) {
135 /* if we hit an empty entry then the option is not defined (yet) */
136 if (cache
->info
[hash
].name
== 0)
138 else if (!strcmp (name
, cache
->info
[hash
].name
))
141 /* this assertion fails if the hash table is full */
147 /** \brief Like strdup but using malloc and with error checking. */
148 #define XSTRDUP(dest,source) do { \
149 uint32_t len = strlen (source); \
150 if (!(dest = malloc(len+1))) { \
151 fprintf (stderr, "%s: %d: out of memory.\n", __FILE__, __LINE__); \
154 memcpy (dest, source, len+1); \
157 static int compare (const void *a
, const void *b
) {
158 return strcmp (*(char *const*)a
, *(char *const*)b
);
160 /** \brief Binary search in a string array. */
162 bsearchStr (const XML_Char
*name
, const XML_Char
*elems
[], uint32_t count
)
164 const XML_Char
**found
;
165 found
= bsearch (&name
, elems
, count
, sizeof (XML_Char
*), compare
);
167 return found
- elems
;
172 /** \brief Locale-independent integer parser.
174 * Works similar to strtol. Leading space is NOT skipped. The input
175 * number may have an optional sign. Radix is specified by base. If
176 * base is 0 then decimal is assumed unless the input number is
177 * prefixed by 0x or 0X for hexadecimal or 0 for octal. After
178 * returning tail points to the first character that is not part of
179 * the integer number. If no number was found then tail points to the
180 * start of the input string. */
182 strToI(const XML_Char
*string
, const XML_Char
**tail
, int base
)
184 int radix
= base
== 0 ? 10 : base
;
187 bool numberFound
= false;
188 const XML_Char
*start
= string
;
190 assert (radix
>= 2 && radix
<= 36);
192 if (*string
== '-') {
195 } else if (*string
== '+')
197 if (base
== 0 && *string
== '0') {
199 if (*(string
+1) == 'x' || *(string
+1) == 'X') {
210 if (*string
>= '0' && *string
< '0' + radix
)
211 digit
= *string
- '0';
213 if (*string
>= '0' && *string
<= '9')
214 digit
= *string
- '0';
215 else if (*string
>= 'a' && *string
< 'a' + radix
- 10)
216 digit
= *string
- 'a' + 10;
217 else if (*string
>= 'A' && *string
< 'A' + radix
- 10)
218 digit
= *string
- 'A' + 10;
222 result
= radix
*result
+ digit
;
227 *tail
= numberFound
? string
: start
;
228 return sign
* result
;
231 /** \brief Locale-independent floating-point parser.
233 * Works similar to strtod. Leading space is NOT skipped. The input
234 * number may have an optional sign. '.' is interpreted as decimal
235 * point and may occur at most once. Optionally the number may end in
236 * [eE]<exponent>, where <exponent> is an integer as recognized by
237 * strToI. In that case the result is number * 10^exponent. After
238 * returning tail points to the first character that is not part of
239 * the floating point number. If no number was found then tail points
240 * to the start of the input string.
242 * Uses two passes for maximum accuracy. */
244 strToF(const XML_Char
*string
, const XML_Char
**tail
)
246 int nDigits
= 0, pointPos
, exponent
;
247 float sign
= 1.0f
, result
= 0.0f
, scale
;
248 const XML_Char
*start
= string
, *numStart
;
251 if (*string
== '-') {
254 } else if (*string
== '+')
257 /* first pass: determine position of decimal point, number of
258 * digits, exponent and the end of the number. */
260 while (*string
>= '0' && *string
<= '9') {
265 if (*string
== '.') {
267 while (*string
>= '0' && *string
<= '9') {
273 /* no digits, no number */
278 if (*string
== 'e' || *string
== 'E') {
279 const XML_Char
*expTail
;
280 exponent
= strToI (string
+1, &expTail
, 10);
281 if (expTail
== string
+1)
289 /* scale of the first digit */
290 scale
= sign
* (float)pow (10.0, (double)(pointPos
-1 + exponent
));
292 /* second pass: parse digits */
294 if (*string
!= '.') {
295 assert (*string
>= '0' && *string
<= '9');
296 result
+= scale
* (float)(*string
- '0');
301 } while (nDigits
> 0);
306 /** \brief Parse a value of a given type. */
308 parseValue(driOptionValue
*v
, driOptionType type
, const XML_Char
*string
)
310 const XML_Char
*tail
= NULL
;
311 /* skip leading white-space */
312 string
+= strspn (string
, " \f\n\r\t\v");
315 if (!strcmp (string
, "false")) {
318 } else if (!strcmp (string
, "true")) {
325 case DRI_ENUM
: /* enum is just a special integer */
327 v
->_int
= strToI (string
, &tail
, 0);
330 v
->_float
= strToF (string
, &tail
);
334 v
->_string
= strndup(string
, STRING_CONF_MAXLEN
);
339 return false; /* empty string (or containing only white-space) */
340 /* skip trailing white space */
342 tail
+= strspn (tail
, " \f\n\r\t\v");
344 return false; /* something left over that is not part of value */
349 /** \brief Parse a list of ranges of type info->type. */
351 parseRanges(driOptionInfo
*info
, const XML_Char
*string
)
353 XML_Char
*cp
, *range
;
355 driOptionRange
*ranges
;
357 XSTRDUP (cp
, string
);
358 /* pass 1: determine the number of ranges (number of commas + 1) */
360 for (nRanges
= 1; *range
; ++range
)
364 if ((ranges
= malloc(nRanges
*sizeof(driOptionRange
))) == NULL
) {
365 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
369 /* pass 2: parse all ranges into preallocated array */
371 for (i
= 0; i
< nRanges
; ++i
) {
374 end
= strchr (range
, ',');
377 sep
= strchr (range
, ':');
378 if (sep
) { /* non-empty interval */
380 if (!parseValue (&ranges
[i
].start
, info
->type
, range
) ||
381 !parseValue (&ranges
[i
].end
, info
->type
, sep
+1))
383 if (info
->type
== DRI_INT
&&
384 ranges
[i
].start
._int
> ranges
[i
].end
._int
)
386 if (info
->type
== DRI_FLOAT
&&
387 ranges
[i
].start
._float
> ranges
[i
].end
._float
)
389 } else { /* empty interval */
390 if (!parseValue (&ranges
[i
].start
, info
->type
, range
))
392 ranges
[i
].end
= ranges
[i
].start
;
404 assert (range
== NULL
);
406 info
->nRanges
= nRanges
;
407 info
->ranges
= ranges
;
411 /** \brief Check if a value is in one of info->ranges. */
413 checkValue(const driOptionValue
*v
, const driOptionInfo
*info
)
416 assert (info
->type
!= DRI_BOOL
); /* should be caught by the parser */
417 if (info
->nRanges
== 0)
419 switch (info
->type
) {
420 case DRI_ENUM
: /* enum is just a special integer */
422 for (i
= 0; i
< info
->nRanges
; ++i
)
423 if (v
->_int
>= info
->ranges
[i
].start
._int
&&
424 v
->_int
<= info
->ranges
[i
].end
._int
)
428 for (i
= 0; i
< info
->nRanges
; ++i
)
429 if (v
->_float
>= info
->ranges
[i
].start
._float
&&
430 v
->_float
<= info
->ranges
[i
].end
._float
)
436 assert (0); /* should never happen */
442 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
445 * Is called from the drivers.
447 * \param f \c printf like format string.
450 __driUtilMessage(const char *f
, ...)
453 const char *libgl_debug
;
455 libgl_debug
=getenv("LIBGL_DEBUG");
456 if (libgl_debug
&& !strstr(libgl_debug
, "quiet")) {
457 fprintf(stderr
, "libGL: ");
459 vfprintf(stderr
, f
, args
);
461 fprintf(stderr
, "\n");
465 /** \brief Output a warning message. */
466 #define XML_WARNING1(msg) do {\
467 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
468 (int) XML_GetCurrentLineNumber(data->parser), \
469 (int) XML_GetCurrentColumnNumber(data->parser)); \
471 #define XML_WARNING(msg,args...) do { \
472 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
473 (int) XML_GetCurrentLineNumber(data->parser), \
474 (int) XML_GetCurrentColumnNumber(data->parser), \
477 /** \brief Output an error message. */
478 #define XML_ERROR1(msg) do { \
479 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
480 (int) XML_GetCurrentLineNumber(data->parser), \
481 (int) XML_GetCurrentColumnNumber(data->parser)); \
483 #define XML_ERROR(msg,args...) do { \
484 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
485 (int) XML_GetCurrentLineNumber(data->parser), \
486 (int) XML_GetCurrentColumnNumber(data->parser), \
489 /** \brief Output a fatal error message and abort. */
490 #define XML_FATAL1(msg) do { \
491 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
493 (int) XML_GetCurrentLineNumber(data->parser), \
494 (int) XML_GetCurrentColumnNumber(data->parser)); \
497 #define XML_FATAL(msg,args...) do { \
498 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
500 (int) XML_GetCurrentLineNumber(data->parser), \
501 (int) XML_GetCurrentColumnNumber(data->parser), \
506 /** \brief Parser context for __driConfigOptions. */
510 driOptionCache
*cache
;
519 /** \brief Elements in __driConfigOptions. */
521 OI_DESCRIPTION
= 0, OI_DRIINFO
, OI_ENUM
, OI_OPTION
, OI_SECTION
, OI_COUNT
523 static const XML_Char
*OptInfoElems
[] = {
524 "description", "driinfo", "enum", "option", "section"
527 /** \brief Parse attributes of an enum element.
529 * We're not actually interested in the data. Just make sure this is ok
530 * for external configuration tools.
533 parseEnumAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
536 const XML_Char
*value
= NULL
, *text
= NULL
;
538 uint32_t opt
= data
->curOption
;
539 for (i
= 0; attr
[i
]; i
+= 2) {
540 if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
541 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
542 else XML_FATAL("illegal enum attribute: %s.", attr
[i
]);
544 if (!value
) XML_FATAL1 ("value attribute missing in enum.");
545 if (!text
) XML_FATAL1 ("text attribute missing in enum.");
546 if (!parseValue (&v
, data
->cache
->info
[opt
].type
, value
))
547 XML_FATAL ("illegal enum value: %s.", value
);
548 if (!checkValue (&v
, &data
->cache
->info
[opt
]))
549 XML_FATAL ("enum value out of valid range: %s.", value
);
552 /** \brief Parse attributes of a description element.
554 * We're not actually interested in the data. Just make sure this is ok
555 * for external configuration tools.
558 parseDescAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
561 const XML_Char
*lang
= NULL
, *text
= NULL
;
562 for (i
= 0; attr
[i
]; i
+= 2) {
563 if (!strcmp (attr
[i
], "lang")) lang
= attr
[i
+1];
564 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
565 else XML_FATAL("illegal description attribute: %s.", attr
[i
]);
567 if (!lang
) XML_FATAL1 ("lang attribute missing in description.");
568 if (!text
) XML_FATAL1 ("text attribute missing in description.");
571 /** \brief Parse attributes of an option element. */
573 parseOptInfoAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
575 enum OptAttr
{OA_DEFAULT
= 0, OA_NAME
, OA_TYPE
, OA_VALID
, OA_COUNT
};
576 static const XML_Char
*optAttr
[] = {"default", "name", "type", "valid"};
577 const XML_Char
*attrVal
[OA_COUNT
] = {NULL
, NULL
, NULL
, NULL
};
578 const char *defaultVal
;
579 driOptionCache
*cache
= data
->cache
;
581 for (i
= 0; attr
[i
]; i
+= 2) {
582 uint32_t attrName
= bsearchStr (attr
[i
], optAttr
, OA_COUNT
);
583 if (attrName
>= OA_COUNT
)
584 XML_FATAL ("illegal option attribute: %s", attr
[i
]);
585 attrVal
[attrName
] = attr
[i
+1];
587 if (!attrVal
[OA_NAME
]) XML_FATAL1 ("name attribute missing in option.");
588 if (!attrVal
[OA_TYPE
]) XML_FATAL1 ("type attribute missing in option.");
589 if (!attrVal
[OA_DEFAULT
]) XML_FATAL1 ("default attribute missing in option.");
591 opt
= findOption (cache
, attrVal
[OA_NAME
]);
592 if (cache
->info
[opt
].name
)
593 XML_FATAL ("option %s redefined.", attrVal
[OA_NAME
]);
594 data
->curOption
= opt
;
596 XSTRDUP (cache
->info
[opt
].name
, attrVal
[OA_NAME
]);
598 if (!strcmp (attrVal
[OA_TYPE
], "bool"))
599 cache
->info
[opt
].type
= DRI_BOOL
;
600 else if (!strcmp (attrVal
[OA_TYPE
], "enum"))
601 cache
->info
[opt
].type
= DRI_ENUM
;
602 else if (!strcmp (attrVal
[OA_TYPE
], "int"))
603 cache
->info
[opt
].type
= DRI_INT
;
604 else if (!strcmp (attrVal
[OA_TYPE
], "float"))
605 cache
->info
[opt
].type
= DRI_FLOAT
;
606 else if (!strcmp (attrVal
[OA_TYPE
], "string"))
607 cache
->info
[opt
].type
= DRI_STRING
;
609 XML_FATAL ("illegal type in option: %s.", attrVal
[OA_TYPE
]);
611 defaultVal
= getenv (cache
->info
[opt
].name
);
612 if (defaultVal
!= NULL
) {
613 /* don't use XML_WARNING, we want the user to see this! */
615 "ATTENTION: default value of option %s overridden by environment.\n",
616 cache
->info
[opt
].name
);
618 defaultVal
= attrVal
[OA_DEFAULT
];
619 if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, defaultVal
))
620 XML_FATAL ("illegal default value for %s: %s.", cache
->info
[opt
].name
, defaultVal
);
622 if (attrVal
[OA_VALID
]) {
623 if (cache
->info
[opt
].type
== DRI_BOOL
)
624 XML_FATAL1 ("boolean option with valid attribute.");
625 if (!parseRanges (&cache
->info
[opt
], attrVal
[OA_VALID
]))
626 XML_FATAL ("illegal valid attribute: %s.", attrVal
[OA_VALID
]);
627 if (!checkValue (&cache
->values
[opt
], &cache
->info
[opt
]))
628 XML_FATAL ("default value out of valid range '%s': %s.",
629 attrVal
[OA_VALID
], defaultVal
);
630 } else if (cache
->info
[opt
].type
== DRI_ENUM
) {
631 XML_FATAL1 ("valid attribute missing in option (mandatory for enums).");
633 cache
->info
[opt
].nRanges
= 0;
634 cache
->info
[opt
].ranges
= NULL
;
638 /** \brief Handler for start element events. */
640 optInfoStartElem(void *userData
, const XML_Char
*name
, const XML_Char
**attr
)
642 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
643 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
647 XML_FATAL1 ("nested <driinfo> elements.");
649 XML_FATAL1 ("attributes specified on <driinfo> element.");
650 data
->inDriInfo
= true;
653 if (!data
->inDriInfo
)
654 XML_FATAL1 ("<section> must be inside <driinfo>.");
656 XML_FATAL1 ("nested <section> elements.");
658 XML_FATAL1 ("attributes specified on <section> element.");
659 data
->inSection
= true;
662 if (!data
->inSection
&& !data
->inOption
)
663 XML_FATAL1 ("<description> must be inside <description> or <option.");
665 XML_FATAL1 ("nested <description> elements.");
667 parseDescAttr (data
, attr
);
670 if (!data
->inSection
)
671 XML_FATAL1 ("<option> must be inside <section>.");
673 XML_FATAL1 ("<option> nested in <description> element.");
675 XML_FATAL1 ("nested <option> elements.");
676 data
->inOption
= true;
677 parseOptInfoAttr (data
, attr
);
680 if (!(data
->inOption
&& data
->inDesc
))
681 XML_FATAL1 ("<enum> must be inside <option> and <description>.");
683 XML_FATAL1 ("nested <enum> elements.");
685 parseEnumAttr (data
, attr
);
688 XML_FATAL ("unknown element: %s.", name
);
692 /** \brief Handler for end element events. */
694 optInfoEndElem(void *userData
, const XML_Char
*name
)
696 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
697 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
700 data
->inDriInfo
= false;
703 data
->inSection
= false;
706 data
->inDesc
= false;
709 data
->inOption
= false;
712 data
->inEnum
= false;
715 assert (0); /* should have been caught by StartElem */
720 driParseOptionInfo(driOptionCache
*info
, const char *configOptions
)
724 struct OptInfoData userData
;
725 struct OptInfoData
*data
= &userData
;
727 /* Make the hash table big enough to fit more than the maximum number of
728 * config options we've ever seen in a driver.
731 info
->info
= calloc(1 << info
->tableSize
, sizeof (driOptionInfo
));
732 info
->values
= calloc(1 << info
->tableSize
, sizeof (driOptionValue
));
733 if (info
->info
== NULL
|| info
->values
== NULL
) {
734 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
738 p
= XML_ParserCreate ("UTF-8"); /* always UTF-8 */
739 XML_SetElementHandler (p
, optInfoStartElem
, optInfoEndElem
);
740 XML_SetUserData (p
, data
);
742 userData
.name
= "__driConfigOptions";
744 userData
.cache
= info
;
745 userData
.inDriInfo
= false;
746 userData
.inSection
= false;
747 userData
.inDesc
= false;
748 userData
.inOption
= false;
749 userData
.inEnum
= false;
750 userData
.curOption
= -1;
752 status
= XML_Parse (p
, configOptions
, strlen (configOptions
), 1);
754 XML_FATAL ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
759 /** \brief Parser context for configuration files. */
763 driOptionCache
*cache
;
765 const char *driverName
, *execName
;
766 uint32_t ignoringDevice
;
767 uint32_t ignoringApp
;
774 /** \brief Elements in configuration files. */
776 OC_APPLICATION
= 0, OC_DEVICE
, OC_DRICONF
, OC_OPTION
, OC_COUNT
778 static const XML_Char
*OptConfElems
[] = {
779 [OC_APPLICATION
] = "application",
780 [OC_DEVICE
] = "device",
781 [OC_DRICONF
] = "driconf",
782 [OC_OPTION
] = "option",
785 /** \brief Parse attributes of a device element. */
787 parseDeviceAttr(struct OptConfData
*data
, const XML_Char
**attr
)
790 const XML_Char
*driver
= NULL
, *screen
= NULL
;
791 for (i
= 0; attr
[i
]; i
+= 2) {
792 if (!strcmp (attr
[i
], "driver")) driver
= attr
[i
+1];
793 else if (!strcmp (attr
[i
], "screen")) screen
= attr
[i
+1];
794 else XML_WARNING("unknown device attribute: %s.", attr
[i
]);
796 if (driver
&& strcmp (driver
, data
->driverName
))
797 data
->ignoringDevice
= data
->inDevice
;
799 driOptionValue screenNum
;
800 if (!parseValue (&screenNum
, DRI_INT
, screen
))
801 XML_WARNING("illegal screen number: %s.", screen
);
802 else if (screenNum
._int
!= data
->screenNum
)
803 data
->ignoringDevice
= data
->inDevice
;
807 /** \brief Parse attributes of an application element. */
809 parseAppAttr(struct OptConfData
*data
, const XML_Char
**attr
)
812 const XML_Char
*exec
= NULL
;
813 for (i
= 0; attr
[i
]; i
+= 2) {
814 if (!strcmp (attr
[i
], "name")) /* not needed here */;
815 else if (!strcmp (attr
[i
], "executable")) exec
= attr
[i
+1];
816 else XML_WARNING("unknown application attribute: %s.", attr
[i
]);
818 if (exec
&& strcmp (exec
, data
->execName
))
819 data
->ignoringApp
= data
->inApp
;
822 /** \brief Parse attributes of an option element. */
824 parseOptConfAttr(struct OptConfData
*data
, const XML_Char
**attr
)
827 const XML_Char
*name
= NULL
, *value
= NULL
;
828 for (i
= 0; attr
[i
]; i
+= 2) {
829 if (!strcmp (attr
[i
], "name")) name
= attr
[i
+1];
830 else if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
831 else XML_WARNING("unknown option attribute: %s.", attr
[i
]);
833 if (!name
) XML_WARNING1 ("name attribute missing in option.");
834 if (!value
) XML_WARNING1 ("value attribute missing in option.");
836 driOptionCache
*cache
= data
->cache
;
837 uint32_t opt
= findOption (cache
, name
);
838 if (cache
->info
[opt
].name
== NULL
)
839 /* don't use XML_WARNING, drirc defines options for all drivers,
840 * but not all drivers support them */
842 else if (getenv (cache
->info
[opt
].name
))
843 /* don't use XML_WARNING, we want the user to see this! */
844 fprintf (stderr
, "ATTENTION: option value of option %s ignored.\n",
845 cache
->info
[opt
].name
);
846 else if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, value
))
847 XML_WARNING ("illegal option value: %s.", value
);
851 /** \brief Handler for start element events. */
853 optConfStartElem(void *userData
, const XML_Char
*name
,
854 const XML_Char
**attr
)
856 struct OptConfData
*data
= (struct OptConfData
*)userData
;
857 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
861 XML_WARNING1 ("nested <driconf> elements.");
863 XML_WARNING1 ("attributes specified on <driconf> element.");
867 if (!data
->inDriConf
)
868 XML_WARNING1 ("<device> should be inside <driconf>.");
870 XML_WARNING1 ("nested <device> elements.");
872 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
873 parseDeviceAttr (data
, attr
);
877 XML_WARNING1 ("<application> should be inside <device>.");
879 XML_WARNING1 ("nested <application> elements.");
881 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
882 parseAppAttr (data
, attr
);
886 XML_WARNING1 ("<option> should be inside <application>.");
888 XML_WARNING1 ("nested <option> elements.");
890 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
891 parseOptConfAttr (data
, attr
);
894 XML_WARNING ("unknown element: %s.", name
);
898 /** \brief Handler for end element events. */
900 optConfEndElem(void *userData
, const XML_Char
*name
)
902 struct OptConfData
*data
= (struct OptConfData
*)userData
;
903 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
909 if (data
->inDevice
-- == data
->ignoringDevice
)
910 data
->ignoringDevice
= 0;
913 if (data
->inApp
-- == data
->ignoringApp
)
914 data
->ignoringApp
= 0;
920 /* unknown element, warning was produced on start tag */;
924 /** \brief Initialize an option cache based on info */
926 initOptionCache(driOptionCache
*cache
, const driOptionCache
*info
)
928 GLuint i
, size
= 1 << info
->tableSize
;
929 cache
->info
= info
->info
;
930 cache
->tableSize
= info
->tableSize
;
931 cache
->values
= malloc((1<<info
->tableSize
) * sizeof (driOptionValue
));
932 if (cache
->values
== NULL
) {
933 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
936 memcpy (cache
->values
, info
->values
,
937 (1<<info
->tableSize
) * sizeof (driOptionValue
));
938 for (i
= 0; i
< size
; ++i
) {
939 if (cache
->info
[i
].type
== DRI_STRING
)
940 XSTRDUP(cache
->values
[i
]._string
, info
->values
[i
]._string
);
944 /** \brief Parse the named configuration file */
946 parseOneConfigFile(XML_Parser p
)
948 #define BUF_SIZE 0x1000
949 struct OptConfData
*data
= (struct OptConfData
*)XML_GetUserData (p
);
953 if ((fd
= open (data
->name
, O_RDONLY
)) == -1) {
954 __driUtilMessage ("Can't open configuration file %s: %s.",
955 data
->name
, strerror (errno
));
961 void *buffer
= XML_GetBuffer (p
, BUF_SIZE
);
963 __driUtilMessage ("Can't allocate parser buffer.");
966 bytesRead
= read (fd
, buffer
, BUF_SIZE
);
967 if (bytesRead
== -1) {
968 __driUtilMessage ("Error reading from configuration file %s: %s.",
969 data
->name
, strerror (errno
));
972 status
= XML_ParseBuffer (p
, bytesRead
, bytesRead
== 0);
974 XML_ERROR ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
986 #define SYSCONFDIR "/etc"
990 driParseConfigFiles(driOptionCache
*cache
, const driOptionCache
*info
,
991 int screenNum
, const char *driverName
)
993 char *filenames
[2] = { SYSCONFDIR
"/drirc", NULL
};
996 struct OptConfData userData
;
998 initOptionCache (cache
, info
);
1000 userData
.cache
= cache
;
1001 userData
.screenNum
= screenNum
;
1002 userData
.driverName
= driverName
;
1003 userData
.execName
= GET_PROGRAM_NAME();
1005 if ((home
= getenv ("HOME"))) {
1006 uint32_t len
= strlen (home
);
1007 filenames
[1] = malloc(len
+ 7+1);
1008 if (filenames
[1] == NULL
)
1009 __driUtilMessage ("Can't allocate memory for %s/.drirc.", home
);
1011 memcpy (filenames
[1], home
, len
);
1012 memcpy (filenames
[1] + len
, "/.drirc", 7+1);
1016 for (i
= 0; i
< 2; ++i
) {
1018 if (filenames
[i
] == NULL
)
1021 p
= XML_ParserCreate (NULL
); /* use encoding specified by file */
1022 XML_SetElementHandler (p
, optConfStartElem
, optConfEndElem
);
1023 XML_SetUserData (p
, &userData
);
1024 userData
.parser
= p
;
1025 userData
.name
= filenames
[i
];
1026 userData
.ignoringDevice
= 0;
1027 userData
.ignoringApp
= 0;
1028 userData
.inDriConf
= 0;
1029 userData
.inDevice
= 0;
1031 userData
.inOption
= 0;
1033 parseOneConfigFile (p
);
1041 driDestroyOptionInfo(driOptionCache
*info
)
1043 driDestroyOptionCache(info
);
1045 uint32_t i
, size
= 1 << info
->tableSize
;
1046 for (i
= 0; i
< size
; ++i
) {
1047 if (info
->info
[i
].name
) {
1048 free(info
->info
[i
].name
);
1049 free(info
->info
[i
].ranges
);
1057 driDestroyOptionCache(driOptionCache
*cache
)
1060 GLuint i
, size
= 1 << cache
->tableSize
;
1061 for (i
= 0; i
< size
; ++i
) {
1062 if (cache
->info
[i
].type
== DRI_STRING
)
1063 free(cache
->values
[i
]._string
);
1066 free(cache
->values
);
1070 driCheckOption(const driOptionCache
*cache
, const char *name
,
1073 uint32_t i
= findOption (cache
, name
);
1074 return cache
->info
[i
].name
!= NULL
&& cache
->info
[i
].type
== type
;
1078 driQueryOptionb(const driOptionCache
*cache
, const char *name
)
1080 uint32_t i
= findOption (cache
, name
);
1081 /* make sure the option is defined and has the correct type */
1082 assert (cache
->info
[i
].name
!= NULL
);
1083 assert (cache
->info
[i
].type
== DRI_BOOL
);
1084 return cache
->values
[i
]._bool
;
1088 driQueryOptioni(const driOptionCache
*cache
, const char *name
)
1090 uint32_t i
= findOption (cache
, name
);
1091 /* make sure the option is defined and has the correct type */
1092 assert (cache
->info
[i
].name
!= NULL
);
1093 assert (cache
->info
[i
].type
== DRI_INT
|| cache
->info
[i
].type
== DRI_ENUM
);
1094 return cache
->values
[i
]._int
;
1098 driQueryOptionf(const driOptionCache
*cache
, const char *name
)
1100 uint32_t i
= findOption (cache
, name
);
1101 /* make sure the option is defined and has the correct type */
1102 assert (cache
->info
[i
].name
!= NULL
);
1103 assert (cache
->info
[i
].type
== DRI_FLOAT
);
1104 return cache
->values
[i
]._float
;
1108 driQueryOptionstr(const driOptionCache
*cache
, const char *name
)
1110 GLuint i
= findOption (cache
, name
);
1111 /* make sure the option is defined and has the correct type */
1112 assert (cache
->info
[i
].name
!= NULL
);
1113 assert (cache
->info
[i
].type
== DRI_STRING
);
1114 return cache
->values
[i
]._string
;