enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[ardour.git] / gtk2_ardour / bundle_env_linux.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 <string>
21 #include <vector>
22 #include <cerrno>
23 #include <cstring>
24
25 #include <glib.h>
26 #include <glibmm/fileutils.h>
27 #include <glibmm/miscutils.h>
28
29 #include <fontconfig/fontconfig.h>
30
31 #include "ardour/ardour.h"
32 #include "ardour/filesystem_paths.h"
33
34 #include "pbd/epa.h"
35 #include "pbd/search_path.h"
36 #include "pbd/pathexpand.h"
37 #include "pbd/file_utils.h"
38
39 #include "bundle_env.h"
40
41 #include "pbd/i18n.h"
42
43 using namespace PBD;
44 using namespace ARDOUR;
45 using namespace std;
46
47 void
48 fixup_bundle_environment (int /*argc*/, char* argv[], string & localedir)
49 {
50         /* THIS IS FOR LINUX - its just about the only place where its
51          * acceptable to build paths directly using '/'.
52          */
53
54         if (!g_getenv ("ARDOUR_BUNDLED")) {
55                 return;
56         }
57
58         EnvironmentalProtectionAgency::set_global_epa (new EnvironmentalProtectionAgency (true, "PREBUNDLE_ENV"));
59
60         std::string path;
61         std::string dir_path = Glib::path_get_dirname (Glib::path_get_dirname (argv[0]));
62 #if defined WINDOWS_VST_SUPPORT
63         // argv[0] will be "wine"
64         if (g_getenv ("INSTALL_DIR")) {
65                 dir_path = g_getenv ("INSTALL_DIR");
66         }
67 #endif
68
69 #ifdef ENABLE_NLS
70         if (!ARDOUR::translations_are_enabled ()) {
71                 localedir = "/this/cannot/exist";
72         } else {
73                 /* force localedir into the bundle */
74                 vector<string> lpath;
75                 lpath.push_back (dir_path);
76                 lpath.push_back ("share");
77                 lpath.push_back ("locale");
78                 localedir = canonical_path (Glib::build_filename (lpath)).c_str();
79         }
80 #endif
81
82         /* note that this function is POSIX/Linux specific, so using / as
83            a dir separator in this context is just fine.
84         */
85
86         export_search_path (dir_path, "ARDOUR_DLL_PATH", "/lib");
87         export_search_path (dir_path, "ARDOUR_CONFIG_PATH", "/etc");
88         export_search_path (dir_path, "ARDOUR_INSTANT_XML_PATH", "/share");
89         export_search_path (dir_path, "ARDOUR_DATA_PATH", "/share");
90         export_search_path (dir_path, "LADSPA_PATH", "/plugins");
91         export_search_path (dir_path, "VAMP_PATH", "/lib");
92         export_search_path (dir_path, "GTK_PATH", "/lib/gtkengines");
93
94         g_setenv ("SUIL_MODULE_DIR", (dir_path + "/lib").c_str(), 1);
95         g_setenv ("PATH", (dir_path + "/bin:" + std::string(g_getenv ("PATH"))).c_str(), 1);
96
97         /* unset GTK2_RC_FILES so that we only load the RC files that we define
98          */
99
100         g_unsetenv ("GTK2_RC_FILES");
101
102         /* Tell fontconfig where to find fonts.conf. Use the system version
103            if it exists, otherwise use the stuff we included in the bundle
104         */
105
106         if (Glib::file_test ("/etc/fonts/fonts.conf", Glib::FILE_TEST_EXISTS)) {
107                 g_setenv ("FONTCONFIG_FILE", "/etc/fonts/fonts.conf", 1);
108                 g_setenv ("FONTCONFIG_PATH", "/etc/fonts", 1);
109         } else {
110                 error << _("No fontconfig file found on your system. Things may looked very odd or ugly") << endmsg;
111         }
112
113         /* this doesn't do much but setting it should prevent various parts of the GTK/GNU stack
114            from looking outside the bundle to find the charset.alias file.
115         */
116         g_setenv ("CHARSETALIASDIR", dir_path.c_str(), 1);
117
118 }
119
120 void
121 load_custom_fonts()
122 {
123         std::string ardour_mono_file;
124
125         if (!find_file (ardour_data_search_path(), "ArdourMono.ttf", ardour_mono_file)) {
126                 cerr << _("Cannot find ArdourMono TrueType font") << endl;
127         }
128
129         FcConfig *config = FcInitLoadConfigAndFonts();
130         FcBool ret = FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(ardour_mono_file.c_str()));
131
132         if (ret == FcFalse) {
133                 cerr << _("Cannot load ArdourMono TrueType font.") << endl;
134         }
135
136         ret = FcConfigSetCurrent(config);
137
138         if (ret == FcFalse) {
139                 cerr << _("Failed to set fontconfig configuration.") << endl;
140         }
141 }