Skip to content

Commit

Permalink
feat: exposing protected methods needed for Wolf
Browse files Browse the repository at this point in the history
  • Loading branch information
ABeltramo committed May 22, 2024
1 parent 15fa550 commit 96d1045
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
2 changes: 2 additions & 0 deletions include/inputtino/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,8 @@ class PS5Joypad : public Joypad {
protected:
typedef struct PS5JoypadState PS5JoypadState;
std::shared_ptr<PS5JoypadState> _state;
std::string get_mac_address() const;
std::vector<std::string> get_sys_nodes() const;

private:
PS5Joypad(uint16_t vendor_id);
Expand Down
43 changes: 27 additions & 16 deletions src/uhid/joypad_ps5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,22 +163,25 @@ template <typename T> 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<std::string> PS5Joypad::get_nodes() const {
std::vector<std::string> PS5Joypad::get_sys_nodes() const {
std::vector<std::string> 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) {
Expand All @@ -200,16 +203,7 @@ std::vector<std::string> 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());
}
Expand All @@ -226,6 +220,23 @@ std::vector<std::string> PS5Joypad::get_nodes() const {
return nodes;
}

std::vector<std::string> PS5Joypad::get_nodes() const {
std::vector<std::string> 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;
Expand Down

0 comments on commit 96d1045

Please sign in to comment.