Skip to content
This repository has been archived by the owner on May 9, 2024. It is now read-only.

Commit

Permalink
Fallback Ptt key implementation #97
Browse files Browse the repository at this point in the history
  • Loading branch information
pierr3 committed Jan 6, 2024
1 parent fbe9b25 commit b343c5d
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 41 deletions.
1 change: 1 addition & 0 deletions include/shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 17 additions & 3 deletions src/application.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "application.h"

#include "afv-native/event.h"
#include "shared.h"

#include <optional>

Expand Down Expand Up @@ -56,6 +57,10 @@ App::App()
toml::find_or<int>(cfg::mConfig, "user", "ptt",
static_cast<int>(sf::Keyboard::Scan::Unknown)));

shared::fallbackPtt = static_cast<sf::Keyboard::Key>(
toml::find_or<int>(cfg::mConfig, "user", "fallbackPtt",
static_cast<int>(sf::Keyboard::Key::Unknown)));

shared::joyStickId = static_cast<int>(
toml::find_or<int>(cfg::mConfig, "user", "joyStickId", -1));
shared::joyStickPtt = static_cast<int>(
Expand Down Expand Up @@ -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<int*>(data);
std::string callsign = *reinterpret_cast<std::string*>(data2);
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
}
}
Expand Down
109 changes: 76 additions & 33 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
#include <cstdio>

#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/System/Clock.hpp>
#include <SFML/Window/Event.hpp>
#include <SFML/Window/Keyboard.hpp>
#include <filesystem>
#include <memory>
#include <string>
#include <thread>
#include <random>

#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 <cstdio>
#include <filesystem>
#include <memory>
#include <random>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/System/Clock.hpp>
#include <SFML/Window/Event.hpp>
#include <SFML/Window/Keyboard.hpp>
#include <string>
#include <thread>

// Main code
int main(int, char**)
Expand All @@ -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)) {
Expand All @@ -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()) {
Expand All @@ -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);
Expand All @@ -109,34 +112,74 @@ 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<std::string>(
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<int>(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<int>(vector_audio::shared::ptt);
vector_audio::Configuration::mConfig["user"]["fallbackPtt"]
= static_cast<int>(vector_audio::shared::fallbackPtt);
vector_audio::Configuration::write_config_async();
vector_audio::shared::capturePttFlag = false;
}
} else if (event.type == sf::Event::JoystickButtonPressed) {

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<int>(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<int>(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;
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/ui/modals/settings.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "ui/modals/settings.h"
#include <SFML/Window/Keyboard.hpp>

void vector_audio::ui::modals::Settings::render(const std::shared_ptr<afv_native::api::atcClient>& mClient, const std::function<void()>& playAlertSound)
void vector_audio::ui::modals::Settings::render(
const std::shared_ptr<afv_native::api::atcClient>& mClient,
const std::function<void()>& playAlertSound)
{
// Settings modal definition
if (ImGui::BeginPopupModal("Settings Panel")) {
Expand Down Expand Up @@ -163,7 +166,7 @@ void vector_audio::ui::modals::Settings::render(const std::shared_ptr<afv_native
if (shared::ptt == sf::Keyboard::Scan::Unknown
&& shared::joyStickId == -1) {
pttKeyName = "Not set";
} else if (shared::ptt != -1) {
} else if (shared::ptt != sf::Keyboard::Scan::Unknown) {
pttKeyName
= "Key: " + sf::Keyboard::getDescription(shared::ptt);
} else if (shared::joyStickId != -1) {
Expand Down Expand Up @@ -314,8 +317,7 @@ void vector_audio::ui::modals::Settings::render(const std::shared_ptr<afv_native
"Optional: You can choose whether to play the\nsound in your "
"right, left, or both ears.");

std::string channelsDisplay[]
= { "Left + Right", "Left", "Right" };
std::string channelsDisplay[] = { "Left + Right", "Left", "Right" };

ImGui::PushItemWidth(-1.0F);
if (ImGui::BeginCombo("##Channel Setup",
Expand Down Expand Up @@ -399,7 +401,7 @@ void vector_audio::ui::modals::Settings::render(const std::shared_ptr<afv_native
ImGui::NewLine();

if (ImGui::Button("Test alert sound")) {
playAlertSound();
playAlertSound();
}
ImGui::SameLine();
vector_audio::util::HelpMarker(
Expand Down

0 comments on commit b343c5d

Please sign in to comment.