1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use std::iter::Enumerate;
use std::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 IntoIter = KeymapIter<'a>;
    type Item = ffi::KeyCode;

    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()
}