config: fix parsing of XF86ScreenSaver key
Browse files at this commit4310c20c320d040f3df7a93de4064e452a1876aeParent: b3245b81 changed file +54 -2
- niri-config/src/binds.rs +54 -2
niri-config/src/binds.rsmodified+54 -2
| @@ -9,7 +9,7 @@ use niri_ipc::{ | ||
| 9 | 9 | ColumnDisplay, LayoutSwitchTarget, PositionChange, SizeChange, WorkspaceReferenceArg, |
| 10 | 10 | }; |
| 11 | 11 | use smithay::input::keyboard::keysyms::KEY_NoSymbol; |
| 12 | -use smithay::input::keyboard::xkb::{keysym_from_name, KEYSYM_CASE_INSENSITIVE}; | |
| 12 | +use smithay::input::keyboard::xkb::{keysym_from_name, KEYSYM_CASE_INSENSITIVE, KEYSYM_NO_FLAGS}; | |
| 13 | 13 | use smithay::input::keyboard::Keysym; |
| 14 | 14 | |
| 15 | 15 | use crate::utils::{expect_only_children, MergeWith}; |
| @@ -984,7 +984,34 @@ impl FromStr for Key { | ||
| 984 | 984 | } else if key.eq_ignore_ascii_case("TouchpadScrollRight") { |
| 985 | 985 | Trigger::TouchpadScrollRight |
| 986 | 986 | } else { |
| 987 | - let keysym = keysym_from_name(key, KEYSYM_CASE_INSENSITIVE); | |
| 987 | + let mut keysym = keysym_from_name(key, KEYSYM_CASE_INSENSITIVE); | |
| 988 | + // The keyboard event handling code can receive either | |
| 989 | + // XF86ScreenSaver or XF86Screensaver, because there is no | |
| 990 | + // case mapping defined between these keysyms. If we just | |
| 991 | + // use the case-insensitive version of keysym_from_name it | |
| 992 | + // is not possible to bind the uppercase version, because the | |
| 993 | + // case-insensitive match prefers the lowercase version when | |
| 994 | + // there is a choice. | |
| 995 | + // | |
| 996 | + // Therefore, when we match this key with the initial | |
| 997 | + // case-insensitive match we try a further case-sensitive match | |
| 998 | + // (so that either key can be bound). If that fails, we change | |
| 999 | + // to the uppercase version because: | |
| 1000 | + // | |
| 1001 | + // - A comment in xkb_keysym_from_name (in libxkbcommon) tells us that the uppercase | |
| 1002 | + // version is the "best" of the two. [0] | |
| 1003 | + // - The xkbcommon crate only has a constant for ScreenSaver. [1] | |
| 1004 | + // | |
| 1005 | + // [0]: https://github.com/xkbcommon/libxkbcommon/blob/45a118d5325b051343b4b174f60c1434196fa7d4/src/keysym.c#L276 | |
| 1006 | + // [1]: https://docs.rs/xkbcommon/latest/xkbcommon/xkb/keysyms/index.html#:~:text=KEY%5FXF86ScreenSaver | |
| 1007 | + // | |
| 1008 | + // See https://github.com/YaLTeR/niri/issues/1969 | |
| 1009 | + if keysym == Keysym::XF86_Screensaver { | |
| 1010 | + keysym = keysym_from_name(key, KEYSYM_NO_FLAGS); | |
| 1011 | + if keysym.raw() == KEY_NoSymbol { | |
| 1012 | + keysym = Keysym::XF86_ScreenSaver; | |
| 1013 | + } | |
| 1014 | + } | |
| 988 | 1015 | if keysym.raw() == KEY_NoSymbol { |
| 989 | 1016 | return Err(miette!("invalid key: {key}")); |
| 990 | 1017 | } |
| @@ -999,6 +1026,31 @@ impl FromStr for Key { | ||
| 999 | 1026 | mod tests { |
| 1000 | 1027 | use super::*; |
| 1001 | 1028 | |
| 1029 | + #[test] | |
| 1030 | + fn parse_xf86_screensaver() { | |
| 1031 | + assert_eq!( | |
| 1032 | + "XF86ScreenSaver".parse::<Key>().unwrap(), | |
| 1033 | + Key { | |
| 1034 | + trigger: Trigger::Keysym(Keysym::XF86_ScreenSaver), | |
| 1035 | + modifiers: Modifiers::empty(), | |
| 1036 | + }, | |
| 1037 | + ); | |
| 1038 | + assert_eq!( | |
| 1039 | + "XF86Screensaver".parse::<Key>().unwrap(), | |
| 1040 | + Key { | |
| 1041 | + trigger: Trigger::Keysym(Keysym::XF86_Screensaver), | |
| 1042 | + modifiers: Modifiers::empty(), | |
| 1043 | + } | |
| 1044 | + ); | |
| 1045 | + assert_eq!( | |
| 1046 | + "xf86screensaver".parse::<Key>().unwrap(), | |
| 1047 | + Key { | |
| 1048 | + trigger: Trigger::Keysym(Keysym::XF86_ScreenSaver), | |
| 1049 | + modifiers: Modifiers::empty(), | |
| 1050 | + } | |
| 1051 | + ); | |
| 1052 | + } | |
| 1053 | + | |
| 1002 | 1054 | #[test] |
| 1003 | 1055 | fn parse_iso_level_shifts() { |
| 1004 | 1056 | assert_eq!( |