2019-02-06 02:30:33 +11:00
|
|
|
use {ControlFlow, EventLoopClosed};
|
2017-02-03 23:05:57 +11:00
|
|
|
use cocoa::{self, appkit, foundation};
|
2017-10-31 21:03:18 +11:00
|
|
|
use cocoa::appkit::{NSApplication, NSEvent, NSEventMask, NSEventModifierFlags, NSEventPhase, NSView, NSWindow};
|
2018-06-12 01:16:39 +10:00
|
|
|
use events::{self, ElementState, Event, TouchPhase, WindowEvent, DeviceEvent, ModifiersState, KeyboardInput};
|
2017-06-09 22:13:30 +10:00
|
|
|
use std::collections::VecDeque;
|
|
|
|
use std::sync::{Arc, Mutex, Weak};
|
2017-09-07 01:32:24 +10:00
|
|
|
use super::window::Window2;
|
2017-02-03 23:05:57 +11:00
|
|
|
use std;
|
2018-05-18 11:28:30 +10:00
|
|
|
use std::os::raw::*;
|
2017-04-23 06:52:35 +10:00
|
|
|
use super::DeviceId;
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2019-02-06 02:30:33 +11:00
|
|
|
pub struct EventLoop {
|
2017-06-09 22:13:30 +10:00
|
|
|
modifiers: Modifiers,
|
|
|
|
pub shared: Arc<Shared>,
|
|
|
|
}
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2019-02-06 02:30:33 +11:00
|
|
|
// State shared between the `EventLoop` and its registered windows.
|
2017-06-09 22:13:30 +10:00
|
|
|
pub struct Shared {
|
2017-09-07 01:32:24 +10:00
|
|
|
pub windows: Mutex<Vec<Weak<Window2>>>,
|
2017-06-09 22:13:30 +10:00
|
|
|
pub pending_events: Mutex<VecDeque<Event>>,
|
2017-02-05 12:51:09 +11:00
|
|
|
// The user event callback given via either of the `poll_events` or `run_forever` methods.
|
|
|
|
//
|
|
|
|
// We store the user's callback here so that it may be accessed by each of the window delegate
|
|
|
|
// callbacks (e.g. resize, close, etc) for the duration of a call to either of the
|
|
|
|
// `poll_events` or `run_forever` methods.
|
|
|
|
//
|
|
|
|
// This is *only* `Some` for the duration of a call to either of these methods and will be
|
|
|
|
// `None` otherwise.
|
2017-03-19 19:09:20 +11:00
|
|
|
user_callback: UserCallback,
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
2017-10-26 05:03:57 +11:00
|
|
|
#[derive(Clone)]
|
2017-05-31 15:00:49 +10:00
|
|
|
pub struct Proxy {}
|
|
|
|
|
2017-02-03 23:05:57 +11:00
|
|
|
struct Modifiers {
|
|
|
|
shift_pressed: bool,
|
|
|
|
ctrl_pressed: bool,
|
|
|
|
win_pressed: bool,
|
|
|
|
alt_pressed: bool,
|
|
|
|
}
|
|
|
|
|
2017-02-05 12:51:09 +11:00
|
|
|
// Wrapping the user callback in a type allows us to:
|
|
|
|
//
|
|
|
|
// - ensure the callback pointer is never accidentally cloned
|
2019-02-06 02:30:33 +11:00
|
|
|
// - ensure that only the `EventLoop` can `store` and `drop` the callback pointer
|
2017-06-09 22:13:30 +10:00
|
|
|
// - Share access to the user callback with the NSWindow callbacks.
|
2017-02-05 12:51:09 +11:00
|
|
|
pub struct UserCallback {
|
2017-06-09 22:13:30 +10:00
|
|
|
mutex: Mutex<Option<*mut FnMut(Event)>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Shared {
|
|
|
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Shared {
|
|
|
|
windows: Mutex::new(Vec::new()),
|
|
|
|
pending_events: Mutex::new(VecDeque::new()),
|
|
|
|
user_callback: UserCallback { mutex: Mutex::new(None) },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call_user_callback_with_pending_events(&self) {
|
|
|
|
loop {
|
|
|
|
let event = match self.pending_events.lock().unwrap().pop_front() {
|
|
|
|
Some(event) => event,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
self.user_callback.call_with_event(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calls the user callback if one exists.
|
|
|
|
//
|
|
|
|
// Otherwise, stores the event in the `pending_events` queue.
|
|
|
|
//
|
|
|
|
// This is necessary for the case when `WindowDelegate` callbacks are triggered during a call
|
|
|
|
// to the user's callback.
|
|
|
|
pub fn call_user_callback_with_event_or_store_in_pending(&self, event: Event) {
|
|
|
|
if self.user_callback.mutex.lock().unwrap().is_some() {
|
|
|
|
unsafe {
|
|
|
|
self.user_callback.call_with_event(event);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.pending_events.lock().unwrap().push_back(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removes the window with the given `Id` from the `windows` list.
|
|
|
|
//
|
2018-04-25 06:20:40 +10:00
|
|
|
// This is called in response to `windowWillClose`.
|
2017-06-09 22:13:30 +10:00
|
|
|
pub fn find_and_remove_window(&self, id: super::window::Id) {
|
|
|
|
if let Ok(mut windows) = self.windows.lock() {
|
|
|
|
windows.retain(|w| match w.upgrade() {
|
|
|
|
Some(w) => w.id() != id,
|
2018-04-29 02:10:06 +10:00
|
|
|
None => false,
|
2017-06-09 22:13:30 +10:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Modifiers {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Modifiers {
|
|
|
|
shift_pressed: false,
|
|
|
|
ctrl_pressed: false,
|
|
|
|
win_pressed: false,
|
|
|
|
alt_pressed: false,
|
|
|
|
}
|
|
|
|
}
|
2017-02-05 12:51:09 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl UserCallback {
|
|
|
|
|
|
|
|
// Here we store user's `callback` behind the mutex so that they may be safely shared between
|
|
|
|
// each of the window delegates.
|
|
|
|
//
|
|
|
|
// In order to make sure that the pointer is always valid, we must manually guarantee that it
|
|
|
|
// is dropped before the callback itself is dropped. Thus, this should *only* be called at the
|
|
|
|
// beginning of a call to `poll_events` and `run_forever`, both of which *must* drop the
|
2017-02-05 19:37:00 +11:00
|
|
|
// callback at the end of their scope using the `drop` method.
|
2017-02-05 12:51:09 +11:00
|
|
|
fn store<F>(&self, callback: &mut F)
|
|
|
|
where F: FnMut(Event)
|
|
|
|
{
|
|
|
|
let trait_object = callback as &mut FnMut(Event);
|
|
|
|
let trait_object_ptr = trait_object as *const FnMut(Event) as *mut FnMut(Event);
|
|
|
|
*self.mutex.lock().unwrap() = Some(trait_object_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emits the given event via the user-given callback.
|
|
|
|
//
|
|
|
|
// This is unsafe as it requires dereferencing the pointer to the user-given callback. We
|
|
|
|
// guarantee this is safe by ensuring the `UserCallback` never lives longer than the user-given
|
|
|
|
// callback.
|
2017-03-19 19:09:20 +11:00
|
|
|
//
|
|
|
|
// Note that the callback may not always be `Some`. This is because some `NSWindowDelegate`
|
|
|
|
// callbacks can be triggered by means other than `NSApp().sendEvent`. For example, if a window
|
|
|
|
// is destroyed or created during a call to the user's callback, the `WindowDelegate` methods
|
|
|
|
// may be called with `windowShouldClose` or `windowDidResignKey`.
|
|
|
|
unsafe fn call_with_event(&self, event: Event) {
|
|
|
|
let callback = match self.mutex.lock().unwrap().take() {
|
|
|
|
Some(callback) => callback,
|
|
|
|
None => return,
|
|
|
|
};
|
2017-02-05 12:51:09 +11:00
|
|
|
(*callback)(event);
|
|
|
|
*self.mutex.lock().unwrap() = Some(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Used to drop the user callback pointer at the end of the `poll_events` and `run_forever`
|
|
|
|
// methods. This is done to enforce our guarantee that the top callback will never live longer
|
|
|
|
// than the call to either `poll_events` or `run_forever` to which it was given.
|
|
|
|
fn drop(&self) {
|
|
|
|
self.mutex.lock().unwrap().take();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2019-02-06 02:30:33 +11:00
|
|
|
impl EventLoop {
|
2017-02-03 23:05:57 +11:00
|
|
|
|
|
|
|
pub fn new() -> Self {
|
2018-04-17 08:09:02 +10:00
|
|
|
// Mark this thread as the main thread of the Cocoa event system.
|
|
|
|
//
|
|
|
|
// This must be done before any worker threads get a chance to call it
|
2019-02-06 02:30:33 +11:00
|
|
|
// (e.g., via `EventLoopProxy::wakeup()`), causing a wrong thread to be
|
2018-04-17 08:09:02 +10:00
|
|
|
// marked as the main thread.
|
|
|
|
unsafe { appkit::NSApp(); }
|
|
|
|
|
2019-02-06 02:30:33 +11:00
|
|
|
EventLoop {
|
2017-06-09 22:13:30 +10:00
|
|
|
shared: Arc::new(Shared::new()),
|
|
|
|
modifiers: Modifiers::new(),
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-09 19:40:22 +10:00
|
|
|
pub fn poll_events<F>(&mut self, mut callback: F)
|
2017-02-03 23:05:57 +11:00
|
|
|
where F: FnMut(Event),
|
|
|
|
{
|
|
|
|
unsafe {
|
2018-07-20 02:02:33 +10:00
|
|
|
if !msg_send![class!(NSThread), isMainThread] {
|
2017-02-04 02:55:42 +11:00
|
|
|
panic!("Events can only be polled from the main thread on macOS");
|
|
|
|
}
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
2017-06-09 22:13:30 +10:00
|
|
|
self.shared.user_callback.store(&mut callback);
|
2017-02-05 12:51:09 +11:00
|
|
|
|
2017-02-03 23:05:57 +11:00
|
|
|
// Loop as long as we have pending events to return.
|
|
|
|
loop {
|
|
|
|
unsafe {
|
2017-02-05 12:51:09 +11:00
|
|
|
// First, yield all pending events.
|
2017-06-09 22:13:30 +10:00
|
|
|
self.shared.call_user_callback_with_pending_events();
|
2017-02-05 12:51:09 +11:00
|
|
|
|
2017-02-03 23:05:57 +11:00
|
|
|
let pool = foundation::NSAutoreleasePool::new(cocoa::base::nil);
|
|
|
|
|
|
|
|
// Poll for the next event, returning `nil` if there are none.
|
|
|
|
let ns_event = appkit::NSApp().nextEventMatchingMask_untilDate_inMode_dequeue_(
|
2017-10-31 21:03:18 +11:00
|
|
|
NSEventMask::NSAnyEventMask.bits() | NSEventMask::NSEventMaskPressure.bits(),
|
2017-02-03 23:05:57 +11:00
|
|
|
foundation::NSDate::distantPast(cocoa::base::nil),
|
|
|
|
foundation::NSDefaultRunLoopMode,
|
|
|
|
cocoa::base::YES);
|
|
|
|
|
|
|
|
let event = self.ns_event_to_event(ns_event);
|
|
|
|
|
|
|
|
let _: () = msg_send![pool, release];
|
|
|
|
|
|
|
|
match event {
|
|
|
|
// Call the user's callback.
|
2018-04-18 10:48:57 +10:00
|
|
|
Some(event) => self.shared.user_callback.call_with_event(event),
|
2017-02-03 23:05:57 +11:00
|
|
|
None => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-09 22:13:30 +10:00
|
|
|
self.shared.user_callback.drop();
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
2017-06-09 19:40:22 +10:00
|
|
|
pub fn run_forever<F>(&mut self, mut callback: F)
|
|
|
|
where F: FnMut(Event) -> ControlFlow
|
2017-02-03 23:05:57 +11:00
|
|
|
{
|
|
|
|
unsafe {
|
2018-07-20 02:02:33 +10:00
|
|
|
if !msg_send![class!(NSThread), isMainThread] {
|
2017-02-04 02:55:42 +11:00
|
|
|
panic!("Events can only be polled from the main thread on macOS");
|
|
|
|
}
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
2017-06-09 19:40:22 +10:00
|
|
|
// Track whether or not control flow has changed.
|
2017-06-09 22:13:30 +10:00
|
|
|
let control_flow = std::cell::Cell::new(ControlFlow::Continue);
|
2017-06-09 19:40:22 +10:00
|
|
|
|
|
|
|
let mut callback = |event| {
|
2017-06-20 21:25:53 +10:00
|
|
|
if let ControlFlow::Break = callback(event) {
|
|
|
|
control_flow.set(ControlFlow::Break);
|
2017-06-09 19:40:22 +10:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-09 22:13:30 +10:00
|
|
|
self.shared.user_callback.store(&mut callback);
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2017-02-05 12:51:09 +11:00
|
|
|
loop {
|
2017-02-03 23:05:57 +11:00
|
|
|
unsafe {
|
2017-02-05 12:51:09 +11:00
|
|
|
// First, yield all pending events.
|
2017-06-09 22:13:30 +10:00
|
|
|
self.shared.call_user_callback_with_pending_events();
|
2017-06-20 21:25:53 +10:00
|
|
|
if let ControlFlow::Break = control_flow.get() {
|
2017-06-09 19:40:22 +10:00
|
|
|
break;
|
|
|
|
}
|
2017-02-05 12:51:09 +11:00
|
|
|
|
2017-02-03 23:05:57 +11:00
|
|
|
let pool = foundation::NSAutoreleasePool::new(cocoa::base::nil);
|
|
|
|
|
|
|
|
// Wait for the next event. Note that this function blocks during resize.
|
|
|
|
let ns_event = appkit::NSApp().nextEventMatchingMask_untilDate_inMode_dequeue_(
|
2017-10-31 21:03:18 +11:00
|
|
|
NSEventMask::NSAnyEventMask.bits() | NSEventMask::NSEventMaskPressure.bits(),
|
2017-02-03 23:05:57 +11:00
|
|
|
foundation::NSDate::distantFuture(cocoa::base::nil),
|
|
|
|
foundation::NSDefaultRunLoopMode,
|
|
|
|
cocoa::base::YES);
|
|
|
|
|
2017-02-05 12:51:09 +11:00
|
|
|
let maybe_event = self.ns_event_to_event(ns_event);
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2017-02-05 12:51:09 +11:00
|
|
|
// Release the pool before calling the top callback in case the user calls either
|
|
|
|
// `run_forever` or `poll_events` within the callback.
|
2017-02-03 23:05:57 +11:00
|
|
|
let _: () = msg_send![pool, release];
|
2017-02-05 12:51:09 +11:00
|
|
|
|
|
|
|
if let Some(event) = maybe_event {
|
2018-04-18 10:48:57 +10:00
|
|
|
self.shared.user_callback.call_with_event(event);
|
2017-06-20 21:25:53 +10:00
|
|
|
if let ControlFlow::Break = control_flow.get() {
|
2017-06-09 19:40:22 +10:00
|
|
|
break;
|
|
|
|
}
|
2017-02-05 12:51:09 +11:00
|
|
|
}
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-09 22:13:30 +10:00
|
|
|
self.shared.user_callback.drop();
|
2017-03-19 19:09:20 +11:00
|
|
|
}
|
|
|
|
|
2017-02-03 23:05:57 +11:00
|
|
|
// Convert some given `NSEvent` into a winit `Event`.
|
2017-06-09 22:13:30 +10:00
|
|
|
unsafe fn ns_event_to_event(&mut self, ns_event: cocoa::base::id) -> Option<Event> {
|
2017-02-03 23:05:57 +11:00
|
|
|
if ns_event == cocoa::base::nil {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2017-02-05 19:01:27 +11:00
|
|
|
// FIXME: Despite not being documented anywhere, an `NSEvent` is produced when a user opens
|
|
|
|
// Spotlight while the NSApplication is in focus. This `NSEvent` produces a `NSEventType`
|
|
|
|
// with value `21`. This causes a SEGFAULT as soon as we try to match on the `NSEventType`
|
2017-02-05 19:21:51 +11:00
|
|
|
// enum as there is no variant associated with the value. Thus, we return early if this
|
|
|
|
// sneaky event occurs. If someone does find some documentation on this, please fix this by
|
|
|
|
// adding an appropriate variant to the `NSEventType` enum in the cocoa-rs crate.
|
2017-02-05 19:01:27 +11:00
|
|
|
if ns_event.eventType() as u64 == 21 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2017-02-03 23:05:57 +11:00
|
|
|
let event_type = ns_event.eventType();
|
2017-02-05 18:12:32 +11:00
|
|
|
let ns_window = ns_event.window();
|
|
|
|
let window_id = super::window::get_window_id(ns_window);
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2017-02-05 18:12:32 +11:00
|
|
|
// FIXME: Document this. Why do we do this? Seems like it passes on events to window/app.
|
|
|
|
// If we don't do this, window does not become main for some reason.
|
2018-05-13 12:10:57 +10:00
|
|
|
appkit::NSApp().sendEvent_(ns_event);
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2017-06-09 22:13:30 +10:00
|
|
|
let windows = self.shared.windows.lock().unwrap();
|
2017-03-19 18:19:24 +11:00
|
|
|
let maybe_window = windows.iter()
|
2017-06-09 22:13:30 +10:00
|
|
|
.filter_map(Weak::upgrade)
|
2017-03-19 18:19:24 +11:00
|
|
|
.find(|window| window_id == window.id());
|
|
|
|
|
2017-02-03 23:05:57 +11:00
|
|
|
let into_event = |window_event| Event::WindowEvent {
|
|
|
|
window_id: ::WindowId(window_id),
|
|
|
|
event: window_event,
|
|
|
|
};
|
|
|
|
|
2017-02-05 18:12:32 +11:00
|
|
|
// Returns `Some` window if one of our windows is the key window.
|
2017-03-19 18:19:24 +11:00
|
|
|
let maybe_key_window = || windows.iter()
|
2017-06-09 22:13:30 +10:00
|
|
|
.filter_map(Weak::upgrade)
|
2017-03-19 18:19:24 +11:00
|
|
|
.find(|window| {
|
|
|
|
let is_key_window: cocoa::base::BOOL = msg_send![*window.window, isKeyWindow];
|
|
|
|
is_key_window == cocoa::base::YES
|
|
|
|
});
|
2017-02-05 18:12:32 +11:00
|
|
|
|
2017-02-03 23:05:57 +11:00
|
|
|
match event_type {
|
2018-08-16 09:42:57 +10:00
|
|
|
// https://github.com/glfw/glfw/blob/50eccd298a2bbc272b4977bd162d3e4b55f15394/src/cocoa_window.m#L881
|
|
|
|
appkit::NSKeyUp => {
|
|
|
|
if let Some(key_window) = maybe_key_window() {
|
|
|
|
if event_mods(ns_event).logo {
|
|
|
|
let _: () = msg_send![*key_window.window, sendEvent:ns_event];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
},
|
|
|
|
// similar to above, but for `<Cmd-.>`, the keyDown is suppressed instead of the
|
|
|
|
// KeyUp, and the above trick does not appear to work.
|
|
|
|
appkit::NSKeyDown => {
|
|
|
|
let modifiers = event_mods(ns_event);
|
|
|
|
let keycode = NSEvent::keyCode(ns_event);
|
|
|
|
if modifiers.logo && keycode == 47 {
|
|
|
|
modifier_event(ns_event, NSEventModifierFlags::NSCommandKeyMask, false)
|
|
|
|
.map(into_event)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
},
|
2017-02-03 23:05:57 +11:00
|
|
|
appkit::NSFlagsChanged => {
|
|
|
|
let mut events = std::collections::VecDeque::new();
|
2018-05-18 11:28:30 +10:00
|
|
|
|
|
|
|
if let Some(window_event) = modifier_event(
|
|
|
|
ns_event,
|
|
|
|
NSEventModifierFlags::NSShiftKeyMask,
|
|
|
|
self.modifiers.shift_pressed,
|
|
|
|
) {
|
2017-06-09 22:13:30 +10:00
|
|
|
self.modifiers.shift_pressed = !self.modifiers.shift_pressed;
|
2017-02-03 23:05:57 +11:00
|
|
|
events.push_back(into_event(window_event));
|
|
|
|
}
|
|
|
|
|
2018-05-18 11:28:30 +10:00
|
|
|
if let Some(window_event) = modifier_event(
|
|
|
|
ns_event,
|
|
|
|
NSEventModifierFlags::NSControlKeyMask,
|
|
|
|
self.modifiers.ctrl_pressed,
|
|
|
|
) {
|
2017-06-09 22:13:30 +10:00
|
|
|
self.modifiers.ctrl_pressed = !self.modifiers.ctrl_pressed;
|
2017-02-03 23:05:57 +11:00
|
|
|
events.push_back(into_event(window_event));
|
|
|
|
}
|
|
|
|
|
2018-05-18 11:28:30 +10:00
|
|
|
if let Some(window_event) = modifier_event(
|
|
|
|
ns_event,
|
|
|
|
NSEventModifierFlags::NSCommandKeyMask,
|
|
|
|
self.modifiers.win_pressed,
|
|
|
|
) {
|
2017-06-09 22:13:30 +10:00
|
|
|
self.modifiers.win_pressed = !self.modifiers.win_pressed;
|
2017-02-03 23:05:57 +11:00
|
|
|
events.push_back(into_event(window_event));
|
|
|
|
}
|
|
|
|
|
2018-05-18 11:28:30 +10:00
|
|
|
if let Some(window_event) = modifier_event(
|
|
|
|
ns_event,
|
|
|
|
NSEventModifierFlags::NSAlternateKeyMask,
|
|
|
|
self.modifiers.alt_pressed,
|
|
|
|
) {
|
2017-06-09 22:13:30 +10:00
|
|
|
self.modifiers.alt_pressed = !self.modifiers.alt_pressed;
|
2017-02-03 23:05:57 +11:00
|
|
|
events.push_back(into_event(window_event));
|
|
|
|
}
|
|
|
|
|
|
|
|
let event = events.pop_front();
|
2018-05-18 11:28:30 +10:00
|
|
|
self.shared.pending_events
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.extend(events.into_iter());
|
2017-02-03 23:05:57 +11:00
|
|
|
event
|
|
|
|
},
|
|
|
|
|
2017-11-27 07:43:13 +11:00
|
|
|
appkit::NSMouseEntered => {
|
|
|
|
let window = match maybe_window.or_else(maybe_key_window) {
|
|
|
|
Some(window) => window,
|
|
|
|
None => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let window_point = ns_event.locationInWindow();
|
|
|
|
let view_point = if ns_window == cocoa::base::nil {
|
|
|
|
let ns_size = foundation::NSSize::new(0.0, 0.0);
|
|
|
|
let ns_rect = foundation::NSRect::new(window_point, ns_size);
|
|
|
|
let window_rect = window.window.convertRectFromScreen_(ns_rect);
|
|
|
|
window.view.convertPoint_fromView_(window_rect.origin, cocoa::base::nil)
|
|
|
|
} else {
|
|
|
|
window.view.convertPoint_fromView_(window_point, cocoa::base::nil)
|
|
|
|
};
|
|
|
|
|
2018-06-15 09:42:18 +10:00
|
|
|
let view_rect = NSView::frame(*window.view);
|
|
|
|
let x = view_point.x as f64;
|
|
|
|
let y = (view_rect.size.height - view_point.y) as f64;
|
|
|
|
let window_event = WindowEvent::CursorMoved {
|
|
|
|
device_id: DEVICE_ID,
|
|
|
|
position: (x, y).into(),
|
|
|
|
modifiers: event_mods(ns_event),
|
|
|
|
};
|
2017-11-27 07:43:13 +11:00
|
|
|
let event = Event::WindowEvent { window_id: ::WindowId(window.id()), event: window_event };
|
|
|
|
self.shared.pending_events.lock().unwrap().push_back(event);
|
|
|
|
Some(into_event(WindowEvent::CursorEntered { device_id: DEVICE_ID }))
|
|
|
|
},
|
2017-11-13 07:56:57 +11:00
|
|
|
appkit::NSMouseExited => { Some(into_event(WindowEvent::CursorLeft { device_id: DEVICE_ID })) },
|
2017-02-05 18:12:32 +11:00
|
|
|
|
2017-02-03 23:05:57 +11:00
|
|
|
appkit::NSMouseMoved |
|
|
|
|
appkit::NSLeftMouseDragged |
|
|
|
|
appkit::NSOtherMouseDragged |
|
|
|
|
appkit::NSRightMouseDragged => {
|
2017-02-05 18:12:32 +11:00
|
|
|
// If the mouse movement was on one of our windows, use it.
|
|
|
|
// Otherwise, if one of our windows is the key window (receiving input), use it.
|
|
|
|
// Otherwise, return `None`.
|
2018-06-18 05:08:26 +10:00
|
|
|
match maybe_window.or_else(maybe_key_window) {
|
|
|
|
Some(_window) => (),
|
2017-02-05 18:12:32 +11:00
|
|
|
None => return None,
|
2018-06-18 05:08:26 +10:00
|
|
|
}
|
2017-02-05 18:12:32 +11:00
|
|
|
|
2018-06-12 01:16:39 +10:00
|
|
|
let mut events = std::collections::VecDeque::with_capacity(3);
|
2017-07-16 11:32:18 +10:00
|
|
|
|
2018-06-15 09:42:18 +10:00
|
|
|
let delta_x = ns_event.deltaX() as f64;
|
2017-07-16 11:32:18 +10:00
|
|
|
if delta_x != 0.0 {
|
2017-07-30 00:56:08 +10:00
|
|
|
let motion_event = DeviceEvent::Motion { axis: 0, value: delta_x };
|
2018-06-15 09:42:18 +10:00
|
|
|
let event = Event::DeviceEvent { device_id: DEVICE_ID, event: motion_event };
|
2017-07-16 11:32:18 +10:00
|
|
|
events.push_back(event);
|
|
|
|
}
|
|
|
|
|
2018-06-15 09:42:18 +10:00
|
|
|
let delta_y = ns_event.deltaY() as f64;
|
2017-07-16 11:32:18 +10:00
|
|
|
if delta_y != 0.0 {
|
2017-07-30 00:56:08 +10:00
|
|
|
let motion_event = DeviceEvent::Motion { axis: 1, value: delta_y };
|
2018-06-15 09:42:18 +10:00
|
|
|
let event = Event::DeviceEvent { device_id: DEVICE_ID, event: motion_event };
|
2017-07-16 11:32:18 +10:00
|
|
|
events.push_back(event);
|
|
|
|
}
|
|
|
|
|
2017-11-13 07:56:57 +11:00
|
|
|
if delta_x != 0.0 || delta_y != 0.0 {
|
|
|
|
let motion_event = DeviceEvent::MouseMotion { delta: (delta_x, delta_y) };
|
2018-06-15 09:42:18 +10:00
|
|
|
let event = Event::DeviceEvent { device_id: DEVICE_ID, event: motion_event };
|
2017-11-13 07:56:57 +11:00
|
|
|
events.push_back(event);
|
|
|
|
}
|
|
|
|
|
2017-07-16 11:32:18 +10:00
|
|
|
let event = events.pop_front();
|
|
|
|
self.shared.pending_events.lock().unwrap().extend(events.into_iter());
|
|
|
|
event
|
2017-02-03 23:05:57 +11:00
|
|
|
},
|
|
|
|
|
|
|
|
appkit::NSScrollWheel => {
|
2017-02-05 18:12:32 +11:00
|
|
|
// If none of the windows received the scroll, return `None`.
|
2018-06-15 09:42:18 +10:00
|
|
|
if maybe_window.is_none() {
|
|
|
|
return None;
|
|
|
|
}
|
2017-02-05 18:12:32 +11:00
|
|
|
|
2017-02-03 23:05:57 +11:00
|
|
|
use events::MouseScrollDelta::{LineDelta, PixelDelta};
|
|
|
|
let delta = if ns_event.hasPreciseScrollingDeltas() == cocoa::base::YES {
|
2018-06-15 09:42:18 +10:00
|
|
|
PixelDelta((
|
|
|
|
ns_event.scrollingDeltaX() as f64,
|
|
|
|
ns_event.scrollingDeltaY() as f64,
|
|
|
|
).into())
|
2017-02-03 23:05:57 +11:00
|
|
|
} else {
|
2018-06-15 09:42:18 +10:00
|
|
|
// TODO: This is probably wrong
|
|
|
|
LineDelta(
|
|
|
|
ns_event.scrollingDeltaX() as f32,
|
|
|
|
ns_event.scrollingDeltaY() as f32,
|
|
|
|
)
|
2017-02-03 23:05:57 +11:00
|
|
|
};
|
|
|
|
let phase = match ns_event.phase() {
|
2017-10-31 21:03:18 +11:00
|
|
|
NSEventPhase::NSEventPhaseMayBegin | NSEventPhase::NSEventPhaseBegan => TouchPhase::Started,
|
|
|
|
NSEventPhase::NSEventPhaseEnded => TouchPhase::Ended,
|
2017-02-03 23:05:57 +11:00
|
|
|
_ => TouchPhase::Moved,
|
|
|
|
};
|
2017-11-13 07:56:57 +11:00
|
|
|
self.shared.pending_events.lock().unwrap().push_back(Event::DeviceEvent {
|
|
|
|
device_id: DEVICE_ID,
|
|
|
|
event: DeviceEvent::MouseWheel {
|
|
|
|
delta: if ns_event.hasPreciseScrollingDeltas() == cocoa::base::YES {
|
2018-06-15 09:42:18 +10:00
|
|
|
PixelDelta((
|
|
|
|
ns_event.scrollingDeltaX() as f64,
|
|
|
|
ns_event.scrollingDeltaY() as f64,
|
|
|
|
).into())
|
2017-11-13 07:56:57 +11:00
|
|
|
} else {
|
2018-06-15 09:42:18 +10:00
|
|
|
LineDelta(
|
|
|
|
ns_event.scrollingDeltaX() as f32,
|
|
|
|
ns_event.scrollingDeltaY() as f32,
|
|
|
|
)
|
2017-11-13 07:56:57 +11:00
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
2017-12-27 08:46:28 +11:00
|
|
|
let window_event = WindowEvent::MouseWheel { device_id: DEVICE_ID, delta: delta, phase: phase, modifiers: event_mods(ns_event) };
|
2017-02-03 23:05:57 +11:00
|
|
|
Some(into_event(window_event))
|
|
|
|
},
|
2017-02-04 00:51:38 +11:00
|
|
|
|
2017-02-03 23:05:57 +11:00
|
|
|
appkit::NSEventTypePressure => {
|
|
|
|
let pressure = ns_event.pressure();
|
|
|
|
let stage = ns_event.stage();
|
2017-04-23 06:52:35 +10:00
|
|
|
let window_event = WindowEvent::TouchpadPressure { device_id: DEVICE_ID, pressure: pressure, stage: stage };
|
2017-02-03 23:05:57 +11:00
|
|
|
Some(into_event(window_event))
|
|
|
|
},
|
2017-02-04 00:51:38 +11:00
|
|
|
|
|
|
|
appkit::NSApplicationDefined => match ns_event.subtype() {
|
|
|
|
appkit::NSEventSubtype::NSApplicationActivatedEventType => {
|
2017-05-31 15:00:49 +10:00
|
|
|
Some(Event::Awakened)
|
2017-02-04 00:51:38 +11:00
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
|
2017-02-03 23:05:57 +11:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 15:00:49 +10:00
|
|
|
pub fn create_proxy(&self) -> Proxy {
|
|
|
|
Proxy {}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Proxy {
|
2019-02-06 02:30:33 +11:00
|
|
|
pub fn wakeup(&self) -> Result<(), EventLoopClosed> {
|
2017-05-31 15:00:49 +10:00
|
|
|
// Awaken the event loop by triggering `NSApplicationActivatedEventType`.
|
|
|
|
unsafe {
|
|
|
|
let pool = foundation::NSAutoreleasePool::new(cocoa::base::nil);
|
|
|
|
let event =
|
|
|
|
NSEvent::otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2_(
|
|
|
|
cocoa::base::nil,
|
|
|
|
appkit::NSApplicationDefined,
|
|
|
|
foundation::NSPoint::new(0.0, 0.0),
|
|
|
|
appkit::NSEventModifierFlags::empty(),
|
|
|
|
0.0,
|
|
|
|
0,
|
|
|
|
cocoa::base::nil,
|
|
|
|
appkit::NSEventSubtype::NSApplicationActivatedEventType,
|
|
|
|
0,
|
|
|
|
0);
|
|
|
|
appkit::NSApp().postEvent_atStart_(event, cocoa::base::NO);
|
|
|
|
foundation::NSAutoreleasePool::drain(pool);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
2019-02-24 07:41:55 +11:00
|
|
|
pub fn char_to_keycode(c: char) -> Option<events::VirtualKeyCode> {
|
|
|
|
// We only translate keys that are affected by keyboard layout.
|
|
|
|
//
|
|
|
|
// Note that since keys are translated in a somewhat "dumb" way (reading character)
|
|
|
|
// there is a concern that some combination, i.e. Cmd+char, causes the wrong
|
|
|
|
// letter to be received, and so we receive the wrong key.
|
|
|
|
//
|
|
|
|
// Implementation reference: https://github.com/WebKit/webkit/blob/82bae82cf0f329dbe21059ef0986c4e92fea4ba6/Source/WebCore/platform/cocoa/KeyEventCocoa.mm#L626
|
|
|
|
Some(match c {
|
|
|
|
'a' | 'A' => events::VirtualKeyCode::A,
|
|
|
|
'b' | 'B' => events::VirtualKeyCode::B,
|
|
|
|
'c' | 'C' => events::VirtualKeyCode::C,
|
|
|
|
'd' | 'D' => events::VirtualKeyCode::D,
|
|
|
|
'e' | 'E' => events::VirtualKeyCode::E,
|
|
|
|
'f' | 'F' => events::VirtualKeyCode::F,
|
|
|
|
'g' | 'G' => events::VirtualKeyCode::G,
|
|
|
|
'h' | 'H' => events::VirtualKeyCode::H,
|
|
|
|
'i' | 'I' => events::VirtualKeyCode::I,
|
|
|
|
'j' | 'J' => events::VirtualKeyCode::J,
|
|
|
|
'k' | 'K' => events::VirtualKeyCode::K,
|
|
|
|
'l' | 'L' => events::VirtualKeyCode::L,
|
|
|
|
'm' | 'M' => events::VirtualKeyCode::M,
|
|
|
|
'n' | 'N' => events::VirtualKeyCode::N,
|
|
|
|
'o' | 'O' => events::VirtualKeyCode::O,
|
|
|
|
'p' | 'P' => events::VirtualKeyCode::P,
|
|
|
|
'q' | 'Q' => events::VirtualKeyCode::Q,
|
|
|
|
'r' | 'R' => events::VirtualKeyCode::R,
|
|
|
|
's' | 'S' => events::VirtualKeyCode::S,
|
|
|
|
't' | 'T' => events::VirtualKeyCode::T,
|
|
|
|
'u' | 'U' => events::VirtualKeyCode::U,
|
|
|
|
'v' | 'V' => events::VirtualKeyCode::V,
|
|
|
|
'w' | 'W' => events::VirtualKeyCode::W,
|
|
|
|
'x' | 'X' => events::VirtualKeyCode::X,
|
|
|
|
'y' | 'Y' => events::VirtualKeyCode::Y,
|
|
|
|
'z' | 'Z' => events::VirtualKeyCode::Z,
|
|
|
|
'1' | '!' => events::VirtualKeyCode::Key1,
|
|
|
|
'2' | '@' => events::VirtualKeyCode::Key2,
|
|
|
|
'3' | '#' => events::VirtualKeyCode::Key3,
|
|
|
|
'4' | '$' => events::VirtualKeyCode::Key4,
|
|
|
|
'5' | '%' => events::VirtualKeyCode::Key5,
|
|
|
|
'6' | '^' => events::VirtualKeyCode::Key6,
|
|
|
|
'7' | '&' => events::VirtualKeyCode::Key7,
|
|
|
|
'8' | '*' => events::VirtualKeyCode::Key8,
|
|
|
|
'9' | '(' => events::VirtualKeyCode::Key9,
|
|
|
|
'0' | ')' => events::VirtualKeyCode::Key0,
|
|
|
|
'=' | '+' => events::VirtualKeyCode::Equals,
|
|
|
|
'-' | '_' => events::VirtualKeyCode::Minus,
|
|
|
|
']' | '}' => events::VirtualKeyCode::RBracket,
|
|
|
|
'[' | '{' => events::VirtualKeyCode::LBracket,
|
|
|
|
'\''| '"' => events::VirtualKeyCode::Apostrophe,
|
|
|
|
';' | ':' => events::VirtualKeyCode::Semicolon,
|
|
|
|
'\\'| '|' => events::VirtualKeyCode::Backslash,
|
|
|
|
',' | '<' => events::VirtualKeyCode::Comma,
|
|
|
|
'/' | '?' => events::VirtualKeyCode::Slash,
|
|
|
|
'.' | '>' => events::VirtualKeyCode::Period,
|
|
|
|
'`' | '~' => events::VirtualKeyCode::Grave,
|
|
|
|
_ => return None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn scancode_to_keycode(code: c_ushort) -> Option<events::VirtualKeyCode> {
|
2017-02-03 23:05:57 +11:00
|
|
|
Some(match code {
|
|
|
|
0x00 => events::VirtualKeyCode::A,
|
|
|
|
0x01 => events::VirtualKeyCode::S,
|
|
|
|
0x02 => events::VirtualKeyCode::D,
|
|
|
|
0x03 => events::VirtualKeyCode::F,
|
|
|
|
0x04 => events::VirtualKeyCode::H,
|
|
|
|
0x05 => events::VirtualKeyCode::G,
|
|
|
|
0x06 => events::VirtualKeyCode::Z,
|
|
|
|
0x07 => events::VirtualKeyCode::X,
|
|
|
|
0x08 => events::VirtualKeyCode::C,
|
|
|
|
0x09 => events::VirtualKeyCode::V,
|
|
|
|
//0x0a => World 1,
|
|
|
|
0x0b => events::VirtualKeyCode::B,
|
|
|
|
0x0c => events::VirtualKeyCode::Q,
|
|
|
|
0x0d => events::VirtualKeyCode::W,
|
|
|
|
0x0e => events::VirtualKeyCode::E,
|
|
|
|
0x0f => events::VirtualKeyCode::R,
|
|
|
|
0x10 => events::VirtualKeyCode::Y,
|
|
|
|
0x11 => events::VirtualKeyCode::T,
|
|
|
|
0x12 => events::VirtualKeyCode::Key1,
|
|
|
|
0x13 => events::VirtualKeyCode::Key2,
|
|
|
|
0x14 => events::VirtualKeyCode::Key3,
|
|
|
|
0x15 => events::VirtualKeyCode::Key4,
|
|
|
|
0x16 => events::VirtualKeyCode::Key6,
|
|
|
|
0x17 => events::VirtualKeyCode::Key5,
|
|
|
|
0x18 => events::VirtualKeyCode::Equals,
|
|
|
|
0x19 => events::VirtualKeyCode::Key9,
|
|
|
|
0x1a => events::VirtualKeyCode::Key7,
|
|
|
|
0x1b => events::VirtualKeyCode::Minus,
|
|
|
|
0x1c => events::VirtualKeyCode::Key8,
|
|
|
|
0x1d => events::VirtualKeyCode::Key0,
|
|
|
|
0x1e => events::VirtualKeyCode::RBracket,
|
|
|
|
0x1f => events::VirtualKeyCode::O,
|
|
|
|
0x20 => events::VirtualKeyCode::U,
|
|
|
|
0x21 => events::VirtualKeyCode::LBracket,
|
|
|
|
0x22 => events::VirtualKeyCode::I,
|
|
|
|
0x23 => events::VirtualKeyCode::P,
|
|
|
|
0x24 => events::VirtualKeyCode::Return,
|
|
|
|
0x25 => events::VirtualKeyCode::L,
|
|
|
|
0x26 => events::VirtualKeyCode::J,
|
|
|
|
0x27 => events::VirtualKeyCode::Apostrophe,
|
|
|
|
0x28 => events::VirtualKeyCode::K,
|
|
|
|
0x29 => events::VirtualKeyCode::Semicolon,
|
|
|
|
0x2a => events::VirtualKeyCode::Backslash,
|
|
|
|
0x2b => events::VirtualKeyCode::Comma,
|
|
|
|
0x2c => events::VirtualKeyCode::Slash,
|
|
|
|
0x2d => events::VirtualKeyCode::N,
|
|
|
|
0x2e => events::VirtualKeyCode::M,
|
|
|
|
0x2f => events::VirtualKeyCode::Period,
|
|
|
|
0x30 => events::VirtualKeyCode::Tab,
|
|
|
|
0x31 => events::VirtualKeyCode::Space,
|
|
|
|
0x32 => events::VirtualKeyCode::Grave,
|
|
|
|
0x33 => events::VirtualKeyCode::Back,
|
|
|
|
//0x34 => unkown,
|
|
|
|
0x35 => events::VirtualKeyCode::Escape,
|
2019-03-26 05:05:07 +11:00
|
|
|
0x36 => events::VirtualKeyCode::RWin,
|
|
|
|
0x37 => events::VirtualKeyCode::LWin,
|
2017-02-03 23:05:57 +11:00
|
|
|
0x38 => events::VirtualKeyCode::LShift,
|
|
|
|
//0x39 => Caps lock,
|
2018-06-07 01:30:26 +10:00
|
|
|
0x3a => events::VirtualKeyCode::LAlt,
|
2017-02-03 23:05:57 +11:00
|
|
|
0x3b => events::VirtualKeyCode::LControl,
|
|
|
|
0x3c => events::VirtualKeyCode::RShift,
|
2018-06-07 01:30:26 +10:00
|
|
|
0x3d => events::VirtualKeyCode::RAlt,
|
2017-02-03 23:05:57 +11:00
|
|
|
0x3e => events::VirtualKeyCode::RControl,
|
|
|
|
//0x3f => Fn key,
|
2018-09-13 03:04:16 +10:00
|
|
|
0x40 => events::VirtualKeyCode::F17,
|
2017-02-03 23:05:57 +11:00
|
|
|
0x41 => events::VirtualKeyCode::Decimal,
|
|
|
|
//0x42 -> unkown,
|
|
|
|
0x43 => events::VirtualKeyCode::Multiply,
|
|
|
|
//0x44 => unkown,
|
|
|
|
0x45 => events::VirtualKeyCode::Add,
|
|
|
|
//0x46 => unkown,
|
|
|
|
0x47 => events::VirtualKeyCode::Numlock,
|
|
|
|
//0x48 => KeypadClear,
|
|
|
|
0x49 => events::VirtualKeyCode::VolumeUp,
|
|
|
|
0x4a => events::VirtualKeyCode::VolumeDown,
|
|
|
|
0x4b => events::VirtualKeyCode::Divide,
|
|
|
|
0x4c => events::VirtualKeyCode::NumpadEnter,
|
2019-04-07 15:25:37 +10:00
|
|
|
0x4e => events::VirtualKeyCode::Subtract,
|
2017-02-03 23:05:57 +11:00
|
|
|
//0x4d => unkown,
|
|
|
|
0x4e => events::VirtualKeyCode::Subtract,
|
2018-09-13 03:04:16 +10:00
|
|
|
0x4f => events::VirtualKeyCode::F18,
|
|
|
|
0x50 => events::VirtualKeyCode::F19,
|
2017-02-03 23:05:57 +11:00
|
|
|
0x51 => events::VirtualKeyCode::NumpadEquals,
|
|
|
|
0x52 => events::VirtualKeyCode::Numpad0,
|
|
|
|
0x53 => events::VirtualKeyCode::Numpad1,
|
|
|
|
0x54 => events::VirtualKeyCode::Numpad2,
|
|
|
|
0x55 => events::VirtualKeyCode::Numpad3,
|
|
|
|
0x56 => events::VirtualKeyCode::Numpad4,
|
|
|
|
0x57 => events::VirtualKeyCode::Numpad5,
|
|
|
|
0x58 => events::VirtualKeyCode::Numpad6,
|
|
|
|
0x59 => events::VirtualKeyCode::Numpad7,
|
2018-09-13 03:04:16 +10:00
|
|
|
0x5a => events::VirtualKeyCode::F20,
|
2017-02-03 23:05:57 +11:00
|
|
|
0x5b => events::VirtualKeyCode::Numpad8,
|
|
|
|
0x5c => events::VirtualKeyCode::Numpad9,
|
2018-12-20 05:32:14 +11:00
|
|
|
0x5d => events::VirtualKeyCode::Yen,
|
|
|
|
//0x5e => JIS Ro,
|
2017-02-03 23:05:57 +11:00
|
|
|
//0x5f => unkown,
|
|
|
|
0x60 => events::VirtualKeyCode::F5,
|
|
|
|
0x61 => events::VirtualKeyCode::F6,
|
|
|
|
0x62 => events::VirtualKeyCode::F7,
|
|
|
|
0x63 => events::VirtualKeyCode::F3,
|
|
|
|
0x64 => events::VirtualKeyCode::F8,
|
|
|
|
0x65 => events::VirtualKeyCode::F9,
|
2018-12-20 05:32:14 +11:00
|
|
|
//0x66 => JIS Eisuu (macOS),
|
2017-02-03 23:05:57 +11:00
|
|
|
0x67 => events::VirtualKeyCode::F11,
|
2018-12-20 05:32:14 +11:00
|
|
|
//0x68 => JIS Kana (macOS),
|
2017-02-03 23:05:57 +11:00
|
|
|
0x69 => events::VirtualKeyCode::F13,
|
2018-09-13 03:04:16 +10:00
|
|
|
0x6a => events::VirtualKeyCode::F16,
|
2017-02-03 23:05:57 +11:00
|
|
|
0x6b => events::VirtualKeyCode::F14,
|
|
|
|
//0x6c => unkown,
|
|
|
|
0x6d => events::VirtualKeyCode::F10,
|
|
|
|
//0x6e => unkown,
|
|
|
|
0x6f => events::VirtualKeyCode::F12,
|
|
|
|
//0x70 => unkown,
|
|
|
|
0x71 => events::VirtualKeyCode::F15,
|
|
|
|
0x72 => events::VirtualKeyCode::Insert,
|
|
|
|
0x73 => events::VirtualKeyCode::Home,
|
|
|
|
0x74 => events::VirtualKeyCode::PageUp,
|
|
|
|
0x75 => events::VirtualKeyCode::Delete,
|
|
|
|
0x76 => events::VirtualKeyCode::F4,
|
|
|
|
0x77 => events::VirtualKeyCode::End,
|
|
|
|
0x78 => events::VirtualKeyCode::F2,
|
|
|
|
0x79 => events::VirtualKeyCode::PageDown,
|
|
|
|
0x7a => events::VirtualKeyCode::F1,
|
|
|
|
0x7b => events::VirtualKeyCode::Left,
|
|
|
|
0x7c => events::VirtualKeyCode::Right,
|
|
|
|
0x7d => events::VirtualKeyCode::Down,
|
|
|
|
0x7e => events::VirtualKeyCode::Up,
|
|
|
|
//0x7f => unkown,
|
|
|
|
|
2018-01-25 23:32:30 +11:00
|
|
|
0xa => events::VirtualKeyCode::Caret,
|
2017-02-03 23:05:57 +11:00
|
|
|
_ => return None,
|
|
|
|
})
|
|
|
|
}
|
2017-01-20 13:18:05 +11:00
|
|
|
|
2019-02-24 07:41:55 +11:00
|
|
|
pub fn check_function_keys(
|
|
|
|
s: &String
|
2018-09-13 03:04:16 +10:00
|
|
|
) -> Option<events::VirtualKeyCode> {
|
2019-02-24 07:41:55 +11:00
|
|
|
if let Some(ch) = s.encode_utf16().next() {
|
|
|
|
return Some(match ch {
|
|
|
|
0xf718 => events::VirtualKeyCode::F21,
|
|
|
|
0xf719 => events::VirtualKeyCode::F22,
|
|
|
|
0xf71a => events::VirtualKeyCode::F23,
|
|
|
|
0xf71b => events::VirtualKeyCode::F24,
|
|
|
|
_ => return None,
|
|
|
|
})
|
2018-09-13 03:04:16 +10:00
|
|
|
}
|
2019-02-24 07:41:55 +11:00
|
|
|
|
2018-09-13 03:04:16 +10:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2018-05-18 11:28:30 +10:00
|
|
|
pub fn event_mods(event: cocoa::base::id) -> ModifiersState {
|
2017-01-20 13:18:05 +11:00
|
|
|
let flags = unsafe {
|
|
|
|
NSEvent::modifierFlags(event)
|
|
|
|
};
|
|
|
|
ModifiersState {
|
2017-10-31 21:03:18 +11:00
|
|
|
shift: flags.contains(NSEventModifierFlags::NSShiftKeyMask),
|
|
|
|
ctrl: flags.contains(NSEventModifierFlags::NSControlKeyMask),
|
|
|
|
alt: flags.contains(NSEventModifierFlags::NSAlternateKeyMask),
|
|
|
|
logo: flags.contains(NSEventModifierFlags::NSCommandKeyMask),
|
2017-01-20 13:18:05 +11:00
|
|
|
}
|
2017-03-19 18:19:24 +11:00
|
|
|
}
|
2017-04-23 06:52:35 +10:00
|
|
|
|
2019-02-24 07:41:55 +11:00
|
|
|
pub fn get_scancode(event: cocoa::base::id) -> c_ushort {
|
|
|
|
// In AppKit, `keyCode` refers to the position (scancode) of a key rather than its character,
|
|
|
|
// and there is no easy way to navtively retrieve the layout-dependent character.
|
|
|
|
// In winit, we use keycode to refer to the key's character, and so this function aligns
|
|
|
|
// AppKit's terminology with ours.
|
|
|
|
unsafe {
|
|
|
|
msg_send![event, keyCode]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-18 11:28:30 +10:00
|
|
|
unsafe fn modifier_event(
|
|
|
|
ns_event: cocoa::base::id,
|
|
|
|
keymask: NSEventModifierFlags,
|
2018-07-26 03:36:33 +10:00
|
|
|
was_key_pressed: bool,
|
2018-05-18 11:28:30 +10:00
|
|
|
) -> Option<WindowEvent> {
|
2018-07-26 03:36:33 +10:00
|
|
|
if !was_key_pressed && NSEvent::modifierFlags(ns_event).contains(keymask)
|
|
|
|
|| was_key_pressed && !NSEvent::modifierFlags(ns_event).contains(keymask) {
|
|
|
|
let state = if was_key_pressed {
|
|
|
|
ElementState::Released
|
|
|
|
} else {
|
|
|
|
ElementState::Pressed
|
|
|
|
};
|
2019-02-24 07:41:55 +11:00
|
|
|
|
|
|
|
let scancode = get_scancode(ns_event);
|
|
|
|
let virtual_keycode = scancode_to_keycode(scancode);
|
2018-05-18 11:28:30 +10:00
|
|
|
Some(WindowEvent::KeyboardInput {
|
|
|
|
device_id: DEVICE_ID,
|
|
|
|
input: KeyboardInput {
|
|
|
|
state,
|
2019-02-24 07:41:55 +11:00
|
|
|
scancode: scancode as u32,
|
2018-05-18 11:28:30 +10:00
|
|
|
virtual_keycode,
|
|
|
|
modifiers: event_mods(ns_event),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-23 06:52:35 +10:00
|
|
|
// Constant device ID, to be removed when this backend is updated to report real device IDs.
|
2018-05-18 11:28:30 +10:00
|
|
|
pub const DEVICE_ID: ::DeviceId = ::DeviceId(DeviceId);
|