enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[ardour.git] / gtk2_ardour / bundle_env_cocoa.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 #include <asl.h>
44 #include <Carbon/Carbon.h>
45 #include <mach-o/dyld.h>
46 #include <sys/param.h>
47
48 using namespace PBD;
49 using namespace ARDOUR;
50 using namespace std;
51
52 extern void set_language_preference (); // cocoacarbon.mm
53
54 static void
55 setup_logging(void)
56 {
57         /* The ASL API has evolved since it was introduced in 10.4. If ASL_LOG_DESCRIPTOR_WRITE is not available,
58            then we're not interested in doing any of this, since its only purpose is to get stderr/stdout to
59            appear in the Console.
60         */
61 #ifdef ASL_LOG_DESCRIPTOR_WRITE
62         aslmsg msg;
63         aslclient c = asl_open (PROGRAM_NAME, "com.apple.console", 0);
64
65         msg = asl_new(ASL_TYPE_MSG);
66         asl_set(msg, ASL_KEY_FACILITY, "com.apple.console");
67         asl_set(msg, ASL_KEY_LEVEL, ASL_STRING_NOTICE);
68         asl_set(msg, ASL_KEY_READ_UID, "-1");
69
70         int fd = dup(2);
71         //asl_set_filter(c, ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG));
72         asl_add_log_file(c, fd);
73         asl_log(c, NULL, ASL_LEVEL_INFO, string_compose ("Hello world from %1", PROGRAM_NAME).c_str());
74         asl_log_descriptor(c, msg, ASL_LEVEL_INFO, 1,  ASL_LOG_DESCRIPTOR_WRITE);
75         asl_log_descriptor(c, msg, ASL_LEVEL_INFO, 2, ASL_LOG_DESCRIPTOR_WRITE);
76 #else
77 #warning This build host has an older ASL API, so no console logging in this build.
78 #endif
79 }
80
81 void
82 fixup_bundle_environment (int argc, char* argv[], string & localedir)
83 {
84         if (!g_getenv ("ARDOUR_BUNDLED")) {
85                 return;
86         }
87
88         EnvironmentalProtectionAgency::set_global_epa (new EnvironmentalProtectionAgency (true, "PREBUNDLE_ENV"));
89
90         set_language_preference ();
91
92         setup_logging ();
93
94         char execpath[MAXPATHLEN+1];
95         uint32_t pathsz = sizeof (execpath);
96
97         _NSGetExecutablePath (execpath, &pathsz);
98
99         std::string path;
100         std::string exec_dir = Glib::path_get_dirname (execpath);
101         std::string bundle_dir;
102         std::string userconfigdir = user_config_directory();
103
104         bundle_dir = Glib::path_get_dirname (exec_dir);
105
106 #ifdef ENABLE_NLS
107         if (!ARDOUR::translations_are_enabled ()) {
108                 localedir = "/this/cannot/exist";
109         } else {
110                 /* force localedir into the bundle */
111
112                 vector<string> lpath;
113                 lpath.push_back (bundle_dir);
114                 lpath.push_back ("Resources");
115                 lpath.push_back ("locale");
116                 localedir = Glib::build_filename (lpath).c_str();
117         }
118 #endif
119
120         export_search_path (bundle_dir, "ARDOUR_DLL_PATH", "/lib");
121
122         /* inside an OS X .app bundle, there is no difference
123            between DATA and CONFIG locations, since OS X doesn't
124            attempt to do anything to expose the notion of
125            machine-independent shared data.
126         */
127
128         export_search_path (bundle_dir, "ARDOUR_DATA_PATH", "/Resources");
129         export_search_path (bundle_dir, "ARDOUR_CONFIG_PATH", "/Resources");
130         export_search_path (bundle_dir, "ARDOUR_INSTANT_XML_PATH", "/Resources");
131         export_search_path (bundle_dir, "LADSPA_PATH", "/Plugins");
132         export_search_path (bundle_dir, "VAMP_PATH", "/lib");
133         export_search_path (bundle_dir, "GTK_PATH", "/lib/gtkengines");
134
135         g_setenv ("SUIL_MODULE_DIR", (bundle_dir + "/lib").c_str(), 1);
136         g_setenv ("PATH", (bundle_dir + "/MacOS:" + std::string(g_getenv ("PATH"))).c_str(), 1);
137
138         /* unset GTK2_RC_FILES so that we only load the RC files that we define
139          */
140
141         g_unsetenv ("GTK2_RC_FILES");
142         g_setenv ("CHARSETALIASDIR", bundle_dir.c_str(), 1);
143         g_setenv ("FONTCONFIG_FILE", Glib::build_filename (bundle_dir, "Resources/fonts.conf").c_str(), 1);
144 }
145
146 void load_custom_fonts()
147 {
148         /* this code will only compile on OS X 10.6 and above, and we currently do not
149          * need it for earlier versions since we fall back on a non-monospace,
150          * non-custom font.
151          */
152
153 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
154         std::string ardour_mono_file;
155
156         if (!find_file (ardour_data_search_path(), "ArdourMono.ttf", ardour_mono_file)) {
157                 cerr << _("Cannot find ArdourMono TrueType font") << endl;
158         }
159
160         CFStringRef ttf;
161         CFURLRef fontURL;
162         CFErrorRef error;
163         ttf = CFStringCreateWithBytes(
164                         kCFAllocatorDefault, (UInt8*) ardour_mono_file.c_str(),
165                         ardour_mono_file.length(),
166                         kCFStringEncodingUTF8, FALSE);
167         fontURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, ttf, kCFURLPOSIXPathStyle, TRUE);
168         if (CTFontManagerRegisterFontsForURL(fontURL, kCTFontManagerScopeProcess, &error) != true) {
169                 cerr << _("Cannot load ArdourMono TrueType font.") << endl;
170         }
171 #endif
172 }