events.rs

  1use crate::{
  2    KeyDownEvent, KeyUpEvent, Keystroke, Modifiers, ModifiersChangedEvent, MouseButton,
  3    MouseDownEvent, MouseExitEvent, MouseMoveEvent, MouseUpEvent, NavigationDirection, Pixels,
  4    PlatformInput, ScrollDelta, ScrollWheelEvent, TouchPhase,
  5    platform::mac::{
  6        LMGetKbdType, NSStringExt, TISCopyCurrentKeyboardLayoutInputSource,
  7        TISGetInputSourceProperty, UCKeyTranslate, kTISPropertyUnicodeKeyLayoutData,
  8    },
  9    point, px,
 10};
 11use cocoa::{
 12    appkit::{NSEvent, NSEventModifierFlags, NSEventPhase, NSEventType},
 13    base::{YES, id},
 14};
 15use core_foundation::data::{CFDataGetBytePtr, CFDataRef};
 16use core_graphics::event::CGKeyCode;
 17use objc::{msg_send, sel, sel_impl};
 18use std::{borrow::Cow, ffi::c_void};
 19
 20const BACKSPACE_KEY: u16 = 0x7f;
 21const SPACE_KEY: u16 = b' ' as u16;
 22const ENTER_KEY: u16 = 0x0d;
 23const NUMPAD_ENTER_KEY: u16 = 0x03;
 24const ESCAPE_KEY: u16 = 0x1b;
 25const TAB_KEY: u16 = 0x09;
 26const SHIFT_TAB_KEY: u16 = 0x19;
 27
 28pub fn key_to_native(key: &str) -> Cow<str> {
 29    use cocoa::appkit::*;
 30    let code = match key {
 31        "space" => SPACE_KEY,
 32        "backspace" => BACKSPACE_KEY,
 33        "up" => NSUpArrowFunctionKey,
 34        "down" => NSDownArrowFunctionKey,
 35        "left" => NSLeftArrowFunctionKey,
 36        "right" => NSRightArrowFunctionKey,
 37        "pageup" => NSPageUpFunctionKey,
 38        "pagedown" => NSPageDownFunctionKey,
 39        "home" => NSHomeFunctionKey,
 40        "end" => NSEndFunctionKey,
 41        "delete" => NSDeleteFunctionKey,
 42        "insert" => NSHelpFunctionKey,
 43        "f1" => NSF1FunctionKey,
 44        "f2" => NSF2FunctionKey,
 45        "f3" => NSF3FunctionKey,
 46        "f4" => NSF4FunctionKey,
 47        "f5" => NSF5FunctionKey,
 48        "f6" => NSF6FunctionKey,
 49        "f7" => NSF7FunctionKey,
 50        "f8" => NSF8FunctionKey,
 51        "f9" => NSF9FunctionKey,
 52        "f10" => NSF10FunctionKey,
 53        "f11" => NSF11FunctionKey,
 54        "f12" => NSF12FunctionKey,
 55        "f13" => NSF13FunctionKey,
 56        "f14" => NSF14FunctionKey,
 57        "f15" => NSF15FunctionKey,
 58        "f16" => NSF16FunctionKey,
 59        "f17" => NSF17FunctionKey,
 60        "f18" => NSF18FunctionKey,
 61        "f19" => NSF19FunctionKey,
 62        "f20" => NSF20FunctionKey,
 63        "f21" => NSF21FunctionKey,
 64        "f22" => NSF22FunctionKey,
 65        "f23" => NSF23FunctionKey,
 66        "f24" => NSF24FunctionKey,
 67        "f25" => NSF25FunctionKey,
 68        "f26" => NSF26FunctionKey,
 69        "f27" => NSF27FunctionKey,
 70        "f28" => NSF28FunctionKey,
 71        "f29" => NSF29FunctionKey,
 72        "f30" => NSF30FunctionKey,
 73        "f31" => NSF31FunctionKey,
 74        "f32" => NSF32FunctionKey,
 75        "f33" => NSF33FunctionKey,
 76        "f34" => NSF34FunctionKey,
 77        "f35" => NSF35FunctionKey,
 78        _ => return Cow::Borrowed(key),
 79    };
 80    Cow::Owned(String::from_utf16(&[code]).unwrap())
 81}
 82
 83unsafe fn read_modifiers(native_event: id) -> Modifiers {
 84    unsafe {
 85        let modifiers = native_event.modifierFlags();
 86        let control = modifiers.contains(NSEventModifierFlags::NSControlKeyMask);
 87        let alt = modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask);
 88        let shift = modifiers.contains(NSEventModifierFlags::NSShiftKeyMask);
 89        let command = modifiers.contains(NSEventModifierFlags::NSCommandKeyMask);
 90        let function = modifiers.contains(NSEventModifierFlags::NSFunctionKeyMask);
 91
 92        Modifiers {
 93            control,
 94            alt,
 95            shift,
 96            platform: command,
 97            function,
 98        }
 99    }
