diff --git a/Cargo.toml b/Cargo.toml index 6dcac0b29a..7e5cd8ed86 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,19 @@ members = [ "glutin_wgl_sys", "glutin_gles2_sys", ] + +[patch.crates-io] +objc2 = { path = "../objc2/objc2" } +objc2-encode = { path = "../objc2/objc2-encode" } +block2 = { path = "../objc2/block2" } +objc-sys = { path = "../objc2/objc-sys" } +block-sys = { path = "../objc2/block-sys" } + +cocoa-foundation = { path = "../core-foundation-rs/cocoa-foundation" } +cocoa = { path = "../core-foundation-rs/cocoa" } +core-foundation-sys = { path = "../core-foundation-rs/core-foundation-sys" } +core-foundation = { path = "../core-foundation-rs/core-foundation" } +core-graphics-types = { path = "../core-foundation-rs/core-graphics-types" } +core-graphics = { path = "../core-foundation-rs/core-graphics" } + +winit = { path = "../winit" } diff --git a/glutin/Cargo.toml b/glutin/Cargo.toml index 626f76a26b..6c51b44ae0 100644 --- a/glutin/Cargo.toml +++ b/glutin/Cargo.toml @@ -34,7 +34,7 @@ parking_lot = "0.12" raw-window-handle = "0.5" [target.'cfg(any(target_os = "ios", target_os = "macos"))'.dependencies] -objc = "0.2.6" +objc2 = "=0.3.0-beta.1" glutin_gles2_sys = { version = "0.1.5", path = "../glutin_gles2_sys" } [target.'cfg(target_os = "macos")'.dependencies] diff --git a/glutin/src/api/ios/mod.rs b/glutin/src/api/ios/mod.rs index cc40fc01bf..640970d929 100644 --- a/glutin/src/api/ios/mod.rs +++ b/glutin/src/api/ios/mod.rs @@ -67,14 +67,17 @@ use crate::{ }; use glutin_gles2_sys as ffi; -use objc::declare::ClassDecl; -use objc::runtime::{Class, Object, Sel, BOOL, NO, YES}; +use objc2::declare::ClassBuilder; +use objc2::foundation::{CGFloat, NSRect, NSUInteger}; +use objc2::runtime::{Bool, Class, Object, Sel}; +use objc2::{class, msg_send, msg_send_bool, sel}; use winit::dpi; use winit::event_loop::EventLoopWindowTarget; use winit::window::WindowBuilder; use std::ffi::CString; use std::os::raw; +use std::ptr; #[derive(Debug, PartialEq)] enum ColorFormat { @@ -85,8 +88,8 @@ enum ColorFormat { impl ColorFormat { #[allow(non_upper_case_globals)] - pub fn for_view(view: ffi::id) -> Self { - let color_format: ffi::NSUInteger = unsafe { msg_send![view, drawableColorFormat] }; + pub fn for_view(view: *mut Object) -> Self { + let color_format: ffi::gles::types::GLint = unsafe { msg_send![view, drawableColorFormat] }; match color_format { ffi::GLKViewDrawableColorFormatRGBA8888 => ColorFormat::Rgba8888, ffi::GLKViewDrawableColorFormatRGB565 => ColorFormat::Rgb565, @@ -117,8 +120,8 @@ impl ColorFormat { } #[allow(non_upper_case_globals)] -fn depth_for_view(view: ffi::id) -> u8 { - let depth_format: ffi::NSUInteger = unsafe { msg_send![view, drawableDepthFormat] }; +fn depth_for_view(view: *mut Object) -> u8 { + let depth_format: ffi::gles::types::GLint = unsafe { msg_send![view, drawableDepthFormat] }; match depth_format { ffi::GLKViewDrawableDepthFormatNone => 0, ffi::GLKViewDrawableDepthFormat16 => 16, @@ -128,8 +131,8 @@ fn depth_for_view(view: ffi::id) -> u8 { } #[allow(non_upper_case_globals)] -fn stencil_for_view(view: ffi::id) -> u8 { - let stencil_format: ffi::NSUInteger = unsafe { msg_send![view, drawableStencilFormat] }; +fn stencil_for_view(view: *mut Object) -> u8 { + let stencil_format: ffi::gles::types::GLint = unsafe { msg_send![view, drawableStencilFormat] }; match stencil_format { ffi::GLKViewDrawableStencilFormatNone => 0, ffi::GLKViewDrawableStencilFormat8 => 8, @@ -138,8 +141,8 @@ fn stencil_for_view(view: ffi::id) -> u8 { } #[allow(non_upper_case_globals)] -fn multisampling_for_view(view: ffi::id) -> Option { - let ms_format: ffi::NSUInteger = unsafe { msg_send![view, drawableMultisample] }; +fn multisampling_for_view(view: *mut Object) -> Option { + let ms_format: ffi::gles::types::GLint = unsafe { msg_send![view, drawableMultisample] }; match ms_format { ffi::GLKViewDrawableMultisampleNone => None, ffi::GLKViewDrawableMultisample4X => Some(4), @@ -149,12 +152,12 @@ fn multisampling_for_view(view: ffi::id) -> Option { #[derive(Debug)] pub struct Context { - eagl_context: ffi::id, - view: ffi::id, // this will be invalid after the `EventLoop` is dropped + eagl_context: *mut Object, + view: *mut Object, // this will be invalid after the `EventLoop` is dropped } -fn validate_version(version: u8) -> Result { - let version = version as ffi::NSUInteger; +fn validate_version(version: u8) -> Result { + let version = version as NSUInteger; if (ffi::kEAGLRenderingAPIOpenGLES1..=ffi::kEAGLRenderingAPIOpenGLES3).contains(&version) { Ok(version) } else { @@ -198,7 +201,7 @@ impl Context { let win = builder.build(el)?; let context = unsafe { let eagl_context = Context::create_context(version)?; - let view = win.ui_view() as ffi::id; + let view = win.ui_view() as *mut Object; let mut context = Context { eagl_context, view }; context.init_context(&win); context @@ -217,15 +220,15 @@ impl Context { Self::new_windowed(wb, el, pf_reqs, gl_attr).map(|(_window, context)| context) } - unsafe fn create_context(mut version: ffi::NSUInteger) -> Result { + unsafe fn create_context(mut version: NSUInteger) -> Result<*mut Object, CreationError> { let context_class = Class::get("EAGLContext").expect("Failed to get class `EAGLContext`"); - let eagl_context: ffi::id = msg_send![context_class, alloc]; - let mut valid_context = ffi::nil; - while valid_context == ffi::nil && version > 0 { - valid_context = msg_send![eagl_context, initWithAPI: version]; + let mut eagl_context: *mut Object = ptr::null_mut(); + while eagl_context.is_null() && version > 0 { + eagl_context = msg_send![context_class, alloc]; + eagl_context = msg_send![eagl_context, initWithAPI: version]; version -= 1; } - if valid_context == ffi::nil { + if eagl_context.is_null() { Err(CreationError::OsError( "Failed to create an OpenGL ES context with any version".to_string(), )) @@ -237,26 +240,26 @@ impl Context { unsafe fn init_context(&mut self, win: &winit::window::Window) { let dict_class = Class::get("NSDictionary").expect("Failed to get class `NSDictionary`"); let number_class = Class::get("NSNumber").expect("Failed to get class `NSNumber`"); - let draw_props: ffi::id = msg_send![dict_class, alloc]; - let draw_props: ffi::id = msg_send![draw_props, - initWithObjects: - vec![ - msg_send![number_class, numberWithBool:NO], - ffi::kEAGLColorFormatRGB565, - ].as_ptr() - forKeys: - vec![ - ffi::kEAGLDrawablePropertyRetainedBacking, - ffi::kEAGLDrawablePropertyColorFormat, - ].as_ptr() - count: 2 + let draw_props: *mut Object = msg_send![dict_class, alloc]; + let objects: Vec<*const Object> = vec![ + msg_send![number_class, numberWithBool: Bool::NO], + ffi::kEAGLColorFormatRGB565.cast(), + ]; + let keys: Vec<*const Object> = vec![ + ffi::kEAGLDrawablePropertyRetainedBacking.cast(), + ffi::kEAGLDrawablePropertyColorFormat.cast(), + ]; + let draw_props: *mut Object = msg_send![draw_props, + initWithObjects: objects.as_ptr(), + forKeys: keys.as_ptr(), + count: 2usize, ]; self.make_current().unwrap(); let view = self.view; - let scale_factor = win.scale_factor() as ffi::CGFloat; + let scale_factor = win.scale_factor() as CGFloat; let _: () = msg_send![view, setContentScaleFactor: scale_factor]; - let layer: ffi::id = msg_send![view, layer]; + let layer: *mut Object = msg_send![view, layer]; let _: () = msg_send![layer, setContentsScale: scale_factor]; let _: () = msg_send![layer, setDrawableProperties: draw_props]; @@ -268,8 +271,8 @@ impl Context { gl.GenRenderbuffers(1, &mut color_render_buf); gl.BindRenderbuffer(ffi::gles::RENDERBUFFER, color_render_buf); - let ok: BOOL = msg_send![self.eagl_context, renderbufferStorage:ffi::gles::RENDERBUFFER fromDrawable:layer]; - if ok != YES { + let ok = msg_send_bool![self.eagl_context, renderbufferStorage:ffi::gles::RENDERBUFFER fromDrawable:layer]; + if ok { panic!("EAGL: could not set renderbufferStorage"); } @@ -292,9 +295,9 @@ impl Context { #[inline] pub fn swap_buffers(&self) -> Result<(), ContextError> { unsafe { - let res: BOOL = - msg_send![self.eagl_context, presentRenderbuffer: ffi::gles::RENDERBUFFER]; - if res == YES { + let res = + msg_send_bool![self.eagl_context, presentRenderbuffer: ffi::gles::RENDERBUFFER]; + if res { Ok(()) } else { Err(ContextError::IoError(std::io::Error::new( @@ -344,8 +347,8 @@ impl Context { #[inline] pub unsafe fn make_current(&self) -> Result<(), ContextError> { let context_class = Class::get("EAGLContext").expect("Failed to get class `EAGLContext`"); - let res: BOOL = msg_send![context_class, setCurrentContext: self.eagl_context]; - if res == YES { + let res = msg_send_bool![context_class, setCurrentContext: self.eagl_context]; + if res { Ok(()) } else { Err(ContextError::IoError(std::io::Error::new( @@ -362,8 +365,9 @@ impl Context { } let context_class = Class::get("EAGLContext").expect("Failed to get class `EAGLContext`"); - let res: BOOL = msg_send![context_class, setCurrentContext: ffi::nil]; - if res == YES { + let context: *mut Object = ptr::null_mut(); + let res = msg_send_bool![context_class, setCurrentContext: context]; + if res { Ok(()) } else { Err(ContextError::IoError(std::io::Error::new( @@ -404,16 +408,16 @@ impl Context { } fn create_view_class() { - extern "C" fn init_with_frame(this: &Object, _: Sel, frame: ffi::CGRect) -> ffi::id { + extern "C" fn init_with_frame(this: &Object, _: Sel, frame: NSRect) -> *mut Object { unsafe { - let view: ffi::id = msg_send![super(this, class!(GLKView)), initWithFrame: frame]; + let view: *mut Object = msg_send![super(this, class!(GLKView)), initWithFrame: frame]; let mask = ffi::UIViewAutoresizingFlexibleWidth | ffi::UIViewAutoresizingFlexibleHeight; let _: () = msg_send![view, setAutoresizingMask: mask]; - let _: () = msg_send![view, setAutoresizesSubviews: YES]; + let _: () = msg_send![view, setAutoresizesSubviews: Bool::YES]; - let layer: ffi::id = msg_send![view, layer]; - let _: () = msg_send![layer, setOpaque: YES]; + let layer: *mut Object = msg_send![view, layer]; + let _: () = msg_send![layer, setOpaque: Bool::YES]; view } @@ -421,21 +425,15 @@ fn create_view_class() { extern "C" fn layer_class(_: &Class, _: Sel) -> *const Class { Class::get("CAEAGLLayer").expect("Failed to get class `CAEAGLLayer`") - as *const objc::runtime::Class + as *const objc2::runtime::Class } let superclass = Class::get("GLKView").expect("Failed to get class `GLKView`"); let mut decl = - ClassDecl::new("MainGLView", superclass).expect("Failed to declare class `MainGLView`"); + ClassBuilder::new("MainGLView", superclass).expect("Failed to declare class `MainGLView`"); unsafe { - decl.add_method( - sel!(initWithFrame:), - init_with_frame as extern "C" fn(&Object, Sel, ffi::CGRect) -> ffi::id, - ); - decl.add_class_method( - sel!(layerClass), - layer_class as extern "C" fn(&Class, Sel) -> *const Class, - ); + decl.add_method(sel!(initWithFrame:), init_with_frame as extern "C" fn(_, _, _) -> _); + decl.add_class_method(sel!(layerClass), layer_class as extern "C" fn(_, _) -> _); decl.register(); } } diff --git a/glutin/src/lib.rs b/glutin/src/lib.rs index 74d46b1f6e..27733ad2f3 100644 --- a/glutin/src/lib.rs +++ b/glutin/src/lib.rs @@ -66,10 +66,6 @@ #![allow(clippy::missing_safety_doc, clippy::too_many_arguments)] #![cfg_attr(feature = "cargo-clippy", deny(warnings))] -#[cfg(any(target_os = "macos", target_os = "ios"))] -#[macro_use] -extern crate objc; - pub mod platform; mod api; diff --git a/glutin/src/platform_impl/ios/mod.rs b/glutin/src/platform_impl/ios/mod.rs index fddec561b3..aceb3c79db 100644 --- a/glutin/src/platform_impl/ios/mod.rs +++ b/glutin/src/platform_impl/ios/mod.rs @@ -1,4 +1,3 @@ #![cfg(target_os = "ios")] pub use crate::api::ios::*; -pub use glutin_gles2_sys::id; diff --git a/glutin/src/platform_impl/macos/helpers.rs b/glutin/src/platform_impl/macos/helpers.rs index 26c01c48a6..66e911b15e 100644 --- a/glutin/src/platform_impl/macos/helpers.rs +++ b/glutin/src/platform_impl/macos/helpers.rs @@ -6,6 +6,7 @@ use crate::{ use cocoa::appkit::*; use cocoa::base::nil; +use objc2::msg_send; pub fn get_gl_profile( opengl: &GlAttributes<&T>, diff --git a/glutin/src/platform_impl/macos/mod.rs b/glutin/src/platform_impl/macos/mod.rs index 6b67085c64..b5a5cba388 100644 --- a/glutin/src/platform_impl/macos/mod.rs +++ b/glutin/src/platform_impl/macos/mod.rs @@ -12,7 +12,7 @@ use cocoa::foundation::NSAutoreleasePool; use core_foundation::base::TCFType; use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFunctionPointerForName}; use core_foundation::string::CFString; -use objc::runtime::{BOOL, NO}; +use objc2::{msg_send, msg_send_bool}; use crate::platform::macos::WindowExtMacOS; @@ -223,12 +223,8 @@ impl Context { let pool = NSAutoreleasePool::new(nil); let current = NSOpenGLContext::currentContext(nil); - let res = if current != nil { - let is_equal: BOOL = msg_send![current, isEqual: context]; - is_equal != NO - } else { - false - }; + let res = + if current != nil { msg_send_bool![current, isEqual: context] } else { false }; let _: () = msg_send![pool, release]; res } diff --git a/glutin_gles2_sys/Cargo.toml b/glutin_gles2_sys/Cargo.toml index e3786ef05a..e7858db8a4 100644 --- a/glutin_gles2_sys/Cargo.toml +++ b/glutin_gles2_sys/Cargo.toml @@ -11,6 +11,3 @@ edition = "2018" [build-dependencies] gl_generator = "0.14" - -[target.'cfg(target_os = "ios")'.dependencies] -objc = "0.2" diff --git a/glutin_gles2_sys/src/lib.rs b/glutin_gles2_sys/src/lib.rs index 118ff58fb1..46a98b9a15 100644 --- a/glutin_gles2_sys/src/lib.rs +++ b/glutin_gles2_sys/src/lib.rs @@ -13,85 +13,36 @@ pub mod gles { include!(concat!(env!("OUT_DIR"), "/gles2_bindings.rs")); } -use objc::runtime::Object; -use objc::{Encode, Encoding}; - use std::os::raw; -pub type id = *mut Object; -pub const nil: id = 0 as id; +type NSUInteger = usize; pub const UIViewAutoresizingFlexibleWidth: NSUInteger = 1 << 1; pub const UIViewAutoresizingFlexibleHeight: NSUInteger = 1 << 4; -#[cfg(target_pointer_width = "32")] -pub type CGFloat = f32; -#[cfg(target_pointer_width = "64")] -pub type CGFloat = f64; - -#[cfg(target_pointer_width = "32")] -pub type NSUInteger = u32; -#[cfg(target_pointer_width = "64")] -pub type NSUInteger = u64; - -#[repr(C)] -#[derive(Debug, Clone)] -pub struct CGPoint { - pub x: CGFloat, - pub y: CGFloat, -} - -#[repr(C)] -#[derive(Debug, Clone)] -pub struct CGRect { - pub origin: CGPoint, - pub size: CGSize, -} - -unsafe impl Encode for CGRect { - fn encode() -> Encoding { - #[cfg(target_pointer_width = "32")] - unsafe { - Encoding::from_str("{CGRect={CGPoint=ff}{CGSize=ff}}") - } - #[cfg(target_pointer_width = "64")] - unsafe { - Encoding::from_str("{CGRect={CGPoint=dd}{CGSize=dd}}") - } - } -} - -#[repr(C)] -#[derive(Debug, Clone)] -pub struct CGSize { - pub width: CGFloat, - pub height: CGFloat, -} - -pub const GLKViewDrawableColorFormatRGBA8888: NSUInteger = 0; -pub const GLKViewDrawableColorFormatRGB565: NSUInteger = 1; -pub const GLKViewDrawableColorFormatSRGBA8888: NSUInteger = 2; +pub const GLKViewDrawableColorFormatRGBA8888: gles::types::GLint = 0; +pub const GLKViewDrawableColorFormatRGB565: gles::types::GLint = 1; +pub const GLKViewDrawableColorFormatSRGBA8888: gles::types::GLint = 2; -pub const GLKViewDrawableDepthFormatNone: NSUInteger = 0; -pub const GLKViewDrawableDepthFormat16: NSUInteger = 1; -pub const GLKViewDrawableDepthFormat24: NSUInteger = 2; +pub const GLKViewDrawableDepthFormatNone: gles::types::GLint = 0; +pub const GLKViewDrawableDepthFormat16: gles::types::GLint = 1; +pub const GLKViewDrawableDepthFormat24: gles::types::GLint = 2; -pub const GLKViewDrawableStencilFormatNone: NSUInteger = 0; -pub const GLKViewDrawableStencilFormat8: NSUInteger = 1; +pub const GLKViewDrawableStencilFormatNone: gles::types::GLint = 0; +pub const GLKViewDrawableStencilFormat8: gles::types::GLint = 1; -pub const GLKViewDrawableMultisampleNone: NSUInteger = 0; -pub const GLKViewDrawableMultisample4X: NSUInteger = 1; +pub const GLKViewDrawableMultisampleNone: gles::types::GLint = 0; +pub const GLKViewDrawableMultisample4X: gles::types::GLint = 1; pub const kEAGLRenderingAPIOpenGLES1: NSUInteger = 1; -#[allow(dead_code)] pub const kEAGLRenderingAPIOpenGLES2: NSUInteger = 2; pub const kEAGLRenderingAPIOpenGLES3: NSUInteger = 3; extern "C" { - pub static kEAGLColorFormatRGB565: id; - // pub static kEAGLColorFormatRGBA8: id; - pub static kEAGLDrawablePropertyColorFormat: id; - pub static kEAGLDrawablePropertyRetainedBacking: id; + pub static kEAGLColorFormatRGB565: *const raw::c_void; + // pub static kEAGLColorFormatRGBA8: *const raw::c_void; + pub static kEAGLDrawablePropertyColorFormat: *const raw::c_void; + pub static kEAGLDrawablePropertyRetainedBacking: *const raw::c_void; } pub const RTLD_LAZY: raw::c_int = 0x001;