niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·055a94d·Ivan Molodetskikh·1 year ago·2247 commits
| 1 | use std::collections::hash_map::Entry; |
| 2 | use std::collections::HashMap; |
| 3 | |
| 4 | use arrayvec::ArrayVec; |
| 5 | use smithay::output::Output; |
| 6 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; |
| 7 | use smithay::reexports::wayland_protocols_wlr; |
| 8 | use smithay::reexports::wayland_server::backend::ClientId; |
| 9 | use smithay::reexports::wayland_server::protocol::wl_output::WlOutput; |
| 10 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; |
| 11 | use smithay::reexports::wayland_server::{ |
| 12 | Client, DataInit, Dispatch, DisplayHandle, GlobalDispatch, New, Resource, |
| 13 | }; |
| 14 | use smithay::wayland::shell::xdg::{ToplevelStateSet, XdgToplevelSurfaceRoleAttributes}; |
| 15 | use wayland_protocols_wlr::foreign_toplevel::v1::server::{ |
| 16 | zwlr_foreign_toplevel_handle_v1, zwlr_foreign_toplevel_manager_v1, |
| 17 | }; |
| 18 | use zwlr_foreign_toplevel_handle_v1::ZwlrForeignToplevelHandleV1; |
| 19 | use zwlr_foreign_toplevel_manager_v1::ZwlrForeignToplevelManagerV1; |
| 20 | |
| 21 | use crate::niri::State; |
| 22 | use crate::utils::with_toplevel_role; |
| 23 | |
| 24 | const VERSION: u32 = 3; |
| 25 | |
| 26 | pub struct ForeignToplevelManagerState { |
| 27 | display: DisplayHandle, |
| 28 | instances: Vec<ZwlrForeignToplevelManagerV1>, |
| 29 | toplevels: HashMap<WlSurface, ToplevelData>, |
| 30 | } |
| 31 | |
| 32 | pub trait ForeignToplevelHandler { |
| 33 | fn foreign_toplevel_manager_state(&mut self) -> &mut ForeignToplevelManagerState; |
| 34 | fn activate(&mut self, wl_surface: WlSurface); |
| 35 | fn close(&mut self, wl_surface: WlSurface); |
| 36 | fn set_fullscreen(&mut self, wl_surface: WlSurface, wl_output: Option<WlOutput>); |
| 37 | fn unset_fullscreen(&mut self, wl_surface: WlSurface); |
| 38 | } |
| 39 | |
| 40 | struct ToplevelData { |
| 41 | title: Option<String>, |
| 42 | app_id: Option<String>, |
| 43 | states: ArrayVec<u32, 3>, |
| 44 | output: Option<Output>, |
| 45 | instances: HashMap<ZwlrForeignToplevelHandleV1, Vec<WlOutput>>, |
| 46 | // FIXME: parent. |
| 47 | } |
| 48 | |
| 49 | pub struct ForeignToplevelGlobalData { |
| 50 | filter: Box<dyn for<'c> Fn(&'c Client) -> bool + Send + Sync>, |
| 51 | } |
| 52 | |
| 53 | impl ForeignToplevelManagerState { |
| 54 | pub fn new<D, F>(display: &DisplayHandle, filter: F) -> Self |
| 55 | where |
| 56 | D: GlobalDispatch<ZwlrForeignToplevelManagerV1, ForeignToplevelGlobalData>, |
| 57 | D: Dispatch<ZwlrForeignToplevelManagerV1, ()>, |
| 58 | D: 'static, |
| 59 | F: for<'c> Fn(&'c Client) -> bool + Send + Sync + 'static, |
| 60 | { |
| 61 | let global_data = ForeignToplevelGlobalData { |
| 62 | filter: Box::new(filter), |
| 63 | }; |
| 64 | display.create_global::<D, ZwlrForeignToplevelManagerV1, _>(VERSION, global_data); |
| 65 | Self { |
| 66 | display: display.clone(), |
| 67 | instances: Vec::new(), |
| 68 | toplevels: HashMap::new(), |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | pub fn refresh(state: &mut State) { |
| 74 | let _span = tracy_client::span!("foreign_toplevel::refresh"); |
| 75 | |
| 76 | let protocol_state = &mut state.niri.foreign_toplevel_state; |
| 77 | |
| 78 | // Handle closed windows. |
| 79 | protocol_state.toplevels.retain(|surface, data| { |
| 80 | if state.niri.layout.find_window_and_output(surface).is_some() { |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | for instance in data.instances.keys() { |
| 85 | instance.closed(); |
| 86 | } |
| 87 | |
| 88 | false |
| 89 | }); |
| 90 | |
| 91 | // Handle new and existing windows. |
| 92 | // |
| 93 | // Save the focused window for last, this way when the focus changes, we will first deactivate |
| 94 | // the previous window and only then activate the newly focused window. |
| 95 | let mut focused = None; |
| 96 | state.niri.layout.with_windows(|mapped, output, _| { |
| 97 | let toplevel = mapped.toplevel(); |
| 98 | let wl_surface = toplevel.wl_surface(); |
| 99 | with_toplevel_role(toplevel, |role| { |
| 100 | if state.niri.keyboard_focus.surface() == Some(wl_surface) { |
| 101 | focused = Some((mapped.window.clone(), output.cloned())); |
| 102 | } else { |
| 103 | refresh_toplevel(protocol_state, wl_surface, role, output, false); |
| 104 | } |
| 105 | }); |
| 106 | }); |
| 107 | |
| 108 | // Finally, refresh the focused window. |
| 109 | if let Some((window, output)) = focused { |
| 110 | let toplevel = window.toplevel().expect("no X11 support"); |
| 111 | let wl_surface = toplevel.wl_surface(); |
| 112 | with_toplevel_role(toplevel, |role| { |
| 113 | refresh_toplevel(protocol_state, wl_surface, role, output.as_ref(), true); |
| 114 | }); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | pub fn on_output_bound(state: &mut State, output: &Output, wl_output: &WlOutput) { |
| 119 | let _span = tracy_client::span!("foreign_toplevel::on_output_bound"); |
| 120 | |
| 121 | let Some(client) = wl_output.client() else { |
| 122 | return; |
| 123 | }; |
| 124 | |
| 125 | let protocol_state = &mut state.niri.foreign_toplevel_state; |
| 126 | for data in protocol_state.toplevels.values_mut() { |
| 127 | if data.output.as_ref() != Some(output) { |
| 128 | continue; |
| 129 | } |
| 130 | |
| 131 | for (instance, outputs) in &mut data.instances { |
| 132 | if instance.client().as_ref() != Some(&client) { |
| 133 | continue; |
| 134 | } |
| 135 | |
| 136 | instance.output_enter(wl_output); |
| 137 | instance.done(); |
| 138 | outputs.push(wl_output.clone()); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | fn refresh_toplevel( |
| 144 | protocol_state: &mut ForeignToplevelManagerState, |
| 145 | wl_surface: &WlSurface, |
| 146 | role: &XdgToplevelSurfaceRoleAttributes, |
| 147 | output: Option<&Output>, |
| 148 | has_focus: bool, |
| 149 | ) { |
| 150 | let states = to_state_vec(&role.current.states, has_focus); |
| 151 | |
| 152 | match protocol_state.toplevels.entry(wl_surface.clone()) { |
| 153 | Entry::Occupied(entry) => { |
| 154 | // Existing window, check if anything changed. |
| 155 | let data = entry.into_mut(); |
| 156 | |
| 157 | let mut new_title = None; |
| 158 | if data.title != role.title { |
| 159 | data.title.clone_from(&role.title); |
| 160 | new_title = role.title.as_deref(); |
| 161 | |
| 162 | if new_title.is_none() { |
| 163 | error!("toplevel title changed to None"); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | let mut new_app_id = None; |
| 168 | if data.app_id != role.app_id { |
| 169 | data.app_id.clone_from(&role.app_id); |
| 170 | new_app_id = role.app_id.as_deref(); |
| 171 | |
| 172 | if new_app_id.is_none() { |
| 173 | error!("toplevel app_id changed to None"); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | let mut states_changed = false; |
| 178 | if data.states != states { |
| 179 | data.states = states; |
| 180 | states_changed = true; |
| 181 | } |
| 182 | |
| 183 | let mut output_changed = false; |
| 184 | if data.output.as_ref() != output { |
| 185 | data.output = output.cloned(); |
| 186 | output_changed = true; |
| 187 | } |
| 188 | |
| 189 | let something_changed = |
| 190 | new_title.is_some() || new_app_id.is_some() || states_changed || output_changed; |
| 191 | |
| 192 | if something_changed { |
| 193 | for (instance, outputs) in &mut data.instances { |
| 194 | if let Some(new_title) = new_title { |
| 195 | instance.title(new_title.to_owned()); |
| 196 | } |
| 197 | if let Some(new_app_id) = new_app_id { |
| 198 | instance.app_id(new_app_id.to_owned()); |
| 199 | } |
| 200 | if states_changed { |
| 201 | instance.state(data.states.iter().flat_map(|x| x.to_ne_bytes()).collect()); |
| 202 | } |
| 203 | if output_changed { |
| 204 | for wl_output in outputs.drain(..) { |
| 205 | instance.output_leave(&wl_output); |
| 206 | } |
| 207 | if let Some(output) = &data.output { |
| 208 | if let Some(client) = instance.client() { |
| 209 | for wl_output in output.client_outputs(&client) { |
| 210 | instance.output_enter(&wl_output); |
| 211 | outputs.push(wl_output); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | instance.done(); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | for outputs in data.instances.values_mut() { |
| 221 | // Clean up dead wl_outputs. |
| 222 | outputs.retain(|x| x.is_alive()); |
| 223 | } |
| 224 | } |
| 225 | Entry::Vacant(entry) => { |
| 226 | // New window, start tracking it. |
| 227 | let mut data = ToplevelData { |
| 228 | title: role.title.clone(), |
| 229 | app_id: role.app_id.clone(), |
| 230 | states, |
| 231 | output: output.cloned(), |
| 232 | instances: HashMap::new(), |
| 233 | }; |
| 234 | |
| 235 | for manager in &protocol_state.instances { |
| 236 | if let Some(client) = manager.client() { |
| 237 | data.add_instance::<State>(&protocol_state.display, &client, manager); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | entry.insert(data); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | impl ToplevelData { |
| 247 | fn add_instance<D>( |
| 248 | &mut self, |
| 249 | handle: &DisplayHandle, |
| 250 | client: &Client, |
| 251 | manager: &ZwlrForeignToplevelManagerV1, |
| 252 | ) where |
| 253 | D: Dispatch<ZwlrForeignToplevelHandleV1, ()>, |
| 254 | D: 'static, |
| 255 | { |
| 256 | let toplevel = client |
| 257 | .create_resource::<ZwlrForeignToplevelHandleV1, _, D>(handle, manager.version(), ()) |
| 258 | .unwrap(); |
| 259 | manager.toplevel(&toplevel); |
| 260 | |
| 261 | if let Some(title) = &self.title { |
| 262 | toplevel.title(title.clone()); |
| 263 | } |
| 264 | if let Some(app_id) = &self.app_id { |
| 265 | toplevel.app_id(app_id.clone()); |
| 266 | } |
| 267 | |
| 268 | toplevel.state(self.states.iter().flat_map(|x| x.to_ne_bytes()).collect()); |
| 269 | |
| 270 | let mut outputs = Vec::new(); |
| 271 | if let Some(output) = &self.output { |
| 272 | for wl_output in output.client_outputs(client) { |
| 273 | toplevel.output_enter(&wl_output); |
| 274 | outputs.push(wl_output); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | toplevel.done(); |
| 279 | |
| 280 | self.instances.insert(toplevel, outputs); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | impl<D> GlobalDispatch<ZwlrForeignToplevelManagerV1, ForeignToplevelGlobalData, D> |
| 285 | for ForeignToplevelManagerState |
| 286 | where |
| 287 | D: GlobalDispatch<ZwlrForeignToplevelManagerV1, ForeignToplevelGlobalData>, |
| 288 | D: Dispatch<ZwlrForeignToplevelManagerV1, ()>, |
| 289 | D: Dispatch<ZwlrForeignToplevelHandleV1, ()>, |
| 290 | D: ForeignToplevelHandler, |
| 291 | { |
| 292 | fn bind( |
| 293 | state: &mut D, |
| 294 | handle: &DisplayHandle, |
| 295 | client: &Client, |
| 296 | resource: New<ZwlrForeignToplevelManagerV1>, |
| 297 | _global_data: &ForeignToplevelGlobalData, |
| 298 | data_init: &mut DataInit<'_, D>, |
| 299 | ) { |
| 300 | let manager = data_init.init(resource, ()); |
| 301 | |
| 302 | let state = state.foreign_toplevel_manager_state(); |
| 303 | |
| 304 | for data in state.toplevels.values_mut() { |
| 305 | data.add_instance::<D>(handle, client, &manager); |
| 306 | } |
| 307 | |
| 308 | state.instances.push(manager); |
| 309 | } |
| 310 | |
| 311 | fn can_view(client: Client, global_data: &ForeignToplevelGlobalData) -> bool { |
| 312 | (global_data.filter)(&client) |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | impl<D> Dispatch<ZwlrForeignToplevelManagerV1, (), D> for ForeignToplevelManagerState |
| 317 | where |
| 318 | D: Dispatch<ZwlrForeignToplevelManagerV1, ()>, |
| 319 | D: ForeignToplevelHandler, |
| 320 | { |
| 321 | fn request( |
| 322 | state: &mut D, |
| 323 | _client: &Client, |
| 324 | resource: &ZwlrForeignToplevelManagerV1, |
| 325 | request: <ZwlrForeignToplevelManagerV1 as Resource>::Request, |
| 326 | _data: &(), |
| 327 | _dhandle: &DisplayHandle, |
| 328 | _data_init: &mut DataInit<'_, D>, |
| 329 | ) { |
| 330 | match request { |
| 331 | zwlr_foreign_toplevel_manager_v1::Request::Stop => { |
| 332 | resource.finished(); |
| 333 | |
| 334 | let state = state.foreign_toplevel_manager_state(); |
| 335 | state.instances.retain(|x| x != resource); |
| 336 | } |
| 337 | _ => unreachable!(), |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | fn destroyed( |
| 342 | state: &mut D, |
| 343 | _client: ClientId, |
| 344 | resource: &ZwlrForeignToplevelManagerV1, |
| 345 | _data: &(), |
| 346 | ) { |
| 347 | let state = state.foreign_toplevel_manager_state(); |
| 348 | state.instances.retain(|x| x != resource); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | impl<D> Dispatch<ZwlrForeignToplevelHandleV1, (), D> for ForeignToplevelManagerState |
| 353 | where |
| 354 | D: Dispatch<ZwlrForeignToplevelHandleV1, ()>, |
| 355 | D: ForeignToplevelHandler, |
| 356 | { |
| 357 | fn request( |
| 358 | state: &mut D, |
| 359 | _client: &Client, |
| 360 | resource: &ZwlrForeignToplevelHandleV1, |
| 361 | request: <ZwlrForeignToplevelHandleV1 as Resource>::Request, |
| 362 | _data: &(), |
| 363 | _dhandle: &DisplayHandle, |
| 364 | _data_init: &mut DataInit<'_, D>, |
| 365 | ) { |
| 366 | let protocol_state = state.foreign_toplevel_manager_state(); |
| 367 | |
| 368 | let Some((surface, _)) = protocol_state |
| 369 | .toplevels |
| 370 | .iter() |
| 371 | .find(|(_, data)| data.instances.contains_key(resource)) |
| 372 | else { |
| 373 | return; |
| 374 | }; |
| 375 | let surface = surface.clone(); |
| 376 | |
| 377 | match request { |
| 378 | zwlr_foreign_toplevel_handle_v1::Request::SetMaximized => (), |
| 379 | zwlr_foreign_toplevel_handle_v1::Request::UnsetMaximized => (), |
| 380 | zwlr_foreign_toplevel_handle_v1::Request::SetMinimized => (), |
| 381 | zwlr_foreign_toplevel_handle_v1::Request::UnsetMinimized => (), |
| 382 | zwlr_foreign_toplevel_handle_v1::Request::Activate { .. } => { |
| 383 | state.activate(surface); |
| 384 | } |
| 385 | zwlr_foreign_toplevel_handle_v1::Request::Close => { |
| 386 | state.close(surface); |
| 387 | } |
| 388 | zwlr_foreign_toplevel_handle_v1::Request::SetRectangle { .. } => (), |
| 389 | zwlr_foreign_toplevel_handle_v1::Request::Destroy => (), |
| 390 | zwlr_foreign_toplevel_handle_v1::Request::SetFullscreen { output } => { |
| 391 | state.set_fullscreen(surface, output); |
| 392 | } |
| 393 | zwlr_foreign_toplevel_handle_v1::Request::UnsetFullscreen => { |
| 394 | state.unset_fullscreen(surface); |
| 395 | } |
| 396 | _ => unreachable!(), |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | fn destroyed( |
| 401 | state: &mut D, |
| 402 | _client: ClientId, |
| 403 | resource: &ZwlrForeignToplevelHandleV1, |
| 404 | _data: &(), |
| 405 | ) { |
| 406 | let state = state.foreign_toplevel_manager_state(); |
| 407 | for data in state.toplevels.values_mut() { |
| 408 | data.instances.retain(|instance, _| instance != resource); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | fn to_state_vec(states: &ToplevelStateSet, has_focus: bool) -> ArrayVec<u32, 3> { |
| 414 | let mut rv = ArrayVec::new(); |
| 415 | if states.contains(xdg_toplevel::State::Maximized) { |
| 416 | rv.push(zwlr_foreign_toplevel_handle_v1::State::Maximized as u32); |
| 417 | } |
| 418 | if states.contains(xdg_toplevel::State::Fullscreen) { |
| 419 | rv.push(zwlr_foreign_toplevel_handle_v1::State::Fullscreen as u32); |
| 420 | } |
| 421 | |
| 422 | // HACK: wlr-foreign-toplevel-management states: |
| 423 | // |
| 424 | // These have the same meaning as the states with the same names defined in xdg-toplevel |
| 425 | // |
| 426 | // However, clients such as sfwbar and fcitx seem to treat the activated state as keyboard |
| 427 | // focus, i.e. they don't expect multiple windows to have it set at once. Even Waybar which |
| 428 | // handles multiple activated windows correctly uses it in its design in such a way that |
| 429 | // keyboard focus would make more sense. Let's do what the clients expect. |
| 430 | if has_focus { |
| 431 | rv.push(zwlr_foreign_toplevel_handle_v1::State::Activated as u32); |
| 432 | } |
| 433 | |
| 434 | rv |
| 435 | } |
| 436 | |
| 437 | #[macro_export] |
| 438 | macro_rules! delegate_foreign_toplevel { |
| 439 | ($(@<$( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+>)? $ty: ty) => { |
| 440 | smithay::reexports::wayland_server::delegate_global_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ |
| 441 | smithay::reexports::wayland_protocols_wlr::foreign_toplevel::v1::server::zwlr_foreign_toplevel_manager_v1::ZwlrForeignToplevelManagerV1: $crate::protocols::foreign_toplevel::ForeignToplevelGlobalData |
| 442 | ] => $crate::protocols::foreign_toplevel::ForeignToplevelManagerState); |
| 443 | smithay::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ |
| 444 | smithay::reexports::wayland_protocols_wlr::foreign_toplevel::v1::server::zwlr_foreign_toplevel_manager_v1::ZwlrForeignToplevelManagerV1: () |
| 445 | ] => $crate::protocols::foreign_toplevel::ForeignToplevelManagerState); |
| 446 | smithay::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ |
| 447 | smithay::reexports::wayland_protocols_wlr::foreign_toplevel::v1::server::zwlr_foreign_toplevel_handle_v1::ZwlrForeignToplevelHandleV1: () |
| 448 | ] => $crate::protocols::foreign_toplevel::ForeignToplevelManagerState); |
| 449 | }; |
| 450 | } |