Skip to main content

KeyRepeater

Struct KeyRepeater 

Source
pub struct KeyRepeater { /* private fields */ }
Expand description

Auto-repeat driven by a timer the caller polls.

Register fd on the same epoll as everything else, feed every real key event to on_key, and call dispatch when the descriptor is readable.

The decisions live in Repeat; this is the timer around them.

Implementations§

Source§

impl KeyRepeater

Source

pub fn new(config: RepeatConfig) -> Result<Self, InputError>

Create a repeater with its own timer.

§Errors

InputError::Config for a zero interval, which would leave the timer firing as fast as the kernel can wake the loop. A zero delay is allowed and means the first repeat lands on the next tick.

InputError::Timer if the timer cannot be created.

Examples found in repository?
examples/input.rs (line 37)
28fn main() {
29    let mut seat = match Seat::open(&SeatOptions::default()) {
30        Ok(seat) => seat,
31        Err(error) => {
32            eprintln!("{error}");
33            std::process::exit(1);
34        }
35    };
36    let mut keyboard = Keyboard::from_names(&KeymapNames::default()).expect("keymap");
37    let mut repeater = KeyRepeater::new(RepeatConfig::default()).expect("timer");
38
39    let started = Instant::now();
40    let mut events = Vec::new();
41    let mut repeats: Vec<KeyEvent> = Vec::new();
42    let mut leds = keyboard.leds();
43
44    loop {
45        {
46            let seat_fd = seat.as_fd();
47            let timer_fd = repeater.fd();
48            let mut fds = [
49                PollFd::new(&seat_fd, PollFlags::IN),
50                PollFd::new(&timer_fd, PollFlags::IN),
51            ];
52            let timeout = Duration::from_secs(30).try_into().expect("timeout");
53            if poll(&mut fds, Some(&timeout)).expect("poll") == 0 {
54                println!("nothing for thirty seconds");
55                return;
56            }
57        }
58
59        let now = u32::try_from(started.elapsed().as_millis()).unwrap_or(u32::MAX);
60
61        events.clear();
62        seat.dispatch(&mut events).expect("dispatch");
63        for event in &mut events {
64            match event {
65                InputEvent::Key(key) => {
66                    keyboard.process_key(key);
67                    repeater.on_key(key, keyboard.should_repeat(key.key));
68                    if report(key) {
69                        return;
70                    }
71                }
72                other => println!("{other:?}"),
73            }
74        }
75
76        repeats.clear();
77        repeater.dispatch(now, &mut repeats).expect("repeat");
78        for key in &mut repeats {
79            // Resolved, not replayed: a Shift pressed since the key went down
80            // changes what this types.
81            keyboard.resolve(key);
82            report(key);
83        }
84
85        let current = keyboard.leds();
86        if current != leds {
87            seat.set_leds(current);
88            leds = current;
89        }
90    }
91}
Source

pub fn fd(&self) -> BorrowedFd<'_>

The descriptor to poll. Readable when a repeat is due.

Examples found in repository?
examples/input.rs (line 47)
28fn main() {
29    let mut seat = match Seat::open(&SeatOptions::default()) {
30        Ok(seat) => seat,
31        Err(error) => {
32            eprintln!("{error}");
33            std::process::exit(1);
34        }
35    };
36    let mut keyboard = Keyboard::from_names(&KeymapNames::default()).expect("keymap");
37    let mut repeater = KeyRepeater::new(RepeatConfig::default()).expect("timer");
38
39    let started = Instant::now();
40    let mut events = Vec::new();
41    let mut repeats: Vec<KeyEvent> = Vec::new();
42    let mut leds = keyboard.leds();
43
44    loop {
45        {
46            let seat_fd = seat.as_fd();
47            let timer_fd = repeater.fd();
48            let mut fds = [
49                PollFd::new(&seat_fd, PollFlags::IN),
50                PollFd::new(&timer_fd, PollFlags::IN),
51            ];
52            let timeout = Duration::from_secs(30).try_into().expect("timeout");
53            if poll(&mut fds, Some(&timeout)).expect("poll") == 0 {
54                println!("nothing for thirty seconds");
55                return;
56            }
57        }
58
59        let now = u32::try_from(started.elapsed().as_millis()).unwrap_or(u32::MAX);
60
61        events.clear();
62        seat.dispatch(&mut events).expect("dispatch");
63        for event in &mut events {
64            match event {
65                InputEvent::Key(key) => {
66                    keyboard.process_key(key);
67                    repeater.on_key(key, keyboard.should_repeat(key.key));
68                    if report(key) {
69                        return;
70                    }
71                }
72                other => println!("{other:?}"),
73            }
74        }
75
76        repeats.clear();
77        repeater.dispatch(now, &mut repeats).expect("repeat");
78        for key in &mut repeats {
79            // Resolved, not replayed: a Shift pressed since the key went down
80            // changes what this types.
81            keyboard.resolve(key);
82            report(key);
83        }
84
85        let current = keyboard.leds();
86        if current != leds {
87            seat.set_leds(current);
88            leds = current;
89        }
90    }
91}
Source

