summaryrefslogtreecommitdiffstats
path: root/osdep/mac/event_helper.swift
blob: 277a9aa08d06302c5c62dc6e9f47457865c30a82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * 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/>.
 */

import Cocoa

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

class EventHelper {
    unowned let appHub: AppHub
    var mpv: OpaquePointer?
    var events: [String:[Int:EventSubscriber]] = [:]

    init?(_ appHub: AppHub, _ mpv: OpaquePointer) {
        if !appHub.isApplication {
            mpv_destroy(mpv)
            return nil
        }

        self.appHub = appHub
        self.mpv = mpv
        mpv_set_wakeup_callback(mpv, 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<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)] ?? [:] {
            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) }
        }
    }
}