1
0
mirror of https://github.com/mpv-player/mpv synced 2024-12-29 02:22:19 +00:00

mac/option: rename option structs to properly represent their content

also optimise option cache setup.
This commit is contained in:
der richter 2024-03-19 23:15:42 +01:00
parent f72dfd48d0
commit 204e3f0df6
10 changed files with 88 additions and 94 deletions

View File

@ -181,12 +181,12 @@ static const char mac_icon[] =
#endif
}
+ (const struct m_sub_options *)getMacOSConf
+ (const struct m_sub_options *)getMacConf
{
return &macos_conf;
}
+ (const struct m_sub_options *)getVoSubConf
+ (const struct m_sub_options *)getVoConf
{
return &vo_sub_opts;
}

View File

@ -28,8 +28,8 @@ struct mpv_handle;
- (NSImage *)getMPVIcon;
- (void)processEvent:(struct mpv_event *)event;
- (void)initCocoaCb:(struct mpv_handle *)ctx;
+ (const struct m_sub_options *)getMacOSConf;
+ (const struct m_sub_options *)getVoSubConf;
+ (const struct m_sub_options *)getMacConf;
+ (const struct m_sub_options *)getVoConf;
@property(nonatomic, retain) MenuBar *menuBar;
@property(nonatomic, assign) size_t openCount;

View File

