diff options
author | der richter <der.richter@gmx.de> | 2019-09-29 18:35:12 +0200 |
---|---|---|
committer | der richter <der.richter@gmx.de> | 2019-10-06 13:29:48 +0200 |
commit | 6d0f0546ee851f4106438c5b92c8d1d152937ea7 (patch) | |
tree | f4efc2a603081fe75c19bea86c261d5320716c5b | |
parent | 2b19a7c964b821945e3ea06cfa81c1c064f2504d (diff) | |
download | mpv-6d0f0546ee851f4106438c5b92c8d1d152937ea7.tar.bz2 mpv-6d0f0546ee851f4106438c5b92c8d1d152937ea7.tar.xz |
cocoa-cb: remove get_property_* usages and split up mpv helper
all the get_property_* usages were removed because in some circumstances
they can lead to deadlocks. they were replaced by accessing the vo and
mp_vo_opts structs directly, like on other vos.
additionally the mpv helper was split into a mpv and libmpv helper, to
differentiate between private and public APIs and for future changes
like a macOS vulkan context for vo=gpu.
-rw-r--r-- | osdep/macos/libmpv_helper.swift (renamed from osdep/macOS_mpv_helper.swift) | 133 | ||||
-rw-r--r-- | osdep/macos/log_helper.swift | 48 | ||||
-rw-r--r-- | osdep/macos/mpv_helper.swift | 78 | ||||
-rw-r--r-- | osdep/macos/swift_compat.swift (renamed from osdep/macOS_swift_compat.swift) | 0 | ||||
-rw-r--r-- | osdep/macos/swift_extensions.swift (renamed from osdep/macOS_swift_extensions.swift) | 7 | ||||
-rw-r--r-- | video/out/cocoa-cb/events_view.swift | 31 | ||||
-rw-r--r-- | video/out/cocoa-cb/title_bar.swift | 10 | ||||
-rw-r--r-- | video/out/cocoa-cb/video_layer.swift | 53 | ||||
-rw-r--r-- | video/out/cocoa-cb/window.swift | 48 | ||||
-rw-r--r-- | video/out/cocoa_cb_common.swift | 114 | ||||
-rw-r--r-- | wscript | 2 | ||||
-rw-r--r-- | wscript_build.py | 8 |
12 files changed, 285 insertions, 247 deletions
diff --git a/osdep/macOS_mpv_helper.swift b/osdep/macos/libmpv_helper.swift index 3494c60cfd..bf069efc6b 100644 --- a/osdep/macOS_mpv_helper.swift +++ b/osdep/macos/libmpv_helper.swift @@ -21,36 +21,23 @@ import OpenGL.GL3 let glDummy: @convention(c) () -> Void = {} -extension Bool { - init(_ num: Int32) { - self.init(num > 0) - } -} - -class MPVHelper: NSObject { +class LibmpvHelper: LogHelper { var mpvHandle: OpaquePointer? var mpvRenderContext: OpaquePointer? - var mpvLog: OpaquePointer? - var inputContext: OpaquePointer? - var mpctx: UnsafeMutablePointer<MPContext>? - var vo: UnsafeMutablePointer<vo>? - var macOpts: macos_opts? + var macOpts: macos_opts = macos_opts() var fbo: GLint = 1 let deinitLock = NSLock() - init(_ mpv: OpaquePointer) { - super.init() + init(_ mpv: OpaquePointer, _ name: String) { + let newlog = mp_log_new(UnsafeMutablePointer<MPContext>(mpv), mp_client_get_log(mpv), name) + super.init(newlog) mpvHandle = mpv - mpvLog = mp_log_new(UnsafeMutablePointer<MPContext>(mpvHandle), - mp_client_get_log(mpvHandle), "cocoacb") + guard let mpctx = UnsafeMutablePointer<MPContext>(mp_client_get_core(mpvHandle)) else { sendError("No MPContext available") exit(1) } - - self.mpctx = mpctx - inputContext = mpctx.pointee.input guard let app = NSApp as? Application, let ptr = mp_get_config_group(mpctx, mp_client_get_global(mpvHandle), @@ -60,14 +47,6 @@ class MPVHelper: NSObject { exit(1) } macOpts = UnsafeMutablePointer<macos_opts>(OpaquePointer(ptr)).pointee - - mpv_observe_property(mpvHandle, 0, "ontop", MPV_FORMAT_FLAG) - mpv_observe_property(mpvHandle, 0, "border", MPV_FORMAT_FLAG) - mpv_observe_property(mpvHandle, 0, "keepaspect-window", MPV_FORMAT_FLAG) - mpv_observe_property(mpvHandle, 0, "macos-title-bar-style", MPV_FORMAT_STRING) - mpv_observe_property(mpvHandle, 0, "macos-title-bar-appearance", MPV_FORMAT_STRING) - mpv_observe_property(mpvHandle, 0, "macos-title-bar-material", MPV_FORMAT_STRING) - mpv_observe_property(mpvHandle, 0, "macos-title-bar-color", MPV_FORMAT_STRING) } func initRender() { @@ -197,11 +176,6 @@ class MPVHelper: NSObject { mpv_render_context_set_parameter(mpvRenderContext, params) } - func command(_ cmd: String) { - if mpvHandle == nil { return } - mpv_command_string(mpvHandle, cmd) - } - func commandAsync(_ cmd: [String?], id: UInt64 = 1) { if mpvHandle == nil { return } var mCmd = cmd @@ -211,6 +185,20 @@ class MPVHelper: NSObject { for ptr in cargs { free(UnsafeMutablePointer(mutating: ptr)) } } + func observeString(_ property: String) { + mpv_observe_property(mpvHandle, 0, property, MPV_FORMAT_STRING) + } + + func observeFlag(_ property: String) { + mpv_observe_property(mpvHandle, 0, property, MPV_FORMAT_FLAG) + } + + // Unsafe function when called while using the render API + func command(_ cmd: String) { + if mpvHandle == nil { return } + mpv_command_string(mpvHandle, cmd) + } + func getBoolProperty(_ name: String) -> Bool { if mpvHandle == nil { return false } var value = Int32() @@ -226,76 +214,13 @@ class MPVHelper: NSObject { } func getStringProperty(_ name: String) -> String? { - guard let mpv = mpvHandle, - let value = mpv_get_property_string(mpv, name) else - { - return nil - } - + guard let mpv = mpvHandle else { return nil } + guard let value = mpv_get_property_string(mpv, name) else { return nil } let str = String(cString: value) mpv_free(value) return str } - func canBeDraggedAt(_ pos: NSPoint) -> Bool { - guard let input = inputContext else { return false } - let canDrag = !mp_input_test_dragging(input, Int32(pos.x), Int32(pos.y)) - return canDrag - } - - func setMousePosition(_ pos: NSPoint) { - guard let input = inputContext else { return } - mp_input_set_mouse_pos(input, Int32(pos.x), Int32(pos.y)) - } - - func putAxis(_ mpkey: Int32, delta: Double) { - guard let input = inputContext else { return } - mp_input_put_wheel(input, mpkey, delta) - } - - func sendVerbose(_ msg: String) { - send(message: msg, type: MSGL_V) - } - - func sendInfo(_ msg: String) { - send(message: msg, type: MSGL_INFO) - } - - func sendWarning(_ msg: String) { - send(message: msg, type: MSGL_WARN) - } - - func sendError(_ msg: String) { - send(message: msg, type: MSGL_ERR) - } - - func send(message msg: String, type t: Int) { - if mpvLog == nil { - sendFallback(message: msg, type: t) - } else { - let args: [CVarArg] = [ (msg as NSString).utf8String ?? "NO MESSAGE"] - mp_msg_va(mpvLog, Int32(t), "%s\n", getVaList(args)) - } - } - - func sendFallback(message msg: String, type t: Int) { - var level = "\u{001B}" - switch t { - case MSGL_V: - level += "[0;30m[VERBOSE]" - case MSGL_INFO: - level += "[0;30m[INFO]" - case MSGL_WARN: - level += "[0;33m" - case MSGL_ERR: - level += "[0;31m" - default: - level += "[0;30m" - } - - print("\(level)[osx/cocoacb] \(msg)\u{001B}[0;30m") - } - func deinitRender() { mpv_render_context_set_update_callback(mpvRenderContext, nil, nil) mp_render_context_set_control_callback(mpvRenderContext, nil, nil) @@ -310,19 +235,7 @@ class MPVHelper: NSObject { mpv_destroy(mpvHandle) } mpvHandle = nil - mpvLog = nil - inputContext = nil - mpctx = nil - } - - // (__bridge void*) - class func bridge<T: AnyObject>(obj: T) -> UnsafeMutableRawPointer { - return UnsafeMutableRawPointer(Unmanaged.passUnretained(obj).toOpaque()) - } - - // (__bridge T*) - class func bridge<T: AnyObject>(ptr: UnsafeRawPointer) -> T { - return Unmanaged<T>.fromOpaque(ptr).takeUnretainedValue() + log = nil } // *(char **) MPV_FORMAT_STRING on mpv_event_property diff --git a/osdep/macos/log_helper.swift b/osdep/macos/log_helper.swift new file mode 100644 index 0000000000..6d834c0631 --- /dev/null +++ b/osdep/macos/log_helper.swift @@ -0,0 +1,48 @@ +/* + * 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 LogHelper: NSObject { + + var log: OpaquePointer? + + init(_ log: OpaquePointer?) { + self.log = log + } + + func sendVerbose(_ msg: String) { + send(message: msg, type: MSGL_V) + } + + func sendInfo(_ msg: String) { + send(message: msg, type: MSGL_INFO) + } + + func sendWarning(_ msg: String) { + send(message: msg, type: MSGL_WARN) + } + + func sendError(_ msg: String) { + send(message: msg, type: MSGL_ERR) + } + + func send(message msg: String, type t: Int) { + let args: [CVarArg] = [ (msg as NSString).utf8String ?? "NO MESSAGE"] + mp_msg_va(log, Int32(t), "%s\n", getVaList(args)) + } +} diff --git a/osdep/macos/mpv_helper.swift b/osdep/macos/mpv_helper.swift new file mode 100644 index 0000000000..ce1fb5ffec --- /dev/null +++ b/osdep/macos/mpv_helper.swift @@ -0,0 +1,78 @@ +/* + * 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 MPVHelper: LogHelper { + + var vo: UnsafeMutablePointer<vo> + var vout: vo { get { return vo.pointee } } + var opts: mp_vo_opts { get { return vout.opts.pointee } } + var input: OpaquePointer { get { return vout.input_ctx } } + var macOpts: macos_opts = macos_opts() + + init(_ vo: UnsafeMutablePointer<vo>, _ name: String) { + self.vo = vo + let newlog = mp_log_new(vo, vo.pointee.log, name) + + super.init(newlog) + + guard let app = NSApp as? Application, + let ptr = mp_get_config_group(vo, + vo.pointee.global, + app.getMacOSConf()) else + { + sendError("macOS config group couldn't be retrieved'") + exit(1) + } + macOpts = UnsafeMutablePointer<macos_opts>(OpaquePointer(ptr)).pointee + } + + func canBeDraggedAt(_ pos: NSPoint) -> Bool { + let canDrag = !mp_input_test_dragging(input, Int32(pos.x), Int32(pos.y)) + return canDrag + } + + func mouseEnabled() -> Bool { + return mp_input_mouse_enabled(input) + } + + func setMousePosition(_ pos: NSPoint) { + mp_input_set_mouse_pos(input, Int32(pos.x), Int32(pos.y)) + } + + func putAxis(_ mpkey: Int32, delta: Double) { + mp_input_put_wheel(input, mpkey, delta) + } + + func command(_ cmd: String) { + let cCmd = UnsafePointer<Int8>(strdup(cmd)) + let mpvCmd = mp_input_parse_cmd(input, bstr0(cCmd), "") + mp_input_queue_cmd(input, mpvCmd) + free(UnsafeMutablePointer(mutating: cCmd)) + } + + // (__bridge void*) + class func bridge<T: AnyObject>(obj: T) -> UnsafeMutableRawPointer { + return UnsafeMutableRawPointer(Unmanaged.passUnretained(obj).toOpaque()) + } + + // (__bridge T*) + class func bridge<T: AnyObject>(ptr: UnsafeRawPointer) -> T { + return Unmanaged<T>.fromOpaque(ptr).takeUnretainedValue() + } +} diff --git a/osdep/macOS_swift_compat.swift b/osdep/macos/swift_compat.swift index c14aa08282..c14aa08282 100644 --- a/osdep/macOS_swift_compat.swift +++ b/osdep/macos/swift_compat.swift diff --git a/osdep/macOS_swift_extensions.swift b/osdep/macos/swift_extensions.swift index 1e30cf4df7..c48ad6e798 100644 --- a/osdep/macOS_swift_extensions.swift +++ b/osdep/macos/swift_extensions.swift @@ -73,3 +73,10 @@ extension NSColor { self.init(calibratedRed: red, green: green, blue: blue, alpha: alpha) } } + +extension Bool { + + init(_ int32: Int32) { + self.init(int32 != 0) + } +} diff --git a/video/out/cocoa-cb/events_view.swift b/video/out/cocoa-cb/events_view.swift index 59441d9793..31533bbb98 100644 --- a/video/out/cocoa-cb/events_view.swift +++ b/video/out/cocoa-cb/events_view.swift @@ -19,8 +19,8 @@ import Cocoa class EventsView: NSView { - weak var cocoaCB: CocoaCB! - var mpv: MPVHelper { get { return cocoaCB.mpv } } + unowned var cocoaCB: CocoaCB + var mpv: MPVHelper? { get { return cocoaCB.mpv } } var tracker: NSTrackingArea? var hasMouseDown: Bool = false @@ -117,64 +117,64 @@ class EventsView: NSView { } override func mouseEntered(with event: NSEvent) { - if mpv.getBoolProperty("input-cursor") { + if mpv?.mouseEnabled() ?? true { cocoa_put_key_with_modifiers(SWIFT_KEY_MOUSE_ENTER, 0) } } override func mouseExited(with event: NSEvent) { - if mpv.getBoolProperty("input-cursor") { + if mpv?.mouseEnabled() ?? true { cocoa_put_key_with_modifiers(SWIFT_KEY_MOUSE_LEAVE, 0) } cocoaCB.titleBar?.hide() } override func mouseMoved(with event: NSEvent) { - if mpv.getBoolProperty("input-cursor") { + if mpv?.mouseEnabled() ?? true { signalMouseMovement(event) } cocoaCB.titleBar?.show() } override func mouseDragged(with event: NSEvent) { - if mpv.getBoolProperty("input-cursor") { + if mpv?.mouseEnabled() ?? true { signalMouseMovement(event) } } override func mouseDown(with event: NSEvent) { - if mpv.getBoolProperty("input-cursor") { + if mpv?.mouseEnabled() ?? true { signalMouseDown(event) } } override func mouseUp(with event: NSEvent) { - if mpv.getBoolProperty("input-cursor") { + if mpv?.mouseEnabled() ?? true { signalMouseUp(event) } cocoaCB.window?.isMoving = false } override func rightMouseDown(with event: NSEvent) { - if mpv.getBoolProperty("input-cursor") { + if mpv?.mouseEnabled() ?? true { signalMouseDown(event) } } override func rightMouseUp(with event: NSEvent) { - if mpv.getBoolProperty("input-cursor") { + if mpv?.mouseEnabled() ?? true { signalMouseUp(event) } } override func otherMouseDown(with event: NSEvent) { - if mpv.getBoolProperty("input-cursor") { + if mpv?.mouseEnabled() ?? true { signalMouseDown(event) } } override func otherMouseUp(with event: NSEvent) { - if mpv.getBoolProperty("input-cursor") { + if mpv?.mouseEnabled() ?? true { signalMouseUp(event) } } @@ -203,7 +203,7 @@ class EventsView: NSView { cocoaCB.window?.updateMovableBackground(point) if !(cocoaCB.window?.isMoving ?? false) { - mpv.setMousePosition(point) + mpv?.setMousePosition(point) } } @@ -219,11 +219,11 @@ class EventsView: NSView { cmd = delta > 0 ? SWIFT_WHEEL_RIGHT : SWIFT_WHEEL_LEFT; } - mpv.putAxis(cmd, delta: abs(delta)) + mpv?.putAxis(cmd, delta: abs(delta)) } override func scrollWheel(with event: NSEvent) { - if !mpv.getBoolProperty("input-cursor") { + if !(mpv?.mouseEnabled() ?? true) { return } @@ -246,7 +246,6 @@ class EventsView: NSView { } func containsMouseLocation() -> Bool { - if cocoaCB == nil { return false } var topMargin: CGFloat = 0.0 let menuBarHeight = NSApp.mainMenu?.menuBarHeight ?? 23.0 diff --git a/video/out/cocoa-cb/title_bar.swift b/video/out/cocoa-cb/title_bar.swift index 41d04a19ad..20812e9029 100644 --- a/video/out/cocoa-cb/title_bar.swift +++ b/video/out/cocoa-cb/title_bar.swift @@ -19,8 +19,8 @@ import Cocoa class TitleBar: NSVisualEffectView { - weak var cocoaCB: CocoaCB! - var mpv: MPVHelper { get { return cocoaCB.mpv } } + unowned var cocoaCB: CocoaCB + var libmpv: LibmpvHelper { get { return cocoaCB.libmpv } } var systemBar: NSView? { get { return cocoaCB.window?.standardWindowButton(.closeButton)?.superview } @@ -67,9 +67,9 @@ class TitleBar: NSVisualEffectView { window.contentView?.addSubview(self, positioned: .above, relativeTo: nil) window.titlebarAppearsTransparent = true window.styleMask.insert(.fullSizeContentView) - set(appearance: Int(mpv.macOpts?.macos_title_bar_appearance ?? 0)) - set(material: Int(mpv.macOpts?.macos_title_bar_material ?? 0)) - set(color: mpv.macOpts?.macos_title_bar_color ?? "#00000000") + 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) { diff --git a/video/out/cocoa-cb/video_layer.swift b/video/out/cocoa-cb/video_layer.swift index 95727ee01f..90a37ae0c5 100644 --- a/video/out/cocoa-cb/video_layer.swift +++ b/video/out/cocoa-cb/video_layer.swift @@ -66,8 +66,8 @@ let attributeLookUp: [UInt32:String] = [ class VideoLayer: CAOpenGLLayer { - weak var cocoaCB: CocoaCB! - var mpv: MPVHelper { get { return cocoaCB.mpv } } + unowned var cocoaCB: CocoaCB + var libmpv: LibmpvHelper { get { return cocoaCB.libmpv } } let displayLock = NSLock() let cglContext: CGLContextObj @@ -101,8 +101,8 @@ class VideoLayer: CAOpenGLLayer { init(cocoaCB ccb: CocoaCB) { cocoaCB = ccb - (cglPixelFormat, bufferDepth) = VideoLayer.createPixelFormat(ccb.mpv) - cglContext = VideoLayer.createContext(ccb.mpv, cglPixelFormat) + (cglPixelFormat, bufferDepth) = VideoLayer.createPixelFormat(ccb.libmpv) + cglContext = VideoLayer.createContext(ccb.libmpv, cglPixelFormat) super.init() autoresizingMask = [.layerWidthSizable, .layerHeightSizable] backgroundColor = NSColor.black.cgColor @@ -115,9 +115,9 @@ class VideoLayer: CAOpenGLLayer { CGLSetParameter(cglContext, kCGLCPSwapInterval, &i) CGLSetCurrentContext(cglContext) - mpv.initRender() - mpv.setRenderUpdateCallback(updateCallback, context: self) - mpv.setRenderControlCallback(cocoaCB.controlCallback, context: cocoaCB) + libmpv.initRender() + libmpv.setRenderUpdateCallback(updateCallback, context: self) + libmpv.setRenderControlCallback(cocoaCB.controlCallback, context: cocoaCB) } //necessary for when the layer containing window changes the screen @@ -144,7 +144,7 @@ class VideoLayer: CAOpenGLLayer { isAsynchronous = false } return cocoaCB.backendState == .initialized && - (forceDraw || mpv.isRenderUpdateFrame()) + (forceDraw || libmpv.isRenderUpdateFrame()) } override func draw(inCGLContext ctx: CGLContextObj, @@ -163,7 +163,7 @@ class VideoLayer: CAOpenGLLayer { } updateSurfaceSize() - mpv.drawRender(surfaceSize, bufferDepth, ctx) + libmpv.drawRender(surfaceSize, bufferDepth, ctx) if needsICCUpdate { needsICCUpdate = false @@ -218,8 +218,8 @@ class VideoLayer: CAOpenGLLayer { CATransaction.flush() if isUpdate && needsFlip { CGLSetCurrentContext(cglContext) - if mpv.isRenderUpdateFrame() { - mpv.drawRender(NSZeroSize, bufferDepth, cglContext, skip: true) + if libmpv.isRenderUpdateFrame() { + libmpv.drawRender(NSZeroSize, bufferDepth, cglContext, skip: true) } } displayLock.unlock() @@ -235,29 +235,29 @@ class VideoLayer: CAOpenGLLayer { } } - class func createPixelFormat(_ mpv: MPVHelper) -> (CGLPixelFormatObj, GLint) { + class func createPixelFormat(_ libmpv: LibmpvHelper) -> (CGLPixelFormatObj, GLint) { var pix: CGLPixelFormatObj? var depth: GLint = 8 var err: CGLError = CGLError(rawValue: 0) - let swRender = mpv.macOpts?.cocoa_cb_sw_renderer ?? -1 + let swRender = libmpv.macOpts.cocoa_cb_sw_renderer if swRender != 1 { - (pix, depth, err) = VideoLayer.findPixelFormat(mpv) + (pix, depth, err) = VideoLayer.findPixelFormat(libmpv) } if (err != kCGLNoError || pix == nil) && swRender != 0 { - (pix, depth, err) = VideoLayer.findPixelFormat(mpv, software: true) + (pix, depth, err) = VideoLayer.findPixelFormat(libmpv, software: true) } guard let pixelFormat = pix, err == kCGLNoError else { - mpv.sendError("Couldn't create any CGL pixel format") + libmpv.sendError("Couldn't create any CGL pixel format") exit(1) } return (pixelFormat, depth) } - class func findPixelFormat(_ mpv: MPVHelper, software: Bool = false) -> (CGLPixelFormatObj?, GLint, CGLError) { + class func findPixelFormat(_ libmpv: LibmpvHelper, software: Bool = false) -> (CGLPixelFormatObj?, GLint, CGLError) { var pix: CGLPixelFormatObj? var err: CGLError = CGLError(rawValue: 0) var npix: GLint = 0 @@ -267,7 +267,7 @@ class VideoLayer: CAOpenGLLayer { glBase.insert(CGLPixelFormatAttribute(ver.rawValue), at: 1) var glFormat = [glBase] - if (mpv.macOpts?.cocoa_cb_10bit_context == 1) { + if (libmpv.macOpts.cocoa_cb_10bit_context == 1) { glFormat += [glFormat10Bit] } glFormat += glFormatOptional @@ -283,7 +283,7 @@ class VideoLayer: CAOpenGLLayer { return attributeLookUp[value.rawValue] ?? String(value.rawValue) }) - mpv.sendVerbose("Created CGL pixel format with attributes: " + + libmpv.sendVerbose("Created CGL pixel format with attributes: " + "\(attArray.joined(separator: ", "))") return (pix, glFormat.contains(glFormat10Bit) ? 16 : 8, err) } @@ -291,24 +291,23 @@ class VideoLayer: CAOpenGLLayer { } let errS = String(cString: CGLErrorString(err)) - mpv.sendWarning("Couldn't create a " + - "\(software ? "software" : "hardware accelerated") " + - "CGL pixel format: \(errS) (\(err.rawValue))") - - if software == false && (mpv.macOpts?.cocoa_cb_sw_renderer ?? -1) == -1 { - mpv.sendWarning("Falling back to software renderer") + 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(_ mpv: MPVHelper, _ pixelFormat: CGLPixelFormatObj) -> CGLContextObj { + 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)) - mpv.sendError("Couldn't create a CGLContext: " + errS) + libmpv.sendError("Couldn't create a CGLContext: " + errS) exit(1) } diff --git a/video/out/cocoa-cb/window.swift b/video/out/cocoa-cb/window.swift index 2f87711d22..1ac0e2bc78 100644 --- a/video/out/cocoa-cb/window.swift +++ b/video/out/cocoa-cb/window.swift @@ -20,7 +20,8 @@ import Cocoa class Window: NSWindow, NSWindowDelegate { weak var cocoaCB: CocoaCB! = nil - var mpv: MPVHelper { get { return cocoaCB.mpv } } + var mpv: MPVHelper? { get { return cocoaCB.mpv } } + var libmpv: LibmpvHelper { get { return cocoaCB.libmpv } } var targetScreen: NSScreen? var previousScreen: NSScreen? @@ -134,7 +135,7 @@ class Window: NSWindow, NSWindowDelegate { setFrame(frame, display: true) } - if mpv.getBoolProperty("native-fs") { + if Bool(mpv?.opts.native_fs ?? 1) { super.toggleFullScreen(sender) } else { if !isInFullscreen { @@ -245,48 +246,35 @@ class Window: NSWindow, NSWindowDelegate { cocoaCB.layer?.update() } - func getFsAnimationDuration(_ def: Double) -> Double{ - let duration = mpv.getStringProperty("macos-fs-animation-duration") ?? "default" - if duration == "default" { + func getFsAnimationDuration(_ def: Double) -> Double { + let duration = libmpv.macOpts.macos_fs_animation_duration + if duration < 0 { return def } else { - return (Double(duration) ?? 0.2)/1000 + return Double(duration)/1000 } } - func setOnTop(_ state: Bool, _ ontopLevel: Any) { - let stdLevel: NSWindow.Level = .normal - + func setOnTop(_ state: Bool, _ ontopLevel: Int) { if state { - if ontopLevel is Int { - switch ontopLevel as? Int { - case .some(-1): - level = .floating - case .some(-2): - level = .statusBar + 1 - default: - level = NSWindow.Level(ontopLevel as? Int ?? stdLevel.rawValue) - } - } else { - switch ontopLevel as? String { - case .some("window"): - level = .floating - case .some("system"): - level = .statusBar + 1 - default: - level = NSWindow.Level(Int(ontopLevel as? String ?? "") ?? stdLevel.rawValue) - } + switch ontopLevel { + case -1: + level = .floating + case -2: + level = .statusBar + 1 + default: + level = NSWindow.Level(ontopLevel) } collectionBehavior.remove(.transient) collectionBehavior.insert(.managed) } else { - level = stdLevel + level = .normal } } func updateMovableBackground(_ pos: NSPoint) { if !isInFullscreen { - isMovableByWindowBackground = mpv.canBeDraggedAt(pos) + isMovableByWindowBackground = mpv?.canBeDraggedAt(pos) ?? true } else { isMovableByWindowBackground = false } @@ -448,7 +436,7 @@ class Window: NSWindow, NSWindowDelegate { @objc func setDoubleWindowSize() { setWindowScale(2.0) } func setWindowScale(_ scale: Double) { - mpv.commandAsync(["osd-auto", "set", "window-scale", "\(scale)"]) + mpv?.command("set window-scale \(scale)") } func windowDidChangeScreen(_ notification: Notification) { diff --git a/video/out/cocoa_cb_common.swift b/video/out/cocoa_cb_common.swift index b08f102964..504f0dfad6 100644 --- a/video/out/cocoa_cb_common.swift +++ b/video/out/cocoa_cb_common.swift @@ -20,7 +20,8 @@ import IOKit.pwr_mgt class CocoaCB: NSObject { - var mpv: MPVHelper + var mpv: MPVHelper? + var libmpv: LibmpvHelper var window: Window? var titleBar: TitleBar? var view: EventsView? @@ -53,15 +54,24 @@ class CocoaCB: NSObject { let queue: DispatchQueue = DispatchQueue(label: "io.mpv.queue") @objc init(_ mpvHandle: OpaquePointer) { - mpv = MPVHelper(mpvHandle) + libmpv = LibmpvHelper(mpvHandle, "cocoacb") super.init() layer = VideoLayer(cocoaCB: self) + + libmpv.observeFlag("ontop") + libmpv.observeFlag("border") + libmpv.observeFlag("keepaspect-window") + libmpv.observeString("macos-title-bar-style") + libmpv.observeString("macos-title-bar-appearance") + libmpv.observeString("macos-title-bar-material") + libmpv.observeString("macos-title-bar-color") } func preinit(_ vo: UnsafeMutablePointer<vo>) { if backendState == .uninitialized { backendState = .needsInit + mpv = MPVHelper(vo, "cocoacb") view = EventsView(cocoaCB: self) view?.layer = layer view?.wantsLayer = true @@ -77,7 +87,7 @@ class CocoaCB: NSObject { } func reconfig(_ vo: UnsafeMutablePointer<vo>) { - mpv.vo = vo + mpv?.vo = vo if backendState == .needsInit { DispatchQueue.main.sync { self.initBackend(vo) } } else { @@ -94,18 +104,18 @@ class CocoaCB: NSObject { setAppIcon() guard let view = self.view else { - mpv.sendError("Something went wrong, no View was initialized") + libmpv.sendError("Something went wrong, no View was initialized") exit(1) } guard let targetScreen = getScreenBy(id: Int(opts.screen_id)) ?? NSScreen.main else { - mpv.sendError("Something went wrong, no Screen was found") + 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 { - mpv.sendError("Something went wrong, no Window was initialized") + libmpv.sendError("Something went wrong, no Window was initialized") exit(1) } @@ -136,7 +146,7 @@ class CocoaCB: NSObject { func updateWindowSize(_ vo: UnsafeMutablePointer<vo>) { let opts: mp_vo_opts = vo.pointee.opts.pointee guard let targetScreen = getScreenBy(id: Int(opts.screen_id)) ?? NSScreen.main else { - mpv.sendWarning("Couldn't update Window size, no Screen available") + libmpv.sendWarning("Couldn't update Window size, no Screen available") return } @@ -162,7 +172,7 @@ class CocoaCB: NSObject { flagsOut: UnsafeMutablePointer<CVOptionFlags>, displayLinkContext: UnsafeMutableRawPointer?) -> CVReturn in let ccb = unsafeBitCast(displayLinkContext, to: CocoaCB.self) - ccb.mpv.reportRenderFlip() + ccb.libmpv.reportRenderFlip() return kCVReturnSuccess } @@ -173,14 +183,14 @@ class CocoaCB: NSObject { guard let screen = getScreenBy(id: Int(opts.screen_id)) ?? NSScreen.main, let link = self.link else { - mpv.sendWarning("Couldn't start DisplayLink, no Screen or DisplayLink available") + libmpv.sendWarning("Couldn't start DisplayLink, no Screen or DisplayLink available") return } CVDisplayLinkSetCurrentCGDisplay(link, screen.displayID) if #available(macOS 10.12, *) { CVDisplayLinkSetOutputHandler(link) { link, now, out, inFlags, outFlags -> CVReturn in - self.mpv.reportRenderFlip() + self.libmpv.reportRenderFlip() return kCVReturnSuccess } } else { @@ -197,7 +207,7 @@ class CocoaCB: NSObject { func updateDisplaylink() { guard let screen = window?.screen, let link = self.link else { - mpv.sendWarning("Couldn't update DisplayLink, no Screen or DisplayLink available") + libmpv.sendWarning("Couldn't update DisplayLi |