diff --git a/docs/wiki/Configuration:-Input.md b/docs/wiki/Configuration:-Input.md index 62f70b5a..9af94dca 100644 --- a/docs/wiki/Configuration:-Input.md +++ b/docs/wiki/Configuration:-Input.md @@ -90,6 +90,7 @@ input { // off map-to-output "eDP-1" // map-to-focused-output + // map-to-focused-window // left-handed // calibration-matrix 1.0 0.0 0.0 0.0 1.0 0.0 } @@ -282,10 +283,16 @@ Valid output names are the same as the ones used for output configuration. Since: 0.1.7 When a tablet is not mapped to any output, it will map to the union of all connected outputs, without aspect ratio correction. -Setting specific to `tablet`: +Settings specific to `tablet`: - `map-to-focused-output`: Since: 26.04 will map the tablet to the focused output, takes precedence over `map-to-output`. +- `map-to-focused-window`: Since: next release will map the tablet to the focused window's geometry, takes precedence over `map-to-focused-output` and `map-to-output`. +Falls back to those when no window is focused (for example, in the overview). + + When the tablet is also mapped to a specific output via `map-to-output`, the `map-to-focused-window` flag will map the tablet to the active window on that output. + If the tablet isn't mapped to any specific output, it will map the tablet to the current focused window regardless of where it is. + ### General Settings These settings are not specific to a particular input device. diff --git a/niri-config/src/input.rs b/niri-config/src/input.rs index dceb5c6c..5a2eb136 100644 --- a/niri-config/src/input.rs +++ b/niri-config/src/input.rs @@ -366,6 +366,8 @@ pub struct Tablet { #[knuffel(child)] pub map_to_focused_output: bool, #[knuffel(child)] + pub map_to_focused_window: bool, + #[knuffel(child)] pub left_handed: bool, } diff --git a/niri-config/src/lib.rs b/niri-config/src/lib.rs index 329b7669..909aeb80 100644 --- a/niri-config/src/lib.rs +++ b/niri-config/src/lib.rs @@ -720,6 +720,7 @@ mod tests { tablet { map-to-output "eDP-1" map-to-focused-output + map-to-focused-window calibration-matrix 1.0 2.0 3.0 \ 4.0 5.0 6.0 } @@ -1113,6 +1114,7 @@ mod tests { "eDP-1", ), map_to_focused_output: true, + map_to_focused_window: true, left_handed: false, }, touch: Touch { diff --git a/src/input/mod.rs b/src/input/mod.rs index 51f3520c..65a9ca61 100644 --- a/src/input/mod.rs +++ b/src/input/mod.rs @@ -297,26 +297,52 @@ impl State { let device_output = event.device().output(self); let device_output = device_output.filter(|output| self.niri.output_exists(output)); let device_output = device_output.as_ref(); - let (target_geo, keep_ratio, px, transform) = - if let Some(output) = device_output.or_else(|| self.niri.output_for_tablet()) { - let geo = self.niri.global_space.output_geometry(output).unwrap(); - ( - geo.to_f64(), - true, - 1. / output.current_scale().fractional_scale(), - output.current_transform(), - ) - } else { - let geo = self.global_bounding_rectangle()?.to_f64(); + let mapped_output = device_output.or_else(|| self.niri.output_for_tablet()); + + // If the tablet is configured to map to the focused window, use that window's geometry on + // the mapped output (or on the focused output if no specific output is mapped). + let map_to_focused_window = self.niri.config.borrow().input.tablet.map_to_focused_window; + // But only if the keyboard focus is on the layout, so that it doesn't trigger on the lock + // screen and such. + let window_target = if map_to_focused_window && self.niri.keyboard_focus.is_layout() { + let output = mapped_output.or_else(|| self.niri.layout.active_output()); + output.and_then(|output| { + let monitor = self.niri.layout.monitor_for_output(output)?; + let mut rect = monitor.active_window_visual_rectangle()?; + let output_geo = self.niri.global_space.output_geometry(output)?; + rect.loc += output_geo.loc.to_f64(); + Some((rect, output)) + }) + } else { + None + }; - // FIXME: this 1 px size should ideally somehow be computed for the rightmost output - // corresponding to the position on the right when clamping. - let output = self.niri.global_space.outputs().next().unwrap(); - let scale = output.current_scale().fractional_scale(); + let (target_geo, keep_ratio, px, transform) = if let Some((rect, output)) = window_target { + ( + rect, + true, + 1. / output.current_scale().fractional_scale(), + output.current_transform(), + ) + } else if let Some(output) = mapped_output { + let geo = self.niri.global_space.output_geometry(output).unwrap(); + ( + geo.to_f64(), + true, + 1. / output.current_scale().fractional_scale(), + output.current_transform(), + ) + } else { + let geo = self.global_bounding_rectangle()?.to_f64(); - // Do not keep ratio for the unified mode as this is what OpenTabletDriver expects. - (geo, false, 1. / scale, Transform::Normal) - }; + // FIXME: this 1 px size should ideally somehow be computed for the rightmost output + // corresponding to the position on the right when clamping. + let output = self.niri.global_space.outputs().next().unwrap(); + let scale = output.current_scale().fractional_scale(); + + // Do not keep ratio for the unified mode as this is what OpenTabletDriver expects. + (geo, false, 1. / scale, Transform::Normal) + }; let mut pos = { let size = transform.invert().transform_size(target_geo.size);