aboutsummaryrefslogtreecommitdiffstats
path: root/src/osx
diff options
context:
space:
mode:
authorGlenn Watson <gw@intuitionlibrary.com>2014-12-16 15:49:22 +1000
committerGlenn Watson <gw@intuitionlibrary.com>2014-12-19 05:44:20 +1000
commit0ad9c3d453076adc5d94008d7e155d7ee5536225 (patch)
tree21deae10784df127b9989e2cfe4a472686ff1655 /src/osx
parent0164449955d1b4787fce03f8800e32f19fd04a01 (diff)
downloadglutin-0ad9c3d453076adc5d94008d7e155d7ee5536225.tar.gz
glutin-0ad9c3d453076adc5d94008d7e155d7ee5536225.zip
Add callback function to allow resize messages to be sent on mac.
Diffstat (limited to 'src/osx')
-rw-r--r--src/osx/mod.rs109
1 files changed, 77 insertions, 32 deletions
diff --git a/src/osx/mod.rs b/src/osx/mod.rs
index 42a9ea2..2634491 100644
--- a/src/osx/mod.rs
+++ b/src/osx/mod.rs
@@ -18,12 +18,12 @@ use core_foundation::base::TCFType;
use core_foundation::string::CFString;
use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFunctionPointerForName};
+use std::cell::Cell;
use std::c_str::CString;
use std::mem;
use std::ptr;
-use std::sync::atomic::{AtomicBool, Relaxed};
-use events::Event::{MouseInput, MouseMoved, ReceivedCharacter, KeyboardInput, MouseWheel};
+use events::Event::{MouseInput, MouseMoved, ReceivedCharacter, KeyboardInput, Resized, MouseWheel};
use events::ElementState::{Pressed, Released};
use events::MouseButton::{LeftMouseButton, RightMouseButton};
use events;
@@ -42,25 +42,24 @@ static mut win_pressed: bool = false;
static mut alt_pressed: bool = false;
static DELEGATE_NAME: &'static [u8] = b"glutin_window_delegate\0";
-static DELEGATE_THIS_IVAR: &'static [u8] = b"glutin_this";
+static DELEGATE_STATE_IVAR: &'static [u8] = b"glutin_state";
-struct InternalState {
- is_closed: AtomicBool,
-}
-
-impl InternalState {
- fn new() -> InternalState {
- InternalState {
- is_closed: AtomicBool::new(false),
- }
- }
+struct DelegateState<'a> {
+ is_closed: bool,
+ context: id,
+ view: id,
+ window: &'a Window,
+ handler: fn(uint, uint),
}
pub struct Window {
view: id,
window: id,
context: id,
- state: Box<InternalState>,
+ delegate: id,
+ resize: fn(uint, uint),
+
+ is_closed: Cell<bool>,
}
#[cfg(feature = "window")]
@@ -103,9 +102,24 @@ impl WindowProxy {
extern fn window_should_close(this: id, _: id) -> id {
unsafe {
let mut stored_value = ptr::null_mut();
- object_getInstanceVariable(this, DELEGATE_THIS_IVAR.as_ptr() as *const i8, &mut stored_value);
- let state = stored_value as *mut InternalState;
- (*state).is_closed.store(true, Relaxed);
+ object_getInstanceVariable(this, DELEGATE_STATE_IVAR.as_ptr() as *const i8, &mut stored_value);
+ let state = stored_value as *mut DelegateState;
+
+ (*state).is_closed = true;
+ }
+ 0
+}
+
+extern fn window_did_resize(this: id, _: id) -> id {
+ unsafe {
+ let mut stored_value = ptr::null_mut();
+ object_getInstanceVariable(this, DELEGATE_STATE_IVAR.as_ptr() as *const i8, &mut stored_value);
+ let state = &mut *(stored_value as *mut DelegateState);
+
+ let _: id = msg_send()(state.context, selector("update"));
+
+ let rect = NSView::frame(state.view);
+ (state.handler)(rect.size.width as uint, rect.size.height as uint);
}
0
}
@@ -136,34 +150,41 @@ impl Window {
window.makeKeyAndOrderFront_(nil);
}
- let window = Window {
- view: view,
- window: window,
- context: context,
- state: box InternalState::new(),
- };
-
// Set up the window delegate to receive events
let ptr_size = mem::size_of::<libc::intptr_t>() as u64;
let ns_object = class("NSObject");
- unsafe {
+ let delegate = unsafe {
// Create a delegate class, add callback methods and store InternalState as user data.
let delegate = objc_allocateClassPair(ns_object, DELEGATE_NAME.as_ptr() as *const i8, 0);
class_addMethod(delegate, selector("windowShouldClose:"), window_should_close, "B@:@".to_c_str().as_ptr());
- class_addIvar(delegate, DELEGATE_THIS_IVAR.as_ptr() as *const i8, ptr_size, 3, "?".to_c_str().as_ptr());
+ class_addMethod(delegate, selector("windowDidResize:"), window_did_resize, "V@:@".to_c_str().as_ptr());
+ class_addIvar(delegate, DELEGATE_STATE_IVAR.as_ptr() as *const i8, ptr_size, 3, "?".to_c_str().as_ptr());
objc_registerClassPair(delegate);
let del_obj = msg_send()(delegate, selector("alloc"));
let del_obj: id = msg_send()(del_obj, selector("init"));
- object_setInstanceVariable(del_obj, DELEGATE_THIS_IVAR.as_ptr() as *const i8,
- &*window.state as *const InternalState as *mut libc::c_void);
- let _: id = msg_send()(window.window, selector("setDelegate:"), del_obj);
- }
+ let _: id = msg_send()(window, selector("setDelegate:"), del_obj);
+ del_obj
+ };
+
+ let window = Window {
+ view: view,
+ window: window,
+ context: context,
+ delegate: delegate,
+ resize: Window::resize,
+
+ is_closed: Cell::new(false),
+ };
Ok(window)
}
+ fn resize(_: uint, _: uint) {
+
+ }
+
fn create_app() -> Option<id> {
unsafe {
let app = NSApp();
@@ -267,7 +288,7 @@ impl Window {
}
pub fn is_closed(&self) -> bool {
- self.state.is_closed.load(Relaxed)
+ self.is_closed.get()
}
pub fn set_title(&self, title: &str) {
@@ -319,7 +340,27 @@ impl Window {
NSDefaultRunLoopMode,
true);
if event == nil { break; }
- NSApp().sendEvent_(event);
+ {
+ // Create a temporary structure with state that delegates called internally
+ // by sendEvent can read and modify. When that returns, update window state.
+ // This allows the synchronous resize loop to continue issuing callbacks
+ // to the user application, by passing handler through to the delegate state.
+ let mut ds = DelegateState {
+ is_closed: self.is_closed.get(),
+ context: self.context,
+ view: self.view,
+ window: self,
+ handler: self.resize,
+ };
+ object_setInstanceVariable(self.delegate,
+ DELEGATE_STATE_IVAR.as_ptr() as *const i8,
+ &mut ds as *mut DelegateState as *mut libc::c_void);
+ NSApp().sendEvent_(event);
+ object_setInstanceVariable(self.delegate,
+ DELEGATE_STATE_IVAR.as_ptr() as *const i8,
+ ptr::null_mut());
+ self.is_closed.set(ds.is_closed);
+}
match event.get_type() {
NSLeftMouseDown => { events.push(MouseInput(Pressed, LeftMouseButton)); },
@@ -434,4 +475,8 @@ impl Window {
pub fn get_api(&self) -> ::Api {
::Api::OpenGl
}
+
+ pub fn set_window_resize_callback(&mut self, callback: fn(uint, uint)) {
+ self.resize = callback;
+ }
}