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
//! Specialized async Executor built on top of winit event loop for running [`AsyncComponent`]

pub mod signal;
#[cfg(target_arch = "wasm32")]
mod wasm;

use std::{
    pin::Pin,
    sync::atomic::Ordering,
    task::{Context, Poll, RawWaker, RawWakerVTable, Waker},
};

use async_component_core::AsyncComponent;
use ref_extended::pin_ref_extended;
use winit::{
    event::Event,
    event_loop::{ControlFlow, EventLoop},
};

use crate::WinitComponent;

use self::signal::WinitSignal;

/// Reserved zero sized user event struct used for waking winit eventloop
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct ExecutorPollEvent;

/// Executor implemented on top of winit eventloop using user event.
///
/// See [`WinitSignal`] for more detail how it utilize winit user event.
#[derive(Debug)]
pub struct WinitExecutor {
    event_loop: Option<EventLoop<ExecutorPollEvent>>,

    state_signal: WinitSignal,
    stream_signal: WinitSignal,
}

impl WinitExecutor {
    /// Create new [`WinitExecutor`]
    pub fn new(event_loop: EventLoop<ExecutorPollEvent>) -> Self {
        let state_signal = WinitSignal::new(event_loop.create_proxy());
        let stream_signal = WinitSignal::new(event_loop.create_proxy());

        Self {
            event_loop: Some(event_loop),

            state_signal,
            stream_signal,
        }
    }

    fn poll_stream(&'static self, component: &mut impl AsyncComponent) {
        if let Ok(_) = self.stream_signal.scheduled.compare_exchange(
            true,
            false,
            Ordering::AcqRel,
            Ordering::Acquire,
        ) {
            while let Poll::Ready(_) =
                Pin::new(&mut *component).poll_next_stream(&mut Context::from_waker(&unsafe {
                    Waker::from_raw(create_raw_waker(&self.stream_signal))
                }))
            {}
        }
    }

    fn poll_state(&'static self, component: &mut impl AsyncComponent) -> Poll<()> {
        if let Ok(_) = self.state_signal.scheduled.compare_exchange(
            true,
            false,
            Ordering::AcqRel,
            Ordering::Acquire,
        ) {
            if Pin::new(component)
                .poll_next_state(&mut Context::from_waker(&unsafe {
                    Waker::from_raw(create_raw_waker(&self.state_signal))
                }))
                .is_ready()
            {
                self.state_signal.scheduled.store(true, Ordering::Release);
            }

            Poll::Ready(())
        } else {
            Poll::Pending
        }
    }

    /// Initializes the winit event loop and run component.
    ///
    /// See [`EventLoop`] for more detail about winit event loop
    pub fn run(mut self, mut component: impl AsyncComponent + WinitComponent + 'static) -> ! {
        let event_loop = self.event_loop.take().unwrap();

        pin_ref_extended!(executor, self);

        event_loop.run(move |event, _, control_flow| {
            match event {
                Event::MainEventsCleared => {
                    component.on_event(&mut Event::MainEventsCleared, control_flow);

                    if let ControlFlow::ExitWithCode(_) = control_flow {
                        return;
                    }

                    executor.poll_stream(&mut component);

                    match executor.poll_state(&mut component) {
                        Poll::Ready(_) => {
                            control_flow.set_poll();
                        }
                        Poll::Pending => {
                            control_flow.set_wait();
                        }
                    }
                }

                // Handled in Event::MainEventsCleared
                Event::UserEvent(_) => {}

                _ => {
                    component.on_event(&mut event.map_nonuser_event().unwrap(), control_flow);
                }
            }
        })
    }
}

fn create_raw_waker(signal: &'static WinitSignal) -> RawWaker {
    unsafe fn waker_clone(this: *const ()) -> RawWaker {
        create_raw_waker(&*(this as *const WinitSignal))
    }

    unsafe fn waker_wake(this: *const ()) {
        let this = &*(this as *const WinitSignal);
        this.wake_by_ref();
    }

    unsafe fn waker_wake_by_ref(this: *const ()) {
        let this = &*(this as *const WinitSignal);
        this.wake_by_ref();
    }

    unsafe fn waker_drop(_: *const ()) {}

    RawWaker::new(
        signal as *const _ as *const (),
        &RawWakerVTable::new(waker_clone, waker_wake, waker_wake_by_ref, waker_drop),
    )
}