fun with the c-preprocessor. platform dependent VST support.
[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/file_utils.h"
31 #include "pbd/textreceiver.h"
32 #include "pbd/failed_constructor.h"
33 #include "pbd/pathexpand.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 "ardour/revision.h"
40 #include "ardour/version.h"
41 #include "ardour/ardour.h"
42 #include "ardour/audioengine.h"
43 #include "ardour/session_utils.h"
44 #include "ardour/filesystem_paths.h"
45
46 #include <gtkmm/main.h>
47 #include <gtkmm2ext/application.h>
48 #include <gtkmm2ext/popup.h>
49 #include <gtkmm2ext/utils.h>
50
51 #include "version.h"
52 #include "utils.h"
53 #include "ardour_ui.h"
54 #include "opts.h"
55 #include "enums.h"
56 #include "bundle_env.h"
57
58 #include "i18n.h"
59
60
61 using namespace std;
62 using namespace Gtk;
63 using namespace ARDOUR_COMMAND_LINE;
64 using namespace ARDOUR;
65 using namespace PBD;
66
67 TextReceiver text_receiver ("ardour");
68
69 extern int curvetest (string);
70
71 static ARDOUR_UI  *ui = 0;
72 static const char* localedir = LOCALEDIR;
73
74 void
75 gui_jack_error ()
76 {
77         MessageDialog win (string_compose (_("%1 could not connect to the audio backend."), PROGRAM_NAME),
78                            false,
79                            Gtk::MESSAGE_INFO,
80                            Gtk::BUTTONS_NONE);
81
82         win.add_button (Stock::QUIT, RESPONSE_CLOSE);
83         win.set_default_response (RESPONSE_CLOSE);
84
85         win.show_all ();
86         win.set_position (Gtk::WIN_POS_CENTER);
87
88         if (!no_splash) {
89                 ui->hide_splash ();
90         }
91
92         /* we just don't care about the result, but we want to block */
93
94         win.run ();
95 }
96
97 static gboolean
98 tell_about_backend_death (void* /* ignored */)
99 {
100         if (AudioEngine::instance()->processed_frames() == 0) {
101                 /* died during startup */
102                 MessageDialog msg (string_compose (_("The audio backend (%1) has failed, or terminated"), AudioEngine::instance()->current_backend_name()), false);
103                 msg.set_position (Gtk::WIN_POS_CENTER);
104                 msg.set_secondary_text (string_compose (_(
105 "%2 exited unexpectedly, and without notifying %1.\n\
106 \n\
107 This could be due to misconfiguration or to an error inside %2.\n\
108 \n\
109 Click OK to exit %1."), PROGRAM_NAME, AudioEngine::instance()->current_backend_name()));
110
111                 msg.run ();
112                 _exit (0);
113
114         } else {
115
116                 /* engine has already run, so this is a mid-session backend death */
117                         
118                 MessageDialog msg (string_compose (_("The audio backend (%1) has failed, or terminated"), AudioEngine::instance()->current_backend_name()), false);
119                 msg.set_secondary_text (string_compose (_("%2 exited unexpectedly, and without notifying %1."),
120                                                          PROGRAM_NAME, AudioEngine::instance()->current_backend_name()));
121                 msg.present ();
122         }
123         return false; /* do not call again */
124 }
125
126 static void
127 sigpipe_handler (int /*signal*/)
128 {
129         /* XXX fix this so that we do this again after a reconnect to the backend
130          */
131
132         static bool done_the_backend_thing = false;
133
134         if (!done_the_backend_thing) {
135                 AudioEngine::instance()->died ();
136                 g_idle_add (tell_about_backend_death, 0);
137                 done_the_backend_thing =  true;
138         }
139 }
140
141 #if (defined WINDOWS_VST_SUPPORT && !defined PLATFORM_WINDOWS)
142 /*
143  *  Release build with MSVC uses ardour_main()
144  */
145 int ardour_main (int argc, char *argv[])
146
147 #elif (defined WINDOWS_VST_SUPPORT && !defined PLATFORM_WINDOWS)
148
149 // prototype for function in windows_vst_plugin_ui.cc
150 extern int windows_vst_gui_init (int* argc, char** argv[]);
151
152 /* this is called from the entry point of a wine-compiled
153    executable that is linked against gtk2_ardour built
154    as a shared library.
155 */
156 extern "C" {
157
158 int ardour_main (int argc, char *argv[])
159
160 #else
161 int main (int argc, char *argv[])
162 #endif
163 {
164         fixup_bundle_environment (argc, argv, &localedir);
165
166         load_custom_fonts(); /* needs to happen before any gtk and pango init calls */
167
168         if (!Glib::thread_supported()) {
169                 Glib::thread_init();
170         }
171
172 #ifdef ENABLE_NLS
173         gtk_set_locale ();
174 #endif
175
176 #ifdef WINDOWS_VST_SUPPORT
177         /* this does some magic that is needed to make GTK and X11 client interact properly.
178          * the platform dependent code is in windows_vst_plugin_ui.cc
179          */
180         windows_vst_gui_init (&argc, &argv);
181 #endif
182
183 #ifdef ENABLE_NLS
184         cerr << "bind txt domain [" << PACKAGE << "] to " << localedir << endl;
185
186         (void) bindtextdomain (PACKAGE, localedir);
187         /* our i18n translations are all in UTF-8, so make sure
188            that even if the user locale doesn't specify UTF-8,
189            we use that when handling them.
190         */
191         (void) bind_textdomain_codeset (PACKAGE,"UTF-8");
192 #endif
193
194         pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
195
196         // catch error message system signals ();
197
198         text_receiver.listen_to (error);
199         text_receiver.listen_to (info);
200         text_receiver.listen_to (fatal);
201         text_receiver.listen_to (warning);
202
203 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
204         if (g_getenv ("BOOST_DEBUG")) {
205                 boost_debug_shared_ptr_show_live_debugging (true);
206         }
207 #endif
208
209         if (parse_opts (argc, argv)) {
210                 exit (1);
211         }
212
213         cout << PROGRAM_NAME
214              << VERSIONSTRING
215              << _(" (built using ")
216              << revision
217 #ifdef __GNUC__
218              << _(" and GCC version ") << __VERSION__
219 #endif
220              << ')'
221              << endl;
222
223         if (just_version) {
224                 exit (0);
225         }
226
227         if (no_splash) {
228                 cerr << _("Copyright (C) 1999-2012 Paul Davis") << endl
229                      << _("Some portions Copyright (C) Steve Harris, Ari Johnson, Brett Viren, Joel Baker, Robin Gareus") << endl
230                      << endl
231                      << string_compose (_("%1 comes with ABSOLUTELY NO WARRANTY"), PROGRAM_NAME) << endl
232                      << _("not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.") << endl
233                      << _("This is free software, and you are welcome to redistribute it ") << endl
234                      << _("under certain conditions; see the source for copying conditions.")
235                      << endl;
236         }
237
238         /* some GUI objects need this */
239
240         if (!ARDOUR::init (ARDOUR_COMMAND_LINE::use_vst, ARDOUR_COMMAND_LINE::try_hw_optimization, localedir)) {
241                 error << string_compose (_("could not initialize %1."), PROGRAM_NAME) << endmsg;
242                 exit (1);
243         }
244
245         if (curvetest_file) {
246                 return curvetest (curvetest_file);
247         }
248
249 #ifndef PLATFORM_WINDOWS
250         if (::signal (SIGPIPE, sigpipe_handler)) {
251                 cerr << _("Cannot xinstall SIGPIPE error handler") << endl;
252         }
253 #endif
254
255         try {
256                 ui = new ARDOUR_UI (&argc, &argv, localedir);
257         } catch (failed_constructor& err) {
258                 error << string_compose (_("could not create %1 GUI"), PROGRAM_NAME) << endmsg;
259                 exit (1);
260         }
261
262         ui->run (text_receiver);
263         Gtkmm2ext::Application::instance()->cleanup();
264         delete ui;
265         ui = 0;
266
267         ARDOUR::cleanup ();
268         pthread_cancel_all ();
269
270         return 0;
271 }
272 #if (defined WINDOWS_VST_SUPPORT && !defined PLATFORM_WINDOWS)
273 } // end of extern "C" block
274 #endif