Overhaul the keyboard API in winit to mimic the W3C specification
to achieve better crossplatform parity. The `KeyboardInput` event
is now uses `KeyEvent` which consists of:
- `physical_key` - a cross platform way to refer to scancodes;
- `logical_key` - keysym value, which shows your key respecting the
layout;
- `text` - the text produced by this keypress;
- `location` - the location of the key on the keyboard;
- `repeat` - whether the key was produced by the repeat.
And also a `platform_specific` field which encapsulates extra
information on desktop platforms, like key without modifiers
and text with all modifiers.
The `Modifiers` were also slightly reworked as in, the information
whether the left or right modifier is pressed is now also exposed
on platforms where it could be queried reliably. The support was
also added for the web and orbital platforms finishing the API
change.
This change made the `OptionAsAlt` API on macOS redundant thus it
was removed all together.
Co-authored-by: Artúr Kovács <kovacs.artur.barnabas@gmail.com>
Co-authored-by: Kirill Chibisov <contact@kchibisov.com>
Co-authored-by: daxpedda <daxpedda@gmail.com>
Fixes: #2631.
Fixes: #2055.
Fixes: #2032.
Fixes: #1904.
Fixes: #1810.
Fixes: #1700.
Fixes: #1443.
Fixes: #1343.
Fixes: #1208.
Fixes: #1151.
Fixes: #812.
Fixes: #600.
Fixes: #361.
Fixes: #343.
78 lines
1.6 KiB
Rust
78 lines
1.6 KiB
Rust
use std::{iter::Enumerate, slice::Iter};
|
|
|
|
use super::*;
|
|
|
|
pub struct Keymap {
|
|
keys: [u8; 32],
|
|
}
|
|
|
|
pub struct KeymapIter<'a> {
|
|
iter: Enumerate<Iter<'a, u8>>,
|
|
index: usize,
|
|
item: Option<u8>,
|
|
}
|
|
|
|
impl Keymap {
|
|
pub fn iter(&self) -> KeymapIter<'_> {
|
|
KeymapIter {
|
|
iter: self.keys.iter().enumerate(),
|
|
index: 0,
|
|
item: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'a> IntoIterator for &'a Keymap {
|
|
type Item = ffi::KeyCode;
|
|
type IntoIter = KeymapIter<'a>;
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
self.iter()
|
|
}
|
|
}
|
|
|
|
impl Iterator for KeymapIter<'_> {
|
|
type Item = ffi::KeyCode;
|
|
|
|
fn next(&mut self) -> Option<ffi::KeyCode> {
|
|
if self.item.is_none() {
|
|
for (index, &item) in self.iter.by_ref() {
|
|
if item != 0 {
|
|
self.index = index;
|
|
self.item = Some(item);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
self.item.take().map(|item| {
|
|
debug_assert!(item != 0);
|
|
|
|
let bit = first_bit(item);
|
|
|
|
if item != bit {
|
|
// Remove the first bit; save the rest for further iterations
|
|
self.item = Some(item ^ bit);
|
|
}
|
|
|
|
let shift = bit.trailing_zeros() + (self.index * 8) as u32;
|
|
shift as ffi::KeyCode
|
|
})
|
|
}
|
|
}
|
|
|
|
impl XConnection {
|
|
pub fn query_keymap(&self) -> Keymap {
|
|
let mut keys = [0; 32];
|
|
|
|
unsafe {
|
|
(self.xlib.XQueryKeymap)(self.display, keys.as_mut_ptr() as *mut c_char);
|
|
}
|
|
|
|
Keymap { keys }
|
|
}
|
|
}
|
|
|
|
fn first_bit(b: u8) -> u8 {
|
|
1 << b.trailing_zeros()
|
|
}
|