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
use ffi::*;
use std::ptr;
use std::thread;
use std::mem;
use std::cell::RefCell;
use std::os::raw::c_void;

unsafe impl Sync for MainLoop {}
unsafe impl Send for MainLoop {}

pub struct MainLoop{
	gst_loop: *mut GMainLoop,
	running: bool
}

impl Drop for MainLoop{
	fn drop(&mut self){
		unsafe{
			if self.running {
				self.quit();
			}
			g_main_loop_unref(mem::transmute(self.gst_loop));
		}
	}
}

impl MainLoop{
	pub fn new() -> MainLoop{
		unsafe{
			MainLoop{ gst_loop: g_main_loop_new(mem::transmute(ptr::null::<c_void>()), 0), running: false }
		}
	}

	pub fn spawn(&mut self){
		if !self.running {
			self.running = true;
			let gst_loop: usize = unsafe{ mem::transmute(self.gst_loop) };
			thread::spawn( move|| {
				unsafe{
					g_main_loop_run ( mem::transmute(gst_loop) );
				}
				/*loop{
					g_main_context_iteration(gst_loop,1);
				}*/
			});
		}
	}

	pub fn run(&mut self){
		unsafe{
			if !self.running {
				self.running = true;
				let gst_loop = self.gst_loop.clone();
				g_main_loop_run ( mem::transmute(gst_loop) );
			}
		}
	}

	pub fn quit(&mut self){
		unsafe{
			if self.running{
				self.running = false;
				g_main_loop_quit(mem::transmute(self.gst_loop));
			}
		}
	}
}

thread_local!(static LOOP: RefCell<MainLoop> = RefCell::new(MainLoop::new()));

pub fn spawn(){
	LOOP.with(|mainloop| mainloop.borrow_mut().spawn());
}

pub fn run(){
	LOOP.with(|mainloop| mainloop.borrow_mut().run());
}

pub fn quit(){
	LOOP.with(|mainloop| mainloop.borrow_mut().quit());
}