]> git.mxchange.org Git - flightgear.git/blob - src/Input/FGDeviceConfigurationMap.cxx
Merge branch 'jmt/gps' into next
[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 <simgear/math/SGMath.hxx>
30
31 #include "FGDeviceConfigurationMap.hxx"
32
33 #include <plib/ul.h>
34
35 #include <simgear/props/props_io.hxx>
36 #include <Main/globals.hxx>
37
38 using simgear::PropertyList;
39
40 FGDeviceConfigurationMap::FGDeviceConfigurationMap( const char * relative_path, SGPropertyNode_ptr aBase, const char * aChildname ) :
41   base(aBase),
42   childname(aChildname)
43 {
44   SGPath path(globals->get_fg_root());
45   path.append( relative_path );
46
47   int index = 1000;
48   scan_dir( path, &index);
49
50   PropertyList childNodes = base->getChildren(childname);
51   for (int k = (int)childNodes.size() - 1; k >= 0; k--) {
52     SGPropertyNode *n = childNodes[k];
53     PropertyList names = n->getChildren("name");
54     if (names.size() ) // && (n->getChildren("axis").size() || n->getChildren("button").size()))
55       for (unsigned int j = 0; j < names.size(); j++)
56         (*this)[names[j]->getStringValue()] = n;
57   }
58 }
59
60 FGDeviceConfigurationMap::~FGDeviceConfigurationMap()
61 {
62   base->removeChildren( childname );
63 }
64
65 void FGDeviceConfigurationMap::scan_dir( SGPath & path, int *index)
66 {
67   ulDir *dir = ulOpenDir(path.c_str());
68   if (dir) {
69     ulDirEnt* dent;
70     while ((dent = ulReadDir(dir)) != 0) {
71       if (dent->d_name[0] == '.')
72         continue;
73
74       SGPath p(path.str());
75       p.append(dent->d_name);
76       scan_dir(p, index);
77     }
78     ulCloseDir(dir);
79
80   } else if (path.extension() == "xml") {
81     SG_LOG(SG_INPUT, SG_DEBUG, "Reading joystick file " << path.str());
82     SGPropertyNode_ptr n = base->getChild(childname, (*index)++, true);
83     readProperties(path.str(), n);
84     n->setStringValue("source", path.c_str());
85   }
86 }
87
88