aboutsummaryrefslogtreecommitdiffstats
path: root/src/api/ios/mod.rs
blob: 9d1b527e59d00e268341f3cba3d6b5b3c1e91aaf (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! iOS support
//!
//! # Building app
//! To build ios app you will need rustc built for this targets:
//!
//!  - armv7-apple-ios
//!  - armv7s-apple-ios
//!  - i386-apple-ios
//!  - aarch64-apple-ios
//!  - x86_64-apple-ios
//!
//! Then
//!
//! ```
//! cargo build --target=...
//! ```
//! The simplest way to integrate your app into xcode environment is to build it
//! as a static library. Wrap your main function and export it.
//!
//! ```rust, ignore
//! #[no_mangle]
//! pub extern fn start_glutin_app() {
//!     start_inner()
//! }
//!
//! fn start_inner() {
//!    ...
//! }
//!
//! ```
//!
//! Compile project and then drag resulting .a into Xcode project. Add glutin.h to xcode.
//!
//! ```c
//! void start_glutin_app();
//! ```
//!
//! Use start_glutin_app inside your xcode's main function.
//!
//!
//! # App lifecycle and events
//!
//! iOS environment is very different from other platforms and you must be very
//! careful with it's events. Familiarize yourself with [app lifecycle](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/).
//!
//!
//! This is how those event are represented in glutin:
//!
//!  - applicationDidBecomeActive is Focused(true)
//!  - applicationWillResignActive is Focused(false)
//!  - applicationDidEnterBackground is Suspended(true)
//!  - applicationWillEnterForeground is Suspended(false)
//!  - applicationWillTerminate is Closed
//!
//! Keep in mind that after Closed event is received every attempt to draw with opengl will result in segfault.
//!
//! Also note that app will not receive Closed event if suspended, it will be SIGKILL'ed




#![cfg(target_os = "ios")]
#![deny(warnings)]

use std::collections::VecDeque;
use std::ptr;
use std::mem;
use std::ffi::CString;

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

use native_monitor::NativeMonitorId;
use { Api, PixelFormat, CreationError, BuilderAttribs, GlContext, CursorState, MouseCursor, Event };
use CreationError::OsError;

mod delegate;
use self::delegate::{ create_delegate_class, create_view_class };

mod ffi;
use self::ffi::{
    gles,
    setjmp,
    dlopen,
    dlsym,
    UIApplicationMain,
    kEAGLColorFormatRGB565,
    CFTimeInterval,
    CFRunLoopRunInMode,
    kCFRunLoopDefaultMode,
    kCFRunLoopRunHandledSource,
    kEAGLDrawablePropertyRetainedBacking,
    kEAGLDrawablePropertyColorFormat,
    RTLD_LAZY,
    RTLD_GLOBAL,
    id,
    nil,
    NSString,
    CGFloat
 };


static mut jmpbuf: [libc::c_int;27] = [0;27];

pub struct MonitorID;

pub struct Window {
    eagl_context: id,
    delegate_state: *mut DelegateState
}

#[derive(Clone)]
pub struct WindowProxy;

pub struct PollEventsIterator<'a> {
    window: &'a Window,
}

pub struct WaitEventsIterator<'a> {
    window: &'a Window,
}

#[derive(Debug)]
struct DelegateState {
    events_queue: VecDeque<Event>,
    window: id,
    controller: id,
    view: id,
    size: (u32,u32),
    scale: f32
}


impl DelegateState {
    fn new(window: id, controller:id, view: id, size: (u32,u32), scale: f32) -> DelegateState {
        DelegateState {
            events_queue: VecDeque::new(),
            window: window,
            controller: controller,
            view: view,
            size: size,
            scale: scale
        }
    }
}


pub fn get_available_monitors() -> VecDeque<MonitorID> {
    let mut rb = VecDeque::new();
    rb.push_back(MonitorID);
    rb
}

pub fn get_primary_monitor() -> MonitorID {
    MonitorID
}

impl MonitorID {
    pub fn get_name(&self) -> Option<String> {
        Some("Primary".to_string())
    }

    pub fn get_native_identifier(&self) -> NativeMonitorId {
        NativeMonitorId::Unavailable
    }

    pub fn get_dimensions(&self) -> (u32, u32) {
        unimplemented!()
    }
}


impl Window {

    pub fn new(builder: BuilderAttribs) -> 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 = state as *mut DelegateState;

                let context = Window::create_context();

                let mut window = Window {
                    eagl_context: context,
                    delegate_state: state
                };

                window.init_context(builder);

