Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keyboard_states: use HashMap #216

Merged
merged 1 commit into from
Jul 19, 2023

Conversation

ConcurrentCrab
Copy link
Contributor

keyboard_stream_map and keyboard_states have to be kept in sync, but beacause of states being a vector, it had different semantics during removal. If 2 devices were added and the first was removed then the vector shifts the second device to the front but the map obviously did not, leading to the collections becoming out of sync. This cause out-of-bounds errors on the states vector when accessing it using stream_map's keys.

This patch changes keyboard_states to be a map as well, and both maps are indexed using the udev input path as a key. A string key is not ideal, but is close to being the only unique ID for a connected device (physical path is not made serialised in many udev events for reasons unknown.), besides being easily accessible from both disjoint APIs being used here, evdev and tokio_udev.

An additional concern may be that tokio's StreamMap manages its own members and removes streams upon completion, but:
a) We don't index into stream_map at all, only consume from it,
so this will never be an issue
b) the corresponding entry in states will be removed as well upon
the udev remove event

keyboard_stream_map and keyboard_states have to be kept in sync, but
beacause of states being a vector, it had different semantics during
removal. If 2 devices were added and the first was removed then the
vector shifts the second device to the front but the map obviously
did not, leading to the collections becoming out of sync. This cause
out-of-bounds errors on the states vector when accessing it using
stream_map's keys.

This patch changes keyboard_states to be a map as well, and both maps
are indexed using the udev input path as a key. A string key is not
ideal, but is close to being the only unique ID for a connected
device (physical path is not made serialised in many udev events for
reasons unknown.), besides being easily accessible from both disjoint
APIs being used here, evdev and tokio_udev.

An additional concern may be that tokio's StreamMap manages its own
members and removes streams upon completion, but:
a) We don't index into stream_map at all, only consume from it,
   so this will never be an issue
b) the corresponding entry in states will be removed as well upon
   the udev remove event
@ConcurrentCrab
Copy link
Contributor Author

Force-pushed some tweaks with logging and error handling.

Copy link
Member

@Shinyzenith Shinyzenith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The patch looks great. Thank you for the in-depth report.

@Shinyzenith Shinyzenith merged commit 1e7630c into waycrate:udev_async Jul 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants