smithay_client_toolkit/seat/keyboard/
repeat.rs1use std::sync::atomic::Ordering;
2
3use calloop::{LoopHandle, RegistrationToken};
4use wayland_client::{
5 protocol::{
6 wl_keyboard::{self, WlKeyboard},
7 wl_seat, wl_surface,
8 },
9 Dispatch, QueueHandle,
10};
11
12use super::{
13 Capability, KeyEvent, KeyboardData, KeyboardDataExt, KeyboardError, KeyboardHandler,
14 RepeatInfo, SeatError, RMLVO,
15};
16use crate::seat::SeatState;
17
18pub(crate) struct RepeatedKey {
19 pub(crate) key: KeyEvent,
20 pub(crate) is_first: bool,
22 pub(crate) surface: wl_surface::WlSurface,
23}
24
25pub type RepeatCallback<T> = Box<dyn FnMut(&mut T, &WlKeyboard, KeyEvent) + 'static>;
26
27pub(crate) struct RepeatData<T> {
28 pub(crate) current_repeat: Option<RepeatedKey>,
29 pub(crate) repeat_info: RepeatInfo,
30 pub(crate) loop_handle: LoopHandle<'static, T>,
31 pub(crate) callback: RepeatCallback<T>,
32 pub(crate) repeat_token: Option<RegistrationToken>,
33}
34
35impl<T> Drop for RepeatData<T> {
36 fn drop(&mut self) {
37 if let Some(token) = self.repeat_token.take() {
38 self.loop_handle.remove(token);
39 }
40 }
41}
42
43impl SeatState {
44 pub fn get_keyboard_with_repeat<D, T>(
59 &mut self,
60 qh: &QueueHandle<D>,
61 seat: &wl_seat::WlSeat,
62 rmlvo: Option<RMLVO>,
63 loop_handle: LoopHandle<'static, T>,
64 callback: RepeatCallback<T>,
65 ) -> Result<wl_keyboard::WlKeyboard, KeyboardError>
66 where
67 D: Dispatch<wl_keyboard::WlKeyboard, KeyboardData<T>> + KeyboardHandler + 'static,
68 T: 'static,
69 {
70 let udata = match rmlvo {
71 Some(rmlvo) => KeyboardData::from_rmlvo(seat.clone(), rmlvo)?,
72 None => KeyboardData::new(seat.clone()),
73 };
74
75 self.get_keyboard_with_repeat_with_data(qh, seat, udata, loop_handle, callback)
76 }
77
78 pub fn get_keyboard_with_repeat_with_data<D, U>(
93 &mut self,
94 qh: &QueueHandle<D>,
95 seat: &wl_seat::WlSeat,
96 mut udata: U,
97 loop_handle: LoopHandle<'static, <U as KeyboardDataExt>::State>,
98 callback: RepeatCallback<<U as KeyboardDataExt>::State>,
99 ) -> Result<wl_keyboard::WlKeyboard, KeyboardError>
100 where
101 D: Dispatch<wl_keyboard::WlKeyboard, U> + KeyboardHandler + 'static,
102 U: KeyboardDataExt + 'static,
103 {
104 let inner =
105 self.seats.iter().find(|inner| &inner.seat == seat).ok_or(SeatError::DeadObject)?;
106
107 if !inner.data.has_keyboard.load(Ordering::SeqCst) {
108 return Err(SeatError::UnsupportedCapability(Capability::Keyboard).into());
109 }
110
111 let kbd_data = udata.keyboard_data_mut();
112 kbd_data.repeat_data.lock().unwrap().replace(RepeatData {
113 current_repeat: None,
114 repeat_info: RepeatInfo::Disable,
115 loop_handle: loop_handle.clone(),
116 callback,
117 repeat_token: None,
118 });
119 kbd_data.init_compose();
120
121 Ok(seat.get_keyboard(qh, udata))
122 }
123}