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
45 #include "xmlconfig.h"
46 #include "u_process.h"
48 /* For systems like Hurd */
53 /** \brief Find an option in an option cache with the name as key */
55 findOption(const driOptionCache
*cache
, const char *name
)
57 uint32_t len
= strlen (name
);
58 uint32_t size
= 1 << cache
->tableSize
, mask
= size
- 1;
62 /* compute a hash from the variable length name */
63 for (i
= 0, shift
= 0; i
< len
; ++i
, shift
= (shift
+8) & 31)
64 hash
+= (uint32_t)name
[i
] << shift
;
66 hash
= (hash
>> (16-cache
->tableSize
/2)) & mask
;
68 /* this is just the starting point of the linear search for the option */
69 for (i
= 0; i
< size
; ++i
, hash
= (hash
+1) & mask
) {
70 /* if we hit an empty entry then the option is not defined (yet) */
71 if (cache
->info
[hash
].name
== 0)
73 else if (!strcmp (name
, cache
->info
[hash
].name
))
76 /* this assertion fails if the hash table is full */
82 /** \brief Like strdup but using malloc and with error checking. */
83 #define XSTRDUP(dest,source) do { \
84 uint32_t len = strlen (source); \
85 if (!(dest = malloc(len+1))) { \
86 fprintf (stderr, "%s: %d: out of memory.\n", __FILE__, __LINE__); \
89 memcpy (dest, source, len+1); \
92 static int compare (const void *a
, const void *b
) {
93 return strcmp (*(char *const*)a
, *(char *const*)b
);
95 /** \brief Binary search in a string array. */
97 bsearchStr (const XML_Char
*name
, const XML_Char
*elems
[], uint32_t count
)
99 const XML_Char
**found
;
100 found
= bsearch (&name
, elems
, count
, sizeof (XML_Char
*), compare
);
102 return found
- elems
;
107 /** \brief Locale-independent integer parser.
109 * Works similar to strtol. Leading space is NOT skipped. The input
110 * number may have an optional sign. Radix is specified by base. If
111 * base is 0 then decimal is assumed unless the input number is
112 * prefixed by 0x or 0X for hexadecimal or 0 for octal. After
113 * returning tail points to the first character that is not part of
114 * the integer number. If no number was found then tail points to the
115 * start of the input string. */
117 strToI(const XML_Char
*string
, const XML_Char
**tail
, int base
)
119 int radix
= base
== 0 ? 10 : base
;
122 bool numberFound
= false;
123 const XML_Char
*start
= string
;
125 assert (radix
>= 2 && radix
<= 36);
127 if (*string
== '-') {
130 } else if (*string
== '+')
132 if (base
== 0 && *string
== '0') {
134 if (*(string
+1) == 'x' || *(string
+1) == 'X') {
145 if (*string
>= '0' && *string
< '0' + radix
)
146 digit
= *string
- '0';
148 if (*string
>= '0' && *string
<= '9')
149 digit
= *string
- '0';
150 else if (*string
>= 'a' && *string
< 'a' + radix
- 10)
151 digit
= *string
- 'a' + 10;
152 else if (*string
>= 'A' && *string
< 'A' + radix
- 10)
153 digit
= *string
- 'A' + 10;
157 result
= radix
*result
+ digit
;
162 *tail
= numberFound
? string
: start
;
163 return sign
* result
;
166 /** \brief Locale-independent floating-point parser.
168 * Works similar to strtod. Leading space is NOT skipped. The input
169 * number may have an optional sign. '.' is interpreted as decimal
170 * point and may occur at most once. Optionally the number may end in
171 * [eE]<exponent>, where <exponent> is an integer as recognized by
172 * strToI. In that case the result is number * 10^exponent. After
173 * returning tail points to the first character that is not part of
174 * the floating point number. If no number was found then tail points
175 * to the start of the input string.
177 * Uses two passes for maximum accuracy. */
179 strToF(const XML_Char
*string
, const XML_Char
**tail
)
181 int nDigits
= 0, pointPos
, exponent
;
182 float sign
= 1.0f
, result
= 0.0f
, scale
;
183 const XML_Char
*start
= string
, *numStart
;
186 if (*string
== '-') {
189 } else if (*string
== '+')
192 /* first pass: determine position of decimal point, number of
193 * digits, exponent and the end of the number. */
195 while (*string
>= '0' && *string
<= '9') {
200 if (*string
== '.') {
202 while (*string
>= '0' && *string
<= '9') {
208 /* no digits, no number */
213 if (*string
== 'e' || *string
== 'E') {
214 const XML_Char
*expTail
;
215 exponent
= strToI (string
+1, &expTail
, 10);
216 if (expTail
== string
+1)
224 /* scale of the first digit */
225 scale
= sign
* (float)pow (10.0, (double)(pointPos
-1 + exponent
));
227 /* second pass: parse digits */
229 if (*string
!= '.') {
230 assert (*string
>= '0' && *string
<= '9');
231 result
+= scale
* (float)(*string
- '0');
236 } while (nDigits
> 0);
241 /** \brief Parse a value of a given type. */
243 parseValue(driOptionValue
*v
, driOptionType type
, const XML_Char
*string
)
245 const XML_Char
*tail
= NULL
;
246 /* skip leading white-space */
247 string
+= strspn (string
, " \f\n\r\t\v");
250 if (!strcmp (string
, "false")) {
253 } else if (!strcmp (string
, "true")) {
260 case DRI_ENUM
: /* enum is just a special integer */
262 v
->_int
= strToI (string
, &tail
, 0);
265 v
->_float
= strToF (string
, &tail
);
269 v
->_string
= strndup(string
, STRING_CONF_MAXLEN
);
274 return false; /* empty string (or containing only white-space) */
275 /* skip trailing white space */
277 tail
+= strspn (tail
, " \f\n\r\t\v");
279 return false; /* something left over that is not part of value */
284 /** \brief Parse a list of ranges of type info->type. */
286 parseRanges(driOptionInfo
*info
, const XML_Char
*string
)
288 XML_Char
*cp
, *range
;
290 driOptionRange
*ranges
;
292 XSTRDUP (cp
, string
);
293 /* pass 1: determine the number of ranges (number of commas + 1) */
295 for (nRanges
= 1; *range
; ++range
)
299 if ((ranges
= malloc(nRanges
*sizeof(driOptionRange
))) == NULL
) {
300 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
304 /* pass 2: parse all ranges into preallocated array */
306 for (i
= 0; i
< nRanges
; ++i
) {
309 end
= strchr (range
, ',');
312 sep
= strchr (range
, ':');
313 if (sep
) { /* non-empty interval */
315 if (!parseValue (&ranges
[i
].start
, info
->type
, range
) ||
316 !parseValue (&ranges
[i
].end
, info
->type
, sep
+1))
318 if (info
->type
== DRI_INT
&&
319 ranges
[i
].start
._int
> ranges
[i
].end
._int
)
321 if (info
->type
== DRI_FLOAT
&&
322 ranges
[i
].start
._float
> ranges
[i
].end
._float
)
324 } else { /* empty interval */
325 if (!parseValue (&ranges
[i
].start
, info
->type
, range
))
327 ranges
[i
].end
= ranges
[i
].start
;
339 assert (range
== NULL
);
341 info
->nRanges
= nRanges
;
342 info
->ranges
= ranges
;
346 /** \brief Check if a value is in one of info->ranges. */
348 checkValue(const driOptionValue
*v
, const driOptionInfo
*info
)
351 assert (info
->type
!= DRI_BOOL
); /* should be caught by the parser */
352 if (info
->nRanges
== 0)
354 switch (info
->type
) {
355 case DRI_ENUM
: /* enum is just a special integer */
357 for (i
= 0; i
< info
->nRanges
; ++i
)
358 if (v
->_int
>= info
->ranges
[i
].start
._int
&&
359 v
->_int
<= info
->ranges
[i
].end
._int
)
363 for (i
= 0; i
< info
->nRanges
; ++i
)
364 if (v
->_float
>= info
->ranges
[i
].start
._float
&&
365 v
->_float
<= info
->ranges
[i
].end
._float
)
371 assert (0); /* should never happen */
377 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
380 * Is called from the drivers.
382 * \param f \c printf like format string.
385 __driUtilMessage(const char *f
, ...)
388 const char *libgl_debug
;
390 libgl_debug
=getenv("LIBGL_DEBUG");
391 if (libgl_debug
&& !strstr(libgl_debug
, "quiet")) {
392 fprintf(stderr
, "libGL: ");
394 vfprintf(stderr
, f
, args
);
396 fprintf(stderr
, "\n");
400 /** \brief Output a warning message. */
401 #define XML_WARNING1(msg) do {\
402 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
403 (int) XML_GetCurrentLineNumber(data->parser), \
404 (int) XML_GetCurrentColumnNumber(data->parser)); \
406 #define XML_WARNING(msg, ...) do { \
407 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
408 (int) XML_GetCurrentLineNumber(data->parser), \
409 (int) XML_GetCurrentColumnNumber(data->parser), \
412 /** \brief Output an error message. */
413 #define XML_ERROR1(msg) do { \
414 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
415 (int) XML_GetCurrentLineNumber(data->parser), \
416 (int) XML_GetCurrentColumnNumber(data->parser)); \
418 #define XML_ERROR(msg, ...) do { \
419 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
420 (int) XML_GetCurrentLineNumber(data->parser), \
421 (int) XML_GetCurrentColumnNumber(data->parser), \
424 /** \brief Output a fatal error message and abort. */
425 #define XML_FATAL1(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)); \
432 #define XML_FATAL(msg, ...) do { \
433 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
435 (int) XML_GetCurrentLineNumber(data->parser), \
436 (int) XML_GetCurrentColumnNumber(data->parser), \
441 /** \brief Parser context for __driConfigOptions. */
445 driOptionCache
*cache
;
454 /** \brief Elements in __driConfigOptions. */
456 OI_DESCRIPTION
= 0, OI_DRIINFO
, OI_ENUM
, OI_OPTION
, OI_SECTION
, OI_COUNT
458 static const XML_Char
*OptInfoElems
[] = {
459 "description", "driinfo", "enum", "option", "section"
462 /** \brief Parse attributes of an enum element.
464 * We're not actually interested in the data. Just make sure this is ok
465 * for external configuration tools.
468 parseEnumAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
471 const XML_Char
*value
= NULL
, *text
= NULL
;
473 uint32_t opt
= data
->curOption
;
474 for (i
= 0; attr
[i
]; i
+= 2) {
475 if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
476 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
477 else XML_FATAL("illegal enum attribute: %s.", attr
[i
]);
479 if (!value
) XML_FATAL1 ("value attribute missing in enum.");
480 if (!text
) XML_FATAL1 ("text attribute missing in enum.");
481 if (!parseValue (&v
, data
->cache
->info
[opt
].type
, value
))
482 XML_FATAL ("illegal enum value: %s.", value
);
483 if (!checkValue (&v
, &data
->cache
->info
[opt
]))
484 XML_FATAL ("enum value out of valid range: %s.", value
);
487 /** \brief Parse attributes of a description element.
489 * We're not actually interested in the data. Just make sure this is ok
490 * for external configuration tools.
493 parseDescAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
496 const XML_Char
*lang
= NULL
, *text
= NULL
;
497 for (i
= 0; attr
[i
]; i
+= 2) {
498 if (!strcmp (attr
[i
], "lang")) lang
= attr
[i
+1];
499 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
500 else XML_FATAL("illegal description attribute: %s.", attr
[i
]);
502 if (!lang
) XML_FATAL1 ("lang attribute missing in description.");
503 if (!text
) XML_FATAL1 ("text attribute missing in description.");
506 /** \brief Parse attributes of an option element. */
508 parseOptInfoAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
510 enum OptAttr
{OA_DEFAULT
= 0, OA_NAME
, OA_TYPE
, OA_VALID
, OA_COUNT
};
511 static const XML_Char
*optAttr
[] = {"default", "name", "type", "valid"};
512 const XML_Char
*attrVal
[OA_COUNT
] = {NULL
, NULL
, NULL
, NULL
};
513 const char *defaultVal
;
514 driOptionCache
*cache
= data
->cache
;
516 for (i
= 0; attr
[i
]; i
+= 2) {
517 uint32_t attrName
= bsearchStr (attr
[i
], optAttr
, OA_COUNT
);
518 if (attrName
>= OA_COUNT
)
519 XML_FATAL ("illegal option attribute: %s", attr
[i
]);
520 attrVal
[attrName
] = attr
[i
+1];
522 if (!attrVal
[OA_NAME
]) XML_FATAL1 ("name attribute missing in option.");
523 if (!attrVal
[OA_TYPE
]) XML_FATAL1 ("type attribute missing in option.");
524 if (!attrVal
[OA_DEFAULT
]) XML_FATAL1 ("default attribute missing in option.");
526 opt
= findOption (cache
, attrVal
[OA_NAME
]);
527 if (cache
->info
[opt
].name
)
528 XML_FATAL ("option %s redefined.", attrVal
[OA_NAME
]);
529 data
->curOption
= opt
;
531 XSTRDUP (cache
->info
[opt
].name
, attrVal
[OA_NAME
]);
533 if (!strcmp (attrVal
[OA_TYPE
], "bool"))
534 cache
->info
[opt
].type
= DRI_BOOL
;
535 else if (!strcmp (attrVal
[OA_TYPE
], "enum"))
536 cache
->info
[opt
].type
= DRI_ENUM
;
537 else if (!strcmp (attrVal
[OA_TYPE
], "int"))
538 cache
->info
[opt
].type
= DRI_INT
;
539 else if (!strcmp (attrVal
[OA_TYPE
], "float"))
540 cache
->info
[opt
].type
= DRI_FLOAT
;
541 else if (!strcmp (attrVal
[OA_TYPE
], "string"))
542 cache
->info
[opt
].type
= DRI_STRING
;
544 XML_FATAL ("illegal type in option: %s.", attrVal
[OA_TYPE
]);
546 defaultVal
= getenv (cache
->info
[opt
].name
);
547 if (defaultVal
!= NULL
) {
548 /* don't use XML_WARNING, we want the user to see this! */
550 "ATTENTION: default value of option %s overridden by environment.\n",
551 cache
->info
[opt
].name
);
553 defaultVal
= attrVal
[OA_DEFAULT
];
554 if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, defaultVal
))
555 XML_FATAL ("illegal default value for %s: %s.", cache
->info
[opt
].name
, defaultVal
);
557 if (attrVal
[OA_VALID
]) {
558 if (cache
->info
[opt
].type
== DRI_BOOL
)
559 XML_FATAL1 ("boolean option with valid attribute.");
560 if (!parseRanges (&cache
->info
[opt
], attrVal
[OA_VALID
]))
561 XML_FATAL ("illegal valid attribute: %s.", attrVal
[OA_VALID
]);
562 if (!checkValue (&cache
->values
[opt
], &cache
->info
[opt
]))
563 XML_FATAL ("default value out of valid range '%s': %s.",
564 attrVal
[OA_VALID
], defaultVal
);
565 } else if (cache
->info
[opt
].type
== DRI_ENUM
) {
566 XML_FATAL1 ("valid attribute missing in option (mandatory for enums).");
568 cache
->info
[opt
].nRanges
= 0;
569 cache
->info
[opt
].ranges
= NULL
;
573 /** \brief Handler for start element events. */
575 optInfoStartElem(void *userData
, const XML_Char
*name
, const XML_Char
**attr
)
577 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
578 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
582 XML_FATAL1 ("nested <driinfo> elements.");
584 XML_FATAL1 ("attributes specified on <driinfo> element.");
585 data
->inDriInfo
= true;
588 if (!data
->inDriInfo
)
589 XML_FATAL1 ("<section> must be inside <driinfo>.");
591 XML_FATAL1 ("nested <section> elements.");
593 XML_FATAL1 ("attributes specified on <section> element.");
594 data
->inSection
= true;
597 if (!data
->inSection
&& !data
->inOption
)
598 XML_FATAL1 ("<description> must be inside <description> or <option.");
600 XML_FATAL1 ("nested <description> elements.");
602 parseDescAttr (data
, attr
);
605 if (!data
->inSection
)
606 XML_FATAL1 ("<option> must be inside <section>.");
608 XML_FATAL1 ("<option> nested in <description> element.");
610 XML_FATAL1 ("nested <option> elements.");
611 data
->inOption
= true;
612 parseOptInfoAttr (data
, attr
);
615 if (!(data
->inOption
&& data
->inDesc
))
616 XML_FATAL1 ("<enum> must be inside <option> and <description>.");
618 XML_FATAL1 ("nested <enum> elements.");
620 parseEnumAttr (data
, attr
);
623 XML_FATAL ("unknown element: %s.", name
);
627 /** \brief Handler for end element events. */
629 optInfoEndElem(void *userData
, const XML_Char
*name
)
631 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
632 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
635 data
->inDriInfo
= false;
638 data
->inSection
= false;
641 data
->inDesc
= false;
644 data
->inOption
= false;
647 data
->inEnum
= false;
650 assert (0); /* should have been caught by StartElem */
655 driParseOptionInfo(driOptionCache
*info
, const char *configOptions
)
659 struct OptInfoData userData
;
660 struct OptInfoData
*data
= &userData
;
662 /* Make the hash table big enough to fit more than the maximum number of
663 * config options we've ever seen in a driver.
666 info
->info
= calloc(1 << info
->tableSize
, sizeof (driOptionInfo
));
667 info
->values
= calloc(1 << info
->tableSize
, sizeof (driOptionValue
));
668 if (info
->info
== NULL
|| info
->values
== NULL
) {
669 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
673 p
= XML_ParserCreate ("UTF-8"); /* always UTF-8 */
674 XML_SetElementHandler (p
, optInfoStartElem
, optInfoEndElem
);
675 XML_SetUserData (p
, data
);
677 userData
.name
= "__driConfigOptions";
679 userData
.cache
= info
;
680 userData
.inDriInfo
= false;
681 userData
.inSection
= false;
682 userData
.inDesc
= false;
683 userData
.inOption
= false;
684 userData
.inEnum
= false;
685 userData
.curOption
= -1;
687 status
= XML_Parse (p
, configOptions
, strlen (configOptions
), 1);
689 XML_FATAL ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
694 /** \brief Parser context for configuration files. */
698 driOptionCache
*cache
;
700 const char *driverName
, *execName
;
701 const char *kernelDriverName
;
702 uint32_t ignoringDevice
;
703 uint32_t ignoringApp
;
710 /** \brief Elements in configuration files. */
712 OC_APPLICATION
= 0, OC_DEVICE
, OC_DRICONF
, OC_OPTION
, OC_COUNT
714 static const XML_Char
*OptConfElems
[] = {
715 [OC_APPLICATION
] = "application",
716 [OC_DEVICE
] = "device",
717 [OC_DRICONF
] = "driconf",
718 [OC_OPTION
] = "option",
721 /** \brief Parse attributes of a device element. */
723 parseDeviceAttr(struct OptConfData
*data
, const XML_Char
**attr
)
726 const XML_Char
*driver
= NULL
, *screen
= NULL
, *kernel
= NULL
;
727 for (i
= 0; attr
[i
]; i
+= 2) {
728 if (!strcmp (attr
[i
], "driver")) driver
= attr
[i
+1];
729 else if (!strcmp (attr
[i
], "screen")) screen
= attr
[i
+1];
730 else if (!strcmp (attr
[i
], "kernel_driver")) kernel
= attr
[i
+1];
731 else XML_WARNING("unknown device attribute: %s.", attr
[i
]);
733 if (driver
&& strcmp (driver
, data
->driverName
))
734 data
->ignoringDevice
= data
->inDevice
;
735 else if (kernel
&& (!data
->kernelDriverName
|| strcmp (kernel
, data
->kernelDriverName
)))
736 data
->ignoringDevice
= data
->inDevice
;
738 driOptionValue screenNum
;
739 if (!parseValue (&screenNum
, DRI_INT
, screen
))
740 XML_WARNING("illegal screen number: %s.", screen
);
741 else if (screenNum
._int
!= data
->screenNum
)
742 data
->ignoringDevice
= data
->inDevice
;
746 /** \brief Parse attributes of an application element. */
748 parseAppAttr(struct OptConfData
*data
, const XML_Char
**attr
)
751 const XML_Char
*exec
= NULL
;
752 for (i
= 0; attr
[i
]; i
+= 2) {
753 if (!strcmp (attr
[i
], "name")) /* not needed here */;
754 else if (!strcmp (attr
[i
], "executable")) exec
= attr
[i
+1];
755 else XML_WARNING("unknown application attribute: %s.", attr
[i
]);
757 if (exec
&& strcmp (exec
, data
->execName
))
758 data
->ignoringApp
= data
->inApp
;
761 /** \brief Parse attributes of an option element. */
763 parseOptConfAttr(struct OptConfData
*data
, const XML_Char
**attr
)
766 const XML_Char
*name
= NULL
, *value
= NULL
;
767 for (i
= 0; attr
[i
]; i
+= 2) {
768 if (!strcmp (attr
[i
], "name")) name
= attr
[i
+1];
769 else if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
770 else XML_WARNING("unknown option attribute: %s.", attr
[i
]);
772 if (!name
) XML_WARNING1 ("name attribute missing in option.");
773 if (!value
) XML_WARNING1 ("value attribute missing in option.");
775 driOptionCache
*cache
= data
->cache
;
776 uint32_t opt
= findOption (cache
, name
);
777 if (cache
->info
[opt
].name
== NULL
)
778 /* don't use XML_WARNING, drirc defines options for all drivers,
779 * but not all drivers support them */
781 else if (getenv (cache
->info
[opt
].name
))
782 /* don't use XML_WARNING, we want the user to see this! */
783 fprintf (stderr
, "ATTENTION: option value of option %s ignored.\n",
784 cache
->info
[opt
].name
);
785 else if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, value
))
786 XML_WARNING ("illegal option value: %s.", value
);
790 /** \brief Handler for start element events. */
792 optConfStartElem(void *userData
, const XML_Char
*name
,
793 const XML_Char
**attr
)
795 struct OptConfData
*data
= (struct OptConfData
*)userData
;
796 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
800 XML_WARNING1 ("nested <driconf> elements.");
802 XML_WARNING1 ("attributes specified on <driconf> element.");
806 if (!data
->inDriConf
)
807 XML_WARNING1 ("<device> should be inside <driconf>.");
809 XML_WARNING1 ("nested <device> elements.");
811 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
812 parseDeviceAttr (data
, attr
);
816 XML_WARNING1 ("<application> should be inside <device>.");
818 XML_WARNING1 ("nested <application> elements.");
820 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
821 parseAppAttr (data
, attr
);
825 XML_WARNING1 ("<option> should be inside <application>.");
827 XML_WARNING1 ("nested <option> elements.");
829 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
830 parseOptConfAttr (data
, attr
);
833 XML_WARNING ("unknown element: %s.", name
);
837 /** \brief Handler for end element events. */
839 optConfEndElem(void *userData
, const XML_Char
*name
)
841 struct OptConfData
*data
= (struct OptConfData
*)userData
;
842 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
848 if (data
->inDevice
-- == data
->ignoringDevice
)
849 data
->ignoringDevice
= 0;
852 if (data
->inApp
-- == data
->ignoringApp
)
853 data
->ignoringApp
= 0;
859 /* unknown element, warning was produced on start tag */;
863 /** \brief Initialize an option cache based on info */
865 initOptionCache(driOptionCache
*cache
, const driOptionCache
*info
)
867 unsigned i
, size
= 1 << info
->tableSize
;
868 cache
->info
= info
->info
;
869 cache
->tableSize
= info
->tableSize
;
870 cache
->values
= malloc((1<<info
->tableSize
) * sizeof (driOptionValue
));
871 if (cache
->values
== NULL
) {
872 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
875 memcpy (cache
->values
, info
->values
,
876 (1<<info
->tableSize
) * sizeof (driOptionValue
));
877 for (i
= 0; i
< size
; ++i
) {
878 if (cache
->info
[i
].type
== DRI_STRING
)
879 XSTRDUP(cache
->values
[i
]._string
, info
->values
[i
]._string
);
884 _parseOneConfigFile(XML_Parser p
)
886 #define BUF_SIZE 0x1000
887 struct OptConfData
*data
= (struct OptConfData
*)XML_GetUserData (p
);
891 if ((fd
= open (data
->name
, O_RDONLY
)) == -1) {
892 __driUtilMessage ("Can't open configuration file %s: %s.",
893 data
->name
, strerror (errno
));
899 void *buffer
= XML_GetBuffer (p
, BUF_SIZE
);
901 __driUtilMessage ("Can't allocate parser buffer.");
904 bytesRead
= read (fd
, buffer
, BUF_SIZE
);
905 if (bytesRead
== -1) {
906 __driUtilMessage ("Error reading from configuration file %s: %s.",
907 data
->name
, strerror (errno
));
910 status
= XML_ParseBuffer (p
, bytesRead
, bytesRead
== 0);
912 XML_ERROR ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
923 /** \brief Parse the named configuration file */
925 parseOneConfigFile(struct OptConfData
*data
, const char *filename
)
929 p
= XML_ParserCreate (NULL
); /* use encoding specified by file */
930 XML_SetElementHandler (p
, optConfStartElem
, optConfEndElem
);
931 XML_SetUserData (p
, data
);
933 data
->name
= filename
;
934 data
->ignoringDevice
= 0;
935 data
->ignoringApp
= 0;
941 _parseOneConfigFile (p
);
946 scandir_filter(const struct dirent
*ent
)
948 #ifndef DT_REG /* systems without d_type in dirent results */
951 if ((lstat(ent
->d_name
, &st
) != 0) ||
952 (!S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
)))
955 if (ent
->d_type
!= DT_REG
&& ent
->d_type
!= DT_LNK
)
959 if (fnmatch("*.conf", ent
->d_name
, 0))
965 /** \brief Parse configuration files in a directory */
967 parseConfigDir(struct OptConfData
*data
, const char *dirname
)
970 struct dirent
**entries
= NULL
;
972 count
= scandir(dirname
, &entries
, scandir_filter
, alphasort
);
976 for (i
= 0; i
< count
; i
++) {
977 char filename
[PATH_MAX
];
979 snprintf(filename
, PATH_MAX
, "%s/%s", dirname
, entries
[i
]->d_name
);
982 parseOneConfigFile(data
, filename
);
989 #define SYSCONFDIR "/etc"
993 #define DATADIR "/usr/share"
997 driParseConfigFiles(driOptionCache
*cache
, const driOptionCache
*info
,
998 int screenNum
, const char *driverName
,
999 const char *kernelDriverName
)
1002 struct OptConfData userData
;
1004 initOptionCache (cache
, info
);
1006 userData
.cache
= cache
;
1007 userData
.screenNum
= screenNum
;
1008 userData
.driverName
= driverName
;
1009 userData
.kernelDriverName
= kernelDriverName
;
1010 userData
.execName
= util_get_process_name();
1012 parseConfigDir(&userData
, DATADIR
"/drirc.d");
1013 parseOneConfigFile(&userData
, SYSCONFDIR
"/drirc");
1015 if ((home
= getenv ("HOME"))) {
1016 char filename
[PATH_MAX
];
1018 snprintf(filename
, PATH_MAX
, "%s/.drirc", home
);
1019 parseOneConfigFile(&userData
, filename
);
1024 driDestroyOptionInfo(driOptionCache
*info
)
1026 driDestroyOptionCache(info
);
1028 uint32_t i
, size
= 1 << info
->tableSize
;
1029 for (i
= 0; i
< size
; ++i
) {
1030 if (info
->info
[i
].name
) {
1031 free(info
->info
[i
].name
);
1032 free(info
->info
[i
].ranges
);
1040 driDestroyOptionCache(driOptionCache
*cache
)
1043 unsigned i
, size
= 1 << cache
->tableSize
;
1044 for (i
= 0; i
< size
; ++i
) {
1045 if (cache
->info
[i
].type
== DRI_STRING
)
1046 free(cache
->values
[i
]._string
);
1049 free(cache
->values
);
1053 driCheckOption(const driOptionCache
*cache
, const char *name
,
1056 uint32_t i
= findOption (cache
, name
);
1057 return cache
->info
[i
].name
!= NULL
&& cache
->info
[i
].type
== type
;
1061 driQueryOptionb(const driOptionCache
*cache
, const char *name
)
1063 uint32_t i
= findOption (cache
, name
);
1064 /* make sure the option is defined and has the correct type */
1065 assert (cache
->info
[i
].name
!= NULL
);
1066 assert (cache
->info
[i
].type
== DRI_BOOL
);
1067 return cache
->values
[i
]._bool
;
1071 driQueryOptioni(const driOptionCache
*cache
, const char *name
)
1073 uint32_t i
= findOption (cache
, name
);
1074 /* make sure the option is defined and has the correct type */
1075 assert (cache
->info
[i
].name
!= NULL
);
1076 assert (cache
->info
[i
].type
== DRI_INT
|| cache
->info
[i
].type
== DRI_ENUM
);
1077 return cache
->values
[i
]._int
;
1081 driQueryOptionf(const driOptionCache
*cache
, const char *name
)
1083 uint32_t i
= findOption (cache
, name
);
1084 /* make sure the option is defined and has the correct type */
1085 assert (cache
->info
[i
].name
!= NULL
);
1086 assert (cache
->info
[i
].type
== DRI_FLOAT
);
1087 return cache
->values
[i
]._float
;
1091 driQueryOptionstr(const driOptionCache
*cache
, const char *name
)
1093 uint32_t i
= findOption (cache
, name
);
1094 /* make sure the option is defined and has the correct type */
1095 assert (cache
->info
[i
].name
!= NULL
);
1096 assert (cache
->info
[i
].type
== DRI_STRING
);
1097 return cache
->values
[i
]._string
;