From 879f61dbbc1300efc36553b5b25ab8913d5d290b Mon Sep 17 00:00:00 2001 From: Sam Ward Date: Sat, 10 Jan 2015 01:56:47 -0800 Subject: [PATCH 1/2] OSX compatibility for Rust 1.0 alpha --- Cargo.toml | 7 +++++++ src/osx/mod.rs | 32 ++++++++++++++++++-------------- src/osx/monitor.rs | 4 ++-- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 42b06f00..8cc14ef8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,7 @@ git = "https://github.com/servo/rust-cocoa" [target.x86_64-apple-darwin.dependencies.cocoa] git = "https://github.com/servo/rust-cocoa" +features = ["arch_64"] [target.i686-apple-darwin.dependencies.core_graphics] git = "https://github.com/servo/rust-core-graphics" @@ -37,6 +38,12 @@ git = "https://github.com/servo/rust-core-graphics" [target.x86_64-apple-darwin.dependencies.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] version = "*" features = ["gdi32", "kernel32", "user32"] diff --git a/src/osx/mod.rs b/src/osx/mod.rs index 8e8b194f..df620893 100644 --- a/src/osx/mod.rs +++ b/src/osx/mod.rs @@ -18,10 +18,13 @@ use core_foundation::string::CFString; use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFunctionPointerForName}; use std::cell::Cell; -use std::c_str::CString; +use std::ffi::{CString, c_str_to_bytes}; use std::mem; use std::ptr; 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::ElementState::{Pressed, Released}; @@ -170,9 +173,9 @@ impl Window { let delegate = unsafe { // 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); - class_addMethod(delegate, selector("windowShouldClose:"), window_should_close, "B@:@".to_c_str().as_ptr()); - class_addMethod(delegate, selector("windowDidResize:"), window_did_resize, "V@:@".to_c_str().as_ptr()); - class_addIvar(delegate, DELEGATE_STATE_IVAR.as_ptr() as *const i8, ptr_size, 3, "?".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, CString::from_slice("V@:@".as_bytes()).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); let del_obj = msg_send()(delegate, selector("alloc")); @@ -268,12 +271,12 @@ impl Window { fn create_context(view: id, vsync: bool) -> Option { unsafe { let attributes = [ - NSOpenGLPFADoubleBuffer as uint, - NSOpenGLPFAClosestPolicy as uint, - NSOpenGLPFAColorSize as uint, 24, - NSOpenGLPFAAlphaSize as uint, 8, - NSOpenGLPFADepthSize as uint, 24, - NSOpenGLPFAStencilSize as uint, 8, + NSOpenGLPFADoubleBuffer as u32, + NSOpenGLPFAClosestPolicy as u32, + NSOpenGLPFAColorSize as u32, 24, + NSOpenGLPFAAlphaSize as u32, 8, + NSOpenGLPFADepthSize as u32, 24, + NSOpenGLPFAStencilSize as u32, 8, 0 ]; @@ -381,8 +384,9 @@ impl Window { events.push_back(MouseMoved((view_point.x as int, view_point.y as int))); }, NSKeyDown => { - let received_str = CString::new(event.characters().UTF8String(), false); - for received_char in received_str.as_str().unwrap().chars() { + let received_c_str = event.characters().UTF8String(); + 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() { events.push_back(ReceivedCharacter(received_char)); } @@ -461,8 +465,8 @@ impl Window { } pub fn get_proc_address(&self, _addr: &str) -> *const () { - let symbol_name: CFString = from_str(_addr).unwrap(); - let framework_name: CFString = from_str("com.apple.opengl").unwrap(); + let symbol_name: CFString = FromStr::from_str(_addr).unwrap(); + let framework_name: CFString = FromStr::from_str("com.apple.opengl").unwrap(); let framework = unsafe { CFBundleGetBundleWithIdentifier(framework_name.as_concrete_TypeRef()) }; diff --git a/src/osx/monitor.rs b/src/osx/monitor.rs index 3fb1d744..e23336f9 100644 --- a/src/osx/monitor.rs +++ b/src/osx/monitor.rs @@ -3,11 +3,11 @@ use std::collections::RingBuf; pub struct MonitorID(u32); -pub fn get_available_monitors() -> Vec { +pub fn get_available_monitors() -> RingBuf { let mut monitors = RingBuf::new(); unsafe { let max_displays = 10u32; - let mut active_displays = [0u32, ..10]; + let mut active_displays = [0u32; 10]; let mut display_count = 0; display::CGGetActiveDisplayList(max_displays, &mut active_displays[0], From 2b0a70e69bfff12d0b19f459aa701fdfb74f2d45 Mon Sep 17 00:00:00 2001 From: Sam Ward Date: Sun, 11 Jan 2015 16:26:26 -0800 Subject: [PATCH 2/2] Revert arch_64 feature flag for rust-cocoa --- Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 8cc14ef8..ddf00a44 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,6 @@ git = "https://github.com/servo/rust-cocoa" [target.x86_64-apple-darwin.dependencies.cocoa] git = "https://github.com/servo/rust-cocoa" -features = ["arch_64"] [target.i686-apple-darwin.dependencies.core_graphics] git = "https://github.com/servo/rust-core-graphics"