blob: 134bf162d76e47ad1be0fee20c5b781da0e04d95 [file] [log] [blame]
Jon Ashburnbe582642014-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>
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060029#include <vkLayer.h>
Ian Elliottf15eb8a2015-02-12 11:36:03 -070030#include "loader_platform.h"
Jon Ashburnbe582642014-12-22 12:04:40 -070031#include "layers_config.h"
Ian Elliott655cad72015-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 Ashburnbe582642014-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 Ashburndec60512015-01-13 17:24:01 -070045 void setOption(const std::string &_option, const std::string &_val);
46
Jon Ashburnbe582642014-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 Ashburndec60512015-01-13 17:24:01 -070055
56static unsigned int convertStringEnumVal(const char *_enum)
57{
58 // only handles single enum values
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060059 if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_IGNORE"))
60 return VK_DBG_LAYER_ACTION_IGNORE;
61 else if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_CALLBACK"))
62 return VK_DBG_LAYER_ACTION_CALLBACK;
63 else if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_LOG_MSG"))
64 return VK_DBG_LAYER_ACTION_LOG_MSG;
65 else if (!strcmp(_enum, "VK_DBG_LAYER_ACTION_BREAK"))
66 return VK_DBG_LAYER_ACTION_BREAK;
67 else if (!strcmp(_enum, "VK_DBG_LAYER_LEVEL_INFO"))
68 return VK_DBG_LAYER_LEVEL_INFO;
69 else if (!strcmp(_enum, "VK_DBG_LAYER_LEVEL_WARN"))
70 return VK_DBG_LAYER_LEVEL_WARN;
71 else if (!strcmp(_enum, "VK_DBG_LAYER_LEVEL_PERF_WARN"))
72 return VK_DBG_LAYER_LEVEL_PERF_WARN;
73 else if (!strcmp(_enum, "VK_DBG_LAYER_LEVEL_ERROR"))
74 return VK_DBG_LAYER_LEVEL_ERROR;
75 else if (!strcmp(_enum, "VK_DBG_LAYER_LEVEL_NONE"))
76 return VK_DBG_LAYER_LEVEL_NONE;
Jon Ashburndec60512015-01-13 17:24:01 -070077 return 0;
78}
Jon Ashburnbe582642014-12-22 12:04:40 -070079const char *getLayerOption(const char *_option)
80{
Jon Ashburnbe582642014-12-22 12:04:40 -070081 return g_configFileObj.getOption(_option);
82}
83
Jon Ashburna8aa8372015-03-03 15:07:15 -070084bool getLayerOptionEnum(const char *_option, uint32_t *optionDefault)
Mark Lobodzinski8a2b75b2015-02-25 12:23:20 -060085{
Jon Ashburna8aa8372015-03-03 15:07:15 -070086 bool res;
Mark Lobodzinski8a2b75b2015-02-25 12:23:20 -060087 const char *option = (g_configFileObj.getOption(_option));
88 if (option != NULL) {
Jon Ashburna8aa8372015-03-03 15:07:15 -070089 *optionDefault = convertStringEnumVal(option);
90 res = false;
Mark Lobodzinski0d6e66d2015-02-25 15:14:06 -060091 } else {
Jon Ashburna8aa8372015-03-03 15:07:15 -070092 res = true;
Mark Lobodzinski8a2b75b2015-02-25 12:23:20 -060093 }
Jon Ashburna8aa8372015-03-03 15:07:15 -070094 return res;
Mark Lobodzinski8a2b75b2015-02-25 12:23:20 -060095}
96
Jon Ashburndec60512015-01-13 17:24:01 -070097void setLayerOptionEnum(const char *_option, const char *_valEnum)
98{
99 unsigned int val = convertStringEnumVal(_valEnum);
100 char strVal[24];
101 snprintf(strVal, 24, "%u", val);
102 g_configFileObj.setOption(_option, strVal);
103}
104
105void setLayerOption(const char *_option, const char *_val)
106{
107 g_configFileObj.setOption(_option, _val);
108}
109
Jon Ashburnbe582642014-12-22 12:04:40 -0700110ConfigFile::ConfigFile() : m_fileIsParsed(false)
111{
112}
113
114ConfigFile::~ConfigFile()
115{
116}
117
118const char *ConfigFile::getOption(const std::string &_option)
119{
120 std::map<std::string, std::string>::const_iterator it;
121 if (!m_fileIsParsed)
122 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600123 parseFile("vk_layer_settings.txt");
Jon Ashburnbe582642014-12-22 12:04:40 -0700124 }
125
126 if ((it = m_valueMap.find(_option)) == m_valueMap.end())
127 return NULL;
128 else
129 return it->second.c_str();
130}
131
Jon Ashburndec60512015-01-13 17:24:01 -0700132void ConfigFile::setOption(const std::string &_option, const std::string &_val)
133{
134 if (!m_fileIsParsed)
135 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600136 parseFile("vk_layer_settings.txt");
Jon Ashburndec60512015-01-13 17:24:01 -0700137 }
138
139 m_valueMap[_option] = _val;
140}
141
Jon Ashburnbe582642014-12-22 12:04:40 -0700142void ConfigFile::parseFile(const char *filename)
143{
144 std::ifstream file;
145 char buf[MAX_CHARS_PER_LINE];
146
147 m_fileIsParsed = true;
148 m_valueMap.clear();
149
150 file.open(filename);
151 if (!file.good())
152 return;
153
154 // read tokens from the file and form option, value pairs
155 file.getline(buf, MAX_CHARS_PER_LINE);
156 while (!file.eof())
157 {
158 char option[512];
159 char value[512];
160
161 char *pComment;
162
163 //discard any comments delimited by '#' in the line
164 pComment = strchr(buf, '#');
165 if (pComment)
166 *pComment = '\0';
167
168 if (sscanf(buf, " %511[^\n\t =] = %511[^\n \t]", option, value) == 2)
169 {
170 std::string optStr(option);
171 std::string valStr(value);
172 m_valueMap[optStr] = valStr;
173 }
174 file.getline(buf, MAX_CHARS_PER_LINE);
175 }
176}
177