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