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 | pub mod background_effect; |
| 2 | mod compositor; |
| 3 | pub mod image_copy_capture; |
| 4 | mod layer_shell; |
| 5 | pub mod virtual_keyboard; |
| 6 | mod xdg_shell; |
| 7 | |
| 8 | use std::fs::File; |
| 9 | use std::io::Write; |
| 10 | use std::os::fd::OwnedFd; |
| 11 | use std::sync::Arc; |
| 12 | use std::thread; |
| 13 | use std::time::Duration; |
| 14 | |
| 15 | use smithay::backend::allocator::dmabuf::Dmabuf; |
| 16 | use smithay::backend::drm::DrmNode; |
| 17 | use smithay::backend::input::{InputEvent, TabletToolDescriptor}; |
| 18 | use smithay::desktop::{PopupKind, PopupManager}; |
| 19 | use smithay::input::dnd::{self, DnDGrab, DndGrabHandler, DndTarget}; |
| 20 | use smithay::input::pointer::{self, CursorIcon, CursorImageStatus, Focus, PointerHandle}; |
| 21 | use smithay::input::tablet::TabletSeatHandler; |
| 22 | use smithay::input::{keyboard, Seat, SeatHandler, SeatState}; |
| 23 | use smithay::output::Output; |
| 24 | use smithay::reexports::rustix::fs::{fcntl_setfl, OFlags}; |
| 25 | use smithay::reexports::wayland_protocols_wlr::screencopy::v1::server::zwlr_screencopy_manager_v1::ZwlrScreencopyManagerV1; |
| 26 | use smithay::reexports::wayland_server::protocol::wl_output::WlOutput; |
| 27 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; |
| 28 | use smithay::reexports::wayland_server::Resource; |
| 29 | use smithay::utils::{Logical, Point, Rectangle, Serial}; |
| 30 | use smithay::wayland::compositor::{get_parent, with_states}; |
| 31 | use smithay::wayland::dmabuf::{DmabufGlobal, DmabufHandler, DmabufState, ImportNotifier}; |
| 32 | use smithay::wayland::drm_lease::{ |
| 33 | DrmLease, DrmLeaseBuilder, DrmLeaseHandler, DrmLeaseRequest, DrmLeaseState, LeaseRejected, |
| 34 | }; |
| 35 | use smithay::wayland::fractional_scale::FractionalScaleHandler; |
| 36 | use smithay::wayland::idle_inhibit::IdleInhibitHandler; |
| 37 | use smithay::wayland::idle_notify::{IdleNotifierHandler, IdleNotifierState}; |
| 38 | use smithay::wayland::input_method::{InputMethodHandler, PopupSurface}; |
| 39 | use smithay::wayland::keyboard_shortcuts_inhibit::{ |
| 40 | KeyboardShortcutsInhibitHandler, KeyboardShortcutsInhibitState, KeyboardShortcutsInhibitor, |
| 41 | }; |
| 42 | use smithay::wayland::output::OutputHandler; |
| 43 | use smithay::wayland::pointer_constraints::{ |
| 44 | with_pointer_constraint, ConstraintRemove, PointerConstraintsHandler, |
| 45 | }; |
| 46 | use smithay::wayland::security_context::{ |
| 47 | SecurityContext, SecurityContextHandler, SecurityContextListenerSource, |
| 48 | }; |
| 49 | use smithay::wayland::selection::data_device::{ |
| 50 | set_data_device_focus, DataDeviceHandler, DataDeviceState, WaylandDndGrabHandler, |
| 51 | }; |
| 52 | use smithay::wayland::selection::ext_data_control::{ |
| 53 | DataControlHandler as ExtDataControlHandler, DataControlState as ExtDataControlState, |
| 54 | }; |
| 55 | use smithay::wayland::selection::primary_selection::{ |
| 56 | set_primary_focus, PrimarySelectionHandler, PrimarySelectionState, |
| 57 | }; |
| 58 | use smithay::wayland::selection::wlr_data_control::{ |
| 59 | DataControlHandler as WlrDataControlHandler, DataControlState as WlrDataControlState, |
| 60 | }; |
| 61 | use smithay::wayland::selection::{SelectionHandler, SelectionTarget}; |
| 62 | use smithay::wayland::session_lock::{ |
| 63 | LockSurface, SessionLockHandler, SessionLockManagerState, SessionLocker, |
| 64 | }; |
| 65 | use smithay::wayland::text_input::TextInputActivation; |
| 66 | use smithay::wayland::virtual_pointer::{VirtualPointerBackend, VirtualPointerHandler}; |
| 67 | use smithay::wayland::xdg_activation::{ |
| 68 | XdgActivationHandler, XdgActivationState, XdgActivationToken, XdgActivationTokenData, |
| 69 | }; |
| 70 | |
| 71 | pub use crate::handlers::xdg_shell::KdeDecorationsModeState; |
| 72 | use crate::input::click_grab::ClickGrab; |
| 73 | use crate::layout::workspace::WorkspaceId; |
| 74 | use crate::layout::{ActivateWindow, LayoutElement}; |
| 75 | use crate::niri::{DndIcon, NewClient, State}; |
| 76 | use crate::protocols::ext_workspace::{self, ExtWorkspaceHandler, ExtWorkspaceManagerState}; |
| 77 | use crate::protocols::foreign_toplevel::{ |
| 78 | self, ForeignToplevelHandler, ForeignToplevelManagerState, |
| 79 | }; |
| 80 | use crate::protocols::gamma_control::{GammaControlHandler, GammaControlManagerState}; |
| 81 | use crate::protocols::mutter_x11_interop::MutterX11InteropHandler; |
| 82 | use crate::protocols::output_management::{OutputManagementHandler, OutputManagementManagerState}; |
| 83 | use crate::protocols::screencopy::{Screencopy, ScreencopyHandler, ScreencopyManagerState}; |
| 84 | use crate::utils::{output_size, send_scale_transform}; |
| 85 | |
| 86 | pub const XDG_ACTIVATION_TOKEN_TIMEOUT: Duration = Duration::from_secs(10); |
| 87 | |
| 88 | impl SeatHandler for State { |
| 89 | type KeyboardFocus = WlSurface; |
| 90 | type PointerFocus = WlSurface; |
| 91 | type TouchFocus = WlSurface; |
| 92 | |
| 93 | fn seat_state(&mut self) -> &mut SeatState<State> { |
| 94 | &mut self.niri.seat_state |
| 95 | } |
| 96 | |
| 97 | fn cursor_image(&mut self, _seat: &Seat<Self>, mut image: CursorImageStatus) { |
| 98 | // FIXME: this hack should be removable once the screenshot UI is tracked with a |
| 99 | // PointerFocus properly. |
| 100 | if self.niri.screenshot_ui.is_open() { |
| 101 | image = CursorImageStatus::Named(CursorIcon::Crosshair); |
| 102 | } |
| 103 | self.niri.cursor_manager.set_cursor_image(image); |
| 104 | // FIXME: more granular |
| 105 | self.niri.queue_redraw_all(); |
| 106 | } |
| 107 | |
| 108 | fn focus_changed(&mut self, seat: &Seat<Self>, focused: Option<&WlSurface>) { |
| 109 | let dh = &self.niri.display_handle; |
| 110 | let client = focused.and_then(|s| dh.get_client(s.id()).ok()); |
| 111 | set_data_device_focus(dh, seat, client.clone()); |
| 112 | set_primary_focus(dh, seat, client); |
| 113 | } |
| 114 | |
| 115 | fn led_state_changed(&mut self, _seat: &Seat<Self>, led_state: keyboard::LedState) { |
| 116 | self.update_keyboard_leds(led_state); |
| 117 | } |
| 118 | |
| 119 | fn click_grab( |
| 120 | &mut self, |
| 121 | start_data: pointer::GrabStartData<Self>, |
| 122 | ) -> impl pointer::PointerGrab<Self> { |
| 123 | ClickGrab::new(start_data) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | impl TabletSeatHandler for State { |
| 128 | type ToolFocus = WlSurface; |
| 129 | |
| 130 | fn tablet_tool_image(&mut self, _tool: &TabletToolDescriptor, image: CursorImageStatus) { |
| 131 | // FIXME: tablet tools should have their own cursors. |
| 132 | self.niri.cursor_manager.set_cursor_image(image); |
| 133 | // FIXME: granular. |
| 134 | self.niri.queue_redraw_all(); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | impl PointerConstraintsHandler for State { |
| 139 | fn new_constraint(&mut self, _surface: &WlSurface, _pointer: &PointerHandle<Self>) { |
| 140 | // Pointer constraints track pointer focus internally, so make sure it's up to date before |
| 141 | // activating a new one. |
| 142 | self.refresh_pointer_contents(); |
| 143 | |
| 144 | self.niri.maybe_activate_pointer_constraint(); |
| 145 | } |
| 146 | |
| 147 | fn cursor_position_hint( |
| 148 | &mut self, |
| 149 | surface: &WlSurface, |
| 150 | pointer: &PointerHandle<Self>, |
| 151 | location: Point<f64, Logical>, |
| 152 | ) { |
| 153 | let is_constraint_active = with_pointer_constraint(surface, pointer, |constraint| { |
| 154 | constraint.is_some_and(|c| c.is_active()) |
| 155 | }); |
| 156 | |
| 157 | if !is_constraint_active { |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | // Note: this is surface under pointer, not pointer focus. So if you start, say, a |
| 162 | // middle-drag in Blender, then touchpad-swipe the window away, the surface under pointer |
| 163 | // will change, even though the real pointer focus remains on the Blender surface due to |
| 164 | // the click grab. |
| 165 | // |
| 166 | // Ideally we would just use the constraint surface, but we need its origin. So this is |
| 167 | // more of a hack because pointer contents has the surface origin available. |
| 168 | // |
| 169 | // FIXME: use the constraint surface somehow, don't use pointer contents. |
| 170 | let Some((ref surface_under_pointer, origin)) = self.niri.pointer_contents.surface else { |
| 171 | return; |
| 172 | }; |
| 173 | |
| 174 | if surface_under_pointer != surface { |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | let mut root = surface.clone(); |
| 179 | while let Some(parent) = get_parent(&root) { |
| 180 | root = parent; |
| 181 | } |
| 182 | |
| 183 | let target = self |
| 184 | .niri |
| 185 | .output_for_root(&root) |
| 186 | .and_then(|output| self.niri.global_space.output_geometry(output)) |
| 187 | .map_or(origin + location, |mut output_geometry| { |
| 188 | // i32 sizes are exclusive, but f64 sizes are inclusive. |
| 189 | output_geometry.size -= (1, 1).into(); |
| 190 | (origin + location).constrain(output_geometry.to_f64()) |
| 191 | }); |
| 192 | self.niri.pointer_constraint_position_hint = Some(target); |
| 193 | } |
| 194 | |
| 195 | fn remove_constraint( |
| 196 | &mut self, |
| 197 | _surface: &WlSurface, |
| 198 | pointer: &PointerHandle<Self>, |
| 199 | reason: ConstraintRemove, |
| 200 | ) { |
| 201 | // Since a pointer constraint is broken when a surface loses pointer focus, and one surface |
| 202 | // can only have a single pointer constraint at once, assume there can be only one |
| 203 | // constraint active at once, and therefore the global position hint should come from that |
| 204 | // one constraint that just got removed. |
| 205 | let Some(target) = self.niri.pointer_constraint_position_hint.take() else { |
| 206 | // The client never sent a position hint. |
| 207 | return; |
| 208 | }; |
| 209 | |
| 210 | // If the constraint was broken by the pointer forcibly leaving the surface (e.g. the user |
| 211 | // opened the overview), then it doesn't make much sense to warp it. |
| 212 | if matches!(reason, ConstraintRemove::PointerLeave(_)) { |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | pointer.set_location(target); |
| 217 | |
| 218 | // Redraw to update the cursor position if it's visible. |
| 219 | if self.niri.pointer_visibility.is_visible() { |
| 220 | // FIXME: redraw only outputs overlapping the cursor. |
| 221 | self.niri.queue_redraw_all(); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | impl InputMethodHandler for State { |
| 227 | fn new_popup(&mut self, surface: PopupSurface) { |
| 228 | let popup = PopupKind::InputMethod(surface); |
| 229 | if let Some(output) = self.output_for_popup(&popup) { |
| 230 | let scale = output.current_scale(); |
| 231 | let transform = output.current_transform(); |
| 232 | let wl_surface = popup.wl_surface(); |
| 233 | with_states(wl_surface, |data| { |
| 234 | send_scale_transform(wl_surface, data, scale, transform); |
| 235 | }); |
| 236 | } |
| 237 | |
| 238 | self.unconstrain_popup(&popup); |
| 239 | |
| 240 | if let Err(err) = self.niri.popups.track_popup(popup) { |
| 241 | warn!("error tracking ime popup {err:?}"); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | fn popup_repositioned(&mut self, surface: PopupSurface) { |
| 246 | let popup = PopupKind::InputMethod(surface); |
| 247 | self.unconstrain_popup(&popup); |
| 248 | } |
| 249 | |
| 250 | fn dismiss_popup(&mut self, surface: PopupSurface) { |
| 251 | if let Some(parent) = surface.get_parent().map(|parent| parent.surface.clone()) { |
| 252 | let _ = PopupManager::dismiss_popup(&parent, &PopupKind::from(surface)); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | fn parent_geometry(&self, parent: &WlSurface) -> Rectangle<i32, Logical> { |
| 257 | self.niri |
| 258 | .layout |
| 259 | .find_window_and_output(parent) |
| 260 | .map(|(mapped, _)| mapped.window.geometry()) |
| 261 | .unwrap_or_default() |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | impl TextInputActivation for State {} |
| 266 | |
| 267 | impl KeyboardShortcutsInhibitHandler for State { |
| 268 | fn keyboard_shortcuts_inhibit_state(&mut self) -> &mut KeyboardShortcutsInhibitState { |
| 269 | &mut self.niri.keyboard_shortcuts_inhibit_state |
| 270 | } |
| 271 | |
| 272 | fn new_inhibitor(&mut self, inhibitor: KeyboardShortcutsInhibitor) { |
| 273 | // FIXME: show a confirmation dialog with a "remember for this application" kind of toggle. |
| 274 | inhibitor.activate(); |
| 275 | self.niri |
| 276 | .keyboard_shortcuts_inhibiting_surfaces |
| 277 | .insert(inhibitor.wl_surface().clone(), inhibitor); |
| 278 | } |
| 279 | |
| 280 | fn inhibitor_destroyed(&mut self, inhibitor: KeyboardShortcutsInhibitor) { |
| 281 | self.niri |
| 282 | .keyboard_shortcuts_inhibiting_surfaces |
| 283 | .remove(&inhibitor.wl_surface().clone()); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | impl SelectionHandler for State { |
| 288 | type SelectionUserData = Arc<[u8]>; |
| 289 | |
| 290 | fn send_selection( |
| 291 | &mut self, |
| 292 | _ty: SelectionTarget, |
| 293 | _mime_type: String, |
| 294 | fd: OwnedFd, |
| 295 | _seat: Seat<Self>, |
| 296 | user_data: &Self::SelectionUserData, |
| 297 | ) { |
| 298 | let _span = tracy_client::span!("send_selection"); |
| 299 | |
| 300 | let buf = user_data.clone(); |
| 301 | thread::spawn(move || { |
| 302 | // Clear O_NONBLOCK, otherwise File::write_all() will stop halfway. |
| 303 | if let Err(err) = fcntl_setfl(&fd, OFlags::empty()) { |
| 304 | warn!("error clearing flags on selection target fd: {err:?}"); |
| 305 | } |
| 306 | if let Err(err) = File::from(fd).write_all(&buf) { |
| 307 | warn!("error writing selection: {err:?}"); |
| 308 | } |
| 309 | }); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | impl DataDeviceHandler for State { |
| 314 | fn data_device_state(&mut self) -> &mut DataDeviceState { |
| 315 | &mut self.niri.data_device_state |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | impl WaylandDndGrabHandler for State { |
| 320 | fn dnd_requested<S: dnd::Source>( |
| 321 | &mut self, |
| 322 | source: S, |
| 323 | icon: Option<WlSurface>, |
| 324 | seat: Seat<Self>, |
| 325 | serial: Serial, |
| 326 | type_: dnd::GrabType, |
| 327 | ) { |
| 328 | self.niri.dnd_icon = icon.map(|surface| DndIcon { |
| 329 | surface, |
| 330 | offset: Point::new(0, 0), |
| 331 | }); |
| 332 | |
| 333 | match type_ { |
| 334 | dnd::GrabType::Pointer => { |
| 335 | let pointer = seat.get_pointer().unwrap(); |
| 336 | let start_data = pointer.grab_start_data().unwrap(); |
| 337 | let grab = |
| 338 | DnDGrab::new_pointer(&self.niri.display_handle, start_data, source, seat); |
| 339 | pointer.set_grab(self, grab, serial, Focus::Keep); |
| 340 | } |
| 341 | dnd::GrabType::Touch => { |
| 342 | let touch = seat.get_touch().unwrap(); |
| 343 | let start_data = touch.grab_start_data().unwrap(); |
| 344 | let grab = DnDGrab::new_touch(&self.niri.display_handle, start_data, source, seat); |
| 345 | touch.set_grab(self, grab, serial); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | // FIXME: more granular |
| 350 | self.niri.queue_redraw_all(); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | impl DndGrabHandler for State { |
| 355 | fn dropped( |
| 356 | &mut self, |
| 357 | target: Option<DndTarget<'_, Self>>, |
| 358 | validated: bool, |
| 359 | _seat: Seat<Self>, |
| 360 | location: Point<f64, Logical>, |
| 361 | ) { |
| 362 | let target: Option<&WlSurface> = target.map(DndTarget::into_inner); |
| 363 | trace!("dnd dropped, target: {target:?}, validated: {validated}"); |
| 364 | |
| 365 | // End DnD before activating a specific window below so that it takes precedence. |
| 366 | self.niri.on_maybe_dnd_ended(); |
| 367 | |
| 368 | // Activate the target output, since that's how Firefox drag-tab-into-new-window works for |
| 369 | // example. On successful drop, additionally activate the target window. |
| 370 | let mut activate_output = true; |
| 371 | if let Some(target) = validated.then_some(target).flatten() { |
| 372 | let root = self.niri.find_root_shell_surface(target); |
| 373 | if let Some((mapped, _)) = self.niri.layout.find_window_and_output(&root) { |
| 374 | let window = mapped.window.clone(); |
| 375 | self.niri.layout.activate_window(&window); |
| 376 | self.niri.layer_shell_on_demand_focus = None; |
| 377 | activate_output = false; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | if activate_output { |
| 382 | // Find the output from drop coordinates. |
| 383 | if let Some((output, _)) = self.niri.output_under(location) { |
| 384 | let output = output.clone(); |
| 385 | self.niri.layout.focus_output(&output); |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | fn cancelled(&mut self, _seat: Seat<Self>, _location: Point<f64, Logical>) { |
| 391 | trace!("dnd cancelled"); |
| 392 | |
| 393 | self.niri.on_maybe_dnd_ended(); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | impl crate::niri::Niri { |
| 398 | fn on_maybe_dnd_ended(&mut self) { |
| 399 | self.layout.dnd_end(); |
| 400 | self.dnd_icon = None; |
| 401 | // FIXME: more granular |
| 402 | self.queue_redraw_all(); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | impl PrimarySelectionHandler for State { |
| 407 | fn primary_selection_state(&mut self) -> &mut PrimarySelectionState { |
| 408 | &mut self.niri.primary_selection_state |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | impl WlrDataControlHandler for State { |
| 413 | fn data_control_state(&mut self) -> &mut WlrDataControlState { |
| 414 | &mut self.niri.wlr_data_control_state |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | impl ExtDataControlHandler for State { |
| 419 | fn data_control_state(&mut self) -> &mut ExtDataControlState { |
| 420 | &mut self.niri.ext_data_control_state |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | impl OutputHandler for State { |
| 425 | fn output_bound(&mut self, output: Output, wl_output: WlOutput) { |
| 426 | foreign_toplevel::on_output_bound(self, &output, &wl_output); |
| 427 | ext_workspace::on_output_bound(self, &output, &wl_output); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | impl DmabufHandler for State { |
| 432 | fn dmabuf_state(&mut self) -> &mut DmabufState { |
| 433 | &mut self.niri.dmabuf_state |
| 434 | } |
| 435 | |
| 436 | fn dmabuf_imported( |
| 437 | &mut self, |
| 438 | _global: &DmabufGlobal, |
| 439 | dmabuf: Dmabuf, |
| 440 | notifier: ImportNotifier, |
| 441 | ) { |
| 442 | if self.backend.import_dmabuf(&dmabuf) { |
| 443 | let _ = notifier.successful::<State>(); |
| 444 | } else { |
| 445 | notifier.failed(); |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | impl SessionLockHandler for State { |
| 451 | fn lock_state(&mut self) -> &mut SessionLockManagerState { |
| 452 | &mut self.niri.session_lock_state |
| 453 | } |
| 454 | |
| 455 | fn lock(&mut self, confirmation: SessionLocker) { |
| 456 | self.niri.lock(confirmation); |
| 457 | } |
| 458 | |
| 459 | fn unlock(&mut self) { |
| 460 | self.niri.unlock(); |
| 461 | self.niri.activate_monitors(&mut self.backend); |
| 462 | self.niri.notify_activity(); |
| 463 | } |
| 464 | |
| 465 | fn new_surface(&mut self, surface: LockSurface, output: WlOutput) { |
| 466 | let Some(output) = self.niri.output_from_resource(&output) else { |
| 467 | warn!("no Output matching WlOutput"); |
| 468 | return; |
| 469 | }; |
| 470 | |
| 471 | configure_lock_surface(&surface, &output); |
| 472 | self.niri.new_lock_surface(surface, &output); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | pub fn configure_lock_surface(surface: &LockSurface, output: &Output) { |
| 477 | surface.with_pending_state(|states| { |
| 478 | let size = output_size(output); |
| 479 | states.size = Some(size.to_i32_round()); |
| 480 | }); |
| 481 | let scale = output.current_scale(); |
| 482 | let transform = output.current_transform(); |
| 483 | let wl_surface = surface.wl_surface(); |
| 484 | with_states(wl_surface, |data| { |
| 485 | send_scale_transform(wl_surface, data, scale, transform); |
| 486 | }); |
| 487 | surface.send_configure(); |
| 488 | } |
| 489 | |
| 490 | impl SecurityContextHandler for State { |
| 491 | fn context_created(&mut self, source: SecurityContextListenerSource, context: SecurityContext) { |
| 492 | self.niri |
| 493 | .event_loop |
| 494 | .insert_source(source, move |client, _, state| { |
| 495 | trace!("inserting a new restricted client, context={context:?}"); |
| 496 | state.niri.insert_client(NewClient { |
| 497 | client, |
| 498 | restricted: true, |
| 499 | credentials_unknown: false, |
| 500 | }); |
| 501 | }) |
| 502 | .unwrap(); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | impl IdleNotifierHandler for State { |
| 507 | fn idle_notifier_state(&mut self) -> &mut IdleNotifierState<Self> { |
| 508 | &mut self.niri.idle_notifier_state |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | impl IdleInhibitHandler for State { |
| 513 | fn inhibit(&mut self, surface: WlSurface) { |
| 514 | self.niri.idle_inhibiting_surfaces.insert(surface); |
| 515 | } |
| 516 | |
| 517 | fn uninhibit(&mut self, surface: WlSurface) { |
| 518 | self.niri.idle_inhibiting_surfaces.remove(&surface); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | impl ForeignToplevelHandler for State { |
| 523 | fn foreign_toplevel_manager_state(&mut self) -> &mut ForeignToplevelManagerState { |
| 524 | &mut self.niri.foreign_toplevel_state |
| 525 | } |
| 526 | |
| 527 | fn activate(&mut self, wl_surface: WlSurface) { |
| 528 | if let Some((mapped, _)) = self.niri.layout.find_window_and_output(&wl_surface) { |
| 529 | let window = mapped.window.clone(); |
| 530 | self.niri.layout.activate_window(&window); |
| 531 | self.niri.layer_shell_on_demand_focus = None; |
| 532 | self.niri.queue_redraw_all(); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | fn close(&mut self, wl_surface: WlSurface) { |
| 537 | if let Some((mapped, _)) = self.niri.layout.find_window_and_output(&wl_surface) { |
| 538 | mapped.toplevel().send_close(); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | fn set_fullscreen(&mut self, wl_surface: WlSurface, wl_output: Option<WlOutput>) { |
| 543 | if let Some((mapped, current_output)) = self.niri.layout.find_window_and_output(&wl_surface) |
| 544 | { |
| 545 | let window = mapped.window.clone(); |
| 546 | |
| 547 | if let Some(requested_output) = |
| 548 | wl_output.and_then(|o| self.niri.output_from_resource(&o)) |
| 549 | { |
| 550 | if Some(&requested_output) != current_output { |
| 551 | self.niri.layout.move_to_output( |
| 552 | Some(&window), |
| 553 | &requested_output, |
| 554 | None, |
| 555 | ActivateWindow::Smart, |
| 556 | ); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | self.niri.layout.set_fullscreen(&window, true); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | fn unset_fullscreen(&mut self, wl_surface: WlSurface) { |
| 565 | if let Some((mapped, _)) = self.niri.layout.find_window_and_output(&wl_surface) { |
| 566 | let window = mapped.window.clone(); |
| 567 | self.niri.layout.set_fullscreen(&window, false); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | fn set_maximized(&mut self, wl_surface: WlSurface) { |
| 572 | if let Some((mapped, _)) = self.niri.layout.find_window_and_output(&wl_surface) { |
| 573 | let window = mapped.window.clone(); |
| 574 | self.niri.layout.set_maximized(&window, true); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | fn unset_maximized(&mut self, wl_surface: WlSurface) { |
| 579 | if let Some((mapped, _)) = self.niri.layout.find_window_and_output(&wl_surface) { |
| 580 | let window = mapped.window.clone(); |
| 581 | self.niri.layout.set_maximized(&window, false); |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | impl ExtWorkspaceHandler for State { |
| 587 | fn ext_workspace_manager_state(&mut self) -> &mut ExtWorkspaceManagerState { |
| 588 | &mut self.niri.ext_workspace_state |
| 589 | } |
| 590 | |
| 591 | fn activate_workspace(&mut self, id: WorkspaceId) { |
| 592 | let reference = niri_config::WorkspaceReference::Id(id.get()); |
| 593 | if let Some((mut output, index)) = self.niri.find_output_and_workspace_index(reference) { |
| 594 | if let Some(active) = self.niri.layout.active_output() { |
| 595 | if output.as_ref() == Some(active) { |
| 596 | output = None; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | if let Some(output) = output { |
| 601 | self.niri.layout.focus_output(&output); |
| 602 | } |
| 603 | self.niri.layout.switch_workspace(index); |
| 604 | // No mouse warp: assuming the layer-shell bar workspaces use-case. |
| 605 | |
| 606 | // FIXME: granular |
| 607 | self.niri.queue_redraw_all(); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | fn assign_workspace(&mut self, ws_id: WorkspaceId, output: Output) { |
| 612 | let reference = niri_config::WorkspaceReference::Id(ws_id.get()); |
| 613 | if let Some((old_output, old_idx)) = self.niri.find_output_and_workspace_index(reference) { |
| 614 | self.niri |
| 615 | .layout |
| 616 | .move_workspace_to_output_by_id(old_idx, old_output, &output); |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | impl ScreencopyHandler for State { |
| 622 | fn frame(&mut self, manager: &ZwlrScreencopyManagerV1, screencopy: Screencopy) { |
| 623 | // This can happen if the output was removed before this was called. |
| 624 | if !self.niri.output_exists(screencopy.output()) { |
| 625 | trace!("screencopy output no longer exists"); |
| 626 | return; |
| 627 | } |
| 628 | |
| 629 | // If with_damage then push it onto the queue for redraw of the output, |
| 630 | // otherwise render it immediately. |
| 631 | if screencopy.with_damage() { |
| 632 | self.niri.screencopy_state.push(manager, screencopy); |
| 633 | } else { |
| 634 | self.backend.with_primary_renderer(|renderer| { |
| 635 | if let Err(err) = self |
| 636 | .niri |
| 637 | .render_for_screencopy_without_damage(renderer, manager, screencopy) |
| 638 | { |
| 639 | warn!("error rendering for screencopy: {err:?}"); |
| 640 | } |
| 641 | }); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | fn screencopy_state(&mut self) -> &mut ScreencopyManagerState { |
| 646 | &mut self.niri.screencopy_state |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | impl VirtualPointerHandler for State { |
| 651 | fn process_virtual_pointer_event(&mut self, event: InputEvent<VirtualPointerBackend>) { |
| 652 | self.process_input_event(event); |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | impl DrmLeaseHandler for State { |
| 657 | fn drm_lease_state(&mut self, node: DrmNode) -> &mut DrmLeaseState { |
| 658 | self.backend |
| 659 | .tty() |
| 660 | .get_device_from_node(node) |
| 661 | .unwrap() |
| 662 | .drm_lease_state |
| 663 | .as_mut() |
| 664 | .unwrap() |
| 665 | } |
| 666 | |
| 667 | fn lease_request( |
| 668 | &mut self, |
| 669 | node: DrmNode, |
| 670 | request: DrmLeaseRequest, |
| 671 | ) -> Result<DrmLeaseBuilder, LeaseRejected> { |
| 672 | debug!( |
| 673 | "Received lease request for {} connectors", |
| 674 | request.connectors.len() |
| 675 | ); |
| 676 | self.backend |
| 677 | .tty() |
| 678 | .get_device_from_node(node) |
| 679 | .unwrap() |
| 680 | .lease_request(request) |
| 681 | } |
| 682 | |
| 683 | fn new_active_lease(&mut self, node: DrmNode, lease: DrmLease) { |
| 684 | debug!("Lease success"); |
| 685 | self.backend |
| 686 | .tty() |
| 687 | .get_device_from_node(node) |
| 688 | .unwrap() |
| 689 | .new_lease(lease); |
| 690 | } |
| 691 | |
| 692 | fn lease_destroyed(&mut self, node: DrmNode, lease_id: u32) { |
| 693 | debug!("Destroyed lease"); |
| 694 | self.backend |
| 695 | .tty() |
| 696 | .get_device_from_node(node) |
| 697 | .unwrap() |
| 698 | .remove_lease(lease_id); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | impl GammaControlHandler for State { |
| 703 | fn gamma_control_manager_state(&mut self) -> &mut GammaControlManagerState { |
| 704 | &mut self.niri.gamma_control_manager_state |
| 705 | } |
| 706 | |
| 707 | fn get_gamma_size(&mut self, output: &Output) -> Option<u32> { |
| 708 | match self.backend.tty().get_gamma_size(output) { |
| 709 | Ok(0) => None, // Setting gamma is not supported. |
| 710 | Ok(size) => Some(size), |
| 711 | Err(err) => { |
| 712 | warn!( |
| 713 | "error getting gamma size for output {}: {err:?}", |
| 714 | output.name() |
| 715 | ); |
| 716 | None |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | fn set_gamma(&mut self, output: &Output, ramp: Option<Vec<u16>>) -> Option<()> { |
| 722 | match self.backend.tty().set_gamma(output, ramp) { |
| 723 | Ok(()) => Some(()), |
| 724 | Err(err) => { |
| 725 | warn!("error setting gamma for output {}: {err:?}", output.name()); |
| 726 | None |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | struct UrgentOnlyMarker; |
| 733 | |
| 734 | impl XdgActivationHandler for State { |
| 735 | fn activation_state(&mut self) -> &mut XdgActivationState { |
| 736 | &mut self.niri.activation_state |
| 737 | } |
| 738 | |
| 739 | fn token_created(&mut self, _token: XdgActivationToken, data: XdgActivationTokenData) -> bool { |
| 740 | // Tokens without a serial are urgency-only. This is not specified, but it seems to be the |
| 741 | // common client behavior. |
| 742 | // |
| 743 | // See also: https://gitlab.freedesktop.org/wayland/wayland-protocols/-/issues/150 |
| 744 | let Some((serial, seat)) = data.serial else { |
| 745 | data.user_data.insert_if_missing(|| UrgentOnlyMarker); |
| 746 | return true; |
| 747 | }; |
| 748 | let Some(seat) = Seat::<State>::from_resource(&seat) else { |
| 749 | return false; |
| 750 | }; |
| 751 | |
| 752 | // Widely-used clients such as Discord and Telegram make new tokens (with invalid serials) |
| 753 | // upon clicking on their tray icon or on their notification. This debug flag makes that |
| 754 | // work. |
| 755 | // |
| 756 | // Clicking on a notification sends clients a perfectly valid activation token from the |
| 757 | // notification daemon, but alas they ignore it. Maybe in the future the clients are fixed, |
| 758 | // and we can remove this debug flag. |
| 759 | let config = self.niri.config.borrow(); |
| 760 | if config.debug.honor_xdg_activation_with_invalid_serial { |
| 761 | return true; |
| 762 | } |
| 763 | |
| 764 | // Check the serial against both a keyboard and a pointer, since layer-shell surfaces |
| 765 | // with no keyboard interactivity won't have any keyboard focus. |
| 766 | let kb_last_enter = seat.get_keyboard().unwrap().last_enter(); |
| 767 | if kb_last_enter.is_some_and(|last_enter| serial.is_no_older_than(&last_enter)) { |
| 768 | return true; |
| 769 | } |
| 770 | |
| 771 | let pointer_last_enter = seat.get_pointer().unwrap().last_enter(); |
| 772 | if pointer_last_enter.is_some_and(|last_enter| serial.is_no_older_than(&last_enter)) { |
| 773 | return true; |
| 774 | } |
| 775 | |
| 776 | false |
| 777 | } |
| 778 | |
| 779 | fn request_activation( |
| 780 | &mut self, |
| 781 | token: XdgActivationToken, |
| 782 | token_data: XdgActivationTokenData, |
| 783 | surface: WlSurface, |
| 784 | ) { |
| 785 | if token_data.timestamp.elapsed() < XDG_ACTIVATION_TOKEN_TIMEOUT { |
| 786 | if let Some((mapped, _)) = self.niri.layout.find_window_and_output_mut(&surface) { |
| 787 | let window = mapped.window.clone(); |
| 788 | match mapped.rules().on_xdg_activate { |
| 789 | Some(niri_config::OnXdgActivate::Ignore) => {} |
| 790 | Some(niri_config::OnXdgActivate::SetUrgent) => { |
| 791 | mapped.set_urgent(true); |
| 792 | self.niri.queue_redraw_all(); |
| 793 | } |
| 794 | Some(niri_config::OnXdgActivate::Focus) => { |
| 795 | self.niri.layout.activate_window(&window); |
| 796 | self.niri.layer_shell_on_demand_focus = None; |
| 797 | self.niri.queue_redraw_all(); |
| 798 | } |
| 799 | None => { |
| 800 | if token_data.user_data.get::<UrgentOnlyMarker>().is_some() { |
| 801 | mapped.set_urgent(true); |
| 802 | self.niri.queue_redraw_all(); |
| 803 | } else { |
| 804 | self.niri.layout.activate_window(&window); |
| 805 | self.niri.layer_shell_on_demand_focus = None; |
| 806 | self.niri.queue_redraw_all(); |
| 807 | } |
| 808 | } |
| 809 | } |
| 810 | } else if let Some(unmapped) = self.niri.unmapped_windows.get_mut(&surface) { |
| 811 | unmapped.activation_token_data = Some(token_data); |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | self.niri.activation_state.remove_token(&token); |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | impl FractionalScaleHandler for State {} |
| 820 | |
| 821 | impl OutputManagementHandler for State { |
| 822 | fn output_management_state(&mut self) -> &mut OutputManagementManagerState { |
| 823 | &mut self.niri.output_management_state |
| 824 | } |
| 825 | |
| 826 | fn apply_output_config(&mut self, config: niri_config::Outputs) { |
| 827 | self.niri.config.borrow_mut().outputs = config; |
| 828 | self.reload_output_config(); |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | impl MutterX11InteropHandler for State {} |