From f3e5fea4f55e21f2f29874552a547fd5c75e629d Mon Sep 17 00:00:00 2001 From: der richter Date: Sat, 9 Mar 2024 15:48:54 +0100 Subject: mac/events: move input ctx related functionality into new input helper preparation for mac/events cleanup and single responsibility principle. all functions are thread safe. --- osdep/mac/events.m | 39 +++++-------------------- osdep/mac/events_objc.h | 2 ++ osdep/mac/input_helper.swift | 69 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 31 deletions(-) create mode 100644 osdep/mac/input_helper.swift (limited to 'osdep') diff --git a/osdep/mac/events.m b/osdep/mac/events.m index 50d9242ca6..f7e7bb6ba3 100644 --- a/osdep/mac/events.m +++ b/osdep/mac/events.m @@ -175,6 +175,7 @@ void cocoa_init_cocoa_cb(void) @implementation EventsResponder @synthesize remoteCommandCenter = _remoteCommandCenter; +@synthesize inputHelper = _inputHelper; + (EventsResponder *)sharedInstance { @@ -182,6 +183,7 @@ void cocoa_init_cocoa_cb(void) static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ responder = [EventsResponder new]; + responder.inputHelper = [[InputHelper alloc] init: nil]; }); return responder; } @@ -197,57 +199,32 @@ void cocoa_init_cocoa_cb(void) - (void)waitForInputContext { - [_input_lock lock]; - while (!_inputContext) - [_input_lock wait]; - [_input_lock unlock]; + [_inputHelper wait]; } - (void)setInputContext:(struct input_ctx *)ctx { - [_input_lock lock]; - _inputContext = ctx; - [_input_lock signal]; - [_input_lock unlock]; + [_inputHelper signalWithInput:ctx]; } - (void)wakeup { - [_input_lock lock]; - if (_inputContext) - mp_input_wakeup(_inputContext); - [_input_lock unlock]; + [_inputHelper wakeup]; } - (bool)queueCommand:(char *)cmd { - bool r = false; - [_input_lock lock]; - if (_inputContext) { - mp_cmd_t *cmdt = mp_input_parse_cmd(_inputContext, bstr0(cmd), ""); - mp_input_queue_cmd(_inputContext, cmdt); - r = true; - } - [_input_lock unlock]; - return r; + return [_inputHelper command:[NSString stringWithUTF8String:cmd]]; } - (void)putKey:(int)keycode { - [_input_lock lock]; - if (_inputContext) - mp_input_put_key(_inputContext, keycode); - [_input_lock unlock]; + [_inputHelper putKey:keycode]; } - (BOOL)useAltGr { - BOOL r = YES; - [_input_lock lock]; - if (_inputContext) - r = mp_input_use_alt_gr(_inputContext); - [_input_lock unlock]; - return r; + return [_inputHelper useAltGr]; } - (void)setIsApplication:(BOOL)isApplication diff --git a/osdep/mac/events_objc.h b/osdep/mac/events_objc.h index 263f1ed118..dd1f191ac3 100644 --- a/osdep/mac/events_objc.h +++ b/osdep/mac/events_objc.h @@ -21,6 +21,7 @@ #include "osdep/mac/events.h" @class RemoteCommandCenter; +@class InputHelper; struct input_ctx; @interface EventsResponder : NSObject @@ -41,5 +42,6 @@ struct input_ctx; - (BOOL)handleMPKey:(int)key withMask:(int)mask; @property(nonatomic, retain) RemoteCommandCenter *remoteCommandCenter; +@property(nonatomic, retain) InputHelper *inputHelper; @end diff --git a/osdep/mac/input_helper.swift b/osdep/mac/input_helper.swift new file mode 100644 index 0000000000..396205fa77 --- /dev/null +++ b/osdep/mac/input_helper.swift @@ -0,0 +1,69 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see . + */ + +class InputHelper: NSObject { + var lock = NSCondition() + private var input: OpaquePointer? + + @objc init(_ input: OpaquePointer? = nil) { + super.init() + self.input = input + } + + @objc func command(_ cmd: String) -> Bool { + lock.withLock { + guard let input = input else { return false } + let cCmd = UnsafePointer(strdup(cmd)) + let mpvCmd = mp_input_parse_cmd(input, bstr0(cCmd), "") + mp_input_queue_cmd(input, mpvCmd) + free(UnsafeMutablePointer(mutating: cCmd)) + return true + } + } + + @objc func putKey(_ key: Int32) { + lock.withLock { + guard let input = input else { return } + mp_input_put_key(input, key) + } + } + + @objc func useAltGr() -> Bool { + lock.withLock { + guard let input = input else { return false } + return mp_input_use_alt_gr(input) + } + } + + @objc func wakeup() { + lock.withLock { + guard let input = input else { return } + mp_input_wakeup(input) + } + } + + @objc func signal(input: OpaquePointer? = nil) { + lock.withLock { + self.input = input + if input != nil { lock.signal() } + } + } + + @objc func wait() { + lock.withLock { while input == nil { lock.wait() } } + } +} -- cgit v1.2.3