2015-04-24 09:51:23 +02:00
|
|
|
#![cfg(target_os = "macos")]
|
|
|
|
|
|
2014-11-25 23:30:09 +01:00
|
|
|
pub use self::headless::HeadlessContext;
|
|
|
|
|
|
2015-03-26 17:04:01 +01:00
|
|
|
use {CreationError, Event, MouseCursor, CursorState};
|
2014-11-19 16:55:44 +01:00
|
|
|
use CreationError::OsError;
|
2014-10-24 12:54:58 +02:00
|
|
|
use libc;
|
2014-10-04 19:17:02 +02:00
|
|
|
|
2015-02-18 16:49:53 +01:00
|
|
|
use Api;
|
2014-12-28 15:08:41 +01:00
|
|
|
use BuilderAttribs;
|
2015-06-16 10:15:31 +02:00
|
|
|
use ContextError;
|
2015-04-30 13:23:37 +02:00
|
|
|
use GlContext;
|
2015-04-30 23:06:22 -04:00
|
|
|
use GlProfile;
|
2015-02-18 16:49:53 +01:00
|
|
|
use GlRequest;
|
2015-04-11 09:06:08 +02:00
|
|
|
use PixelFormat;
|
2015-06-22 17:58:32 +02:00
|
|
|
use Robustness;
|
2015-03-18 14:16:35 -07:00
|
|
|
use native_monitor::NativeMonitorId;
|
2014-10-04 19:17:02 +02:00
|
|
|
|
2015-03-22 01:31:32 -04:00
|
|
|
use objc::runtime::{Class, Object, Sel, BOOL, YES, NO};
|
|
|
|
|
use objc::declare::ClassDecl;
|
|
|
|
|
|
2015-06-15 23:28:29 +02:00
|
|
|
use cgl;
|
|
|
|
|
use cgl::{CGLEnable, kCGLCECrashOnRemovedFunctions, CGLSetParameter, kCGLCPSurfaceOpacity};
|
|
|
|
|
use cgl::CGLContextObj as CGL_CGLContextObj;
|
|
|
|
|
|
2015-04-02 15:19:51 -07:00
|
|
|
use cocoa::base::{id, nil};
|
|
|
|
|
use cocoa::foundation::{NSAutoreleasePool, NSDate, NSDefaultRunLoopMode, NSPoint, NSRect, NSSize,
|
|
|
|
|
NSString, NSUInteger};
|
2014-10-23 17:18:47 +02:00
|
|
|
use cocoa::appkit;
|
2014-10-04 15:49:39 +02:00
|
|
|
use cocoa::appkit::*;
|
2015-01-30 22:14:49 +11:00
|
|
|
use cocoa::appkit::NSEventSubtype::*;
|
2014-08-03 09:25:30 +02:00
|
|
|
|
2014-10-04 15:49:39 +02:00
|
|
|
use core_foundation::base::TCFType;
|
|
|
|
|
use core_foundation::string::CFString;
|
|
|
|
|
use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFunctionPointerForName};
|
|
|
|
|
|
2015-05-05 21:46:33 +02:00
|
|
|
use core_graphics::display::{CGAssociateMouseAndMouseCursorPosition, CGMainDisplayID, CGDisplayPixelsHigh};
|
2015-04-24 19:33:21 +02:00
|
|
|
|
2015-03-22 01:31:32 -04:00
|
|
|
use std::ffi::CStr;
|
2015-02-21 23:59:37 +11:00
|
|
|
use std::collections::VecDeque;
|
2015-01-10 01:56:47 -08:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
use std::str::from_utf8;
|
2015-02-10 16:33:04 +10:00
|
|
|
use std::sync::Mutex;
|
2015-01-10 01:56:47 -08:00
|
|
|
use std::ascii::AsciiExt;
|
2015-03-16 13:51:15 -07:00
|
|
|
use std::ops::Deref;
|
2014-10-23 17:18:47 +02:00
|
|
|
|
2015-04-12 16:02:06 +02:00
|
|
|
use events::Event::{Awakened, MouseInput, MouseMoved, ReceivedCharacter, KeyboardInput, MouseWheel, Closed};
|
2014-11-19 06:09:54 +01:00
|
|
|
use events::ElementState::{Pressed, Released};
|
2015-02-05 16:52:53 +01:00
|
|
|
use events::MouseButton;
|
2014-10-23 17:18:47 +02:00
|
|
|
use events;
|
2014-10-15 11:49:10 +02:00
|
|
|
|
2014-11-04 18:03:38 +01:00
|
|
|
pub use self::monitor::{MonitorID, get_available_monitors, get_primary_monitor};
|
|
|
|
|
|
|
|
|
|
mod monitor;
|
2014-10-15 11:49:10 +02:00
|
|
|
mod event;
|
2014-11-25 23:30:09 +01:00
|
|
|
mod headless;
|
|
|
|
|
|
2014-10-23 17:18:47 +02:00
|
|
|
static mut shift_pressed: bool = false;
|
|
|
|
|
static mut ctrl_pressed: bool = false;
|
|
|
|
|
static mut win_pressed: bool = false;
|
2014-10-23 18:01:09 +02:00
|
|
|
static mut alt_pressed: bool = false;
|
2014-10-23 17:18:47 +02:00
|
|
|
|
2015-02-21 23:59:37 +11:00
|
|
|
struct DelegateState {
|
2015-03-16 13:51:15 -07:00
|
|
|
context: IdRef,
|
|
|
|
|
view: IdRef,
|
|
|
|
|
window: IdRef,
|
2015-04-05 15:21:11 -07:00
|
|
|
resize_handler: Option<fn(u32, u32)>,
|
2015-04-12 16:02:06 +02:00
|
|
|
|
|
|
|
|
/// Events that have been retreived with XLib but not dispatched with iterators yet
|
|
|
|
|
pending_events: Mutex<VecDeque<Event>>,
|
2014-11-19 10:09:29 +10:00
|
|
|
}
|
|
|
|
|
|
2015-01-20 08:12:53 +11:00
|
|
|
struct WindowDelegate {
|
2015-04-05 15:21:11 -07:00
|
|
|
state: Box<DelegateState>,
|
|
|
|
|
_this: IdRef,
|
2015-01-20 08:12:53 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WindowDelegate {
|
|
|
|
|
/// Get the delegate class, initiailizing it neccessary
|
2015-03-22 01:31:32 -04:00
|
|
|
fn class() -> *const Class {
|
2015-01-20 08:12:53 +11:00
|
|
|
use std::sync::{Once, ONCE_INIT};
|
|
|
|
|
|
2015-03-22 01:31:32 -04:00
|
|
|
extern fn window_should_close(this: &Object, _: Sel, _: id) -> BOOL {
|
2015-01-20 08:12:53 +11:00
|
|
|
unsafe {
|
2015-03-22 01:31:32 -04:00
|
|
|
let state: *mut libc::c_void = *this.get_ivar("glutinState");
|
|
|
|
|
let state = state as *mut DelegateState;
|
2015-04-12 16:02:06 +02:00
|
|
|
(*state).pending_events.lock().unwrap().push_back(Closed);
|
2015-01-20 08:12:53 +11:00
|
|
|
}
|
2015-04-04 01:00:10 +02:00
|
|
|
YES
|
2015-01-20 08:12:53 +11:00
|
|
|
}
|
|
|
|
|
|
2015-03-22 01:31:32 -04:00
|
|
|
extern fn window_did_resize(this: &Object, _: Sel, _: id) {
|
2015-01-20 08:12:53 +11:00
|
|
|
unsafe {
|
2015-03-22 01:31:32 -04:00
|
|
|
let state: *mut libc::c_void = *this.get_ivar("glutinState");
|
|
|
|
|
let state = &mut *(state as *mut DelegateState);
|
2015-01-20 08:12:53 +11:00
|
|
|
|
2015-03-26 21:44:03 -07:00
|
|
|
let _: () = msg_send![*state.context, update];
|
2015-01-20 08:12:53 +11:00
|
|
|
|
2015-04-05 15:21:11 -07:00
|
|
|
if let Some(handler) = state.resize_handler {
|
2015-03-16 13:51:15 -07:00
|
|
|
let rect = NSView::frame(*state.view);
|
|
|
|
|
let scale_factor = NSWindow::backingScaleFactor(*state.window) as f32;
|
2015-01-20 08:12:53 +11:00
|
|
|
(handler)((scale_factor * rect.size.width as f32) as u32,
|
|
|
|
|
(scale_factor * rect.size.height as f32) as u32);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-22 01:31:32 -04:00
|
|
|
static mut delegate_class: *const Class = 0 as *const Class;
|
|
|
|
|
static INIT: Once = ONCE_INIT;
|
|
|
|
|
|
|
|
|
|
INIT.call_once(|| unsafe {
|
|
|
|
|
// Create new NSWindowDelegate
|
|
|
|
|
let superclass = Class::get("NSObject").unwrap();
|
|
|
|
|
let mut decl = ClassDecl::new(superclass, "GlutinWindowDelegate").unwrap();
|
|
|
|
|
|
|
|
|
|
// Add callback methods
|
|
|
|
|
decl.add_method(sel!(windowShouldClose:),
|
|
|
|
|
window_should_close as extern fn(&Object, Sel, id) -> BOOL);
|
|
|
|
|
decl.add_method(sel!(windowDidResize:),
|
|
|
|
|
window_did_resize as extern fn(&Object, Sel, id));
|
|
|
|
|
|
|
|
|
|
// Store internal state as user data
|
|
|
|
|
decl.add_ivar::<*mut libc::c_void>("glutinState");
|
|
|
|
|
|
|
|
|
|
delegate_class = decl.register();
|
|
|
|
|
});
|
2015-01-20 08:12:53 +11:00
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
delegate_class
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-05 15:21:11 -07:00
|
|
|
fn new(state: DelegateState) -> WindowDelegate {
|
|
|
|
|
// Box the state so we can give a pointer to it
|
|
|
|
|
let mut state = Box::new(state);
|
|
|
|
|
let state_ptr: *mut DelegateState = &mut *state;
|
2015-01-20 08:12:53 +11:00
|
|
|
unsafe {
|
2015-04-05 15:21:11 -07:00
|
|
|
let delegate = IdRef::new(msg_send![WindowDelegate::class(), new]);
|
|
|
|
|
|
|
|
|
|
(&mut **delegate).set_ivar("glutinState", state_ptr as *mut libc::c_void);
|
|
|
|
|
let _: () = msg_send![*state.window, setDelegate:*delegate];
|
|
|
|
|
|
|
|
|
|
WindowDelegate { state: state, _this: delegate }
|
2015-01-20 08:12:53 +11:00
|
|
|
}
|
|
|
|
|
}
|
2015-04-05 15:21:11 -07:00
|
|
|
}
|
2015-01-20 08:12:53 +11:00
|
|
|
|
2015-04-05 15:21:11 -07:00
|
|
|
impl Drop for WindowDelegate {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
unsafe {
|
|
|
|
|
// Nil the window's delegate so it doesn't still reference us
|
|
|
|
|
let _: () = msg_send![*self.state.window, setDelegate:nil];
|
|
|
|
|
}
|
2015-01-20 08:12:53 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-04 15:49:39 +02:00
|
|
|
pub struct Window {
|
2015-03-16 13:51:15 -07:00
|
|
|
view: IdRef,
|
|
|
|
|
window: IdRef,
|
|
|
|
|
context: IdRef,
|
2015-04-30 15:41:21 +02:00
|
|
|
pixel_format: PixelFormat,
|
2015-01-20 08:12:53 +11:00
|
|
|
delegate: WindowDelegate,
|
2014-10-04 15:49:39 +02:00
|
|
|
}
|
2014-08-03 09:25:30 +02:00
|
|
|
|
2014-12-29 22:56:15 +01:00
|
|
|
#[cfg(feature = "window")]
|
|
|
|
|
unsafe impl Send for Window {}
|
|
|
|
|
#[cfg(feature = "window")]
|
|
|
|
|
unsafe impl Sync for Window {}
|
|
|
|
|
|
2014-12-17 14:49:11 +10:00
|
|
|
#[cfg(feature = "window")]
|
2015-01-03 23:11:59 +01:00
|
|
|
#[derive(Clone)]
|
2014-12-17 14:49:11 +10:00
|
|
|
pub struct WindowProxy;
|
|
|
|
|
|
|
|
|
|
impl WindowProxy {
|
|
|
|
|
pub fn wakeup_event_loop(&self) {
|
2014-12-18 14:22:58 +10:00
|
|
|
unsafe {
|
|
|
|
|
let pool = NSAutoreleasePool::new(nil);
|
|
|
|
|
let event =
|
2015-01-30 22:14:49 +11:00
|
|
|
NSEvent::otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2_(
|
|
|
|
|
nil, NSApplicationDefined, NSPoint::new(0.0, 0.0), NSEventModifierFlags::empty(),
|
|
|
|
|
0.0, 0, nil, NSApplicationActivatedEventType, 0, 0);
|
2015-01-26 10:59:40 +11:00
|
|
|
NSApp().postEvent_atStart_(event, YES);
|
2014-12-18 14:22:58 +10:00
|
|
|
pool.drain();
|
|
|
|
|
}
|
2014-12-17 14:49:11 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-10 16:33:04 +10:00
|
|
|
pub struct PollEventsIterator<'a> {
|
|
|
|
|
window: &'a Window,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> Iterator for PollEventsIterator<'a> {
|
|
|
|
|
type Item = Event;
|
|
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Event> {
|
2015-04-12 16:02:06 +02:00
|
|
|
if let Some(ev) = self.window.delegate.state.pending_events.lock().unwrap().pop_front() {
|
2015-02-10 16:33:04 +10:00
|
|
|
return Some(ev);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
let event = NSApp().nextEventMatchingMask_untilDate_inMode_dequeue_(
|
|
|
|
|
NSAnyEventMask.bits(),
|
|
|
|
|
NSDate::distantPast(nil),
|
|
|
|
|
NSDefaultRunLoopMode,
|
|
|
|
|
YES);
|
|
|
|
|
if event == nil { return None; }
|
|
|
|
|
|
2015-04-24 15:53:02 +02:00
|
|
|
let event_type = msg_send![event, type];
|
|
|
|
|
NSApp().sendEvent_(if let NSKeyDown = event_type { nil } else { event });
|
|
|
|
|
|
|
|
|
|
let event = match event_type {
|
2015-02-10 16:33:04 +10:00
|
|
|
NSLeftMouseDown => { Some(MouseInput(Pressed, MouseButton::Left)) },
|
|
|
|
|
NSLeftMouseUp => { Some(MouseInput(Released, MouseButton::Left)) },
|
|
|
|
|
NSRightMouseDown => { Some(MouseInput(Pressed, MouseButton::Right)) },
|
|
|
|
|
NSRightMouseUp => { Some(MouseInput(Released, MouseButton::Right)) },
|
2015-02-22 15:15:50 +11:00
|
|
|
NSMouseMoved |
|
|
|
|
|
NSLeftMouseDragged |
|
|
|
|
|
NSOtherMouseDragged |
|
|
|
|
|
NSRightMouseDragged => {
|
2015-02-10 16:33:04 +10:00
|
|
|
let window_point = event.locationInWindow();
|
2015-03-22 01:31:32 -04:00
|
|
|
let window: id = msg_send![event, window];
|
|
|
|
|
let view_point = if window == nil {
|
2015-02-10 16:33:04 +10:00
|
|
|
let window_rect = self.window.window.convertRectFromScreen_(NSRect::new(window_point, NSSize::new(0.0, 0.0)));
|
|
|
|
|
self.window.view.convertPoint_fromView_(window_rect.origin, nil)
|
|
|
|
|
} else {
|
|
|
|
|
self.window.view.convertPoint_fromView_(window_point, nil)
|
|
|
|
|
};
|
2015-03-16 13:51:15 -07:00
|
|
|
let view_rect = NSView::frame(*self.window.view);
|
2015-02-10 16:33:04 +10:00
|
|
|
let scale_factor = self.window.hidpi_factor();
|
2015-04-24 15:53:02 +02:00
|
|
|
|
2015-02-10 16:33:04 +10:00
|
|
|
Some(MouseMoved(((scale_factor * view_point.x as f32) as i32,
|
|
|
|
|
(scale_factor * (view_rect.size.height - view_point.y) as f32) as i32)))
|
|
|
|
|
},
|
|
|
|
|
NSKeyDown => {
|
2015-02-21 23:59:37 +11:00
|
|
|
let mut events = VecDeque::new();
|
2015-02-10 16:33:04 +10:00
|
|
|
let received_c_str = event.characters().UTF8String();
|
2015-02-21 18:32:05 -05:00
|
|
|
let received_str = CStr::from_ptr(received_c_str);
|
|
|
|
|
for received_char in from_utf8(received_str.to_bytes()).unwrap().chars() {
|
2015-02-10 16:33:04 +10:00
|
|
|
if received_char.is_ascii() {
|
|
|
|
|
events.push_back(ReceivedCharacter(received_char));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let vkey = event::vkeycode_to_element(NSEvent::keyCode(event));
|
|
|
|
|
events.push_back(KeyboardInput(Pressed, NSEvent::keyCode(event) as u8, vkey));
|
|
|
|
|
let event = events.pop_front();
|
2015-04-12 16:02:06 +02:00
|
|
|
self.window.delegate.state.pending_events.lock().unwrap().extend(events.into_iter());
|
2015-02-10 16:33:04 +10:00
|
|
|
event
|
|
|
|
|
},
|
|
|
|
|
NSKeyUp => {
|
|
|
|
|
let vkey = event::vkeycode_to_element(NSEvent::keyCode(event));
|
2015-04-24 15:53:02 +02:00
|
|
|
|
2015-02-10 16:33:04 +10:00
|
|
|
Some(KeyboardInput(Released, NSEvent::keyCode(event) as u8, vkey))
|
|
|
|
|
},
|
|
|
|
|
NSFlagsChanged => {
|
2015-02-21 23:59:37 +11:00
|
|
|
let mut events = VecDeque::new();
|
2015-02-10 16:33:04 +10:00
|
|
|
let shift_modifier = Window::modifier_event(event, appkit::NSShiftKeyMask, events::VirtualKeyCode::LShift, shift_pressed);
|
|
|
|
|
if shift_modifier.is_some() {
|
|
|
|
|
shift_pressed = !shift_pressed;
|
|
|
|
|
events.push_back(shift_modifier.unwrap());
|
|
|
|
|
}
|
|
|
|
|
let ctrl_modifier = Window::modifier_event(event, appkit::NSControlKeyMask, events::VirtualKeyCode::LControl, ctrl_pressed);
|
|
|
|
|
if ctrl_modifier.is_some() {
|
|
|
|
|
ctrl_pressed = !ctrl_pressed;
|
|
|
|
|
events.push_back(ctrl_modifier.unwrap());
|
|
|
|
|
}
|
|
|
|
|
let win_modifier = Window::modifier_event(event, appkit::NSCommandKeyMask, events::VirtualKeyCode::LWin, win_pressed);
|
|
|
|
|
if win_modifier.is_some() {
|
|
|
|
|
win_pressed = !win_pressed;
|
|
|
|
|
events.push_back(win_modifier.unwrap());
|
|
|
|
|
}
|
|
|
|
|
let alt_modifier = Window::modifier_event(event, appkit::NSAlternateKeyMask, events::VirtualKeyCode::LAlt, alt_pressed);
|
|
|
|
|
if alt_modifier.is_some() {
|
|
|
|
|
alt_pressed = !alt_pressed;
|
|
|
|
|
events.push_back(alt_modifier.unwrap());
|
|
|
|
|
}
|
|
|
|
|
let event = events.pop_front();
|
2015-04-12 16:02:06 +02:00
|
|
|
self.window.delegate.state.pending_events.lock().unwrap().extend(events.into_iter());
|
2015-02-10 16:33:04 +10:00
|
|
|
event
|
|
|
|
|
},
|
2015-06-12 15:32:11 +01:00
|
|
|
NSScrollWheel => {
|
2015-06-14 18:09:02 +01:00
|
|
|
use events::MouseScrollDelta::{LineDelta, PixelDelta};
|
|
|
|
|
let delta = if event.hasPreciseScrollingDeltas() == YES {
|
|
|
|
|
PixelDelta(event.scrollingDeltaX() as f32, event.scrollingDeltaY() as f32)
|
|
|
|
|
} else {
|
|
|
|
|
LineDelta(event.scrollingDeltaX() as f32, event.scrollingDeltaY() as f32)
|
|
|
|
|
};
|
2015-06-12 15:32:11 +01:00
|
|
|
Some(MouseWheel(delta))
|
|
|
|
|
},
|
2015-02-10 16:33:04 +10:00
|
|
|
_ => { None },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
event
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct WaitEventsIterator<'a> {
|
|
|
|
|
window: &'a Window,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> Iterator for WaitEventsIterator<'a> {
|
|
|
|
|
type Item = Event;
|
|
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Event> {
|
|
|
|
|
loop {
|
2015-04-12 16:02:06 +02:00
|
|
|
if let Some(ev) = self.window.delegate.state.pending_events.lock().unwrap().pop_front() {
|
2015-02-10 16:33:04 +10:00
|
|
|
return Some(ev);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
let event = NSApp().nextEventMatchingMask_untilDate_inMode_dequeue_(
|
|
|
|
|
NSAnyEventMask.bits(),
|
|
|
|
|
NSDate::distantFuture(nil),
|
|
|
|
|
NSDefaultRunLoopMode,
|
|
|
|
|
NO);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// calling poll_events()
|
|
|
|
|
if let Some(ev) = self.window.poll_events().next() {
|
|
|
|
|
return Some(ev);
|
2015-03-20 08:06:46 +10:00
|
|
|
} else {
|
|
|
|
|
return Some(Awakened);
|
2015-02-10 16:33:04 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-04 19:17:02 +02:00
|
|
|
impl Window {
|
2015-01-18 20:11:23 +11:00
|
|
|
#[cfg(feature = "window")]
|
|
|
|
|
pub fn new(builder: BuilderAttribs) -> Result<Window, CreationError> {
|
|
|
|
|
if builder.sharing.is_some() {
|
|
|
|
|
unimplemented!()
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-22 17:58:32 +02:00
|
|
|
match builder.gl_robustness {
|
|
|
|
|
Robustness::RobustNoResetNotification | Robustness::RobustLoseContextOnReset => {
|
2015-07-20 19:34:34 +02:00
|
|
|
return Err(CreationError::RobustnessNotSupported);
|
2015-06-22 17:58:32 +02:00
|
|
|
},
|
|
|
|
|
_ => ()
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-05 23:45:38 +02:00
|
|
|
let app = match Window::create_app() {
|
|
|
|
|
Some(app) => app,
|
2014-11-05 16:42:18 +01:00
|
|
|
None => { return Err(OsError(format!("Couldn't create NSApplication"))); },
|
2014-10-05 23:45:38 +02:00
|
|
|
};
|
2015-04-30 15:41:21 +02:00
|
|
|
|
|
|
|
|
let window = match Window::create_window(&builder)
|
2015-01-18 20:11:23 +11:00
|
|
|
{
|
2014-10-05 23:45:38 +02:00
|
|
|
Some(window) => window,
|
2014-11-05 16:42:18 +01:00
|
|
|
None => { return Err(OsError(format!("Couldn't create NSWindow"))); },
|
2014-10-05 23:45:38 +02:00
|
|
|
};
|
2015-03-16 13:51:15 -07:00
|
|
|
let view = match Window::create_view(*window) {
|
2014-10-05 23:45:38 +02:00
|
|
|
Some(view) => view,
|
2014-11-05 16:42:18 +01:00
|
|
|
None => { return Err(OsError(format!("Couldn't create NSView"))); },
|
2014-10-05 23:45:38 +02:00
|
|
|
};
|
|
|
|
|
|
2015-04-30 15:41:21 +02:00
|
|
|
// TODO: perhaps we should return error from create_context so we can
|
|
|
|
|
// determine the cause of failure and possibly recover?
|
|
|
|
|
let (context, pf) = match Window::create_context(*view, &builder) {
|
2015-05-24 15:52:56 +02:00
|
|
|
Ok((context, pf)) => (context, pf),
|
|
|
|
|
Err(e) => { return Err(OsError(format!("Couldn't create OpenGL context: {}", e))); },
|
2014-10-05 23:45:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
unsafe {
|
2015-06-16 11:29:17 +02:00
|
|
|
if builder.transparent {
|
|
|
|
|
let clear_col = {
|
|
|
|
|
let cls = Class::get("NSColor").unwrap();
|
|
|
|
|
|
|
|
|
|
msg_send![cls, clearColor]
|
|
|
|
|
};
|
|
|
|
|
window.setOpaque_(NO);
|
|
|
|
|
window.setBackgroundColor_(clear_col);
|
|
|
|
|
|
|
|
|
|
let obj = context.CGLContextObj();
|
|
|
|
|
|
|
|
|
|
let mut opacity = 0;
|
|
|
|
|
CGLSetParameter(obj, kCGLCPSurfaceOpacity, &mut opacity);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-26 10:59:40 +11:00
|
|
|
app.activateIgnoringOtherApps_(YES);
|
2015-01-18 20:11:23 +11:00
|
|
|
if builder.visible {
|
2014-12-19 08:44:45 +10:00
|
|
|
window.makeKeyAndOrderFront_(nil);
|
|
|
|
|
} else {
|
|
|
|
|
window.makeKeyWindow();
|
|
|
|
|
}
|
2014-10-05 23:45:38 +02:00
|
|
|
}
|
|
|
|
|
|
2015-04-05 15:21:11 -07:00
|
|
|
let ds = DelegateState {
|
|
|
|
|
context: context.clone(),
|
|
|
|
|
view: view.clone(),
|
|
|
|
|
window: window.clone(),
|
|
|
|
|
resize_handler: None,
|
2015-04-12 16:02:06 +02:00
|
|
|
pending_events: Mutex::new(VecDeque::new()),
|
2015-04-05 15:21:11 -07:00
|
|
|
};
|
|
|
|
|
|
2014-12-16 15:49:22 +10:00
|
|
|
let window = Window {
|
|
|
|
|
view: view,
|
|
|
|
|
window: window,
|
|
|
|
|
context: context,
|
2015-04-30 15:41:21 +02:00
|
|
|
pixel_format: pf,
|
2015-04-05 15:21:11 -07:00
|
|
|
delegate: WindowDelegate::new(ds),
|
2014-12-16 15:49:22 +10:00
|
|
|
};
|
2014-11-19 10:09:29 +10:00
|
|
|
|
2014-10-05 23:45:38 +02:00
|
|
|
Ok(window)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn create_app() -> Option<id> {
|
|
|
|
|
unsafe {
|
|
|
|
|
let app = NSApp();
|
|
|
|
|
if app == nil {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
app.setActivationPolicy_(NSApplicationActivationPolicyRegular);
|
|
|
|
|
app.finishLaunching();
|
|
|
|
|
Some(app)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-04 15:49:39 +02:00
|
|
|
|
2015-04-30 15:41:21 +02:00
|
|
|
fn create_window(builder: &BuilderAttribs) -> Option<IdRef> {
|
2015-04-26 11:57:38 +02:00
|
|
|
unsafe {
|
2015-04-30 15:41:21 +02:00
|
|
|
let screen = match builder.monitor {
|
2015-04-26 11:57:38 +02:00
|
|
|
Some(ref monitor_id) => {
|
|
|
|
|
let native_id = match monitor_id.get_native_identifier() {
|
|
|
|
|
NativeMonitorId::Numeric(num) => num,
|
|
|
|
|
_ => panic!("OS X monitors should always have a numeric native ID")
|
|
|
|
|
};
|
|
|
|
|
let matching_screen = {
|
|
|
|
|
let screens = NSScreen::screens(nil);
|
|
|
|
|
let count: NSUInteger = msg_send![screens, count];
|
|
|
|
|
let key = IdRef::new(NSString::alloc(nil).init_str("NSScreenNumber"));
|
|
|
|
|
let mut matching_screen: Option<id> = None;
|
|
|
|
|
for i in (0..count) {
|
|
|
|
|
let screen = msg_send![screens, objectAtIndex:i as NSUInteger];
|
|
|
|
|
let device_description = NSScreen::deviceDescription(screen);
|
|
|
|
|
let value: id = msg_send![device_description, objectForKey:*key];
|
|
|
|
|
if value != nil {
|
|
|
|
|
let screen_number: NSUInteger = msg_send![value, unsignedIntegerValue];
|
|
|
|
|
if screen_number as u32 == native_id {
|
|
|
|
|
matching_screen = Some(screen);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-03-16 14:09:45 -07:00
|
|
|
}
|
|
|
|
|
}
|
2015-04-26 11:57:38 +02:00
|
|
|
matching_screen
|
|
|
|
|
};
|
|
|
|
|
Some(matching_screen.unwrap_or(NSScreen::mainScreen(nil)))
|
|
|
|
|
},
|
|
|
|
|
None => None
|
|
|
|
|
};
|
2015-03-16 14:09:45 -07:00
|
|
|
let frame = match screen {
|
|
|
|
|
Some(screen) => NSScreen::frame(screen),
|
|
|
|
|
None => {
|
2015-04-30 15:41:21 +02:00
|
|
|
let (width, height) = builder.dimensions.unwrap_or((800, 600));
|
2015-03-16 14:09:45 -07:00
|
|
|
NSRect::new(NSPoint::new(0., 0.), NSSize::new(width as f64, height as f64))
|
|
|
|
|
}
|
2014-11-06 16:40:30 +01:00
|
|
|
};
|
|
|
|
|
|
2015-06-16 11:29:17 +02:00
|
|
|
let masks = if screen.is_some() || !builder.decorations {
|
2015-01-18 20:22:33 +11:00
|
|
|
NSBorderlessWindowMask as NSUInteger
|
|
|
|
|
} else {
|
|
|
|
|
NSTitledWindowMask as NSUInteger |
|
|
|
|
|
NSClosableWindowMask as NSUInteger |
|
|
|
|
|
NSMiniaturizableWindowMask as NSUInteger |
|
|
|
|
|
NSResizableWindowMask as NSUInteger
|
2014-11-06 16:40:30 +01:00
|
|
|
};
|
2014-10-04 15:49:39 +02:00
|
|
|
|
2015-03-16 13:51:15 -07:00
|
|
|
let window = IdRef::new(NSWindow::alloc(nil).initWithContentRect_styleMask_backing_defer_(
|
2015-01-18 20:22:33 +11:00
|
|
|
frame,
|
2014-11-06 16:40:30 +01:00
|
|
|
masks,
|
2014-10-04 15:49:39 +02:00
|
|
|
NSBackingStoreBuffered,
|
2015-01-26 10:59:40 +11:00
|
|
|
NO,
|
2015-03-16 13:51:15 -07:00
|
|
|
));
|
2015-03-18 14:49:16 -07:00
|
|
|
window.non_nil().map(|window| {
|
2015-04-30 15:41:21 +02:00
|
|
|
let title = IdRef::new(NSString::alloc(nil).init_str(&builder.title));
|
2015-03-16 13:51:15 -07:00
|
|
|
window.setTitle_(*title);
|
2015-01-26 10:59:40 +11:00
|
|
|
window.setAcceptsMouseMovedEvents_(YES);
|
2015-03-16 14:09:45 -07:00
|
|
|
if screen.is_some() {
|
2014-11-06 16:40:30 +01:00
|
|
|
window.setLevel_(NSMainMenuWindowLevel as i64 + 1);
|
|
|
|
|
}
|
2014-11-28 19:13:58 +01:00
|
|
|
else {
|
|
|
|
|
window.center();
|
|
|
|
|
}
|
2015-03-18 14:49:16 -07:00
|
|
|
window
|
|
|
|
|
})
|
2014-10-05 23:45:38 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-16 13:51:15 -07:00
|
|
|
fn create_view(window: id) -> Option<IdRef> {
|
2014-10-05 23:45:38 +02:00
|
|
|
unsafe {
|
2015-03-16 13:51:15 -07:00
|
|
|
let view = IdRef::new(NSView::alloc(nil).init());
|
2015-03-18 14:49:16 -07:00
|
|
|
view.non_nil().map(|view| {
|
2015-01-26 10:59:40 +11:00
|
|
|
view.setWantsBestResolutionOpenGLSurface_(YES);
|
2015-03-16 13:51:15 -07:00
|
|
|
window.setContentView_(*view);
|
2015-03-18 14:49:16 -07:00
|
|
|
view
|
|
|
|
|
})
|
2014-10-05 23:45:38 +02:00
|
|
|
}
|
|
|
|
|
}
|
2014-10-04 15:49:39 +02:00
|
|
|
|
2015-05-24 15:52:56 +02:00
|
|
|
fn create_context(view: id, builder: &BuilderAttribs) -> Result<(IdRef, PixelFormat), CreationError> {
|
2015-04-30 23:06:22 -04:00
|
|
|
let profile = match (builder.gl_version, builder.gl_version.to_gl_version(), builder.gl_profile) {
|
|
|
|
|
(GlRequest::Latest, _, Some(GlProfile::Compatibility)) => NSOpenGLProfileVersionLegacy as u32,
|
2015-05-24 15:52:56 +02:00
|
|
|
(GlRequest::Latest, _, _) => {
|
|
|
|
|
if NSAppKitVersionNumber.floor() >= NSAppKitVersionNumber10_9 {
|
|
|
|
|
NSOpenGLProfileVersion4_1Core as u32
|
|
|
|
|
} else if NSAppKitVersionNumber.floor() >= NSAppKitVersionNumber10_7 {
|
|
|
|
|
NSOpenGLProfileVersion3_2Core as u32
|
|
|
|
|
} else {
|
|
|
|
|
NSOpenGLProfileVersionLegacy as u32
|
|
|
|
|
}
|
|
|
|
|
},
|
2015-05-02 06:41:36 +01:00
|
|
|
(_, Some((1 ... 2, _)), Some(GlProfile::Core)) |
|
|
|
|
|
(_, Some((3 ... 4, _)), Some(GlProfile::Compatibility)) =>
|
2015-04-29 23:05:47 -04:00
|
|
|
return Err(CreationError::NotSupported),
|
2015-05-02 06:41:36 +01:00
|
|
|
(_, Some((1 ... 2, _)), _) => NSOpenGLProfileVersionLegacy as u32,
|
|
|
|
|
(_, Some((3, 0 ... 2)), _) => NSOpenGLProfileVersion3_2Core as u32,
|
|
|
|
|
(_, Some((3 ... 4, _)), _) => NSOpenGLProfileVersion4_1Core as u32,
|
2015-04-29 23:05:47 -04:00
|
|
|
_ => return Err(CreationError::NotSupported),
|
2014-12-29 13:26:44 +01:00
|
|
|
};
|
2015-04-26 11:57:38 +02:00
|
|
|
|
2015-04-30 15:41:21 +02:00
|
|
|
// NOTE: OS X no longer has the concept of setting individual
|
|
|
|
|
// color component's bit size. Instead we can only specify the
|
|
|
|
|
// full color size and hope for the best. Another hiccup is that
|
|
|
|
|
// `NSOpenGLPFAColorSize` also includes `NSOpenGLPFAAlphaSize`,
|
|
|
|
|
// so we have to account for that as well.
|
|
|
|
|
let alpha_depth = builder.alpha_bits.unwrap_or(8);
|
|
|
|
|
let color_depth = builder.color_bits.unwrap_or(24) + alpha_depth;
|
|
|
|
|
|
|
|
|
|
let mut attributes = vec![
|
|
|
|
|
NSOpenGLPFADoubleBuffer as u32,
|
|
|
|
|
NSOpenGLPFAClosestPolicy as u32,
|
|
|
|
|
NSOpenGLPFAColorSize as u32, color_depth as u32,
|
|
|
|
|
NSOpenGLPFAAlphaSize as u32, alpha_depth as u32,
|
|
|
|
|
NSOpenGLPFADepthSize as u32, builder.depth_bits.unwrap_or(24) as u32,
|
|
|
|
|
NSOpenGLPFAStencilSize as u32, builder.stencil_bits.unwrap_or(8) as u32,
|
|
|
|
|
NSOpenGLPFAOpenGLProfile as u32, profile,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// A color depth higher than 64 implies we're using either 16-bit
|
|
|
|
|
// floats or 32-bit floats and OS X requires a flag to be set
|
|
|
|
|
// accordingly.
|
|
|
|
|
if color_depth >= 64 {
|
|
|
|
|
attributes.push(NSOpenGLPFAColorFloat as u32);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
builder.multisampling.map(|samples| {
|
|
|
|
|
attributes.push(NSOpenGLPFAMultisample as u32);
|
|
|
|
|
attributes.push(NSOpenGLPFASampleBuffers as u32); attributes.push(1);
|
|
|
|
|
attributes.push(NSOpenGLPFASamples as u32); attributes.push(samples as u32);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// attribute list must be null terminated.
|
|
|
|
|
attributes.push(0);
|
2015-04-26 11:57:38 +02:00
|
|
|
|
2015-05-24 15:52:56 +02:00
|
|
|
unsafe {
|
2015-03-16 13:51:15 -07:00
|
|
|
let pixelformat = IdRef::new(NSOpenGLPixelFormat::alloc(nil).initWithAttributes_(&attributes));
|
2015-04-30 15:41:21 +02:00
|
|
|
|
|
|
|
|
if let Some(pixelformat) = pixelformat.non_nil() {
|
|
|
|
|
|
|
|
|
|
// TODO: Add context sharing
|
2015-03-18 14:49:16 -07:00
|
|
|
let context = IdRef::new(NSOpenGLContext::alloc(nil).initWithFormat_shareContext_(*pixelformat, nil));
|
2015-04-30 15:41:21 +02:00
|
|
|
|
|
|
|
|
if let Some(cxt) = context.non_nil() {
|
|
|
|
|
let pf = {
|
|
|
|
|
let get_attr = |attrib: NSOpenGLPixelFormatAttribute| -> i32 {
|
|
|
|
|
let mut value = 0;
|
2015-05-05 21:46:33 +02:00
|
|
|
|
|
|
|
|
NSOpenGLPixelFormat::getValues_forAttribute_forVirtualScreen_(
|
2015-04-30 15:41:21 +02:00
|
|
|
*pixelformat,
|
|
|
|
|
&mut value,
|
|
|
|
|
attrib,
|
|
|
|
|
NSOpenGLContext::currentVirtualScreen(*cxt));
|
|
|
|
|
|
|
|
|
|
value
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
PixelFormat {
|
|
|
|
|
hardware_accelerated: get_attr(NSOpenGLPFAAccelerated) != 0,
|
|
|
|
|
color_bits: (get_attr(NSOpenGLPFAColorSize) - get_attr(NSOpenGLPFAAlphaSize)) as u8,
|
|
|
|
|
alpha_bits: get_attr(NSOpenGLPFAAlphaSize) as u8,
|
|
|
|
|
depth_bits: get_attr(NSOpenGLPFADepthSize) as u8,
|
|
|
|
|
stencil_bits: get_attr(NSOpenGLPFAStencilSize) as u8,
|
|
|
|
|
stereoscopy: get_attr(NSOpenGLPFAStereo) != 0,
|
|
|
|
|
double_buffer: get_attr(NSOpenGLPFADoubleBuffer) != 0,
|
|
|
|
|
multisampling: if get_attr(NSOpenGLPFAMultisample) > 0 {
|
|
|
|
|
Some(get_attr(NSOpenGLPFASamples) as u16)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
},
|
|
|
|
|
srgb: true,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
cxt.setView_(view);
|
2015-05-23 18:35:47 +02:00
|
|
|
let value = if builder.vsync { 1 } else { 0 };
|
|
|
|
|
cxt.setValues_forParameter_(&value, NSOpenGLContextParameter::NSOpenGLCPSwapInterval);
|
2015-04-30 15:41:21 +02:00
|
|
|
|
2015-06-15 23:28:29 +02:00
|
|
|
CGLEnable(cxt.CGLContextObj(), kCGLCECrashOnRemovedFunctions);
|
|
|
|
|
|
2015-05-24 15:52:56 +02:00
|
|
|
Ok((cxt, pf))
|
2015-04-30 15:41:21 +02:00
|
|
|
} else {
|
2015-05-24 15:52:56 +02:00
|
|
|
Err(CreationError::NotSupported)
|
2015-04-30 15:41:21 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
2015-05-24 15:52:56 +02:00
|
|
|
Err(CreationError::NotSupported)
|
2015-04-30 15:41:21 +02:00
|
|
|
}
|
2015-05-24 15:52:56 +02:00
|
|
|
}
|
2014-08-03 09:25:30 +02:00
|
|
|
}
|
|
|
|
|
|
2014-11-10 20:12:32 +11:00
|
|
|
pub fn set_title(&self, title: &str) {
|
|
|
|
|
unsafe {
|
2015-03-16 13:51:15 -07:00
|
|
|
let title = IdRef::new(NSString::alloc(nil).init_str(title));
|
|
|
|
|
self.window.setTitle_(*title);
|
2014-11-10 20:12:32 +11:00
|
|
|
}
|
2014-08-03 09:25:30 +02:00
|
|
|
}
|
|
|
|
|
|
2014-11-01 09:02:01 +01:00
|
|
|
pub fn show(&self) {
|
2015-03-16 13:51:15 -07:00
|
|
|
unsafe { NSWindow::makeKeyAndOrderFront_(*self.window, nil); }
|
2014-11-01 09:02:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn hide(&self) {
|
2015-03-16 13:51:15 -07:00
|
|
|
unsafe { NSWindow::orderOut_(*self.window, nil); }
|
2014-11-01 09:02:01 +01:00
|
|
|
}
|
|
|
|
|
|
2015-01-15 20:49:03 +01:00
|
|
|
pub fn get_position(&self) -> Option<(i32, i32)> {
|
2015-01-26 14:22:50 +11:00
|
|
|
unsafe {
|
2015-03-16 13:51:15 -07:00
|
|
|
let content_rect = NSWindow::contentRectForFrameRect_(*self.window, NSWindow::frame(*self.window));
|
2015-04-24 19:33:21 +02:00
|
|
|
|
|
|
|
|
// TODO: consider extrapolating the calculations for the y axis to
|
|
|
|
|
// a private method
|
|
|
|
|
Some((content_rect.origin.x as i32, (CGDisplayPixelsHigh(CGMainDisplayID()) as f64 - (content_rect.origin.y + content_rect.size.height)) as i32))
|
2015-01-26 14:22:50 +11:00
|
|
|
}
|
2014-08-03 09:25:30 +02:00
|
|
|
}
|
|
|
|
|
|
2015-01-26 14:22:50 +11:00
|
|
|
pub fn set_position(&self, x: i32, y: i32) {
|
|
|
|
|
unsafe {
|
2015-04-24 19:33:21 +02:00
|
|
|
let frame = NSWindow::frame(*self.view);
|
|
|
|
|
|
|
|
|
|
// NOTE: `setFrameOrigin` might not give desirable results when
|
|
|
|
|
// setting window, as it treats bottom left as origin.
|
|
|
|
|
// `setFrameTopLeftPoint` treats top left as origin (duh), but
|
|
|
|
|
// does not equal the value returned by `get_window_position`
|
|
|
|
|
// (there is a difference by 22 for me on yosemite)
|
|
|
|
|
|
|
|
|
|
// TODO: consider extrapolating the calculations for the y axis to
|
|
|
|
|
// a private method
|
|
|
|
|
let dummy = NSRect::new(NSPoint::new(x as f64, CGDisplayPixelsHigh(CGMainDisplayID()) as f64 - (frame.size.height + y as f64)), NSSize::new(0f64, 0f64));
|
|
|
|
|
let conv = NSWindow::frameRectForContentRect_(*self.window, dummy);
|
|
|
|
|
|
|
|
|
|
// NSWindow::setFrameTopLeftPoint_(*self.window, conv.origin);
|
|
|
|
|
NSWindow::setFrameOrigin_(*self.window, conv.origin);
|
2015-01-26 14:22:50 +11:00
|
|
|
}
|
2014-08-03 09:25:30 +02:00
|
|
|
}
|
|
|
|
|
|
2015-01-15 20:20:52 +01:00
|
|
|
pub fn get_inner_size(&self) -> Option<(u32, u32)> {
|
2015-01-26 14:22:50 +11:00
|
|
|
unsafe {
|
2015-03-16 13:51:15 -07:00
|
|
|
let view_frame = NSView::frame(*self.view);
|
2015-01-26 14:22:50 +11:00
|
|
|
Some((view_frame.size.width as u32, view_frame.size.height as u32))
|
|
|
|
|
}
|
2014-08-03 09:25:30 +02:00
|
|
|
}
|
|
|
|
|
|
2015-01-15 20:20:52 +01:00
|
|
|
pub fn get_outer_size(&self) -> Option<(u32, u32)> {
|
2015-01-26 14:22:50 +11:00
|
|
|
unsafe {
|
2015-03-16 13:51:15 -07:00
|
|
|
let window_frame = NSWindow::frame(*self.window);
|
2015-01-26 14:22:50 +11:00
|
|
|
Some((window_frame.size.width as u32, window_frame.size.height as u32))
|
|
|
|
|
}
|
2014-08-03 09:25:30 +02:00
|
|
|
}
|
|
|
|
|
|
2015-01-26 14:22:50 +11:00
|
|
|
pub fn set_inner_size(&self, width: u32, height: u32) {
|
|
|
|
|
unsafe {
|
2015-03-16 13:51:15 -07:00
|
|
|
NSWindow::setContentSize_(*self.window, NSSize::new(width as f64, height as f64));
|
2015-01-26 14:22:50 +11:00
|
|
|
}
|
2014-08-03 09:25:30 +02:00
|
|
|
}
|
|
|
|
|
|
2014-12-17 14:49:11 +10:00
|
|
|
pub fn create_window_proxy(&self) -> WindowProxy {
|
|
|
|
|
WindowProxy
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-10 16:33:04 +10:00
|
|
|
pub fn poll_events(&self) -> PollEventsIterator {
|
|
|
|
|
PollEventsIterator {
|
|
|
|
|
window: self
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-15 11:49:10 +02:00
|
|
|
|
2015-02-10 16:33:04 +10:00
|
|
|
pub fn wait_events(&self) -> WaitEventsIterator {
|
|
|
|
|
WaitEventsIterator {
|
|
|
|
|
window: self
|
2014-10-04 15:49:39 +02:00
|
|
|
}
|
2014-10-06 17:54:15 +02:00
|
|
|
}
|
|
|
|
|
|
2015-01-30 22:14:49 +11:00
|
|
|
unsafe fn modifier_event(event: id, keymask: NSEventModifierFlags, key: events::VirtualKeyCode, key_pressed: bool) -> Option<Event> {
|
|
|
|
|
if !key_pressed && NSEvent::modifierFlags(event).contains(keymask) {
|
|
|
|
|
return Some(KeyboardInput(Pressed, NSEvent::keyCode(event) as u8, Some(key)));
|
|
|
|
|
} else if key_pressed && !NSEvent::modifierFlags(event).contains(keymask) {
|
|
|
|
|
return Some(KeyboardInput(Released, NSEvent::keyCode(event) as u8, Some(key)));
|
2014-10-23 17:18:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-24 15:20:25 +10:00
|
|
|
pub fn platform_display(&self) -> *mut libc::c_void {
|
|
|
|
|
unimplemented!()
|
|
|
|
|
}
|
2014-11-18 17:55:26 +01:00
|
|
|
|
2015-02-20 12:32:40 -08:00
|
|
|
pub fn platform_window(&self) -> *mut libc::c_void {
|
|
|
|
|
unimplemented!()
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-15 20:59:54 +01:00
|
|
|
pub fn set_window_resize_callback(&mut self, callback: Option<fn(u32, u32)>) {
|
2015-04-05 15:21:11 -07:00
|
|
|
self.delegate.state.resize_handler = callback;
|
2014-12-16 15:49:22 +10:00
|
|
|
}
|
2015-01-12 16:22:37 -08:00
|
|
|
|
|
|
|
|
pub fn set_cursor(&self, cursor: MouseCursor) {
|
2015-02-27 01:27:54 -08:00
|
|
|
let cursor_name = match cursor {
|
2015-04-14 22:15:55 +02:00
|
|
|
MouseCursor::Arrow | MouseCursor::Default => "arrowCursor",
|
|
|
|
|
MouseCursor::Hand => "pointingHandCursor",
|
|
|
|
|
MouseCursor::Grabbing | MouseCursor::Grab => "closedHandCursor",
|
2015-02-27 01:27:54 -08:00
|
|
|
MouseCursor::Text => "IBeamCursor",
|
2015-04-14 22:15:55 +02:00
|
|
|
MouseCursor::VerticalText => "IBeamCursorForVerticalLayout",
|
2015-02-27 01:27:54 -08:00
|
|
|
MouseCursor::Copy => "dragCopyCursor",
|
|
|
|
|
MouseCursor::Alias => "dragLinkCursor",
|
2015-04-14 22:15:55 +02:00
|
|
|
MouseCursor::NotAllowed | MouseCursor::NoDrop => "operationNotAllowedCursor",
|
|
|
|
|
MouseCursor::ContextMenu => "contextualMenuCursor",
|
|
|
|
|
MouseCursor::Crosshair => "crosshairCursor",
|
|
|
|
|
MouseCursor::EResize => "resizeRightCursor",
|
|
|
|
|
MouseCursor::NResize => "resizeUpCursor",
|
|
|
|
|
MouseCursor::WResize => "resizeLeftCursor",
|
|
|
|
|
MouseCursor::SResize => "resizeDownCursor",
|
|
|
|
|
MouseCursor::EwResize | MouseCursor::ColResize => "resizeLeftRightCursor",
|
|
|
|
|
MouseCursor::NsResize | MouseCursor::RowResize => "resizeUpDownCursor",
|
2015-02-27 01:27:54 -08:00
|
|
|
|
|
|
|
|
/// TODO: Find appropriate OSX cursors
|
2015-04-14 22:15:55 +02:00
|
|
|
MouseCursor::NeResize | MouseCursor::NwResize |
|
|
|
|
|
MouseCursor::SeResize | MouseCursor::SwResize |
|
|
|
|
|
MouseCursor::NwseResize | MouseCursor::NeswResize |
|
|
|
|
|
|
|
|
|
|
MouseCursor::Cell | MouseCursor::NoneCursor |
|
2015-02-27 01:27:54 -08:00
|
|
|
MouseCursor::Wait | MouseCursor::Progress | MouseCursor::Help |
|
|
|
|
|
MouseCursor::Move | MouseCursor::AllScroll | MouseCursor::ZoomIn |
|
|
|
|
|
MouseCursor::ZoomOut => "arrowCursor",
|
|
|
|
|
};
|
2015-03-22 01:31:32 -04:00
|
|
|
let sel = Sel::register(cursor_name);
|
|
|
|
|
let cls = Class::get("NSCursor").unwrap();
|
2015-02-27 01:27:54 -08:00
|
|
|
unsafe {
|
2015-03-22 01:31:32 -04:00
|
|
|
use objc::MessageArguments;
|
|
|
|
|
let cursor: id = ().send(cls as *const _ as id, sel);
|
2015-03-26 21:44:03 -07:00
|
|
|
let _: () = msg_send![cursor, set];
|
2015-02-27 01:27:54 -08:00
|
|
|
}
|
2015-01-12 16:22:37 -08:00
|
|
|
}
|
2014-12-19 11:27:03 +10:00
|
|
|
|
2015-03-26 17:04:01 +01:00
|
|
|
pub fn set_cursor_state(&self, state: CursorState) -> Result<(), String> {
|
2015-04-14 22:15:55 +02:00
|
|
|
let cls = Class::get("NSCursor").unwrap();
|
2015-05-05 21:46:33 +02:00
|
|
|
|
|
|
|
|
// TODO: Check for errors.
|
2015-04-14 22:15:55 +02:00
|
|
|
match state {
|
|
|
|
|
CursorState::Normal => {
|
|
|
|
|
let _: () = unsafe { msg_send![cls, unhide] };
|
2015-05-05 21:46:33 +02:00
|
|
|
let _: i32 = unsafe { CGAssociateMouseAndMouseCursorPosition(true) };
|
2015-04-14 22:15:55 +02:00
|
|
|
Ok(())
|
|
|
|
|
},
|
|
|
|
|
CursorState::Hide => {
|
|
|
|
|
let _: () = unsafe { msg_send![cls, hide] };
|
|
|
|
|
Ok(())
|
|
|
|
|
},
|
|
|
|
|
CursorState::Grab => {
|
2015-05-05 21:46:33 +02:00
|
|
|
let _: i32 = unsafe { CGAssociateMouseAndMouseCursorPosition(false) };
|
|
|
|
|
Ok(())
|
2015-04-14 22:15:55 +02:00
|
|
|
}
|
|
|
|
|
}
|
2015-03-26 17:04:01 +01:00
|
|
|
}
|
|
|
|
|
|
2014-12-19 11:27:03 +10:00
|
|
|
pub fn hidpi_factor(&self) -> f32 {
|
|
|
|
|
unsafe {
|
2015-03-16 13:51:15 -07:00
|
|
|
NSWindow::backingScaleFactor(*self.window) as f32
|
2014-12-19 11:27:03 +10:00
|
|
|
}
|
|
|
|
|
}
|
2015-03-10 10:29:07 +01:00
|
|
|
|
|
|
|
|
pub fn set_cursor_position(&self, x: i32, y: i32) -> Result<(), ()> {
|
|
|
|
|
unimplemented!();
|
|
|
|
|
}
|
2014-08-03 09:25:30 +02:00
|
|
|
}
|
2015-03-16 13:51:15 -07:00
|
|
|
|
2015-04-30 13:23:37 +02:00
|
|
|
impl GlContext for Window {
|
2015-06-16 10:15:31 +02:00
|
|
|
unsafe fn make_current(&self) -> Result<(), ContextError> {
|
2015-04-30 13:23:37 +02:00
|
|
|
let _: () = msg_send![*self.context, update];
|
|
|
|
|
self.context.makeCurrentContext();
|
2015-06-16 10:15:31 +02:00
|
|
|
Ok(())
|
2015-04-30 13:23:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_current(&self) -> bool {
|
|
|
|
|
unsafe {
|
|
|
|
|
let current = NSOpenGLContext::currentContext(nil);
|
|
|
|
|
if current != nil {
|
|
|
|
|
let is_equal: BOOL = msg_send![current, isEqual:*self.context];
|
|
|
|
|
is_equal != NO
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_proc_address(&self, addr: &str) -> *const libc::c_void {
|
2015-04-30 18:25:55 +02:00
|
|
|
let symbol_name: CFString = FromStr::from_str(addr).unwrap();
|
2015-04-30 13:23:37 +02:00
|
|
|
let framework_name: CFString = FromStr::from_str("com.apple.opengl").unwrap();
|
|
|
|
|
let framework = unsafe {
|
|
|
|
|
CFBundleGetBundleWithIdentifier(framework_name.as_concrete_TypeRef())
|
|
|
|
|
};
|
|
|
|
|
let symbol = unsafe {
|
|
|
|
|
CFBundleGetFunctionPointerForName(framework, symbol_name.as_concrete_TypeRef())
|
|
|
|
|
};
|
|
|
|
|
symbol as *const _
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-16 10:15:31 +02:00
|
|
|
fn swap_buffers(&self) -> Result<(), ContextError> {
|
2015-04-30 13:23:37 +02:00
|
|
|
unsafe { self.context.flushBuffer(); }
|
2015-06-16 10:15:31 +02:00
|
|
|
Ok(())
|
2015-04-30 13:23:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_api(&self) -> ::Api {
|
|
|
|
|
::Api::OpenGl
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_pixel_format(&self) -> PixelFormat {
|
|
|
|
|
self.pixel_format.clone()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-16 13:51:15 -07:00
|
|
|
struct IdRef(id);
|
|
|
|
|
|
|
|
|
|
impl IdRef {
|
|
|
|
|
fn new(i: id) -> IdRef {
|
|
|
|
|
IdRef(i)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn retain(i: id) -> IdRef {
|
2015-03-18 14:49:16 -07:00
|
|
|
if i != nil {
|
2015-03-26 21:44:03 -07:00
|
|
|
let _: id = unsafe { msg_send![i, retain] };
|
2015-03-18 14:49:16 -07:00
|
|
|
}
|
2015-03-16 13:51:15 -07:00
|
|
|
IdRef(i)
|
|
|
|
|
}
|
2015-03-18 14:49:16 -07:00
|
|
|
|
|
|
|
|
fn non_nil(self) -> Option<IdRef> {
|
|
|
|
|
if self.0 == nil { None } else { Some(self) }
|
|
|
|
|
}
|
2015-03-16 13:51:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for IdRef {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
if self.0 != nil {
|
2015-03-26 21:44:03 -07:00
|
|
|
let _: () = unsafe { msg_send![self.0, release] };
|
2015-03-16 13:51:15 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Deref for IdRef {
|
|
|
|
|
type Target = id;
|
|
|
|
|
fn deref<'a>(&'a self) -> &'a id {
|
|
|
|
|
&self.0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Clone for IdRef {
|
|
|
|
|
fn clone(&self) -> IdRef {
|
|
|
|
|
if self.0 != nil {
|
2015-03-26 21:44:03 -07:00
|
|
|
let _: id = unsafe { msg_send![self.0, retain] };
|
2015-03-16 13:51:15 -07:00
|
|
|
}
|
|
|
|
|
IdRef(self.0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|