1
0
mirror of https://github.com/mpv-player/mpv synced 2025-03-20 10:17:31 +00:00

mac/events: remove redundant functions and optimise input helper usage

some redundant functions that jump through hoops.
This commit is contained in:
der richter 2024-03-10 00:53:44 +01:00
parent 055e9cd93e
commit 18fb71498b
12 changed files with 35 additions and 115 deletions

View File

@ -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

View File

@ -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;

View File

@ -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);

View File

@ -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

View File

@ -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;

View File

@ -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))
}
}

View File

@ -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<CChar>(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<CChar>(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<CChar>(mutating: $0))
}
EventsResponder.sharedInstance().inputHelper.command(menuConfig.command)
}
@objc func url(_ menuItem: MenuItem) {

View File

@ -165,11 +165,8 @@ class RemoteCommandCenter: NSObject {
return .commandFailed
}
let success = String(format: "seek %.02f absolute", posEvent.positionTime).withCString {
EventsResponder.sharedInstance().queueCommand(UnsafeMutablePointer<Int8>(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<mpv_event>) {

View File

@ -16,7 +16,6 @@
*/
#import <Cocoa/Cocoa.h>
#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;

View File

@ -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

View File

@ -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)
}
}

View File

@ -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
}