explicitly shutdown engine at exit
[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/ardour.h"
41 #include "ardour/audioengine.h"
42 #include "ardour/session_utils.h"
43 #include "ardour/filesystem_paths.h"
44
45 #include <gtkmm/main.h>
46 #include <gtkmm2ext/application.h>
47 #include <gtkmm2ext/popup.h>
48 #include <gtkmm2ext/utils.h>
49
50 #include "ardour_ui.h"
51 #include "opts.h"
52 #include "enums.h"
53 #include "bundle_env.h"
54
55 #include "i18n.h"
56
57 #ifdef PLATFORM_WINDOWS
58 #include <fcntl.h> // Needed for '_fmode'
59 #include <shellapi.h> // console
60 #endif
61
62 #ifdef WAF_BUILD
63 #include "gtk2ardour-version.h"
64 #endif
65
66 using namespace std;
67 using namespace Gtk;
68 using namespace ARDOUR_COMMAND_LINE;
69 using namespace ARDOUR;
70 using namespace PBD;
71
72 TextReceiver text_receiver ("ardour");
73
74 extern int curvetest (string);
75
76 static ARDOUR_UI  *ui = 0;
77 static const char* localedir = LOCALEDIR;
78
79 void
80 gui_jack_error ()
81 {
82         MessageDialog win (string_compose (_("%1 could not connect to the audio backend."), PROGRAM_NAME),
83                            false,
84                            Gtk::MESSAGE_INFO,
85                            Gtk::BUTTONS_NONE);
86
87         win.add_button (Stock::QUIT, RESPONSE_CLOSE);
88         win.set_default_response (RESPONSE_CLOSE);
89
90         win.show_all ();
91         win.set_position (Gtk::WIN_POS_CENTER);
92
93         if (!no_splash) {
94                 ui->hide_splash ();
95         }
96
97         /* we just don't care about the result, but we want to block */
98
99         win.run ();
100 }
101
102 static gboolean
103 tell_about_backend_death (void* /* ignored */)
104 {
105         if (AudioEngine::instance()->processed_frames() == 0) {
106                 /* died during startup */
107                 MessageDialog msg (string_compose (_("The audio backend (%1) has failed, or terminated"), AudioEngine::instance()->current_backend_name()), false);
108                 msg.set_position (Gtk::WIN_POS_CENTER);
109                 msg.set_secondary_text (string_compose (_(
110 "%2 exited unexpectedly, and without notifying %1.\n\
111 \n\
112 This could be due to misconfiguration or to an error inside %2.\n\
113 \n\
114 Click OK to exit %1."), PROGRAM_NAME, AudioEngine::instance()->current_backend_name()));
115
116                 msg.run ();
117                 _exit (0);
118
119         } else {
120
121                 /* engine has already run, so this is a mid-session backend death */
122                         
123                 MessageDialog msg (string_compose (_("The audio backend (%1) has failed, or terminated"), AudioEngine::instance()->current_backend_name()), false);
124                 msg.set_secondary_text (string_compose (_("%2 exited unexpectedly, and without notifying %1."),
125                                                          PROGRAM_NAME, AudioEngine::instance()->current_backend_name()));
126                 msg.present ();
127         }
128         return false; /* do not call again */
129 }
130
131 #ifndef PLATFORM_WINDOWS
132 static void
133 sigpipe_handler (int /*signal*/)
134 {
135         /* XXX fix this so that we do this again after a reconnect to the backend
136          */
137
138         static bool done_the_backend_thing = false;
139
140         if (!done_the_backend_thing) {
141                 AudioEngine::instance()->died ();
142                 g_idle_add (tell_about_backend_death, 0);
143                 done_the_backend_thing =  true;
144         }
145 }
146 #endif
147
148
149 #if (!defined COMPILER_MSVC && defined PLATFORM_WINDOWS)
150 static bool IsAConsolePort (HANDLE handle)
151 {
152         DWORD mode;
153         return (GetConsoleMode(handle, &mode) != 0);
154 }
155 #endif
156
157
158 #if (defined(COMPILER_MSVC) && defined(NDEBUG) && !defined(RDC_BUILD))
159 /*
160  *  Release build with MSVC uses ardour_main()
161  */
162 int ardour_main (int argc, char *argv[])
163
164 #elif (defined WINDOWS_VST_SUPPORT && !defined PLATFORM_WINDOWS)
165
166 // prototype for function in windows_vst_plugin_ui.cc
167 extern int windows_vst_gui_init (int* argc, char** argv[]);
168
169 /* this is called from the entry point of a wine-compiled
170    executable that is linked against gtk2_ardour built
171    as a shared library.
172 */
173 extern "C" {
174
175 int ardour_main (int argc, char *argv[])
176
177 #else
178 int main (int argc, char *argv[])
179 #endif
180 {
181         fixup_bundle_environment (argc, argv, &localedir);
182
183         load_custom_fonts(); /* needs to happen before any gtk and pango init calls */
184
185         if (!Glib::thread_supported()) {
186                 Glib::thread_init();
187         }
188
189 #ifdef ENABLE_NLS
190         gtk_set_locale ();
191 #endif
192
193 #if (!defined COMPILER_MSVC && defined PLATFORM_WINDOWS)
194         /* re-attach to the console so we can see 'printf()' output etc.
195          * for MSVC see  gtk2_ardour/msvc/winmain.cc
196          */
197         FILE  *pStdOut = 0, *pStdErr = 0;
198         BOOL  bConsole = AttachConsole(ATTACH_PARENT_PROCESS);
199         HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
200
201         if ((bConsole) && (IsAConsolePort(hStdOut))) {
202                 pStdOut = freopen( "CONOUT$", "w", stdout );
203                 pStdErr = freopen( "CONOUT$", "w", stderr );
204         }
205 #endif
206
207 #if (defined WINDOWS_VST_SUPPORT && !defined PLATFORM_WINDOWS)
208         /* this does some magic that is needed to make GTK and X11 client interact properly.
209          * the platform dependent code is in windows_vst_plugin_ui.cc
210          */
211         windows_vst_gui_init (&argc, &argv);
212 #endif
213
214 #ifdef ENABLE_NLS
215         cerr << "bind txt domain [" << PACKAGE << "] to " << localedir << endl;
216
217         (void) bindtextdomain (PACKAGE, localedir);
218         /* our i18n translations are all in UTF-8, so make sure
219            that even if the user locale doesn't specify UTF-8,
220            we use that when handling them.
221         */
222         (void) bind_textdomain_codeset (PACKAGE,"UTF-8");
223 #endif
224
225         pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
226
227         // catch error message system signals ();
228
229         text_receiver.listen_to (error);
230         text_receiver.listen_to (info);
231         text_receiver.listen_to (fatal);
232         text_receiver.listen_to (warning);
233
234 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
235         if (g_getenv ("BOOST_DEBUG")) {
236                 boost_debug_shared_ptr_show_live_debugging (true);
237         }
238 #endif
239
240         if (parse_opts (argc, argv)) {
241 #if (defined(COMPILER_MSVC) && defined(NDEBUG) && !defined(RDC_BUILD))
242         // Since we don't ordinarily have access to stdout and stderr with
243         // an MSVC app, let the user know we encountered a parsing error.
244         Gtk::Main app(&argc, &argv); // Calls 'gtk_init()'
245
246         Gtk::MessageDialog dlgReportParseError (_("\n   Ardour could not understand your command line      "),
247                                                       false, MESSAGE_ERROR, BUTTONS_CLOSE, true);
248         dlgReportParseError.set_title (_("An error was encountered while launching Ardour"));
249                 dlgReportParseError.run ();
250 #endif
251                 exit (1);
252         }
253
254         cout << PROGRAM_NAME
255              << VERSIONSTRING
256              << _(" (built using ")
257              << revision
258 #ifdef __GNUC__
259              << _(" and GCC version ") << __VERSION__
260 #endif
261              << ')'
262              << endl;
263
264         if (just_version) {
265                 exit (0);
266         }
267
268         if (no_splash) {
269                 cerr << _("Copyright (C) 1999-2012 Paul Davis") << endl
270                      << _("Some portions Copyright (C) Steve Harris, Ari Johnson, Brett Viren, Joel Baker, Robin Gareus") << endl
271                      << endl
272                      << string_compose (_("%1 comes with ABSOLUTELY NO WARRANTY"), PROGRAM_NAME) << endl
273                      << _("not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.") << endl
274                      << _("This is free software, and you are welcome to redistribute it ") << endl
275                      << _("under certain conditions; see the source for copying conditions.")
276                      << endl;
277         }
278
279         /* some GUI objects need this */
280
281         if (!ARDOUR::init (ARDOUR_COMMAND_LINE::use_vst, ARDOUR_COMMAND_LINE::try_hw_optimization, localedir)) {
282                 error << string_compose (_("could not initialize %1."), PROGRAM_NAME) << endmsg;
283                 exit (1);
284         }
285
286         if (curvetest_file) {
287                 return curvetest (curvetest_file);
288         }
289
290 #ifndef PLATFORM_WINDOWS
291         if (::signal (SIGPIPE, sigpipe_handler)) {
292                 cerr << _("Cannot xinstall SIGPIPE error handler") << endl;
293         }
294 #endif
295
296         try {
297                 ui = new ARDOUR_UI (&argc, &argv, localedir);
298         } catch (failed_constructor& err) {
299                 error << string_compose (_("could not create %1 GUI"), PROGRAM_NAME) << endmsg;
300                 exit (1);
301         }
302
303         ui->run (text_receiver);
304         ui->disconnect_from_engine();
305         Gtkmm2ext::Application::instance()->cleanup();
306         delete ui;
307         ui = 0;
308
309         ARDOUR::cleanup ();
310         pthread_cancel_all ();
311
312 #if (!defined COMPILER_MSVC && defined PLATFORM_WINDOWS)
313         if (pStdOut) {
314                 fclose (pStdOut);
315         }
316         if (pStdErr) {
317                 fclose (pStdErr);
318         }
319
320         if (bConsole) {
321                 // Detach and free the console from our application
322                 INPUT_RECORD input_record;
323
324                 input_record.EventType = KEY_EVENT;
325                 input_record.Event.KeyEvent.bKeyDown = TRUE;
326                 input_record.Event.KeyEvent.dwControlKeyState = 0;
327                 input_record.Event.KeyEvent.uChar.UnicodeChar = VK_RETURN;
328                 input_record.Event.KeyEvent.wRepeatCount      = 1;
329                 input_record.Event.KeyEvent.wVirtualKeyCode   = VK_RETURN;
330                 input_record.Event.KeyEvent.wVirtualScanCode  = MapVirtualKey( VK_RETURN, 0 );
331
332                 DWORD written = 0;
333                 WriteConsoleInput( GetStdHandle( STD_INPUT_HANDLE ), &input_record, 1, &written );
334
335                 FreeConsole();
336         }
337 #endif
338
339         return 0;
340 }
341 #if (defined WINDOWS_VST_SUPPORT && !defined PLATFORM_WINDOWS)
342 } // end of extern "C" block
343 #endif