From 5ab30609616aee2c8e8a03addb05bd0914940a68 Mon Sep 17 00:00:00 2001 From: der richter Date: Sat, 23 Mar 2024 22:27:21 +0100 Subject: [PATCH] cocoa-cb: use EventHelper for event handling --- osdep/mac/app_hub.swift | 26 +++++++++++--------------- osdep/mac/application.m | 3 --- video/out/cocoa_cb_common.swift | 12 ++++-------- 3 files changed, 15 insertions(+), 26 deletions(-) diff --git a/osdep/mac/app_hub.swift b/osdep/mac/app_hub.swift index d95faf733f..6166ea4c94 100644 --- a/osdep/mac/app_hub.swift +++ b/osdep/mac/app_hub.swift @@ -20,6 +20,7 @@ class AppHub: NSObject { var mpv: OpaquePointer? @objc var input: InputHelper + var event: EventHelper? #if HAVE_MACOS_MEDIA_PLAYER var remote: RemoteCommandCenter? #endif @@ -31,21 +32,16 @@ class AppHub: NSObject { } @objc func initMpv(_ mpv: OpaquePointer) { - if isApplication { - self.mpv = mpv - mpv_observe_property(mpv, 0, "duration", MPV_FORMAT_DOUBLE) - mpv_observe_property(mpv, 0, "time-pos", MPV_FORMAT_DOUBLE) - mpv_observe_property(mpv, 0, "speed", MPV_FORMAT_DOUBLE) - mpv_observe_property(mpv, 0, "pause", MPV_FORMAT_FLAG) - mpv_observe_property(mpv, 0, "media-title", MPV_FORMAT_STRING) - mpv_observe_property(mpv, 0, "chapter-metadata/title", MPV_FORMAT_STRING) - mpv_observe_property(mpv, 0, "metadata/by-key/album", MPV_FORMAT_STRING) - mpv_observe_property(mpv, 0, "metadata/by-key/artist", MPV_FORMAT_STRING) - mpv_set_wakeup_callback(mpv, wakeup, TypeHelper.bridge(obj: self)) - return - } - - mpv_destroy(mpv) + self.mpv = mpv + mpv_observe_property(mpv, 0, "duration", MPV_FORMAT_DOUBLE) + mpv_observe_property(mpv, 0, "time-pos", MPV_FORMAT_DOUBLE) + mpv_observe_property(mpv, 0, "speed", MPV_FORMAT_DOUBLE) + mpv_observe_property(mpv, 0, "pause", MPV_FORMAT_FLAG) + mpv_observe_property(mpv, 0, "media-title", MPV_FORMAT_STRING) + mpv_observe_property(mpv, 0, "chapter-metadata/title", MPV_FORMAT_STRING) + mpv_observe_property(mpv, 0, "metadata/by-key/album", MPV_FORMAT_STRING) + mpv_observe_property(mpv, 0, "metadata/by-key/artist", MPV_FORMAT_STRING) + event = EventHelper(mpv) } @objc func initInput(_ input: OpaquePointer?) { diff --git a/osdep/mac/application.m b/osdep/mac/application.m index 439a109b7e..946c276956 100644 --- a/osdep/mac/application.m +++ b/osdep/mac/application.m @@ -166,9 +166,6 @@ static const char mac_icon[] = #if HAVE_MACOS_TOUCHBAR [(TouchBar *)self.touchBar processEvent:event]; #endif - if (_cocoa_cb) { - [_cocoa_cb processEvent:event]; - } } - (void)initCocoaCb:(struct mpv_handle *)ctx diff --git a/video/out/cocoa_cb_common.swift b/video/out/cocoa_cb_common.swift index 7ac0f35096..3426ca6cf8 100644 --- a/video/out/cocoa_cb_common.swift +++ b/video/out/cocoa_cb_common.swift @@ -17,7 +17,7 @@ import Cocoa -class CocoaCB: Common { +class CocoaCB: Common, EventSubscriber { var libmpv: LibmpvHelper var layer: GLLayer? @@ -37,6 +37,7 @@ class CocoaCB: Common { libmpv = LibmpvHelper(mpvHandle, newlog) super.init(option, newlog) layer = GLLayer(cocoaCB: self) + AppHub.shared.event?.subscribe(self, event: .init(name: "MPV_EVENT_SHUTDOWN")) } func preinit(_ vo: UnsafeMutablePointer) { @@ -221,12 +222,7 @@ class CocoaCB: Common { } } - @objc func processEvent(_ event: UnsafePointer) { - switch event.pointee.event_id { - case MPV_EVENT_SHUTDOWN: - shutdown() - default: - break - } + func handle(event: EventHelper.Event) { + if event.name == String(describing: MPV_EVENT_SHUTDOWN) { shutdown() } } }