mac/touchbar: use EventHelper for event handling

also remove remaining old event handling.
This commit is contained in:
der richter 2024-03-24 00:50:41 +01:00
parent f9618ea487
commit 6846338cf2
4 changed files with 22 additions and 66 deletions

View File

@ -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<mpv_event>) {
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
}
}
}

View File

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

View File

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

View File

@ -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<mpv_event>) {
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<mpv_event>) {
let pData = OpaquePointer(event.pointee.data)
guard let property = UnsafePointer<mpv_event_property>(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
}
}
}