Implement `niri msg pick-window`
Browse files at this commit7 changed files +244 -1
- niri-ipc/src/lib.rs +4 -0
- src/cli.rs +2 -0
- src/input/mod.rs +14 -1
- src/input/pick_window_grab.rs +167 -0
- src/ipc/client.rs +18 -0
- src/ipc/server.rs +34 -0
- src/niri.rs +5 -0
niri-ipc/src/lib.rsmodified+4 -0
| @@ -63,6 +63,8 @@ pub enum Request { | |||
| 63 | FocusedOutput, | 63 | FocusedOutput, |
| 64 | /// Request information about the focused window. | 64 | /// Request information about the focused window. |
| 65 | FocusedWindow, | 65 | FocusedWindow, |
| 66 | + /// Request picking a window and get its information. | ||
| 67 | + PickWindow, | ||
| 66 | /// Perform an action. | 68 | /// Perform an action. |
| 67 | Action(Action), | 69 | Action(Action), |
| 68 | /// Change output configuration temporarily. | 70 | /// Change output configuration temporarily. |
| @@ -129,6 +131,8 @@ pub enum Response { | |||
| 129 | FocusedOutput(Option<Output>), | 131 | FocusedOutput(Option<Output>), |
| 130 | /// Information about the focused window. | 132 | /// Information about the focused window. |
| 131 | FocusedWindow(Option<Window>), | 133 | FocusedWindow(Option<Window>), |
| 134 | + /// Information about the picked window. | ||
| 135 | + PickedWindow(Option<Window>), | ||
| 132 | /// Output configuration change result. | 136 | /// Output configuration change result. |
| 133 | OutputConfigChanged(OutputConfigChanged), | 137 | OutputConfigChanged(OutputConfigChanged), |
| 134 | } | 138 | } |
src/cli.rsmodified+2 -0
| @@ -72,6 +72,8 @@ pub enum Msg { | |||
| 72 | FocusedOutput, | 72 | FocusedOutput, |
| 73 | /// Print information about the focused window. | 73 | /// Print information about the focused window. |
| 74 | FocusedWindow, | 74 | FocusedWindow, |
| 75 | + /// Pick a window with the mouse and print information about it. | ||
| 76 | + PickWindow, | ||
| 75 | /// Perform an action. | 77 | /// Perform an action. |
| 76 | Action { | 78 | Action { |
| 77 | #[command(subcommand)] | 79 | #[command(subcommand)] |
src/input/mod.rsmodified+14 -1
| @@ -48,6 +48,7 @@ use crate::utils::{center, get_monotonic_time, ResizeEdge}; | |||
| 48 | 48 | ||
| 49 | pub mod backend_ext; | 49 | pub mod backend_ext; |
| 50 | pub mod move_grab; | 50 | pub mod move_grab; |
| 51 | +pub mod pick_window_grab; | ||
| 51 | pub mod resize_grab; | 52 | pub mod resize_grab; |
| 52 | pub mod scroll_tracker; | 53 | pub mod scroll_tracker; |
| 53 | pub mod spatial_movement_grab; | 54 | pub mod spatial_movement_grab; |
| @@ -367,7 +368,6 @@ impl State { | |||
| 367 | serial, | 368 | serial, |
| 368 | time, | 369 | time, |
| 369 | |this, mods, keysym| { | 370 | |this, mods, keysym| { |
| 370 | - let bindings = &this.niri.config.borrow().binds; | ||
| 371 | let key_code = event.key_code(); | 371 | let key_code = event.key_code(); |
| 372 | let modified = keysym.modified_sym(); | 372 | let modified = keysym.modified_sym(); |
| 373 | let raw = keysym.raw_latin_sym_or_raw_current_sym(); | 373 | let raw = keysym.raw_latin_sym_or_raw_current_sym(); |
| @@ -383,6 +383,19 @@ impl State { | |||
| 383 | } | 383 | } |
| 384 | } | 384 | } |
| 385 | 385 | ||
| 386 | + if pressed && raw == Some(Keysym::Escape) && this.niri.pick_window.is_some() { | ||
| 387 | + // We window picking state so the pick window grab must be active. | ||
| 388 | + // Unsetting it cancels window picking. | ||
| 389 | + this.niri | ||
| 390 | + .seat | ||
| 391 | + .get_pointer() | ||
| 392 | + .unwrap() | ||
| 393 | + .unset_grab(this, serial, time); | ||
| 394 | + this.niri.suppressed_keys.insert(key_code); | ||
| 395 | + return FilterResult::Intercept(None); | ||
| 396 | + } | ||
| 397 | + | ||
| 398 | + let bindings = &this.niri.config.borrow().binds; | ||
| 386 | should_intercept_key( | 399 | should_intercept_key( |
| 387 | &mut this.niri.suppressed_keys, | 400 | &mut this.niri.suppressed_keys, |
| 388 | bindings, | 401 | bindings, |
src/input/pick_window_grab.rsadded+167 -0
| @@ -0,0 +1,167 @@ | |||
| 1 | +use smithay::backend::input::ButtonState; | ||
| 2 | +use smithay::input::pointer::{ | ||
| 3 | + AxisFrame, ButtonEvent, CursorImageStatus, GestureHoldBeginEvent, GestureHoldEndEvent, | ||
| 4 | + GesturePinchBeginEvent, GesturePinchEndEvent, GesturePinchUpdateEvent, GestureSwipeBeginEvent, | ||
| 5 | + GestureSwipeEndEvent, GestureSwipeUpdateEvent, GrabStartData as PointerGrabStartData, | ||
| 6 | + MotionEvent, PointerGrab, PointerInnerHandle, RelativeMotionEvent, | ||
| 7 | +}; | ||
| 8 | +use smithay::input::SeatHandler; | ||
| 9 | +use smithay::utils::{Logical, Point}; | ||
| 10 | + | ||
| 11 | +use crate::niri::State; | ||
| 12 | +use crate::window::Mapped; | ||
| 13 | + | ||
| 14 | +pub struct PickWindowGrab { | ||
| 15 | + start_data: PointerGrabStartData<State>, | ||
| 16 | +} | ||
| 17 | + | ||
| 18 | +impl PickWindowGrab { | ||
| 19 | + pub fn new(start_data: PointerGrabStartData<State>) -> Self { | ||
| 20 | + Self { start_data } | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + fn on_ungrab(&mut self, state: &mut State) { | ||
| 24 | + if let Some(tx) = state.niri.pick_window.take() { | ||
| 25 | + let _ = tx.send_blocking(None); | ||
| 26 | + } | ||
| 27 | + state | ||
| 28 | + .niri | ||
| 29 | + .cursor_manager | ||
| 30 | + .set_cursor_image(CursorImageStatus::default_named()); | ||
| 31 | + // Redraw to update the cursor. | ||
| 32 | + state.niri.queue_redraw_all(); | ||
| 33 | + } | ||
| 34 | +} | ||
| 35 | + | ||
| 36 | +impl PointerGrab<State> for PickWindowGrab { | ||
| 37 | + fn motion( | ||
| 38 | + &mut self, | ||
| 39 | + data: &mut State, | ||
| 40 | + handle: &mut PointerInnerHandle<'_, State>, | ||
| 41 | + _focus: Option<(<State as SeatHandler>::PointerFocus, Point<f64, Logical>)>, | ||
| 42 | + event: &MotionEvent, | ||
| 43 | + ) { | ||
| 44 | + handle.motion(data, None, event); | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + fn relative_motion( | ||
| 48 | + &mut self, | ||
| 49 | + data: &mut State, | ||
| 50 | + handle: &mut PointerInnerHandle<'_, State>, | ||
| 51 | + _focus: Option<(<State as SeatHandler>::PointerFocus, Point<f64, Logical>)>, | ||
| 52 | + event: &RelativeMotionEvent, | ||
| 53 | + ) { | ||
| 54 | + handle.relative_motion(data, None, event); | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + fn button( | ||
| 58 | + &mut self, | ||
| 59 | + data: &mut State, | ||
| 60 | + handle: &mut PointerInnerHandle<'_, State>, | ||
| 61 | + event: &ButtonEvent, | ||
| 62 | + ) { | ||
| 63 | + if event.state == ButtonState::Pressed { | ||
| 64 | + if let Some(tx) = data.niri.pick_window.take() { | ||
| 65 | + let _ = tx.send_blocking( | ||
| 66 | + data.niri | ||
| 67 | + .window_under(handle.current_location()) | ||
| 68 | + .map(Mapped::id), | ||
| 69 | + ); | ||
| 70 | + } | ||
| 71 | + handle.unset_grab(self, data, event.serial, event.time, true); | ||
| 72 | + } | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + fn axis( | ||
| 76 | + &mut self, | ||
| 77 | + data: &mut State, | ||
| 78 | + handle: &mut PointerInnerHandle<'_, State>, | ||
| 79 | + details: AxisFrame, | ||
| 80 | + ) { | ||
| 81 | + handle.axis(data, details); | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + fn frame(&mut self, data: &mut State, handle: &mut PointerInnerHandle<'_, State>) { | ||
| 85 | + handle.frame(data); | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + fn gesture_swipe_begin( | ||
| 89 | + &mut self, | ||
| 90 | + data: &mut State, | ||
| 91 | + handle: &mut PointerInnerHandle<'_, State>, | ||
| 92 | + event: &GestureSwipeBeginEvent, | ||
| 93 | + ) { | ||
| 94 | + handle.gesture_swipe_begin(data, event); | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + fn gesture_swipe_update( | ||
| 98 | + &mut self, | ||
| 99 | + data: &mut State, | ||
| 100 | + handle: &mut PointerInnerHandle<'_, State>, | ||
| 101 | + event: &GestureSwipeUpdateEvent, | ||
| 102 | + ) { | ||
| 103 | + handle.gesture_swipe_update(data, event); | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + fn gesture_swipe_end( | ||
| 107 | + &mut self, | ||
| 108 | + data: &mut State, | ||
| 109 | + handle: &mut PointerInnerHandle<'_, State>, | ||
| 110 | + event: &GestureSwipeEndEvent, | ||
| 111 | + ) { | ||
| 112 | + handle.gesture_swipe_end(data, event); | ||
| 113 | + } | ||
| 114 | + | ||
| 115 | + fn gesture_pinch_begin( | ||
| 116 | + &mut self, | ||
| 117 | + data: &mut State, | ||
| 118 | + handle: &mut PointerInnerHandle<'_, State>, | ||
| 119 | + event: &GesturePinchBeginEvent, | ||
| 120 | + ) { | ||
| 121 | + handle.gesture_pinch_begin(data, event); | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + fn gesture_pinch_update( | ||
| 125 | + &mut self, | ||
| 126 | + data: &mut State, | ||
| 127 | + handle: &mut PointerInnerHandle<'_, State>, | ||
| 128 | + event: &GesturePinchUpdateEvent, | ||
| 129 | + ) { | ||
| 130 | + handle.gesture_pinch_update(data, event); | ||
| 131 | + } | ||
| 132 | + | ||
| 133 | + fn gesture_pinch_end( | ||
| 134 | + &mut self, | ||
| 135 | + data: &mut State, | ||
| 136 | + handle: &mut PointerInnerHandle<'_, State>, | ||
| 137 | + event: &GesturePinchEndEvent, | ||
| 138 | + ) { | ||
| 139 | + handle.gesture_pinch_end(data, event); | ||
| 140 | + } | ||
| 141 | + | ||
| 142 | + fn gesture_hold_begin( | ||
| 143 | + &mut self, | ||
| 144 | + data: &mut State, | ||
| 145 | + handle: &mut PointerInnerHandle<'_, State>, | ||
| 146 | + event: &GestureHoldBeginEvent, | ||
| 147 | + ) { | ||
| 148 | + handle.gesture_hold_begin(data, event); | ||
| 149 | + } | ||
| 150 | + | ||
| 151 | + fn gesture_hold_end( | ||
| 152 | + &mut self, | ||
| 153 | + data: &mut State, | ||
| 154 | + handle: &mut PointerInnerHandle<'_, State>, | ||
| 155 | + event: &GestureHoldEndEvent, | ||
| 156 | + ) { | ||
| 157 | + handle.gesture_hold_end(data, event); | ||
| 158 | + } | ||
| 159 | + | ||
| 160 | + fn start_data(&self) -> &PointerGrabStartData<State> { | ||
| 161 | + &self.start_data | ||
| 162 | + } | ||
| 163 | + | ||
| 164 | + fn unset(&mut self, data: &mut State) { | ||
| 165 | + self.on_ungrab(data); | ||
| 166 | + } | ||
| 167 | +} | ||
src/ipc/client.rsmodified+18 -0
| @@ -19,6 +19,7 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { | |||
| 19 | Msg::Outputs => Request::Outputs, | 19 | Msg::Outputs => Request::Outputs, |
| 20 | Msg::FocusedWindow => Request::FocusedWindow, | 20 | Msg::FocusedWindow => Request::FocusedWindow, |
| 21 | Msg::FocusedOutput => Request::FocusedOutput, | 21 | Msg::FocusedOutput => Request::FocusedOutput, |
| 22 | + Msg::PickWindow => Request::PickWindow, | ||
| 22 | Msg::Action { action } => Request::Action(action.clone()), | 23 | Msg::Action { action } => Request::Action(action.clone()), |
| 23 | Msg::Output { output, action } => Request::Output { | 24 | Msg::Output { output, action } => Request::Output { |
| 24 | output: output.clone(), | 25 | output: output.clone(), |
| @@ -252,6 +253,23 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { | |||
| 252 | println!("No output is focused."); | 253 | println!("No output is focused."); |
| 253 | } | 254 | } |
| 254 | } | 255 | } |
| 256 | + Msg::PickWindow => { | ||
| 257 | + let Response::PickedWindow(window) = response else { | ||
| 258 | + bail!("unexpected response: expected PickedWindow, got {response:?}"); | ||
| 259 | + }; | ||
| 260 | + | ||
| 261 | + if json { | ||
| 262 | + let window = serde_json::to_string(&window).context("error formatting response")?; | ||
| 263 | + println!("{window}"); | ||
| 264 | + return Ok(()); | ||
| 265 | + } | ||
| 266 | + | ||
| 267 | + if let Some(window) = window { | ||
| 268 | + print_window(&window); | ||
| 269 | + } else { | ||
| 270 | + println!("No window selected."); | ||
| 271 | + } | ||
| 272 | + } | ||
| 255 | Msg::Action { .. } => { | 273 | Msg::Action { .. } => { |
| 256 | let Response::Handled = response else { | 274 | let Response::Handled = response else { |
| 257 | bail!("unexpected response: expected Handled, got {response:?}"); | 275 | bail!("unexpected response: expected Handled, got {response:?}"); |
src/ipc/server.rsmodified+34 -0
| @@ -18,12 +18,17 @@ use niri_config::OutputName; | |||
| 18 | use niri_ipc::state::{EventStreamState, EventStreamStatePart as _}; | 18 | use niri_ipc::state::{EventStreamState, EventStreamStatePart as _}; |
| 19 | use niri_ipc::{Event, KeyboardLayouts, OutputConfigChanged, Reply, Request, Response, Workspace}; | 19 | use niri_ipc::{Event, KeyboardLayouts, OutputConfigChanged, Reply, Request, Response, Workspace}; |
| 20 | use smithay::desktop::layer_map_for_output; | 20 | use smithay::desktop::layer_map_for_output; |
| 21 | +use smithay::input::pointer::{ | ||
| 22 | + CursorIcon, CursorImageStatus, Focus, GrabStartData as PointerGrabStartData, | ||
| 23 | +}; | ||
| 21 | use smithay::reexports::calloop::generic::Generic; | 24 | use smithay::reexports::calloop::generic::Generic; |
| 22 | use smithay::reexports::calloop::{Interest, LoopHandle, Mode, PostAction}; | 25 | use smithay::reexports::calloop::{Interest, LoopHandle, Mode, PostAction}; |
| 23 | use smithay::reexports::rustix::fs::unlink; | 26 | use smithay::reexports::rustix::fs::unlink; |
| 27 | +use smithay::utils::SERIAL_COUNTER; | ||
| 24 | use smithay::wayland::shell::wlr_layer::{KeyboardInteractivity, Layer}; | 28 | use smithay::wayland::shell::wlr_layer::{KeyboardInteractivity, Layer}; |
| 25 | 29 | ||
| 26 | use crate::backend::IpcOutputMap; | 30 | use crate::backend::IpcOutputMap; |
| 31 | +use crate::input::pick_window_grab::PickWindowGrab; | ||
| 27 | use crate::layout::workspace::WorkspaceId; | 32 | use crate::layout::workspace::WorkspaceId; |
| 28 | use crate::niri::State; | 33 | use crate::niri::State; |
| 29 | use crate::utils::{version, with_toplevel_role}; | 34 | use crate::utils::{version, with_toplevel_role}; |
| @@ -322,6 +327,35 @@ async fn process(ctx: &ClientCtx, request: Request) -> Reply { | |||
| 322 | let window = windows.values().find(|win| win.is_focused).cloned(); | 327 | let window = windows.values().find(|win| win.is_focused).cloned(); |
| 323 | Response::FocusedWindow(window) | 328 | Response::FocusedWindow(window) |
| 324 | } | 329 | } |
| 330 | + Request::PickWindow => { | ||
| 331 | + let (tx, rx) = async_channel::bounded(1); | ||
| 332 | + ctx.event_loop.insert_idle(move |state| { | ||
| 333 | + let pointer = state.niri.seat.get_pointer().unwrap(); | ||
| 334 | + let start_data = PointerGrabStartData { | ||
| 335 | + focus: None, | ||
| 336 | + button: 0, | ||
| 337 | + location: pointer.current_location(), | ||
| 338 | + }; | ||
| 339 | + let grab = PickWindowGrab::new(start_data); | ||
| 340 | + // The `WindowPickGrab` ungrab handler will cancel the previous ongoing pick, if | ||
| 341 | + // any. | ||
| 342 | + pointer.set_grab(state, grab, SERIAL_COUNTER.next_serial(), Focus::Clear); | ||
| 343 | + state.niri.pick_window = Some(tx); | ||
| 344 | + state | ||
| 345 | + .niri | ||
| 346 | + .cursor_manager | ||
| 347 | + .set_cursor_image(CursorImageStatus::Named(CursorIcon::Crosshair)); | ||
| 348 | + // Redraw to update the cursor. | ||
| 349 | + state.niri.queue_redraw_all(); | ||
| 350 | + }); | ||
| 351 | + let result = rx.recv().await; | ||
| 352 | + let id = result.map_err(|_| String::from("error getting picked window info"))?; | ||
| 353 | + let window = id.and_then(|id| { | ||
| 354 | + let state = ctx.event_stream_state.borrow(); | ||
| 355 | + state.windows.windows.get(&id.get()).cloned() | ||
| 356 | + }); | ||
| 357 | + Response::PickedWindow(window) | ||
| 358 | + } | ||
| 325 | Request::Action(action) => { | 359 | Request::Action(action) => { |
| 326 | let (tx, rx) = async_channel::bounded(1); | 360 | let (tx, rx) = async_channel::bounded(1); |
| 327 | 361 | ||
src/niri.rsmodified+5 -0
| @@ -159,6 +159,7 @@ use crate::utils::{ | |||
| 159 | center, center_f64, expand_home, get_monotonic_time, ipc_transform_to_smithay, logical_output, | 159 | center, center_f64, expand_home, get_monotonic_time, ipc_transform_to_smithay, logical_output, |
| 160 | make_screenshot_path, output_matches_name, output_size, send_scale_transform, write_png_rgba8, | 160 | make_screenshot_path, output_matches_name, output_size, send_scale_transform, write_png_rgba8, |
| 161 | }; | 161 | }; |
| 162 | +use crate::window::mapped::MappedId; | ||
| 162 | use crate::window::{InitialConfigureState, Mapped, ResolvedWindowRules, Unmapped, WindowRef}; | 163 | use crate::window::{InitialConfigureState, Mapped, ResolvedWindowRules, Unmapped, WindowRef}; |
| 163 | 164 | ||
| 164 | const CLEAR_COLOR_LOCKED: [f32; 4] = [0.3, 0.1, 0.1, 1.]; | 165 | const CLEAR_COLOR_LOCKED: [f32; 4] = [0.3, 0.1, 0.1, 1.]; |
| @@ -356,6 +357,8 @@ pub struct Niri { | |||
| 356 | pub hotkey_overlay: HotkeyOverlay, | 357 | pub hotkey_overlay: HotkeyOverlay, |
| 357 | pub exit_confirm_dialog: Option<ExitConfirmDialog>, | 358 | pub exit_confirm_dialog: Option<ExitConfirmDialog>, |
| 358 | 359 | ||
| 360 | + pub pick_window: Option<async_channel::Sender<Option<MappedId>>>, | ||
| 361 | + | ||
| 359 | pub debug_draw_opaque_regions: bool, | 362 | pub debug_draw_opaque_regions: bool, |
| 360 | pub debug_draw_damage: bool, | 363 | pub debug_draw_damage: bool, |
| 361 | 364 | ||
| @@ -2172,6 +2175,8 @@ impl Niri { | |||
| 2172 | hotkey_overlay, | 2175 | hotkey_overlay, |
| 2173 | exit_confirm_dialog, | 2176 | exit_confirm_dialog, |
| 2174 | 2177 | ||
| 2178 | + pick_window: None, | ||
| 2179 | + | ||
| 2175 | debug_draw_opaque_regions: false, | 2180 | debug_draw_opaque_regions: false, |
| 2176 | debug_draw_damage: false, | 2181 | debug_draw_damage: false, |
| 2177 | 2182 | ||