niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·085cd14·Ivan Molodetskikh·3 years ago·78 commits
| 1 | use std::process::Command; |
| 2 | |
| 3 | use smithay::backend::input::{ |
| 4 | AbsolutePositionEvent, Axis, AxisSource, ButtonState, Event, InputBackend, InputEvent, |
| 5 | KeyState, KeyboardKeyEvent, PointerAxisEvent, PointerButtonEvent, PointerMotionEvent, |
| 6 | TabletToolEvent, TabletToolTipEvent, TabletToolTipState, |
| 7 | }; |
| 8 | use smithay::backend::libinput::LibinputInputBackend; |
| 9 | use smithay::input::keyboard::{keysyms, FilterResult, KeysymHandle, ModifiersState}; |
| 10 | use smithay::input::pointer::{AxisFrame, ButtonEvent, MotionEvent, RelativeMotionEvent}; |
| 11 | use smithay::utils::SERIAL_COUNTER; |
| 12 | |
| 13 | use crate::niri::Niri; |
| 14 | use crate::utils::get_monotonic_time; |
| 15 | |
| 16 | enum Action { |
| 17 | None, |
| 18 | Quit, |
| 19 | ChangeVt(i32), |
| 20 | Spawn(String), |
| 21 | CloseWindow, |
| 22 | ToggleFullscreen, |
| 23 | FocusLeft, |
| 24 | FocusRight, |
| 25 | FocusDown, |
| 26 | FocusUp, |
| 27 | MoveLeft, |
| 28 | MoveRight, |
| 29 | MoveDown, |
| 30 | MoveUp, |
| 31 | ConsumeIntoColumn, |
| 32 | ExpelFromColumn, |
| 33 | SwitchWorkspaceDown, |
| 34 | SwitchWorkspaceUp, |
| 35 | MoveToWorkspaceDown, |
| 36 | MoveToWorkspaceUp, |
| 37 | ToggleWidth, |
| 38 | ToggleFullWidth, |
| 39 | } |
| 40 | |
| 41 | pub enum CompositorMod { |
| 42 | Super, |
| 43 | Alt, |
| 44 | } |
| 45 | |
| 46 | impl From<Action> for FilterResult<Action> { |
| 47 | fn from(value: Action) -> Self { |
| 48 | match value { |
| 49 | Action::None => FilterResult::Forward, |
| 50 | action => FilterResult::Intercept(action), |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | fn action(comp_mod: CompositorMod, keysym: KeysymHandle, mods: ModifiersState) -> Action { |
| 56 | use keysyms::*; |
| 57 | |
| 58 | let modified = keysym.modified_sym(); |
| 59 | if matches!(modified, KEY_XF86Switch_VT_1..=KEY_XF86Switch_VT_12) { |
| 60 | let vt = (modified - KEY_XF86Switch_VT_1 + 1) as i32; |
| 61 | return Action::ChangeVt(vt); |
| 62 | } |
| 63 | |
| 64 | let mod_down = match comp_mod { |
| 65 | CompositorMod::Super => mods.logo, |
| 66 | CompositorMod::Alt => mods.alt, |
| 67 | }; |
| 68 | |
| 69 | if !mod_down { |
| 70 | return Action::None; |
| 71 | } |
| 72 | |
| 73 | // FIXME: these don't work in the Russian layout. I guess I'll need to |
| 74 | // find a US keymap, then map keys somehow. |
| 75 | #[allow(non_upper_case_globals)] // wat |
| 76 | match modified { |
| 77 | KEY_E => Action::Quit, |
| 78 | KEY_t => Action::Spawn("alacritty".to_owned()), |
| 79 | KEY_d => Action::Spawn("fuzzel".to_owned()), |
| 80 | KEY_n => Action::Spawn("nautilus".to_owned()), |
| 81 | KEY_q => Action::CloseWindow, |
| 82 | KEY_F => Action::ToggleFullscreen, |
| 83 | KEY_h | KEY_Left if mods.ctrl => Action::MoveLeft, |
| 84 | KEY_l | KEY_Right if mods.ctrl => Action::MoveRight, |
| 85 | KEY_j | KEY_Down if mods.ctrl => Action::MoveDown, |
| 86 | KEY_k | KEY_Up if mods.ctrl => Action::MoveUp, |
| 87 | KEY_h | KEY_Left => Action::FocusLeft, |
| 88 | KEY_l | KEY_Right => Action::FocusRight, |
| 89 | KEY_j | KEY_Down => Action::FocusDown, |
| 90 | KEY_k | KEY_Up => Action::FocusUp, |
| 91 | KEY_u if mods.ctrl => Action::MoveToWorkspaceDown, |
| 92 | KEY_i if mods.ctrl => Action::MoveToWorkspaceUp, |
| 93 | KEY_u => Action::SwitchWorkspaceDown, |
| 94 | KEY_i => Action::SwitchWorkspaceUp, |
| 95 | KEY_comma => Action::ConsumeIntoColumn, |
| 96 | KEY_period => Action::ExpelFromColumn, |
| 97 | KEY_r => Action::ToggleWidth, |
| 98 | KEY_f => Action::ToggleFullWidth, |
| 99 | _ => Action::None, |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | impl Niri { |
| 104 | pub fn process_input_event<I: InputBackend>( |
| 105 | &mut self, |
| 106 | change_vt: &mut dyn FnMut(i32), |
| 107 | comp_mod: CompositorMod, |
| 108 | event: InputEvent<I>, |
| 109 | ) { |
| 110 | let _span = tracy_client::span!("process_input_event"); |
| 111 | trace!("process_input_event"); |
| 112 | |
| 113 | // A bit of a hack, but animation end runs some logic (i.e. workspace clean-up) and it |
| 114 | // doesn't always trigger due to damage, etc. So run it here right before it might prove |
| 115 | // important. Besides, animations affect the input, so it's best to have up-to-date values |
| 116 | // here. |
| 117 | self.monitor_set.advance_animations(get_monotonic_time()); |
| 118 | |
| 119 | match event { |
| 120 | InputEvent::Keyboard { event, .. } => { |
| 121 | let serial = SERIAL_COUNTER.next_serial(); |
| 122 | let time = Event::time_msec(&event); |
| 123 | |
| 124 | let action = self.seat.get_keyboard().unwrap().input( |
| 125 | self, |
| 126 | event.key_code(), |
| 127 | event.state(), |
| 128 | serial, |
| 129 | time, |
| 130 | |_, mods, keysym| { |
| 131 | if event.state() == KeyState::Pressed { |
| 132 | action(comp_mod, keysym, *mods).into() |
| 133 | } else { |
| 134 | FilterResult::Forward |
| 135 | } |
| 136 | }, |
| 137 | ); |
| 138 | |
| 139 | if let Some(action) = action { |
| 140 | match action { |
| 141 | Action::None => unreachable!(), |
| 142 | Action::Quit => { |
| 143 | info!("quitting because quit bind was pressed"); |
| 144 | self.stop_signal.stop() |
| 145 | } |
| 146 | Action::ChangeVt(vt) => { |
| 147 | (*change_vt)(vt); |
| 148 | } |
| 149 | Action::Spawn(command) => { |
| 150 | if let Err(err) = Command::new(command).spawn() { |
| 151 | warn!("error spawning alacritty: {err}"); |
| 152 | } |
| 153 | } |
| 154 | Action::CloseWindow => { |
| 155 | if let Some(window) = self.monitor_set.focus() { |
| 156 | window.toplevel().send_close(); |
| 157 | } |
| 158 | } |
| 159 | Action::ToggleFullscreen => { |
| 160 | if let Some(window) = self.monitor_set.focus() { |
| 161 | // FIXME |
| 162 | } |
| 163 | } |
| 164 | Action::MoveLeft => { |
| 165 | self.monitor_set.move_left(); |
| 166 | // FIXME: granular |
| 167 | self.queue_redraw_all(); |
| 168 | } |
| 169 | Action::MoveRight => { |
| 170 | self.monitor_set.move_right(); |
| 171 | // FIXME: granular |
| 172 | self.queue_redraw_all(); |
| 173 | } |
| 174 | Action::MoveDown => { |
| 175 | self.monitor_set.move_down(); |
| 176 | // FIXME: granular |
| 177 | self.queue_redraw_all(); |
| 178 | } |
| 179 | Action::MoveUp => { |
| 180 | self.monitor_set.move_up(); |
| 181 | // FIXME: granular |
| 182 | self.queue_redraw_all(); |
| 183 | } |
| 184 | Action::FocusLeft => { |
| 185 | self.monitor_set.focus_left(); |
| 186 | } |
| 187 | Action::FocusRight => { |
| 188 | self.monitor_set.focus_right(); |
| 189 | } |
| 190 | Action::FocusDown => { |
| 191 | self.monitor_set.focus_down(); |
| 192 | } |
| 193 | Action::FocusUp => { |
| 194 | self.monitor_set.focus_up(); |
| 195 | } |
| 196 | Action::MoveToWorkspaceDown => { |
| 197 | self.monitor_set.move_to_workspace_down(); |
| 198 | // FIXME: granular |
| 199 | self.queue_redraw_all(); |
| 200 | } |
| 201 | Action::MoveToWorkspaceUp => { |
| 202 | self.monitor_set.move_to_workspace_up(); |
| 203 | // FIXME: granular |
| 204 | self.queue_redraw_all(); |
| 205 | } |
| 206 | Action::SwitchWorkspaceDown => { |
| 207 | self.monitor_set.switch_workspace_down(); |
| 208 | // FIXME: granular |
| 209 | self.queue_redraw_all(); |
| 210 | } |
| 211 | Action::SwitchWorkspaceUp => { |
| 212 | self.monitor_set.switch_workspace_up(); |
| 213 | // FIXME: granular |
| 214 | self.queue_redraw_all(); |
| 215 | } |
| 216 | Action::ConsumeIntoColumn => { |
| 217 | self.monitor_set.consume_into_column(); |
| 218 | // FIXME: granular |
| 219 | self.queue_redraw_all(); |
| 220 | } |
| 221 | Action::ExpelFromColumn => { |
| 222 | self.monitor_set.expel_from_column(); |
| 223 | // FIXME: granular |
| 224 | self.queue_redraw_all(); |
| 225 | } |
| 226 | Action::ToggleWidth => { |
| 227 | self.monitor_set.toggle_width(); |
| 228 | } |
| 229 | Action::ToggleFullWidth => { |
| 230 | self.monitor_set.toggle_full_width(); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | InputEvent::PointerMotion { event, .. } => { |
| 236 | let serial = SERIAL_COUNTER.next_serial(); |
| 237 | |
| 238 | let pointer = self.seat.get_pointer().unwrap(); |
| 239 | let mut pos = pointer.current_location(); |
| 240 | |
| 241 | pos += event.delta(); |
| 242 | |
| 243 | let mut min_x = i32::MAX; |
| 244 | let mut min_y = i32::MAX; |
| 245 | let mut max_x = 0; |
| 246 | let mut max_y = 0; |
| 247 | for output in self.global_space.outputs() { |
| 248 | // FIXME: smarter clamping. |
| 249 | let geom = self.global_space.output_geometry(output).unwrap(); |
| 250 | min_x = min_x.min(geom.loc.x); |
| 251 | min_y = min_y.min(geom.loc.y); |
| 252 | max_x = max_x.max(geom.loc.x + geom.size.w); |
| 253 | max_y = max_y.max(geom.loc.y + geom.size.h); |
| 254 | } |
| 255 | |
| 256 | pos.x = pos.x.clamp(min_x as f64, max_x as f64); |
| 257 | pos.y = pos.y.clamp(min_y as f64, max_y as f64); |
| 258 | |
| 259 | let under = self.surface_under_and_global_space(pos); |
| 260 | |
| 261 | pointer.motion( |
| 262 | self, |
| 263 | under.clone(), |
| 264 | &MotionEvent { |
| 265 | location: pos, |
| 266 | serial, |
| 267 | time: event.time_msec(), |
| 268 | }, |
| 269 | ); |
| 270 | |
| 271 | pointer.relative_motion( |
| 272 | self, |
| 273 | under, |
| 274 | &RelativeMotionEvent { |
| 275 | delta: event.delta(), |
| 276 | delta_unaccel: event.delta_unaccel(), |
| 277 | utime: event.time(), |
| 278 | }, |
| 279 | ); |
| 280 | |
| 281 | // Redraw to update the cursor position. |
| 282 | // FIXME: redraw only outputs overlapping the cursor. |
| 283 | self.queue_redraw_all(); |
| 284 | } |
| 285 | InputEvent::PointerMotionAbsolute { event, .. } => { |
| 286 | let output = self.global_space.outputs().next().unwrap(); |
| 287 | |
| 288 | let output_geo = self.global_space.output_geometry(output).unwrap(); |
| 289 | |
| 290 | let pos = event.position_transformed(output_geo.size) + output_geo.loc.to_f64(); |
| 291 | |
| 292 | let serial = SERIAL_COUNTER.next_serial(); |
| 293 | |
| 294 | let pointer = self.seat.get_pointer().unwrap(); |
| 295 | |
| 296 | let under = self.surface_under_and_global_space(pos); |
| 297 | |
| 298 | pointer.motion( |
| 299 | self, |
| 300 | under, |
| 301 | &MotionEvent { |
| 302 | location: pos, |
| 303 | serial, |
| 304 | time: event.time_msec(), |
| 305 | }, |
| 306 | ); |
| 307 | |
| 308 | // Redraw to update the cursor position. |
| 309 | // FIXME: redraw only outputs overlapping the cursor. |
| 310 | self.queue_redraw_all(); |
| 311 | } |
| 312 | InputEvent::PointerButton { event, .. } => { |
| 313 | let pointer = self.seat.get_pointer().unwrap(); |
| 314 | |
| 315 | let serial = SERIAL_COUNTER.next_serial(); |
| 316 | |
| 317 | let button = event.button_code(); |
| 318 | |
| 319 | let button_state = event.state(); |
| 320 | |
| 321 | if ButtonState::Pressed == button_state && !pointer.is_grabbed() { |
| 322 | if let Some(window) = self.window_under_cursor() { |
| 323 | let window = window.clone(); |
| 324 | self.monitor_set.activate_window(&window); |
| 325 | } else { |
| 326 | let output = self.output_under_cursor().unwrap(); |
| 327 | self.monitor_set.activate_output(&output); |
| 328 | } |
| 329 | }; |
| 330 | |
| 331 | pointer.button( |
| 332 | self, |
| 333 | &ButtonEvent { |
| 334 | button, |
| 335 | state: button_state, |
| 336 | serial, |
| 337 | time: event.time_msec(), |
| 338 | }, |
| 339 | ); |
| 340 | } |
| 341 | InputEvent::PointerAxis { event, .. } => { |
| 342 | let source = event.source(); |
| 343 | |
| 344 | let horizontal_amount = event.amount(Axis::Horizontal).unwrap_or_else(|| { |
| 345 | event.amount_discrete(Axis::Horizontal).unwrap_or(0.0) * 3.0 |
| 346 | }); |
| 347 | let vertical_amount = event |
| 348 | .amount(Axis::Vertical) |
| 349 | .unwrap_or_else(|| event.amount_discrete(Axis::Vertical).unwrap_or(0.0) * 3.0); |
| 350 | let horizontal_amount_discrete = event.amount_discrete(Axis::Horizontal); |
| 351 | let vertical_amount_discrete = event.amount_discrete(Axis::Vertical); |
| 352 | |
| 353 | let mut frame = AxisFrame::new(event.time_msec()).source(source); |
| 354 | if horizontal_amount != 0.0 { |
| 355 | frame = frame.value(Axis::Horizontal, horizontal_amount); |
| 356 | if let Some(discrete) = horizontal_amount_discrete { |
| 357 | frame = frame.discrete(Axis::Horizontal, discrete as i32); |
| 358 | } |
| 359 | } else if source == AxisSource::Finger { |
| 360 | frame = frame.stop(Axis::Horizontal); |
| 361 | } |
| 362 | if vertical_amount != 0.0 { |
| 363 | frame = frame.value(Axis::Vertical, vertical_amount); |
| 364 | if let Some(discrete) = vertical_amount_discrete { |
| 365 | frame = frame.discrete(Axis::Vertical, discrete as i32); |
| 366 | } |
| 367 | } else if source == AxisSource::Finger { |
| 368 | frame = frame.stop(Axis::Vertical); |
| 369 | } |
| 370 | |
| 371 | self.seat.get_pointer().unwrap().axis(self, frame); |
| 372 | } |
| 373 | // FIXME: tablet_tool protocol |
| 374 | InputEvent::TabletToolAxis { event, .. } => { |
| 375 | // FIXME: allow mapping tablet to different outputs. |
| 376 | let output = self.global_space.outputs().next().unwrap(); |
| 377 | |
| 378 | let output_geo = self.global_space.output_geometry(output).unwrap(); |
| 379 | |
| 380 | let pos = event.position_transformed(output_geo.size) + output_geo.loc.to_f64(); |
| 381 | |
| 382 | let serial = SERIAL_COUNTER.next_serial(); |
| 383 | |
| 384 | let pointer = self.seat.get_pointer().unwrap(); |
| 385 | |
| 386 | let under = self.surface_under_and_global_space(pos); |
| 387 | |
| 388 | pointer.motion( |
| 389 | self, |
| 390 | under, |
| 391 | &MotionEvent { |
| 392 | location: pos, |
| 393 | serial, |
| 394 | time: event.time_msec(), |
| 395 | }, |
| 396 | ); |
| 397 | |
| 398 | // Redraw to update the cursor position. |
| 399 | // FIXME: redraw only outputs overlapping the cursor. |
| 400 | self.queue_redraw_all(); |
| 401 | } |
| 402 | InputEvent::TabletToolTip { event, .. } => { |
| 403 | let pointer = self.seat.get_pointer().unwrap(); |
| 404 | |
| 405 | if event.tip_state() == TabletToolTipState::Down && !pointer.is_grabbed() { |
| 406 | if let Some(window) = self.window_under_cursor() { |
| 407 | let window = window.clone(); |
| 408 | self.monitor_set.activate_window(&window); |
| 409 | } else { |
| 410 | let output = self.output_under_cursor().unwrap(); |
| 411 | self.monitor_set.activate_output(&output); |
| 412 | } |
| 413 | }; |
| 414 | } |
| 415 | _ => {} |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | pub fn process_libinput_event(&mut self, event: &mut InputEvent<LibinputInputBackend>) { |
| 420 | if let InputEvent::DeviceAdded { device } = event { |
| 421 | // According to Mutter code, this setting is specific to touchpads. |
| 422 | let is_touchpad = device.config_tap_finger_count() > 0; |
| 423 | if is_touchpad { |
| 424 | let _ = device.config_tap_set_enabled(true); |
| 425 | let _ = device.config_scroll_set_natural_scroll_enabled(true); |
| 426 | let _ = device.config_accel_set_speed(0.2); |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | } |