config/binds: Try XF86-prefixed key names
Browse files at this commitfb706d63b94071fea741f89711dcec169d9fe56dParent: 9955d921 changed file +42 -1
- niri-config/src/binds.rs +42 -1
niri-config/src/binds.rsmodified+42 -1
| @@ -1358,6 +1358,24 @@ impl FromStr for Key { | |||
| 1358 | Trigger::Modifier(ModKey::IsoLevel5Shift) | 1358 | Trigger::Modifier(ModKey::IsoLevel5Shift) |
| 1359 | } else { | 1359 | } else { |
| 1360 | let mut keysym = keysym_from_name(key, KEYSYM_CASE_INSENSITIVE); | 1360 | let mut keysym = keysym_from_name(key, KEYSYM_CASE_INSENSITIVE); |
| 1361 | + | ||
| 1362 | + let with_xf86; | ||
| 1363 | + let mut final_key = key; | ||
| 1364 | + if keysym.raw() == KEY_NoSymbol { | ||
| 1365 | + // If the key didn't match, try it with an XF86 prefix. | ||
| 1366 | + // This way, you can write e.g. AudioMute instead of XF86AudioMute. | ||
| 1367 | + // | ||
| 1368 | + // This is actually needed for the xdg-desktop-portal-gnome implementation of the | ||
| 1369 | + // GlobalShortcuts portal, as it will request non-XF86-prefixed names. Mutter then | ||
| 1370 | + // does this same exact prefixing: | ||
| 1371 | + // https://gitlab.gnome.org/GNOME/mutter/-/blob/b48811767189fb5446bd1b831e98271a21866e09/src/core/meta-accel-parse.c#L302-311 | ||
| 1372 | + with_xf86 = format!("XF86{key}"); | ||
| 1373 | + keysym = keysym_from_name(&with_xf86, KEYSYM_CASE_INSENSITIVE); | ||
| 1374 | + if keysym.raw() != KEY_NoSymbol { | ||
| 1375 | + final_key = &with_xf86; | ||
| 1376 | + } | ||
| 1377 | + } | ||
| 1378 | + | ||
| 1361 | // The keyboard event handling code can receive either | 1379 | // The keyboard event handling code can receive either |
| 1362 | // XF86ScreenSaver or XF86Screensaver, because there is no | 1380 | // XF86ScreenSaver or XF86Screensaver, because there is no |
| 1363 | // case mapping defined between these keysyms. If we just | 1381 | // case mapping defined between these keysyms. If we just |
| @@ -1380,7 +1398,7 @@ impl FromStr for Key { | |||
| 1380 | // | 1398 | // |
| 1381 | // See https://github.com/niri-wm/niri/issues/1969 | 1399 | // See https://github.com/niri-wm/niri/issues/1969 |
| 1382 | if keysym == Keysym::XF86_Screensaver { | 1400 | if keysym == Keysym::XF86_Screensaver { |
| 1383 | - keysym = keysym_from_name(key, KEYSYM_NO_FLAGS); | 1401 | + keysym = keysym_from_name(final_key, KEYSYM_NO_FLAGS); |
| 1384 | if keysym.raw() == KEY_NoSymbol { | 1402 | if keysym.raw() == KEY_NoSymbol { |
| 1385 | keysym = Keysym::XF86_ScreenSaver; | 1403 | keysym = Keysym::XF86_ScreenSaver; |
| 1386 | } | 1404 | } |
| @@ -1431,6 +1449,29 @@ mod tests { | |||
| 1431 | modifiers: Modifiers::empty(), | 1449 | modifiers: Modifiers::empty(), |
| 1432 | } | 1450 | } |
| 1433 | ); | 1451 | ); |
| 1452 | + | ||
| 1453 | + // And now with the auto-prepended XF86 prefix. | ||
| 1454 | + assert_eq!( | ||
| 1455 | + "ScreenSaver".parse::<Key>().unwrap(), | ||
| 1456 | + Key { | ||
| 1457 | + trigger: Trigger::Keysym(Keysym::XF86_ScreenSaver), | ||
| 1458 | + modifiers: Modifiers::empty(), | ||
| 1459 | + }, | ||
| 1460 | + ); | ||
| 1461 | + assert_eq!( | ||
| 1462 | + "Screensaver".parse::<Key>().unwrap(), | ||
| 1463 | + Key { | ||
| 1464 | + trigger: Trigger::Keysym(Keysym::XF86_Screensaver), | ||
| 1465 | + modifiers: Modifiers::empty(), | ||
| 1466 | + } | ||
| 1467 | + ); | ||
| 1468 | + assert_eq!( | ||
| 1469 | + "screensaver".parse::<Key>().unwrap(), | ||
| 1470 | + Key { | ||
| 1471 | + trigger: Trigger::Keysym(Keysym::XF86_ScreenSaver), | ||
| 1472 | + modifiers: Modifiers::empty(), | ||
| 1473 | + } | ||
| 1474 | + ); | ||
| 1434 | } | 1475 | } |
| 1435 | 1476 | ||
| 1436 | #[test] | 1477 | #[test] |