1 // jsclient.cxx -- simple UDP networked joystick client
3 // Copyright (C) 2003 by Manuel Bessler and Stephen Lowry
5 // based on joyclient.cxx by Curtis Olson
6 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
25 #include <simgear/debug/logstream.hxx>
26 #include <simgear/io/iochannel.hxx>
28 #include <Aircraft/aircraft.hxx>
29 #include <Main/fg_props.hxx>
31 #include "jsclient.hxx"
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");
38 for( int i = 0; i < 4; ++i )
40 axisdef[i] = fgGetNode("/jsclient/axis", i);
41 if( axisdef[i] != NULL )
43 axisdefstr[i] = axisdef[i]->getStringValue();
44 SG_LOG( SG_IO, SG_INFO, "jsclient axis[" << i << "] mapped to property " << axisdefstr[i]);
51 FGJsClient::~FGJsClient() {
55 // open hailing frequencies
56 bool FGJsClient::open() {
58 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
59 << "is already in use, ignoring" );
63 SGIOChannel *io = get_io_channel();
65 if ( ! io->open( get_direction() ) ) {
66 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
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;
83 if ( get_direction() == SG_IO_OUT ) {
84 SG_LOG( SG_IO, SG_ALERT, "JsClient protocol is read only" );
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." );
92 msg = (long int *)buf;
93 for( int i = 0; i < 4; ++i )
95 axis[i] = ((double)msg[i] / 2147483647.0);
96 if ( fabs(axis[i]) < 0.05 )
98 if( axisdefstr[i].length() != 0 )
99 fgSetFloat(axisdefstr[i].c_str(), axis[i]);
103 while ( io->read( (char *)(& buf), length ) == length ) {
104 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
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 )
111 axis[i] = ((double)msg[i] / 2147483647.0);
112 if ( fabs(axis[i]) < 0.05 )
114 if( axisdefstr[i].length() != 0 )
115 fgSetFloat(axisdefstr[i].c_str(), axis[i]);
126 bool FGJsClient::close() {
127 SGIOChannel *io = get_io_channel();
129 set_enabled( false );
131 if ( ! io->close() ) {