disable custom font loading on OSX
[ardour.git] / gtk2_ardour / main.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 <cstdlib>
21 #include <signal.h>
22 #include <cerrno>
23 #include <fstream>
24 #include <vector>
25
26 #include <sigc++/bind.h>
27 #include <gtkmm/settings.h>
28
29 #include "pbd/error.h"
30 #include "pbd/epa.h"
31 #include "pbd/file_utils.h"
32 #include "pbd/textreceiver.h"
33 #include "pbd/failed_constructor.h"
34 #include "pbd/pthread_utils.h"
35 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
36 #include "pbd/boost_debug.h"
37 #endif
38
39 #include <jack/jack.h>
40
41 #include "ardour/svn_revision.h"
42 #include "ardour/version.h"
43 #include "ardour/ardour.h"
44 #include "ardour/audioengine.h"
45 #include "ardour/session_utils.h"
46 #include "ardour/filesystem_paths.h"
47
48 #include <gtkmm/main.h>
49 #include <gtkmm2ext/application.h>
50 #include <gtkmm2ext/popup.h>
51 #include <gtkmm2ext/utils.h>
52
53 #include <fontconfig/fontconfig.h>
54
55 #include "version.h"
56 #include "utils.h"
57 #include "ardour_ui.h"
58 #include "opts.h"
59 #include "enums.h"
60
61 #include "i18n.h"
62
63 #if (defined __APPLE__ && defined OSX_CUSTOM_FONT)
64 #include <Carbon/Carbon.h>
65 #endif
66
67 using namespace std;
68 using namespace Gtk;
69 using namespace ARDOUR_COMMAND_LINE;
70 using namespace ARDOUR;
71 using namespace PBD;
72
73 TextReceiver text_receiver ("ardour");
74
75 extern int curvetest (string);
76
77 static ARDOUR_UI  *ui = 0;
78 static const char* localedir = LOCALEDIR;
79
80 void
81 gui_jack_error ()
82 {
83         MessageDialog win (string_compose (_("%1 could not connect to JACK."), PROGRAM_NAME),
84                            false,
85                            Gtk::MESSAGE_INFO,
86                            Gtk::BUTTONS_NONE);
87         win.set_secondary_text(string_compose (_("There are several possible reasons:\n\
88 \n\
89 1) JACK is not running.\n\
90 2) JACK is running as another user, perhaps root.\n\
91 3) There is already another client called \"%1\".\n\
92 \n\
93 Please consider the possibilities, and perhaps (re)start JACK."), PROGRAM_NAME));
94
95         win.add_button (Stock::QUIT, RESPONSE_CLOSE);
96         win.set_default_response (RESPONSE_CLOSE);
97
98         win.show_all ();
99         win.set_position (Gtk::WIN_POS_CENTER);
100
101         if (!no_splash) {
102                 ui->hide_splash ();
103         }
104
105         /* we just don't care about the result, but we want to block */
106
107         win.run ();
108 }
109
110 static void export_search_path (const string& base_dir, const char* varname, const char* dir)
111 {
112         string path;
113         const char * cstr = getenv (varname);
114
115         if (cstr) {
116                 path = cstr;
117                 path += ':';
118         } else {
119                 path = "";
120         }
121         path += base_dir;
122         path += dir;
123
124         setenv (varname, path.c_str(), 1);
125 }
126
127 #ifdef __APPLE__
128
129 #include <mach-o/dyld.h>
130 #include <sys/param.h>
131
132 extern void set_language_preference (); // cocoacarbon.mm
133
134 void
135 fixup_bundle_environment (int, char* [])
136 {
137         if (!getenv ("ARDOUR_BUNDLED")) {
138                 return;
139         }
140
141         EnvironmentalProtectionAgency::set_global_epa (new EnvironmentalProtectionAgency (true, "PREBUNDLE_ENV"));
142
143         set_language_preference ();
144
145         char execpath[MAXPATHLEN+1];
146         uint32_t pathsz = sizeof (execpath);
147
148         _NSGetExecutablePath (execpath, &pathsz);
149
150         std::string path;
151         std::string exec_dir = Glib::path_get_dirname (execpath);
152         std::string bundle_dir;
153         std::string userconfigdir = user_config_directory();
154
155         bundle_dir = Glib::path_get_dirname (exec_dir);
156
157 #ifdef ENABLE_NLS
158         if (!ARDOUR::translations_are_enabled ()) {
159                 localedir = "/this/cannot/exist";
160         } else {
161                 /* force localedir into the bundle */
162                 
163                 vector<string> lpath;
164                 lpath.push_back (bundle_dir);
165                 lpath.push_back ("share");
166                 lpath.push_back ("locale");
167                 localedir = strdup (Glib::build_filename (lpath).c_str());
168         }
169 #endif
170                 
171         export_search_path (bundle_dir, "ARDOUR_DLL_PATH", "/lib");
172
173         /* inside an OS X .app bundle, there is no difference
174            between DATA and CONFIG locations, since OS X doesn't
175            attempt to do anything to expose the notion of
176            machine-independent shared data.
177         */
178
179         export_search_path (bundle_dir, "ARDOUR_DATA_PATH", "/Resources");
180         export_search_path (bundle_dir, "ARDOUR_CONFIG_PATH", "/Resources");
181         export_search_path (bundle_dir, "ARDOUR_INSTANT_XML_PATH", "/Resources");
182         export_search_path (bundle_dir, "LADSPA_PATH", "/Plugins");
183         export_search_path (bundle_dir, "VAMP_PATH", "/lib");
184         export_search_path (bundle_dir, "SUIL_MODULE_DIR", "/lib");
185         export_search_path (bundle_dir, "GTK_PATH", "/lib/gtkengines");
186
187         /* unset GTK_RC_FILES so that we only load the RC files that we define
188          */
189
190         unsetenv ("GTK_RC_FILES");
191
192         /* write a pango.rc file and tell pango to use it. we'd love
193            to put this into the PROGRAM_NAME.app bundle and leave it there,
194            but the user may not have write permission. so ...
195
196            we also have to make sure that the user ardour directory
197            actually exists ...
198         */
199
200         if (g_mkdir_with_parents (userconfigdir.c_str(), 0755) < 0) {
201                 error << string_compose (_("cannot create user %3 folder %1 (%2)"), userconfigdir, strerror (errno), PROGRAM_NAME)
202                       << endmsg;
203         } else {
204                 
205                 path = Glib::build_filename (userconfigdir, "pango.rc");
206                 std::ofstream pangorc (path.c_str());
207                 if (!pangorc) {
208                         error << string_compose (_("cannot open pango.rc file %1") , path) << endmsg;
209                 } else {
210                         pangorc << "[Pango]\nModuleFiles="
211                                 << Glib::build_filename (bundle_dir, "Resources/pango.modules") 
212                                 << endl;
213                         pangorc.close ();
214                         
215                         setenv ("PANGO_RC_FILE", path.c_str(), 1);
216                 }
217         }
218         
219         setenv ("CHARSETALIASDIR", bundle_dir.c_str(), 1);
220         setenv ("FONTCONFIG_FILE", Glib::build_filename (bundle_dir, "Resources/fonts.conf").c_str(), 1);
221         setenv ("GDK_PIXBUF_MODULE_FILE", Glib::build_filename (bundle_dir, "Resources/gdk-pixbuf.loaders").c_str(), 1);
222 }
223
224 static void load_custom_fonts() {
225 #ifdef OSX_CUSTOM_FONT
226         std::string ardour_mono_file;
227
228         if (!find_file_in_search_path (ardour_data_search_path(), "ArdourMono.ttf", ardour_mono_file)) {
229                 cerr << _("Cannot find ArdourMono TrueType font") << endl;
230         }
231
232         CFStringRef ttf;
233         CFURLRef fontURL;
234         CFErrorRef error;
235         ttf = CFStringCreateWithBytes(
236                         kCFAllocatorDefault, (UInt8*) ardour_mono_file.c_str(),
237                         ardour_mono_file.length(),
238                         kCFStringEncodingUTF8, FALSE);
239         fontURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, ttf, kCFURLPOSIXPathStyle, TRUE);
240         if (CTFontManagerRegisterFontsForURL(fontURL, kCTFontManagerScopeProcess, &error) != true) {
241                 cerr << _("Cannot load ArdourMono TrueType font.") << endl;
242         }
243 #endif
244 }
245
246 #else
247
248 void
249 fixup_bundle_environment (int /*argc*/, char* argv[])
250 {
251         /* THIS IS FOR LINUX - its just about the only place where its
252          * acceptable to build paths directly using '/'.
253          */
254
255         if (!getenv ("ARDOUR_BUNDLED")) {
256                 return;
257         }
258
259         EnvironmentalProtectionAgency::set_global_epa (new EnvironmentalProtectionAgency (true, "PREBUNDLE_ENV"));
260
261         std::string path;
262         std::string dir_path = Glib::path_get_dirname (Glib::path_get_dirname (argv[0]));
263         std::string userconfigdir = user_config_directory();
264
265 #ifdef ENABLE_NLS
266         if (!ARDOUR::translations_are_enabled ()) {
267                 localedir = "/this/cannot/exist";
268         } else {
269                 /* force localedir into the bundle */
270                 vector<string> lpath;
271                 lpath.push_back (dir_path);
272                 lpath.push_back ("share");
273                 lpath.push_back ("locale");
274                 localedir = realpath (Glib::build_filename (lpath).c_str(), NULL);
275         }
276 #endif
277
278         /* note that this function is POSIX/Linux specific, so using / as
279            a dir separator in this context is just fine.
280         */
281
282         export_search_path (dir_path, "ARDOUR_DLL_PATH", "/lib");
283         export_search_path (dir_path, "ARDOUR_CONFIG_PATH", "/etc");
284         export_search_path (dir_path, "ARDOUR_INSTANT_XML_PATH", "/share");
285         export_search_path (dir_path, "ARDOUR_DATA_PATH", "/share");
286         export_search_path (dir_path, "LADSPA_PATH", "/plugins");
287         export_search_path (dir_path, "VAMP_PATH", "/lib");
288         export_search_path (dir_path, "SUIL_MODULE_DIR", "/lib");
289         export_search_path (dir_path, "GTK_PATH", "/lib/gtkengines");
290
291         /* unset GTK_RC_FILES so that we only load the RC files that we define
292          */
293
294         unsetenv ("GTK_RC_FILES");
295
296         /* Tell fontconfig where to find fonts.conf. Use the system version
297            if it exists, otherwise use the stuff we included in the bundle
298         */
299
300         if (Glib::file_test ("/etc/fonts/fonts.conf", Glib::FILE_TEST_EXISTS)) {
301                 setenv ("FONTCONFIG_FILE", "/etc/fonts/fonts.conf", 1);
302                 setenv ("FONTCONFIG_PATH", "/etc/fonts", 1);
303         } else {
304                 error << _("No fontconfig file found on your system. Things may looked very odd or ugly") << endmsg;
305         }
306
307         /* write a pango.rc file and tell pango to use it. we'd love
308            to put this into the Ardour.app bundle and leave it there,
309            but the user may not have write permission. so ...
310
311            we also have to make sure that the user ardour directory
312            actually exists ...
313         */
314
315         if (g_mkdir_with_parents (userconfigdir.c_str(), 0755) < 0) {
316                 error << string_compose (_("cannot create user %3 folder %1 (%2)"), userconfigdir, strerror (errno), PROGRAM_NAME)
317                       << endmsg;
318         } else {
319                 
320                 path = Glib::build_filename (userconfigdir, "pango.rc");
321                 std::ofstream pangorc (path.c_str());
322                 if (!pangorc) {
323                         error << string_compose (_("cannot open pango.rc file %1") , path) << endmsg;
324                 } else {
325                         pangorc << "[Pango]\nModuleFiles="
326                                 << Glib::build_filename (userconfigdir, "pango.modules")
327                                 << endl;
328                         pangorc.close ();
329                 }
330                 
331                 setenv ("PANGO_RC_FILE", path.c_str(), 1);
332                 
333                 /* similar for GDK pixbuf loaders, but there's no RC file required
334                    to specify where it lives.
335                 */
336                 
337                 setenv ("GDK_PIXBUF_MODULE_FILE", Glib::build_filename (userconfigdir, "gdk-pixbuf.loaders").c_str(), 1);
338         }
339
340         /* this doesn't do much but setting it should prevent various parts of the GTK/GNU stack
341            from looking outside the bundle to find the charset.alias file.
342         */
343         setenv ("CHARSETALIASDIR", dir_path.c_str(), 1);
344
345 }
346
347 static void load_custom_fonts() {
348         std::string ardour_mono_file;
349         if (!find_file_in_search_path (ardour_data_search_path(), "ArdourMono.ttf", ardour_mono_file)) {
350                 cerr << _("Cannot find ArdourMono TrueType font") << endl;
351         }
352
353         FcConfig *config = FcInitLoadConfigAndFonts();
354         FcBool ret = FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(ardour_mono_file.c_str()));
355         if (ret == FcFalse) {
356                 cerr << _("Cannot load ArdourMono TrueType font.") << endl;
357         }
358         ret = FcConfigSetCurrent(config);
359         if (ret == FcFalse) {
360                 cerr << _("Failed to set fontconfig configuration.") << endl;
361         }
362 }
363
364 #endif
365
366 static gboolean
367 tell_about_jack_death (void* /* ignored */)
368 {
369         if (AudioEngine::instance()->processed_frames() == 0) {
370                 /* died during startup */
371                 MessageDialog msg (_("JACK exited"), false);
372                 msg.set_position (Gtk::WIN_POS_CENTER);
373                 msg.set_secondary_text (string_compose (_(
374 "JACK exited unexpectedly, and without notifying %1.\n\
375 \n\
376 This could be due to misconfiguration or to an error inside JACK.\n\
377 \n\
378 Click OK to exit %1."), PROGRAM_NAME));
379
380                 msg.run ();
381                 _exit (0);
382
383         } else {
384
385                 /* engine has already run, so this is a mid-session JACK death */
386
387                 MessageDialog* msg = manage (new MessageDialog (_("JACK exited"), false));
388                 msg->set_secondary_text (string_compose (_(
389 "JACK exited unexpectedly, and without notifying %1.\n\
390 \n\
391 This is probably due to an error inside JACK. You should restart JACK\n\
392 and reconnect %1 to it, or exit %1 now. You cannot save your\n\
393 session at this time, because we would lose your connection information.\n"), PROGRAM_NAME));
394                 msg->present ();
395         }
396         return false; /* do not call again */
397 }
398
399 static void
400 sigpipe_handler (int /*signal*/)
401 {
402         /* XXX fix this so that we do this again after a reconnect to JACK
403          */
404
405         static bool done_the_jack_thing = false;
406
407         if (!done_the_jack_thing) {
408                 AudioEngine::instance()->died ();
409                 g_idle_add (tell_about_jack_death, 0);
410                 done_the_jack_thing =  true;
411         }
412 }
413
414 #ifdef WINDOWS_VST_SUPPORT
415
416 extern int windows_vst_gui_init (int* argc, char** argv[]);
417
418 /* this is called from the entry point of a wine-compiled
419    executable that is linked against gtk2_ardour built
420    as a shared library.
421 */
422 extern "C" {
423 int ardour_main (int argc, char *argv[])
424 #else
425 int main (int argc, char *argv[])
426 #endif
427 {
428         fixup_bundle_environment (argc, argv);
429
430         load_custom_fonts(); /* needs to happend before any gtk and pango init calls */
431
432         if (!Glib::thread_supported()) {
433                 Glib::thread_init();
434         }
435
436 #ifdef ENABLE_NLS
437         gtk_set_locale ();
438 #endif
439
440 #ifdef WINDOWS_VST_SUPPORT
441         /* this does some magic that is needed to make GTK and Wine's own
442            X11 client interact properly.
443         */
444         windows_vst_gui_init (&argc, &argv);
445 #endif
446
447 #ifdef ENABLE_NLS
448         cerr << "bnd txt domain [" << PACKAGE << "] to " << localedir << endl;
449
450         (void) bindtextdomain (PACKAGE, localedir);
451         /* our i18n translations are all in UTF-8, so make sure
452            that even if the user locale doesn't specify UTF-8,
453            we use that when handling them.
454         */
455         (void) bind_textdomain_codeset (PACKAGE,"UTF-8");
456 #endif
457
458         pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
459
460         // catch error message system signals ();
461
462         text_receiver.listen_to (error);
463         text_receiver.listen_to (info);
464         text_receiver.listen_to (fatal);
465         text_receiver.listen_to (warning);
466
467 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
468         if (getenv ("BOOST_DEBUG")) {
469                 boost_debug_shared_ptr_show_live_debugging (true);
470         }
471 #endif
472
473         if (parse_opts (argc, argv)) {
474                 exit (1);
475         }
476
477         if (curvetest_file) {
478                 return curvetest (curvetest_file);
479         }
480
481         cout << PROGRAM_NAME
482              << VERSIONSTRING
483              << _(" (built using ")
484              << svn_revision
485 #ifdef __GNUC__
486              << _(" and GCC version ") << __VERSION__
487 #endif
488              << ')'
489              << endl;
490
491         if (just_version) {
492                 exit (0);
493         }
494
495         if (no_splash) {
496                 cerr << _("Copyright (C) 1999-2012 Paul Davis") << endl
497                      << _("Some portions Copyright (C) Steve Harris, Ari Johnson, Brett Viren, Joel Baker, Robin Gareus") << endl
498                      << endl
499                      << string_compose (_("%1 comes with ABSOLUTELY NO WARRANTY"), PROGRAM_NAME) << endl
500                      << _("not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.") << endl
501                      << _("This is free software, and you are welcome to redistribute it ") << endl
502                      << _("under certain conditions; see the source for copying conditions.")
503                      << endl;
504         }
505
506         /* some GUI objects need this */
507
508         PBD::ID::init ();
509
510         if (::signal (SIGPIPE, sigpipe_handler)) {
511                 cerr << _("Cannot xinstall SIGPIPE error handler") << endl;
512         }
513
514         try {
515                 ui = new ARDOUR_UI (&argc, &argv, localedir);
516         } catch (failed_constructor& err) {
517                 error << string_compose (_("could not create %1 GUI"), PROGRAM_NAME) << endmsg;
518                 exit (1);
519         }
520
521         ui->run (text_receiver);
522         Gtkmm2ext::Application::instance()->cleanup();
523         ui = 0;
524
525         ARDOUR::cleanup ();
526         pthread_cancel_all ();
527
528         return 0;
529 }
530 #ifdef WINDOWS_VST_SUPPORT
531 } // end of extern C block
532 #endif
533