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.
This commit is contained in:
der richter 2024-03-09 15:48:54 +01:00
parent 5dd2d19519
commit f3e5fea4f5
4 changed files with 80 additions and 31 deletions

View File

@ -1513,6 +1513,7 @@ features += {'swift': swift.allowed()}
swift_sources = []
if features['cocoa'] and features['swift']
swift_sources += files('osdep/mac/libmpv_helper.swift',
'osdep/mac/input_helper.swift',
'osdep/mac/log_helper.swift',
'osdep/mac/mpv_helper.swift',
'osdep/mac/menu_bar.swift',

View File

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

View File

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

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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<Int8>(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() } }
}
}