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
use super::glutin_runtime::*;
use super::glutin_thread_event::*;
use ::desync::*;
use glutin::event_loop::{EventLoop, EventLoopProxy};
use std::mem;
use std::sync::*;
use std::sync::mpsc;
use std::thread;
use std::collections::{HashMap};
lazy_static! {
static ref GLUTIN_THREAD: Desync<Option<Arc<GlutinThread>>> = Desync::new(None);
}
pub struct GlutinThread {
event_proxy: Desync<EventLoopProxy<GlutinThreadEvent>>
}
impl GlutinThread {
pub fn send_event(&self, event: GlutinThreadEvent) {
self.event_proxy.desync(move |proxy| { proxy.send_event(event).ok(); });
}
}
pub fn glutin_thread() -> Arc<GlutinThread> {
GLUTIN_THREAD.sync(|thread| {
if let Some(thread) = thread {
Arc::clone(thread)
} else {
let new_thread = create_glutin_thread();
*thread = Some(Arc::clone(&new_thread));
new_thread
}
})
}
struct StopGlutinWhenDropped;
impl Drop for StopGlutinWhenDropped {
fn drop(&mut self) {
glutin_thread().send_event(GlutinThreadEvent::StopWhenAllWindowsClosed);
}
}
pub fn with_2d_graphics<TAppFn: 'static+Send+FnOnce() -> ()>(app_fn: TAppFn) {
let (send_proxy, recv_proxy) = mpsc::channel();
thread::Builder::new()
.name("Application thread".into())
.spawn(move || {
GLUTIN_THREAD.sync(move |thread| {
let proxy = recv_proxy.recv().expect("Glutin thread will send us a proxy after initialising");
*thread = Some(Arc::new(GlutinThread {
event_proxy: Desync::new(proxy)
}));
});
let stop_glutin = StopGlutinWhenDropped;
app_fn();
mem::drop(stop_glutin);
})
.expect("Application thread is running");
run_glutin_thread(send_proxy);
}
fn create_glutin_thread() -> Arc<GlutinThread> {
let (send_proxy, recv_proxy) = mpsc::channel();
thread::Builder::new()
.name("Glutin event thread".into())
.spawn(move || {
run_glutin_thread(send_proxy)
})
.expect("Glutin thread is running");
let proxy = recv_proxy.recv().expect("Glutin thread will send us a proxy after initialising");
Arc::new(GlutinThread {
event_proxy: Desync::new(proxy)
})
}
fn run_glutin_thread(send_proxy: mpsc::Sender<EventLoopProxy<GlutinThreadEvent>>) {
let event_loop = EventLoop::with_user_event();
let proxy = event_loop.create_proxy();
send_proxy.send(proxy).expect("Main thread is waiting to receive its proxy");
let mut runtime = GlutinRuntime {
window_events: HashMap::new(),
futures: HashMap::new(),
will_stop_when_no_windows: false,
will_exit: false,
pointer_id: HashMap::new(),
pointer_state: HashMap::new()
};
event_loop.run(move |event, window_target, control_flow| {
runtime.handle_event(event, window_target, control_flow);
});
}