101e04c3fSmrg/*
201e04c3fSmrg * XML DRI client-side driver configuration
301e04c3fSmrg * Copyright (C) 2003 Felix Kuehling
401e04c3fSmrg *
501e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
601e04c3fSmrg * copy of this software and associated documentation files (the "Software"),
701e04c3fSmrg * to deal in the Software without restriction, including without limitation
801e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
901e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the
1001e04c3fSmrg * Software is furnished to do so, subject to the following conditions:
1101e04c3fSmrg *
1201e04c3fSmrg * The above copyright notice and this permission notice shall be included
1301e04c3fSmrg * in all copies or substantial portions of the Software.
1401e04c3fSmrg *
1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1601e04c3fSmrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1801e04c3fSmrg * FELIX KUEHLING, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
1901e04c3fSmrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
2001e04c3fSmrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
2101e04c3fSmrg * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2201e04c3fSmrg *
2301e04c3fSmrg */
2401e04c3fSmrg/**
2501e04c3fSmrg * \file xmlconfig.h
2601e04c3fSmrg * \brief Driver-independent client-side part of the XML configuration
2701e04c3fSmrg * \author Felix Kuehling
2801e04c3fSmrg */
2901e04c3fSmrg
3001e04c3fSmrg#ifndef __XMLCONFIG_H
3101e04c3fSmrg#define __XMLCONFIG_H
3201e04c3fSmrg
3301e04c3fSmrg#include "util/mesa-sha1.h"
3401e04c3fSmrg#include "util/ralloc.h"
357ec681f3Smrg#include <stdint.h>
3601e04c3fSmrg#include <string.h>
3701e04c3fSmrg
387ec681f3Smrg#ifdef __cplusplus
397ec681f3Smrgextern "C" {
407ec681f3Smrg#endif
417ec681f3Smrg
427ec681f3Smrg#if defined(ANDROID) || defined(_WIN32)
437ec681f3Smrg#define WITH_XMLCONFIG 0
447ec681f3Smrg#else
457ec681f3Smrg#define WITH_XMLCONFIG 1
467ec681f3Smrg#endif
477ec681f3Smrg
487ec681f3Smrg#define STRING_CONF_MAXLEN 1024
4901e04c3fSmrg
5001e04c3fSmrg/** \brief Option data types */
5101e04c3fSmrgtypedef enum driOptionType {
527ec681f3Smrg   DRI_BOOL, DRI_ENUM, DRI_INT, DRI_FLOAT, DRI_STRING, DRI_SECTION
5301e04c3fSmrg} driOptionType;
5401e04c3fSmrg
5501e04c3fSmrg/** \brief Option value */
5601e04c3fSmrgtypedef union driOptionValue {
577ec681f3Smrg   unsigned char _bool; /**< \brief Boolean */
587ec681f3Smrg   int _int;      /**< \brief Integer or Enum */
597ec681f3Smrg   float _float;  /**< \brief Floating-point */
607ec681f3Smrg   char *_string;   /**< \brief String */
6101e04c3fSmrg} driOptionValue;
6201e04c3fSmrg
6301e04c3fSmrg/** \brief Single range of valid values
6401e04c3fSmrg *
6501e04c3fSmrg * For empty ranges (a single value) start == end */
6601e04c3fSmrgtypedef struct driOptionRange {
677ec681f3Smrg   driOptionValue start; /**< \brief Start */
687ec681f3Smrg   driOptionValue end;   /**< \brief End */
6901e04c3fSmrg} driOptionRange;
7001e04c3fSmrg
7101e04c3fSmrg/** \brief Information about an option */
7201e04c3fSmrgtypedef struct driOptionInfo {
737ec681f3Smrg   char *name;             /**< \brief Name */
747ec681f3Smrg   driOptionType type;     /**< \brief Type */
757ec681f3Smrg   driOptionRange range;   /**< \brief Valid range of the option (or 0:0) */
7601e04c3fSmrg} driOptionInfo;
7701e04c3fSmrg
7801e04c3fSmrg/** \brief Option cache
7901e04c3fSmrg *
8001e04c3fSmrg * \li One in <driver>Screen caching option info and the default values
8101e04c3fSmrg * \li One in each <driver>Context with the actual values for that context */
8201e04c3fSmrgtypedef struct driOptionCache {
837ec681f3Smrg   driOptionInfo *info;
847ec681f3Smrg   /**< \brief Array of option infos
857ec681f3Smrg    *
867ec681f3Smrg    * Points to the same array in the screen and all contexts */
877ec681f3Smrg   driOptionValue *values;
887ec681f3Smrg   /**< \brief Array of option values
897ec681f3Smrg    *
907ec681f3Smrg    * \li Default values in screen
917ec681f3Smrg    * \li Actual values in contexts
927ec681f3Smrg    */
937ec681f3Smrg   unsigned int tableSize;
947ec681f3Smrg   /**< \brief Size of the arrays
957ec681f3Smrg    *
967ec681f3Smrg    * In the current implementation it's not actually a size but log2(size).
977ec681f3Smrg    * The value is the same in the screen and all contexts. */
9801e04c3fSmrg} driOptionCache;
9901e04c3fSmrg
1007ec681f3Smrgtypedef struct driEnumDescription {
1017ec681f3Smrg   int value;
1027ec681f3Smrg   const char *desc;
1037ec681f3Smrg} driEnumDescription;
1047ec681f3Smrg
1057ec681f3Smrg/**
1067ec681f3Smrg * Struct for a driver's definition of an option, its default value, and the
1077ec681f3Smrg * text documenting it.
1087ec681f3Smrg */
1097ec681f3Smrgtypedef struct driOptionDescription {
1107ec681f3Smrg   const char *desc;
1117ec681f3Smrg
1127ec681f3Smrg   driOptionInfo info;
1137ec681f3Smrg   driOptionValue value;
1147ec681f3Smrg   driEnumDescription enums[4];
1157ec681f3Smrg} driOptionDescription;
1167ec681f3Smrg
1177ec681f3Smrg/** Returns an XML string describing the options for the driver. */
1187ec681f3Smrgchar *
1197ec681f3SmrgdriGetOptionsXml(const driOptionDescription *configOptions, unsigned numOptions);
1207ec681f3Smrg
1217ec681f3Smrg/** \brief Parse driconf option array from configOptions
12201e04c3fSmrg *
1237ec681f3Smrg * To be called in <driver>CreateScreen
12401e04c3fSmrg *
12501e04c3fSmrg * \param info    pointer to a driOptionCache that will store the option info
1267ec681f3Smrg * \param configOptions   Array of XML document describing available configuration opts
12701e04c3fSmrg *
12801e04c3fSmrg * For the option information to be available to external configuration tools
12901e04c3fSmrg * it must be a public symbol __driConfigOptions. It is also passed as a
13001e04c3fSmrg * parameter to driParseOptionInfo in order to avoid driver-independent code
13101e04c3fSmrg * depending on symbols in driver-specific code. */
1327ec681f3Smrgvoid driParseOptionInfo(driOptionCache *info,
1337ec681f3Smrg                        const driOptionDescription *configOptions,
1347ec681f3Smrg                        unsigned numOptions);
13501e04c3fSmrg/** \brief Initialize option cache from info and parse configuration files
13601e04c3fSmrg *
1377ec681f3Smrg * To be called in <driver>CreateContext. screenNum, driverName,
1387ec681f3Smrg * kernelDriverName, applicationName and engineName select device sections. */
1397ec681f3Smrgvoid driParseConfigFiles(driOptionCache *cache, const driOptionCache *info,
1407ec681f3Smrg                         int screenNum, const char *driverName,
1417ec681f3Smrg                         const char *kernelDriverName,
1427ec681f3Smrg                         const char *deviceName,
1437ec681f3Smrg                         const char *applicationName, uint32_t applicationVersion,
1447ec681f3Smrg                         const char *engineName, uint32_t engineVersion);
14501e04c3fSmrg/** \brief Destroy option info
14601e04c3fSmrg *
14701e04c3fSmrg * To be called in <driver>DestroyScreen */
1487ec681f3Smrgvoid driDestroyOptionInfo(driOptionCache *info);
14901e04c3fSmrg/** \brief Destroy option cache
15001e04c3fSmrg *
15101e04c3fSmrg * To be called in <driver>DestroyContext */
1527ec681f3Smrgvoid driDestroyOptionCache(driOptionCache *cache);
15301e04c3fSmrg
15401e04c3fSmrg/** \brief Check if there exists a certain option */
1557ec681f3Smrgunsigned char driCheckOption(const driOptionCache *cache, const char *name,
1567ec681f3Smrg                             driOptionType type);
15701e04c3fSmrg
15801e04c3fSmrg/** \brief Query a boolean option value */
1597ec681f3Smrgunsigned char driQueryOptionb(const driOptionCache *cache, const char *name);
16001e04c3fSmrg/** \brief Query an integer option value */
1617ec681f3Smrgint driQueryOptioni(const driOptionCache *cache, const char *name);
16201e04c3fSmrg/** \brief Query a floating-point option value */
1637ec681f3Smrgfloat driQueryOptionf(const driOptionCache *cache, const char *name);
16401e04c3fSmrg/** \brief Query a string option value */
1657ec681f3Smrgchar *driQueryOptionstr(const driOptionCache *cache, const char *name);
1667ec681f3Smrg
1677ec681f3Smrg/* Overrides for the unit tests to control drirc parsing. */
1687ec681f3Smrgvoid driInjectDataDir(const char *dir);
1697ec681f3Smrgvoid driInjectExecName(const char *exec);
17001e04c3fSmrg
17101e04c3fSmrg/**
17201e04c3fSmrg * Returns a hash of the options for this application.
17301e04c3fSmrg */
17401e04c3fSmrgstatic inline void
17501e04c3fSmrgdriComputeOptionsSha1(const driOptionCache *cache, unsigned char *sha1)
17601e04c3fSmrg{
17701e04c3fSmrg   void *ctx = ralloc_context(NULL);
17801e04c3fSmrg   char *dri_options = ralloc_strdup(ctx, "");
17901e04c3fSmrg
18001e04c3fSmrg   for (int i = 0; i < 1 << cache->tableSize; i++) {
18101e04c3fSmrg      if (cache->info[i].name == NULL)
18201e04c3fSmrg         continue;
18301e04c3fSmrg
18401e04c3fSmrg      bool ret = false;
18501e04c3fSmrg      switch (cache->info[i].type) {
18601e04c3fSmrg      case DRI_BOOL:
18701e04c3fSmrg         ret = ralloc_asprintf_append(&dri_options, "%s:%u,",
18801e04c3fSmrg                                      cache->info[i].name,
18901e04c3fSmrg                                      cache->values[i]._bool);
19001e04c3fSmrg         break;
19101e04c3fSmrg      case DRI_INT:
19201e04c3fSmrg      case DRI_ENUM:
19301e04c3fSmrg         ret = ralloc_asprintf_append(&dri_options, "%s:%d,",
19401e04c3fSmrg                                      cache->info[i].name,
19501e04c3fSmrg                                      cache->values[i]._int);
19601e04c3fSmrg         break;
19701e04c3fSmrg      case DRI_FLOAT:
19801e04c3fSmrg         ret = ralloc_asprintf_append(&dri_options, "%s:%f,",
19901e04c3fSmrg                                      cache->info[i].name,
20001e04c3fSmrg                                      cache->values[i]._float);
20101e04c3fSmrg         break;
20201e04c3fSmrg      case DRI_STRING:
20301e04c3fSmrg         ret = ralloc_asprintf_append(&dri_options, "%s:%s,",
20401e04c3fSmrg                                      cache->info[i].name,
20501e04c3fSmrg                                      cache->values[i]._string);
20601e04c3fSmrg         break;
20701e04c3fSmrg      default:
20801e04c3fSmrg         unreachable("unsupported dri config type!");
20901e04c3fSmrg      }
21001e04c3fSmrg
21101e04c3fSmrg      if (!ret) {
21201e04c3fSmrg         break;
21301e04c3fSmrg      }
21401e04c3fSmrg   }
21501e04c3fSmrg
21601e04c3fSmrg   _mesa_sha1_compute(dri_options, strlen(dri_options), sha1);
21701e04c3fSmrg   ralloc_free(ctx);
21801e04c3fSmrg}
21901e04c3fSmrg
2207ec681f3Smrg#ifdef __cplusplus
2217ec681f3Smrg} /* extern C */
2227ec681f3Smrg#endif
2237ec681f3Smrg
22401e04c3fSmrg#endif
225