From 18fb71498be4a569f1c2ea99d897aa6bd1035202 Mon Sep 17 00:00:00 2001 From: der richter Date: Sun, 10 Mar 2024 00:53:44 +0100 Subject: [PATCH] mac/events: remove redundant functions and optimise input helper usage some redundant functions that jump through hoops. --- osdep/mac/application.m | 27 +++---------- osdep/mac/application_objc.h | 3 -- osdep/mac/events.h | 3 -- osdep/mac/events.m | 56 +++------------------------ osdep/mac/events_objc.h | 8 ---- osdep/mac/input_helper.swift | 4 +- osdep/mac/menu_bar.swift | 16 +++----- osdep/mac/remote_command_center.swift | 7 +--- osdep/mac/touchbar.h | 2 - osdep/mac/touchbar.m | 12 ++++-- video/out/mac/view.swift | 10 ++--- video/out/mac/window.swift | 2 +- 12 files changed, 35 insertions(+), 115 deletions(-) diff --git a/osdep/mac/application.m b/osdep/mac/application.m index 0c62cf724b..cd6d146a10 100644 --- a/osdep/mac/application.m +++ b/osdep/mac/application.m @@ -117,7 +117,7 @@ static void terminate_cocoa_application(void) { if ([self modalWindow] || ![_eventsResponder processKeyEvent:event]) [super sendEvent:event]; - [_eventsResponder wakeup]; + [_eventsResponder.inputHelper wakeup]; } - (id)init @@ -162,7 +162,6 @@ static const char mac_icon[] = - (NSTouchBar *)makeTouchBar { TouchBar *tBar = [[TouchBar alloc] init]; - [tBar setApp:self]; tBar.delegate = tBar; tBar.customizationIdentifier = customID; tBar.defaultItemIdentifiers = @[play, previousItem, nextItem, seekBar]; @@ -202,17 +201,6 @@ static const char mac_icon[] = return &vo_sub_opts; } -- (void)queueCommand:(char *)cmd -{ - [_eventsResponder queueCommand:cmd]; -} - -- (void)stopMPV:(char *)cmd -{ - if (![_eventsResponder queueCommand:cmd]) - terminate_cocoa_application(); -} - - (void)applicationWillFinishLaunching:(NSNotification *)notification { NSAppleEventManager *em = [NSAppleEventManager sharedAppleEventManager]; @@ -225,7 +213,8 @@ static const char mac_icon[] = - (void)handleQuitEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent { - [self stopMPV:"quit"]; + if (![_eventsResponder.inputHelper command:@"quit"]) + terminate_cocoa_application(); } - (void)getUrl:(NSAppleEventDescriptor *)event @@ -240,7 +229,7 @@ static const char mac_icon[] = range:NSMakeRange(0, [MPV_PROTOCOL length])]; url = [url stringByRemovingPercentEncoding]; - [_eventsResponder handleFilesArray:@[url]]; + [_eventsResponder.inputHelper openWithFiles:@[url]]; } - (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames @@ -249,14 +238,10 @@ static const char mac_icon[] = mpv_shared_app().openCount--; return; } - [self openFiles:filenames]; -} -- (void)openFiles:(NSArray *)filenames -{ SEL cmpsel = @selector(localizedStandardCompare:); NSArray *files = [filenames sortedArrayUsingSelector:cmpsel]; - [_eventsResponder handleFilesArray:files]; + [_eventsResponder.inputHelper openWithFiles:files]; } @end @@ -357,7 +342,7 @@ int cocoa_main(int argc, char *argv[]) } mp_thread_create(&playback_thread_id, playback_thread, &ctx); - [[EventsResponder sharedInstance] waitForInputContext]; + [[EventsResponder sharedInstance].inputHelper wait]; cocoa_run_runloop(); // This should never be reached: cocoa_run_runloop blocks until the diff --git a/osdep/mac/application_objc.h b/osdep/mac/application_objc.h index 6e492eef0e..b79ce1f167 100644 --- a/osdep/mac/application_objc.h +++ b/osdep/mac/application_objc.h @@ -27,9 +27,6 @@ struct mpv_handle; - (NSImage *)getMPVIcon; - (void)processEvent:(struct mpv_event *)event; -- (void)queueCommand:(char *)cmd; -- (void)stopMPV:(char *)cmd; -- (void)openFiles:(NSArray *)filenames; - (void)initCocoaCb:(struct mpv_handle *)ctx; + (const struct m_sub_options *)getMacOSConf; + (const struct m_sub_options *)getVoSubConf; diff --git a/osdep/mac/events.h b/osdep/mac/events.h index 1767b28778..574aae9b5a 100644 --- a/osdep/mac/events.h +++ b/osdep/mac/events.h @@ -24,9 +24,6 @@ struct input_ctx; struct mpv_handle; -void cocoa_put_key(int keycode); -void cocoa_put_key_with_modifiers(int keycode, int modifiers); - void cocoa_init_media_keys(void); void cocoa_uninit_media_keys(void); diff --git a/osdep/mac/events.m b/osdep/mac/events.m index 1c2928d89d..02b414428d 100644 --- a/osdep/mac/events.m +++ b/osdep/mac/events.m @@ -129,20 +129,9 @@ void cocoa_uninit_media_keys(void) [[EventsResponder sharedInstance] stopMediaKeys]; } -void cocoa_put_key(int keycode) -{ - [[EventsResponder sharedInstance] putKey:keycode]; -} - -void cocoa_put_key_with_modifiers(int keycode, int modifiers) -{ - keycode |= [[EventsResponder sharedInstance] mapKeyModifiers:modifiers]; - cocoa_put_key(keycode); -} - void cocoa_set_input_context(struct input_ctx *input_context) { - [[EventsResponder sharedInstance] setInputContext:input_context]; + [[EventsResponder sharedInstance].inputHelper signalWithInput:input_context]; } static void wakeup(void *context) @@ -186,36 +175,6 @@ void cocoa_init_cocoa_cb(void) return responder; } -- (void)waitForInputContext -{ - [_inputHelper wait]; -} - -- (void)setInputContext:(struct input_ctx *)ctx -{ - [_inputHelper signalWithInput:ctx]; -} - -- (void)wakeup -{ - [_inputHelper wakeup]; -} - -- (bool)queueCommand:(char *)cmd -{ - return [_inputHelper command:[NSString stringWithUTF8String:cmd]]; -} - -- (void)putKey:(int)keycode -{ - [_inputHelper putKey:keycode]; -} - -- (BOOL)useAltGr -{ - return [_inputHelper useAltGr]; -} - - (void)setIsApplication:(BOOL)isApplication { _is_application = isApplication; @@ -304,7 +263,7 @@ void cocoa_init_cocoa_cb(void) if (cocoaModifiers & NSEventModifierFlagControl) mask |= MP_KEY_MODIFIER_CTRL; if (LeftAltPressed(cocoaModifiers) || - (RightAltPressed(cocoaModifiers) && ![self useAltGr])) + (RightAltPressed(cocoaModifiers) && ![_inputHelper useAltGr])) mask |= MP_KEY_MODIFIER_ALT; if (cocoaModifiers & NSEventModifierFlagCommand) mask |= MP_KEY_MODIFIER_META; @@ -329,9 +288,9 @@ void cocoa_init_cocoa_cb(void) -(BOOL)handleMPKey:(int)key withMask:(int)mask { if (key > 0) { - cocoa_put_key(key | mask); + [_inputHelper putKey:key | mask modifiers:0]; if (mask & MP_KEY_STATE_UP) - cocoa_put_key(MP_INPUT_RELEASE_ALL); + [_inputHelper putKey:MP_INPUT_RELEASE_ALL modifiers:0]; return YES; } else { return NO; @@ -344,7 +303,7 @@ void cocoa_init_cocoa_cb(void) NSString *chars; - if ([self useAltGr] && RightAltPressed([event modifierFlags])) { + if ([_inputHelper useAltGr] && RightAltPressed([event modifierFlags])) { chars = [event characters]; } else { chars = [event charactersIgnoringModifiers]; @@ -369,9 +328,4 @@ void cocoa_init_cocoa_cb(void) return false; } -- (void)handleFilesArray:(NSArray *)files -{ - [_inputHelper openWithFiles:files]; -} - @end diff --git a/osdep/mac/events_objc.h b/osdep/mac/events_objc.h index dd1f191ac3..01c0957991 100644 --- a/osdep/mac/events_objc.h +++ b/osdep/mac/events_objc.h @@ -27,16 +27,8 @@ struct input_ctx; @interface EventsResponder : NSObject + (EventsResponder *)sharedInstance; -- (void)setInputContext:(struct input_ctx *)ctx; - (void)setIsApplication:(BOOL)isApplication; -/// Blocks until inputContext is present. -- (void)waitForInputContext; -- (void)wakeup; -- (void)putKey:(int)keycode; -- (void)handleFilesArray:(NSArray *)files; - -- (bool)queueCommand:(char *)cmd; - (bool)processKeyEvent:(NSEvent *)event; - (BOOL)handleMPKey:(int)key withMask:(int)mask; diff --git a/osdep/mac/input_helper.swift b/osdep/mac/input_helper.swift index f2e046042c..9e1b4811a9 100644 --- a/osdep/mac/input_helper.swift +++ b/osdep/mac/input_helper.swift @@ -26,10 +26,10 @@ class InputHelper: NSObject { self.mpv = mpv } - @objc func putKey(_ key: Int32) { + @objc func putKey(_ key: Int32, modifiers: NSEvent.ModifierFlags = .init(rawValue: 0)) { lock.withLock { guard let input = input else { return } - mp_input_put_key(input, key) + mp_input_put_key(input, key | mapModifier(modifiers)) } } diff --git a/osdep/mac/menu_bar.swift b/osdep/mac/menu_bar.swift index 2a906eb95a..631007a074 100644 --- a/osdep/mac/menu_bar.swift +++ b/osdep/mac/menu_bar.swift @@ -318,9 +318,7 @@ class MenuBar: NSObject { @objc func quit(_ menuItem: MenuItem) { guard let menuConfig = menuItem.config else { return } - menuConfig.command.withCString { - (NSApp as? Application)?.stopMPV(UnsafeMutablePointer(mutating: $0)) - } + EventsResponder.sharedInstance().inputHelper.command(menuConfig.command) } @objc func openFiles() { @@ -329,7 +327,7 @@ class MenuBar: NSObject { panel.canChooseDirectories = true if panel.runModal() == .OK { - (NSApp as? Application)?.openFiles(panel.urls.map { $0.path }) + EventsResponder.sharedInstance().inputHelper.open(files: panel.urls.map { $0.path }) } } @@ -337,9 +335,7 @@ class MenuBar: NSObject { let panel = NSOpenPanel() if panel.runModal() == .OK, let url = panel.urls.first { - "loadlist \"\(url.path)\"".withCString { - EventsResponder.sharedInstance().queueCommand(UnsafeMutablePointer(mutating: $0)) - } + EventsResponder.sharedInstance().inputHelper.command("loadlist \"\(url.path)\"") } } @@ -359,15 +355,13 @@ class MenuBar: NSObject { } if alert.runModal() == .alertFirstButtonReturn && input.stringValue.count > 0 { - (NSApp as? Application)?.openFiles([input.stringValue]) + EventsResponder.sharedInstance().inputHelper.open(files: [input.stringValue]) } } @objc func command(_ menuItem: MenuItem) { guard let menuConfig = menuItem.config else { return } - menuConfig.command.withCString { - EventsResponder.sharedInstance().queueCommand(UnsafeMutablePointer(mutating: $0)) - } + EventsResponder.sharedInstance().inputHelper.command(menuConfig.command) } @objc func url(_ menuItem: MenuItem) { diff --git a/osdep/mac/remote_command_center.swift b/osdep/mac/remote_command_center.swift index f6613a1f9a..0bfced2422 100644 --- a/osdep/mac/remote_command_center.swift +++ b/osdep/mac/remote_command_center.swift @@ -165,11 +165,8 @@ class RemoteCommandCenter: NSObject { return .commandFailed } - let success = String(format: "seek %.02f absolute", posEvent.positionTime).withCString { - EventsResponder.sharedInstance().queueCommand(UnsafeMutablePointer(mutating: $0)) - } - - return success ? .success : .commandFailed + let cmd = String(format: "seek %.02f absolute", posEvent.positionTime) + return EventsResponder.sharedInstance().inputHelper.command(cmd) ? .success : .commandFailed } @objc func processEvent(_ event: UnsafeMutablePointer) { diff --git a/osdep/mac/touchbar.h b/osdep/mac/touchbar.h index 38c4672a9b..4cd711ffeb 100644 --- a/osdep/mac/touchbar.h +++ b/osdep/mac/touchbar.h @@ -16,7 +16,6 @@ */ #import -#import "osdep/mac/application_objc.h" #define BASE_ID @"io.mpv.touchbar" static NSTouchBarCustomizationIdentifier customID = BASE_ID; @@ -37,7 +36,6 @@ struct mpv_event; -(void)processEvent:(struct mpv_event *)event; -@property(nonatomic, retain) Application *app; @property(nonatomic, retain) NSDictionary *touchbarItems; @property(nonatomic, assign) double duration; @property(nonatomic, assign) double position; diff --git a/osdep/mac/touchbar.m b/osdep/mac/touchbar.m index ce92c6cfc9..18fe896428 100644 --- a/osdep/mac/touchbar.m +++ b/osdep/mac/touchbar.m @@ -17,10 +17,16 @@ #include "player/client.h" #import "osdep/mac/touchbar.h" +#import "osdep/mac/events_objc.h" + +#include "config.h" + +#if HAVE_SWIFT +#include "osdep/mac/swift.h" +#endif @implementation TouchBar -@synthesize app = _app; @synthesize touchbarItems = _touchbar_items; @synthesize duration = _duration; @synthesize position = _position; @@ -231,7 +237,7 @@ - (void)buttonAction:(NSButton *)sender { NSString *identifier = [self getIdentifierFromView:sender]; - [self.app queueCommand:(char *)[self.touchbarItems[identifier][@"cmd"] UTF8String]]; + [[EventsResponder sharedInstance].inputHelper command:self.touchbarItems[identifier][@"cmd"]]; } - (void)seekbarChanged:(NSSlider *)slider @@ -239,7 +245,7 @@ NSString *identifier = [self getIdentifierFromView:slider]; NSString *seek = [NSString stringWithFormat: self.touchbarItems[identifier][@"cmd"], slider.doubleValue]; - [self.app queueCommand:(char *)[seek UTF8String]]; + [[EventsResponder sharedInstance].inputHelper command:seek]; } - (NSString *)formatTime:(int)time diff --git a/video/out/mac/view.swift b/video/out/mac/view.swift index 433284188c..ea4a715a28 100644 --- a/video/out/mac/view.swift +++ b/video/out/mac/view.swift @@ -53,7 +53,7 @@ class View: NSView, CALayerDelegate { addTrackingArea(tracker!) if containsMouseLocation() { - cocoa_put_key_with_modifiers(SWIFT_KEY_MOUSE_LEAVE, 0) + input?.putKey(SWIFT_KEY_MOUSE_LEAVE) } } @@ -118,14 +118,14 @@ class View: NSView, CALayerDelegate { override func mouseEntered(with event: NSEvent) { if input?.mouseEnabled() ?? true { - cocoa_put_key_with_modifiers(SWIFT_KEY_MOUSE_ENTER, 0) + input?.putKey(SWIFT_KEY_MOUSE_ENTER) } common.updateCursorVisibility() } override func mouseExited(with event: NSEvent) { if input?.mouseEnabled() ?? true { - cocoa_put_key_with_modifiers(SWIFT_KEY_MOUSE_LEAVE, 0) + input?.putKey(SWIFT_KEY_MOUSE_LEAVE) } common.titleBar?.hide() common.setCursorVisibility(true) @@ -202,7 +202,7 @@ class View: NSView, CALayerDelegate { func signalMouseEvent(_ event: NSEvent, _ state: UInt32) { hasMouseDown = state == MP_KEY_STATE_DOWN let mpkey = getMpvButton(event) - cocoa_put_key_with_modifiers((mpkey | Int32(state)), Int32(event.modifierFlags.rawValue)) + input?.putKey((mpkey | Int32(state)), modifiers: event.modifierFlags) } func signalMouseMovement(_ event: NSEvent) { @@ -250,7 +250,7 @@ class View: NSView, CALayerDelegate { mpkey = deltaX > 0 ? SWIFT_WHEEL_LEFT : SWIFT_WHEEL_RIGHT } - cocoa_put_key_with_modifiers(mpkey, Int32(modifiers.rawValue)) + input?.putKey(mpkey, modifiers: modifiers) } } diff --git a/video/out/mac/window.swift b/video/out/mac/window.swift index 26c0a44d83..59b9098af0 100644 --- a/video/out/mac/window.swift +++ b/video/out/mac/window.swift @@ -556,7 +556,7 @@ class Window: NSWindow, NSWindowDelegate { } func windowShouldClose(_ sender: NSWindow) -> Bool { - cocoa_put_key(MP_KEY_CLOSE_WIN) + input?.putKey(MP_KEY_CLOSE_WIN) return false }