2023-09-30 14:01:04 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
|
|
|
|
class MacCommon: Common {
|
|
|
|
@objc var layer: MetalLayer?
|
|
|
|
|
2024-04-06 21:31:05 +00:00
|
|
|
var presentation: Presentation?
|
2023-09-30 14:01:04 +00:00
|
|
|
var timer: PreciseTimer?
|
|
|
|
var swapTime: UInt64 = 0
|
|
|
|
let swapLock: NSCondition = NSCondition()
|
|
|
|
|
|
|
|
@objc init(_ vo: UnsafeMutablePointer<vo>) {
|
2024-03-26 22:32:22 +00:00
|
|
|
let log = LogHelper(mp_log_new(vo, vo.pointee.log, "mac"))
|
2024-03-19 21:25:39 +00:00
|
|
|
let option = OptionHelper(vo, vo.pointee.global)
|
2024-03-26 22:32:22 +00:00
|
|
|
super.init(option, log)
|
2024-10-14 19:47:37 +00:00
|
|
|
eventsLock.withLock { self.vo = vo }
|
2024-03-19 18:50:44 +00:00
|
|
|
input = InputHelper(vo.pointee.input_ctx, option)
|
2024-04-06 21:31:05 +00:00
|
|
|
presentation = Presentation(common: self)
|
2023-09-30 14:01:04 +00:00
|
|
|
timer = PreciseTimer(common: self)
|
|
|
|
|
|
|
|
DispatchQueue.main.sync {
|
|
|
|
layer = MetalLayer(common: self)
|
|
|
|
initMisc(vo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func config(_ vo: UnsafeMutablePointer<vo>) -> Bool {
|
2024-10-14 19:47:37 +00:00
|
|
|
eventsLock.withLock { self.vo = vo }
|
2023-09-30 14:01:04 +00:00
|
|
|
|
|
|
|
DispatchQueue.main.sync {
|
|
|
|
let previousActiveApp = getActiveApp()
|
|
|
|
initApp()
|
|
|
|
|
2024-10-05 12:16:33 +00:00
|
|
|
let (_, wr, _) = getInitProperties(vo)
|
2023-09-30 14:01:04 +00:00
|
|
|
|
|
|
|
guard let layer = self.layer else {
|
2024-03-26 22:45:18 +00:00
|
|
|
log.error("Something went wrong, no MetalLayer was initialized")
|
2023-09-30 14:01:04 +00:00
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if window == nil {
|
|
|
|
initView(vo, layer)
|
|
|
|
initWindow(vo, previousActiveApp)
|
|
|
|
initWindowState()
|
|
|
|
}
|
|
|
|
|
2024-10-05 13:07:33 +00:00
|
|
|
if option.vo.auto_window_resize {
|
2023-09-30 14:01:04 +00:00
|
|
|
window?.updateSize(wr.size)
|
|
|
|
}
|
|
|
|
|
2024-03-19 22:15:42 +00:00
|
|
|
if option.vo.focus_on == 2 {
|
2024-03-10 12:44:40 +00:00
|
|
|
NSApp.activate(ignoringOtherApps: true)
|
|
|
|
}
|
|
|
|
|
2023-09-30 14:01:04 +00:00
|
|
|
windowDidResize()
|
2024-02-26 21:48:05 +00:00
|
|
|
updateICCProfile()
|
2023-09-30 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func uninit(_ vo: UnsafeMutablePointer<vo>) {
|
|
|
|
window?.waitForAnimation()
|
|
|
|
|
|
|
|
timer?.terminate()
|
|
|
|
|
|
|
|
DispatchQueue.main.sync {
|
|
|
|
window?.delegate = nil
|
|
|
|
window?.close()
|
|
|
|
|
|
|
|
uninitCommon()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func swapBuffer() {
|
2024-04-06 21:31:05 +00:00
|
|
|
if option.mac.macos_render_timer > RENDER_TIMER_SYSTEM {
|
2023-09-30 14:01:04 +00:00
|
|
|
swapLock.lock()
|
2024-04-27 20:11:33 +00:00
|
|
|
while swapTime < 1 {
|
2023-09-30 14:01:04 +00:00
|
|
|
swapLock.wait()
|
|
|
|
}
|
|
|
|
swapTime = 0
|
|
|
|
swapLock.unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-06 21:31:05 +00:00
|
|
|
@objc func fillVsync(info: UnsafeMutablePointer<vo_vsync_info>) {
|
|
|
|
if option.mac.macos_render_timer != RENDER_TIMER_PRESENTATION_FEEDBACK { return }
|
|
|
|
|
|
|
|
let next = presentation?.next()
|
|
|
|
info.pointee.vsync_duration = next?.duration ?? -1
|
|
|
|
info.pointee.skipped_vsyncs = next?.skipped ?? -1
|
|
|
|
info.pointee.last_queue_display_time = next?.time ?? -1
|
|
|
|
}
|
|
|
|
|
2023-09-30 14:01:04 +00:00
|
|
|
override func displayLinkCallback(_ displayLink: CVDisplayLink,
|
2024-04-27 20:11:33 +00:00
|
|
|
_ inNow: UnsafePointer<CVTimeStamp>,
|
|
|
|
_ inOutputTime: UnsafePointer<CVTimeStamp>,
|
|
|
|
_ flagsIn: CVOptionFlags,
|
|
|
|
_ flagsOut: UnsafeMutablePointer<CVOptionFlags>) -> CVReturn {
|
2023-11-07 21:35:19 +00:00
|
|
|
let signalSwap = {
|
|
|
|
self.swapLock.lock()
|
|
|
|
self.swapTime += 1
|
|
|
|
self.swapLock.signal()
|
|
|
|
self.swapLock.unlock()
|
2023-09-30 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-06 21:31:05 +00:00
|
|
|
if option.mac.macos_render_timer > RENDER_TIMER_SYSTEM {
|
2024-03-19 22:15:42 +00:00
|
|
|
if let timer = self.timer, option.mac.macos_render_timer == RENDER_TIMER_PRECISE {
|
2023-09-30 14:01:04 +00:00
|
|
|
timer.scheduleAt(time: inOutputTime.pointee.hostTime, closure: signalSwap)
|
|
|
|
return kCVReturnSuccess
|
|
|
|
}
|
|
|
|
|
|
|
|
signalSwap()
|
2024-04-06 21:31:05 +00:00
|
|
|
return kCVReturnSuccess
|
|
|
|
}
|
|
|
|
|
|
|
|
if option.mac.macos_render_timer == RENDER_TIMER_PRESENTATION_FEEDBACK {
|
|
|
|
presentation?.add(time: inOutputTime.pointee)
|
2023-09-30 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return kCVReturnSuccess
|
|
|
|
}
|
|
|
|
|
|
|
|
override func startDisplayLink(_ vo: UnsafeMutablePointer<vo>) {
|
|
|
|
super.startDisplayLink(vo)
|
|
|
|
timer?.updatePolicy(periodSeconds: 1 / currentFps())
|
|
|
|
}
|
|
|
|
|
|
|
|
override func updateDisplaylink() {
|
|
|
|
super.updateDisplaylink()
|
|
|
|
timer?.updatePolicy(periodSeconds: 1 / currentFps())
|
|
|
|
}
|
|
|
|
|
|
|
|
override func lightSensorUpdate() {
|
|
|
|
flagEvents(VO_EVENT_AMBIENT_LIGHTING_CHANGED)
|
|
|
|
}
|
|
|
|
|
2024-02-26 21:48:05 +00:00
|
|
|
override func updateICCProfile() {
|
2023-09-30 14:01:04 +00:00
|
|
|
flagEvents(VO_EVENT_ICC_PROFILE_CHANGED)
|
|
|
|
}
|
|
|
|
|
|
|
|
override func windowDidResize() {
|
2024-03-06 21:02:43 +00:00
|
|
|
flagEvents(VO_EVENT_RESIZE | VO_EVENT_EXPOSE)
|
2023-09-30 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func windowDidChangeScreenProfile() {
|
2024-02-26 21:48:05 +00:00
|
|
|
updateICCProfile()
|
2023-09-30 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func windowDidChangeBackingProperties() {
|
|
|
|
layer?.contentsScale = window?.backingScaleFactor ?? 1
|
|
|
|
windowDidResize()
|
|
|
|
}
|
|
|
|
}
|