On Thu Nov 28 18:29:35 2024 +0000, Rémi Bernon wrote:
It doesn't seem to be a problem for joystick ids to change, as far as I can tell Windows does it as well (although it keeps some sort of a cache but given the details below, it also seems quite futile). With this test:
desc.attributes.ProductID = 1; if (!hid_device_start( &desc, 1 )) goto done; hr = IDirectInput8_EnumDevices( di8, DI8DEVCLASS_GAMECTRL, select_default_instance, &d, DIEDFL_ALLDEVICES ); ok( hr == DI_OK, "got hr %#lx.\n", hr ); hid_device_stop( &desc, 1 ); desc.attributes.ProductID = 2; if (!hid_device_start( &desc, 1 )) goto done; hr = IDirectInput8_EnumDevices( di8, DI8DEVCLASS_GAMECTRL, select_default_instance, &d, DIEDFL_ALLDEVICES ); ok( hr == DI_OK, "got hr %#lx.\n", hr ); desc.attributes.ProductID = 1; if (!hid_device_start( &desc, 1 )) goto done; hr = IDirectInput8_EnumDevices( di8, DI8DEVCLASS_GAMECTRL, select_default_instance, &d, DIEDFL_ALLDEVICES ); ok( hr == DI_OK, "got hr %#lx.\n", hr ); hid_device_stop( &desc, 1 ); desc.attributes.ProductID = 2; hid_device_stop( &desc, 1 );
First enumeration shows one device 1 with ID 0. Second enumeration shows one device 2 also with ID 0. Third enumeration, which is done after plugging device 1 back and without unplugging device 2, shows that device 2 ID changes to 1 to avoid collision. It seems to me that using the device enumeration index as joystick ID is just good enough.
Fwiw, device that were created before the enumerated ID change still keep the ID they had at the time of creation, so if you create a IDirectInputDevice from device 1 after first enumeration, it gets ID 0 whenever you query it (including after it's been unplugged). Similarly, if you create a IDirectInputDevice from device 2 after second enumeration, it returns ID 0 whenever you query it, including after the third enumeration when the enumerated device 2 has changed to ID 1.