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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/io/iochannel.hxx>
31 #include <Aircraft/aircraft.hxx>
32 #include <Main/fg_props.hxx>
34 #include "jsclient.hxx"
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");
41 for( int i = 0; i < 4; ++i )
43 axisdef[i] = fgGetNode("/jsclient/axis", i);
44 if( axisdef[i] != NULL )
46 axisdefstr[i] = axisdef[i]->getStringValue();
47 SG_LOG( SG_IO, SG_INFO, "jsclient axis[" << i << "] mapped to property " << axisdefstr[i]);
54 FGJsClient::~FGJsClient() {
58 // open hailing frequencies
59 bool FGJsClient::open() {
61 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
62 << "is already in use, ignoring" );
66 SGIOChannel *io = get_io_channel();
68 if ( ! io->open( get_direction() ) ) {
69 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
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;
86 if ( get_direction() == SG_IO_OUT ) {
87 SG_LOG( SG_IO, SG_ALERT, "JsClient protocol is read only" );
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." );
95 msg = (long int *)buf;
96 for( int i = 0; i < 4; ++i )
98 axis[i] = ((double)msg[i] / 2147483647.0);
99 if ( fabs(axis[i]) < 0.05 )
101 if( axisdefstr[i].length() != 0 )
102 fgSetFloat(axisdefstr[i].c_str(), axis[i]);
106 while ( io->read( (char *)(& buf), length ) == length ) {
107 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
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 )
114 axis[i] = ((double)msg[i] / 2147483647.0);
115 if ( fabs(axis[i]) < 0.05 )
117 if( axisdefstr[i].length() != 0 )
118 fgSetFloat(axisdefstr[i].c_str(), axis[i]);
129 bool FGJsClient::close() {
130 SGIOChannel *io = get_io_channel();
132 set_enabled( false );
134 if ( ! io->close() ) {