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