From 5048cf9a153aa2dc7603e3f3d84ed6de61d62a3e Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Fri, 22 Mar 2024 19:24:08 -0500 Subject: [PATCH] ControllerInterface/Pipes: Cleanup and minor fixes. --- .../ControllerInterface/Pipes/Pipes.cpp | 154 +++++++++++------- .../ControllerInterface/Pipes/Pipes.h | 38 ----- 2 files changed, 95 insertions(+), 97 deletions(-) diff --git a/Source/Core/InputCommon/ControllerInterface/Pipes/Pipes.cpp b/Source/Core/InputCommon/ControllerInterface/Pipes/Pipes.cpp index 50175ebaf7..bdffeda833 100644 --- a/Source/Core/InputCommon/ControllerInterface/Pipes/Pipes.cpp +++ b/Source/Core/InputCommon/ControllerInterface/Pipes/Pipes.cpp @@ -5,40 +5,88 @@ #include #include -#include -#include -#include -#include +#include #include -#include #include -#include -#include +#include #include +#include +#include + #include "Common/FileUtil.h" +#include "Common/Logging/Log.h" #include "Common/StringUtil.h" #include "InputCommon/ControllerInterface/ControllerInterface.h" namespace ciface::Pipes { -static const std::array s_button_tokens{ - {"A", "B", "X", "Y", "Z", "START", "L", "R", "D_UP", "D_DOWN", "D_LEFT", "D_RIGHT"}}; +static constexpr std::array s_button_tokens = {"A", "B", "X", "Y", "Z", "START", + "L", "R", "D_UP", "D_DOWN", "D_LEFT", "D_RIGHT"}; -static const std::array s_shoulder_tokens{{"L", "R"}}; +static constexpr std::array s_shoulder_tokens{"L", "R"}; -static const std::array s_axis_tokens{{"MAIN", "C"}}; +static constexpr std::array s_axis_tokens{"MAIN", "C"}; -static double StringToDouble(const std::string& text) +static double StringToDouble(std::string text) { - std::istringstream is(text); - // ignore current locale - is.imbue(std::locale::classic()); - double result; - is >> result; + double result = 0; + TryParse(text, &result); return result; } +class PipeDevice : public Core::Device +{ +public: + PipeDevice(int fd, std::string name); + ~PipeDevice(); + + Core::DeviceRemoval UpdateInput() override; + std::string GetName() const override { return m_name; } + std::string GetSource() const override { return "Pipe"; } + +private: + class ButtonInput : public Input + { + public: + ButtonInput(std::string_view name, const bool* value) : m_name(std::move(name)), m_state(*value) + { + } + std::string GetName() const override { return "Button " + std::string{m_name}; } + ControlState GetState() const override { return m_state; } + + private: + const std::string_view m_name; + const bool& m_state; + }; + + template + class AnalogInput : public Input + { + public: + AnalogInput(std::string name, const ControlState* value) + : m_name(std::move(name)), m_state(*value) + { + } + std::string GetName() const override { return "Axis " + m_name + (Range < 0 ? " -" : " +"); } + ControlState GetState() const override { return m_state / Range; } + + private: + const std::string m_name; + const ControlState& m_state; + }; + + void AddAxis(std::string name); + void ParseCommand(const std::string& command); + void SetAxis(const std::string& entry, double value); + + const int m_fd; + const std::string m_name; + std::string m_buf; + std::map m_buttons; + std::map m_axes; +}; + class InputBackend final : public ciface::InputBackend { public: @@ -53,43 +101,42 @@ std::unique_ptr CreateInputBackend(ControllerInterface* co void InputBackend::PopulateDevices() { - // Search the Pipes directory for files that we can open in read-only, - // non-blocking mode. The device name is the virtual name of the file. - File::FSTEntry fst; - std::string dir_path = File::GetUserPath(D_PIPES_IDX); - if (!File::Exists(dir_path)) - return; - fst = File::ScanDirectoryTree(dir_path, false); - if (!fst.isDirectory) - return; - for (unsigned int i = 0; i < fst.size; ++i) + // Search the "Pipes" directory for fifo files that we can open in read-only, + // non-blocking mode. The filename becomes the Device name. + const std::string& dir_path = File::GetUserPath(D_PIPES_IDX); + std::error_code error; + for (const auto& dir_entry : std::filesystem::directory_iterator{dir_path, error}) { - const File::FSTEntry& child = fst.children[i]; - if (child.isDirectory) + if (!dir_entry.is_fifo()) continue; - int fd = open(child.physicalName.c_str(), O_RDONLY | O_NONBLOCK); + + const int fd = open(dir_entry.path().c_str(), O_RDONLY | O_NONBLOCK); if (fd < 0) continue; - g_controller_interface.AddDevice(std::make_shared(fd, child.virtualName)); + + GetControllerInterface().AddDevice( + std::make_shared(fd, dir_entry.path().filename())); } + + if (error) + NOTICE_LOG_FMT(CONTROLLERINTERFACE, "Pipes/PopulateDevices: {}", error.message()); } -PipeDevice::PipeDevice(int fd, const std::string& name) : m_fd(fd), m_name(name) +PipeDevice::PipeDevice(int fd, std::string name) : m_fd(fd), m_name(std::move(name)) { for (const auto& tok : s_button_tokens) { - PipeInput* btn = new PipeInput("Button " + tok); - AddInput(btn); - m_buttons[tok] = btn; + auto [iter, inserted] = m_buttons.emplace(tok, false); + AddInput(new ButtonInput(tok, &iter->second)); } for (const auto& tok : s_shoulder_tokens) { - AddAxis(tok, 0.0); + AddAxis(tok); } for (const auto& tok : s_axis_tokens) { - AddAxis(tok + " X", 0.5); - AddAxis(tok + " Y", 0.5); + AddAxis(tok + std::string{" X"}); + AddAxis(tok + std::string{" Y"}); } } @@ -103,7 +150,7 @@ Core::DeviceRemoval PipeDevice::UpdateInput() // Read any pending characters off the pipe. If we hit a newline, // then dequeue a command off the front of m_buf and parse it. char buf[32]; - ssize_t bytes_read = read(m_fd, buf, sizeof buf); + ssize_t bytes_read = read(m_fd, buf, sizeof(buf)); while (bytes_read > 0) { m_buf.append(buf, bytes_read); @@ -120,29 +167,18 @@ Core::DeviceRemoval PipeDevice::UpdateInput() return Core::DeviceRemoval::Keep; } -void PipeDevice::AddAxis(const std::string& name, double value) +void PipeDevice::AddAxis(std::string name) { - // Dolphin uses separate axes for left/right, which complicates things. - PipeInput* ax_hi = new PipeInput("Axis " + name + " +"); - ax_hi->SetState(value); - PipeInput* ax_lo = new PipeInput("Axis " + name + " -"); - ax_lo->SetState(value); - m_axes[name + " +"] = ax_hi; - m_axes[name + " -"] = ax_lo; - AddFullAnalogSurfaceInputs(ax_lo, ax_hi); + auto [iter, inserted] = m_axes.emplace(name, 0.0); + AddInput(new AnalogInput<-1>(name, &iter->second)); + AddInput(new AnalogInput<+1>(std::move(name), &iter->second)); } void PipeDevice::SetAxis(const std::string& entry, double value) { - value = std::clamp(value, 0.0, 1.0); - double hi = std::max(0.0, value - 0.5) * 2.0; - double lo = (0.5 - std::min(0.5, value)) * 2.0; - auto search_hi = m_axes.find(entry + " +"); - if (search_hi != m_axes.end()) - search_hi->second->SetState(hi); - auto search_lo = m_axes.find(entry + " -"); - if (search_lo != m_axes.end()) - search_lo->second->SetState(lo); + auto search = m_axes.find(entry); + if (search != m_axes.end()) + search->second = std::clamp(value, 0.0, 1.0) * 2.0 - 1.0; } void PipeDevice::ParseCommand(const std::string& command) @@ -154,14 +190,14 @@ void PipeDevice::ParseCommand(const std::string& command) { auto search = m_buttons.find(tokens[1]); if (search != m_buttons.end()) - search->second->SetState(tokens[0] == "PRESS" ? 1.0 : 0.0); + search->second = tokens[0] == "PRESS"; } else if (tokens[0] == "SET") { if (tokens.size() == 3) { double value = StringToDouble(tokens[2]); - SetAxis(tokens[1], (value / 2.0) + 0.5); + SetAxis(tokens[1], (value + 1.0) / 2.0); } else if (tokens.size() == 4) { diff --git a/Source/Core/InputCommon/ControllerInterface/Pipes/Pipes.h b/Source/Core/InputCommon/ControllerInterface/Pipes/Pipes.h index 857d5e8128..919266dd81 100644 --- a/Source/Core/InputCommon/ControllerInterface/Pipes/Pipes.h +++ b/Source/Core/InputCommon/ControllerInterface/Pipes/Pipes.h @@ -3,10 +3,6 @@ #pragma once -#include -#include -#include - #include "InputCommon/ControllerInterface/ControllerInterface.h" namespace ciface::Pipes @@ -23,38 +19,4 @@ namespace ciface::Pipes std::unique_ptr CreateInputBackend(ControllerInterface* controller_interface); -class PipeDevice : public Core::Device -{ -public: - PipeDevice(int fd, const std::string& name); - ~PipeDevice(); - - Core::DeviceRemoval UpdateInput() override; - std::string GetName() const override { return m_name; } - std::string GetSource() const override { return "Pipe"; } - -private: - class PipeInput : public Input - { - public: - PipeInput(const std::string& name) : m_name(name), m_state(0.0) {} - std::string GetName() const override { return m_name; } - ControlState GetState() const override { return m_state; } - void SetState(ControlState state) { m_state = state; } - - private: - const std::string m_name; - ControlState m_state; - }; - - void AddAxis(const std::string& name, double value); - void ParseCommand(const std::string& command); - void SetAxis(const std::string& entry, double value); - - const int m_fd; - const std::string m_name; - std::string m_buf; - std::map m_buttons; - std::map m_axes; -}; } // namespace ciface::Pipes