diff --git a/Cargo.toml b/Cargo.toml index 6333080..9256621 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,25 @@ user32-sys = "0.1.2" winapi = "0.2.4" kernel32-sys = "0.1.4" gdi32-sys = "0.1.1" + +[target.x86_64-apple-darwin.dependencies] +objc = "0.1.8" +cgl = "0.1" +cocoa = "0.1.4" +core-foundation = "0" +core-graphics = "0" + +[target.i686-pc-windows-gnu.dependencies] +winapi = "0.2.4" +user32-sys = "0.1.2" +kernel32-sys = "0.1.4" +gdi32-sys = "0.1.1" + +[target.i686-pc-windows-msvc.dependencies] +winapi = "0.2.4" +user32-sys = "0.1.2" +kernel32-sys = "0.1.4" +gdi32-sys = "0.1.1" + + + diff --git a/src/lib.rs b/src/lib.rs index 3aed2ca..afe32cc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,43 @@ extern crate libc; + +#[cfg(target_os = "macos")] +#[macro_use] +extern crate objc; +#[cfg(target_os = "macos")] +extern crate cgl; +#[cfg(target_os = "macos")] +extern crate cocoa; +#[cfg(target_os = "macos")] +extern crate core_foundation; +#[cfg(target_os = "macos")] + +/// Error that can happen while creating a window or a headless renderer. +#[derive(Debug)] +pub enum CreationError { + OsError(String), + NotSupported, +} + +impl CreationError { + fn to_string(&self) -> &str { + match *self { + CreationError::OsError(ref text) => &text, + CreationError::NotSupported => "Some of the requested attributes are not supported", + } + } +} + #[cfg(target_os = "windows")] pub mod windows; - +#[cfg(target_os = "windows")] pub use windows::*; +#[cfg(target_os = "macos")] +pub mod macos; +#[cfg(target_os = "mac")] +pub use macos::*; + + /* #[cfg(target_os = "macos")] diff --git a/src/macos.rs b/src/macos.rs new file mode 100644 index 0000000..d1a6596 --- /dev/null +++ b/src/macos.rs @@ -0,0 +1,85 @@ +#![cfg(target_os = "macos")] + +use CreationError; +use CreationError::OsError; + +use libc; +use cocoa::appkit; +use cocoa::appkit::*; +use cocoa::appkit::NSEventSubtype::*; + +use cocoa::base::{id, nil}; +use objc::runtime::{Class, Object, Sel, BOOL, YES, NO}; + +use cocoa::foundation::{NSAutoreleasePool, NSDate, NSDefaultRunLoopMode, NSPoint, NSRect, NSSize, + NSString, NSUInteger}; + + +struct Minifb { + temp: isize, +} + +impl Minifb { + pub unsafe fn new(name: &str, width: isize, height: isize) -> Result { + let app = match Self::create_app() { + Some(app) => app, + None => { return Err(OsError(format!("Couldn't create NSApplication"))); }, + }; + + let masks = 0u64; + + //let masks = NSResizableWindowMask as NSUInteger | + // NSClosableWindowMask as NSUInteger | + // NSTitledWindowMaskas as NSUInteger; + + let frame = NSRect::new(NSPoint::new(0., 0.), NSSize::new(width as f64, height as f64)); + + let window = IdRef::new(NSWindow::alloc(nil).initWithContentRect_styleMask_backing_defer_( + frame, + masks, + NSBackingStoreBuffered, + NO, + )); + + if window.is_nil() { + return Err(OsError(format!("Unable to create window"))); + } + + } + + fn create_app() -> Option { + unsafe { + let app = NSApp(); + if app == nil { + None + } else { + app.setActivationPolicy_(NSApplicationActivationPolicyRegular); + Some(app) + } + } + } + + +} + +struct IdRef(id); + +impl IdRef { + fn new(i: id) -> IdRef { + IdRef(i) + } + + #[allow(dead_code)] + fn retain(i: id) -> IdRef { + if i != nil { + let _: id = unsafe { msg_send![i, retain] }; + } + IdRef(i) + } + + fn non_nil(self) -> Option { + if self.0 == nil { None } else { Some(self) } + } +} + + diff --git a/src/windows.rs b/src/windows.rs index d4e5e1c..c7d3a5c 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -76,7 +76,6 @@ unsafe extern "system" fn wnd_proc(window: winapi::HWND, user32::ReleaseDC(window, dc); } - _ => (), }