]> git.mxchange.org Git - flightgear.git/blob - src/Input/FGDeviceConfigurationMap.cxx
Minor renaming issue.
[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 <simgear/misc/sg_dir.hxx>
32 #include <simgear/props/props_io.hxx>
33 #include <Main/globals.hxx>
34
35 using simgear::PropertyList;
36
37 FGDeviceConfigurationMap::FGDeviceConfigurationMap( const char * relative_path, SGPropertyNode_ptr aBase, const char * aChildname ) :
38   base(aBase),
39   childname(aChildname)
40 {
41   int index = 1000;
42   scan_dir( SGPath(globals->get_fg_home(), relative_path), &index);
43   scan_dir( SGPath(globals->get_fg_root(), relative_path), &index);
44
45   PropertyList childNodes = base->getChildren(childname);
46   for (int k = (int)childNodes.size() - 1; k >= 0; k--) {
47     SGPropertyNode *n = childNodes[k];
48     PropertyList names = n->getChildren("name");
49     if (names.size() ) // && (n->getChildren("axis").size() || n->getChildren("button").size()))
50       for (unsigned int j = 0; j < names.size(); j++)
51         (*this)[names[j]->getStringValue()] = n;
52   }
53 }
54
55 FGDeviceConfigurationMap::~FGDeviceConfigurationMap()
56 {
57   // Ensure that the children don't hang around when deleted, as if 
58   // re-created, we need to ensure that the set of names doesn't contain
59   // any unexpected history.
60   base->removeChildren( childname, false );
61 }
62
63 void FGDeviceConfigurationMap::scan_dir(const SGPath & path, int *index)
64 {
65   simgear::Dir dir(path);
66   simgear::PathList children = dir.children(simgear::Dir::TYPE_FILE | 
67     simgear::Dir::TYPE_DIR | simgear::Dir::NO_DOT_OR_DOTDOT);
68
69   for (unsigned int c=0; c<children.size(); ++c) {
70     SGPath path(children[c]);
71     if (path.isDir()) {
72       scan_dir(path, index);
73     } else if (path.extension() == "xml") {
74       SG_LOG(SG_INPUT, SG_DEBUG, "Reading joystick file " << path.str());
75       SGPropertyNode_ptr n = base->getChild(childname, (*index)++, true);
76       readProperties(path.str(), n);
77       n->setStringValue("source", path.c_str());
78     }
79   }
80 }
81
82