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

Ignore extra/unknown SDL_Keymod bits (mod_) #882

Merged
merged 2 commits into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
In this file will be listed the changes, especially the breaking ones that one should be careful of
when upgrading from a version of rust-sdl2 to another.

### v0.32.3 (unreleased)

[PR #882](https://github.com/Rust-SDL2/rust-sdl2/pull/882)
Ignore unknown bits in `SDL_Keysym`'s `mod` field (key modifiers) when constructing `Event::KeyDown` and `Event::KeyUp`. Deprecate `sdl2::event::Event::unwrap_keymod`, which had been made public accidentally.

### v0.32.2

[PR #898](https://github.com/Rust-SDL2/rust-sdl2/pull/898):
Expand Down
46 changes: 44 additions & 2 deletions src/sdl2/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,7 @@ impl Event {
window_id: event.windowID,
keycode: Keycode::from_i32(event.keysym.sym as i32),
scancode: Scancode::from_i32(event.keysym.scancode as i32),
keymod: Event::unwrap_keymod(keyboard::Mod::from_bits(event.keysym.mod_)),
keymod: keyboard::Mod::from_bits_truncate(event.keysym.mod_),
repeat: event.repeat != 0
}
}
Expand All @@ -1291,7 +1291,7 @@ impl Event {
window_id: event.windowID,
keycode: Keycode::from_i32(event.keysym.sym as i32),
scancode: Scancode::from_i32(event.keysym.scancode as i32),
keymod: keyboard::Mod::from_bits(event.keysym.mod_).unwrap(),
keymod: keyboard::Mod::from_bits_truncate(event.keysym.mod_),
repeat: event.repeat != 0
}
}
Expand Down Expand Up @@ -1620,6 +1620,7 @@ impl Event {
}} // close unsafe & match
}

#[deprecated(since = "0.32.3", note = "This method has been made public accidentally")]
pub fn unwrap_keymod(keymod_option: Option<keyboard::Mod>) -> keyboard::Mod {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's pub, so I'm not sure if it can be removed without causing breaking change. It might be not intended as pub, however, or might be not visible from outside of crate.

Copy link
Member

Choose a reason for hiding this comment

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

It is currently visible as you can see here: https://docs.rs/sdl2/0.32.2/sdl2/event/enum.Event.html#method.unwrap_keymod

However I don't see why it was made public since nothing returns an Option<Mod> anymore to my knowledge. Maybe keep it, but annotate it with #[deprecate]? See https://github.com/rust-lang/rfcs/blob/master/text/1270-deprecation.md

Also, can you put a note of the deprecation in the changelog? That would be helpful.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Restored this method, added deprecation, added an entry to changelog (I didn't mark deprecation with bold font because probably no one is using this API, so it's low importance deprecation).

Tried to build an app against modified version, at least keyboard input works, including detection of pressed shift and control keys (not tested all modifiers).

Also added tests for unknown bits.

match keymod_option {
None => keyboard::Mod::empty(),
Expand Down Expand Up @@ -2043,6 +2044,47 @@ mod test {
let e2 = Event::from_ll(e.clone().to_ll().unwrap());
assert_eq!(e, e2);
}
}

#[test]
fn test_from_ll_keymod_keydown_unknown_bits() {
let mut raw_event = Event::KeyDown {
timestamp: 0,
window_id: 1,
keycode: None,
scancode: Some(Scancode::Q),
keymod: Mod::empty(),
repeat: false,
}.to_ll().unwrap();

// Simulate SDL setting bits unknown to us, see PR #780
unsafe { raw_event.key.keysym.mod_ = 0xffff; }

if let Event::KeyDown { keymod, .. } = Event::from_ll(raw_event) {
assert_eq!(keymod, Mod::all());
} else {
panic!()
}
}

#[test]
fn test_from_ll_keymod_keyup_unknown_bits() {
let mut raw_event = Event::KeyUp {
timestamp: 0,
window_id: 1,
keycode: None,
scancode: Some(Scancode::Q),
keymod: Mod::empty(),
repeat: false,
}.to_ll().unwrap();

// Simulate SDL setting bits unknown to us, see PR #780
unsafe { raw_event.key.keysym.mod_ = 0xffff; }

if let Event::KeyUp { keymod, .. } = Event::from_ll(raw_event) {
assert_eq!(keymod, Mod::all());
} else {
panic!()
}
}
}