servo: Add a bunch of Cocoa boilerplate to main. Still no worky

Source-Repo: https://github.com/servo/servo
Source-Revision: 6ba352788c0b742817d1a1d3700a915ca6929388
This commit is contained in:
Brian Anderson 2012-04-18 19:15:51 -07:00
parent 46550a8b69
commit eb46cce39a

View File

@ -1,3 +1,132 @@
#[cfg(target_os = "linux")]
mod platform {
fn runmain(f: fn()) {
f()
}
}
#[cfg(target_os = "macos")]
mod platform {
use cocoa;
import cocoa::base::*;
mod NSApplication {
fn sharedApplication() -> id {
let klass = str::as_c_str("NSApplication") { |s|
objc::objc_getClass(s)
};
let sel = str::as_c_str("sharedApplication") { |s|
objc::sel_registerName(s)
};
let nsapp = objc::objc_msgSend(klass, sel);
#debug("nsapp: %d", (nsapp as int));
ret nsapp;
}
}
mod NSAutoreleasePool {
fn alloc() -> id {
let klass = str::as_c_str("NSAutoreleasePool") { |s|
objc::objc_getClass(s)
};
let sel = str::as_c_str("alloc") { |s|
objc::sel_registerName(s)
};
let pool = objc::objc_msgSend(klass, sel);
#debug("autorelease pool: %?", pool);
ret pool;
}
fn init(pool: id) {
let sel = str::as_c_str("init") { |s|
objc::sel_registerName(s)
};
objc::objc_msgSend(pool, sel);
}
fn release(pool: id) {
let sel = str::as_c_str("release") { |s|
objc::sel_registerName(s)
};
objc::objc_msgSend(pool, sel);
}
}
mod NSApp {
fn setDelegate(nsapp: id, main: id) {
#debug("NSApp::setDelegate");
let sel = str::as_c_str("setDelegate") { |s|
objc::sel_registerName(s)
};
cocoa::msgSend1Id(nsapp, sel, main);
}
fn run(nsapp: id) {
#debug("NSApp::run");
let sel = str::as_c_str("run") { |s|
objc::sel_registerName(s)
};
objc::objc_msgSend(nsapp, sel);
}
}
mod MainObj {
crust fn applicationDidFinishLaunching(this: id, _sel: SEL) {
#debug("applicationDidFinishLaunching");
}
fn create() -> id {
let NSObject = str::as_c_str("NSObject") { |s|
objc::objc_getClass(s)
};
let MainObj = str::as_c_str("MainObj") { |s|
objc::objc_allocateClassPair(NSObject, s, 0 as libc::size_t)
};
let launchfn = str::as_c_str("applicationDidFinishLaunching:") { |s|
objc::sel_registerName(s)
};
let _ = str::as_c_str("@@:") { |types|
objc::class_addMethod(MainObj, launchfn, applicationDidFinishLaunching, types)
};
objc::objc_registerClassPair(MainObj);
let sel = str::as_c_str("alloc") { |s|
objc::sel_registerName(s)
};
let mainobj = objc::objc_msgSend(MainObj, sel);
let sel = str::as_c_str("init") { |s|
objc::sel_registerName(s)
};
objc::objc_msgSend(mainobj, sel);
ret mainobj;
}
fn release(mainobj: id) {
let sel = str::as_c_str("release") { |s|
objc::sel_registerName(s)
};
objc::objc_msgSend(mainobj, sel);
}
}
fn runmain(f: fn()) {
let pool = NSAutoreleasePool::alloc();
NSAutoreleasePool::init(pool);
let NSApp = NSApplication::sharedApplication();
let mainobj = MainObj::create();
NSApp::setDelegate(NSApp, mainobj);
NSApp::run(NSApp);
MainObj::release(mainobj);
NSAutoreleasePool::release(pool);
}
}
enum msg {
get_draw_target(comm::chan<AzDrawTargetRef>),
add_key_handler(comm::chan<()>),
@ -7,6 +136,28 @@ enum msg {
fn osmain() -> comm::chan<msg> {
on_osmain::<msg> {|po|
platform::runmain {||
mainloop(po);
}
}
}
// A function for spawning into the platform's main thread
fn on_osmain<T: send>(f: fn~(comm::port<T>)) -> comm::chan<T> {
let builder = task::builder();
let opts = {
sched: some({
mode: task::osmain,
native_stack_size: none
})
with task::get_opts(builder)
};
task::set_opts(builder, opts);
ret task::run_listener(builder, f);
}
fn mainloop(po: comm::port<msg>) {
let mut key_handlers = [];
sdl::init([
@ -77,19 +228,4 @@ fn osmain() -> comm::chan<msg> {
cairo_surface_destroy(cairo_surf);
sdl::video::unlock_surface(sdl_surf);
sdl::quit();
}
}
// A function for spawning into the platform's main thread
fn on_osmain<T: send>(f: fn~(comm::port<T>)) -> comm::chan<T> {
let builder = task::builder();
let opts = {
sched: some({
mode: task::osmain,
native_stack_size: none
})
with task::get_opts(builder)
};
task::set_opts(builder, opts);
ret task::run_listener(builder, f);
}