| 1 | /* |
| 2 | * SPDX-FileCopyrightText: (C) 2019-2022 swift Project Community / Contributors (https://swift-project.org/) |
| 3 | * SPDX-FileCopyrightText: (C) 2019-2022 Lars Toenning <[email protected]> |
| 4 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #ifdef HAVE_CONFIG_H |
| 8 | #include <config.h> |
| 9 | #endif |
| 10 | |
| 11 | #include "plugin.h" |
| 12 | #include "swift_connection.hxx" |
| 13 | #include <Main/fg_props.hxx> |
| 14 | #include <simgear/props/props.hxx> |
| 15 | #include <simgear/structure/commands.hxx> |
| 16 | #include <simgear/structure/event_mgr.hxx> |
| 17 | #include <simgear/structure/subsystem_mgr.hxx> |
| 18 | |
| 19 | namespace flightgear::swift { |
| 20 | |
| 21 | bool SwiftConnection::startServer([[maybe_unused]] const SGPropertyNode*, [[maybe_unused]] SGPropertyNode* root) |
| 22 | { |
| 23 | m_plugin = std::make_unique<CPlugin>(); |
| 24 | return true; |
| 25 | } |
| 26 | |
| 27 | bool SwiftConnection::stopServer([[maybe_unused]] const SGPropertyNode* arg, [[maybe_unused]] SGPropertyNode* root) |
| 28 | { |
| 29 | m_plugin.reset(); |
| 30 | return true; |
| 31 | } |
| 32 | |
| 33 | SwiftConnection::~SwiftConnection() |
| 34 | { |
| 35 | shutdownSwift(); |
| 36 | } |
| 37 | |
| 38 | void SwiftConnection::init() |
| 39 | { |
| 40 | if (!m_initialized) { |
| 41 | globals->get_commands()->addCommand("swiftStart", this, &SwiftConnection::startServer); |
| 42 | globals->get_commands()->addCommand("swiftStop", this, &SwiftConnection::stopServer); |
| 43 | |
| 44 | fgSetBool("/sim/swift/available", true); |
| 45 | m_initialized = true; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | void SwiftConnection::update(double delta_time_sec) |
| 50 | { |
| 51 | if (m_plugin) { |
| 52 | m_plugin->fastLoop(); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | void SwiftConnection::shutdown() |
| 57 | { |
| 58 | shutdownSwift(); |
| 59 | } |
| 60 | |
| 61 | void SwiftConnection::shutdownSwift() |
| 62 | { |
| 63 | if (m_initialized) { |
| 64 | fgSetBool("/sim/swift/available", false); |
| 65 | m_initialized = false; |
| 66 | |
| 67 | globals->get_commands()->removeCommand("swiftStart"); |
| 68 | globals->get_commands()->removeCommand("swiftStop"); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void SwiftConnection::reinit() |
| 73 | { |
| 74 | shutdown(); |
| 75 | init(); |
| 76 | } |
| 77 | |
| 78 | } // namespace flightgear::swift |
| 79 | |
| 80 | // Register the subsystem. |
| 81 | SGSubsystemMgr::Registrant<flightgear::swift::SwiftConnection> registrantSwiftConnection( |
| 82 | SGSubsystemMgr::POST_FDM); |