2 Copyright (C) 2014 John Emmas
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "bundle_env.h"
32 #include <glib/gstdio.h>
34 #include <fontconfig/fontconfig.h>
36 #include "ardour/ardour.h"
37 #include "ardour/search_paths.h"
38 #include "ardour/filesystem_paths.h"
40 #include "pbd/file_utils.h"
45 using namespace ARDOUR;
48 get_windows_drive_volume_letter()
50 static std::string ret;
51 char path[PATH_MAX+1];
52 LPITEMIDLIST pidl = 0;
55 if (S_OK == SHGetSpecialFolderLocation (0, CSIDL_WINDOWS, &pidl))
57 if (SHGetPathFromIDListA (pidl, path)) {
58 path[2] = '\0'; // Gives us just the drive letter and colon
64 // The above should never fail - but just in case...
65 else if (char *env_path = getenv ("windir"))
67 strcpy (path, env_path);
68 path[2] = '\0'; // Gives us just the drive letter and colon
81 // Gives the top-level Ardour installation folder (on Windows)
82 // Typically, this will be somehwere like "C:\Program Files"
84 gchar* pExeRoot = g_win32_get_package_installation_directory_of_module (0);
87 pExeRoot = g_build_filename("C:\\", "Program Files", PROGRAM_NAME, 0);
91 gchar tmp[PATH_MAX+1];
94 strcpy(tmp, pExeRoot);
95 if (0 != (p = strrchr (tmp, G_DIR_SEPARATOR))) {
98 if (0 != (p = g_build_filename(tmp, 0))) {
111 fixup_config_file (Glib::ustring str_file_to_fix)
115 bool conversion_needed = false;
116 bool succeeded = false;
118 fstream file_to_fix (fd = g_fopen(str_file_to_fix.c_str(), "r+b"));
120 if (file_to_fix.is_open()) {
121 vector<std::string> lines;
124 file_to_fix.seekg (0, std::ios::beg);
125 file_to_fix.seekp (0, std::ios::beg);
128 while (!file_to_fix.eof() && file_to_fix.getline (buf, sizeof(buf))) {
131 if (!conversion_needed && (std::string::npos != line.find("$(")))
132 conversion_needed = true;
133 lines.push_back(line);
136 if (conversion_needed) {
138 std::string::size_type token_begin, token_end;
139 vector<string>::iterator i;
141 for (i = lines.begin(); i != lines.end(); ++i) {
142 if (string::npos != (token_begin = i->find("$("))) {
143 if (string::npos != (token_end = i->find(")", token_begin))) {
144 std::string str_replace_with;
145 std::string str_to_replace = i->substr(token_begin, ((token_end+1)-token_begin));
147 if (0 == str_to_replace.compare("$(CWD)")) {
148 // Replace our token with the current working directory
149 if (getcwd(buf, sizeof(buf))) {
150 if (buf[strlen(buf)-1] == G_DIR_SEPARATOR)
151 buf[strlen(buf)-1] = '\0';
152 str_replace_with = buf;
154 // Replace the first occurrence of our token with the required string
155 i->erase(token_begin, ((token_end+1)-token_begin));
156 i->insert(token_begin, str_replace_with);
160 } else if (0 == str_to_replace.compare("$(WINDRIVE)")){
161 // Replace our token with the drive letter (and colon) for the user's Windows volume
162 str_replace_with = get_windows_drive_volume_letter();
164 // Replace the first occurrence of our token with the required string
165 i->erase(token_begin, ((token_end+1)-token_begin));
166 i->insert(token_begin, str_replace_with);
168 // Assume that our token represents an environment variable
169 std::string envvar_name = str_to_replace.substr(2, str_to_replace.length()-3);
171 if (const char *envvar_value = getenv(envvar_name.c_str())) {
172 strcpy(buf, envvar_value);
173 if (buf[strlen(buf)-1] == G_DIR_SEPARATOR)
174 buf[strlen(buf)-1] = '\0';
175 str_replace_with = buf;
177 // Replace the first occurrence of our token with the required string
178 i->erase(token_begin, ((token_end+1)-token_begin));
179 i->insert(token_begin, str_replace_with);
182 cerr << _("ERROR: unknown environment variable") << endl;
190 file_to_fix.clear (); // Clear the EOF flag etc
191 file_to_fix.seekg (0, std::ios::beg); // Seek our 'get' ptr to the file start pos
192 // (our 'put' ptr shouldn't have moved yet).
193 chsize(fileno (fd), 0); // Truncate the file, ready for re-writing
195 for (i = lines.begin(); i != lines.end(); ++i) {
197 // Write the converted contents to our file
198 file_to_fix << (*i).c_str() << endl;
215 cerr << _("ERROR: Could not open config file '") << str_file_to_fix << "'" << endl;
222 fixup_fonts_config ()
224 string fonts_conf_file;
227 fonts_conf_file = get_module_folder();
229 if (!fonts_conf_file.empty()) {
230 fonts_conf_file += "\\";
231 fonts_conf_file += PROGRAM_NAME;
232 fonts_conf_file += FONTS_CONF_LOCATION;
234 if (PBD::find_file_in_search_path (ARDOUR::ardour_config_search_path(), "fonts.conf", fonts_conf_file)) {
236 Glib::setenv ("FONTCONFIG_FILE", fonts_conf_file, true);
238 if (0 == fixup_config_file (fonts_conf_file))
239 cerr << _("ERROR: processing error for 'fonts.conf' file") << endl;
241 cerr << _("ERROR: Malformed module folder (fonts.conf)") << endl;
246 fixup_pango_config ()
248 string pango_modules_file;
250 #if defined(DEBUG) || defined(RDC_BUILD)
251 // Make sure we pick up the debuggable DLLs !!!
252 pango_modules_file = get_module_folder();
254 if (!pango_modules_file.empty()) {
255 pango_modules_file += "\\";
256 pango_modules_file += PROGRAM_NAME;
257 pango_modules_file += PANGO_CONF_LOCATION;
259 // JE - handy for non-English locale testing (Greek, in this case)
260 Glib::ustring pango_modules_path = Glib::locale_to_utf8("C:\\Program Files\\Mixbus3\\etc\\��������\\pango.modules");
263 Glib::ustring pango_modules_path = pango_modules_file;
265 pango_modules_path.resize (pango_modules_path.size()-14); // Remove "/pango.modules" from the end
267 if (PBD::find_file_in_search_path (ARDOUR::ardour_config_search_path(), "pango.modules", pango_modules_file)) {
269 Glib::ustring pango_modules_path = pango_modules_file;
270 pango_modules_path.resize (pango_modules_path.size()-14); // Remove "/pango.modules" from the end
272 // Set an environment variable so we can find our pango modules. Note
273 // that this requires a modified version of libpango (pango-utils.c)
274 Glib::setenv ("PANGO_MODULE_PATH", Glib::filename_from_utf8(pango_modules_path), true);
276 if (0 == fixup_config_file (pango_modules_file))
277 cerr << _("ERROR: processing error for 'pango.modules' file") << endl;
279 cerr << _("ERROR: Malformed module folder (pango.modules)") << endl;
284 fixup_pixbuf_loaders_config ()
286 string gdk_pixbuf_loaders_file;
288 #if defined(DEBUG) || defined(RDC_BUILD)
289 // Make sure we pick up the debuggable DLLs !!!
290 gdk_pixbuf_loaders_file = get_module_folder();
292 if (!gdk_pixbuf_loaders_file.empty()) {
293 gdk_pixbuf_loaders_file += "\\";
294 gdk_pixbuf_loaders_file += PROGRAM_NAME;
295 gdk_pixbuf_loaders_file += PIXBUFLOADERS_CONF_LOCATION;
297 if (PBD::find_file_in_search_path (ARDOUR::ardour_config_search_path(), "gdk-pixbuf.loaders", gdk_pixbuf_loaders_file)) {
299 // Set an environment variable so we can find our pixbuf modules.
300 Glib::setenv ("GDK_PIXBUF_MODULE_FILE", Glib::filename_from_utf8(gdk_pixbuf_loaders_file), true);
302 if (0 == fixup_config_file (gdk_pixbuf_loaders_file))
303 cerr << _("ERROR: processing error for 'gdk-pixbuf.loaders' file") << endl;
305 cerr << _("ERROR: Malformed module folder (gdk-pixbuf.loaders)") << endl;
310 fixup_clearlooks_config ()
312 string clearlooks_la_file;
314 #if defined(DEBUG) || defined(RDC_BUILD)
315 // Make sure we pick up the debuggable DLLs !!!
316 clearlooks_la_file = get_module_folder();
318 if (!clearlooks_la_file.empty()) {
319 clearlooks_la_file += "\\";
320 clearlooks_la_file += PROGRAM_NAME;
321 clearlooks_la_file += CLEARLOOKS_CONF_LOCATION;
323 if (PBD::find_file_in_search_path (ARDOUR::ardour_config_search_path(), "libclearlooks.la", clearlooks_la_file)) {
325 // Set an environment variable so we can find our clearlooks engine.
326 // Note that this requires a modified version of libgtk (gtkthemes.c)
327 Glib::setenv ("GTK_THEME_ENGINE_FILE", Glib::filename_from_utf8(clearlooks_la_file).c_str(), true);
329 if (0 == fixup_config_file (clearlooks_la_file))
330 cerr << _("ERROR: processing error for 'clearlooks.la' file") << endl;
332 cerr << _("ERROR: Malformed module folder (clearlooks.la)") << endl;
337 fixup_bundle_environment (int argc, char* argv[], const char** localedir)
339 std::string exec_path = argv[0];
340 std::string dir_path = Glib::path_get_dirname (exec_path);
342 // Make sure that our runtime CWD is set to Mixbus's install
343 // folder, regardless of where the caller's CWD was set to.
344 g_chdir (dir_path.c_str());
346 EnvironmentalProtectionAgency::set_global_epa (new EnvironmentalProtectionAgency (true));
348 // Now set 'dir_path' so we can append some relative paths
349 dir_path = Glib::path_get_dirname (dir_path);
354 // First, set up 'ARDOUR_DLL_PATH'
356 path += "\\lib\\ardour3\\surfaces;";
358 path += "\\lib\\ardour3\\panners;";
360 path += "\\lib\\ardour3\\backends;";
363 Glib::setenv ("ARDOUR_DLL_PATH", path, true);
366 // Next, set up 'ARDOUR_DATA_PATH'
367 path = get_module_folder() + "\\";
368 path += PROGRAM_NAME;
370 Glib::setenv ("ARDOUR_DATA_PATH", path, true);
373 // Next, set up 'ARDOUR_CONFIG_PATH'
375 path = user_config_directory() + "\\win64;";
377 path = user_config_directory() + "\\win32;";
379 Glib::setenv ("ARDOUR_CONFIG_PATH", path, true);
382 // Next, set up 'ARDOUR_PATH'
383 path = user_config_directory();
384 path = Glib::path_get_dirname (path);
385 path += G_SEARCHPATH_SEPARATOR;
386 path += windows_search_path().to_string();
388 path += windows_search_path().to_string();
389 path += "\\pixmaps;";
390 path += ardour_data_search_path().to_string(); // In fact, adds both the 'data' search
391 path += G_SEARCHPATH_SEPARATOR; // path and our 'config' search path
394 Glib::setenv ("ARDOUR_PATH", path, true);
397 // Next, set up 'ARDOUR_INSTANT_XML_PATH'
398 path = user_config_directory();
399 Glib::setenv ("ARDOUR_INSTANT_XML_PATH", path, true);
402 // Next, set up 'LADSPA_PATH'
403 path = ladspa_search_path().to_string();
404 Glib::setenv ("LADSPA_PATH", path, true);
407 // Next, set up 'VAMP_PATH'
408 cstr = getenv ("VAMP_PATH");
411 path += G_SEARCHPATH_SEPARATOR;
415 path += get_module_folder() + "\\";
416 path += PROGRAM_NAME;
417 path += "\\bin\\vamp";
418 path += G_SEARCHPATH_SEPARATOR;
419 path += "%ProgramFiles%\\Vamp Plugins";
420 Glib::setenv ("VAMP_PATH", path, true);
423 // Next, set up 'ARDOUR_CONTROL_SURFACE_PATH'
424 cstr = getenv ("ARDOUR_CONTROL_SURFACE_PATH");
427 path += G_SEARCHPATH_SEPARATOR;
431 path += control_protocol_search_path().to_string();
432 Glib::setenv ("ARDOUR_CONTROL_SURFACE_PATH", path, true);
435 // Next, set up 'GTK_LOCALEDIR'
436 if (ARDOUR::translations_are_enabled ()) {
437 path = windows_search_path().to_string();
439 Glib::setenv ("GTK_LOCALEDIR", path, true);
443 // Next, set up 'GTK_PATH'
444 cstr = getenv ("GTK_PATH");
447 path += G_SEARCHPATH_SEPARATOR;
451 path += user_config_directory();
452 path += "\\.gtk-2.0";
453 Glib::setenv ("GTK_PATH", path, true);
456 // Unset GTK_RC_FILES so that we only load the RC files that we define
457 Glib::unsetenv ("GTK_RC_FILES");
460 // and set a '$HOME' environment variable. This variable changes the value returned
461 // by 'g_get_home_dir()' so to prevent that function from unexpectedly changing its
462 // mind, we'll set '$HOME' to whatever 'g_get_home_dir()' is already returning!!
463 if (NULL == getenv("HOME")) {
464 Glib::setenv ("HOME", Glib::locale_from_utf8(g_get_home_dir()), true);
467 fixup_fonts_config();
468 fixup_pango_config();
469 fixup_clearlooks_config();
470 fixup_pixbuf_loaders_config();
474 void load_custom_fonts()
476 std::string ardour_mono_file;
478 if (!find_file_in_search_path (ardour_data_search_path(), "ArdourMono.ttf", ardour_mono_file)) {
479 cerr << _("Cannot find ArdourMono TrueType font") << endl;
482 FcConfig *config = FcInitLoadConfigAndFonts();
483 FcBool ret = FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(ardour_mono_file.c_str()));
485 if (ret == FcFalse) {
486 cerr << _("Cannot load ArdourMono TrueType font.") << endl;
489 ret = FcConfigSetCurrent(config);
491 if (ret == FcFalse) {
492 cerr << _("Failed to set fontconfig configuration.") << endl;