summaryrefslogtreecommitdiff
path: root/gtk2_ardour/bundle_env_msvc.cc
blob: f55652616940eee6aaee59d00d11f50ae3999da8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
 * Copyright (C) 2014-2017 John Emmas <john@creativepost.co.uk>
 * Copyright (C) 2015-2016 Paul Davis <paul@linuxaudiosystems.com>
 * Copyright (C) 2015-2016 Robin Gareus <robin@gareus.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "bundle_env.h"
#include "pbd/i18n.h"

#include <shlobj.h>
#include <stdlib.h>

#include <iostream>
#include <string>
#include <vector>
#include <fstream>

#include <glibmm.h>
#include "pbd/gstdio_compat.h"

#include <fontconfig/fontconfig.h>

#include "ardour/ardour.h"
#include "ardour/search_paths.h"
#include "ardour/filesystem_paths.h"

#include "pbd/file_utils.h"
#include "pbd/epa.h"

using namespace std;
using namespace PBD;
using namespace ARDOUR;

std::string
get_windows_drive_volume_letter()
{
static std::string ret;
char path[PATH_MAX+1];
LPITEMIDLIST pidl = 0;

	if (!ret.length()) {
		if (S_OK == SHGetSpecialFolderLocation (0, CSIDL_WINDOWS, &pidl))
		{
			if (SHGetPathFromIDListA (pidl, path)) {
				path[2] = '\0'; // Gives us just the drive letter and colon
				ret = path;
			}

			CoTaskMemFree (pidl);
		}
		// The above should never fail - but just in case...
		else if (char *env_path = getenv ("windir"))
		{
			strcpy (path, env_path);
			path[2] = '\0'; // Gives us just the drive letter and colon
			ret = path;
		}
	}

	return ret;
}

const string
get_module_folder ()
{
std::string ret;

	// Gives the top-level Ardour installation folder (on Windows)
	// Typically, this will be somehwere like "C:\Program Files"

	gchar* pExeRoot = g_win32_get_package_installation_directory_of_module (0);

	if (0 == pExeRoot) {
		pExeRoot = g_build_filename("C:\\", "Program Files", PROGRAM_NAME, 0);
	}

	if (pExeRoot) {
		gchar  tmp[PATH_MAX+1];
		gchar* p;

		strcpy(tmp, pExeRoot);
		if (0 != (p = strrchr (tmp, G_DIR_SEPARATOR))) {
			*p = '\0';

			if (0 != (p = g_build_filename(tmp, 0))) {
				ret = p;
				g_free (p);
			}
		}

		g_free (pExeRoot);
	}

	return (ret);
}

