2 * XML DRI client-side driver configuration
3 * Copyright (C) 2003 Felix Kuehling
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * FELIX KUEHLING, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
21 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * \brief Driver-independent client-side part of the XML configuration
27 * \author Felix Kuehling
39 #include "xmlconfig.h"
43 /** \brief Find an option in an option cache with the name as key */
45 findOption(const driOptionCache
*cache
, const char *name
)
47 uint32_t len
= strlen (name
);
48 uint32_t size
= 1 << cache
->tableSize
, mask
= size
- 1;
52 /* compute a hash from the variable length name */
53 for (i
= 0, shift
= 0; i
< len
; ++i
, shift
= (shift
+8) & 31)
54 hash
+= (uint32_t)name
[i
] << shift
;
56 hash
= (hash
>> (16-cache
->tableSize
/2)) & mask
;
58 /* this is just the starting point of the linear search for the option */
59 for (i
= 0; i
< size
; ++i
, hash
= (hash
+1) & mask
) {
60 /* if we hit an empty entry then the option is not defined (yet) */
61 if (cache
->info
[hash
].name
== 0)
63 else if (!strcmp (name
, cache
->info
[hash
].name
))
66 /* this assertion fails if the hash table is full */
72 /** \brief Like strdup but using malloc and with error checking. */
73 #define XSTRDUP(dest,source) do { \
74 uint32_t len = strlen (source); \
75 if (!(dest = malloc(len+1))) { \
76 fprintf (stderr, "%s: %d: out of memory.\n", __FILE__, __LINE__); \
79 memcpy (dest, source, len+1); \
82 static int compare (const void *a
, const void *b
) {
83 return strcmp (*(char *const*)a
, *(char *const*)b
);
85 /** \brief Binary search in a string array. */
87 bsearchStr (const XML_Char
*name
, const XML_Char
*elems
[], uint32_t count
)
89 const XML_Char
**found
;
90 found
= bsearch (&name
, elems
, count
, sizeof (XML_Char
*), compare
);
97 /** \brief Locale-independent integer parser.
99 * Works similar to strtol. Leading space is NOT skipped. The input
100 * number may have an optional sign. Radix is specified by base. If
101 * base is 0 then decimal is assumed unless the input number is
102 * prefixed by 0x or 0X for hexadecimal or 0 for octal. After
103 * returning tail points to the first character that is not part of
104 * the integer number. If no number was found then tail points to the
105 * start of the input string. */
107 strToI(const XML_Char
*string
, const XML_Char
**tail
, int base
)
109 int radix
= base
== 0 ? 10 : base
;
112 bool numberFound
= false;
113 const XML_Char
*start
= string
;
115 assert (radix
>= 2 && radix
<= 36);
117 if (*string
== '-') {
120 } else if (*string
== '+')
122 if (base
== 0 && *string
== '0') {
124 if (*(string
+1) == 'x' || *(string
+1) == 'X') {
135 if (*string
>= '0' && *string
< '0' + radix
)
136 digit
= *string
- '0';
138 if (*string
>= '0' && *string
<= '9')
139 digit
= *string
- '0';
140 else if (*string
>= 'a' && *string
< 'a' + radix
- 10)
141 digit
= *string
- 'a' + 10;
142 else if (*string
>= 'A' && *string
< 'A' + radix
- 10)
143 digit
= *string
- 'A' + 10;
147 result
= radix
*result
+ digit
;
152 *tail
= numberFound
? string
: start
;
153 return sign
* result
;
156 /** \brief Locale-independent floating-point parser.
158 * Works similar to strtod. Leading space is NOT skipped. The input
159 * number may have an optional sign. '.' is interpreted as decimal
160 * point and may occur at most once. Optionally the number may end in
161 * [eE]<exponent>, where <exponent> is an integer as recognized by
162 * strToI. In that case the result is number * 10^exponent. After
163 * returning tail points to the first character that is not part of
164 * the floating point number. If no number was found then tail points
165 * to the start of the input string.
167 * Uses two passes for maximum accuracy. */
169 strToF(const XML_Char
*string
, const XML_Char
**tail
)
171 int nDigits
= 0, pointPos
, exponent
;
172 float sign
= 1.0f
, result
= 0.0f
, scale
;
173 const XML_Char
*start
= string
, *numStart
;
176 if (*string
== '-') {
179 } else if (*string
== '+')
182 /* first pass: determine position of decimal point, number of
183 * digits, exponent and the end of the number. */
185 while (*string
>= '0' && *string
<= '9') {
190 if (*string
== '.') {
192 while (*string
>= '0' && *string
<= '9') {
198 /* no digits, no number */
203 if (*string
== 'e' || *string
== 'E') {
204 const XML_Char
*expTail
;
205 exponent
= strToI (string
+1, &expTail
, 10);
206 if (expTail
== string
+1)
214 /* scale of the first digit */
215 scale
= sign
* (float)pow (10.0, (double)(pointPos
-1 + exponent
));
217 /* second pass: parse digits */
219 if (*string
!= '.') {
220 assert (*string
>= '0' && *string
<= '9');
221 result
+= scale
* (float)(*string
- '0');
226 } while (nDigits
> 0);
231 /** \brief Parse a value of a given type. */
233 parseValue(driOptionValue
*v
, driOptionType type
, const XML_Char
*string
)
235 const XML_Char
*tail
= NULL
;
236 /* skip leading white-space */
237 string
+= strspn (string
, " \f\n\r\t\v");
240 if (!strcmp (string
, "false")) {
243 } else if (!strcmp (string
, "true")) {
250 case DRI_ENUM
: /* enum is just a special integer */
252 v
->_int
= strToI (string
, &tail
, 0);
255 v
->_float
= strToF (string
, &tail
);
259 v
->_string
= strndup(string
, STRING_CONF_MAXLEN
);
264 return false; /* empty string (or containing only white-space) */
265 /* skip trailing white space */
267 tail
+= strspn (tail
, " \f\n\r\t\v");
269 return false; /* something left over that is not part of value */
274 /** \brief Parse a list of ranges of type info->type. */
276 parseRanges(driOptionInfo
*info
, const XML_Char
*string
)
278 XML_Char
*cp
, *range
;
280 driOptionRange
*ranges
;
282 XSTRDUP (cp
, string
);
283 /* pass 1: determine the number of ranges (number of commas + 1) */
285 for (nRanges
= 1; *range
; ++range
)
289 if ((ranges
= malloc(nRanges
*sizeof(driOptionRange
))) == NULL
) {
290 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
294 /* pass 2: parse all ranges into preallocated array */
296 for (i
= 0; i
< nRanges
; ++i
) {
299 end
= strchr (range
, ',');
302 sep
= strchr (range
, ':');
303 if (sep
) { /* non-empty interval */
305 if (!parseValue (&ranges
[i
].start
, info
->type
, range
) ||
306 !parseValue (&ranges
[i
].end
, info
->type
, sep
+1))
308 if (info
->type
== DRI_INT
&&
309 ranges
[i
].start
._int
> ranges
[i
].end
._int
)
311 if (info
->type
== DRI_FLOAT
&&
312 ranges
[i
].start
._float
> ranges
[i
].end
._float
)
314 } else { /* empty interval */
315 if (!parseValue (&ranges
[i
].start
, info
->type
, range
))
317 ranges
[i
].end
= ranges
[i
].start
;
329 assert (range
== NULL
);
331 info
->nRanges
= nRanges
;
332 info
->ranges
= ranges
;
336 /** \brief Check if a value is in one of info->ranges. */
338 checkValue(const driOptionValue
*v
, const driOptionInfo
*info
)
341 assert (info
->type
!= DRI_BOOL
); /* should be caught by the parser */
342 if (info
->nRanges
== 0)
344 switch (info
->type
) {
345 case DRI_ENUM
: /* enum is just a special integer */
347 for (i
= 0; i
< info
->nRanges
; ++i
)
348 if (v
->_int
>= info
->ranges
[i
].start
._int
&&
349 v
->_int
<= info
->ranges
[i
].end
._int
)
353 for (i
= 0; i
< info
->nRanges
; ++i
)
354 if (v
->_float
>= info
->ranges
[i
].start
._float
&&
355 v
->_float
<= info
->ranges
[i
].end
._float
)
361 assert (0); /* should never happen */
367 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
370 * Is called from the drivers.
372 * \param f \c printf like format string.
375 __driUtilMessage(const char *f
, ...)
378 const char *libgl_debug
;
380 libgl_debug
=getenv("LIBGL_DEBUG");
381 if (libgl_debug
&& !strstr(libgl_debug
, "quiet")) {
382 fprintf(stderr
, "libGL: ");
384 vfprintf(stderr
, f
, args
);
386 fprintf(stderr
, "\n");
390 /** \brief Output a warning message. */
391 #define XML_WARNING1(msg) do {\
392 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
393 (int) XML_GetCurrentLineNumber(data->parser), \
394 (int) XML_GetCurrentColumnNumber(data->parser)); \
396 #define XML_WARNING(msg, ...) do { \
397 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
398 (int) XML_GetCurrentLineNumber(data->parser), \
399 (int) XML_GetCurrentColumnNumber(data->parser), \
402 /** \brief Output an error message. */
403 #define XML_ERROR1(msg) do { \
404 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
405 (int) XML_GetCurrentLineNumber(data->parser), \
406 (int) XML_GetCurrentColumnNumber(data->parser)); \
408 #define XML_ERROR(msg, ...) do { \
409 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
410 (int) XML_GetCurrentLineNumber(data->parser), \
411 (int) XML_GetCurrentColumnNumber(data->parser), \
414 /** \brief Output a fatal error message and abort. */
415 #define XML_FATAL1(msg) do { \
416 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
418 (int) XML_GetCurrentLineNumber(data->parser), \
419 (int) XML_GetCurrentColumnNumber(data->parser)); \
422 #define XML_FATAL(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), \
431 /** \brief Parser context for __driConfigOptions. */
435 driOptionCache
*cache
;
444 /** \brief Elements in __driConfigOptions. */
446 OI_DESCRIPTION
= 0, OI_DRIINFO
, OI_ENUM
, OI_OPTION
, OI_SECTION
, OI_COUNT
448 static const XML_Char
*OptInfoElems
[] = {
449 "description", "driinfo", "enum", "option", "section"
452 /** \brief Parse attributes of an enum element.
454 * We're not actually interested in the data. Just make sure this is ok
455 * for external configuration tools.
458 parseEnumAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
461 const XML_Char
*value
= NULL
, *text
= NULL
;
463 uint32_t opt
= data
->curOption
;
464 for (i
= 0; attr
[i
]; i
+= 2) {
465 if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
466 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
467 else XML_FATAL("illegal enum attribute: %s.", attr
[i
]);
469 if (!value
) XML_FATAL1 ("value attribute missing in enum.");
470 if (!text
) XML_FATAL1 ("text attribute missing in enum.");
471 if (!parseValue (&v
, data
->cache
->info
[opt
].type
, value
))
472 XML_FATAL ("illegal enum value: %s.", value
);
473 if (!checkValue (&v
, &data
->cache
->info
[opt
]))
474 XML_FATAL ("enum value out of valid range: %s.", value
);
477 /** \brief Parse attributes of a description element.
479 * We're not actually interested in the data. Just make sure this is ok
480 * for external configuration tools.
483 parseDescAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
486 const XML_Char
*lang
= NULL
, *text
= NULL
;
487 for (i
= 0; attr
[i
]; i
+= 2) {
488 if (!strcmp (attr
[i
], "lang")) lang
= attr
[i
+1];
489 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
490 else XML_FATAL("illegal description attribute: %s.", attr
[i
]);
492 if (!lang
) XML_FATAL1 ("lang attribute missing in description.");
493 if (!text
) XML_FATAL1 ("text attribute missing in description.");
496 /** \brief Parse attributes of an option element. */
498 parseOptInfoAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
500 enum OptAttr
{OA_DEFAULT
= 0, OA_NAME
, OA_TYPE
, OA_VALID
, OA_COUNT
};
501 static const XML_Char
*optAttr
[] = {"default", "name", "type", "valid"};
502 const XML_Char
*attrVal
[OA_COUNT
] = {NULL
, NULL
, NULL
, NULL
};
503 const char *defaultVal
;
504 driOptionCache
*cache
= data
->cache
;
506 for (i
= 0; attr
[i
]; i
+= 2) {
507 uint32_t attrName
= bsearchStr (attr
[i
], optAttr
, OA_COUNT
);
508 if (attrName
>= OA_COUNT
)
509 XML_FATAL ("illegal option attribute: %s", attr
[i
]);
510 attrVal
[attrName
] = attr
[i
+1];
512 if (!attrVal
[OA_NAME
]) XML_FATAL1 ("name attribute missing in option.");
513 if (!attrVal
[OA_TYPE
]) XML_FATAL1 ("type attribute missing in option.");
514 if (!attrVal
[OA_DEFAULT
]) XML_FATAL1 ("default attribute missing in option.");
516 opt
= findOption (cache
, attrVal
[OA_NAME
]);
517 if (cache
->info
[opt
].name
)
518 XML_FATAL ("option %s redefined.", attrVal
[OA_NAME
]);
519 data
->curOption
= opt
;
521 XSTRDUP (cache
->info
[opt
].name
, attrVal
[OA_NAME
]);
523 if (!strcmp (attrVal
[OA_TYPE
], "bool"))
524 cache
->info
[opt
].type
= DRI_BOOL
;
525 else if (!strcmp (attrVal
[OA_TYPE
], "enum"))
526 cache
->info
[opt
].type
= DRI_ENUM
;
527 else if (!strcmp (attrVal
[OA_TYPE
], "int"))
528 cache
->info
[opt
].type
= DRI_INT
;
529 else if (!strcmp (attrVal
[OA_TYPE
], "float"))
530 cache
->info
[opt
].type
= DRI_FLOAT
;
531 else if (!strcmp (attrVal
[OA_TYPE
], "string"))
532 cache
->info
[opt
].type
= DRI_STRING
;
534 XML_FATAL ("illegal type in option: %s.", attrVal
[OA_TYPE
]);
536 defaultVal
= getenv (cache
->info
[opt
].name
);
537 if (defaultVal
!= NULL
) {
538 /* don't use XML_WARNING, we want the user to see this! */
540 "ATTENTION: default value of option %s overridden by environment.\n",
541 cache
->info
[opt
].name
);
543 defaultVal
= attrVal
[OA_DEFAULT
];
544 if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, defaultVal
))
545 XML_FATAL ("illegal default value for %s: %s.", cache
->info
[opt
].name
, defaultVal
);
547 if (attrVal
[OA_VALID
]) {
548 if (cache
->info
[opt
].type
== DRI_BOOL
)
549 XML_FATAL1 ("boolean option with valid attribute.");
550 if (!parseRanges (&cache
->info
[opt
], attrVal
[OA_VALID
]))
551 XML_FATAL ("illegal valid attribute: %s.", attrVal
[OA_VALID
]);
552 if (!checkValue (&cache
->values
[opt
], &cache
->info
[opt
]))
553 XML_FATAL ("default value out of valid range '%s': %s.",
554 attrVal
[OA_VALID
], defaultVal
);
555 } else if (cache
->info
[opt
].type
== DRI_ENUM
) {
556 XML_FATAL1 ("valid attribute missing in option (mandatory for enums).");
558 cache
->info
[opt
].nRanges
= 0;
559 cache
->info
[opt
].ranges
= NULL
;
563 /** \brief Handler for start element events. */
565 optInfoStartElem(void *userData
, const XML_Char
*name
, const XML_Char
**attr
)
567 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
568 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
572 XML_FATAL1 ("nested <driinfo> elements.");
574 XML_FATAL1 ("attributes specified on <driinfo> element.");
575 data
->inDriInfo
= true;
578 if (!data
->inDriInfo
)
579 XML_FATAL1 ("<section> must be inside <driinfo>.");
581 XML_FATAL1 ("nested <section> elements.");
583 XML_FATAL1 ("attributes specified on <section> element.");
584 data
->inSection
= true;
587 if (!data
->inSection
&& !data
->inOption
)
588 XML_FATAL1 ("<description> must be inside <description> or <option.");
590 XML_FATAL1 ("nested <description> elements.");
592 parseDescAttr (data
, attr
);
595 if (!data
->inSection
)
596 XML_FATAL1 ("<option> must be inside <section>.");
598 XML_FATAL1 ("<option> nested in <description> element.");
600 XML_FATAL1 ("nested <option> elements.");
601 data
->inOption
= true;
602 parseOptInfoAttr (data
, attr
);
605 if (!(data
->inOption
&& data
->inDesc
))
606 XML_FATAL1 ("<enum> must be inside <option> and <description>.");
608 XML_FATAL1 ("nested <enum> elements.");
610 parseEnumAttr (data
, attr
);
613 XML_FATAL ("unknown element: %s.", name
);
617 /** \brief Handler for end element events. */
619 optInfoEndElem(void *userData
, const XML_Char
*name
)
621 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
622 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
625 data
->inDriInfo
= false;
628 data
->inSection
= false;
631 data
->inDesc
= false;
634 data
->inOption
= false;
637 data
->inEnum
= false;
640 assert (0); /* should have been caught by StartElem */
645 driParseOptionInfo(driOptionCache
*info
, const char *configOptions
)
649 struct OptInfoData userData
;
650 struct OptInfoData
*data
= &userData
;
652 /* Make the hash table big enough to fit more than the maximum number of
653 * config options we've ever seen in a driver.
656 info
->info
= calloc(1 << info
->tableSize
, sizeof (driOptionInfo
));
657 info
->values
= calloc(1 << info
->tableSize
, sizeof (driOptionValue
));
658 if (info
->info
== NULL
|| info
->values
== NULL
) {
659 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
663 p
= XML_ParserCreate ("UTF-8"); /* always UTF-8 */
664 XML_SetElementHandler (p
, optInfoStartElem
, optInfoEndElem
);
665 XML_SetUserData (p
, data
);
667 userData
.name
= "__driConfigOptions";
669 userData
.cache
= info
;
670 userData
.inDriInfo
= false;
671 userData
.inSection
= false;
672 userData
.inDesc
= false;
673 userData
.inOption
= false;
674 userData
.inEnum
= false;
675 userData
.curOption
= -1;
677 status
= XML_Parse (p
, configOptions
, strlen (configOptions
), 1);
679 XML_FATAL ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
684 /** \brief Parser context for configuration files. */
688 driOptionCache
*cache
;
690 const char *driverName
, *execName
;
691 uint32_t ignoringDevice
;
692 uint32_t ignoringApp
;
699 /** \brief Elements in configuration files. */
701 OC_APPLICATION
= 0, OC_DEVICE
, OC_DRICONF
, OC_OPTION
, OC_COUNT
703 static const XML_Char
*OptConfElems
[] = {
704 [OC_APPLICATION
] = "application",
705 [OC_DEVICE
] = "device",
706 [OC_DRICONF
] = "driconf",
707 [OC_OPTION
] = "option",
710 /** \brief Parse attributes of a device element. */
712 parseDeviceAttr(struct OptConfData
*data
, const XML_Char
**attr
)
715 const XML_Char
*driver
= NULL
, *screen
= NULL
;
716 for (i
= 0; attr
[i
]; i
+= 2) {
717 if (!strcmp (attr
[i
], "driver")) driver
= attr
[i
+1];
718 else if (!strcmp (attr
[i
], "screen")) screen
= attr
[i
+1];
719 else XML_WARNING("unknown device attribute: %s.", attr
[i
]);
721 if (driver
&& strcmp (driver
, data
->driverName
))
722 data
->ignoringDevice
= data
->inDevice
;
724 driOptionValue screenNum
;
725 if (!parseValue (&screenNum
, DRI_INT
, screen
))
726 XML_WARNING("illegal screen number: %s.", screen
);
727 else if (screenNum
._int
!= data
->screenNum
)
728 data
->ignoringDevice
= data
->inDevice
;
732 /** \brief Parse attributes of an application element. */
734 parseAppAttr(struct OptConfData
*data
, const XML_Char
**attr
)
737 const XML_Char
*exec
= NULL
;
738 for (i
= 0; attr
[i
]; i
+= 2) {
739 if (!strcmp (attr
[i
], "name")) /* not needed here */;
740 else if (!strcmp (attr
[i
], "executable")) exec
= attr
[i
+1];
741 else XML_WARNING("unknown application attribute: %s.", attr
[i
]);
743 if (exec
&& strcmp (exec
, data
->execName
))
744 data
->ignoringApp
= data
->inApp
;
747 /** \brief Parse attributes of an option element. */
749 parseOptConfAttr(struct OptConfData
*data
, const XML_Char
**attr
)
752 const XML_Char
*name
= NULL
, *value
= NULL
;
753 for (i
= 0; attr
[i
]; i
+= 2) {
754 if (!strcmp (attr
[i
], "name")) name
= attr
[i
+1];
755 else if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
756 else XML_WARNING("unknown option attribute: %s.", attr
[i
]);
758 if (!name
) XML_WARNING1 ("name attribute missing in option.");
759 if (!value
) XML_WARNING1 ("value attribute missing in option.");
761 driOptionCache
*cache
= data
->cache
;
762 uint32_t opt
= findOption (cache
, name
);
763 if (cache
->info
[opt
].name
== NULL
)
764 /* don't use XML_WARNING, drirc defines options for all drivers,
765 * but not all drivers support them */
767 else if (getenv (cache
->info
[opt
].name
))
768 /* don't use XML_WARNING, we want the user to see this! */
769 fprintf (stderr
, "ATTENTION: option value of option %s ignored.\n",
770 cache
->info
[opt
].name
);
771 else if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, value
))
772 XML_WARNING ("illegal option value: %s.", value
);
776 /** \brief Handler for start element events. */
778 optConfStartElem(void *userData
, const XML_Char
*name
,
779 const XML_Char
**attr
)
781 struct OptConfData
*data
= (struct OptConfData
*)userData
;
782 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
786 XML_WARNING1 ("nested <driconf> elements.");
788 XML_WARNING1 ("attributes specified on <driconf> element.");
792 if (!data
->inDriConf
)
793 XML_WARNING1 ("<device> should be inside <driconf>.");
795 XML_WARNING1 ("nested <device> elements.");
797 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
798 parseDeviceAttr (data
, attr
);
802 XML_WARNING1 ("<application> should be inside <device>.");
804 XML_WARNING1 ("nested <application> elements.");
806 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
807 parseAppAttr (data
, attr
);
811 XML_WARNING1 ("<option> should be inside <application>.");
813 XML_WARNING1 ("nested <option> elements.");
815 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
816 parseOptConfAttr (data
, attr
);
819 XML_WARNING ("unknown element: %s.", name
);
823 /** \brief Handler for end element events. */
825 optConfEndElem(void *userData
, const XML_Char
*name
)
827 struct OptConfData
*data
= (struct OptConfData
*)userData
;
828 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
834 if (data
->inDevice
-- == data
->ignoringDevice
)
835 data
->ignoringDevice
= 0;
838 if (data
->inApp
-- == data
->ignoringApp
)
839 data
->ignoringApp
= 0;
845 /* unknown element, warning was produced on start tag */;
849 /** \brief Initialize an option cache based on info */
851 initOptionCache(driOptionCache
*cache
, const driOptionCache
*info
)
853 unsigned i
, size
= 1 << info
->tableSize
;
854 cache
->info
= info
->info
;
855 cache
->tableSize
= info
->tableSize
;
856 cache
->values
= malloc((1<<info
->tableSize
) * sizeof (driOptionValue
));
857 if (cache
->values
== NULL
) {
858 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
861 memcpy (cache
->values
, info
->values
,
862 (1<<info
->tableSize
) * sizeof (driOptionValue
));
863 for (i
= 0; i
< size
; ++i
) {
864 if (cache
->info
[i
].type
== DRI_STRING
)
865 XSTRDUP(cache
->values
[i
]._string
, info
->values
[i
]._string
);
869 /** \brief Parse the named configuration file */
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
)));
911 #define SYSCONFDIR "/etc"
915 driParseConfigFiles(driOptionCache
*cache
, const driOptionCache
*info
,
916 int screenNum
, const char *driverName
)
918 char *filenames
[2] = { SYSCONFDIR
"/drirc", NULL
};
921 struct OptConfData userData
;
923 initOptionCache (cache
, info
);
925 userData
.cache
= cache
;
926 userData
.screenNum
= screenNum
;
927 userData
.driverName
= driverName
;
928 userData
.execName
= util_get_process_name();
930 if ((home
= getenv ("HOME"))) {
931 uint32_t len
= strlen (home
);
932 filenames
[1] = malloc(len
+ 7+1);
933 if (filenames
[1] == NULL
)
934 __driUtilMessage ("Can't allocate memory for %s/.drirc.", home
);
936 memcpy (filenames
[1], home
, len
);
937 memcpy (filenames
[1] + len
, "/.drirc", 7+1);
941 for (i
= 0; i
< 2; ++i
) {
943 if (filenames
[i
] == NULL
)
946 p
= XML_ParserCreate (NULL
); /* use encoding specified by file */
947 XML_SetElementHandler (p
, optConfStartElem
, optConfEndElem
);
948 XML_SetUserData (p
, &userData
);
950 userData
.name
= filenames
[i
];
951 userData
.ignoringDevice
= 0;
952 userData
.ignoringApp
= 0;
953 userData
.inDriConf
= 0;
954 userData
.inDevice
= 0;
956 userData
.inOption
= 0;
958 parseOneConfigFile (p
);
966 driDestroyOptionInfo(driOptionCache
*info
)
968 driDestroyOptionCache(info
);
970 uint32_t i
, size
= 1 << info
->tableSize
;
971 for (i
= 0; i
< size
; ++i
) {
972 if (info
->info
[i
].name
) {
973 free(info
->info
[i
].name
);
974 free(info
->info
[i
].ranges
);
982 driDestroyOptionCache(driOptionCache
*cache
)
985 unsigned i
, size
= 1 << cache
->tableSize
;
986 for (i
= 0; i
< size
; ++i
) {
987 if (cache
->info
[i
].type
== DRI_STRING
)
988 free(cache
->values
[i
]._string
);
995 driCheckOption(const driOptionCache
*cache
, const char *name
,
998 uint32_t i
= findOption (cache
, name
);
999 return cache
->info
[i
].name
!= NULL
&& cache
->info
[i
].type
== type
;
1003 driQueryOptionb(const driOptionCache
*cache
, const char *name
)
1005 uint32_t i
= findOption (cache
, name
);
1006 /* make sure the option is defined and has the correct type */
1007 assert (cache
->info
[i
].name
!= NULL
);
1008 assert (cache
->info
[i
].type
== DRI_BOOL
);
1009 return cache
->values
[i
]._bool
;
1013 driQueryOptioni(const driOptionCache
*cache
, const char *name
)
1015 uint32_t i
= findOption (cache
, name
);
1016 /* make sure the option is defined and has the correct type */
1017 assert (cache
->info
[i
].name
!= NULL
);
1018 assert (cache
->info
[i
].type
== DRI_INT
|| cache
->info
[i
].type
== DRI_ENUM
);
1019 return cache
->values
[i
]._int
;
1023 driQueryOptionf(const driOptionCache
*cache
, const char *name
)
1025 uint32_t i
= findOption (cache
, name
);
1026 /* make sure the option is defined and has the correct type */
1027 assert (cache
->info
[i
].name
!= NULL
);
1028 assert (cache
->info
[i
].type
== DRI_FLOAT
);
1029 return cache
->values
[i
]._float
;
1033 driQueryOptionstr(const driOptionCache
*cache
, const char *name
)
1035 uint32_t i
= findOption (cache
, name
);
1036 /* make sure the option is defined and has the correct type */
1037 assert (cache
->info
[i
].name
!= NULL
);
1038 assert (cache
->info
[i
].type
== DRI_STRING
);
1039 return cache
->values
[i
]._string
;