zeth / inputs

Cross-platform Python support for keyboards, mice and gamepads
BSD 3-Clause "New" or "Revised" License
269 stars 87 forks source link

Allow re-scan of devices to support hot-plugging #99

Open j3kestrel opened 3 years ago

j3kestrel commented 3 years ago

The use of a bare generator in EVENT_MAP['type_codes'] makes EVENT_MAP a single use object. Converting the generator to a tuple allows reuse of EVENT_MAP and constructing a new DeviceManager that re-scans for devices.

Addresses issue #66

j3kestrel commented 3 years ago

I now see that pull request #81 fixes the same problem, but doesn't add a function to re-scan devices.

zeth commented 3 years ago

Thanks. I just need to find a day to test with the 100 devices.

BirdLogics commented 2 years ago

This should also fix #106. Attempting to create a new DeviceManager instance manually does not work as rescan because of the bare generator in EVENT_MAP.

cathartyc commented 7 months ago

This fix works only when inputs is imported as a whole. It would not change the instance of devices when it is imported individually (from inputs import devices), as it is shown instead in the examples.


>>> import inputs
>>> from inputs import devices
>>> 
>>> inputs.devices == devices
True
>>> inputs.rescan_devices()
>>> 
>>> inputs.devices == devices
False
>>>
j3kestrel commented 7 months ago

I suspect that is because input.devices not mutated, it is replaced. As a result, inputs.devices and local devices no longer reference the same object. Any local references to input.devices would need to be refreshed after using rescan_devices().

import inputs`
devices = inputs.devices
[...]
inputs.rescan_devices()
devices = inputs.devices

Or, perhaps rescan_devices() could return devices so that it becomes devices = inputs.rescan_devices(). The problem with all of this is that it doesn't solve the problem of a developer expecting devices to be mutated instead of replaced and not expecting to need to re-cache the local reference.

Is there a better solution other than updating examples and documentation?