blob: 5528358f4c9561c66ee6936ac23e6a8ebb048d25 [file] [log] [blame]
Jon Ashburn47e92892014-12-22 12:04:40 -07001/**************************************************************************
2 *
3 * Copyright 2014 Lunarg, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 **************************************************************************/
25#include <fstream>
26#include <string>
27#include <map>
28#include <string.h>
Jon Ashburn0f1dbf12015-01-13 17:24:01 -070029#include <xglLayer.h>
Ian Elliott5ada89b2015-02-12 11:36:03 -070030#include "loader_platform.h"
Jon Ashburn47e92892014-12-22 12:04:40 -070031#include "layers_config.h"
Ian Elliott20f06872015-02-12 17:08:34 -070032// The following is #included again to catch certain OS-specific functions
33// being used:
34#include "loader_platform.h"
Jon Ashburn47e92892014-12-22 12:04:40 -070035
36#define MAX_CHARS_PER_LINE 4096
37
38class ConfigFile
39{
40public:
41 ConfigFile();
42 ~ConfigFile();
43
44 const char *getOption(const std::string &_option);
Jon Ashburn0f1dbf12015-01-13 17:24:01 -070045 void setOption(const std::string &_option, const std::string &_val);
46
Jon Ashburn47e92892014-12-22 12:04:40 -070047private:
48 bool m_fileIsParsed;
49 std::map<std::string, std::string> m_valueMap;
50
51 void parseFile(const char *filename);
52};
53
54static ConfigFile g_configFileObj;
Jon Ashburn0f1dbf12015-01-13 17:24:01 -070055
56static unsigned int convertStringEnumVal(const char *_enum)
57{
58 // only handles single enum values
59 if (!strcmp(_enum, "XGL_DBG_LAYER_ACTION_IGNORE"))
60 return XGL_DBG_LAYER_ACTION_IGNORE;
61 else if (!strcmp(_enum, "XGL_DBG_LAYER_ACTION_CALLBACK"))
62 return XGL_DBG_LAYER_ACTION_CALLBACK;
63 else if (!strcmp(_enum, "XGL_DBG_LAYER_ACTION_LOG_MSG"))
64 return XGL_DBG_LAYER_ACTION_LOG_MSG;
65 else if (!strcmp(_enum, "XGL_DBG_LAYER_ACTION_BREAK"))
66 return XGL_DBG_LAYER_ACTION_BREAK;
67 else if (!strcmp(_enum, "XGL_DBG_LAYER_LEVEL_INFO"))
68 return XGL_DBG_LAYER_LEVEL_INFO;
69 else if (!strcmp(_enum, "XGL_DBG_LAYER_LEVEL_WARN"))
70 return XGL_DBG_LAYER_LEVEL_WARN;
71 else if (!strcmp(_enum, "XGL_DBG_LAYER_LEVEL_PERF_WARN"))
72 return XGL_DBG_LAYER_LEVEL_PERF_WARN;
73 else if (!strcmp(_enum, "XGL_DBG_LAYER_LEVEL_ERROR"))
74 return XGL_DBG_LAYER_LEVEL_ERROR;
75 else if (!strcmp(_enum, "XGL_DBG_LAYER_LEVEL_NONE"))
76 return XGL_DBG_LAYER_LEVEL_NONE;
77 return 0;
78}
Jon Ashburn47e92892014-12-22 12:04:40 -070079const char *getLayerOption(const char *_option)
80{
Jon Ashburn47e92892014-12-22 12:04:40 -070081 return g_configFileObj.getOption(_option);
82}
83
Jon Ashburn0f1dbf12015-01-13 17:24:01 -070084void setLayerOptionEnum(const char *_option, const char *_valEnum)
85{
86 unsigned int val = convertStringEnumVal(_valEnum);
87 char strVal[24];
88 snprintf(strVal, 24, "%u", val);
89 g_configFileObj.setOption(_option, strVal);
90}
91
92void setLayerOption(const char *_option, const char *_val)
93{
94 g_configFileObj.setOption(_option, _val);
95}
96
Jon Ashburn47e92892014-12-22 12:04:40 -070097ConfigFile::ConfigFile() : m_fileIsParsed(false)
98{
99}
100
101ConfigFile::~ConfigFile()
102{
103}
104
105const char *ConfigFile::getOption(const std::string &_option)
106{
107 std::map<std::string, std::string>::const_iterator it;
108 if (!m_fileIsParsed)
109 {
110 parseFile("xgl_layer_settings.txt");
111 }
112
113 if ((it = m_valueMap.find(_option)) == m_valueMap.end())
114 return NULL;
115 else
116 return it->second.c_str();
117}
118
Jon Ashburn0f1dbf12015-01-13 17:24:01 -0700119void ConfigFile::setOption(const std::string &_option, const std::string &_val)
120{
121 if (!m_fileIsParsed)
122 {
123 parseFile("xgl_layer_settings.txt");
124 }
125
126 m_valueMap[_option] = _val;
127}
128
Jon Ashburn47e92892014-12-22 12:04:40 -0700129void ConfigFile::parseFile(const char *filename)
130{
131 std::ifstream file;
132 char buf[MAX_CHARS_PER_LINE];
133
134 m_fileIsParsed = true;
135 m_valueMap.clear();
136
137 file.open(filename);
138 if (!file.good())
139 return;
140
141 // read tokens from the file and form option, value pairs
142 file.getline(buf, MAX_CHARS_PER_LINE);
143 while (!file.eof())
144 {
145 char option[512];
146 char value[512];
147
148 char *pComment;
149
150 //discard any comments delimited by '#' in the line
151 pComment = strchr(buf, '#');
152 if (pComment)
153 *pComment = '\0';
154
155 if (sscanf(buf, " %511[^\n\t =] = %511[^\n \t]", option, value) == 2)
156 {
157 std::string optStr(option);
158 std::string valStr(value);
159 m_valueMap[optStr] = valStr;
160 }
161 file.getline(buf, MAX_CHARS_PER_LINE);
162 }
163}
164