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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
extern crate glfw;
extern crate libc;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate serde_derive;
extern crate serde;
#[macro_use]
extern crate serde_json;
#[macro_use]
extern crate log;
extern crate futures;
extern crate tokio;
extern crate tinyfiledialogs;
extern crate gl;

pub mod ffi;
pub mod plugins;
pub mod codec;
pub mod channel;
mod draw;
mod utils;

use std::{
    slice,
    mem,
    borrow::Cow,
    collections::HashMap,
    ptr::{null},
    ffi::{CString, CStr},
    sync::{Arc, Weak, Mutex},
    time::{SystemTime, UNIX_EPOCH},
    cell::{Cell, RefCell},
    sync::mpsc:: { self, Sender, Receiver },
};
use libc::{c_void};
use self::ffi::{
    FlutterOpenGLRendererConfig,
    FlutterRendererType,
    FlutterResult,
    FlutterPlatformMessage,
    FlutterWindowMetricsEvent,
    FlutterEngineRun,
    FlutterEngineSendWindowMetricsEvent,
};
pub use self::plugins::{
    PlatformMessage,
    PluginRegistry,
    Plugin,
    textinput::TextInputPlugin,
    window::WindowPlugin,
    platform::PlatformPlugin,
    dialog::DialogPlugin,
};
use utils::{ CStringVec };
use glfw::{ Context, Action, Key, Modifiers };
use tokio::runtime::Runtime;

pub use glfw::Window;

pub struct FlutterEngineArgs {
    pub assets_path: String,
    pub icu_data_path: String,
    pub title: String,
    pub width: u32,
    pub height: u32,
    pub bg_color: (u8, u8, u8),
    pub window_mode: WindowMode,
    pub command_line_args: Option<Vec<String>>
}

impl Default for FlutterEngineArgs {
    fn default() -> Self {
        FlutterEngineArgs {
            assets_path: String::from(""),
            icu_data_path: String::from(""),
            title: String::from(""),
            width: 1024,
            height: 768,
            bg_color: (255, 255, 255),
            window_mode: WindowMode::Windowed,
            command_line_args: None,
        }
    }
}

const DEFAULT_DPI: f64 = 160.0;

pub enum WindowMode {
    FullScreen(usize), // monitor index
    Windowed,
    Frameless,
}

extern fn present(data: *const c_void) -> bool {
    trace!("present");
    unsafe {
        let window: &mut glfw::Window = &mut *(data as *mut glfw::Window);
        window.swap_buffers();

        // A work around for black screen on window start in macOS Mojave (10.14)
        if cfg!(target_os = "macos") {
            static mut IS_INITIALLY_VISIBLE: bool = false;
            if !IS_INITIALLY_VISIBLE {
                let pos = window.get_pos();
                window.set_pos(pos.0 + 1, pos.1);
                window.set_pos(pos.0, pos.1);
                IS_INITIALLY_VISIBLE = true;
            }
        }
    }
    true
}

extern fn make_current(data: *const c_void) -> bool {
    trace!("make_current");
    unsafe {
        let window: &mut glfw::Window = &mut *(data as *mut glfw::Window);
        window.make_current();
    }
    true
}

extern fn clear_current(_data: *const c_void) -> bool {
    trace!("clear_current");
    glfw::make_context_current(None);
    true
}

extern fn fbo_callback(_data: *const c_void) -> u32 {
    trace!("fbo_callback");
    0
}

extern fn make_resource_current(_data: *const c_void) -> bool {
    trace!("make_resource_current");
    false
}

extern fn gl_proc_resolver(_data: *const c_void, proc: *const libc::c_char) -> *const c_void {
    unsafe {
        return glfw::ffi::glfwGetProcAddress(proc);
    }
}

extern fn platform_message_callback(ptr: *const FlutterPlatformMessage, data: *const c_void) {
    trace!("platform_message_callback");
    unsafe {
        let msg = &*ptr;
        let mmsg = into_platform_message(msg);
        let window: &mut glfw::Window = &mut *(data as *mut glfw::Window);
        if let Some(engine) = FlutterEngine::get_engine(window.window_ptr()) {
            FlutterEngineInner::handle_platform_msg(mmsg, engine, window);
        }
    }
}

