provide instructional hint for keyeditor; fix keybinding issues on OS X (needs curren...
[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 #ifdef __APPLE__
63
64 #include <mach-o/dyld.h>
65 #include <sys/param.h>
66 #include <fstream>
67
68 void
69 fixup_bundle_environment ()
70 {
71         if (!getenv ("ARDOUR_BUNDLED")) {
72                 return;
73         }
74
75         char execpath[MAXPATHLEN+1];
76         uint32_t pathsz = sizeof (execpath);
77
78         _NSGetExecutablePath (execpath, &pathsz);
79
80         Glib::ustring exec_path (execpath);
81         Glib::ustring dir_path = Glib::path_get_dirname (exec_path);
82         Glib::ustring path;
83         const char *cstr = getenv ("PATH");
84
85         /* ensure that we find any bundled executables (e.g. JACK) */
86
87         path = dir_path;
88         if (cstr) {
89                 path += ':';
90                 path += cstr;
91         }
92         setenv ("PATH", path.c_str(), 1);
93
94         path = dir_path;
95         path += "/../Resources";
96         path += dir_path;
97         path += "/../Resources/Surfaces";
98         path += dir_path;
99         path += "/../Resources/Panners";
100
101         setenv ("ARDOUR_MODULE_PATH", path.c_str(), 1);
102
103         path = dir_path;
104         path += "/../Resources/icons:";
105         path += dir_path;
106         path += "/../Resources/pixmaps:";
107         path += dir_path;
108         path += "/../Resources/share:";
109         path += dir_path;
110         path += "/../Resources";
111
112         setenv ("ARDOUR_PATH", path.c_str(), 1);
113         setenv ("ARDOUR_CONFIG_PATH", path.c_str(), 1);
114         setenv ("ARDOUR_DATA_PATH", path.c_str(), 1);
115
116         cstr = getenv ("LADSPA_PATH");
117         if (cstr) {
118                 path = cstr;
119                 path += ':';
120         }
121         path = dir_path;
122         path += "/../Plugins";
123         
124         setenv ("LADSPA_PATH", path.c_str(), 1);
125
126         cstr = getenv ("VAMP_PATH");
127         if (cstr) {
128                 path = cstr;
129                 path += ':';
130         }
131         path = dir_path;
132         path += "/../Frameworks";
133         
134         setenv ("VAMP_PATH", path.c_str(), 1);
135
136         cstr = getenv ("ARDOUR_CONTROL_SURFACE_PATH");
137         if (cstr) {
138                 path = cstr;
139                 path += ':';
140         }
141         path = dir_path;
142         path += "/../Surfaces";
143         
144         setenv ("ARDOUR_CONTROL_SURFACE_PATH", path.c_str(), 1);
145
146         cstr = getenv ("LV2_PATH");
147         if (cstr) {
148                 path = cstr;
149                 path += ':';
150         }
151         path = dir_path;
152         path += "/../Plugins";
153         
154         setenv ("LV2_PATH", path.c_str(), 1);
155
156         path = dir_path;
157         path += "/../Frameworks/clearlooks";
158
159         setenv ("GTK_PATH", path.c_str(), 1);
160
161         path = dir_path;
162         path += "/../Resources/locale";
163         
164         localedir = strdup (path.c_str());
165
166         /* write a pango.rc file and tell pango to use it */
167
168         path = dir_path;
169         path += "/../Resources/pango.rc";
170
171         std::ofstream pangorc (path.c_str());
172         if (!pangorc) {
173                 error << string_compose (_("cannot open pango.rc file %1") , path) << endmsg;
174         } else {
175                 pangorc << "[Pango]\nModuleFiles=";
176                 Glib::ustring mpath = dir_path;
177                 mpath += "/../Resources/pango.modules";
178                 pangorc << mpath << endl;
179                 
180                 pangorc.close ();
181                 setenv ("PANGO_RC_FILE", path.c_str(), 1);
182         }
183
184         // gettext charset aliases
185
186         setenv ("CHARSETALIASDIR", path.c_str(), 1);
187
188         // font config
189         
190         path = dir_path;
191         path += "/../Resources/fonts.conf";
192
193         setenv ("FONTCONFIG_FILE", path.c_str(), 1);
194
195         // GDK Pixbuf loader module file
196
197         path = dir_path;
198         path += "/../Resources/gdk-pixbuf.loaders";
199
200         setenv ("GDK_PIXBUF_MODULE_FILE", path.c_str(), 1);
201
202         if (getenv ("ARDOUR_WITH_JACK")) {
203                 // JACK driver dir
204                 
205                 path = dir_path;
206                 path += "/../Frameworks";
207                 
208                 setenv ("JACK_DRIVER_DIR", path.c_str(), 1);
209         }
210 }
211
212 #endif
213
214 static void
215 setup_keybindings (ARDOUR_UI* ui)
216 {
217         Glib::ustring path;
218
219         if (keybindings_path.empty()) {
220                 keybindings_path = "ardour";
221         }
222
223         std::string kbpath;
224         
225         if (keybindings_path.find (".bindings") == string::npos) {
226
227                 // just a style name - allow user to
228                 // specify the layout type. 
229                 
230                 char* layout;
231                 
232                 if ((layout = getenv ("ARDOUR_KEYBOARD_LAYOUT")) != 0) {
233                         keybindings_path += '-';
234                         keybindings_path += layout;
235                 }
236
237                 keybindings_path += ".bindings";
238         } 
239
240         
241         // XXX timbyr - we need a portable test for "is-absolute" here 
242         
243         if (keybindings_path[0] != '/' && keybindings_path[0] != '.') {
244
245                 /* not absolute - look in the usual places */
246                 
247                 path = find_config_file (keybindings_path);
248
249                 if (path.empty()) {
250                         warning << string_compose (_("Key bindings file \"%1\" not found. Default bindings used instead"), 
251                                                    keybindings_path)
252                                 << endmsg;
253                 }
254
255         } else {
256
257                 // absolute path from user - use it as is
258
259                 path = keybindings_path;
260         }
261
262         if (!path.empty()) {
263                 ui->set_keybindings_path (path);
264         }
265 }
266
267 #ifdef VST_SUPPORT
268 /* this is called from the entry point of a wine-compiled
269    executable that is linked against gtk2_ardour built
270    as a shared library.
271 */
272 extern "C" {
273 int ardour_main (int argc, char *argv[])
274 #else
275 int main (int argc, char* argv[])
276 #endif
277 {
278         vector<Glib::ustring> null_file_list;
279
280 #ifdef __APPLE__
281         fixup_bundle_environment ();
282 #endif
283
284         Glib::thread_init();
285         gtk_set_locale ();
286
287         (void) bindtextdomain (PACKAGE, localedir);
288         /* our i18n translations are all in UTF-8, so make sure
289            that even if the user locale doesn't specify UTF-8,
290            we use that when handling them.
291         */
292         (void) bind_textdomain_codeset (PACKAGE,"UTF-8");
293         (void) textdomain (PACKAGE);
294
295         pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
296
297         // catch error message system signals ();
298
299         text_receiver.listen_to (error);
300         text_receiver.listen_to (info);
301         text_receiver.listen_to (fatal);
302         text_receiver.listen_to (warning);
303
304         if (parse_opts (argc, argv)) {
305                 exit (1);
306         }
307
308         if (curvetest_file) {
309                 return curvetest (curvetest_file);
310         }
311         
312         cout << _("Ardour/GTK ") 
313              << VERSIONSTRING
314              << _("\n   (built using ")
315              << ardour_svn_revision
316 #ifdef __GNUC__
317              << _(" and GCC version ") << __VERSION__ 
318 #endif
319              << ')'
320              << endl;
321         
322         if (just_version) {
323                 exit (0);
324         }
325
326         if (no_splash) {
327                 cerr << _("Copyright (C) 1999-2007 Paul Davis") << endl
328                      << _("Some portions Copyright (C) Steve Harris, Ari Johnson, Brett Viren, Joel Baker") << endl
329                      << endl
330                      << _("Ardour comes with ABSOLUTELY NO WARRANTY") << endl
331                      << _("not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.") << endl
332                      << _("This is free software, and you are welcome to redistribute it ") << endl
333                      << _("under certain conditions; see the source for copying conditions.")
334                      << endl;
335         }
336
337         /* some GUI objects need this */
338
339         PBD::ID::init ();
340
341         try { 
342                 ui = new ARDOUR_UI (&argc, &argv);
343         } catch (failed_constructor& err) {
344                 error << _("could not create ARDOUR GUI") << endmsg;
345                 exit (1);
346         }
347
348         setup_keybindings (ui);
349
350         ui->run (text_receiver);
351         ui = 0;
352
353         ARDOUR::cleanup ();
354         pthread_cancel_all ();
355         return 0;
356 }
357 #ifdef VST_SUPPORT
358 } // end of extern C block
359 #endif
360