| 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 - http://www.flightgear.org/~curt |
| 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | // |
| 21 | // $Id$ |
| 22 | |
| 23 | // TODO FIXME Module still contains lots of "static SGPropertyNode"s below. |
| 24 | |
| 25 | #ifdef HAVE_CONFIG_H |
| 26 | # include <config.h> |
| 27 | #endif |
| 28 | |
| 29 | #include <simgear/debug/logstream.hxx> |
| 30 | #include <simgear/io/lowlevel.hxx> // endian tests |
| 31 | #include <simgear/io/iochannel.hxx> |
| 32 | #include <simgear/timing/sg_time.hxx> |
| 33 | |
| 34 | #if FG_HAVE_DDS |
| 35 | #include <simgear/io/SGDataDistributionService.hxx> |
| 36 | #endif |
| 37 | |
| 38 | #include <Main/fg_props.hxx> |
| 39 | #include <Main/globals.hxx> |
| 40 | #include <Scenery/scenery.hxx> |
| 41 | #include <FDM/flightProperties.hxx> |
| 42 | |
| 43 | #include "native_structs.hxx" |
| 44 | #include "native_gui.hxx" |
| 45 | |
| 46 | // FreeBSD works better with this included last ... (?) |
| 47 | #if defined( _MSC_VER ) |
| 48 | # include <windows.h> |
| 49 | #elif defined( __MINGW32__ ) |
| 50 | # include <winsock2.h> |
| 51 | #else |
| 52 | # include <netinet/in.h> // htonl() ntohl() |
| 53 | #endif |
| 54 | |
| 55 | // open hailing frequencies |
| 56 | bool FGNativeGUI::open() { |
| 57 | if ( is_enabled() ) { |
| 58 | SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " |
| 59 | << "is already in use, ignoring" ); |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | SGIOChannel *io = get_io_channel(); |
| 64 | |
| 65 | #if FG_HAVE_DDS |
| 66 | if ( io->get_type() == sgDDSType ) { |
| 67 | SG_DDS_Topic *dds = static_cast<SG_DDS_Topic*>(io); |
| 68 | dds->setup("FG_DDS_GUI" , &FG_DDS_GUI_desc, sizeof (FG_DDS_GUI)); |
| 69 | } |
| 70 | #endif |
| 71 | |
| 72 | if ( ! io->open( get_direction() ) ) { |
| 73 | SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." ); |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | set_enabled( true ); |
| 78 | |
| 79 | fgSetDouble("/position/sea-level-radius-ft", SG_EQUATORIAL_RADIUS_FT); |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | // process work for this port |
| 84 | bool FGNativeGUI::process() { |
| 85 | SGIOChannel *io = get_io_channel(); |
| 86 | int length; |
| 87 | char *buf; |
| 88 | |
| 89 | if ( io->get_type() == sgDDSType ) { |
| 90 | buf = reinterpret_cast<char*>(&gui.dds); |
| 91 | length = sizeof(FG_DDS_GUI); |
| 92 | } else { |
| 93 | buf = reinterpret_cast<char*>(&gui.net); |
| 94 | length = sizeof(FGNetGUI); |
| 95 | } |
| 96 | |
| 97 | if ( get_direction() == SG_IO_OUT ) { |
| 98 | // cout << "size of fdm_state = " << length << endl; |
| 99 | if ( io->get_type() == sgDDSType ) { |
| 100 | FGProps2GUI( globals->get_props(), &gui.dds ); |
| 101 | } else { |
| 102 | FGProps2GUI( globals->get_props(), &gui.net ); |
| 103 | } |
| 104 | |
| 105 | if ( ! io->write( buf, length ) ) { |
| 106 | SG_LOG( SG_IO, SG_ALERT, "Error writing data." ); |
| 107 | return false; |
| 108 | } |
| 109 | } else if ( get_direction() == SG_IO_IN ) { |
| 110 | if ( io->get_type() == sgFileType ) { |
| 111 | if ( io->read( buf, length ) == length ) { |
| 112 | SG_LOG( SG_IO, SG_DEBUG, "Success reading data." ); |
| 113 | FGGUI2Props( globals->get_props(), &gui.net ); |
| 114 | } |
| 115 | } if ( io->get_type() == sgDDSType ) { |
| 116 | while ( io->read( buf, length ) == length ) { |
| 117 | SG_LOG( SG_IO, SG_DEBUG, "Success reading data." ); |
| 118 | FGGUI2Props( globals->get_props(), &gui.dds ); |
| 119 | } |
| 120 | } else { |
| 121 | while ( io->read( buf, length ) == length ) { |
| 122 | SG_LOG( SG_IO, SG_DEBUG, "Success reading data." ); |
| 123 | FGGUI2Props( globals->get_props(), &gui.net ); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | // close the channel |
| 132 | bool FGNativeGUI::close() { |
| 133 | SGIOChannel *io = get_io_channel(); |
| 134 | |
| 135 | set_enabled( false ); |
| 136 | |
| 137 | if ( ! io->close() ) { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | return true; |
| 142 | } |