extern fn root_isolate_create_callback(_data: *const c_void) {
    trace!("root_isolate_create_callback");
}

extern fn window_refreshed(ptr: *mut glfw::ffi::GLFWwindow) {
    if let Some(engine) = FlutterEngine::get_engine(ptr) {
        let mut w: i32 = 0;
        let mut h: i32 = 0;
        let mut w2: i32 = 0;
        let mut h2: i32 = 0;

        unsafe {
            glfw::ffi::glfwGetWindowSize(ptr, &mut w, &mut h);
            glfw::ffi::glfwGetFramebufferSize(ptr, &mut w2, &mut h2);
        }

        engine.send_window_metrics_change((w, h), (w2, h2));
    }
}

/// consider refactor this with TryFrom trait?
fn into_platform_message(msg: &FlutterPlatformMessage) -> PlatformMessage {
    unsafe {
        let channel = CStr::from_ptr(msg.channel);
        trace!("Unpacking platform msg from channel {:?}", channel);

        let message = slice::from_raw_parts(msg.message, msg.message_size);
        let response_handle = if msg.response_handle == null() {
            None
        } else {
            Some(&*msg.response_handle)
        };
        PlatformMessage {
            channel: Cow::Owned(channel.to_string_lossy().into_owned()),
            message,
            response_handle,
        }
    }
}

