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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use std::ffi::{CStr, CString};
use ffi::*;
use crate::keymap::Keymap;
#[derive(Debug)]
pub struct Leds<'a>(pub &'a Keymap);
impl<'a> Leds<'a> {
pub fn len(&self) -> usize {
unsafe {
xkb_keymap_num_leds(self.0.as_ptr()) as usize
}
}
pub fn by_index(&self, index: usize) -> Option<&str> {
unsafe {
xkb_keymap_led_get_name(self.0.as_ptr(), index as xkb_led_index_t)
.as_ref().map(|ptr| CStr::from_ptr(ptr).to_str().unwrap())
}
}
pub fn by_name<S: AsRef<str>>(&self, name: S) -> Option<usize> {
unsafe {
let string = CString::new(name.as_ref()).unwrap();
match xkb_keymap_led_get_index(self.0.as_ptr(), string.as_ptr()) {
XKB_LED_INVALID =>
None,
index =>
Some(index as usize)
}
}
}
pub fn iter(&self) -> Iter {
Iter::new(self)
}
}
pub struct Iter<'a> {
inner: &'a Leds<'a>,
current: xkb_led_index_t,
length: xkb_led_index_t,
}
impl<'a> Iter<'a> {
fn new(inner: &'a Leds<'a>) -> Iter<'a> {
Iter {
inner: inner,
current: 0,
length: inner.len() as xkb_led_index_t,
}
}
}
impl<'a> Iterator for Iter<'a> {
type Item = (usize, &'a str);
fn next(&mut self) -> Option<Self::Item> {
if self.current == self.length {
return None;
}
unsafe {
let index = self.current;
let name = xkb_keymap_led_get_name(self.inner.0.as_ptr(), index);
self.current += 1;
Some((index as usize, CStr::from_ptr(name).to_str().unwrap()))
}
}
}
impl<'a> IntoIterator for &'a Leds<'a> {
type Item = (usize, &'a str);
type IntoIter = Iter<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}