From 61a09501d6324314febe48d4a432d3d352018875 Mon Sep 17 00:00:00 2001 From: der richter Date: Tue, 15 Oct 2024 18:56:27 +0200 Subject: [PATCH] mac/app: add option to adjust Bundle PATH variable App Bundles operate in their own shell environment that is different from the one in the terminal. the default PATH variable for all Bundles is /usr/bin:/bin:/usr/sbin:/sbin. because of that mpv can not find binaries installed by package manager that might be used in scripts for example. add an option to prepend paths to the Bundle PATH. we prepend to make the order fully configurable, opposed to appending where the default Bundle binaries would always take precedence. --- DOCS/man/options.rst | 15 +++++++++++++++ osdep/mac/app_bridge.h | 1 + osdep/mac/app_bridge.m | 5 +++++ osdep/mac/app_hub.swift | 7 +++++++ osdep/mac/application.swift | 12 ++---------- osdep/mac/type_helper.swift | 11 +++++++++++ 6 files changed, 41 insertions(+), 10 deletions(-) diff --git a/DOCS/man/options.rst b/DOCS/man/options.rst index a530d58711..21c4bb6170 100644 --- a/DOCS/man/options.rst +++ b/DOCS/man/options.rst @@ -6601,6 +6601,21 @@ them. precedence over any other shortcuts, they are not propagated to the mpv core and they can't be used in config files like ``input.conf`` or script bindings. +``--macos-bundle-path=path1,path2,...`` + App Bundles operate in their own shell environment that is different from the one in the + terminal. The default PATH variable for all Bundles is ``/usr/bin:/bin:/usr/sbin:/sbin``. + Because of that mpv can not find binaries installed by package manager that might be used in + scripts for example. This option prepends all given paths to the default Bundle PATH. + + Default value in following order: + + :/usr/local/bin: homebrew (Intel) install path + :/usr/local/sbin: homebrew (Intel) install path + :/opt/local/bin: MacPorts install path + :/opt/local/sbin: MacPorts install path + :/opt/homebrew/bin: homebrew (ARM) install path + :/opt/homebrew/sbin: homebrew (ARM) install path + ``--android-surface-size=`` Set dimensions of the rendering surface used by the Android gpu context. Needs to be set by the embedding application if the dimensions change during diff --git a/osdep/mac/app_bridge.h b/osdep/mac/app_bridge.h index 94ba745b3a..edc6cc0b74 100644 --- a/osdep/mac/app_bridge.h +++ b/osdep/mac/app_bridge.h @@ -63,6 +63,7 @@ struct macos_opts { int macos_geometry_calculation; int macos_render_timer; bool macos_menu_shortcuts; + char **macos_bundle_path; int cocoa_cb_sw_renderer; bool cocoa_cb_10bit_context; int cocoa_cb_output_csp; diff --git a/osdep/mac/app_bridge.m b/osdep/mac/app_bridge.m index ead9603c9d..530fa2dd31 100644 --- a/osdep/mac/app_bridge.m +++ b/osdep/mac/app_bridge.m @@ -53,6 +53,7 @@ const struct m_sub_options macos_conf = { {"callback", RENDER_TIMER_CALLBACK}, {"precise", RENDER_TIMER_PRECISE}, {"system", RENDER_TIMER_SYSTEM}, {"feedback", RENDER_TIMER_PRESENTATION_FEEDBACK})}, {"macos-menu-shortcuts", OPT_BOOL(macos_menu_shortcuts)}, + {"macos-bundle-path", OPT_STRINGLIST(macos_bundle_path)}, {"cocoa-cb-sw-renderer", OPT_CHOICE(cocoa_cb_sw_renderer, {"auto", -1}, {"no", 0}, {"yes", 1})}, {"cocoa-cb-10bit-context", OPT_BOOL(cocoa_cb_10bit_context)}, @@ -80,6 +81,10 @@ const struct m_sub_options macos_conf = { .macos_fs_animation_duration = -1, .macos_render_timer = RENDER_TIMER_CALLBACK, .macos_menu_shortcuts = true, + .macos_bundle_path = (char *[]){ + "/usr/local/bin", "/usr/local/sbin", "/opt/local/bin", "/opt/local/sbin", + "/opt/homebrew/bin", "/opt/homebrew/sbin", NULL + }, .cocoa_cb_sw_renderer = -1, .cocoa_cb_10bit_context = true, .cocoa_cb_output_csp = MAC_CSP_AUTO, diff --git a/osdep/mac/app_hub.swift b/osdep/mac/app_hub.swift index fd917bc80f..14d4bb9731 100644 --- a/osdep/mac/app_hub.swift +++ b/osdep/mac/app_hub.swift @@ -38,6 +38,7 @@ class AppHub: NSObject { let MPV_PROTOCOL: String = "mpv://" var isApplication: Bool { return NSApp is Application } + var isBundle: Bool { return ProcessInfo.processInfo.environment["MPVBUNDLE"] == "true" } var openEvents: Int = 0 private override init() { @@ -59,6 +60,12 @@ class AppHub: NSObject { input.option = option DispatchQueue.main.sync { menu = MenuBar(self) } } + if let bundlePath = option?.mac.macos_bundle_path, isBundle { + let path = TypeHelper.toStringArray(bundlePath).joined(separator: ":") + ":" + + (ProcessInfo.processInfo.environment["PATH"] ?? "") + log.verbose("Setting Bundle PATH to: \(path)") + _ = path.withCString { setenv("PATH", $0, 1) } + } #if HAVE_MACOS_MEDIA_PLAYER remote?.registerEvents() diff --git a/osdep/mac/application.swift b/osdep/mac/application.swift index 6bc424a654..e2bdd599f7 100644 --- a/osdep/mac/application.swift +++ b/osdep/mac/application.swift @@ -20,7 +20,6 @@ import Cocoa class Application: NSApplication, NSApplicationDelegate { var appHub: AppHub { return AppHub.shared } var eventManager: NSAppleEventManager { return NSAppleEventManager.shared() } - var isBundle: Bool { return ProcessInfo.processInfo.environment["MPVBUNDLE"] == "true" } var playbackThreadId: mp_thread! var argc: Int32? var argv: UnsafeMutablePointer?>? @@ -78,20 +77,13 @@ class Application: NSApplication, NSApplicationDelegate { } func setupBundle() { - if !isBundle { return } + if !appHub.isBundle { return } // started from finder the first argument after the binary may start with -psn_ if CommandLine.argc > 1 && CommandLine.arguments[1].hasPrefix("-psn_") { argc? = 1 argv?[1] = nil } - - let path = (ProcessInfo.processInfo.environment["PATH"] ?? "") + - ":/usr/local/bin:/usr/local/sbin" + // homebrew Intel - ":/opt/local/bin:/opt/local/sbin" + // MacPorts - ":/opt/homebrew/bin:/opt/homebrew/sbin" // homebrew ARM - appHub.log.verbose("Setting Bundle $PATH to: \(path)") - _ = path.withCString { setenv("PATH", $0, 1) } } let playbackThread: @convention(c) (UnsafeMutableRawPointer) -> UnsafeMutableRawPointer? = { (ptr: UnsafeMutableRawPointer) in @@ -108,7 +100,7 @@ class Application: NSApplication, NSApplicationDelegate { NSApp = self NSApp.delegate = self - NSApp.setActivationPolicy(isBundle ? .regular : .accessory) + NSApp.setActivationPolicy(appHub.isBundle ? .regular : .accessory) setupBundle() pthread_create(&playbackThreadId, nil, playbackThread, TypeHelper.bridge(obj: self)) appHub.input.wait() diff --git a/osdep/mac/type_helper.swift b/osdep/mac/type_helper.swift index f76a693f71..44de5af283 100644 --- a/osdep/mac/type_helper.swift +++ b/osdep/mac/type_helper.swift @@ -50,6 +50,17 @@ class TypeHelper { } } + // char ** OPT_STRINGLIST + class func toStringArray(_ obj: UnsafeMutablePointer?>?) -> [String] { + guard var cStringArray = obj else { return [] } + var stringArray: [String] = [] + while let cString = cStringArray.pointee { + stringArray.append(String(cString: cString)) + cStringArray += 1 + } + return stringArray + } + // *(char **) MPV_FORMAT_STRING class func toString(_ obj: UnsafeMutableRawPointer?) -> String? { guard let str = obj else { return nil }