]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_gui.cxx
Started a new FGMenuBar class to handle a different XML-configurable
[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 <FDM/flight.hxx>
34 #include <Time/tmp.hxx>
35 #include <Main/fg_props.hxx>
36 #include <Main/globals.hxx>
37
38 #include "native_gui.hxx"
39
40 // FreeBSD works better with this included last ... (?)
41 #if defined(WIN32) && !defined(__CYGWIN__)
42 #  include <windows.h>
43 #else
44 #  include <netinet/in.h>       // htonl() ntohl()
45 #endif
46
47
48 // The function htond is defined this way due to the way some
49 // processors and OSes treat floating point values.  Some will raise
50 // an exception whenever a "bad" floating point value is loaded into a
51 // floating point register.  Solaris is notorious for this, but then
52 // so is LynxOS on the PowerPC.  By translating the data in place,
53 // there is no need to load a FP register with the "corruped" floating
54 // point value.  By doing the BIG_ENDIAN test, I can optimize the
55 // routine for big-endian processors so it can be as efficient as
56 // possible
57 static void htond (double &x)   
58 {
59     if ( sgIsLittleEndian() ) {
60         int    *Double_Overlay;
61         int     Holding_Buffer;
62     
63         Double_Overlay = (int *) &x;
64         Holding_Buffer = Double_Overlay [0];
65     
66         Double_Overlay [0] = htonl (Double_Overlay [1]);
67         Double_Overlay [1] = htonl (Holding_Buffer);
68     } else {
69         return;
70     }
71 }
72
73
74 FGNativeGUI::FGNativeGUI() {
75 }
76
77 FGNativeGUI::~FGNativeGUI() {
78 }
79
80
81 // open hailing frequencies
82 bool FGNativeGUI::open() {
83     if ( is_enabled() ) {
84         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
85                 << "is already in use, ignoring" );
86         return false;
87     }
88
89     SGIOChannel *io = get_io_channel();
90
91     if ( ! io->open( get_direction() ) ) {
92         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
93         return false;
94     }
95
96     set_enabled( true );
97
98     cur_fdm_state->_set_Sea_level_radius( SG_EQUATORIAL_RADIUS_FT );
99     return true;
100 }
101
102
103 void FGProps2NetGUI( FGNetGUI *net ) {
104     int i;
105
106     // Version sanity checking
107     net->version = FG_NET_GUI_VERSION;
108
109     // Aero parameters
110     net->longitude = cur_fdm_state->get_Longitude();
111     net->latitude = cur_fdm_state->get_Latitude();
112     net->altitude = cur_fdm_state->get_Altitude() * SG_FEET_TO_METER;
113     net->phi = cur_fdm_state->get_Phi();
114     net->theta = cur_fdm_state->get_Theta();
115     net->psi = cur_fdm_state->get_Psi();
116
117     // Velocities
118     net->vcas = cur_fdm_state->get_V_calibrated_kts();
119     net->climb_rate = cur_fdm_state->get_Climb_Rate();
120
121     // Consumables
122     net->num_tanks = FGNetGUI::FG_MAX_TANKS;
123     for ( i = 0; i < net->num_tanks; ++i ) {
124         SGPropertyNode *node = fgGetNode("/consumables/fuel/tank", i, true);
125         net->fuel_quantity[i] = node->getDoubleValue("level-gal_us");
126     }
127
128     // the following really aren't used in this context
129     net->cur_time = globals->get_time_params()->get_cur_time();
130     net->warp = globals->get_warp();
131
132     // Convert the net buffer to network format
133     net->version = htonl(net->version);
134
135     htond(net->longitude);
136     htond(net->latitude);
137     htond(net->altitude);
138     htond(net->phi);
139     htond(net->theta);
140     htond(net->psi);
141     htond(net->vcas);
142     htond(net->climb_rate);
143
144     for ( i = 0; i < net->num_tanks; ++i ) {
145         htond(net->fuel_quantity[i]);
146     }
147     net->num_tanks = htonl(net->num_tanks);
148
149     net->cur_time = htonl( net->cur_time );
150     net->warp = htonl( net->warp );
151 }
152
153
154 void FGNetGUI2Props( FGNetGUI *net ) {
155     int i;
156
157     // Convert to the net buffer from network format
158     net->version = ntohl(net->version);
159
160     htond(net->longitude);
161     htond(net->latitude);
162     htond(net->altitude);
163     htond(net->phi);
164     htond(net->theta);
165     htond(net->psi);
166     htond(net->vcas);
167     htond(net->climb_rate);
168
169     net->num_tanks = htonl(net->num_tanks);
170     for ( i = 0; i < net->num_tanks; ++i ) {
171         htond(net->fuel_quantity[i]);
172     }
173
174     net->cur_time = ntohl(net->cur_time);
175     net->warp = ntohl(net->warp);
176
177     if ( net->version == FG_NET_GUI_VERSION ) {
178         // cout << "pos = " << net->longitude << " " << net->latitude << endl;
179         // cout << "sea level rad = " << cur_fdm_state->get_Sea_level_radius()
180         //      << endl;
181         cur_fdm_state->_updateGeodeticPosition( net->latitude,
182                                                 net->longitude,
183                                                 net->altitude
184                                                   * SG_METER_TO_FEET );
185         cur_fdm_state->_set_Euler_Angles( net->phi,
186                                           net->theta,
187                                           net->psi );
188
189         cur_fdm_state->_set_V_calibrated_kts( net->vcas );
190         cur_fdm_state->_set_Climb_Rate( net->climb_rate );
191
192         for (i = 0; i < net->num_tanks; ++i ) {
193             SGPropertyNode * node
194                 = fgGetNode("/consumables/fuel/tank", i, true);
195             node->setDoubleValue("level-gal_us", net->fuel_quantity[i] );
196         }
197
198         if ( net->cur_time ) {
199             fgSetLong("/sim/time/cur-time-override", net->cur_time);
200         }
201
202         globals->set_warp( net->warp );
203     } else {
204         SG_LOG( SG_IO, SG_ALERT,
205                 "Error: version mismatch in FGNetNativeGUI2Props()" );
206         SG_LOG( SG_IO, SG_ALERT,
207                 "\tread " << net->version << " need " << FG_NET_GUI_VERSION );
208         SG_LOG( SG_IO, SG_ALERT,
209                 "\tNeed to upgrade net_fdm.hxx and recompile." );
210     }
211 }
212
213
214 // process work for this port
215 bool FGNativeGUI::process() {
216     SGIOChannel *io = get_io_channel();
217     int length = sizeof(buf);
218
219     if ( get_direction() == SG_IO_OUT ) {
220         // cout << "size of cur_fdm_state = " << length << endl;
221         FGProps2NetGUI( &buf );
222         if ( ! io->write( (char *)(& buf), length ) ) {
223             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
224             return false;
225         }
226     } else if ( get_direction() == SG_IO_IN ) {
227         if ( io->get_type() == sgFileType ) {
228             if ( io->read( (char *)(& buf), length ) == length ) {
229                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
230                 FGNetGUI2Props( &buf );
231             }
232         } else {
233             while ( io->read( (char *)(& buf), length ) == length ) {
234                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
235                 FGNetGUI2Props( &buf );
236             }
237         }
238     }
239
240     return true;
241 }
242
243
244 // close the channel
245 bool FGNativeGUI::close() {
246     SGIOChannel *io = get_io_channel();
247
248     set_enabled( false );
249
250     if ( ! io->close() ) {
251         return false;
252     }
253
254     return true;
255 }