]> git.mxchange.org Git - flightgear.git/blob - src/Network/jsclient.cxx
Merge branch 'durk/ai-atc' into next
[flightgear.git] / src / Network / jsclient.cxx
1 // jsclient.cxx -- simple UDP networked joystick client
2 //
3 // Copyright (C) 2003 by Manuel Bessler and Stephen Lowry
4 //
5 // based on joyclient.cxx by Curtis Olson
6 // Copyright (C) 2000  Curtis L. Olson - http://www.flightgear.org/~curt
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 // $Id$
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/io/iochannel.hxx>
30 #include <simgear/misc/stdint.hxx>
31
32 #include <Aircraft/aircraft.hxx>
33 #include <Main/fg_props.hxx>
34
35 #include "jsclient.hxx"
36
37
38 FGJsClient::FGJsClient() {
39         active = fgHasNode("/jsclient"); // if exist, assume bindings are defined
40         SG_LOG( SG_IO, SG_INFO, "/jsclient exists, activating JsClient remote joystick support");
41
42         for( int i = 0; i < 4; ++i )
43         {
44             axisdef[i] = fgGetNode("/jsclient/axis", i);
45             if( axisdef[i] != NULL )
46             {
47                     axisdefstr[i] = axisdef[i]->getStringValue();
48                     SG_LOG( SG_IO, SG_INFO, "jsclient axis[" << i << "] mapped to property " << axisdefstr[i]);
49             }
50             else
51                 axisdefstr[i] = "";
52         }
53 }
54
55 FGJsClient::~FGJsClient() {
56 }
57
58
59 // open hailing frequencies
60 bool FGJsClient::open() {
61     if ( is_enabled() ) {
62         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
63                 << "is already in use, ignoring" );
64         return false;
65     }
66
67     SGIOChannel *io = get_io_channel();
68
69     if ( ! io->open( get_direction() ) ) {
70         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
71         return false;
72     }
73
74     set_enabled( true );
75
76     return true;
77 }
78
79
80 // process work for this port
81 bool FGJsClient::process() {
82     SGIOChannel *io = get_io_channel();
83     int length = 4+4+4+4+4+4;
84
85 //    if( ! active )
86 //          return true;
87     if ( get_direction() == SG_IO_OUT ) {
88         SG_LOG( SG_IO, SG_ALERT, "JsClient protocol is read only" );
89         return false;
90     } else if ( get_direction() == SG_IO_IN ) {
91         SG_LOG( SG_IO, SG_DEBUG, "Searching for data." );
92         if ( io->get_type() == sgFileType ) {
93             if ( io->read( (char *)(& buf), length ) == length ) {
94                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
95                 int32_t *msg = (int32_t *)buf;
96                 for( int i = 0; i < 4; ++i )
97                 {
98                         axis[i] = ((double)msg[i] / 2147483647.0);
99                         if ( fabs(axis[i]) < 0.05 )
100                             axis[i] = 0.0;
101                         if( axisdefstr[i].length() != 0 )
102                             fgSetFloat(axisdefstr[i].c_str(), axis[i]);
103                 }
104             }
105         } else {
106             while ( io->read( (char *)(& buf), length ) == length ) {
107                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
108                 int32_t *msg = (int32_t *)buf;
109                 SG_LOG( SG_IO, SG_DEBUG, "ax0 = " << msg[0] << " ax1 = "
110                         << msg[1] << "ax2 = " << msg[2] << "ax3 = " << msg[3]);
111                 for( int i = 0; i < 4; ++i )
112                 {
113                         axis[i] = ((double)msg[i] / 2147483647.0);
114                         if ( fabs(axis[i]) < 0.05 )
115                             axis[i] = 0.0;
116                         if( axisdefstr[i].length() != 0 )
117                             fgSetFloat(axisdefstr[i].c_str(), axis[i]);
118                 }
119             }
120         }
121     }
122
123     return true;
124 }
125
126
127 // close the channel
128 bool FGJsClient::close() {
129     SGIOChannel *io = get_io_channel();
130
131     set_enabled( false );
132
133     if ( ! io->close() ) {
134         return false;
135     }
136
137     return true;
138 }