#![feature(unsafe_destructor)]
#![unstable]
extern crate gl_common;
extern crate libc;
#[cfg(target_os = "windows")]
extern crate winapi;
#[cfg(target_os = "macos")]
extern crate cocoa;
#[cfg(target_os = "macos")]
extern crate core_foundation;
#[cfg(target_os = "macos")]
extern crate core_graphics;
pub use events::*;
use std::default::Default;
use std::collections::ring_buf::IntoIter as RingBufIter;
#[cfg(all(not(target_os = "windows"), not(target_os = "linux"), not(target_os = "macos"), not(target_os = "android")))]
use this_platform_is_not_supported;
#[cfg(target_os = "windows")]
#[path="win32/mod.rs"]
mod winimpl;
#[cfg(target_os = "linux")]
#[path="x11/mod.rs"]
mod winimpl;
#[cfg(target_os = "macos")]
#[path="osx/mod.rs"]
mod winimpl;
#[cfg(target_os = "android")]
#[path="android/mod.rs"]
mod winimpl;
mod events;
#[cfg(feature = "window")]
pub struct MonitorID(winimpl::MonitorID);
#[derive(Clone, Show, PartialEq, Eq)]
pub enum CreationError {
OsError(String),
NotSupported,
}
impl std::error::Error for CreationError {
fn description(&self) -> &str {
match self {
&CreationError::OsError(ref text) => text.as_slice(),
&CreationError::NotSupported => "Some of the requested attributes are not supported",
}
}
}
#[derive(Show, Clone, Copy, PartialEq, Eq)]
pub enum Api {
OpenGl,
OpenGlEs,
}
#[cfg(feature = "window")]
pub struct WindowBuilder<'a> {
attribs: BuilderAttribs<'a>
}
struct BuilderAttribs<'a> {
headless: bool,
strict: bool,
sharing: Option<&'a winimpl::Window>,
dimensions: Option<(uint, uint)>,
title: String,
monitor: Option<winimpl::MonitorID>,
gl_version: Option<(uint, uint)>,
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,
}
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: None,
gl_debug: cfg!(ndebug),
vsync: false,
visible: true,
multisampling: None,
depth_bits: None,
stencil_bits: None,
color_bits: None,
alpha_bits: None,
stereoscopy: false,
}
}
}
#[cfg(feature = "window")]
impl<'a> WindowBuilder<'a> {
pub fn new() -> WindowBuilder<'a> {
WindowBuilder {
attribs: BuilderAttribs::new(),
}
}
pub fn with_dimensions(mut self, width: uint, height: uint) -> WindowBuilder<'a> {
self.attribs.dimensions = Some((width, height));
self
}
pub fn with_title(mut self, title: String) -> WindowBuilder<'a> {
self.attribs.title = title;
self
}
pub fn with_fullscreen(mut self, monitor: MonitorID) -> WindowBuilder<'a> {
let MonitorID(monitor) = monitor;
self.attribs.monitor = Some(monitor);
self
}
pub fn with_shared_lists(mut self, other: &'a Window) -> WindowBuilder<'a> {
self.attribs.sharing = Some(&other.window);
self
}
pub fn with_gl_version(mut self, version: (uint, uint)) -> WindowBuilder<'a> {
self.attribs.gl_version = Some(version);
self
}
pub fn with_gl_debug_flag(mut self, flag: bool) -> WindowBuilder<'a> {
self.attribs.gl_debug = flag;
self
}
pub fn with_vsync(mut self) -> WindowBuilder<'a> {
self.attribs.vsync = true;
self
}
pub fn with_visibility(mut self, visible: bool) -> WindowBuilder<'a> {
self.attribs.visible = visible;
self
}
pub fn with_multisampling(mut self, samples: u16) -> WindowBuilder<'a> {
use std::num::UnsignedInt;
assert!(samples.is_power_of_two());
self.attribs.multisampling = Some(samples);
self
}
pub fn with_depth_buffer(mut self, bits: u8) -> WindowBuilder<'a> {
self.attribs.depth_bits = Some(bits);
self
}
pub fn with_stencil_buffer(mut self, bits: u8) -> WindowBuilder<'a> {
self.attribs.stencil_bits = Some(bits);
self
}
pub fn with_pixel_format(mut self, color_bits: u8, alpha_bits: u8) -> WindowBuilder<'a> {
self.attribs.color_bits = Some(color_bits);
self.attribs.alpha_bits = Some(alpha_bits);
self
}
pub fn with_stereoscopy(mut self) -> WindowBuilder<'a> {
self.attribs.stereoscopy = true;
self
}
pub fn build(mut self) -> Result<Window, CreationError> {
if self.attribs.dimensions.is_none() && self.attribs.monitor.is_some() {
self.attribs.dimensions = Some(self.attribs.monitor.as_ref().unwrap().get_dimensions())
}
if self.attribs.dimensions.is_none() {
self.attribs.dimensions = Some((1024, 768));
}
winimpl::Window::new(self.attribs).map(|w| Window { window: w })
}
pub fn build_strict(mut self) -> Result<Window, CreationError> {
self.attribs.strict = true;
self.build()
}
}
#[cfg(feature = "headless")]
pub struct HeadlessRendererBuilder {
attribs: BuilderAttribs<'static>,
}
#[cfg(feature = "headless")]
impl HeadlessRendererBuilder {
pub fn new(width: uint, height: uint) -> HeadlessRendererBuilder {
HeadlessRendererBuilder {
attribs: BuilderAttribs {
headless: true,
dimensions: Some((width, height)),
.. BuilderAttribs::new()
},
}
}
pub fn with_gl_version(mut self, version: (uint, uint)) -> HeadlessRendererBuilder {
self.attribs.gl_version = Some(version);
self
}
pub fn with_gl_debug_flag(mut self, flag: bool) -> HeadlessRendererBuilder {
self.attribs.gl_debug = flag;
self
}
pub fn build(self) -> Result<HeadlessContext, CreationError> {
winimpl::HeadlessContext::new(self.attribs).map(|w| HeadlessContext { context: w })
}
pub fn build_strict(mut self) -> Result<HeadlessContext, CreationError> {
self.attribs.strict = true;
self.build()
}
}
#[cfg(feature = "window")]
pub struct Window {
window: winimpl::Window,
}
#[cfg(feature = "window")]
impl Default for Window {
fn default() -> Window {
Window::new().unwrap()
}
}
#[cfg(feature = "window")]
impl Window {
#[inline]
#[cfg(feature = "window")]
pub fn new() -> Result<Window, CreationError> {
let builder = WindowBuilder::new();
builder.build()
}
#[inline]
pub fn is_closed(&self) -> bool {
self.window.is_closed()
}
#[inline]
#[deprecated = "Use is_closed instead"]
pub fn should_close(&self) -> bool {
self.is_closed()
}
#[inline]
pub fn set_title(&self, title: &str) {
self.window.set_title(title)
}
#[inline]
pub fn show(&self) {
self.window.show()
}
#[inline]
pub fn hide(&self) {
self.window.hide()
}
#[inline]
pub fn get_position(&self) -> Option<(int, int)> {
self.window.get_position()
}
#[inline]
pub fn set_position(&self, x: int, y: int) {
self.window.set_position(x, y)
}
#[inline]
pub fn get_inner_size(&self) -> Option<(uint, uint)> {
self.window.get_inner_size()
}
#[inline]
pub fn get_outer_size(&self) -> Option<(uint, uint)> {
self.window.get_outer_size()
}
#[inline]
pub fn set_inner_size(&self, x: uint, y: uint) {
self.window.set_inner_size(x, y)
}
#[inline]
pub fn poll_events(&self) -> PollEventsIterator {
PollEventsIterator { data: self.window.poll_events().into_iter() }
}
#[inline]
pub fn wait_events(&self) -> WaitEventsIterator {
WaitEventsIterator { data: self.window.wait_events().into_iter() }
}
#[inline]
pub unsafe fn make_current(&self) {
self.window.make_current()
}
#[inline]
pub fn get_proc_address(&self, addr: &str) -> *const libc::c_void {
self.window.get_proc_address(addr) as *const libc::c_void
}
#[inline]
pub fn swap_buffers(&self) {
self.window.swap_buffers()
}
#[inline]
pub unsafe fn platform_display(&self) -> *mut libc::c_void {
self.window.platform_display()
}
pub fn get_api(&self) -> Api {
self.window.get_api()
}
#[inline]
pub fn create_window_proxy(&self) -> WindowProxy {
WindowProxy {
proxy: self.window.create_window_proxy()
}
}
#[experimental]
pub fn set_window_resize_callback(&mut self, callback: Option<fn(uint, uint)>) {
self.window.set_window_resize_callback(callback);
}
}
#[cfg(feature = "window")]
impl gl_common::GlFunctionsSource for Window {
fn get_proc_addr(&self, addr: &str) -> *const libc::c_void {
self.get_proc_address(addr)
}
}
#[cfg(feature = "window")]
#[derive(Clone)]
pub struct WindowProxy {
proxy: winimpl::WindowProxy,
}
#[cfg(feature = "window")]
impl WindowProxy {
#[inline]
pub fn wakeup_event_loop(&self) {
self.proxy.wakeup_event_loop();
}
}
#[cfg(feature = "headless")]
pub struct HeadlessContext {
context: winimpl::HeadlessContext,
}
#[cfg(feature = "headless")]
impl HeadlessContext {
#[inline]
pub unsafe fn make_current(&self) {
self.context.make_current()
}
#[inline]
pub fn get_proc_address(&self, addr: &str) -> *const libc::c_void {
self.context.get_proc_address(addr) as *const libc::c_void
}
pub fn get_api(&self) -> Api {
self.context.get_api()
}
#[experimental]
pub fn set_window_resize_callback(&mut self, _: Option<fn(uint, uint)>) {
}
}
#[cfg(feature = "headless")]
impl gl_common::GlFunctionsSource for HeadlessContext {
fn get_proc_addr(&self, addr: &str) -> *const libc::c_void {
self.get_proc_address(addr)
}
}
pub struct PollEventsIterator<'a> {
data: RingBufIter<Event>,
}
impl<'a> Iterator for PollEventsIterator<'a> {
type Item = Event;
fn next(&mut self) -> Option<Event> {
self.data.next()
}
}
pub struct WaitEventsIterator<'a> {
data: RingBufIter<Event>,
}
impl<'a> Iterator for WaitEventsIterator<'a> {
type Item = Event;
fn next(&mut self) -> Option<Event> {
self.data.next()
}
}
#[cfg(feature = "window")]
pub struct AvailableMonitorsIter {
data: RingBufIter<winimpl::MonitorID>,
}
#[cfg(feature = "window")]
impl Iterator for AvailableMonitorsIter {
type Item = MonitorID;
fn next(&mut self) -> Option<MonitorID> {
self.data.next().map(|id| MonitorID(id))
}
}
#[cfg(feature = "window")]
pub fn get_available_monitors() -> AvailableMonitorsIter {
let data = winimpl::get_available_monitors();
AvailableMonitorsIter{ data: data.into_iter() }
}
#[cfg(feature = "window")]
pub fn get_primary_monitor() -> MonitorID {
MonitorID(winimpl::get_primary_monitor())
}
#[cfg(feature = "window")]
impl MonitorID {
pub fn get_name(&self) -> Option<String> {
let &MonitorID(ref id) = self;
id.get_name()
}
pub fn get_dimensions(&self) -> (uint, uint) {
let &MonitorID(ref id) = self;
id.get_dimensions()
}
}