diff --git a/changelog.md b/changelog.md index 0d7fd24f..6fcd69e6 100644 --- a/changelog.md +++ b/changelog.md @@ -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): diff --git a/src/sdl2/event.rs b/src/sdl2/event.rs index 8ff46871..21be5f23 100644 --- a/src/sdl2/event.rs +++ b/src/sdl2/event.rs @@ -1255,7 +1255,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 } } @@ -1267,7 +1267,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 } } @@ -1596,6 +1596,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 { match keymod_option { None => keyboard::Mod::empty(), @@ -2019,7 +2020,48 @@ 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!() + } } }