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 /osdep | |
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.
Diffstat (limited to 'osdep')
-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 |
5 files changed, 156 insertions, 110 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) + } +} |