diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/cocoa/mod.rs | 52 | ||||
-rw-r--r-- | src/x11/window/mod.rs | 42 |
2 files changed, 48 insertions, 46 deletions
diff --git a/src/cocoa/mod.rs b/src/cocoa/mod.rs index 94ac5b3..bbdb8ea 100644 --- a/src/cocoa/mod.rs +++ b/src/cocoa/mod.rs @@ -12,6 +12,7 @@ use cocoa::base::{selector, msg_send, msg_send_stret, class_addMethod, class_add use cocoa::base::{object_setInstanceVariable, object_getInstanceVariable}; use cocoa::appkit; use cocoa::appkit::*; +use cocoa::appkit::NSEventSubtype::*; use core_foundation::base::TCFType; use core_foundation::string::CFString; @@ -89,7 +90,7 @@ impl WindowDelegate { if let Some(handler) = state.handler { let rect = NSView::frame(state.view); - let scale_factor = state.window.backingScaleFactor() as f32; + let scale_factor = NSWindow::backingScaleFactor(state.window) as f32; (handler)((scale_factor * rect.size.width as f32) as u32, (scale_factor * rect.size.height as f32) as u32); } @@ -179,8 +180,9 @@ impl WindowProxy { unsafe { let pool = NSAutoreleasePool::new(nil); let event = - NSEvent::otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2( - nil, NSApplicationDefined, NSPoint::new(0.0, 0.0), 0, 0.0, 0, ptr::null_mut(), 0, 0, 0); + 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); NSApp().postEvent_atStart_(event, YES); pool.drain(); } @@ -364,10 +366,7 @@ impl Window { pub fn get_position(&self) -> Option<(i32, i32)> { unsafe { - // let content_rect = NSWindow::contentRectForFrameRect_(self.window, NSWindow::frame(self.window)); - let content_rect: NSRect = msg_send_stret()(self.window, - selector("contentRectForFrameRect:"), - NSWindow::frame(self.window)); + let content_rect = NSWindow::contentRectForFrameRect_(self.window, NSWindow::frame(self.window)); // NOTE: coordinate system might be inconsistent with other backends Some((content_rect.origin.x as i32, content_rect.origin.y as i32)) } @@ -410,7 +409,7 @@ impl Window { loop { unsafe { let event = NSApp().nextEventMatchingMask_untilDate_inMode_dequeue_( - NSAnyEventMask as u64, + NSAnyEventMask.bits(), NSDate::distantPast(nil), NSDefaultRunLoopMode, YES); @@ -433,7 +432,7 @@ impl Window { self.is_closed.set(ds.is_closed); } - match event.get_type() { + match msg_send()(event, selector("type")) { NSLeftMouseDown => { events.push_back(MouseInput(Pressed, LeftMouseButton)); }, NSLeftMouseUp => { events.push_back(MouseInput(Released, LeftMouseButton)); }, NSRightMouseDown => { events.push_back(MouseInput(Pressed, RightMouseButton)); }, @@ -461,30 +460,30 @@ impl Window { } } - let vkey = event::vkeycode_to_element(event.keycode()); - events.push_back(KeyboardInput(Pressed, event.keycode() as u8, vkey)); + let vkey = event::vkeycode_to_element(NSEvent::keyCode(event)); + events.push_back(KeyboardInput(Pressed, NSEvent::keyCode(event) as u8, vkey)); }, NSKeyUp => { - let vkey = event::vkeycode_to_element(event.keycode()); - events.push_back(KeyboardInput(Released, event.keycode() as u8, vkey)); + let vkey = event::vkeycode_to_element(NSEvent::keyCode(event)); + events.push_back(KeyboardInput(Released, NSEvent::keyCode(event) as u8, vkey)); }, NSFlagsChanged => { - let shift_modifier = Window::modifier_event(event, appkit::NSShiftKeyMask as u64, events::VirtualKeyCode::LShift, shift_pressed); + 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 as u64, events::VirtualKeyCode::LControl, ctrl_pressed); + 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 as u64, events::VirtualKeyCode::LWin, win_pressed); + 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 as u64, events::VirtualKeyCode::LAlt, alt_pressed); + 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()); @@ -501,25 +500,20 @@ impl Window { events } - unsafe fn modifier_event(event: id, keymask: u64, key: events::VirtualKeyCode, key_pressed: bool) -> Option<Event> { - if !key_pressed && Window::modifier_key_pressed(event, keymask) { - return Some(KeyboardInput(Pressed, event.keycode() as u8, Some(key))); - } - else if key_pressed && !Window::modifier_key_pressed(event, keymask) { - return Some(KeyboardInput(Released, event.keycode() as u8, Some(key))); + 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))); } return None; } - unsafe fn modifier_key_pressed(event: id, modifier: u64) -> bool { - event.modifierFlags() & modifier != 0 - } - pub fn wait_events(&self) -> RingBuf<Event> { unsafe { let event = NSApp().nextEventMatchingMask_untilDate_inMode_dequeue_( - NSAnyEventMask as u64, + NSAnyEventMask.bits(), NSDate::distantFuture(nil), NSDefaultRunLoopMode, NO); @@ -568,7 +562,7 @@ impl Window { pub fn hidpi_factor(&self) -> f32 { unsafe { - self.window.backingScaleFactor() as f32 + NSWindow::backingScaleFactor(self.window) as f32 } } } diff --git a/src/x11/window/mod.rs b/src/x11/window/mod.rs index efdc759..d412ce0 100644 --- a/src/x11/window/mod.rs +++ b/src/x11/window/mod.rs @@ -7,7 +7,7 @@ use std::cell::Cell; use std::sync::atomic::AtomicBool; use std::collections::RingBuf; use super::ffi; -use std::sync::{Arc, Once, ONCE_INIT}; +use std::sync::{Arc, Once, ONCE_INIT, Weak}; use std::sync::{StaticMutex, MUTEX_INIT}; pub use self::monitor::{MonitorID, get_available_monitors, get_primary_monitor}; @@ -36,7 +36,7 @@ fn ensure_thread_init() { fn with_c_str<F, T>(s: &str, f: F) -> T where F: FnOnce(*const libc::c_char) -> T { use std::ffi::CString; let c_str = CString::from_slice(s.as_bytes()); - f(c_str.as_slice_with_nul().as_ptr()) + f(c_str.as_slice_with_nul().as_ptr()) } struct XWindow { @@ -50,6 +50,9 @@ struct XWindow { im: ffi::XIM, } +unsafe impl Send for XWindow {} +unsafe impl Sync for XWindow {} + unsafe impl Send for Window {} unsafe impl Sync for Window {} @@ -74,25 +77,30 @@ impl Drop for XWindow { #[derive(Clone)] pub struct WindowProxy { - x: Arc<XWindow>, + x: Weak<XWindow>, } impl WindowProxy { pub fn wakeup_event_loop(&self) { - let mut xev = ffi::XClientMessageEvent { - type_: ffi::ClientMessage, - window: self.x.window, - format: 32, - message_type: 0, - serial: 0, - send_event: 0, - display: self.x.display, - l: [0, 0, 0, 0, 0], - }; + match self.x.upgrade() { + Some(x) => { + let mut xev = ffi::XClientMessageEvent { + type_: ffi::ClientMessage, + window: x.window, + format: 32, + message_type: 0, + serial: 0, + send_event: 0, + display: x.display, + l: [0, 0, 0, 0, 0], + }; - unsafe { - ffi::XSendEvent(self.x.display, self.x.window, 0, 0, mem::transmute(&mut xev)); - ffi::XFlush(self.x.display); + unsafe { + ffi::XSendEvent(x.display, x.window, 0, 0, mem::transmute(&mut xev)); + ffi::XFlush(x.display); + } + } + None => {} } } } @@ -488,7 +496,7 @@ impl Window { pub fn create_window_proxy(&self) -> WindowProxy { WindowProxy { - x: self.x.clone() + x: self.x.downgrade() } } |