Merge pull request #187 from tomaka/update-rustc

Update for Rustc
This commit is contained in:
tomaka 2015-01-08 13:44:26 +01:00
commit 181e4a1d8f
10 changed files with 52 additions and 58 deletions

View file

@ -1,7 +1,5 @@
#![feature(phase)]
#[cfg(target_os = "android")] #[cfg(target_os = "android")]
#[phase(plugin, link)] #[macro_use]
extern crate android_glue; extern crate android_glue;
extern crate glutin; extern crate glutin;

View file

@ -1,7 +1,5 @@
#![feature(phase)]
#[cfg(target_os = "android")] #[cfg(target_os = "android")]
#[phase(plugin, link)] #[macro_use]
extern crate android_glue; extern crate android_glue;
extern crate glutin; extern crate glutin;

View file

@ -1,8 +1,5 @@
#![cfg(feature = "window")] #![cfg(feature = "window")]
#[phase(plugin)]
extern crate gl_generator;
use glutin; use glutin;
#[cfg(not(target_os = "android"))] #[cfg(not(target_os = "android"))]
@ -23,12 +20,12 @@ pub struct Context {
pub fn load(window: &glutin::Window) -> Context { pub fn load(window: &glutin::Window) -> Context {
let gl = gl::Gl::load(window); let gl = gl::Gl::load(window);
let version = { let version = unsafe {
use std::c_str::CString; use std::ffi;
unsafe { CString::new(gl.GetString(gl::VERSION) as *const i8, false) } ffi::c_str_to_bytes(&(gl.GetString(gl::VERSION) as *const i8)).to_string()
}; };
println!("OpenGL version {}", version.as_str().unwrap()); println!("OpenGL version {}", version);
Context { gl: gl } Context { gl: gl }
} }

View file

@ -1,7 +1,5 @@
#![feature(phase)]
#[cfg(target_os = "android")] #[cfg(target_os = "android")]
#[phase(plugin, link)] #[macro_use]
extern crate android_glue; extern crate android_glue;
extern crate glutin; extern crate glutin;

View file

@ -1,8 +1,5 @@
#![feature(unsafe_destructor)] #![feature(unsafe_destructor)]
#![feature(globs)]
#![feature(phase)]
#![unstable] #![unstable]
#![feature(associated_types)]
//! The purpose of this library is to provide an OpenGL context on as many //! The purpose of this library is to provide an OpenGL context on as many
//! platforms as possible. //! platforms as possible.

View file

@ -6,6 +6,7 @@ use {CreationError, Event};
use CreationError::OsError; use CreationError::OsError;
use std::cell::RefCell; use std::cell::RefCell;
use std::ffi::CString;
use std::rc::Rc; use std::rc::Rc;
use std::sync::mpsc::{Sender, Receiver, channel}; use std::sync::mpsc::{Sender, Receiver, channel};
@ -224,11 +225,12 @@ pub fn new_window(builder_dimensions: Option<(uint, uint)>, builder_title: Strin
// loading the extra WGL functions // loading the extra WGL functions
let extra_functions = gl::wgl_extra::Wgl::load_with(|addr| { let extra_functions = gl::wgl_extra::Wgl::load_with(|addr| {
use libc; use libc;
use std::c_str::ToCStr;
let addr = CString::from_slice(addr.as_bytes());
let addr = addr.as_slice_with_nul().as_ptr();
unsafe { unsafe {
let addr = addr.to_c_str(); gl::wgl::GetProcAddress(addr) as *const libc::c_void
gl::wgl::GetProcAddress(addr.as_ptr()) as *const libc::c_void
} }
}); });

View file

@ -1,5 +1,6 @@
use std::sync::atomic::AtomicBool; use std::sync::atomic::AtomicBool;
use std::ptr; use std::ptr;
use std::ffi::CString;
use std::collections::RingBuf; use std::collections::RingBuf;
use std::sync::mpsc::Receiver; use std::sync::mpsc::Receiver;
use libc; use libc;
@ -256,14 +257,13 @@ impl Window {
/// See the docs in the crate root file. /// See the docs in the crate root file.
pub fn get_proc_address(&self, addr: &str) -> *const () { pub fn get_proc_address(&self, addr: &str) -> *const () {
use std::c_str::ToCStr; let addr = CString::from_slice(addr.as_bytes());
let addr = addr.as_slice_with_nul().as_ptr();
unsafe { unsafe {
addr.with_c_str(|s| { let p = gl::wgl::GetProcAddress(addr) as *const ();
let p = gl::wgl::GetProcAddress(s) as *const (); if !p.is_null() { return p; }
if !p.is_null() { return p; } winapi::GetProcAddress(self.gl_library, addr) as *const ()
winapi::GetProcAddress(self.gl_library, s) as *const ()
})
} }
} }

View file

@ -5,6 +5,12 @@ use libc;
use std::{mem, ptr}; use std::{mem, ptr};
use super::ffi; use super::ffi;
fn with_c_str<F, T>(s: &str, f: F) -> T where F: FnOnce(*const i8) -> T {
use std::ffi::CString;
let c_str = CString::from_slice(s.as_bytes());
f(c_str.as_slice_with_nul().as_ptr())
}
pub struct HeadlessContext { pub struct HeadlessContext {
context: ffi::OSMesaContext, context: ffi::OSMesaContext,
buffer: Vec<u32>, buffer: Vec<u32>,
@ -41,10 +47,8 @@ impl HeadlessContext {
} }
pub fn get_proc_address(&self, addr: &str) -> *const () { pub fn get_proc_address(&self, addr: &str) -> *const () {
use std::c_str::ToCStr;
unsafe { unsafe {
addr.with_c_str(|s| { with_c_str(addr, |s| {
ffi::OSMesaGetProcAddress(mem::transmute(s)) as *const () ffi::OSMesaGetProcAddress(mem::transmute(s)) as *const ()
}) })
} }

View file

@ -17,13 +17,19 @@ mod monitor;
static THREAD_INIT: Once = ONCE_INIT; static THREAD_INIT: Once = ONCE_INIT;
fn ensure_thread_init() { fn ensure_thread_init() {
THREAD_INIT.doit(|| { THREAD_INIT.call_once(|| {
unsafe { unsafe {
ffi::XInitThreads(); ffi::XInitThreads();
} }
}); });
} }
fn with_c_str<F, T>(s: &str, f: F) -> T where F: FnOnce(*const i8) -> T {
use std::ffi::CString;
let c_str = CString::from_slice(s.as_bytes());
f(c_str.as_slice_with_nul().as_ptr())
}
struct XWindow { struct XWindow {
display: *mut ffi::Display, display: *mut ffi::Display,
window: ffi::Window, window: ffi::Window,
@ -234,13 +240,13 @@ impl Window {
// creating window, step 2 // creating window, step 2
let wm_delete_window = unsafe { let wm_delete_window = unsafe {
use std::c_str::ToCStr; let mut wm_delete_window = with_c_str("WM_DELETE_WINDOW", |delete_window|
ffi::XInternAtom(display, delete_window, 0)
let delete_window = "WM_DELETE_WINDOW".to_c_str(); );
let mut wm_delete_window = ffi::XInternAtom(display, delete_window.as_ptr(), 0);
ffi::XSetWMProtocols(display, window, &mut wm_delete_window, 1); ffi::XSetWMProtocols(display, window, &mut wm_delete_window, 1);
let c_title = builder.title.to_c_str(); with_c_str(&*builder.title, |title| {;
ffi::XStoreName(display, window, c_title.as_ptr()); ffi::XStoreName(display, window, title);
});
ffi::XFlush(display); ffi::XFlush(display);
wm_delete_window wm_delete_window
@ -257,13 +263,15 @@ impl Window {
// creating input context // creating input context
let ic = unsafe { let ic = unsafe {
use std::c_str::ToCStr; let ic = with_c_str("inputStyle", |input_style|
with_c_str("clientWindow", |client_window|
let input_style = "inputStyle".to_c_str(); ffi::XCreateIC(
let client_window = "clientWindow".to_c_str(); im, input_style,
let ic = ffi::XCreateIC(im, input_style.as_ptr(), ffi::XIMPreeditNothing | ffi::XIMStatusNothing, client_window,
ffi::XIMPreeditNothing | ffi::XIMStatusNothing, client_window.as_ptr(), window, ptr::null()
window, ptr::null()); )
)
);
if ic.is_null() { if ic.is_null() {
return Err(OsError(format!("XCreateIC failed"))); return Err(OsError(format!("XCreateIC failed")));
} }
@ -302,8 +310,7 @@ impl Window {
// loading the extra GLX functions // loading the extra GLX functions
let extra_functions = ffi::glx_extra::Glx::load_with(|addr| { let extra_functions = ffi::glx_extra::Glx::load_with(|addr| {
use std::c_str::ToCStr; with_c_str(addr, |s| {
addr.with_c_str(|s| {
use libc; use libc;
ffi::glx::GetProcAddress(s as *const u8) as *const libc::c_void ffi::glx::GetProcAddress(s as *const u8) as *const libc::c_void
}) })
@ -356,12 +363,10 @@ impl Window {
} }
pub fn set_title(&self, title: &str) { pub fn set_title(&self, title: &str) {
use std::c_str::ToCStr; with_c_str(title, |title| unsafe {
let c_title = title.to_c_str(); ffi::XStoreName(self.x.display, self.x.window, title);
unsafe {
ffi::XStoreName(self.x.display, self.x.window, c_title.as_ptr());
ffi::XFlush(self.x.display); ffi::XFlush(self.x.display);
} })
} }
pub fn show(&self) { pub fn show(&self) {
@ -582,11 +587,10 @@ impl Window {
} }
pub fn get_proc_address(&self, addr: &str) -> *const () { pub fn get_proc_address(&self, addr: &str) -> *const () {
use std::c_str::ToCStr;
use std::mem; use std::mem;
unsafe { unsafe {
addr.with_c_str(|s| { with_c_str(addr, |s| {
ffi::glx::GetProcAddress(mem::transmute(s)) as *const () ffi::glx::GetProcAddress(mem::transmute(s)) as *const ()
}) })
} }

View file

@ -1,7 +1,3 @@
#![feature(phase)]
#[phase(plugin)]
extern crate gl_generator;
extern crate glutin; extern crate glutin;
extern crate libc; extern crate libc;