fn handle_event(window: &mut glfw::Window, event: glfw::WindowEvent) {
    match event {
        glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) => {
            window.set_should_close(true)
        }
        glfw::WindowEvent::Key(key, _, Action::Press, modifiers) => {
            match key {
                Key::Enter => {
                    FlutterEngine::with_plugin(window.window_ptr(), "flutter/textinput", |p: &Box<TextInputPlugin>| {
                        if modifiers.contains(Modifiers::Control) {
                            p.perform_action("done");
                        } else {
                            // TODO
                            // why add_char plus newline action?
                            p.add_chars("\n");
                            p.perform_action("newline");
                        }
                    });
                },
                Key::Backspace => {
                    FlutterEngine::with_plugin(window.window_ptr(), "flutter/textinput", |p: &Box<TextInputPlugin>| {
                        p.backspace();
                    });
                },
                Key::Delete => {
                    FlutterEngine::with_plugin(window.window_ptr(), "flutter/textinput", |p: &Box<TextInputPlugin>| {
                        p.delete();
                    });
                },
                Key::Up => {
                    FlutterEngine::with_plugin(window.window_ptr(), "flutter/textinput", |p: &Box<TextInputPlugin>| {
                        p.move_cursor_up(modifiers);
                    });
                },
                Key::Down => {
                    FlutterEngine::with_plugin(window.window_ptr(), "flutter/textinput", |p: &Box<TextInputPlugin>| {
                        p.move_cursor_down(modifiers);
                    });
                },
                Key::Left => {
                    FlutterEngine::with_plugin(window.window_ptr(), "flutter/textinput", |p: &Box<TextInputPlugin>| {
                        p.move_cursor_left(modifiers);
                    });
                },
                Key::Right => {
                    FlutterEngine::with_plugin(window.window_ptr(), "flutter/textinput", |p: &Box<TextInputPlugin>| {
                        p.move_cursor_right(modifiers);
                    });
                },
                Key::Home => {
                    FlutterEngine::with_plugin(window.window_ptr(), "flutter/textinput", |p: &Box<TextInputPlugin>| {
                        p.move_cursor_home(modifiers);
                    });
                },
                Key::End => {
                    FlutterEngine::with_plugin(window.window_ptr(), "flutter/textinput", |p: &Box<TextInputPlugin>| {
                        p.move_cursor_end(modifiers);
                    });
                },
                Key::A => {
                    if cfg!(target_os = "macos") && modifiers.contains(Modifiers::Super) || modifiers.contains(Modifiers::Control) {
                        FlutterEngine::with_plugin(window.window_ptr(), "flutter/textinput", |p: &Box<TextInputPlugin>| {
                            p.select_all();
                        });
                    }
                },
                Key::X => {
                    if cfg!(target_os = "macos") && modifiers.contains(Modifiers::Super) || modifiers.contains(Modifiers::Control) {
                        FlutterEngine::with_plugin(window.window_ptr(), "flutter/textinput", |p: &Box<TextInputPlugin>| {
                            let s = p.get_selected_text();
                            p.remove_selected_text();
                            window.set_clipboard_string(&s);
                        });
                    }
                },
                Key::C => {
                    if cfg!(target_os = "macos") && modifiers.contains(Modifiers::Super) || modifiers.contains(Modifiers::Control) {
                        FlutterEngine::with_plugin(window.window_ptr(), "flutter/textinput", |p: &Box<TextInputPlugin>| {
                            let s = p.get_selected_text();
                            window.set_clipboard_string(&s);
                        });
                    }
                },
                Key::V => {
                    if cfg!(target_os = "macos") && modifiers.contains(Modifiers::Super) || modifiers.contains(Modifiers::Control) {
                        FlutterEngine::with_plugin(window.window_ptr(), "flutter/textinput", |p: &Box<TextInputPlugin>| {
                            let s = window.get_clipboard_string();
                            p.add_chars(&s);
                        });
                    }
                },
                _ => (),
            }
        }
        glfw::WindowEvent::Char(c) => {
            FlutterEngine::with_plugin(window.window_ptr(), "flutter/textinput", |p: &Box<TextInputPlugin>| {
                p.add_chars(&c.to_string());
            });
        }
        glfw::WindowEvent::FramebufferSize(w, h) => {
            if let Some(engine) = FlutterEngine::get_engine(window.window_ptr()) {
                let w_size = window.get_size();
                engine.send_window_metrics_change(w_size, (w, h));
            }
        },
        glfw::WindowEvent::CursorPos(x, y) => {
            FlutterEngine::with_plugin(window.window_ptr(), "flutter-rs/window", |p: &Box<WindowPlugin>| {
                // window dragging is handled by window plugin
                if !p.drag_window(window, x, y) {
                    if let Some(engine) = FlutterEngine::get_engine(window.window_ptr()) {
                        if window.get_mouse_button(glfw::MouseButton::Button1) == glfw::Action::Press {
                            let w_size = window.get_size();
                            let size = window.get_framebuffer_size();
                            let pixels_per_screen_coordinate = size.0 as f64 / w_size.0 as f64;
                            engine.send_cursor_position_at_phase(x * pixels_per_screen_coordinate, y * pixels_per_screen_coordinate, ffi::FlutterPointerPhase::Move);
                        }
                    }
                }
            });

        },
        glfw::WindowEvent::MouseButton(button, action, _modifiers) => {
            if button == glfw::MouseButton::Button1 {
                let pos = window.get_cursor_pos();
                let phase = if action == glfw::Action::Press {
                    ffi::FlutterPointerPhase::Down
                } else {
                    ffi::FlutterPointerPhase::Up
                };
                let w_size = window.get_size();
                let size = window.get_framebuffer_size();
                let pixels_per_screen_coordinate = size.0 as f64 / w_size.0 as f64;

                if let Some(engine) = FlutterEngine::get_engine(window.window_ptr()) {
                    engine.send_cursor_position_at_phase(pos.0 * pixels_per_screen_coordinate, pos.1 * pixels_per_screen_coordinate, phase);
                }
            }
        },
        _ => {}
    }
}

pub struct FlutterEngineInner {
    args: FlutterEngineArgs,
    config: ffi::FlutterRendererConfig,
    proj_args: ffi::FlutterProjectArgs,
    ptr: *const ffi::FlutterEngine,
    registry: RefCell<PluginRegistry>,
    glfw: RefCell<Option<(
        glfw::Window,
        std::sync::mpsc::Receiver<(f64, glfw::WindowEvent)>
    )>>,
    dpi: Cell<f64>,
    rt: RefCell<Runtime>, // A tokio async runtime
    tx: Sender<Box<dyn Fn() + Send>>,
    rx: Receiver<Box<dyn Fn() + Send>>,
}

