vk_layer_config.cpp revision 0f1dbf15031846dee079e13a243de1ddfb65aaff
147e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn/**************************************************************************
247e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn *
347e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn * Copyright 2014 Lunarg, Inc.
447e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn * All Rights Reserved.
547e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn *
647e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn * Permission is hereby granted, free of charge, to any person obtaining a copy
747e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn * of this software and associated documentation files (the "Software"), to deal
847e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn * in the Software without restriction, including without limitation the rights
947e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1047e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn * copies of the Software, and to permit persons to whom the Software is
1147e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn * furnished to do so, subject to the following conditions:
1247e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn *
1347e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn * The above copyright notice and this permission notice shall be included in
1447e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn * all copies or substantial portions of the Software.
1547e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn *
1647e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1747e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1847e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1947e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2047e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2147e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2247e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn * THE SOFTWARE.
2347e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn *
2447e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn **************************************************************************/
2547e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn#include <fstream>
2647e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn#include <string>
2747e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn#include <map>
2847e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn#include <string.h>
290f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn#include <xglLayer.h>
3047e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn#include "layers_config.h"
3147e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn
3247e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn#define MAX_CHARS_PER_LINE 4096
3347e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn
3447e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburnclass ConfigFile
3547e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn{
3647e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburnpublic:
3747e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn    ConfigFile();
3847e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn    ~ConfigFile();
3947e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn
4047e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn    const char *getOption(const std::string &_option);
410f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn    void setOption(const std::string &_option, const std::string &_val);
420f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn
4347e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburnprivate:
4447e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn    bool m_fileIsParsed;
4547e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn    std::map<std::string, std::string> m_valueMap;
4647e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn
4747e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn    void parseFile(const char *filename);
4847e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn};
4947e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn
5047e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburnstatic ConfigFile g_configFileObj;
510f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn
520f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburnstatic unsigned int convertStringEnumVal(const char *_enum)
530f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn{
540f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn    // only handles single enum values
550f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn    if (!strcmp(_enum, "XGL_DBG_LAYER_ACTION_IGNORE"))
560f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn        return XGL_DBG_LAYER_ACTION_IGNORE;
570f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn    else if (!strcmp(_enum, "XGL_DBG_LAYER_ACTION_CALLBACK"))
580f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn        return XGL_DBG_LAYER_ACTION_CALLBACK;
590f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn    else if (!strcmp(_enum, "XGL_DBG_LAYER_ACTION_LOG_MSG"))
600f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn        return XGL_DBG_LAYER_ACTION_LOG_MSG;
610f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn    else if (!strcmp(_enum, "XGL_DBG_LAYER_ACTION_BREAK"))
620f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn        return XGL_DBG_LAYER_ACTION_BREAK;
630f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn    else if (!strcmp(_enum, "XGL_DBG_LAYER_LEVEL_INFO"))
640f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn        return XGL_DBG_LAYER_LEVEL_INFO;
650f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn    else if (!strcmp(_enum, "XGL_DBG_LAYER_LEVEL_WARN"))
660f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn        return XGL_DBG_LAYER_LEVEL_WARN;
670f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn    else if (!strcmp(_enum, "XGL_DBG_LAYER_LEVEL_PERF_WARN"))
680f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn        return XGL_DBG_LAYER_LEVEL_PERF_WARN;
690f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn    else if (!strcmp(_enum, "XGL_DBG_LAYER_LEVEL_ERROR"))
700f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn        return XGL_DBG_LAYER_LEVEL_ERROR;
710f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn    else if (!strcmp(_enum, "XGL_DBG_LAYER_LEVEL_NONE"))
720f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn        return XGL_DBG_LAYER_LEVEL_NONE;
730f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn    return 0;
740f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn}
7547e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburnconst char *getLayerOption(const char *_option)
7647e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn{
7747e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn    return g_configFileObj.getOption(_option);
7847e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn}
7947e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn
800f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburnvoid setLayerOptionEnum(const char *_option, const char *_valEnum)
810f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn{
820f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn    unsigned int val = convertStringEnumVal(_valEnum);
830f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn    char strVal[24];
840f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn    snprintf(strVal, 24, "%u", val);
850f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn    g_configFileObj.setOption(_option, strVal);
860f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn}
870f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn
880f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburnvoid setLayerOption(const char *_option, const char *_val)
890f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn{
900f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn    g_configFileObj.setOption(_option, _val);
910f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn}
920f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn
9347e9289b849a08f0830ddf729fe6637ec081c731Jon AshburnConfigFile::ConfigFile() : m_fileIsParsed(false)
9447e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn{
9547e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn}
9647e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn
9747e9289b849a08f0830ddf729fe6637ec081c731Jon AshburnConfigFile::~ConfigFile()
9847e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn{
9947e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn}
10047e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn
10147e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburnconst char *ConfigFile::getOption(const std::string &_option)
10247e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn{
10347e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn    std::map<std::string, std::string>::const_iterator it;
10447e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn    if (!m_fileIsParsed)
10547e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn    {
10647e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn        parseFile("xgl_layer_settings.txt");
10747e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn    }
10847e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn
10947e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn    if ((it = m_valueMap.find(_option)) == m_valueMap.end())
11047e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn        return NULL;
11147e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn    else
11247e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn        return it->second.c_str();
11347e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn}
11447e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn
1150f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburnvoid ConfigFile::setOption(const std::string &_option, const std::string &_val)
1160f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn{
1170f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn    if (!m_fileIsParsed)
1180f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn    {
1190f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn        parseFile("xgl_layer_settings.txt");
1200f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn    }
1210f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn
1220f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn    m_valueMap[_option] = _val;
1230f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn}
1240f1dbf15031846dee079e13a243de1ddfb65aaffJon Ashburn
12547e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburnvoid ConfigFile::parseFile(const char *filename)
12647e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn{
12747e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn    std::ifstream file;
12847e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn    char buf[MAX_CHARS_PER_LINE];
12947e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn
13047e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn    m_fileIsParsed = true;
13147e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn    m_valueMap.clear();
13247e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn
13347e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn    file.open(filename);
13447e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn    if (!file.good())
13547e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn        return;
13647e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn
13747e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn    // read tokens from the file and form option, value pairs
13847e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn    file.getline(buf, MAX_CHARS_PER_LINE);
13947e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn    while (!file.eof())
14047e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn    {
14147e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn        char option[512];
14247e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn        char value[512];
14347e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn
14447e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn        char *pComment;
14547e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn
14647e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn        //discard any comments delimited by '#' in the line
14747e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn        pComment = strchr(buf, '#');
14847e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn        if (pComment)
14947e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn            *pComment = '\0';
15047e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn
15147e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn        if (sscanf(buf, " %511[^\n\t =] = %511[^\n \t]", option, value) == 2)
15247e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn        {
15347e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn            std::string optStr(option);
15447e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn            std::string valStr(value);
15547e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn            m_valueMap[optStr] = valStr;
15647e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn        }
15747e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn        file.getline(buf, MAX_CHARS_PER_LINE);
15847e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn    }
15947e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn}
16047e9289b849a08f0830ddf729fe6637ec081c731Jon Ashburn
161