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
use std::sync::Arc;
use std::sync::atomic::{AtomicU8, Ordering};
use std::thread::spawn;
use winit::window::WindowBuilder;
use crossbeam_utils::atomic::AtomicCell;
use flume::{TryRecvError, TrySendError, unbounded};
use crate::event_loop::{ControlFlow, SharedControlFlow};
use crate::event::{Event, UserEvent};
use crate::messages::{AppProxyRegisterInfo, ProxyRegister, ProxyRegisterBody, ProxyRegisterInfo, ProxyRequest, ProxyResponse, REGISTER_PROXY};
pub fn run(rest: impl FnOnce() + Send + 'static) -> ! {
let (register_proxy, recv_register) = unbounded();
unsafe {
REGISTER_PROXY = Some(register_proxy);
}
let mut proxy_channels = Vec::new();
EXIT_FLAG.with(|exit_flag| exit_flag.store(1, Ordering::Release));
spawn(rest);
winit::event_loop::EventLoop::<UserEvent>::with_user_event().run(move |event, window_target, control_flow| {
let (event, physical_size) = Event::from(event);
for ProxyRegister(info) in recv_register.try_iter() {
if let Some(info) = info.upgrade() {
let control_flow = Arc::new(AtomicCell::new(ControlFlow::Poll));
let (proxy_send, recv_from_proxy) = unbounded();
let (send_to_proxy, proxy_recv) = unbounded();
proxy_channels.push(AppProxyRegisterInfo {
recv_from_proxy,
send_to_proxy,
control_flow: control_flow.clone()
});
match info.take() {
ProxyRegisterBody::Init => {},
ProxyRegisterBody::Polled { waker } => waker.wake(),
ProxyRegisterBody::Ready { info: _ } => unreachable!("proxy event loop registered twice")
}
info.store(ProxyRegisterBody::Ready {
info: ProxyRegisterInfo {
control_flow,
send: proxy_send,
recv: proxy_recv,
}
});
}
}
let mut shared_control_flow = SharedControlFlow::Wait;
let mut proxy_idxs_to_remove = Vec::new();
for (proxy_idx, AppProxyRegisterInfo { control_flow, recv_from_proxy, send_to_proxy}) in proxy_channels.iter_mut().enumerate() {
loop {
let request = match recv_from_proxy.try_recv() {
Ok(request) => request,
Err(TryRecvError::Empty) => break,
Err(TryRecvError::Disconnected) => {
proxy_idxs_to_remove.push(proxy_idx);
break
}
};
let response = match request {
ProxyRequest::SpawnWindow { configure } => {
ProxyResponse::SpawnWindow { result: configure(WindowBuilder::new()).build(&window_target) }
}
ProxyRequest::RunOnMainThread { action } => {
ProxyResponse::RunOnMainThread { return_value: action() }
}
};
match send_to_proxy.try_send(response) {
Ok(_) => (),
Err(TrySendError::Full(_)) => unreachable!("event loop channel (unbounded) full?"),
Err(TrySendError::Disconnected(_)) => {
proxy_idxs_to_remove.push(proxy_idx);
break
}
}
}
match send_to_proxy.try_send(ProxyResponse::Event(event.clone())) {
Ok(_) => (),
Err(TrySendError::Full(_)) => unreachable!("event loop channel (unbounded) full?"),
Err(TrySendError::Disconnected(_)) => proxy_idxs_to_remove.push(proxy_idx)
}
match control_flow.load() {
ControlFlow::Poll => shared_control_flow = shared_control_flow.min(SharedControlFlow::Poll),
ControlFlow::Wait => shared_control_flow = shared_control_flow.min(SharedControlFlow::Wait),
ControlFlow::WaitUntil(instant) => shared_control_flow = shared_control_flow.min(SharedControlFlow::WaitUntil(instant)),
ControlFlow::ExitLocal => {
}
ControlFlow::ExitApp => shared_control_flow = shared_control_flow.min(SharedControlFlow::ExitApp),
}
}
for proxy_to_remove in proxy_idxs_to_remove.into_iter().rev() {
proxy_channels.remove(proxy_to_remove);
}
event.into(physical_size);
*control_flow = match shared_control_flow {
SharedControlFlow::Wait => winit::event_loop::ControlFlow::Wait,
SharedControlFlow::Poll => winit::event_loop::ControlFlow::Poll,
SharedControlFlow::WaitUntil(instant) => winit::event_loop::ControlFlow::WaitUntil(instant),
SharedControlFlow::ExitApp => winit::event_loop::ControlFlow::Exit,
};
if EXIT_FLAG.with(|exit_flag| exit_flag.load(Ordering::Acquire)) == 2 {
*control_flow = winit::event_loop::ControlFlow::Exit;
}
})
}
pub fn exit() {
if EXIT_FLAG.with(|exit_flag| exit_flag.load(Ordering::Acquire)) == 0 {
std::process::exit(0);
}
EXIT_FLAG.with(|exit_flag| exit_flag.store(2, Ordering::Release));
}
thread_local! {
static EXIT_FLAG: Arc<AtomicU8> = Arc::new(AtomicU8::new(0));
}