bool
fixup_config_file (Glib::ustring str_file_to_fix)
{
FILE* fd;
char  buf[4096];
bool  conversion_needed = false;
bool  succeeded = false;

	fstream file_to_fix (fd = g_fopen(str_file_to_fix.c_str(), "r+b"));

	if (file_to_fix.is_open()) {
		vector<std::string> lines;
		std::string line;

		file_to_fix.seekg (0, std::ios::beg);
		file_to_fix.seekp (0, std::ios::beg);

		try {
			while (!file_to_fix.eof() && file_to_fix.getline (buf, sizeof(buf))) {
				line = buf;

				if (!conversion_needed && (std::string::npos != line.find("$(")))
					conversion_needed = true;
				lines.push_back(line);
			}

			if (conversion_needed) {
				bool error = false;
				std::string::size_type token_begin, token_end;
				vector<string>::iterator i;

				for (i = lines.begin(); i != lines.end(); ++i) {
					if (string::npos != (token_begin = i->find("$("))) {
						if (string::npos != (token_end = i->find(")", token_begin))) {
							std::string str_replace_with;
							std::string str_to_replace = i->substr(token_begin, ((token_end+1)-token_begin));

							if (0 == str_to_replace.compare("$(CWD)")) {
								// Replace our token with the current working directory
								if (getcwd(buf, sizeof(buf))) {
									if (buf[strlen(buf)-1] == G_DIR_SEPARATOR)
										buf[strlen(buf)-1] = '\0';
									str_replace_with = buf;

									// Replace the first occurrence of our token with the required string
									i->erase(token_begin, ((token_end+1)-token_begin));
									i->insert(token_begin, str_replace_with);
								} else {
									error = true;
								}
							} else if (0 == str_to_replace.compare("$(WINDRIVE)")){
								// Replace our token with the drive letter (and colon) for the user's Windows volume
								str_replace_with = get_windows_drive_volume_letter();

								// Replace the first occurrence of our token with the required string
								i->erase(token_begin, ((token_end+1)-token_begin));
								i->insert(token_begin, str_replace_with);
							} else if (0 == str_to_replace.compare("$(LOCALCACHEDIR)")){
								// Replace our token with the path to our Ardour cache directory
								str_replace_with = user_cache_directory();

								// Replace the first occurrence of our token with the required string
								i->erase(token_begin, ((token_end+1)-token_begin));
								i->insert(token_begin, str_replace_with);
							} else {
								// Assume that our token represents an environment variable
								std::string envvar_name = str_to_replace.substr(2, str_to_replace.length()-3);

								if (const char *envvar_value = getenv(envvar_name.c_str())) {
									strcpy(buf, envvar_value);
									if (buf[strlen(buf)-1] == G_DIR_SEPARATOR)
										buf[strlen(buf)-1] = '\0';
									str_replace_with = buf;

									// Replace the first occurrence of our token with the required string
									i->erase(token_begin, ((token_end+1)-token_begin));
									i->insert(token_begin, str_replace_with);
								} else {
									error = true;
									cerr << _("ERROR: unknown environment variable") << endl;
								}
							}
						}
					}
				}

				if (!error) {
					file_to_fix.clear ();                  // Clear the EOF flag etc
					file_to_fix.seekg (0, std::ios::beg);  // Seek our 'get' ptr to the file start pos
														   // (our 'put' ptr shouldn't have moved yet).
					chsize(fileno (fd), 0);                // Truncate the file, ready for re-writing

					for (i = lines.begin(); i != lines.end(); ++i) {

						// Write the converted contents to our file
						file_to_fix << (*i).c_str() << endl;
					}

					try {
						file_to_fix.close();
						succeeded = true;
					} catch (...) {}
				}
			} else {
				file_to_fix.close();
				succeeded = true;
			}
		} catch (...) {
			file_to_fix.close();
			succeeded = false;
		}
	} else {
		cerr << _("ERROR: Could not open config file '") << str_file_to_fix << "'" << endl;
	}

	return succeeded;
}

