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
42 #include "xmlconfig.h"
43 #include "u_process.h"
46 /** \brief Find an option in an option cache with the name as key */
48 findOption(const driOptionCache
*cache
, const char *name
)
50 uint32_t len
= strlen (name
);
51 uint32_t size
= 1 << cache
->tableSize
, mask
= size
- 1;
55 /* compute a hash from the variable length name */
56 for (i
= 0, shift
= 0; i
< len
; ++i
, shift
= (shift
+8) & 31)
57 hash
+= (uint32_t)name
[i
] << shift
;
59 hash
= (hash
>> (16-cache
->tableSize
/2)) & mask
;
61 /* this is just the starting point of the linear search for the option */
62 for (i
= 0; i
< size
; ++i
, hash
= (hash
+1) & mask
) {
63 /* if we hit an empty entry then the option is not defined (yet) */
64 if (cache
->info
[hash
].name
== 0)
66 else if (!strcmp (name
, cache
->info
[hash
].name
))
69 /* this assertion fails if the hash table is full */
75 /** \brief Like strdup but using malloc and with error checking. */
76 #define XSTRDUP(dest,source) do { \
77 uint32_t len = strlen (source); \
78 if (!(dest = malloc(len+1))) { \
79 fprintf (stderr, "%s: %d: out of memory.\n", __FILE__, __LINE__); \
82 memcpy (dest, source, len+1); \
85 static int compare (const void *a
, const void *b
) {
86 return strcmp (*(char *const*)a
, *(char *const*)b
);
88 /** \brief Binary search in a string array. */
90 bsearchStr (const XML_Char
*name
, const XML_Char
*elems
[], uint32_t count
)
92 const XML_Char
**found
;
93 found
= bsearch (&name
, elems
, count
, sizeof (XML_Char
*), compare
);
100 /** \brief Locale-independent integer parser.
102 * Works similar to strtol. Leading space is NOT skipped. The input
103 * number may have an optional sign. Radix is specified by base. If
104 * base is 0 then decimal is assumed unless the input number is
105 * prefixed by 0x or 0X for hexadecimal or 0 for octal. After
106 * returning tail points to the first character that is not part of
107 * the integer number. If no number was found then tail points to the
108 * start of the input string. */
110 strToI(const XML_Char
*string
, const XML_Char
**tail
, int base
)
112 int radix
= base
== 0 ? 10 : base
;
115 bool numberFound
= false;
116 const XML_Char
*start
= string
;
118 assert (radix
>= 2 && radix
<= 36);
120 if (*string
== '-') {
123 } else if (*string
== '+')
125 if (base
== 0 && *string
== '0') {
127 if (*(string
+1) == 'x' || *(string
+1) == 'X') {
138 if (*string
>= '0' && *string
< '0' + radix
)
139 digit
= *string
- '0';
141 if (*string
>= '0' && *string
<= '9')
142 digit
= *string
- '0';
143 else if (*string
>= 'a' && *string
< 'a' + radix
- 10)
144 digit
= *string
- 'a' + 10;
145 else if (*string
>= 'A' && *string
< 'A' + radix
- 10)
146 digit
= *string
- 'A' + 10;
150 result
= radix
*result
+ digit
;
155 *tail
= numberFound
? string
: start
;
156 return sign
* result
;
159 /** \brief Locale-independent floating-point parser.
161 * Works similar to strtod. Leading space is NOT skipped. The input
162 * number may have an optional sign. '.' is interpreted as decimal
163 * point and may occur at most once. Optionally the number may end in
164 * [eE]<exponent>, where <exponent> is an integer as recognized by
165 * strToI. In that case the result is number * 10^exponent. After
166 * returning tail points to the first character that is not part of
167 * the floating point number. If no number was found then tail points
168 * to the start of the input string.
170 * Uses two passes for maximum accuracy. */
172 strToF(const XML_Char
*string
, const XML_Char
**tail
)
174 int nDigits
= 0, pointPos
, exponent
;
175 float sign
= 1.0f
, result
= 0.0f
, scale
;
176 const XML_Char
*start
= string
, *numStart
;
179 if (*string
== '-') {
182 } else if (*string
== '+')
185 /* first pass: determine position of decimal point, number of
186 * digits, exponent and the end of the number. */
188 while (*string
>= '0' && *string
<= '9') {
193 if (*string
== '.') {
195 while (*string
>= '0' && *string
<= '9') {
201 /* no digits, no number */
206 if (*string
== 'e' || *string
== 'E') {
207 const XML_Char
*expTail
;
208 exponent
= strToI (string
+1, &expTail
, 10);
209 if (expTail
== string
+1)
217 /* scale of the first digit */
218 scale
= sign
* (float)pow (10.0, (double)(pointPos
-1 + exponent
));
220 /* second pass: parse digits */
222 if (*string
!= '.') {
223 assert (*string
>= '0' && *string
<= '9');
224 result
+= scale
* (float)(*string
- '0');
229 } while (nDigits
> 0);
234 /** \brief Parse a value of a given type. */
236 parseValue(driOptionValue
*v
, driOptionType type
, const XML_Char
*string
)
238 const XML_Char
*tail
= NULL
;
239 /* skip leading white-space */
240 string
+= strspn (string
, " \f\n\r\t\v");
243 if (!strcmp (string
, "false")) {
246 } else if (!strcmp (string
, "true")) {
253 case DRI_ENUM
: /* enum is just a special integer */
255 v
->_int
= strToI (string
, &tail
, 0);
258 v
->_float
= strToF (string
, &tail
);
262 v
->_string
= strndup(string
, STRING_CONF_MAXLEN
);
267 return false; /* empty string (or containing only white-space) */
268 /* skip trailing white space */
270 tail
+= strspn (tail
, " \f\n\r\t\v");
272 return false; /* something left over that is not part of value */
277 /** \brief Parse a list of ranges of type info->type. */
279 parseRanges(driOptionInfo
*info
, const XML_Char
*string
)
281 XML_Char
*cp
, *range
;
283 driOptionRange
*ranges
;
285 XSTRDUP (cp
, string
);
286 /* pass 1: determine the number of ranges (number of commas + 1) */
288 for (nRanges
= 1; *range
; ++range
)
292 if ((ranges
= malloc(nRanges
*sizeof(driOptionRange
))) == NULL
) {
293 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
297 /* pass 2: parse all ranges into preallocated array */
299 for (i
= 0; i
< nRanges
; ++i
) {
302 end
= strchr (range
, ',');
305 sep
= strchr (range
, ':');
306 if (sep
) { /* non-empty interval */
308 if (!parseValue (&ranges
[i
].start
, info
->type
, range
) ||
309 !parseValue (&ranges
[i
].end
, info
->type
, sep
+1))
311 if (info
->type
== DRI_INT
&&
312 ranges
[i
].start
._int
> ranges
[i
].end
._int
)
314 if (info
->type
== DRI_FLOAT
&&
315 ranges
[i
].start
._float
> ranges
[i
].end
._float
)
317 } else { /* empty interval */
318 if (!parseValue (&ranges
[i
].start
, info
->type
, range
))
320 ranges
[i
].end
= ranges
[i
].start
;
332 assert (range
== NULL
);
334 info
->nRanges
= nRanges
;
335 info
->ranges
= ranges
;
339 /** \brief Check if a value is in one of info->ranges. */
341 checkValue(const driOptionValue
*v
, const driOptionInfo
*info
)
344 assert (info
->type
!= DRI_BOOL
); /* should be caught by the parser */
345 if (info
->nRanges
== 0)
347 switch (info
->type
) {
348 case DRI_ENUM
: /* enum is just a special integer */
350 for (i
= 0; i
< info
->nRanges
; ++i
)
351 if (v
->_int
>= info
->ranges
[i
].start
._int
&&
352 v
->_int
<= info
->ranges
[i
].end
._int
)
356 for (i
= 0; i
< info
->nRanges
; ++i
)
357 if (v
->_float
>= info
->ranges
[i
].start
._float
&&
358 v
->_float
<= info
->ranges
[i
].end
._float
)
364 assert (0); /* should never happen */
370 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
373 * Is called from the drivers.
375 * \param f \c printf like format string.
378 __driUtilMessage(const char *f
, ...)
381 const char *libgl_debug
;
383 libgl_debug
=getenv("LIBGL_DEBUG");
384 if (libgl_debug
&& !strstr(libgl_debug
, "quiet")) {
385 fprintf(stderr
, "libGL: ");
387 vfprintf(stderr
, f
, args
);
389 fprintf(stderr
, "\n");
393 /** \brief Output a warning message. */
394 #define XML_WARNING1(msg) do {\
395 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
396 (int) XML_GetCurrentLineNumber(data->parser), \
397 (int) XML_GetCurrentColumnNumber(data->parser)); \
399 #define XML_WARNING(msg, ...) do { \
400 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
401 (int) XML_GetCurrentLineNumber(data->parser), \
402 (int) XML_GetCurrentColumnNumber(data->parser), \
405 /** \brief Output an error message. */
406 #define XML_ERROR1(msg) do { \
407 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
408 (int) XML_GetCurrentLineNumber(data->parser), \
409 (int) XML_GetCurrentColumnNumber(data->parser)); \
411 #define XML_ERROR(msg, ...) do { \
412 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
413 (int) XML_GetCurrentLineNumber(data->parser), \
414 (int) XML_GetCurrentColumnNumber(data->parser), \
417 /** \brief Output a fatal error message and abort. */
418 #define XML_FATAL1(msg) do { \
419 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
421 (int) XML_GetCurrentLineNumber(data->parser), \
422 (int) XML_GetCurrentColumnNumber(data->parser)); \
425 #define XML_FATAL(msg, ...) do { \
426 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
428 (int) XML_GetCurrentLineNumber(data->parser), \
429 (int) XML_GetCurrentColumnNumber(data->parser), \
434 /** \brief Parser context for __driConfigOptions. */
438 driOptionCache
*cache
;
447 /** \brief Elements in __driConfigOptions. */
449 OI_DESCRIPTION
= 0, OI_DRIINFO
, OI_ENUM
, OI_OPTION
, OI_SECTION
, OI_COUNT
451 static const XML_Char
*OptInfoElems
[] = {
452 "description", "driinfo", "enum", "option", "section"
455 /** \brief Parse attributes of an enum element.
457 * We're not actually interested in the data. Just make sure this is ok
458 * for external configuration tools.
461 parseEnumAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
464 const XML_Char
*value
= NULL
, *text
= NULL
;
466 uint32_t opt
= data
->curOption
;
467 for (i
= 0; attr
[i
]; i
+= 2) {
468 if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
469 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
470 else XML_FATAL("illegal enum attribute: %s.", attr
[i
]);
472 if (!value
) XML_FATAL1 ("value attribute missing in enum.");
473 if (!text
) XML_FATAL1 ("text attribute missing in enum.");
474 if (!parseValue (&v
, data
->cache
->info
[opt
].type
, value
))
475 XML_FATAL ("illegal enum value: %s.", value
);
476 if (!checkValue (&v
, &data
->cache
->info
[opt
]))
477 XML_FATAL ("enum value out of valid range: %s.", value
);
480 /** \brief Parse attributes of a description element.
482 * We're not actually interested in the data. Just make sure this is ok
483 * for external configuration tools.
486 parseDescAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
489 const XML_Char
*lang
= NULL
, *text
= NULL
;
490 for (i
= 0; attr
[i
]; i
+= 2) {
491 if (!strcmp (attr
[i
], "lang")) lang
= attr
[i
+1];
492 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
493 else XML_FATAL("illegal description attribute: %s.", attr
[i
]);
495 if (!lang
) XML_FATAL1 ("lang attribute missing in description.");
496 if (!text
) XML_FATAL1 ("text attribute missing in description.");
499 /** \brief Parse attributes of an option element. */
501 parseOptInfoAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
503 enum OptAttr
{OA_DEFAULT
= 0, OA_NAME
, OA_TYPE
, OA_VALID
, OA_COUNT
};
504 static const XML_Char
*optAttr
[] = {"default", "name", "type", "valid"};
505 const XML_Char
*attrVal
[OA_COUNT
] = {NULL
, NULL
, NULL
, NULL
};
506 const char *defaultVal
;
507 driOptionCache
*cache
= data
->cache
;
509 for (i
= 0; attr
[i
]; i
+= 2) {
510 uint32_t attrName
= bsearchStr (attr
[i
], optAttr
, OA_COUNT
);
511 if (attrName
>= OA_COUNT
)
512 XML_FATAL ("illegal option attribute: %s", attr
[i
]);
513 attrVal
[attrName
] = attr
[i
+1];
515 if (!attrVal
[OA_NAME
]) XML_FATAL1 ("name attribute missing in option.");
516 if (!attrVal
[OA_TYPE
]) XML_FATAL1 ("type attribute missing in option.");
517 if (!attrVal
[OA_DEFAULT
]) XML_FATAL1 ("default attribute missing in option.");
519 opt
= findOption (cache
, attrVal
[OA_NAME
]);
520 if (cache
->info
[opt
].name
)
521 XML_FATAL ("option %s redefined.", attrVal
[OA_NAME
]);
522 data
->curOption
= opt
;
524 XSTRDUP (cache
->info
[opt
].name
, attrVal
[OA_NAME
]);
526 if (!strcmp (attrVal
[OA_TYPE
], "bool"))
527 cache
->info
[opt
].type
= DRI_BOOL
;
528 else if (!strcmp (attrVal
[OA_TYPE
], "enum"))
529 cache
->info
[opt
].type
= DRI_ENUM
;
530 else if (!strcmp (attrVal
[OA_TYPE
], "int"))
531 cache
->info
[opt
].type
= DRI_INT
;
532 else if (!strcmp (attrVal
[OA_TYPE
], "float"))
533 cache
->info
[opt
].type
= DRI_FLOAT
;
534 else if (!strcmp (attrVal
[OA_TYPE
], "string"))
535 cache
->info
[opt
].type
= DRI_STRING
;
537 XML_FATAL ("illegal type in option: %s.", attrVal
[OA_TYPE
]);
539 defaultVal
= getenv (cache
->info
[opt
].name
);
540 if (defaultVal
!= NULL
) {
541 /* don't use XML_WARNING, we want the user to see this! */
543 "ATTENTION: default value of option %s overridden by environment.\n",
544 cache
->info
[opt
].name
);
546 defaultVal
= attrVal
[OA_DEFAULT
];
547 if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, defaultVal
))
548 XML_FATAL ("illegal default value for %s: %s.", cache
->info
[opt
].name
, defaultVal
);
550 if (attrVal
[OA_VALID
]) {
551 if (cache
->info
[opt
].type
== DRI_BOOL
)
552 XML_FATAL1 ("boolean option with valid attribute.");
553 if (!parseRanges (&cache
->info
[opt
], attrVal
[OA_VALID
]))
554 XML_FATAL ("illegal valid attribute: %s.", attrVal
[OA_VALID
]);
555 if (!checkValue (&cache
->values
[opt
], &cache
->info
[opt
]))
556 XML_FATAL ("default value out of valid range '%s': %s.",
557 attrVal
[OA_VALID
], defaultVal
);
558 } else if (cache
->info
[opt
].type
== DRI_ENUM
) {
559 XML_FATAL1 ("valid attribute missing in option (mandatory for enums).");
561 cache
->info
[opt
].nRanges
= 0;
562 cache
->info
[opt
].ranges
= NULL
;
566 /** \brief Handler for start element events. */
568 optInfoStartElem(void *userData
, const XML_Char
*name
, const XML_Char
**attr
)
570 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
571 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
575 XML_FATAL1 ("nested <driinfo> elements.");
577 XML_FATAL1 ("attributes specified on <driinfo> element.");
578 data
->inDriInfo
= true;
581 if (!data
->inDriInfo
)
582 XML_FATAL1 ("<section> must be inside <driinfo>.");
584 XML_FATAL1 ("nested <section> elements.");
586 XML_FATAL1 ("attributes specified on <section> element.");
587 data
->inSection
= true;
590 if (!data
->inSection
&& !data
->inOption
)
591 XML_FATAL1 ("<description> must be inside <description> or <option.");
593 XML_FATAL1 ("nested <description> elements.");
595 parseDescAttr (data
, attr
);
598 if (!data
->inSection
)
599 XML_FATAL1 ("<option> must be inside <section>.");
601 XML_FATAL1 ("<option> nested in <description> element.");
603 XML_FATAL1 ("nested <option> elements.");
604 data
->inOption
= true;
605 parseOptInfoAttr (data
, attr
);
608 if (!(data
->inOption
&& data
->inDesc
))
609 XML_FATAL1 ("<enum> must be inside <option> and <description>.");
611 XML_FATAL1 ("nested <enum> elements.");
613 parseEnumAttr (data
, attr
);
616 XML_FATAL ("unknown element: %s.", name
);
620 /** \brief Handler for end element events. */
622 optInfoEndElem(void *userData
, const XML_Char
*name
)
624 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
625 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
628 data
->inDriInfo
= false;
631 data
->inSection
= false;
634 data
->inDesc
= false;
637 data
->inOption
= false;
640 data
->inEnum
= false;
643 assert (0); /* should have been caught by StartElem */
648 driParseOptionInfo(driOptionCache
*info
, const char *configOptions
)
652 struct OptInfoData userData
;
653 struct OptInfoData
*data
= &userData
;
655 /* Make the hash table big enough to fit more than the maximum number of
656 * config options we've ever seen in a driver.
659 info
->info
= calloc(1 << info
->tableSize
, sizeof (driOptionInfo
));
660 info
->values
= calloc(1 << info
->tableSize
, sizeof (driOptionValue
));
661 if (info
->info
== NULL
|| info
->values
== NULL
) {
662 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
666 p
= XML_ParserCreate ("UTF-8"); /* always UTF-8 */
667 XML_SetElementHandler (p
, optInfoStartElem
, optInfoEndElem
);
668 XML_SetUserData (p
, data
);
670 userData
.name
= "__driConfigOptions";
672 userData
.cache
= info
;
673 userData
.inDriInfo
= false;
674 userData
.inSection
= false;
675 userData
.inDesc
= false;
676 userData
.inOption
= false;
677 userData
.inEnum
= false;
678 userData
.curOption
= -1;
680 status
= XML_Parse (p
, configOptions
, strlen (configOptions
), 1);
682 XML_FATAL ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
687 /** \brief Parser context for configuration files. */
691 driOptionCache
*cache
;
693 const char *driverName
, *execName
;
694 const char *kernelDriverName
;
695 uint32_t ignoringDevice
;
696 uint32_t ignoringApp
;
703 /** \brief Elements in configuration files. */
705 OC_APPLICATION
= 0, OC_DEVICE
, OC_DRICONF
, OC_OPTION
, OC_COUNT
707 static const XML_Char
*OptConfElems
[] = {
708 [OC_APPLICATION
] = "application",
709 [OC_DEVICE
] = "device",
710 [OC_DRICONF
] = "driconf",
711 [OC_OPTION
] = "option",
714 /** \brief Parse attributes of a device element. */
716 parseDeviceAttr(struct OptConfData
*data
, const XML_Char
**attr
)
719 const XML_Char
*driver
= NULL
, *screen
= NULL
, *kernel
= NULL
;
720 for (i
= 0; attr
[i
]; i
+= 2) {
721 if (!strcmp (attr
[i
], "driver")) driver
= attr
[i
+1];
722 else if (!strcmp (attr
[i
], "screen")) screen
= attr
[i
+1];
723 else if (!strcmp (attr
[i
], "kernel_driver")) kernel
= attr
[i
+1];
724 else XML_WARNING("unknown device attribute: %s.", attr
[i
]);
726 if (driver
&& strcmp (driver
, data
->driverName
))
727 data
->ignoringDevice
= data
->inDevice
;
728 else if (kernel
&& (!data
->kernelDriverName
|| strcmp (kernel
, data
->kernelDriverName
)))
729 data
->ignoringDevice
= data
->inDevice
;
731 driOptionValue screenNum
;
732 if (!parseValue (&screenNum
, DRI_INT
, screen
))
733 XML_WARNING("illegal screen number: %s.", screen
);
734 else if (screenNum
._int
!= data
->screenNum
)
735 data
->ignoringDevice
= data
->inDevice
;
739 /** \brief Parse attributes of an application element. */
741 parseAppAttr(struct OptConfData
*data
, const XML_Char
**attr
)
744 const XML_Char
*exec
= NULL
;
745 for (i
= 0; attr
[i
]; i
+= 2) {
746 if (!strcmp (attr
[i
], "name")) /* not needed here */;
747 else if (!strcmp (attr
[i
], "executable")) exec
= attr
[i
+1];
748 else XML_WARNING("unknown application attribute: %s.", attr
[i
]);
750 if (exec
&& strcmp (exec
, data
->execName
))
751 data
->ignoringApp
= data
->inApp
;
754 /** \brief Parse attributes of an option element. */
756 parseOptConfAttr(struct OptConfData
*data
, const XML_Char
**attr
)
759 const XML_Char
*name
= NULL
, *value
= NULL
;
760 for (i
= 0; attr
[i
]; i
+= 2) {
761 if (!strcmp (attr
[i
], "name")) name
= attr
[i
+1];
762 else if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
763 else XML_WARNING("unknown option attribute: %s.", attr
[i
]);
765 if (!name
) XML_WARNING1 ("name attribute missing in option.");
766 if (!value
) XML_WARNING1 ("value attribute missing in option.");
768 driOptionCache
*cache
= data
->cache
;
769 uint32_t opt
= findOption (cache
, name
);
770 if (cache
->info
[opt
].name
== NULL
)
771 /* don't use XML_WARNING, drirc defines options for all drivers,
772 * but not all drivers support them */
774 else if (getenv (cache
->info
[opt
].name
))
775 /* don't use XML_WARNING, we want the user to see this! */
776 fprintf (stderr
, "ATTENTION: option value of option %s ignored.\n",
777 cache
->info
[opt
].name
);
778 else if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, value
))
779 XML_WARNING ("illegal option value: %s.", value
);
783 /** \brief Handler for start element events. */
785 optConfStartElem(void *userData
, const XML_Char
*name
,
786 const XML_Char
**attr
)
788 struct OptConfData
*data
= (struct OptConfData
*)userData
;
789 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
793 XML_WARNING1 ("nested <driconf> elements.");
795 XML_WARNING1 ("attributes specified on <driconf> element.");
799 if (!data
->inDriConf
)
800 XML_WARNING1 ("<device> should be inside <driconf>.");
802 XML_WARNING1 ("nested <device> elements.");
804 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
805 parseDeviceAttr (data
, attr
);
809 XML_WARNING1 ("<application> should be inside <device>.");
811 XML_WARNING1 ("nested <application> elements.");
813 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
814 parseAppAttr (data
, attr
);
818 XML_WARNING1 ("<option> should be inside <application>.");
820 XML_WARNING1 ("nested <option> elements.");
822 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
823 parseOptConfAttr (data
, attr
);
826 XML_WARNING ("unknown element: %s.", name
);
830 /** \brief Handler for end element events. */
832 optConfEndElem(void *userData
, const XML_Char
*name
)
834 struct OptConfData
*data
= (struct OptConfData
*)userData
;
835 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
841 if (data
->inDevice
-- == data
->ignoringDevice
)
842 data
->ignoringDevice
= 0;
845 if (data
->inApp
-- == data
->ignoringApp
)
846 data
->ignoringApp
= 0;
852 /* unknown element, warning was produced on start tag */;
856 /** \brief Initialize an option cache based on info */
858 initOptionCache(driOptionCache
*cache
, const driOptionCache
*info
)
860 unsigned i
, size
= 1 << info
->tableSize
;
861 cache
->info
= info
->info
;
862 cache
->tableSize
= info
->tableSize
;
863 cache
->values
= malloc((1<<info
->tableSize
) * sizeof (driOptionValue
));
864 if (cache
->values
== NULL
) {
865 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
868 memcpy (cache
->values
, info
->values
,
869 (1<<info
->tableSize
) * sizeof (driOptionValue
));
870 for (i
= 0; i
< size
; ++i
) {
871 if (cache
->info
[i
].type
== DRI_STRING
)
872 XSTRDUP(cache
->values
[i
]._string
, info
->values
[i
]._string
);
877 _parseOneConfigFile(XML_Parser p
)
879 #define BUF_SIZE 0x1000
880 struct OptConfData
*data
= (struct OptConfData
*)XML_GetUserData (p
);
884 if ((fd
= open (data
->name
, O_RDONLY
)) == -1) {
885 __driUtilMessage ("Can't open configuration file %s: %s.",
886 data
->name
, strerror (errno
));
892 void *buffer
= XML_GetBuffer (p
, BUF_SIZE
);
894 __driUtilMessage ("Can't allocate parser buffer.");
897 bytesRead
= read (fd
, buffer
, BUF_SIZE
);
898 if (bytesRead
== -1) {
899 __driUtilMessage ("Error reading from configuration file %s: %s.",
900 data
->name
, strerror (errno
));
903 status
= XML_ParseBuffer (p
, bytesRead
, bytesRead
== 0);
905 XML_ERROR ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
916 /** \brief Parse the named configuration file */
918 parseOneConfigFile(struct OptConfData
*data
, const char *filename
)
922 p
= XML_ParserCreate (NULL
); /* use encoding specified by file */
923 XML_SetElementHandler (p
, optConfStartElem
, optConfEndElem
);
924 XML_SetUserData (p
, data
);
926 data
->name
= filename
;
927 data
->ignoringDevice
= 0;
928 data
->ignoringApp
= 0;
934 _parseOneConfigFile (p
);
939 scandir_filter(const struct dirent
*ent
)
941 #ifndef DT_REG /* systems without d_type in dirent results */
944 if ((lstat(ent
->d_name
, &st
) != 0) ||
945 (!S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
)))
948 if (ent
->d_type
!= DT_REG
&& ent
->d_type
!= DT_LNK
)
952 if (fnmatch("*.conf", ent
->d_name
, 0))
958 /** \brief Parse configuration files in a directory */
960 parseConfigDir(struct OptConfData
*data
, const char *dirname
)
963 struct dirent
**entries
= NULL
;
965 count
= scandir(dirname
, &entries
, scandir_filter
, alphasort
);
969 for (i
= 0; i
< count
; i
++) {
970 char filename
[PATH_MAX
];
972 snprintf(filename
, PATH_MAX
, "%s/%s", dirname
, entries
[i
]->d_name
);
975 parseOneConfigFile(data
, filename
);
982 #define SYSCONFDIR "/etc"
986 #define DATADIR "/usr/share"
990 driParseConfigFiles(driOptionCache
*cache
, const driOptionCache
*info
,
991 int screenNum
, const char *driverName
,
992 const char *kernelDriverName
)
995 struct OptConfData userData
;
997 initOptionCache (cache
, info
);
999 userData
.cache
= cache
;
1000 userData
.screenNum
= screenNum
;
1001 userData
.driverName
= driverName
;
1002 userData
.kernelDriverName
= kernelDriverName
;
1003 userData
.execName
= util_get_process_name();
1005 parseConfigDir(&userData
, DATADIR
"/drirc.d");
1006 parseOneConfigFile(&userData
, SYSCONFDIR
"/drirc");
1008 if ((home
= getenv ("HOME"))) {
1009 char filename
[PATH_MAX
];
1011 snprintf(filename
, PATH_MAX
, "%s/.drirc", home
);
1012 parseOneConfigFile(&userData
, filename
);
1017 driDestroyOptionInfo(driOptionCache
*info
)
1019 driDestroyOptionCache(info
);
1021 uint32_t i
, size
= 1 << info
->tableSize
;
1022 for (i
= 0; i
< size
; ++i
) {
1023 if (info
->info
[i
].name
) {
1024 free(info
->info
[i
].name
);
1025 free(info
->info
[i
].ranges
);
1033 driDestroyOptionCache(driOptionCache
*cache
)
1036 unsigned i
, size
= 1 << cache
->tableSize
;
1037 for (i
= 0; i
< size
; ++i
) {
1038 if (cache
->info
[i
].type
== DRI_STRING
)
1039 free(cache
->values
[i
]._string
);
1042 free(cache
->values
);
1046 driCheckOption(const driOptionCache
*cache
, const char *name
,
1049 uint32_t i
= findOption (cache
, name
);
1050 return cache
->info
[i
].name
!= NULL
&& cache
->info
[i
].type
== type
;
1054 driQueryOptionb(const driOptionCache
*cache
, const char *name
)
1056 uint32_t i
= findOption (cache
, name
);
1057 /* make sure the option is defined and has the correct type */
1058 assert (cache
->info
[i
].name
!= NULL
);
1059 assert (cache
->info
[i
].type
== DRI_BOOL
);
1060 return cache
->values
[i
]._bool
;
1064 driQueryOptioni(const driOptionCache
*cache
, const char *name
)
1066 uint32_t i
= findOption (cache
, name
);
1067 /* make sure the option is defined and has the correct type */
1068 assert (cache
->info
[i
].name
!= NULL
);
1069 assert (cache
->info
[i
].type
== DRI_INT
|| cache
->info
[i
].type
== DRI_ENUM
);
1070 return cache
->values
[i
]._int
;
1074 driQueryOptionf(const driOptionCache
*cache
, const char *name
)
1076 uint32_t i
= findOption (cache
, name
);
1077 /* make sure the option is defined and has the correct type */
1078 assert (cache
->info
[i
].name
!= NULL
);
1079 assert (cache
->info
[i
].type
== DRI_FLOAT
);
1080 return cache
->values
[i
]._float
;
1084 driQueryOptionstr(const driOptionCache
*cache
, const char *name
)
1086 uint32_t i
= findOption (cache
, name
);
1087 /* make sure the option is defined and has the correct type */
1088 assert (cache
->info
[i
].name
!= NULL
);
1089 assert (cache
->info
[i
].type
== DRI_STRING
);
1090 return cache
->values
[i
]._string
;