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 | //! ext-workspace protocol implementation. |
| 2 | //! |
| 3 | //! This is how we map the protocol concepts to the niri concepts: |
| 4 | //! |
| 5 | //! - Workspace groups are outputs. |
| 6 | //! - Workspace coordinates: X = 0, Y = workspace index. They need to be two-dimensional because 1D |
| 7 | //! coordinates are defined to be a plain list without a geometric interpretation, while we do |
| 8 | //! order workspaces in a vertical line. |
| 9 | //! - Workspace id: name for named workspaces, unset for unnamed. Because ids in this protocol are |
| 10 | //! expected to be stable across sessions. |
| 11 | //! - Workspace name: name for named workspaces, index for unnamed. |
| 12 | |
| 13 | use std::collections::hash_map::Entry; |
| 14 | use std::collections::HashMap; |
| 15 | use std::mem; |
| 16 | |
| 17 | use arrayvec::ArrayVec; |
| 18 | use ext_workspace_group_handle_v1::ExtWorkspaceGroupHandleV1; |
| 19 | use ext_workspace_handle_v1::ExtWorkspaceHandleV1; |
| 20 | use ext_workspace_manager_v1::ExtWorkspaceManagerV1; |
| 21 | use smithay::output::{Output, WeakOutput}; |
| 22 | use smithay::reexports::wayland_protocols::ext::workspace::v1::server::{ |
| 23 | ext_workspace_group_handle_v1, ext_workspace_handle_v1, ext_workspace_manager_v1, |
| 24 | }; |
| 25 | use smithay::reexports::wayland_server::protocol::wl_output::WlOutput; |
| 26 | use smithay::reexports::wayland_server::{ |
| 27 | Client, DataInit, Dispatch, DisplayHandle, GlobalDispatch, New, Resource, |
| 28 | }; |
| 29 | use wayland_backend::server::ClientId; |
| 30 | |
| 31 | use crate::layout::monitor::Monitor; |
| 32 | use crate::layout::workspace::{Workspace, WorkspaceId}; |
| 33 | use crate::niri::State; |
| 34 | use crate::window::Mapped; |
| 35 | |
| 36 | const VERSION: u32 = 1; |
| 37 | |
| 38 | pub trait ExtWorkspaceHandler { |
| 39 | fn ext_workspace_manager_state(&mut self) -> &mut ExtWorkspaceManagerState; |
| 40 | fn activate_workspace(&mut self, id: WorkspaceId); |
| 41 | fn assign_workspace(&mut self, ws_id: WorkspaceId, output: Output); |
| 42 | } |
| 43 | |
| 44 | enum Action { |
| 45 | Assign(WorkspaceId, WeakOutput), |
| 46 | Activate(WorkspaceId), |
| 47 | } |
| 48 | |
| 49 | impl Action { |
| 50 | fn order(&self) -> u8 { |
| 51 | // First assign everything (move across outputs), then activate. |
| 52 | match self { |
| 53 | Action::Assign(_, _) => 0, |
| 54 | Action::Activate(_) => 1, |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | pub struct ExtWorkspaceManagerState { |
| 60 | display: DisplayHandle, |
| 61 | instances: HashMap<ExtWorkspaceManagerV1, Vec<Action>>, |
| 62 | workspace_groups: HashMap<Output, ExtWorkspaceGroupData>, |
| 63 | workspaces: HashMap<WorkspaceId, ExtWorkspaceData>, |
| 64 | } |
| 65 | |
| 66 | struct ExtWorkspaceGroupData { |
| 67 | instances: Vec<ExtWorkspaceGroupHandleV1>, |
| 68 | } |
| 69 | |
| 70 | struct ExtWorkspaceData { |
| 71 | // id cannot change once set. |
| 72 | id: Option<String>, |
| 73 | name: String, |
| 74 | coordinates: ArrayVec<u32, 2>, |
| 75 | state: ext_workspace_handle_v1::State, |
| 76 | instances: Vec<ExtWorkspaceHandleV1>, |
| 77 | output: Option<Output>, |
| 78 | } |
| 79 | |
| 80 | pub struct ExtWorkspaceGlobalData { |
| 81 | filter: Box<dyn for<'c> Fn(&'c Client) -> bool + Send + Sync>, |
| 82 | } |
| 83 | |
| 84 | pub fn refresh(state: &mut State) { |
| 85 | let _span = tracy_client::span!("ext_workspace::refresh"); |
| 86 | |
| 87 | let protocol_state = &mut state.niri.ext_workspace_state; |
| 88 | |
| 89 | let mut changed = false; |
| 90 | |
| 91 | // Remove workspaces that no longer exist (sending workspace_leave to workspace groups). |
| 92 | let mut seen_workspaces = HashMap::new(); |
| 93 | for (mon, _, ws) in state.niri.layout.workspaces() { |
| 94 | let output = mon.map(|mon| mon.output()); |
| 95 | seen_workspaces.insert(ws.id(), output); |
| 96 | } |
| 97 | |
| 98 | protocol_state.workspaces.retain(|id, workspace| { |
| 99 | if seen_workspaces.contains_key(id) { |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | remove_workspace_instances(&protocol_state.workspace_groups, workspace); |
| 104 | changed = true; |
| 105 | false |
| 106 | }); |
| 107 | |
| 108 | // Remove workspace groups for outputs that no longer exist. |
| 109 | protocol_state.workspace_groups.retain(|output, data| { |
| 110 | if state.niri.sorted_outputs.contains(output) { |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | for group in &data.instances { |
| 115 | // Send workspace_leave for all workspaces in this group with matching manager. |
| 116 | let manager: &ExtWorkspaceManagerV1 = group.data().unwrap(); |
| 117 | for ws in protocol_state.workspaces.values() { |
| 118 | if ws.output.as_ref() == Some(output) { |
| 119 | for workspace in &ws.instances { |
| 120 | if workspace.data() == Some(manager) { |
| 121 | group.workspace_leave(workspace); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | group.removed(); |
| 128 | } |
| 129 | |
| 130 | changed = true; |
| 131 | false |
| 132 | }); |
| 133 | |
| 134 | // Update existing workspaces and create new ones. |
| 135 | for (mon, ws_idx, ws) in state.niri.layout.workspaces() { |
| 136 | changed |= refresh_workspace(protocol_state, mon, ws_idx, ws); |
| 137 | } |
| 138 | |
| 139 | // Update workspace groups and create new ones, sending workspace_enter events as needed. |
| 140 | for output in &state.niri.sorted_outputs { |
| 141 | changed |= refresh_workspace_group(protocol_state, output); |
| 142 | } |
| 143 | |
| 144 | if changed { |
| 145 | for manager in protocol_state.instances.keys() { |
| 146 | manager.done(); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | pub fn on_output_bound(state: &mut State, output: &Output, wl_output: &WlOutput) { |
| 152 | let Some(client) = wl_output.client() else { |
| 153 | return; |
| 154 | }; |
| 155 | |
| 156 | let mut sent = false; |
| 157 | |
| 158 | let protocol_state = &mut state.niri.ext_workspace_state; |
| 159 | if let Some(data) = protocol_state.workspace_groups.get_mut(output) { |
| 160 | for group in &mut data.instances { |
| 161 | if group.client().as_ref() != Some(&client) { |
| 162 | continue; |
| 163 | } |
| 164 | |
| 165 | group.output_enter(wl_output); |
| 166 | sent = true; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | if !sent { |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | for manager in protocol_state.instances.keys() { |
| 175 | if manager.client().as_ref() == Some(&client) { |
| 176 | manager.done(); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | fn refresh_workspace_group(protocol_state: &mut ExtWorkspaceManagerState, output: &Output) -> bool { |
| 182 | if protocol_state.workspace_groups.contains_key(output) { |
| 183 | // Existing workspace group. Nothing can actually change since our workspace groups are tied |
| 184 | // to an output. |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | // New workspace group, start tracking it. |
| 189 | let mut data = ExtWorkspaceGroupData { |
| 190 | instances: Vec::new(), |
| 191 | }; |
| 192 | |
| 193 | // Create workspace group handle for each manager instance. |
| 194 | for manager in protocol_state.instances.keys() { |
| 195 | if let Some(client) = manager.client() { |
| 196 | data.add_instance::<State>(&protocol_state.display, &client, manager, output); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // Send workspace_enter for all existing workspaces on this output. |
| 201 | for group in &data.instances { |
| 202 | let manager: &ExtWorkspaceManagerV1 = group.data().unwrap(); |
| 203 | for (_, ws) in protocol_state.workspaces.iter() { |
| 204 | if ws.output.as_ref() != Some(output) { |
| 205 | continue; |
| 206 | } |
| 207 | for workspace in &ws.instances { |
| 208 | if workspace.data() == Some(manager) { |
| 209 | group.workspace_enter(workspace); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | protocol_state.workspace_groups.insert(output.clone(), data); |
| 216 | true |
| 217 | } |
| 218 | |
| 219 | fn send_workspace_enter_leave( |
| 220 | workspace_groups: &HashMap<Output, ExtWorkspaceGroupData>, |
| 221 | data: &ExtWorkspaceData, |
| 222 | enter: bool, |
| 223 | ) { |
| 224 | if let Some(output) = &data.output { |
| 225 | if let Some(group_data) = workspace_groups.get(output) { |
| 226 | for group in &group_data.instances { |
| 227 | let manager: &ExtWorkspaceManagerV1 = group.data().unwrap(); |
| 228 | for workspace in &data.instances { |
| 229 | if workspace.data() == Some(manager) { |
| 230 | if enter { |
| 231 | group.workspace_enter(workspace); |
| 232 | } else { |
| 233 | group.workspace_leave(workspace); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | fn remove_workspace_instances( |
| 243 | workspace_groups: &HashMap<Output, ExtWorkspaceGroupData>, |
| 244 | data: &ExtWorkspaceData, |
| 245 | ) { |
| 246 | send_workspace_enter_leave(workspace_groups, data, false); |
| 247 | |
| 248 | for workspace in &data.instances { |
| 249 | workspace.removed(); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | fn build_name(ws: &Workspace<Mapped>, ws_idx: usize) -> String { |
| 254 | ws.name().cloned().unwrap_or_else(|| { |
| 255 | // Add 1 since this is a human-readable name, and our action indexing is 1-based. |
| 256 | (ws_idx + 1).to_string() |
| 257 | }) |
| 258 | } |
| 259 | |
| 260 | fn refresh_workspace( |
| 261 | protocol_state: &mut ExtWorkspaceManagerState, |
| 262 | mon: Option<&Monitor<Mapped>>, |
| 263 | ws_idx: usize, |
| 264 | ws: &Workspace<Mapped>, |
| 265 | ) -> bool { |
| 266 | let mut state = ext_workspace_handle_v1::State::empty(); |
| 267 | if mon.is_some_and(|mon| mon.active_workspace_idx() == ws_idx) { |
| 268 | state |= ext_workspace_handle_v1::State::Active; |
| 269 | } |
| 270 | if ws.is_urgent() { |
| 271 | state |= ext_workspace_handle_v1::State::Urgent; |
| 272 | } |
| 273 | |
| 274 | let output = mon.map(|mon| mon.output()); |
| 275 | |
| 276 | match protocol_state.workspaces.entry(ws.id()) { |
| 277 | Entry::Occupied(entry) => { |
| 278 | // Existing workspace, check if anything changed. |
| 279 | let data = entry.into_mut(); |
| 280 | |
| 281 | let mut id_set = false; |
| 282 | let mut recreate = false; |
| 283 | let id = ws.name(); |
| 284 | if data.id.as_ref() != id { |
| 285 | if data.id.is_some() { |
| 286 | recreate = true; |
| 287 | } else { |
| 288 | id_set = true; |
| 289 | } |
| 290 | data.id = id.cloned(); |
| 291 | } |
| 292 | |
| 293 | let mut coordinates_changed = false; |
| 294 | if data.coordinates[1] != ws_idx as u32 { |
| 295 | data.coordinates[1] = ws_idx as u32; |
| 296 | coordinates_changed = true; |
| 297 | } |
| 298 | |
| 299 | let mut state_changed = false; |
| 300 | if data.state != state { |
| 301 | data.state = state; |
| 302 | state_changed = true; |
| 303 | } |
| 304 | |
| 305 | // Recreate means name got changed or unset (meaning data.name is back to ws_idx). |
| 306 | let check = recreate |
| 307 | || if data.id.is_some() { |
| 308 | // True means workspace got named, going from ws_idx to name. |
| 309 | id_set |
| 310 | } else { |
| 311 | // The workspace is unnamed, check if ws_idx changed. |
| 312 | coordinates_changed |
| 313 | }; |
| 314 | let mut name_changed = false; |
| 315 | if check { |
| 316 | let new_name = build_name(ws, ws_idx); |
| 317 | // This will likely be true, except if the workspace got named its index. |
| 318 | if data.name != new_name { |
| 319 | data.name = new_name; |
| 320 | name_changed = true; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | let mut output_changed = false; |
| 325 | if data.output.as_ref() != output { |
| 326 | send_workspace_enter_leave(&protocol_state.workspace_groups, data, false); |
| 327 | data.output = output.cloned(); |
| 328 | output_changed = true; |
| 329 | } |
| 330 | |
| 331 | if recreate { |
| 332 | remove_workspace_instances(&protocol_state.workspace_groups, data); |
| 333 | data.instances.clear(); |
| 334 | |
| 335 | for manager in protocol_state.instances.keys() { |
| 336 | if let Some(client) = manager.client() { |
| 337 | data.add_instance::<State>(&protocol_state.display, &client, manager); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | send_workspace_enter_leave(&protocol_state.workspace_groups, data, true); |
| 342 | return true; |
| 343 | } |
| 344 | |
| 345 | if output_changed { |
| 346 | // Send workspace_enter to the new output's group. If the group doesn't exist yet |
| 347 | // (new groups are created after refreshing workspaces), then workspace_enter() will |
| 348 | // be sent when the group is created. |
| 349 | send_workspace_enter_leave(&protocol_state.workspace_groups, data, true); |
| 350 | } |
| 351 | |
| 352 | let something_changed = id_set || name_changed || coordinates_changed || state_changed; |
| 353 | if something_changed { |
| 354 | for instance in &data.instances { |
| 355 | if id_set { |
| 356 | instance.id(data.id.clone().unwrap()); |
| 357 | } |
| 358 | if name_changed { |
| 359 | instance.name(data.name.clone()); |
| 360 | } |
| 361 | if coordinates_changed { |
| 362 | instance.coordinates( |
| 363 | data.coordinates |
| 364 | .iter() |
| 365 | .flat_map(|x| x.to_ne_bytes()) |
| 366 | .collect(), |
| 367 | ); |
| 368 | } |
| 369 | if state_changed { |
| 370 | instance.state(data.state); |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | output_changed || something_changed |
| 376 | } |
| 377 | Entry::Vacant(entry) => { |
| 378 | // New workspace, start tracking it. |
| 379 | let mut data = ExtWorkspaceData { |
| 380 | id: ws.name().cloned(), |
| 381 | name: build_name(ws, ws_idx), |
| 382 | coordinates: ArrayVec::from([0, ws_idx as u32]), |
| 383 | state, |
| 384 | instances: Vec::new(), |
| 385 | output: output.cloned(), |
| 386 | }; |
| 387 | |
| 388 | for manager in protocol_state.instances.keys() { |
| 389 | if let Some(client) = manager.client() { |
| 390 | data.add_instance::<State>(&protocol_state.display, &client, manager); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | send_workspace_enter_leave(&protocol_state.workspace_groups, &data, true); |
| 395 | entry.insert(data); |
| 396 | true |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | impl ExtWorkspaceGroupData { |
| 402 | fn add_instance<D>( |
| 403 | &mut self, |
| 404 | handle: &DisplayHandle, |
| 405 | client: &Client, |
| 406 | manager: &ExtWorkspaceManagerV1, |
| 407 | output: &Output, |
| 408 | ) -> &ExtWorkspaceGroupHandleV1 |
| 409 | where |
| 410 | D: Dispatch<ExtWorkspaceGroupHandleV1, ExtWorkspaceManagerV1>, |
| 411 | D: 'static, |
| 412 | { |
| 413 | let group = client |
| 414 | .create_resource::<ExtWorkspaceGroupHandleV1, _, D>( |
| 415 | handle, |
| 416 | manager.version(), |
| 417 | manager.clone(), |
| 418 | ) |
| 419 | .unwrap(); |
| 420 | manager.workspace_group(&group); |
| 421 | |
| 422 | group.capabilities(ext_workspace_group_handle_v1::GroupCapabilities::empty()); |
| 423 | |
| 424 | for wl_output in output.client_outputs(client) { |
| 425 | group.output_enter(&wl_output); |
| 426 | } |
| 427 | |
| 428 | self.instances.push(group); |
| 429 | self.instances.last().unwrap() |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | impl ExtWorkspaceData { |
| 434 | fn add_instance<D>( |
| 435 | &mut self, |
| 436 | handle: &DisplayHandle, |
| 437 | client: &Client, |
| 438 | manager: &ExtWorkspaceManagerV1, |
| 439 | ) -> &ExtWorkspaceHandleV1 |
| 440 | where |
| 441 | D: Dispatch<ExtWorkspaceHandleV1, ExtWorkspaceManagerV1>, |
| 442 | D: 'static, |
| 443 | { |
| 444 | let workspace = client |
| 445 | .create_resource::<ExtWorkspaceHandleV1, _, D>( |
| 446 | handle, |
| 447 | manager.version(), |
| 448 | manager.clone(), |
| 449 | ) |
| 450 | .unwrap(); |
| 451 | manager.workspace(&workspace); |
| 452 | |
| 453 | if let Some(id) = self.id.clone() { |
| 454 | workspace.id(id); |
| 455 | } |
| 456 | |
| 457 | workspace.name(self.name.clone()); |
| 458 | workspace.coordinates( |
| 459 | self.coordinates |
| 460 | .iter() |
| 461 | .flat_map(|x| x.to_ne_bytes()) |
| 462 | .collect(), |
| 463 | ); |
| 464 | workspace.state(self.state); |
| 465 | workspace.capabilities( |
| 466 | ext_workspace_handle_v1::WorkspaceCapabilities::Activate |
| 467 | | ext_workspace_handle_v1::WorkspaceCapabilities::Assign, |
| 468 | ); |
| 469 | |
| 470 | self.instances.push(workspace); |
| 471 | self.instances.last().unwrap() |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | impl ExtWorkspaceManagerState { |
| 476 | pub fn new<D, F>(display: &DisplayHandle, filter: F) -> Self |
| 477 | where |
| 478 | D: GlobalDispatch<ExtWorkspaceManagerV1, ExtWorkspaceGlobalData>, |
| 479 | D: Dispatch<ExtWorkspaceManagerV1, ()>, |
| 480 | D: 'static, |
| 481 | F: for<'c> Fn(&'c Client) -> bool + Send + Sync + 'static, |
| 482 | { |
| 483 | let global_data = ExtWorkspaceGlobalData { |
| 484 | filter: Box::new(filter), |
| 485 | }; |
| 486 | display.create_global::<D, ExtWorkspaceManagerV1, _>(VERSION, global_data); |
| 487 | Self { |
| 488 | display: display.clone(), |
| 489 | instances: HashMap::new(), |
| 490 | workspace_groups: HashMap::new(), |
| 491 | workspaces: HashMap::new(), |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | impl<D> GlobalDispatch<ExtWorkspaceManagerV1, ExtWorkspaceGlobalData, D> |
| 497 | for ExtWorkspaceManagerState |
| 498 | where |
| 499 | D: GlobalDispatch<ExtWorkspaceManagerV1, ExtWorkspaceGlobalData>, |
| 500 | D: Dispatch<ExtWorkspaceManagerV1, ()>, |
| 501 | D: Dispatch<ExtWorkspaceHandleV1, ExtWorkspaceManagerV1>, |
| 502 | D: ExtWorkspaceHandler, |
| 503 | { |
| 504 | fn bind( |
| 505 | state: &mut D, |
| 506 | handle: &DisplayHandle, |
| 507 | client: &Client, |
| 508 | resource: New<ExtWorkspaceManagerV1>, |
| 509 | _global_data: &ExtWorkspaceGlobalData, |
| 510 | data_init: &mut DataInit<'_, D>, |
| 511 | ) { |
| 512 | let manager = data_init.init(resource, ()); |
| 513 | |
| 514 | let state = state.ext_workspace_manager_state(); |
| 515 | |
| 516 | // Send existing workspaces to the new client. |
| 517 | let mut new_workspaces: HashMap<_, Vec<_>> = HashMap::new(); |
| 518 | for data in state.workspaces.values_mut() { |
| 519 | let output = data.output.clone(); |
| 520 | let workspace = data.add_instance::<State>(handle, client, &manager); |
| 521 | |
| 522 | if let Some(output) = output { |
| 523 | new_workspaces.entry(output).or_default().push(workspace); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | // Create workspace groups for all outputs. |
| 528 | for (output, group_data) in &mut state.workspace_groups { |
| 529 | let group = group_data.add_instance::<State>(handle, client, &manager, output); |
| 530 | |
| 531 | for workspace in new_workspaces.get(output).into_iter().flatten() { |
| 532 | group.workspace_enter(workspace); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | manager.done(); |
| 537 | state.instances.insert(manager, Vec::new()); |
| 538 | } |
| 539 | |
| 540 | fn can_view(client: Client, global_data: &ExtWorkspaceGlobalData) -> bool { |
| 541 | (global_data.filter)(&client) |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | impl<D> Dispatch<ExtWorkspaceManagerV1, (), D> for ExtWorkspaceManagerState |
| 546 | where |
| 547 | D: Dispatch<ExtWorkspaceManagerV1, ()>, |
| 548 | D: ExtWorkspaceHandler, |
| 549 | { |
| 550 | fn request( |
| 551 | state: &mut D, |
| 552 | _client: &Client, |
| 553 | resource: &ExtWorkspaceManagerV1, |
| 554 | request: <ExtWorkspaceManagerV1 as Resource>::Request, |
| 555 | _data: &(), |
| 556 | _dhandle: &DisplayHandle, |
| 557 | _data_init: &mut DataInit<'_, D>, |
| 558 | ) { |
| 559 | match request { |
| 560 | ext_workspace_manager_v1::Request::Commit => { |
| 561 | let protocol_state = state.ext_workspace_manager_state(); |
| 562 | let actions = protocol_state.instances.get_mut(resource).unwrap(); |
| 563 | let mut actions = mem::take(actions); |
| 564 | |
| 565 | actions.sort_by_key(Action::order); |
| 566 | |
| 567 | for action in actions { |
| 568 | match action { |
| 569 | Action::Assign(ws_id, output) => { |
| 570 | if let Some(output) = output.upgrade() { |
| 571 | state.assign_workspace(ws_id, output); |
| 572 | } |
| 573 | } |
| 574 | Action::Activate(id) => state.activate_workspace(id), |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | ext_workspace_manager_v1::Request::Stop => { |
| 579 | resource.finished(); |
| 580 | |
| 581 | let state = state.ext_workspace_manager_state(); |
| 582 | state.instances.retain(|x, _| x != resource); |
| 583 | |
| 584 | for data in state.workspace_groups.values_mut() { |
| 585 | data.instances |
| 586 | .retain(|instance| instance.data() != Some(resource)); |
| 587 | } |
| 588 | |
| 589 | for data in state.workspaces.values_mut() { |
| 590 | data.instances |
| 591 | .retain(|instance| instance.data() != Some(resource)); |
| 592 | } |
| 593 | } |
| 594 | _ => unreachable!(), |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | fn destroyed(state: &mut D, _client: ClientId, resource: &ExtWorkspaceManagerV1, _data: &()) { |
| 599 | let state = state.ext_workspace_manager_state(); |
| 600 | state.instances.retain(|x, _| x != resource); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | impl<D> Dispatch<ExtWorkspaceHandleV1, ExtWorkspaceManagerV1, D> for ExtWorkspaceManagerState |
| 605 | where |
| 606 | D: Dispatch<ExtWorkspaceHandleV1, ExtWorkspaceManagerV1>, |
| 607 | D: ExtWorkspaceHandler, |
| 608 | { |
| 609 | fn request( |
| 610 | state: &mut D, |
| 611 | _client: &Client, |
| 612 | resource: &ExtWorkspaceHandleV1, |
| 613 | request: <ExtWorkspaceHandleV1 as Resource>::Request, |
| 614 | data: &ExtWorkspaceManagerV1, |
| 615 | _dhandle: &DisplayHandle, |
| 616 | _data_init: &mut DataInit<'_, D>, |
| 617 | ) { |
| 618 | let protocol_state = state.ext_workspace_manager_state(); |
| 619 | |
| 620 | let Some((workspace, _)) = protocol_state |
| 621 | .workspaces |
| 622 | .iter() |
| 623 | .find(|(_, data)| data.instances.contains(resource)) |
| 624 | else { |
| 625 | return; |
| 626 | }; |
| 627 | let workspace = *workspace; |
| 628 | |
| 629 | match request { |
| 630 | ext_workspace_handle_v1::Request::Activate => { |
| 631 | let actions = protocol_state.instances.get_mut(data).unwrap(); |
| 632 | actions.push(Action::Activate(workspace)); |
| 633 | } |
| 634 | ext_workspace_handle_v1::Request::Deactivate => (), |
| 635 | ext_workspace_handle_v1::Request::Assign { workspace_group } => { |
| 636 | if let Some(output) = protocol_state |
| 637 | .workspace_groups |
| 638 | .iter() |
| 639 | .find(|(_, data)| data.instances.contains(&workspace_group)) |
| 640 | .map(|(output, _)| output.clone()) |
| 641 | { |
| 642 | let actions = protocol_state.instances.get_mut(data).unwrap(); |
| 643 | actions.push(Action::Assign(workspace, output.downgrade())); |
| 644 | } |
| 645 | } |
| 646 | ext_workspace_handle_v1::Request::Remove => (), |
| 647 | ext_workspace_handle_v1::Request::Destroy => (), |
| 648 | _ => unreachable!(), |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | fn destroyed( |
| 653 | state: &mut D, |
| 654 | _client: ClientId, |
| 655 | resource: &ExtWorkspaceHandleV1, |
| 656 | _data: &ExtWorkspaceManagerV1, |
| 657 | ) { |
| 658 | let state = state.ext_workspace_manager_state(); |
| 659 | for data in state.workspaces.values_mut() { |
| 660 | data.instances.retain(|instance| instance != resource); |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | impl<D> Dispatch<ExtWorkspaceGroupHandleV1, ExtWorkspaceManagerV1, D> for ExtWorkspaceManagerState |
| 666 | where |
| 667 | D: Dispatch<ExtWorkspaceGroupHandleV1, ExtWorkspaceManagerV1>, |
| 668 | D: ExtWorkspaceHandler, |
| 669 | { |
| 670 | fn request( |
| 671 | _state: &mut D, |
| 672 | _client: &Client, |
| 673 | _resource: &ExtWorkspaceGroupHandleV1, |
| 674 | request: <ExtWorkspaceGroupHandleV1 as Resource>::Request, |
| 675 | _data: &ExtWorkspaceManagerV1, |
| 676 | _dhandle: &DisplayHandle, |
| 677 | _data_init: &mut DataInit<'_, D>, |
| 678 | ) { |
| 679 | match request { |
| 680 | ext_workspace_group_handle_v1::Request::CreateWorkspace { .. } => (), |
| 681 | ext_workspace_group_handle_v1::Request::Destroy => (), |
| 682 | _ => unreachable!(), |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | fn destroyed( |
| 687 | state: &mut D, |
| 688 | _client: ClientId, |
| 689 | resource: &ExtWorkspaceGroupHandleV1, |
| 690 | _data: &ExtWorkspaceManagerV1, |
| 691 | ) { |
| 692 | let state = state.ext_workspace_manager_state(); |
| 693 | for data in state.workspace_groups.values_mut() { |
| 694 | data.instances.retain(|instance| instance != resource); |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | #[macro_export] |
| 700 | macro_rules! delegate_ext_workspace { |
| 701 | ($(@<$( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+>)? $ty: ty) => { |
| 702 | smithay::reexports::wayland_server::delegate_global_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ |
| 703 | smithay::reexports::wayland_protocols::ext::workspace::v1::server::ext_workspace_manager_v1::ExtWorkspaceManagerV1: $crate::protocols::ext_workspace::ExtWorkspaceGlobalData |
| 704 | ] => $crate::protocols::ext_workspace::ExtWorkspaceManagerState); |
| 705 | smithay::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ |
| 706 | smithay::reexports::wayland_protocols::ext::workspace::v1::server::ext_workspace_manager_v1::ExtWorkspaceManagerV1: () |
| 707 | ] => $crate::protocols::ext_workspace::ExtWorkspaceManagerState); |
| 708 | smithay::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ |
| 709 | smithay::reexports::wayland_protocols::ext::workspace::v1::server::ext_workspace_handle_v1::ExtWorkspaceHandleV1: smithay::reexports::wayland_protocols::ext::workspace::v1::server::ext_workspace_manager_v1::ExtWorkspaceManagerV1 |
| 710 | ] => $crate::protocols::ext_workspace::ExtWorkspaceManagerState); |
| 711 | smithay::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ |
| 712 | smithay::reexports::wayland_protocols::ext::workspace::v1::server::ext_workspace_group_handle_v1::ExtWorkspaceGroupHandleV1: smithay::reexports::wayland_protocols::ext::workspace::v1::server::ext_workspace_manager_v1::ExtWorkspaceManagerV1 |
| 713 | ] => $crate::protocols::ext_workspace::ExtWorkspaceManagerState); |
| 714 | }; |
| 715 | } |