@ -232,11 +232,11 @@ class InputHelper: NSObject {
@objc func open(files: [String]) {
lock.withLock {
guard let input = input else { return }
if (option?.opts.drag_and_drop ?? -1) == -2 { return }
if (option?.vo.drag_and_drop ?? -1) == -2 { return }
var action = NSEvent.modifierFlags.contains(.shift) ? DND_APPEND : DND_REPLACE
if (option?.opts.drag_and_drop ?? -1) >= 0 {
action = mp_dnd_action(UInt32(option?.opts.drag_and_drop ?? Int32(DND_REPLACE.rawValue)))
if (option?.vo.drag_and_drop ?? -1) >= 0 {
action = mp_dnd_action(UInt32(option?.vo.drag_and_drop ?? Int32(DND_REPLACE.rawValue)))
}
let filesClean = files.map{ $0.hasPrefix("file:///.file/id=") ? (URL(string: $0)?.path ?? $0) : $0 }

View File

@ -20,61 +20,55 @@ import Cocoa
typealias swift_wakeup_cb_fn = (@convention(c) (UnsafeMutableRawPointer?) -> Void)?
class OptionHelper: NSObject {
var optsCachePtr: UnsafeMutablePointer<m_config_cache>
var macOptsCachePtr: UnsafeMutablePointer<m_config_cache>
var voCachePtr: UnsafeMutablePointer<m_config_cache>
var macCachePtr: UnsafeMutablePointer<m_config_cache>
var optsPtr: UnsafeMutablePointer<mp_vo_opts>
{ get { return UnsafeMutablePointer<mp_vo_opts>(OpaquePointer(optsCachePtr.pointee.opts)) } }
var macOptsPtr: UnsafeMutablePointer<macos_opts>
{ get { return UnsafeMutablePointer<macos_opts>(OpaquePointer(macOptsCachePtr.pointee.opts)) } }
var voPtr: UnsafeMutablePointer<mp_vo_opts>
{ get { return UnsafeMutablePointer<mp_vo_opts>(OpaquePointer(voCachePtr.pointee.opts)) } }
var macPtr: UnsafeMutablePointer<macos_opts>
{ get { return UnsafeMutablePointer<macos_opts>(OpaquePointer(macCachePtr.pointee.opts)) } }
// these computed properties return a local copy of the struct accessed:
// - don't use if you rely on the pointers
// - only for reading
var opts: mp_vo_opts { get { return optsPtr.pointee } }
var macOpts: macos_opts { get { return macOptsPtr.pointee } }
var vo: mp_vo_opts { get { return voPtr.pointee } }
var mac: macos_opts { get { return macPtr.pointee } }
init(_ taParent: UnsafeMutableRawPointer, _ global: OpaquePointer?) {
guard let cache = m_config_cache_alloc(taParent, global, Application.getVoSubConf()),
let macCache = m_config_cache_alloc(taParent, global, Application.getMacOSConf()) else
{
// will never be hit, mp_get_config_group asserts for invalid groups
exit(1)
}
optsCachePtr = cache
macOptsCachePtr = macCache
voCachePtr = m_config_cache_alloc(taParent, global, Application.getVoConf())
macCachePtr = m_config_cache_alloc(taParent, global, Application.getMacConf())
}
func nextChangedOption(property: inout UnsafeMutableRawPointer?) -> Bool {
return m_config_cache_get_next_changed(optsCachePtr, &property)
return m_config_cache_get_next_changed(voCachePtr, &property)
}
func setOption(fullscreen: Bool) {
optsPtr.pointee.fullscreen = fullscreen
_ = withUnsafeMutableBytes(of: &optsPtr.pointee.fullscreen) { (ptr: UnsafeMutableRawBufferPointer) in
m_config_cache_write_opt(optsCachePtr, ptr.baseAddress)
voPtr.pointee.fullscreen = fullscreen
_ = withUnsafeMutableBytes(of: &voPtr.pointee.fullscreen) { (ptr: UnsafeMutableRawBufferPointer) in
m_config_cache_write_opt(voCachePtr, ptr.baseAddress)
}
}
func setOption(minimized: Bool) {
optsPtr.pointee.window_minimized = minimized
_ = withUnsafeMutableBytes(of: &optsPtr.pointee.window_minimized) { (ptr: UnsafeMutableRawBufferPointer) in
m_config_cache_write_opt(optsCachePtr, ptr.baseAddress)
voPtr.pointee.window_minimized = minimized
_ = withUnsafeMutableBytes(of: &voPtr.pointee.window_minimized) { (ptr: UnsafeMutableRawBufferPointer) in
m_config_cache_write_opt(voCachePtr, ptr.baseAddress)
}
}
func setOption(maximized: Bool) {
optsPtr.pointee.window_maximized = maximized
_ = withUnsafeMutableBytes(of: &optsPtr.pointee.window_maximized) { (ptr: UnsafeMutableRawBufferPointer) in
m_config_cache_write_opt(optsCachePtr, ptr.baseAddress)
voPtr.pointee.window_maximized = maximized
_ = withUnsafeMutableBytes(of: &voPtr.pointee.window_maximized) { (ptr: UnsafeMutableRawBufferPointer) in
m_config_cache_write_opt(voCachePtr, ptr.baseAddress)
}
}
func setMacOptionCallback(_ callback: swift_wakeup_cb_fn, context object: AnyObject) {
m_config_cache_set_wakeup_cb(macOptsCachePtr, callback, TypeHelper.bridge(obj: object))
m_config_cache_set_wakeup_cb(macCachePtr, callback, TypeHelper.bridge(obj: object))
}
func nextChangedMacOption(property: inout UnsafeMutableRawPointer?) -> Bool {
return m_config_cache_get_next_changed(macOptsCachePtr, &property)
return m_config_cache_get_next_changed(macCachePtr, &property)
}
}

View File

@ -65,11 +65,11 @@ class CocoaCB: Common {
self.vo = vo
if backendState == .needsInit {
DispatchQueue.main.sync { self.initBackend(vo) }
} else if option.opts.auto_window_resize {
} else if option.vo.auto_window_resize {
DispatchQueue.main.async {
self.updateWindowSize(vo)
self.layer?.update(force: true)
if self.option.opts.focus_on == 2 {
if self.option.vo.focus_on == 2 {
NSApp.activate(ignoringOtherApps: true)
}
}
@ -204,7 +204,7 @@ class CocoaCB: Common {
func shutdown(_ destroy: Bool = false) {
isShuttingDown = window?.isAnimating ?? false ||
window?.isInFullscreen ?? false && option.opts.native_fs
window?.isInFullscreen ?? false && option.vo.native_fs
if window?.isInFullscreen ?? false && !(window?.isAnimating ?? false) {
window?.close()
}

View File

@ -63,7 +63,7 @@ class Common: NSObject {
func initApp() {
var policy: NSApplication.ActivationPolicy = .regular
switch option.macOpts.macos_app_activation_policy {
switch option.mac.macos_app_activation_policy {
case 0:
policy = .regular
case 1:
@ -92,16 +92,16 @@ class Common: NSObject {
exit(1)
}
window.setOnTop(Bool(option.opts.ontop), Int(option.opts.ontop_level))
window.setOnAllWorkspaces(Bool(option.opts.all_workspaces))
window.keepAspect = Bool(option.opts.keepaspect_window)
window.setOnTop(Bool(option.vo.ontop), Int(option.vo.ontop_level))
window.setOnAllWorkspaces(Bool(option.vo.all_workspaces))
window.keepAspect = Bool(option.vo.keepaspect_window)
window.title = title
window.border = Bool(option.opts.border)
window.border = Bool(option.vo.border)
titleBar = TitleBar(frame: wr, window: window, common: self)
let maximized = Bool(option.opts.window_maximized)
let minimized = Bool(option.opts.window_minimized)
let maximized = Bool(option.vo.window_maximized)
let minimized = Bool(option.vo.window_minimized)
window.isRestorable = false
window.isReleasedWhenClosed = false
window.setMaximized((minimized || !maximized) ? window.isZoomed : maximized)
@ -115,10 +115,10 @@ class Common: NSObject {
window.orderFront(nil)
}
NSApp.activate(ignoringOtherApps: option.opts.focus_on >= 1)
NSApp.activate(ignoringOtherApps: option.vo.focus_on >= 1)
// workaround for macOS 10.15 to refocus the previous App
if option.opts.focus_on == 0 {
if option.vo.focus_on == 0 {
previousActiveApp?.activate()
}
}
@ -139,7 +139,7 @@ class Common: NSObject {
}
func initWindowState() {
if option.opts.fullscreen {
if option.vo.fullscreen {
DispatchQueue.main.async {
self.window?.toggleFullScreen(nil)
}
@ -401,9 +401,9 @@ class Common: NSObject {
}
func getTargetScreen(forFullscreen fs: Bool) -> NSScreen? {
let screenID = fs ? option.opts.fsscreen_id : option.opts.screen_id
let screenID = fs ? option.vo.fsscreen_id : option.vo.screen_id
var name: String?
if let screenName = fs ? option.opts.fsscreen_name : option.opts.screen_name {
if let screenName = fs ? option.vo.fsscreen_name : option.vo.screen_name {
name = String(cString: screenName)
}
return getScreenBy(id: Int(screenID)) ?? getScreenBy(name: name)
@ -418,7 +418,7 @@ class Common: NSObject {
func getWindowGeometry(forScreen screen: NSScreen,
videoOut vo: UnsafeMutablePointer<vo>) -> NSRect {
let r = screen.convertRectToBacking(screen.frame)
let targetFrame = option.macOpts.macos_geometry_calculation == FRAME_VISIBLE
let targetFrame = option.mac.macos_geometry_calculation == FRAME_VISIBLE
? screen.visibleFrame : screen.frame
let rv = screen.convertRectToBacking(targetFrame)
@ -506,43 +506,43 @@ class Common: NSObject {
var opt: UnsafeMutableRawPointer?
while option.nextChangedOption(property: &opt) {
switch opt {
case TypeHelper.toPointer(&option.optsPtr.pointee.border):
case TypeHelper.toPointer(&option.voPtr.pointee.border):
DispatchQueue.main.async {
self.window?.border = Bool(self.option.opts.border)
self.window?.border = Bool(self.option.vo.border)
}
case TypeHelper.toPointer(&option.optsPtr.pointee.fullscreen):
case TypeHelper.toPointer(&option.voPtr.pointee.fullscreen):
DispatchQueue.main.async {
self.window?.toggleFullScreen(nil)
}
case TypeHelper.toPointer(&option.optsPtr.pointee.ontop): fallthrough
case TypeHelper.toPointer(&option.optsPtr.pointee.ontop_level):
case TypeHelper.toPointer(&option.voPtr.pointee.ontop): fallthrough
case TypeHelper.toPointer(&option.voPtr.pointee.ontop_level):
DispatchQueue.main.async {
self.window?.setOnTop(Bool(self.option.opts.ontop), Int(self.option.opts.ontop_level))
self.window?.setOnTop(Bool(self.option.vo.ontop), Int(self.option.vo.ontop_level))
}
case TypeHelper.toPointer(&option.optsPtr.pointee.all_workspaces):
case TypeHelper.toPointer(&option.voPtr.pointee.all_workspaces):
DispatchQueue.main.async {
self.window?.setOnAllWorkspaces(Bool(self.option.opts.all_workspaces))
self.window?.setOnAllWorkspaces(Bool(self.option.vo.all_workspaces))
}
case TypeHelper.toPointer(&option.optsPtr.pointee.keepaspect_window):
case TypeHelper.toPointer(&option.voPtr.pointee.keepaspect_window):
DispatchQueue.main.async {
self.window?.keepAspect = Bool(self.option.opts.keepaspect_window)
self.window?.keepAspect = Bool(self.option.vo.keepaspect_window)
}
case TypeHelper.toPointer(&option.optsPtr.pointee.window_minimized):
case TypeHelper.toPointer(&option.voPtr.pointee.window_minimized):
DispatchQueue.main.async {
self.window?.setMinimized(Bool(self.option.opts.window_minimized))
self.window?.setMinimized(Bool(self.option.vo.window_minimized))
}
case TypeHelper.toPointer(&option.optsPtr.pointee.window_maximized):
case TypeHelper.toPointer(&option.voPtr.pointee.window_maximized):
DispatchQueue.main.async {
self.window?.setMaximized(Bool(self.option.opts.window_maximized))
self.window?.setMaximized(Bool(self.option.vo.window_maximized))
}
case TypeHelper.toPointer(&option.optsPtr.pointee.cursor_passthrough):
case TypeHelper.toPointer(&option.voPtr.pointee.cursor_passthrough):
DispatchQueue.main.async {
self.window?.ignoresMouseEvents = self.option.opts.cursor_passthrough
self.window?.ignoresMouseEvents = self.option.vo.cursor_passthrough
}
case TypeHelper.toPointer(&option.optsPtr.pointee.geometry): fallthrough
case TypeHelper.toPointer(&option.optsPtr.pointee.autofit): fallthrough
case TypeHelper.toPointer(&option.optsPtr.pointee.autofit_smaller): fallthrough
case TypeHelper.toPointer(&option.optsPtr.pointee.autofit_larger):
case TypeHelper.toPointer(&option.voPtr.pointee.geometry): fallthrough
case TypeHelper.toPointer(&option.voPtr.pointee.autofit): fallthrough
case TypeHelper.toPointer(&option.voPtr.pointee.autofit_smaller): fallthrough
case TypeHelper.toPointer(&option.voPtr.pointee.autofit_larger):
DispatchQueue.main.async {
let (_, wr) = self.getInitProperties(vo)
self.window?.updateFrame(wr)
@ -608,7 +608,7 @@ class Common: NSObject {
let sizeData = data!.assumingMemoryBound(to: Int32.self)
let size = UnsafeMutableBufferPointer(start: sizeData, count: 2)
var rect = window?.unfsContentFrame ?? NSRect(x: 0, y: 0, width: 1280, height: 720)
if let screen = window?.currentScreen, !Bool(option.opts.hidpi_window_scale) {
if let screen = window?.currentScreen, !Bool(option.vo.hidpi_window_scale) {
rect = screen.convertRectToBacking(rect)
}
@ -620,7 +620,7 @@ class Common: NSObject {
let size = UnsafeBufferPointer(start: sizeData, count: 2)
var rect = NSMakeRect(0, 0, CGFloat(size[0]), CGFloat(size[1]))
DispatchQueue.main.async {
if let screen = self.window?.currentScreen, !Bool(self.option.opts.hidpi_window_scale) {
if let screen = self.window?.currentScreen, !Bool(self.option.vo.hidpi_window_scale) {
rect = screen.convertRectFromBacking(rect)
}
self.window?.updateSize(rect.size)
@ -673,12 +673,12 @@ class Common: NSObject {
var opt: UnsafeMutableRawPointer?
while option.nextChangedMacOption(property: &opt) {
switch opt {
case TypeHelper.toPointer(&option.macOptsPtr.pointee.macos_title_bar_appearance):
titleBar?.set(appearance: Int(option.macOpts.macos_title_bar_appearance))
case TypeHelper.toPointer(&option.macOptsPtr.pointee.macos_title_bar_material):
titleBar?.set(material: Int(option.macOpts.macos_title_bar_material))
case TypeHelper.toPointer(&option.macOptsPtr.pointee.macos_title_bar_color):
titleBar?.set(color: option.macOpts.macos_title_bar_color)
case TypeHelper.toPointer(&option.macPtr.pointee.macos_title_bar_appearance):
titleBar?.set(appearance: Int(option.mac.macos_title_bar_appearance))
case TypeHelper.toPointer(&option.macPtr.pointee.macos_title_bar_material):
titleBar?.set(material: Int(option.mac.macos_title_bar_material))
case TypeHelper.toPointer(&option.macPtr.pointee.macos_title_bar_color):
titleBar?.set(color: option.mac.macos_title_bar_color)
default:
break
}

View File

@ -249,7 +249,7 @@ class GLLayer: CAOpenGLLayer {
var pix: CGLPixelFormatObj?
var depth: GLint = 8
var err: CGLError = CGLError(rawValue: 0)
let swRender = ccb.option.macOpts.cocoa_cb_sw_renderer
let swRender = ccb.option.mac.cocoa_cb_sw_renderer
if swRender != 1 {
(pix, depth, err) = GLLayer.findPixelFormat(ccb)
@ -277,12 +277,12 @@ class GLLayer: CAOpenGLLayer {
glBase.insert(CGLPixelFormatAttribute(ver.rawValue), at: 1)
var glFormat = [glBase]
if ccb.option.macOpts.cocoa_cb_10bit_context {
if ccb.option.mac.cocoa_cb_10bit_context {
glFormat += [glFormat10Bit]
}
glFormat += glFormatOptional
if !ccb.option.macOpts.macos_force_dedicated_gpu {
if !ccb.option.mac.macos_force_dedicated_gpu {
glFormat += [glFormatAutoGPU]
}
@ -308,7 +308,7 @@ class GLLayer: CAOpenGLLayer {
ccb.log.sendWarning("Couldn't create a " +
"\(software ? "software" : "hardware accelerated") " +
"CGL pixel format: \(errS) (\(err.rawValue))")
if software == false && ccb.option.macOpts.cocoa_cb_sw_renderer == -1 {
if software == false && ccb.option.mac.cocoa_cb_sw_renderer == -1 {
ccb.log.sendWarning("Falling back to software renderer")
}

View File

@ -64,9 +64,9 @@ class TitleBar: NSVisualEffectView {
window.contentView?.addSubview(self, positioned: .above, relativeTo: nil)
window.titlebarAppearsTransparent = true
window.styleMask.insert(.fullSizeContentView)
set(appearance: Int(option.macOpts.macos_title_bar_appearance))
set(material: Int(option.macOpts.macos_title_bar_material))
set(color: option.macOpts.macos_title_bar_color)
set(appearance: Int(option.mac.macos_title_bar_appearance))
set(material: Int(option.mac.macos_title_bar_material))
set(color: option.mac.macos_title_bar_color)
}
required init?(coder: NSCoder) {

View File

@ -92,7 +92,7 @@ class Window: NSWindow, NSWindowDelegate {
title = com.title
minSize = NSMakeSize(160, 90)
collectionBehavior = .fullScreenPrimary
ignoresMouseEvents = option.opts.cursor_passthrough
ignoresMouseEvents = option.vo.cursor_passthrough
delegate = self
if let cView = contentView {
@ -143,7 +143,7 @@ class Window: NSWindow, NSWindowDelegate {
setFrame(frame, display: true)
}
if Bool(option.opts.native_fs) {
if Bool(option.vo.native_fs) {
super.toggleFullScreen(sender)
} else {
if !isInFullscreen {
@ -283,7 +283,7 @@ class Window: NSWindow, NSWindowDelegate {
}
func getFsAnimationDuration(_ def: Double) -> Double {
let duration = option.macOpts.macos_fs_animation_duration
let duration = option.mac.macos_fs_animation_duration
if duration < 0 {
return def
} else {

View File

@ -59,12 +59,12 @@ class MacCommon: Common {
}
if !NSEqualSizes(window?.unfsContentFramePixel.size ?? NSZeroSize, wr.size) &&
option.opts.auto_window_resize
option.vo.auto_window_resize
{
window?.updateSize(wr.size)
}
if option.opts.focus_on == 2 {
if option.vo.focus_on == 2 {
NSApp.activate(ignoringOtherApps: true)
}
@ -89,7 +89,7 @@ class MacCommon: Common {
}
@objc func swapBuffer() {
if option.macOpts.macos_render_timer != RENDER_TIMER_SYSTEM {
if option.mac.macos_render_timer != RENDER_TIMER_SYSTEM {
swapLock.lock()
while(swapTime < 1) {
swapLock.wait()
@ -112,8 +112,8 @@ class MacCommon: Common {
self.swapLock.unlock()
}
if option.macOpts.macos_render_timer != RENDER_TIMER_SYSTEM {
if let timer = self.timer, option.macOpts.macos_render_timer == RENDER_TIMER_PRECISE {
if option.mac.macos_render_timer != RENDER_TIMER_SYSTEM {
if let timer = self.timer, option.mac.macos_render_timer == RENDER_TIMER_PRECISE {
timer.scheduleAt(time: inOutputTime.pointee.hostTime, closure: signalSwap)
return kCVReturnSuccess
}