set mingw vamp plugin path
[ardour.git] / gtk2_ardour / bundle_env_mingw.cc
1 /*
2     Copyright (C) 2001-2012 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <stdlib.h>
21 #include "bundle_env.h"
22 #include "i18n.h"
23
24 #include <glibmm.h>
25 #include <fontconfig/fontconfig.h>
26 #include <pango/pangoft2.h>
27 #include <pango/pangocairo.h>
28
29 #include <windows.h>
30 #include <wingdi.h>
31 #include <shlobj.h> // CSIDL_*
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 #include "pbd/windows_special_dirs.h"
40
41 using namespace std;
42 using namespace PBD;
43 using namespace ARDOUR;
44
45
46 /* query top-level Ardour installation path.
47  * Typically, this will be somehwere like
48  * "C:\Program Files (x86)\Ardour"
49  */
50 const std::string
51 get_install_path ()
52 {
53         const gchar* pExeRoot = g_win32_get_package_installation_directory_of_module (0);
54
55         if (0 == pExeRoot) {
56                 HKEY key;
57                 DWORD size = PATH_MAX;
58                 char tmp[PATH_MAX+1];
59                 if (
60 #ifdef __MINGW64__
61                                 (ERROR_SUCCESS == RegOpenKeyExA (HKEY_LOCAL_MACHINE, "Software\\Ardour\\ardour3\\w64", 0, KEY_READ, &key))
62 #else
63                                 (ERROR_SUCCESS == RegOpenKeyExA (HKEY_LOCAL_MACHINE, "Software\\Ardour\\ardour3\\w32", 0, KEY_READ, &key))
64 #endif
65                                 &&(ERROR_SUCCESS == RegQueryValueExA (key, "Install_Dir", 0, NULL, reinterpret_cast<LPBYTE>(tmp), &size))
66                          )
67                 {
68                         pExeRoot = Glib::locale_to_utf8(tmp).c_str();
69                 }
70         }
71
72         if (0 == pExeRoot) {
73                 const char *program_files = PBD::get_win_special_folder (CSIDL_PROGRAM_FILES);
74                 if (program_files) {
75                         pExeRoot = g_build_filename(program_files, PROGRAM_NAME, NULL);
76                 }
77         }
78
79         if (pExeRoot && Glib::file_test(pExeRoot, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_DIR)) {
80                 return std::string (pExeRoot);
81         }
82         return "";
83 }
84
85
86 void
87 fixup_bundle_environment (int, char* [], const char** localedir)
88 {
89         EnvironmentalProtectionAgency::set_global_epa (new EnvironmentalProtectionAgency (true));
90         /* what to do ? */
91         // we should at least set ARDOUR_DATA_PATH to prevent the warning message.
92         // setting a FONTCONFIG_FILE won't hurt either see bundle_env_msvc.cc
93         // (pangocairo prefers the windows gdi backend unless PANGOCAIRO_BACKEND=fc is set)
94
95         // Unset GTK_RC_FILES so that only ardour specific files are loaded
96         Glib::unsetenv ("GTK_RC_FILES");
97
98
99         std::string path;
100         const char *cstr;
101         cstr = getenv ("VAMP_PATH");
102         if (cstr) {
103                 path = cstr;
104                 path += G_SEARCHPATH_SEPARATOR;
105         } else {
106                 path = "";
107         }
108         path += Glib::build_filename(get_install_path(), "lib", "ardour3", "vamp", NULL);
109         path += G_SEARCHPATH_SEPARATOR;
110         path += "%ProgramFiles%\\Vamp Plugins"; // default vamp path
111         path += G_SEARCHPATH_SEPARATOR;
112         path += "%COMMONPROGRAMFILES%\\Vamp Plugins";
113         Glib::setenv ("VAMP_PATH", path, true);
114 }
115
116 static __cdecl void
117 unload_custom_fonts()
118 {
119         std::string ardour_mono_file;
120         if (!find_file (ardour_data_search_path(), "ArdourMono.ttf", ardour_mono_file)) {
121                 return;
122         }
123         RemoveFontResource(ardour_mono_file.c_str());
124 }
125
126 void
127 load_custom_fonts()
128 {
129         std::string ardour_mono_file;
130
131         if (!find_file (ardour_data_search_path(), "ArdourMono.ttf", ardour_mono_file)) {
132                 cerr << _("Cannot find ArdourMono TrueType font") << endl;
133                 return;
134         }
135
136         if (pango_font_map_get_type() == PANGO_TYPE_FT2_FONT_MAP) {
137                 FcConfig *config = FcInitLoadConfigAndFonts();
138                 FcBool ret = FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(ardour_mono_file.c_str()));
139
140                 if (ret == FcFalse) {
141                         cerr << _("Cannot load ArdourMono TrueType font.") << endl;
142                 }
143
144                 ret = FcConfigSetCurrent(config);
145
146                 if (ret == FcFalse) {
147                         cerr << _("Failed to set fontconfig configuration.") << endl;
148                 }
149         } else {
150                 // pango with win32 backend
151                 if (0 == AddFontResource(ardour_mono_file.c_str())) {
152                         cerr << _("Cannot register ArdourMono TrueType font with windows gdi.") << endl;
153                 } else {
154                         atexit (&unload_custom_fonts);
155                 }
156         }
157 }