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