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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate shared_library;
extern crate gl_common;
extern crate libc;
#[cfg(target_os = "windows")]
extern crate winapi;
#[cfg(target_os = "windows")]
extern crate kernel32;
#[cfg(target_os = "windows")]
extern crate shell32;
#[cfg(target_os = "windows")]
extern crate gdi32;
#[cfg(target_os = "windows")]
extern crate user32;
#[cfg(target_os = "windows")]
extern crate dwmapi;
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[macro_use]
extern crate objc;
#[cfg(target_os = "macos")]
extern crate cgl;
#[cfg(target_os = "macos")]
extern crate cocoa;
#[cfg(target_os = "macos")]
extern crate core_foundation;
#[cfg(target_os = "macos")]
extern crate core_graphics;
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly"))]
extern crate x11_dl;
pub use events::*;
pub use headless::{HeadlessRendererBuilder, HeadlessContext};
pub use window::{WindowBuilder, WindowProxy, PollEventsIterator, WaitEventsIterator};
pub use window::{AvailableMonitorsIter, MonitorId, get_available_monitors, get_primary_monitor};
pub use native_monitor::NativeMonitorId;
use std::io;
#[cfg(not(target_os = "macos"))]
use std::cmp::Ordering;
mod api;
mod platform;
mod events;
mod headless;
mod window;
pub mod os;
pub struct Window {
window: platform::Window,
}
pub trait GlContext {
unsafe fn make_current(&self) -> Result<(), ContextError>;
fn is_current(&self) -> bool;
fn get_proc_address(&self, addr: &str) -> *const libc::c_void;
fn swap_buffers(&self) -> Result<(), ContextError>;
fn get_api(&self) -> Api;
fn get_pixel_format(&self) -> PixelFormat;
}
#[derive(Debug)]
pub enum CreationError {
OsError(String),
NotSupported,
NoBackendAvailable(Box<std::error::Error + Send>),
RobustnessNotSupported,
OpenGlVersionNotSupported,
NoAvailablePixelFormat,
}
impl CreationError {
fn to_string(&self) -> &str {
match *self {
CreationError::OsError(ref text) => &text,
CreationError::NotSupported => "Some of the requested attributes are not supported",
CreationError::NoBackendAvailable(_) => "No backend is available",
CreationError::RobustnessNotSupported => "You requested robustness, but it is \
not supported.",
CreationError::OpenGlVersionNotSupported => "The requested OpenGL version is not \
supported.",
CreationError::NoAvailablePixelFormat => "Couldn't find any pixel format that matches \
the criterias.",
}
}
}
impl std::fmt::Display for CreationError {
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
formatter.write_str(self.to_string())
}
}
impl std::error::Error for CreationError {
fn description(&self) -> &str {
self.to_string()
}
fn cause(&self) -> Option<&std::error::Error> {
match *self {
CreationError::NoBackendAvailable(ref err) => Some(&**err),
_ => None
}
}
}
#[derive(Debug)]
pub enum ContextError {
IoError(io::Error),
ContextLost,
}
impl ContextError {
fn to_string(&self) -> &str {
use std::error::Error;
match *self {
ContextError::IoError(ref err) => err.description(),
ContextError::ContextLost => "Context lost"
}
}
}
impl std::fmt::Display for ContextError {
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
formatter.write_str(self.to_string())
}
}
impl std::error::Error for ContextError {
fn description(&self) -> &str {
self.to_string()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Api {
OpenGl,
OpenGlEs,
WebGl,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GlProfile {
Compatibility,
Core,
}
#[derive(Debug, Copy, Clone)]
pub enum GlRequest {
Latest,
Specific(Api, (u8, u8)),
GlThenGles {
opengl_version: (u8, u8),
opengles_version: (u8, u8),
},
}
impl GlRequest {
pub fn to_gl_version(&self) -> Option<(u8, u8)> {
match self {
&GlRequest::Specific(Api::OpenGl, version) => Some(version),
&GlRequest::GlThenGles { opengl_version: version, .. } => Some(version),
_ => None,
}
}
}
pub static GL_CORE: GlRequest = GlRequest::Specific(Api::OpenGl, (3, 2));
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Robustness {
NotRobust,
NoError,
RobustNoResetNotification,
TryRobustNoResetNotification,
RobustLoseContextOnReset,
TryRobustLoseContextOnReset,
}
#[derive(Debug, Copy, Clone)]
pub enum MouseCursor {
Default,
Crosshair,
Hand,
Arrow,
Move,
Text,
Wait,
Help,
Progress,
NotAllowed,
ContextMenu,
NoneCursor,
Cell,
VerticalText,
Alias,
Copy,
NoDrop,
Grab,
Grabbing,
AllScroll,
ZoomIn,
ZoomOut,
EResize,
NResize,
NeResize,
NwResize,
SResize,
SeResize,
SwResize,
WResize,
EwResize,
NsResize,
NeswResize,
NwseResize,
ColResize,
RowResize,
}
#[derive(Debug, Copy, Clone)]
pub enum CursorState {
Normal,
Hide,
Grab,
}
#[allow(missing_docs)]
#[derive(Debug, Clone)]
pub struct PixelFormat {
pub hardware_accelerated: bool,
pub color_bits: u8,
pub alpha_bits: u8,
pub depth_bits: u8,
pub stencil_bits: u8,
pub stereoscopy: bool,
pub double_buffer: bool,
pub multisampling: Option<u16>,
pub srgb: bool,
}
#[derive(Clone, Debug)]
#[allow(missing_docs)]
pub struct PixelFormatRequirements {
pub multisampling: Option<u16>,
pub depth_bits: Option<u8>,
pub stencil_bits: Option<u8>,
pub color_bits: Option<u8>,
pub alpha_bits: Option<u8>,
pub stereoscopy: bool,
pub srgb: Option<bool>,
}
impl PixelFormatRequirements {
#[cfg(not(target_os = "macos"))]
fn choose_pixel_format<T, I>(&self, iter: I) -> Result<(T, PixelFormat), CreationError>
where I: IntoIterator<Item=(T, PixelFormat)>, T: Clone
{
let iter = iter.into_iter().filter(|&(_, ref format)| {
if format.color_bits < self.color_bits.unwrap_or(0) {
return false;
}
if format.alpha_bits < self.alpha_bits.unwrap_or(0) {
return false;
}
if format.depth_bits < self.depth_bits.unwrap_or(0) {
return false;
}
if format.stencil_bits < self.stencil_bits.unwrap_or(0) {
return false;
}
if !format.stereoscopy && self.stereoscopy {
return false;
}
if let Some(req_ms) = self.multisampling {
match format.multisampling {
Some(val) if val >= req_ms => (),
_ => return false
}
} else {
if format.multisampling.is_some() {
return false;
}
}
if let Some(srgb) = self.srgb {
if srgb != format.srgb {
return false;
}
}
true
});
let mut formats = iter.collect::<Vec<_>>();
formats.sort_by(|&(_, ref left), &(_, ref right)| {
if left.hardware_accelerated && !right.hardware_accelerated {
return Ordering::Less;
} else if right.hardware_accelerated && !left.hardware_accelerated {
return Ordering::Greater;
}
if left.srgb && !right.srgb {
return Ordering::Less;
} else if right.srgb && !left.srgb {
return Ordering::Greater;
}
if left.color_bits + left.alpha_bits != right.color_bits + right.alpha_bits {
return (right.color_bits + right.alpha_bits)
.cmp(&(left.color_bits + left.alpha_bits));
}
if left.double_buffer && !right.double_buffer {
return Ordering::Less;
} else if right.double_buffer && !left.double_buffer {
return Ordering::Greater;
}
if left.depth_bits != right.depth_bits {
return (right.depth_bits).cmp(&left.depth_bits);
}
if left.stencil_bits != right.stencil_bits {
return (right.stencil_bits).cmp(&left.stencil_bits);
}
if left.multisampling.is_some() && right.multisampling.is_none() {
return Ordering::Less;
} else if right.multisampling.is_some() && left.multisampling.is_none() {
return Ordering::Greater;
}
return Ordering::Equal;
});
formats.into_iter().next().ok_or(CreationError::NoAvailablePixelFormat)
}
}
impl Default for PixelFormatRequirements {
#[inline]
fn default() -> PixelFormatRequirements {
PixelFormatRequirements {
multisampling: None,
depth_bits: None,
stencil_bits: None,
color_bits: None,
alpha_bits: None,
stereoscopy: false,
srgb: None,
}
}
}
#[derive(Clone)]
pub struct WindowAttributes {
pub dimensions: Option<(u32, u32)>,
pub monitor: Option<platform::MonitorId>,
pub title: String,
pub visible: bool,
pub transparent: bool,
pub decorations: bool,
pub multitouch: bool,
}
impl Default for WindowAttributes {
#[inline]
fn default() -> WindowAttributes {
WindowAttributes {
dimensions: None,
monitor: None,
title: "glutin window".to_owned(),
visible: true,
transparent: false,
decorations: true,
multitouch: false,
}
}
}
#[derive(Clone)]
pub struct GlAttributes<S> {
pub sharing: Option<S>,
pub version: GlRequest,
pub profile: Option<GlProfile>,
pub debug: bool,
pub robustness: Robustness,
pub vsync: bool,
}
impl<S> GlAttributes<S> {
#[inline]
pub fn map_sharing<F, T>(self, f: F) -> GlAttributes<T> where F: FnOnce(S) -> T {
GlAttributes {
sharing: self.sharing.map(f),
version: self.version,
profile: self.profile,
debug: self.debug,
robustness: self.robustness,
vsync: self.vsync,
}
}
}
impl<S> Default for GlAttributes<S> {
#[inline]
fn default() -> GlAttributes<S> {
GlAttributes {
sharing: None,
version: GlRequest::Latest,
profile: None,
debug: cfg!(debug_assertions),
robustness: Robustness::NotRobust,
vsync: false,
}
}
}
mod native_monitor {
#[derive(Clone, PartialEq, Eq)]
pub enum NativeMonitorId {
Numeric(u32),
Name(String),
Unavailable
}
}