add execpath dir to PATH for bundled OSX build
[ardour.git] / gtk2_ardour / main.cc
1 /*
2     Copyright (C) 2001-2007 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
22 #include <sigc++/bind.h>
23 #include <gtkmm/settings.h>
24 #include <glibmm/ustring.h>
25
26 #include <pbd/error.h>
27 #include <pbd/textreceiver.h>
28 #include <pbd/failed_constructor.h>
29 #include <pbd/pthread_utils.h>
30
31 #include <jack/jack.h>
32
33 #include <ardour/version.h>
34 #include <ardour/ardour.h>
35 #include <ardour/audioengine.h>
36
37 #include <gtkmm/main.h>
38 #include <gtkmm2ext/popup.h>
39 #include <gtkmm2ext/utils.h>
40
41 #include "svn_revision.h"
42 #include "version.h"
43 #include "ardour_ui.h"
44 #include "opts.h"
45 #include "enums.h"
46
47 #include "i18n.h"
48
49 using namespace Gtk;
50 using namespace ARDOUR_COMMAND_LINE;
51 using namespace ARDOUR;
52 using namespace PBD;
53 using namespace sigc;
54
55 TextReceiver text_receiver ("ardour");
56
57 extern int curvetest (string);
58
59 static ARDOUR_UI  *ui = 0;
60 static char* localedir = LOCALEDIR;
61
62 gint
63 show_ui_callback (void *arg)
64 {
65         ARDOUR_UI * ui = (ARDOUR_UI *) arg;
66
67         ui->hide_splash();
68         
69         return FALSE;
70 }
71
72 #ifdef __APPLE__
73
74 #include <mach-o/dyld.h>
75 #include <sys/param.h>
76 #include <fstream>
77
78 void
79 fixup_bundle_environment ()
80 {
81         if (!getenv ("ARDOUR_BUNDLED")) {
82                 return;
83         }
84
85         char execpath[MAXPATHLEN+1];
86         uint32_t pathsz = sizeof (execpath);
87
88         _NSGetExecutablePath (execpath, &pathsz);
89
90         Glib::ustring exec_path (execpath);
91         Glib::ustring dir_path = Glib::path_get_dirname (exec_path);
92         Glib::ustring path;
93         const char *cstr = getenv ("PATH");
94
95         /* ensure that we find any bundled executables (e.g. JACK) */
96
97         path = dir_path;
98         if (cstr) {
99                 path += ':';
100                 path += cstr;
101         }
102         setenv ("PATH", path._cstr(), 1);
103
104         path = dir_path;
105         path += "/../Resources";
106         path += dir_path;
107         path += "/../Resources/Surfaces";
108         path += dir_path;
109         path += "/../Resources/Panners";
110
111         setenv ("ARDOUR_MODULE_PATH", path.c_str(), 1);
112
113         path = dir_path;
114         path += "/../Resources/icons:";
115         path += dir_path;
116         path += "/../Resources/pixmaps:";
117         path += dir_path;
118         path += "/../Resources/share:";
119         path += dir_path;
120         path += "/../Resources";
121
122         setenv ("ARDOUR_PATH", path.c_str(), 1);
123         setenv ("ARDOUR_CONFIG_PATH", path.c_str(), 1);
124         setenv ("ARDOUR_DATA_PATH", path.c_str(), 1);
125
126         path = dir_path;
127         path += "/../Frameworks/clearlooks";
128
129         setenv ("GTK_PATH", path.c_str(), 1);
130
131         path = dir_path;
132         path += "/../Resources/locale";
133         
134         localedir = strdup (path.c_str());
135
136         /* write a pango.rc file and tell pango to use it */
137
138         path = dir_path;
139         path += "/../Resources/pango.rc";
140
141         std::ofstream pangorc (path.c_str());
142         if (!pangorc) {
143                 error << string_compose (_("cannot open pango.rc file %1") , path) << endmsg;
144         } else {
145                 pangorc << "[Pango]\nModuleFiles=";
146                 Glib::ustring mpath = dir_path;
147                 mpath += "/../Resources/pango.modules";
148                 pangorc << mpath << endl;
149                 
150                 pangorc.close ();
151                 setenv ("PANGO_RC_FILE", path.c_str(), 1);
152         }
153
154         // gettext charset aliases
155
156         setenv ("CHARSETALIASDIR", path.c_str(), 1);
157
158         // font config
159         
160         path = dir_path;
161         path += "/../Resources/fonts.conf";
162
163         setenv ("FONTCONFIG_FILE", path.c_str(), 1);
164
165         // GDK Pixbuf loader module file
166
167         path = dir_path;
168         path += "/../Resources/gdk-pixbuf.loaders";
169
170         setenv ("GDK_PIXBUF_MODULE_FILE", path.c_str(), 1);
171
172         if (getenv ("ARDOUR_WITH_JACK")) {
173                 // JACK driver dir
174                 
175                 path = dir_path;
176                 path += "/../Frameworks";
177                 
178                 setenv ("JACK_DRIVER_DIR", path.c_str(), 1);
179         }
180 }
181
182 #endif
183
184 #ifdef VST_SUPPORT
185 /* this is called from the entry point of a wine-compiled
186    executable that is linked against gtk2_ardour built
187    as a shared library.
188 */
189 extern "C" {
190 int ardour_main (int argc, char *argv[])
191 #else
192 int main (int argc, char* argv[])
193 #endif
194 {
195         vector<Glib::ustring> null_file_list;
196
197 #ifdef __APPLE__
198         fixup_bundle_environment ();
199 #endif
200
201         Glib::thread_init();
202         gtk_set_locale ();
203
204         (void) bindtextdomain (PACKAGE, localedir);
205         /* our i18n translations are all in UTF-8, so make sure
206            that even if the user locale doesn't specify UTF-8,
207            we use that when handling them.
208         */
209         (void) bind_textdomain_codeset (PACKAGE,"UTF-8");
210         (void) textdomain (PACKAGE);
211
212         pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
213
214         // catch error message system signals ();
215
216         text_receiver.listen_to (error);
217         text_receiver.listen_to (info);
218         text_receiver.listen_to (fatal);
219         text_receiver.listen_to (warning);
220
221         if (parse_opts (argc, argv)) {
222                 exit (1);
223         }
224
225         if (curvetest_file) {
226                 return curvetest (curvetest_file);
227         }
228         
229         cout << _("Ardour/GTK ") 
230              << VERSIONSTRING
231              << _("\n   (built using ")
232              << ardour_svn_revision
233 #ifdef __GNUC__
234              << _(" and GCC version ") << __VERSION__ 
235 #endif
236              << ')'
237              << endl;
238         
239         if (just_version) {
240                 exit (0);
241         }
242
243         if (no_splash) {
244                 cerr << _("Copyright (C) 1999-2007 Paul Davis") << endl
245                      << _("Some portions Copyright (C) Steve Harris, Ari Johnson, Brett Viren, Joel Baker") << endl
246                      << endl
247                      << _("Ardour comes with ABSOLUTELY NO WARRANTY") << endl
248                      << _("not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.") << endl
249                      << _("This is free software, and you are welcome to redistribute it ") << endl
250                      << _("under certain conditions; see the source for copying conditions.")
251                      << endl;
252         }
253
254         /* some GUI objects need this */
255
256         PBD::ID::init ();
257
258         try { 
259                 ui = new ARDOUR_UI (&argc, &argv);
260         } catch (failed_constructor& err) {
261                 error << _("could not create ARDOUR GUI") << endmsg;
262                 exit (1);
263         }
264
265         if (!no_splash) {
266                 ui->show_splash ();
267                 if (session_name.length()) {  
268                         g_timeout_add (4000, show_ui_callback, ui);
269                 }
270         }
271
272         if (!keybindings_path.empty()) {
273                 ui->set_keybindings_path (keybindings_path);
274         }
275
276         ui->run (text_receiver);
277         ui = 0;
278
279         ARDOUR::cleanup ();
280         pthread_cancel_all ();
281         return 0;
282 }
283 #ifdef VST_SUPPORT
284 } // end of extern C block
285 #endif
286