035d0296efe8879e60953fa91072fc808a921bee
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
40 #include "xmlconfig.h"
41 #include "u_process.h"
44 /** \brief Find an option in an option cache with the name as key */
46 findOption(const driOptionCache
*cache
, const char *name
)
48 uint32_t len
= strlen (name
);
49 uint32_t size
= 1 << cache
->tableSize
, mask
= size
- 1;
53 /* compute a hash from the variable length name */
54 for (i
= 0, shift
= 0; i
< len
; ++i
, shift
= (shift
+8) & 31)
55 hash
+= (uint32_t)name
[i
] << shift
;
57 hash
= (hash
>> (16-cache
->tableSize
/2)) & mask
;
59 /* this is just the starting point of the linear search for the option */
60 for (i
= 0; i
< size
; ++i
, hash
= (hash
+1) & mask
) {
61 /* if we hit an empty entry then the option is not defined (yet) */
62 if (cache
->info
[hash
].name
== 0)
64 else if (!strcmp (name
, cache
->info
[hash
].name
))
67 /* this assertion fails if the hash table is full */
73 /** \brief Like strdup but using malloc and with error checking. */
74 #define XSTRDUP(dest,source) do { \
75 uint32_t len = strlen (source); \
76 if (!(dest = malloc(len+1))) { \
77 fprintf (stderr, "%s: %d: out of memory.\n", __FILE__, __LINE__); \
80 memcpy (dest, source, len+1); \
83 static int compare (const void *a
, const void *b
) {
84 return strcmp (*(char *const*)a
, *(char *const*)b
);
86 /** \brief Binary search in a string array. */
88 bsearchStr (const XML_Char
*name
, const XML_Char
*elems
[], uint32_t count
)
90 const XML_Char
**found
;
91 found
= bsearch (&name
, elems
, count
, sizeof (XML_Char
*), compare
);
98 /** \brief Locale-independent integer parser.
100 * Works similar to strtol. Leading space is NOT skipped. The input
101 * number may have an optional sign. Radix is specified by base. If
102 * base is 0 then decimal is assumed unless the input number is
103 * prefixed by 0x or 0X for hexadecimal or 0 for octal. After
104 * returning tail points to the first character that is not part of
105 * the integer number. If no number was found then tail points to the
106 * start of the input string. */
108 strToI(const XML_Char
*string
, const XML_Char
**tail
, int base
)
110 int radix
= base
== 0 ? 10 : base
;
113 bool numberFound
= false;
114 const XML_Char
*start
= string
;
116 assert (radix
>= 2 && radix
<= 36);
118 if (*string
== '-') {
121 } else if (*string
== '+')
123 if (base
== 0 && *string
== '0') {
125 if (*(string
+1) == 'x' || *(string
+1) == 'X') {
136 if (*string
>= '0' && *string
< '0' + radix
)
137 digit
= *string
- '0';
139 if (*string
>= '0' && *string
<= '9')
140 digit
= *string
- '0';
141 else if (*string
>= 'a' && *string
< 'a' + radix
- 10)
142 digit
= *string
- 'a' + 10;
143 else if (*string
>= 'A' && *string
< 'A' + radix
- 10)
144 digit
= *string
- 'A' + 10;
148 result
= radix
*result
+ digit
;
153 *tail
= numberFound
? string
: start
;
154 return sign
* result
;
157 /** \brief Locale-independent floating-point parser.
159 * Works similar to strtod. Leading space is NOT skipped. The input
160 * number may have an optional sign. '.' is interpreted as decimal
161 * point and may occur at most once. Optionally the number may end in
162 * [eE]<exponent>, where <exponent> is an integer as recognized by
163 * strToI. In that case the result is number * 10^exponent. After
164 * returning tail points to the first character that is not part of
165 * the floating point number. If no number was found then tail points
166 * to the start of the input string.
168 * Uses two passes for maximum accuracy. */
170 strToF(const XML_Char
*string
, const XML_Char
**tail
)
172 int nDigits
= 0, pointPos
, exponent
;
173 float sign
= 1.0f
, result
= 0.0f
, scale
;
174 const XML_Char
*start
= string
, *numStart
;
177 if (*string
== '-') {
180 } else if (*string
== '+')
183 /* first pass: determine position of decimal point, number of
184 * digits, exponent and the end of the number. */
186 while (*string
>= '0' && *string
<= '9') {
191 if (*string
== '.') {
193 while (*string
>= '0' && *string
<= '9') {
199 /* no digits, no number */
204 if (*string
== 'e' || *string
== 'E') {
205 const XML_Char
*expTail
;
206 exponent
= strToI (string
+1, &expTail
, 10);
207 if (expTail
== string
+1)
215 /* scale of the first digit */
216 scale
= sign
* (float)pow (10.0, (double)(pointPos
-1 + exponent
));
218 /* second pass: parse digits */
220 if (*string
!= '.') {
221 assert (*string
>= '0' && *string
<= '9');
222 result
+= scale
* (float)(*string
- '0');
227 } while (nDigits
> 0);
232 /** \brief Parse a value of a given type. */
234 parseValue(driOptionValue
*v
, driOptionType type
, const XML_Char
*string
)
236 const XML_Char
*tail
= NULL
;
237 /* skip leading white-space */
238 string
+= strspn (string
, " \f\n\r\t\v");
241 if (!strcmp (string
, "false")) {
244 } else if (!strcmp (string
, "true")) {
251 case DRI_ENUM
: /* enum is just a special integer */
253 v
->_int
= strToI (string
, &tail
, 0);
256 v
->_float
= strToF (string
, &tail
);
260 v
->_string
= strndup(string
, STRING_CONF_MAXLEN
);
265 return false; /* empty string (or containing only white-space) */
266 /* skip trailing white space */
268 tail
+= strspn (tail
, " \f\n\r\t\v");
270 return false; /* something left over that is not part of value */
275 /** \brief Parse a list of ranges of type info->type. */
277 parseRanges(driOptionInfo
*info
, const XML_Char
*string
)
279 XML_Char
*cp
, *range
;
281 driOptionRange
*ranges
;
283 XSTRDUP (cp
, string
);
284 /* pass 1: determine the number of ranges (number of commas + 1) */
286 for (nRanges
= 1; *range
; ++range
)
290 if ((ranges
= malloc(nRanges
*sizeof(driOptionRange
))) == NULL
) {
291 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
295 /* pass 2: parse all ranges into preallocated array */
297 for (i
= 0; i
< nRanges
; ++i
) {
300 end
= strchr (range
, ',');
303 sep
= strchr (range
, ':');
304 if (sep
) { /* non-empty interval */
306 if (!parseValue (&ranges
[i
].start
, info
->type
, range
) ||
307 !parseValue (&ranges
[i
].end
, info
->type
, sep
+1))
309 if (info
->type
== DRI_INT
&&
310 ranges
[i
].start
._int
> ranges
[i
].end
._int
)
312 if (info
->type
== DRI_FLOAT
&&
313 ranges
[i
].start
._float
> ranges
[i
].end
._float
)
315 } else { /* empty interval */
316 if (!parseValue (&ranges
[i
].start
, info
->type
, range
))
318 ranges
[i
].end
= ranges
[i
].start
;
330 assert (range
== NULL
);
332 info
->nRanges
= nRanges
;
333 info
->ranges
= ranges
;
337 /** \brief Check if a value is in one of info->ranges. */
339 checkValue(const driOptionValue
*v
, const driOptionInfo
*info
)
342 assert (info
->type
!= DRI_BOOL
); /* should be caught by the parser */
343 if (info
->nRanges
== 0)
345 switch (info
->type
) {
346 case DRI_ENUM
: /* enum is just a special integer */
348 for (i
= 0; i
< info
->nRanges
; ++i
)
349 if (v
->_int
>= info
->ranges
[i
].start
._int
&&
350 v
->_int
<= info
->ranges
[i
].end
._int
)
354 for (i
= 0; i
< info
->nRanges
; ++i
)
355 if (v
->_float
>= info
->ranges
[i
].start
._float
&&
356 v
->_float
<= info
->ranges
[i
].end
._float
)
362 assert (0); /* should never happen */
368 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
371 * Is called from the drivers.
373 * \param f \c printf like format string.
376 __driUtilMessage(const char *f
, ...)
379 const char *libgl_debug
;
381 libgl_debug
=getenv("LIBGL_DEBUG");
382 if (libgl_debug
&& !strstr(libgl_debug
, "quiet")) {
383 fprintf(stderr
, "libGL: ");
385 vfprintf(stderr
, f
, args
);
387 fprintf(stderr
, "\n");
391 /** \brief Output a warning message. */
392 #define XML_WARNING1(msg) do {\
393 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
394 (int) XML_GetCurrentLineNumber(data->parser), \
395 (int) XML_GetCurrentColumnNumber(data->parser)); \
397 #define XML_WARNING(msg, ...) do { \
398 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
399 (int) XML_GetCurrentLineNumber(data->parser), \
400 (int) XML_GetCurrentColumnNumber(data->parser), \
403 /** \brief Output an error message. */
404 #define XML_ERROR1(msg) do { \
405 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
406 (int) XML_GetCurrentLineNumber(data->parser), \
407 (int) XML_GetCurrentColumnNumber(data->parser)); \
409 #define XML_ERROR(msg, ...) do { \
410 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
411 (int) XML_GetCurrentLineNumber(data->parser), \
412 (int) XML_GetCurrentColumnNumber(data->parser), \
415 /** \brief Output a fatal error message and abort. */
416 #define XML_FATAL1(msg) do { \
417 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
419 (int) XML_GetCurrentLineNumber(data->parser), \
420 (int) XML_GetCurrentColumnNumber(data->parser)); \
423 #define XML_FATAL(msg, ...) do { \
424 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
426 (int) XML_GetCurrentLineNumber(data->parser), \
427 (int) XML_GetCurrentColumnNumber(data->parser), \
432 /** \brief Parser context for __driConfigOptions. */
436 driOptionCache
*cache
;
445 /** \brief Elements in __driConfigOptions. */
447 OI_DESCRIPTION
= 0, OI_DRIINFO
, OI_ENUM
, OI_OPTION
, OI_SECTION
, OI_COUNT
449 static const XML_Char
*OptInfoElems
[] = {
450 "description", "driinfo", "enum", "option", "section"
453 /** \brief Parse attributes of an enum element.
455 * We're not actually interested in the data. Just make sure this is ok
456 * for external configuration tools.
459 parseEnumAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
462 const XML_Char
*value
= NULL
, *text
= NULL
;
464 uint32_t opt
= data
->curOption
;
465 for (i
= 0; attr
[i
]; i
+= 2) {
466 if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
467 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
468 else XML_FATAL("illegal enum attribute: %s.", attr
[i
]);
470 if (!value
) XML_FATAL1 ("value attribute missing in enum.");
471 if (!text
) XML_FATAL1 ("text attribute missing in enum.");
472 if (!parseValue (&v
, data
->cache
->info
[opt
].type
, value
))
473 XML_FATAL ("illegal enum value: %s.", value
);
474 if (!checkValue (&v
, &data
->cache
->info
[opt
]))
475 XML_FATAL ("enum value out of valid range: %s.", value
);
478 /** \brief Parse attributes of a description element.
480 * We're not actually interested in the data. Just make sure this is ok
481 * for external configuration tools.
484 parseDescAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
487 const XML_Char
*lang
= NULL
, *text
= NULL
;
488 for (i
= 0; attr
[i
]; i
+= 2) {
489 if (!strcmp (attr
[i
], "lang")) lang
= attr
[i
+1];
490 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
491 else XML_FATAL("illegal description attribute: %s.", attr
[i
]);
493 if (!lang
) XML_FATAL1 ("lang attribute missing in description.");
494 if (!text
) XML_FATAL1 ("text attribute missing in description.");
497 /** \brief Parse attributes of an option element. */
499 parseOptInfoAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
501 enum OptAttr
{OA_DEFAULT
= 0, OA_NAME
, OA_TYPE
, OA_VALID
, OA_COUNT
};
502 static const XML_Char
*optAttr
[] = {"default", "name", "type", "valid"};
503 const XML_Char
*attrVal
[OA_COUNT
] = {NULL
, NULL
, NULL
, NULL
};
504 const char *defaultVal
;
505 driOptionCache
*cache
= data
->cache
;
507 for (i
= 0; attr
[i
]; i
+= 2) {
508 uint32_t attrName
= bsearchStr (attr
[i
], optAttr
, OA_COUNT
);
509 if (attrName
>= OA_COUNT
)
510 XML_FATAL ("illegal option attribute: %s", attr
[i
]);
511 attrVal
[attrName
] = attr
[i
+1];
513 if (!attrVal
[OA_NAME
]) XML_FATAL1 ("name attribute missing in option.");
514 if (!attrVal
[OA_TYPE
]) XML_FATAL1 ("type attribute missing in option.");
515 if (!attrVal
[OA_DEFAULT
]) XML_FATAL1 ("default attribute missing in option.");
517 opt
= findOption (cache
, attrVal
[OA_NAME
]);
518 if (cache
->info
[opt
].name
)
519 XML_FATAL ("option %s redefined.", attrVal
[OA_NAME
]);
520 data
->curOption
= opt
;
522 XSTRDUP (cache
->info
[opt
].name
, attrVal
[OA_NAME
]);
524 if (!strcmp (attrVal
[OA_TYPE
], "bool"))
525 cache
->info
[opt
].type
= DRI_BOOL
;
526 else if (!strcmp (attrVal
[OA_TYPE
], "enum"))
527 cache
->info
[opt
].type
= DRI_ENUM
;
528 else if (!strcmp (attrVal
[OA_TYPE
], "int"))
529 cache
->info
[opt
].type
= DRI_INT
;
530 else if (!strcmp (attrVal
[OA_TYPE
], "float"))
531 cache
->info
[opt
].type
= DRI_FLOAT
;
532 else if (!strcmp (attrVal
[OA_TYPE
], "string"))
533 cache
->info
[opt
].type
= DRI_STRING
;
535 XML_FATAL ("illegal type in option: %s.", attrVal
[OA_TYPE
]);
537 defaultVal
= getenv (cache
->info
[opt
].name
);
538 if (defaultVal
!= NULL
) {
539 /* don't use XML_WARNING, we want the user to see this! */
541 "ATTENTION: default value of option %s overridden by environment.\n",
542 cache
->info
[opt
].name
);
544 defaultVal
= attrVal
[OA_DEFAULT
];
545 if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, defaultVal
))
546 XML_FATAL ("illegal default value for %s: %s.", cache
->info
[opt
].name
, defaultVal
);
548 if (attrVal
[OA_VALID
]) {
549 if (cache
->info
[opt
].type
== DRI_BOOL
)
550 XML_FATAL1 ("boolean option with valid attribute.");
551 if (!parseRanges (&cache
->info
[opt
], attrVal
[OA_VALID
]))
552 XML_FATAL ("illegal valid attribute: %s.", attrVal
[OA_VALID
]);
553 if (!checkValue (&cache
->values
[opt
], &cache
->info
[opt
]))
554 XML_FATAL ("default value out of valid range '%s': %s.",
555 attrVal
[OA_VALID
], defaultVal
);
556 } else if (cache
->info
[opt
].type
== DRI_ENUM
) {
557 XML_FATAL1 ("valid attribute missing in option (mandatory for enums).");
559 cache
->info
[opt
].nRanges
= 0;
560 cache
->info
[opt
].ranges
= NULL
;
564 /** \brief Handler for start element events. */
566 optInfoStartElem(void *userData
, const XML_Char
*name
, const XML_Char
**attr
)
568 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
569 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
573 XML_FATAL1 ("nested <driinfo> elements.");
575 XML_FATAL1 ("attributes specified on <driinfo> element.");
576 data
->inDriInfo
= true;
579 if (!data
->inDriInfo
)
580 XML_FATAL1 ("<section> must be inside <driinfo>.");
582 XML_FATAL1 ("nested <section> elements.");
584 XML_FATAL1 ("attributes specified on <section> element.");
585 data
->inSection
= true;
588 if (!data
->inSection
&& !data
->inOption
)
589 XML_FATAL1 ("<description> must be inside <description> or <option.");
591 XML_FATAL1 ("nested <description> elements.");
593 parseDescAttr (data
, attr
);
596 if (!data
->inSection
)
597 XML_FATAL1 ("<option> must be inside <section>.");
599 XML_FATAL1 ("<option> nested in <description> element.");
601 XML_FATAL1 ("nested <option> elements.");
602 data
->inOption
= true;
603 parseOptInfoAttr (data
, attr
);
606 if (!(data
->inOption
&& data
->inDesc
))
607 XML_FATAL1 ("<enum> must be inside <option> and <description>.");
609 XML_FATAL1 ("nested <enum> elements.");
611 parseEnumAttr (data
, attr
);
614 XML_FATAL ("unknown element: %s.", name
);
618 /** \brief Handler for end element events. */
620 optInfoEndElem(void *userData
, const XML_Char
*name
)
622 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
623 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
626 data
->inDriInfo
= false;
629 data
->inSection
= false;
632 data
->inDesc
= false;
635 data
->inOption
= false;
638 data
->inEnum
= false;
641 assert (0); /* should have been caught by StartElem */
646 driParseOptionInfo(driOptionCache
*info
, const char *configOptions
)
650 struct OptInfoData userData
;
651 struct OptInfoData
*data
= &userData
;
653 /* Make the hash table big enough to fit more than the maximum number of
654 * config options we've ever seen in a driver.
657 info
->info
= calloc(1 << info
->tableSize
, sizeof (driOptionInfo
));
658 info
->values
= calloc(1 << info
->tableSize
, sizeof (driOptionValue
));
659 if (info
->info
== NULL
|| info
->values
== NULL
) {
660 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
664 p
= XML_ParserCreate ("UTF-8"); /* always UTF-8 */
665 XML_SetElementHandler (p
, optInfoStartElem
, optInfoEndElem
);
666 XML_SetUserData (p
, data
);
668 userData
.name
= "__driConfigOptions";
670 userData
.cache
= info
;
671 userData
.inDriInfo
= false;
672 userData
.inSection
= false;
673 userData
.inDesc
= false;
674 userData
.inOption
= false;
675 userData
.inEnum
= false;
676 userData
.curOption
= -1;
678 status
= XML_Parse (p
, configOptions
, strlen (configOptions
), 1);
680 XML_FATAL ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
685 /** \brief Parser context for configuration files. */
689 driOptionCache
*cache
;
691 const char *driverName
, *execName
;
692 uint32_t ignoringDevice
;
693 uint32_t ignoringApp
;
700 /** \brief Elements in configuration files. */
702 OC_APPLICATION
= 0, OC_DEVICE
, OC_DRICONF
, OC_OPTION
, OC_COUNT
704 static const XML_Char
*OptConfElems
[] = {
705 [OC_APPLICATION
] = "application",
706 [OC_DEVICE
] = "device",
707 [OC_DRICONF
] = "driconf",
708 [OC_OPTION
] = "option",
711 /** \brief Parse attributes of a device element. */
713 parseDeviceAttr(struct OptConfData
*data
, const XML_Char
**attr
)
716 const XML_Char
*driver
= NULL
, *screen
= NULL
;
717 for (i
= 0; attr
[i
]; i
+= 2) {
718 if (!strcmp (attr
[i
], "driver")) driver
= attr
[i
+1];
719 else if (!strcmp (attr
[i
], "screen")) screen
= attr
[i
+1];
720 else XML_WARNING("unknown device attribute: %s.", attr
[i
]);
722 if (driver
&& strcmp (driver
, data
->driverName
))
723 data
->ignoringDevice
= data
->inDevice
;
725 driOptionValue screenNum
;
726 if (!parseValue (&screenNum
, DRI_INT
, screen
))
727 XML_WARNING("illegal screen number: %s.", screen
);
728 else if (screenNum
._int
!= data
->screenNum
)
729 data
->ignoringDevice
= data
->inDevice
;
733 /** \brief Parse attributes of an application element. */
735 parseAppAttr(struct OptConfData
*data
, const XML_Char
**attr
)
738 const XML_Char
*exec
= NULL
;
739 for (i
= 0; attr
[i
]; i
+= 2) {
740 if (!strcmp (attr
[i
], "name")) /* not needed here */;
741 else if (!strcmp (attr
[i
], "executable")) exec
= attr
[i
+1];
742 else XML_WARNING("unknown application attribute: %s.", attr
[i
]);
744 if (exec
&& strcmp (exec
, data
->execName
))
745 data
->ignoringApp
= data
->inApp
;
748 /** \brief Parse attributes of an option element. */
750 parseOptConfAttr(struct OptConfData
*data
, const XML_Char
**attr
)
753 const XML_Char
*name
= NULL
, *value
= NULL
;
754 for (i
= 0; attr
[i
]; i
+= 2) {
755 if (!strcmp (attr
[i
], "name")) name
= attr
[i
+1];
756 else if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
757 else XML_WARNING("unknown option attribute: %s.", attr
[i
]);
759 if (!name
) XML_WARNING1 ("name attribute missing in option.");
760 if (!value
) XML_WARNING1 ("value attribute missing in option.");
762 driOptionCache
*cache
= data
->cache
;
763 uint32_t opt
= findOption (cache
, name
);
764 if (cache
->info
[opt
].name
== NULL
)
765 /* don't use XML_WARNING, drirc defines options for all drivers,
766 * but not all drivers support them */
768 else if (getenv (cache
->info
[opt
].name
))
769 /* don't use XML_WARNING, we want the user to see this! */
770 fprintf (stderr
, "ATTENTION: option value of option %s ignored.\n",
771 cache
->info
[opt
].name
);
772 else if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, value
))
773 XML_WARNING ("illegal option value: %s.", value
);
777 /** \brief Handler for start element events. */
779 optConfStartElem(void *userData
, const XML_Char
*name
,
780 const XML_Char
**attr
)
782 struct OptConfData
*data
= (struct OptConfData
*)userData
;
783 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
787 XML_WARNING1 ("nested <driconf> elements.");
789 XML_WARNING1 ("attributes specified on <driconf> element.");
793 if (!data
->inDriConf
)
794 XML_WARNING1 ("<device> should be inside <driconf>.");
796 XML_WARNING1 ("nested <device> elements.");
798 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
799 parseDeviceAttr (data
, attr
);
803 XML_WARNING1 ("<application> should be inside <device>.");
805 XML_WARNING1 ("nested <application> elements.");
807 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
808 parseAppAttr (data
, attr
);
812 XML_WARNING1 ("<option> should be inside <application>.");
814 XML_WARNING1 ("nested <option> elements.");
816 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
817 parseOptConfAttr (data
, attr
);
820 XML_WARNING ("unknown element: %s.", name
);
824 /** \brief Handler for end element events. */
826 optConfEndElem(void *userData
, const XML_Char
*name
)
828 struct OptConfData
*data
= (struct OptConfData
*)userData
;
829 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
835 if (data
->inDevice
-- == data
->ignoringDevice
)
836 data
->ignoringDevice
= 0;
839 if (data
->inApp
-- == data
->ignoringApp
)
840 data
->ignoringApp
= 0;
846 /* unknown element, warning was produced on start tag */;
850 /** \brief Initialize an option cache based on info */
852 initOptionCache(driOptionCache
*cache
, const driOptionCache
*info
)
854 unsigned i
, size
= 1 << info
->tableSize
;
855 cache
->info
= info
->info
;
856 cache
->tableSize
= info
->tableSize
;
857 cache
->values
= malloc((1<<info
->tableSize
) * sizeof (driOptionValue
));
858 if (cache
->values
== NULL
) {
859 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
862 memcpy (cache
->values
, info
->values
,
863 (1<<info
->tableSize
) * sizeof (driOptionValue
));
864 for (i
= 0; i
< size
; ++i
) {
865 if (cache
->info
[i
].type
== DRI_STRING
)
866 XSTRDUP(cache
->values
[i
]._string
, info
->values
[i
]._string
);
871 _parseOneConfigFile(XML_Parser p
)
873 #define BUF_SIZE 0x1000
874 struct OptConfData
*data
= (struct OptConfData
*)XML_GetUserData (p
);
878 if ((fd
= open (data
->name
, O_RDONLY
)) == -1) {
879 __driUtilMessage ("Can't open configuration file %s: %s.",
880 data
->name
, strerror (errno
));
886 void *buffer
= XML_GetBuffer (p
, BUF_SIZE
);
888 __driUtilMessage ("Can't allocate parser buffer.");
891 bytesRead
= read (fd
, buffer
, BUF_SIZE
);
892 if (bytesRead
== -1) {
893 __driUtilMessage ("Error reading from configuration file %s: %s.",
894 data
->name
, strerror (errno
));
897 status
= XML_ParseBuffer (p
, bytesRead
, bytesRead
== 0);
899 XML_ERROR ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
910 /** \brief Parse the named configuration file */
912 parseOneConfigFile(struct OptConfData
*data
, const char *filename
)
916 p
= XML_ParserCreate (NULL
); /* use encoding specified by file */
917 XML_SetElementHandler (p
, optConfStartElem
, optConfEndElem
);
918 XML_SetUserData (p
, data
);
920 data
->name
= filename
;
921 data
->ignoringDevice
= 0;
922 data
->ignoringApp
= 0;
928 _parseOneConfigFile (p
);
933 #define SYSCONFDIR "/etc"
937 driParseConfigFiles(driOptionCache
*cache
, const driOptionCache
*info
,
938 int screenNum
, const char *driverName
)
941 struct OptConfData userData
;
943 initOptionCache (cache
, info
);
945 userData
.cache
= cache
;
946 userData
.screenNum
= screenNum
;
947 userData
.driverName
= driverName
;
948 userData
.execName
= util_get_process_name();
950 parseOneConfigFile(&userData
, SYSCONFDIR
"/drirc");
952 if ((home
= getenv ("HOME"))) {
953 char filename
[PATH_MAX
];
955 snprintf(filename
, PATH_MAX
, "%s/.drirc", home
);
956 parseOneConfigFile(&userData
, filename
);
961 driDestroyOptionInfo(driOptionCache
*info
)
963 driDestroyOptionCache(info
);
965 uint32_t i
, size
= 1 << info
->tableSize
;
966 for (i
= 0; i
< size
; ++i
) {
967 if (info
->info
[i
].name
) {
968 free(info
->info
[i
].name
);
969 free(info
->info
[i
].ranges
);
977 driDestroyOptionCache(driOptionCache
*cache
)
980 unsigned i
, size
= 1 << cache
->tableSize
;
981 for (i
= 0; i
< size
; ++i
) {
982 if (cache
->info
[i
].type
== DRI_STRING
)
983 free(cache
->values
[i
]._string
);
990 driCheckOption(const driOptionCache
*cache
, const char *name
,
993 uint32_t i
= findOption (cache
, name
);
994 return cache
->info
[i
].name
!= NULL
&& cache
->info
[i
].type
== type
;
998 driQueryOptionb(const driOptionCache
*cache
, const char *name
)
1000 uint32_t i
= findOption (cache
, name
);
1001 /* make sure the option is defined and has the correct type */
1002 assert (cache
->info
[i
].name
!= NULL
);
1003 assert (cache
->info
[i
].type
== DRI_BOOL
);
1004 return cache
->values
[i
]._bool
;
1008 driQueryOptioni(const driOptionCache
*cache
, const char *name
)
1010 uint32_t i
= findOption (cache
, name
);
1011 /* make sure the option is defined and has the correct type */
1012 assert (cache
->info
[i
].name
!= NULL
);
1013 assert (cache
->info
[i
].type
== DRI_INT
|| cache
->info
[i
].type
== DRI_ENUM
);
1014 return cache
->values
[i
]._int
;
1018 driQueryOptionf(const driOptionCache
*cache
, const char *name
)
1020 uint32_t i
= findOption (cache
, name
);
1021 /* make sure the option is defined and has the correct type */
1022 assert (cache
->info
[i
].name
!= NULL
);
1023 assert (cache
->info
[i
].type
== DRI_FLOAT
);
1024 return cache
->values
[i
]._float
;
1028 driQueryOptionstr(const driOptionCache
*cache
, const char *name
)
1030 uint32_t i
= findOption (cache
, name
);
1031 /* make sure the option is defined and has the correct type */
1032 assert (cache
->info
[i
].name
!= NULL
);
1033 assert (cache
->info
[i
].type
== DRI_STRING
);
1034 return cache
->values
[i
]._string
;