diff --git a/include/shared.h b/include/shared.h index 820b56f..4b8375f 100644 --- a/include/shared.h +++ b/include/shared.h @@ -46,6 +46,7 @@ inline int headsetOutputChannel = 0; inline bool capturePttFlag = false; inline sf::Keyboard::Scancode ptt = sf::Keyboard::Scan::Unknown; +inline sf::Keyboard::Key fallbackPtt = sf::Keyboard::Unknown; inline int joyStickId = -1; inline int joyStickPtt = -1; inline bool isPttOpen = false; diff --git a/src/application.cpp b/src/application.cpp index 16a3789..beccc6a 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -1,6 +1,7 @@ #include "application.h" #include "afv-native/event.h" +#include "shared.h" #include @@ -56,6 +57,10 @@ App::App() toml::find_or(cfg::mConfig, "user", "ptt", static_cast(sf::Keyboard::Scan::Unknown))); + shared::fallbackPtt = static_cast( + toml::find_or(cfg::mConfig, "user", "fallbackPtt", + static_cast(sf::Keyboard::Key::Unknown))); + shared::joyStickId = static_cast( toml::find_or(cfg::mConfig, "user", "joyStickId", -1)); shared::joyStickPtt = static_cast( @@ -313,7 +318,8 @@ void App::eventCallback( } if (evt == afv_native::ClientEventType::PilotRxOpen) { - // Bug in that this applies to RX to all station types, including ATC, not only pilots + // Bug in that this applies to RX to all station types, including ATC, + // not only pilots if (data != nullptr && data2 != nullptr) { int frequency = *reinterpret_cast(data); std::string callsign = *reinterpret_cast(data2); @@ -377,7 +383,11 @@ void App::render_frame() shared::isPttOpen = false; } } else { - if (!sf::Keyboard::isKeyPressed(shared::ptt)) { + if (shared::fallbackPtt != sf::Keyboard::Unknown) { + if (!sf::Keyboard::isKeyPressed(shared::fallbackPtt)) { + shared::isPttOpen = false; + } + } else if (!sf::Keyboard::isKeyPressed(shared::ptt)) { shared::isPttOpen = false; } } @@ -390,7 +400,11 @@ void App::render_frame() shared::isPttOpen = true; } } else { - if (sf::Keyboard::isKeyPressed(shared::ptt)) { + if (shared::fallbackPtt != sf::Keyboard::Unknown) { + if (sf::Keyboard::isKeyPressed(shared::fallbackPtt)) { + shared::isPttOpen = true; + } + } else if (sf::Keyboard::isKeyPressed(shared::ptt)) { shared::isPttOpen = true; } } diff --git a/src/main.cpp b/src/main.cpp index 099bd31..050fa7c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,26 +1,25 @@ -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - #include "application.h" #include "config.h" #include "data_file_handler.h" #include "imgui-SFML.h" #include "imgui.h" -#include "shared.h" #include "native/single_instance.h" +#include "native/window_manager.h" +#include "shared.h" #include "spdlog/spdlog.h" #include "ui/style.h" #include "updater.h" -#include "native/window_manager.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include // Main code int main(int, char**) @@ -45,10 +44,13 @@ int main(int, char**) std::string iconName = "icon_mac.png"; #endif - if (!image.loadFromFile((vector_audio::Configuration::get_resource_folder() / iconName).string())) { + if (!image.loadFromFile( + (vector_audio::Configuration::get_resource_folder() / iconName) + .string())) { spdlog::error("Could not load application icon"); } else { - window.setIcon(image.getSize().x, image.getSize().y, image.getPixelsPtr()); + window.setIcon( + image.getSize().x, image.getSize().y, image.getPixelsPtr()); } if (!ImGui::SFML::Init(window, false)) { @@ -67,7 +69,8 @@ int main(int, char**) // Setup Dear ImGui style ImGui::StyleColorsDark(); - std::filesystem::path p = vector_audio::Configuration::get_resource_folder() / std::filesystem::path("JetBrainsMono-Regular.ttf"); + std::filesystem::path p = vector_audio::Configuration::get_resource_folder() + / std::filesystem::path("JetBrainsMono-Regular.ttf"); io.Fonts->AddFontFromFileTTF(p.string().c_str(), 18.0); if (!ImGui::SFML::UpdateFontTexture()) { @@ -94,12 +97,12 @@ int main(int, char**) // Poll and handle events (inputs, window resize, etc.) // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to // tell if dear imgui wants to use your inputs. - // - When io.WantCaptureMouse is true, do not dispatch mouse input data to - // your main application. + // - When io.WantCaptureMouse is true, do not dispatch mouse input data + // to your main application. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input - // data to your main application. Generally you may always pass all inputs - // to dear imgui, and hide them from your application based on those two - // flags. + // data to your main application. Generally you may always pass all + // inputs to dear imgui, and hide them from your application based on + // those two flags. sf::Event event; while (window.pollEvent(event)) { ImGui::SFML::ProcessEvent(window, event); @@ -109,13 +112,45 @@ int main(int, char**) } else if (event.type == sf::Event::KeyPressed) { // Capture the new Ptt key if (vector_audio::shared::capturePttFlag) { + vector_audio::shared::fallbackPtt + = sf::Keyboard::Key::Unknown; // Reset fallback vector_audio::shared::ptt = event.key.scancode; + if (vector_audio::shared::ptt + == sf::Keyboard::Scan::Unknown) { + spdlog::warn("Unknown scancode key when trying to " + "register PTT, falling back to key code"); + auto fallbackKey + = event.key.code; // Fallback to key code + if (fallbackKey != sf::Keyboard::Key::Unknown) { + + vector_audio::shared::ptt + = sf::Keyboard::delocalize(fallbackKey); + vector_audio::shared::fallbackPtt = fallbackKey; + auto keyName = static_cast( + sf::Keyboard::getDescription( + vector_audio::shared::ptt)); + spdlog::info("Registered PTT key through " + "delocalized logical key: {}", + keyName); + + } else { + spdlog::error( + "Could not register PTT key, even with " + "fallback."); + } + } + vector_audio::shared::joyStickId = -1; vector_audio::shared::joyStickPtt = -1; - vector_audio::Configuration::mConfig["user"]["joyStickId"] = vector_audio::shared::joyStickId; - vector_audio::Configuration::mConfig["user"]["joyStickPtt"] = vector_audio::shared::joyStickPtt; - vector_audio::Configuration::mConfig["user"]["ptt"] = static_cast(vector_audio::shared::ptt); + vector_audio::Configuration::mConfig["user"]["joyStickId"] + = vector_audio::shared::joyStickId; + vector_audio::Configuration::mConfig["user"]["joyStickPtt"] + = vector_audio::shared::joyStickPtt; + vector_audio::Configuration::mConfig["user"]["ptt"] + = static_cast(vector_audio::shared::ptt); + vector_audio::Configuration::mConfig["user"]["fallbackPtt"] + = static_cast(vector_audio::shared::fallbackPtt); vector_audio::Configuration::write_config_async(); vector_audio::shared::capturePttFlag = false; } @@ -123,20 +158,28 @@ int main(int, char**) if (vector_audio::shared::capturePttFlag) { vector_audio::shared::ptt = sf::Keyboard::Scan::Unknown; - - vector_audio::shared::joyStickId = event.joystickButton.joystickId; - vector_audio::shared::joyStickPtt = event.joystickButton.button; - - vector_audio::Configuration::mConfig["user"]["joyStickId"] = vector_audio::shared::joyStickId; - vector_audio::Configuration::mConfig["user"]["joyStickPtt"] = vector_audio::shared::joyStickPtt; - vector_audio::Configuration::mConfig["user"]["ptt"] = static_cast(vector_audio::shared::ptt); + vector_audio::shared::fallbackPtt + = sf::Keyboard::Key::Unknown; + + vector_audio::shared::joyStickId + = event.joystickButton.joystickId; + vector_audio::shared::joyStickPtt + = event.joystickButton.button; + + vector_audio::Configuration::mConfig["user"]["joyStickId"] + = vector_audio::shared::joyStickId; + vector_audio::Configuration::mConfig["user"]["joyStickPtt"] + = vector_audio::shared::joyStickPtt; + vector_audio::Configuration::mConfig["user"]["ptt"] + = static_cast(vector_audio::shared::ptt); vector_audio::Configuration::write_config_async(); vector_audio::shared::capturePttFlag = false; } } if (vector_audio::shared::keepWindowOnTop != alwaysOnTop) { - vector_audio::setAlwaysOnTop(window, vector_audio::shared::keepWindowOnTop); + vector_audio::setAlwaysOnTop( + window, vector_audio::shared::keepWindowOnTop); alwaysOnTop = vector_audio::shared::keepWindowOnTop; } } diff --git a/src/ui/modals/settings.cpp b/src/ui/modals/settings.cpp index 15aa6f3..bb2db18 100644 --- a/src/ui/modals/settings.cpp +++ b/src/ui/modals/settings.cpp @@ -1,6 +1,9 @@ #include "ui/modals/settings.h" +#include -void vector_audio::ui::modals::Settings::render(const std::shared_ptr& mClient, const std::function& playAlertSound) +void vector_audio::ui::modals::Settings::render( + const std::shared_ptr& mClient, + const std::function& playAlertSound) { // Settings modal definition if (ImGui::BeginPopupModal("Settings Panel")) { @@ -163,7 +166,7 @@ void vector_audio::ui::modals::Settings::render(const std::shared_ptr