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"
41 #undef GET_PROGRAM_NAME
43 #if (defined(__GNU_LIBRARY__) || defined(__GLIBC__)) && !defined(__UCLIBC__)
44 # if !defined(__GLIBC__) || (__GLIBC__ < 2)
45 /* These aren't declared in any libc5 header */
46 extern char *program_invocation_name
, *program_invocation_short_name
;
51 char * arg
= strrchr(program_invocation_name
, '/');
55 return program_invocation_name
;
57 # define GET_PROGRAM_NAME() __getProgramName()
58 #elif defined(__CYGWIN__)
59 # define GET_PROGRAM_NAME() program_invocation_short_name
60 #elif defined(__FreeBSD__) && (__FreeBSD__ >= 2)
61 # include <osreldate.h>
62 # if (__FreeBSD_version >= 440000)
64 # define GET_PROGRAM_NAME() getprogname()
66 #elif defined(__NetBSD__) && defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 106000100)
68 # define GET_PROGRAM_NAME() getprogname()
69 #elif defined(__DragonFly__)
71 # define GET_PROGRAM_NAME() getprogname()
72 #elif defined(__APPLE__)
74 # define GET_PROGRAM_NAME() getprogname()
76 /* Solaris has getexecname() which returns the full path - return just
77 the basename to match BSD getprogname() */
84 static const char *progname
;
86 if (progname
== NULL
) {
87 const char *e
= getexecname();
89 /* Have to make a copy since getexecname can return a readonly
90 string, but basename expects to be able to modify its arg. */
93 progname
= basename(n
);
100 # define GET_PROGRAM_NAME() __getProgramName()
103 #if !defined(GET_PROGRAM_NAME)
104 # if defined(__OpenBSD__) || defined(NetBSD) || defined(__UCLIBC__) || defined(ANDROID)
105 /* This is a hack. It's said to work on OpenBSD, NetBSD and GNU.
106 * Rogelio M.Serrano Jr. reported it's also working with UCLIBC. It's
107 * used as a last resort, if there is no documented facility available. */
111 extern const char *__progname
;
112 char * arg
= strrchr(__progname
, '/');
118 # define GET_PROGRAM_NAME() __getProgramName()
120 # define GET_PROGRAM_NAME() ""
121 # warning "Per application configuration won't work with your OS version."
125 /** \brief Find an option in an option cache with the name as key */
127 findOption(const driOptionCache
*cache
, const char *name
)
129 uint32_t len
= strlen (name
);
130 uint32_t size
= 1 << cache
->tableSize
, mask
= size
- 1;
134 /* compute a hash from the variable length name */
135 for (i
= 0, shift
= 0; i
< len
; ++i
, shift
= (shift
+8) & 31)
136 hash
+= (uint32_t)name
[i
] << shift
;
138 hash
= (hash
>> (16-cache
->tableSize
/2)) & mask
;
140 /* this is just the starting point of the linear search for the option */
141 for (i
= 0; i
< size
; ++i
, hash
= (hash
+1) & mask
) {
142 /* if we hit an empty entry then the option is not defined (yet) */
143 if (cache
->info
[hash
].name
== 0)
145 else if (!strcmp (name
, cache
->info
[hash
].name
))
148 /* this assertion fails if the hash table is full */
154 /** \brief Like strdup but using malloc and with error checking. */
155 #define XSTRDUP(dest,source) do { \
156 uint32_t len = strlen (source); \
157 if (!(dest = malloc(len+1))) { \
158 fprintf (stderr, "%s: %d: out of memory.\n", __FILE__, __LINE__); \
161 memcpy (dest, source, len+1); \
164 static int compare (const void *a
, const void *b
) {
165 return strcmp (*(char *const*)a
, *(char *const*)b
);
167 /** \brief Binary search in a string array. */
169 bsearchStr (const XML_Char
*name
, const XML_Char
*elems
[], uint32_t count
)
171 const XML_Char
**found
;
172 found
= bsearch (&name
, elems
, count
, sizeof (XML_Char
*), compare
);
174 return found
- elems
;
179 /** \brief Locale-independent integer parser.
181 * Works similar to strtol. Leading space is NOT skipped. The input
182 * number may have an optional sign. Radix is specified by base. If
183 * base is 0 then decimal is assumed unless the input number is
184 * prefixed by 0x or 0X for hexadecimal or 0 for octal. After
185 * returning tail points to the first character that is not part of
186 * the integer number. If no number was found then tail points to the
187 * start of the input string. */
189 strToI(const XML_Char
*string
, const XML_Char
**tail
, int base
)
191 int radix
= base
== 0 ? 10 : base
;
194 bool numberFound
= false;
195 const XML_Char
*start
= string
;
197 assert (radix
>= 2 && radix
<= 36);
199 if (*string
== '-') {
202 } else if (*string
== '+')
204 if (base
== 0 && *string
== '0') {
206 if (*(string
+1) == 'x' || *(string
+1) == 'X') {
217 if (*string
>= '0' && *string
< '0' + radix
)
218 digit
= *string
- '0';
220 if (*string
>= '0' && *string
<= '9')
221 digit
= *string
- '0';
222 else if (*string
>= 'a' && *string
< 'a' + radix
- 10)
223 digit
= *string
- 'a' + 10;
224 else if (*string
>= 'A' && *string
< 'A' + radix
- 10)
225 digit
= *string
- 'A' + 10;
229 result
= radix
*result
+ digit
;
234 *tail
= numberFound
? string
: start
;
235 return sign
* result
;
238 /** \brief Locale-independent floating-point parser.
240 * Works similar to strtod. Leading space is NOT skipped. The input
241 * number may have an optional sign. '.' is interpreted as decimal
242 * point and may occur at most once. Optionally the number may end in
243 * [eE]<exponent>, where <exponent> is an integer as recognized by
244 * strToI. In that case the result is number * 10^exponent. After
245 * returning tail points to the first character that is not part of
246 * the floating point number. If no number was found then tail points
247 * to the start of the input string.
249 * Uses two passes for maximum accuracy. */
251 strToF(const XML_Char
*string
, const XML_Char
**tail
)
253 int nDigits
= 0, pointPos
, exponent
;
254 float sign
= 1.0f
, result
= 0.0f
, scale
;
255 const XML_Char
*start
= string
, *numStart
;
258 if (*string
== '-') {
261 } else if (*string
== '+')
264 /* first pass: determine position of decimal point, number of
265 * digits, exponent and the end of the number. */
267 while (*string
>= '0' && *string
<= '9') {
272 if (*string
== '.') {
274 while (*string
>= '0' && *string
<= '9') {
280 /* no digits, no number */
285 if (*string
== 'e' || *string
== 'E') {
286 const XML_Char
*expTail
;
287 exponent
= strToI (string
+1, &expTail
, 10);
288 if (expTail
== string
+1)
296 /* scale of the first digit */
297 scale
= sign
* (float)pow (10.0, (double)(pointPos
-1 + exponent
));
299 /* second pass: parse digits */
301 if (*string
!= '.') {
302 assert (*string
>= '0' && *string
<= '9');
303 result
+= scale
* (float)(*string
- '0');
308 } while (nDigits
> 0);
313 /** \brief Parse a value of a given type. */
315 parseValue(driOptionValue
*v
, driOptionType type
, const XML_Char
*string
)
317 const XML_Char
*tail
= NULL
;
318 /* skip leading white-space */
319 string
+= strspn (string
, " \f\n\r\t\v");
322 if (!strcmp (string
, "false")) {
325 } else if (!strcmp (string
, "true")) {
332 case DRI_ENUM
: /* enum is just a special integer */
334 v
->_int
= strToI (string
, &tail
, 0);
337 v
->_float
= strToF (string
, &tail
);
341 v
->_string
= strndup(string
, STRING_CONF_MAXLEN
);
346 return false; /* empty string (or containing only white-space) */
347 /* skip trailing white space */
349 tail
+= strspn (tail
, " \f\n\r\t\v");
351 return false; /* something left over that is not part of value */
356 /** \brief Parse a list of ranges of type info->type. */
358 parseRanges(driOptionInfo
*info
, const XML_Char
*string
)
360 XML_Char
*cp
, *range
;
362 driOptionRange
*ranges
;
364 XSTRDUP (cp
, string
);
365 /* pass 1: determine the number of ranges (number of commas + 1) */
367 for (nRanges
= 1; *range
; ++range
)
371 if ((ranges
= malloc(nRanges
*sizeof(driOptionRange
))) == NULL
) {
372 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
376 /* pass 2: parse all ranges into preallocated array */
378 for (i
= 0; i
< nRanges
; ++i
) {
381 end
= strchr (range
, ',');
384 sep
= strchr (range
, ':');
385 if (sep
) { /* non-empty interval */
387 if (!parseValue (&ranges
[i
].start
, info
->type
, range
) ||
388 !parseValue (&ranges
[i
].end
, info
->type
, sep
+1))
390 if (info
->type
== DRI_INT
&&
391 ranges
[i
].start
._int
> ranges
[i
].end
._int
)
393 if (info
->type
== DRI_FLOAT
&&
394 ranges
[i
].start
._float
> ranges
[i
].end
._float
)
396 } else { /* empty interval */
397 if (!parseValue (&ranges
[i
].start
, info
->type
, range
))
399 ranges
[i
].end
= ranges
[i
].start
;
411 assert (range
== NULL
);
413 info
->nRanges
= nRanges
;
414 info
->ranges
= ranges
;
418 /** \brief Check if a value is in one of info->ranges. */
420 checkValue(const driOptionValue
*v
, const driOptionInfo
*info
)
423 assert (info
->type
!= DRI_BOOL
); /* should be caught by the parser */
424 if (info
->nRanges
== 0)
426 switch (info
->type
) {
427 case DRI_ENUM
: /* enum is just a special integer */
429 for (i
= 0; i
< info
->nRanges
; ++i
)
430 if (v
->_int
>= info
->ranges
[i
].start
._int
&&
431 v
->_int
<= info
->ranges
[i
].end
._int
)
435 for (i
= 0; i
< info
->nRanges
; ++i
)
436 if (v
->_float
>= info
->ranges
[i
].start
._float
&&
437 v
->_float
<= info
->ranges
[i
].end
._float
)
443 assert (0); /* should never happen */
449 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
452 * Is called from the drivers.
454 * \param f \c printf like format string.
457 __driUtilMessage(const char *f
, ...)
460 const char *libgl_debug
;
462 libgl_debug
=getenv("LIBGL_DEBUG");
463 if (libgl_debug
&& !strstr(libgl_debug
, "quiet")) {
464 fprintf(stderr
, "libGL: ");
466 vfprintf(stderr
, f
, args
);
468 fprintf(stderr
, "\n");
472 /** \brief Output a warning message. */
473 #define XML_WARNING1(msg) do {\
474 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
475 (int) XML_GetCurrentLineNumber(data->parser), \
476 (int) XML_GetCurrentColumnNumber(data->parser)); \
478 #define XML_WARNING(msg, ...) do { \
479 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
480 (int) XML_GetCurrentLineNumber(data->parser), \
481 (int) XML_GetCurrentColumnNumber(data->parser), \
484 /** \brief Output an error message. */
485 #define XML_ERROR1(msg) do { \
486 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
487 (int) XML_GetCurrentLineNumber(data->parser), \
488 (int) XML_GetCurrentColumnNumber(data->parser)); \
490 #define XML_ERROR(msg, ...) do { \
491 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
492 (int) XML_GetCurrentLineNumber(data->parser), \
493 (int) XML_GetCurrentColumnNumber(data->parser), \
496 /** \brief Output a fatal error message and abort. */
497 #define XML_FATAL1(msg) do { \
498 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
500 (int) XML_GetCurrentLineNumber(data->parser), \
501 (int) XML_GetCurrentColumnNumber(data->parser)); \
504 #define XML_FATAL(msg, ...) do { \
505 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
507 (int) XML_GetCurrentLineNumber(data->parser), \
508 (int) XML_GetCurrentColumnNumber(data->parser), \
513 /** \brief Parser context for __driConfigOptions. */
517 driOptionCache
*cache
;
526 /** \brief Elements in __driConfigOptions. */
528 OI_DESCRIPTION
= 0, OI_DRIINFO
, OI_ENUM
, OI_OPTION
, OI_SECTION
, OI_COUNT
530 static const XML_Char
*OptInfoElems
[] = {
531 "description", "driinfo", "enum", "option", "section"
534 /** \brief Parse attributes of an enum element.
536 * We're not actually interested in the data. Just make sure this is ok
537 * for external configuration tools.
540 parseEnumAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
543 const XML_Char
*value
= NULL
, *text
= NULL
;
545 uint32_t opt
= data
->curOption
;
546 for (i
= 0; attr
[i
]; i
+= 2) {
547 if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
548 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
549 else XML_FATAL("illegal enum attribute: %s.", attr
[i
]);
551 if (!value
) XML_FATAL1 ("value attribute missing in enum.");
552 if (!text
) XML_FATAL1 ("text attribute missing in enum.");
553 if (!parseValue (&v
, data
->cache
->info
[opt
].type
, value
))
554 XML_FATAL ("illegal enum value: %s.", value
);
555 if (!checkValue (&v
, &data
->cache
->info
[opt
]))
556 XML_FATAL ("enum value out of valid range: %s.", value
);
559 /** \brief Parse attributes of a description element.
561 * We're not actually interested in the data. Just make sure this is ok
562 * for external configuration tools.
565 parseDescAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
568 const XML_Char
*lang
= NULL
, *text
= NULL
;
569 for (i
= 0; attr
[i
]; i
+= 2) {
570 if (!strcmp (attr
[i
], "lang")) lang
= attr
[i
+1];
571 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
572 else XML_FATAL("illegal description attribute: %s.", attr
[i
]);
574 if (!lang
) XML_FATAL1 ("lang attribute missing in description.");
575 if (!text
) XML_FATAL1 ("text attribute missing in description.");
578 /** \brief Parse attributes of an option element. */
580 parseOptInfoAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
582 enum OptAttr
{OA_DEFAULT
= 0, OA_NAME
, OA_TYPE
, OA_VALID
, OA_COUNT
};
583 static const XML_Char
*optAttr
[] = {"default", "name", "type", "valid"};
584 const XML_Char
*attrVal
[OA_COUNT
] = {NULL
, NULL
, NULL
, NULL
};
585 const char *defaultVal
;
586 driOptionCache
*cache
= data
->cache
;
588 for (i
= 0; attr
[i
]; i
+= 2) {
589 uint32_t attrName
= bsearchStr (attr
[i
], optAttr
, OA_COUNT
);
590 if (attrName
>= OA_COUNT
)
591 XML_FATAL ("illegal option attribute: %s", attr
[i
]);
592 attrVal
[attrName
] = attr
[i
+1];
594 if (!attrVal
[OA_NAME
]) XML_FATAL1 ("name attribute missing in option.");
595 if (!attrVal
[OA_TYPE
]) XML_FATAL1 ("type attribute missing in option.");
596 if (!attrVal
[OA_DEFAULT
]) XML_FATAL1 ("default attribute missing in option.");
598 opt
= findOption (cache
, attrVal
[OA_NAME
]);
599 if (cache
->info
[opt
].name
)
600 XML_FATAL ("option %s redefined.", attrVal
[OA_NAME
]);
601 data
->curOption
= opt
;
603 XSTRDUP (cache
->info
[opt
].name
, attrVal
[OA_NAME
]);
605 if (!strcmp (attrVal
[OA_TYPE
], "bool"))
606 cache
->info
[opt
].type
= DRI_BOOL
;
607 else if (!strcmp (attrVal
[OA_TYPE
], "enum"))
608 cache
->info
[opt
].type
= DRI_ENUM
;
609 else if (!strcmp (attrVal
[OA_TYPE
], "int"))
610 cache
->info
[opt
].type
= DRI_INT
;
611 else if (!strcmp (attrVal
[OA_TYPE
], "float"))
612 cache
->info
[opt
].type
= DRI_FLOAT
;
613 else if (!strcmp (attrVal
[OA_TYPE
], "string"))
614 cache
->info
[opt
].type
= DRI_STRING
;
616 XML_FATAL ("illegal type in option: %s.", attrVal
[OA_TYPE
]);
618 defaultVal
= getenv (cache
->info
[opt
].name
);
619 if (defaultVal
!= NULL
) {
620 /* don't use XML_WARNING, we want the user to see this! */
622 "ATTENTION: default value of option %s overridden by environment.\n",
623 cache
->info
[opt
].name
);
625 defaultVal
= attrVal
[OA_DEFAULT
];
626 if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, defaultVal
))
627 XML_FATAL ("illegal default value for %s: %s.", cache
->info
[opt
].name
, defaultVal
);
629 if (attrVal
[OA_VALID
]) {
630 if (cache
->info
[opt
].type
== DRI_BOOL
)
631 XML_FATAL1 ("boolean option with valid attribute.");
632 if (!parseRanges (&cache
->info
[opt
], attrVal
[OA_VALID
]))
633 XML_FATAL ("illegal valid attribute: %s.", attrVal
[OA_VALID
]);
634 if (!checkValue (&cache
->values
[opt
], &cache
->info
[opt
]))
635 XML_FATAL ("default value out of valid range '%s': %s.",
636 attrVal
[OA_VALID
], defaultVal
);
637 } else if (cache
->info
[opt
].type
== DRI_ENUM
) {
638 XML_FATAL1 ("valid attribute missing in option (mandatory for enums).");
640 cache
->info
[opt
].nRanges
= 0;
641 cache
->info
[opt
].ranges
= NULL
;
645 /** \brief Handler for start element events. */
647 optInfoStartElem(void *userData
, const XML_Char
*name
, const XML_Char
**attr
)
649 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
650 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
654 XML_FATAL1 ("nested <driinfo> elements.");
656 XML_FATAL1 ("attributes specified on <driinfo> element.");
657 data
->inDriInfo
= true;
660 if (!data
->inDriInfo
)
661 XML_FATAL1 ("<section> must be inside <driinfo>.");
663 XML_FATAL1 ("nested <section> elements.");
665 XML_FATAL1 ("attributes specified on <section> element.");
666 data
->inSection
= true;
669 if (!data
->inSection
&& !data
->inOption
)
670 XML_FATAL1 ("<description> must be inside <description> or <option.");
672 XML_FATAL1 ("nested <description> elements.");
674 parseDescAttr (data
, attr
);
677 if (!data
->inSection
)
678 XML_FATAL1 ("<option> must be inside <section>.");
680 XML_FATAL1 ("<option> nested in <description> element.");
682 XML_FATAL1 ("nested <option> elements.");
683 data
->inOption
= true;
684 parseOptInfoAttr (data
, attr
);
687 if (!(data
->inOption
&& data
->inDesc
))
688 XML_FATAL1 ("<enum> must be inside <option> and <description>.");
690 XML_FATAL1 ("nested <enum> elements.");
692 parseEnumAttr (data
, attr
);
695 XML_FATAL ("unknown element: %s.", name
);
699 /** \brief Handler for end element events. */
701 optInfoEndElem(void *userData
, const XML_Char
*name
)
703 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
704 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
707 data
->inDriInfo
= false;
710 data
->inSection
= false;
713 data
->inDesc
= false;
716 data
->inOption
= false;
719 data
->inEnum
= false;
722 assert (0); /* should have been caught by StartElem */
727 driParseOptionInfo(driOptionCache
*info
, const char *configOptions
)
731 struct OptInfoData userData
;
732 struct OptInfoData
*data
= &userData
;
734 /* Make the hash table big enough to fit more than the maximum number of
735 * config options we've ever seen in a driver.
738 info
->info
= calloc(1 << info
->tableSize
, sizeof (driOptionInfo
));
739 info
->values
= calloc(1 << info
->tableSize
, sizeof (driOptionValue
));
740 if (info
->info
== NULL
|| info
->values
== NULL
) {
741 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
745 p
= XML_ParserCreate ("UTF-8"); /* always UTF-8 */
746 XML_SetElementHandler (p
, optInfoStartElem
, optInfoEndElem
);
747 XML_SetUserData (p
, data
);
749 userData
.name
= "__driConfigOptions";
751 userData
.cache
= info
;
752 userData
.inDriInfo
= false;
753 userData
.inSection
= false;
754 userData
.inDesc
= false;
755 userData
.inOption
= false;
756 userData
.inEnum
= false;
757 userData
.curOption
= -1;
759 status
= XML_Parse (p
, configOptions
, strlen (configOptions
), 1);
761 XML_FATAL ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
766 /** \brief Parser context for configuration files. */
770 driOptionCache
*cache
;
772 const char *driverName
, *execName
;
773 uint32_t ignoringDevice
;
774 uint32_t ignoringApp
;
781 /** \brief Elements in configuration files. */
783 OC_APPLICATION
= 0, OC_DEVICE
, OC_DRICONF
, OC_OPTION
, OC_COUNT
785 static const XML_Char
*OptConfElems
[] = {
786 [OC_APPLICATION
] = "application",
787 [OC_DEVICE
] = "device",
788 [OC_DRICONF
] = "driconf",
789 [OC_OPTION
] = "option",
792 /** \brief Parse attributes of a device element. */
794 parseDeviceAttr(struct OptConfData
*data
, const XML_Char
**attr
)
797 const XML_Char
*driver
= NULL
, *screen
= NULL
;
798 for (i
= 0; attr
[i
]; i
+= 2) {
799 if (!strcmp (attr
[i
], "driver")) driver
= attr
[i
+1];
800 else if (!strcmp (attr
[i
], "screen")) screen
= attr
[i
+1];
801 else XML_WARNING("unknown device attribute: %s.", attr
[i
]);
803 if (driver
&& strcmp (driver
, data
->driverName
))
804 data
->ignoringDevice
= data
->inDevice
;
806 driOptionValue screenNum
;
807 if (!parseValue (&screenNum
, DRI_INT
, screen
))
808 XML_WARNING("illegal screen number: %s.", screen
);
809 else if (screenNum
._int
!= data
->screenNum
)
810 data
->ignoringDevice
= data
->inDevice
;
814 /** \brief Parse attributes of an application element. */
816 parseAppAttr(struct OptConfData
*data
, const XML_Char
**attr
)
819 const XML_Char
*exec
= NULL
;
820 for (i
= 0; attr
[i
]; i
+= 2) {
821 if (!strcmp (attr
[i
], "name")) /* not needed here */;
822 else if (!strcmp (attr
[i
], "executable")) exec
= attr
[i
+1];
823 else XML_WARNING("unknown application attribute: %s.", attr
[i
]);
825 if (exec
&& strcmp (exec
, data
->execName
))
826 data
->ignoringApp
= data
->inApp
;
829 /** \brief Parse attributes of an option element. */
831 parseOptConfAttr(struct OptConfData
*data
, const XML_Char
**attr
)
834 const XML_Char
*name
= NULL
, *value
= NULL
;
835 for (i
= 0; attr
[i
]; i
+= 2) {
836 if (!strcmp (attr
[i
], "name")) name
= attr
[i
+1];
837 else if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
838 else XML_WARNING("unknown option attribute: %s.", attr
[i
]);
840 if (!name
) XML_WARNING1 ("name attribute missing in option.");
841 if (!value
) XML_WARNING1 ("value attribute missing in option.");
843 driOptionCache
*cache
= data
->cache
;
844 uint32_t opt
= findOption (cache
, name
);
845 if (cache
->info
[opt
].name
== NULL
)
846 /* don't use XML_WARNING, drirc defines options for all drivers,
847 * but not all drivers support them */
849 else if (getenv (cache
->info
[opt
].name
))
850 /* don't use XML_WARNING, we want the user to see this! */
851 fprintf (stderr
, "ATTENTION: option value of option %s ignored.\n",
852 cache
->info
[opt
].name
);
853 else if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, value
))
854 XML_WARNING ("illegal option value: %s.", value
);
858 /** \brief Handler for start element events. */
860 optConfStartElem(void *userData
, const XML_Char
*name
,
861 const XML_Char
**attr
)
863 struct OptConfData
*data
= (struct OptConfData
*)userData
;
864 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
868 XML_WARNING1 ("nested <driconf> elements.");
870 XML_WARNING1 ("attributes specified on <driconf> element.");
874 if (!data
->inDriConf
)
875 XML_WARNING1 ("<device> should be inside <driconf>.");
877 XML_WARNING1 ("nested <device> elements.");
879 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
880 parseDeviceAttr (data
, attr
);
884 XML_WARNING1 ("<application> should be inside <device>.");
886 XML_WARNING1 ("nested <application> elements.");
888 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
889 parseAppAttr (data
, attr
);
893 XML_WARNING1 ("<option> should be inside <application>.");
895 XML_WARNING1 ("nested <option> elements.");
897 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
898 parseOptConfAttr (data
, attr
);
901 XML_WARNING ("unknown element: %s.", name
);
905 /** \brief Handler for end element events. */
907 optConfEndElem(void *userData
, const XML_Char
*name
)
909 struct OptConfData
*data
= (struct OptConfData
*)userData
;
910 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
916 if (data
->inDevice
-- == data
->ignoringDevice
)
917 data
->ignoringDevice
= 0;
920 if (data
->inApp
-- == data
->ignoringApp
)
921 data
->ignoringApp
= 0;
927 /* unknown element, warning was produced on start tag */;
931 /** \brief Initialize an option cache based on info */
933 initOptionCache(driOptionCache
*cache
, const driOptionCache
*info
)
935 unsigned i
, size
= 1 << info
->tableSize
;
936 cache
->info
= info
->info
;
937 cache
->tableSize
= info
->tableSize
;
938 cache
->values
= malloc((1<<info
->tableSize
) * sizeof (driOptionValue
));
939 if (cache
->values
== NULL
) {
940 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
943 memcpy (cache
->values
, info
->values
,
944 (1<<info
->tableSize
) * sizeof (driOptionValue
));
945 for (i
= 0; i
< size
; ++i
) {
946 if (cache
->info
[i
].type
== DRI_STRING
)
947 XSTRDUP(cache
->values
[i
]._string
, info
->values
[i
]._string
);
951 /** \brief Parse the named configuration file */
953 parseOneConfigFile(XML_Parser p
)
955 #define BUF_SIZE 0x1000
956 struct OptConfData
*data
= (struct OptConfData
*)XML_GetUserData (p
);
960 if ((fd
= open (data
->name
, O_RDONLY
)) == -1) {
961 __driUtilMessage ("Can't open configuration file %s: %s.",
962 data
->name
, strerror (errno
));
968 void *buffer
= XML_GetBuffer (p
, BUF_SIZE
);
970 __driUtilMessage ("Can't allocate parser buffer.");
973 bytesRead
= read (fd
, buffer
, BUF_SIZE
);
974 if (bytesRead
== -1) {
975 __driUtilMessage ("Error reading from configuration file %s: %s.",
976 data
->name
, strerror (errno
));
979 status
= XML_ParseBuffer (p
, bytesRead
, bytesRead
== 0);
981 XML_ERROR ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
993 #define SYSCONFDIR "/etc"
997 driParseConfigFiles(driOptionCache
*cache
, const driOptionCache
*info
,
998 int screenNum
, const char *driverName
)
1000 char *filenames
[2] = { SYSCONFDIR
"/drirc", NULL
};
1003 struct OptConfData userData
;
1005 initOptionCache (cache
, info
);
1007 userData
.cache
= cache
;
1008 userData
.screenNum
= screenNum
;
1009 userData
.driverName
= driverName
;
1010 userData
.execName
= GET_PROGRAM_NAME();
1012 if ((home
= getenv ("HOME"))) {
1013 uint32_t len
= strlen (home
);
1014 filenames
[1] = malloc(len
+ 7+1);
1015 if (filenames
[1] == NULL
)
1016 __driUtilMessage ("Can't allocate memory for %s/.drirc.", home
);
1018 memcpy (filenames
[1], home
, len
);
1019 memcpy (filenames
[1] + len
, "/.drirc", 7+1);
1023 for (i
= 0; i
< 2; ++i
) {
1025 if (filenames
[i
] == NULL
)
1028 p
= XML_ParserCreate (NULL
); /* use encoding specified by file */
1029 XML_SetElementHandler (p
, optConfStartElem
, optConfEndElem
);
1030 XML_SetUserData (p
, &userData
);
1031 userData
.parser
= p
;
1032 userData
.name
= filenames
[i
];
1033 userData
.ignoringDevice
= 0;
1034 userData
.ignoringApp
= 0;
1035 userData
.inDriConf
= 0;
1036 userData
.inDevice
= 0;
1038 userData
.inOption
= 0;
1040 parseOneConfigFile (p
);
1048 driDestroyOptionInfo(driOptionCache
*info
)
1050 driDestroyOptionCache(info
);
1052 uint32_t i
, size
= 1 << info
->tableSize
;
1053 for (i
= 0; i
< size
; ++i
) {
1054 if (info
->info
[i
].name
) {
1055 free(info
->info
[i
].name
);
1056 free(info
->info
[i
].ranges
);
1064 driDestroyOptionCache(driOptionCache
*cache
)
1067 unsigned i
, size
= 1 << cache
->tableSize
;
1068 for (i
= 0; i
< size
; ++i
) {
1069 if (cache
->info
[i
].type
== DRI_STRING
)
1070 free(cache
->values
[i
]._string
);
1073 free(cache
->values
);
1077 driCheckOption(const driOptionCache
*cache
, const char *name
,
1080 uint32_t i
= findOption (cache
, name
);
1081 return cache
->info
[i
].name
!= NULL
&& cache
->info
[i
].type
== type
;
1085 driQueryOptionb(const driOptionCache
*cache
, const char *name
)
1087 uint32_t i
= findOption (cache
, name
);
1088 /* make sure the option is defined and has the correct type */
1089 assert (cache
->info
[i
].name
!= NULL
);
1090 assert (cache
->info
[i
].type
== DRI_BOOL
);
1091 return cache
->values
[i
]._bool
;
1095 driQueryOptioni(const driOptionCache
*cache
, const char *name
)
1097 uint32_t i
= findOption (cache
, name
);
1098 /* make sure the option is defined and has the correct type */
1099 assert (cache
->info
[i
].name
!= NULL
);
1100 assert (cache
->info
[i
].type
== DRI_INT
|| cache
->info
[i
].type
== DRI_ENUM
);
1101 return cache
->values
[i
]._int
;
1105 driQueryOptionf(const driOptionCache
*cache
, const char *name
)
1107 uint32_t i
= findOption (cache
, name
);
1108 /* make sure the option is defined and has the correct type */
1109 assert (cache
->info
[i
].name
!= NULL
);
1110 assert (cache
->info
[i
].type
== DRI_FLOAT
);
1111 return cache
->values
[i
]._float
;
1115 driQueryOptionstr(const driOptionCache
*cache
, const char *name
)
1117 uint32_t i
= findOption (cache
, name
);
1118 /* make sure the option is defined and has the correct type */
1119 assert (cache
->info
[i
].name
!= NULL
);
1120 assert (cache
->info
[i
].type
== DRI_STRING
);
1121 return cache
->values
[i
]._string
;