2019-11-30 15:08:32 +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
|
2019-11-30 15:08:32 +00:00
|
|
|
import MediaPlayer
|
|
|
|
|
2024-02-06 20:15:56 +00:00
|
|
|
extension RemoteCommandCenter {
|
2024-02-06 21:46:58 +00:00
|
|
|
typealias ConfigHandler = (MPRemoteCommandEvent) -> (MPRemoteCommandHandlerStatus)
|
|
|
|
|
2019-11-30 15:08:32 +00:00
|
|
|
enum KeyType {
|
|
|
|
case normal
|
|
|
|
case repeatable
|
|
|
|
}
|
|
|
|
|
2024-02-06 20:04:37 +00:00
|
|
|
struct Config {
|
|
|
|
let key: Int32
|
|
|
|
let type: KeyType
|
2024-04-03 18:48:21 +00:00
|
|
|
var state: NSEvent.EventType = .applicationDefined
|
2024-02-06 21:46:58 +00:00
|
|
|
let handler: ConfigHandler
|
2024-02-06 20:04:37 +00:00
|
|
|
|
2024-04-27 20:11:33 +00:00
|
|
|
init(key: Int32 = 0, type: KeyType = .normal, handler: @escaping ConfigHandler = { _ in return .commandFailed }) {
|
2024-02-06 20:04:37 +00:00
|
|
|
self.key = key
|
|
|
|
self.type = type
|
2024-02-06 21:46:58 +00:00
|
|
|
self.handler = handler
|
2024-02-06 20:04:37 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-06 20:15:56 +00:00
|
|
|
}
|
2024-02-06 20:04:37 +00:00
|
|
|
|
2024-03-23 23:04:53 +00:00
|
|
|
class RemoteCommandCenter: EventSubscriber {
|
|
|
|
unowned let appHub: AppHub
|
2024-04-27 20:11:33 +00:00
|
|
|
var event: EventHelper? { return appHub.event }
|
|
|
|
var input: InputHelper { return appHub.input }
|
|
|
|
var configs: [MPRemoteCommand: Config] = [:]
|
2024-02-06 20:53:08 +00:00
|
|
|
var disabledCommands: [MPRemoteCommand] = []
|
2024-02-07 23:29:37 +00:00
|
|
|
var isPaused: Bool = false { didSet { updateInfoCenter() } }
|
|
|
|
var duration: Double = 0 { didSet { updateInfoCenter() } }
|
|
|
|
var position: Double = 0 { didSet { updateInfoCenter() } }
|
2024-03-23 22:57:03 +00:00
|
|
|
var rate: Double = 1 { didSet { updateInfoCenter() } }
|
2024-02-09 20:00:37 +00:00
|
|
|
var title: String = "" { didSet { updateInfoCenter() } }
|
2024-02-13 20:47:21 +00:00
|
|
|
var chapter: String? { didSet { updateInfoCenter() } }
|
|
|
|
var album: String? { didSet { updateInfoCenter() } }
|
|
|
|
var artist: String? { didSet { updateInfoCenter() } }
|
2024-10-17 12:22:42 +00:00
|
|
|
var path: String?
|
|
|
|
var coverPath: String?
|
|
|
|
var cover: NSImage? { didSet { updateInfoCenter() } }
|
|
|
|
var defaultCover: NSImage
|
2019-11-30 15:08:32 +00:00
|
|
|
|
2024-10-17 12:22:42 +00:00
|
|
|
let queue: DispatchQueue = DispatchQueue(label: "io.mpv.remote.queue")
|
2024-04-27 20:11:33 +00:00
|
|
|
var infoCenter: MPNowPlayingInfoCenter { return MPNowPlayingInfoCenter.default() }
|
|
|
|
var commandCenter: MPRemoteCommandCenter { return MPRemoteCommandCenter.shared() }
|
2024-02-06 20:53:08 +00:00
|
|
|
|
2024-03-23 23:04:53 +00:00
|
|
|
init(_ appHub: AppHub) {
|
|
|
|
self.appHub = appHub
|
2024-10-17 12:22:42 +00:00
|
|
|
defaultCover = appHub.getIcon()
|
2019-11-30 15:08:32 +00:00
|
|
|
|
2024-02-06 20:53:08 +00:00
|
|
|
configs = [
|
2024-02-06 21:46:58 +00:00
|
|
|
commandCenter.pauseCommand: Config(key: MP_KEY_PAUSEONLY, handler: keyHandler),
|
|
|
|
commandCenter.playCommand: Config(key: MP_KEY_PLAYONLY, handler: keyHandler),
|
|
|
|
commandCenter.stopCommand: Config(key: MP_KEY_STOP, handler: keyHandler),
|
|
|
|
commandCenter.nextTrackCommand: Config(key: MP_KEY_NEXT, handler: keyHandler),
|
|
|
|
commandCenter.previousTrackCommand: Config(key: MP_KEY_PREV, handler: keyHandler),
|
|
|
|
commandCenter.togglePlayPauseCommand: Config(key: MP_KEY_PLAY, handler: keyHandler),
|
|
|
|
commandCenter.seekForwardCommand: Config(key: MP_KEY_FORWARD, type: .repeatable, handler: keyHandler),
|
2024-02-07 23:29:37 +00:00
|
|
|
commandCenter.seekBackwardCommand: Config(key: MP_KEY_REWIND, type: .repeatable, handler: keyHandler),
|
2024-04-27 20:11:33 +00:00
|
|
|
commandCenter.changePlaybackPositionCommand: Config(handler: seekHandler)
|
2024-02-06 20:53:08 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
disabledCommands = [
|
|
|
|
commandCenter.changePlaybackRateCommand,
|
|
|
|
commandCenter.changeRepeatModeCommand,
|
|
|
|
commandCenter.changeShuffleModeCommand,
|
|
|
|
commandCenter.skipForwardCommand,
|
|
|
|
commandCenter.skipBackwardCommand,
|
|
|
|
commandCenter.enableLanguageOptionCommand,
|
|
|
|
commandCenter.disableLanguageOptionCommand,
|
|
|
|
commandCenter.ratingCommand,
|
|
|
|
commandCenter.likeCommand,
|
|
|
|
commandCenter.dislikeCommand,
|
2024-04-27 20:11:33 +00:00
|
|
|
commandCenter.bookmarkCommand
|
2024-02-06 20:53:08 +00:00
|
|
|
]
|
|
|
|
|
2019-11-30 15:08:32 +00:00
|
|
|
for cmd in disabledCommands {
|
|
|
|
cmd.isEnabled = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-23 23:04:53 +00:00
|
|
|
func registerEvents() {
|
|
|
|
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: "media-title", format: MPV_FORMAT_STRING))
|
|
|
|
event?.subscribe(self, event: .init(name: "chapter-metadata/title", format: MPV_FORMAT_STRING))
|
|
|
|
event?.subscribe(self, event: .init(name: "metadata/by-key/album", format: MPV_FORMAT_STRING))
|
|
|
|
event?.subscribe(self, event: .init(name: "metadata/by-key/artist", format: MPV_FORMAT_STRING))
|
2024-10-17 12:22:42 +00:00
|
|
|
event?.subscribe(self, event: .init(name: "path", format: MPV_FORMAT_STRING))
|
|
|
|
event?.subscribe(self, event: .init(name: "track-list", format: MPV_FORMAT_NODE))
|
2024-03-23 23:04:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func start() {
|
2024-02-06 21:46:58 +00:00
|
|
|
for (cmd, config) in configs {
|
2019-11-30 15:08:32 +00:00
|
|
|
cmd.isEnabled = true
|
2024-02-06 21:46:58 +00:00
|
|
|
cmd.addTarget(handler: config.handler)
|
2019-11-30 15:08:32 +00:00
|
|
|
}
|
|
|
|
|
2024-02-07 23:29:37 +00:00
|
|
|
updateInfoCenter()
|
2020-02-15 15:13:37 +00:00
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(
|
|
|
|
self,
|
|
|
|
selector: #selector(self.makeCurrent),
|
|
|
|
name: NSApplication.willBecomeActiveNotification,
|
|
|
|
object: nil
|
|
|
|
)
|
2019-11-30 15:08:32 +00:00
|
|
|
}
|
|
|
|
|
2024-03-23 23:04:53 +00:00
|
|
|
func stop() {
|
2024-02-06 20:04:37 +00:00
|
|
|
for (cmd, _) in configs {
|
2019-11-30 15:08:32 +00:00
|
|
|
cmd.isEnabled = false
|
|
|
|
cmd.removeTarget(nil)
|
|
|
|
}
|
|
|
|
|
2024-02-06 20:53:08 +00:00
|
|
|
infoCenter.nowPlayingInfo = nil
|
|
|
|
infoCenter.playbackState = .unknown
|
2024-02-07 22:47:10 +00:00
|
|
|
|
|
|
|
NotificationCenter.default.removeObserver(
|
|
|
|
self,
|
|
|
|
name: NSApplication.willBecomeActiveNotification,
|
|
|
|
object: nil
|
|
|
|
)
|
2020-01-21 18:14:00 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 15:13:37 +00:00
|
|
|
@objc func makeCurrent(notification: NSNotification) {
|
2024-02-06 20:53:08 +00:00
|
|
|
infoCenter.playbackState = .paused
|
|
|
|
infoCenter.playbackState = .playing
|
2024-02-07 23:29:37 +00:00
|
|
|
updateInfoCenter()
|
2020-01-21 18:14:00 +00:00
|
|
|
}
|
|
|
|
|
2024-02-07 23:29:37 +00:00
|
|
|
func updateInfoCenter() {
|
2024-10-17 12:22:42 +00:00
|
|
|
let cover = cover ?? defaultCover
|
2024-02-13 21:23:06 +00:00
|
|
|
infoCenter.playbackState = isPaused ? .paused : .playing
|
|
|
|
infoCenter.nowPlayingInfo = (infoCenter.nowPlayingInfo ?? [:]).merging([
|
|
|
|
MPNowPlayingInfoPropertyMediaType: NSNumber(value: MPNowPlayingInfoMediaType.video.rawValue),
|
|
|
|
MPNowPlayingInfoPropertyPlaybackProgress: NSNumber(value: 0.0),
|
2024-02-08 00:05:25 +00:00
|
|
|
MPNowPlayingInfoPropertyPlaybackRate: NSNumber(value: isPaused ? 0 : rate),
|
2024-02-07 23:29:37 +00:00
|
|
|
MPNowPlayingInfoPropertyElapsedPlaybackTime: NSNumber(value: position),
|
|
|
|
MPMediaItemPropertyPlaybackDuration: NSNumber(value: duration),
|
2024-02-09 20:00:37 +00:00
|
|
|
MPMediaItemPropertyTitle: title,
|
2024-02-13 20:47:21 +00:00
|
|
|
MPMediaItemPropertyArtist: artist ?? chapter ?? "",
|
|
|
|
MPMediaItemPropertyAlbumTitle: album ?? "",
|
2024-10-17 12:22:42 +00:00
|
|
|
MPMediaItemPropertyArtwork: MPMediaItemArtwork(boundsSize: cover.size) { _ in return cover }
|
2024-02-07 23:29:37 +00:00
|
|
|
]) { (_, new) in new }
|
2019-11-30 15:08:32 +00:00
|
|
|
}
|
|
|
|
|
2024-10-17 12:22:42 +00:00
|
|
|
func updateCover(tracks: [Any?]) {
|
|
|
|
var imageCoverPath: String?
|
|
|
|
var externalCoverPath: String?
|
|
|
|
|
|
|
|
for item in tracks {
|
|
|
|
guard let track = item as? [String: Any?] else { continue }
|
|
|
|
if (track["image"] as? Bool) == true {
|
|
|
|
// opened file is an image
|
|
|
|
if track["external"] as? Bool == false && track["albumart"] as? Bool == false && imageCoverPath == nil {
|
|
|
|
imageCoverPath = path
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// external cover
|
|
|
|
if let filename = track["external-filename"] as? String, externalCoverPath == nil {
|
|
|
|
externalCoverPath = filename
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// read cover image on separate thread
|
|
|
|
queue.async {
|
|
|
|
guard let path = imageCoverPath ?? externalCoverPath else {
|
|
|
|
self.cover = nil
|
|
|
|
self.coverPath = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if self.coverPath == path { return }
|
|
|
|
|
|
|
|
var image = NSImage(contentsOf: URL(fileURLWithPath: path))
|
|
|
|
if let url = URL(string: path), image == nil {
|
|
|
|
image = NSImage(contentsOf: url)
|
|
|
|
}
|
|
|
|
self.cover = image
|
|
|
|
self.coverPath = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-06 21:46:58 +00:00
|
|
|
lazy var keyHandler: ConfigHandler = { event in
|
|
|
|
guard let config = self.configs[event.command] else {
|
2019-11-30 15:08:32 +00:00
|
|
|
return .commandFailed
|
|
|
|
}
|
|
|
|
|
2024-02-06 20:04:37 +00:00
|
|
|
var state = config.state
|
|
|
|
if config.type == .repeatable {
|
2024-04-03 18:48:21 +00:00
|
|
|
state = config.state == .keyDown ? .keyUp : .keyDown
|
2024-02-06 21:43:04 +00:00
|
|
|
self.configs[event.command]?.state = state
|
2019-11-30 15:08:32 +00:00
|
|
|
}
|
2024-04-03 18:48:21 +00:00
|
|
|
self.input.put(key: config.key, type: state)
|
2019-11-30 15:08:32 +00:00
|
|
|
|
|
|
|
return .success
|
|
|
|
}
|
2020-01-21 18:14:00 +00:00
|
|
|
|
2024-02-09 23:17:06 +00:00
|
|
|
lazy var seekHandler: ConfigHandler = { event in
|
|
|
|
guard let posEvent = event as? MPChangePlaybackPositionCommandEvent else {
|
|
|
|
return .commandFailed
|
|
|
|
}
|
|
|
|
|
2024-03-09 23:53:44 +00:00
|
|
|
let cmd = String(format: "seek %.02f absolute", posEvent.positionTime)
|
2024-03-24 00:48:22 +00:00
|
|
|
return self.input.command(cmd) ? .success : .commandFailed
|
2024-02-09 23:17:06 +00:00
|
|
|
}
|
|
|
|
|
2024-03-23 23:04:53 +00:00
|
|
|
func handle(event: EventHelper.Event) {
|
|
|
|
switch event.name {
|
|
|
|
case "time-pos":
|
|
|
|
let newPosition = max(event.double ?? 0, 0)
|
2024-02-08 00:05:25 +00:00
|
|
|
if Int((floor(newPosition) - floor(position)) / rate) != 0 {
|
2024-02-07 23:29:37 +00:00
|
|
|
position = newPosition
|
|
|
|
}
|
2024-03-23 23:04:53 +00:00
|
|
|
case "pause": isPaused = event.bool ?? false
|
|
|
|
case "duration": duration = event.double ?? 0
|
|
|
|
case "speed": rate = event.double ?? 1
|
|
|
|
case "media-title": title = event.string ?? ""
|
|
|
|
case "chapter-metadata/title": chapter = event.string
|
|
|
|
case "metadata/by-key/album": album = event.string
|
|
|
|
case "metadata/by-key/artist": artist = event.string
|
2024-10-17 12:22:42 +00:00
|
|
|
case "path": path = event.string
|
|
|
|
case "track-list": updateCover(tracks: event.array)
|
2024-03-23 23:04:53 +00:00
|
|
|
default: break
|
2020-01-21 18:14:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|