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