spell_framework/wayland_adapter/
lock_impl.rs

1use slint::{
2    PhysicalSize, SharedString,
3    platform::{/*Key,*/ WindowEvent},
4};
5use smithay_client_toolkit::{
6    compositor::CompositorHandler,
7    output::{OutputHandler, OutputState},
8    reexports::client::{
9        Connection, QueueHandle,
10        protocol::{wl_output, wl_seat, wl_surface},
11    },
12    registry::{ProvidesRegistryState, RegistryState},
13    registry_handlers,
14    seat::{
15        Capability, SeatHandler, SeatState,
16        keyboard::{KeyboardHandler /*Keysym*/},
17    },
18    session_lock::{
19        SessionLock, SessionLockHandler, SessionLockSurface, SessionLockSurfaceConfigure,
20    },
21    shm::{Shm, ShmHandler, slot::Buffer},
22};
23use tracing::{info /*warn*/};
24
25use crate::{
26    slint_adapter::SpellSkiaWinAdapter,
27    wayland_adapter::{SpellLock, way_helper::get_string},
28};
29
30impl ProvidesRegistryState for SpellLock {
31    fn registry(&mut self) -> &mut RegistryState {
32        &mut self.registry_state
33    }
34    registry_handlers![OutputState,];
35}
36
37impl ShmHandler for SpellLock {
38    fn shm_state(&mut self) -> &mut Shm {
39        &mut self.shm
40    }
41}
42
43impl OutputHandler for SpellLock {
44    fn output_state(&mut self) -> &mut OutputState {
45        &mut self.output_state
46    }
47
48    fn new_output(
49        &mut self,
50        _conn: &Connection,
51        _qh: &QueueHandle<Self>,
52        _output: wl_output::WlOutput,
53    ) {
54        info!("New output source added");
55    }
56
57    fn update_output(
58        &mut self,
59        _conn: &Connection,
60        _qh: &QueueHandle<Self>,
61        _output: wl_output::WlOutput,
62    ) {
63        info!("Updated output source");
64    }
65
66    fn output_destroyed(
67        &mut self,
68        _conn: &Connection,
69        _qh: &QueueHandle<Self>,
70        _output: wl_output::WlOutput,
71    ) {
72        info!("Output is destroyed");
73    }
74}
75
76impl CompositorHandler for SpellLock {
77    fn scale_factor_changed(
78        &mut self,
79        _conn: &Connection,
80        _qh: &QueueHandle<Self>,
81        _surface: &wl_surface::WlSurface,
82        _new_factor: i32,
83    ) {
84        info!("Scale factor changed");
85    }
86
87    fn transform_changed(
88        &mut self,
89        _conn: &Connection,
90        _qh: &QueueHandle<Self>,
91        _surface: &wl_surface::WlSurface,
92        _new_transform: wl_output::Transform,
93    ) {
94        info!("Compositor transformation changed");
95    }
96
97    fn frame(
98        &mut self,
99        _conn: &Connection,
100        qh: &QueueHandle<Self>,
101        _surface: &wl_surface::WlSurface,
102        _time: u32,
103    ) {
104        self.converter_lock(qh);
105    }
106
107    fn surface_enter(
108        &mut self,
109        _conn: &Connection,
110        _qh: &QueueHandle<Self>,
111        _surface: &wl_surface::WlSurface,
112        _output: &wl_output::WlOutput,
113    ) {
114        info!("Surface entered");
115    }
116
117    fn surface_leave(
118        &mut self,
119        _conn: &Connection,
120        _qh: &QueueHandle<Self>,
121        _surface: &wl_surface::WlSurface,
122        _output: &wl_output::WlOutput,
123    ) {
124        info!("Surface left");
125    }
126}
127
128impl SessionLockHandler for SpellLock {
129    fn locked(&mut self, _conn: &Connection, _qh: &QueueHandle<Self>, _session_lock: SessionLock) {
130        info!("Session is locked");
131    }
132
133    fn finished(
134        &mut self,
135        _conn: &Connection,
136        _qh: &QueueHandle<Self>,
137        _session_lock: SessionLock,
138    ) {
139        info!("Session could not be locked");
140        self.is_locked = true;
141    }
142    fn configure(
143        &mut self,
144        _conn: &Connection,
145        qh: &QueueHandle<Self>,
146        _surface: SessionLockSurface,
147        _configure: SessionLockSurfaceConfigure,
148        _serial: u32,
149    ) {
150        self.converter_lock(qh);
151    }
152}
153
154impl KeyboardHandler for SpellLock {
155    fn enter(
156        &mut self,
157        _conn: &Connection,
158        _qh: &QueueHandle<Self>,
159        _keyboard: &smithay_client_toolkit::reexports::client::protocol::wl_keyboard::WlKeyboard,
160        _surface: &smithay_client_toolkit::reexports::client::protocol::wl_surface::WlSurface,
161        _serial: u32,
162        _raw: &[u32],
163        _keysyms: &[smithay_client_toolkit::seat::keyboard::Keysym],
164    ) {
165        info!("Keyboard focus entered");
166    }
167
168    fn leave(
169        &mut self,
170        _conn: &Connection,
171        _qh: &QueueHandle<Self>,
172        _keyboard: &smithay_client_toolkit::reexports::client::protocol::wl_keyboard::WlKeyboard,
173        _surface: &smithay_client_toolkit::reexports::client::protocol::wl_surface::WlSurface,
174        _serial: u32,
175    ) {
176        info!("Keyboard focus left");
177    }
178
179    fn press_key(
180        &mut self,
181        _conn: &Connection,
182        _qh: &QueueHandle<Self>,
183        _keyboard: &smithay_client_toolkit::reexports::client::protocol::wl_keyboard::WlKeyboard,
184        _serial: u32,
185        event: smithay_client_toolkit::seat::keyboard::KeyEvent,
186    ) {
187        let string_val: SharedString = get_string(event);
188        info!("Key pressed with value : {:?}", string_val);
189        // if string_val == <slint::platform::Key as Into<SharedString>>::into(Key::Backspace) {
190        //     self.loop_handle.enable(&self.backspace.unwrap()).unwrap();
191        //     self.slint_part.as_ref().unwrap().adapters[0]
192        //         .try_dispatch_event(WindowEvent::KeyPressed { text: string_val })
193        //         .unwrap();
194        // } else {
195        self.slint_part.as_ref().unwrap().adapters[0]
196            .try_dispatch_event(WindowEvent::KeyPressed { text: string_val })
197            .unwrap();
198        //}
199    }
200
201    fn release_key(
202        &mut self,
203        _conn: &Connection,
204        _qh: &QueueHandle<Self>,
205        _keyboard: &smithay_client_toolkit::reexports::client::protocol::wl_keyboard::WlKeyboard,
206        _serial: u32,
207        /*mut*/ event: smithay_client_toolkit::seat::keyboard::KeyEvent,
208    ) {
209        info!("Key is released");
210        // if let Err(err) = self.loop_handle.disable(&self.backspace.unwrap()) {
211        //     warn!("{}", err);
212        // }
213        // let key_sym = Keysym::new(event.raw_code);
214        // event.keysym = key_sym;
215        let string_val: SharedString = get_string(event);
216        self.slint_part.as_ref().unwrap().adapters[0]
217            .try_dispatch_event(WindowEvent::KeyReleased { text: string_val })
218            .unwrap();
219    }
220
221    // TODO needs to be implemented to enable functionalities of ctl, shift, alt etc.
222    fn update_modifiers(
223        &mut self,
224        _conn: &Connection,
225        _qh: &QueueHandle<Self>,
226        _keyboard: &smithay_client_toolkit::reexports::client::protocol::wl_keyboard::WlKeyboard,
227        _serial: u32,
228        _modifiers: smithay_client_toolkit::seat::keyboard::Modifiers,
229        _raw_modifiers: smithay_client_toolkit::seat::keyboard::RawModifiers,
230        _layout: u32,
231    ) {
232    }
233
234    fn repeat_key(
235        &mut self,
236        _conn: &Connection,
237        _qh: &QueueHandle<Self>,
238        _keyboard: &smithay_client_toolkit::reexports::client::protocol::wl_keyboard::WlKeyboard,
239        _serial: u32,
240        _event: smithay_client_toolkit::seat::keyboard::KeyEvent,
241    ) {
242        info!("Repeated key entered");
243    }
244    // TODO This method needs to be implemented after the looping mecha is changed to calloop.
245    fn update_repeat_info(
246        &mut self,
247        _conn: &Connection,
248        _qh: &QueueHandle<Self>,
249        _keyboard: &smithay_client_toolkit::reexports::client::protocol::wl_keyboard::WlKeyboard,
250        _info: smithay_client_toolkit::seat::keyboard::RepeatInfo,
251    ) {
252    }
253}
254
255impl SeatHandler for SpellLock {
256    fn seat_state(&mut self) -> &mut SeatState {
257        &mut self.seat_state
258    }
259
260    fn new_seat(&mut self, _: &Connection, _: &QueueHandle<Self>, _: wl_seat::WlSeat) {}
261
262    fn new_capability(
263        &mut self,
264        _conn: &Connection,
265        qh: &QueueHandle<Self>,
266        seat: wl_seat::WlSeat,
267        capability: Capability,
268    ) {
269        if capability == Capability::Keyboard && self.keyboard_state.board.is_none() {
270            info!("Setting keyboard capability");
271            let keyboard = self
272                .seat_state
273                .get_keyboard(qh, &seat, None)
274                .expect("Failed to create keyboard");
275            self.keyboard_state.board = Some(keyboard);
276        }
277    }
278
279    fn remove_capability(
280        &mut self,
281        _conn: &Connection,
282        _: &QueueHandle<Self>,
283        _: wl_seat::WlSeat,
284        capability: Capability,
285    ) {
286        if capability == Capability::Keyboard && self.keyboard_state.board.is_some() {
287            info!("Unsettting keyboard capability");
288            self.keyboard_state.board.take().unwrap().release();
289        }
290    }
291
292    fn remove_seat(&mut self, _: &Connection, _: &QueueHandle<Self>, _: wl_seat::WlSeat) {}
293}
294
295/// It is an internal struct used by [`SpellLock`] internally.
296pub struct SpellSlintLock {
297    pub(crate) adapters: Vec<std::rc::Rc<SpellSkiaWinAdapter>>,
298    pub(crate) size: Vec<PhysicalSize>,
299    pub(crate) wayland_buffer: Vec<Buffer>,
300}