From 6846338cf251cca3d1abf1e7a18e64891ac73e10 Mon Sep 17 00:00:00 2001 From: der richter Date: Sun, 24 Mar 2024 00:50:41 +0100 Subject: mac/touchbar: use EventHelper for event handling also remove remaining old event handling. --- osdep/mac/app_hub.swift | 31 +--------------------------- osdep/mac/application.m | 7 ------- osdep/mac/application_objc.h | 1 - osdep/mac/touch_bar.swift | 49 +++++++++++++++++++------------------------- 4 files changed, 22 insertions(+), 66 deletions(-) diff --git a/osdep/mac/app_hub.swift b/osdep/mac/app_hub.swift index 1b7173e973..d5b0827be3 100644 --- a/osdep/mac/app_hub.swift +++ b/osdep/mac/app_hub.swift @@ -40,16 +40,12 @@ class AppHub: NSObject { @objc func initMpv(_ mpv: OpaquePointer) { 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) event = EventHelper(mpv) #if HAVE_MACOS_MEDIA_PLAYER remote?.registerEvents() #endif #if HAVE_MACOS_TOUCHBAR - touchBar = TouchBar() + touchBar = TouchBar(self) #endif } @@ -73,29 +69,4 @@ class AppHub: NSObject { remote?.stop() #endif } - - let wakeup: EventHelper.wakeup_cb = { ( ctx ) in - let event = unsafeBitCast(ctx, to: AppHub.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) { - if let app = NSApp as? Application { - app.processEvent(event) - } - - switch event.pointee.event_id { - case MPV_EVENT_SHUTDOWN: - mpv_destroy(mpv) - mpv = nil - default: break - } - } } diff --git a/osdep/mac/application.m b/osdep/mac/application.m index ad4b4c5dca..f13b386a10 100644 --- a/osdep/mac/application.m +++ b/osdep/mac/application.m @@ -161,13 +161,6 @@ static const char mac_icon[] = } #endif -- (void)processEvent:(struct mpv_event *)event -{ -#if HAVE_MACOS_TOUCHBAR - [(TouchBar *)self.touchBar processEvent:event]; -#endif -} - - (void)initCocoaCb:(struct mpv_handle *)ctx { #if HAVE_MACOS_COCOA_CB diff --git a/osdep/mac/application_objc.h b/osdep/mac/application_objc.h index 3417652949..4c8beb20b3 100644 --- a/osdep/mac/application_objc.h +++ b/osdep/mac/application_objc.h @@ -26,7 +26,6 @@ struct mpv_handle; @interface Application : NSApplication - (NSImage *)getMPVIcon; -- (void)processEvent:(struct mpv_event *)event; - (void)initCocoaCb:(struct mpv_handle *)ctx; + (const struct m_sub_options *)getMacConf; + (const struct m_sub_options *)getVoConf; diff --git a/osdep/mac/touch_bar.swift b/osdep/mac/touch_bar.swift index d61338eca5..60ae600c44 100644 --- a/osdep/mac/touch_bar.swift +++ b/osdep/mac/touch_bar.swift @@ -68,14 +68,17 @@ extension TouchBar { } } -class TouchBar: NSTouchBar, NSTouchBarDelegate { +class TouchBar: NSTouchBar, NSTouchBarDelegate, EventSubscriber { + unowned let appHub: AppHub + var event: EventHelper? { get { return appHub.event } } var configs: [NSTouchBarItem.Identifier:Config] = [:] var isPaused: Bool = false { didSet { updatePlayButton() } } var position: Double = 0 { didSet { updateTouchBarTimeItems() } } var duration: Double = 0 { didSet { updateTouchBarTimeItems() } } var rate: Double = 1 - override init() { + init(_ appHub: AppHub) { + self.appHub = appHub super.init() configs = [ @@ -133,10 +136,16 @@ class TouchBar: NSTouchBar, NSTouchBarDelegate { customizationAllowedItemIdentifiers = [.play, .seekBar, .previousItem, .nextItem, .previousChapter, .nextChapter, .cycleAudio, .cycleSubtitle, .currentPosition, .timeLeft] addObserver(self, forKeyPath: "visible", options: [.new], context: nil) + + event?.subscribe(self, event: .init(name: "duration", format: MPV_FORMAT_DOUBLE)) + event?.subscribe(self, event: .init(name: "time-pos", format: MPV_FORMAT_DOUBLE)) + event?.subscribe(self, event: .init(name: "speed", format: MPV_FORMAT_DOUBLE)) + event?.subscribe(self, event: .init(name: "pause", format: MPV_FORMAT_FLAG)) + event?.subscribe(self, event: .init(name: "MPV_EVENT_END_FILE")) } required init?(coder: NSCoder) { - super.init(coder: coder) + fatalError("init(coder:) has not been implemented") } func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? { @@ -266,36 +275,20 @@ class TouchBar: NSTouchBar, NSTouchBarDelegate { return nil } - @objc func processEvent(_ event: UnsafeMutablePointer) { - switch event.pointee.event_id { - case MPV_EVENT_END_FILE: + func handle(event: EventHelper.Event) { + switch event.name { + case "MPV_EVENT_END_FILE": position = 0 duration = 0 - case MPV_EVENT_PROPERTY_CHANGE: - handlePropertyChange(event) - default: - break - } - } - - func handlePropertyChange(_ event: UnsafeMutablePointer) { - let pData = OpaquePointer(event.pointee.data) - guard let property = UnsafePointer(pData)?.pointee else { return } - - switch String(cString: property.name) { - case "time-pos" where property.format == MPV_FORMAT_DOUBLE: - let newPosition = max(TypeHelper.toDouble(property.data) ?? 0, 0) + case "time-pos": + let newPosition = max(event.double ?? 0, 0) if Int((floor(newPosition) - floor(position)) / rate) != 0 { position = newPosition } - case "duration" where property.format == MPV_FORMAT_DOUBLE: - duration = TypeHelper.toDouble(property.data) ?? 0 - case "pause" where property.format == MPV_FORMAT_FLAG: - isPaused = TypeHelper.toBool(property.data) ?? false - case "speed" where property.format == MPV_FORMAT_DOUBLE: - rate = TypeHelper.toDouble(property.data) ?? 1 - default: - break + case "pause": isPaused = event.bool ?? false + case "duration": duration = event.double ?? 0 + case "speed": rate = event.double ?? 1 + default: break } } } -- cgit v1.2.3