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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
pub mod conf;
mod event;
pub mod fs;
pub mod graphics;
pub mod native;
#[cfg(feature = "log-impl")]
pub mod log;
pub use event::*;
pub use graphics::*;
mod default_icon;
pub use native::{gl, NativeDisplay};
pub use graphics::GraphicsContext as Context;
pub mod date {
#[cfg(not(target_arch = "wasm32"))]
pub fn now() -> f64 {
use std::time::SystemTime;
let time = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or_else(|e| panic!("{}", e));
time.as_secs_f64()
}
#[cfg(target_arch = "wasm32")]
pub fn now() -> f64 {
use crate::native;
unsafe { native::wasm::now() }
}
}
impl Context {
pub(crate) fn as_mut(&mut self, display: &mut dyn NativeDisplay) -> &mut Context {
self.display = Some(display);
self
}
pub fn display(&self) -> &dyn NativeDisplay {
unsafe { &*self.display.unwrap() }
}
pub fn display_mut(&self) -> &mut dyn NativeDisplay {
unsafe { &mut *self.display.unwrap() }
}
pub fn screen_size(&self) -> (f32, f32) {
self.display().screen_size()
}
pub fn dpi_scale(&self) -> f32 {
self.display().dpi_scale()
}
pub fn high_dpi(&self) -> bool {
self.display().high_dpi()
}
pub fn order_quit(&mut self) {
self.display_mut().order_quit();
}
pub fn request_quit(&mut self) {
self.display_mut().request_quit();
}
pub fn cancel_quit(&mut self) {
self.display_mut().cancel_quit();
}
pub fn set_cursor_grab(&mut self, grab: bool) {
self.display_mut().set_cursor_grab(grab);
}
pub fn show_mouse(&mut self, shown: bool) {
self.display_mut().show_mouse(shown);
}
pub fn set_mouse_cursor(&mut self, cursor_icon: CursorIcon) {
self.display_mut().set_mouse_cursor(cursor_icon);
}
pub fn set_window_size(&mut self, new_width: u32, new_height: u32) {
self.display_mut().set_window_size(new_width, new_height);
}
pub fn set_fullscreen(&mut self, fullscreen: bool) {
self.display_mut().set_fullscreen(fullscreen);
}
pub fn clipboard_get(&mut self) -> Option<String> {
self.display_mut().clipboard_get()
}
pub fn clipboard_set(&mut self, data: &str) {
self.display_mut().clipboard_set(data);
}
pub fn dropped_file_count(&mut self) -> usize {
self.display_mut().dropped_file_count()
}
pub fn dropped_file_bytes(&mut self, index: usize) -> Option<Vec<u8>> {
self.display_mut().dropped_file_bytes(index)
}
pub fn dropped_file_path(&mut self, index: usize) -> Option<std::path::PathBuf> {
self.display_mut().dropped_file_path(index)
}
pub fn quit(&mut self) {
self.display_mut().order_quit()
}
}
#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)]
pub enum CursorIcon {
Default,
Help,
Pointer,
Wait,
Crosshair,
Text,
Move,
NotAllowed,
EWResize,
NSResize,
NESWResize,
NWSEResize,
}
pub fn start<F>(conf: conf::Conf, f: F)
where
F: 'static + FnOnce(&mut Context) -> Box<dyn EventHandler>,
{
#[cfg(target_os = "linux")]
{
let mut f = Some(f);
let f = &mut f;
match conf.platform.linux_backend {
conf::LinuxBackend::X11Only => {
native::linux_x11::run(&conf, f).expect("X11 backend failed")
}
conf::LinuxBackend::WaylandOnly => {
native::linux_wayland::run(&conf, f).expect("Wayland backend failed")
}
conf::LinuxBackend::X11WithWaylandFallback => {
if native::linux_x11::run(&conf, f).is_none() {
println!("Failed to initialize through X11! Trying wayland instead");
native::linux_wayland::run(&conf, f);
}
}
conf::LinuxBackend::WaylandWithX11Fallback => {
if native::linux_wayland::run(&conf, f).is_none() {
println!("Failed to initialize through wayland! Trying X11 instead");
native::linux_x11::run(&conf, f);
}
}
}
}
#[cfg(target_os = "android")]
unsafe {
native::android::run(conf, f);
}
#[cfg(target_arch = "wasm32")]
{
native::wasm::run(&conf, f);
}
#[cfg(target_os = "windows")]
{
native::windows::run(&conf, f);
}
#[cfg(target_os = "macos")]
unsafe {
native::macos::run(conf, f);
}
}