1
//! Cross-thread "bring this window to the front" requests.
2
//!
3
//! A raise is asked for from OFF the event loop - the MPRIS thread when a
4
//! desktop's media widget is clicked (9h-i-a-ii), and in future anything else
5
//! that has to reach a window it does not own. Activating a window is a
6
//! platform call that belongs on the loop's own thread, so the request is
7
//! parked here and drained once per pass, exactly like the media keys.
8
//!
9
//! # The request names a WINDOW, not "the app"
10
//!
11
//! An app can have several, and the one that registered the media session is
12
//! not necessarily the one that happens to be drained first. So the request
13
//! carries the target's `registry_window_id` - the native handle every backend
14
//! already keys its window registry by - and a window raises itself only when
15
//! the id matches. Without that, a two-window app would raise whichever window
16
//! reached the drain first, which is a coin flip.
17

            
18
use alloc::vec::Vec;
19

            
20
/// Pending raise targets, by `registry_window_id`.
21
static PENDING: std::sync::Mutex<Vec<u64>> = std::sync::Mutex::new(Vec::new());
22

            
23
/// Ask for a window to be raised, from any thread.
24
///
25
/// A target already pending is DROPPED rather than queued twice: raising is
26
/// idempotent, and a desktop that sends `Raise` twice while the loop is busy
27
/// means one raise, not two. Same argument the media-key channel makes.
28
8
pub fn request_raise(window_id: u64) {
29
8
    if window_id == 0 {
30
        // 0 is "no registry id" (headless, or a backend with no native
31
        // handle). Queuing it would make every window match.
32
1
        return;
33
7
    }
34
7
    let mut q = PENDING
35
7
        .lock()
36
7
        .unwrap_or_else(std::sync::PoisonError::into_inner);
37
7
    if q.contains(&window_id) {
38
1
        return;
39
6
    }
40
6
    q.push(window_id);
41
8
}
42

            
43
/// Does this window have a raise waiting? Removes it if so.
44
///
45
/// Per-window rather than a plain drain, because the loop that asks is one
46
/// window's and the request may be for another's - taking the whole queue
47
/// would swallow a sibling's raise.
48
11
pub fn take_raise_request(window_id: u64) -> bool {
49
11
    if window_id == 0 {
50
2
        return false;
51
9
    }
52
9
    let mut q = PENDING
53
9
        .lock()
54
9
        .unwrap_or_else(std::sync::PoisonError::into_inner);
55
9
    match q.iter().position(|id| *id == window_id) {
56
6
        Some(i) => {
57
6
            q.remove(i);
58
6
            true
59
        }
60
3
        None => false,
61
    }
62
11
}
63

            
64
#[cfg(test)]
65
mod tests {
66
    use super::*;
67

            
68
    /// `PENDING` is a process-global and the harness runs these in parallel.
69
    static SERIAL: std::sync::Mutex<()> = std::sync::Mutex::new(());
70

            
71
4
    fn exclusive() -> std::sync::MutexGuard<'static, ()> {
72
4
        let g = SERIAL
73
4
            .lock()
74
4
            .unwrap_or_else(std::sync::PoisonError::into_inner);
75
4
        let mut q = PENDING
76
4
            .lock()
77
4
            .unwrap_or_else(std::sync::PoisonError::into_inner);
78
4
        q.clear();
79
4
        drop(q);
80
4
        g
81
4
    }
82

            
83
    #[test]
84
1
    fn a_request_is_taken_once_by_the_window_it_names() {
85
1
        let _guard = exclusive();
86
1
        request_raise(42);
87
1
        assert!(take_raise_request(42));
88
1
        assert!(
89
1
            !take_raise_request(42),
90
            "a raise must not fire twice for one request"
91
        );
92
1
    }
93

            
94
    /// THE POINT OF KEYING BY WINDOW. A two-window app must not have one
95
    /// window answer the other's raise - the media session belongs to whichever
96
    /// window registered it.
97
    #[test]
98
1
    fn another_window_does_not_swallow_the_request() {
99
1
        let _guard = exclusive();
100
1
        request_raise(7);
101
1
        assert!(!take_raise_request(9), "window 9 answered window 7's raise");
102
1
        assert!(take_raise_request(7), "and then 7's request was gone");
103
1
    }
104

            
105
    #[test]
106
1
    fn a_repeated_request_raises_once_and_two_windows_are_independent() {
107
1
        let _guard = exclusive();
108
1
        request_raise(1);
109
1
        request_raise(1);
110
1
        assert!(take_raise_request(1));
111
1
        assert!(!take_raise_request(1), "a repeat must coalesce");
112

            
113
1
        request_raise(1);
114
1
        request_raise(2);
115
1
        assert!(take_raise_request(2));
116
1
        assert!(take_raise_request(1), "taking 2 must not drop 1");
117
1
    }
118

            
119
    /// `0` means "this backend has no native handle" (headless). Accepting it
120
    /// would make every window match every request.
121
    #[test]
122
1
    fn the_zero_id_is_never_queued_and_never_matches() {
123
1
        let _guard = exclusive();
124
1
        request_raise(0);
125
1
        assert!(!take_raise_request(0));
126
1
        request_raise(5);
127
1
        assert!(!take_raise_request(0), "id 0 must not match a real request");
128
1
        assert!(take_raise_request(5));
129
1
    }
130
}