Implement niri msg focused-window
Browse files at this commit30911023654b1f1028eb41bade9b1627d3c8344aParent: a7b38195 changed files +77 -0
- niri-ipc/src/lib.rs +13 -0
- src/cli.rs +2 -0
- src/ipc/client.rs +30 -0
- src/ipc/server.rs +25 -0
- src/niri.rs +7 -0
niri-ipc/src/lib.rsmodified+13 -0
| @@ -14,6 +14,8 @@ pub const SOCKET_PATH_ENV: &str = "NIRI_SOCKET"; | |||
| 14 | pub enum Request { | 14 | pub enum Request { |
| 15 | /// Request information about connected outputs. | 15 | /// Request information about connected outputs. |
| 16 | Outputs, | 16 | Outputs, |
| 17 | + /// Request information about the focused window. | ||
| 18 | + FocusedWindow, | ||
| 17 | /// Perform an action. | 19 | /// Perform an action. |
| 18 | Action(Action), | 20 | Action(Action), |
| 19 | } | 21 | } |
| @@ -37,6 +39,8 @@ pub enum Response { | |||
| 37 | /// | 39 | /// |
| 38 | /// Map from connector name to output info. | 40 | /// Map from connector name to output info. |
| 39 | Outputs(HashMap<String, Output>), | 41 | Outputs(HashMap<String, Output>), |
| 42 | + /// Information about the focused window. | ||
| 43 | + FocusedWindow(Option<Window>), | ||
| 40 | } | 44 | } |
| 41 | 45 | ||
| 42 | /// Actions that niri can perform. | 46 | /// Actions that niri can perform. |
| @@ -308,6 +312,15 @@ pub enum Transform { | |||
| 308 | Flipped270, | 312 | Flipped270, |
| 309 | } | 313 | } |
| 310 | 314 | ||
| 315 | +/// Toplevel window. | ||
| 316 | +#[derive(Serialize, Deserialize, Debug, Clone)] | ||
| 317 | +pub struct Window { | ||
| 318 | + /// Title, if set. | ||
| 319 | + pub title: Option<String>, | ||
| 320 | + /// Application ID, if set. | ||
| 321 | + pub app_id: Option<String>, | ||
| 322 | +} | ||
| 323 | + | ||
| 311 | impl FromStr for SizeChange { | 324 | impl FromStr for SizeChange { |
| 312 | type Err = &'static str; | 325 | type Err = &'static str; |
| 313 | 326 | ||
src/cli.rsmodified+2 -0
| @@ -54,6 +54,8 @@ pub enum Sub { | |||
| 54 | pub enum Msg { | 54 | pub enum Msg { |
| 55 | /// List connected outputs. | 55 | /// List connected outputs. |
| 56 | Outputs, | 56 | Outputs, |
| 57 | + /// Print information about the focused window. | ||
| 58 | + FocusedWindow, | ||
| 57 | /// Perform an action. | 59 | /// Perform an action. |
| 58 | Action { | 60 | Action { |
| 59 | #[command(subcommand)] | 61 | #[command(subcommand)] |
src/ipc/client.rsmodified+30 -0
| @@ -21,6 +21,7 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { | |||
| 21 | 21 | ||
| 22 | let request = match &msg { | 22 | let request = match &msg { |
| 23 | Msg::Outputs => Request::Outputs, | 23 | Msg::Outputs => Request::Outputs, |
| 24 | + Msg::FocusedWindow => Request::FocusedWindow, | ||
| 24 | Msg::Action { action } => Request::Action(action.clone()), | 25 | Msg::Action { action } => Request::Action(action.clone()), |
| 25 | }; | 26 | }; |
| 26 | let mut buf = serde_json::to_vec(&request).unwrap(); | 27 | let mut buf = serde_json::to_vec(&request).unwrap(); |
| @@ -147,6 +148,35 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { | |||
| 147 | println!(); | 148 | println!(); |
| 148 | } | 149 | } |
| 149 | } | 150 | } |
| 151 | + Msg::FocusedWindow => { | ||
| 152 | + let Response::FocusedWindow(window) = response else { | ||
| 153 | + bail!("unexpected response: expected FocusedWindow, got {response:?}"); | ||
| 154 | + }; | ||
| 155 | + | ||
| 156 | + if json { | ||
| 157 | + let window = serde_json::to_string(&window).context("error formatting response")?; | ||
| 158 | + println!("{window}"); | ||
| 159 | + return Ok(()); | ||
| 160 | + } | ||
| 161 | + | ||
| 162 | + if let Some(window) = window { | ||
| 163 | + println!("Focused window:"); | ||
| 164 | + | ||
| 165 | + if let Some(title) = window.title { | ||
| 166 | + println!(" Title: \"{title}\""); | ||
| 167 | + } else { | ||
| 168 | + println!(" Title: (unset)"); | ||
| 169 | + } | ||
| 170 | + | ||
| 171 | + if let Some(app_id) = window.app_id { | ||
| 172 | + println!(" App ID: \"{app_id}\""); | ||
| 173 | + } else { | ||
| 174 | + println!(" App ID: (unset)"); | ||
| 175 | + } | ||
| 176 | + } else { | ||
| 177 | + println!("No window is focused."); | ||
| 178 | + } | ||
| 179 | + } | ||
| 150 | Msg::Action { .. } => { | 180 | Msg::Action { .. } => { |
| 151 | let Response::Handled = response else { | 181 | let Response::Handled = response else { |
| 152 | bail!("unexpected response: expected Handled, got {response:?}"); | 182 | bail!("unexpected response: expected Handled, got {response:?}"); |
src/ipc/server.rsmodified+25 -0
| @@ -9,9 +9,12 @@ use directories::BaseDirs; | |||
| 9 | use futures_util::io::{AsyncReadExt, BufReader}; | 9 | use futures_util::io::{AsyncReadExt, BufReader}; |
| 10 | use futures_util::{AsyncBufReadExt, AsyncWriteExt}; | 10 | use futures_util::{AsyncBufReadExt, AsyncWriteExt}; |
| 11 | use niri_ipc::{Request, Response}; | 11 | use niri_ipc::{Request, Response}; |
| 12 | +use smithay::desktop::Window; | ||
| 12 | use smithay::reexports::calloop::generic::Generic; | 13 | use smithay::reexports::calloop::generic::Generic; |
| 13 | use smithay::reexports::calloop::{Interest, LoopHandle, Mode, PostAction}; | 14 | use smithay::reexports::calloop::{Interest, LoopHandle, Mode, PostAction}; |
| 14 | use smithay::reexports::rustix::fs::unlink; | 15 | use smithay::reexports::rustix::fs::unlink; |
| 16 | +use smithay::wayland::compositor::with_states; | ||
| 17 | +use smithay::wayland::shell::xdg::XdgToplevelSurfaceData; | ||
| 15 | 18 | ||
| 16 | use crate::backend::IpcOutputMap; | 19 | use crate::backend::IpcOutputMap; |
| 17 | use crate::niri::State; | 20 | use crate::niri::State; |
| @@ -23,6 +26,7 @@ pub struct IpcServer { | |||
| 23 | struct ClientCtx { | 26 | struct ClientCtx { |
| 24 | event_loop: LoopHandle<'static, State>, | 27 | event_loop: LoopHandle<'static, State>, |
| 25 | ipc_outputs: Arc<Mutex<IpcOutputMap>>, | 28 | ipc_outputs: Arc<Mutex<IpcOutputMap>>, |
| 29 | + ipc_focused_window: Arc<Mutex<Option<Window>>>, | ||
| 26 | } | 30 | } |
| 27 | 31 | ||
| 28 | impl IpcServer { | 32 | impl IpcServer { |
| @@ -87,6 +91,7 @@ fn on_new_ipc_client(state: &mut State, stream: UnixStream) { | |||
| 87 | let ctx = ClientCtx { | 91 | let ctx = ClientCtx { |
| 88 | event_loop: state.niri.event_loop.clone(), | 92 | event_loop: state.niri.event_loop.clone(), |
| 89 | ipc_outputs: state.backend.ipc_outputs(), | 93 | ipc_outputs: state.backend.ipc_outputs(), |
| 94 | + ipc_focused_window: state.niri.ipc_focused_window.clone(), | ||
| 90 | }; | 95 | }; |
| 91 | 96 | ||
| 92 | let future = async move { | 97 | let future = async move { |
| @@ -128,6 +133,26 @@ fn process(ctx: &ClientCtx, buf: &str) -> anyhow::Result<Response> { | |||
| 128 | let ipc_outputs = ctx.ipc_outputs.lock().unwrap().clone(); | 133 | let ipc_outputs = ctx.ipc_outputs.lock().unwrap().clone(); |
| 129 | Response::Outputs(ipc_outputs) | 134 | Response::Outputs(ipc_outputs) |
| 130 | } | 135 | } |
| 136 | + Request::FocusedWindow => { | ||
| 137 | + let window = ctx.ipc_focused_window.lock().unwrap().clone(); | ||
| 138 | + let window = window.map(|window| { | ||
| 139 | + let wl_surface = window.toplevel().expect("no X11 support").wl_surface(); | ||
| 140 | + with_states(wl_surface, |states| { | ||
| 141 | + let role = states | ||
| 142 | + .data_map | ||
| 143 | + .get::<XdgToplevelSurfaceData>() | ||
| 144 | + .unwrap() | ||
| 145 | + .lock() | ||
| 146 | + .unwrap(); | ||
| 147 | + | ||
| 148 | + niri_ipc::Window { | ||
| 149 | + title: role.title.clone(), | ||
| 150 | + app_id: role.app_id.clone(), | ||
| 151 | + } | ||
| 152 | + }) | ||
| 153 | + }); | ||
| 154 | + Response::FocusedWindow(window) | ||
| 155 | + } | ||
| 131 | Request::Action(action) => { | 156 | Request::Action(action) => { |
| 132 | let action = niri_config::Action::from(action); | 157 | let action = niri_config::Action::from(action); |
| 133 | ctx.event_loop.insert_idle(move |state| { | 158 | ctx.event_loop.insert_idle(move |state| { |
src/niri.rsmodified+7 -0
| @@ -230,6 +230,7 @@ pub struct Niri { | |||
| 230 | 230 | ||
| 231 | pub ipc_server: Option<IpcServer>, | 231 | pub ipc_server: Option<IpcServer>, |
| 232 | pub ipc_outputs_changed: bool, | 232 | pub ipc_outputs_changed: bool, |
| 233 | + pub ipc_focused_window: Arc<Mutex<Option<Window>>>, | ||
| 233 | 234 | ||
| 234 | // Casts are dropped before PipeWire to prevent a double-free (yay). | 235 | // Casts are dropped before PipeWire to prevent a double-free (yay). |
| 235 | pub casts: Vec<Cast>, | 236 | pub casts: Vec<Cast>, |
| @@ -715,6 +716,8 @@ impl State { | |||
| 715 | focus | 716 | focus |
| 716 | ); | 717 | ); |
| 717 | 718 | ||
| 719 | + let mut newly_focused_window = None; | ||
| 720 | + | ||
| 718 | // Tell the windows their new focus state for window rule purposes. | 721 | // Tell the windows their new focus state for window rule purposes. |
| 719 | if let KeyboardFocus::Layout { | 722 | if let KeyboardFocus::Layout { |
| 720 | surface: Some(surface), | 723 | surface: Some(surface), |
| @@ -730,9 +733,12 @@ impl State { | |||
| 730 | { | 733 | { |
| 731 | if let Some((mapped, _)) = self.niri.layout.find_window_and_output_mut(surface) { | 734 | if let Some((mapped, _)) = self.niri.layout.find_window_and_output_mut(surface) { |
| 732 | mapped.set_is_focused(true); | 735 | mapped.set_is_focused(true); |
| 736 | + newly_focused_window = Some(mapped.window.clone()); | ||
| 733 | } | 737 | } |
| 734 | } | 738 | } |
| 735 | 739 | ||
| 740 | + *self.niri.ipc_focused_window.lock().unwrap() = newly_focused_window; | ||
| 741 | + | ||
| 736 | if let Some(grab) = self.niri.popup_grab.as_mut() { | 742 | if let Some(grab) = self.niri.popup_grab.as_mut() { |
| 737 | if Some(&grab.root) != focus.surface() { | 743 | if Some(&grab.root) != focus.surface() { |
| 738 | trace!( | 744 | trace!( |
| @@ -1390,6 +1396,7 @@ impl Niri { | |||
| 1390 | 1396 | ||
| 1391 | ipc_server, | 1397 | ipc_server, |
| 1392 | ipc_outputs_changed: false, | 1398 | ipc_outputs_changed: false, |
| 1399 | + ipc_focused_window: Arc::new(Mutex::new(None)), | ||
| 1393 | 1400 | ||
| 1394 | pipewire, | 1401 | pipewire, |
| 1395 | casts: vec![], | 1402 | casts: vec![], |