From f79a591ae471c4da6fbb40c1da4bc4220f90aba4 Mon Sep 17 00:00:00 2001 From: der richter Date: Sat, 27 Jun 2020 11:12:46 +0200 Subject: cocoa-cb: generalisation of backend independent parts move all backend independent code parts in their own folder and files, to simplify adding new backends. the goal is to only extend one class and add the backend dependent parts there. usually only the (un)init, config and related parts need to be implemented per backend. furthermore all needed windowing and related events are propagated and can be overwritten. the other backend dependent part is usually the surface for rendering, for example the opengl oder metal layer. in the best case a new backend can be added with only a few hundred lines. --- video/out/cocoa-cb/events_view.swift | 296 ------------------- video/out/cocoa-cb/title_bar.swift | 256 ---------------- video/out/cocoa-cb/video_layer.swift | 323 --------------------- video/out/cocoa-cb/window.swift | 543 ---------------------------------- video/out/cocoa_cb_common.swift | 521 +++++---------------------------- video/out/mac/common.swift | 544 ++++++++++++++++++++++++++++++++++ video/out/mac/gl_layer.swift | 322 +++++++++++++++++++++ video/out/mac/title_bar.swift | 255 ++++++++++++++++ video/out/mac/view.swift | 297 +++++++++++++++++++ video/out/mac/window.swift | 547 +++++++++++++++++++++++++++++++++++ 10 files changed, 2042 insertions(+), 1862 deletions(-) delete mode 100644 video/out/cocoa-cb/events_view.swift delete mode 100644 video/out/cocoa-cb/title_bar.swift delete mode 100644 video/out/cocoa-cb/video_layer.swift delete mode 100644 video/out/cocoa-cb/window.swift create mode 100644 video/out/mac/common.swift create mode 100644 video/out/mac/gl_layer.swift create mode 100644 video/out/mac/title_bar.swift create mode 100644 video/out/mac/view.swift create mode 100644 video/out/mac/window.swift (limited to 'video') diff --git a/video/out/cocoa-cb/events_view.swift b/video/out/cocoa-cb/events_view.swift deleted file mode 100644 index 68e967b90a..0000000000 --- a/video/out/cocoa-cb/events_view.swift +++ /dev/null @@ -1,296 +0,0 @@ -/* - * 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 . - */ - -import Cocoa - -class EventsView: NSView { - - unowned var cocoaCB: CocoaCB - var mpv: MPVHelper? { get { return cocoaCB.mpv } } - - var tracker: NSTrackingArea? - var hasMouseDown: Bool = false - - override var isFlipped: Bool { return true } - override var acceptsFirstResponder: Bool { return true } - - - init(cocoaCB ccb: CocoaCB) { - cocoaCB = ccb - super.init(frame: NSMakeRect(0, 0, 960, 480)) - autoresizingMask = [.width, .height] - wantsBestResolutionOpenGLSurface = true - registerForDraggedTypes([ .fileURLCompat, .URLCompat, .string ]) - } - - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - - override func updateTrackingAreas() { - if let tracker = self.tracker { - removeTrackingArea(tracker) - } - - tracker = NSTrackingArea(rect: bounds, - options: [.activeAlways, .mouseEnteredAndExited, .mouseMoved, .enabledDuringMouseDrag], - owner: self, userInfo: nil) - // here tracker is guaranteed to be none-nil - addTrackingArea(tracker!) - - if containsMouseLocation() { - cocoa_put_key_with_modifiers(SWIFT_KEY_MOUSE_LEAVE, 0) - } - } - - override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation { - guard let types = sender.draggingPasteboard.types else { return [] } - if types.contains(.fileURLCompat) || types.contains(.URLCompat) || types.contains(.string) { - return .copy - } - return [] - } - - func isURL(_ str: String) -> Bool { - // force unwrapping is fine here, regex is guarnteed to be valid - let regex = try! NSRegularExpression(pattern: "^(https?|ftp)://[^\\s/$.?#].[^\\s]*$", - options: .caseInsensitive) - let isURL = regex.numberOfMatches(in: str, - options: [], - range: NSRange(location: 0, length: str.count)) - return isURL > 0 - } - - override func performDragOperation(_ sender: NSDraggingInfo) -> Bool { - let pb = sender.draggingPasteboard - guard let types = pb.types else { return false } - - if types.contains(.fileURLCompat) || types.contains(.URLCompat) { - if let urls = pb.readObjects(forClasses: [NSURL.self]) as? [URL] { - let files = urls.map { $0.absoluteString } - EventsResponder.sharedInstance().handleFilesArray(files) - return true - } - } else if types.contains(.string) { - guard let str = pb.string(forType: .string) else { return false } - var filesArray: [String] = [] - - for val in str.components(separatedBy: "\n") { - let url = val.trimmingCharacters(in: .whitespacesAndNewlines) - let path = (url as NSString).expandingTildeInPath - if isURL(url) { - filesArray.append(url) - } else if path.starts(with: "/") { - filesArray.append(path) - } - } - EventsResponder.sharedInstance().handleFilesArray(filesArray) - return true - } - return false - } - - override func acceptsFirstMouse(for event: NSEvent?) -> Bool { - return true - } - - override func becomeFirstResponder() -> Bool { - return true - } - - override func resignFirstResponder() -> Bool { - return true - } - - override func mouseEntered(with event: NSEvent) { - if mpv?.mouseEnabled() ?? true { - cocoa_put_key_with_modifiers(SWIFT_KEY_MOUSE_ENTER, 0) - } - cocoaCB.updateCursorVisibility() - } - - override func mouseExited(with event: NSEvent) { - if mpv?.mouseEnabled() ?? true { - cocoa_put_key_with_modifiers(SWIFT_KEY_MOUSE_LEAVE, 0) - } - cocoaCB.titleBar?.hide() - cocoaCB.setCursorVisiblility(true) - } - - override func mouseMoved(with event: NSEvent) { - if mpv?.mouseEnabled() ?? true { - signalMouseMovement(event) - } - cocoaCB.titleBar?.show() - } - - override func mouseDragged(with event: NSEvent) { - if mpv?.mouseEnabled() ?? true { - signalMouseMovement(event) - } - } - - override func mouseDown(with event: NSEvent) { - if mpv?.mouseEnabled() ?? true { - signalMouseDown(event) - } - } - - override func mouseUp(with event: NSEvent) { - if mpv?.mouseEnabled() ?? true { - signalMouseUp(event) - } - cocoaCB.window?.isMoving = false - } - - override func rightMouseDown(with event: NSEvent) { - if mpv?.mouseEnabled() ?? true { - signalMouseDown(event) - } - } - - override func rightMouseUp(with event: NSEvent) { - if mpv?.mouseEnabled() ?? true { - signalMouseUp(event) - } - } - - override func otherMouseDown(with event: NSEvent) { - if mpv?.mouseEnabled() ?? true { - signalMouseDown(event) - } - } - - override func otherMouseUp(with event: NSEvent) { - if mpv?.mouseEnabled() ?? true { - signalMouseUp(event) - } - } - - override func magnify(with event: NSEvent) { - cocoaCB.layer?.inLiveResize = event.phase == .ended ? false : true - cocoaCB.window?.addWindowScale(Double(event.magnification)) - } - - func signalMouseDown(_ event: NSEvent) { - signalMouseEvent(event, MP_KEY_STATE_DOWN) - if event.clickCount > 1 { - signalMouseEvent(event, MP_KEY_STATE_UP) - } - } - - func signalMouseUp(_ event: NSEvent) { - signalMouseEvent(event, MP_KEY_STATE_UP) - } - - func signalMouseEvent(_ event: NSEvent, _ state: UInt32) { - hasMouseDown = state == MP_KEY_STATE_DOWN - let mpkey = getMpvButton(event) - cocoa_put_key_with_modifiers((mpkey | Int32(state)), Int32(event.modifierFlags.rawValue)) - } - - func signalMouseMovement(_ event: NSEvent) { - var point = convert(event.locationInWindow, from: nil) - point = convertToBacking(point) - point.y = -point.y - - cocoaCB.window?.updateMovableBackground(point) - if !(cocoaCB.window?.isMoving ?? false) { - mpv?.setMousePosition(point) - } - } - - func preciseScroll(_ event: NSEvent) { - var delta: Double - var cmd: Int32 - - if abs(event.deltaY) >= abs(event.deltaX) { - delta = Double(event.deltaY) * 0.1 - cmd = delta > 0 ? SWIFT_WHEEL_UP : SWIFT_WHEEL_DOWN - } else { - delta = Double(event.deltaX) * 0.1 - cmd = delta > 0 ? SWIFT_WHEEL_RIGHT : SWIFT_WHEEL_LEFT - } - - mpv?.putAxis(cmd, delta: abs(delta)) - } - - override func scrollWheel(with event: NSEvent) { - if !(mpv?.mouseEnabled() ?? true) { - return - } - - if event.hasPreciseScrollingDeltas { - preciseScroll(event) - } else { - let modifiers = event.modifierFlags - let deltaX = modifiers.contains(.shift) ? event.scrollingDeltaY : event.scrollingDeltaX - let deltaY = modifiers.contains(.shift) ? event.scrollingDeltaX : event.scrollingDeltaY - var mpkey: Int32 - - if abs(deltaY) >= abs(deltaX) { - mpkey = deltaY > 0 ? SWIFT_WHEEL_UP : SWIFT_WHEEL_DOWN - } else { - mpkey = deltaX > 0 ? SWIFT_WHEEL_RIGHT : SWIFT_WHEEL_LEFT - } - - cocoa_put_key_with_modifiers(mpkey, Int32(modifiers.rawValue)) - } - } - - func containsMouseLocation() -> Bool { - var topMargin: CGFloat = 0.0 - let menuBarHeight = NSApp.mainMenu?.menuBarHeight ?? 23.0 - - guard let window = cocoaCB.window else { return false } - guard var vF = window.screen?.frame else { return false } - - if window.isInFullscreen && (menuBarHeight > 0) { - topMargin = TitleBar.height + 1 + menuBarHeight - } - - vF.size.height -= topMargin - - let vFW = window.convertFromScreen(vF) - let vFV = convert(vFW, from: nil) - let pt = convert(window.mouseLocationOutsideOfEventStream, from: nil) - - var clippedBounds = bounds.intersection(vFV) - if !window.isInFullscreen { - clippedBounds.origin.y += TitleBar.height - clippedBounds.size.height -= TitleBar.height - } - return clippedBounds.contains(pt) - } - - func canHideCursor() -> Bool { - guard let window = cocoaCB.window else { return false } - return !hasMouseDown && containsMouseLocation() && window.isKeyWindow - } - - func getMpvButton(_ event: NSEvent) -> Int32 { - let buttonNumber = event.buttonNumber - switch (buttonNumber) { - case 0: return SWIFT_MBTN_LEFT - case 1: return SWIFT_MBTN_RIGHT - case 2: return SWIFT_MBTN_MID - case 3: return SWIFT_MBTN_BACK - case 4: return SWIFT_MBTN_FORWARD - default: return SWIFT_MBTN9 + Int32(buttonNumber - 5) - } - } -} diff --git a/video/out/cocoa-cb/title_bar.swift b/video/out/cocoa-cb/title_bar.swift deleted file mode 100644 index c1c8b09526..0000000000 --- a/video/out/cocoa-cb/title_bar.swift +++ /dev/null @@ -1,256 +0,0 @@ -/* - * 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 . - */ - -import Cocoa - -class TitleBar: NSVisualEffectView { - - unowned var cocoaCB: CocoaCB - var libmpv: LibmpvHelper { get { return cocoaCB.libmpv } } - - var systemBar: NSView? { - get { return cocoaCB.window?.standardWindowButton(.closeButton)?.superview } - } - static var height: CGFloat { - get { return NSWindow.frameRect(forContentRect: CGRect.zero, styleMask: .titled).size.height } - } - var buttons: [NSButton] { - get { return ([.closeButton, .miniaturizeButton, .zoomButton] as [NSWindow.ButtonType]).compactMap { cocoaCB.window?.standardWindowButton($0) } } - } - - override var material: NSVisualEffectView.Material { - get { return super.material } - set { - super.material = newValue - // fix for broken deprecated materials - if material == .light || material == .dark { - state = .active - } else if #available(macOS 10.11, *), - material == .mediumLight || material == .ultraDark - { - state = .active - } else { - state = .followsWindowActiveState - } - - } - } - - init(frame: NSRect, window: NSWindow, cocoaCB ccb: CocoaCB) { - let f = NSMakeRect(0, frame.size.height - TitleBar.height, - frame.size.width, TitleBar.height) - cocoaCB = ccb - super.init(frame: f) - buttons.forEach { $0.isHidden = true } - isHidden = true - alphaValue = 0 - blendingMode = .withinWindow - autoresizingMask = [.width, .minYMargin] - systemBar?.alphaValue = 0 - state = .followsWindowActiveState - wantsLayer = true - - window.contentView?.addSubview(self, positioned: .above, relativeTo: nil) - window.titlebarAppearsTransparent = true - window.styleMask.insert(.fullSizeContentView) - set(appearance: Int(libmpv.macOpts.macos_title_bar_appearance)) - set(material: Int(libmpv.macOpts.macos_title_bar_material)) - set(color: libmpv.macOpts.macos_title_bar_color) - } - - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - - // catch these events so they are not propagated to the underlying view - override func mouseDown(with event: NSEvent) { } - - override func mouseUp(with event: NSEvent) { - if event.clickCount > 1 { - let def = UserDefaults.standard - var action = def.string(forKey: "AppleActionOnDoubleClick") - - // macOS 10.10 and earlier - if action == nil { - action = def.bool(forKey: "AppleMiniaturizeOnDoubleClick") == true ? - "Minimize" : "Maximize" - } - - if action == "Minimize" { - window?.miniaturize(self) - } else if action == "Maximize" { - window?.zoom(self) - } - } - - cocoaCB.window?.isMoving = false - } - - func set(appearance: Any) { - if appearance is Int { - window?.appearance = appearanceFrom(string: String(appearance as? Int ?? 0)) - } else { - window?.appearance = appearanceFrom(string: appearance as? String ?? "auto") - } - } - - func set(material: Any) { - if material is Int { - self.material = materialFrom(string: String(material as? Int ?? 0)) - } else { - self.material = materialFrom(string: material as? String ?? "titlebar") - } - } - - func set(color: Any) { - if color is String { - layer?.backgroundColor = NSColor(hex: color as? String ?? "#00000000").cgColor - } else { - let col = color as? m_color ?? m_color(r: 0, g: 0, b: 0, a: 0) - let red = CGFloat(col.r)/255 - let green = CGFloat(col.g)/255 - let blue = CGFloat(col.b)/255 - let alpha = CGFloat(col.a)/255 - layer?.backgroundColor = NSColor(calibratedRed: red, green: green, - blue: blue, alpha: alpha).cgColor - } - } - - func show() { - guard let window = cocoaCB.window else { return } - if !window.border && !window.isInFullscreen { return } - let loc = cocoaCB.view?.convert(window.mouseLocationOutsideOfEventStream, from: nil) - - buttons.forEach { $0.isHidden = false } - NSAnimationContext.runAnimationGroup({ (context) -> Void in - context.duration = 0.20 - systemBar?.animator().alphaValue = 1 - if !window.isInFullscreen && !window.isAnimating { - animator().alphaValue = 1 - isHidden = false - } - }, completionHandler: nil ) - - if loc?.y ?? 0 > TitleBar.height { - hideDelayed() - } else { - NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(hide), object: nil) - } - } - - @objc func hide() { - guard let window = cocoaCB.window else { return } - if window.isInFullscreen && !window.isAnimating { - alphaValue = 0 - isHidden = true - return - } - NSAnimationContext.runAnimationGroup({ (context) -> Void in - context.duration = 0.20 - systemBar?.animator().alphaValue = 0 - animator().alphaValue = 0 - }, completionHandler: { - self.buttons.forEach { $0.isHidden = true } - self.isHidden = true - }) - } - - func hideDelayed() { - NSObject.cancelPreviousPerformRequests(withTarget: self, - selector: #selector(hide), - object: nil) - perform(#selector(hide), with: nil, afterDelay: 0.5) - } - - func appearanceFrom(string: String) -> NSAppearance? { - switch string { - case "1", "aqua": - return NSAppearance(named: .aqua) - case "3", "vibrantLight": - return NSAppearance(named: .vibrantLight) - case "4", "vibrantDark": - return NSAppearance(named: .vibrantDark) - default: break - } - - if #available(macOS 10.14, *) { - switch string { - case "2", "darkAqua": - return NSAppearance(named: .darkAqua) - case "5", "aquaHighContrast": - return NSAppearance(named: .accessibilityHighContrastAqua) - case "6", "darkAquaHighContrast": - return NSAppearance(named: .accessibilityHighContrastDarkAqua) - case "7", "vibrantLightHighContrast": - return NSAppearance(named: .accessibilityHighContrastVibrantLight) - case "8", "vibrantDarkHighContrast": - return NSAppearance(named: .accessibilityHighContrastVibrantDark) - case "0", "auto": fallthrough - default: -#if HAVE_MACOS_10_14_FEATURES - return nil -#else - break -#endif - } - } - - let style = UserDefaults.standard.string(forKey: "AppleInterfaceStyle") - return appearanceFrom(string: style == nil ? "aqua" : "vibrantDark") - } - - func materialFrom(string: String) -> NSVisualEffectView.Material { - switch string { - case "1", "selection": return .selection - case "0", "titlebar": return .titlebar - case "14", "dark": return .dark - case "15", "light": return .light - default: break - } - -#if HAVE_MACOS_10_11_FEATURES - if #available(macOS 10.11, *) { - switch string { - case "2,", "menu": return .menu - case "3", "popover": return .popover - case "4", "sidebar": return .sidebar - case "16", "mediumLight": return .mediumLight - case "17", "ultraDark": return .ultraDark - default: break - } - } -#endif -#if HAVE_MACOS_10_14_FEATURES - if #available(macOS 10.14, *) { - switch string { - case "5,", "headerView": return .headerView - case "6", "sheet": return .sheet - case "7", "windowBackground": return .windowBackground - case "8", "hudWindow": return .hudWindow - case "9", "fullScreen": return .fullScreenUI - case "10", "toolTip": return .toolTip - case "11", "contentBackground": return .contentBackground - case "12", "underWindowBackground": return .underWindowBackground - case "13", "underPageBackground": return .underPageBackground - default: break - } - } -#endif - - return .titlebar - } -} diff --git a/video/out/cocoa-cb/video_layer.swift b/video/out/cocoa-cb/video_layer.swift deleted file mode 100644 index f9a195b554..0000000000 --- a/video/out/cocoa-cb/video_layer.swift +++ /dev/null @@ -1,323 +0,0 @@ -/* - * 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 . - */ - -import Cocoa -import OpenGL.GL -import OpenGL.GL3 - -let glVersions: [CGLOpenGLProfile] = [ - kCGLOGLPVersion_3_2_Core, - kCGLOGLPVersion_Legacy -] - -let glFormatBase: [CGLPixelFormatAttribute] = [ - kCGLPFAOpenGLProfile, - kCGLPFAAccelerated, - kCGLPFADoubleBuffer -] - -let glFormatSoftwareBase: [CGLPixelFormatAttribute] = [ - kCGLPFAOpenGLProfile, - kCGLPFARendererID, - CGLPixelFormatAttribute(UInt32(kCGLRendererGenericFloatID)), - kCGLPFADoubleBuffer -] - -let glFormatOptional: [[CGLPixelFormatAttribute]] = [ - [kCGLPFABackingStore], - [kCGLPFAAllowOfflineRenderers] -] - -let glFormat10Bit: [CGLPixelFormatAttribute] = [ - kCGLPFAColorSize, - _CGLPixelFormatAttribute(rawValue: 64), - kCGLPFAColorFloat -] - -let glFormatAutoGPU: [CGLPixelFormatAttribute] = [ - kCGLPFASupportsAutomaticGraphicsSwitching -] - -let attributeLookUp: [UInt32:String] = [ - kCGLOGLPVersion_3_2_Core.rawValue: "kCGLOGLPVersion_3_2_Core", - kCGLOGLPVersion_Legacy.rawValue: "kCGLOGLPVersion_Legacy", - kCGLPFAOpenGLProfile.rawValue: "kCGLPFAOpenGLProfile", - UInt32(kCGLRendererGenericFloatID): "kCGLRendererGenericFloatID", - kCGLPFARendererID.rawValue: "kCGLPFARendererID", - kCGLPFAAccelerated.rawValue: "kCGLPFAAccelerated", - kCGLPFADoubleBuffer.rawValue: "kCGLPFADoubleBuffer", - kCGLPFABackingStore.rawValue: "kCGLPFABackingStore", - kCGLPFAColorSize.rawValue: "kCGLPFAColorSize", - kCGLPFAColorFloat.rawValue: "kCGLPFAColorFloat", - kCGLPFAAllowOfflineRenderers.rawValue: "kCGLPFAAllowOfflineRenderers", - kCGLPFASupportsAutomaticGraphicsSwitching.rawValue: "kCGLPFASupportsAutomaticGraphicsSwitching", -] - -class VideoLayer: CAOpenGLLayer { - - unowned var cocoaCB: CocoaCB - var libmpv: LibmpvHelper { get { return cocoaCB.libmpv } } - - let displayLock = NSLock() - let cglContext: CGLContextObj - let cglPixelFormat: CGLPixelFormatObj - var needsFlip: Bool = false - var forceDraw: Bool = false - var surfaceSize: NSSize = NSSize(width: 0, height: 0) - var bufferDepth: GLint = 8 - - enum Draw: Int { case normal = 1, atomic, atomicEnd } - var draw: Draw = .normal - - let queue: DispatchQueue = DispatchQueue(label: "io.mpv.queue.draw") - - var needsICCUpdate: Bool = false { - didSet { - if needsICCUpdate == true { - update() - } - } - } - - var inLiveResize: Bool = false { - didSet { - if inLiveResize { - isAsynchronous = true - } - update(force: true) - } - } - - init(cocoaCB ccb: CocoaCB) { - cocoaCB = ccb - (cglPixelFormat, bufferDepth) = VideoLayer.createPixelFormat(ccb.libmpv) - cglContext = VideoLayer.createContext(ccb.libmpv, cglPixelFormat) - super.init() - autoresizingMask = [.layerWidthSizable, .layerHeightSizable] - backgroundColor = NSColor.black.cgColor - - if #available(macOS 10.12, *), bufferDepth > 8 { - contentsFormat = .RGBA16Float - } - - var i: GLint = 1 - CGLSetParameter(cglContext, kCGLCPSwapInterval, &i) - CGLSetCurrentContext(cglContext) - - libmpv.initRender() - libmpv.setRenderUpdateCallback(updateCallback, context: self) - libmpv.setRenderControlCallback(cocoaCB.controlCallback, context: cocoaCB) - } - - //necessary for when the layer containing window changes the screen - override init(layer: Any) { - guard let oldLayer = layer as? VideoLayer else { - fatalError("init(layer: Any) passed an invalid layer") - } - cocoaCB = oldLayer.cocoaCB - surfaceSize = oldLayer.surfaceSize - cglPixelFormat = oldLayer.cglPixelFormat - cglContext = oldLayer.cglContext - super.init() - } - - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - - override func canDraw(inCGLContext ctx: CGLContextObj, - pixelFormat pf: CGLPixelFormatObj, - forLayerTime t: CFTimeInterval, - displayTime ts: UnsafePointer?) -> Bool { - if inLiveResize == false { - isAsynchronous = false - } - return cocoaCB.backendState == .initialized && - (forceDraw || libmpv.isRenderUpdateFrame()) - } - - override func draw(inCGLContext ctx: CGLContextObj, - pixelFormat pf: CGLPixelFormatObj, - forLayerTime t: CFTimeInterval, - displayTime ts: UnsafePointer?) { - needsFlip = false - forceDraw = false - - if draw.rawValue >= Draw.atomic.rawValue { - if draw == .atomic { - draw = .atomicEnd - } else { - atomicDrawingEnd() - } - } - - updateSurfaceSize() - libmpv.drawRender(surfaceSize, bufferDepth, ctx) - - if needsICCUpdate { - needsICCUpdate = false - cocoaCB.updateICCProfile() - } - } - - func updateSurfaceSize() { - var dims: [GLint] = [0, 0, 0, 0] - glGetIntegerv(GLenum(GL_VIEWPORT), &dims) - surfaceSize = NSSize(width: CGFloat(dims[2]), height: CGFloat(dims[3])) - - if NSEqualSizes(surfaceSize, NSZeroSize) { - surfaceSize = bounds.size - surfaceSize.width *= contentsScale - surfaceSize.height *= contentsScale - } - } - - func atomicDrawingStart() { - if draw == .normal { - NSDisableScreenUpdates() - draw = .atomic - } - } - - func atomicDrawingEnd() { - if draw.rawValue >= Draw.atomic.rawValue { - NSEnableScreenUpdates() - draw = .normal - } - } - - override func copyCGLPixelFormat(forDisplayMask mask: UInt32) -> CGLPixelFormatObj { - return cglPixelFormat - } - - override func copyCGLContext(forPixelFormat pf: CGLPixelFormatObj) -> CGLContextObj { - contentsScale = cocoaCB.window?.backingScaleFactor ?? 1.0 - return cglContext - } - - let updateCallback: mpv_render_update_fn = { (ctx) in - let layer: VideoLayer = unsafeBitCast(ctx, to: VideoLayer.self) - layer.update() - } - - override func display() { - displayLock.lock() - let isUpdate = needsFlip - super.display() - CATransaction.flush() - if isUpdate && needsFlip { - CGLSetCurrentContext(cglContext) - if libmpv.isRenderUpdateFrame() { - libmpv.drawRender(NSZeroSize, bufferDepth, cglContext, skip: true) - } - } - displayLock.unlock() - } - - func update(force: Bool = false) { - if force { forceDraw = true } - queue.async { - if self.forceDraw || !self.inLiveResize { - self.needsFlip = true - self.display() - } - } - } - - class func createPixelFormat(_ libmpv: LibmpvHelper) -> (CGLPixelFormatObj, GLint) { - var pix: CGLPixelFormatObj? - var depth: GLint = 8 - var err: CGLError = CGLError(rawValue: 0) - let swRender = libmpv.macOpts.cocoa_cb_sw_renderer - - if swRender != 1 { - (pix, depth, err) = VideoLayer.findPixelFormat(libmpv) - } - - if (err != kCGLNoError || pix == nil) && swRender != 0 { - (pix, depth, err) = VideoLayer.findPixelFormat(libmpv, software: true) - } - - guard let pixelFormat = pix, err == kCGLNoError else { - libmpv.sendError("Couldn't create any CGL pixel format") - exit(1) - } - - return (pixelFormat, depth) - } - - class func findPixelFormat(_ libmpv: LibmpvHelper, software: Bool = false) -> (CGLPixelFormatObj?, GLint, CGLError) { - var pix: CGLPixelFormatObj? - var err: CGLError = CGLError(rawValue: 0) - var npix: GLint = 0 - - for ver in glVersions { - var glBase = software ? glFormatSoftwareBase : glFormatBase - glBase.insert(CGLPixelFormatAttribute(ver.rawValue), at: 1) - - var glFormat = [glBase] - if (libmpv.macOpts.cocoa_cb_10bit_context == 1) { - glFormat += [glFormat10Bit] - } - glFormat += glFormatOptional - - if (libmpv.macOpts.macos_force_dedicated_gpu == 0) { - glFormat += [glFormatAutoGPU] - } - - for index in stride(from: glFormat.count-1, through: 0, by: -1) { - let format = glFormat.flatMap { $0 } + [_CGLPixelFormatAttribute(rawValue: 0)] - err = CGLChoosePixelFormat(format, &pix, &npix) - - if err == kCGLBadAttribute || err == kCGLBadPixelFormat || pix == nil { - glFormat.remove(at: index) - } else { - let attArray = format.map({ (value: _CGLPixelFormatAttribute) -> String in - return attributeLookUp[value.rawValue] ?? String(value.rawValue) - }) - - libmpv.sendVerbose("Created CGL pixel format with attributes: " + - "\(attArray.joined(separator: ", "))") - return (pix, glFormat.contains(glFormat10Bit) ? 16 : 8, err) - } - } - } - - let errS = String(cString: CGLErrorString(err)) - libmpv.sendWarning("Couldn't create a " + - "\(software ? "software" : "hardware accelerated") " + - "CGL pixel format: \(errS) (\(err.rawValue))") - if software == false && libmpv.macOpts.cocoa_cb_sw_renderer == -1 { - libmpv.sendWarning("Falling back to software renderer") - } - - return (pix, 8, err) - } - - class func createContext(_ libmpv: LibmpvHelper, _ pixelFormat: CGLPixelFormatObj) -> CGLContextObj { - var context: CGLContextObj? - let error = CGLCreateContext(pixelFormat, nil, &context) - - guard let cglContext = context, error == kCGLNoError else { - let errS = String(cString: CGLErrorString(error)) - libmpv.sendError("Couldn't create a CGLContext: " + errS) - exit(1) - } - - return cglContext - } -} diff --git a/video/out/cocoa-cb/window.swift b/video/out/cocoa-cb/window.swift deleted file mode 100644 index ae1c7cf276..0000000000 --- a/video/out/cocoa-cb/window.swift +++ /dev/null @@ -1,543 +0,0 @@ -/* - * 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 . - */ - -import Cocoa - -class Window: NSWindow, NSWindowDelegate { - - weak var cocoaCB: CocoaCB! = nil - var mpv: MPVHelper? { get { return cocoaCB.mpv } } - var libmpv: LibmpvHelper { get { return cocoaCB.libmpv } } - - var targetScreen: NSScreen? - var previousScreen: NSScreen? - var currentScreen: NSScreen? - var unfScreen: NSScreen? - - var unfsContentFrame: NSRect? - var isInFullscreen: Bool = false - var isAnimating: Bool = false - var isMoving: Bool = false - var forceTargetScreen: Bool = false - - var keepAspect: Bool = true { - didSet { - if let contentViewFrame = contentView?.frame, !isInFullscreen { - unfsContentFrame = convertToScreen(contentViewFrame) - } - - if keepAspect { - contentAspectRatio = unfsContentFrame?.size ?? contentAspectRatio - } else { - resizeIncrements = NSSize(width: 1.0, height: 1.0) - } - } - } - - var border: Bool = true { - didSet { if !border { cocoaCB.titleBar?.hide() } } - } - - override var canBecomeKey: Bool { return true } - override var canBecomeMain: Bool { return true } - - override var styleMask: NSWindow.StyleMask { - get { return super.styleMask } - set { - let responder = firstResponder - let windowTitle = title - super.styleMask = newValue - makeFirstResponder(responder) - title = windowTitle - } - } - - convenience init(contentRect: NSRect, screen: NSScreen?, view: NSView, cocoaCB ccb: CocoaCB) { - self.init(contentRect: contentRect, - styleMask: [.titled, .closable, .miniaturizable, .resizable], - backing: .buffered, defer: false, screen: screen) - - // workaround for an AppKit bug where the NSWindow can't be placed on a - // none Main screen NSScreen outside the Main screen's frame bounds - if let wantedScreen = screen, screen != NSScreen.main { - var absoluteWantedOrigin = contentRect.origin - absoluteWantedOrigin.x += wantedScreen.frame.origin.x - absoluteWantedOrigin.y += wantedScreen.frame.origin.y - - if !NSEqualPoints(absoluteWantedOrigin, self.frame.origin) { - self.setFrameOrigin(absoluteWantedOrigin) - } - } - - cocoaCB = ccb - title = cocoaCB.title - minSize = NSMakeSize(160, 90) - collectionBehavior = .fullScreenPrimary - delegate = self - - if let cView = contentView { - cView.addSubview(view) - view.frame = cView.frame - unfsContentFrame = convertToScreen(cView.frame) - } - - targetScreen = screen - currentScreen = screen - unfScreen = screen - - if let app = NSApp as? Application { - app.menuBar.register(#selector(setHalfWindowSize), for: MPM_H_SIZE) - app.menuBar.register(#selector(setNormalWindowSize), for: MPM_N_SIZE) - app.menuBar.register(#selector(setDoubleWindowSize), for: MPM_D_SIZE) - app.menuBar.register(#selector(performMiniaturize(_:)), for: MPM_MINIMIZE) - app.menuBar.register(#selector(performZoom(_:)), for: MPM_ZOOM) - } - } - - override func toggleFullScreen(_ sender: Any?) { - if isAnimating { - return - } - - isAnimating = true - - targetScreen = cocoaCB.getTargetScreen(forFullscreen: !isInFullscreen) - if targetScreen == nil && previousScreen == nil { - targetScreen = screen - } else if targetScreen == nil { - targetScreen = previousScreen - previousScreen = nil - } else { - previousScreen = screen - } - - if let contentViewFrame = contentView?.frame, !isInFullscreen { - unfsContentFrame = convertToScreen(contentViewFrame) - unfScreen = screen - } - // move window to target screen when going to fullscreen - if let tScreen = targetScreen, !isInFullscreen && (tScreen != screen) { - let frame = calculateWindowPosition(for: tScreen, withoutBounds: false) - setFrame(frame, display: true) - } - - if Bool(mpv?.opts.native_fs ?? 1) { - super.toggleFullScreen(sender) - } else { - if !isInFullscreen { - setToFullScreen() - } - else { - setToWindow() - } - } - } - - func customWindowsToEnterFullScreen(for window: NSWindow) -> [NSWindow]? { - return [window] - } - - func customWindowsToExitFullScreen(for window: NSWindow) -> [NSWindow]? { - return [window] - } - - func window(_ window: NSWindow, startCustomAnimationToEnterFullScreenWithDuration duration: TimeInterval) { - guard let tScreen = targetScreen else { return } - cocoaCB.view?.layerContentsPlacement = .scaleProportionallyToFit - cocoaCB.titleBar?.hide() - NSAnimationContext.runAnimationGroup({ (context) -> Void in - context.duration = getFsAnimationDuration(duration - 0.05) - window.animator().setFrame(tScreen.frame, display: true) - }, completionHandler: { }) - } - - func window(_ window: NSWindow, startCustomAnimationToExitFullScreenWithDuration duration: TimeInterval) { - guard let tScreen = targetScreen, let currentScreen = screen else { return } - let newFrame = calculateWindowPosition(for: tScreen, withoutBounds: tScreen == screen) - let intermediateFrame = aspectFit(rect: newFrame, in: currentScreen.frame) - cocoaCB.view?.layerContentsPlacement = .scaleProportionallyToFill - cocoaCB.titleBar?.hide() - styleMask.remove(.fullScreen) - setFrame(intermediateFrame, display: true) - - NSAnimationContext.runAnimationGroup({ (context) -> Void in - context.duration = getFsAnimationDuration(duration - 0.05) - window.animator().setFrame(newFrame, display: true) - }, completionHandler: { }) - } - - func windowDidEnterFullScreen(_ notification: Notification) { - isInFullscreen = true - cocoaCB.mpv?.setConfigProperty(fullscreen: isInFullscreen) - cocoaCB.updateCursorVisibility() - endAnimation(frame) - cocoaCB.titleBar?.show() - } - - func windowDidExitFullScreen(_ notification: Notification) { - guard let tScreen = targetScreen else { return } - isInFullscreen = false - cocoaCB.mpv?.setConfigProperty(fullscreen: isInFullscreen) - endAnimation(calculateWindowPosition(for: tScreen, withoutBounds: targetScreen == screen)) - cocoaCB.view?.layerContentsPlacement = .scaleProportionallyToFit - } - - func windowDidFailToEnterFullScreen(_ window: NSWindow) { - guard let tScreen = targetScreen else { return } - let newFrame = calculateWindowPosition(for: tScreen, withoutBounds: targetScreen == screen) - setFrame(newFrame, display: true) - endAnimation() - } - - func windowDidFailToExitFullScreen(_ window: NSWindow) { - guard let targetFrame = targetScreen?.frame else { return } - setFrame(targetFrame, display: true) - endAnimation() - cocoaCB.view?.layerContentsPlacement = .scaleProportionallyToFit - } - - func endAnimation(_ newFrame: NSRect = NSZeroRect) { - if !NSEqualRects(newFrame, NSZeroRect) && isAnimating { - NSAnimationContext.runAnimationGroup({ (context) -> Void in - context.duration = 0.01 - self.animator().setFrame(newFrame, display: true) - }, completionHandler: nil ) - } - - isAnimating = false - cocoaCB.layer?.update() - cocoaCB.checkShutdown() - } - - func setToFullScreen() { - guard let targetFrame = targetScreen?.frame else { return } - styleMask.insert(.fullScreen) - NSApp.presentationOptions = [.autoHideMenuBar, .autoHideDock] - setFrame(targetFrame, display: true) - endAnimation() - isInFullscreen = true - cocoaCB.mpv?.setConfigProperty(fullscreen: isInFullscreen) - cocoaCB.layer?.update() - } - - func setToWindow() { - guard let tScreen = targetScreen else { return } - let newFrame = calculateWindowPosition(for: tScreen, withoutBounds: targetScreen == screen) - NSApp.presentationOptions = [] - setFrame(newFrame, display: true) - styleMask.remove(.fullScreen) - endAnimation() - isInFullscreen = false - cocoaCB.mpv?.setConfigProperty(fullscreen: isInFullscreen) - cocoaCB.layer?.update() - } - - func getFsAnimationDuration(_ def: Double) -> Double { - let duration = libmpv.macOpts.macos_fs_animation_duration - if duration < 0 { - return def - } else { - return Double(duration)/1000 - } - } - - func setOnTop(_ state: Bool, _ ontopLevel: Int) { - if state { - switch ontopLevel { - case -1: - level = .floating - case -2: - level = .statusBar + 1 - default: - level = NSWindow.Level(ontopLevel) - } - collectionBehavior.remove(.transient) - collectionBehavior.insert(.managed) - } else { - level = .normal - } - } - - func setMinimized(_ stateWanted: Bool) { - if isMiniaturized == stateWanted { return } - - if stateWanted { - performMiniaturize(self) - } else { - deminiaturize(self) - } - } - - func setMaximized(_ stateWanted: Bool) { - if isZoomed == stateWanted { return } - - zoom(self) - } - - func updateMovableBackground(_ pos: NSPoint) { - if !isInFullscreen { - isMovableByWindowBackground = mpv?.canBeDraggedAt(pos) ?? true - } else { - isMovableByWindowBackground = false - } - } - - func updateFrame(_ rect: NSRect) { - if rect != frame { - let cRect = frameRect(forContentRect: rect) - unfsContentFrame = rect - setFrame(cRect, display: true) - cocoaCB.layer?.update(force: true) - } - } - - func updateSize(_ size: NSSize) { - if let currentSize = contentView?.frame.size, size != currentSize { - let newContentFrame = centeredContentSize(for: frame, size: size) - if !isInFullscreen { - updateFrame(newContentFrame) - } else { - unfsContentFrame = newContentFrame - } - } - } - - override func setFrame(_ frameRect: NSRect, display flag: Bool) { - if frameRect.width < minSize.width || frameRect.height < minSize.height { - mpv?.sendVerbose("tried to set too small window size: \(frameRect.size)") - return - } - - super.setFrame(frameRect, display: flag) - - if let size = unfsContentFrame?.size, keepAspect { - contentAspectRatio = size - } - } - - func centeredContentSize(for rect: NSRect, size sz: NSSize) -> NSRect { - let cRect = contentRect(forFrameRect: rect) - let dx = (cRect.size.width - sz.width) / 2 - let dy = (cRect.size.height - sz.height) / 2 - return NSInsetRect(cRect, dx, dy) - } - - func aspectFit(rect r: NSRect, in rTarget: NSRect) -> NSRect { - var s = rTarget.width / r.width - if r.height*s > rTarget.height { - s = rTarget.height / r.height - } - let w = r.width * s - let h = r.height * s - return NSRect(x: rTarget.midX - w/2, y: rTarget.midY - h/2, width: w, height: h) - } - - func calculateWindowPosition(for tScreen: NSScreen, withoutBounds: Bool) -> NSRect { - guard let contentFrame = unfsContentFrame, let screen = unfScreen else { - return frame - } - var newFrame = frameRect(forContentRect: contentFrame) - let targetFrame = tScreen.frame - let targetVisibleFrame = tScreen.visibleFrame - let unfsScreenFrame = screen.frame - let visibleWindow = NSIntersectionRect(unfsScreenFrame, newFrame) - - // calculate visible area of every side - let left = newFrame.origin.x - unfsScreenFrame.origin.x - let right = unfsScreenFrame.size.width - - (newFrame.origin.x - unfsScreenFrame.origin.x + newFrame.size.width) - let bottom = newFrame.origin.y - unfsScreenFrame.origin.y - let top = unfsScreenFrame.size.height - - (newFrame.origin.y - unfsScreenFrame.origin.y + newFrame.size.height) - - // normalize visible areas, decide which one to take horizontal/vertical - var xPer = (unfsScreenFrame.size.width - visibleWindow.size.width) - var yPer = (unfsScreenFrame.size.height - visibleWindow.size.height) - if xPer != 0 { xPer = (left >= 0 || right < 0 ? left : right) / xPer } - if yPer != 0 { yPer = (bottom >= 0 || top < 0 ? bottom : top) / yPer } - - // calculate visible area for every side for target screen - let xNewLeft = targetFrame.origin.x + - (targetFrame.size.width - visibleWindow.size.width) * xPer - let xNewRight = targetFrame.origin.x + targetFrame.size.width - - (targetFrame.size.width - visibleWindow.size.width) * xPer - newFrame.size.width - let yNewBottom = targetFrame.origin.y + - (targetFrame.size.height - visibleWindow.size.height) * yPer - let yNewTop = targetFrame.origin.y + targetFrame.size.height - - (targetFrame.size.height - visibleWindow.size.height) * yPer - newFrame.size.height - - // calculate new coordinates, decide which one to take horizontal/vertical - newFrame.origin.x = left >= 0 || right < 0 ? xNewLeft : xNewRight - newFrame.origin.y = bottom >= 0 || top < 0 ? yNewBottom : yNewTop - - // don't place new window on top of a visible menubar - let topMar = targetFrame.size.height - - (newFrame.origin.y - targetFrame.origin.y + newFrame.size.height) - let menuBarHeight = targetFrame.size.height - - (targetVisibleFrame.size.height + targetVisibleFrame.origin.y) - if topMar < menuBarHeight { - newFrame.origin.y -= top - menuBarHeight - } - - if withoutBounds { - return newFrame - } - - // screen bounds right and left - if newFrame.origin.x + newFrame.size.width > targetFrame.origin.x + targetFrame.size.width { - newFrame.origin.x = targetFrame.origin.x + targetFrame.size.width - newFrame.size.width - } - if newFrame.origin.x < targetFrame.origin.x { - newFrame.origin.x = targetFrame.origin.x - } - - // screen bounds top and bottom - if newFrame.origin.y + newFrame.size.height > targetFrame.origin.y + targetFrame.size.height { - newFrame.origin.y = targetFrame.origin.y + targetFrame.size.height - newFrame.size.height - } - if newFrame.origin.y < targetFrame.origin.y { - newFrame.origin.y = targetFrame.origin.y - } - return newFrame - } - - override func constrainFrameRect(_ frameRect: NSRect, to tScreen: NSScreen?) -> NSRect { - if (isAnimating && !isInFullscreen) || (!isAnimating && isInFullscreen) { - return frameRect - } - - guard let ts: NSScreen = tScreen ?? screen ?? NSScreen.main else { - return frameRect - } - var nf: NSRect = frameRect - let of: NSRect = frame - let vf: NSRect = (isAnimating ? (targetScreen ?? ts) : ts).visibleFrame - let ncf: NSRect = contentRect(forFrameRect: nf) - - // screen bounds top and bottom - if NSMaxY(nf) > NSMaxY(vf) { - nf.origin.y = NSMaxY(vf) - NSHeight(nf) - } - if NSMaxY(ncf) < NSMinY(vf) { - nf.origin.y = NSMinY(vf) + NSMinY(ncf) - NSMaxY(ncf) - } - - // screen bounds right and left - if NSMinX(nf) > NSMaxX(vf) { - nf.origin.x = NSMaxX(vf) - NSWidth(nf) - } - if NSMaxX(nf) < NSMinX(vf) { - nf.origin.x = NSMinX(vf) - } - - if NSHeight(nf) < NSHeight(vf) && NSHeight(of) > NSHeight(vf) && !isInFullscreen { - // If the window height is smaller than the visible frame, but it was - // bigger previously recenter the smaller window vertically. This is - // needed to counter the 'snap to top' behaviour. - nf.origin.y = (NSHeight(vf) - NSHeight(nf)) / 2 - } - return nf - } - - @objc func setNormalWindowSize() { setWindowScale(1.0) } - @objc func setHalfWindowSize() { setWindowScale(0.5) } - @objc func setDoubleWindowSize() { setWindowScale(2.0) } - - func setWindowScale(_ scale: Double) { - mpv?.command("set window-scale \(scale)") - } - - func addWindowScale(_ scale: Double) { - if !isInFullscreen { - mpv?.command("add window-scale \(scale)") - } - } - - func windowDidChangeScreen(_ notification: Notification) { - if screen == nil { - return - } - if !isAnimating && (currentScreen != screen) { - previousScreen = screen - } - if currentScreen != screen { - cocoaCB.updateDisplaylink() - cocoaCB.layer?.update(force: true) - } - currentScreen = screen - } - - func windowDidChangeScreenProfile(_ notification: Notification) { - cocoaCB.layer?.needsICCUpdate = true - } - - func windowDidChangeBackingProperties(_ notification: Notification) { - cocoaCB.layer?.contentsScale = backingScaleFactor - cocoaCB.flagEvents(VO_EVENT_DPI) - } - - func windowWillStartLiveResize(_ notification: Notification) { - cocoaCB.layer?.inLiveResize = true - } - - func windowDidEndLiveResize(_ notification: Notification) { - cocoaCB.layer?.inLiveResize = false - cocoaCB.mpv?.setConfigProperty(maximized: isZoomed) - - if let contentViewFrame = contentView?.frame, - !isAnimating && !isInFullscreen - { - unfsContentFrame = convertToScreen(contentViewFrame) - } - } - - func windowShouldClose(_ sender: NSWindow) -> Bool { - cocoa_put_key(MP_KEY_CLOSE_WIN) - return false - } - - func windowDidMiniaturize(_ notification: Notification) { - cocoaCB.mpv?.setConfigProperty(minimized: true) - } - - func windowDidDeminiaturize(_ notification: Notification) { - cocoaCB.mpv?.setConfigProperty(minimized: false) - } - - func windowDidResignKey(_ notification: Notification) { - cocoaCB.setCursorVisiblility(true) - } - - func windowDidBecomeKey(_ notification: Notification) { - cocoaCB.updateCursorVisibility() - } - - func windowDidChangeOcclusionState(_ notification: Notification) { - if occlusionState.contains(.visible) { - cocoaCB.layer?.update(force: true) - cocoaCB.updateCursorVisibility() - } - } - - func windowWillMove(_ notification: Notification) { - isMoving = true - } - - func windowDidMove(_ notification: Notification) { - cocoaCB.mpv?.setConfigProperty(maximized: isZoomed) - } -} diff --git a/video/out/cocoa_cb_common.swift b/video/out/cocoa_cb_common.swift index c3f0ab2cc8..10e3c30cc1 100644 --- a/video/out/cocoa_cb_common.swift +++ b/video/out/cocoa_cb_common.swift @@ -16,25 +16,13 @@ */ import Cocoa -import IOKit.pwr_mgt -class CocoaCB: NSObject { - - var mpv: MPVHelper? +class CocoaCB: Common { var libmpv: LibmpvHelper - var window: Window? - var titleBar: TitleBar? - var view: EventsView? - var layer: VideoLayer? - var link: CVDisplayLink? + var layer: GLLayer? - var cursorVisibilityWanted: Bool = true @objc var isShuttingDown: Bool = false - var title: String = "mpv" { - didSet { if let window = window { window.title = title } } - } - enum State { case uninitialized case needsInit @@ -42,20 +30,12 @@ class CocoaCB: NSObject { } var backendState: State = .uninitialized - let eventsLock = NSLock() - var events: Int = 0 - - var lightSensor: io_connect_t = 0 - var lastLmu: UInt64 = 0 - var lightSensorIOPort: IONotificationPortRef? - var displaySleepAssertion: IOPMAssertionID = IOPMAssertionID(0) - - let queue: DispatchQueue = DispatchQueue(label: "io.mpv.queue") @objc init(_ mpvHandle: OpaquePointer) { - libmpv = LibmpvHelper(mpvHandle, "cocoacb") - super.init() - layer = VideoLayer(cocoaCB: self) + let newlog = mp_log_new(UnsafeMutablePointer(mpvHandle), mp_client_get_log(mpvHandle), "cocoacb") + libmpv = LibmpvHelper(mpvHandle, newlog) + super.init(newlog) + layer = GLLayer(cocoaCB: self) libmpv.observeString("macos-title-bar-style") libmpv.observeString("macos-title-bar-appearance") @@ -64,17 +44,18 @@ class CocoaCB: NSObject { } func preinit(_ vo: UnsafeMutablePointer) { - mpv = MPVHelper(vo, "cocoacb") + mpv = MPVHelper(vo, log) if backendState == .uninitialized { backendState = .needsInit - view = EventsView(cocoaCB: self) - view?.layer = layer - view?.wantsLayer = true - view?.layerContentsPlacement = .scaleProportionallyToFit - startDisplayLink(vo) - initLightSensor() - addDisplayReconfigureObserver() + + guard let layer = self.layer else { + log.sendError("Something went wrong, no GLLayer was initialized") + exit(1) + } + + initView(vo, layer) + initMisc(vo) } } @@ -97,58 +78,10 @@ class CocoaCB: NSObject { } func initBackend(_ vo: UnsafeMutablePointer) { - NSApp.setActivationPolicy(.regular) - setAppIcon() - - guard let opts: mp_vo_opts = mpv?.opts else { - libmpv.sendError("Something went wrong, no MPVHelper was initialized") - exit(1) - } - guard let view = self.view else { - libmpv.sendError("Something went wrong, no View was initialized") - exit(1) - } - guard let targetScreen = getScreenBy(id: Int(opts.screen_id)) ?? NSScreen.main else { - libmpv.sendError("Something went wrong, no Screen was found") - exit(1) - } - - let wr = getWindowGeometry(forScreen: targetScreen, videoOut: vo) - window = Window(contentRect: wr, screen: targetScreen, view: view, cocoaCB: self) - guard let window = self.window else { - libmpv.sendError("Something went wrong, no Window was initialized") - exit(1) - } - + initApp() + initWindow(vo) updateICCProfile() - window.setOnTop(Bool(opts.ontop), Int(opts.ontop_level)) - window.keepAspect = Bool(opts.keepaspect_window) - window.title = title - window.border = Bool(opts.border) - - titleBar = TitleBar(frame: wr, window: window, cocoaCB: self) - - let minimized = Bool(opts.window_minimized) - window.isRestorable = false - window.isReleasedWhenClosed = false - window.setMaximized(minimized ? false : Bool(opts.window_maximized)) - window.setMinimized(minimized) - window.makeMain() - window.makeKey() - - if !minimized { - window.orderFront(nil) - } - - NSApp.activate(ignoringOtherApps: true) - - if Bool(opts.fullscreen) { - DispatchQueue.main.async { - self.window?.toggleFullScreen(nil) - } - } else { - window.isMovableByWindowBackground = true - } + initWindowState() backendState = .initialized } @@ -157,7 +90,7 @@ class CocoaCB: NSObject { guard let opts: mp_vo_opts = mpv?.opts, let targetScreen = getScreenBy(id: Int(opts.screen_id)) ?? NSScreen.main else { - libmpv.sendWarning("Couldn't update Window size, no Screen available") + log.sendWarning("Couldn't update Window size, no Screen available") return } @@ -172,119 +105,23 @@ class CocoaCB: NSObject { window?.updateSize(wr.size) } - func setAppIcon() { - if let app = NSApp as? Application, - ProcessInfo.processInfo.environment["MPVBUNDLE"] != "true" - { - NSApp.applicationIconImage = app.getMPVIcon() - } - } - - let linkCallback: CVDisplayLinkOutputCallback = { - (displayLink: CVDisplayLink, - inNow: UnsafePointer, - inOutputTime: UnsafePointer, - flagsIn: CVOptionFlags, - flagsOut: UnsafeMutablePointer, - displayLinkContext: UnsafeMutableRawPointer?) -> CVReturn in - let ccb = unsafeBitCast(displayLinkContext, to: CocoaCB.self) - ccb.libmpv.reportRenderFlip() + override func displayLinkCallback(_ displayLink: CVDisplayLink, + _ inNow: UnsafePointer, + _ inOutputTime: UnsafePointer, + _ flagsIn: CVOptionFlags, + _ flagsOut: UnsafeMutablePointer) -> CVReturn + { + libmpv.reportRenderFlip() return kCVReturnSuccess } - func startDisplayLink(_ vo: UnsafeMutablePointer) { - CVDisplayLinkCreateWithActiveCGDisplays(&link) - - guard let opts: mp_vo_opts = mpv?.opts, - let screen = getScreenBy(id: Int(opts.screen_id)) ?? NSScreen.main, - let link = self.link else - { - libmpv.sendWarning("Couldn't start DisplayLink, no MPVHelper, Screen or DisplayLink available") - return - } - - CVDisplayLinkSetCurrentCGDisplay(link, screen.displayID) - if #available(macOS 10.12, *) { - CVDisplayLinkSetOutputHandler(link) { link, now, out, inFlags, outFlags -> CVReturn in - self.libmpv.reportRenderFlip() - return kCVReturnSuccess - } - } else { - CVDisplayLinkSetOutputCallback(link, linkCallback, MPVHelper.bridge(obj: self)) - } - CVDisplayLinkStart(link) - } - - func stopDisplaylink() { - if let link = self.link, CVDisplayLinkIsRunning(link) { - CVDisplayLinkStop(link) - } - } - - func updateDisplaylink() { - guard let screen = window?.screen, let link = self.link else { - libmpv.sendWarning("Couldn't update DisplayLink, no Screen or DisplayLink available") - return - } - - CVDisplayLinkSetCurrentCGDisplay(link, screen.displayID) - queue.asyncAfter(deadline: DispatchTime.now() + 0.1) { - self.flagEvents(VO_EVENT_WIN_STATE) - } - } - - func currentFps() -> Double { - if let link = self.link { - var actualFps = CVDisplayLinkGetActualOutputVideoRefreshPeriod(link) - let nominalData = CVDisplayLinkGetNominalOutputVideoRefreshPeriod(link) - - if (nominalData.flags & Int32(CVTimeFlags.isIndefinite.rawValue)) < 1 { - let nominalFps = Double(nominalData.timeScale) / Double(nominalData.timeValue) - - if actualFps > 0 { - actualFps = 1/actualFps - } - - if fabs(actualFps - nominalFps) > 0.1 { - libmpv.sendVerbose("Falling back to nominal display refresh rate: \(nominalFps)") - return nominalFps - } else { - return actualFps - } - } - } else { - libmpv.sendWarning("No DisplayLink available") - } - - libmpv.sendWarning("Falling back to standard display refresh rate: 60Hz") - return 60.0 - } - - func enableDisplaySleep() { - IOPMAssertionRelease(displaySleepAssertion) - displaySleepAssertion = IOPMAssertionID(0) + override func lightSensorUpdate() { + libmpv.setRenderLux(lmuToLux(lastLmu)) } - func disableDisplaySleep() { - if displaySleepAssertion != IOPMAssertionID(0) { return } - IOPMAssertionCreateWithName( - kIOPMAssertionTypePreventUserIdleDisplaySleep as CFString, - IOPMAssertionLevel(kIOPMAssertionLevelOn), - "io.mpv.video_playing_back" as CFString, - &displaySleepAssertion) - } - - func updateCursorVisibility() { - setCursorVisiblility(cursorVisibilityWanted) - } - - func setCursorVisiblility(_ visible: Bool) { - NSCursor.setHiddenUntilMouseMoves(!visible && (view?.canHideCursor() ?? false)) - } - - func updateICCProfile() { + override func updateICCProfile() { guard let colorSpace = window?.screen?.colorSpace else { - libmpv.sendWarning("Couldn't update ICC Profile, no color space available") + log.sendWarning("Couldn't update ICC Profile, no color space available") return } @@ -294,288 +131,86 @@ class CocoaCB: NSObject { } } - func lmuToLux(_ v: UInt64) -> Int { - // the polinomial approximation for apple lmu value -> lux was empirically - // derived by firefox developers (Apple provides no documentation). - // https://bugzilla.mozilla.org/show_bug.cgi?id=793728 - let power_c4: Double = 1 / pow(10, 27) - let power_c3: Double = 1 / pow(10, 19) - let power_c2: Double = 1 / pow(10, 12) - let power_c1: Double = 1 / pow(10, 5) - - let lum = Double(v) - let term4: Double = -3.0 * power_c4 * pow(lum, 4.0) - let term3: Double = 2.6 * power_c3 * pow(lum, 3.0) - let term2: Double = -3.4 * power_c2 * pow(lum, 2.0) - let term1: Double = 3.9 * power_c1 * lum - - let lux = Int(ceil(term4 + term3 + term2 + term1 - 0.19)) - return lux > 0 ? lux : 0 + override func windowDidEndAnimation() { + lay