forge
Explore
Log in

Don't you dare go Hollow

Implement `niri msg pick-window`

Browse files at this commit

16405b9b2b99edaf9388df6a7228ca07f110769dUnverifiedbbb651 🇮🇱 and Ivan MolodetskikhParent: 4719cc6
* feat: `niri msg pick-window`

* fixes

---------
niri-ipc/src/lib.rsmodified+4 -0
@@ -63,6 +63,8 @@ pub enum Request {
6363 FocusedOutput,
6464 /// Request information about the focused window.
6565 FocusedWindow,
66+ /// Request picking a window and get its information.
67+ PickWindow,
6668 /// Perform an action.
6769 Action(Action),
6870 /// Change output configuration temporarily.
@@ -129,6 +131,8 @@ pub enum Response {
129131 FocusedOutput(Option<Output>),
130132 /// Information about the focused window.
131133 FocusedWindow(Option<Window>),
134+ /// Information about the picked window.
135+ PickedWindow(Option<Window>),
132136 /// Output configuration change result.
133137 OutputConfigChanged(OutputConfigChanged),
134138 }
src/cli.rsmodified+2 -0
@@ -72,6 +72,8 @@ pub enum Msg {
7272 FocusedOutput,
7373 /// Print information about the focused window.
7474 FocusedWindow,
75+ /// Pick a window with the mouse and print information about it.
76+ PickWindow,
7577 /// Perform an action.
7678 Action {
7779 #[command(subcommand)]
src/input/mod.rsmodified+14 -1
@@ -48,6 +48,7 @@ use crate::utils::{center, get_monotonic_time, ResizeEdge};
4848
4949 pub mod backend_ext;
5050 pub mod move_grab;
51+pub mod pick_window_grab;
5152 pub mod resize_grab;
5253 pub mod scroll_tracker;
5354 pub mod spatial_movement_grab;
@@ -367,7 +368,6 @@ impl State {
367368 serial,
368369 time,
369370 |this, mods, keysym| {
370- let bindings = &this.niri.config.borrow().binds;
371371 let key_code = event.key_code();
372372 let modified = keysym.modified_sym();
373373 let raw = keysym.raw_latin_sym_or_raw_current_sym();
@@ -383,6 +383,19 @@ impl State {
383383 }
384384 }
385385
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;
386399 should_intercept_key(
387400 &mut this.niri.suppressed_keys,
388401 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<()> {
1919 Msg::Outputs => Request::Outputs,
2020 Msg::FocusedWindow => Request::FocusedWindow,
2121 Msg::FocusedOutput => Request::FocusedOutput,
22+ Msg::PickWindow => Request::PickWindow,
2223 Msg::Action { action } => Request::Action(action.clone()),
2324 Msg::Output { output, action } => Request::Output {
2425 output: output.clone(),
@@ -252,6 +253,23 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> {
252253 println!("No output is focused.");
253254 }
254255 }
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+ }
255273 Msg::Action { .. } => {
256274 let Response::Handled = response else {
257275 bail!("unexpected response: expected Handled, got {response:?}");
src/ipc/server.rsmodified+34 -0
@@ -18,12 +18,17 @@ use niri_config::OutputName;
1818 use niri_ipc::state::{EventStreamState, EventStreamStatePart as _};
1919 use niri_ipc::{Event, KeyboardLayouts, OutputConfigChanged, Reply, Request, Response, Workspace};
2020 use smithay::desktop::layer_map_for_output;
21+use smithay::input::pointer::{
22+ CursorIcon, CursorImageStatus, Focus, GrabStartData as PointerGrabStartData,
23+};
2124 use smithay::reexports::calloop::generic::Generic;
2225 use smithay::reexports::calloop::{Interest, LoopHandle, Mode, PostAction};
2326 use smithay::reexports::rustix::fs::unlink;
27+use smithay::utils::SERIAL_COUNTER;
2428 use smithay::wayland::shell::wlr_layer::{KeyboardInteractivity, Layer};
2529
2630 use crate::backend::IpcOutputMap;
31+use crate::input::pick_window_grab::PickWindowGrab;
2732 use crate::layout::workspace::WorkspaceId;
2833 use crate::niri::State;
2934 use crate::utils::{version, with_toplevel_role};
@@ -322,6 +327,35 @@ async fn process(ctx: &ClientCtx, request: Request) -> Reply {
322327 let window = windows.values().find(|win| win.is_focused).cloned();
323328 Response::FocusedWindow(window)
324329 }
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+ }
325359 Request::Action(action) => {
326360 let (tx, rx) = async_channel::bounded(1);
327361
src/niri.rsmodified+5 -0
@@ -159,6 +159,7 @@ use crate::utils::{
159159 center, center_f64, expand_home, get_monotonic_time, ipc_transform_to_smithay, logical_output,
160160 make_screenshot_path, output_matches_name, output_size, send_scale_transform, write_png_rgba8,
161161 };
162+use crate::window::mapped::MappedId;
162163 use crate::window::{InitialConfigureState, Mapped, ResolvedWindowRules, Unmapped, WindowRef};
163164
164165 const CLEAR_COLOR_LOCKED: [f32; 4] = [0.3, 0.1, 0.1, 1.];
@@ -356,6 +357,8 @@ pub struct Niri {
356357 pub hotkey_overlay: HotkeyOverlay,
357358 pub exit_confirm_dialog: Option<ExitConfirmDialog>,
358359
360+ pub pick_window: Option<async_channel::Sender<Option<MappedId>>>,
361+
359362 pub debug_draw_opaque_regions: bool,
360363 pub debug_draw_damage: bool,
361364
@@ -2172,6 +2175,8 @@ impl Niri {
21722175 hotkey_overlay,
21732176 exit_confirm_dialog,
21742177
2178+ pick_window: None,
2179+
21752180 debug_draw_opaque_regions: false,
21762181 debug_draw_damage: false,
21772182