| 1 | // Copyright 2015 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "InputCommon/ControllerInterface/Pipes/Pipes.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <array> |
| 8 | #include <cstdlib> |
| 9 | #include <fcntl.h> |
| 10 | #include <iostream> |
| 11 | #include <locale> |
| 12 | #include <map> |
| 13 | #include <sstream> |
| 14 | #include <string> |
| 15 | #include <unistd.h> |
| 16 | #include <utility> |
| 17 | #include <vector> |
| 18 | |
| 19 | #include "Common/FileUtil.h" |
| 20 | #include "Common/StringUtil.h" |
| 21 | #include "InputCommon/ControllerInterface/ControllerInterface.h" |
| 22 | |
| 23 | namespace ciface::Pipes |
| 24 | { |
| 25 | static const std::array<std::string, 12> s_button_tokens{ |
| 26 | {"A", "B", "X", "Y", "Z", "START", "L", "R", "D_UP", "D_DOWN", "D_LEFT", "D_RIGHT"}}; |
| 27 | |
| 28 | static const std::array<std::string, 2> s_shoulder_tokens{{"L", "R"}}; |
| 29 | |
| 30 | static const std::array<std::string, 2> s_axis_tokens{{"MAIN", "C"}}; |
| 31 | |
| 32 | static double StringToDouble(const std::string& text) |
| 33 | { |
| 34 | std::istringstream is(text); |
| 35 | // ignore current locale |
| 36 | is.imbue(std::locale::classic()); |
| 37 | double result; |
| 38 | is >> result; |
| 39 | return result; |
| 40 | } |
| 41 | |
| 42 | class InputBackend final : public ciface::InputBackend |
| 43 | { |
| 44 | public: |
| 45 | using ciface::InputBackend::InputBackend; |
| 46 | void PopulateDevices() override; |
| 47 | }; |
| 48 | |
| 49 | std::unique_ptr<ciface::InputBackend> CreateInputBackend(ControllerInterface* controller_interface) |
| 50 | { |
| 51 | return std::make_unique<InputBackend>(controller_interface); |
| 52 | } |
| 53 | |
| 54 | void InputBackend::PopulateDevices() |
| 55 | { |
| 56 | // Search the Pipes directory for files that we can open in read-only, |
| 57 | // non-blocking mode. The device name is the virtual name of the file. |
| 58 | File::FSTEntry fst; |
| 59 | std::string dir_path = File::GetUserPath(D_PIPES_IDX); |
| 60 | if (!File::Exists(dir_path)) |
| 61 | return; |
| 62 | fst = File::ScanDirectoryTree(dir_path, false); |
| 63 | if (!fst.isDirectory) |
| 64 | return; |
| 65 | for (unsigned int i = 0; i < fst.size; ++i) |
| 66 | { |
| 67 | const File::FSTEntry& child = fst.children[i]; |
| 68 | if (child.isDirectory) |
| 69 | continue; |
| 70 | int fd = open(child.physicalName.c_str(), O_RDONLY | O_NONBLOCK); |
| 71 | if (fd < 0) |
| 72 | continue; |
| 73 | g_controller_interface.AddDevice(std::make_shared<PipeDevice>(fd, child.virtualName)); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | PipeDevice::PipeDevice(int fd, std::string name) : m_fd(fd), m_name(std::move(name)) |
| 78 | { |
| 79 | for (const auto& tok : s_button_tokens) |
| 80 | { |
| 81 | PipeInput* btn = new PipeInput("Button " + tok); |
| 82 | AddInput(btn); |
| 83 | m_buttons[tok] = btn; |
| 84 | } |
| 85 | for (const auto& tok : s_shoulder_tokens) |
| 86 | { |
| 87 | AddAxis(tok, 0.0); |
| 88 | } |
| 89 | for (const auto& tok : s_axis_tokens) |
| 90 | { |
| 91 | AddAxis(tok + " X", 0.5); |
| 92 | AddAxis(tok + " Y", 0.5); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | PipeDevice::~PipeDevice() |
| 97 | { |
| 98 | close(m_fd); |
| 99 | } |
| 100 | |
| 101 | Core::DeviceRemoval PipeDevice::UpdateInput() |
| 102 | { |
| 103 | // Read any pending characters off the pipe. If we hit a newline, |
| 104 | // then dequeue a command off the front of m_buf and parse it. |
| 105 | char buf[32]; |
| 106 | ssize_t bytes_read = read(m_fd, buf, sizeof buf); |
| 107 | while (bytes_read > 0) |
| 108 | { |
| 109 | m_buf.append(buf, bytes_read); |
| 110 | bytes_read = read(m_fd, buf, sizeof buf); |
| 111 | } |
| 112 | std::size_t newline = m_buf.find("\n"); |
| 113 | while (newline != std::string::npos) |
| 114 | { |
| 115 | std::string command = m_buf.substr(0, newline); |
| 116 | ParseCommand(command); |
| 117 | m_buf.erase(0, newline + 1); |
| 118 | newline = m_buf.find("\n"); |
| 119 | } |
| 120 | return Core::DeviceRemoval::Keep; |
| 121 | } |
| 122 | |
| 123 | void PipeDevice::AddAxis(const std::string& name, double value) |
| 124 | { |
| 125 | // Dolphin uses separate axes for left/right, which complicates things. |
| 126 | PipeInput* ax_hi = new PipeInput("Axis " + name + " +"); |
| 127 | ax_hi->SetState(value); |
| 128 | PipeInput* ax_lo = new PipeInput("Axis " + name + " -"); |
| 129 | ax_lo->SetState(value); |
| 130 | m_axes[name + " +"] = ax_hi; |
| 131 | m_axes[name + " -"] = ax_lo; |
| 132 | AddFullAnalogSurfaceInputs(ax_lo, ax_hi); |
| 133 | } |
| 134 | |
| 135 | void PipeDevice::SetAxis(const std::string& entry, double value) |
| 136 | { |
| 137 | value = std::clamp(value, 0.0, 1.0); |
| 138 | double hi = std::max(0.0, value - 0.5) * 2.0; |
| 139 | double lo = (0.5 - std::min(0.5, value)) * 2.0; |
| 140 | auto search_hi = m_axes.find(entry + " +"); |
| 141 | if (search_hi != m_axes.end()) |
| 142 | search_hi->second->SetState(hi); |
| 143 | auto search_lo = m_axes.find(entry + " -"); |
| 144 | if (search_lo != m_axes.end()) |
| 145 | search_lo->second->SetState(lo); |
| 146 | } |
| 147 | |
| 148 | void PipeDevice::ParseCommand(const std::string& command) |
| 149 | { |
| 150 | const std::vector<std::string> tokens = SplitString(command, ' '); |
| 151 | if (tokens.size() < 2 || tokens.size() > 4) |
| 152 | return; |
| 153 | if (tokens[0] == "PRESS" || tokens[0] == "RELEASE") |
| 154 | { |
| 155 | auto search = m_buttons.find(tokens[1]); |
| 156 | if (search != m_buttons.end()) |
| 157 | search->second->SetState(tokens[0] == "PRESS" ? 1.0 : 0.0); |
| 158 | } |
| 159 | else if (tokens[0] == "SET") |
| 160 | { |
| 161 | if (tokens.size() == 3) |
| 162 | { |
| 163 | double value = StringToDouble(tokens[2]); |
| 164 | SetAxis(tokens[1], (value / 2.0) + 0.5); |
| 165 | } |
| 166 | else if (tokens.size() == 4) |
| 167 | { |
| 168 | double x = StringToDouble(tokens[2]); |
| 169 | double y = StringToDouble(tokens[3]); |
| 170 | SetAxis(tokens[1] + " X", x); |
| 171 | SetAxis(tokens[1] + " Y", y); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } // namespace ciface::Pipes |