Add with_toplevel_role() util function
Browse files at this commit86bdc6898bc8a13368ba852692ff0cc248345128Parent: e5ca3356 changed files +47 -94
- src/ipc/server.rs +8 -28
- src/niri.rs +2 -15
- src/protocols/foreign_toplevel.rs +10 -26
- src/utils/mod.rs +20 -1
- src/window/mapped.rs +3 -10
- src/window/mod.rs +4 -14
src/ipc/server.rsmodified+8 -28
| @@ -19,13 +19,11 @@ use niri_ipc::{Event, KeyboardLayouts, OutputConfigChanged, Reply, Request, Resp | |||
| 19 | use smithay::reexports::calloop::generic::Generic; | 19 | use smithay::reexports::calloop::generic::Generic; |
| 20 | use smithay::reexports::calloop::{Interest, LoopHandle, Mode, PostAction}; | 20 | use smithay::reexports::calloop::{Interest, LoopHandle, Mode, PostAction}; |
| 21 | use smithay::reexports::rustix::fs::unlink; | 21 | use smithay::reexports::rustix::fs::unlink; |
| 22 | -use smithay::wayland::compositor::with_states; | ||
| 23 | -use smithay::wayland::shell::xdg::XdgToplevelSurfaceData; | ||
| 24 | 22 | ||
| 25 | use crate::backend::IpcOutputMap; | 23 | use crate::backend::IpcOutputMap; |
| 26 | use crate::layout::workspace::WorkspaceId; | 24 | use crate::layout::workspace::WorkspaceId; |
| 27 | use crate::niri::State; | 25 | use crate::niri::State; |
| 28 | -use crate::utils::version; | 26 | +use crate::utils::{version, with_toplevel_role}; |
| 29 | use crate::window::Mapped; | 27 | use crate::window::Mapped; |
| 30 | 28 | ||
| 31 | // If an event stream client fails to read events fast enough that we accumulate more than this | 29 | // If an event stream client fails to read events fast enough that we accumulate more than this |
| @@ -361,22 +359,12 @@ async fn handle_event_stream_client(client: EventStreamClient) -> anyhow::Result | |||
| 361 | } | 359 | } |
| 362 | 360 | ||
| 363 | fn make_ipc_window(mapped: &Mapped, workspace_id: Option<WorkspaceId>) -> niri_ipc::Window { | 361 | fn make_ipc_window(mapped: &Mapped, workspace_id: Option<WorkspaceId>) -> niri_ipc::Window { |
| 364 | - let wl_surface = mapped.toplevel().wl_surface(); | 362 | + with_toplevel_role(mapped.toplevel(), |role| niri_ipc::Window { |
| 365 | - with_states(wl_surface, |states| { | 363 | + id: mapped.id().get(), |
| 366 | - let role = states | 364 | + title: role.title.clone(), |
| 367 | - .data_map | 365 | + app_id: role.app_id.clone(), |
| 368 | - .get::<XdgToplevelSurfaceData>() | 366 | + workspace_id: workspace_id.map(|id| id.get()), |
| 369 | - .unwrap() | 367 | + is_focused: mapped.is_focused(), |
| 370 | - .lock() | ||
| 371 | - .unwrap(); | ||
| 372 | - | ||
| 373 | - niri_ipc::Window { | ||
| 374 | - id: mapped.id().get(), | ||
| 375 | - title: role.title.clone(), | ||
| 376 | - app_id: role.app_id.clone(), | ||
| 377 | - workspace_id: workspace_id.map(|id| id.get()), | ||
| 378 | - is_focused: mapped.is_focused(), | ||
| 379 | - } | ||
| 380 | }) | 368 | }) |
| 381 | } | 369 | } |
| 382 | 370 | ||
| @@ -559,15 +547,7 @@ impl State { | |||
| 559 | let workspace_id = ws_id.map(|id| id.get()); | 547 | let workspace_id = ws_id.map(|id| id.get()); |
| 560 | let mut changed = ipc_win.workspace_id != workspace_id; | 548 | let mut changed = ipc_win.workspace_id != workspace_id; |
| 561 | 549 | ||
| 562 | - let wl_surface = mapped.toplevel().wl_surface(); | 550 | + changed |= with_toplevel_role(mapped.toplevel(), |role| { |
| 563 | - changed |= with_states(wl_surface, |states| { | ||
| 564 | - let role = states | ||
| 565 | - .data_map | ||
| 566 | - .get::<XdgToplevelSurfaceData>() | ||
| 567 | - .unwrap() | ||
| 568 | - .lock() | ||
| 569 | - .unwrap(); | ||
| 570 | - | ||
| 571 | ipc_win.title != role.title || ipc_win.app_id != role.app_id | 551 | ipc_win.title != role.title || ipc_win.app_id != role.app_id |
| 572 | }); | 552 | }); |
| 573 | 553 | ||
src/niri.rsmodified+2 -15
| @@ -1541,7 +1541,7 @@ impl State { | |||
| 1541 | to_introspect: &async_channel::Sender<NiriToIntrospect>, | 1541 | to_introspect: &async_channel::Sender<NiriToIntrospect>, |
| 1542 | msg: IntrospectToNiri, | 1542 | msg: IntrospectToNiri, |
| 1543 | ) { | 1543 | ) { |
| 1544 | - use smithay::wayland::shell::xdg::XdgToplevelSurfaceData; | 1544 | + use crate::utils::with_toplevel_role; |
| 1545 | 1545 | ||
| 1546 | let IntrospectToNiri::GetWindows = msg; | 1546 | let IntrospectToNiri::GetWindows = msg; |
| 1547 | let _span = tracy_client::span!("GetWindows"); | 1547 | let _span = tracy_client::span!("GetWindows"); |
| @@ -1549,21 +1549,8 @@ impl State { | |||
| 1549 | let mut windows = HashMap::new(); | 1549 | let mut windows = HashMap::new(); |
| 1550 | 1550 | ||
| 1551 | self.niri.layout.with_windows(|mapped, _, _| { | 1551 | self.niri.layout.with_windows(|mapped, _, _| { |
| 1552 | - let wl_surface = mapped | ||
| 1553 | - .window | ||
| 1554 | - .toplevel() | ||
| 1555 | - .expect("no X11 support") | ||
| 1556 | - .wl_surface(); | ||
| 1557 | - | ||
| 1558 | let id = mapped.id().get(); | 1552 | let id = mapped.id().get(); |
| 1559 | - let props = with_states(wl_surface, |states| { | 1553 | + let props = with_toplevel_role(mapped.toplevel(), |role| { |
| 1560 | - let role = states | ||
| 1561 | - .data_map | ||
| 1562 | - .get::<XdgToplevelSurfaceData>() | ||
| 1563 | - .unwrap() | ||
| 1564 | - .lock() | ||
| 1565 | - .unwrap(); | ||
| 1566 | - | ||
| 1567 | gnome_shell_introspect::WindowProperties { | 1554 | gnome_shell_introspect::WindowProperties { |
| 1568 | title: role.title.clone().unwrap_or_default(), | 1555 | title: role.title.clone().unwrap_or_default(), |
| 1569 | app_id: role.app_id.clone().unwrap_or_default(), | 1556 | app_id: role.app_id.clone().unwrap_or_default(), |
src/protocols/foreign_toplevel.rsmodified+10 -26
| @@ -11,10 +11,7 @@ use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; | |||
| 11 | use smithay::reexports::wayland_server::{ | 11 | use smithay::reexports::wayland_server::{ |
| 12 | Client, DataInit, Dispatch, DisplayHandle, GlobalDispatch, New, Resource, | 12 | Client, DataInit, Dispatch, DisplayHandle, GlobalDispatch, New, Resource, |
| 13 | }; | 13 | }; |
| 14 | -use smithay::wayland::compositor::with_states; | 14 | +use smithay::wayland::shell::xdg::{ToplevelStateSet, XdgToplevelSurfaceRoleAttributes}; |
| 15 | -use smithay::wayland::shell::xdg::{ | ||
| 16 | - ToplevelStateSet, XdgToplevelSurfaceData, XdgToplevelSurfaceRoleAttributes, | ||
| 17 | -}; | ||
| 18 | use wayland_protocols_wlr::foreign_toplevel::v1::server::{ | 15 | use wayland_protocols_wlr::foreign_toplevel::v1::server::{ |
| 19 | zwlr_foreign_toplevel_handle_v1, zwlr_foreign_toplevel_manager_v1, | 16 | zwlr_foreign_toplevel_handle_v1, zwlr_foreign_toplevel_manager_v1, |
| 20 | }; | 17 | }; |
| @@ -22,6 +19,7 @@ use zwlr_foreign_toplevel_handle_v1::ZwlrForeignToplevelHandleV1; | |||
| 22 | use zwlr_foreign_toplevel_manager_v1::ZwlrForeignToplevelManagerV1; | 19 | use zwlr_foreign_toplevel_manager_v1::ZwlrForeignToplevelManagerV1; |
| 23 | 20 | ||
| 24 | use crate::niri::State; | 21 | use crate::niri::State; |
| 22 | +use crate::utils::with_toplevel_role; | ||
| 25 | 23 | ||
| 26 | const VERSION: u32 = 3; | 24 | const VERSION: u32 = 3; |
| 27 | 25 | ||
| @@ -96,37 +94,23 @@ pub fn refresh(state: &mut State) { | |||
| 96 | // the previous window and only then activate the newly focused window. | 94 | // the previous window and only then activate the newly focused window. |
| 97 | let mut focused = None; | 95 | let mut focused = None; |
| 98 | state.niri.layout.with_windows(|mapped, output, _| { | 96 | state.niri.layout.with_windows(|mapped, output, _| { |
| 99 | - let wl_surface = mapped.toplevel().wl_surface(); | 97 | + let toplevel = mapped.toplevel(); |
| 100 | - | 98 | + let wl_surface = toplevel.wl_surface(); |
| 101 | - with_states(wl_surface, |states| { | 99 | + with_toplevel_role(toplevel, |role| { |
| 102 | - let role = states | ||
| 103 | - .data_map | ||
| 104 | - .get::<XdgToplevelSurfaceData>() | ||
| 105 | - .unwrap() | ||
| 106 | - .lock() | ||
| 107 | - .unwrap(); | ||
| 108 | - | ||
| 109 | if state.niri.keyboard_focus.surface() == Some(wl_surface) { | 100 | if state.niri.keyboard_focus.surface() == Some(wl_surface) { |
| 110 | focused = Some((mapped.window.clone(), output.cloned())); | 101 | focused = Some((mapped.window.clone(), output.cloned())); |
| 111 | } else { | 102 | } else { |
| 112 | - refresh_toplevel(protocol_state, wl_surface, &role, output, false); | 103 | + refresh_toplevel(protocol_state, wl_surface, role, output, false); |
| 113 | } | 104 | } |
| 114 | }); | 105 | }); |
| 115 | }); | 106 | }); |
| 116 | 107 | ||
| 117 | // Finally, refresh the focused window. | 108 | // Finally, refresh the focused window. |
| 118 | if let Some((window, output)) = focused { | 109 | if let Some((window, output)) = focused { |
| 119 | - let wl_surface = window.toplevel().expect("no x11 support").wl_surface(); | 110 | + let toplevel = window.toplevel().expect("no X11 support"); |
| 120 | - | 111 | + let wl_surface = toplevel.wl_surface(); |
| 121 | - with_states(wl_surface, |states| { | 112 | + with_toplevel_role(toplevel, |role| { |
| 122 | - let role = states | 113 | + refresh_toplevel(protocol_state, wl_surface, role, output.as_ref(), true); |
| 123 | - .data_map | ||
| 124 | - .get::<XdgToplevelSurfaceData>() | ||
| 125 | - .unwrap() | ||
| 126 | - .lock() | ||
| 127 | - .unwrap(); | ||
| 128 | - | ||
| 129 | - refresh_toplevel(protocol_state, wl_surface, &role, output.as_ref(), true); | ||
| 130 | }); | 114 | }); |
| 131 | } | 115 | } |
| 132 | } | 116 | } |
src/utils/mod.rsmodified+20 -1
| @@ -17,8 +17,11 @@ use smithay::reexports::rustix::time::{clock_gettime, ClockId}; | |||
| 17 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; | 17 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; |
| 18 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; | 18 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; |
| 19 | use smithay::utils::{Coordinate, Logical, Point, Rectangle, Size, Transform}; | 19 | use smithay::utils::{Coordinate, Logical, Point, Rectangle, Size, Transform}; |
| 20 | -use smithay::wayland::compositor::{send_surface_state, SurfaceData}; | 20 | +use smithay::wayland::compositor::{send_surface_state, with_states, SurfaceData}; |
| 21 | use smithay::wayland::fractional_scale::with_fractional_scale; | 21 | use smithay::wayland::fractional_scale::with_fractional_scale; |
| 22 | +use smithay::wayland::shell::xdg::{ | ||
| 23 | + ToplevelSurface, XdgToplevelSurfaceData, XdgToplevelSurfaceRoleAttributes, | ||
| 24 | +}; | ||
| 22 | 25 | ||
| 23 | pub mod id; | 26 | pub mod id; |
| 24 | pub mod scale; | 27 | pub mod scale; |
| @@ -221,6 +224,22 @@ pub fn output_matches_name(output: &Output, target: &str) -> bool { | |||
| 221 | name.matches(target) | 224 | name.matches(target) |
| 222 | } | 225 | } |
| 223 | 226 | ||
| 227 | +pub fn with_toplevel_role<T>( | ||
| 228 | + toplevel: &ToplevelSurface, | ||
| 229 | + f: impl FnOnce(&mut XdgToplevelSurfaceRoleAttributes) -> T, | ||
| 230 | +) -> T { | ||
| 231 | + with_states(toplevel.wl_surface(), |states| { | ||
| 232 | + let mut role = states | ||
| 233 | + .data_map | ||
| 234 | + .get::<XdgToplevelSurfaceData>() | ||
| 235 | + .unwrap() | ||
| 236 | + .lock() | ||
| 237 | + .unwrap(); | ||
| 238 | + | ||
| 239 | + f(&mut role) | ||
| 240 | + }) | ||
| 241 | +} | ||
| 242 | + | ||
| 224 | #[cfg(feature = "dbus")] | 243 | #[cfg(feature = "dbus")] |
| 225 | pub fn show_screenshot_notification(image_path: Option<PathBuf>) { | 244 | pub fn show_screenshot_notification(image_path: Option<PathBuf>) { |
| 226 | let mut notification = notify_rust::Notification::new(); | 245 | let mut notification = notify_rust::Notification::new(); |
src/window/mapped.rsmodified+3 -10
| @@ -15,7 +15,7 @@ use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; | |||
| 15 | use smithay::reexports::wayland_server::Resource as _; | 15 | use smithay::reexports::wayland_server::Resource as _; |
| 16 | use smithay::utils::{Logical, Point, Rectangle, Scale, Serial, Size, Transform}; | 16 | use smithay::utils::{Logical, Point, Rectangle, Scale, Serial, Size, Transform}; |
| 17 | use smithay::wayland::compositor::{remove_pre_commit_hook, with_states, HookId}; | 17 | use smithay::wayland::compositor::{remove_pre_commit_hook, with_states, HookId}; |
| 18 | -use smithay::wayland::shell::xdg::{SurfaceCachedState, ToplevelSurface, XdgToplevelSurfaceData}; | 18 | +use smithay::wayland::shell::xdg::{SurfaceCachedState, ToplevelSurface}; |
| 19 | 19 | ||
| 20 | use super::{ResolvedWindowRules, WindowRef}; | 20 | use super::{ResolvedWindowRules, WindowRef}; |
| 21 | use crate::handlers::KdeDecorationsModeState; | 21 | use crate::handlers::KdeDecorationsModeState; |
| @@ -33,7 +33,7 @@ use crate::render_helpers::surface::render_snapshot_from_surface_tree; | |||
| 33 | use crate::render_helpers::{BakedBuffer, RenderTarget, SplitElements}; | 33 | use crate::render_helpers::{BakedBuffer, RenderTarget, SplitElements}; |
| 34 | use crate::utils::id::IdCounter; | 34 | use crate::utils::id::IdCounter; |
| 35 | use crate::utils::transaction::Transaction; | 35 | use crate::utils::transaction::Transaction; |
| 36 | -use crate::utils::{send_scale_transform, ResizeEdge}; | 36 | +use crate::utils::{send_scale_transform, with_toplevel_role, ResizeEdge}; |
| 37 | 37 | ||
| 38 | #[derive(Debug)] | 38 | #[derive(Debug)] |
| 39 | pub struct Mapped { | 39 | pub struct Mapped { |
| @@ -631,14 +631,7 @@ impl LayoutElement for Mapped { | |||
| 631 | let _span = | 631 | let _span = |
| 632 | trace_span!("configure_intent", surface = ?self.toplevel().wl_surface().id()).entered(); | 632 | trace_span!("configure_intent", surface = ?self.toplevel().wl_surface().id()).entered(); |
| 633 | 633 | ||
| 634 | - with_states(self.toplevel().wl_surface(), |states| { | 634 | + with_toplevel_role(self.toplevel(), |attributes| { |
| 635 | - let attributes = states | ||
| 636 | - .data_map | ||
| 637 | - .get::<XdgToplevelSurfaceData>() | ||
| 638 | - .unwrap() | ||
| 639 | - .lock() | ||
| 640 | - .unwrap(); | ||
| 641 | - | ||
| 642 | if let Some(server_pending) = &attributes.server_pending { | 635 | if let Some(server_pending) = &attributes.server_pending { |
| 643 | let current_server = attributes.current_server_state(); | 636 | let current_server = attributes.current_server_state(); |
| 644 | if server_pending != current_server { | 637 | if server_pending != current_server { |
src/window/mod.rsmodified+4 -14
| @@ -1,11 +1,9 @@ | |||
| 1 | use niri_config::{BlockOutFrom, BorderRule, CornerRadius, Match, WindowRule}; | 1 | use niri_config::{BlockOutFrom, BorderRule, CornerRadius, Match, WindowRule}; |
| 2 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; | 2 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; |
| 3 | -use smithay::wayland::compositor::with_states; | 3 | +use smithay::wayland::shell::xdg::{ToplevelSurface, XdgToplevelSurfaceRoleAttributes}; |
| 4 | -use smithay::wayland::shell::xdg::{ | ||
| 5 | - ToplevelSurface, XdgToplevelSurfaceData, XdgToplevelSurfaceRoleAttributes, | ||
| 6 | -}; | ||
| 7 | 4 | ||
| 8 | use crate::layout::workspace::ColumnWidth; | 5 | use crate::layout::workspace::ColumnWidth; |
| 6 | +use crate::utils::with_toplevel_role; | ||
| 9 | 7 | ||
| 10 | pub mod mapped; | 8 | pub mod mapped; |
| 11 | pub use mapped::Mapped; | 9 | pub use mapped::Mapped; |
| @@ -144,15 +142,7 @@ impl ResolvedWindowRules { | |||
| 144 | 142 | ||
| 145 | let mut resolved = ResolvedWindowRules::empty(); | 143 | let mut resolved = ResolvedWindowRules::empty(); |
| 146 | 144 | ||
| 147 | - let toplevel = window.toplevel(); | 145 | + with_toplevel_role(window.toplevel(), |role| { |
| 148 | - with_states(toplevel.wl_surface(), |states| { | ||
| 149 | - let mut role = states | ||
| 150 | - .data_map | ||
| 151 | - .get::<XdgToplevelSurfaceData>() | ||
| 152 | - .unwrap() | ||
| 153 | - .lock() | ||
| 154 | - .unwrap(); | ||
| 155 | - | ||
| 156 | // Ensure server_pending like in Smithay's with_pending_state(). | 146 | // Ensure server_pending like in Smithay's with_pending_state(). |
| 157 | if role.server_pending.is_none() { | 147 | if role.server_pending.is_none() { |
| 158 | role.server_pending = Some(role.current_server_state().clone()); | 148 | role.server_pending = Some(role.current_server_state().clone()); |
| @@ -169,7 +159,7 @@ impl ResolvedWindowRules { | |||
| 169 | } | 159 | } |
| 170 | } | 160 | } |
| 171 | 161 | ||
| 172 | - window_matches(window, &role, m) | 162 | + window_matches(window, role, m) |
| 173 | }; | 163 | }; |
| 174 | 164 | ||
| 175 | if !(rule.matches.is_empty() || rule.matches.iter().any(matches)) { | 165 | if !(rule.matches.is_empty() || rule.matches.iter().any(matches)) { |