]> git.mxchange.org Git - flightgear.git/blob - src/Network/jsclient.cxx
Merge branch 'jmt/track-bug' 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
31 #include <Aircraft/aircraft.hxx>
32 #include <Main/fg_props.hxx>
33
34 #include "jsclient.hxx"
35
36
37 FGJsClient::FGJsClient() {
38         active = fgHasNode("/jsclient"); // if exist, assume bindings are defined
39         SG_LOG( SG_IO, SG_INFO, "/jsclient exists, activating JsClient remote joystick support");
40
41         for( int i = 0; i < 4; ++i )
42         {
43             axisdef[i] = fgGetNode("/jsclient/axis", i);
44             if( axisdef[i] != NULL )
45             {
46                     axisdefstr[i] = axisdef[i]->getStringValue();
47                     SG_LOG( SG_IO, SG_INFO, "jsclient axis[" << i << "] mapped to property " << axisdefstr[i]);
48             }
49             else
50                 axisdefstr[i] = "";
51         }
52 }
53
54 FGJsClient::~FGJsClient() {
55 }
56
57
58 // open hailing frequencies
59 bool FGJsClient::open() {
60     if ( is_enabled() ) {
61         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
62                 << "is already in use, ignoring" );
63         return false;
64     }
65
66     SGIOChannel *io = get_io_channel();
67
68     if ( ! io->open( get_direction() ) ) {
69         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
70         return false;
71     }
72
73     set_enabled( true );
74
75     return true;
76 }
77
78
79 // process work for this port
80 bool FGJsClient::process() {
81     SGIOChannel *io = get_io_channel();
82     int length = 4+4+4+4+4+4;
83
84 //    if( ! active )
85 //          return true;
86     if ( get_direction() == SG_IO_OUT ) {
87         SG_LOG( SG_IO, SG_ALERT, "JsClient protocol is read only" );
88         return false;
89     } else if ( get_direction() == SG_IO_IN ) {
90         SG_LOG( SG_IO, SG_DEBUG, "Searching for data." );
91         if ( io->get_type() == sgFileType ) {
92             if ( io->read( (char *)(& buf), length ) == length ) {
93                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
94                 long int *msg;
95                 msg = (long int *)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                 long int *msg;
109                 msg = (long int *)buf;
110                 SG_LOG( SG_IO, SG_DEBUG, "ax0 = " << msg[0] << " ax1 = "
111                         << msg[1] << "ax2 = " << msg[2] << "ax3 = " << msg[3]);
112                 for( int i = 0; i < 4; ++i )
113                 {
114                         axis[i] = ((double)msg[i] / 2147483647.0);
115                         if ( fabs(axis[i]) < 0.05 )
116                             axis[i] = 0.0;
117                         if( axisdefstr[i].length() != 0 )
118                             fgSetFloat(axisdefstr[i].c_str(), axis[i]);
119                 }
120             }
121         }
122     }
123
124     return true;
125 }
126
127
128 // close the channel
129 bool FGJsClient::close() {
130     SGIOChannel *io = get_io_channel();
131
132     set_enabled( false );
133
134     if ( ! io->close() ) {
135         return false;
136     }
137
138     return true;
139 }