Add client-server binds tests
Browse files at this commit3f430ad2c75ea448f6f679d36e99994855b31739Parent: 5f4469b7 changed files +621 -4
- niri-config/src/binds.rs +4 -0
- src/input/mod.rs +6 -0
- src/niri.rs +6 -0
- src/tests/binds.rs +284 -0
- src/tests/client.rs +215 -4
- src/tests/mod.rs +2 -0
- src/tests/test_input_backend.rs +104 -0
niri-config/src/binds.rsmodified+4 -0
| @@ -393,6 +393,10 @@ pub enum Action { | |||
| 393 | MruSetScope(MruScope), | 393 | MruSetScope(MruScope), |
| 394 | #[knuffel(skip)] | 394 | #[knuffel(skip)] |
| 395 | MruCycleScope, | 395 | MruCycleScope, |
| 396 | + | ||
| 397 | + // Doesn't look like knuffel allows gating on #[cfg(test)]... | ||
| 398 | + #[knuffel(skip)] | ||
| 399 | + TestAction, | ||
| 396 | } | 400 | } |
| 397 | 401 | ||
| 398 | impl From<niri_ipc::Action> for Action { | 402 | impl From<niri_ipc::Action> for Action { |
src/input/mod.rsmodified+6 -0
| @@ -2428,6 +2428,12 @@ impl State { | |||
| 2428 | self.niri.queue_redraw_mru_output(); | 2428 | self.niri.queue_redraw_mru_output(); |
| 2429 | } | 2429 | } |
| 2430 | } | 2430 | } |
| 2431 | + Action::TestAction => { | ||
| 2432 | + #[cfg(test)] | ||
| 2433 | + { | ||
| 2434 | + self.niri.test_action_count += 1; | ||
| 2435 | + } | ||
| 2436 | + } | ||
| 2431 | } | 2437 | } |
| 2432 | } | 2438 | } |
| 2433 | 2439 | ||
src/niri.rsmodified+6 -0
| @@ -435,6 +435,9 @@ pub struct Niri { | |||
| 435 | 435 | ||
| 436 | #[cfg(feature = "xdp-gnome-screencast")] | 436 | #[cfg(feature = "xdp-gnome-screencast")] |
| 437 | pub casting: Screencasting, | 437 | pub casting: Screencasting, |
| 438 | + | ||
| 439 | + #[cfg(test)] | ||
| 440 | + pub test_action_count: usize, | ||
| 438 | } | 441 | } |
| 439 | 442 | ||
| 440 | smithay::delegate_dispatch2!(State); | 443 | smithay::delegate_dispatch2!(State); |
| @@ -2776,6 +2779,9 @@ impl Niri { | |||
| 2776 | 2779 | ||
| 2777 | #[cfg(feature = "xdp-gnome-screencast")] | 2780 | #[cfg(feature = "xdp-gnome-screencast")] |
| 2778 | casting: screencasting, | 2781 | casting: screencasting, |
| 2782 | + | ||
| 2783 | + #[cfg(test)] | ||
| 2784 | + test_action_count: 0, | ||
| 2779 | }; | 2785 | }; |
| 2780 | 2786 | ||
| 2781 | niri.reset_pointer_inactivity_timer(); | 2787 | niri.reset_pointer_inactivity_timer(); |
src/tests/binds.rsadded+284 -0
| @@ -0,0 +1,284 @@ | |||
| 1 | +use std::fmt::Write as _; | ||
| 2 | + | ||
| 3 | +use insta::assert_snapshot; | ||
| 4 | +use niri_config::{Action, Config}; | ||
| 5 | +use smithay::backend::input::{InputEvent, InputTime, KeyState, Keycode}; | ||
| 6 | +use smithay::input::keyboard::xkb::Keymap; | ||
| 7 | +use wayland_client::protocol::wl_surface::WlSurface; | ||
| 8 | + | ||
| 9 | +use crate::tests::client::ClientId; | ||
| 10 | +use crate::tests::fixture::Fixture; | ||
| 11 | +use crate::tests::test_input_backend::{TestInputBackend, TestKeyboardKeyEvent}; | ||
| 12 | + | ||
| 13 | +enum Op { | ||
| 14 | + Press(Keycode), | ||
| 15 | + Release(Keycode), | ||
| 16 | +} | ||
| 17 | + | ||
| 18 | +fn parse(keymap: &Keymap, input: &str) -> Vec<Op> { | ||
| 19 | + let mut ops = Vec::new(); | ||
| 20 | + for part in input.split_ascii_whitespace() { | ||
| 21 | + let name = &part[1..]; | ||
| 22 | + let Some(key) = keymap.key_by_name(name) else { | ||
| 23 | + panic!("unknown key {name}"); | ||
| 24 | + }; | ||
| 25 | + | ||
| 26 | + let c = part.bytes().next().unwrap(); | ||
| 27 | + let op = match c { | ||
| 28 | + b'+' => Op::Press(key), | ||
| 29 | + b'-' => Op::Release(key), | ||
| 30 | + _ => panic!("keys must begin with + or -, got {c}"), | ||
| 31 | + }; | ||
| 32 | + | ||
| 33 | + ops.push(op); | ||
| 34 | + } | ||
| 35 | + ops | ||
| 36 | +} | ||
| 37 | + | ||
| 38 | +fn set_up(config: &str) -> (Fixture, ClientId, WlSurface) { | ||
| 39 | + let mut config = Config::parse_mem(config).unwrap(); | ||
| 40 | + // knuffel doesn't understand #[cfg(test)]... | ||
| 41 | + for bind in &mut config.binds.0 { | ||
| 42 | + bind.action = Action::TestAction; | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + let mut f = Fixture::with_config(config); | ||
| 46 | + f.add_output(1, (1920, 1080)); | ||
| 47 | + | ||
| 48 | + let id = f.add_client(); | ||
| 49 | + let window = f.client(id).create_window(); | ||
| 50 | + let surface = window.surface.clone(); | ||
| 51 | + window.commit(); | ||
| 52 | + f.roundtrip(id); | ||
| 53 | + | ||
| 54 | + let window = f.client(id).window(&surface); | ||
| 55 | + window.attach_new_buffer(); | ||
| 56 | + window.ack_last_and_commit(); | ||
| 57 | + f.roundtrip(id); | ||
| 58 | + | ||
| 59 | + let _ = f.client(id).state.recent_keyboard_events(&surface); | ||
| 60 | + | ||
| 61 | + (f, id, surface) | ||
| 62 | +} | ||
| 63 | + | ||
| 64 | +fn run_f(f: &mut Fixture, id: ClientId, surface: &WlSurface, input: &str) -> String { | ||
| 65 | + let state = f.niri_state(); | ||
| 66 | + let keyboard = state.niri.seat.get_keyboard().unwrap(); | ||
| 67 | + let ops = keyboard.with_xkb_state(state, |xkb| { | ||
| 68 | + let xkb = xkb.xkb().lock().unwrap(); | ||
| 69 | + let keymap = unsafe { xkb.keymap() }; | ||
| 70 | + parse(keymap, input) | ||
| 71 | + }); | ||
| 72 | + | ||
| 73 | + let mut rv = String::new(); | ||
| 74 | + | ||
| 75 | + for op in ops { | ||
| 76 | + let (code, key_state) = match op { | ||
| 77 | + Op::Press(code) => (code, KeyState::Pressed), | ||
| 78 | + Op::Release(code) => (code, KeyState::Released), | ||
| 79 | + }; | ||
| 80 | + | ||
| 81 | + let state = f.niri_state(); | ||
| 82 | + let keyboard = state.niri.seat.get_keyboard().unwrap(); | ||
| 83 | + keyboard.with_xkb_state(state, |xkb| { | ||
| 84 | + let xkb = xkb.xkb().lock().unwrap(); | ||
| 85 | + let xkb_state = unsafe { xkb.state() }; | ||
| 86 | + let keymap = xkb_state.get_keymap(); | ||
| 87 | + | ||
| 88 | + let c = match key_state { | ||
| 89 | + KeyState::Pressed => "+", | ||
| 90 | + KeyState::Released => "-", | ||
| 91 | + }; | ||
| 92 | + | ||
| 93 | + let name = keymap.key_get_name(code).unwrap_or("None"); | ||
| 94 | + let keysym = xkb_state.key_get_one_sym(code); | ||
| 95 | + | ||
| 96 | + let _ = writeln!(&mut rv, "{c}{name} {:>3} {keysym:?}", code.raw()); | ||
| 97 | + }); | ||
| 98 | + | ||
| 99 | + let prev = state.niri.test_action_count; | ||
| 100 | + | ||
| 101 | + state.process_input_event(InputEvent::<TestInputBackend>::Keyboard { | ||
| 102 | + event: TestKeyboardKeyEvent { | ||
| 103 | + time: InputTime::from_micros(0), | ||
| 104 | + code, | ||
| 105 | + state: key_state, | ||
| 106 | + count: 1, // niri doesn't use this | ||
| 107 | + }, | ||
| 108 | + }); | ||
| 109 | + | ||
| 110 | + let diff = f.niri().test_action_count - prev; | ||
| 111 | + for _ in 0..diff { | ||
| 112 | + let _ = writeln!(&mut rv, " niri test-action"); | ||
| 113 | + } | ||
| 114 | + | ||
| 115 | + f.roundtrip(id); | ||
| 116 | + for event in f.client(id).state.recent_keyboard_events(surface) { | ||
| 117 | + let _ = writeln!(&mut rv, " surface {event}"); | ||
| 118 | + } | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + rv | ||
| 122 | +} | ||
| 123 | + | ||
| 124 | +fn run(config: &str, input: &str) -> String { | ||
| 125 | + let (mut f, id, surface) = set_up(config); | ||
| 126 | + run_f(&mut f, id, &surface, input) | ||
| 127 | +} | ||
| 128 | + | ||
| 129 | +#[test] | ||
| 130 | +fn combos() { | ||
| 131 | + let c = " | ||
| 132 | + binds { | ||
| 133 | + Mod+Ctrl+Q { close-window; } | ||
| 134 | + } | ||
| 135 | + "; | ||
| 136 | + | ||
| 137 | + // Action press/release. | ||
| 138 | + assert_snapshot!( | ||
| 139 | + run(c, "+LWIN +LCTL +LatQ -LatQ -LCTL -LWIN"), | ||
| 140 | + @" | ||
| 141 | + +LWIN 133 XK_Super_L | ||
| 142 | + surface modifiers: depressed=64, latched=0, locked=0, group=0 | ||
| 143 | + surface key pressed: 125 | ||
| 144 | + +LCTL 37 XK_Control_L | ||
| 145 | + surface modifiers: depressed=68, latched=0, locked=0, group=0 | ||
| 146 | + surface key pressed: 29 | ||
| 147 | + +AD01 24 XK_q | ||
| 148 | + niri test-action | ||
| 149 | + -AD01 24 XK_q | ||
| 150 | + -LCTL 37 XK_Control_L | ||
| 151 | + surface modifiers: depressed=64, latched=0, locked=0, group=0 | ||
| 152 | + surface key released: 29 | ||
| 153 | + -LWIN 133 XK_Super_L | ||
| 154 | + surface modifiers: depressed=0, latched=0, locked=0, group=0 | ||
| 155 | + surface key released: 125 | ||
| 156 | + " | ||
| 157 | + ); | ||
| 158 | + | ||
| 159 | + // Extra Alt = no action. | ||
| 160 | + assert_snapshot!( | ||
| 161 | + run(c, "+LWIN +LCTL +LALT +LatQ -LatQ -LALT -LCTL -LWIN"), | ||
| 162 | + @" | ||
| 163 | + +LWIN 133 XK_Super_L | ||
| 164 | + surface modifiers: depressed=64, latched=0, locked=0, group=0 | ||
| 165 | + surface key pressed: 125 | ||
| 166 | + +LCTL 37 XK_Control_L | ||
| 167 | + surface modifiers: depressed=68, latched=0, locked=0, group=0 | ||
| 168 | + surface key pressed: 29 | ||
| 169 | + +LALT 64 XK_Alt_L | ||
| 170 | + surface modifiers: depressed=76, latched=0, locked=0, group=0 | ||
| 171 | + surface key pressed: 56 | ||
| 172 | + +AD01 24 XK_q | ||
| 173 | + surface key pressed: 16 | ||
| 174 | + -AD01 24 XK_q | ||
| 175 | + surface key released: 16 | ||
| 176 | + -LALT 64 XK_Alt_L | ||
| 177 | + surface modifiers: depressed=68, latched=0, locked=0, group=0 | ||
| 178 | + surface key released: 56 | ||
| 179 | + -LCTL 37 XK_Control_L | ||
| 180 | + surface modifiers: depressed=64, latched=0, locked=0, group=0 | ||
| 181 | + surface key released: 29 | ||
| 182 | + -LWIN 133 XK_Super_L | ||
| 183 | + surface modifiers: depressed=0, latched=0, locked=0, group=0 | ||
| 184 | + surface key released: 125 | ||
| 185 | + " | ||
| 186 | + ); | ||
| 187 | + | ||
| 188 | + // Key that doesn't correspond to any bind. | ||
| 189 | + assert_snapshot!( | ||
| 190 | + run(c, "+LWIN +LCTL +LatA -LatA -LCTL -LWIN"), | ||
| 191 | + @" | ||
| 192 | + +LWIN 133 XK_Super_L | ||
| 193 | + surface modifiers: depressed=64, latched=0, locked=0, group=0 | ||
| 194 | + surface key pressed: 125 | ||
| 195 | + +LCTL 37 XK_Control_L | ||
| 196 | + surface modifiers: depressed=68, latched=0, locked=0, group=0 | ||
| 197 | + surface key pressed: 29 | ||
| 198 | + +AC01 38 XK_a | ||
| 199 | + surface key pressed: 30 | ||
| 200 | + -AC01 38 XK_a | ||
| 201 | + surface key released: 30 | ||
| 202 | + -LCTL 37 XK_Control_L | ||
| 203 | + surface modifiers: depressed=64, latched=0, locked=0, group=0 | ||
| 204 | + surface key released: 29 | ||
| 205 | + -LWIN 133 XK_Super_L | ||
| 206 | + surface modifiers: depressed=0, latched=0, locked=0, group=0 | ||
| 207 | + surface key released: 125 | ||
| 208 | + " | ||
| 209 | + ); | ||
| 210 | + | ||
| 211 | + // Press action, press arbitrary, release action, release arbitrary. | ||
| 212 | + assert_snapshot!( | ||
| 213 | + run(c, "+LWIN +LCTL +LatQ +LatA -LatQ -LatA -LCTL -LWIN"), | ||
| 214 | + @" | ||
| 215 | + +LWIN 133 XK_Super_L | ||
| 216 | + surface modifiers: depressed=64, latched=0, locked=0, group=0 | ||
| 217 | + surface key pressed: 125 | ||
| 218 | + +LCTL 37 XK_Control_L | ||
| 219 | + surface modifiers: depressed=68, latched=0, locked=0, group=0 | ||
| 220 | + surface key pressed: 29 | ||
| 221 | + +AD01 24 XK_q | ||
| 222 | + niri test-action | ||
| 223 | + +AC01 38 XK_a | ||
| 224 | + surface key pressed: 30 | ||
| 225 | + -AD01 24 XK_q | ||
| 226 | + -AC01 38 XK_a | ||
| 227 | + surface key released: 30 | ||
| 228 | + -LCTL 37 XK_Control_L | ||
| 229 | + surface modifiers: depressed=64, latched=0, locked=0, group=0 | ||
| 230 | + surface key released: 29 | ||
| 231 | + -LWIN 133 XK_Super_L | ||
| 232 | + surface modifiers: depressed=0, latched=0, locked=0, group=0 | ||
| 233 | + surface key released: 125 | ||
| 234 | + " | ||
| 235 | + ); | ||
| 236 | + | ||
| 237 | + // Press arbitrary, press action, release arbitrary, release action. | ||
| 238 | + assert_snapshot!( | ||
| 239 | + run(c, "+LWIN +LCTL +LatA +LatQ -LatA -LatQ -LCTL -LWIN"), | ||
| 240 | + @" | ||
| 241 | + +LWIN 133 XK_Super_L | ||
| 242 | + surface modifiers: depressed=64, latched=0, locked=0, group=0 | ||
| 243 | + surface key pressed: 125 | ||
| 244 | + +LCTL 37 XK_Control_L | ||
| 245 | + surface modifiers: depressed=68, latched=0, locked=0, group=0 | ||
| 246 | + surface key pressed: 29 | ||
| 247 | + +AC01 38 XK_a | ||
| 248 | + surface key pressed: 30 | ||
| 249 | + +AD01 24 XK_q | ||
| 250 | + niri test-action | ||
| 251 | + -AC01 38 XK_a | ||
| 252 | + surface key released: 30 | ||
| 253 | + -AD01 24 XK_q | ||
| 254 | + -LCTL 37 XK_Control_L | ||
| 255 | + surface modifiers: depressed=64, latched=0, locked=0, group=0 | ||
| 256 | + surface key released: 29 | ||
| 257 | + -LWIN 133 XK_Super_L | ||
| 258 | + surface modifiers: depressed=0, latched=0, locked=0, group=0 | ||
| 259 | + surface key released: 125 | ||
| 260 | + " | ||
| 261 | + ); | ||
| 262 | + | ||
| 263 | + // Trigger action then release mods. | ||
| 264 | + assert_snapshot!( | ||
| 265 | + run(c, "+LWIN +LCTL +LatQ -LCTL -LWIN -LatQ"), | ||
| 266 | + @" | ||
| 267 | + +LWIN 133 XK_Super_L | ||
| 268 | + surface modifiers: depressed=64, latched=0, locked=0, group=0 | ||
| 269 | + surface key pressed: 125 | ||
| 270 | + +LCTL 37 XK_Control_L | ||
| 271 | + surface modifiers: depressed=68, latched=0, locked=0, group=0 | ||
| 272 | + surface key pressed: 29 | ||
| 273 | + +AD01 24 XK_q | ||
| 274 | + niri test-action | ||
| 275 | + -LCTL 37 XK_Control_L | ||
| 276 | + surface modifiers: depressed=64, latched=0, locked=0, group=0 | ||
| 277 | + surface key released: 29 | ||
| 278 | + -LWIN 133 XK_Super_L | ||
| 279 | + surface modifiers: depressed=0, latched=0, locked=0, group=0 | ||
| 280 | + surface key released: 125 | ||
| 281 | + -AD01 24 XK_q | ||
| 282 | + " | ||
| 283 | + ); | ||
| 284 | +} | ||
src/tests/client.rsmodified+215 -4
| @@ -30,8 +30,10 @@ use wayland_client::protocol::wl_buffer::{self, WlBuffer}; | |||
| 30 | use wayland_client::protocol::wl_callback::{self, WlCallback}; | 30 | use wayland_client::protocol::wl_callback::{self, WlCallback}; |
| 31 | use wayland_client::protocol::wl_compositor::WlCompositor; | 31 | use wayland_client::protocol::wl_compositor::WlCompositor; |
| 32 | use wayland_client::protocol::wl_display::WlDisplay; | 32 | use wayland_client::protocol::wl_display::WlDisplay; |
| 33 | +use wayland_client::protocol::wl_keyboard::{self, WlKeyboard}; | ||
| 33 | use wayland_client::protocol::wl_output::{self, WlOutput}; | 34 | use wayland_client::protocol::wl_output::{self, WlOutput}; |
| 34 | use wayland_client::protocol::wl_registry::{self, WlRegistry}; | 35 | use wayland_client::protocol::wl_registry::{self, WlRegistry}; |
| 36 | +use wayland_client::protocol::wl_seat::{self, WlSeat}; | ||
| 35 | use wayland_client::protocol::wl_surface::{self, WlSurface}; | 37 | use wayland_client::protocol::wl_surface::{self, WlSurface}; |
| 36 | use wayland_client::{Connection, Dispatch, Proxy as _, QueueHandle}; | 38 | use wayland_client::{Connection, Dispatch, Proxy as _, QueueHandle}; |
| 37 | 39 | ||
| @@ -51,6 +53,8 @@ pub struct State { | |||
| 51 | 53 | ||
| 52 | pub globals: Vec<Global>, | 54 | pub globals: Vec<Global>, |
| 53 | pub outputs: HashMap<WlOutput, String>, | 55 | pub outputs: HashMap<WlOutput, String>, |
| 56 | + pub surfaces: HashMap<WlSurface, Surface>, | ||
| 57 | + pub seats: HashMap<WlSeat, Seat>, | ||
| 54 | 58 | ||
| 55 | pub compositor: Option<WlCompositor>, | 59 | pub compositor: Option<WlCompositor>, |
| 56 | pub xdg_wm_base: Option<XdgWmBase>, | 60 | pub xdg_wm_base: Option<XdgWmBase>, |
| @@ -91,6 +95,41 @@ pub struct LayerSurface { | |||
| 91 | pub configures_looked_at: usize, | 95 | pub configures_looked_at: usize, |
| 92 | } | 96 | } |
| 93 | 97 | ||
| 98 | +#[derive(Debug, Default)] | ||
| 99 | +pub struct Seat { | ||
| 100 | + pub keyboard: Option<Keyboard>, | ||
| 101 | +} | ||
| 102 | + | ||
| 103 | +#[derive(Debug)] | ||
| 104 | +pub struct Surface { | ||
| 105 | + pub keyboard_events_received: Vec<KeyboardEvent>, | ||
| 106 | + pub keyboard_events_looked_at: usize, | ||
| 107 | +} | ||
| 108 | + | ||
| 109 | +#[derive(Debug)] | ||
| 110 | +pub struct Keyboard { | ||
| 111 | + pub proxy: WlKeyboard, | ||
| 112 | + pub surface: Option<WlSurface>, | ||
| 113 | +} | ||
| 114 | + | ||
| 115 | +#[derive(Debug)] | ||
| 116 | +pub enum KeyboardEvent { | ||
| 117 | + Enter { | ||
| 118 | + keys: Vec<u8>, | ||
| 119 | + }, | ||
| 120 | + Leave, | ||
| 121 | + Key { | ||
| 122 | + key: u32, | ||
| 123 | + state: wl_keyboard::KeyState, | ||
| 124 | + }, | ||
| 125 | + Modifiers { | ||
| 126 | + depressed: u32, | ||
| 127 | + latched: u32, | ||
| 128 | + locked: u32, | ||
| 129 | + group: u32, | ||
| 130 | + }, | ||
| 131 | +} | ||
| 132 | + | ||
| 94 | #[derive(Debug, Clone, Default)] | 133 | #[derive(Debug, Clone, Default)] |
| 95 | pub struct Configure { | 134 | pub struct Configure { |
| 96 | pub size: (i32, i32), | 135 | pub size: (i32, i32), |
| @@ -138,6 +177,41 @@ impl ClientId { | |||
| 138 | } | 177 | } |
| 139 | } | 178 | } |
| 140 | 179 | ||
| 180 | +impl fmt::Display for KeyboardEvent { | ||
| 181 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| 182 | + match self { | ||
| 183 | + KeyboardEvent::Enter { keys } => { | ||
| 184 | + write!(f, "enter: {keys:?}")?; | ||
| 185 | + } | ||
| 186 | + KeyboardEvent::Leave => { | ||
| 187 | + write!(f, "leave")?; | ||
| 188 | + } | ||
| 189 | + KeyboardEvent::Key { key, state } => { | ||
| 190 | + let action = match state { | ||
| 191 | + wl_keyboard::KeyState::Released => "released", | ||
| 192 | + wl_keyboard::KeyState::Pressed => "pressed", | ||
| 193 | + wl_keyboard::KeyState::Repeated => "repeated", | ||
| 194 | + _ => unreachable!(), | ||
| 195 | + }; | ||
| 196 | + write!(f, "key {action}: {key}")?; | ||
| 197 | + } | ||
| 198 | + KeyboardEvent::Modifiers { | ||
| 199 | + depressed, | ||
| 200 | + latched, | ||
| 201 | + locked, | ||
| 202 | + group, | ||
| 203 | + } => { | ||
| 204 | + write!( | ||
| 205 | + f, | ||
| 206 | + "modifiers: depressed={depressed}, latched={latched}, \ | ||
| 207 | + locked={locked}, group={group}" | ||
| 208 | + )?; | ||
| 209 | + } | ||
| 210 | + } | ||
| 211 | + Ok(()) | ||
| 212 | + } | ||
| 213 | +} | ||
| 214 | + | ||
| 141 | impl fmt::Display for Configure { | 215 | impl fmt::Display for Configure { |
| 142 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 216 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 143 | write!(f, "size: {} × {}, ", self.size.0, self.size.1)?; | 217 | write!(f, "size: {} × {}, ", self.size.0, self.size.1)?; |
| @@ -179,6 +253,8 @@ impl Client { | |||
| 179 | qh: qh.clone(), | 253 | qh: qh.clone(), |
| 180 | globals: Vec::new(), | 254 | globals: Vec::new(), |
| 181 | outputs: HashMap::new(), | 255 | outputs: HashMap::new(), |
| 256 | + surfaces: HashMap::new(), | ||
| 257 | + seats: HashMap::new(), | ||
| 182 | compositor: None, | 258 | compositor: None, |
| 183 | xdg_wm_base: None, | 259 | xdg_wm_base: None, |
| 184 | layer_shell: None, | 260 | layer_shell: None, |
| @@ -249,12 +325,25 @@ impl Client { | |||
| 249 | } | 325 | } |
| 250 | 326 | ||
| 251 | impl State { | 327 | impl State { |
| 252 | - pub fn create_window(&mut self) -> &mut Window { | 328 | + pub fn create_surface(&mut self) -> WlSurface { |
| 253 | let compositor = self.compositor.as_ref().unwrap(); | 329 | let compositor = self.compositor.as_ref().unwrap(); |
| 330 | + let surface = compositor.create_surface(&self.qh, ()); | ||
| 331 | + self.surfaces.insert( | ||
| 332 | + surface.clone(), | ||
| 333 | + Surface { | ||
| 334 | + keyboard_events_received: Vec::new(), | ||
| 335 | + keyboard_events_looked_at: 0, | ||
| 336 | + }, | ||
| 337 | + ); | ||
| 338 | + surface | ||
| 339 | + } | ||
| 340 | + | ||
| 341 | + pub fn create_window(&mut self) -> &mut Window { | ||
| 342 | + let surface = self.create_surface(); | ||
| 343 | + | ||
| 254 | let xdg_wm_base = self.xdg_wm_base.as_ref().unwrap(); | 344 | let xdg_wm_base = self.xdg_wm_base.as_ref().unwrap(); |
| 255 | let viewporter = self.viewporter.as_ref().unwrap(); | 345 | let viewporter = self.viewporter.as_ref().unwrap(); |
| 256 | 346 | ||
| 257 | - let surface = compositor.create_surface(&self.qh, ()); | ||
| 258 | let xdg_surface = xdg_wm_base.get_xdg_surface(&surface, &self.qh, ()); | 347 | let xdg_surface = xdg_wm_base.get_xdg_surface(&surface, &self.qh, ()); |
| 259 | let xdg_toplevel = xdg_surface.get_toplevel(&self.qh, ()); | 348 | let xdg_toplevel = xdg_surface.get_toplevel(&self.qh, ()); |
| 260 | let viewport = viewporter.get_viewport(&surface, &self.qh, ()); | 349 | let viewport = viewporter.get_viewport(&surface, &self.qh, ()); |
| @@ -291,11 +380,11 @@ impl State { | |||
| 291 | layer: zwlr_layer_shell_v1::Layer, | 380 | layer: zwlr_layer_shell_v1::Layer, |
| 292 | namespace: String, | 381 | namespace: String, |
| 293 | ) -> &mut LayerSurface { | 382 | ) -> &mut LayerSurface { |
| 294 | - let compositor = self.compositor.as_ref().unwrap(); | 383 | + let surface = self.create_surface(); |
| 384 | + | ||
| 295 | let layer_shell = self.layer_shell.as_ref().unwrap(); | 385 | let layer_shell = self.layer_shell.as_ref().unwrap(); |
| 296 | let viewporter = self.viewporter.as_ref().unwrap(); | 386 | let viewporter = self.viewporter.as_ref().unwrap(); |
| 297 | 387 | ||
| 298 | - let surface = compositor.create_surface(&self.qh, ()); | ||
| 299 | let layer_surface = | 388 | let layer_surface = |
| 300 | layer_shell.get_layer_surface(&surface, output, layer, namespace, &self.qh, ()); | 389 | layer_shell.get_layer_surface(&surface, output, layer, namespace, &self.qh, ()); |
| 301 | let viewport = viewporter.get_viewport(&surface, &self.qh, ()); | 390 | let viewport = viewporter.get_viewport(&surface, &self.qh, ()); |
| @@ -323,6 +412,14 @@ impl State { | |||
| 323 | .find(|w| w.surface == *surface) | 412 | .find(|w| w.surface == *surface) |
| 324 | .unwrap() | 413 | .unwrap() |
| 325 | } | 414 | } |
| 415 | + | ||
| 416 | + pub fn recent_keyboard_events( | ||
| 417 | + &mut self, | ||
| 418 | + surface: &WlSurface, | ||
| 419 | + ) -> impl Iterator<Item = &KeyboardEvent> + '_ { | ||
| 420 | + let surface = self.surfaces.get_mut(surface).unwrap(); | ||
| 421 | + surface.recent_keyboard_events() | ||
| 422 | + } | ||
| 326 | } | 423 | } |
| 327 | 424 | ||
| 328 | impl Window { | 425 | impl Window { |
| @@ -476,6 +573,14 @@ impl LayerSurface { | |||
| 476 | } | 573 | } |
| 477 | } | 574 | } |
| 478 | 575 | ||
| 576 | +impl Surface { | ||
| 577 | + pub fn recent_keyboard_events(&mut self) -> impl Iterator<Item = &KeyboardEvent> { | ||
| 578 | + let start = self.keyboard_events_looked_at; | ||
| 579 | + self.keyboard_events_looked_at = self.keyboard_events_received.len(); | ||
| 580 | + self.keyboard_events_received[start..].iter() | ||
| 581 | + } | ||
| 582 | +} | ||
| 583 | + | ||
| 479 | impl Dispatch<WlCallback, Arc<SyncData>> for State { | 584 | impl Dispatch<WlCallback, Arc<SyncData>> for State { |
| 480 | fn event( | 585 | fn event( |
| 481 | _state: &mut Self, | 586 | _state: &mut Self, |
| @@ -529,6 +634,11 @@ impl Dispatch<WlRegistry, ()> for State { | |||
| 529 | let version = min(version, WlOutput::interface().version); | 634 | let version = min(version, WlOutput::interface().version); |
| 530 | let output = registry.bind(name, version, qh, ()); | 635 | let output = registry.bind(name, version, qh, ()); |
| 531 | state.outputs.insert(output, String::new()); | 636 | state.outputs.insert(output, String::new()); |
| 637 | + } else if interface == WlSeat::interface().name { | ||
| 638 | + let version = min(version, WlSeat::interface().version); | ||
| 639 | + state | ||
| 640 | + .seats | ||
| 641 | + .insert(registry.bind(name, version, qh, ()), Seat::default()); | ||
| 532 | } | 642 | } |
| 533 | 643 | ||
| 534 | let global = Global { | 644 | let global = Global { |
| @@ -567,6 +677,107 @@ impl Dispatch<WlOutput, ()> for State { | |||
| 567 | } | 677 | } |
| 568 | } | 678 | } |
| 569 | 679 | ||
| 680 | +impl Dispatch<WlSeat, ()> for State { | ||
| 681 | + fn event( | ||
| 682 | + state: &mut Self, | ||
| 683 | + seat: &WlSeat, | ||
| 684 | + event: <WlSeat as wayland_client::Proxy>::Event, | ||
| 685 | + _data: &(), | ||
| 686 | + _conn: &Connection, | ||
| 687 | + qh: &QueueHandle<Self>, | ||
| 688 | + ) { | ||
| 689 | + match event { | ||
| 690 | + wl_seat::Event::Capabilities { capabilities } => { | ||
| 691 | + let capabilities = capabilities.into_result().unwrap(); | ||
| 692 | + let data = state.seats.get_mut(seat).unwrap(); | ||
| 693 | + | ||
| 694 | + if capabilities.contains(wl_seat::Capability::Keyboard) { | ||
| 695 | + if data.keyboard.is_none() { | ||
| 696 | + let keyboard = Keyboard { | ||
| 697 | + proxy: seat.get_keyboard(qh, seat.clone()), | ||
| 698 | + surface: None, | ||
| 699 | + }; | ||
| 700 | + data.keyboard = Some(keyboard); | ||
| 701 | + } | ||
| 702 | + } else { | ||
| 703 | + if let Some(keyboard) = data.keyboard.take() { | ||
| 704 | + keyboard.proxy.release(); | ||
| 705 | + } | ||
| 706 | + } | ||
| 707 | + } | ||
| 708 | + wl_seat::Event::Name { .. } => (), | ||
| 709 | + _ => unreachable!(), | ||
| 710 | + } | ||
| 711 | + } | ||
| 712 | +} | ||
| 713 | + | ||
| 714 | +impl Dispatch<WlKeyboard, WlSeat> for State { | ||
| 715 | + fn event( | ||
| 716 | + state: &mut Self, | ||
| 717 | + _keyboard: &WlKeyboard, | ||
| 718 | + event: <WlKeyboard as wayland_client::Proxy>::Event, | ||
| 719 | + seat: &WlSeat, | ||
| 720 | + _conn: &Connection, | ||
| 721 | + _qhandle: &QueueHandle<Self>, | ||
| 722 | + ) { | ||
| 723 | + let seat = state.seats.get_mut(seat).unwrap(); | ||
| 724 | + let keyboard = seat.keyboard.as_mut().unwrap(); | ||
| 725 | + | ||
| 726 | + match event { | ||
| 727 | + wl_keyboard::Event::Keymap { .. } => (), | ||
| 728 | + wl_keyboard::Event::Enter { surface, keys, .. } => { | ||
| 729 | + keyboard.surface = Some(surface.clone()); | ||
| 730 | + | ||
| 731 | + let event = KeyboardEvent::Enter { keys }; | ||
| 732 | + let surface = state.surfaces.get_mut(&surface).unwrap(); | ||
| 733 | + surface.keyboard_events_received.push(event); | ||
| 734 | + } | ||
| 735 | + wl_keyboard::Event::Leave { .. } => { | ||
| 736 | + let surface = keyboard.surface.take().unwrap(); | ||
| 737 | + | ||
| 738 | + let event = KeyboardEvent::Leave; | ||
| 739 | + let surface = state.surfaces.get_mut(&surface).unwrap(); | ||
| 740 | + surface.keyboard_events_received.push(event); | ||
| 741 | + } | ||
| 742 | + wl_keyboard::Event::Key { | ||
| 743 | + key, | ||
| 744 | + state: key_state, | ||
| 745 | + .. | ||
| 746 | + } => { | ||
| 747 | + let key_state = key_state.into_result().unwrap(); | ||
| 748 | + let surface = keyboard.surface.clone().unwrap(); | ||
| 749 | + | ||
| 750 | + let event = KeyboardEvent::Key { | ||
| 751 | + key, | ||
| 752 | + state: key_state, | ||
| 753 | + }; | ||
| 754 | + let surface = state.surfaces.get_mut(&surface).unwrap(); | ||
| 755 | + surface.keyboard_events_received.push(event); | ||
| 756 | + } | ||
| 757 | + wl_keyboard::Event::Modifiers { | ||
| 758 | + mods_depressed, | ||
| 759 | + mods_latched, | ||
| 760 | + mods_locked, | ||
| 761 | + group, | ||
| 762 | + .. | ||
| 763 | + } => { | ||
| 764 | + let surface = keyboard.surface.clone().unwrap(); | ||
| 765 | + | ||
| 766 | + let event = KeyboardEvent::Modifiers { | ||
| 767 | + depressed: mods_depressed, | ||
| 768 | + latched: mods_latched, | ||
| 769 | + locked: mods_locked, | ||
| 770 | + group, | ||
| 771 | + }; | ||
| 772 | + let surface = state.surfaces.get_mut(&surface).unwrap(); | ||
| 773 | + surface.keyboard_events_received.push(event); | ||
| 774 | + } | ||
| 775 | + wl_keyboard::Event::RepeatInfo { .. } => (), | ||
| 776 | + _ => unreachable!(), | ||
| 777 | + } | ||
| 778 | + } | ||
| 779 | +} | ||
| 780 | + | ||
| 570 | impl Dispatch<WlCompositor, ()> for State { | 781 | impl Dispatch<WlCompositor, ()> for State { |
| 571 | fn event( | 782 | fn event( |
| 572 | _state: &mut Self, | 783 | _state: &mut Self, |
src/tests/mod.rsmodified+2 -0
| @@ -3,8 +3,10 @@ use fixture::Fixture; | |||
| 3 | mod client; | 3 | mod client; |
| 4 | mod fixture; | 4 | mod fixture; |
| 5 | mod server; | 5 | mod server; |
| 6 | +mod test_input_backend; | ||
| 6 | 7 | ||
| 7 | mod animations; | 8 | mod animations; |
| 9 | +mod binds; | ||
| 8 | mod floating; | 10 | mod floating; |
| 9 | mod fullscreen; | 11 | mod fullscreen; |
| 10 | mod layer_shell; | 12 | mod layer_shell; |
src/tests/test_input_backend.rsadded+104 -0
| @@ -0,0 +1,104 @@ | |||
| 1 | +use smithay::backend::input::{ | ||
| 2 | + Device, DeviceCapability, Event, InputBackend, InputTime, KeyState, KeyboardKeyEvent, Keycode, | ||
| 3 | + UnusedEvent, | ||
| 4 | +}; | ||
| 5 | +use smithay::output::Output; | ||
| 6 | + | ||
| 7 | +use crate::input::backend_ext::NiriInputDevice; | ||
| 8 | + | ||
| 9 | +pub struct TestInputBackend; | ||
| 10 | + | ||
| 11 | +#[derive(PartialEq, Eq, Hash)] | ||
| 12 | +pub struct TestInputDevice; | ||
| 13 | + | ||
| 14 | +pub struct TestKeyboardKeyEvent { | ||
| 15 | + pub time: InputTime, | ||
| 16 | + pub code: Keycode, | ||
| 17 | + pub state: KeyState, | ||
| 18 | + pub count: u32, | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | +impl InputBackend for TestInputBackend { | ||
| 22 | + type Device = TestInputDevice; | ||
| 23 | + | ||
| 24 | + type KeyboardKeyEvent = TestKeyboardKeyEvent; | ||
| 25 | + type PointerAxisEvent = UnusedEvent; | ||
| 26 | + type PointerButtonEvent = UnusedEvent; | ||
| 27 | + type PointerMotionEvent = UnusedEvent; | ||
| 28 | + type PointerMotionAbsoluteEvent = UnusedEvent; | ||
| 29 | + | ||
| 30 | + type GestureSwipeBeginEvent = UnusedEvent; | ||
| 31 | + type GestureSwipeUpdateEvent = UnusedEvent; | ||
| 32 | + type GestureSwipeEndEvent = UnusedEvent; | ||
| 33 | + type GesturePinchBeginEvent = UnusedEvent; | ||
| 34 | + type GesturePinchUpdateEvent = UnusedEvent; | ||
| 35 | + type GesturePinchEndEvent = UnusedEvent; | ||
| 36 | + type GestureHoldBeginEvent = UnusedEvent; | ||
| 37 | + type GestureHoldEndEvent = UnusedEvent; | ||
| 38 | + | ||
| 39 | + type TouchDownEvent = UnusedEvent; | ||
| 40 | + type TouchUpEvent = UnusedEvent; | ||
| 41 | + type TouchMotionEvent = UnusedEvent; | ||
| 42 | + type TouchCancelEvent = UnusedEvent; | ||
| 43 | + type TouchFrameEvent = UnusedEvent; | ||
| 44 | + type TabletToolAxisEvent = UnusedEvent; | ||
| 45 | + type TabletToolProximityEvent = UnusedEvent; | ||
| 46 | + type TabletToolTipEvent = UnusedEvent; | ||
| 47 | + type TabletToolButtonEvent = UnusedEvent; | ||
| 48 | + | ||
| 49 | + type SwitchToggleEvent = UnusedEvent; | ||
| 50 | + | ||
| 51 | + type SpecialEvent = UnusedEvent; | ||
| 52 | +} | ||
| 53 | + | ||
| 54 | +impl Device for TestInputDevice { | ||
| 55 | + fn id(&self) -> String { | ||
| 56 | + String::from("test") | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + fn name(&self) -> String { | ||
| 60 | + String::from("test input device") | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + fn has_capability(&self, capability: DeviceCapability) -> bool { | ||
| 64 | + matches!(capability, DeviceCapability::Keyboard) | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + fn usb_id(&self) -> Option<(u32, u32)> { | ||
| 68 | + None | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + fn syspath(&self) -> Option<std::path::PathBuf> { | ||
| 72 | + None | ||
| 73 | + } | ||
| 74 | +} | ||
| 75 | + | ||
| 76 | +impl NiriInputDevice for TestInputDevice { | ||
| 77 | + fn output(&self, _state: &crate::niri::State) -> Option<Output> { | ||
| 78 | + None | ||
| 79 | + } | ||
| 80 | +} | ||
| 81 | + | ||
| 82 | +impl Event<TestInputBackend> for TestKeyboardKeyEvent { | ||
| 83 | + fn time(&self) -> InputTime { | ||
| 84 | + self.time | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + fn device(&self) -> <TestInputBackend as InputBackend>::Device { | ||
| 88 | + TestInputDevice | ||
| 89 | + } | ||
| 90 | +} | ||
| 91 | + | ||
| 92 | +impl KeyboardKeyEvent<TestInputBackend> for TestKeyboardKeyEvent { | ||
| 93 | + fn key_code(&self) -> Keycode { | ||
| 94 | + self.code | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + fn state(&self) -> KeyState { | ||
| 98 | + self.state | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + fn count(&self) -> u32 { | ||
| 102 | + self.count | ||
| 103 | + } | ||
| 104 | +} | ||