History

mac/event: move key event handling to input helper and optimise it

der richter committed 2 years ago

Commit 3ef3bbf1 parent 5482eec9 files · +134 −171Browse files

Unified · Split

9 files
  1. osdep/mac/application.m +1 −1
  2. osdep/mac/events.h +0 −1
  3. osdep/mac/events.m +0 −146
  4. osdep/mac/events_objc.h +0 −4
  5. osdep/mac/input_helper.swift +125 −12
  6. osdep/mac/remote_command_center.swift +1 −1
  7. osdep/mac/swift_bridge.h +1 −0
  8. video/out/mac/view.swift +5 −5
  9. video/out/mac/window.swift +1 −1
osdep/mac/application.m+1−1
@@ -115,7 +115,7 @@ static void terminate_cocoa_application(void) · 114 unmodified lines
115115
116116 - (void)sendEvent:(NSEvent *)event
117117 {
118− if ([self modalWindow] || ![_eventsResponder processKeyEvent:event])
118+ if ([self modalWindow] || ![_eventsResponder.inputHelper processKeyWithEvent:event])
119119 [super sendEvent:event];
120120 [_eventsResponder.inputHelper wakeup];
121121 }
osdep/mac/events.h+0−1
@@ -19,7 +19,6 @@ · 18 unmodified lines
1919
2020 #ifndef MAC_EVENTS
2121 #define MAC_EVENTS
22−#include "input/keycodes.h"
2322
2423 struct input_ctx;
2524 struct mpv_handle;
osdep/mac/events.m+0−146
@@ -17,21 +17,12 @@ · 16 unmodified lines
1717 * License along with mpv. If not, see <http://www.gnu.org/licenses/>.
1818 */
1919
20−// Carbon header is included but Carbon is NOT linked to mpv's binary. This
21−// file only needs this include to use the keycode definitions in keymap.
22−#import <Carbon/Carbon.h>
23−
24−// Media keys definitions
25−#import <IOKit/hidsystem/ev_keymap.h>
2620 #import <Cocoa/Cocoa.h>
2721
2822 #include "mpv_talloc.h"
2923 #include "input/event.h"
3024 #include "input/input.h"
3125 #include "player/client.h"
32−#include "input/keycodes.h"
33−// doesn't make much sense, but needed to access keymap functionality
34−#include "video/out/vo.h"
3526
3627 #import "osdep/mac/events_objc.h"
3728 #import "osdep/mac/application_objc.h"
@@ -48,77 +39,13 @@ · 10 unmodified lines
4839 BOOL _is_application;
4940 }
5041
51−- (NSEvent *)handleKey:(NSEvent *)event;
5242 - (BOOL)setMpvHandle:(struct mpv_handle *)ctx;
5343 - (void)initCocoaCb;
5444 - (void)readEvents;
5545 - (void)startMediaKeys;
5646 - (void)stopMediaKeys;
57−- (int)mapKeyModifiers:(int)cocoaModifiers;
58−- (int)keyModifierMask:(NSEvent *)event;
5947 @end
6048
61−
62−#define NSLeftAlternateKeyMask (0x000020 | NSEventModifierFlagOption)
63−#define NSRightAlternateKeyMask (0x000040 | NSEventModifierFlagOption)
64−
65−static bool LeftAltPressed(int mask)
66−{
67− return (mask & NSLeftAlternateKeyMask) == NSLeftAlternateKeyMask;
68−}
69−
70−static bool RightAltPressed(int mask)
71−{
72− return (mask & NSRightAlternateKeyMask) == NSRightAlternateKeyMask;
73−}
74−
75−static const struct mp_keymap keymap[] = {
76− // special keys
77− {kVK_Return, MP_KEY_ENTER}, {kVK_Escape, MP_KEY_ESC},
78− {kVK_Delete, MP_KEY_BACKSPACE}, {kVK_Option, MP_KEY_BACKSPACE},
79− {kVK_Control, MP_KEY_BACKSPACE}, {kVK_Shift, MP_KEY_BACKSPACE},
80− {kVK_Tab, MP_KEY_TAB},
81−
82− // cursor keys
83− {kVK_UpArrow, MP_KEY_UP}, {kVK_DownArrow, MP_KEY_DOWN},
84− {kVK_LeftArrow, MP_KEY_LEFT}, {kVK_RightArrow, MP_KEY_RIGHT},
85−
86− // navigation block
87− {kVK_Help, MP_KEY_INSERT}, {kVK_ForwardDelete, MP_KEY_DELETE},
88− {kVK_Home, MP_KEY_HOME}, {kVK_End, MP_KEY_END},
89− {kVK_PageUp, MP_KEY_PAGE_UP}, {kVK_PageDown, MP_KEY_PAGE_DOWN},
90−
91− // F-keys
92− {kVK_F1, MP_KEY_F + 1}, {kVK_F2, MP_KEY_F + 2}, {kVK_F3, MP_KEY_F + 3},
93− {kVK_F4, MP_KEY_F + 4}, {kVK_F5, MP_KEY_F + 5}, {kVK_F6, MP_KEY_F + 6},
94− {kVK_F7, MP_KEY_F + 7}, {kVK_F8, MP_KEY_F + 8}, {kVK_F9, MP_KEY_F + 9},
95− {kVK_F10, MP_KEY_F + 10}, {kVK_F11, MP_KEY_F + 11}, {kVK_F12, MP_KEY_F + 12},
96− {kVK_F13, MP_KEY_F + 13}, {kVK_F14, MP_KEY_F + 14}, {kVK_F15, MP_KEY_F + 15},
97− {kVK_F16, MP_KEY_F + 16}, {kVK_F17, MP_KEY_F + 17}, {kVK_F18, MP_KEY_F + 18},
98− {kVK_F19, MP_KEY_F + 19}, {kVK_F20, MP_KEY_F + 20},
99−
100− // numpad
101− {kVK_ANSI_KeypadPlus, '+'}, {kVK_ANSI_KeypadMinus, '-'},
102− {kVK_ANSI_KeypadMultiply, '*'}, {kVK_ANSI_KeypadDivide, '/'},
103− {kVK_ANSI_KeypadEnter, MP_KEY_KPENTER},
104− {kVK_ANSI_KeypadDecimal, MP_KEY_KPDEC},
105− {kVK_ANSI_Keypad0, MP_KEY_KP0}, {kVK_ANSI_Keypad1, MP_KEY_KP1},
106− {kVK_ANSI_Keypad2, MP_KEY_KP2}, {kVK_ANSI_Keypad3, MP_KEY_KP3},
107− {kVK_ANSI_Keypad4, MP_KEY_KP4}, {kVK_ANSI_Keypad5, MP_KEY_KP5},
108− {kVK_ANSI_Keypad6, MP_KEY_KP6}, {kVK_ANSI_Keypad7, MP_KEY_KP7},
109− {kVK_ANSI_Keypad8, MP_KEY_KP8}, {kVK_ANSI_Keypad9, MP_KEY_KP9},
110−
111− {0, 0}
112−};
113−
114−static int convert_key(unsigned key, unsigned charcode)
115−{
116− int mpkey = lookup_keymap_table(keymap, key);
117− if (mpkey)
118− return mpkey;
119− return charcode;
120−}
121−
12249 void cocoa_init_media_keys(void)
12350 {
12451 [[EventsResponder sharedInstance] startMediaKeys];
@@ -255,77 +182,4 @@ void cocoa_init_cocoa_cb(void) · 130 unmodified lines
255182 [_remoteCommandCenter stop];
256183 }
257184
258−- (int)mapKeyModifiers:(int)cocoaModifiers
259−{
260− int mask = 0;
261− if (cocoaModifiers & NSEventModifierFlagShift)
262− mask |= MP_KEY_MODIFIER_SHIFT;
263− if (cocoaModifiers & NSEventModifierFlagControl)
264− mask |= MP_KEY_MODIFIER_CTRL;
265− if (LeftAltPressed(cocoaModifiers) ||
266− (RightAltPressed(cocoaModifiers) && ![_inputHelper useAltGr]))
267− mask |= MP_KEY_MODIFIER_ALT;
268− if (cocoaModifiers & NSEventModifierFlagCommand)
269− mask |= MP_KEY_MODIFIER_META;
270− return mask;
271−}
272−
273−- (int)mapTypeModifiers:(NSEventType)type
274−{
275− NSDictionary *map = @{
276− @(NSEventTypeKeyDown) : @(MP_KEY_STATE_DOWN),
277− @(NSEventTypeKeyUp) : @(MP_KEY_STATE_UP),
278− };
279− return [map[@(type)] intValue];
280−}
281−
282−- (int)keyModifierMask:(NSEvent *)event
283−{
284− return [self mapKeyModifiers:[event modifierFlags]] |
285− [self mapTypeModifiers:[event type]];
286−}
287−
288−-(BOOL)handleMPKey:(int)key withMask:(int)mask
289−{
290− if (key > 0) {
291− [_inputHelper putKey:key | mask modifiers:0];
292− if (mask & MP_KEY_STATE_UP)
293− [_inputHelper putKey:MP_INPUT_RELEASE_ALL modifiers:0];
294− return YES;
295− } else {
296− return NO;
297− }
298−}
299−
300−- (NSEvent*)handleKey:(NSEvent *)event
301−{
302− if ([event isARepeat]) return nil;
303−
304− NSString *chars;
305−
306− if ([_inputHelper useAltGr] && RightAltPressed([event modifierFlags])) {
307− chars = [event characters];
308− } else {
309− chars = [event charactersIgnoringModifiers];
310− }
311−
312− struct bstr t = bstr0([chars UTF8String]);
313− int key = convert_key([event keyCode], bstr_decode_utf8(t, &t));
314−
315− if (key > -1)
316− [self handleMPKey:key withMask:[self keyModifierMask:event]];
317−
318− return nil;
319−}
320−
321−- (bool)processKeyEvent:(NSEvent *)event
322−{
323− if (event.type == NSEventTypeKeyDown || event.type == NSEventTypeKeyUp){
324− if (![[NSApp mainMenu] performKeyEquivalent:event])
325− [self handleKey:event];
326− return true;
327− }
328− return false;
329−}
330−
331185 @end
osdep/mac/events_objc.h+0−4
@@ -29,10 +29,6 @@ struct input_ctx; · 28 unmodified lines
2929 + (EventsResponder *)sharedInstance;
3030 - (void)setIsApplication:(BOOL)isApplication;
3131
32−- (bool)processKeyEvent:(NSEvent *)event;
33−
34−- (BOOL)handleMPKey:(int)key withMask:(int)mask;
35−
3632 @property(nonatomic, retain) RemoteCommandCenter *remoteCommandCenter;
3733 @property(nonatomic, retain) InputHelper *inputHelper;
3834
osdep/mac/input_helper.swift+125−12
@@ -15,21 +15,130 @@ · 14 unmodified lines
1515 * License along with mpv. If not, see <http://www.gnu.org/licenses/>.
1616 */
1717
18+import Carbon.HIToolbox
19+
1820 class InputHelper: NSObject {
1921 var mpv: MPVHelper?
2022 var lock = NSCondition()
2123 private var input: OpaquePointer?
2224
25+ let keymap: [mp_keymap] = [
26+ // special keys
27+ mp_keymap(from: Int32(kVK_Return), to: MP_KEY_ENTER),
28+ mp_keymap(from: Int32(kVK_Escape), to: MP_KEY_ESC),
29+ mp_keymap(from: Int32(kVK_Delete), to: MP_KEY_BACKSPACE),
30+ mp_keymap(from: Int32(kVK_Option), to: MP_KEY_BACKSPACE),
31+ mp_keymap(from: Int32(kVK_Control), to: MP_KEY_BACKSPACE),
32+ mp_keymap(from: Int32(kVK_Shift), to: MP_KEY_BACKSPACE),
33+ mp_keymap(from: Int32(kVK_Tab), to: MP_KEY_TAB),
34+
35+ // cursor keys
36+ mp_keymap(from: Int32(kVK_UpArrow), to: MP_KEY_UP),
37+ mp_keymap(from: Int32(kVK_DownArrow), to: MP_KEY_DOWN),
38+ mp_keymap(from: Int32(kVK_LeftArrow), to: MP_KEY_LEFT),
39+ mp_keymap(from: Int32(kVK_RightArrow), to: MP_KEY_RIGHT),
40+
41+ // navigation block
42+ mp_keymap(from: Int32(kVK_Help), to: MP_KEY_INSERT),
43+ mp_keymap(from: Int32(kVK_ForwardDelete), to: MP_KEY_DELETE),
44+ mp_keymap(from: Int32(kVK_Home), to: MP_KEY_HOME),
45+ mp_keymap(from: Int32(kVK_End), to: MP_KEY_END),
46+ mp_keymap(from: Int32(kVK_PageUp), to: MP_KEY_PAGE_UP),
47+ mp_keymap(from: Int32(kVK_PageDown), to: MP_KEY_PAGE_DOWN),
48+
49+ // F-keys
50+ mp_keymap(from: Int32(kVK_F1), to: MP_KEY_F + 1),
51+ mp_keymap(from: Int32(kVK_F2), to: MP_KEY_F + 2),
52+ mp_keymap(from: Int32(kVK_F3), to: MP_KEY_F + 3),
53+ mp_keymap(from: Int32(kVK_F4), to: MP_KEY_F + 4),
54+ mp_keymap(from: Int32(kVK_F5), to: MP_KEY_F + 5),
55+ mp_keymap(from: Int32(kVK_F6), to: MP_KEY_F + 6),
56+ mp_keymap(from: Int32(kVK_F7), to: MP_KEY_F + 7),
57+ mp_keymap(from: Int32(kVK_F8), to: MP_KEY_F + 8),
58+ mp_keymap(from: Int32(kVK_F9), to: MP_KEY_F + 9),
59+ mp_keymap(from: Int32(kVK_F10), to: MP_KEY_F + 10),
60+ mp_keymap(from: Int32(kVK_F11), to: MP_KEY_F + 11),
61+ mp_keymap(from: Int32(kVK_F12), to: MP_KEY_F + 12),
62+ mp_keymap(from: Int32(kVK_F13), to: MP_KEY_F + 13),
63+ mp_keymap(from: Int32(kVK_F14), to: MP_KEY_F + 14),
64+ mp_keymap(from: Int32(kVK_F15), to: MP_KEY_F + 15),
65+ mp_keymap(from: Int32(kVK_F16), to: MP_KEY_F + 16),
66+ mp_keymap(from: Int32(kVK_F17), to: MP_KEY_F + 17),
67+ mp_keymap(from: Int32(kVK_F18), to: MP_KEY_F + 18),
68+ mp_keymap(from: Int32(kVK_F19), to: MP_KEY_F + 19),
69+ mp_keymap(from: Int32(kVK_F20), to: MP_KEY_F + 20),
70+
71+ // numpad
72+ mp_keymap(from: Int32(kVK_ANSI_KeypadPlus), to: Int32(Character("+").asciiValue ?? 0)),
73+ mp_keymap(from: Int32(kVK_ANSI_KeypadMinus), to: Int32(Character("-").asciiValue ?? 0)),
74+ mp_keymap(from: Int32(kVK_ANSI_KeypadMultiply), to: Int32(Character("*").asciiValue ?? 0)),
75+ mp_keymap(from: Int32(kVK_ANSI_KeypadDivide), to: Int32(Character("/").asciiValue ?? 0)),
76+ mp_keymap(from: Int32(kVK_ANSI_KeypadEnter), to: MP_KEY_KPENTER),
77+ mp_keymap(from: Int32(kVK_ANSI_KeypadDecimal), to: MP_KEY_KPDEC),
78+ mp_keymap(from: Int32(kVK_ANSI_Keypad0), to: MP_KEY_KP0),
79+ mp_keymap(from: Int32(kVK_ANSI_Keypad1), to: MP_KEY_KP1),
80+ mp_keymap(from: Int32(kVK_ANSI_Keypad2), to: MP_KEY_KP2),
81+ mp_keymap(from: Int32(kVK_ANSI_Keypad3), to: MP_KEY_KP3),
82+ mp_keymap(from: Int32(kVK_ANSI_Keypad4), to: MP_KEY_KP4),
83+ mp_keymap(from: Int32(kVK_ANSI_Keypad5), to: MP_KEY_KP5),
84+ mp_keymap(from: Int32(kVK_ANSI_Keypad6), to: MP_KEY_KP6),
85+ mp_keymap(from: Int32(kVK_ANSI_Keypad7), to: MP_KEY_KP7),
86+ mp_keymap(from: Int32(kVK_ANSI_Keypad8), to: MP_KEY_KP8),
87+ mp_keymap(from: Int32(kVK_ANSI_Keypad9), to: MP_KEY_KP9),
88+
89+ mp_keymap(from: 0, to: 0)
90+ ]
91+
2392 @objc init(_ input: OpaquePointer? = nil, _ mpv: MPVHelper? = nil) {
2493 super.init()
2594 self.input = input
2695 self.mpv = mpv
2796 }
2897
29− @objc func putKey(_ key: Int32, modifiers: NSEvent.ModifierFlags = .init(rawValue: 0)) {
98+ @objc func put(
99+ key: Int32,
100+ modifiers: NSEvent.ModifierFlags = .init(rawValue: 0),
101+ type: NSEvent.EventType = .applicationDefined
102+ ) {
30103 lock.withLock {
31− guard let input = input else { return }
32− mp_input_put_key(input, key | mapModifier(modifiers))
104+ putKey(key, modifiers: modifiers, type: type)
105+ }
106+ }
107+
108+ private func putKey(
109+ _ key: Int32,
110+ modifiers: NSEvent.ModifierFlags = .init(rawValue: 0),
111+ type: NSEvent.EventType = .applicationDefined
112+ ) {
113+ if key < 1 { return }
114+
115+ guard let input = input else { return }
116+ let code = key | mapModifier(modifiers) | mapType(type)
117+ mp_input_put_key(input, code)
118+
119+ if type == .keyUp {
120+ mp_input_put_key(input, MP_INPUT_RELEASE_ALL)
121+ }
122+ }
123+
124+ @objc func processKey(event: NSEvent) -> Bool {
125+ if event.type != .keyDown && event.type != .keyUp { return false }
126+ if NSApp.mainMenu?.performKeyEquivalent(with: event) ?? false || event.isARepeat { return true }
127+
128+ return lock.withLock {
129+ let mpkey = lookup_keymap_table(keymap, Int32(event.keyCode))
130+ if mpkey > 0 {
131+ putKey(mpkey, modifiers: event.modifierFlags, type: event.type)
132+ return true
133+ }
134+
135+ guard let chars = event.characters, let charsNoMod = event.charactersIgnoringModifiers else { return false }
136+ let key = (useAltGr() && event.modifierFlags.contains(.optionRight)) ? chars : charsNoMod
137+ key.withCString {
138+ var bstr = bstr0($0)
139+ putKey(bstr_decode_utf8(bstr, &bstr), modifiers: event.modifierFlags, type: event.type)
140+ }
141+ return true
33142 }
34143 }
35144
@@ -72,9 +181,17 @@ class InputHelper: NSObject { · 36 unmodified lines
72181 }
73182 }
74183
184+ private func mapType(_ type: NSEvent.EventType) -> Int32 {
185+ let typeMapping: [NSEvent.EventType:UInt32] = [
186+ .keyDown: MP_KEY_STATE_DOWN,
187+ .keyUp: MP_KEY_STATE_UP,
188+ ]
189+
190+ return Int32(typeMapping[type] ?? 0);
191+ }
192+
75193 private func mapModifier(_ modifiers: NSEvent.ModifierFlags) -> Int32 {
76194 var mask: UInt32 = 0;
77− guard let input = input else { return Int32(mask) }
78195
79196 if modifiers.contains(.shift) {
80197 mask |= MP_KEY_MODIFIER_SHIFT
@@ -85,9 +202,7 @@ class InputHelper: NSObject { · 4 unmodified lines
85202 if modifiers.contains(.command) {
86203 mask |= MP_KEY_MODIFIER_META
87204 }
88− if modifiers.contains(.optionLeft) ||
89− modifiers.contains(.optionRight) && !mp_input_use_alt_gr(input)
90− {
205+ if modifiers.contains(.optionLeft) || modifiers.contains(.optionRight) && !useAltGr() {
91206 mask |= MP_KEY_MODIFIER_ALT
92207 }
93208
@@ -111,11 +226,9 @@ class InputHelper: NSObject { · 17 unmodified lines
111226 }
112227 }
113228
114− @objc func useAltGr() -> Bool {
115− lock.withLock {
116− guard let input = input else { return false }
117− return mp_input_use_alt_gr(input)
118− }
229+ private func useAltGr() -> Bool {
230+ guard let input = input else { return false }
231+ return mp_input_use_alt_gr(input)
119232 }
120233
121234 @objc func wakeup() {
osdep/mac/remote_command_center.swift+1−1
@@ -155,7 +155,7 @@ class RemoteCommandCenter: NSObject { · 154 unmodified lines
155155 self.configs[event.command]?.state = state
156156 }
157157
158− EventsResponder.sharedInstance().handleMPKey(config.key, withMask: Int32(state))
158+ EventsResponder.sharedInstance().inputHelper.put(key: config.key | Int32(state))
159159
160160 return .success
161161 }
osdep/mac/swift_bridge.h+1−0
@@ -28,6 +28,7 @@ · 27 unmodified lines
2828 #include "player/core.h"
2929 #include "input/input.h"
3030 #include "input/event.h"
31+#include "input/keycodes.h"
3132 #include "video/out/win_state.h"
3233
3334 #include "osdep/mac/application_objc.h"
video/out/mac/view.swift+5−5
@@ -53,7 +53,7 @@ class View: NSView, CALayerDelegate { · 52 unmodified lines
5353 addTrackingArea(tracker!)
5454
5555 if containsMouseLocation() {
56− input?.putKey(SWIFT_KEY_MOUSE_LEAVE)
56+ input?.put(key: SWIFT_KEY_MOUSE_LEAVE)
5757 }
5858 }
5959
@@ -118,14 +118,14 @@ class View: NSView, CALayerDelegate { · 58 unmodified lines
118118
119119 override func mouseEntered(with event: NSEvent) {
120120 if input?.mouseEnabled() ?? true {
121− input?.putKey(SWIFT_KEY_MOUSE_ENTER)
121+ input?.put(key: SWIFT_KEY_MOUSE_ENTER)
122122 }
123123 common.updateCursorVisibility()
124124 }
125125
126126 override func mouseExited(with event: NSEvent) {
127127 if input?.mouseEnabled() ?? true {
128− input?.putKey(SWIFT_KEY_MOUSE_LEAVE)
128+ input?.put(key: SWIFT_KEY_MOUSE_LEAVE)
129129 }
130130 common.titleBar?.hide()
131131 common.setCursorVisibility(true)
@@ -202,7 +202,7 @@ class View: NSView, CALayerDelegate { · 70 unmodified lines
202202 func signalMouseEvent(_ event: NSEvent, _ state: UInt32) {
203203 hasMouseDown = state == MP_KEY_STATE_DOWN
204204 let mpkey = getMpvButton(event)
205− input?.putKey((mpkey | Int32(state)), modifiers: event.modifierFlags)
205+ input?.put(key: (mpkey | Int32(state)), modifiers: event.modifierFlags)
206206 }
207207
208208 func signalMouseMovement(_ event: NSEvent) {
@@ -250,7 +250,7 @@ class View: NSView, CALayerDelegate { · 41 unmodified lines
250250 mpkey = deltaX > 0 ? SWIFT_WHEEL_LEFT : SWIFT_WHEEL_RIGHT
251251 }
252252
253− input?.putKey(mpkey, modifiers: modifiers)
253+ input?.put(key: mpkey, modifiers: modifiers)
254254 }
255255 }
256256
video/out/mac/window.swift+1−1
@@ -556,7 +556,7 @@ class Window: NSWindow, NSWindowDelegate { · 555 unmodified lines
556556 }
557557
558558 func windowShouldClose(_ sender: NSWindow) -> Bool {
559− input?.putKey(MP_KEY_CLOSE_WIN)
559+ input?.put(key: MP_KEY_CLOSE_WIN)
560560 return false
561561 }
562562

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t