2 * Copyright (C) 2005-2006 Taybin Rutkin <taybin@taybin.com>
3 * Copyright (C) 2005-2007 Doug McLain <doug@nostar.net>
4 * Copyright (C) 2005-2019 Paul Davis <paul@linuxaudiosystems.com>
5 * Copyright (C) 2006-2011 David Robillard <d@drobilla.net>
6 * Copyright (C) 2006-2015 Tim Mayberry <mojofunk@gmail.com>
7 * Copyright (C) 2006 Nick Mainsbridge <mainsbridge@gmail.com>
8 * Copyright (C) 2007-2012 Carl Hetherington <carl@carlh.net>
9 * Copyright (C) 2008-2009 Hans Baier <hansfbaier@googlemail.com>
10 * Copyright (C) 2013-2017 John Emmas <john@creativepost.co.uk>
11 * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
35 #include <sigc++/bind.h>
36 #include <gtkmm/settings.h>
38 #include <curl/curl.h>
40 #include "pbd/error.h"
41 #include "pbd/file_utils.h"
42 #include "pbd/textreceiver.h"
43 #include "pbd/failed_constructor.h"
44 #include "pbd/pathexpand.h"
45 #include "pbd/pthread_utils.h"
46 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
47 #include "pbd/boost_debug.h"
50 #include "ardour/revision.h"
51 #include "ardour/ardour.h"
52 #include "ardour/audioengine.h"
53 #include "ardour/session_utils.h"
54 #include "ardour/filesystem_paths.h"
56 #include <gtkmm/main.h>
57 #include <gtkmm/stock.h>
59 #include <gtkmm2ext/application.h>
60 #include <gtkmm2ext/utils.h>
62 #include "ardour_ui.h"
63 #include "ui_config.h"
66 #include "bundle_env.h"
70 #ifdef PLATFORM_WINDOWS
71 #include <fcntl.h> // Needed for '_fmode'
72 #include <shellapi.h> // console
76 #include "gtk2ardour-version.h"
85 using namespace ARDOUR_COMMAND_LINE;
86 using namespace ARDOUR;
89 TextReceiver text_receiver (PROGRAM_NAME);
91 extern int curvetest (string);
93 static ARDOUR_UI *ui = 0;
94 static string localedir (LOCALEDIR);
99 MessageDialog win (string_compose (_("%1 could not connect to the audio backend."), PROGRAM_NAME),
104 win.add_button (Stock::QUIT, RESPONSE_CLOSE);
105 win.set_default_response (RESPONSE_CLOSE);
108 win.set_position (Gtk::WIN_POS_CENTER);
114 /* we just don't care about the result, but we want to block */
120 static void ardour_g_log (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data) {
122 case G_LOG_FLAG_FATAL:
123 case G_LOG_LEVEL_CRITICAL:
124 fatal << "g_log: " << message << endmsg;
126 case G_LOG_LEVEL_ERROR:
127 error << "g_log: " << message << endmsg;
129 case G_LOG_LEVEL_WARNING:
130 warning << "g_log: " << message << endmsg;
132 case G_LOG_LEVEL_MESSAGE:
133 case G_LOG_LEVEL_INFO:
135 info << "g_log: " << message << endmsg;
142 tell_about_backend_death (void* /* ignored */)
144 if (AudioEngine::instance()->processed_samples() == 0) {
145 /* died during startup */
146 MessageDialog msg (string_compose (_("The audio backend (%1) has failed, or terminated"), AudioEngine::instance()->current_backend_name()), false);
147 msg.set_position (Gtk::WIN_POS_CENTER);
148 msg.set_secondary_text (string_compose (_(
149 "%2 exited unexpectedly, and without notifying %1.\n\
151 This could be due to misconfiguration or to an error inside %2.\n\
153 Click OK to exit %1."), PROGRAM_NAME, AudioEngine::instance()->current_backend_name()));
156 _exit (EXIT_SUCCESS);
160 /* engine has already run, so this is a mid-session backend death */
162 MessageDialog msg (string_compose (_("The audio backend (%1) has failed, or terminated"), AudioEngine::instance()->current_backend_name()), false);
163 msg.set_secondary_text (string_compose (_("%2 exited unexpectedly, and without notifying %1."),
164 PROGRAM_NAME, AudioEngine::instance()->current_backend_name()));
167 return false; /* do not call again */
170 #ifndef PLATFORM_WINDOWS
172 sigpipe_handler (int /*signal*/)
174 /* XXX fix this so that we do this again after a reconnect to the backend */
176 static bool done_the_backend_thing = false;
178 if (!done_the_backend_thing) {
179 AudioEngine::instance()->died ();
180 g_idle_add (tell_about_backend_death, 0);
181 done_the_backend_thing = true;
186 #if (!defined COMPILER_MSVC && defined PLATFORM_WINDOWS)
188 static FILE* pStdOut = 0;
189 static FILE* pStdErr = 0;
190 static BOOL bConsole;
191 static HANDLE hStdOut;
194 IsAConsolePort (HANDLE handle)
197 return (GetConsoleMode(handle, &mode) != 0);
201 console_madness_begin ()
203 bConsole = AttachConsole(ATTACH_PARENT_PROCESS);
204 hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
206 /* re-attach to the console so we can see 'printf()' output etc.
207 * for MSVC see gtk2_ardour/msvc/winmain.cc
210 if ((bConsole) && (IsAConsolePort(hStdOut))) {
211 pStdOut = freopen( "CONOUT$", "w", stdout );
212 pStdErr = freopen( "CONOUT$", "w", stderr );
217 console_madness_end ()
227 // Detach and free the console from our application
228 INPUT_RECORD input_record;
230 input_record.EventType = KEY_EVENT;
231 input_record.Event.KeyEvent.bKeyDown = TRUE;
232 input_record.Event.KeyEvent.dwControlKeyState = 0;
233 input_record.Event.KeyEvent.uChar.UnicodeChar = VK_RETURN;
234 input_record.Event.KeyEvent.wRepeatCount = 1;
235 input_record.Event.KeyEvent.wVirtualKeyCode = VK_RETURN;
236 input_record.Event.KeyEvent.wVirtualScanCode = MapVirtualKey( VK_RETURN, 0 );
239 WriteConsoleInput( GetStdHandle( STD_INPUT_HANDLE ), &input_record, 1, &written );
245 static void command_line_parse_error (int *argc, char** argv[]) {}
247 #elif (defined(COMPILER_MSVC) && defined(NDEBUG) && !defined(RDC_BUILD))
249 // these are not used here. for MSVC see gtk2_ardour/msvc/winmain.cc
250 static void console_madness_begin () {}
251 static void console_madness_end () {}
253 static void command_line_parse_error (int *argc, char** argv[]) {
254 // Since we don't ordinarily have access to stdout and stderr with
255 // an MSVC app, let the user know we encountered a parsing error.
256 Gtk::Main app(argc, argv); // Calls 'gtk_init()'
258 Gtk::MessageDialog dlgReportParseError (string_compose (_("\n %1 could not understand your command line "), PROGRAM_NAME),
259 false, MESSAGE_ERROR, BUTTONS_CLOSE, true);
260 dlgReportParseError.set_title (string_compose (_("An error was encountered while launching %1"), PROGRAM_NAME));
261 dlgReportParseError.run ();
265 static void console_madness_begin () {}
266 static void console_madness_end () {}
267 static void command_line_parse_error (int *argc, char** argv[]) {}
270 #if (defined(COMPILER_MSVC) && defined(NDEBUG) && !defined(RDC_BUILD))
272 * Release build with MSVC uses ardour_main()
274 int ardour_main (int argc, char *argv[])
276 #elif (defined WINDOWS_VST_SUPPORT && !defined PLATFORM_WINDOWS)
278 // prototype for function in windows_vst_plugin_ui.cc
279 extern int windows_vst_gui_init (int* argc, char** argv[]);
281 /* this is called from the entry point of a wine-compiled
282 executable that is linked against gtk2_ardour built
287 int ardour_main (int argc, char *argv[])
290 int nomain (int argc, char *argv[])
292 int main (int argc, char *argv[])
295 ARDOUR::check_for_old_configuration_files();
297 /* global init is not thread safe.*/
298 if (curl_global_init (CURL_GLOBAL_DEFAULT)) {
299 cerr << "curl_global_init() failed. The web is gone. We're all doomed." << endl;
302 fixup_bundle_environment (argc, argv, localedir);
304 load_custom_fonts(); /* needs to happen before any gtk and pango init calls */
306 if (!Glib::thread_supported()) {
315 /* initialize C locale to user preference */
316 if (ARDOUR::translations_are_enabled ()) {
317 setlocale (LC_ALL, "");
321 console_madness_begin();
323 #if (defined WINDOWS_VST_SUPPORT && !defined PLATFORM_WINDOWS)
324 /* this does some magic that is needed to make GTK and X11 client interact properly.
325 * the platform dependent code is in windows_vst_plugin_ui.cc
327 windows_vst_gui_init (&argc, &argv);
331 cerr << "bind txt domain [" << PACKAGE << "] to " << localedir << endl;
333 (void) bindtextdomain (PACKAGE, localedir.c_str());
334 /* our i18n translations are all in UTF-8, so make sure
335 that even if the user locale doesn't specify UTF-8,
336 we use that when handling them.
338 (void) bind_textdomain_codeset (PACKAGE,"UTF-8");
341 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
343 // catch error message system signals ();
345 text_receiver.listen_to (error);
346 text_receiver.listen_to (info);
347 text_receiver.listen_to (fatal);
348 text_receiver.listen_to (warning);
350 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
351 if (g_getenv ("BOOST_DEBUG")) {
352 boost_debug_shared_ptr_show_live_debugging (true);
356 if (parse_opts (argc, argv)) {
357 command_line_parse_error (&argc, &argv);
363 << _(" (built using ")
366 << _(" and GCC version ") << __VERSION__
376 cerr << _("Copyright (C) 1999-2019 Paul Davis") << endl
377 << _("Some portions Copyright (C) Steve Harris, Ari Johnson, Brett Viren, Joel Baker, Robin Gareus") << endl
379 << string_compose (_("%1 comes with ABSOLUTELY NO WARRANTY"), PROGRAM_NAME) << endl
380 << _("not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.") << endl
381 << _("This is free software, and you are welcome to redistribute it ") << endl
382 << _("under certain conditions; see the source for copying conditions.")
386 if (!ARDOUR::init (ARDOUR_COMMAND_LINE::use_vst, ARDOUR_COMMAND_LINE::try_hw_optimization, localedir.c_str())) {
387 error << string_compose (_("could not initialize %1."), PROGRAM_NAME) << endmsg;
388 Gtk::Main main (argc, argv);
389 Gtk::MessageDialog msg (string_compose (_("Could not initialize %1 (likely due to corrupt config files).\n"
390 "Run %1 from a commandline for more information."), PROGRAM_NAME),
391 false, Gtk::MESSAGE_ERROR , Gtk::BUTTONS_OK, true);
396 #ifndef PLATFORM_WINDOWS
397 if (::signal (SIGPIPE, sigpipe_handler)) {
398 cerr << _("Cannot xinstall SIGPIPE error handler") << endl;
402 DEBUG_TRACE (DEBUG::Locale, string_compose ("main() locale '%1'\n", setlocale (LC_NUMERIC, NULL)));
404 if (UIConfiguration::instance().pre_gui_init ()) {
405 error << _("Could not complete pre-GUI initialization") << endmsg;
410 ui = new ARDOUR_UI (&argc, &argv, localedir.c_str());
411 } catch (failed_constructor& err) {
412 error << string_compose (_("could not create %1 GUI"), PROGRAM_NAME) << endmsg;
417 g_log_set_handler (NULL,
418 GLogLevelFlags (G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL | G_LOG_LEVEL_CRITICAL | G_LOG_FLAG_RECURSION),
419 &ardour_g_log, NULL);
422 ui->run (text_receiver);
423 Gtkmm2ext::Application::instance()->cleanup();
429 if (getenv ("ARDOUR_RUNNING_UNDER_VALGRIND")) {
430 Glib::usleep(100000);
435 pthread_cancel_all ();
438 if (getenv ("ARDOUR_RUNNING_UNDER_VALGRIND")) {
439 Glib::usleep(100000);
444 console_madness_end ();
448 #if (defined WINDOWS_VST_SUPPORT && !defined PLATFORM_WINDOWS)
449 } // end of extern "C" block