niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·9d8f4f8·Ivan Molodetskikh·1 month ago·2997 commits
| 1 | use std::collections::hash_map::Entry; |
| 2 | use std::collections::{HashMap, HashSet}; |
| 3 | use std::sync::Arc; |
| 4 | |
| 5 | use arrayvec::ArrayVec; |
| 6 | use smithay::output::Output; |
| 7 | use smithay::reexports::wayland_protocols::ext::foreign_toplevel_list::v1::server::{ |
| 8 | ext_foreign_toplevel_handle_v1::{self, ExtForeignToplevelHandleV1}, ext_foreign_toplevel_list_v1::{self, ExtForeignToplevelListV1}, |
| 9 | }; |
| 10 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; |
| 11 | use smithay::reexports::wayland_protocols_wlr::foreign_toplevel::v1::server::{ |
| 12 | zwlr_foreign_toplevel_handle_v1::{self, ZwlrForeignToplevelHandleV1}, zwlr_foreign_toplevel_manager_v1::{self, ZwlrForeignToplevelManagerV1}, |
| 13 | }; |
| 14 | use smithay::reexports::wayland_server::backend::ClientId; |
| 15 | use smithay::reexports::wayland_server::protocol::wl_output::WlOutput; |
| 16 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; |
| 17 | use smithay::reexports::wayland_server::{ |
| 18 | Client, DataInit, Dispatch, DisplayHandle, GlobalDispatch, New, Resource, |
| 19 | }; |
| 20 | use smithay::wayland::{Dispatch2, GlobalDispatch2}; |
| 21 | use smithay::wayland::shell::xdg::{ |
| 22 | ToplevelState, ToplevelStateSet, XdgToplevelSurfaceRoleAttributes |
| 23 | }; |
| 24 | |
| 25 | use crate::niri::State; |
| 26 | use crate::protocols::EmptyData; |
| 27 | use crate::window::mapped::MappedId; |
| 28 | use crate::utils::with_toplevel_role_and_current; |
| 29 | |
| 30 | const EXT_LIST_VERSION: u32 = 1; |
| 31 | const WLR_MANAGEMENT_VERSION: u32 = 3; |
| 32 | |
| 33 | pub struct ForeignToplevelManagerState { |
| 34 | display: DisplayHandle, |
| 35 | ext_list_instances: HashSet<ExtForeignToplevelListV1>, |
| 36 | wlr_management_instances: HashSet<ZwlrForeignToplevelManagerV1>, |
| 37 | toplevels: HashMap<WlSurface, ToplevelData>, |
| 38 | } |
| 39 | |
| 40 | pub trait ForeignToplevelHandler { |
| 41 | fn foreign_toplevel_manager_state(&mut self) -> &mut ForeignToplevelManagerState; |
| 42 | fn activate(&mut self, wl_surface: WlSurface); |
| 43 | fn close(&mut self, wl_surface: WlSurface); |
| 44 | fn set_fullscreen(&mut self, wl_surface: WlSurface, wl_output: Option<WlOutput>); |
| 45 | fn unset_fullscreen(&mut self, wl_surface: WlSurface); |
| 46 | fn set_maximized(&mut self, wl_surface: WlSurface); |
| 47 | fn unset_maximized(&mut self, wl_surface: WlSurface); |
| 48 | } |
| 49 | |
| 50 | struct ToplevelData { |
| 51 | identifier: MappedId, |
| 52 | title: Option<String>, |
| 53 | app_id: Option<String>, |
| 54 | states: ArrayVec<u32, 3>, |
| 55 | output: Option<Output>, |
| 56 | |
| 57 | ext_list_instances: HashSet<ExtForeignToplevelHandleV1>, |
| 58 | wlr_management_instances: HashMap<ZwlrForeignToplevelHandleV1, Vec<WlOutput>>, |
| 59 | // FIXME: parent. |
| 60 | } |
| 61 | |
| 62 | #[derive(Clone)] |
| 63 | pub struct ForeignToplevelGlobalData { |
| 64 | filter: Arc<dyn for<'c> Fn(&'c Client) -> bool + Send + Sync>, |
| 65 | } |
| 66 | |
| 67 | impl ForeignToplevelManagerState { |
| 68 | pub fn new<D, F>(display: &DisplayHandle, filter: F) -> Self |
| 69 | where |
| 70 | D: GlobalDispatch<ZwlrForeignToplevelManagerV1, ForeignToplevelGlobalData>, |
| 71 | D: GlobalDispatch<ExtForeignToplevelListV1, ForeignToplevelGlobalData>, |
| 72 | D: 'static, |
| 73 | F: for<'c> Fn(&'c Client) -> bool + Send + Sync + 'static, |
| 74 | { |
| 75 | let global_data = ForeignToplevelGlobalData { |
| 76 | filter: Arc::new(filter), |
| 77 | }; |
| 78 | display |
| 79 | .create_global::<D, ExtForeignToplevelListV1, _>(EXT_LIST_VERSION, global_data.clone()); |
| 80 | display.create_global::<D, ZwlrForeignToplevelManagerV1, _>( |
| 81 | WLR_MANAGEMENT_VERSION, |
| 82 | global_data, |
| 83 | ); |
| 84 | Self { |
| 85 | display: display.clone(), |
| 86 | ext_list_instances: HashSet::new(), |
| 87 | wlr_management_instances: HashSet::new(), |
| 88 | toplevels: HashMap::new(), |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | pub fn refresh(state: &mut State) { |
| 94 | let _span = tracy_client::span!("foreign_toplevel::refresh"); |
| 95 | |
| 96 | let protocol_state = &mut state.niri.foreign_toplevel_state; |
| 97 | |
| 98 | // Handle closed windows. |
| 99 | protocol_state.toplevels.retain(|surface, data| { |
| 100 | if state.niri.layout.find_window_and_output(surface).is_some() { |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | for instance in data.ext_list_instances.iter() { |
| 105 | instance.closed(); |
| 106 | } |
| 107 | |
| 108 | for instance in data.wlr_management_instances.keys() { |
| 109 | instance.closed(); |
| 110 | } |
| 111 | |
| 112 | false |
| 113 | }); |
| 114 | |
| 115 | // Handle new and existing windows. |
| 116 | // |
| 117 | // Save the focused window for last, this way when the focus changes, we will first deactivate |
| 118 | // the previous window and only then activate the newly focused window. |
| 119 | let mut focused = None; |
| 120 | state.niri.layout.with_windows(|mapped, output, _, _| { |
| 121 | let toplevel = mapped.toplevel(); |
| 122 | let wl_surface = toplevel.wl_surface(); |
| 123 | with_toplevel_role_and_current(toplevel, |role, cur| { |
| 124 | let Some(cur) = cur else { |
| 125 | error!("mapped must have had initial commit"); |
| 126 | return; |
| 127 | }; |
| 128 | |
| 129 | if state.niri.keyboard_focus.surface() == Some(wl_surface) { |
| 130 | focused = Some((mapped.id(), mapped.window.clone(), output.cloned())); |
| 131 | } else { |
| 132 | refresh_toplevel( |
| 133 | protocol_state, |
| 134 | wl_surface, |
| 135 | mapped.id(), |
| 136 | role, |
| 137 | cur, |
| 138 | output, |
| 139 | false, |
| 140 | ); |
| 141 | } |
| 142 | }); |
| 143 | }); |
| 144 | |
| 145 | // Finally, refresh the focused window. |
| 146 | if let Some((identifier, window, output)) = focused { |
| 147 | let toplevel = window.toplevel().expect("no X11 support"); |
| 148 | let wl_surface = toplevel.wl_surface(); |
| 149 | with_toplevel_role_and_current(toplevel, |role, cur| { |
| 150 | let Some(cur) = cur else { |
| 151 | error!("mapped must have had initial commit"); |
| 152 | return; |
| 153 | }; |
| 154 | |
| 155 | refresh_toplevel( |
| 156 | protocol_state, |
| 157 | wl_surface, |
| 158 | identifier, |
| 159 | role, |
| 160 | cur, |
| 161 | output.as_ref(), |
| 162 | true, |
| 163 | ); |
| 164 | }); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | pub fn on_output_bound(state: &mut State, output: &Output, wl_output: &WlOutput) { |
| 169 | let _span = tracy_client::span!("foreign_toplevel::on_output_bound"); |
| 170 | |
| 171 | let Some(client) = wl_output.client() else { |
| 172 | return; |
| 173 | }; |
| 174 | |
| 175 | let protocol_state = &mut state.niri.foreign_toplevel_state; |
| 176 | for data in protocol_state.toplevels.values_mut() { |
| 177 | if data.output.as_ref() != Some(output) { |
| 178 | continue; |
| 179 | } |
| 180 | |
| 181 | for (instance, outputs) in &mut data.wlr_management_instances { |
| 182 | if instance.client().as_ref() != Some(&client) { |
| 183 | continue; |
| 184 | } |
| 185 | |
| 186 | instance.output_enter(wl_output); |
| 187 | instance.done(); |
| 188 | outputs.push(wl_output.clone()); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | fn refresh_toplevel( |
| 194 | protocol_state: &mut ForeignToplevelManagerState, |
| 195 | wl_surface: &WlSurface, |
| 196 | identifier: MappedId, |
| 197 | role: &XdgToplevelSurfaceRoleAttributes, |
| 198 | current: &ToplevelState, |
| 199 | output: Option<&Output>, |
| 200 | has_focus: bool, |
| 201 | ) { |
| 202 | let states = to_state_vec(¤t.states, has_focus); |
| 203 | |
| 204 | match protocol_state.toplevels.entry(wl_surface.clone()) { |
| 205 | Entry::Occupied(entry) => { |
| 206 | // Existing window, check if anything changed. |
| 207 | let data = entry.into_mut(); |
| 208 | |
| 209 | let mut new_title = None; |
| 210 | if data.title != role.title { |
| 211 | data.title.clone_from(&role.title); |
| 212 | new_title = role.title.as_deref(); |
| 213 | |
| 214 | if new_title.is_none() { |
| 215 | error!("toplevel title changed to None"); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | let mut new_app_id = None; |
| 220 | if data.app_id != role.app_id { |
| 221 | data.app_id.clone_from(&role.app_id); |
| 222 | new_app_id = role.app_id.as_deref(); |
| 223 | |
| 224 | if new_app_id.is_none() { |
| 225 | error!("toplevel app_id changed to None"); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | let mut states_changed = false; |
| 230 | if data.states != states { |
| 231 | data.states = states; |
| 232 | states_changed = true; |
| 233 | } |
| 234 | |
| 235 | let mut output_changed = false; |
| 236 | if data.output.as_ref() != output { |
| 237 | data.output = output.cloned(); |
| 238 | output_changed = true; |
| 239 | } |
| 240 | |
| 241 | let something_changed_for_ext = new_title.is_some() || new_app_id.is_some(); |
| 242 | let something_changed_for_wlr = |
| 243 | new_title.is_some() || new_app_id.is_some() || states_changed || output_changed; |
| 244 | |
| 245 | if something_changed_for_ext { |
| 246 | for instance in &data.ext_list_instances { |
| 247 | if let Some(new_title) = new_title { |
| 248 | instance.title(new_title.to_owned()); |
| 249 | } |
| 250 | if let Some(new_app_id) = new_app_id { |
| 251 | instance.app_id(new_app_id.to_owned()); |
| 252 | } |
| 253 | instance.done(); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if something_changed_for_wlr { |
| 258 | for (instance, outputs) in &mut data.wlr_management_instances { |
| 259 | if let Some(new_title) = new_title { |
| 260 | instance.title(new_title.to_owned()); |
| 261 | } |
| 262 | if let Some(new_app_id) = new_app_id { |
| 263 | instance.app_id(new_app_id.to_owned()); |
| 264 | } |
| 265 | if states_changed { |
| 266 | instance.state(data.states.iter().flat_map(|x| x.to_ne_bytes()).collect()); |
| 267 | } |
| 268 | if output_changed { |
| 269 | for wl_output in outputs.drain(..) { |
| 270 | instance.output_leave(&wl_output); |
| 271 | } |
| 272 | if let Some(output) = &data.output { |
| 273 | if let Some(client) = instance.client() { |
| 274 | for wl_output in output.client_outputs(&client) { |
| 275 | instance.output_enter(&wl_output); |
| 276 | outputs.push(wl_output); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | instance.done(); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | for outputs in data.wlr_management_instances.values_mut() { |
| 286 | // Clean up dead wl_outputs. |
| 287 | outputs.retain(|x| x.is_alive()); |
| 288 | } |
| 289 | } |
| 290 | Entry::Vacant(entry) => { |
| 291 | // New window, start tracking it. |
| 292 | let mut data = ToplevelData { |
| 293 | identifier, |
| 294 | title: role.title.clone(), |
| 295 | app_id: role.app_id.clone(), |
| 296 | states, |
| 297 | output: output.cloned(), |
| 298 | ext_list_instances: HashSet::new(), |
| 299 | wlr_management_instances: HashMap::new(), |
| 300 | }; |
| 301 | |
| 302 | for manager in &protocol_state.ext_list_instances { |
| 303 | if let Some(client) = manager.client() { |
| 304 | data.add_ext_instance::<State>(&protocol_state.display, &client, manager); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | for manager in &protocol_state.wlr_management_instances { |
| 309 | if let Some(client) = manager.client() { |
| 310 | data.add_wlr_instance::<State>(&protocol_state.display, &client, manager); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | entry.insert(data); |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | impl ToplevelData { |
| 320 | fn add_ext_instance<D>( |
| 321 | &mut self, |
| 322 | handle: &DisplayHandle, |
| 323 | client: &Client, |
| 324 | manager: &ExtForeignToplevelListV1, |
| 325 | ) where |
| 326 | D: Dispatch<ExtForeignToplevelHandleV1, EmptyData>, |
| 327 | D: 'static, |
| 328 | { |
| 329 | let toplevel = client |
| 330 | .create_resource::<ExtForeignToplevelHandleV1, _, D>( |
| 331 | handle, |
| 332 | manager.version(), |
| 333 | EmptyData, |
| 334 | ) |
| 335 | .unwrap(); |
| 336 | manager.toplevel(&toplevel); |
| 337 | |
| 338 | toplevel.identifier(self.identifier.to_protocol_identifier()); |
| 339 | |
| 340 | if let Some(title) = &self.title { |
| 341 | toplevel.title(title.clone()); |
| 342 | } |
| 343 | if let Some(app_id) = &self.app_id { |
| 344 | toplevel.app_id(app_id.clone()); |
| 345 | } |
| 346 | |
| 347 | toplevel.done(); |
| 348 | |
| 349 | self.ext_list_instances.insert(toplevel); |
| 350 | } |
| 351 | |
| 352 | fn add_wlr_instance<D>( |
| 353 | &mut self, |
| 354 | handle: &DisplayHandle, |
| 355 | client: &Client, |
| 356 | manager: &ZwlrForeignToplevelManagerV1, |
| 357 | ) where |
| 358 | D: Dispatch<ZwlrForeignToplevelHandleV1, EmptyData>, |
| 359 | D: 'static, |
| 360 | { |
| 361 | let toplevel = client |
| 362 | .create_resource::<ZwlrForeignToplevelHandleV1, _, D>( |
| 363 | handle, |
| 364 | manager.version(), |
| 365 | EmptyData, |
| 366 | ) |
| 367 | .unwrap(); |
| 368 | manager.toplevel(&toplevel); |
| 369 | |
| 370 | if let Some(title) = &self.title { |
| 371 | toplevel.title(title.clone()); |
| 372 | } |
| 373 | if let Some(app_id) = &self.app_id { |
| 374 | toplevel.app_id(app_id.clone()); |
| 375 | } |
| 376 | |
| 377 | toplevel.state(self.states.iter().flat_map(|x| x.to_ne_bytes()).collect()); |
| 378 | |
| 379 | let mut outputs = Vec::new(); |
| 380 | if let Some(output) = &self.output { |
| 381 | for wl_output in output.client_outputs(client) { |
| 382 | toplevel.output_enter(&wl_output); |
| 383 | outputs.push(wl_output); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | toplevel.done(); |
| 388 | |
| 389 | self.wlr_management_instances.insert(toplevel, outputs); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | impl<D> GlobalDispatch2<ExtForeignToplevelListV1, D> for ForeignToplevelGlobalData |
| 394 | where |
| 395 | D: Dispatch<ExtForeignToplevelListV1, EmptyData>, |
| 396 | D: Dispatch<ExtForeignToplevelHandleV1, EmptyData>, |
| 397 | D: ForeignToplevelHandler, |
| 398 | { |
| 399 | fn bind( |
| 400 | &self, |
| 401 | state: &mut D, |
| 402 | handle: &DisplayHandle, |
| 403 | client: &Client, |
| 404 | resource: New<ExtForeignToplevelListV1>, |
| 405 | data_init: &mut DataInit<'_, D>, |
| 406 | ) { |
| 407 | let manager = data_init.init(resource, EmptyData); |
| 408 | |
| 409 | let state = state.foreign_toplevel_manager_state(); |
| 410 | |
| 411 | for data in state.toplevels.values_mut() { |
| 412 | data.add_ext_instance::<D>(handle, client, &manager); |
| 413 | } |
| 414 | |
| 415 | state.ext_list_instances.insert(manager); |
| 416 | } |
| 417 | |
| 418 | fn can_view(&self, client: &Client) -> bool { |
| 419 | (self.filter)(client) |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | impl<D> Dispatch2<ExtForeignToplevelListV1, D> for EmptyData |
| 424 | where |
| 425 | D: ForeignToplevelHandler, |
| 426 | { |
| 427 | fn request( |
| 428 | &self, |
| 429 | state: &mut D, |
| 430 | _client: &Client, |
| 431 | resource: &ExtForeignToplevelListV1, |
| 432 | request: <ExtForeignToplevelListV1 as Resource>::Request, |
| 433 | _dhandle: &DisplayHandle, |
| 434 | _data_init: &mut DataInit<'_, D>, |
| 435 | ) { |
| 436 | match request { |
| 437 | ext_foreign_toplevel_list_v1::Request::Stop => { |
| 438 | resource.finished(); |
| 439 | |
| 440 | // remove the instance here so we won't send any more events. |
| 441 | let state = state.foreign_toplevel_manager_state(); |
| 442 | state.ext_list_instances.remove(resource); |
| 443 | } |
| 444 | ext_foreign_toplevel_list_v1::Request::Destroy => {} |
| 445 | _ => unreachable!(), |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | fn destroyed(&self, state: &mut D, _client: ClientId, resource: &ExtForeignToplevelListV1) { |
| 450 | // also remove the instance here, in case `stop` was never sent, e.g. sudden disconnect. |
| 451 | let state = state.foreign_toplevel_manager_state(); |
| 452 | state.ext_list_instances.remove(resource); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | impl<D> Dispatch2<ExtForeignToplevelHandleV1, D> for EmptyData |
| 457 | where |
| 458 | D: ForeignToplevelHandler, |
| 459 | { |
| 460 | fn request( |
| 461 | &self, |
| 462 | _state: &mut D, |
| 463 | _client: &Client, |
| 464 | _resource: &ExtForeignToplevelHandleV1, |
| 465 | request: <ExtForeignToplevelHandleV1 as Resource>::Request, |
| 466 | _dhandle: &DisplayHandle, |
| 467 | _data_init: &mut DataInit<'_, D>, |
| 468 | ) { |
| 469 | match request { |
| 470 | ext_foreign_toplevel_handle_v1::Request::Destroy => {} |
| 471 | _ => unreachable!(), |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | fn destroyed(&self, state: &mut D, _client: ClientId, resource: &ExtForeignToplevelHandleV1) { |
| 476 | let state = state.foreign_toplevel_manager_state(); |
| 477 | for data in state.toplevels.values_mut() { |
| 478 | data.ext_list_instances.remove(resource); |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | impl<D> GlobalDispatch2<ZwlrForeignToplevelManagerV1, D> for ForeignToplevelGlobalData |
| 484 | where |
| 485 | D: Dispatch<ZwlrForeignToplevelManagerV1, EmptyData>, |
| 486 | D: Dispatch<ZwlrForeignToplevelHandleV1, EmptyData>, |
| 487 | D: ForeignToplevelHandler, |
| 488 | { |
| 489 | fn bind( |
| 490 | &self, |
| 491 | state: &mut D, |
| 492 | handle: &DisplayHandle, |
| 493 | client: &Client, |
| 494 | resource: New<ZwlrForeignToplevelManagerV1>, |
| 495 | data_init: &mut DataInit<'_, D>, |
| 496 | ) { |
| 497 | let manager = data_init.init(resource, EmptyData); |
| 498 | |
| 499 | let state = state.foreign_toplevel_manager_state(); |
| 500 | |
| 501 | for data in state.toplevels.values_mut() { |
| 502 | data.add_wlr_instance::<D>(handle, client, &manager); |
| 503 | } |
| 504 | |
| 505 | state.wlr_management_instances.insert(manager); |
| 506 | } |
| 507 | |
| 508 | fn can_view(&self, client: &Client) -> bool { |
| 509 | (self.filter)(client) |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | impl<D> Dispatch2<ZwlrForeignToplevelManagerV1, D> for EmptyData |
| 514 | where |
| 515 | D: ForeignToplevelHandler, |
| 516 | { |
| 517 | fn request( |
| 518 | &self, |
| 519 | state: &mut D, |
| 520 | _client: &Client, |
| 521 | resource: &ZwlrForeignToplevelManagerV1, |
| 522 | request: <ZwlrForeignToplevelManagerV1 as Resource>::Request, |
| 523 | _dhandle: &DisplayHandle, |
| 524 | _data_init: &mut DataInit<'_, D>, |
| 525 | ) { |
| 526 | match request { |
| 527 | zwlr_foreign_toplevel_manager_v1::Request::Stop => { |
| 528 | resource.finished(); |
| 529 | |
| 530 | // remove the instance here so we won't send any more events. |
| 531 | let state = state.foreign_toplevel_manager_state(); |
| 532 | state.wlr_management_instances.remove(resource); |
| 533 | } |
| 534 | _ => unreachable!(), |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | fn destroyed(&self, state: &mut D, _client: ClientId, resource: &ZwlrForeignToplevelManagerV1) { |
| 539 | // also remove the instance here, in case `stop` was never sent, e.g. sudden disconnect. |
| 540 | let state = state.foreign_toplevel_manager_state(); |
| 541 | state.wlr_management_instances.remove(resource); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | impl<D> Dispatch2<ZwlrForeignToplevelHandleV1, D> for EmptyData |
| 546 | where |
| 547 | D: ForeignToplevelHandler, |
| 548 | { |
| 549 | fn request( |
| 550 | &self, |
| 551 | state: &mut D, |
| 552 | _client: &Client, |
| 553 | resource: &ZwlrForeignToplevelHandleV1, |
| 554 | request: <ZwlrForeignToplevelHandleV1 as Resource>::Request, |
| 555 | _dhandle: &DisplayHandle, |
| 556 | _data_init: &mut DataInit<'_, D>, |
| 557 | ) { |
| 558 | let protocol_state = state.foreign_toplevel_manager_state(); |
| 559 | |
| 560 | let Some((surface, _)) = protocol_state |
| 561 | .toplevels |
| 562 | .iter() |
| 563 | .find(|(_, data)| data.wlr_management_instances.contains_key(resource)) |
| 564 | else { |
| 565 | return; |
| 566 | }; |
| 567 | let surface = surface.clone(); |
| 568 | |
| 569 | match request { |
| 570 | zwlr_foreign_toplevel_handle_v1::Request::SetMaximized => state.set_maximized(surface), |
| 571 | zwlr_foreign_toplevel_handle_v1::Request::UnsetMaximized => { |
| 572 | state.unset_maximized(surface) |
| 573 | } |
| 574 | zwlr_foreign_toplevel_handle_v1::Request::SetMinimized => (), |
| 575 | zwlr_foreign_toplevel_handle_v1::Request::UnsetMinimized => (), |
| 576 | zwlr_foreign_toplevel_handle_v1::Request::Activate { .. } => { |
| 577 | state.activate(surface); |
| 578 | } |
| 579 | zwlr_foreign_toplevel_handle_v1::Request::Close => { |
| 580 | state.close(surface); |
| 581 | } |
| 582 | zwlr_foreign_toplevel_handle_v1::Request::SetRectangle { .. } => (), |
| 583 | zwlr_foreign_toplevel_handle_v1::Request::Destroy => (), |
| 584 | zwlr_foreign_toplevel_handle_v1::Request::SetFullscreen { output } => { |
| 585 | state.set_fullscreen(surface, output); |
| 586 | } |
| 587 | zwlr_foreign_toplevel_handle_v1::Request::UnsetFullscreen => { |
| 588 | state.unset_fullscreen(surface); |
| 589 | } |
| 590 | _ => unreachable!(), |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | fn destroyed(&self, state: &mut D, _client: ClientId, resource: &ZwlrForeignToplevelHandleV1) { |
| 595 | let state = state.foreign_toplevel_manager_state(); |
| 596 | for data in state.toplevels.values_mut() { |
| 597 | data.wlr_management_instances.remove(resource); |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | fn to_state_vec(states: &ToplevelStateSet, has_focus: bool) -> ArrayVec<u32, 3> { |
| 603 | let mut rv = ArrayVec::new(); |
| 604 | if states.contains(xdg_toplevel::State::Maximized) { |
| 605 | rv.push(zwlr_foreign_toplevel_handle_v1::State::Maximized as u32); |
| 606 | } |
| 607 | if states.contains(xdg_toplevel::State::Fullscreen) { |
| 608 | rv.push(zwlr_foreign_toplevel_handle_v1::State::Fullscreen as u32); |
| 609 | } |
| 610 | |
| 611 | // HACK: wlr-foreign-toplevel-management states: |
| 612 | // |
| 613 | // These have the same meaning as the states with the same names defined in xdg-toplevel |
| 614 | // |
| 615 | // However, clients such as sfwbar and fcitx seem to treat the activated state as keyboard |
| 616 | // focus, i.e. they don't expect multiple windows to have it set at once. Even Waybar which |
| 617 | // handles multiple activated windows correctly uses it in its design in such a way that |
| 618 | // keyboard focus would make more sense. Let's do what the clients expect. |
| 619 | if has_focus { |
| 620 | rv.push(zwlr_foreign_toplevel_handle_v1::State::Activated as u32); |
| 621 | } |
| 622 | |
| 623 | rv |
| 624 | } |