100}
101
102impl PlatformInput {
103    pub(crate) unsafe fn from_native(
104        native_event: id,
105        window_height: Option<Pixels>,
106    ) -> Option<Self> {
107        unsafe {
108            let event_type = native_event.eventType();
109
110            // Filter out event types that aren't in the NSEventType enum.
111            // See https://github.com/servo/cocoa-rs/issues/155#issuecomment-323482792 for details.
112            match event_type as u64 {
113                0 | 21 | 32 | 33 | 35 | 36 | 37 => {
114                    return None;
115                }
116                _ => {}
117            }
118
119            match event_type {
120                NSEventType::NSFlagsChanged => {
121                    Some(Self::ModifiersChanged(ModifiersChangedEvent {
122                        modifiers: read_modifiers(native_event),
123                    }))
124                }
125                NSEventType::NSKeyDown => Some(Self::KeyDown(KeyDownEvent {
126                    keystroke: parse_keystroke(native_event),
127                    is_held: native_event.isARepeat() == YES,
128                })),
129                NSEventType::NSKeyUp => Some(Self::KeyUp(KeyUpEvent {
130                    keystroke: parse_keystroke(native_event),
131                })),
132                NSEventType::NSLeftMouseDown
133                | NSEventType::NSRightMouseDown
134                | NSEventType::NSOtherMouseDown => {
135                    let button = match native_event.buttonNumber() {
136                        0 => MouseButton::Left,
137                        1 => MouseButton::Right,
138                        2 => MouseButton::Middle,
139                        3 => MouseButton::Navigate(NavigationDirection::Back),
140                        4 => MouseButton::Navigate(NavigationDirection::Forward),
141                        // Other mouse buttons aren't tracked currently
142                        _ => return None,
143                    };
144                    window_height.map(|window_height| {
145                        Self::MouseDown(MouseDownEvent {
146                            button,
147                            position: point(
148                                px(native_event.locationInWindow().x as f32),
149                                // MacOS screen coordinates are relative to bottom left
150                                window_height - px(native_event.locationInWindow().y as f32),
151                            ),
152                            modifiers: read_modifiers(native_event),
153                            click_count: native_event.clickCount() as usize,
154                            first_mouse: false,
155                        })
156                    })
157                }
158                NSEventType::NSLeftMouseUp
159                | NSEventType::NSRightMouseUp
160                | NSEventType::NSOtherMouseUp => {
161                    let button = match native_event.buttonNumber() {
162                        0 => MouseButton::Left,
163                        1 => MouseButton::Right,
164                        2 => MouseButton::Middle,
165                        3 => MouseButton::Navigate(NavigationDirection::Back),
166                        4 => MouseButton::Navigate(NavigationDirection::Forward),
167                        // Other mouse buttons aren't tracked currently
168                        _ => return None,
169                    };
170
171                    window_height.map(|window_height| {
172                        Self::MouseUp(MouseUpEvent {
173                            button,
174                            position: point(
175                                px(native_event.locationInWindow().x as f32),
176                                window_height - px(native_event.locationInWindow().y as f32),
177                            ),
178                            modifiers: read_modifiers(native_event),
179                            click_count: native_event.clickCount() as usize,
180                        })
181                    })
182                }
183                // Some mice (like Logitech MX Master) send navigation buttons as swipe events
184                NSEventType::NSEventTypeSwipe => {
185                    let navigation_direction = match native_event.phase() {
186                        NSEventPhase::NSEventPhaseEnded => match native_event.deltaX() {
187                            x if x > 0.0 => Some(NavigationDirection::Back),
188                            x if x < 0.0 => Some(NavigationDirection::Forward),
189                            _ => return None,
190                        },
191                        _ => return None,
192                    };
193
194                    match navigation_direction {
195                        Some(direction) => window_height.map(|window_height| {
196                            Self::MouseDown(MouseDownEvent {
197                                button: MouseButton::Navigate(direction),
198                                position: point(
199                                    px(native_event.locationInWindow().x as f32),
200                                    window_height - px(native_event.locationInWindow().y as f32),
201                                ),
202                                modifiers: read_modifiers(native_event),
203                                click_count: 1,
204                                first_mouse: false,
205                            })
206                        }),
207                        _ => None,
208                    }
209                }
210                NSEventType::NSScrollWheel => window_height.map(|window_height| {
211                    let phase = match native_event.phase() {
212                        NSEventPhase::NSEventPhaseMayBegin | NSEventPhase::NSEventPhaseBegan => {
213                            TouchPhase::Started
214                        }
215                        NSEventPhase::NSEventPhaseEnded => TouchPhase::Ended,
216                        _ => TouchPhase::Moved,
217                    };
218
219                    let raw_data = point(
220                        native_event.scrollingDeltaX() as f32,
221                        native_event.scrollingDeltaY() as f32,
222                    );
223
224                    let delta = if native_event.hasPreciseScrollingDeltas() == YES {
225                        ScrollDelta::Pixels(raw_data.map(px))
226                    } else {
227                        ScrollDelta::Lines(raw_data)
228                    };
229
230                    Self::ScrollWheel(ScrollWheelEvent {
231                        position: point(
232                            px(native_event.locationInWindow().x as f32),
233                            window_height - px(native_event.locationInWindow().y as f32),
234                        ),
235                        delta,
236                        touch_phase: phase,
237                        modifiers: read_modifiers(native_event),
238                    })
239                }),
240                NSEventType::NSLeftMouseDragged
241                | NSEventType::NSRightMouseDragged
242                | NSEventType::NSOtherMouseDragged => {
243                    let pressed_button = match native_event.buttonNumber() {
244                        0 => MouseButton::Left,
245                        1 => MouseButton::Right,
246                        2 => MouseButton::Middle,
247                        3 => MouseButton::Navigate(NavigationDirection::Back),
248                        4 => MouseButton::Navigate(NavigationDirection::Forward),
249                        // Other mouse buttons aren't tracked currently
250                        _ => return None,
251                    };
252
253                    window_height.map(|window_height| {
254                        Self::MouseMove(MouseMoveEvent {
255                            pressed_button: Some(pressed_button),
256                            position: point(
257                                px(native_event.locationInWindow().x as f32),
258                                window_height - px(native_event.locationInWindow().y as f32),
259                            ),
260                            modifiers: read_modifiers(native_event),
261                        })
262                    })
263                }
264                NSEventType::NSMouseMoved => window_height.map(|window_height| {
265                    Self::MouseMove(MouseMoveEvent {
266                        position: point(
267                            px(native_event.locationInWindow().x as f32),
268                            window_height - px(native_event.locationInWindow().y as f32),
269                        ),
270                        pressed_button: None,
271                        modifiers: read_modifiers(native_event),
272                    })
273                }),
274                NSEventType::NSMouseExited => window_height.map(|window_height| {
275                    Self::MouseExited(MouseExitEvent {
276                        position: point(
277                            px(native_event.locationInWindow().x as f32),
278                            window_height - px(native_event.locationInWindow().y as f32),
279                        ),
280
281                        pressed_button: None,
282                        modifiers: read_modifiers(native_event),
283                    })
284                }),
285                _ => None,
286            }
287        }
288    }
289}
290
291unsafe fn parse_keystroke(native_event: id) -> Keystroke {
292    unsafe {
293        use cocoa::appkit::*;
294
295        let mut characters = native_event
296            .charactersIgnoringModifiers()
297            .to_str()
298            .to_string();
299        let mut key_char = None;
300        let first_char = characters.chars().next().map(|ch| ch as u16);
301        let modifiers = native_event.modifierFlags();
302
303        let control = modifiers.contains(NSEventModifierFlags::NSControlKeyMask);
304        let alt = modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask);
305        let mut shift = modifiers.contains(NSEventModifierFlags::NSShiftKeyMask);
306        let command = modifiers.contains(NSEventModifierFlags::NSCommandKeyMask);
307        let function = modifiers.contains(NSEventModifierFlags::NSFunctionKeyMask)
308            && first_char.map_or(true, |ch| {
309                !(NSUpArrowFunctionKey..=NSModeSwitchFunctionKey).contains(&ch)
310            });
311
312        #[allow(non_upper_case_globals)]
313        let key = match first_char {
314            Some(SPACE_KEY) => {
315                key_char = Some(" ".to_string());
316                "space".to_string()
317            }
318            Some(TAB_KEY) => {
319                key_char = Some("\t".to_string());
320                "tab".to_string()
321            }
322            Some(ENTER_KEY) | Some(NUMPAD_ENTER_KEY) => {
323                key_char = Some("\n".to_string());
324                "enter".to_string()
325            }
326            Some(BACKSPACE_KEY) => "backspace".to_string(),
327            Some(ESCAPE_KEY) => "escape".to_string(),
328            Some(SHIFT_TAB_KEY) => "tab".to_string(),
329            Some(NSUpArrowFunctionKey) => "up".to_string(),
330            Some(NSDownArrowFunctionKey) => "down".to_string(),
331            Some(NSLeftArrowFunctionKey) => "left".to_string(),
332            Some(NSRightArrowFunctionKey) => "right".to_string(),
333            Some(NSPageUpFunctionKey) => "pageup".to_string(),
334            Some(NSPageDownFunctionKey) => "pagedown".to_string(),
335            Some(NSHomeFunctionKey) => "home".to_string(),
336            Some(NSEndFunctionKey) => "end".to_string(),
337            Some(NSDeleteFunctionKey) => "delete".to_string(),
338            // Observed Insert==NSHelpFunctionKey not NSInsertFunctionKey.
339            Some(NSHelpFunctionKey) => "insert".to_string(),
340            Some(NSF1FunctionKey) => "f1".to_string(),
341            Some(NSF2FunctionKey) => "f2".to_string(),
342            Some(NSF3FunctionKey) => "f3".to_string(),
343            Some(NSF4FunctionKey) => "f4".to_string(),
344            Some(NSF5FunctionKey) => "f5".to_string(),
345            Some(NSF6FunctionKey) => "f6".to_string(),
346            Some(NSF7FunctionKey) => "f7".to_string(),
347            Some(NSF8FunctionKey) => "f8".to_string(),
348            Some(NSF9FunctionKey) => "f9".to_string(),
349            Some(NSF10FunctionKey) => "f10".to_string(),
350            Some(NSF11FunctionKey) => "f11".to_string(),
351            Some(NSF12FunctionKey) => "f12".to_string(),
352            Some(NSF13FunctionKey) => "f13".to_string(),
353            Some(NSF14FunctionKey) => "f14".to_string(),
354            Some(NSF15FunctionKey) => "f15".to_string(),
355            Some(NSF16FunctionKey) => "f16".to_string(),
356            Some(NSF17FunctionKey) => "f17".to_string(),
357            Some(NSF18FunctionKey) => "f18".to_string(),
358            Some(NSF19FunctionKey) => "f19".to_string(),
359            Some(NSF20FunctionKey) => "f20".to_string(),
360            Some(NSF21FunctionKey) => "f21".to_string(),
361            Some(NSF22FunctionKey) => "f22".to_string(),
362            Some(NSF23FunctionKey) => "f23".to_string(),
363            Some(NSF24FunctionKey) => "f24".to_string(),
364            Some(NSF25FunctionKey) => "f25".to_string(),
365            Some(NSF26FunctionKey) => "f26".to_string(),
366            Some(NSF27FunctionKey) => "f27".to_string(),
367            Some(NSF28FunctionKey) => "f28".to_string(),
368            Some(NSF29FunctionKey) => "f29".to_string(),
369            Some(NSF30FunctionKey) => "f30".to_string(),
370            Some(NSF31FunctionKey) => "f31".to_string(),
371            Some(NSF32FunctionKey) => "f32".to_string(),
372            Some(NSF33FunctionKey) => "f33".to_string(),
373            Some(NSF34FunctionKey) => "f34".to_string(),
374            Some(NSF35FunctionKey) => "f35".to_string(),
375            _ => {
376                // Cases to test when modifying this:
377                //
378                //           qwerty key | none | cmd   | cmd-shift
379                // * Armenian         s | ս    | cmd-s | cmd-shift-s  (layout is non-ASCII, so we use cmd layout)
380                // * Dvorak+QWERTY    s | o    | cmd-s | cmd-shift-s  (layout switches on cmd)
381                // * Ukrainian+QWERTY s | с    | cmd-s | cmd-shift-s  (macOS reports cmd-s instead of cmd-S)
382                // * Czech            7 | ý    | cmd-ý | cmd-7        (layout has shifted numbers)
383                // * Norwegian        7 | 7    | cmd-7 | cmd-/        (macOS reports cmd-shift-7 instead of cmd-/)
384                // * Russian          7 | 7    | cmd-7 | cmd-&        (shift-7 is . but when cmd is down, should use cmd layout)
385                // * German QWERTZ    ; | ö    | cmd-ö | cmd-Ö        (Zed's shift special case only applies to a-z)
386                //
387                let mut chars_ignoring_modifiers =
388                    chars_for_modified_key(native_event.keyCode(), NO_MOD);
389                let mut chars_with_shift =
390                    chars_for_modified_key(native_event.keyCode(), SHIFT_MOD);
391                let always_use_cmd_layout = always_use_command_layout();
392
393                // Handle Dvorak+QWERTY / Russian / Armenian
394                if command || always_use_cmd_layout {
395                    let chars_with_cmd = chars_for_modified_key(native_event.keyCode(), CMD_MOD);
396                    let chars_with_both =
397                        chars_for_modified_key(native_event.keyCode(), CMD_MOD | SHIFT_MOD);
398
399                    // We don't do this in the case that the shifted command key generates
400                    // the same character as the unshifted command key (Norwegian, e.g.)
401                    if chars_with_both != chars_with_cmd {
402                        chars_with_shift = chars_with_both;
403
404                    // Handle edge-case where cmd-shift-s reports cmd-s instead of
405                    // cmd-shift-s (Ukrainian, etc.)
406                    } else if chars_with_cmd.to_ascii_uppercase() != chars_with_cmd {
407                        chars_with_shift = chars_with_cmd.to_ascii_uppercase();
408                    }
409                    chars_ignoring_modifiers = chars_with_cmd;
410                }
411
412                if !control && !command && !function {
413                    let mut mods = NO_MOD;
414                    if shift {
415                        mods |= SHIFT_MOD;
416                    }
417                    if alt {
418                        mods |= OPTION_MOD;
419                    }
420
421                    key_char = Some(chars_for_modified_key(native_event.keyCode(), mods));
422                }
423
424                let mut key = if shift
425                    && chars_ignoring_modifiers
426                        .chars()
427                        .all(|c| c.is_ascii_lowercase())
428                {
429                    chars_ignoring_modifiers
430                } else if shift {
431                    shift = false;
432                    chars_with_shift
433                } else {
434                    chars_ignoring_modifiers
435                };
436
437                key
438            }
439        };
440
441        Keystroke {
442            modifiers: Modifiers {
443                control,
444                alt,
445                shift,
446                platform: command,
447                function,
448            },
449            key,
450            key_char,
451        }
452    }
453}
454
455fn always_use_command_layout() -> bool {
456    if chars_for_modified_key(0, NO_MOD).is_ascii() {
457        return false;
458    }
459
460    chars_for_modified_key(0, CMD_MOD).is_ascii()
461}
462
463const NO_MOD: u32 = 0;
464const CMD_MOD: u32 = 1;
465const SHIFT_MOD: u32 = 2;
466const OPTION_MOD: u32 = 8;
467
468fn chars_for_modified_key(code: CGKeyCode, modifiers: u32) -> String {
469    // Values from: https://github.com/phracker/MacOSX-SDKs/blob/master/MacOSX10.6.sdk/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Headers/Events.h#L126
470    // shifted >> 8 for UCKeyTranslate
471    const CG_SPACE_KEY: u16 = 49;
472    // https://github.com/phracker/MacOSX-SDKs/blob/master/MacOSX10.6.sdk/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/CarbonCore.framework/Versions/A/Headers/UnicodeUtilities.h#L278
473    #[allow(non_upper_case_globals)]
474    const kUCKeyActionDown: u16 = 0;
475    #[allow(non_upper_case_globals)]
476    const kUCKeyTranslateNoDeadKeysMask: u32 = 0;
477
478    let keyboard_type = unsafe { LMGetKbdType() as u32 };
479    const BUFFER_SIZE: usize = 4;
480    let mut dead_key_state = 0;
481    let mut buffer: [u16; BUFFER_SIZE] = [0; BUFFER_SIZE];
482    let mut buffer_size: usize = 0;
483
484    let keyboard = unsafe { TISCopyCurrentKeyboardLayoutInputSource() };
485    if keyboard.is_null() {
486        return "".to_string();
487    }
488    let layout_data = unsafe {
489        TISGetInputSourceProperty(keyboard, kTISPropertyUnicodeKeyLayoutData as *const c_void)
490            as CFDataRef
491    };
492    if layout_data.is_null() {
493        unsafe {
494            let _: () = msg_send![keyboard, release];
495        }
496        return "".to_string();
497    }
498    let keyboard_layout = unsafe { CFDataGetBytePtr(layout_data) };
499
500    unsafe {
501        UCKeyTranslate(
502            keyboard_layout as *const c_void,
503            code,
504            kUCKeyActionDown,
505            modifiers,
506            keyboard_type,
507            kUCKeyTranslateNoDeadKeysMask,
508            &mut dead_key_state,
509            BUFFER_SIZE,
510            &mut buffer_size as *mut usize,
511            &mut buffer as *mut u16,
512        );
513        if dead_key_state != 0 {
514            UCKeyTranslate(
515                keyboard_layout as *const c_void,
516                CG_SPACE_KEY,
517                kUCKeyActionDown,
518                modifiers,
519                keyboard_type,
520                kUCKeyTranslateNoDeadKeysMask,
521                &mut dead_key_state,
522                BUFFER_SIZE,
523                &mut buffer_size as *mut usize,
524                &mut buffer as *mut u16,
525            );
526        }
527        let _: () = msg_send![keyboard, release];
528    }
529    String::from_utf16(&buffer[..buffer_size]).unwrap_or_default()
530}