]> git.mxchange.org Git - flightgear.git/blob - utils/fgpanel/FGPanelProtocol.cxx
Make cmake for fgpanel work under linux
[flightgear.git] / utils / fgpanel / FGPanelProtocol.cxx
1 //
2 //  Written and (c) Torsten Dreyer - Torsten(at)t3r_dot_de
3 //
4 //  This program is free software; you can redistribute it and/or
5 //  modify it under the terms of the GNU General Public License as
6 //  published by the Free Software Foundation; either version 2 of the
7 //  License, or (at your option) any later version.
8 // 
9 //  This program is distributed in the hope that it will be useful, but
10 //  WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 //  General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program; if not, write to the Free Software
16 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 //
18 #ifdef HAVE_CONFIG_H
19 #  include <config.h>
20 #endif
21
22 #ifdef HAVE_WINDOWS_H
23 #include <windows.h>
24 #endif
25
26 #ifdef WIN32
27 #define strtof strtod
28 #endif
29
30 #include "FGPanelProtocol.hxx"
31 #include "ApplicationProperties.hxx"
32 #include <simgear/io/sg_socket.hxx>
33 #include <simgear/misc/strutils.hxx>
34
35 class PropertySetter {
36 public:
37   PropertySetter( SGPropertyNode_ptr node ) : _node(node) {}
38   virtual void setValue( const char * value ) = 0;
39 protected:
40   SGPropertyNode_ptr _node;
41 };
42
43 class BoolPropertySetter : public PropertySetter {
44 public:
45   BoolPropertySetter( SGPropertyNode_ptr node ) : PropertySetter(node) {}
46   virtual void setValue( const char * value ) {
47     _node->setBoolValue( atoi( value ) != 0 );
48   }
49 };
50
51 class IntPropertySetter : public PropertySetter {
52 public:
53   IntPropertySetter( SGPropertyNode_ptr node ) : PropertySetter(node) {}
54   virtual void setValue( const char * value ) {
55     _node->setIntValue( atol( value ) );
56   }
57 };
58
59 class FloatPropertySetter : public PropertySetter {
60 public:
61   FloatPropertySetter( SGPropertyNode_ptr node ) : PropertySetter(node) {}
62   virtual void setValue( const char * value ) {
63     _node->setFloatValue( strtof( value, NULL ) );
64   }
65 };
66
67 class DoublePropertySetter : public PropertySetter {
68 public:
69   DoublePropertySetter( SGPropertyNode_ptr node ) : PropertySetter(node) {}
70   virtual void setValue( const char * value ) {
71     _node->setDoubleValue( strtod( value, NULL ) );
72   }
73 };
74
75 class StringPropertySetter : public PropertySetter {
76 public:
77   StringPropertySetter( SGPropertyNode_ptr node ) : PropertySetter(node) {}
78   virtual void setValue( const char * value ) {
79     _node->setStringValue( value );
80   }
81 };
82
83 FGPanelProtocol::FGPanelProtocol( SGPropertyNode_ptr aRoot )
84   : SGSubsystem(),
85    root(aRoot),
86    io(NULL)
87 {
88   SGPropertyNode_ptr outputNode = root->getNode( "protocol/generic/output" );
89   if( outputNode ) {
90     vector<SGPropertyNode_ptr> chunks = outputNode->getChildren( "chunk" );
91     for( vector<SGPropertyNode_ptr>::size_type i = 0; i < chunks.size(); i++ ) {
92       SGPropertyNode_ptr chunk = chunks[i];
93
94       SGPropertyNode_ptr nodeNode = chunk->getNode("node", false );
95       if( nodeNode == NULL )
96         continue;
97
98       SGPropertyNode_ptr node = ApplicationProperties::Properties->getNode( nodeNode->getStringValue(), true );
99
100       string type = "";
101       SGPropertyNode_ptr typeNode = chunk->getNode( "type", false );
102       if( typeNode != NULL ) type = typeNode->getStringValue();
103       if( type == "float" ) {
104         propertySetterVector.push_back( new FloatPropertySetter( node ) );
105       } else if( type == "double" || type == "fixed" ) {
106         propertySetterVector.push_back( new DoublePropertySetter( node ) );
107       } else if( type == "bool" || type == "boolean" ) {
108         propertySetterVector.push_back( new BoolPropertySetter( node ) );
109       } else if( type == "string" ) {
110         propertySetterVector.push_back( new StringPropertySetter( node ) );
111       } else {
112         propertySetterVector.push_back( new IntPropertySetter( node ) );
113       }
114     }
115   }
116 }
117
118 FGPanelProtocol::~FGPanelProtocol()
119 {
120   for( PropertySetterVector::size_type i = 0; i < propertySetterVector.size(); i++ )
121     delete propertySetterVector[i];
122 }
123
124 void FGPanelProtocol::update( double dt )
125 {
126   char buf[8192];
127
128   if( io == NULL )
129     return;
130
131   int length = io->readline( buf, sizeof(buf)-1 );
132   buf[sizeof(buf)-1] = 0;
133   if ( length > 0 ) {
134     vector<string> tokens = simgear::strutils::split( buf, "," );
135     for( vector<string>::size_type i = 0; i < tokens.size(); i++ ) {
136       if( i < propertySetterVector.size() )
137         propertySetterVector[i]->setValue( tokens[i].c_str() );
138     }
139   }
140 }
141
142 void FGPanelProtocol::init()
143 {
144   SGPropertyNode_ptr listenNode = root->getNode( "listen" );
145   if( listenNode == NULL ) {
146     return;
147   }
148
149   string hostname = listenNode->getNode( "host", true )->getStringValue();
150   string port = listenNode->getNode( "port", true )->getStringValue();
151   string style = listenNode->getNode( "style", true )->getStringValue();
152
153   if( io != NULL )
154     delete io;
155
156   io = new SGSocket( hostname, port, style );
157
158   if( !io->open( SG_IO_IN ) ) {
159     cerr << "can't open socket " << style << ":" << hostname << ":" << port << endl;
160   }
161 }
162
163 void FGPanelProtocol::reinit()
164 {
165   init();
166 }