inputs
inputs copied to clipboard
Allow re-scan of devices to support hot-plugging
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
I now see that pull request #81 fixes the same problem, but doesn't add a function to re-scan devices.
Thanks. I just need to find a day to test with the 100 devices.
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
.
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
>>>
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?