Skip to content

Commit

Permalink
Auto merge of rust-windowing#134 - NatalyaKovalova:ios-support, r=jdm
Browse files Browse the repository at this point in the history
Ios support

Compilation without build/linkage errors for iOS

For building could be used:
cargo build --target aarch64-apple-ios

@larsbergstrom

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/glutin/134)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo authored Nov 21, 2017
2 parents 8bb72af + 6be142e commit 318e226
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 36 deletions.
2 changes: 2 additions & 0 deletions src/api/egl/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ pub type EGLNativeWindowType = *const libc::c_void;
pub type EGLNativeWindowType = *const libc::c_void;
#[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd"))]
pub type EGLNativeWindowType = *const libc::c_void;
#[cfg(target_os = "ios")]
pub type EGLNativeWindowType = *const libc::c_void;
31 changes: 15 additions & 16 deletions src/api/ios/delegate.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use libc;
use std::mem;
use std::os::raw;
use super::DelegateState;
use Event;
use events::{ Touch, TouchPhase };
Expand Down Expand Up @@ -52,8 +52,7 @@ pub fn create_delegate_class() {

let state = Box::new(DelegateState::new(window, view_controller, view, size, scale as f32));
let state_ptr: *mut DelegateState = mem::transmute(state);
this.set_ivar("glutinState", state_ptr as *mut libc::c_void);

this.set_ivar("glutinState", state_ptr as *mut raw::c_void);

let _: () = msg_send![this, performSelector:sel!(postLaunch:) withObject:nil afterDelay:0.0];
}
Expand All @@ -66,39 +65,39 @@ pub fn create_delegate_class() {

extern fn did_become_active(this: &Object, _: Sel, _: id) {
unsafe {
let state: *mut libc::c_void = *this.get_ivar("glutinState");
let state: *mut raw::c_void = *this.get_ivar("glutinState");
let state = &mut *(state as *mut DelegateState);
state.events_queue.push_back(Event::Focused(true));
}
}

extern fn will_resign_active(this: &Object, _: Sel, _: id) {
unsafe {
let state: *mut libc::c_void = *this.get_ivar("glutinState");
let state: *mut raw::c_void = *this.get_ivar("glutinState");
let state = &mut *(state as *mut DelegateState);
state.events_queue.push_back(Event::Focused(false));
}
}

extern fn will_enter_foreground(this: &Object, _: Sel, _: id) {
unsafe {
let state: *mut libc::c_void = *this.get_ivar("glutinState");
let state: *mut raw::c_void = *this.get_ivar("glutinState");
let state = &mut *(state as *mut DelegateState);
state.events_queue.push_back(Event::Suspended(false));
}
}

extern fn did_enter_background(this: &Object, _: Sel, _: id) {
unsafe {
let state: *mut libc::c_void = *this.get_ivar("glutinState");
let state: *mut raw::c_void = *this.get_ivar("glutinState");
let state = &mut *(state as *mut DelegateState);
state.events_queue.push_back(Event::Suspended(true));
}
}

extern fn will_terminate(this: &Object, _: Sel, _: id) {
unsafe {
let state: *mut libc::c_void = *this.get_ivar("glutinState");
let state: *mut raw::c_void = *this.get_ivar("glutinState");
let state = &mut *(state as *mut DelegateState);
// push event to the front to garantee that we'll process it
// immidiatly after jump
Expand All @@ -109,7 +108,7 @@ pub fn create_delegate_class() {

extern fn handle_touches(this: &Object, _: Sel, touches: id, _:id) {
unsafe {
let state: *mut libc::c_void = *this.get_ivar("glutinState");
let state: *mut raw::c_void = *this.get_ivar("glutinState");
let state = &mut *(state as *mut DelegateState);

let touches_enum: id = msg_send![touches, objectEnumerator];
Expand Down Expand Up @@ -140,7 +139,7 @@ pub fn create_delegate_class() {
}

let superclass = Class::get("UIResponder").unwrap();
let mut decl = ClassDecl::new(superclass, "AppDelegate").unwrap();
let mut decl = ClassDecl::new("AppDelegate", superclass).unwrap();

unsafe {
decl.add_method(sel!(application:didFinishLaunchingWithOptions:),
Expand Down Expand Up @@ -179,7 +178,7 @@ pub fn create_delegate_class() {
decl.add_method(sel!(postLaunch:),
post_launch as extern fn(&Object, Sel, id));

decl.add_ivar::<*mut libc::c_void>("glutinState");
decl.add_ivar::<*mut raw::c_void>("glutinState");;

decl.register();
}
Expand All @@ -188,11 +187,11 @@ pub fn create_delegate_class() {

pub fn create_view_class() {
let superclass = Class::get("UIViewController").unwrap();
let decl = ClassDecl::new(superclass, "MainViewController").unwrap();
let decl = ClassDecl::new("MainViewController",superclass).unwrap();

decl.register();

extern fn init_for_gl(this: &Object, _: Sel, frame: *const libc::c_void) -> id {
extern fn init_for_gl(this: &Object, _: Sel, frame: *const raw::c_void) -> id {
unsafe {
let bounds: *const CGRect = mem::transmute(frame);
let view: id = msg_send![this, initWithFrame:(*bounds).clone()];
Expand All @@ -213,14 +212,14 @@ pub fn create_view_class() {


let superclass = Class::get("UIView").unwrap();
let mut decl = ClassDecl::new(superclass, "MainView").unwrap();
let mut decl = ClassDecl::new("MainView", superclass).unwrap();

unsafe {
decl.add_method(sel!(initForGl:),
init_for_gl as extern fn(&Object, Sel, *const libc::c_void) -> id);
init_for_gl as extern fn(&Object, Sel, *const raw::c_void) -> id);

decl.add_class_method(sel!(layerClass),
layer_class as extern fn(&Class, Sel) -> *const Class);
decl.register();
}
}
}
14 changes: 7 additions & 7 deletions src/api/ios/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ use std::ffi::CString;

use libc;
use objc::runtime::{ Object, Class };
use std::os::raw::c_void;

#[allow(non_camel_case_types)]
pub type id = *mut Object;

#[allow(non_camel_case_types)]
#[allow(non_upper_case_globals)]
pub const nil: id = 0 as id;

pub type CFStringRef = *const libc::c_void;
pub type CFStringRef = *const c_void;
pub type CFTimeInterval = f64;
pub type Boolean = u32;

Expand Down Expand Up @@ -77,19 +77,19 @@ extern {
}

extern {
pub fn setjmp(env: *mut libc::c_void) -> libc::c_int;
pub fn longjmp(env: *mut libc::c_void, val: libc::c_int);
pub fn setjmp(env: *mut c_void) -> libc::c_int;
pub fn longjmp(env: *mut c_void, val: libc::c_int);
}

pub const RTLD_LAZY: libc::c_int = 0x001;
pub const RTLD_GLOBAL: libc::c_int = 0x100;

extern {
pub fn dlopen(filename: *const libc::c_char, flag: libc::c_int) -> *mut libc::c_void;
pub fn dlsym(handle: *mut libc::c_void, symbol: *const libc::c_char) -> *mut libc::c_void;
pub fn dlopen(filename: *const libc::c_char, flag: libc::c_int) -> *mut c_void;
pub fn dlsym(handle: *mut c_void, symbol: *const libc::c_char) -> *mut c_void;
}

pub trait NSString {
pub trait NSString: Sized {
unsafe fn alloc(_: Self) -> id {
msg_send![class("NSString"), alloc]
}
Expand Down
23 changes: 11 additions & 12 deletions src/api/ios/mod.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,16 @@ use std::collections::VecDeque;
use std::ptr;
use std::io;
use std::mem;
use std::os::raw;
use std::ffi::CString;

use libc;
use libc::c_void;

use objc::runtime::{Class, BOOL, YES, NO };

use native_monitor::NativeMonitorId;
use { Api, PixelFormat, CreationError, GlContext, CursorState, MouseCursor, Event };
use { PixelFormatRequirements, GlAttributes, WindowAttributes, ContextError };
use CreationError::OsError;

mod delegate;
use self::delegate::{ create_delegate_class, create_view_class };
Expand All @@ -101,7 +102,7 @@ use self::ffi::{
CGFloat
};


#[allow(non_upper_case_globals)]
static mut jmpbuf: [libc::c_int;27] = [0;27];

#[derive(Clone)]
Expand Down Expand Up @@ -182,14 +183,14 @@ pub struct PlatformSpecificWindowBuilderAttributes;

impl Window {

pub fn new(builder: &WindowAttributes, _: &PixelFormatRequirements, _: &GlAttributes<&Window>,
pub fn new(win_attribs: &WindowAttributes, _: &PixelFormatRequirements, _: &GlAttributes<&Window>,
_: &PlatformSpecificWindowBuilderAttributes) -> Result<Window, CreationError>
{
unsafe {
if setjmp(mem::transmute(&mut jmpbuf)) != 0 {
let app: id = msg_send![Class::get("UIApplication").unwrap(), sharedApplication];
let delegate: id = msg_send![app, delegate];
let state: *mut libc::c_void = *(&*delegate).get_ivar("glutinState");
let state: *mut raw::c_void = *(&*delegate).get_ivar("glutinState");
let state = state as *mut DelegateState;

let context = Window::create_context();
Expand All @@ -199,8 +200,7 @@ impl Window {
delegate_state: state
};

window.init_context(builder);

window.init_context(win_attribs);
return Ok(window)
}
}
Expand Down Expand Up @@ -240,8 +240,7 @@ impl Window {
let layer: id = msg_send![state.view, layer];
let _: () = msg_send![layer, setContentsScale:state.scale as CGFloat];
let _: () = msg_send![layer, setDrawableProperties: draw_props];

let gl = gles::Gles2::load_with(|symbol| self.get_proc_address(symbol));
let gl = gles::Gles2::load_with(|symbol| self.get_proc_address(symbol) as *const _);
let mut color_render_buf: gles::types::GLuint = 0;
let mut frame_buf: gles::types::GLuint = 0;
gl.GenRenderbuffers(1, &mut color_render_buf);
Expand Down Expand Up @@ -328,12 +327,12 @@ impl Window {
}

#[inline]
pub fn platform_display(&self) -> *mut libc::c_void {
pub fn platform_display(&self) -> *mut c_void {
unimplemented!();
}

#[inline]
pub fn platform_window(&self) -> *mut libc::c_void {
pub fn platform_window(&self) -> *mut c_void {
unimplemented!()
}

Expand Down Expand Up @@ -393,7 +392,7 @@ impl GlContext for Window {
let path = CString::new("/System/Library/Frameworks/OpenGLES.framework/OpenGLES").unwrap();
unsafe {
let lib = dlopen(path.as_ptr(), RTLD_LAZY | RTLD_GLOBAL);
dlsym(lib, addr_c.as_ptr()) as *const _
dlsym(lib, addr_c.as_ptr()) as *const ()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub use window::{AvailableMonitorsIter, MonitorId, get_available_monitors, get_p
pub use native_monitor::NativeMonitorId;

use std::io;
#[cfg(not(target_os = "macos"))]
#[cfg(all(not(target_os = "macos"),not(target_os = "ios")))]
use std::cmp::Ordering;
use std::path::PathBuf;

Expand Down

0 comments on commit 318e226

Please sign in to comment.