#[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 gdi32;
#[cfg(target_os = "windows")]
extern crate user32;
#[cfg(target_os = "macos")]
#[macro_use]
extern crate objc;
#[cfg(target_os = "macos")]
extern crate cocoa;
#[cfg(target_os = "macos")]
extern crate core_foundation;
#[cfg(target_os = "macos")]
extern crate glutin_core_graphics as core_graphics;
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
extern crate x11_dl;
pub use events::*;
pub use headless::{HeadlessRendererBuilder, HeadlessContext};
#[cfg(feature = "window")]
pub use window::{WindowBuilder, Window, WindowProxy, PollEventsIterator, WaitEventsIterator};
#[cfg(feature = "window")]
pub use window::{AvailableMonitorsIter, MonitorID, get_available_monitors, get_primary_monitor};
#[cfg(feature = "window")]
pub use native_monitor::NativeMonitorId;
mod api;
mod platform;
mod events;
mod headless;
#[cfg(feature = "window")]
mod window;
pub trait GlContext {
unsafe fn make_current(&self);
fn is_current(&self) -> bool;
fn get_proc_address(&self, addr: &str) -> *const libc::c_void;
fn swap_buffers(&self);
fn get_api(&self) -> Api;
fn get_pixel_format(&self) -> PixelFormat;
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CreationError {
OsError(String),
NotSupported,
}
impl CreationError {
fn to_string(&self) -> &str {
match *self {
CreationError::OsError(ref text) => &text,
CreationError::NotSupported => "Some of the requested attributes are not supported",
}
}
}
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()
}
}
#[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)]
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,
}
#[doc(hidden)]
pub struct BuilderAttribs<'a> {
#[allow(dead_code)]
headless: bool,
strict: bool,
sharing: Option<&'a platform::Window>,
dimensions: Option<(u32, u32)>,
title: String,
monitor: Option<platform::MonitorID>,
gl_version: GlRequest,
gl_profile: Option<GlProfile>,
gl_debug: bool,
vsync: bool,
visible: bool,
multisampling: Option<u16>,
depth_bits: Option<u8>,
stencil_bits: Option<u8>,
color_bits: Option<u8>,
alpha_bits: Option<u8>,
stereoscopy: bool,
srgb: Option<bool>,
}
impl BuilderAttribs<'static> {
fn new() -> BuilderAttribs<'static> {
BuilderAttribs {
headless: false,
strict: false,
sharing: None,
dimensions: None,
title: "glutin window".to_string(),
monitor: None,
gl_version: GlRequest::Latest,
gl_profile: None,
gl_debug: cfg!(debug_assertions),
vsync: false,
visible: true,
multisampling: None,
depth_bits: None,
stencil_bits: None,
color_bits: None,
alpha_bits: None,
stereoscopy: false,
srgb: None,
}
}
}
impl<'a> BuilderAttribs<'a> {
#[allow(dead_code)]
fn extract_non_static(mut self) -> (BuilderAttribs<'static>, Option<&'a platform::Window>) {
let sharing = self.sharing.take();
let new_attribs = BuilderAttribs {
headless: self.headless,
strict: self.strict,
sharing: None,
dimensions: self.dimensions,
title: self.title,
monitor: self.monitor,
gl_version: self.gl_version,
gl_profile: self.gl_profile,
gl_debug: self.gl_debug,
vsync: self.vsync,
visible: self.visible,
multisampling: self.multisampling,
depth_bits: self.depth_bits,
stencil_bits: self.stencil_bits,
color_bits: self.color_bits,
alpha_bits: self.alpha_bits,
stereoscopy: self.stereoscopy,
srgb: self.srgb,
};
(new_attribs, sharing)
}
fn choose_pixel_format<T, I>(&self, iter: I) -> Result<(T, PixelFormat), CreationError>
where I: Iterator<Item=(T, PixelFormat)>, T: Clone
{
let mut current_result = None;
let mut current_software_result = None;
for (id, format) in iter {
if format.color_bits < self.color_bits.unwrap_or(0) {
continue;
}
if format.alpha_bits < self.alpha_bits.unwrap_or(0) {
continue;
}
if format.depth_bits < self.depth_bits.unwrap_or(0) {
continue;
}
if format.stencil_bits < self.stencil_bits.unwrap_or(0) {
continue;
}
if !format.stereoscopy && self.stereoscopy {
continue;
}
if self.multisampling.is_some() && format.multisampling.is_none() {
continue;
}
if let Some(srgb) = self.srgb {
if srgb != format.srgb {
continue;
}
}
current_software_result = Some((id.clone(), format.clone()));
if format.hardware_accelerated {
current_result = Some((id, format));
}
}
current_result.or(current_software_result)
.ok_or(CreationError::NotSupported)
}
}
mod native_monitor {
#[derive(PartialEq, Eq)]
pub enum NativeMonitorId {
Numeric(u32),
Name(String),
Unavailable
}
}