impl FlutterEngineInner {
    fn run(&self) {
        let mut g = glfw::Glfw;

        // setup window
        let mut monitor_id = 0;
        let (mut window, events) = {
            let tip = "Failed to create GLFW window.";
            match self.args.window_mode {
                WindowMode::Frameless => {
                    g.window_hint(glfw::WindowHint::Decorated(false));
                    g.create_window(
                        self.args.width,
                        self.args.height,
                        &self.args.title,
                        glfw::WindowMode::Windowed,
                    ).expect(tip)
                },
                WindowMode::FullScreen(idx) => {
                    monitor_id = idx;
                    g.with_connected_monitors(|g, monitors| {
                        let monitor = monitors.get(idx).expect("Cannot find specified monitor");
                        g.create_window(
                            self.args.width,
                            self.args.height,
                            &self.args.title,
                            glfw::WindowMode::FullScreen(&monitor),
                        ).expect(tip)
                    })
                },
                _ => {
                    g.create_window(
                        self.args.width,
                        self.args.height,
                        &self.args.title,
                        glfw::WindowMode::Windowed,
                    ).expect(tip)
                },
            }
        };

        self.dpi.set(self.get_dpi(&mut g, monitor_id));

        window.set_key_polling(true);
        window.set_framebuffer_size_polling(true);
        window.set_size_polling(true);
        window.set_mouse_button_polling(true);
        window.set_cursor_pos_polling(true);
        window.set_char_polling(true);
        window.make_current();

        self.add_system_plugins();

        // poll_events is blocked during window resize. This callback fix redraw freeze during window resize.
        // See https://github.com/glfw/glfw/issues/408 for details
        unsafe {
            glfw::ffi::glfwSetWindowRefreshCallback(
                window.window_ptr(),
                Some(window_refreshed)
            );
        }

        // move window and events to FlutterEngineInner struct
        self.glfw.replace(Some((window, events)));

        self.with_window_mut(|window| {
            // draw inital background color
            draw::init_gl(window);
            draw::draw_bg(window, &self.args);

            unsafe {
                let ret = FlutterEngineRun(
                    1,
                    &self.config,
                    &self.proj_args,
                    window as *const glfw::Window as *const c_void,
                    &self.ptr as *const *const ffi::FlutterEngine);

                assert!(ret == FlutterResult::Success, "Cannot start flutter engine");
            }

            let window_size = window.get_size();
            let buf_size = window.get_framebuffer_size();
            self.send_window_metrics_change(window_size, buf_size);
        });
    }

    fn with_window_mut(&self, cbk: impl FnOnce(&mut glfw::Window)) {
        let mut pack = self.glfw.borrow_mut();
        let (window, _) = pack.as_mut().unwrap();
        cbk(window);
    }

    pub fn with_async(&self, cbk: impl FnOnce(&mut Runtime)) {
        let rt = &mut *self.rt.borrow_mut();
        cbk(rt);
    }

    pub fn ui_thread(&self, f: Box<dyn Fn() + Send>) {
        let _ = self.tx.send(f);
    }

    fn add_system_plugins(&self) {
        let registry = &mut *self.registry.borrow_mut();
        
        let plugin = TextInputPlugin::new();
        registry.add_plugin(Box::new(plugin));

        let plugin = PlatformPlugin::new();
        registry.add_plugin(Box::new(plugin));
        
        let plugin = DialogPlugin::new();
        registry.add_plugin(Box::new(plugin));

        let plugin = WindowPlugin::new();
        registry.add_plugin(Box::new(plugin));
    }

    fn event_loop(&self) {
        if let Some((window, events)) = &mut *self.glfw.borrow_mut() {
            while !window.should_close() {
                // glfw.poll_events();
                // window.glfw.wait_events();
                window.glfw.wait_events_timeout(1.0/60.0);

                for (_, event) in glfw::flush_messages(&events) {
                    handle_event(window, event);
                }

                // This is required, otherwise windows won't trigger platform_message_callback
                unsafe {
                    ffi::__FlutterEngineFlushPendingTasksNow();
                }

                // process ui thread callback queue
                for v in self.rx.try_recv() {
                   v();
                }
            }
        }
    }