void
fixup_fonts_config ()
{
string fonts_conf_file;

#ifdef DEBUG
	fonts_conf_file = get_module_folder();

	if (!fonts_conf_file.empty()) {
		fonts_conf_file += "\\";
		fonts_conf_file += PROGRAM_NAME;
		fonts_conf_file += PROGRAM_VERSION;
		fonts_conf_file += FONTS_CONF_LOCATION;
#else
	if (PBD::find_file (ARDOUR::ardour_config_search_path(), "fonts.conf", fonts_conf_file)) {
#endif
		Glib::setenv ("FONTCONFIG_FILE", fonts_conf_file, true);

		if (0 == fixup_config_file (fonts_conf_file))
			cerr << _("ERROR: processing error for 'fonts.conf' file") << endl;
	} else {
		cerr << _("ERROR: Malformed module folder (fonts.conf)") << endl;
	}
}

void
fixup_pango_config ()
{
string pango_modules_file;

#if defined(DEBUG) || defined(RDC_BUILD)
	// Make sure we pick up the debuggable DLLs !!!
	pango_modules_file = get_module_folder();

	if (!pango_modules_file.empty()) {
		pango_modules_file += "\\";
		pango_modules_file += PROGRAM_NAME;
		pango_modules_file += PROGRAM_VERSION;
		pango_modules_file += PANGO_CONF_LOCATION;
#if 0
// JE - handy for non-English locale testing (Greek, in this case)
		Glib::ustring pango_modules_path = Glib::locale_to_utf8("C:\\Program Files\\Mixbus3\\etc\\ΔΗΜΗΤΡΗΣ\\pango.modules");
/**/
#else
		Glib::ustring pango_modules_path = pango_modules_file;
#endif
		pango_modules_path.resize (pango_modules_path.size()-14); // Remove "/pango.modules" from the end
#else
	if (PBD::find_file (ARDOUR::ardour_config_search_path(), "pango.modules", pango_modules_file)) {

		Glib::ustring pango_modules_path = pango_modules_file;
		pango_modules_path.resize (pango_modules_path.size()-14); // Remove "/pango.modules" from the end
#endif
		// Set an environment variable so we can find our pango modules. Note
		// that this requires a modified version of libpango (pango-utils.c)
		Glib::setenv ("PANGO_MODULE_PATH", Glib::filename_from_utf8(pango_modules_path), true);

		if (0 == fixup_config_file (pango_modules_file))
			cerr << _("ERROR: processing error for 'pango.modules' file") << endl;
	} else {
		cerr << _("ERROR: Malformed module folder (pango.modules)") << endl;
	}
}

void
fixup_pixbuf_loaders_config ()
{
string gdk_pixbuf_loaders_file;

#if defined(DEBUG) || defined(RDC_BUILD)
	// Make sure we pick up the debuggable DLLs !!!
	gdk_pixbuf_loaders_file = get_module_folder();

	if (!gdk_pixbuf_loaders_file.empty()) {
		gdk_pixbuf_loaders_file += "\\";
		gdk_pixbuf_loaders_file += PROGRAM_NAME;
		gdk_pixbuf_loaders_file += PROGRAM_VERSION;
		gdk_pixbuf_loaders_file += PIXBUFLOADERS_CONF_LOCATION;
#else
	if (PBD::find_file (ARDOUR::ardour_config_search_path(), "gdk-pixbuf.loaders", gdk_pixbuf_loaders_file)) {
#endif
		// Set an environment variable so we can find our pixbuf modules.
		Glib::setenv ("GDK_PIXBUF_MODULE_FILE", Glib::filename_from_utf8(gdk_pixbuf_loaders_file), true);

		if (0 == fixup_config_file (gdk_pixbuf_loaders_file))
			cerr << _("ERROR: processing error for 'gdk-pixbuf.loaders' file") << endl;
	} else {
		cerr << _("ERROR: Malformed module folder (gdk-pixbuf.loaders)") << endl;
	}
}

void
fixup_clearlooks_config ()
{
string clearlooks_la_file;

#if defined(DEBUG) || defined(RDC_BUILD)
	// Make sure we pick up the debuggable DLLs !!!
	clearlooks_la_file = get_module_folder();

	if (!clearlooks_la_file.empty()) {
		clearlooks_la_file += "\\";
		clearlooks_la_file += PROGRAM_NAME;
		clearlooks_la_file += PROGRAM_VERSION;
		clearlooks_la_file += CLEARLOOKS_CONF_LOCATION;
#else
	if (PBD::find_file (ARDOUR::ardour_config_search_path(), "libclearlooks.la", clearlooks_la_file)) {
#endif
		// Set an environment variable so we can find our clearlooks engine.
		// Note that this requires a modified version of libgtk (gtkthemes.c)
		Glib::setenv ("GTK_THEME_ENGINE_FILE", Glib::filename_from_utf8(clearlooks_la_file).c_str(), true);

		if (0 == fixup_config_file (clearlooks_la_file))
			cerr << _("ERROR: processing error for 'clearlooks.la' file") << endl;
	} else {
		cerr << _("ERROR: Malformed module folder (clearlooks.la)") << endl;
	}
}

void
fixup_bundle_environment (int argc, char* argv[], string & localedir)
{
	std::string exec_path = argv[0];
	std::string dir_path  = Glib::path_get_dirname (exec_path);

	// Make sure that our runtime CWD is set to Mixbus's install
	// folder, regardless of where the caller's CWD was set to.
	g_chdir (dir_path.c_str());

	EnvironmentalProtectionAgency::set_global_epa (new EnvironmentalProtectionAgency (true));

	// Now set 'dir_path' so we can append some relative paths
	dir_path = Glib::path_get_dirname (dir_path);

	std::string path;
	const  char *cstr;

	// First, set up 'ARDOUR_DLL_PATH'
	path  = dir_path;
	path += "\\lib\\ardour3\\surfaces;";
	path += dir_path;
	path += "\\lib\\ardour3\\panners;";
	path += dir_path;
	path += "\\lib\\ardour3\\backends;";
	path += dir_path;
	path += "\\bin";
	Glib::setenv ("ARDOUR_DLL_PATH", path, true);


	// Next, set up 'ARDOUR_DATA_PATH'
	path  = get_module_folder() + "\\";
	path += PROGRAM_NAME;
	path += PROGRAM_VERSION;
	path += "\\share";
	Glib::setenv ("ARDOUR_DATA_PATH", path, true);


	// Next, set up 'ARDOUR_CONFIG_PATH'
#ifdef _WIN64
	path = user_config_directory() + "\\win64;";
#else
	path = user_config_directory() + "\\win32;";
#endif
	Glib::setenv ("ARDOUR_CONFIG_PATH", path, true);


	// Next, set up 'ARDOUR_INSTANT_XML_PATH'
	path = user_config_directory();
	Glib::setenv ("ARDOUR_INSTANT_XML_PATH", path, true);


	// Next, set up 'LADSPA_PATH'
	path = ladspa_search_path().to_string();
	Glib::setenv ("LADSPA_PATH", path, true);


	// Next, set up 'SUIL_MODULE_DIR'
	Glib::setenv ("SUIL_MODULE_DIR", Glib::build_filename(ardour_dll_directory(), "suil"), true);


	// Next, set up 'VAMP_PATH'
	cstr = getenv ("VAMP_PATH");
	if (cstr) {
		path = cstr;
		path += G_SEARCHPATH_SEPARATOR;
	} else {
		path = "";
	}
	path += get_module_folder() + "\\";
	path += PROGRAM_NAME;
	path += PROGRAM_VERSION;
	path += "\\bin\\vamp";
	path += G_SEARCHPATH_SEPARATOR;
	path += "%ProgramFiles%\\Vamp Plugins";
	Glib::setenv ("VAMP_PATH", path, true);


	// Next, set up 'ARDOUR_CONTROL_SURFACE_PATH'
	cstr = getenv ("ARDOUR_CONTROL_SURFACE_PATH");
	if (cstr) {
		path = cstr;
		path += G_SEARCHPATH_SEPARATOR;
	} else {
		path = "";
	}
	path += control_protocol_search_path().to_string();
	Glib::setenv ("ARDOUR_CONTROL_SURFACE_PATH", path, true);


	// Next, set up 'GTK_LOCALEDIR'
	if (ARDOUR::translations_are_enabled ()) {
		path = windows_search_path().to_string();
		path += "\\locale";
		Glib::setenv ("GTK_LOCALEDIR", path, true);

		// and return the same path to our caller
		localedir = path;
	}


	// Next, set up 'GTK_PATH'
	cstr = getenv ("GTK_PATH");
	if (cstr) {
		path = cstr;
		path += G_SEARCHPATH_SEPARATOR;
	} else {
		path = "";
	}
	path += user_config_directory();
	path += "\\.gtk-2.0";
	Glib::setenv ("GTK_PATH", path, true);


	// Unset GTK2_RC_FILES so that we only load the RC files that we define
	Glib::unsetenv ("GTK2_RC_FILES");


	// and set a '$HOME' environment variable. This variable changes the value returned
	// by 'g_get_home_dir()' so to prevent that function from unexpectedly changing its
	// mind, we'll set '$HOME' to whatever 'g_get_home_dir()' is already returning!!
	if (NULL == getenv("HOME")) {
		Glib::setenv ("HOME", Glib::locale_from_utf8(g_get_home_dir()), true);
	}

	fixup_fonts_config();
	fixup_clearlooks_config();

#ifdef DLL_PIXBUF_LOADERS
	fixup_pixbuf_loaders_config();
#endif
#ifdef DLL_PANGO_MODULES
	fixup_pango_config();
#endif
}


void load_custom_fonts()
{
	FcConfig* config = FcInitLoadConfigAndFonts();

	std::string font_file;

	if (!find_file (ardour_data_search_path(), "ArdourMono.ttf", font_file)) {
		cerr << _("Cannot find ArdourMono TrueType font") << endl;
	} else {
		FcBool ret = FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(font_file.c_str()));
		if (ret == FcFalse) {
			cerr << _("Cannot load ArdourMono TrueType font.") << endl;
		}
	}

	if (!find_file (ardour_data_search_path(), "ArdourSans.ttf", font_file)) {
		cerr << _("Cannot find ArdourSans TrueType font") << endl;
	} else {
		FcBool ret = FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(font_file.c_str()));
		if (ret == FcFalse) {
			cerr << _("Cannot load ArdourSans TrueType font.") << endl;
		}
	}

	if (FcFalse == FcConfigSetCurrent(config)) {
		cerr << _("Failed to set fontconfig configuration.") << endl;
	}
}