1 /*
2  * Copyright (C) 2014-2015 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2014-2016 Paul Davis <paul@linuxaudiosystems.com>
4  *
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.
9  *
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.
14  *
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.
18  */
19 
20 #include <stdlib.h>
21 #include <string>
22 #include "bundle_env.h"
23 #include "pbd/i18n.h"
24 
25 #include <glibmm.h>
26 #include <fontconfig/fontconfig.h>
27 #include <pango/pangoft2.h>
28 #include <pango/pangocairo.h>
29 
30 #include <windows.h>
31 #include <wingdi.h>
32 
33 #include "ardour/ardour.h"
34 #include "ardour/search_paths.h"
35 #include "ardour/filesystem_paths.h"
36 
37 #include "pbd/file_utils.h"
38 #include "pbd/epa.h"
39 
40 using namespace std;
41 using namespace PBD;
42 using namespace ARDOUR;
43 
44 
45 enum MY_PROCESS_DPI_AWARENESS {
46   PROCESS_DPI_UNAWARE,
47   PROCESS_SYSTEM_DPI_AWARE,
48   PROCESS_PER_MONITOR_DPI_AWARE
49 };
50 
51 typedef HRESULT (WINAPI* SetProcessDpiAwareness_t)(MY_PROCESS_DPI_AWARENESS);
52 
53 void
fixup_bundle_environment(int,char * [],string & localedir)54 fixup_bundle_environment (int, char* [], string & localedir)
55 {
56 	EnvironmentalProtectionAgency::set_global_epa (new EnvironmentalProtectionAgency (true));
57 	/* what to do ? */
58 	// we should at least set ARDOUR_DATA_PATH to prevent the warning message.
59 	// setting a FONTCONFIG_FILE won't hurt either see bundle_env_msvc.cc
60 	// (pangocairo prefers the windows gdi backend unless PANGOCAIRO_BACKEND=fc is set)
61 
62 	// Unset GTK2_RC_FILES so that only ardour specific files are loaded
63 	Glib::unsetenv ("GTK2_RC_FILES");
64 
65 	std::string path;
66 
67 	if (ARDOUR::translations_are_enabled ()) {
68 		path = windows_search_path().to_string();
69 		path += "\\locale";
70 		Glib::setenv ("GTK_LOCALEDIR", path, true);
71 
72 		// and return the same path to our caller
73 		localedir = path;
74 	}
75 
76 	const char *cstr;
77 	cstr = getenv ("VAMP_PATH");
78 	if (cstr) {
79 		path = cstr;
80 		path += G_SEARCHPATH_SEPARATOR;
81 	} else {
82 		path = "";
83 	}
84 	path += Glib::build_filename(ardour_dll_directory(), "vamp");
85 	path += G_SEARCHPATH_SEPARATOR;
86 	path += "%ProgramFiles%\\Vamp Plugins"; // default vamp path
87 	path += G_SEARCHPATH_SEPARATOR;
88 	path += "%COMMONPROGRAMFILES%\\Vamp Plugins";
89 	Glib::setenv ("VAMP_PATH", path, true);
90 
91 	Glib::setenv ("SUIL_MODULE_DIR", Glib::build_filename(ardour_dll_directory(), "suil"), true);
92 
93 	/* XXX this should really be PRODUCT_EXE see tools/x-win/package.sh
94 	 * ardour on windows does not have a startup wrapper script.
95 	 *
96 	 * then again, there's probably nobody using NSM on windows.
97 	 * because neither nsmd nor the GUI is currently available for windows.
98 	 * furthermore it'll be even less common for derived products.
99 	 */
100 	Glib::setenv ("ARDOUR_SELF", Glib::build_filename(ardour_dll_directory(), "ardour.exe"), true);
101 
102 	/* https://docs.microsoft.com/en-us/windows/win32/api/shellscalingapi/nf-shellscalingapi-setprocessdpiawareness */
103 	HMODULE module = LoadLibraryA ("Shcore.dll");
104 	if (module) {
105 		SetProcessDpiAwareness_t setProcessDpiAwareness = reinterpret_cast<SetProcessDpiAwareness_t> (GetProcAddress (module, "SetProcessDpiAwareness"));
106 		if (setProcessDpiAwareness) {
107 			setProcessDpiAwareness (PROCESS_SYSTEM_DPI_AWARE);
108 		}
109 		FreeLibrary (module);
110 	}
111 }
112 
113 static __cdecl void
unload_custom_fonts()114 unload_custom_fonts()
115 {
116 	std::string font_file;
117 	if (find_file (ardour_data_search_path(), "ArdourMono.ttf", font_file)) {
118 		RemoveFontResource(font_file.c_str());
119 	}
120 	if (find_file (ardour_data_search_path(), "ArdourSans.ttf", font_file)) {
121 		RemoveFontResource(font_file.c_str());
122 	}
123 }
124 
125 void
load_custom_fonts()126 load_custom_fonts()
127 {
128 	std::string ardour_mono_file;
129 	std::string ardour_sans_file;
130 
131 	if (!find_file (ardour_data_search_path(), "ArdourMono.ttf", ardour_mono_file)) {
132 		cerr << _("Cannot find ArdourMono TrueType font") << endl;
133 	}
134 
135 	if (!find_file (ardour_data_search_path(), "ArdourSans.ttf", ardour_sans_file)) {
136 		cerr << _("Cannot find ArdourSans TrueType font") << endl;
137 	}
138 
139 	if (ardour_mono_file.empty () && ardour_sans_file.empty ()) {
140 		return;
141 	}
142 
143 	if (pango_font_map_get_type() == PANGO_TYPE_FT2_FONT_MAP) {
144 		FcConfig *config = FcInitLoadConfigAndFonts();
145 
146 		if (!ardour_mono_file.empty () && FcFalse == FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(ardour_mono_file.c_str()))) {
147 			cerr << _("Cannot load ArdourMono TrueType font.") << endl;
148 		}
149 
150 		if (!ardour_sans_file.empty () && FcFalse == FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(ardour_sans_file.c_str()))) {
151 			cerr << _("Cannot load ArdourSans TrueType font.") << endl;
152 		}
153 
154 		if (FcFalse == FcConfigSetCurrent(config)) {
155 			cerr << _("Failed to set fontconfig configuration.") << endl;
156 		}
157 	} else {
158 		// pango with win32 backend
159 		if (0 == AddFontResource(ardour_mono_file.c_str())) {
160 			cerr << _("Cannot register ArdourMono TrueType font with windows gdi.") << endl;
161 		}
162 		if (0 == AddFontResource(ardour_sans_file.c_str())) {
163 			cerr << _("Cannot register ArdourSans TrueType font with windows gdi.") << endl;
164 		}
165 		atexit (&unload_custom_fonts);
166 	}
167 }
168