]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_gui.cxx
Add generic input support
[flightgear.git] / src / Network / native_gui.cxx
1 // native_gui.cxx -- FGFS external gui data export class
2 //
3 // Written by Curtis Olson, started January 2002.
4 //
5 // Copyright (C) 2002  Curtis L. Olson - curt@flightgear.org
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/io/lowlevel.hxx> // endian tests
30 #include <simgear/io/iochannel.hxx>
31 #include <simgear/timing/sg_time.hxx>
32
33 #include <Cockpit/radiostack.hxx>
34 #include <FDM/flight.hxx>
35 #include <Time/tmp.hxx>
36 #include <Main/fg_props.hxx>
37 #include <Main/globals.hxx>
38 #include <Scenery/scenery.hxx>
39
40 #include "native_gui.hxx"
41
42 // FreeBSD works better with this included last ... (?)
43 #if defined(WIN32) && !defined(__CYGWIN__)
44 #  include <windows.h>
45 #else
46 #  include <netinet/in.h>       // htonl() ntohl()
47 #endif
48
49
50 // #define FG_USE_NETWORK_BYTE_ORDER
51 #if defined( FG_USE_NETWORK_BYTE_ORDER )
52
53 // The function htond is defined this way due to the way some
54 // processors and OSes treat floating point values.  Some will raise
55 // an exception whenever a "bad" floating point value is loaded into a
56 // floating point register.  Solaris is notorious for this, but then
57 // so is LynxOS on the PowerPC.  By translating the data in place,
58 // there is no need to load a FP register with the "corruped" floating
59 // point value.  By doing the BIG_ENDIAN test, I can optimize the
60 // routine for big-endian processors so it can be as efficient as
61 // possible
62 static void htond (double &x)   
63 {
64     if ( sgIsLittleEndian() ) {
65         int    *Double_Overlay;
66         int     Holding_Buffer;
67     
68         Double_Overlay = (int *) &x;
69         Holding_Buffer = Double_Overlay [0];
70     
71         Double_Overlay [0] = htonl (Double_Overlay [1]);
72         Double_Overlay [1] = htonl (Holding_Buffer);
73     } else {
74         return;
75     }
76 }
77 static void htonf (float &x)    
78 {
79     if ( sgIsLittleEndian() ) {
80         int    *Float_Overlay;
81         int     Holding_Buffer;
82     
83         Float_Overlay = (int *) &x;
84         Holding_Buffer = Float_Overlay [0];
85     
86         Float_Overlay [0] = htonl (Holding_Buffer);
87     } else {
88         return;
89     }
90 }
91 #endif
92
93
94 FGNativeGUI::FGNativeGUI() {
95 }
96
97 FGNativeGUI::~FGNativeGUI() {
98 }
99
100
101 // open hailing frequencies
102 bool FGNativeGUI::open() {
103     if ( is_enabled() ) {
104         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
105                 << "is already in use, ignoring" );
106         return false;
107     }
108
109     SGIOChannel *io = get_io_channel();
110
111     if ( ! io->open( get_direction() ) ) {
112         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
113         return false;
114     }
115
116     set_enabled( true );
117
118     cur_fdm_state->_set_Sea_level_radius( SG_EQUATORIAL_RADIUS_FT );
119     return true;
120 }
121
122
123 void FGProps2NetGUI( FGNetGUI *net ) {
124     int i;
125
126     // Version sanity checking
127     net->version = FG_NET_GUI_VERSION;
128
129     // Aero parameters
130     net->longitude = cur_fdm_state->get_Longitude();
131     net->latitude = cur_fdm_state->get_Latitude();
132     net->altitude = cur_fdm_state->get_Altitude() * SG_FEET_TO_METER;
133     net->phi = cur_fdm_state->get_Phi();
134     net->theta = cur_fdm_state->get_Theta();
135     net->psi = cur_fdm_state->get_Psi();
136
137     // Velocities
138     net->vcas = cur_fdm_state->get_V_calibrated_kts();
139     net->climb_rate = cur_fdm_state->get_Climb_Rate();
140
141     // Consumables
142     net->num_tanks = FGNetGUI::FG_MAX_TANKS;
143     for ( i = 0; i < net->num_tanks; ++i ) {
144         SGPropertyNode *node = fgGetNode("/consumables/fuel/tank", i, true);
145         net->fuel_quantity[i] = node->getDoubleValue("level-gal_us");
146     }
147
148     // Environment
149     net->cur_time = globals->get_time_params()->get_cur_time();
150     net->warp = globals->get_warp();
151     net->ground_elev = globals->get_scenery()->get_cur_elev();
152
153     // Approach
154     net->tuned_freq = current_radiostack->get_navcom1()->get_nav_freq();
155     net->nav_radial = current_radiostack->get_navcom1()->get_nav_target_radial();
156     net->in_range = current_radiostack->get_navcom1()->get_nav_inrange();
157
158     if ( current_radiostack->get_navcom1()->get_nav_loc() ) {
159         // is an ILS
160         net->dist_nm
161             = current_radiostack->get_navcom1()->get_nav_gs_dist_signed()
162               * SG_METER_TO_NM;
163     } else {
164         // is a VOR
165         net->dist_nm = current_radiostack->get_navcom1()->get_nav_loc_dist()
166             * SG_METER_TO_NM;
167     }
168
169     net->course_deviation_deg
170         = current_radiostack->get_navcom1()->get_nav_reciprocal_radial()
171         - current_radiostack->get_navcom1()->get_nav_target_radial();
172     if ( net->course_deviation_deg < -1000.0 
173          || net->course_deviation_deg > 1000.0 )
174     {
175         // Sanity check ...
176         net->course_deviation_deg = 0.0;
177     }
178     while ( net->course_deviation_deg >  180.0 ) {
179         net->course_deviation_deg -= 360.0;
180     }
181     while ( net->course_deviation_deg < -180.0 ) {
182         net->course_deviation_deg += 360.0;
183     }
184     if ( fabs(net->course_deviation_deg) > 90.0 )
185         net->course_deviation_deg
186             = ( net->course_deviation_deg<0.0
187                 ? -net->course_deviation_deg - 180.0
188                 : -net->course_deviation_deg + 180.0 );
189
190     if ( current_radiostack->get_navcom1()->get_nav_loc() ) {
191         // is an ILS
192         net->gs_deviation_deg
193             = current_radiostack->get_navcom1()->get_nav_gs_deflection()
194             / 5.0;
195     } else {
196         // is an ILS
197         net->gs_deviation_deg = -9999.0;
198     }
199
200 #if defined( FG_USE_NETWORK_BYTE_ORDER )
201     // Convert the net buffer to network format
202     net->version = htonl(net->version);
203
204     htond(net->longitude);
205     htond(net->latitude);
206     htonf(net->altitude);
207     htonf(net->phi);
208     htonf(net->theta);
209     htonf(net->psi);
210     htonf(net->vcas);
211     htonf(net->climb_rate);
212
213     for ( i = 0; i < net->num_tanks; ++i ) {
214         htonf(net->fuel_quantity[i]);
215     }
216     net->num_tanks = htonl(net->num_tanks);
217
218     net->cur_time = htonl( net->cur_time );
219     net->warp = htonl( net->warp );
220     net->ground_elev = htonl( net->ground_elev );
221
222     htonf(net->tuned_freq);
223     htonf(net->nav_radial);
224     net->in_range = htonl(net->in_range);
225     htonf(net->dist_nm);
226     htonf(net->course_deviation_deg);
227     htonf(net->gs_deviation_deg);
228 #endif
229 }
230
231
232 void FGNetGUI2Props( FGNetGUI *net ) {
233     int i;
234
235 #if defined( FG_USE_NETWORK_BYTE_ORDER )
236     // Convert to the net buffer from network format
237     net->version = ntohl(net->version);
238
239     htond(net->longitude);
240     htond(net->latitude);
241     htonf(net->altitude);
242     htonf(net->phi);
243     htonf(net->theta);
244     htonf(net->psi);
245     htonf(net->vcas);
246     htonf(net->climb_rate);
247
248     net->num_tanks = htonl(net->num_tanks);
249     for ( i = 0; i < net->num_tanks; ++i ) {
250         htonf(net->fuel_quantity[i]);
251     }
252
253     net->cur_time = ntohl(net->cur_time);
254     net->warp = ntohl(net->warp);
255     net->ground_elev = htonl( net->ground_elev );
256
257     htonf(net->tuned_freq);
258     net->in_range = htonl(net->in_range);
259     htonf(net->dist_nm);
260     htonf(net->course_deviation_deg);
261     htonf(net->gs_deviation_deg);
262 #endif
263
264     if ( net->version == FG_NET_GUI_VERSION ) {
265         // cout << "pos = " << net->longitude << " " << net->latitude << endl;
266         // cout << "sea level rad = " << cur_fdm_state->get_Sea_level_radius()
267         //      << endl;
268         cur_fdm_state->_updateGeodeticPosition( net->latitude,
269                                                 net->longitude,
270                                                 net->altitude
271                                                   * SG_METER_TO_FEET );
272         cur_fdm_state->_set_Euler_Angles( net->phi,
273                                           net->theta,
274                                           net->psi );
275
276         cur_fdm_state->_set_V_calibrated_kts( net->vcas );
277         cur_fdm_state->_set_Climb_Rate( net->climb_rate );
278
279         for (i = 0; i < net->num_tanks; ++i ) {
280             SGPropertyNode * node
281                 = fgGetNode("/consumables/fuel/tank", i, true);
282             node->setDoubleValue("level-gal_us", net->fuel_quantity[i] );
283         }
284
285         if ( net->cur_time ) {
286             fgSetLong("/sim/time/cur-time-override", net->cur_time);
287         }
288
289         globals->set_warp( net->warp );
290         globals->get_scenery()->set_cur_elev( net->ground_elev );
291
292         // Approach
293         fgSetDouble( "/radios/nav[0]/frequencies/selected-mhz",
294                      net->tuned_freq );
295         fgSetBool( "/radios/nav[0]/in-range", net->in_range );
296         fgSetDouble( "/radios/dme/distance-nm", net->dist_nm );
297         fgSetDouble( "/radios/nav[0]/heading-needle-deflection",
298                      net->course_deviation_deg );
299         fgSetDouble( "/radios/nav[0]/gs-needle-deflection",
300                      net->gs_deviation_deg );
301     } else {
302         SG_LOG( SG_IO, SG_ALERT,
303                 "Error: version mismatch in FGNetNativeGUI2Props()" );
304         SG_LOG( SG_IO, SG_ALERT,
305                 "\tread " << net->version << " need " << FG_NET_GUI_VERSION );
306         SG_LOG( SG_IO, SG_ALERT,
307                 "\tNeed to upgrade net_fdm.hxx and recompile." );
308     }
309 }
310
311
312 // process work for this port
313 bool FGNativeGUI::process() {
314     SGIOChannel *io = get_io_channel();
315     int length = sizeof(buf);
316
317     if ( get_direction() == SG_IO_OUT ) {
318         // cout << "size of cur_fdm_state = " << length << endl;
319         FGProps2NetGUI( &buf );
320         if ( ! io->write( (char *)(& buf), length ) ) {
321             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
322             return false;
323         }
324     } else if ( get_direction() == SG_IO_IN ) {
325         if ( io->get_type() == sgFileType ) {
326             if ( io->read( (char *)(& buf), length ) == length ) {
327                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
328                 FGNetGUI2Props( &buf );
329             }
330         } else {
331             while ( io->read( (char *)(& buf), length ) == length ) {
332                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
333                 FGNetGUI2Props( &buf );
334             }
335         }
336     }
337
338     return true;
339 }
340
341
342 // close the channel
343 bool FGNativeGUI::close() {
344     SGIOChannel *io = get_io_channel();
345
346     set_enabled( false );
347
348     if ( ! io->close() ) {
349         return false;
350     }
351
352     return true;
353 }