aboutsummaryrefslogtreecommitdiffstats
path: root/src/api/ios/ffi.rs
blob: 72f2ff244eb7ac53e6f20a1185f323a4851d4273 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use std::ffi::CString;

use libc;
use objc::runtime::{ Object, Class };

#[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 CFTimeInterval = f64;
pub type Boolean = u32;

#[allow(non_upper_case_globals)]
pub const kCFRunLoopRunHandledSource: i32 = 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;

#[allow(non_upper_case_globals)]
pub const UIViewAutoresizingFlexibleWidth: NSUInteger = 1 << 1;
#[allow(non_upper_case_globals)]
pub const UIViewAutoresizingFlexibleHeight: NSUInteger = 1 << 4;


#[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
}

#[repr(C)]
#[derive(Debug, Clone)]
pub struct CGSize {
    pub width: CGFloat,
    pub height: CGFloat
}

pub mod gles {
    include!(concat!(env!("OUT_DIR"), "/gles2_bindings.rs"));
}

#[link(name = "UIKit", kind = "framework")]
#[link(name = "CoreFoundation", kind = "framework")]
#[link(name = "GlKit", kind = "framework")]
extern {
    pub static kCFRunLoopDefaultMode: CFStringRef;

    pub static kEAGLColorFormatRGB565: id;
    // pub static kEAGLColorFormatRGBA8: id;
    pub static kEAGLDrawablePropertyColorFormat: id;
    pub static kEAGLDrawablePropertyRetainedBacking: id;

    // int UIApplicationMain ( int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName );
    pub fn UIApplicationMain(argc: libc::c_int, argv: *const libc::c_char, principalClassName: id, delegateClassName: id) -> libc::c_int;

    // SInt32 CFRunLoopRunInMode ( CFStringRef mode, CFTimeInterval seconds, Boolean returnAfterSourceHandled );
    pub fn CFRunLoopRunInMode(mode: CFStringRef, seconds: CFTimeInterval, returnAfterSourceHandled: Boolean) -> i32;
}

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 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 trait NSString {
    unsafe fn alloc(_: Self) -> id {
        msg_send![class("NSString"), alloc]
    }

    #[allow(non_snake_case)]
    unsafe fn initWithUTF8String_(self, c_string: *const i8) -> id;
    #[allow(non_snake_case)]
    unsafe fn stringByAppendingString_(self, other: id) -> id;
    unsafe fn init_str(self, string: &str) -> Self;
    #[allow(non_snake_case)]
    unsafe fn UTF8String(self) -> *const libc::c_char;
}

impl NSString for id {
    unsafe fn initWithUTF8String_(self, c_string: *const i8) -> id {
        msg_send![self, initWithUTF8String:c_string as id]
    }

    unsafe fn stringByAppendingString_(self, other: id) -> id {
        msg_send![self, stringByAppendingString:other]
    }

    unsafe fn init_str(self, string: &str) -> id {
        let cstring = CString::new(string).unwrap();
        self.initWithUTF8String_(cstring.as_ptr())
    }

    unsafe fn UTF8String(self) -> *const libc::c_char {
        msg_send![self, UTF8String]
    }
}

#[inline]
pub fn class(name: &str) -> *mut Class {
    unsafe {
        ::std::mem::transmute(Class::get(name))
    }
}