From 96d104570b8694e092e2da86c4312990fc54dbc1 Mon Sep 17 00:00:00 2001 From: ABeltramo Date: Wed, 22 May 2024 20:45:12 +0100 Subject: [PATCH] feat: exposing protected methods needed for Wolf --- include/inputtino/input.hpp | 2 ++ src/uhid/joypad_ps5.cpp | 43 +++++++++++++++++++++++-------------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/include/inputtino/input.hpp b/include/inputtino/input.hpp index 838bb62..3de4195 100644 --- a/include/inputtino/input.hpp +++ b/include/inputtino/input.hpp @@ -426,6 +426,8 @@ class PS5Joypad : public Joypad { protected: typedef struct PS5JoypadState PS5JoypadState; std::shared_ptr _state; + std::string get_mac_address() const; + std::vector get_sys_nodes() const; private: PS5Joypad(uint16_t vendor_id); diff --git a/src/uhid/joypad_ps5.cpp b/src/uhid/joypad_ps5.cpp index bf549e8..56bc1c7 100644 --- a/src/uhid/joypad_ps5.cpp +++ b/src/uhid/joypad_ps5.cpp @@ -163,22 +163,25 @@ template std::string to_hex(T i) { return stream.str(); } -std::string mac_to_str(const PS5JoypadState &state) { +std::string PS5Joypad::get_mac_address() const { std::stringstream stream; - stream << std::hex << (unsigned int)state.mac_address[0] << ":" << (unsigned int)state.mac_address[1] << ":" - << (unsigned int)state.mac_address[2] << ":" << (unsigned int)state.mac_address[3] << ":" - << (unsigned int)state.mac_address[4] << ":" << (unsigned int)state.mac_address[5]; + stream << std::hex << (unsigned int)_state->mac_address[0] << ":" << (unsigned int)_state->mac_address[1] << ":" + << (unsigned int)_state->mac_address[2] << ":" << (unsigned int)_state->mac_address[3] << ":" + << (unsigned int)_state->mac_address[4] << ":" << (unsigned int)_state->mac_address[5]; return stream.str(); } /** * The trick here is to match the devices under /sys/devices/virtual/misc/uhid/ * with the MAC address that we've set for the current device + * + * @returns a list of paths to the created input devices ex: + * /sys/devices/virtual/misc/uhid/0003:054C:0CE6.000D/input/input58/ */ -std::vector PS5Joypad::get_nodes() const { +std::vector PS5Joypad::get_sys_nodes() const { std::vector nodes; auto base_path = "/sys/devices/virtual/misc/uhid/"; - auto target_mac = mac_to_str(*this->_state); + auto target_mac = get_mac_address(); if (std::filesystem::exists(base_path)) { auto uhid_entries = std::filesystem::directory_iterator{base_path}; for (auto uhid_entry : uhid_entries) { @@ -200,16 +203,7 @@ std::vector PS5Joypad::get_nodes() const { std::ifstream dev_uniq_file{dev_uniq_path}; std::string line; std::getline(dev_uniq_file, line); - if (line.find(target_mac) != std::string::npos) { - // Found a match! Let's scan the folders for the corresponding event and js node - auto dev_nodes = std::filesystem::directory_iterator{dev_entry.path()}; - for (auto dev_node : dev_nodes) { - if (dev_node.is_directory() && (dev_node.path().filename().string().rfind("event", 0) == 0 || - dev_node.path().filename().string().rfind("js", 0) == 0)) { - nodes.push_back("/dev/input/" / dev_node.path().filename()); - } - } - } + nodes.push_back(dev_entry.path().string()); } else { fprintf(stderr, "Unable to get joypad nodes, path %s does not exist\n", dev_uniq_path.string().c_str()); } @@ -226,6 +220,23 @@ std::vector PS5Joypad::get_nodes() const { return nodes; } +std::vector PS5Joypad::get_nodes() const { + std::vector nodes; + + auto sys_nodes = get_sys_nodes(); + for (const auto dev_entry : sys_nodes) { + auto dev_nodes = std::filesystem::directory_iterator{dev_entry}; + for (auto dev_node : dev_nodes) { + if (dev_node.is_directory() && (dev_node.path().filename().string().rfind("event", 0) == 0 || + dev_node.path().filename().string().rfind("js", 0) == 0)) { + nodes.push_back(("/dev/input/" / dev_node.path().filename()).string()); + } + } + } + + return nodes; +} + void PS5Joypad::set_pressed_buttons(int pressed) { { // First reset everything to non-pressed this->_state->current_state.buttons[0] = 0;