History

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.

der richter committed 2 years ago

Commit 61a09501 parent c0fb6d06 files · +41 −10Browse files

Unified · Split

6 files
  1. DOCS/man/options.rst +15 −0
  2. osdep/mac/app_bridge.h +1 −0
  3. osdep/mac/app_bridge.m +5 −0
  4. osdep/mac/app_hub.swift +7 −0
  5. osdep/mac/application.swift +2 −10
  6. osdep/mac/type_helper.swift +11 −0
DOCS/man/options.rst+15−0
@@ -6601,6 +6601,21 @@ them. · 6600 unmodified lines
66016601 precedence over any other shortcuts, they are not propagated to the mpv core and they can't be
66026602 used in config files like ``input.conf`` or script bindings.
66036603
6604+``--macos-bundle-path=path1,path2,...``
6605+ App Bundles operate in their own shell environment that is different from the one in the
6606+ terminal. The default PATH variable for all Bundles is ``/usr/bin:/bin:/usr/sbin:/sbin``.
6607+ Because of that mpv can not find binaries installed by package manager that might be used in
6608+ scripts for example. This option prepends all given paths to the default Bundle PATH.
6609+
6610+ Default value in following order:
6611+
6612+ :/usr/local/bin: homebrew (Intel) install path
6613+ :/usr/local/sbin: homebrew (Intel) install path
6614+ :/opt/local/bin: MacPorts install path
6615+ :/opt/local/sbin: MacPorts install path
6616+ :/opt/homebrew/bin: homebrew (ARM) install path
6617+ :/opt/homebrew/sbin: homebrew (ARM) install path
6618+
66046619 ``--android-surface-size=<WxH>``
66056620 Set dimensions of the rendering surface used by the Android gpu context.
66066621 Needs to be set by the embedding application if the dimensions change during
osdep/mac/app_bridge.h+1−0
@@ -63,6 +63,7 @@ struct macos_opts { · 62 unmodified lines
6363 int macos_geometry_calculation;
6464 int macos_render_timer;
6565 bool macos_menu_shortcuts;
66+ char **macos_bundle_path;
6667 int cocoa_cb_sw_renderer;
6768 bool cocoa_cb_10bit_context;
6869 int cocoa_cb_output_csp;
osdep/mac/app_bridge.m+5−0
@@ -53,6 +53,7 @@ const struct m_sub_options macos_conf = { · 52 unmodified lines
5353 {"callback", RENDER_TIMER_CALLBACK}, {"precise", RENDER_TIMER_PRECISE},
5454 {"system", RENDER_TIMER_SYSTEM}, {"feedback", RENDER_TIMER_PRESENTATION_FEEDBACK})},
5555 {"macos-menu-shortcuts", OPT_BOOL(macos_menu_shortcuts)},
56+ {"macos-bundle-path", OPT_STRINGLIST(macos_bundle_path)},
5657 {"cocoa-cb-sw-renderer", OPT_CHOICE(cocoa_cb_sw_renderer,
5758 {"auto", -1}, {"no", 0}, {"yes", 1})},
5859 {"cocoa-cb-10bit-context", OPT_BOOL(cocoa_cb_10bit_context)},
@@ -80,6 +81,10 @@ const struct m_sub_options macos_conf = { · 21 unmodified lines
8081 .macos_fs_animation_duration = -1,
8182 .macos_render_timer = RENDER_TIMER_CALLBACK,
8283 .macos_menu_shortcuts = true,
84+ .macos_bundle_path = (char *[]){
85+ "/usr/local/bin", "/usr/local/sbin", "/opt/local/bin", "/opt/local/sbin",
86+ "/opt/homebrew/bin", "/opt/homebrew/sbin", NULL
87+ },
8388 .cocoa_cb_sw_renderer = -1,
8489 .cocoa_cb_10bit_context = true,
8590 .cocoa_cb_output_csp = MAC_CSP_AUTO,
osdep/mac/app_hub.swift+7−0
@@ -38,6 +38,7 @@ class AppHub: NSObject { · 37 unmodified lines
3838
3939 let MPV_PROTOCOL: String = "mpv://"
4040 var isApplication: Bool { return NSApp is Application }
41+ var isBundle: Bool { return ProcessInfo.processInfo.environment["MPVBUNDLE"] == "true" }
4142 var openEvents: Int = 0
4243
4344 private override init() {
@@ -59,6 +60,12 @@ class AppHub: NSObject { · 15 unmodified lines
5960 input.option = option
6061 DispatchQueue.main.sync { menu = MenuBar(self) }
6162 }
63+ if let bundlePath = option?.mac.macos_bundle_path, isBundle {
64+ let path = TypeHelper.toStringArray(bundlePath).joined(separator: ":") + ":" +
65+ (ProcessInfo.processInfo.environment["PATH"] ?? "")
66+ log.verbose("Setting Bundle PATH to: \(path)")
67+ _ = path.withCString { setenv("PATH", $0, 1) }
68+ }
6269
6370 #if HAVE_MACOS_MEDIA_PLAYER
6471 remote?.registerEvents()
osdep/mac/application.swift+2−10
@@ -20,7 +20,6 @@ import Cocoa · 19 unmodified lines
2020 class Application: NSApplication, NSApplicationDelegate {
2121 var appHub: AppHub { return AppHub.shared }
2222 var eventManager: NSAppleEventManager { return NSAppleEventManager.shared() }
23− var isBundle: Bool { return ProcessInfo.processInfo.environment["MPVBUNDLE"] == "true" }
2423 var playbackThreadId: mp_thread!
2524 var argc: Int32?
2625 var argv: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?
@@ -78,20 +77,13 @@ class Application: NSApplication, NSApplicationDelegate { · 51 unmodified lines
7877 }
7978
8079 func setupBundle() {
81− if !isBundle { return }
80+ if !appHub.isBundle { return }
8281
8382 // started from finder the first argument after the binary may start with -psn_
8483 if CommandLine.argc > 1 && CommandLine.arguments[1].hasPrefix("-psn_") {
8584 argc? = 1
8685 argv?[1] = nil
8786 }
88−
89− let path = (ProcessInfo.processInfo.environment["PATH"] ?? "") +
90− ":/usr/local/bin:/usr/local/sbin" + // homebrew Intel
91− ":/opt/local/bin:/opt/local/sbin" + // MacPorts
92− ":/opt/homebrew/bin:/opt/homebrew/sbin" // homebrew ARM
93− appHub.log.verbose("Setting Bundle $PATH to: \(path)")
94− _ = path.withCString { setenv("PATH", $0, 1) }
9587 }
9688
9789 let playbackThread: @convention(c) (UnsafeMutableRawPointer) -> UnsafeMutableRawPointer? = { (ptr: UnsafeMutableRawPointer) in
@@ -108,7 +100,7 @@ class Application: NSApplication, NSApplicationDelegate { · 10 unmodified lines
108100
109101 NSApp = self
110102 NSApp.delegate = self
111− NSApp.setActivationPolicy(isBundle ? .regular : .accessory)
103+ NSApp.setActivationPolicy(appHub.isBundle ? .regular : .accessory)
112104 setupBundle()
113105 pthread_create(&playbackThreadId, nil, playbackThread, TypeHelper.bridge(obj: self))
114106 appHub.input.wait()
osdep/mac/type_helper.swift+11−0
@@ -50,6 +50,17 @@ class TypeHelper { · 49 unmodified lines
5050 }
5151 }
5252
53+ // char ** OPT_STRINGLIST
54+ class func toStringArray(_ obj: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?) -> [String] {
55+ guard var cStringArray = obj else { return [] }
56+ var stringArray: [String] = []
57+ while let cString = cStringArray.pointee {
58+ stringArray.append(String(cString: cString))
59+ cStringArray += 1
60+ }
61+ return stringArray
62+ }
63+
5364 // *(char **) MPV_FORMAT_STRING
5465 class func toString(_ obj: UnsafeMutableRawPointer?) -> String? {
5566 guard let str = obj else { return nil }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t