    fn get_dpi(&self, glfw: &mut glfw::Glfw, monitor_id: usize) -> f64 {
        glfw.with_connected_monitors(|_, monitors| {
            let m = monitors.get(monitor_id).unwrap();
            let mode = m.get_video_mode().unwrap();
            let physical_size = m.get_physical_size();
            if physical_size.0 <= 0 {
                return 160.0;
            }
            mode.width as f64 / (physical_size.0 as f64 / 25.4)
        })
    }

    fn send_window_metrics_change(&self, window_size: (i32, i32), buf_size: (i32, i32)) {
        let pixel_ratio = buf_size.0 as f64 / window_size.0 as f64 * self.dpi.get() / DEFAULT_DPI;
        let evt = FlutterWindowMetricsEvent {
            struct_size: mem::size_of::<FlutterWindowMetricsEvent>(),
            width: buf_size.0 as usize,
            height: buf_size.1 as usize,
            pixel_ratio,
        };
        unsafe {
            FlutterEngineSendWindowMetricsEvent(self.ptr, &evt as *const FlutterWindowMetricsEvent);
        }
    }

    fn send_cursor_position_at_phase(&self, x: f64, y: f64, phase: ffi::FlutterPointerPhase) {
        let duration = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
        let evt = &ffi::FlutterPointerEvent {
            struct_size: mem::size_of::<ffi::FlutterPointerEvent>(),
            timestamp: (duration.as_secs() as f64 * 1e6 + duration.subsec_nanos() as f64 / 1e3) as usize,
            phase: phase,
            x: x,
            y: y,
        };

        unsafe {
            ffi::FlutterEngineSendPointerEvent(
                self.ptr,
                evt,
                1
            );
        }
    }

    pub fn send_platform_message(&self, message: &PlatformMessage) {
        trace!("Sending message {:?} on channel {}", message, message.channel);
        let msg: FlutterPlatformMessage = message.into();
        unsafe {
            ffi::FlutterEngineSendPlatformMessage(
                self.ptr,
                &msg as *const ffi::FlutterPlatformMessage,
            );
        }
        // we need to manually drop this message
        // msg.drop();
    }

    pub fn send_platform_message_response(&self, response_handle: &ffi::FlutterPlatformMessageResponseHandle, bytes: &[u8]) {
        trace!("Sending message response");
        unsafe {
            ffi::FlutterEngineSendPlatformMessageResponse(
                self.ptr,
                response_handle,
                bytes as *const [u8] as *const _,
                bytes.len(),
            );
        }
    }

    fn handle_platform_msg(msg: PlatformMessage, engine: Arc<FlutterEngineInner>, window: &mut glfw::Window) {
        let mut registry = engine.registry.borrow_mut();
        registry.handle(msg, engine.clone(), window);
    }
}

// Has to be Send and Sync to use with lazy_static
unsafe impl Send for FlutterEngineInner {}
unsafe impl Sync for FlutterEngineInner {}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash,)]
struct WindowKey(*mut glfw::ffi::GLFWwindow);

unsafe impl Send for WindowKey {}
unsafe impl Sync for WindowKey {}

lazy_static! {
    static ref ENGINES: Mutex<HashMap<WindowKey, Weak<FlutterEngineInner>>> = Mutex::new(HashMap::new());
}

// Use Arc, since ENGINES need to have a weak ref of FlutterEngineInner
pub struct FlutterEngine {
    inner: Arc<FlutterEngineInner>,
}