pub const fn held_key(&self) -> Option<u32>

The key currently repeating.

Source

pub fn on_key(&mut self, event: &KeyEvent, repeats: bool)

Feed a real key event.

repeats is the keymap’s answer for this key; see Repeat::on_key.

Examples found in repository?
examples/input.rs (line 67)
28fn main() {
29    let mut seat = match Seat::open(&SeatOptions::default()) {
30        Ok(seat) => seat,
31        Err(error) => {
32            eprintln!("{error}");
33            std::process::exit(1);
34        }
35    };
36    let mut keyboard = Keyboard::from_names(&KeymapNames::default()).expect("keymap");
37    let mut repeater = KeyRepeater::new(RepeatConfig::default()).expect("timer");
38
39    let started = Instant::now();
40    let mut events = Vec::new();
41    let mut repeats: Vec<KeyEvent> = Vec::new();
42    let mut leds = keyboard.leds();
43
44    loop {
45        {
46            let seat_fd = seat.as_fd();
47            let timer_fd = repeater.fd();
48            let mut fds = [
49                PollFd::new(&seat_fd, PollFlags::IN),
50                PollFd::new(&timer_fd, PollFlags::IN),
51            ];
52            let timeout = Duration::from_secs(30).try_into().expect("timeout");
53            if poll(&mut fds, Some(&timeout)).expect("poll") == 0 {
54                println!("nothing for thirty seconds");
55                return;
56            }
57        }
58
59        let now = u32::try_from(started.elapsed().as_millis()).unwrap_or(u32::MAX);
60
61        events.clear();
62        seat.dispatch(&mut events).expect("dispatch");
63        for event in &mut events {
64            match event {
65                InputEvent::Key(key) => {
66                    keyboard.process_key(key);
67                    repeater.on_key(key, keyboard.should_repeat(key.key));
68                    if report(key) {
69                        return;
70                    }
71                }
72                other => println!("{other:?}"),
73            }
74        }
75
76        repeats.clear();
77        repeater.dispatch(now, &mut repeats).expect("repeat");
78        for key in &mut repeats {
79            // Resolved, not replayed: a Shift pressed since the key went down
80            // changes what this types.
81            keyboard.resolve(key);
82            report(key);
83        }
84
85        let current = keyboard.leds();
86        if current != leds {
87            seat.set_leds(current);
88            leds = current;
89        }
90    }
91}
Source

pub fn cancel(&mut self)

Stop repeating, and stop the timer.

Source

pub fn dispatch( &mut self, time_ms: u32, out: &mut Vec<KeyEvent>, ) -> Result<(), InputError>

Drain the timer and append whatever repeats are due.

Nothing is appended if no key is held, or if the descriptor had nothing to say. The events carry only the key – run them through Keyboard::resolve for sym and utf8.

§Errors

InputError::Timer if the timer cannot be read. EAGAIN is not one: a descriptor that was not ready simply produces nothing.

Examples found in repository?
examples/input.rs (line 77)
28fn main() {
29    let mut seat = match Seat::open(&SeatOptions::default()) {
30        Ok(seat) => seat,
31        Err(error) => {
32            eprintln!("{error}");
33            std::process::exit(1);
34        }
35    };
36    let mut keyboard = Keyboard::from_names(&KeymapNames::default()).expect("keymap");
37    let mut repeater = KeyRepeater::new(RepeatConfig::default()).expect("timer");
38
39    let started = Instant::now();
40    let mut events = Vec::new();
41    let mut repeats: Vec<KeyEvent> = Vec::new();
42    let mut leds = keyboard.leds();
43
44    loop {
45        {
46            let seat_fd = seat.as_fd();
47            let timer_fd = repeater.fd();
48            let mut fds = [
49                PollFd::new(&seat_fd, PollFlags::IN),
50                PollFd::new(&timer_fd, PollFlags::IN),
51            ];
52            let timeout = Duration::from_secs(30).try_into().expect("timeout");
53            if poll(&mut fds, Some(&timeout)).expect("poll") == 0 {
54                println!("nothing for thirty seconds");
55                return;
56            }
57        }
58
59        let now = u32::try_from(started.elapsed().as_millis()).unwrap_or(u32::MAX);
60
61        events.clear();
62        seat.dispatch(&mut events).expect("dispatch");
63        for event in &mut events {
64            match event {
65                InputEvent::Key(key) => {
66                    keyboard.process_key(key);
67                    repeater.on_key(key, keyboard.should_repeat(key.key));
68                    if report(key) {
69                        return;
70                    }
71                }
72                other => println!("{other:?}"),
73            }
74        }
75
76        repeats.clear();
77        repeater.dispatch(now, &mut repeats).expect("repeat");
78        for key in &mut repeats {
79            // Resolved, not replayed: a Shift pressed since the key went down
80            // changes what this types.
81            keyboard.resolve(key);
82            report(key);
83        }
84
85        let current = keyboard.leds();
86        if current != leds {
87            seat.set_leds(current);
88            leds = current;
89        }
90    }
91}

Trait Implementations§

Source§

impl Debug for KeyRepeater

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.