use smithay::{ backend::input::{ AbsolutePositionEvent, Axis, AxisSource, ButtonState, Event, GestureBeginEvent, GestureEndEvent, GesturePinchUpdateEvent, GestureSwipeUpdateEvent, InputBackend, InputEvent, KeyboardKeyEvent, PointerAxisEvent, PointerButtonEvent, PointerMotionEvent, TouchEvent, }, input::{ keyboard::{FilterResult, KeyboardTarget}, pointer::{AxisFrame, ButtonEvent, MotionEvent}, }, utils::{Logical, Point, SERIAL_COUNTER}, }; use crate::state::WeftCompositorState; pub fn process_input_event(state: &mut WeftCompositorState, event: InputEvent) { match event { InputEvent::Keyboard { event } => handle_keyboard::(state, event), InputEvent::PointerMotion { event } => handle_pointer_motion::(state, event), InputEvent::PointerMotionAbsolute { event } => { handle_pointer_motion_absolute::(state, event) } InputEvent::PointerButton { event } => handle_pointer_button::(state, event), InputEvent::PointerAxis { event } => handle_pointer_axis::(state, event), InputEvent::TouchDown { event } => handle_touch_down::(state, event), InputEvent::TouchUp { event } => handle_touch_up::(state, event), InputEvent::TouchMotion { event } => handle_touch_motion::(state, event), InputEvent::TouchFrame { event } => handle_touch_frame::(state, event), InputEvent::TouchCancel { event } => handle_touch_cancel::(state, event), InputEvent::GestureSwipeBegin { event } => handle_gesture_swipe_begin::(state, event), InputEvent::GestureSwipeUpdate { event } => handle_gesture_swipe_update::(state, event), InputEvent::GestureSwipeEnd { event } => handle_gesture_swipe_end::(state, event), InputEvent::GesturePinchBegin { event } => handle_gesture_pinch_begin::(state, event), InputEvent::GesturePinchUpdate { event } => handle_gesture_pinch_update::(state, event), InputEvent::GesturePinchEnd { event } => handle_gesture_pinch_end::(state, event), InputEvent::GestureHoldBegin { event } => handle_gesture_hold_begin::(state, event), InputEvent::GestureHoldEnd { event } => handle_gesture_hold_end::(state, event), // Device added/removed events are handled at the backend level. InputEvent::DeviceAdded { .. } | InputEvent::DeviceRemoved { .. } => {} _ => {} } } /// What the compositor does with a key instead of forwarding it. enum Shortcut { /// Alt+F4: close the focused application window. Close, /// A Super key tap: show the shell. Shell, /// Nothing: a suppressed key. None, } fn handle_keyboard(state: &mut WeftCompositorState, event: B::KeyboardKeyEvent) { use smithay::backend::input::KeyState; use smithay::input::keyboard::keysyms; let serial = SERIAL_COUNTER.next_serial(); let time = event.time_msec(); let key_state = event.state(); let Some(keyboard) = state.seat.get_keyboard() else { return; }; // Alt+F4 and the Super keys belong to the compositor. Alt+F4 asks the // focused application window to close. Super's key events are never // delivered to an application: tapped alone, without another key or a // button, it brings the shell's panel to the front with keyboard focus // and is then delivered to the shell, which opens or closes its // launcher. Held, it still sets the Super modifier for other keys. let (filtered, mods_changed) = keyboard.input_intercept::<_, _>( state, event.key_code(), key_state, |state, mods, keysym| { // A release is taken only when its press was, so no client // sees half of a key. let code = keysym.raw_code().raw(); let sym = keysym.modified_sym(); if key_state == KeyState::Pressed { if mods.alt && sym == keysyms::KEY_F4.into() { state.super_tap = None; state.suppressed_keys.push(code); return FilterResult::Intercept(Shortcut::Close); } if sym == keysyms::KEY_Super_L.into() || sym == keysyms::KEY_Super_R.into() { state.super_tap = (!mods.ctrl && !mods.alt && !mods.shift).then_some(code); state.suppressed_keys.push(code); return FilterResult::Intercept(Shortcut::None); } state.super_tap = None; } else if let Some(i) = state.suppressed_keys.iter().position(|&k| k == code) { state.suppressed_keys.remove(i); if state.super_tap.take() == Some(code) { return FilterResult::Intercept(Shortcut::Shell); } return FilterResult::Intercept(Shortcut::None); } FilterResult::Forward }, ); let shortcut = match filtered { FilterResult::Forward => { keyboard.input_forward( state, event.key_code(), key_state, serial, time, mods_changed, ); return; } FilterResult::Intercept(shortcut) => shortcut, }; // The focused client follows the modifiers even when the key that // changed them is taken, so none stays held there. if mods_changed && let Some(focus) = keyboard.current_focus() { let seat = state.seat.clone(); focus.modifiers(&seat, state, keyboard.modifier_state(), serial); } match shortcut { Shortcut::Close => state.close_focused_window(), Shortcut::Shell => { if state.activate_panel() { // The tap itself, for the shell; the keyboard's own state // already saw it. let code = event.key_code(); keyboard.input_forward(state, code, KeyState::Pressed, serial, time, false); let release = SERIAL_COUNTER.next_serial(); keyboard.input_forward(state, code, KeyState::Released, release, time, false); } } Shortcut::None => {} } } fn handle_pointer_motion( state: &mut WeftCompositorState, event: B::PointerMotionEvent, ) { let delta = event.delta(); state.pointer_location += delta; clamp_pointer_to_output_space(state); let serial = SERIAL_COUNTER.next_serial(); let pointer_location = state.pointer_location; let under = surface_under(state, pointer_location); if let Some(pointer) = state.seat.get_pointer() { pointer.motion( state, under, &MotionEvent { location: pointer_location, serial, time: event.time_msec(), }, ); pointer.frame(state); } } fn handle_pointer_motion_absolute( state: &mut WeftCompositorState, event: B::PointerMotionAbsoluteEvent, ) { let output = state.space.outputs().next().cloned(); if let Some(output) = output { let output_geo = state.space.output_geometry(&output).unwrap_or_default(); let pos = event.position_transformed(output_geo.size); state.pointer_location = output_geo.loc.to_f64() + pos; } let serial = SERIAL_COUNTER.next_serial(); let pointer_location = state.pointer_location; let under = surface_under(state, pointer_location); if let Some(pointer) = state.seat.get_pointer() { pointer.motion( state, under, &MotionEvent { location: pointer_location, serial, time: event.time_msec(), }, ); pointer.frame(state); } } fn handle_pointer_button( state: &mut WeftCompositorState, event: B::PointerButtonEvent, ) { let serial = SERIAL_COUNTER.next_serial(); let button = event.button_code(); let button_state = event.state(); // On press: activate the window under the pointer, which raises an // application window and focuses it. A held Super key is then no // longer a tap. if button_state == ButtonState::Pressed { state.super_tap = None; let pointer_location = state.pointer_location; let window = state .space .element_under(pointer_location) .map(|(window, _)| window.clone()); if let Some(window) = window { state.activate_window(&window); } else if let Some((surface, _loc)) = surface_under(state, pointer_location) { if let Some(keyboard) = state.seat.get_keyboard() { keyboard.set_focus(state, Some(surface), serial); } } else if let Some(keyboard) = state.seat.get_keyboard() { keyboard.set_focus(state, None, serial); } } if let Some(pointer) = state.seat.get_pointer() { pointer.button( state, &ButtonEvent { button, state: button_state, serial, time: event.time_msec(), }, ); pointer.frame(state); } } fn handle_pointer_axis( state: &mut WeftCompositorState, event: B::PointerAxisEvent, ) { let horizontal = event.amount(Axis::Horizontal); let vertical = event.amount(Axis::Vertical); let h_discrete = event.amount_v120(Axis::Horizontal); let v_discrete = event.amount_v120(Axis::Vertical); let source = event.source(); let mut frame = AxisFrame::new(event.time_msec()).source(source); if let Some(v) = horizontal { if v != 0.0 { frame = frame.value(Axis::Horizontal, v); } if let Some(d) = h_discrete { frame = frame.v120(Axis::Horizontal, d as i32); } } if let Some(v) = vertical { if v != 0.0 { frame = frame.value(Axis::Vertical, v); } if let Some(d) = v_discrete { frame = frame.v120(Axis::Vertical, d as i32); } } if source == AxisSource::Finger { if event.amount(Axis::Horizontal).unwrap_or(0.0) == 0.0 { frame = frame.stop(Axis::Horizontal); } if event.amount(Axis::Vertical).unwrap_or(0.0) == 0.0 { frame = frame.stop(Axis::Vertical); } } if let Some(pointer) = state.seat.get_pointer() { pointer.axis(state, frame); pointer.frame(state); } } fn handle_touch_down(state: &mut WeftCompositorState, event: B::TouchDownEvent) { state.super_tap = None; let serial = SERIAL_COUNTER.next_serial(); let output = state.space.outputs().next().cloned(); if let Some(output) = output { let output_geo = state.space.output_geometry(&output).unwrap_or_default(); let pos = event.position_transformed(output_geo.size); let location = output_geo.loc.to_f64() + pos; let under = surface_under(state, location); if let Some(touch) = state.seat.get_touch() { touch.down( state, under, &smithay::input::touch::DownEvent { slot: event.slot(), location, serial, time: event.time_msec(), }, ); } } } fn handle_touch_up(state: &mut WeftCompositorState, event: B::TouchUpEvent) { let serial = SERIAL_COUNTER.next_serial(); if let Some(touch) = state.seat.get_touch() { touch.up( state, &smithay::input::touch::UpEvent { slot: event.slot(), serial, time: event.time_msec(), }, ); } } fn handle_touch_motion( state: &mut WeftCompositorState, event: B::TouchMotionEvent, ) { let output = state.space.outputs().next().cloned(); if let Some(output) = output { let output_geo = state.space.output_geometry(&output).unwrap_or_default(); let pos = event.position_transformed(output_geo.size); let location = output_geo.loc.to_f64() + pos; let under = surface_under(state, location); if let Some(touch) = state.seat.get_touch() { touch.motion( state, under, &smithay::input::touch::MotionEvent { slot: event.slot(), location, time: event.time_msec(), }, ); } } } fn handle_touch_frame( state: &mut WeftCompositorState, _event: B::TouchFrameEvent, ) { if let Some(touch) = state.seat.get_touch() { touch.frame(state); } } fn handle_touch_cancel( state: &mut WeftCompositorState, _event: B::TouchCancelEvent, ) { if let Some(touch) = state.seat.get_touch() { touch.cancel(state); } } const NAVIGATION_SWIPE_FINGERS: u32 = 3; const NAVIGATION_SWIPE_THRESHOLD: f64 = 100.0; fn handle_gesture_swipe_begin( state: &mut WeftCompositorState, event: B::GestureSwipeBeginEvent, ) { let serial = SERIAL_COUNTER.next_serial(); let fingers = event.fingers(); if let Some(pointer) = state.seat.get_pointer() { pointer.gesture_swipe_begin( state, &smithay::input::pointer::GestureSwipeBeginEvent { serial, time: event.time_msec(), fingers, }, ); } state.gesture_state.in_progress = true; state.gesture_state.fingers = fingers; state.gesture_state.dx = 0.0; state.gesture_state.dy = 0.0; } fn handle_gesture_swipe_update( state: &mut WeftCompositorState, event: B::GestureSwipeUpdateEvent, ) { let delta = event.delta(); if let Some(pointer) = state.seat.get_pointer() { pointer.gesture_swipe_update( state, &smithay::input::pointer::GestureSwipeUpdateEvent { time: event.time_msec(), delta, }, ); } if state.gesture_state.in_progress { state.gesture_state.dx += delta.x; state.gesture_state.dy += delta.y; } } fn handle_gesture_swipe_end( state: &mut WeftCompositorState, event: B::GestureSwipeEndEvent, ) { let serial = SERIAL_COUNTER.next_serial(); let cancelled = event.cancelled(); if let Some(pointer) = state.seat.get_pointer() { pointer.gesture_swipe_end( state, &smithay::input::pointer::GestureSwipeEndEvent { serial, time: event.time_msec(), cancelled, }, ); } let gs = std::mem::take(&mut state.gesture_state); if !cancelled && gs.in_progress && gs.fingers >= NAVIGATION_SWIPE_FINGERS && (gs.dx.abs() >= NAVIGATION_SWIPE_THRESHOLD || gs.dy.abs() >= NAVIGATION_SWIPE_THRESHOLD) { state .weft_shell_state .send_navigation_gesture_to_panels(0, gs.fingers, gs.dx, gs.dy); } } fn handle_gesture_pinch_begin( state: &mut WeftCompositorState, event: B::GesturePinchBeginEvent, ) { let serial = SERIAL_COUNTER.next_serial(); if let Some(pointer) = state.seat.get_pointer() { pointer.gesture_pinch_begin( state, &smithay::input::pointer::GesturePinchBeginEvent { serial, time: event.time_msec(), fingers: event.fingers(), }, ); } } fn handle_gesture_pinch_update( state: &mut WeftCompositorState, event: B::GesturePinchUpdateEvent, ) { if let Some(pointer) = state.seat.get_pointer() { pointer.gesture_pinch_update( state, &smithay::input::pointer::GesturePinchUpdateEvent { time: event.time_msec(), delta: event.delta(), scale: event.scale(), rotation: event.rotation(), }, ); } } fn handle_gesture_pinch_end( state: &mut WeftCompositorState, event: B::GesturePinchEndEvent, ) { let serial = SERIAL_COUNTER.next_serial(); if let Some(pointer) = state.seat.get_pointer() { pointer.gesture_pinch_end( state, &smithay::input::pointer::GesturePinchEndEvent { serial, time: event.time_msec(), cancelled: event.cancelled(), }, ); } } fn handle_gesture_hold_begin( state: &mut WeftCompositorState, event: B::GestureHoldBeginEvent, ) { let serial = SERIAL_COUNTER.next_serial(); if let Some(pointer) = state.seat.get_pointer() { pointer.gesture_hold_begin( state, &smithay::input::pointer::GestureHoldBeginEvent { serial, time: event.time_msec(), fingers: event.fingers(), }, ); } } fn handle_gesture_hold_end( state: &mut WeftCompositorState, event: B::GestureHoldEndEvent, ) { let serial = SERIAL_COUNTER.next_serial(); if let Some(pointer) = state.seat.get_pointer() { pointer.gesture_hold_end( state, &smithay::input::pointer::GestureHoldEndEvent { serial, time: event.time_msec(), cancelled: event.cancelled(), }, ); } } /// Returns the surface and its local coordinates under the given position. pub fn surface_under( state: &WeftCompositorState, point: Point, ) -> Option<( smithay::reexports::wayland_server::protocol::wl_surface::WlSurface, Point, )> { state.space.element_under(point).and_then(|(window, loc)| { window .surface_under( point - loc.to_f64(), smithay::desktop::WindowSurfaceType::ALL, ) .map(|(surface, surface_loc)| (surface, (loc.to_f64() + surface_loc.to_f64()))) }) } fn clamp_pointer_to_output_space(state: &mut WeftCompositorState) { let bbox = state .space .outputs() .filter_map(|o| state.space.output_geometry(o)) .fold( smithay::utils::Rectangle::::default(), |acc, r| acc.merge(r), ); if bbox.size.w > 0 && bbox.size.h > 0 { state.pointer_location.x = state .pointer_location .x .clamp(bbox.loc.x as f64, (bbox.loc.x + bbox.size.w - 1) as f64); state.pointer_location.y = state .pointer_location .y .clamp(bbox.loc.y as f64, (bbox.loc.y + bbox.size.h - 1) as f64); } }