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
use std::{mem, sync::Mutex};
use wayland_client::{
    globals::GlobalList,
    protocol::{wl_output, wl_surface},
    Connection, Dispatch, QueueHandle, WEnum,
};
use wayland_protocols::wp::presentation_time::client::{wp_presentation, wp_presentation_feedback};

use crate::{error::GlobalError, globals::GlobalData, registry::GlobalProxy};

#[derive(Debug)]
pub struct PresentTime {
    pub clk_id: u32,
    pub tv_sec: u64,
    pub tv_nsec: u32,
}

#[derive(Debug)]
pub struct PresentationTimeState {
    presentation: GlobalProxy<wp_presentation::WpPresentation>,
    clk_id: Option<u32>,
}

impl PresentationTimeState {
    /// Bind `wp_presentation` global, if it exists
    pub fn bind<D>(globals: &GlobalList, qh: &QueueHandle<D>) -> Self
    where
        D: Dispatch<wp_presentation::WpPresentation, GlobalData> + 'static,
    {
        let presentation = GlobalProxy::from(globals.bind(qh, 1..=1, GlobalData));
        Self { presentation, clk_id: None }
    }

    /// Request feedback for current submission to surface.
    pub fn feedback<D>(
        &self,
        surface: &wl_surface::WlSurface,
        qh: &QueueHandle<D>,
    ) -> Result<wp_presentation_feedback::WpPresentationFeedback, GlobalError>
    where
        D: Dispatch<wp_presentation_feedback::WpPresentationFeedback, PresentationTimeData>
            + 'static,
    {
        let udata = PresentationTimeData {
            wl_surface: surface.clone(),
            sync_outputs: Mutex::new(Vec::new()),
        };
        Ok(self.presentation.get()?.feedback(surface, qh, udata))
    }
}

pub trait PresentationTimeHandler: Sized {
    fn presentation_time_state(&mut self) -> &mut PresentationTimeState;

    /// Content update displayed to user at indicated time
    #[allow(clippy::too_many_arguments)]
    fn presented(
        &mut self,
        conn: &Connection,
        qh: &QueueHandle<Self>,
        feedback: &wp_presentation_feedback::WpPresentationFeedback,
        surface: &wl_surface::WlSurface,
        outputs: Vec<wl_output::WlOutput>,
        time: PresentTime,
        refresh: u32,
        seq: u64,
        flags: WEnum<wp_presentation_feedback::Kind>,
    );

    /// Content update not displayed
    fn discarded(
        &mut self,
        conn: &Connection,
        qh: &QueueHandle<Self>,
        feedback: &wp_presentation_feedback::WpPresentationFeedback,
        surface: &wl_surface::WlSurface,
    );
}

#[doc(hidden)]
#[derive(Debug)]
pub struct PresentationTimeData {
    wl_surface: wl_surface::WlSurface,
    sync_outputs: Mutex<Vec<wl_output::WlOutput>>,
}

impl<D> Dispatch<wp_presentation::WpPresentation, GlobalData, D> for PresentationTimeState
where
    D: Dispatch<wp_presentation::WpPresentation, GlobalData> + PresentationTimeHandler,
{
    fn event(
        data: &mut D,
        _presentation: &wp_presentation::WpPresentation,
        event: wp_presentation::Event,
        _: &GlobalData,
        _conn: &Connection,
        _qh: &QueueHandle<D>,
    ) {
        match event {
            wp_presentation::Event::ClockId { clk_id } => {
                data.presentation_time_state().clk_id = Some(clk_id);
            }
            _ => unreachable!(),
        }
    }
}

impl<D> Dispatch<wp_presentation_feedback::WpPresentationFeedback, PresentationTimeData, D>
    for PresentationTimeState
where
    D: Dispatch<wp_presentation_feedback::WpPresentationFeedback, PresentationTimeData>
        + PresentationTimeHandler,
{
    fn event(
        data: &mut D,
        feedback: &wp_presentation_feedback::WpPresentationFeedback,
        event: wp_presentation_feedback::Event,
        udata: &PresentationTimeData,
        conn: &Connection,
        qh: &QueueHandle<D>,
    ) {
        match event {
            wp_presentation_feedback::Event::SyncOutput { output } => {
                udata.sync_outputs.lock().unwrap().push(output);
            }
            wp_presentation_feedback::Event::Presented {
                tv_sec_hi,
                tv_sec_lo,
                tv_nsec,
                refresh,
                seq_hi,
                seq_lo,
                flags,
            } => {
                let sync_outputs = mem::take(&mut *udata.sync_outputs.lock().unwrap());
                let clk_id = data.presentation_time_state().clk_id.unwrap(); // XXX unwrap
                let time = PresentTime {
                    clk_id,
                    tv_sec: ((tv_sec_hi as u64) << 32) | (tv_sec_lo as u64),
                    tv_nsec,
                };
                let seq = ((seq_hi as u64) << 32) | (seq_lo as u64);
                data.presented(
                    conn,
                    qh,
                    feedback,
                    &udata.wl_surface,
                    sync_outputs,
                    time,
                    refresh,
                    seq,
                    flags,
                );
            }
            wp_presentation_feedback::Event::Discarded => {
                data.discarded(conn, qh, feedback, &udata.wl_surface)
            }
            _ => {}
        }
    }
}

#[macro_export]
macro_rules! delegate_presentation_time {
    ($(@<$( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+>)? $ty: ty) => {
        $crate::reexports::client::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [
            $crate::reexports::protocols::wp::presentation_time::client::wp_presentation::WpPresentation: $crate::globals::GlobalData
        ] => $crate::presentation_time::PresentationTimeState);
        $crate::reexports::client::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [
            $crate::reexports::protocols::wp::presentation_time::client::wp_presentation_feedback::WpPresentationFeedback: $crate::presentation_time::PresentationTimeData
        ] => $crate::presentation_time::PresentationTimeState);
    };
}