add luadoc binary (developer tool) to dump lua bindings
[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 <vector>
24
25 #include <sigc++/bind.h>
26 #include <gtkmm/settings.h>
27
28 #ifdef HAVE_FFTW35F
29 #include <fftw3.h>
30 #endif
31
32 #include "pbd/error.h"
33 #include "pbd/file_utils.h"
34 #include "pbd/textreceiver.h"
35 #include "pbd/failed_constructor.h"
36 #include "pbd/pathexpand.h"
37 #include "pbd/pthread_utils.h"
38 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
39 #include "pbd/boost_debug.h"
40 #endif
41
42 #include "ardour/revision.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 "ardour_ui.h"
54 #include "ui_config.h"
55 #include "opts.h"
56 #include "enums.h"
57 #include "bundle_env.h"
58
59 #include "i18n.h"
60
61 #ifdef PLATFORM_WINDOWS
62 #include <fcntl.h> // Needed for '_fmode'
63 #include <shellapi.h> // console
64 #endif
65
66 #ifdef WAF_BUILD
67 #include "gtk2ardour-version.h"
68 #endif
69
70 using namespace std;
71 using namespace Gtk;
72 using namespace ARDOUR_COMMAND_LINE;
73 using namespace ARDOUR;
74 using namespace PBD;
75
76 TextReceiver text_receiver ("ardour");
77
78 extern int curvetest (string);
79
80 static ARDOUR_UI  *ui = 0;
81 static string localedir (LOCALEDIR);
82
83 void
84 gui_jack_error ()
85 {
86         MessageDialog win (string_compose (_("%1 could not connect to the audio backend."), PROGRAM_NAME),
87                            false,
88                            Gtk::MESSAGE_INFO,
89                            Gtk::BUTTONS_NONE);
90
91         win.add_button (Stock::QUIT, RESPONSE_CLOSE);
92         win.set_default_response (RESPONSE_CLOSE);
93
94         win.show_all ();
95         win.set_position (Gtk::WIN_POS_CENTER);
96
97         if (!no_splash) {
98                 ui->hide_splash ();
99         }
100
101         /* we just don't care about the result, but we want to block */
102
103         win.run ();
104 }
105
106 #ifndef NDEBUG
107 static void ardour_g_log (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data) {
108         switch (log_level) {
109                 case G_LOG_FLAG_FATAL:
110                 case G_LOG_LEVEL_CRITICAL:
111                         fatal << "g_log: " << message << endmsg;
112                         break;
113                 case G_LOG_LEVEL_ERROR:
114                         error << "g_log: " << message << endmsg;
115                         break;
116                 case G_LOG_LEVEL_WARNING:
117                         warning << "g_log: " << message << endmsg;
118                         break;
119                 case G_LOG_LEVEL_MESSAGE:
120                 case G_LOG_LEVEL_INFO:
121                 default:
122                         info << "g_log: " << message << endmsg;
123                         break;
124         }
125 }
126 #endif
127
128 static gboolean
129 tell_about_backend_death (void* /* ignored */)
130 {
131         if (AudioEngine::instance()->processed_frames() == 0) {
132                 /* died during startup */
133                 MessageDialog msg (string_compose (_("The audio backend (%1) has failed, or terminated"), AudioEngine::instance()->current_backend_name()), false);
134                 msg.set_position (Gtk::WIN_POS_CENTER);
135                 msg.set_secondary_text (string_compose (_(
136 "%2 exited unexpectedly, and without notifying %1.\n\
137 \n\
138 This could be due to misconfiguration or to an error inside %2.\n\
139 \n\
140 Click OK to exit %1."), PROGRAM_NAME, AudioEngine::instance()->current_backend_name()));
141
142                 msg.run ();
143                 _exit (0);
144
145         } else {
146
147                 /* engine has already run, so this is a mid-session backend death */
148
149                 MessageDialog msg (string_compose (_("The audio backend (%1) has failed, or terminated"), AudioEngine::instance()->current_backend_name()), false);
150                 msg.set_secondary_text (string_compose (_("%2 exited unexpectedly, and without notifying %1."),
151                                                          PROGRAM_NAME, AudioEngine::instance()->current_backend_name()));
152                 msg.present ();
153         }
154         return false; /* do not call again */
155 }
156
157 #ifndef PLATFORM_WINDOWS
158 static void
159 sigpipe_handler (int /*signal*/)
160 {
161         /* XXX fix this so that we do this again after a reconnect to the backend */
162
163         static bool done_the_backend_thing = false;
164
165         if (!done_the_backend_thing) {
166                 AudioEngine::instance()->died ();
167                 g_idle_add (tell_about_backend_death, 0);
168                 done_the_backend_thing =  true;
169         }
170 }
171 #endif
172
173 #if (!defined COMPILER_MSVC && defined PLATFORM_WINDOWS)
174
175 static FILE* pStdOut = 0;
176 static FILE* pStdErr = 0;
177 static BOOL  bConsole;
178 static HANDLE hStdOut;
179
180 static bool
181 IsAConsolePort (HANDLE handle)
182 {
183         DWORD mode;
184         return (GetConsoleMode(handle, &mode) != 0);
185 }
186
187 static void
188 console_madness_begin ()
189 {
190         bConsole = AttachConsole(ATTACH_PARENT_PROCESS);
191         hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
192
193         /* re-attach to the console so we can see 'printf()' output etc.
194          * for MSVC see  gtk2_ardour/msvc/winmain.cc
195          */
196
197         if ((bConsole) && (IsAConsolePort(hStdOut))) {
198                 pStdOut = freopen( "CONOUT$", "w", stdout );
199                 pStdErr = freopen( "CONOUT$", "w", stderr );
200         }
201 }
202
203 static void
204 console_madness_end ()
205 {
206         if (pStdOut) {
207                 fclose (pStdOut);
208         }
209         if (pStdErr) {
210                 fclose (pStdErr);
211         }
212
213         if (bConsole) {
214                 // Detach and free the console from our application
215                 INPUT_RECORD input_record;
216
217                 input_record.EventType = KEY_EVENT;
218                 input_record.Event.KeyEvent.bKeyDown = TRUE;
219                 input_record.Event.KeyEvent.dwControlKeyState = 0;
220                 input_record.Event.KeyEvent.uChar.UnicodeChar = VK_RETURN;
221                 input_record.Event.KeyEvent.wRepeatCount      = 1;
222                 input_record.Event.KeyEvent.wVirtualKeyCode   = VK_RETURN;
223                 input_record.Event.KeyEvent.wVirtualScanCode  = MapVirtualKey( VK_RETURN, 0 );
224
225                 DWORD written = 0;
226                 WriteConsoleInput( GetStdHandle( STD_INPUT_HANDLE ), &input_record, 1, &written );
227
228                 FreeConsole();
229         }
230 }
231
232 static void command_line_parse_error (int *argc, char** argv[]) {}
233
234 #elif (defined(COMPILER_MSVC) && defined(NDEBUG) && !defined(RDC_BUILD))
235
236 // these are not used here. for MSVC see  gtk2_ardour/msvc/winmain.cc
237 static void console_madness_begin () {}
238 static void console_madness_end () {}
239
240 static void command_line_parse_error (int *argc, char** argv[]) {
241         // Since we don't ordinarily have access to stdout and stderr with
242         // an MSVC app, let the user know we encountered a parsing error.
243         Gtk::Main app(argc, argv); // Calls 'gtk_init()'
244
245         Gtk::MessageDialog dlgReportParseError (string_compose (_("\n   %1 could not understand your command line      "), PROGRAM_NAME),
246                         false, MESSAGE_ERROR, BUTTONS_CLOSE, true);
247         dlgReportParseError.set_title (string_compose (_("An error was encountered while launching %1"), PROGRAM_NAME));
248         dlgReportParseError.run ();
249 }
250
251 #else
252 static void console_madness_begin () {}
253 static void console_madness_end () {}
254 static void command_line_parse_error (int *argc, char** argv[]) {}
255 #endif
256
257 #if (defined(COMPILER_MSVC) && defined(NDEBUG) && !defined(RDC_BUILD))
258 /*
259  *  Release build with MSVC uses ardour_main()
260  */
261 int ardour_main (int argc, char *argv[])
262
263 #elif (defined WINDOWS_VST_SUPPORT && !defined PLATFORM_WINDOWS)
264
265 // prototype for function in windows_vst_plugin_ui.cc
266 extern int windows_vst_gui_init (int* argc, char** argv[]);
267
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
274 int ardour_main (int argc, char *argv[])
275
276 #elif defined NOMAIN
277 int nomain (int argc, char *argv[])
278 #else
279 int main (int argc, char *argv[])
280 #endif
281 {
282         ARDOUR::check_for_old_configuration_files();
283
284         fixup_bundle_environment (argc, argv, localedir);
285
286         load_custom_fonts(); /* needs to happen before any gtk and pango init calls */
287
288         if (!Glib::thread_supported()) {
289                 Glib::thread_init();
290         }
291
292 #ifdef HAVE_FFTW35F
293         fftwf_make_planner_thread_safe ();
294 #endif
295
296 #ifdef ENABLE_NLS
297         gtk_set_locale ();
298 #endif
299
300         console_madness_begin();
301
302 #if (defined WINDOWS_VST_SUPPORT && !defined PLATFORM_WINDOWS)
303         /* this does some magic that is needed to make GTK and X11 client interact properly.
304          * the platform dependent code is in windows_vst_plugin_ui.cc
305          */
306         windows_vst_gui_init (&argc, &argv);
307 #endif
308
309 #ifdef ENABLE_NLS
310         cerr << "bind txt domain [" << PACKAGE << "] to " << localedir << endl;
311
312         (void) bindtextdomain (PACKAGE, localedir.c_str());
313         /* our i18n translations are all in UTF-8, so make sure
314            that even if the user locale doesn't specify UTF-8,
315            we use that when handling them.
316         */
317         (void) bind_textdomain_codeset (PACKAGE,"UTF-8");
318 #endif
319
320         pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
321
322         // catch error message system signals ();
323
324         text_receiver.listen_to (error);
325         text_receiver.listen_to (info);
326         text_receiver.listen_to (fatal);
327         text_receiver.listen_to (warning);
328
329 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
330         if (g_getenv ("BOOST_DEBUG")) {
331                 boost_debug_shared_ptr_show_live_debugging (true);
332         }
333 #endif
334
335         if (parse_opts (argc, argv)) {
336                 command_line_parse_error (&argc, &argv);
337                 exit (1);
338         }
339
340         cout << PROGRAM_NAME
341              << VERSIONSTRING
342              << _(" (built using ")
343              << revision
344 #ifdef __GNUC__
345              << _(" and GCC version ") << __VERSION__
346 #endif
347              << ')'
348              << endl;
349
350         if (just_version) {
351                 exit (0);
352         }
353
354         if (no_splash) {
355                 cerr << _("Copyright (C) 1999-2015 Paul Davis") << endl
356                      << _("Some portions Copyright (C) Steve Harris, Ari Johnson, Brett Viren, Joel Baker, Robin Gareus") << endl
357                      << endl
358                      << string_compose (_("%1 comes with ABSOLUTELY NO WARRANTY"), PROGRAM_NAME) << endl
359                      << _("not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.") << endl
360                      << _("This is free software, and you are welcome to redistribute it ") << endl
361                      << _("under certain conditions; see the source for copying conditions.")
362                      << endl;
363         }
364
365         if (!ARDOUR::init (ARDOUR_COMMAND_LINE::use_vst, ARDOUR_COMMAND_LINE::try_hw_optimization, localedir.c_str())) {
366                 error << string_compose (_("could not initialize %1."), PROGRAM_NAME) << endmsg;
367                 exit (1);
368         }
369
370         if (curvetest_file) {
371                 return curvetest (curvetest_file);
372         }
373
374 #ifndef PLATFORM_WINDOWS
375         if (::signal (SIGPIPE, sigpipe_handler)) {
376                 cerr << _("Cannot xinstall SIGPIPE error handler") << endl;
377         }
378 #endif
379
380         if (UIConfiguration::instance().pre_gui_init ()) {
381                 error << _("Could not complete pre-GUI initialization") << endmsg;
382                 exit (1);
383         }
384
385         try {
386                 ui = new ARDOUR_UI (&argc, &argv, localedir.c_str());
387         } catch (failed_constructor& err) {
388                 error << string_compose (_("could not create %1 GUI"), PROGRAM_NAME) << endmsg;
389                 exit (1);
390         }
391
392 #ifndef NDEBUG
393         g_log_set_handler (NULL,
394                         GLogLevelFlags (G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL |  G_LOG_LEVEL_CRITICAL | G_LOG_FLAG_RECURSION),
395                         &ardour_g_log, NULL);
396 #endif
397
398         ui->run (text_receiver);
399         Gtkmm2ext::Application::instance()->cleanup();
400         delete ui;
401         ui = 0;
402
403         ARDOUR::cleanup ();
404         pthread_cancel_all ();
405
406         console_madness_end ();
407
408         return 0;
409 }
410 #if (defined WINDOWS_VST_SUPPORT && !defined PLATFORM_WINDOWS)
411 } // end of extern "C" block
412 #endif