summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorder richter <der.richter@gmx.de>2024-03-09 15:48:54 +0100
committerder richter <der.richter@gmx.de>2024-03-14 23:33:15 +0100
commitf3e5fea4f55e21f2f29874552a547fd5c75e629d (patch)
tree6134423c6732278fc4e4fc7f1e2c277e4ead1f3e
parent5dd2d19519a31998f2bea5c697a11d8c547b1e70 (diff)
downloadmpv-f3e5fea4f55e21f2f29874552a547fd5c75e629d.tar.bz2
mpv-f3e5fea4f55e21f2f29874552a547fd5c75e629d.tar.xz
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.
-rw-r--r--meson.build1
-rw-r--r--osdep/mac/events.m39
-rw-r--r--osdep/mac/events_objc.h2
-rw-r--r--osdep/mac/input_helper.swift69
4 files changed, 80 insertions, 31 deletions
diff --git a/meson.build b/meson.build
index f458b1fa66..159d1feebc 100644
--- a/meson.build
+++ b/meson.build
@@ -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',
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 <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() } }
+ }
+}