2024-03-23 15:23:19 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2024-03-27 22:22:56 +00:00
|
|
|
import Cocoa
|
|
|
|
|
2024-03-23 15:23:19 +00:00
|
|
|
protocol EventSubscriber: AnyObject {
|
|
|
|
var uid: Int { get }
|
|
|
|
func handle(event: EventHelper.Event)
|
|
|
|
}
|
|
|
|
|
|
|
|
extension EventSubscriber {
|
2024-04-27 20:11:33 +00:00
|
|
|
var uid: Int { return Int(bitPattern: ObjectIdentifier(self)) }
|
2024-03-23 15:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension EventHelper {
|
2024-04-27 20:35:36 +00:00
|
|
|
typealias WakeupCallback = (@convention(c) (UnsafeMutableRawPointer?) -> Void)?
|
2024-03-23 15:23:19 +00:00
|
|
|
|
|
|
|
struct Event {
|
|
|
|
var id: String {
|
2024-04-27 20:11:33 +00:00
|
|
|
return name + (name.starts(with: "MPV_EVENT_") ? "" : String(format.rawValue))
|
2024-03-23 15:23:19 +00:00
|
|
|
}
|
|
|
|
var idReset: String {
|
2024-04-27 20:11:33 +00:00
|
|
|
return name + (name.starts(with: "MPV_EVENT_") ? "" : String(MPV_FORMAT_NONE.rawValue))
|
2024-03-23 15:23:19 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-26 20:50:35 +00:00
|
|
|
class EventHelper {
|
2024-03-24 13:11:13 +00:00
|
|
|
unowned let appHub: AppHub
|
2024-03-23 15:23:19 +00:00
|
|
|
var mpv: OpaquePointer?
|
2024-04-27 20:11:33 +00:00
|
|
|
var events: [String: [Int: EventSubscriber]] = [:]
|
2024-03-23 15:23:19 +00:00
|
|
|
|
2024-03-26 22:21:34 +00:00
|
|
|
init?(_ appHub: AppHub, _ mpv: OpaquePointer) {
|
2024-03-24 13:11:13 +00:00
|
|
|
if !appHub.isApplication {
|
2024-03-26 22:21:34 +00:00
|
|
|
mpv_destroy(mpv)
|
2024-03-24 13:11:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
self.appHub = appHub
|
2024-03-26 22:21:34 +00:00
|
|
|
self.mpv = mpv
|
|
|
|
mpv_set_wakeup_callback(mpv, wakeup, TypeHelper.bridge(obj: self))
|
2024-03-23 15:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-27 20:35:36 +00:00
|
|
|
let wakeup: WakeupCallback = { ( ctx ) in
|
2024-03-23 15:23:19 +00:00
|
|
|
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<mpv_event>) {
|
|
|
|
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<mpv_event>) {
|
|
|
|
let pData = OpaquePointer(mpvEvent.pointee.data)
|
|
|
|
guard let property = UnsafePointer<mpv_event_property>(pData)?.pointee else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let name = String(cString: property.name)
|
|
|
|
let format = property.format
|
|
|
|
for (_, subscriber) in events[name + String(format.rawValue)] ?? [:] {
|
2024-04-27 20:11:33 +00:00
|
|
|
var event: Event?
|
2024-03-23 15:23:19 +00:00
|
|
|
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) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|