Merge pull request #191 from sbward/osx-1.0-alpha

OSX compatibility for Rust 1.0 alpha
This commit is contained in:
tomaka 2015-01-12 21:21:53 +01:00
commit 318f0d2d06
3 changed files with 26 additions and 16 deletions

View file

@ -37,6 +37,12 @@ git = "https://github.com/servo/rust-core-graphics"
[target.x86_64-apple-darwin.dependencies.core_graphics] [target.x86_64-apple-darwin.dependencies.core_graphics]
git = "https://github.com/servo/rust-core-graphics" git = "https://github.com/servo/rust-core-graphics"
[target.i686-apple-darwin.dependencies.core_foundation]
git = "https://github.com/servo/rust-core-foundation"
[target.x86_64-apple-darwin.dependencies.core_foundation]
git = "https://github.com/servo/rust-core-foundation"
[target.i686-pc-windows-gnu.dependencies.winapi] [target.i686-pc-windows-gnu.dependencies.winapi]
version = "*" version = "*"
features = ["gdi32", "kernel32", "user32"] features = ["gdi32", "kernel32", "user32"]

View file

@ -18,10 +18,13 @@ use core_foundation::string::CFString;
use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFunctionPointerForName}; use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFunctionPointerForName};
use std::cell::Cell; use std::cell::Cell;
use std::c_str::CString; use std::ffi::{CString, c_str_to_bytes};
use std::mem; use std::mem;
use std::ptr; use std::ptr;
use std::collections::RingBuf; use std::collections::RingBuf;
use std::str::FromStr;
use std::str::from_utf8;
use std::ascii::AsciiExt;
use events::Event::{MouseInput, MouseMoved, ReceivedCharacter, KeyboardInput, MouseWheel}; use events::Event::{MouseInput, MouseMoved, ReceivedCharacter, KeyboardInput, MouseWheel};
use events::ElementState::{Pressed, Released}; use events::ElementState::{Pressed, Released};
@ -170,9 +173,9 @@ impl Window {
let delegate = unsafe { let delegate = unsafe {
// Create a delegate class, add callback methods and store InternalState as user data. // Create a delegate class, add callback methods and store InternalState as user data.
let delegate = objc_allocateClassPair(ns_object, DELEGATE_NAME.as_ptr() as *const i8, 0); let delegate = objc_allocateClassPair(ns_object, DELEGATE_NAME.as_ptr() as *const i8, 0);
class_addMethod(delegate, selector("windowShouldClose:"), window_should_close, "B@:@".to_c_str().as_ptr()); class_addMethod(delegate, selector("windowShouldClose:"), window_should_close, CString::from_slice("B@:@".as_bytes()).as_ptr());
class_addMethod(delegate, selector("windowDidResize:"), window_did_resize, "V@:@".to_c_str().as_ptr()); class_addMethod(delegate, selector("windowDidResize:"), window_did_resize, CString::from_slice("V@:@".as_bytes()).as_ptr());
class_addIvar(delegate, DELEGATE_STATE_IVAR.as_ptr() as *const i8, ptr_size, 3, "?".to_c_str().as_ptr()); class_addIvar(delegate, DELEGATE_STATE_IVAR.as_ptr() as *const i8, ptr_size, 3, CString::from_slice("?".as_bytes()).as_ptr());
objc_registerClassPair(delegate); objc_registerClassPair(delegate);
let del_obj = msg_send()(delegate, selector("alloc")); let del_obj = msg_send()(delegate, selector("alloc"));
@ -268,12 +271,12 @@ impl Window {
fn create_context(view: id, vsync: bool) -> Option<id> { fn create_context(view: id, vsync: bool) -> Option<id> {
unsafe { unsafe {
let attributes = [ let attributes = [
NSOpenGLPFADoubleBuffer as uint, NSOpenGLPFADoubleBuffer as u32,
NSOpenGLPFAClosestPolicy as uint, NSOpenGLPFAClosestPolicy as u32,
NSOpenGLPFAColorSize as uint, 24, NSOpenGLPFAColorSize as u32, 24,
NSOpenGLPFAAlphaSize as uint, 8, NSOpenGLPFAAlphaSize as u32, 8,
NSOpenGLPFADepthSize as uint, 24, NSOpenGLPFADepthSize as u32, 24,
NSOpenGLPFAStencilSize as uint, 8, NSOpenGLPFAStencilSize as u32, 8,
0 0
]; ];
@ -381,8 +384,9 @@ impl Window {
events.push_back(MouseMoved((view_point.x as int, view_point.y as int))); events.push_back(MouseMoved((view_point.x as int, view_point.y as int)));
}, },
NSKeyDown => { NSKeyDown => {
let received_str = CString::new(event.characters().UTF8String(), false); let received_c_str = event.characters().UTF8String();
for received_char in received_str.as_str().unwrap().chars() { let received_str = CString::from_slice(c_str_to_bytes(&received_c_str));
for received_char in from_utf8(received_str.as_bytes()).unwrap().chars() {
if received_char.is_ascii() { if received_char.is_ascii() {
events.push_back(ReceivedCharacter(received_char)); events.push_back(ReceivedCharacter(received_char));
} }
@ -461,8 +465,8 @@ impl Window {
} }
pub fn get_proc_address(&self, _addr: &str) -> *const () { pub fn get_proc_address(&self, _addr: &str) -> *const () {
let symbol_name: CFString = from_str(_addr).unwrap(); let symbol_name: CFString = FromStr::from_str(_addr).unwrap();
let framework_name: CFString = from_str("com.apple.opengl").unwrap(); let framework_name: CFString = FromStr::from_str("com.apple.opengl").unwrap();
let framework = unsafe { let framework = unsafe {
CFBundleGetBundleWithIdentifier(framework_name.as_concrete_TypeRef()) CFBundleGetBundleWithIdentifier(framework_name.as_concrete_TypeRef())
}; };

View file

@ -3,11 +3,11 @@ use std::collections::RingBuf;
pub struct MonitorID(u32); pub struct MonitorID(u32);
pub fn get_available_monitors() -> Vec<MonitorID> { pub fn get_available_monitors() -> RingBuf<MonitorID> {
let mut monitors = RingBuf::new(); let mut monitors = RingBuf::new();
unsafe { unsafe {
let max_displays = 10u32; let max_displays = 10u32;
let mut active_displays = [0u32, ..10]; let mut active_displays = [0u32; 10];
let mut display_count = 0; let mut display_count = 0;
display::CGGetActiveDisplayList(max_displays, display::CGGetActiveDisplayList(max_displays,
&mut active_displays[0], &mut active_displays[0],