X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FInput%2FFGDeviceConfigurationMap.cxx;h=d62a32f9e88bd243976fc91fa94e56bc0bdc36cb;hb=5151f7f5e0ba7c7e6688bdfbc4cea365196e1ed3;hp=5ad2a0bd7435f3786c0049777386bcce6ed52e91;hpb=664bb77d95c2e74b49d35847a9c47854a25f5012;p=flightgear.git diff --git a/src/Input/FGDeviceConfigurationMap.cxx b/src/Input/FGDeviceConfigurationMap.cxx index 5ad2a0bd7..d62a32f9e 100644 --- a/src/Input/FGDeviceConfigurationMap.cxx +++ b/src/Input/FGDeviceConfigurationMap.cxx @@ -26,63 +26,139 @@ # include #endif -#include - #include "FGDeviceConfigurationMap.hxx" -#include +#include +#include #include +#include + #include
+#include using simgear::PropertyList; +using std::string; -FGDeviceConfigurationMap::FGDeviceConfigurationMap( const char * relative_path, SGPropertyNode_ptr aBase, const char * aChildname ) : - base(aBase), - childname(aChildname) +FGDeviceConfigurationMap::FGDeviceConfigurationMap( const string& relative_path, + SGPropertyNode* nodePath, + const std::string& nodeName) { - SGPath path(globals->get_fg_root()); - path.append( relative_path ); + // scan for over-ride configurations, loaded via joysticks.xml, etc + BOOST_FOREACH(SGPropertyNode_ptr preloaded, nodePath->getChildren(nodeName)) { + BOOST_FOREACH(SGPropertyNode* nameProp, preloaded->getChildren("name")) { + overrideDict[nameProp->getStringValue()] = preloaded; + } // of names iteration + } // of defined overrides iteration + + scan_dir( SGPath(globals->get_fg_home(), relative_path)); + scan_dir( SGPath(globals->get_fg_root(), relative_path)); +} - int index = 1000; - scan_dir( path, &index); +FGDeviceConfigurationMap::~FGDeviceConfigurationMap() +{ +} - PropertyList childNodes = base->getChildren(childname); - for (int k = (int)childNodes.size() - 1; k >= 0; k--) { - SGPropertyNode *n = childNodes[k]; - PropertyList names = n->getChildren("name"); - if (names.size() ) // && (n->getChildren("axis").size() || n->getChildren("button").size())) - for (unsigned int j = 0; j < names.size(); j++) - (*this)[names[j]->getStringValue()] = n; +SGPropertyNode_ptr +FGDeviceConfigurationMap::configurationForDeviceName(const std::string& name) +{ + NameNodeMap::iterator j = overrideDict.find(name); + if (j != overrideDict.end()) { + return j->second; + } + +// no override, check out list of config files + NamePathMap::iterator it = namePathMap.find(name); + if (it == namePathMap.end()) { + return SGPropertyNode_ptr(); } + + SGPropertyNode_ptr result(new SGPropertyNode); + try { + readProperties(it->second.str(), result); + result->setStringValue("source", it->second.c_str()); + } catch (sg_exception&) { + SG_LOG(SG_INPUT, SG_WARN, "parse failure reading:" << it->second); + return NULL; + } + return result; } -FGDeviceConfigurationMap::~FGDeviceConfigurationMap() +bool FGDeviceConfigurationMap::hasConfiguration(const std::string& name) const { - base->removeChildren( childname ); + NameNodeMap::const_iterator j = overrideDict.find(name); + if (j != overrideDict.end()) { + return true; + } + + return namePathMap.find(name) != namePathMap.end(); } -void FGDeviceConfigurationMap::scan_dir( SGPath & path, int *index) +void FGDeviceConfigurationMap::scan_dir(const SGPath & path) { - ulDir *dir = ulOpenDir(path.c_str()); - if (dir) { - ulDirEnt* dent; - while ((dent = ulReadDir(dir)) != 0) { - if (dent->d_name[0] == '.') - continue; + if (!path.exists()) + return; + + flightgear::NavDataCache* cache = flightgear::NavDataCache::instance(); + flightgear::NavDataCache::Transaction txn(cache); + + simgear::Dir dir(path); + simgear::PathList children = dir.children(simgear::Dir::TYPE_FILE | + simgear::Dir::TYPE_DIR | simgear::Dir::NO_DOT_OR_DOTDOT); + + BOOST_FOREACH(SGPath path, children) { + if (path.isDir()) { + scan_dir(path); + } else if (path.extension() == "xml") { + if (cache->isCachedFileModified(path)) { + refreshCacheForFile(path); + } else { + readCachedData(path); + } // of cached file stamp is valid + } // of child is a file with '.xml' extension + } // of directory children iteration + + txn.commit(); +} - SGPath p(path.str()); - p.append(dent->d_name); - scan_dir(p, index); +void FGDeviceConfigurationMap::readCachedData(const SGPath& path) +{ + flightgear::NavDataCache* cache = flightgear::NavDataCache::instance(); + NamePathMap::iterator it; + BOOST_FOREACH(string s, cache->readStringListProperty(path.str())) { + // important - only insert if not already present. This ensures + // user configs can override those in the base package, since they are + // searched first. + it = namePathMap.find(s); + if (it == namePathMap.end()) { + namePathMap.insert(std::make_pair(s, path)); } - ulCloseDir(dir); + } // of cached names iteration +} - } else if (path.extension() == "xml") { - SG_LOG(SG_INPUT, SG_DEBUG, "Reading joystick file " << path.str()); - SGPropertyNode_ptr n = base->getChild(childname, (*index)++, true); +void FGDeviceConfigurationMap::refreshCacheForFile(const SGPath& path) +{ + SG_LOG(SG_INPUT, SG_DEBUG, "Reading joystick file " << path.str()); + SGPropertyNode_ptr n(new SGPropertyNode); + try { readProperties(path.str(), n); - n->setStringValue("source", path.c_str()); + } catch (sg_exception&) { + SG_LOG(SG_INPUT, SG_WARN, "parse failure reading:" << path); + return; } + + NamePathMap::iterator it; + string_list names; + BOOST_FOREACH(SGPropertyNode* nameProp, n->getChildren("name")) { + names.push_back(nameProp->getStringValue()); + // same comment as readCachedData: only insert if not already present + it = namePathMap.find(names.back()); + if (it == namePathMap.end()) { + namePathMap.insert(std::make_pair(names.back(), path)); + } + } + + flightgear::NavDataCache* cache = flightgear::NavDataCache::instance(); + cache->stampCacheFile(path); + cache->writeStringListProperty(path.str(), names); } - -