2 * Copyright (C) 2014-2016 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2015-2018 Robin Gareus <robin@gareus.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <glibmm/fileutils.h>
30 #include <glibmm/miscutils.h>
32 #include <fontconfig/fontconfig.h>
34 #include "ardour/ardour.h"
35 #include "ardour/filesystem_paths.h"
38 #include "pbd/file_utils.h"
39 #include "pbd/search_path.h"
40 #include "pbd/pathexpand.h"
42 #include "bundle_env.h"
47 #include <Carbon/Carbon.h>
48 #include <mach-o/dyld.h>
49 #include <sys/param.h>
52 using namespace ARDOUR;
55 extern void set_language_preference (); // cocoacarbon.mm
56 extern void no_app_nap (); // cocoacarbon.mm
61 char path[PATH_MAX+1];
62 snprintf (path, sizeof (path), "%s/stderr.log", user_config_directory().c_str());
64 int efd = ::open (path, O_CREAT|O_WRONLY|O_TRUNC, 0644);
67 if (dup2 (efd, STDERR_FILENO) < 0) {
74 snprintf (path, sizeof (path), "%s/stdout.log", user_config_directory().c_str());
76 int ofd = ::open (path, O_CREAT|O_WRONLY|O_TRUNC, 0644);
79 if (dup2 (ofd, STDOUT_FILENO) < 0) {
88 fixup_bundle_environment (int argc, char* argv[], string & localedir)
90 /* if running from a bundle, stdout/stderr will be redirect to null by
91 * launchd. That's not useful for anyone, so fix that. Use the same
92 * mechanism is not running from a bundle, but ARDOUR_LOGGING is
93 * set. This allows us to test the stderr/stdout redirects directly
97 if (g_getenv ("ARDOUR_BUNDLED") || g_getenv ("ARDOUR_LOGGING")) {
103 if (!g_getenv ("ARDOUR_BUNDLED")) {
107 if (g_getenv ("ARDOUR_SELF")) {
108 g_setenv ("ARDOUR_SELF", argv[0], 1);
110 if (g_getenv ("PREBUNDLE_ENV")) {
111 EnvironmentalProtectionAgency::set_global_epa (new EnvironmentalProtectionAgency (true, "PREBUNDLE_ENV"));
114 set_language_preference ();
116 char execpath[MAXPATHLEN+1];
117 uint32_t pathsz = sizeof (execpath);
119 _NSGetExecutablePath (execpath, &pathsz);
122 std::string exec_dir = Glib::path_get_dirname (execpath);
123 std::string bundle_dir;
124 std::string userconfigdir = user_config_directory();
126 bundle_dir = Glib::path_get_dirname (exec_dir);
129 if (!ARDOUR::translations_are_enabled ()) {
130 localedir = "/this/cannot/exist";
132 /* force localedir into the bundle */
134 vector<string> lpath;
135 lpath.push_back (bundle_dir);
136 lpath.push_back ("Resources");
137 lpath.push_back ("locale");
138 localedir = Glib::build_filename (lpath).c_str();
142 export_search_path (bundle_dir, "ARDOUR_DLL_PATH", "/lib");
144 /* inside an OS X .app bundle, there is no difference
145 between DATA and CONFIG locations, since OS X doesn't
146 attempt to do anything to expose the notion of
147 machine-independent shared data.
150 export_search_path (bundle_dir, "ARDOUR_DATA_PATH", "/Resources");
151 export_search_path (bundle_dir, "ARDOUR_CONFIG_PATH", "/Resources");
152 export_search_path (bundle_dir, "ARDOUR_INSTANT_XML_PATH", "/Resources");
153 export_search_path (bundle_dir, "LADSPA_PATH", "/Plugins");
154 export_search_path (bundle_dir, "VAMP_PATH", "/lib");
155 export_search_path (bundle_dir, "GTK_PATH", "/lib/gtkengines");
157 g_setenv ("SUIL_MODULE_DIR", (bundle_dir + "/lib").c_str(), 1);
158 g_setenv ("PATH", (bundle_dir + "/MacOS:" + std::string(g_getenv ("PATH"))).c_str(), 1);
160 /* unset GTK2_RC_FILES so that we only load the RC files that we define
163 g_unsetenv ("GTK2_RC_FILES");
164 g_setenv ("CHARSETALIASDIR", bundle_dir.c_str(), 1);
165 g_setenv ("FONTCONFIG_FILE", Glib::build_filename (bundle_dir, "Resources/fonts.conf").c_str(), 1);
168 void load_custom_fonts()
170 /* this code will only compile on OS X 10.6 and above, and we currently do not
171 * need it for earlier versions since we fall back on a non-monospace,
175 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
176 std::string font_file;
178 if (!find_file (ardour_data_search_path(), "ArdourMono.ttf", font_file)) {
179 cerr << _("Cannot find ArdourMono TrueType font") << endl;
184 ttf = CFStringCreateWithBytes(
185 kCFAllocatorDefault, (const UInt8*) font_file.c_str(),
187 kCFStringEncodingUTF8, FALSE);
188 fontURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, ttf, kCFURLPOSIXPathStyle, TRUE);
189 if (CTFontManagerRegisterFontsForURL(fontURL, kCTFontManagerScopeProcess, &error) != true) {
190 cerr << _("Cannot load ArdourMono TrueType font.") << endl;
194 if (!find_file (ardour_data_search_path(), "ArdourSans.ttf", font_file)) {
195 cerr << _("Cannot find ArdourSans TrueType font") << endl;
200 ttf = CFStringCreateWithBytes(
201 kCFAllocatorDefault, (const UInt8*) font_file.c_str(),
203 kCFStringEncodingUTF8, FALSE);
204 fontURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, ttf, kCFURLPOSIXPathStyle, TRUE);
205 if (CTFontManagerRegisterFontsForURL(fontURL, kCTFontManagerScopeProcess, &error) != true) {
206 cerr << _("Cannot load ArdourSans TrueType font.") << endl;