impl FlutterEngine {
    pub fn new(args: FlutterEngineArgs) -> FlutterEngine {
        let config: ffi::FlutterRendererConfig = ffi::FlutterRendererConfig {
            kind: FlutterRendererType::OpenGL,
            open_gl: FlutterOpenGLRendererConfig {
                struct_size: mem::size_of::<FlutterOpenGLRendererConfig>(),
                make_current: make_current,
                clear_current: clear_current,
                present: present,
                fbo_callback: fbo_callback,
                make_resource_current: make_resource_current,
                fbo_reset_after_present: false,
                surface_transformation: None,
                gl_proc_resolver: gl_proc_resolver,
                gl_external_texture_frame_callback: None,
            },
        };

        // FlutterProjectArgs is expecting a full argv, so when processing it for flags the first
        // item is treated as the executable and ignored. Add a dummy value so that all provided arguments
        // are used.
        let vm_args = {
            let mut cli_args = vec!["placeholder"];
            if let Some(a) = &args.command_line_args {
                cli_args.extend(a.iter().map(|v| v.as_str()));
            } else {
                // use default args
                cli_args.push("--observatory-port=50300");
            };
            CStringVec::new(&cli_args)
        };

        let proj_args = ffi::FlutterProjectArgs {
            struct_size: mem::size_of::<ffi::FlutterProjectArgs>(),
            assets_path: CString::new(args.assets_path.to_string()).unwrap().into_raw(),
            main_path: CString::new("").unwrap().into_raw(),
            packages_path: CString::new("").unwrap().into_raw(),
            icu_data_path: CString::new(args.icu_data_path.to_string()).unwrap().into_raw(),
            command_line_argc: vm_args.len() as i32,
            command_line_argv: vm_args.into_raw(),
            platform_message_callback: platform_message_callback,
            vm_snapshot_data: std::ptr::null(),
            vm_snapshot_data_size: 0,
            vm_snapshot_instructions: std::ptr::null(),
            vm_snapshot_instructions_size: 0,
            isolate_snapshot_data: std::ptr::null(),
            isolate_snapshot_data_size: 0,
            isolate_snapshot_instructions: std::ptr::null(),
            isolate_snapshot_instructions_size: 0,
            root_isolate_create_callback: root_isolate_create_callback,
        };

        info!("Project args {:?}", proj_args);
        info!("OpenGL config {:?}", config);

        let (tx, rx) = mpsc::channel();
        let inner = Arc::new(FlutterEngineInner {
            args,
            config,
            proj_args,
            ptr: null(),
            registry: RefCell::new(PluginRegistry::new()),
            glfw: RefCell::new(None),
            dpi: Cell::new(DEFAULT_DPI),
            rt: RefCell::new(Runtime::new().expect("Cannot init tokio runtime")),
            tx,
            rx,
        });
        inner.registry.borrow_mut().set_engine(Arc::downgrade(&inner));
        FlutterEngine {
            inner,
        }
    }

    pub fn run(&self) {
        self.inner.run();
        {
            let glfw = &*self.inner.glfw.borrow();
            let (window, _) = glfw.as_ref().unwrap();
            let mut guard = ENGINES.lock().unwrap();
            guard.insert(WindowKey(window.window_ptr()), Arc::downgrade(&self.inner));
        }
        self.inner.event_loop();
    }

    pub fn shutdown(&self) {
        unsafe {
            ffi::FlutterEngineShutdown(self.inner.ptr);
        }
    }

    pub fn add_plugin(&self, plugin: Box<dyn Plugin>) {
        let mut registry = self.inner.registry.borrow_mut();
        registry.add_plugin(plugin);
    }

    fn get_engine(window_ptr: *mut glfw::ffi::GLFWwindow) -> Option<Arc<FlutterEngineInner>> {
        let guard = ENGINES.lock().unwrap();
        if let Some(weak) = guard.get(&WindowKey(window_ptr)) {
            weak.upgrade()
        } else {
            None
        }
    }

    fn with_plugin<T, F: FnMut(&Box<T>)>(window_ptr: *mut glfw::ffi::GLFWwindow, channel: &str, mut cbk: F) {
        if let Some(engine) = FlutterEngine::get_engine(window_ptr) {
            if let Some(plugin) = engine.registry.borrow().get_plugin(channel) {
                unsafe {
                    let p = std::mem::transmute::<&Box<dyn Plugin>, &Box<T>>(plugin);
                    cbk(p);
                }
            }
        }
    }
}

impl Drop for FlutterEngine {
    fn drop(&mut self) {
        // TODO: destroy window?
    }
}

/// init must be called from the main thread
pub fn init() {
    glfw::init(glfw::FAIL_ON_ERRORS).unwrap();
}