-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy pathmod.rs
More file actions
812 lines (738 loc) · 28.4 KB
/
Copy pathmod.rs
File metadata and controls
812 lines (738 loc) · 28.4 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
use crate::{
shell::{CosmicSurface, MinimizedWindow, Shell, Trigger, element::CosmicMapped},
state::{Common, State},
utils::prelude::*,
wayland::handlers::{xdg_shell::PopupGrabData, xwayland_keyboard_grab::XWaylandGrabSeatData},
};
use indexmap::IndexSet;
use smithay::{
backend::input::InputTime,
desktop::{PopupUngrabStrategy, layer_map_for_output},
input::{Seat, pointer::MotionEvent},
output::Output,
reexports::wayland_server::{Resource, protocol::wl_surface::WlSurface},
utils::{IsAlive, Point, SERIAL_COUNTER, Serial},
wayland::{
pointer_constraints::with_pointer_constraint,
seat::WaylandFocus,
selection::{data_device::set_data_device_focus, primary_selection::set_primary_focus},
shell::wlr_layer::{KeyboardInteractivity, Layer},
},
};
use std::{borrow::Cow, hash::Hash, mem, sync::Mutex};
use tracing::{debug, trace};
pub use self::order::{Stage, render_input_order};
use self::target::{KeyboardFocusTarget, WindowGroup};
use super::{SeatExt, grabs::SeatMoveGrabState, layout::floating::FloatingLayout};
mod order;
pub mod target;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FocusTarget {
Window(CosmicMapped),
Fullscreen(CosmicSurface),
}
impl PartialEq<CosmicMapped> for FocusTarget {
fn eq(&self, other: &CosmicMapped) -> bool {
matches!(self, FocusTarget::Window(mapped) if mapped == other)
}
}
impl PartialEq<CosmicSurface> for FocusTarget {
fn eq(&self, other: &CosmicSurface) -> bool {
matches!(self, FocusTarget::Fullscreen(surface) if surface == other)
}
}
impl indexmap::Equivalent<FocusTarget> for CosmicMapped {
fn equivalent(&self, key: &FocusTarget) -> bool {
key == self
}
}
impl indexmap::Equivalent<FocusTarget> for CosmicSurface {
fn equivalent(&self, key: &FocusTarget) -> bool {
key == self
}
}
impl Hash for FocusTarget {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
match self {
FocusTarget::Window(window) => window.hash(state),
FocusTarget::Fullscreen(surface) => surface.hash(state),
}
}
}
impl From<CosmicMapped> for FocusTarget {
fn from(value: CosmicMapped) -> Self {
Self::Window(value)
}
}
impl From<CosmicSurface> for FocusTarget {
fn from(value: CosmicSurface) -> Self {
Self::Fullscreen(value)
}
}
impl From<FocusTarget> for KeyboardFocusTarget {
fn from(val: FocusTarget) -> Self {
match val {
FocusTarget::Window(mapped) => KeyboardFocusTarget::Element(mapped),
FocusTarget::Fullscreen(surface) => KeyboardFocusTarget::Fullscreen(surface),
}
}
}
impl FocusTarget {
pub fn alive(&self) -> bool {
match self {
FocusTarget::Window(mapped) => mapped.alive(),
FocusTarget::Fullscreen(surface) => surface.alive(),
}
}
fn is_minimized(&self) -> bool {
match self {
FocusTarget::Window(mapped) => mapped.is_minimized(),
FocusTarget::Fullscreen(surface) => surface.is_minimized(),
}
}
pub fn wl_surface(&self) -> Option<WlSurface> {
match self {
FocusTarget::Window(mapped) => mapped.active_window().wl_surface().map(Cow::into_owned),
FocusTarget::Fullscreen(surface) => surface.wl_surface().map(Cow::into_owned),
}
}
}
pub struct FocusStack<'a>(pub(super) Option<&'a IndexSet<FocusTarget>>);
pub struct FocusStackMut<'a>(pub(super) &'a mut IndexSet<FocusTarget>);
impl FocusStack<'_> {
/// returns the last unminimized window in the focus stack that is still alive
pub fn last(&self) -> Option<&FocusTarget> {
self.0
.as_ref()
.and_then(|set| set.iter().rev().find(|w| w.alive() && !w.is_minimized()))
}
pub fn iter(&self) -> impl Iterator<Item = &'_ FocusTarget> {
self.0
.iter()
.flat_map(|set| set.iter().rev().filter(|w| w.alive() && !w.is_minimized()))
}
}
impl FocusStackMut<'_> {
pub fn append(&mut self, target: impl Into<FocusTarget>) {
let target = target.into();
self.0.retain(|w| w.alive());
self.0.shift_remove(&target);
self.0.insert(target);
}
pub fn remove<T>(&mut self, target: &T) -> bool
where
T: Hash + indexmap::Equivalent<FocusTarget>,
{
self.0.shift_remove(target)
}
pub fn last(&self) -> Option<&FocusTarget> {
self.0.iter().rev().find(|w| w.alive() && !w.is_minimized())
}
pub fn iter(&self) -> impl Iterator<Item = &'_ FocusTarget> {
self.0
.iter()
.rev()
.filter(|w| w.alive() && !w.is_minimized())
}
}
pub struct ActiveFocus(Mutex<Option<KeyboardFocusTarget>>);
impl ActiveFocus {
fn set(seat: &Seat<State>, target: Option<KeyboardFocusTarget>) {
if !seat
.user_data()
.insert_if_missing_threadsafe(|| ActiveFocus(Mutex::new(target.clone())))
{
*seat
.user_data()
.get::<ActiveFocus>()
.unwrap()
.0
.lock()
.unwrap() = target;
}
}
fn get(seat: &Seat<State>) -> Option<KeyboardFocusTarget> {
seat.user_data()
.get::<ActiveFocus>()
.and_then(|a| a.0.lock().unwrap().clone())
}
}
impl Shell {
/// Set the keyboard focus to the given target
/// Note: `update_cursor` is used to determine whether to update the pointer location if cursor_follows_focus is enabled
/// if the focus change was due to a pointer event, this should be set to false
#[profiling::function]
pub fn set_focus(
state: &mut State,
target: Option<&KeyboardFocusTarget>,
seat: &Seat<State>,
serial: Option<Serial>,
update_cursor: bool,
) {
let focus_target = match target {
Some(KeyboardFocusTarget::Element(mapped)) => Some(FocusTarget::Window(mapped.clone())),
Some(KeyboardFocusTarget::Fullscreen(surface)) => {
Some(FocusTarget::Fullscreen(surface.clone()))
}
_ => None,
};
if let Some(target) = focus_target {
state.common.shell.write().append_focus_stack(target, seat);
}
update_focus_state(seat, target, state, serial, update_cursor);
state.common.shell.write().update_active();
}
// We suppress Element(X) to Fullscreen(X) transition to avoid
// loss of focus by the X window when having a transition to fullscreen
// but in the case of X11 unmap/map the leave/enter needs to happen for the X11
// internal state to be focused on the window
pub fn set_focus_on_x11_map(
state: &mut State,
target: &KeyboardFocusTarget,
seat: &Seat<State>,
update_cursor: bool,
) {
let need_reset = seat
.get_keyboard()
.and_then(|keyboard| keyboard.current_focus())
.and_then(|current| current.x11_surface())
.is_some_and(|current| Some(current) == target.x11_surface());
if need_reset {
update_focus_state(seat, None, state, None, false);
}
Shell::set_focus(state, Some(target), seat, None, update_cursor);
}
pub fn append_focus_stack(&mut self, target: impl Into<FocusTarget>, seat: &Seat<State>) {
let target = target.into();
if target.is_minimized() {
return;
}
// update FocusStack and notify layouts about new focus (if any window)
let workspace = target
.wl_surface()
.and_then(|surface| self.workspace_for_surface(&surface));
let workspace = if let Some(workspace) = workspace {
self.workspaces.space_for_handle_mut(&workspace.0).unwrap()
} else {
//should this be the active output or the focused output?
self.active_space_mut(&seat.focused_or_active_output())
.unwrap()
};
let mut focus_stack = workspace.focus_stack.get_mut(seat);
if Some(&target) != focus_stack.last() {
trace!(?target, "Focusing window.");
focus_stack.append(target);
// also remove popup grabs, if we are switching focus
if let Some(mut popup_grab) = seat
.user_data()
.get::<PopupGrabData>()
.and_then(|x| x.take())
&& !popup_grab.has_ended()
{
popup_grab.ungrab(PopupUngrabStrategy::All);
}
}
}
fn update_active(&mut self) {
// update activate status
let focused_windows = self
.seats
.iter()
.filter_map(|seat| {
if matches!(
seat.get_keyboard().unwrap().current_focus(),
Some(KeyboardFocusTarget::Group(_)) | Some(KeyboardFocusTarget::LockSurface(_))
) {
return None;
}
let output = seat.focused_or_active_output();
// `focused_or_active_output()` is seat-stored state that is never
// validated against `sets`. `Common::refresh_focus` re-points stale
// seats before it calls `update_active`, but the other caller,
// `Shell::set_focus`, does not - and it fires as a consequence of an
// output change, with no user input needed.
//
// The fallback inside `active_space` is no help on its own. Removing
// the last output parks its set in `backup_set` and skips the seat
// fix-ups; the next `add_output` then *takes* that backup for the new
// output. `sets` is non-empty and `backup_set` is `None` while the
// seat still names the output that went away.
let Some(space) = self.active_space(&output) else {
tracing::warn!(
target: "cosmic_comp::wsdiag",
output = %output.name(),
"update_active: output absent from workspace sets (upstream unwrap would abort here)"
);
return None;
};
let stack = space.focus_stack.get(seat);
stack.last().and_then(|target| match target {
FocusTarget::Window(window) => Some(window.clone()),
FocusTarget::Fullscreen(_) => None,
})
})
.collect::<Vec<_>>();
for output in self.outputs().cloned().collect::<Vec<_>>().into_iter() {
let set = self.workspaces.sets.get_mut(&output).unwrap();
for focused in focused_windows.iter() {
raise_with_children(&mut set.sticky_layer, focused);
}
for window in set.sticky_layer.mapped() {
window.set_activated(focused_windows.contains(window));
window.configure();
}
for window in set
.minimized_windows
.iter()
.flat_map(MinimizedWindow::mapped)
{
window.set_activated(false);
window.configure();
}
let workspace = &mut set.workspaces[set.active];
for fs in workspace.get_fullscreen_surfaces() {
let is_focused = self.seats.iter().any(|seat| {
if let Some(KeyboardFocusTarget::Fullscreen(s)) =
seat.get_keyboard().unwrap().current_focus()
{
s == fs.surface
} else {
false
}
});
fs.surface.set_activated(is_focused);
fs.surface.send_configure();
}
for focused in focused_windows.iter() {
raise_with_children(&mut workspace.floating_layer, focused);
}
for window in workspace.mapped() {
window.set_activated(focused_windows.contains(window));
window.configure();
}
for m in workspace.minimized_windows.iter() {
if let Some(window) = m.mapped() {
window.set_activated(false);
window.configure();
} else {
for surface in m.windows() {
surface.set_activated(false);
surface.send_configure();
}
}
}
for (i, workspace) in set.workspaces.iter().enumerate() {
if i == set.active {
continue;
}
for window in workspace.mapped() {
window.set_activated(false);
window.configure();
}
}
}
}
}
/// Internal, used to ensure that ActiveFocus, KeyboardFocusTarget, and FocusedOutput are all in sync
#[profiling::function]
fn update_focus_state(
seat: &Seat<State>,
target: Option<&KeyboardFocusTarget>,
state: &mut State,
serial: Option<Serial>,
should_update_cursor: bool,
) {
// update keyboard focus
if let Some(keyboard) = seat.get_keyboard() {
// remove constraint when target changed
let old_focus = keyboard.current_focus();
if let Some(old_target) = old_focus
&& target != Some(&old_target)
&& let Some(surface) = old_target.wl_surface()
&& let Some(pointer) = seat.get_pointer()
{
with_pointer_constraint(&surface, &pointer, |constraint| {
if let Some(constraint) = constraint {
constraint.deactivate();
}
});
}
if should_update_cursor
&& state.common.config.cosmic_conf.cursor_follows_focus
&& target.is_some()
{
//need to borrow mutably for surface under
let shell = state.common.shell.read();
// get the top left corner of the target element
let geometry = shell.focused_geometry(target.unwrap());
if let Some(geometry) = geometry {
// get the center of the target element
let window_center = Point::from((geometry.size.w / 2, geometry.size.h / 2));
let new_pos = (geometry.loc + window_center).to_f64();
// create a pointer target from the target element
let output = shell
.outputs()
.find(|output| output.geometry().to_f64().contains(new_pos))
.cloned()
.unwrap_or(seat.active_output());
crate::input::update_output_image_copy_cursor_position(
&shell,
&state.common.clock,
&output,
seat,
new_pos,
);
let focus = State::surface_under(new_pos, &output, &shell)
.map(|(focus, loc)| (focus, loc.as_logical()));
//drop here to avoid multiple borrows
mem::drop(shell);
seat.get_pointer().unwrap().motion(
state,
focus,
&MotionEvent {
location: new_pos.as_logical(),
serial: SERIAL_COUNTER.next_serial(),
time: InputTime::now(),
},
);
}
}
if target.is_some_and(|t| {
matches!(
t,
KeyboardFocusTarget::LayerSurface(layer_surface) if layer_surface.cached_state().keyboard_interactivity == KeyboardInteractivity::Exclusive
)
}) {
keyboard.unset_grab(state);
}
let serial = serial.unwrap_or_else(|| SERIAL_COUNTER.next_serial());
state
.common
.xwayland_notify_focus_change(target.cloned(), serial);
ActiveFocus::set(seat, target.cloned());
keyboard.set_focus(state, target.cloned(), serial);
std::mem::drop(keyboard);
//update the focused output or set it to the active output
if target.is_some() {
// Get focused output calls visible_output_for_surface internally
// what should happen if the target is some, but it's not visible?
// should this be an error?
seat.set_focused_output(
state
.common
.shell
.read()
.get_output_for_focus(seat)
.as_ref(),
)
} else {
seat.set_focused_output(None);
};
}
}
fn raise_with_children(floating_layer: &mut FloatingLayout, focused: &CosmicMapped) {
if floating_layer.mapped().any(|m| m == focused) {
floating_layer.space.raise_element(focused, true);
for element in floating_layer
.space
.elements()
.filter(|elem| elem != &focused)
.filter(|elem| {
let parent = elem
.active_window()
.0
.toplevel()
.and_then(|toplevel| toplevel.parent());
parent.is_some_and(|parent| {
focused
.active_window()
.wl_surface()
.map(Cow::into_owned)
.map(|focused| parent == focused)
.unwrap_or(false)
})
})
.cloned()
.collect::<Vec<_>>()
.into_iter()
{
raise_with_children(floating_layer, &element);
}
}
}
impl Common {
#[profiling::function]
pub fn refresh_focus(state: &mut State) {
let seats = state
.common
.shell
.read()
.seats
.iter()
.cloned()
.collect::<Vec<_>>();
for seat in &seats {
let mut xwayland_grab = seat
.user_data()
.get_or_insert(XWaylandGrabSeatData::default)
.grab
.lock()
.unwrap();
xwayland_grab.take_if(|(surface, g)| !g.grab().is_alive() || !surface.alive());
{
let shell = state.common.shell.read();
let focused_output = seat.focused_output();
let active_output = seat.active_output();
// If the focused or active output is not in the list of outputs, switch to the first output
if focused_output.is_some_and(|f| !shell.outputs().any(|o| &f == o)) {
seat.set_focused_output(None);
}
if !shell.outputs().any(|o| o == &active_output) {
if let Some(other) = shell.outputs().next() {
seat.set_active_output(other);
}
continue;
}
}
update_pointer_focus(state, seat);
let output = seat.focused_or_active_output();
let mut shell = state.common.shell.write();
let last_known_focus = ActiveFocus::get(seat);
if let Some(target) = last_known_focus {
if target.alive() {
if focus_target_is_valid(&mut shell, seat, &output, target) {
continue; // Focus is valid
} else {
trace!("Wrong Window, focus fixup");
}
} else {
if let KeyboardFocusTarget::Popup(_) = target
&& let Some(popup_grab) = seat
.user_data()
.get::<PopupGrabData>()
.and_then(|x| x.take())
&& !popup_grab.has_ended()
&& let Some(new) = popup_grab.current_grab()
{
trace!("restore focus to previous popup grab");
std::mem::drop(shell);
// TODO: verify whether cursor should be updated at end of popup grab
update_focus_state(
seat,
Some(&new),
state,
Some(SERIAL_COUNTER.next_serial()),
false,
);
seat.user_data()
.get_or_insert::<PopupGrabData, _>(PopupGrabData::default)
.set(Some(popup_grab));
continue;
}
trace!("Surface dead, focus fixup");
}
} else {
let workspace = shell.active_space(&output).unwrap();
let focus_stack = workspace.focus_stack.get(seat);
if focus_stack.last().is_none() {
continue; // Focus is valid
} else {
trace!("No previous window, focus fixup");
}
}
// fixup focus
{
// also remove popup grabs, if we are switching focus
if let Some(mut popup_grab) = seat
.user_data()
.get::<PopupGrabData>()
.and_then(|x| x.take())
&& !popup_grab.has_ended()
{
popup_grab.ungrab(PopupUngrabStrategy::All);
}
// update keyboard focus
let target = update_focus_target(&shell, seat, &output);
std::mem::drop(shell);
//I can probably feature gate this condition
debug!("Restoring focus to {:?}", target.as_ref());
update_focus_state(seat, target.as_ref(), state, None, false);
}
}
// Update clipboard and primary focus
//
// For now, needs to be here instead of in `focus_changed` to update focus
// when the active element of a stack changes.
for seat in &seats {
if let Some(keyboard) = seat.get_keyboard() {
let focus = keyboard.current_focus();
let client = focus
.as_ref()
.and_then(|t| t.wl_surface())
.and_then(|s| state.common.display_handle.get_client(s.id()).ok());
set_data_device_focus(&state.common.display_handle, seat, client.clone());
set_primary_focus(&state.common.display_handle, seat, client);
}
}
state.common.shell.write().update_active()
}
}
fn focus_target_is_valid(
shell: &mut Shell,
seat: &Seat<State>,
output: &Output,
target: KeyboardFocusTarget,
) -> bool {
// If a session lock is active, only lock surfaces can be focused
if shell.session_lock.is_some() {
return matches!(target, KeyboardFocusTarget::LockSurface(_));
}
// If an exclusive layer shell surface exists (on any output), only exclusive
// shell surfaces can have focus, on the highest layer with exclusive surfaces.
if let Some(layer) = exclusive_layer_surface_layer(shell) {
return if let KeyboardFocusTarget::LayerSurface(layer_surface) = target {
let data = layer_surface.cached_state();
(data.keyboard_interactivity, data.layer) == (KeyboardInteractivity::Exclusive, layer)
} else {
false
};
}
match target {
KeyboardFocusTarget::Element(mapped) => {
if seat
.user_data()
.get::<SeatMoveGrabState>()
.is_some_and(|state| {
state
.lock()
.unwrap()
.as_ref()
.is_some_and(|state| state.element() == mapped)
})
{
return true;
}
let is_sticky = shell
.workspaces
.sets
.get(output)
.unwrap()
.sticky_layer
.mapped()
.any(|m| m == &mapped);
let workspace = shell.active_space(output).unwrap();
let focus_stack = workspace.focus_stack.get(seat);
let is_in_focus_stack = focus_stack.last().map(|m| m == &mapped).unwrap_or(false);
if is_sticky && !is_in_focus_stack {
shell.append_focus_stack(mapped, seat);
}
is_sticky || is_in_focus_stack
}
KeyboardFocusTarget::LayerSurface(layer) => {
layer_map_for_output(output).layers().any(|l| l == &layer)
}
KeyboardFocusTarget::Group(WindowGroup { node, .. }) => shell
.workspaces
.active(output)
.unwrap()
.1
.tiling_layer
.has_node(&node),
KeyboardFocusTarget::Fullscreen(window) => {
let workspace = shell.active_space(output).unwrap();
workspace
.get_fullscreen_surfaces()
.any(|f| f.surface == window)
}
KeyboardFocusTarget::Popup(_) => true,
KeyboardFocusTarget::LockSurface(_) => false,
}
}
fn update_focus_target(
shell: &Shell,
seat: &Seat<State>,
output: &Output,
) -> Option<KeyboardFocusTarget> {
if let Some(session_lock) = &shell.session_lock {
session_lock
.surfaces
.get(output)
.cloned()
.map(KeyboardFocusTarget::from)
} else if let Some(layer) = exclusive_layer_surface_layer(shell) {
layer_map_for_output(output)
.layers()
.find(|layer_surface| {
let data = layer_surface.cached_state();
(data.keyboard_interactivity, data.layer)
== (KeyboardInteractivity::Exclusive, layer)
})
.cloned()
.map(KeyboardFocusTarget::from)
} else {
let workspace = shell.active_space(output).unwrap();
if let Some(Trigger::KeyboardSwap(_, desc)) = shell.overview_mode().0.active_trigger()
&& workspace.handle == desc.handle
&& workspace.tiling_layer.has_node(&desc.node)
&& let Some(focus) = workspace.tiling_layer.node_desc_to_focus(desc)
{
return Some(focus);
}
workspace
.focus_stack
.get(seat)
.last()
.cloned()
.map(Into::<KeyboardFocusTarget>::into)
.or_else(|| {
workspace
.mapped()
.next()
.cloned()
.map(KeyboardFocusTarget::Element)
.or_else(|| {
workspace
.get_fullscreen(seat)
.cloned()
.map(|fs| KeyboardFocusTarget::Fullscreen(fs.surface))
})
})
}
}
fn update_pointer_focus(state: &mut State, seat: &Seat<State>) {
if let Some(pointer) = seat.get_pointer() {
let output = seat.active_output();
let position = pointer.current_location().as_global();
let shell = state.common.shell.write();
let under = State::surface_under(position, &output, &shell)
.map(|(target, pos)| (target, pos.as_logical()));
drop(shell);
if pointer.current_focus().as_ref() != under.as_ref().map(|(target, _)| target) {
pointer.motion(
state,
under,
&MotionEvent {
location: pointer.current_location(),
serial: SERIAL_COUNTER.next_serial(),
time: InputTime::now(),
},
);
}
}
}
// Get the top-most layer, if any, with at least one surface with exclusive keyboard interactivity.
// Only considers surface in `Top` or `Overlay` layer.
fn exclusive_layer_surface_layer(shell: &Shell) -> Option<Layer> {
let mut layer = None;
for output in shell.outputs() {
for layer_surface in layer_map_for_output(output).layers() {
let data = layer_surface.cached_state();
if data.keyboard_interactivity == KeyboardInteractivity::Exclusive
&& data.layer as u32 >= layer.unwrap_or(Layer::Top) as u32
{
layer = Some(data.layer);
}
}
}
layer
}