]> git.mxchange.org Git - flightgear.git/blob - src/Input/FGDeviceConfigurationMap.cxx
Cached joystick config loading.
[flightgear.git] / src / Input / FGDeviceConfigurationMap.cxx
1 // FGDeviceConfigurationMap.cxx -- a map to access xml device configuration
2 //
3 // Written by Torsten Dreyer, started August 2009
4 // Based on work from David Megginson, started May 2001.
5 //
6 // Copyright (C) 2009 Torsten Dreyer, Torsten (at) t3r _dot_ de
7 // Copyright (C) 2001 David Megginson, david@megginson.com
8 //
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.
13 //
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.
18 //
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.
22 //
23 // $Id$
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include "FGDeviceConfigurationMap.hxx"
30
31 #include <boost/foreach.hpp>
32
33 #include <simgear/misc/sg_dir.hxx>
34 #include <simgear/props/props_io.hxx>
35 #include <simgear/structure/exception.hxx>
36
37 #include <Main/globals.hxx>
38 #include <Navaids/NavDataCache.hxx>
39
40 using simgear::PropertyList;
41 using std::string;
42
43 FGDeviceConfigurationMap::FGDeviceConfigurationMap( const string& relative_path)
44 {
45   scan_dir( SGPath(globals->get_fg_home(), relative_path));
46   scan_dir( SGPath(globals->get_fg_root(), relative_path));
47 }
48
49 FGDeviceConfigurationMap::~FGDeviceConfigurationMap()
50 {
51 }
52
53 SGPropertyNode_ptr
54 FGDeviceConfigurationMap::configurationForDeviceName(const std::string& name)
55 {
56   NamePathMap::iterator it = namePathMap.find(name);
57   if (it == namePathMap.end()) {
58     return SGPropertyNode_ptr();
59   }
60       
61   SGPropertyNode_ptr result(new SGPropertyNode);
62   try {
63     readProperties(it->second.str(), result);
64     result->setStringValue("source", it->second.c_str());
65   } catch (sg_exception& e) {
66     SG_LOG(SG_INPUT, SG_WARN, "parse failure reading:" << it->second);
67     return NULL;
68   }
69   return result;
70 }
71
72 bool FGDeviceConfigurationMap::hasConfiguration(const std::string& name) const
73 {
74   return namePathMap.find(name) != namePathMap.end();
75 }
76
77 void FGDeviceConfigurationMap::scan_dir(const SGPath & path)
78 {
79   if (!path.exists())
80     return;
81   
82   simgear::Dir dir(path);
83   simgear::PathList children = dir.children(simgear::Dir::TYPE_FILE | 
84     simgear::Dir::TYPE_DIR | simgear::Dir::NO_DOT_OR_DOTDOT);
85   flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
86   
87   BOOST_FOREACH(SGPath path, children) {
88     if (path.isDir()) {
89       scan_dir(path);
90     } else if (path.extension() == "xml") {
91       if (cache->isCachedFileModified(path)) {
92         refreshCacheForFile(path);
93       } else {
94         readCachedData(path);
95       } // of cached file stamp is valid
96     } // of child is a file with '.xml' extension
97   } // of directory children iteration
98 }
99
100 void FGDeviceConfigurationMap::readCachedData(const SGPath& path)
101 {
102   flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
103   NamePathMap::iterator it;
104   BOOST_FOREACH(string s, cache->readStringListProperty(path.str())) {
105     // important - only insert if not already present. This ensures
106     // user configs can override those in the base package, since they are
107     // searched first.
108     it = namePathMap.find(s);
109     if (it == namePathMap.end()) {
110       namePathMap.insert(std::make_pair(s, path));
111     }
112   } // of cached names iteration
113 }
114
115 void FGDeviceConfigurationMap::refreshCacheForFile(const SGPath& path)
116 {
117   SG_LOG(SG_INPUT, SG_DEBUG, "Reading joystick file " << path.str());
118   SGPropertyNode_ptr n(new SGPropertyNode);
119   try {
120     readProperties(path.str(), n);
121   } catch (sg_exception& e) {
122     SG_LOG(SG_INPUT, SG_WARN, "parse failure reading:" << path);
123     return;
124   }
125   
126   NamePathMap::iterator it;
127   string_list names;
128   BOOST_FOREACH(SGPropertyNode* nameProp, n->getChildren("name")) {
129     names.push_back(nameProp->getStringValue());
130     // same comment as readCachedData: only insert if not already present
131     it = namePathMap.find(names.back());
132     if (it == namePathMap.end()) {
133       namePathMap.insert(std::make_pair(names.back(), path));
134     }
135   }
136   
137   flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
138   cache->stampCacheFile(path);
139   cache->writeStringListProperty(path.str(), names);
140 }