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"
45 /* For systems like Hurd */
50 /** \brief Find an option in an option cache with the name as key */
52 findOption(const driOptionCache
*cache
, const char *name
)
54 uint32_t len
= strlen (name
);
55 uint32_t size
= 1 << cache
->tableSize
, mask
= size
- 1;
59 /* compute a hash from the variable length name */
60 for (i
= 0, shift
= 0; i
< len
; ++i
, shift
= (shift
+8) & 31)
61 hash
+= (uint32_t)name
[i
] << shift
;
63 hash
= (hash
>> (16-cache
->tableSize
/2)) & mask
;
65 /* this is just the starting point of the linear search for the option */
66 for (i
= 0; i
< size
; ++i
, hash
= (hash
+1) & mask
) {
67 /* if we hit an empty entry then the option is not defined (yet) */
68 if (cache
->info
[hash
].name
== 0)
70 else if (!strcmp (name
, cache
->info
[hash
].name
))
73 /* this assertion fails if the hash table is full */
79 /** \brief Like strdup but using malloc and with error checking. */
80 #define XSTRDUP(dest,source) do { \
81 uint32_t len = strlen (source); \
82 if (!(dest = malloc(len+1))) { \
83 fprintf (stderr, "%s: %d: out of memory.\n", __FILE__, __LINE__); \
86 memcpy (dest, source, len+1); \
89 static int compare (const void *a
, const void *b
) {
90 return strcmp (*(char *const*)a
, *(char *const*)b
);
92 /** \brief Binary search in a string array. */
94 bsearchStr (const XML_Char
*name
, const XML_Char
*elems
[], uint32_t count
)
96 const XML_Char
**found
;
97 found
= bsearch (&name
, elems
, count
, sizeof (XML_Char
*), compare
);
104 /** \brief Locale-independent integer parser.
106 * Works similar to strtol. Leading space is NOT skipped. The input
107 * number may have an optional sign. Radix is specified by base. If
108 * base is 0 then decimal is assumed unless the input number is
109 * prefixed by 0x or 0X for hexadecimal or 0 for octal. After
110 * returning tail points to the first character that is not part of
111 * the integer number. If no number was found then tail points to the
112 * start of the input string. */
114 strToI(const XML_Char
*string
, const XML_Char
**tail
, int base
)
116 int radix
= base
== 0 ? 10 : base
;
119 bool numberFound
= false;
120 const XML_Char
*start
= string
;
122 assert (radix
>= 2 && radix
<= 36);
124 if (*string
== '-') {
127 } else if (*string
== '+')
129 if (base
== 0 && *string
== '0') {
131 if (*(string
+1) == 'x' || *(string
+1) == 'X') {
142 if (*string
>= '0' && *string
< '0' + radix
)
143 digit
= *string
- '0';
145 if (*string
>= '0' && *string
<= '9')
146 digit
= *string
- '0';
147 else if (*string
>= 'a' && *string
< 'a' + radix
- 10)
148 digit
= *string
- 'a' + 10;
149 else if (*string
>= 'A' && *string
< 'A' + radix
- 10)
150 digit
= *string
- 'A' + 10;
154 result
= radix
*result
+ digit
;
159 *tail
= numberFound
? string
: start
;
160 return sign
* result
;
163 /** \brief Locale-independent floating-point parser.
165 * Works similar to strtod. Leading space is NOT skipped. The input
166 * number may have an optional sign. '.' is interpreted as decimal
167 * point and may occur at most once. Optionally the number may end in
168 * [eE]<exponent>, where <exponent> is an integer as recognized by
169 * strToI. In that case the result is number * 10^exponent. After
170 * returning tail points to the first character that is not part of
171 * the floating point number. If no number was found then tail points
172 * to the start of the input string.
174 * Uses two passes for maximum accuracy. */
176 strToF(const XML_Char
*string
, const XML_Char
**tail
)
178 int nDigits
= 0, pointPos
, exponent
;
179 float sign
= 1.0f
, result
= 0.0f
, scale
;
180 const XML_Char
*start
= string
, *numStart
;
183 if (*string
== '-') {
186 } else if (*string
== '+')
189 /* first pass: determine position of decimal point, number of
190 * digits, exponent and the end of the number. */
192 while (*string
>= '0' && *string
<= '9') {
197 if (*string
== '.') {
199 while (*string
>= '0' && *string
<= '9') {
205 /* no digits, no number */
210 if (*string
== 'e' || *string
== 'E') {
211 const XML_Char
*expTail
;
212 exponent
= strToI (string
+1, &expTail
, 10);
213 if (expTail
== string
+1)
221 /* scale of the first digit */
222 scale
= sign
* (float)pow (10.0, (double)(pointPos
-1 + exponent
));
224 /* second pass: parse digits */
226 if (*string
!= '.') {
227 assert (*string
>= '0' && *string
<= '9');
228 result
+= scale
* (float)(*string
- '0');
233 } while (nDigits
> 0);
238 /** \brief Parse a value of a given type. */
240 parseValue(driOptionValue
*v
, driOptionType type
, const XML_Char
*string
)
242 const XML_Char
*tail
= NULL
;
243 /* skip leading white-space */
244 string
+= strspn (string
, " \f\n\r\t\v");
247 if (!strcmp (string
, "false")) {
250 } else if (!strcmp (string
, "true")) {
257 case DRI_ENUM
: /* enum is just a special integer */
259 v
->_int
= strToI (string
, &tail
, 0);
262 v
->_float
= strToF (string
, &tail
);
266 v
->_string
= strndup(string
, STRING_CONF_MAXLEN
);
271 return false; /* empty string (or containing only white-space) */
272 /* skip trailing white space */
274 tail
+= strspn (tail
, " \f\n\r\t\v");
276 return false; /* something left over that is not part of value */
281 /** \brief Parse a list of ranges of type info->type. */
283 parseRanges(driOptionInfo
*info
, const XML_Char
*string
)
285 XML_Char
*cp
, *range
;
287 driOptionRange
*ranges
;
289 XSTRDUP (cp
, string
);
290 /* pass 1: determine the number of ranges (number of commas + 1) */
292 for (nRanges
= 1; *range
; ++range
)
296 if ((ranges
= malloc(nRanges
*sizeof(driOptionRange
))) == NULL
) {
297 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
301 /* pass 2: parse all ranges into preallocated array */
303 for (i
= 0; i
< nRanges
; ++i
) {
306 end
= strchr (range
, ',');
309 sep
= strchr (range
, ':');
310 if (sep
) { /* non-empty interval */
312 if (!parseValue (&ranges
[i
].start
, info
->type
, range
) ||
313 !parseValue (&ranges
[i
].end
, info
->type
, sep
+1))
315 if (info
->type
== DRI_INT
&&
316 ranges
[i
].start
._int
> ranges
[i
].end
._int
)
318 if (info
->type
== DRI_FLOAT
&&
319 ranges
[i
].start
._float
> ranges
[i
].end
._float
)
321 } else { /* empty interval */
322 if (!parseValue (&ranges
[i
].start
, info
->type
, range
))
324 ranges
[i
].end
= ranges
[i
].start
;
336 assert (range
== NULL
);
338 info
->nRanges
= nRanges
;
339 info
->ranges
= ranges
;
343 /** \brief Check if a value is in one of info->ranges. */
345 checkValue(const driOptionValue
*v
, const driOptionInfo
*info
)
348 assert (info
->type
!= DRI_BOOL
); /* should be caught by the parser */
349 if (info
->nRanges
== 0)
351 switch (info
->type
) {
352 case DRI_ENUM
: /* enum is just a special integer */
354 for (i
= 0; i
< info
->nRanges
; ++i
)
355 if (v
->_int
>= info
->ranges
[i
].start
._int
&&
356 v
->_int
<= info
->ranges
[i
].end
._int
)
360 for (i
= 0; i
< info
->nRanges
; ++i
)
361 if (v
->_float
>= info
->ranges
[i
].start
._float
&&
362 v
->_float
<= info
->ranges
[i
].end
._float
)
368 assert (0); /* should never happen */
374 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
377 * Is called from the drivers.
379 * \param f \c printf like format string.
382 __driUtilMessage(const char *f
, ...)
385 const char *libgl_debug
;
387 libgl_debug
=getenv("LIBGL_DEBUG");
388 if (libgl_debug
&& !strstr(libgl_debug
, "quiet")) {
389 fprintf(stderr
, "libGL: ");
391 vfprintf(stderr
, f
, args
);
393 fprintf(stderr
, "\n");
397 /** \brief Output a warning message. */
398 #define XML_WARNING1(msg) do {\
399 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
400 (int) XML_GetCurrentLineNumber(data->parser), \
401 (int) XML_GetCurrentColumnNumber(data->parser)); \
403 #define XML_WARNING(msg, ...) do { \
404 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
405 (int) XML_GetCurrentLineNumber(data->parser), \
406 (int) XML_GetCurrentColumnNumber(data->parser), \
409 /** \brief Output an error message. */
410 #define XML_ERROR1(msg) do { \
411 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
412 (int) XML_GetCurrentLineNumber(data->parser), \
413 (int) XML_GetCurrentColumnNumber(data->parser)); \
415 #define XML_ERROR(msg, ...) do { \
416 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
417 (int) XML_GetCurrentLineNumber(data->parser), \
418 (int) XML_GetCurrentColumnNumber(data->parser), \
421 /** \brief Output a fatal error message and abort. */
422 #define XML_FATAL1(msg) do { \
423 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
425 (int) XML_GetCurrentLineNumber(data->parser), \
426 (int) XML_GetCurrentColumnNumber(data->parser)); \
429 #define XML_FATAL(msg, ...) do { \
430 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
432 (int) XML_GetCurrentLineNumber(data->parser), \
433 (int) XML_GetCurrentColumnNumber(data->parser), \
438 /** \brief Parser context for __driConfigOptions. */
442 driOptionCache
*cache
;
451 /** \brief Elements in __driConfigOptions. */
453 OI_DESCRIPTION
= 0, OI_DRIINFO
, OI_ENUM
, OI_OPTION
, OI_SECTION
, OI_COUNT
455 static const XML_Char
*OptInfoElems
[] = {
456 "description", "driinfo", "enum", "option", "section"
459 /** \brief Parse attributes of an enum element.
461 * We're not actually interested in the data. Just make sure this is ok
462 * for external configuration tools.
465 parseEnumAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
468 const XML_Char
*value
= NULL
, *text
= NULL
;
470 uint32_t opt
= data
->curOption
;
471 for (i
= 0; attr
[i
]; i
+= 2) {
472 if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
473 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
474 else XML_FATAL("illegal enum attribute: %s.", attr
[i
]);
476 if (!value
) XML_FATAL1 ("value attribute missing in enum.");
477 if (!text
) XML_FATAL1 ("text attribute missing in enum.");
478 if (!parseValue (&v
, data
->cache
->info
[opt
].type
, value
))
479 XML_FATAL ("illegal enum value: %s.", value
);
480 if (!checkValue (&v
, &data
->cache
->info
[opt
]))
481 XML_FATAL ("enum value out of valid range: %s.", value
);
484 /** \brief Parse attributes of a description element.
486 * We're not actually interested in the data. Just make sure this is ok
487 * for external configuration tools.
490 parseDescAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
493 const XML_Char
*lang
= NULL
, *text
= NULL
;
494 for (i
= 0; attr
[i
]; i
+= 2) {
495 if (!strcmp (attr
[i
], "lang")) lang
= attr
[i
+1];
496 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
497 else XML_FATAL("illegal description attribute: %s.", attr
[i
]);
499 if (!lang
) XML_FATAL1 ("lang attribute missing in description.");
500 if (!text
) XML_FATAL1 ("text attribute missing in description.");
503 /** \brief Parse attributes of an option element. */
505 parseOptInfoAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
507 enum OptAttr
{OA_DEFAULT
= 0, OA_NAME
, OA_TYPE
, OA_VALID
, OA_COUNT
};
508 static const XML_Char
*optAttr
[] = {"default", "name", "type", "valid"};
509 const XML_Char
*attrVal
[OA_COUNT
] = {NULL
, NULL
, NULL
, NULL
};
510 const char *defaultVal
;
511 driOptionCache
*cache
= data
->cache
;
513 for (i
= 0; attr
[i
]; i
+= 2) {
514 uint32_t attrName
= bsearchStr (attr
[i
], optAttr
, OA_COUNT
);
515 if (attrName
>= OA_COUNT
)
516 XML_FATAL ("illegal option attribute: %s", attr
[i
]);
517 attrVal
[attrName
] = attr
[i
+1];
519 if (!attrVal
[OA_NAME
]) XML_FATAL1 ("name attribute missing in option.");
520 if (!attrVal
[OA_TYPE
]) XML_FATAL1 ("type attribute missing in option.");
521 if (!attrVal
[OA_DEFAULT
]) XML_FATAL1 ("default attribute missing in option.");
523 opt
= findOption (cache
, attrVal
[OA_NAME
]);
524 if (cache
->info
[opt
].name
)
525 XML_FATAL ("option %s redefined.", attrVal
[OA_NAME
]);
526 data
->curOption
= opt
;
528 XSTRDUP (cache
->info
[opt
].name
, attrVal
[OA_NAME
]);
530 if (!strcmp (attrVal
[OA_TYPE
], "bool"))
531 cache
->info
[opt
].type
= DRI_BOOL
;
532 else if (!strcmp (attrVal
[OA_TYPE
], "enum"))
533 cache
->info
[opt
].type
= DRI_ENUM
;
534 else if (!strcmp (attrVal
[OA_TYPE
], "int"))
535 cache
->info
[opt
].type
= DRI_INT
;
536 else if (!strcmp (attrVal
[OA_TYPE
], "float"))
537 cache
->info
[opt
].type
= DRI_FLOAT
;
538 else if (!strcmp (attrVal
[OA_TYPE
], "string"))
539 cache
->info
[opt
].type
= DRI_STRING
;
541 XML_FATAL ("illegal type in option: %s.", attrVal
[OA_TYPE
]);
543 defaultVal
= getenv (cache
->info
[opt
].name
);
544 if (defaultVal
!= NULL
) {
545 /* don't use XML_WARNING, we want the user to see this! */
547 "ATTENTION: default value of option %s overridden by environment.\n",
548 cache
->info
[opt
].name
);
550 defaultVal
= attrVal
[OA_DEFAULT
];
551 if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, defaultVal
))
552 XML_FATAL ("illegal default value for %s: %s.", cache
->info
[opt
].name
, defaultVal
);
554 if (attrVal
[OA_VALID
]) {
555 if (cache
->info
[opt
].type
== DRI_BOOL
)
556 XML_FATAL1 ("boolean option with valid attribute.");
557 if (!parseRanges (&cache
->info
[opt
], attrVal
[OA_VALID
]))
558 XML_FATAL ("illegal valid attribute: %s.", attrVal
[OA_VALID
]);
559 if (!checkValue (&cache
->values
[opt
], &cache
->info
[opt
]))
560 XML_FATAL ("default value out of valid range '%s': %s.",
561 attrVal
[OA_VALID
], defaultVal
);
562 } else if (cache
->info
[opt
].type
== DRI_ENUM
) {
563 XML_FATAL1 ("valid attribute missing in option (mandatory for enums).");
565 cache
->info
[opt
].nRanges
= 0;
566 cache
->info
[opt
].ranges
= NULL
;
570 /** \brief Handler for start element events. */
572 optInfoStartElem(void *userData
, const XML_Char
*name
, const XML_Char
**attr
)
574 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
575 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
579 XML_FATAL1 ("nested <driinfo> elements.");
581 XML_FATAL1 ("attributes specified on <driinfo> element.");
582 data
->inDriInfo
= true;
585 if (!data
->inDriInfo
)
586 XML_FATAL1 ("<section> must be inside <driinfo>.");
588 XML_FATAL1 ("nested <section> elements.");
590 XML_FATAL1 ("attributes specified on <section> element.");
591 data
->inSection
= true;
594 if (!data
->inSection
&& !data
->inOption
)
595 XML_FATAL1 ("<description> must be inside <description> or <option.");
597 XML_FATAL1 ("nested <description> elements.");
599 parseDescAttr (data
, attr
);
602 if (!data
->inSection
)
603 XML_FATAL1 ("<option> must be inside <section>.");
605 XML_FATAL1 ("<option> nested in <description> element.");
607 XML_FATAL1 ("nested <option> elements.");
608 data
->inOption
= true;
609 parseOptInfoAttr (data
, attr
);
612 if (!(data
->inOption
&& data
->inDesc
))
613 XML_FATAL1 ("<enum> must be inside <option> and <description>.");
615 XML_FATAL1 ("nested <enum> elements.");
617 parseEnumAttr (data
, attr
);
620 XML_FATAL ("unknown element: %s.", name
);
624 /** \brief Handler for end element events. */
626 optInfoEndElem(void *userData
, const XML_Char
*name
)
628 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
629 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
632 data
->inDriInfo
= false;
635 data
->inSection
= false;
638 data
->inDesc
= false;
641 data
->inOption
= false;
644 data
->inEnum
= false;
647 assert (0); /* should have been caught by StartElem */
652 driParseOptionInfo(driOptionCache
*info
, const char *configOptions
)
656 struct OptInfoData userData
;
657 struct OptInfoData
*data
= &userData
;
659 /* Make the hash table big enough to fit more than the maximum number of
660 * config options we've ever seen in a driver.
663 info
->info
= calloc(1 << info
->tableSize
, sizeof (driOptionInfo
));
664 info
->values
= calloc(1 << info
->tableSize
, sizeof (driOptionValue
));
665 if (info
->info
== NULL
|| info
->values
== NULL
) {
666 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
670 p
= XML_ParserCreate ("UTF-8"); /* always UTF-8 */
671 XML_SetElementHandler (p
, optInfoStartElem
, optInfoEndElem
);
672 XML_SetUserData (p
, data
);
674 userData
.name
= "__driConfigOptions";
676 userData
.cache
= info
;
677 userData
.inDriInfo
= false;
678 userData
.inSection
= false;
679 userData
.inDesc
= false;
680 userData
.inOption
= false;
681 userData
.inEnum
= false;
682 userData
.curOption
= -1;
684 status
= XML_Parse (p
, configOptions
, strlen (configOptions
), 1);
686 XML_FATAL ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
691 /** \brief Parser context for configuration files. */
695 driOptionCache
*cache
;
697 const char *driverName
, *execName
;
698 const char *kernelDriverName
;
699 uint32_t ignoringDevice
;
700 uint32_t ignoringApp
;
707 /** \brief Elements in configuration files. */
709 OC_APPLICATION
= 0, OC_DEVICE
, OC_DRICONF
, OC_OPTION
, OC_COUNT
711 static const XML_Char
*OptConfElems
[] = {
712 [OC_APPLICATION
] = "application",
713 [OC_DEVICE
] = "device",
714 [OC_DRICONF
] = "driconf",
715 [OC_OPTION
] = "option",
718 /** \brief Parse attributes of a device element. */
720 parseDeviceAttr(struct OptConfData
*data
, const XML_Char
**attr
)
723 const XML_Char
*driver
= NULL
, *screen
= NULL
, *kernel
= NULL
;
724 for (i
= 0; attr
[i
]; i
+= 2) {
725 if (!strcmp (attr
[i
], "driver")) driver
= attr
[i
+1];
726 else if (!strcmp (attr
[i
], "screen")) screen
= attr
[i
+1];
727 else if (!strcmp (attr
[i
], "kernel_driver")) kernel
= attr
[i
+1];
728 else XML_WARNING("unknown device attribute: %s.", attr
[i
]);
730 if (driver
&& strcmp (driver
, data
->driverName
))
731 data
->ignoringDevice
= data
->inDevice
;
732 else if (kernel
&& (!data
->kernelDriverName
|| strcmp (kernel
, data
->kernelDriverName
)))
733 data
->ignoringDevice
= data
->inDevice
;
735 driOptionValue screenNum
;
736 if (!parseValue (&screenNum
, DRI_INT
, screen
))
737 XML_WARNING("illegal screen number: %s.", screen
);
738 else if (screenNum
._int
!= data
->screenNum
)
739 data
->ignoringDevice
= data
->inDevice
;
743 /** \brief Parse attributes of an application element. */
745 parseAppAttr(struct OptConfData
*data
, const XML_Char
**attr
)
748 const XML_Char
*exec
= NULL
;
749 for (i
= 0; attr
[i
]; i
+= 2) {
750 if (!strcmp (attr
[i
], "name")) /* not needed here */;
751 else if (!strcmp (attr
[i
], "executable")) exec
= attr
[i
+1];
752 else XML_WARNING("unknown application attribute: %s.", attr
[i
]);
754 if (exec
&& strcmp (exec
, data
->execName
))
755 data
->ignoringApp
= data
->inApp
;
758 /** \brief Parse attributes of an option element. */
760 parseOptConfAttr(struct OptConfData
*data
, const XML_Char
**attr
)
763 const XML_Char
*name
= NULL
, *value
= NULL
;
764 for (i
= 0; attr
[i
]; i
+= 2) {
765 if (!strcmp (attr
[i
], "name")) name
= attr
[i
+1];
766 else if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
767 else XML_WARNING("unknown option attribute: %s.", attr
[i
]);
769 if (!name
) XML_WARNING1 ("name attribute missing in option.");
770 if (!value
) XML_WARNING1 ("value attribute missing in option.");
772 driOptionCache
*cache
= data
->cache
;
773 uint32_t opt
= findOption (cache
, name
);
774 if (cache
->info
[opt
].name
== NULL
)
775 /* don't use XML_WARNING, drirc defines options for all drivers,
776 * but not all drivers support them */
778 else if (getenv (cache
->info
[opt
].name
))
779 /* don't use XML_WARNING, we want the user to see this! */
780 fprintf (stderr
, "ATTENTION: option value of option %s ignored.\n",
781 cache
->info
[opt
].name
);
782 else if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, value
))
783 XML_WARNING ("illegal option value: %s.", value
);
787 /** \brief Handler for start element events. */
789 optConfStartElem(void *userData
, const XML_Char
*name
,
790 const XML_Char
**attr
)
792 struct OptConfData
*data
= (struct OptConfData
*)userData
;
793 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
797 XML_WARNING1 ("nested <driconf> elements.");
799 XML_WARNING1 ("attributes specified on <driconf> element.");
803 if (!data
->inDriConf
)
804 XML_WARNING1 ("<device> should be inside <driconf>.");
806 XML_WARNING1 ("nested <device> elements.");
808 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
809 parseDeviceAttr (data
, attr
);
813 XML_WARNING1 ("<application> should be inside <device>.");
815 XML_WARNING1 ("nested <application> elements.");
817 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
818 parseAppAttr (data
, attr
);
822 XML_WARNING1 ("<option> should be inside <application>.");
824 XML_WARNING1 ("nested <option> elements.");
826 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
827 parseOptConfAttr (data
, attr
);
830 XML_WARNING ("unknown element: %s.", name
);
834 /** \brief Handler for end element events. */
836 optConfEndElem(void *userData
, const XML_Char
*name
)
838 struct OptConfData
*data
= (struct OptConfData
*)userData
;
839 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
845 if (data
->inDevice
-- == data
->ignoringDevice
)
846 data
->ignoringDevice
= 0;
849 if (data
->inApp
-- == data
->ignoringApp
)
850 data
->ignoringApp
= 0;
856 /* unknown element, warning was produced on start tag */;
860 /** \brief Initialize an option cache based on info */
862 initOptionCache(driOptionCache
*cache
, const driOptionCache
*info
)
864 unsigned i
, size
= 1 << info
->tableSize
;
865 cache
->info
= info
->info
;
866 cache
->tableSize
= info
->tableSize
;
867 cache
->values
= malloc((1<<info
->tableSize
) * sizeof (driOptionValue
));
868 if (cache
->values
== NULL
) {
869 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
872 memcpy (cache
->values
, info
->values
,
873 (1<<info
->tableSize
) * sizeof (driOptionValue
));
874 for (i
= 0; i
< size
; ++i
) {
875 if (cache
->info
[i
].type
== DRI_STRING
)
876 XSTRDUP(cache
->values
[i
]._string
, info
->values
[i
]._string
);
881 _parseOneConfigFile(XML_Parser p
)
883 #define BUF_SIZE 0x1000
884 struct OptConfData
*data
= (struct OptConfData
*)XML_GetUserData (p
);
888 if ((fd
= open (data
->name
, O_RDONLY
)) == -1) {
889 __driUtilMessage ("Can't open configuration file %s: %s.",
890 data
->name
, strerror (errno
));
896 void *buffer
= XML_GetBuffer (p
, BUF_SIZE
);
898 __driUtilMessage ("Can't allocate parser buffer.");
901 bytesRead
= read (fd
, buffer
, BUF_SIZE
);
902 if (bytesRead
== -1) {
903 __driUtilMessage ("Error reading from configuration file %s: %s.",
904 data
->name
, strerror (errno
));
907 status
= XML_ParseBuffer (p
, bytesRead
, bytesRead
== 0);
909 XML_ERROR ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
920 /** \brief Parse the named configuration file */
922 parseOneConfigFile(struct OptConfData
*data
, const char *filename
)
926 p
= XML_ParserCreate (NULL
); /* use encoding specified by file */
927 XML_SetElementHandler (p
, optConfStartElem
, optConfEndElem
);
928 XML_SetUserData (p
, data
);
930 data
->name
= filename
;
931 data
->ignoringDevice
= 0;
932 data
->ignoringApp
= 0;
938 _parseOneConfigFile (p
);
943 scandir_filter(const struct dirent
*ent
)
945 #ifndef DT_REG /* systems without d_type in dirent results */
948 if ((lstat(ent
->d_name
, &st
) != 0) ||
949 (!S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
)))
952 if (ent
->d_type
!= DT_REG
&& ent
->d_type
!= DT_LNK
)
956 if (fnmatch("*.conf", ent
->d_name
, 0))
962 /** \brief Parse configuration files in a directory */
964 parseConfigDir(struct OptConfData
*data
, const char *dirname
)
967 struct dirent
**entries
= NULL
;
969 count
= scandir(dirname
, &entries
, scandir_filter
, alphasort
);
973 for (i
= 0; i
< count
; i
++) {
974 char filename
[PATH_MAX
];
976 snprintf(filename
, PATH_MAX
, "%s/%s", dirname
, entries
[i
]->d_name
);
979 parseOneConfigFile(data
, filename
);
986 #define SYSCONFDIR "/etc"
990 #define DATADIR "/usr/share"
994 driParseConfigFiles(driOptionCache
*cache
, const driOptionCache
*info
,
995 int screenNum
, const char *driverName
,
996 const char *kernelDriverName
)
999 struct OptConfData userData
;
1001 initOptionCache (cache
, info
);
1003 userData
.cache
= cache
;
1004 userData
.screenNum
= screenNum
;
1005 userData
.driverName
= driverName
;
1006 userData
.kernelDriverName
= kernelDriverName
;
1007 userData
.execName
= util_get_process_name();
1009 parseConfigDir(&userData
, DATADIR
"/drirc.d");
1010 parseOneConfigFile(&userData
, SYSCONFDIR
"/drirc");
1012 if ((home
= getenv ("HOME"))) {
1013 char filename
[PATH_MAX
];
1015 snprintf(filename
, PATH_MAX
, "%s/.drirc", home
);
1016 parseOneConfigFile(&userData
, filename
);
1021 driDestroyOptionInfo(driOptionCache
*info
)
1023 driDestroyOptionCache(info
);
1025 uint32_t i
, size
= 1 << info
->tableSize
;
1026 for (i
= 0; i
< size
; ++i
) {
1027 if (info
->info
[i
].name
) {
1028 free(info
->info
[i
].name
);
1029 free(info
->info
[i
].ranges
);
1037 driDestroyOptionCache(driOptionCache
*cache
)
1040 unsigned i
, size
= 1 << cache
->tableSize
;
1041 for (i
= 0; i
< size
; ++i
) {
1042 if (cache
->info
[i
].type
== DRI_STRING
)
1043 free(cache
->values
[i
]._string
);
1046 free(cache
->values
);
1050 driCheckOption(const driOptionCache
*cache
, const char *name
,
1053 uint32_t i
= findOption (cache
, name
);
1054 return cache
->info
[i
].name
!= NULL
&& cache
->info
[i
].type
== type
;
1058 driQueryOptionb(const driOptionCache
*cache
, const char *name
)
1060 uint32_t i
= findOption (cache
, name
);
1061 /* make sure the option is defined and has the correct type */
1062 assert (cache
->info
[i
].name
!= NULL
);
1063 assert (cache
->info
[i
].type
== DRI_BOOL
);
1064 return cache
->values
[i
]._bool
;
1068 driQueryOptioni(const driOptionCache
*cache
, const char *name
)
1070 uint32_t i
= findOption (cache
, name
);
1071 /* make sure the option is defined and has the correct type */
1072 assert (cache
->info
[i
].name
!= NULL
);
1073 assert (cache
->info
[i
].type
== DRI_INT
|| cache
->info
[i
].type
== DRI_ENUM
);
1074 return cache
->values
[i
]._int
;
1078 driQueryOptionf(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_FLOAT
);
1084 return cache
->values
[i
]._float
;
1088 driQueryOptionstr(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_STRING
);
1094 return cache
->values
[i
]._string
;