                return Ok(window)
            }
        }

        create_delegate_class();
        create_view_class();
        Window::start_app();

        Err(CreationError::OsError(format!("Couldn't create UIApplication")))
    }

    unsafe fn init_context(&mut self, builder: BuilderAttribs) {
        let draw_props: id = msg_send![Class::get("NSDictionary").unwrap(), alloc];
            let draw_props: id = msg_send![draw_props,
                    initWithObjects:
                        vec![
                            msg_send![Class::get("NSNumber").unwrap(), numberWithBool: NO],
                            kEAGLColorFormatRGB565
                        ].as_ptr()
                    forKeys:
                        vec![
                            kEAGLDrawablePropertyRetainedBacking,
                            kEAGLDrawablePropertyColorFormat
                        ].as_ptr()
                    count: 2
            ];
        self.make_current();

        let state = &mut *self.delegate_state;

        if builder.multitouch {
            let _: () = msg_send![state.view, setMultipleTouchEnabled:YES];
        }

        let _: () = msg_send![state.view, setContentScaleFactor:state.scale as CGFloat];

        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 mut color_render_buf: gles::types::GLuint = 0;
        let mut frame_buf: gles::types::GLuint = 0;
        gl.GenRenderbuffers(1, &mut color_render_buf);
        gl.BindRenderbuffer(gles::RENDERBUFFER, color_render_buf);

        let ok: BOOL = msg_send![self.eagl_context, renderbufferStorage:gles::RENDERBUFFER fromDrawable:layer];
        if ok != YES {
            panic!("EAGL: could not set renderbufferStorage");
        }

        gl.GenFramebuffers(1, &mut frame_buf);
        gl.BindFramebuffer(gles::FRAMEBUFFER, frame_buf);

        gl.FramebufferRenderbuffer(gles::FRAMEBUFFER, gles::COLOR_ATTACHMENT0, gles::RENDERBUFFER, color_render_buf);

        let status = gl.CheckFramebufferStatus(gles::FRAMEBUFFER);
        if gl.CheckFramebufferStatus(gles::FRAMEBUFFER) != gles::FRAMEBUFFER_COMPLETE {
            panic!("framebuffer status: {:?}", status);
        }
    }

    fn create_context() -> id {
        unsafe {
            let eagl_context: id = msg_send![Class::get("EAGLContext").unwrap(), alloc];
            let eagl_context: id = msg_send![eagl_context, initWithAPI:2]; // es2
            eagl_context
        }
    }

    fn start_app() {
        unsafe {
            UIApplicationMain(0, ptr::null(), nil, NSString::alloc(nil).init_str("AppDelegate"));
        }
    }

    pub fn is_closed(&self) -> bool {
        false
    }

    pub fn set_title(&self, _: &str) {
    }

    pub fn show(&self) {
    }

    pub fn hide(&self) {
    }

    pub fn get_position(&self) -> Option<(i32, i32)> {
        None
    }

    pub fn set_position(&self, _x: i32, _y: i32) {
    }

    pub fn get_inner_size(&self) -> Option<(u32, u32)> {
        unsafe { Some((&*self.delegate_state).size) }
    }

    pub fn get_outer_size(&self) -> Option<(u32, u32)> {
        self.get_inner_size()
    }

    pub fn set_inner_size(&self, _x: u32, _y: u32) {
    }

    pub fn poll_events(&self) -> PollEventsIterator {
        PollEventsIterator {
            window: self
        }
    }

    pub fn wait_events(&self) -> WaitEventsIterator {
        WaitEventsIterator {
            window: self
        }
    }

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

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

    pub fn get_pixel_format(&self) -> PixelFormat {
        unimplemented!();
    }

    pub fn set_window_resize_callback(&mut self, _: Option<fn(u32, u32)>) {
    }

    pub fn set_cursor(&self, _: MouseCursor) {
    }

    pub fn set_cursor_state(&self, _: CursorState) -> Result<(), String> {
        Ok(())
    }

    pub fn hidpi_factor(&self) -> f32 {
        unsafe { (&*self.delegate_state) }.scale
    }

    pub fn set_cursor_position(&self, _x: i32, _y: i32) -> Result<(), ()> {
        unimplemented!();
    }

    pub fn create_window_proxy(&self) -> WindowProxy {
        WindowProxy
    }

}

impl GlContext for Window {
    unsafe fn make_current(&self) {
        let _:BOOL = msg_send![Class::get("EAGLContext").unwrap(), setCurrentContext: self.eagl_context];
    }

    fn is_current(&self) -> bool {
        false
    }

    fn get_proc_address(&self, addr: &str) -> *const libc::c_void {
        let addr_c = CString::new(addr).unwrap();
        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())
        }
    }

    fn swap_buffers(&self) {
        unsafe { let _:BOOL = msg_send![self.eagl_context, presentRenderbuffer: gles::RENDERBUFFER]; }
    }

    fn get_api(&self) -> Api {
        unimplemented!()
    }

    fn get_pixel_format(&self) -> PixelFormat {
        unimplemented!()
    }
}

impl WindowProxy {
    pub fn wakeup_event_loop(&self) {
        unimplemented!()
    }
}


impl<'a> Iterator for WaitEventsIterator<'a> {
    type Item = Event;

    fn next(&mut self) -> Option<Event> {
        loop {
            if let Some(ev) = self.window.poll_events().next() {
                return Some(ev);
            }
        }
    }
}

impl<'a> Iterator for PollEventsIterator<'a> {
    type Item = Event;

    fn next(&mut self) -> Option<Event> {
        unsafe {
            let state = &mut *self.window.delegate_state;

            if let Some(event) = state.events_queue.pop_front() {
                return Some(event)
            }

            // jump hack, so we won't quit on willTerminate event before processing it
            if setjmp(mem::transmute(&mut jmpbuf)) != 0 {
                return state.events_queue.pop_front()
            }

            // run runloop
            let seconds: CFTimeInterval = 0.000002;
            while CFRunLoopRunInMode(kCFRunLoopDefaultMode, seconds, 1) == kCFRunLoopRunHandledSource {}

            state.events_queue.pop_front()
        }
    }
}