1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
mod compositor;
mod layer_shell;
mod xdg_shell;
use std::fs::File;
use std::io::Write;
use std::os::fd::OwnedFd;
use std::sync::Arc;
use std::thread;
use smithay::backend::allocator::dmabuf::Dmabuf;
use smithay::backend::renderer::ImportDma;
use smithay::desktop::{PopupKind, PopupManager};
use smithay::input::pointer::{CursorIcon, CursorImageStatus};
use smithay::input::{Seat, SeatHandler, SeatState};
use smithay::output::Output;
use smithay::reexports::wayland_server::protocol::wl_data_source::WlDataSource;
use smithay::reexports::wayland_server::protocol::wl_output::WlOutput;
use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;
use smithay::reexports::wayland_server::Resource;
use smithay::utils::{Logical, Rectangle, Size};
use smithay::wayland::compositor::{send_surface_state, with_states};
use smithay::wayland::dmabuf::{DmabufGlobal, DmabufHandler, DmabufState, ImportNotifier};
use smithay::wayland::input_method::{InputMethodHandler, PopupSurface};
use smithay::wayland::selection::data_device::{
set_data_device_focus, ClientDndGrabHandler, DataDeviceHandler, DataDeviceState,
ServerDndGrabHandler,
};
use smithay::wayland::selection::primary_selection::{
set_primary_focus, PrimarySelectionHandler, PrimarySelectionState,
};
use smithay::wayland::selection::wlr_data_control::{DataControlHandler, DataControlState};
use smithay::wayland::selection::{SelectionHandler, SelectionTarget};
use smithay::wayland::session_lock::{
LockSurface, SessionLockHandler, SessionLockManagerState, SessionLocker,
};
use smithay::{
delegate_cursor_shape, delegate_data_control, delegate_data_device, delegate_dmabuf,
delegate_input_method_manager, delegate_output, delegate_pointer_gestures,
delegate_presentation, delegate_primary_selection, delegate_seat, delegate_session_lock,
delegate_tablet_manager, delegate_text_input_manager, delegate_virtual_keyboard_manager,
};
use crate::layout::output_size;
use crate::niri::State;
impl SeatHandler for State {
type KeyboardFocus = WlSurface;
type PointerFocus = WlSurface;
fn seat_state(&mut self) -> &mut SeatState<State> {
&mut self.niri.seat_state
}
fn cursor_image(&mut self, _seat: &Seat<Self>, mut image: CursorImageStatus) {
// FIXME: this hack should be removable once the screenshot UI is tracked with a
// PointerFocus properly.
if self.niri.screenshot_ui.is_open() {
image = CursorImageStatus::Named(CursorIcon::Crosshair);
}
self.niri.cursor_manager.set_cursor_image(image);
// FIXME: more granular
self.niri.queue_redraw_all();
}
fn focus_changed(&mut self, seat: &Seat<Self>, focused: Option<&WlSurface>) {
let dh = &self.niri.display_handle;
let client = focused.and_then(|s| dh.get_client(s.id()).ok());
set_data_device_focus(dh, seat, client.clone());
set_primary_focus(dh, seat, client);
}
}
delegate_seat!(State);
delegate_cursor_shape!(State);
delegate_tablet_manager!(State);
delegate_pointer_gestures!(State);
delegate_text_input_manager!(State);
impl InputMethodHandler for State {
fn new_popup(&mut self, surface: PopupSurface) {
if let Some((_, output)) = surface
.get_parent()
.and_then(|parent| self.niri.layout.find_window_and_output(&parent.surface))
{
let scale = output.current_scale().integer_scale();
let transform = output.current_transform();
let wl_surface = surface.wl_surface();
with_states(wl_surface, |data| {
send_surface_state(wl_surface, data, scale, transform);
});
}
if let Err(err) = self.niri.popups.track_popup(PopupKind::from(surface)) {
warn!("error tracking ime popup {err:?}");
}
}
fn dismiss_popup(&mut self, surface: PopupSurface) {
if let Some(parent) = surface.get_parent().map(|parent| parent.surface.clone()) {
let _ = PopupManager::dismiss_popup(&parent, &PopupKind::from(surface));
}
}
fn parent_geometry(&self, parent: &WlSurface) -> Rectangle<i32, Logical> {
self.niri
.layout
.find_window_and_output(parent)
.map(|(window, _)| window.geometry())
.unwrap_or_default()
}
}
delegate_input_method_manager!(State);
delegate_virtual_keyboard_manager!(State);
impl SelectionHandler for State {
type SelectionUserData = Arc<[u8]>;
fn send_selection(
&mut self,
_ty: SelectionTarget,
_mime_type: String,
fd: OwnedFd,
_seat: Seat<Self>,
user_data: &Self::SelectionUserData,
) {
let _span = tracy_client::span!("send_selection");
let buf = user_data.clone();
thread::spawn(move || {
if let Err(err) = File::from(fd).write_all(&buf) {
warn!("error writing selection: {err:?}");
}
});
}
}
impl DataDeviceHandler for State {
fn data_device_state(&self) -> &DataDeviceState {
&self.niri.data_device_state
}
}
impl ClientDndGrabHandler for State {
fn started(
&mut self,
_source: Option<WlDataSource>,
icon: Option<WlSurface>,
_seat: Seat<Self>,
) {
self.niri.dnd_icon = icon;
// FIXME: more granular
self.niri.queue_redraw_all();
}
fn dropped(&mut self, _seat: Seat<Self>) {
self.niri.dnd_icon = None;
// FIXME: more granular
self.niri.queue_redraw_all();
}
}
impl ServerDndGrabHandler for State {}
delegate_data_device!(State);
impl PrimarySelectionHandler for State {
fn primary_selection_state(&self) -> &PrimarySelectionState {
&self.niri.primary_selection_state
}
}
delegate_primary_selection!(State);
impl DataControlHandler for State {
fn data_control_state(&self) -> &DataControlState {
&self.niri.data_control_state
}
}
delegate_data_control!(State);
delegate_output!(State);
delegate_presentation!(State);
impl DmabufHandler for State {
fn dmabuf_state(&mut self) -> &mut DmabufState {
self.backend.tty().dmabuf_state()
}
fn dmabuf_imported(
&mut self,
_global: &DmabufGlobal,
dmabuf: Dmabuf,
notifier: ImportNotifier,
) {
let renderer = self.backend.renderer().expect(
"the dmabuf global must be created and destroyed together with the output device",
);
match renderer.import_dmabuf(&dmabuf, None) {
Ok(_texture) => {
let _ = notifier.successful::<State>();
}
Err(err) => {
debug!("error importing dmabuf: {err:?}");
notifier.failed();
}
}
}
}
delegate_dmabuf!(State);
impl SessionLockHandler for State {
fn lock_state(&mut self) -> &mut SessionLockManagerState {
&mut self.niri.session_lock_state
}
fn lock(&mut self, confirmation: SessionLocker) {
self.niri.lock(confirmation);
}
fn unlock(&mut self) {
self.niri.unlock();
}
fn new_surface(&mut self, surface: LockSurface, output: WlOutput) {
let Some(output) = Output::from_resource(&output) else {
error!("no Output matching WlOutput");
return;
};
configure_lock_surface(&surface, &output);
self.niri.new_lock_surface(surface, &output);
}
}
delegate_session_lock!(State);
pub fn configure_lock_surface(surface: &LockSurface, output: &Output) {
surface.with_pending_state(|states| {
let size = output_size(output);
states.size = Some(Size::from((size
|