1 // FGDeviceConfigurationMap.cxx -- a map to access xml device configuration
3 // Written by Torsten Dreyer, started August 2009
4 // Based on work from David Megginson, started May 2001.
6 // Copyright (C) 2009 Torsten Dreyer, Torsten (at) t3r _dot_ de
7 // Copyright (C) 2001 David Megginson, david@megginson.com
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include "FGDeviceConfigurationMap.hxx"
31 #include <boost/foreach.hpp>
33 #include <simgear/misc/sg_dir.hxx>
34 #include <simgear/props/props_io.hxx>
35 #include <simgear/structure/exception.hxx>
37 #include <Main/globals.hxx>
38 #include <Navaids/NavDataCache.hxx>
40 using simgear::PropertyList;
43 FGDeviceConfigurationMap::FGDeviceConfigurationMap( const string& relative_path,
44 SGPropertyNode* nodePath,
45 const std::string& nodeName)
47 // scan for over-ride configurations, loaded via joysticks.xml, etc
48 BOOST_FOREACH(SGPropertyNode_ptr preloaded, nodePath->getChildren(nodeName)) {
49 BOOST_FOREACH(SGPropertyNode* nameProp, preloaded->getChildren("name")) {
50 overrideDict[nameProp->getStringValue()] = preloaded;
51 } // of names iteration
52 } // of defined overrides iteration
54 scan_dir( SGPath(globals->get_fg_home(), relative_path));
55 scan_dir( SGPath(globals->get_fg_root(), relative_path));
58 FGDeviceConfigurationMap::~FGDeviceConfigurationMap()
63 FGDeviceConfigurationMap::configurationForDeviceName(const std::string& name)
65 NameNodeMap::iterator j = overrideDict.find(name);
66 if (j != overrideDict.end()) {
70 // no override, check out list of config files
71 NamePathMap::iterator it = namePathMap.find(name);
72 if (it == namePathMap.end()) {
73 return SGPropertyNode_ptr();
76 SGPropertyNode_ptr result(new SGPropertyNode);
78 readProperties(it->second.str(), result);
79 result->setStringValue("source", it->second.c_str());
80 } catch (sg_exception&) {
81 SG_LOG(SG_INPUT, SG_WARN, "parse failure reading:" << it->second);
87 bool FGDeviceConfigurationMap::hasConfiguration(const std::string& name) const
89 NameNodeMap::const_iterator j = overrideDict.find(name);
90 if (j != overrideDict.end()) {
94 return namePathMap.find(name) != namePathMap.end();
97 void FGDeviceConfigurationMap::scan_dir(const SGPath & path)
99 SG_LOG(SG_INPUT, SG_DEBUG, "Scanning " << path << " for input devices");
103 flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
104 flightgear::NavDataCache::Transaction txn(cache);
106 simgear::Dir dir(path);
107 simgear::PathList children = dir.children(simgear::Dir::TYPE_FILE |
108 simgear::Dir::TYPE_DIR | simgear::Dir::NO_DOT_OR_DOTDOT);
110 BOOST_FOREACH(SGPath path, children) {
113 } else if (path.extension() == "xml") {
114 if (cache->isCachedFileModified(path)) {
115 refreshCacheForFile(path);
117 readCachedData(path);
118 } // of cached file stamp is valid
119 } // of child is a file with '.xml' extension
120 } // of directory children iteration
125 void FGDeviceConfigurationMap::readCachedData(const SGPath& path)
127 flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
128 NamePathMap::iterator it;
129 BOOST_FOREACH(string s, cache->readStringListProperty(path.str())) {
130 // important - only insert if not already present. This ensures
131 // user configs can override those in the base package, since they are
133 it = namePathMap.find(s);
134 if (it == namePathMap.end()) {
135 namePathMap.insert(std::make_pair(s, path));
137 } // of cached names iteration
140 void FGDeviceConfigurationMap::refreshCacheForFile(const SGPath& path)
142 SG_LOG(SG_INPUT, SG_DEBUG, "Reading device file " << path.str());
143 SGPropertyNode_ptr n(new SGPropertyNode);
145 readProperties(path.str(), n);
146 } catch (sg_exception&) {
147 SG_LOG(SG_INPUT, SG_ALERT, "parse failure reading:" << path);
151 NamePathMap::iterator it;
153 BOOST_FOREACH(SGPropertyNode* nameProp, n->getChildren("name")) {
154 names.push_back(nameProp->getStringValue());
155 // same comment as readCachedData: only insert if not already present
156 it = namePathMap.find(names.back());
157 if (it == namePathMap.end()) {
158 namePathMap.insert(std::make_pair(names.back(), path));
162 flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
163 cache->stampCacheFile(path);
164 cache->writeStringListProperty(path.str(), names);