aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/android/mod.rs1
-rw-r--r--src/cocoa/mod.rs14
-rw-r--r--src/lib.rs6
-rw-r--r--src/win32/callback.rs18
-rw-r--r--src/win32/init.rs43
-rw-r--r--src/win32/make_current_guard.rs5
-rw-r--r--src/win32/mod.rs10
-rw-r--r--src/x11/headless.rs12
-rw-r--r--src/x11/window/mod.rs57
9 files changed, 69 insertions, 97 deletions
diff --git a/src/android/mod.rs b/src/android/mod.rs
index 79590f4..5c7096c 100644
--- a/src/android/mod.rs
+++ b/src/android/mod.rs
@@ -385,7 +385,6 @@ impl WindowProxy {
}
}
-#[unsafe_destructor]
impl Drop for Window {
fn drop(&mut self) {
use std::ptr;
diff --git a/src/cocoa/mod.rs b/src/cocoa/mod.rs
index c26510c..ca9d888 100644
--- a/src/cocoa/mod.rs
+++ b/src/cocoa/mod.rs
@@ -13,7 +13,9 @@ use native_monitor::NativeMonitorId;
use objc::runtime::{Class, Object, Sel, BOOL, YES, NO};
use objc::declare::ClassDecl;
-use cocoa::base::{id, nil, NSUInteger};
+use cocoa::base::{id, nil};
+use cocoa::foundation::{NSAutoreleasePool, NSDate, NSDefaultRunLoopMode, NSPoint, NSRect, NSSize,
+ NSString, NSUInteger};
use cocoa::appkit;
use cocoa::appkit::*;
use cocoa::appkit::NSEventSubtype::*;
@@ -717,15 +719,5 @@ impl Clone for IdRef {
}
IdRef(self.0)
}
-
- fn clone_from(&mut self, source: &IdRef) {
- if source.0 != nil {
- let _: id = unsafe { msg_send![source.0, retain] };
- }
- if self.0 != nil {
- let _: () = unsafe { msg_send![self.0, release] };
- }
- self.0 = source.0;
- }
}
diff --git a/src/lib.rs b/src/lib.rs
index 813ec0c..1e10768 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,6 +1,3 @@
-#![feature(collections, unsafe_destructor, os, core, std_misc, alloc)]
-#![unstable]
-
//! The purpose of this library is to provide an OpenGL context on as many
//! platforms as possible.
//!
@@ -26,6 +23,9 @@
//!
//! By default only `window` is enabled.
+#[macro_use]
+extern crate lazy_static;
+
extern crate gl_common;
extern crate libc;
diff --git a/src/win32/callback.rs b/src/win32/callback.rs
index 66d88e7..e852eeb 100644
--- a/src/win32/callback.rs
+++ b/src/win32/callback.rs
@@ -1,12 +1,8 @@
use std::mem;
use std::ptr;
-use std::rc::Rc;
use std::cell::RefCell;
use std::sync::mpsc::Sender;
-use std::sync::{
- Arc,
- Mutex
-};
+use std::sync::{Arc, Mutex};
use CursorState;
use Event;
@@ -232,16 +228,12 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
if let Ok(cursor_state) = cstash.cursor_state.lock() {
match *cursor_state {
CursorState::Normal => {
- unsafe {
- user32::SetCursor(user32::LoadCursorW(
- ptr::null_mut(),
- winapi::IDC_ARROW));
- }
+ user32::SetCursor(user32::LoadCursorW(
+ ptr::null_mut(),
+ winapi::IDC_ARROW));
},
CursorState::Grab | CursorState::Hide => {
- unsafe {
- user32::SetCursor(ptr::null_mut());
- }
+ user32::SetCursor(ptr::null_mut());
}
}
}
diff --git a/src/win32/init.rs b/src/win32/init.rs
index c296a9d..d83c90f 100644
--- a/src/win32/init.rs
+++ b/src/win32/init.rs
@@ -1,11 +1,8 @@
use std::sync::atomic::AtomicBool;
-use std::sync::{
- Arc,
- Mutex
-};
+use std::sync::{Arc, Mutex};
+use std::io;
use std::ptr;
use std::mem;
-use std::os;
use std::thread;
use super::callback;
@@ -23,7 +20,8 @@ use CursorState;
use GlRequest;
use PixelFormat;
-use std::ffi::{CStr, CString};
+use std::ffi::{CStr, CString, OsStr};
+use std::os::windows::ffi::OsStrExt;
use std::sync::mpsc::channel;
use libc;
@@ -41,8 +39,9 @@ pub fn new_window(builder: BuilderAttribs<'static>, builder_sharelists: Option<C
-> Result<Window, CreationError>
{
// initializing variables to be sent to the task
- let title = builder.title.as_slice().utf16_units()
- .chain(Some(0).into_iter()).collect::<Vec<u16>>(); // title to utf16
+
+ let title = OsStr::from_str(&builder.title).encode_wide().chain(Some(0).into_iter())
+ .collect::<Vec<_>>();
let (tx, rx) = channel();
@@ -125,13 +124,13 @@ unsafe fn init(title: Vec<u16>, builder: BuilderAttribs<'static>,
if handle.is_null() {
return Err(OsError(format!("CreateWindowEx function failed: {}",
- os::error_string(os::errno()))));
+ format!("{}", io::Error::last_os_error()))));
}
let hdc = user32::GetDC(handle);
if hdc.is_null() {
let err = Err(OsError(format!("GetDC function failed: {}",
- os::error_string(os::errno()))));
+ format!("{}", io::Error::last_os_error()))));
return err;
}
@@ -191,13 +190,13 @@ unsafe fn init(title: Vec<u16>, builder: BuilderAttribs<'static>,
if handle.is_null() {
return Err(OsError(format!("CreateWindowEx function failed: {}",
- os::error_string(os::errno()))));
+ format!("{}", io::Error::last_os_error()))));
}
let hdc = user32::GetDC(handle);
if hdc.is_null() {
return Err(OsError(format!("GetDC function failed: {}",
- os::error_string(os::errno()))));
+ format!("{}", io::Error::last_os_error()))));
}
WindowWrapper(handle, hdc)
@@ -268,8 +267,8 @@ unsafe fn init(title: Vec<u16>, builder: BuilderAttribs<'static>,
}
unsafe fn register_window_class() -> Vec<u16> {
- let class_name: Vec<u16> = "Window Class".utf16_units().chain(Some(0).into_iter())
- .collect::<Vec<u16>>();
+ let class_name = OsStr::from_str("Window Class").encode_wide().chain(Some(0).into_iter())
+ .collect::<Vec<_>>();
let class = winapi::WNDCLASSEXW {
cbSize: mem::size_of::<winapi::WNDCLASSEXW>() as winapi::UINT,
@@ -377,7 +376,7 @@ unsafe fn create_context(extra: Option<(&gl::wgl_extra::Wgl, &BuilderAttribs<'st
Some(extra_functions.CreateContextAttribsARB(hdc.1 as *const libc::c_void,
share as *const libc::c_void,
- attributes.as_slice().as_ptr()))
+ attributes.as_ptr()))
} else {
None
@@ -399,7 +398,7 @@ unsafe fn create_context(extra: Option<(&gl::wgl_extra::Wgl, &BuilderAttribs<'st
if ctxt.is_null() {
return Err(OsError(format!("OpenGL context creation failed: {}",
- os::error_string(os::errno()))));
+ format!("{}", io::Error::last_os_error()))));
}
Ok(ContextWrapper(ctxt as winapi::HGLRC))
@@ -519,26 +518,26 @@ unsafe fn set_pixel_format(hdc: &WindowWrapper, id: libc::c_int) -> Result<(), C
as winapi::UINT, &mut output) == 0
{
return Err(OsError(format!("DescribePixelFormat function failed: {}",
- os::error_string(os::errno()))));
+ format!("{}", io::Error::last_os_error()))));
}
if gdi32::SetPixelFormat(hdc.1, id, &output) == 0 {
return Err(OsError(format!("SetPixelFormat function failed: {}",
- os::error_string(os::errno()))));
+ format!("{}", io::Error::last_os_error()))));
}
Ok(())
}
unsafe fn load_opengl32_dll() -> Result<winapi::HMODULE, CreationError> {
- let name = "opengl32.dll".utf16_units().chain(Some(0).into_iter())
- .collect::<Vec<u16>>().as_ptr();
+ let name = OsStr::from_str("opengl32.dll").encode_wide().chain(Some(0).into_iter())
+ .collect::<Vec<_>>();
- let lib = kernel32::LoadLibraryW(name);
+ let lib = kernel32::LoadLibraryW(name.as_ptr());
if lib.is_null() {
return Err(OsError(format!("LoadLibrary function failed: {}",
- os::error_string(os::errno()))));
+ format!("{}", io::Error::last_os_error()))));
}
Ok(lib)
diff --git a/src/win32/make_current_guard.rs b/src/win32/make_current_guard.rs
index d6bcc8e..8983899 100644
--- a/src/win32/make_current_guard.rs
+++ b/src/win32/make_current_guard.rs
@@ -1,5 +1,5 @@
use std::marker::PhantomData;
-use std::os;
+use std::io;
use libc;
use winapi;
@@ -30,7 +30,7 @@ impl<'a, 'b> CurrentContextGuard<'a, 'b> {
if result == 0 {
return Err(CreationError::OsError(format!("wglMakeCurrent function failed: {}",
- os::error_string(os::errno()))));
+ format!("{}", io::Error::last_os_error()))));
}
Ok(CurrentContextGuard {
@@ -42,7 +42,6 @@ impl<'a, 'b> CurrentContextGuard<'a, 'b> {
}
}
-#[unsafe_destructor]
impl<'a, 'b> Drop for CurrentContextGuard<'a, 'b> {
fn drop(&mut self) {
unsafe {
diff --git a/src/win32/mod.rs b/src/win32/mod.rs
index 2769bf1..be72d53 100644
--- a/src/win32/mod.rs
+++ b/src/win32/mod.rs
@@ -2,6 +2,8 @@ use std::sync::atomic::AtomicBool;
use std::mem;
use std::ptr;
use std::ffi::CString;
+use std::ffi::OsStr;
+use std::os::windows::ffi::OsStrExt;
use std::sync::{
Arc,
Mutex
@@ -109,10 +111,11 @@ impl Window {
///
/// Calls SetWindowText on the HWND.
pub fn set_title(&self, text: &str) {
+ let text = OsStr::from_str(text).encode_wide().chain(Some(0).into_iter())
+ .collect::<Vec<_>>();
+
unsafe {
- user32::SetWindowTextW(self.window.0,
- text.utf16_units().chain(Some(0).into_iter())
- .collect::<Vec<u16>>().as_ptr() as winapi::LPCWSTR);
+ user32::SetWindowTextW(self.window.0, text.as_ptr() as winapi::LPCWSTR);
}
}
@@ -395,7 +398,6 @@ impl<'a> Iterator for WaitEventsIterator<'a> {
}
}
-#[unsafe_destructor]
impl Drop for Window {
fn drop(&mut self) {
unsafe {
diff --git a/src/x11/headless.rs b/src/x11/headless.rs
index e23dfa7..291f04d 100644
--- a/src/x11/headless.rs
+++ b/src/x11/headless.rs
@@ -5,12 +5,6 @@ use libc;
use std::{mem, ptr};
use super::ffi;
-fn with_c_str<F, T>(s: &str, f: F) -> T where F: FnOnce(*const libc::c_char) -> T {
- use std::ffi::CString;
- let c_str = CString::from_slice(s.as_bytes());
- f(c_str.as_ptr())
-}
-
pub struct HeadlessContext {
context: ffi::OSMesaContext,
buffer: Vec<u32>,
@@ -53,9 +47,9 @@ impl HeadlessContext {
pub fn get_proc_address(&self, addr: &str) -> *const () {
unsafe {
- with_c_str(addr, |s| {
- ffi::OSMesaGetProcAddress(mem::transmute(s)) as *const ()
- })
+ use std::ffi::CString;
+ let c_str = CString::new(addr.as_bytes().to_vec()).unwrap();
+ ffi::OSMesaGetProcAddress(mem::transmute(c_str.as_ptr())) as *const ()
}
}
diff --git a/src/x11/window/mod.rs b/src/x11/window/mod.rs
index 7c93ad3..fa8f91c 100644
--- a/src/x11/window/mod.rs
+++ b/src/x11/window/mod.rs
@@ -8,7 +8,6 @@ use std::sync::atomic::AtomicBool;
use std::collections::VecDeque;
use super::ffi;
use std::sync::{Arc, Mutex, Once, ONCE_INIT, Weak};
-use std::sync::{StaticMutex, MUTEX_INIT};
use Api;
use CursorState;
@@ -21,6 +20,11 @@ mod monitor;
static THREAD_INIT: Once = ONCE_INIT;
+// XOpenIM doesn't seem to be thread-safe
+lazy_static! { // TODO: use a static mutex when that's possible, and put me back in my function
+ static ref GLOBAL_XOPENIM_LOCK: Mutex<()> = Mutex::new(());
+}
+
fn x_error_callback(_: *mut ffi::Display, event: *mut ffi::XErrorEvent) -> libc::c_int {
unsafe {
println!("[glutin] x error code={} major={} minor={}!", (*event).error_code, (*event).request_code, (*event).minor_code);
@@ -39,7 +43,7 @@ fn ensure_thread_init() {
fn with_c_str<F, T>(s: &str, f: F) -> T where F: FnOnce(*const libc::c_char) -> T {
use std::ffi::CString;
- let c_str = CString::from_slice(s.as_bytes());
+ let c_str = CString::new(s.as_bytes().to_vec()).unwrap();
f(c_str.as_ptr())
}
@@ -82,30 +86,25 @@ impl Drop for XWindow {
#[derive(Clone)]
pub struct WindowProxy {
- x: Weak<XWindow>,
+ x: Arc<XWindow>,
}
impl WindowProxy {
pub fn wakeup_event_loop(&self) {
- match self.x.upgrade() {
- Some(x) => {
- let mut xev = ffi::XClientMessageEvent {
- type_: ffi::ClientMessage,
- window: x.window,
- format: 32,
- message_type: 0,
- serial: 0,
- send_event: 0,
- display: x.display,
- l: [0, 0, 0, 0, 0],
- };
+ let mut xev = ffi::XClientMessageEvent {
+ type_: ffi::ClientMessage,
+ window: self.x.window,
+ format: 32,
+ message_type: 0,
+ serial: 0,
+ send_event: 0,
+ display: self.x.display,
+ l: [0, 0, 0, 0, 0],
+ };
- unsafe {
- ffi::XSendEvent(x.display, x.window, 0, 0, mem::transmute(&mut xev));
- ffi::XFlush(x.display);
- }
- }
- None => {}
+ unsafe {
+ ffi::XSendEvent(self.x.display, self.x.window, 0, 0, mem::transmute(&mut xev));
+ ffi::XFlush(self.x.display);
}
}
}
@@ -118,15 +117,13 @@ impl<'a> Iterator for PollEventsIterator<'a> {
type Item = Event;
fn next(&mut self) -> Option<Event> {
- use std::num::Int;
-
if let Some(ev) = self.window.pending_events.lock().unwrap().pop_front() {
return Some(ev);
}
loop {
let mut xev = unsafe { mem::uninitialized() };
- let res = unsafe { ffi::XCheckMaskEvent(self.window.x.display, Int::max_value(), &mut xev) };
+ let res = unsafe { ffi::XCheckMaskEvent(self.window.x.display, -1, &mut xev) };
if res == 0 {
let res = unsafe { ffi::XCheckTypedEvent(self.window.x.display, ffi::ClientMessage, &mut xev) };
@@ -192,12 +189,12 @@ impl<'a> Iterator for PollEventsIterator<'a> {
mem::transmute(buffer.as_mut_ptr()),
buffer.len() as libc::c_int, ptr::null_mut(), ptr::null_mut());
- str::from_utf8(&buffer.as_slice()[..count as usize]).unwrap_or("").to_string()
+ str::from_utf8(&buffer[..count as usize]).unwrap_or("").to_string()
};
{
let mut pending = self.window.pending_events.lock().unwrap();
- for chr in written.as_slice().chars() {
+ for chr in written.chars() {
pending.push_back(ReceivedCharacter(chr));
}
}
@@ -445,8 +442,6 @@ impl Window {
// creating IM
let im = unsafe {
- // XOpenIM doesn't seem to be thread-safe
- static GLOBAL_XOPENIM_LOCK: StaticMutex = MUTEX_INIT;
let _lock = GLOBAL_XOPENIM_LOCK.lock().unwrap();
let im = ffi::XOpenIM(display, ptr::null(), ptr::null_mut(), ptr::null_mut());
@@ -681,7 +676,7 @@ impl Window {
pub fn create_window_proxy(&self) -> WindowProxy {
WindowProxy {
- x: self.x.downgrade()
+ x: self.x.clone()
}
}
@@ -778,7 +773,7 @@ impl Window {
MouseCursor::AllScroll | MouseCursor::ZoomIn |
MouseCursor::ZoomOut => "left_ptr",
};
- let c_string = CString::from_slice(cursor_name.as_bytes());
+ let c_string = CString::new(cursor_name.as_bytes().to_vec()).unwrap();
let xcursor = ffi::XcursorLibraryLoadCursor(self.x.display, c_string.as_ptr());
ffi::XDefineCursor (self.x.display, self.x.window, xcursor);
ffi::XFlush(self.x.display);
@@ -828,7 +823,7 @@ impl Window {
1.0
}
- pub fn set_cursor_position(&self, x: i32, y: i32) -> Result<(), ()> {
+ pub fn set_cursor_position(&self, _x: i32, _y: i32) -> Result<(), ()> {
unimplemented!();
}
}