2024-03-23 19:09:15 +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 19:09:15 +00:00
|
|
|
class AppHub: NSObject {
|
|
|
|
@objc static let shared = AppHub()
|
|
|
|
|
|
|
|
var mpv: OpaquePointer?
|
2024-03-30 18:26:45 +00:00
|
|
|
var input: InputHelper
|
2024-03-30 19:22:53 +00:00
|
|
|
var log: LogHelper
|
2024-03-27 20:57:56 +00:00
|
|
|
var option: OptionHelper?
|
2024-03-23 21:27:21 +00:00
|
|
|
var event: EventHelper?
|
2024-03-26 19:16:42 +00:00
|
|
|
var menu: MenuBar?
|
2024-03-23 19:09:15 +00:00
|
|
|
#if HAVE_MACOS_MEDIA_PLAYER
|
|
|
|
var remote: RemoteCommandCenter?
|
|
|
|
#endif
|
2024-03-23 23:34:04 +00:00
|
|
|
#if HAVE_MACOS_TOUCHBAR
|
2024-03-30 18:26:45 +00:00
|
|
|
var touchBar: TouchBar?
|
2024-03-23 23:34:04 +00:00
|
|
|
#endif
|
2024-03-26 20:30:46 +00:00
|
|
|
#if HAVE_MACOS_COCOA_CB
|
|
|
|
var cocoaCb: CocoaCB?
|
|
|
|
#endif
|
2024-03-23 19:09:15 +00:00
|
|
|
|
2024-03-31 01:29:31 +00:00
|
|
|
let MPV_PROTOCOL: String = "mpv://"
|
2024-04-27 20:11:33 +00:00
|
|
|
var isApplication: Bool { return NSApp is Application }
|
2024-04-03 18:12:31 +00:00
|
|
|
var openEvents: Int = 0
|
2024-03-23 19:09:15 +00:00
|
|
|
|
|
|
|
private override init() {
|
|
|
|
input = InputHelper()
|
2024-03-30 19:22:53 +00:00
|
|
|
log = LogHelper()
|
2024-03-23 23:04:53 +00:00
|
|
|
super.init()
|
|
|
|
#if HAVE_MACOS_MEDIA_PLAYER
|
|
|
|
remote = RemoteCommandCenter(self)
|
|
|
|
#endif
|
2024-03-31 23:30:48 +00:00
|
|
|
log.verbose("AppHub initialised")
|
2024-03-23 19:09:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func initMpv(_ mpv: OpaquePointer) {
|
2024-03-24 13:11:13 +00:00
|
|
|
event = EventHelper(self, mpv)
|
2024-04-01 12:47:46 +00:00
|
|
|
if let mpv = event?.mpv {
|
|
|
|
self.mpv = mpv
|
2024-04-10 17:41:38 +00:00
|
|
|
log.log = mp_log_new(nil, mp_client_get_log(mpv), "app")
|
2024-04-01 12:47:46 +00:00
|
|
|
option = OptionHelper(UnsafeMutablePointer(mpv), mp_client_get_global(mpv))
|
|
|
|
input.option = option
|
2024-06-22 11:38:32 +00:00
|
|
|
DispatchQueue.main.sync { menu = MenuBar(self) }
|
2024-04-01 12:47:46 +00:00
|
|
|
}
|
2024-03-24 13:11:13 +00:00
|
|
|
|
2024-03-23 23:04:53 +00:00
|
|
|
#if HAVE_MACOS_MEDIA_PLAYER
|
|
|
|
remote?.registerEvents()
|
2024-03-23 23:34:04 +00:00
|
|
|
#endif
|
|
|
|
#if HAVE_MACOS_TOUCHBAR
|
2024-03-23 23:50:41 +00:00
|
|
|
touchBar = TouchBar(self)
|
2024-03-23 23:04:53 +00:00
|
|
|
#endif
|
2024-03-31 23:30:48 +00:00
|
|
|
log.verbose("AppHub functionality initialised")
|
2024-03-23 19:09:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func initInput(_ input: OpaquePointer?) {
|
2024-03-31 23:30:48 +00:00
|
|
|
log.verbose("Initialising Input")
|
2024-03-23 19:09:15 +00:00
|
|
|
self.input.signal(input: input)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func initCocoaCb() {
|
2024-03-26 20:30:46 +00:00
|
|
|
#if HAVE_MACOS_COCOA_CB
|
|
|
|
if !isApplication { return }
|
2024-03-31 23:30:48 +00:00
|
|
|
log.verbose("Initialising CocoaCB")
|
2024-03-26 20:30:46 +00:00
|
|
|
DispatchQueue.main.sync {
|
|
|
|
self.cocoaCb = self.cocoaCb ?? CocoaCB(mpv_create_client(mpv, "cocoacb"))
|
|
|
|
}
|
|
|
|
#endif
|
2024-03-23 19:09:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func startRemote() {
|
|
|
|
#if HAVE_MACOS_MEDIA_PLAYER
|
2024-03-31 23:30:48 +00:00
|
|
|
log.verbose("Starting RemoteCommandCenter")
|
2024-03-23 19:09:15 +00:00
|
|
|
remote?.start()
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func stopRemote() {
|
|
|
|
#if HAVE_MACOS_MEDIA_PLAYER
|
2024-03-31 23:30:48 +00:00
|
|
|
log.verbose("Stoping RemoteCommandCenter")
|
2024-03-23 19:09:15 +00:00
|
|
|
remote?.stop()
|
|
|
|
#endif
|
|
|
|
}
|
2024-03-25 23:06:42 +00:00
|
|
|
|
2024-03-31 01:29:31 +00:00
|
|
|
func open(urls: [URL]) {
|
|
|
|
let files = urls.map {
|
|
|
|
if $0.isFileURL { return $0.path }
|
|
|
|
var path = $0.absoluteString
|
|
|
|
if path.hasPrefix(MPV_PROTOCOL) { path.removeFirst(MPV_PROTOCOL.count) }
|
|
|
|
return path.removingPercentEncoding ?? path
|
|
|
|
}.sorted { (strL: String, strR: String) -> Bool in
|
|
|
|
return strL.localizedStandardCompare(strR) == .orderedAscending
|
|
|
|
}
|
2024-04-03 18:12:31 +00:00
|
|
|
log.verbose("\(openEvents > 0 ? "Appending" : "Opening") dropped files: \(files)")
|
|
|
|
input.open(files: files, append: openEvents > 0)
|
|
|
|
openEvents += 1
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { self.openEvents -= 1 }
|
2024-03-31 01:29:31 +00:00
|
|
|
}
|
|
|
|
|
2024-03-25 23:06:42 +00:00
|
|
|
func getIcon() -> NSImage {
|
|
|
|
guard let iconData = app_bridge_icon(), let icon = NSImage(data: iconData) else {
|
|
|
|
return NSImage(size: NSSize(width: 1, height: 1))
|
|
|
|
}
|
|
|
|
return icon
|
|
|
|
}
|
2024-03-26 18:52:37 +00:00
|
|
|
|
|
|
|
func getMacConf() -> UnsafePointer<m_sub_options>? {
|
|
|
|
return app_bridge_mac_conf()
|
|
|
|
}
|
|
|
|
|
|
|
|
func getVoConf() -> UnsafePointer<m_sub_options>? {
|
|
|
|
return app_bridge_vo_conf()
|
|
|
|
}
|
2024-03-23 19:09:15 +00:00
|
|
|
}
|