From deb9d30e905b7b4645f18a26e6274a2859221631 Mon Sep 17 00:00:00 2001 From: der richter Date: Sat, 23 Mar 2024 16:23:19 +0100 Subject: [PATCH] mac/event: add helper to subscribe to mpv events and property changes preparation to remove duplicate code from all classes that implement their own observer loops. --- meson.build | 1 + osdep/mac/event_helper.swift | 139 +++++++++++++++++++++++++++++++ osdep/mac/swift_extensions.swift | 27 ++++++ 3 files changed, 167 insertions(+) create mode 100644 osdep/mac/event_helper.swift diff --git a/meson.build b/meson.build index 2625adb696..08cae3966d 100644 --- a/meson.build +++ b/meson.build @@ -1518,6 +1518,7 @@ features += {'swift': swift.allowed()} swift_sources = [] if features['cocoa'] and features['swift'] swift_sources += files('osdep/mac/libmpv_helper.swift', + 'osdep/mac/event_helper.swift', 'osdep/mac/input_helper.swift', 'osdep/mac/log_helper.swift', 'osdep/mac/menu_bar.swift', diff --git a/osdep/mac/event_helper.swift b/osdep/mac/event_helper.swift new file mode 100644 index 0000000000..f0b2bf42b1 --- /dev/null +++ b/osdep/mac/event_helper.swift @@ -0,0 +1,139 @@ +/* + * 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 . + */ + +protocol EventSubscriber: AnyObject { + var uid: Int { get } + func handle(event: EventHelper.Event) +} + +extension EventSubscriber { + var uid: Int { get { return Int(bitPattern: ObjectIdentifier(self)) }} +} + +extension EventHelper { + typealias wakeup_cb = (@convention(c) (UnsafeMutableRawPointer?) -> Void)? + + struct Event { + var id: String { + get { name + (name.starts(with: "MPV_EVENT_") ? "" : String(format.rawValue)) } + } + var idReset: String { + get { name + (name.starts(with: "MPV_EVENT_") ? "" : String(MPV_FORMAT_NONE.rawValue)) } + } + let name: String + let format: mpv_format + let string: String? + let bool: Bool? + let double: Double? + + init( + name: String = "", + format: mpv_format = MPV_FORMAT_NONE, + string: String? = nil, + bool: Bool? = nil, + double: Double? = nil + + ) { + self.name = name + self.format = format + self.string = string + self.bool = bool + self.double = double + } + } +} + +public class EventHelper: NSObject { + var mpv: OpaquePointer? + var events: [String:[Int:EventSubscriber]] = [:] + + @objc init(_ mpvHandle: OpaquePointer) { + self.mpv = mpvHandle + super.init() + mpv_set_wakeup_callback(mpvHandle, wakeup, TypeHelper.bridge(obj: self)) + } + + func subscribe(_ subscriber: any EventSubscriber, event: Event) { + guard let mpv = mpv else { return } + + if !event.name.isEmpty { + if !events.keys.contains(event.idReset) { + events[event.idReset] = [:] + } + if !events.keys.contains(event.id) { + mpv_observe_property(mpv, 0, event.name, event.format) + events[event.id] = [:] + } + events[event.idReset]?[subscriber.uid] = subscriber + events[event.id]?[subscriber.uid] = subscriber + } + } + + let wakeup: wakeup_cb = { ( ctx ) in + let event = unsafeBitCast(ctx, to: EventHelper.self) + DispatchQueue.main.async { event.eventLoop() } + } + + func eventLoop() { + while let mpv = mpv, let event = mpv_wait_event(mpv, 0) { + if event.pointee.event_id == MPV_EVENT_NONE { break } + handle(event: event) + } + } + + func handle(event: UnsafeMutablePointer) { + switch event.pointee.event_id { + case MPV_EVENT_PROPERTY_CHANGE: + handle(property: event) + default: + for (_, subscriber) in events[String(describing: event.pointee.event_id)] ?? [:] { + subscriber.handle(event: .init(name: String(describing: event.pointee.event_id))) + } + } + + if event.pointee.event_id == MPV_EVENT_SHUTDOWN { + mpv_destroy(mpv) + mpv = nil + } + } + + func handle(property mpvEvent: UnsafeMutablePointer) { + let pData = OpaquePointer(mpvEvent.pointee.data) + guard let property = UnsafePointer(pData)?.pointee else { + return + } + + let name = String(cString: property.name) + let format = property.format + for (_, subscriber) in events[name + String(format.rawValue)] ?? [:] { + var event: Event? = nil + switch format { + case MPV_FORMAT_STRING: + event = .init(name: name, format: format, string: TypeHelper.toString(property.data)) + case MPV_FORMAT_FLAG: + event = .init(name: name, format: format, bool: TypeHelper.toBool(property.data)) + case MPV_FORMAT_DOUBLE: + event = .init(name: name, format: format, double: TypeHelper.toDouble(property.data)) + case MPV_FORMAT_NONE: + event = .init(name: name, format: format) + default: break + } + + if let e = event { subscriber.handle(event: e) } + } + } +} diff --git a/osdep/mac/swift_extensions.swift b/osdep/mac/swift_extensions.swift index 99d32644cc..ed6c86cd2a 100644 --- a/osdep/mac/swift_extensions.swift +++ b/osdep/mac/swift_extensions.swift @@ -53,6 +53,33 @@ extension mp_keymap { } } +extension mpv_event_id: CustomStringConvertible { + public var description: String { + switch self { + case MPV_EVENT_NONE: return "MPV_EVENT_NONE2" + case MPV_EVENT_SHUTDOWN: return "MPV_EVENT_SHUTDOWN" + case MPV_EVENT_LOG_MESSAGE: return "MPV_EVENT_LOG_MESSAGE" + case MPV_EVENT_GET_PROPERTY_REPLY: return "MPV_EVENT_GET_PROPERTY_REPLY" + case MPV_EVENT_SET_PROPERTY_REPLY: return "MPV_EVENT_SET_PROPERTY_REPLY" + case MPV_EVENT_COMMAND_REPLY: return "MPV_EVENT_COMMAND_REPLY" + case MPV_EVENT_START_FILE: return "MPV_EVENT_START_FILE" + case MPV_EVENT_END_FILE: return "MPV_EVENT_END_FILE" + case MPV_EVENT_FILE_LOADED: return "MPV_EVENT_FILE_LOADED" + case MPV_EVENT_IDLE: return "MPV_EVENT_IDLE" + case MPV_EVENT_TICK: return "MPV_EVENT_TICK" + case MPV_EVENT_CLIENT_MESSAGE: return "MPV_EVENT_CLIENT_MESSAGE" + case MPV_EVENT_VIDEO_RECONFIG: return "MPV_EVENT_VIDEO_RECONFIG" + case MPV_EVENT_AUDIO_RECONFIG: return "MPV_EVENT_AUDIO_RECONFIG" + case MPV_EVENT_SEEK: return "MPV_EVENT_SEEK" + case MPV_EVENT_PLAYBACK_RESTART: return "MPV_EVENT_PLAYBACK_RESTART" + case MPV_EVENT_PROPERTY_CHANGE: return "MPV_EVENT_PROPERTY_CHANGE" + case MPV_EVENT_QUEUE_OVERFLOW: return "MPV_EVENT_QUEUE_OVERFLOW" + case MPV_EVENT_HOOK: return "MPV_EVENT_HOOK" + default: return "MPV_EVENT_" + String(self.rawValue) + } + } +} + extension Bool { init(_ int32: Int32) { self.init(int32 != 0)