summaryrefslogtreecommitdiffstats
path: root/video/out/cocoa-cb
diff options
context:
space:
mode:
Diffstat (limited to 'video/out/cocoa-cb')
-rw-r--r--video/out/cocoa-cb/events_view.swift296
-rw-r--r--video/out/cocoa-cb/title_bar.swift256
-rw-r--r--video/out/cocoa-cb/video_layer.swift323
-rw-r--r--video/out/cocoa-cb/window.swift543
4 files changed, 0 insertions, 1418 deletions
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 <http://www.gnu.org/licenses/>.
- */
-
-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 <http://www.gnu.org/licenses/>.
- */
-
-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 <http://www.gnu.org/licenses/>.
- */
-
-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<CVTimeStamp>?) -> 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<CVTimeStamp>?) {
- 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 <http://www.gnu.org/licenses/>.
- */
-
-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 {
-