diff --git a/include/inputtino/input.hpp b/include/inputtino/input.hpp index b4500ec..b63f0f0 100644 --- a/include/inputtino/input.hpp +++ b/include/inputtino/input.hpp @@ -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 = ""; }; /** diff --git a/src/uhid/joypad_ps5.cpp b/src/uhid/joypad_ps5.cpp index 790dede..964ed86 100644 --- a/src/uhid/joypad_ps5.cpp +++ b/src/uhid/joypad_ps5.cpp @@ -117,10 +117,10 @@ static void on_uhid_event(std::shared_ptr state, uhid_event ev, } void generate_mac_address(PS5JoypadState *state) { - std::default_random_engine generator; - std::uniform_int_distribution distribution(0, 0xFF); + auto rand = std::bind(std::uniform_int_distribution{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(); } } @@ -155,6 +155,14 @@ Result 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) { diff --git a/tests/testJoypads.cpp b/tests/testJoypads.cpp index 5944400..a936c6f 100644 --- a/tests/testJoypads.cpp +++ b/tests/testJoypads.cpp @@ -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