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.
This commit is contained in:
der richter
2024-10-19 17:18:29 +02:00
parent c0fb6d0d62
commit 61a09501d6
6 changed files with 41 additions and 10 deletions
+15
View File
@@ -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=<WxH>``
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
+1
View File
@@ -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;
+5
View File
@@ -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,
+7
View File
@@ -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()
+2 -10
View File
@@ -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<UnsafeMutablePointer<CChar>?>?
@@ -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()
+11
View File
@@ -50,6 +50,17 @@ class TypeHelper {
}
}
// char ** OPT_STRINGLIST
class func toStringArray(_ obj: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?) -> [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 }