Skip to content

Commit

Permalink
fix: proper random MAC address to allow multiple virtual PS joypads
Browse files Browse the repository at this point in the history
  • Loading branch information
ABeltramo committed Jul 29, 2024
1 parent f8f5a81 commit 23823cc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
4 changes: 2 additions & 2 deletions include/inputtino/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ struct DeviceDefinition {
uint16_t product_id;
uint16_t version;

std::string device_phys = "00:11:22:33:44:55";
std::string device_uniq = "00:11:22:33:44:55";
std::string device_phys = "";
std::string device_uniq = "";
};

/**
Expand Down
14 changes: 11 additions & 3 deletions src/uhid/joypad_ps5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ static void on_uhid_event(std::shared_ptr<PS5JoypadState> state, uhid_event ev,
}

void generate_mac_address(PS5JoypadState *state) {
std::default_random_engine generator;
std::uniform_int_distribution<unsigned char> distribution(0, 0xFF);
auto rand = std::bind(std::uniform_int_distribution<unsigned char>{0, 0xFF},
std::default_random_engine{std::random_device()()});
for (int i = 0; i < 6; i++) {
state->mac_address[i] = distribution(generator);
state->mac_address[i] = rand();
}
}

Expand Down Expand Up @@ -155,6 +155,14 @@ Result<PS5Joypad> PS5Joypad::create(const DeviceDefinition &device) {
.report_description = {&uhid::ps5_rdesc[0], &uhid::ps5_rdesc[0] + sizeof(uhid::ps5_rdesc)}};

auto joypad = PS5Joypad(device.vendor_id);

if (def.phys.empty()) {
def.phys = joypad.get_mac_address();
}
if (def.uniq.empty()) {
def.uniq = joypad.get_mac_address();
}

auto dev =
uhid::Device::create(def, [state = joypad._state](uhid_event ev, int fd) { on_uhid_event(state, ev, fd); });
if (dev) {
Expand Down
16 changes: 16 additions & 0 deletions tests/testJoypads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@ TEST_CASE_METHOD(SDLTestsFixture, "PS Joypad", "[SDL]") {
}
REQUIRE(gc);

{ // Test creating a second device
REQUIRE(SDL_NumJoysticks() == 1);
auto joypad2 = std::move(*PS5Joypad::create());
std::this_thread::sleep_for(250ms);

auto devices2 = joypad2.get_nodes();
REQUIRE_THAT(devices2, SizeIs(5)); // 3 eventXX and 2 jsYY
REQUIRE_THAT(devices2, Contains(ContainsSubstring("/dev/input/event")));
REQUIRE_THAT(devices2, Contains(ContainsSubstring("/dev/input/js")));

flush_sdl_events();
REQUIRE(SDL_NumJoysticks() == 2);
SDL_GameController *gc2 = SDL_GameControllerOpen(1);
REQUIRE(SDL_GameControllerGetType(gc2) == SDL_CONTROLLER_TYPE_PS5);
}

REQUIRE(SDL_GameControllerGetType(gc) == SDL_CONTROLLER_TYPE_PS5);
{ // Rumble
// Checking for basic capability
Expand Down

0 comments on commit 23823cc

Please sign in to comment.