Keep monitor aspect ratio and clamp to monitor for tablets
Browse files at this commit0e29e7f6ff800af0effdad19be4e63e7e9cd84a4Parent: 626c7202 changed files +86 -20
- src/input.rs +80 -20
- src/niri.rs +6 -0
src/input.rsmodified+80 -20
| @@ -1,3 +1,4 @@ | |||
| 1 | +use std::any::Any; | ||
| 1 | use std::collections::HashSet; | 2 | use std::collections::HashSet; |
| 2 | 3 | ||
| 3 | use smithay::backend::input::{ | 4 | use smithay::backend::input::{ |
| @@ -14,7 +15,8 @@ use smithay::input::pointer::{ | |||
| 14 | GesturePinchBeginEvent, GesturePinchEndEvent, GesturePinchUpdateEvent, GestureSwipeBeginEvent, | 15 | GesturePinchBeginEvent, GesturePinchEndEvent, GesturePinchUpdateEvent, GestureSwipeBeginEvent, |
| 15 | GestureSwipeEndEvent, GestureSwipeUpdateEvent, MotionEvent, RelativeMotionEvent, | 16 | GestureSwipeEndEvent, GestureSwipeUpdateEvent, MotionEvent, RelativeMotionEvent, |
| 16 | }; | 17 | }; |
| 17 | -use smithay::utils::SERIAL_COUNTER; | 18 | +use smithay::reexports::input; |
| 19 | +use smithay::utils::{Logical, Point, SERIAL_COUNTER}; | ||
| 18 | use smithay::wayland::tablet_manager::{TabletDescriptor, TabletSeatTrait}; | 20 | use smithay::wayland::tablet_manager::{TabletDescriptor, TabletSeatTrait}; |
| 19 | 21 | ||
| 20 | use crate::config::{Action, Binds, LayoutAction, Modifiers}; | 22 | use crate::config::{Action, Binds, LayoutAction, Modifiers}; |
| @@ -28,8 +30,16 @@ pub enum CompositorMod { | |||
| 28 | Alt, | 30 | Alt, |
| 29 | } | 31 | } |
| 30 | 32 | ||
| 33 | +#[derive(Debug, Clone, Copy, PartialEq)] | ||
| 34 | +pub struct TabletData { | ||
| 35 | + pub aspect_ratio: f64, | ||
| 36 | +} | ||
| 37 | + | ||
| 31 | impl State { | 38 | impl State { |
| 32 | - pub fn process_input_event<I: InputBackend>(&mut self, event: InputEvent<I>) { | 39 | + pub fn process_input_event<I: InputBackend>(&mut self, event: InputEvent<I>) |
| 40 | + where | ||
| 41 | + I::Device: 'static, // Needed for downcasting. | ||
| 42 | + { | ||
| 33 | let _span = tracy_client::span!("process_input_event"); | 43 | let _span = tracy_client::span!("process_input_event"); |
| 34 | 44 | ||
| 35 | // A bit of a hack, but animation end runs some logic (i.e. workspace clean-up) and it | 45 | // A bit of a hack, but animation end runs some logic (i.e. workspace clean-up) and it |
| @@ -582,14 +592,10 @@ impl State { | |||
| 582 | pointer.frame(self); | 592 | pointer.frame(self); |
| 583 | } | 593 | } |
| 584 | InputEvent::TabletToolAxis { event, .. } => { | 594 | InputEvent::TabletToolAxis { event, .. } => { |
| 585 | - let Some(output) = self.niri.output_for_tablet() else { | 595 | + let Some(pos) = self.compute_tablet_position(&event) else { |
| 586 | return; | 596 | return; |
| 587 | }; | 597 | }; |
| 588 | 598 | ||
| 589 | - let output_geo = self.niri.global_space.output_geometry(output).unwrap(); | ||
| 590 | - | ||
| 591 | - let pos = event.position_transformed(output_geo.size) + output_geo.loc.to_f64(); | ||
| 592 | - | ||
| 593 | let serial = SERIAL_COUNTER.next_serial(); | 599 | let serial = SERIAL_COUNTER.next_serial(); |
| 594 | 600 | ||
| 595 | let pointer = self.niri.seat.get_pointer().unwrap(); | 601 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| @@ -671,14 +677,10 @@ impl State { | |||
| 671 | } | 677 | } |
| 672 | } | 678 | } |
| 673 | InputEvent::TabletToolProximity { event, .. } => { | 679 | InputEvent::TabletToolProximity { event, .. } => { |
| 674 | - let Some(output) = self.niri.output_for_tablet() else { | 680 | + let Some(pos) = self.compute_tablet_position(&event) else { |
| 675 | return; | 681 | return; |
| 676 | }; | 682 | }; |
| 677 | 683 | ||
| 678 | - let output_geo = self.niri.global_space.output_geometry(output).unwrap(); | ||
| 679 | - | ||
| 680 | - let pos = event.position_transformed(output_geo.size) + output_geo.loc.to_f64(); | ||
| 681 | - | ||
| 682 | let serial = SERIAL_COUNTER.next_serial(); | 684 | let serial = SERIAL_COUNTER.next_serial(); |
| 683 | 685 | ||
| 684 | let pointer = self.niri.seat.get_pointer().unwrap(); | 686 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| @@ -927,17 +929,75 @@ impl State { | |||
| 927 | } | 929 | } |
| 928 | 930 | ||
| 929 | pub fn process_libinput_event(&mut self, event: &mut InputEvent<LibinputInputBackend>) { | 931 | pub fn process_libinput_event(&mut self, event: &mut InputEvent<LibinputInputBackend>) { |
| 930 | - if let InputEvent::DeviceAdded { device } = event { | 932 | + match event { |
| 931 | - // According to Mutter code, this setting is specific to touchpads. | 933 | + InputEvent::DeviceAdded { device } => { |
| 932 | - let is_touchpad = device.config_tap_finger_count() > 0; | 934 | + // According to Mutter code, this setting is specific to touchpads. |
| 933 | - if is_touchpad { | 935 | + let is_touchpad = device.config_tap_finger_count() > 0; |
| 934 | - let c = &self.niri.config.borrow().input.touchpad; | 936 | + if is_touchpad { |
| 935 | - let _ = device.config_tap_set_enabled(c.tap); | 937 | + let c = &self.niri.config.borrow().input.touchpad; |
| 936 | - let _ = device.config_scroll_set_natural_scroll_enabled(c.natural_scroll); | 938 | + let _ = device.config_tap_set_enabled(c.tap); |
| 937 | - let _ = device.config_accel_set_speed(c.accel_speed); | 939 | + let _ = device.config_scroll_set_natural_scroll_enabled(c.natural_scroll); |
| 940 | + let _ = device.config_accel_set_speed(c.accel_speed); | ||
| 941 | + } | ||
| 942 | + | ||
| 943 | + if device.has_capability(smithay::reexports::input::DeviceCapability::TabletTool) { | ||
| 944 | + match device.size() { | ||
| 945 | + Some((w, h)) => { | ||
| 946 | + self.niri.tablets.insert( | ||
| 947 | + device.clone(), | ||
| 948 | + TabletData { | ||
| 949 | + aspect_ratio: w / h, | ||
| 950 | + }, | ||
| 951 | + ); | ||
| 952 | + } | ||
| 953 | + None => { | ||
| 954 | + warn!("tablet tool device has no size"); | ||
| 955 | + } | ||
| 956 | + } | ||
| 957 | + } | ||
| 958 | + } | ||
| 959 | + InputEvent::DeviceRemoved { device } => { | ||
| 960 | + self.niri.tablets.remove(device); | ||
| 938 | } | 961 | } |
| 962 | + _ => (), | ||
| 939 | } | 963 | } |
| 940 | } | 964 | } |
| 965 | + | ||
| 966 | + fn compute_tablet_position<I: InputBackend>( | ||
| 967 | + &mut self, | ||
| 968 | + event: &(impl Event<I> + TabletToolEvent<I>), | ||
| 969 | + ) -> Option<Point<f64, Logical>> | ||
| 970 | + where | ||
| 971 | + I::Device: 'static, | ||
| 972 | + { | ||
| 973 | + let output = self.niri.output_for_tablet()?; | ||
| 974 | + let output_geo = self.niri.global_space.output_geometry(output).unwrap(); | ||
| 975 | + | ||
| 976 | + let mut pos = event.position_transformed(output_geo.size); | ||
| 977 | + pos.x /= output_geo.size.w as f64; | ||
| 978 | + pos.y /= output_geo.size.h as f64; | ||
| 979 | + | ||
| 980 | + let device = event.device(); | ||
| 981 | + if let Some(device) = (&device as &dyn Any).downcast_ref::<input::Device>() { | ||
| 982 | + if let Some(data) = self.niri.tablets.get(device) { | ||
| 983 | + // This code does the same thing as mutter with "keep aspect ratio" enabled. | ||
| 984 | + let output_aspect_ratio = output_geo.size.w as f64 / output_geo.size.h as f64; | ||
| 985 | + let ratio = data.aspect_ratio / output_aspect_ratio; | ||
| 986 | + | ||
| 987 | + if ratio > 1. { | ||
| 988 | + pos.x *= ratio; | ||
| 989 | + } else { | ||
| 990 | + pos.y /= ratio; | ||
| 991 | + } | ||
| 992 | + } | ||
| 993 | + }; | ||
| 994 | + | ||
| 995 | + pos.x *= output_geo.size.w as f64; | ||
| 996 | + pos.y *= output_geo.size.h as f64; | ||
| 997 | + pos.x = pos.x.clamp(0.0, output_geo.size.w as f64 - 1.); | ||
| 998 | + pos.y = pos.y.clamp(0.0, output_geo.size.h as f64 - 1.); | ||
| 999 | + Some(pos + output_geo.loc.to_f64()) | ||
| 1000 | + } | ||
| 941 | } | 1001 | } |
| 942 | 1002 | ||
| 943 | /// Check whether the key should be intercepted and mark intercepted | 1003 | /// Check whether the key should be intercepted and mark intercepted |
src/niri.rsmodified+6 -0
| @@ -41,6 +41,7 @@ use smithay::reexports::calloop::timer::{TimeoutAction, Timer}; | |||
| 41 | use smithay::reexports::calloop::{ | 41 | use smithay::reexports::calloop::{ |
| 42 | self, Idle, Interest, LoopHandle, LoopSignal, Mode, PostAction, RegistrationToken, | 42 | self, Idle, Interest, LoopHandle, LoopSignal, Mode, PostAction, RegistrationToken, |
| 43 | }; | 43 | }; |
| 44 | +use smithay::reexports::input; | ||
| 44 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel::WmCapabilities; | 45 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel::WmCapabilities; |
| 45 | use smithay::reexports::wayland_protocols_misc::server_decoration as _server_decoration; | 46 | use smithay::reexports::wayland_protocols_misc::server_decoration as _server_decoration; |
| 46 | use smithay::reexports::wayland_server::backend::{ | 47 | use smithay::reexports::wayland_server::backend::{ |
| @@ -86,6 +87,7 @@ use crate::dbus::gnome_shell_screenshot::{NiriToScreenshot, ScreenshotToNiri}; | |||
| 86 | use crate::dbus::mutter_screen_cast::{self, ScreenCastToNiri}; | 87 | use crate::dbus::mutter_screen_cast::{self, ScreenCastToNiri}; |
| 87 | use crate::frame_clock::FrameClock; | 88 | use crate::frame_clock::FrameClock; |
| 88 | use crate::handlers::configure_lock_surface; | 89 | use crate::handlers::configure_lock_surface; |
| 90 | +use crate::input::TabletData; | ||
| 89 | use crate::layout::{output_size, Layout, MonitorRenderElement}; | 91 | use crate::layout::{output_size, Layout, MonitorRenderElement}; |
| 90 | use crate::pw_utils::{Cast, PipeWire}; | 92 | use crate::pw_utils::{Cast, PipeWire}; |
| 91 | use crate::screenshot_ui::{ScreenshotUi, ScreenshotUiRenderElement}; | 93 | use crate::screenshot_ui::{ScreenshotUi, ScreenshotUiRenderElement}; |
| @@ -121,6 +123,8 @@ pub struct Niri { | |||
| 121 | // When false, we're idling with monitors powered off. | 123 | // When false, we're idling with monitors powered off. |
| 122 | pub monitors_active: bool, | 124 | pub monitors_active: bool, |
| 123 | 125 | ||
| 126 | + pub tablets: HashMap<input::Device, TabletData>, | ||
| 127 | + | ||
| 124 | // Smithay state. | 128 | // Smithay state. |
| 125 | pub compositor_state: CompositorState, | 129 | pub compositor_state: CompositorState, |
| 126 | pub xdg_shell_state: XdgShellState, | 130 | pub xdg_shell_state: XdgShellState, |
| @@ -694,6 +698,8 @@ impl Niri { | |||
| 694 | unmapped_windows: HashMap::new(), | 698 | unmapped_windows: HashMap::new(), |
| 695 | monitors_active: true, | 699 | monitors_active: true, |
| 696 | 700 | ||
| 701 | + tablets: HashMap::new(), | ||
| 702 | + | ||
| 697 | compositor_state, | 703 | compositor_state, |
| 698 | xdg_shell_state, | 704 | xdg_shell_state, |
| 699 | xdg_decoration_state, | 705 | xdg_decoration_state, |