FlightGear next
jsclient.cxx
Go to the documentation of this file.
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 <Main/fg_props.hxx>
33
34#include "jsclient.hxx"
35
36
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
56
57
58// open hailing frequencies
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
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 int32_t *msg = (int32_t *)buf;
95 for( int i = 0; i < 4; ++i )
96 {
97 axis[i] = ((double)msg[i] / 2147483647.0);
98 if ( fabs(axis[i]) < 0.05 )
99 axis[i] = 0.0;
100 if( axisdefstr[i].length() != 0 )
101 fgSetFloat(axisdefstr[i].c_str(), axis[i]);
102 }
103 }
104 } else {
105 while ( io->read( (char *)(& buf), length ) == length ) {
106 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
107 int32_t *msg = (int32_t *)buf;
108 SG_LOG( SG_IO, SG_DEBUG, "ax0 = " << msg[0] << " ax1 = "
109 << msg[1] << "ax2 = " << msg[2] << "ax3 = " << msg[3]);
110 for( int i = 0; i < 4; ++i )
111 {
112 axis[i] = ((double)msg[i] / 2147483647.0);
113 if ( fabs(axis[i]) < 0.05 )
114 axis[i] = 0.0;
115 if( axisdefstr[i].length() != 0 )
116 fgSetFloat(axisdefstr[i].c_str(), axis[i]);
117 }
118 }
119 }
120 }
121
122 return true;
123}
124
125
126// close the channel
128 SGIOChannel *io = get_io_channel();
129
130 set_enabled( false );
131
132 if ( ! io->close() ) {
133 return false;
134 }
135
136 return true;
137}
#define i(x)
bool open()
Definition jsclient.cxx:59
bool process()
Definition jsclient.cxx:80
bool close()
Definition jsclient.cxx:127
SGProtocolDir get_direction() const
Definition protocol.hxx:65
SGIOChannel * get_io_channel() const
Definition protocol.hxx:90
void set_enabled(const bool b)
Definition protocol.hxx:88
bool is_enabled() const
Definition protocol.hxx:87
bool fgHasNode(const char *path)
Test whether a given node exists.
Definition fg_props.cxx:507
SGPropertyNode * fgGetNode(const char *path, bool create)
Get a property node.
Definition proptest.cpp:27
bool fgSetFloat(const char *name, float val)
Set a float value for a property.
Definition proptest.cpp:23