narrow-mixer strip tweak:
[ardour.git] / gtk2_ardour / ardour_ui.cc
1 /*
2     Copyright (C) 1999-2007 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 #ifdef WAF_BUILD
21 #include "gtk2ardour-config.h"
22 #endif
23
24 #include <stdint.h>
25
26 #include <algorithm>
27 #include <cmath>
28 #include <fcntl.h>
29 #include <signal.h>
30 #include <unistd.h>
31 #include <time.h>
32 #include <cerrno>
33 #include <fstream>
34
35 #include <iostream>
36
37 #include <sys/resource.h>
38 #include <sys/types.h>
39 #include <sys/sysctl.h>
40
41 #include <gtkmm/messagedialog.h>
42 #include <gtkmm/accelmap.h>
43
44 #include "pbd/error.h"
45 #include "pbd/basename.h"
46 #include "pbd/compose.h"
47 #include "pbd/failed_constructor.h"
48 #include "pbd/enumwriter.h"
49 #include "pbd/memento_command.h"
50 #include "pbd/openuri.h"
51 #include "pbd/file_utils.h"
52
53 #include "gtkmm2ext/application.h"
54 #include "gtkmm2ext/bindings.h"
55 #include "gtkmm2ext/gtk_ui.h"
56 #include "gtkmm2ext/utils.h"
57 #include "gtkmm2ext/click_box.h"
58 #include "gtkmm2ext/fastmeter.h"
59 #include "gtkmm2ext/popup.h"
60 #include "gtkmm2ext/window_title.h"
61
62 #include "midi++/manager.h"
63
64 #include "ardour/ardour.h"
65 #include "ardour/audioengine.h"
66 #include "ardour/audiofilesource.h"
67 #include "ardour/automation_watch.h"
68 #include "ardour/diskstream.h"
69 #include "ardour/filename_extensions.h"
70 #include "ardour/port.h"
71 #include "ardour/process_thread.h"
72 #include "ardour/profile.h"
73 #include "ardour/recent_sessions.h"
74 #include "ardour/session_directory.h"
75 #include "ardour/session_route.h"
76 #include "ardour/session_state_utils.h"
77 #include "ardour/session_utils.h"
78 #include "ardour/slave.h"
79
80 #include "timecode/time.h"
81
82 typedef uint64_t microseconds_t;
83
84 #include "about.h"
85 #include "actions.h"
86 #include "add_route_dialog.h"
87 #include "ambiguous_file_dialog.h"
88 #include "ardour_ui.h"
89 #include "audio_clock.h"
90 #include "bundle_manager.h"
91 #include "engine_dialog.h"
92 #include "gain_meter.h"
93 #include "global_port_matrix.h"
94 #include "gui_object.h"
95 #include "gui_thread.h"
96 #include "keyboard.h"
97 #include "location_ui.h"
98 #include "main_clock.h"
99 #include "missing_file_dialog.h"
100 #include "missing_plugin_dialog.h"
101 #include "mixer_ui.h"
102 #include "mouse_cursors.h"
103 #include "opts.h"
104 #include "processor_box.h"
105 #include "prompter.h"
106 #include "public_editor.h"
107 #include "route_time_axis.h"
108 #include "session_metadata_dialog.h"
109 #include "shuttle_control.h"
110 #include "speaker_dialog.h"
111 #include "splash.h"
112 #include "startup.h"
113 #include "theme_manager.h"
114 #include "time_axis_view_item.h"
115 #include "utils.h"
116 #include "window_proxy.h"
117
118 #include "i18n.h"
119
120 using namespace ARDOUR;
121 using namespace PBD;
122 using namespace Gtkmm2ext;
123 using namespace Gtk;
124 using namespace std;
125
126 ARDOUR_UI *ARDOUR_UI::theArdourUI = 0;
127 UIConfiguration *ARDOUR_UI::ui_config = 0;
128
129 sigc::signal<void,bool> ARDOUR_UI::Blink;
130 sigc::signal<void>      ARDOUR_UI::RapidScreenUpdate;
131 sigc::signal<void>      ARDOUR_UI::SuperRapidScreenUpdate;
132 sigc::signal<void, framepos_t, bool, framepos_t> ARDOUR_UI::Clock;
133
134 ARDOUR_UI::ARDOUR_UI (int *argcp, char **argvp[])
135
136         : Gtkmm2ext::UI (PROGRAM_NAME, argcp, argvp)
137
138         , gui_object_state (new GUIObjectState)
139         , primary_clock (new MainClock (X_("primary"), false, X_("transport"), true, true, true, false, true))
140         , secondary_clock (new MainClock (X_("secondary"), false, X_("secondary"), true, true, false, false, true))
141
142           /* big clock */
143
144         , big_clock (new AudioClock (X_("bigclock"), false, "big", true, true, false, false))
145
146           /* transport */
147
148         , roll_controllable (new TransportControllable ("transport roll", *this, TransportControllable::Roll))
149         , stop_controllable (new TransportControllable ("transport stop", *this, TransportControllable::Stop))
150         , goto_start_controllable (new TransportControllable ("transport goto start", *this, TransportControllable::GotoStart))
151         , goto_end_controllable (new TransportControllable ("transport goto end", *this, TransportControllable::GotoEnd))
152         , auto_loop_controllable (new TransportControllable ("transport auto loop", *this, TransportControllable::AutoLoop))
153         , play_selection_controllable (new TransportControllable ("transport play selection", *this, TransportControllable::PlaySelection))
154         , rec_controllable (new TransportControllable ("transport rec-enable", *this, TransportControllable::RecordEnable))
155
156         , auto_return_button (ArdourButton::led_default_elements)
157         , follow_edits_button (ArdourButton::led_default_elements)
158         , auto_input_button (ArdourButton::led_default_elements)
159
160         , auditioning_alert_button (_("audition"))
161         , solo_alert_button (_("solo"))
162         , feedback_alert_button (_("feedback"))
163
164         , error_log_button (_("Errors"))
165
166         , _status_bar_visibility (X_("status-bar"))
167         , _feedback_exists (false)
168
169 {
170         Gtkmm2ext::init();
171
172         about = 0;
173         splash = 0;
174         _startup = 0;
175
176         if (theArdourUI == 0) {
177                 theArdourUI = this;
178         }
179
180         ui_config = new UIConfiguration();
181         theme_manager = new ThemeManager();
182
183         key_editor = 0;
184
185         editor = 0;
186         mixer = 0;
187         editor = 0;
188         engine = 0;
189         _session_is_new = false;
190         big_clock_window = 0;
191         big_clock_height = 0;
192         big_clock_resize_in_progress = false;
193         session_selector_window = 0;
194         last_key_press_time = 0;
195         add_route_dialog = 0;
196         route_params = 0;
197         bundle_manager = 0;
198         rc_option_editor = 0;
199         session_option_editor = 0;
200         location_ui = 0;
201         open_session_selector = 0;
202         have_configure_timeout = false;
203         have_disk_speed_dialog_displayed = false;
204         session_loaded = false;
205         ignore_dual_punch = false;
206         original_big_clock_width = -1;
207         original_big_clock_height = -1;
208         original_big_clock_font_size = 0;
209
210         roll_button.set_controllable (roll_controllable);
211         stop_button.set_controllable (stop_controllable);
212         goto_start_button.set_controllable (goto_start_controllable);
213         goto_end_button.set_controllable (goto_end_controllable);
214         auto_loop_button.set_controllable (auto_loop_controllable);
215         play_selection_button.set_controllable (play_selection_controllable);
216         rec_button.set_controllable (rec_controllable);
217
218         roll_button.set_name ("transport button");
219         stop_button.set_name ("transport button");
220         goto_start_button.set_name ("transport button");
221         goto_end_button.set_name ("transport button");
222         auto_loop_button.set_name ("transport button");
223         play_selection_button.set_name ("transport button");
224         rec_button.set_name ("transport recenable button");
225         midi_panic_button.set_name ("transport button");
226
227         goto_start_button.set_tweaks (ArdourButton::ShowClick);
228         goto_end_button.set_tweaks (ArdourButton::ShowClick);
229         midi_panic_button.set_tweaks (ArdourButton::ShowClick);
230         
231         last_configure_time= 0;
232         last_peak_grab = 0;
233
234         ARDOUR::Diskstream::DiskOverrun.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::disk_overrun_handler, this), gui_context());
235         ARDOUR::Diskstream::DiskUnderrun.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::disk_underrun_handler, this), gui_context());
236
237         /* handle dialog requests */
238
239         ARDOUR::Session::Dialog.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::session_dialog, this, _1), gui_context());
240
241         /* handle pending state with a dialog (PROBLEM: needs to return a value and thus cannot be x-thread) */
242
243         ARDOUR::Session::AskAboutPendingState.connect_same_thread (forever_connections, boost::bind (&ARDOUR_UI::pending_state_dialog, this));
244
245         /* handle sr mismatch with a dialog (PROBLEM: needs to return a value and thus cannot be x-thread) */
246
247         ARDOUR::Session::AskAboutSampleRateMismatch.connect_same_thread (forever_connections, boost::bind (&ARDOUR_UI::sr_mismatch_dialog, this, _1, _2));
248
249         /* handle requests to quit (coming from JACK session) */
250
251         ARDOUR::Session::Quit.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::finish, this), gui_context ());
252
253         /* tell the user about feedback */
254
255         ARDOUR::Session::FeedbackDetected.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::feedback_detected, this), gui_context ());
256         ARDOUR::Session::SuccessfulGraphSort.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::successful_graph_sort, this), gui_context ());
257
258         /* handle requests to deal with missing files */
259
260         ARDOUR::Session::MissingFile.connect_same_thread (forever_connections, boost::bind (&ARDOUR_UI::missing_file, this, _1, _2, _3));
261
262         /* and ambiguous files */
263
264         ARDOUR::FileSource::AmbiguousFileName.connect_same_thread (forever_connections, boost::bind (&ARDOUR_UI::ambiguous_file, this, _1, _2, _3));
265
266         /* lets get this party started */
267
268         try {
269                 if (ARDOUR::init (ARDOUR_COMMAND_LINE::use_vst, ARDOUR_COMMAND_LINE::try_hw_optimization)) {
270                         throw failed_constructor ();
271                 }
272
273                 setup_gtk_ardour_enums ();
274                 setup_profile ();
275
276                 SessionEvent::create_per_thread_pool ("GUI", 512);
277
278         } catch (failed_constructor& err) {
279                 error << string_compose (_("could not initialize %1."), PROGRAM_NAME) << endmsg;
280                 // pass it on up
281                 throw;
282         }
283
284         /* we like keyboards */
285
286         keyboard = new ArdourKeyboard(*this);
287
288         XMLNode* node = ARDOUR_UI::instance()->keyboard_settings();
289         if (node) {
290                 keyboard->set_state (*node, Stateful::loading_state_version);
291         }
292
293         /* we don't like certain modifiers */
294         Bindings::set_ignored_state (GDK_LOCK_MASK|GDK_MOD2_MASK|GDK_MOD3_MASK);
295
296         reset_dpi();
297
298         TimeAxisViewItem::set_constant_heights ();
299
300         /* The following must happen after ARDOUR::init() so that Config is set up */
301
302         location_ui = new ActionWindowProxy<LocationUIWindow> (X_("locations"), Config->extra_xml (X_("UI")), X_("ToggleLocations"));
303         big_clock_window = new ActionWindowProxy<Gtk::Window> (X_("bigclock"), Config->extra_xml (X_("UI")), X_("ToggleBigClock"));
304         speaker_config_window = new ActionWindowProxy<SpeakerDialog> (X_("speakerconf"), Config->extra_xml (X_("UI")), X_("toggle-speaker-config"));
305
306         for (ARDOUR::DataType::iterator i = ARDOUR::DataType::begin(); i != ARDOUR::DataType::end(); ++i) {
307                 _global_port_matrix[*i] = new ActionWindowProxy<GlobalPortMatrixWindow> (
308                         string_compose ("GlobalPortMatrix-%1", (*i).to_string()),
309                         Config->extra_xml (X_("UI")),
310                         string_compose ("toggle-%1-connection-manager", (*i).to_string())
311                         );
312         }
313
314         setup_clock ();
315
316         SpeakerDialog* s = new SpeakerDialog ();
317         s->signal_unmap().connect (sigc::bind (sigc::ptr_fun (&ActionManager::uncheck_toggleaction), X_("<Actions>/Common/toggle-speaker-config")));
318         speaker_config_window->set (s);
319
320         starting.connect (sigc::mem_fun(*this, &ARDOUR_UI::startup));
321         stopping.connect (sigc::mem_fun(*this, &ARDOUR_UI::shutdown));
322
323         _process_thread = new ProcessThread ();
324         _process_thread->init ();
325
326         DPIReset.connect (sigc::mem_fun (*this, &ARDOUR_UI::resize_text_widgets));
327 }
328
329
330 int
331 ARDOUR_UI::create_engine ()
332 {
333         // this gets called every time by new_session()
334
335         if (engine) {
336                 return 0;
337         }
338
339         loading_message (_("Starting audio engine"));
340
341         try {
342                 engine = new ARDOUR::AudioEngine (ARDOUR_COMMAND_LINE::jack_client_name, ARDOUR_COMMAND_LINE::jack_session_uuid);
343
344         } catch (...) {
345
346                 return -1;
347         }
348
349         engine->Stopped.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::engine_stopped, this), gui_context());
350         engine->Running.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::engine_running, this), gui_context());
351         engine->SampleRateChanged.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::update_sample_rate, this, _1), gui_context());
352
353         engine->Halted.connect_same_thread (forever_connections, boost::bind (&ARDOUR_UI::engine_halted, this, _1, false));
354
355         ARDOUR::Port::set_connecting_blocked (ARDOUR_COMMAND_LINE::no_connect_ports);
356
357         post_engine ();
358
359         return 0;
360 }
361
362 void
363 ARDOUR_UI::post_engine ()
364 {
365         /* Things to be done once we create the AudioEngine
366          */
367
368         ARDOUR::init_post_engine ();
369
370         /* load up the UI manager */
371
372         ActionManager::init ();
373
374         _tooltips.enable();
375
376         if (setup_windows ()) {
377                 throw failed_constructor ();
378         }
379
380         /* Do this after setup_windows (), as that's when the _status_bar_visibility is created */
381         XMLNode* n = Config->extra_xml (X_("UI"));
382         if (n) {
383                 _status_bar_visibility.set_state (*n);
384         }
385         
386         check_memory_locking();
387
388         /* this is the first point at which all the keybindings are available */
389
390         if (ARDOUR_COMMAND_LINE::show_key_actions) {
391                 vector<string> names;
392                 vector<string> paths;
393                 vector<string> tooltips;
394                 vector<string> keys;
395                 vector<AccelKey> bindings;
396
397                 ActionManager::get_all_actions (names, paths, tooltips, keys, bindings);
398
399                 vector<string>::iterator n;
400                 vector<string>::iterator k;
401                 for (n = names.begin(), k = keys.begin(); n != names.end(); ++n, ++k) {
402                         cout << "Action: " << (*n) << " bound to " << (*k) << endl;
403                 }
404
405                 exit (0);
406         }
407
408         blink_timeout_tag = -1;
409
410         /* this being a GUI and all, we want peakfiles */
411
412         AudioFileSource::set_build_peakfiles (true);
413         AudioFileSource::set_build_missing_peakfiles (true);
414
415         /* set default clock modes */
416
417         if (Profile->get_sae()) {
418                 primary_clock->set_mode (AudioClock::BBT);
419                 secondary_clock->set_mode (AudioClock::MinSec);
420         }  else {
421                 primary_clock->set_mode (AudioClock::Timecode);
422                 secondary_clock->set_mode (AudioClock::BBT);
423         }
424
425         /* start the time-of-day-clock */
426
427 #ifndef GTKOSX
428         /* OS X provides a nearly-always visible wallclock, so don't be stupid */
429         update_wall_clock ();
430         Glib::signal_timeout().connect_seconds (sigc::mem_fun(*this, &ARDOUR_UI::update_wall_clock), 1);
431 #endif
432
433         update_disk_space ();
434         update_cpu_load ();
435         update_sample_rate (engine->frame_rate());
436         update_timecode_format ();
437
438         Config->ParameterChanged.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::parameter_changed, this, _1), gui_context());
439         boost::function<void (string)> pc (boost::bind (&ARDOUR_UI::parameter_changed, this, _1));
440         Config->map_parameters (pc);
441
442         /* now start and maybe save state */
443
444         if (do_engine_start () == 0) {
445                 if (_session && _session_is_new) {
446                         /* we need to retain initial visual
447                            settings for a new session
448                         */
449                         _session->save_state ("");
450                 }
451         }
452 }
453
454 ARDOUR_UI::~ARDOUR_UI ()
455 {
456         delete keyboard;
457         delete editor;
458         delete mixer;
459         delete add_route_dialog;
460 }
461
462 void
463 ARDOUR_UI::pop_back_splash (Gtk::Window& win)
464 {
465         if (Splash::instance()) {
466                 Splash::instance()->pop_back_for (win);
467         }
468 }
469
470 gint
471 ARDOUR_UI::configure_timeout ()
472 {
473         if (last_configure_time == 0) {
474                 /* no configure events yet */
475                 return true;
476         }
477
478         /* force a gap of 0.5 seconds since the last configure event
479          */
480
481         if (get_microseconds() - last_configure_time < 500000) {
482                 return true;
483         } else {
484                 have_configure_timeout = false;
485                 save_ardour_state ();
486                 return false;
487         }
488 }
489
490 gboolean
491 ARDOUR_UI::configure_handler (GdkEventConfigure* /*conf*/)
492 {
493         if (have_configure_timeout) {
494                 last_configure_time = get_microseconds();
495         } else {
496                 Glib::signal_timeout().connect (sigc::mem_fun(*this, &ARDOUR_UI::configure_timeout), 100);
497                 have_configure_timeout = true;
498         }
499
500         return FALSE;
501 }
502
503 void
504 ARDOUR_UI::set_transport_controllable_state (const XMLNode& node)
505 {
506         const XMLProperty* prop;
507
508         if ((prop = node.property ("roll")) != 0) {
509                 roll_controllable->set_id (prop->value());
510         }
511         if ((prop = node.property ("stop")) != 0) {
512                 stop_controllable->set_id (prop->value());
513         }
514         if ((prop = node.property ("goto-start")) != 0) {
515                 goto_start_controllable->set_id (prop->value());
516         }
517         if ((prop = node.property ("goto-end")) != 0) {
518                 goto_end_controllable->set_id (prop->value());
519         }
520         if ((prop = node.property ("auto-loop")) != 0) {
521                 auto_loop_controllable->set_id (prop->value());
522         }
523         if ((prop = node.property ("play-selection")) != 0) {
524                 play_selection_controllable->set_id (prop->value());
525         }
526         if ((prop = node.property ("rec")) != 0) {
527                 rec_controllable->set_id (prop->value());
528         }
529         if ((prop = node.property ("shuttle")) != 0) {
530                 shuttle_box->controllable()->set_id (prop->value());
531         }
532 }
533
534 XMLNode&
535 ARDOUR_UI::get_transport_controllable_state ()
536 {
537         XMLNode* node = new XMLNode(X_("TransportControllables"));
538         char buf[64];
539
540         roll_controllable->id().print (buf, sizeof (buf));
541         node->add_property (X_("roll"), buf);
542         stop_controllable->id().print (buf, sizeof (buf));
543         node->add_property (X_("stop"), buf);
544         goto_start_controllable->id().print (buf, sizeof (buf));
545         node->add_property (X_("goto_start"), buf);
546         goto_end_controllable->id().print (buf, sizeof (buf));
547         node->add_property (X_("goto_end"), buf);
548         auto_loop_controllable->id().print (buf, sizeof (buf));
549         node->add_property (X_("auto_loop"), buf);
550         play_selection_controllable->id().print (buf, sizeof (buf));
551         node->add_property (X_("play_selection"), buf);
552         rec_controllable->id().print (buf, sizeof (buf));
553         node->add_property (X_("rec"), buf);
554         shuttle_box->controllable()->id().print (buf, sizeof (buf));
555         node->add_property (X_("shuttle"), buf);
556
557         return *node;
558 }
559
560
561 gint
562 ARDOUR_UI::autosave_session ()
563 {
564         if (g_main_depth() > 1) {
565                 /* inside a recursive main loop,
566                    give up because we may not be able to
567                    take a lock.
568                 */
569                 return 1;
570         }
571
572         if (!Config->get_periodic_safety_backups()) {
573                 return 1;
574         }
575
576         if (_session) {
577                 _session->maybe_write_autosave();
578         }
579
580         return 1;
581 }
582
583 void
584 ARDOUR_UI::update_autosave ()
585 {
586         ENSURE_GUI_THREAD (*this, &ARDOUR_UI::update_autosave)
587
588         if (_session && _session->dirty()) {
589                 if (_autosave_connection.connected()) {
590                         _autosave_connection.disconnect();
591                 }
592
593                 _autosave_connection = Glib::signal_timeout().connect (sigc::mem_fun (*this, &ARDOUR_UI::autosave_session),
594                                 Config->get_periodic_safety_backup_interval() * 1000);
595
596         } else {
597                 if (_autosave_connection.connected()) {
598                         _autosave_connection.disconnect();
599                 }
600         }
601 }
602
603 void
604 ARDOUR_UI::startup ()
605 {
606         Application* app = Application::instance ();
607
608         app->ShouldQuit.connect (sigc::mem_fun (*this, &ARDOUR_UI::queue_finish));
609         app->ShouldLoad.connect (sigc::mem_fun (*this, &ARDOUR_UI::idle_load));
610
611 #ifdef PHONE_HOME
612         call_the_mothership (VERSIONSTRING);
613 #endif
614
615         app->ready ();
616
617         if (get_session_parameters (true, ARDOUR_COMMAND_LINE::new_session, ARDOUR_COMMAND_LINE::load_template)) {
618                 exit (1);
619         }
620
621         use_config ();
622
623         goto_editor_window ();
624
625         /* Add the window proxies here; their addition may cause windows to be opened, and we want them
626            to be opened on top of the editor window that goto_editor_window() just opened.
627         */
628         add_window_proxy (location_ui);
629         add_window_proxy (big_clock_window);
630         for (ARDOUR::DataType::iterator i = ARDOUR::DataType::begin(); i != ARDOUR::DataType::end(); ++i) {
631                 add_window_proxy (_global_port_matrix[*i]);
632         }
633
634         /* We have to do this here since goto_editor_window() ends up calling show_all() on the
635          * editor window, and we may want stuff to be hidden.
636          */
637         _status_bar_visibility.update ();
638
639         BootMessage (string_compose (_("%1 is ready for use"), PROGRAM_NAME));
640 }
641
642 void
643 ARDOUR_UI::no_memory_warning ()
644 {
645         XMLNode node (X_("no-memory-warning"));
646         Config->add_instant_xml (node);
647 }
648
649 void
650 ARDOUR_UI::check_memory_locking ()
651 {
652 #ifdef __APPLE__
653         /* OS X doesn't support mlockall(2), and so testing for memory locking capability there is pointless */
654         return;
655 #else // !__APPLE__
656
657         XMLNode* memory_warning_node = Config->instant_xml (X_("no-memory-warning"));
658
659         if (engine->is_realtime() && memory_warning_node == 0) {
660
661                 struct rlimit limits;
662                 int64_t ram;
663                 long pages, page_size;
664 #ifdef __FreeBSD__
665                 size_t pages_len=sizeof(pages);
666                 if ((page_size = getpagesize()) < 0 ||
667                                 sysctlbyname("hw.availpages", &pages, &pages_len, NULL, 0))
668 #else
669                 if ((page_size = sysconf (_SC_PAGESIZE)) < 0 ||(pages = sysconf (_SC_PHYS_PAGES)) < 0)
670 #endif
671                 {
672                         ram = 0;
673                 } else {
674                         ram = (int64_t) pages * (int64_t) page_size;
675                 }
676
677                 if (getrlimit (RLIMIT_MEMLOCK, &limits)) {
678                         return;
679                 }
680
681                 if (limits.rlim_cur != RLIM_INFINITY) {
682
683                         if (ram == 0 || ((double) limits.rlim_cur / ram) < 0.75) {
684
685                                 MessageDialog msg (
686                                         string_compose (
687                                                 _("WARNING: Your system has a limit for maximum amount of locked memory. "
688                                                   "This might cause %1 to run out of memory before your system "
689                                                   "runs out of memory. \n\n"
690                                                   "You can view the memory limit with 'ulimit -l', "
691                                                   "and it is normally controlled by %2"),
692                                                 PROGRAM_NAME).c_str(), 
693 #ifdef __FreeBSD__
694                                         X_("/etc/login.conf")
695 #else
696                                         X_(" /etc/security/limits.conf")
697 #endif
698                                         );
699
700                                 msg.set_default_response (RESPONSE_OK);
701
702                                 VBox* vbox = msg.get_vbox();
703                                 HBox hbox;
704                                 CheckButton cb (_("Do not show this window again"));
705
706                                 cb.signal_toggled().connect (sigc::mem_fun (*this, &ARDOUR_UI::no_memory_warning));
707                                 
708                                 hbox.pack_start (cb, true, false);
709                                 vbox->pack_start (hbox);
710                                 cb.show();
711                                 vbox->show();
712                                 hbox.show ();
713
714                                 pop_back_splash (msg);
715
716                                 editor->ensure_float (msg);
717                                 msg.run ();
718                         }
719                 }
720         }
721 #endif // !__APPLE__
722 }
723
724
725 void
726 ARDOUR_UI::queue_finish ()
727 {
728         Glib::signal_idle().connect (mem_fun (*this, &ARDOUR_UI::idle_finish));
729 }
730
731 bool
732 ARDOUR_UI::idle_finish ()
733 {
734         finish ();
735         return false; /* do not call again */
736 }
737
738 void
739 ARDOUR_UI::finish()
740 {
741         if (_session) {
742
743                 if (_session->dirty()) {
744                         vector<string> actions;
745                         actions.push_back (_("Don't quit"));
746                         actions.push_back (_("Just quit"));
747                         actions.push_back (_("Save and quit"));
748                         switch (ask_about_saving_session(actions)) {
749                         case -1:
750                                 return;
751                                 break;
752                         case 1:
753                                 /* use the default name */
754                                 if (save_state_canfail ("")) {
755                                         /* failed - don't quit */
756                                         MessageDialog msg (*editor,
757                                                            string_compose (_("\
758 %1 was unable to save your session.\n\n\
759 If you still wish to quit, please use the\n\n\
760 \"Just quit\" option."), PROGRAM_NAME));
761                                         pop_back_splash(msg);
762                                         msg.run ();
763                                         return;
764                                 }
765                                 break;
766                         case 0:
767                                 break;
768                         }
769                 }
770
771                 second_connection.disconnect ();
772                 point_one_second_connection.disconnect ();
773                 point_oh_five_second_connection.disconnect ();
774                 point_zero_one_second_connection.disconnect();
775         }
776
777         /* Save state before deleting the session, as that causes some
778            windows to be destroyed before their visible state can be
779            saved.
780         */
781         save_ardour_state ();
782
783         loading_message (string_compose (_("Please wait while %1 cleans up..."), PROGRAM_NAME));
784
785         if (_session) {
786                 // _session->set_deletion_in_progress ();
787                 _session->set_clean ();
788                 _session->remove_pending_capture_state ();
789                 delete _session;
790                 _session = 0;
791         }
792
793         ArdourDialog::close_all_dialogs ();
794         engine->stop (true);
795         quit ();
796 }
797
798 int
799 ARDOUR_UI::ask_about_saving_session (const vector<string>& actions)
800 {
801         ArdourDialog window (_("Unsaved Session"));
802         Gtk::HBox dhbox;  // the hbox for the image and text
803         Gtk::Label  prompt_label;
804         Gtk::Image* dimage = manage (new Gtk::Image(Stock::DIALOG_WARNING,  Gtk::ICON_SIZE_DIALOG));
805
806         string msg;
807
808         assert (actions.size() >= 3);
809
810         window.add_button (actions[0], RESPONSE_REJECT);
811         window.add_button (actions[1], RESPONSE_APPLY);
812         window.add_button (actions[2], RESPONSE_ACCEPT);
813
814         window.set_default_response (RESPONSE_ACCEPT);
815
816         Gtk::Button noquit_button (msg);
817         noquit_button.set_name ("EditorGTKButton");
818
819         string prompt;
820
821         if (_session->snap_name() == _session->name()) {
822                 prompt = string_compose(_("The session \"%1\"\nhas not been saved.\n\nAny changes made this time\nwill be lost unless you save it.\n\nWhat do you want to do?"),
823                                         _session->snap_name());
824         } else {
825                 prompt = string_compose(_("The snapshot \"%1\"\nhas not been saved.\n\nAny changes made this time\nwill be lost unless you save it.\n\nWhat do you want to do?"),
826                                         _session->snap_name());
827         }
828
829         prompt_label.set_text (prompt);
830         prompt_label.set_name (X_("PrompterLabel"));
831         prompt_label.set_alignment(ALIGN_LEFT, ALIGN_TOP);
832
833         dimage->set_alignment(ALIGN_CENTER, ALIGN_TOP);
834         dhbox.set_homogeneous (false);
835         dhbox.pack_start (*dimage, false, false, 5);
836         dhbox.pack_start (prompt_label, true, false, 5);
837         window.get_vbox()->pack_start (dhbox);
838
839         window.set_name (_("Prompter"));
840         window.set_position (Gtk::WIN_POS_MOUSE);
841         window.set_modal (true);
842         window.set_resizable (false);
843
844         dhbox.show();
845         prompt_label.show();
846         dimage->show();
847         window.show();
848         window.set_keep_above (true);
849         window.present ();
850
851         ResponseType r = (ResponseType) window.run();
852
853         window.hide ();
854
855         switch (r) {
856         case RESPONSE_ACCEPT: // save and get out of here
857                 return 1;
858         case RESPONSE_APPLY:  // get out of here
859                 return 0;
860         default:
861                 break;
862         }
863
864         return -1;
865 }
866
867
868 gint
869 ARDOUR_UI::every_second ()
870 {
871         update_cpu_load ();
872         update_buffer_load ();
873         update_disk_space ();
874         update_timecode_format ();
875         return TRUE;
876 }
877
878 gint
879 ARDOUR_UI::every_point_one_seconds ()
880 {
881         shuttle_box->update_speed_display ();
882         RapidScreenUpdate(); /* EMIT_SIGNAL */
883         return TRUE;
884 }
885
886 gint
887 ARDOUR_UI::every_point_zero_one_seconds ()
888 {
889         // august 2007: actual update frequency: 40Hz, not 100Hz
890
891         SuperRapidScreenUpdate(); /* EMIT_SIGNAL */
892         return TRUE;
893 }
894
895 void
896 ARDOUR_UI::update_sample_rate (framecnt_t)
897 {
898         char buf[64];
899
900         ENSURE_GUI_THREAD (*this, &ARDOUR_UI::update_sample_rate, ignored)
901
902         if (!engine->connected()) {
903
904                 snprintf (buf, sizeof (buf), "%s", _("disconnected"));
905
906         } else {
907
908                 framecnt_t rate = engine->frame_rate();
909
910                 if (fmod (rate, 1000.0) != 0.0) {
911                         snprintf (buf, sizeof (buf), _("JACK: <span foreground=\"green\">%.1f kHz / %4.1f ms</span>"),
912                                   (float) rate/1000.0f,
913                                   (engine->frames_per_cycle() / (float) rate) * 1000.0f);
914                 } else {
915                         snprintf (buf, sizeof (buf), _("JACK: <span foreground=\"green\">%" PRId64 " kHz / %4.1f ms</span>"),
916                                   rate/1000,
917                                   (engine->frames_per_cycle() / (float) rate) * 1000.0f);
918                 }
919         }
920
921         sample_rate_label.set_markup (buf);
922 }
923
924 void
925 ARDOUR_UI::update_format ()
926 {
927         if (!_session) {
928                 format_label.set_text ("");
929                 return;
930         }
931
932         stringstream s;
933         s << _("File:") << X_(" <span foreground=\"green\">");
934
935         switch (_session->config.get_native_file_header_format ()) {
936         case BWF:
937                 s << _("BWF");
938                 break;
939         case WAVE:
940                 s << _("WAV");
941                 break;
942         case WAVE64:
943                 s << _("WAV64");
944                 break;
945         case CAF:
946                 s << _("CAF");
947                 break;
948         case AIFF:
949                 s << _("AIFF");
950                 break;
951         case iXML:
952                 s << _("iXML");
953                 break;
954         case RF64:
955                 s << _("RF64");
956                 break;
957         }
958
959         s << " ";
960         
961         switch (_session->config.get_native_file_data_format ()) {
962         case FormatFloat:
963                 s << _("32-float");
964                 break;
965         case FormatInt24:
966                 s << _("24-int");
967                 break;
968         case FormatInt16:
969                 s << _("16-int");
970                 break;
971         }
972
973         s << X_("</span>");
974
975         format_label.set_markup (s.str ());
976 }
977
978 void
979 ARDOUR_UI::update_cpu_load ()
980 {
981         char buf[64];
982
983         /* If this text is changed, the set_size_request_to_display_given_text call in ARDOUR_UI::resize_text_widgets
984            should also be changed.
985         */
986
987         float const c = engine->get_cpu_load ();
988         snprintf (buf, sizeof (buf), _("DSP: <span foreground=\"%s\">%5.1f%%</span>"), c >= 90 ? X_("red") : X_("green"), c);
989         cpu_load_label.set_markup (buf);
990 }
991
992 void
993 ARDOUR_UI::update_buffer_load ()
994 {
995         char buf[256];
996
997         uint32_t const playback = _session ? _session->playback_load () : 100;
998         uint32_t const capture = _session ? _session->capture_load () : 100;
999
1000         /* If this text is changed, the set_size_request_to_display_given_text call in ARDOUR_UI::resize_text_widgets
1001            should also be changed.
1002         */
1003         
1004         if (_session) {
1005                 snprintf (
1006                         buf, sizeof (buf),
1007                         _("Buffers: <span foreground=\"green\">p:</span><span foreground=\"%s\">%" PRIu32 "%%</span> "
1008                                    "<span foreground=\"green\">c:</span><span foreground=\"%s\">%" PRIu32 "%%</span>"),
1009                         playback <= 5 ? X_("red") : X_("green"),
1010                         playback,
1011                         capture <= 5 ? X_("red") : X_("green"),
1012                         capture
1013                         );
1014
1015                 buffer_load_label.set_markup (buf);
1016         } else {
1017                 buffer_load_label.set_text ("");
1018         }
1019 }
1020
1021 void
1022 ARDOUR_UI::count_recenabled_streams (Route& route)
1023 {
1024         Track* track = dynamic_cast<Track*>(&route);
1025         if (track && track->record_enabled()) {
1026                 rec_enabled_streams += track->n_inputs().n_total();
1027         }
1028 }
1029
1030 void
1031 ARDOUR_UI::update_disk_space()
1032 {
1033         if (_session == 0) {
1034                 return;
1035         }
1036
1037         boost::optional<framecnt_t> opt_frames = _session->available_capture_duration();
1038         char buf[64];
1039         framecnt_t fr = _session->frame_rate();
1040
1041         if (!opt_frames) {
1042                 /* Available space is unknown */
1043                 snprintf (buf, sizeof (buf), "%s", _("Disk: <span foreground=\"green\">Unknown</span>"));
1044         } else if (opt_frames.get_value_or (0) == max_framecnt) {
1045                 snprintf (buf, sizeof (buf), "%s", _("Disk: <span foreground=\"green\">24hrs+</span>"));
1046         } else {
1047                 rec_enabled_streams = 0;
1048                 _session->foreach_route (this, &ARDOUR_UI::count_recenabled_streams);
1049
1050                 framecnt_t frames = opt_frames.get_value_or (0);
1051
1052                 if (rec_enabled_streams) {
1053                         frames /= rec_enabled_streams;
1054                 }
1055
1056                 int hrs;
1057                 int mins;
1058                 int secs;
1059
1060                 hrs  = frames / (fr * 3600);
1061
1062                 if (hrs > 24) {
1063                         snprintf (buf, sizeof (buf), "%s", _("Disk: <span foreground=\"green\">&gt;24 hrs</span>"));
1064                 } else {
1065                         frames -= hrs * fr * 3600;
1066                         mins = frames / (fr * 60);
1067                         frames -= mins * fr * 60;
1068                         secs = frames / fr;
1069                         
1070                         bool const low = (hrs == 0 && mins <= 30);
1071                         
1072                         snprintf (
1073                                 buf, sizeof(buf),
1074                                 _("Disk: <span foreground=\"%s\">%02dh:%02dm:%02ds</span>"),
1075                                 low ? X_("red") : X_("green"),
1076                                 hrs, mins, secs
1077                                 );
1078                 }
1079         }
1080
1081         disk_space_label.set_markup (buf);
1082 }
1083
1084 void
1085 ARDOUR_UI::update_timecode_format ()
1086 {
1087         char buf[64];
1088
1089         if (_session) {
1090                 bool matching;
1091                 TimecodeSlave* tcslave;
1092                 SyncSource sync_src = Config->get_sync_source();
1093
1094                 if ((sync_src == LTC || sync_src == MTC) && (tcslave = dynamic_cast<TimecodeSlave*>(_session->slave())) != 0) {
1095                         matching = (tcslave->apparent_timecode_format() == _session->config.get_timecode_format());
1096                 } else {
1097                         matching = true;
1098                 }
1099                         
1100                 snprintf (buf, sizeof (buf), S_("Timecode|TC: <span foreground=\"%s\">%s</span>"),
1101                           matching ? X_("green") : X_("red"),
1102                           Timecode::timecode_format_name (_session->config.get_timecode_format()).c_str());
1103         } else {
1104                 snprintf (buf, sizeof (buf), "TC: n/a");
1105         }
1106
1107         timecode_format_label.set_markup (buf);
1108 }       
1109
1110 gint
1111 ARDOUR_UI::update_wall_clock ()
1112 {
1113         time_t now;
1114         struct tm *tm_now;
1115         static int last_min = -1;
1116
1117         time (&now);
1118         tm_now = localtime (&now);
1119         if (last_min != tm_now->tm_min) {
1120                 char buf[16];
1121                 sprintf (buf, "%02d:%02d", tm_now->tm_hour, tm_now->tm_min);
1122                 wall_clock_label.set_text (buf);
1123                 last_min = tm_now->tm_min;
1124         }
1125
1126         return TRUE;
1127 }
1128
1129 void
1130 ARDOUR_UI::redisplay_recent_sessions ()
1131 {
1132         std::vector<std::string> session_directories;
1133         RecentSessionsSorter cmp;
1134
1135         recent_session_display.set_model (Glib::RefPtr<TreeModel>(0));
1136         recent_session_model->clear ();
1137
1138         ARDOUR::RecentSessions rs;
1139         ARDOUR::read_recent_sessions (rs);
1140
1141         if (rs.empty()) {
1142                 recent_session_display.set_model (recent_session_model);
1143                 return;
1144         }
1145
1146         // sort them alphabetically
1147         sort (rs.begin(), rs.end(), cmp);
1148
1149         for (ARDOUR::RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
1150                 session_directories.push_back ((*i).second);
1151         }
1152
1153         for (vector<std::string>::const_iterator i = session_directories.begin();
1154                         i != session_directories.end(); ++i)
1155         {
1156                 std::vector<std::string> state_file_paths;
1157
1158                 // now get available states for this session
1159
1160                 get_state_files_in_directory (*i, state_file_paths);
1161
1162                 vector<string*>* states;
1163                 vector<const gchar*> item;
1164                 string fullpath = *i;
1165
1166                 /* remove any trailing / */
1167
1168                 if (fullpath[fullpath.length() - 1] == '/') {
1169                         fullpath = fullpath.substr (0, fullpath.length() - 1);
1170                 }
1171
1172                 /* check whether session still exists */
1173                 if (!Glib::file_test(fullpath.c_str(), Glib::FILE_TEST_EXISTS)) {
1174                         /* session doesn't exist */
1175                         continue;
1176                 }
1177
1178                 /* now get available states for this session */
1179
1180                 if ((states = Session::possible_states (fullpath)) == 0) {
1181                         /* no state file? */
1182                         continue;
1183                 }
1184
1185                 std::vector<string> state_file_names(get_file_names_no_extension (state_file_paths));
1186
1187                 Gtk::TreeModel::Row row = *(recent_session_model->append());
1188
1189                 row[recent_session_columns.visible_name] = Glib::path_get_basename (fullpath);
1190                 row[recent_session_columns.fullpath] = fullpath;
1191
1192                 if (state_file_names.size() > 1) {
1193
1194                         // add the children
1195
1196                         for (std::vector<std::string>::iterator i2 = state_file_names.begin();
1197                                         i2 != state_file_names.end(); ++i2)
1198                         {
1199
1200                                 Gtk::TreeModel::Row child_row = *(recent_session_model->append (row.children()));
1201
1202                                 child_row[recent_session_columns.visible_name] = *i2;
1203                                 child_row[recent_session_columns.fullpath] = fullpath;
1204                         }
1205                 }
1206         }
1207
1208         recent_session_display.set_tooltip_column(1); // recent_session_columns.fullpath
1209         recent_session_display.set_model (recent_session_model);
1210 }
1211
1212 void
1213 ARDOUR_UI::build_session_selector ()
1214 {
1215         session_selector_window = new ArdourDialog (_("Recent Sessions"));
1216
1217         Gtk::ScrolledWindow *scroller = manage (new Gtk::ScrolledWindow);
1218
1219         session_selector_window->add_button (Stock::CANCEL, RESPONSE_CANCEL);
1220         session_selector_window->add_button (Stock::OPEN, RESPONSE_ACCEPT);
1221         session_selector_window->set_default_response (RESPONSE_ACCEPT);
1222         recent_session_model = TreeStore::create (recent_session_columns);
1223         recent_session_display.set_model (recent_session_model);
1224         recent_session_display.append_column (_("Recent Sessions"), recent_session_columns.visible_name);
1225         recent_session_display.set_headers_visible (false);
1226         recent_session_display.get_selection()->set_mode (SELECTION_BROWSE);
1227         recent_session_display.signal_row_activated().connect (sigc::mem_fun (*this, &ARDOUR_UI::recent_session_row_activated));
1228
1229         scroller->add (recent_session_display);
1230         scroller->set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
1231
1232         session_selector_window->set_name ("SessionSelectorWindow");
1233         session_selector_window->set_size_request (200, 400);
1234         session_selector_window->get_vbox()->pack_start (*scroller);
1235
1236         recent_session_display.show();
1237         scroller->show();
1238 }
1239
1240 void
1241 ARDOUR_UI::recent_session_row_activated (const TreePath& /*path*/, TreeViewColumn* /*col*/)
1242 {
1243         session_selector_window->response (RESPONSE_ACCEPT);
1244 }
1245
1246 void
1247 ARDOUR_UI::open_recent_session ()
1248 {
1249         bool can_return = (_session != 0);
1250
1251         if (session_selector_window == 0) {
1252                 build_session_selector ();
1253         }
1254
1255         redisplay_recent_sessions ();
1256
1257         while (true) {
1258
1259                 session_selector_window->set_position (WIN_POS_MOUSE);
1260
1261                 ResponseType r = (ResponseType) session_selector_window->run ();
1262
1263                 switch (r) {
1264                 case RESPONSE_ACCEPT:
1265                         break;
1266                 default:
1267                         if (can_return) {
1268                                 session_selector_window->hide();
1269                                 return;
1270                         } else {
1271                                 exit (1);
1272                         }
1273                 }
1274
1275                 if (recent_session_display.get_selection()->count_selected_rows() == 0) {
1276                         continue;
1277                 }
1278
1279                 session_selector_window->hide();
1280
1281                 Gtk::TreeModel::iterator i = recent_session_display.get_selection()->get_selected();
1282
1283                 if (i == recent_session_model->children().end()) {
1284                         return;
1285                 }
1286
1287                 std::string path = (*i)[recent_session_columns.fullpath];
1288                 std::string state = (*i)[recent_session_columns.visible_name];
1289
1290                 _session_is_new = false;
1291
1292                 if (load_session (path, state) == 0) {
1293                         break;
1294                 }
1295
1296                 can_return = false;
1297         }
1298 }
1299
1300 bool
1301 ARDOUR_UI::check_audioengine ()
1302 {
1303         if (engine) {
1304                 if (!engine->connected()) {
1305                         MessageDialog msg (string_compose (
1306                                                    _("%1 is not connected to JACK\n"
1307                                                      "You cannot open or close sessions in this condition"),
1308                                                    PROGRAM_NAME));
1309                         pop_back_splash (msg);
1310                         msg.run ();
1311                         return false;
1312                 }
1313                 return true;
1314         } else {
1315                 return false;
1316         }
1317 }
1318
1319 void
1320 ARDOUR_UI::open_session ()
1321 {
1322         if (!check_audioengine()) {
1323                 return;
1324
1325         }
1326
1327         /* popup selector window */
1328
1329         if (open_session_selector == 0) {
1330
1331                 /* ardour sessions are folders */
1332
1333                 open_session_selector = new Gtk::FileChooserDialog (_("Open Session"), FILE_CHOOSER_ACTION_OPEN);
1334                 open_session_selector->add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1335                 open_session_selector->add_button (Gtk::Stock::OPEN, Gtk::RESPONSE_ACCEPT);
1336                 open_session_selector->set_default_response(Gtk::RESPONSE_ACCEPT);
1337                 
1338                 if (_session) {
1339                         string session_parent_dir = Glib::path_get_dirname(_session->path());
1340                         string::size_type last_dir_sep = session_parent_dir.rfind(G_DIR_SEPARATOR);
1341                         session_parent_dir = session_parent_dir.substr(0, last_dir_sep);
1342                         open_session_selector->set_current_folder(session_parent_dir);
1343                 } else {
1344                         open_session_selector->set_current_folder(Config->get_default_session_parent_dir());
1345                 }
1346
1347                 open_session_selector->add_shortcut_folder (Config->get_default_session_parent_dir());
1348
1349                 FileFilter session_filter;
1350                 session_filter.add_pattern ("*.ardour");
1351                 session_filter.set_name (string_compose (_("%1 sessions"), PROGRAM_NAME));
1352                 open_session_selector->add_filter (session_filter);
1353                 open_session_selector->set_filter (session_filter);
1354         }
1355
1356         int response = open_session_selector->run();
1357         open_session_selector->hide ();
1358
1359         switch (response) {
1360         case RESPONSE_ACCEPT:
1361                 break;
1362         default:
1363                 open_session_selector->hide();
1364                 return;
1365         }
1366
1367         open_session_selector->hide();
1368         string session_path = open_session_selector->get_filename();
1369         string path, name;
1370         bool isnew;
1371
1372         if (session_path.length() > 0) {
1373                 if (ARDOUR::find_session (session_path, path, name, isnew) == 0) {
1374                         _session_is_new = isnew;
1375                         load_session (path, name);
1376                 }
1377         }
1378 }
1379
1380
1381 void
1382 ARDOUR_UI::session_add_mixed_track (const ChanCount& input, const ChanCount& output, RouteGroup* route_group, 
1383                                     uint32_t how_many, const string& name_template, PluginInfoPtr instrument)
1384 {
1385         list<boost::shared_ptr<MidiTrack> > tracks;
1386
1387         if (_session == 0) {
1388                 warning << _("You cannot add a track without a session already loaded.") << endmsg;
1389                 return;
1390         }
1391
1392         try {
1393                 tracks = _session->new_midi_track (input, output, instrument, ARDOUR::Normal, route_group, how_many, name_template);
1394                 
1395                 if (tracks.size() != how_many) {
1396                         error << string_compose(P_("could not create %1 new mixed track", "could not create %1 new mixed tracks", how_many), how_many) << endmsg;
1397                 }
1398         }
1399
1400         catch (...) {
1401                 MessageDialog msg (*editor,
1402                                    string_compose (_("There are insufficient JACK ports available\n\
1403 to create a new track or bus.\n\
1404 You should save %1, exit and\n\
1405 restart JACK with more ports."), PROGRAM_NAME));
1406                 msg.run ();
1407         }
1408 }
1409         
1410
1411 void
1412 ARDOUR_UI::session_add_midi_route (bool disk, RouteGroup* route_group, uint32_t how_many, const string& name_template, PluginInfoPtr instrument)
1413 {
1414         ChanCount one_midi_channel;
1415         one_midi_channel.set (DataType::MIDI, 1);
1416
1417         if (disk) {
1418                 session_add_mixed_track (one_midi_channel, one_midi_channel, route_group, how_many, name_template, instrument);
1419         }
1420 }
1421
1422 void
1423 ARDOUR_UI::session_add_audio_route (
1424         bool track,
1425         int32_t input_channels,
1426         int32_t output_channels,
1427         ARDOUR::TrackMode mode,
1428         RouteGroup* route_group,
1429         uint32_t how_many,
1430         string const & name_template
1431         )
1432 {
1433         list<boost::shared_ptr<AudioTrack> > tracks;
1434         RouteList routes;
1435
1436         if (_session == 0) {
1437                 warning << _("You cannot add a track or bus without a session already loaded.") << endmsg;
1438                 return;
1439         }
1440
1441         try {
1442                 if (track) {
1443                         tracks = _session->new_audio_track (input_channels, output_channels, mode, route_group, how_many, name_template);
1444
1445                         if (tracks.size() != how_many) {
1446                                 error << string_compose (P_("could not create %1 new audio track", "could not create %1 new audio tracks", how_many), how_many) 
1447                                       << endmsg;
1448                         }
1449
1450                 } else {
1451
1452                         routes = _session->new_audio_route (input_channels, output_channels, route_group, how_many, name_template);
1453
1454                         if (routes.size() != how_many) {
1455                                 error << string_compose (P_("could not create %1 new audio bus", "could not create %1 new audio busses", how_many), how_many)
1456                                       << endmsg;
1457                         }
1458                 }
1459         }
1460
1461         catch (...) {
1462                 MessageDialog msg (*editor,
1463                                    string_compose (_("There are insufficient JACK ports available\n\
1464 to create a new track or bus.\n\
1465 You should save %1, exit and\n\
1466 restart JACK with more ports."), PROGRAM_NAME));
1467                 pop_back_splash (msg);
1468                 msg.run ();
1469         }
1470 }
1471
1472 void
1473 ARDOUR_UI::transport_goto_start ()
1474 {
1475         if (_session) {
1476                 _session->goto_start();
1477
1478                 /* force displayed area in editor to start no matter
1479                    what "follow playhead" setting is.
1480                 */
1481
1482                 if (editor) {
1483                         editor->center_screen (_session->current_start_frame ());
1484                 }
1485         }
1486 }
1487
1488 void
1489 ARDOUR_UI::transport_goto_zero ()
1490 {
1491         if (_session) {
1492                 _session->request_locate (0);
1493
1494                 /* force displayed area in editor to start no matter
1495                    what "follow playhead" setting is.
1496                 */
1497
1498                 if (editor) {
1499                         editor->reset_x_origin (0);
1500                 }
1501         }
1502 }
1503
1504 void
1505 ARDOUR_UI::transport_goto_wallclock ()
1506 {
1507         if (_session && editor) {
1508
1509                 time_t now;
1510                 struct tm tmnow;
1511                 framepos_t frames;
1512
1513                 time (&now);
1514                 localtime_r (&now, &tmnow);
1515
1516                 frames = tmnow.tm_hour * (60 * 60 * _session->frame_rate());
1517                 frames += tmnow.tm_min * (60 * _session->frame_rate());
1518                 frames += tmnow.tm_sec * _session->frame_rate();
1519
1520                 _session->request_locate (frames, _session->transport_rolling ());
1521
1522                 /* force displayed area in editor to start no matter
1523                    what "follow playhead" setting is.
1524                 */
1525
1526                 if (editor) {
1527                         editor->center_screen (frames);
1528                 }
1529         }
1530 }
1531
1532 void
1533 ARDOUR_UI::transport_goto_end ()
1534 {
1535         if (_session) {
1536                 framepos_t const frame = _session->current_end_frame();
1537                 _session->request_locate (frame);
1538
1539                 /* force displayed area in editor to start no matter
1540                    what "follow playhead" setting is.
1541                 */
1542
1543                 if (editor) {
1544                         editor->center_screen (frame);
1545                 }
1546         }
1547 }
1548
1549 void
1550 ARDOUR_UI::transport_stop ()
1551 {
1552         if (!_session) {
1553                 return;
1554         }
1555
1556         if (_session->is_auditioning()) {
1557                 _session->cancel_audition ();
1558                 return;
1559         }
1560
1561         _session->request_stop (false, true);
1562 }
1563
1564 void
1565 ARDOUR_UI::transport_record (bool roll)
1566 {
1567
1568         if (_session) {
1569                 switch (_session->record_status()) {
1570                 case Session::Disabled:
1571                         if (_session->ntracks() == 0) {
1572                                 MessageDialog msg (*editor, _("Please create one or more tracks before trying to record.\nYou can do this with the \"Add Track or Bus\" option in the Session menu."));
1573                                 msg.run ();
1574                                 return;
1575                         }
1576                         _session->maybe_enable_record ();
1577                         if (roll) {
1578                                 transport_roll ();
1579                         }
1580                         break;
1581                 case Session::Recording:
1582                         if (roll) {
1583                                 _session->request_stop();
1584                         } else {
1585                                 _session->disable_record (false, true);
1586                         }
1587                         break;
1588
1589                 case Session::Enabled:
1590                         _session->disable_record (false, true);
1591                 }
1592         }
1593 }
1594
1595 void
1596 ARDOUR_UI::transport_roll ()
1597 {
1598         if (!_session) {
1599                 return;
1600         }
1601
1602         if (_session->is_auditioning()) {
1603                 return;
1604         }
1605
1606 #if 0
1607         if (_session->config.get_external_sync()) {
1608                 switch (Config->get_sync_source()) {
1609                 case JACK:
1610                         break;
1611                 default:
1612                         /* transport controlled by the master */
1613                         return;
1614                 }
1615         }
1616 #endif
1617
1618         bool rolling = _session->transport_rolling();
1619
1620         if (_session->get_play_loop()) {
1621                 /* XXX it is not possible to just leave seamless loop and keep
1622                    playing at present (nov 4th 2009)
1623                 */
1624                 if (!Config->get_seamless_loop()) {
1625                         _session->request_play_loop (false, true);
1626                 }
1627         } else if (_session->get_play_range () && !Config->get_always_play_range()) {
1628                 /* stop playing a range if we currently are */
1629                 _session->request_play_range (0, true);
1630         }
1631
1632         if (!rolling) {
1633                 _session->request_transport_speed (1.0f);
1634         }
1635 }
1636
1637 bool
1638 ARDOUR_UI::get_smart_mode() const
1639 {
1640         return ( editor->get_smart_mode() );
1641 }
1642
1643
1644 void
1645 ARDOUR_UI::toggle_roll (bool with_abort, bool roll_out_of_bounded_mode)
1646 {
1647
1648         if (!_session) {
1649                 return;
1650         }
1651
1652         if (_session->is_auditioning()) {
1653                 _session->cancel_audition ();
1654                 return;
1655         }
1656
1657         if (_session->config.get_external_sync()) {
1658                 switch (Config->get_sync_source()) {
1659                 case JACK:
1660                         break;
1661                 default:
1662                         /* transport controlled by the master */
1663                         return;
1664                 }
1665         }
1666
1667         bool rolling = _session->transport_rolling();
1668         bool affect_transport = true;
1669
1670         if (rolling && roll_out_of_bounded_mode) {
1671                 /* drop out of loop/range playback but leave transport rolling */
1672                 if (_session->get_play_loop()) {
1673                         if (Config->get_seamless_loop()) {
1674                                 /* the disk buffers contain copies of the loop - we can't
1675                                    just keep playing, so stop the transport. the user
1676                                    can restart as they wish.
1677                                 */
1678                                 affect_transport = true;
1679                         } else {
1680                                 /* disk buffers are normal, so we can keep playing */
1681                                 affect_transport = false;
1682                         }
1683                         _session->request_play_loop (false, true);
1684                 } else if (_session->get_play_range ()) {
1685                         affect_transport = false;
1686                         _session->request_play_range (0, true);
1687                 }
1688         }
1689
1690         if (affect_transport) {
1691                 if (rolling) {
1692                         _session->request_stop (with_abort, true);
1693                 } else {
1694                         if ( Config->get_always_play_range() ) {
1695                                 _session->request_play_range (&editor->get_selection().time, true);
1696                         }
1697
1698                         _session->request_transport_speed (1.0f);
1699                 }
1700         }
1701 }
1702
1703 void
1704 ARDOUR_UI::toggle_session_auto_loop ()
1705 {
1706         Location * looploc = _session->locations()->auto_loop_location();
1707
1708         if (!_session || !looploc) {
1709                 return;
1710         }
1711
1712         if (_session->get_play_loop()) {
1713
1714                 if (_session->transport_rolling()) {
1715
1716                         _session->request_locate (looploc->start(), true);
1717                         _session->request_play_loop (false);
1718
1719                 } else {
1720                         _session->request_play_loop (false);
1721                 }
1722         } else {
1723                 _session->request_play_loop (true);
1724         }
1725         
1726         //show the loop markers
1727         looploc->set_hidden (false, this);
1728 }
1729
1730 void
1731 ARDOUR_UI::transport_play_selection ()
1732 {
1733         if (!_session) {
1734                 return;
1735         }
1736
1737         editor->play_selection ();
1738 }
1739
1740 void
1741 ARDOUR_UI::transport_play_preroll ()
1742 {
1743         if (!_session) {
1744                 return;
1745         }
1746         editor->play_with_preroll ();
1747 }
1748
1749 void
1750 ARDOUR_UI::transport_rewind (int option)
1751 {
1752         float current_transport_speed;
1753
1754         if (_session) {
1755                 current_transport_speed = _session->transport_speed();
1756
1757                 if (current_transport_speed >= 0.0f) {
1758                         switch (option) {
1759                         case 0:
1760                                 _session->request_transport_speed (-1.0f);
1761                                 break;
1762                         case 1:
1763                                 _session->request_transport_speed (-4.0f);
1764                                 break;
1765                         case -1:
1766                                 _session->request_transport_speed (-0.5f);
1767                                 break;
1768                         }
1769                 } else {
1770                         /* speed up */
1771                         _session->request_transport_speed (current_transport_speed * 1.5f);
1772                 }
1773         }
1774 }
1775
1776 void
1777 ARDOUR_UI::transport_forward (int option)
1778 {
1779         if (!_session) {
1780                 return;
1781         }
1782         
1783         float current_transport_speed = _session->transport_speed();
1784         
1785         if (current_transport_speed <= 0.0f) {
1786                 switch (option) {
1787                 case 0:
1788                         _session->request_transport_speed (1.0f);
1789                         break;
1790                 case 1:
1791                         _session->request_transport_speed (4.0f);
1792                         break;
1793                 case -1:
1794                         _session->request_transport_speed (0.5f);
1795                         break;
1796                 }
1797         } else {
1798                 /* speed up */
1799                 _session->request_transport_speed (current_transport_speed * 1.5f);
1800         }
1801 }
1802
1803 void
1804 ARDOUR_UI::toggle_record_enable (uint32_t rid)
1805 {
1806         if (!_session) {
1807                 return;
1808         }
1809
1810         boost::shared_ptr<Route> r;
1811
1812         if ((r = _session->route_by_remote_id (rid)) != 0) {
1813
1814                 Track* t;
1815
1816                 if ((t = dynamic_cast<Track*>(r.get())) != 0) {
1817                         t->set_record_enabled (!t->record_enabled(), this);
1818                 }
1819         }
1820 }
1821
1822 void
1823 ARDOUR_UI::map_transport_state ()
1824 {
1825         if (!_session) {
1826                 auto_loop_button.unset_active_state ();
1827                 play_selection_button.unset_active_state ();
1828                 roll_button.unset_active_state ();
1829                 stop_button.set_active_state (Gtkmm2ext::ExplicitActive);
1830                 return;
1831         }
1832
1833         shuttle_box->map_transport_state ();
1834
1835         float sp = _session->transport_speed();
1836
1837         if (sp != 0.0f) {
1838
1839                 /* we're rolling */
1840
1841                 if (_session->get_play_range()) {
1842
1843                         play_selection_button.set_active_state (Gtkmm2ext::ExplicitActive);
1844                         roll_button.unset_active_state ();
1845                         auto_loop_button.unset_active_state ();
1846
1847                 } else if (_session->get_play_loop ()) {
1848
1849                         auto_loop_button.set_active (true);
1850                         play_selection_button.set_active (false);
1851                         roll_button.set_active (false);
1852
1853                 } else {
1854
1855                         roll_button.set_active (true);
1856                         play_selection_button.set_active (false);
1857                         auto_loop_button.set_active (false);
1858                 }
1859
1860                 if (Config->get_always_play_range()) {
1861                         /* light up both roll and play-selection if they are joined */
1862                         roll_button.set_active (true);
1863                         play_selection_button.set_active (true);
1864                 }
1865
1866                 stop_button.set_active (false);
1867
1868         } else {
1869
1870                 stop_button.set_active (true);
1871                 roll_button.set_active (false);
1872                 play_selection_button.set_active (false);
1873                 auto_loop_button.set_active (false);
1874                 update_disk_space ();
1875         }
1876 }
1877
1878 void
1879 ARDOUR_UI::engine_stopped ()
1880 {
1881         ENSURE_GUI_THREAD (*this, &ARDOUR_UI::engine_stopped)
1882         ActionManager::set_sensitive (ActionManager::jack_sensitive_actions, false);
1883         ActionManager::set_sensitive (ActionManager::jack_opposite_sensitive_actions, true);
1884 }
1885
1886 void
1887 ARDOUR_UI::engine_running ()
1888 {
1889         ENSURE_GUI_THREAD (*this, &ARDOUR_UI::engine_running)
1890         ActionManager::set_sensitive (ActionManager::jack_sensitive_actions, true);
1891         ActionManager::set_sensitive (ActionManager::jack_opposite_sensitive_actions, false);
1892
1893         Glib::RefPtr<Action> action;
1894         const char* action_name = 0;
1895
1896         switch (engine->frames_per_cycle()) {
1897         case 32:
1898                 action_name = X_("JACKLatency32");
1899                 break;
1900         case 64:
1901                 action_name = X_("JACKLatency64");
1902                 break;
1903         case 128:
1904                 action_name = X_("JACKLatency128");
1905                 break;
1906         case 512:
1907                 action_name = X_("JACKLatency512");
1908                 break;
1909         case 1024:
1910                 action_name = X_("JACKLatency1024");
1911                 break;
1912         case 2048:
1913                 action_name = X_("JACKLatency2048");
1914                 break;
1915         case 4096:
1916                 action_name = X_("JACKLatency4096");
1917                 break;
1918         case 8192:
1919                 action_name = X_("JACKLatency8192");
1920                 break;
1921         default:
1922                 /* XXX can we do anything useful ? */
1923                 break;
1924         }
1925
1926         if (action_name) {
1927
1928                 action = ActionManager::get_action (X_("JACK"), action_name);
1929
1930                 if (action) {
1931                         Glib::RefPtr<RadioAction> ract = Glib::RefPtr<RadioAction>::cast_dynamic (action);
1932                         ract->set_active ();
1933                 }
1934         }
1935 }
1936
1937 void
1938 ARDOUR_UI::engine_halted (const char* reason, bool free_reason)
1939 {
1940         if (!Gtkmm2ext::UI::instance()->caller_is_ui_thread()) {
1941                 /* we can't rely on the original string continuing to exist when we are called
1942                    again in the GUI thread, so make a copy and note that we need to
1943                    free it later.
1944                 */
1945                 char *copy = strdup (reason);
1946                 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&ARDOUR_UI::engine_halted, this, copy, true));
1947                 return;
1948         }
1949
1950         ActionManager::set_sensitive (ActionManager::jack_sensitive_actions, false);
1951         ActionManager::set_sensitive (ActionManager::jack_opposite_sensitive_actions, true);
1952
1953         update_sample_rate (0);
1954
1955         string msgstr;
1956
1957         /* if the reason is a non-empty string, it means that the backend was shutdown
1958            rather than just Ardour.
1959         */
1960
1961         if (strlen (reason)) {
1962                 msgstr = string_compose (_("The audio backend (JACK) was shutdown because:\n\n%1"), reason);
1963         } else {
1964                 msgstr = string_compose (_("\
1965 JACK has either been shutdown or it\n\
1966 disconnected %1 because %1\n\
1967 was not fast enough. Try to restart\n\
1968 JACK, reconnect and save the session."), PROGRAM_NAME);
1969         }
1970
1971         MessageDialog msg (*editor, msgstr);
1972         pop_back_splash (msg);
1973         msg.run ();
1974
1975         if (free_reason) {
1976                 free (const_cast<char*> (reason));
1977         }
1978 }
1979
1980 int32_t
1981 ARDOUR_UI::do_engine_start ()
1982 {
1983         try {
1984                 engine->start();
1985         }
1986
1987         catch (...) {
1988                 engine->stop ();
1989                 error << _("Unable to start the session running")
1990                       << endmsg;
1991                 unload_session ();
1992                 return -2;
1993         }
1994
1995         return 0;
1996 }
1997
1998 void
1999 ARDOUR_UI::update_clocks ()
2000 {
2001         if (!editor || !editor->dragging_playhead()) {
2002                 Clock (_session->audible_frame(), false, editor->get_preferred_edit_position()); /* EMIT_SIGNAL */
2003         }
2004 }
2005
2006 void
2007 ARDOUR_UI::start_clocking ()
2008 {
2009         clock_signal_connection = RapidScreenUpdate.connect (sigc::mem_fun(*this, &ARDOUR_UI::update_clocks));
2010 }
2011
2012 void
2013 ARDOUR_UI::stop_clocking ()
2014 {
2015         clock_signal_connection.disconnect ();
2016 }
2017
2018 gint
2019 ARDOUR_UI::_blink (void *arg)
2020 {
2021         ((ARDOUR_UI *) arg)->blink ();
2022         return TRUE;
2023 }
2024
2025 void
2026 ARDOUR_UI::blink ()
2027 {
2028         Blink (blink_on = !blink_on); /* EMIT_SIGNAL */
2029 }
2030
2031 void
2032 ARDOUR_UI::start_blinking ()
2033 {
2034         /* Start the blink signal. Everybody with a blinking widget
2035            uses Blink to drive the widget's state.
2036         */
2037
2038         if (blink_timeout_tag < 0) {
2039                 blink_on = false;
2040                 blink_timeout_tag = g_timeout_add (240, _blink, this);
2041         }
2042 }
2043
2044 void
2045 ARDOUR_UI::stop_blinking ()
2046 {
2047         if (blink_timeout_tag >= 0) {
2048                 g_source_remove (blink_timeout_tag);
2049                 blink_timeout_tag = -1;
2050         }
2051 }
2052
2053
2054 /** Ask the user for the name of a new snapshot and then take it.
2055  */
2056
2057 void
2058 ARDOUR_UI::snapshot_session (bool switch_to_it)
2059 {
2060         ArdourPrompter prompter (true);
2061         string snapname;
2062
2063         prompter.set_name ("Prompter");
2064         prompter.add_button (Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
2065         prompter.set_title (_("Take Snapshot"));
2066         prompter.set_prompt (_("Name of new snapshot"));
2067
2068         if (!switch_to_it) {
2069                 char timebuf[128];
2070                 time_t n;
2071                 struct tm local_time;
2072
2073                 time (&n);
2074                 localtime_r (&n, &local_time);
2075                 strftime (timebuf, sizeof(timebuf), "%FT%H.%M.%S", &local_time);
2076                 prompter.set_initial_text (timebuf);
2077         }
2078
2079   again:
2080         switch (prompter.run()) {
2081         case RESPONSE_ACCEPT:
2082         {
2083                 prompter.get_result (snapname);
2084
2085                 bool do_save = (snapname.length() != 0);
2086
2087                 if (do_save) {
2088                         char illegal = Session::session_name_is_legal(snapname);
2089                         if (illegal) {
2090                                 MessageDialog msg (string_compose (_("To ensure compatibility with various systems\n"
2091                                                      "snapshot names may not contain a '%1' character"), illegal));
2092                                 msg.run ();
2093                                 goto again;
2094                         }
2095                 }
2096
2097                 vector<std::string> p;
2098                 get_state_files_in_directory (_session->session_directory().root_path(), p);
2099                 vector<string> n = get_file_names_no_extension (p);
2100                 if (find (n.begin(), n.end(), snapname) != n.end()) {
2101
2102                         ArdourDialog confirm (_("Confirm Snapshot Overwrite"), true);
2103                         Label m (_("A snapshot already exists with that name.  Do you want to overwrite it?"));
2104                         confirm.get_vbox()->pack_start (m, true, true);
2105                         confirm.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
2106                         confirm.add_button (_("Overwrite"), Gtk::RESPONSE_ACCEPT);
2107                         confirm.show_all ();
2108                         switch (confirm.run()) {
2109                         case RESPONSE_CANCEL:
2110                                 do_save = false;
2111                         }
2112                 }
2113
2114                 if (do_save) {
2115                         save_state (snapname, switch_to_it);
2116                 }
2117                 break;
2118         }
2119
2120         default:
2121                 break;
2122         }
2123 }
2124
2125 /** Ask the user for a new session name and then rename the session to it.
2126  */
2127
2128 void
2129 ARDOUR_UI::rename_session ()
2130 {
2131         if (!_session) {
2132                 return;
2133         }
2134
2135         ArdourPrompter prompter (true);
2136         string name;
2137
2138         prompter.set_name ("Prompter");
2139         prompter.add_button (Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
2140         prompter.set_title (_("Rename Session"));
2141         prompter.set_prompt (_("New session name"));
2142
2143   again:
2144         switch (prompter.run()) {
2145         case RESPONSE_ACCEPT:
2146         {
2147                 prompter.get_result (name);
2148
2149                 bool do_rename = (name.length() != 0);
2150
2151                 if (do_rename) {
2152                         char illegal = Session::session_name_is_legal (name);
2153
2154                         if (illegal) {
2155                                 MessageDialog msg (string_compose (_("To ensure compatibility with various systems\n"
2156                                                                      "session names may not contain a '%1' character"), illegal));
2157                                 msg.run ();
2158                                 goto again;
2159                         }
2160
2161                         switch (_session->rename (name)) {
2162                         case -1: {
2163                                 MessageDialog msg (_("That name is already in use by another directory/folder. Please try again."));
2164                                 msg.set_position (WIN_POS_MOUSE);
2165                                 msg.run ();
2166                                 goto again;
2167                                 break;
2168                         }
2169                         case 0:
2170                                 break;
2171                         default: {
2172                                 MessageDialog msg (_("Renaming this session failed.\nThings could be seriously messed up at this point"));
2173                                 msg.set_position (WIN_POS_MOUSE);
2174                                 msg.run ();
2175                                 break;
2176                         }
2177                         }
2178                 }
2179                 
2180                 break;
2181         }
2182
2183         default:
2184                 break;
2185         }
2186 }
2187
2188 void
2189 ARDOUR_UI::save_state (const string & name, bool switch_to_it)
2190 {
2191         XMLNode* node = new XMLNode (X_("UI"));
2192
2193         for (list<WindowProxyBase*>::iterator i = _window_proxies.begin(); i != _window_proxies.end(); ++i) {
2194                 if (!(*i)->rc_configured()) {
2195                         node->add_child_nocopy (*((*i)->get_state ()));
2196                 }
2197         }
2198
2199         node->add_child_nocopy (gui_object_state->get_state());
2200
2201         _session->add_extra_xml (*node);
2202
2203         save_state_canfail (name, switch_to_it);
2204 }
2205
2206 int
2207 ARDOUR_UI::save_state_canfail (string name, bool switch_to_it)
2208 {
2209         if (_session) {
2210                 int ret;
2211
2212                 if (name.length() == 0) {
2213                         name = _session->snap_name();
2214                 }
2215
2216                 if ((ret = _session->save_state (name, false, switch_to_it)) != 0) {
2217                         return ret;
2218                 }
2219         }
2220
2221         save_ardour_state (); /* XXX cannot fail? yeah, right ... */
2222         return 0;
2223 }
2224
2225 void
2226 ARDOUR_UI::primary_clock_value_changed ()
2227 {
2228         if (_session) {
2229                 _session->request_locate (primary_clock->current_time ());
2230         }
2231 }
2232
2233 void
2234 ARDOUR_UI::big_clock_value_changed ()
2235 {
2236         if (_session) {
2237                 _session->request_locate (big_clock->current_time ());
2238         }
2239 }
2240
2241 void
2242 ARDOUR_UI::secondary_clock_value_changed ()
2243 {
2244         if (_session) {
2245                 _session->request_locate (secondary_clock->current_time ());
2246         }
2247 }
2248
2249 void
2250 ARDOUR_UI::transport_rec_enable_blink (bool onoff)
2251 {
2252         if (_session == 0) {
2253                 return;
2254         }
2255
2256         if (_session->step_editing()) {
2257                 return;
2258         }
2259
2260         Session::RecordState const r = _session->record_status ();
2261         bool const h = _session->have_rec_enabled_track ();
2262
2263         if (r == Session::Enabled || (r == Session::Recording && !h)) {
2264                 if (onoff) {
2265                         rec_button.set_active_state (Gtkmm2ext::ExplicitActive);
2266                 } else {
2267                         rec_button.set_active_state (Gtkmm2ext::ImplicitActive);
2268                 }
2269         } else if (r == Session::Recording && h) {
2270                 rec_button.set_active_state (Gtkmm2ext::ExplicitActive);
2271         } else {
2272                 rec_button.unset_active_state ();
2273         }
2274 }
2275
2276 void
2277 ARDOUR_UI::save_template ()
2278 {
2279         ArdourPrompter prompter (true);
2280         string name;
2281
2282         if (!check_audioengine()) {
2283                 return;
2284         }
2285
2286         prompter.set_name (X_("Prompter"));
2287         prompter.set_title (_("Save Template"));
2288         prompter.set_prompt (_("Name for template:"));
2289         prompter.set_initial_text(_session->name() + _("-template"));
2290         prompter.add_button (Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
2291
2292         switch (prompter.run()) {
2293         case RESPONSE_ACCEPT:
2294                 prompter.get_result (name);
2295
2296                 if (name.length()) {
2297                         _session->save_template (name);
2298                 }
2299                 break;
2300
2301         default:
2302                 break;
2303         }
2304 }
2305
2306 void
2307 ARDOUR_UI::edit_metadata ()
2308 {
2309         SessionMetadataEditor dialog;
2310         dialog.set_session (_session);
2311         editor->ensure_float (dialog);
2312         dialog.run ();
2313 }
2314
2315 void
2316 ARDOUR_UI::import_metadata ()
2317 {
2318         SessionMetadataImporter dialog;
2319         dialog.set_session (_session);
2320         editor->ensure_float (dialog);
2321         dialog.run ();
2322 }
2323
2324 bool
2325 ARDOUR_UI::ask_about_loading_existing_session (const std::string& session_path)
2326 {
2327         std::string str = string_compose (_("This session\n%1\nalready exists. Do you want to open it?"), session_path);
2328
2329         MessageDialog msg (str,
2330                            false,
2331                            Gtk::MESSAGE_WARNING,
2332                            Gtk::BUTTONS_YES_NO,
2333                            true);
2334
2335
2336         msg.set_name (X_("OpenExistingDialog"));
2337         msg.set_title (_("Open Existing Session"));
2338         msg.set_wmclass (X_("existing_session"), PROGRAM_NAME);
2339         msg.set_position (Gtk::WIN_POS_MOUSE);
2340         pop_back_splash (msg);
2341
2342         switch (msg.run()) {
2343         case RESPONSE_YES:
2344                 return true;
2345                 break;
2346         }
2347         return false;
2348 }
2349
2350 int
2351 ARDOUR_UI::build_session_from_nsd (const std::string& session_path, const std::string& session_name)
2352 {
2353         BusProfile bus_profile;
2354
2355         if (Profile->get_sae()) {
2356
2357                 bus_profile.master_out_channels = 2;
2358                 bus_profile.input_ac = AutoConnectPhysical;
2359                 bus_profile.output_ac = AutoConnectMaster;
2360                 bus_profile.requested_physical_in = 0; // use all available
2361                 bus_profile.requested_physical_out = 0; // use all available
2362
2363         } else {
2364
2365                 /* get settings from advanced section of NSD */
2366
2367                 if (_startup->create_master_bus()) {
2368                         bus_profile.master_out_channels = (uint32_t) _startup->master_channel_count();
2369                 } else {
2370                         bus_profile.master_out_channels = 0;
2371                 }
2372
2373                 if (_startup->connect_inputs()) {
2374                         bus_profile.input_ac = AutoConnectPhysical;
2375                 } else {
2376                         bus_profile.input_ac = AutoConnectOption (0);
2377                 }
2378
2379                 bus_profile.output_ac = AutoConnectOption (0);
2380
2381                 if (_startup->connect_outputs ()) {
2382                         if (_startup->connect_outs_to_master()) {
2383                                 bus_profile.output_ac = AutoConnectMaster;
2384                         } else if (_startup->connect_outs_to_physical()) {
2385                                 bus_profile.output_ac = AutoConnectPhysical;
2386                         }
2387                 }
2388
2389                 bus_profile.requested_physical_in = (uint32_t) _startup->input_limit_count();
2390                 bus_profile.requested_physical_out = (uint32_t) _startup->output_limit_count();
2391         }
2392
2393         if (build_session (session_path, session_name, bus_profile)) {
2394                 return -1;
2395         }
2396
2397         return 0;
2398 }
2399
2400 void
2401 ARDOUR_UI::idle_load (const std::string& path)
2402 {
2403         if (_session) {
2404                 if (Glib::file_test (path, Glib::FILE_TEST_IS_DIR)) {
2405                         /* /path/to/foo => /path/to/foo, foo */
2406                         load_session (path, basename_nosuffix (path));
2407                 } else {
2408                         /* /path/to/foo/foo.ardour => /path/to/foo, foo */
2409                         load_session (Glib::path_get_dirname (path), basename_nosuffix (path));
2410                 }
2411
2412         } else {
2413                 ARDOUR_COMMAND_LINE::session_name = path;
2414         }
2415 }
2416
2417 /** @param quit_on_cancel true if exit() should be called if the user clicks `cancel' in the new session dialog */
2418 int
2419 ARDOUR_UI::get_session_parameters (bool quit_on_cancel, bool should_be_new, string load_template)
2420 {
2421         string session_name;
2422         string session_path;
2423         string template_name;
2424         int ret = -1;
2425         bool likely_new = false;
2426
2427         /* deal with any existing DIRTY session now, rather than later. don't
2428          * treat a non-dirty session this way, so that it stays visible 
2429          * as we bring up the new session dialog.
2430          */
2431
2432         if (_session && _session->dirty()) {
2433                 if (unload_session (false)) {
2434                         /* unload cancelled by user */
2435                         return 0;
2436                 }
2437                 ARDOUR_COMMAND_LINE::session_name = "";
2438         }
2439
2440         if (!load_template.empty()) {
2441                 should_be_new = true;
2442                 template_name = load_template;
2443         }
2444
2445         while (ret != 0) {
2446
2447                 if (!ARDOUR_COMMAND_LINE::session_name.empty()) {
2448
2449                         /* if they named a specific statefile, use it, otherwise they are
2450                            just giving a session folder, and we want to use it as is
2451                            to find the session.
2452                         */
2453
2454                         string::size_type suffix = ARDOUR_COMMAND_LINE::session_name.find (statefile_suffix);
2455
2456                         if (suffix != string::npos) {
2457                                 session_path = Glib::path_get_dirname (ARDOUR_COMMAND_LINE::session_name);
2458                                 session_name = ARDOUR_COMMAND_LINE::session_name.substr (0, suffix);
2459                                 session_name = Glib::path_get_basename (session_name);
2460                         } else {
2461                                 session_path = ARDOUR_COMMAND_LINE::session_name;
2462                                 session_name = Glib::path_get_basename (ARDOUR_COMMAND_LINE::session_name);
2463                         }
2464                 } else {
2465                         session_path = "";
2466                         session_name = "";
2467                 }
2468
2469                 delete _startup;
2470                 _startup = new ArdourStartup (should_be_new, session_name, session_path, load_template);
2471                 
2472                 if (!_startup->ready_without_display()) {
2473                         _startup->present ();
2474                         main().run();
2475                         _startup->hide ();
2476                 }
2477                 
2478                 switch (_startup->response()) {
2479                 case RESPONSE_OK:
2480                         break;
2481                 default:
2482                         if (quit_on_cancel) {
2483                                 exit (1);
2484                         } else {
2485                                 return ret;
2486                         }
2487                 }
2488
2489                 /* if we run the startup dialog again, offer more than just "new session" */
2490                 
2491                 should_be_new = false;
2492                 
2493                 session_name = _startup->session_name (likely_new);
2494                 
2495                 string::size_type suffix = session_name.find (statefile_suffix);
2496                 
2497                 if (suffix != string::npos) {
2498                         session_name = session_name.substr (0, suffix);
2499                 }
2500                 
2501                 /* this shouldn't happen, but we catch it just in case it does */
2502                 
2503                 if (session_name.empty()) {
2504                         continue;
2505                 }
2506                 
2507                 if (_startup->use_session_template()) {
2508                         template_name = _startup->session_template_name();
2509                         _session_is_new = true;
2510                 }
2511                 
2512                 if (session_name[0] == G_DIR_SEPARATOR ||
2513                     (session_name.length() > 2 && session_name[0] == '.' && session_name[1] == G_DIR_SEPARATOR) ||
2514                     (session_name.length() > 3 && session_name[0] == '.' && session_name[1] == '.' && session_name[2] == G_DIR_SEPARATOR)) {
2515                         
2516                         /* absolute path or cwd-relative path specified for session name: infer session folder
2517                            from what was given.
2518                         */
2519                         
2520                         session_path = Glib::path_get_dirname (session_name);
2521                         session_name = Glib::path_get_basename (session_name);
2522                         
2523                 } else {
2524
2525                         session_path = _startup->session_folder();
2526                         
2527                         char illegal = Session::session_name_is_legal (session_name);
2528                         
2529                         if (illegal) {
2530                                 MessageDialog msg (*_startup,
2531                                                    string_compose (_("To ensure compatibility with various systems\n"
2532                                                                      "session names may not contain a '%1' character"),
2533                                                                    illegal));
2534                                 msg.run ();
2535                                 ARDOUR_COMMAND_LINE::session_name = ""; // cancel that
2536                                 continue;
2537                         }
2538                 }
2539         
2540                 if (create_engine ()) {
2541                         break;
2542                 }
2543
2544                 if (Glib::file_test (session_path, Glib::FileTest (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) {
2545
2546                         if (likely_new) {
2547
2548                                 std::string existing = Glib::build_filename (session_path, session_name);
2549
2550                                 if (!ask_about_loading_existing_session (existing)) {
2551                                         ARDOUR_COMMAND_LINE::session_name = ""; // cancel that
2552                                         continue;
2553                                 }
2554                         }
2555
2556                         _session_is_new = false;
2557
2558                 } else {
2559
2560                         if (!likely_new) {
2561                                 if (_startup) {
2562                                         pop_back_splash (*_startup);
2563                                 } else {
2564                                         hide_splash ();
2565                                 }
2566
2567                                 MessageDialog msg (string_compose (_("There is no existing session at \"%1\""), session_path));
2568                                 msg.run ();
2569                                 ARDOUR_COMMAND_LINE::session_name = ""; // cancel that
2570                                 continue;
2571                         }
2572
2573                         char illegal = Session::session_name_is_legal(session_name);
2574                         if (illegal) {
2575                                 pop_back_splash (*_startup);
2576                                 MessageDialog msg (*_startup, string_compose(_("To ensure compatibility with various systems\n"
2577                                                      "session names may not contain a '%1' character"), illegal));
2578                                 msg.run ();
2579                                 ARDOUR_COMMAND_LINE::session_name = ""; // cancel that
2580                                 continue;
2581                         }
2582
2583                         _session_is_new = true;
2584                 }
2585
2586                 if (likely_new && template_name.empty()) {
2587
2588                         ret = build_session_from_nsd (session_path, session_name);
2589
2590                 } else {
2591
2592                         ret = load_session (session_path, session_name, template_name);
2593
2594                         if (ret == -2) {
2595                                 /* not connected to the AudioEngine, so quit to avoid an infinite loop */
2596                                 exit (1);
2597                         }
2598
2599                         if (!ARDOUR_COMMAND_LINE::immediate_save.empty()) {
2600                                 _session->save_state (ARDOUR_COMMAND_LINE::immediate_save, false);
2601                                 exit (1);
2602                         }
2603                 }
2604         }
2605
2606         return ret;
2607 }
2608
2609 void
2610 ARDOUR_UI::close_session()
2611 {
2612         if (!check_audioengine()) {
2613                 return;
2614         }
2615
2616         if (unload_session (true)) {
2617                 return;
2618         }
2619
2620         ARDOUR_COMMAND_LINE::session_name = "";
2621
2622         if (get_session_parameters (true, false)) {
2623                 exit (1);
2624         }
2625
2626         goto_editor_window ();
2627 }
2628
2629 /** @param snap_name Snapshot name (without .ardour suffix).
2630  *  @return -2 if the load failed because we are not connected to the AudioEngine.
2631  */
2632 int
2633 ARDOUR_UI::load_session (const std::string& path, const std::string& snap_name, std::string mix_template)
2634 {
2635         Session *new_session;
2636         int unload_status;
2637         int retval = -1;
2638
2639         session_loaded = false;
2640
2641         if (!check_audioengine()) {
2642                 return -2;
2643         }
2644
2645         unload_status = unload_session ();
2646
2647         if (unload_status < 0) {
2648                 goto out;
2649         } else if (unload_status > 0) {
2650                 retval = 0;
2651                 goto out;
2652         }
2653
2654         loading_message (string_compose (_("Please wait while %1 loads your session"), PROGRAM_NAME));
2655
2656         try {
2657                 new_session = new Session (*engine, path, snap_name, 0, mix_template);
2658         }
2659
2660         /* this one is special */
2661
2662         catch (AudioEngine::PortRegistrationFailure& err) {
2663
2664                 MessageDialog msg (err.what(),
2665                                    true,
2666                                    Gtk::MESSAGE_INFO,
2667                                    Gtk::BUTTONS_CLOSE);
2668
2669                 msg.set_title (_("Port Registration Error"));
2670                 msg.set_secondary_text (_("Click the Close button to try again."));
2671                 msg.set_position (Gtk::WIN_POS_CENTER);
2672                 pop_back_splash (msg);
2673                 msg.present ();
2674
2675                 int response = msg.run ();
2676
2677                 msg.hide ();
2678
2679                 switch (response) {
2680                 case RESPONSE_CANCEL:
2681                         exit (1);
2682                 default:
2683                         break;
2684                 }
2685                 goto out;
2686         }
2687
2688         catch (...) {
2689
2690                 MessageDialog msg (string_compose(
2691                                            _("Session \"%1 (snapshot %2)\" did not load successfully"),
2692                                            path, snap_name),
2693                                    true,
2694                                    Gtk::MESSAGE_INFO,
2695                                    BUTTONS_OK);
2696
2697                 msg.set_title (_("Loading Error"));
2698                 msg.set_secondary_text (_("Click the Refresh button to try again."));
2699                 msg.add_button (Stock::REFRESH, 1);
2700                 msg.set_position (Gtk::WIN_POS_CENTER);
2701                 pop_back_splash (msg);
2702                 msg.present ();
2703
2704                 int response = msg.run ();
2705
2706                 switch (response) {
2707                 case 1:
2708                         break;
2709                 default:
2710                         exit (1);
2711                 }
2712
2713                 msg.hide ();
2714
2715                 goto out;
2716         }
2717
2718         {
2719                 list<string> const u = new_session->unknown_processors ();
2720                 if (!u.empty()) {
2721                         MissingPluginDialog d (_session, u);
2722                         d.run ();
2723                 }
2724         }
2725
2726         /* Now the session been created, add the transport controls */
2727         new_session->add_controllable(roll_controllable);
2728         new_session->add_controllable(stop_controllable);
2729         new_session->add_controllable(goto_start_controllable);
2730         new_session->add_controllable(goto_end_controllable);
2731         new_session->add_controllable(auto_loop_controllable);
2732         new_session->add_controllable(play_selection_controllable);
2733         new_session->add_controllable(rec_controllable);
2734
2735         set_session (new_session);
2736
2737         session_loaded = true;
2738
2739         goto_editor_window ();
2740
2741         if (_session) {
2742                 _session->set_clean ();
2743         }
2744
2745         flush_pending ();
2746         retval = 0;
2747
2748   out:
2749         return retval;
2750 }
2751
2752 int
2753 ARDOUR_UI::build_session (const std::string& path, const std::string& snap_name, BusProfile& bus_profile)
2754 {
2755         Session *new_session;
2756         int x;
2757
2758         if (!check_audioengine()) {
2759                 return -1;
2760         }
2761
2762         session_loaded = false;
2763
2764         x = unload_session ();
2765
2766         if (x < 0) {
2767                 return -1;
2768         } else if (x > 0) {
2769                 return 0;
2770         }
2771
2772         _session_is_new = true;
2773
2774         try {
2775                 new_session = new Session (*engine, path, snap_name, &bus_profile);
2776         }
2777
2778         catch (...) {
2779
2780                 MessageDialog msg (string_compose(_("Could not create session in \"%1\""), path));
2781                 pop_back_splash (msg);
2782                 msg.run ();
2783                 return -1;
2784         }
2785
2786         /* Give the new session the default GUI state, if such things exist */
2787
2788         XMLNode* n;
2789         n = Config->instant_xml (X_("Editor"));
2790         if (n) {
2791                 new_session->add_instant_xml (*n, false);
2792         }
2793         n = Config->instant_xml (X_("Mixer"));
2794         if (n) {
2795                 new_session->add_instant_xml (*n, false);
2796         }
2797
2798         /* Put the playhead at 0 and scroll fully left */
2799         n = new_session->instant_xml (X_("Editor"));
2800         if (n) {
2801                 n->add_property (X_("playhead"), X_("0"));
2802                 n->add_property (X_("left-frame"), X_("0"));
2803         }
2804
2805         set_session (new_session);
2806
2807         session_loaded = true;
2808
2809         new_session->save_state(new_session->name());
2810
2811         return 0;
2812 }
2813
2814 void
2815 ARDOUR_UI::launch_chat ()
2816 {
2817 #ifdef __APPLE__
2818         open_uri("http://webchat.freenode.net/?channels=ardour-osx");
2819 #else
2820         open_uri("http://webchat.freenode.net/?channels=ardour");
2821 #endif
2822 }
2823
2824 void
2825 ARDOUR_UI::show_about ()
2826 {
2827         if (about == 0) {
2828                 about = new About;
2829                 about->signal_response().connect (sigc::mem_fun (*this, &ARDOUR_UI::about_signal_response));
2830         }
2831
2832         about->set_transient_for(*editor);
2833         about->show_all ();
2834 }
2835
2836 void
2837 ARDOUR_UI::launch_manual ()
2838 {
2839         PBD::open_uri("http://ardour.org/flossmanual");
2840 }
2841
2842 void
2843 ARDOUR_UI::launch_reference ()
2844 {
2845         PBD::open_uri ("http://ardour.org/refmanual");
2846 }
2847
2848 void
2849 ARDOUR_UI::hide_about ()
2850 {
2851         if (about) {
2852                 about->get_window()->set_cursor ();
2853                 about->hide ();
2854         }
2855 }
2856
2857 void
2858 ARDOUR_UI::about_signal_response (int /*response*/)
2859 {
2860         hide_about();
2861 }
2862
2863 void
2864 ARDOUR_UI::loading_message (const std::string& msg)
2865 {
2866         if (ARDOUR_COMMAND_LINE::no_splash) {
2867                 return;
2868         }
2869
2870         if (!splash) {
2871                 show_splash ();
2872         }
2873
2874         splash->message (msg);
2875 }
2876
2877 void
2878 ARDOUR_UI::show_splash ()
2879 {
2880         if (splash == 0) {
2881                 try {
2882                         splash = new Splash;
2883                 } catch (...) {
2884                         return;
2885                 }
2886         }
2887
2888         splash->display ();
2889 }
2890
2891 void
2892 ARDOUR_UI::hide_splash ()
2893 {
2894         delete splash;
2895         splash = 0;
2896 }
2897
2898 void
2899 ARDOUR_UI::display_cleanup_results (ARDOUR::CleanupReport& rep, const gchar* list_title, const bool msg_delete)
2900 {
2901         size_t removed;
2902
2903         removed = rep.paths.size();
2904
2905         if (removed == 0) {
2906                 MessageDialog msgd (*editor,
2907                                     _("No files were ready for clean-up"),
2908                                     true,
2909                                     Gtk::MESSAGE_INFO,
2910                                     Gtk::BUTTONS_OK);
2911                 msgd.set_title (_("Clean-up"));
2912                 msgd.set_secondary_text (_("If this seems suprising, \n\
2913 check for any existing snapshots.\n\
2914 These may still include regions that\n\
2915 require some unused files to continue to exist."));
2916
2917                 msgd.run ();
2918                 return;
2919         }
2920
2921         ArdourDialog results (_("Clean-up"), true, false);
2922
2923         struct CleanupResultsModelColumns : public Gtk::TreeModel::ColumnRecord {
2924             CleanupResultsModelColumns() {
2925                     add (visible_name);
2926                     add (fullpath);
2927             }
2928             Gtk::TreeModelColumn<std::string> visible_name;
2929             Gtk::TreeModelColumn<std::string> fullpath;
2930         };
2931
2932
2933         CleanupResultsModelColumns results_columns;
2934         Glib::RefPtr<Gtk::ListStore> results_model;
2935         Gtk::TreeView results_display;
2936
2937         results_model = ListStore::create (results_columns);
2938         results_display.set_model (results_model);
2939         results_display.append_column (list_title, results_columns.visible_name);
2940
2941         results_display.set_name ("CleanupResultsList");
2942         results_display.set_headers_visible (true);
2943         results_display.set_headers_clickable (false);
2944         results_display.set_reorderable (false);
2945
2946         Gtk::ScrolledWindow list_scroller;
2947         Gtk::Label txt;
2948         Gtk::VBox dvbox;
2949         Gtk::HBox dhbox;  // the hbox for the image and text
2950         Gtk::HBox ddhbox; // the hbox we eventually pack into the dialog's vbox
2951         Gtk::Image* dimage = manage (new Gtk::Image(Stock::DIALOG_INFO,  Gtk::ICON_SIZE_DIALOG));
2952
2953         dimage->set_alignment(ALIGN_LEFT, ALIGN_TOP);
2954
2955         const string dead_directory = _session->session_directory().dead_path();
2956
2957         /* subst:
2958            %1 - number of files removed
2959            %2 - location of "dead"
2960            %3 - size of files affected
2961            %4 - prefix for "bytes" to produce sensible results (e.g. mega, kilo, giga)
2962         */
2963
2964         const char* bprefix;
2965         double space_adjusted = 0;
2966
2967         if (rep.space < 1000) {
2968                 bprefix = X_("");
2969                 space_adjusted = rep.space;
2970         } else if (rep.space < 1000000) {
2971                 bprefix = _("kilo");
2972                 space_adjusted = truncf((float)rep.space / 1000.0);
2973         } else if (rep.space < 1000000 * 1000) {
2974                 bprefix = _("mega");
2975                 space_adjusted = truncf((float)rep.space / (1000.0 * 1000.0));
2976         } else {
2977                 bprefix = _("giga");
2978                 space_adjusted = truncf((float)rep.space / (1000.0 * 1000 * 1000.0));
2979         }
2980
2981         if (msg_delete) {
2982                 txt.set_markup (string_compose (P_("\
2983 The following file was deleted from %2,\n\
2984 releasing %3 %4bytes of disk space", "\
2985 The following %1 files were deleted from %2,\n\
2986 releasing %3 %4bytes of disk space", removed),
2987                                         removed, Glib::Markup::escape_text (dead_directory), space_adjusted, bprefix, PROGRAM_NAME));
2988         } else {
2989                 txt.set_markup (string_compose (P_("\
2990 The following file was not in use and \n\
2991 has been moved to: %2\n\n\
2992 After a restart of %5\n\n\
2993 <span face=\"mono\">Session -> Clean-up -> Flush Wastebasket</span>\n\n\
2994 will release an additional %3 %4bytes of disk space.\n", "\
2995 The following %1 files were not in use and \n\
2996 have been moved to: %2\n\n\
2997 After a restart of %5\n\n\
2998 <span face=\"mono\">Session -> Clean-up -> Flush Wastebasket</span>\n\n\
2999 will release an additional %3 %4bytes of disk space.\n", removed),
3000                                         removed, Glib::Markup::escape_text (dead_directory), space_adjusted, bprefix, PROGRAM_NAME));
3001         }
3002
3003         dhbox.pack_start (*dimage, true, false, 5);
3004         dhbox.pack_start (txt, true, false, 5);
3005
3006         for (vector<string>::iterator i = rep.paths.begin(); i != rep.paths.end(); ++i) {
3007                 TreeModel::Row row = *(results_model->append());
3008                 row[results_columns.visible_name] = *i;
3009                 row[results_columns.fullpath] = *i;
3010         }
3011
3012         list_scroller.add (results_display);
3013         list_scroller.set_size_request (-1, 150);
3014         list_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
3015
3016         dvbox.pack_start (dhbox, true, false, 5);
3017         dvbox.pack_start (list_scroller, true, false, 5);
3018         ddhbox.pack_start (dvbox, true, false, 5);
3019
3020         results.get_vbox()->pack_start (ddhbox, true, false, 5);
3021         results.add_button (Stock::CLOSE, RESPONSE_CLOSE);
3022         results.set_default_response (RESPONSE_CLOSE);
3023         results.set_position (Gtk::WIN_POS_MOUSE);
3024
3025         results_display.show();
3026         list_scroller.show();
3027         txt.show();
3028         dvbox.show();
3029         dhbox.show();
3030         ddhbox.show();
3031         dimage->show();
3032
3033         //results.get_vbox()->show();
3034         results.set_resizable (false);
3035
3036         results.run ();
3037
3038 }
3039
3040 void
3041 ARDOUR_UI::cleanup ()
3042 {
3043         if (_session == 0) {
3044                 /* shouldn't happen: menu item is insensitive */
3045                 return;
3046         }
3047
3048
3049         MessageDialog checker (_("Are you sure you want to clean-up?"),
3050                                 true,
3051                                 Gtk::MESSAGE_QUESTION,
3052                                 Gtk::BUTTONS_NONE);
3053
3054         checker.set_title (_("Clean-up"));
3055
3056         checker.set_secondary_text(_("Clean-up is a destructive operation.\n\
3057 ALL undo/redo information will be lost if you clean-up.\n\
3058 Clean-up will move all unused files to a \"dead\" location."));
3059
3060         checker.add_button (Stock::CANCEL, RESPONSE_CANCEL);
3061         checker.add_button (_("Clean-up"), RESPONSE_ACCEPT);
3062         checker.set_default_response (RESPONSE_CANCEL);
3063
3064         checker.set_name (_("CleanupDialog"));
3065         checker.set_wmclass (X_("ardour_cleanup"), PROGRAM_NAME);
3066         checker.set_position (Gtk::WIN_POS_MOUSE);
3067
3068         switch (checker.run()) {
3069         case RESPONSE_ACCEPT:
3070                 break;
3071         default:
3072                 return;
3073         }
3074
3075         ARDOUR::CleanupReport rep;
3076
3077         editor->prepare_for_cleanup ();
3078
3079         /* do not allow flush until a session is reloaded */
3080
3081         Glib::RefPtr<Action> act = ActionManager::get_action (X_("Main"), X_("FlushWastebasket"));
3082         if (act) {
3083                 act->set_sensitive (false);
3084         }
3085
3086         if (_session->cleanup_sources (rep)) {
3087                 editor->finish_cleanup ();
3088                 return;
3089         }
3090
3091         editor->finish_cleanup ();
3092
3093         checker.hide();
3094         display_cleanup_results (rep, _("Cleaned Files"), false);
3095 }
3096
3097 void
3098 ARDOUR_UI::flush_trash ()
3099 {
3100         if (_session == 0) {
3101                 /* shouldn't happen: menu item is insensitive */
3102                 return;
3103         }
3104
3105         ARDOUR::CleanupReport rep;
3106
3107         if (_session->cleanup_trash_sources (rep)) {
3108                 return;
3109         }
3110
3111         display_cleanup_results (rep, _("deleted file"), true);
3112 }
3113
3114 void
3115 ARDOUR_UI::add_route (Gtk::Window* float_window)
3116 {
3117         int count;
3118
3119         if (!_session) {
3120                 return;
3121         }
3122
3123         if (add_route_dialog == 0) {
3124                 add_route_dialog = new AddRouteDialog (_session);
3125                 add_route_dialog->set_position (WIN_POS_MOUSE);
3126                 if (float_window) {
3127                         add_route_dialog->set_transient_for (*float_window);
3128                 }
3129         }
3130
3131         if (add_route_dialog->is_visible()) {
3132                 /* we're already doing this */
3133                 return;
3134         }
3135
3136         ResponseType r = (ResponseType) add_route_dialog->run ();
3137
3138         add_route_dialog->hide();
3139
3140         switch (r) {
3141                 case RESPONSE_ACCEPT:
3142                         break;
3143                 default:
3144                         return;
3145                         break;
3146         }
3147
3148         if ((count = add_route_dialog->count()) <= 0) {
3149                 return;
3150         }
3151
3152         PBD::ScopedConnection idle_connection;
3153
3154         if (count > 8) {
3155                 ARDOUR::GUIIdle.connect (idle_connection, MISSING_INVALIDATOR, boost::bind (&Gtkmm2ext::UI::flush_pending, this), gui_context());
3156         }
3157
3158         string template_path = add_route_dialog->track_template();
3159
3160         if (!template_path.empty()) {
3161                 _session->new_route_from_template (count, template_path);
3162                 return;
3163         }
3164
3165         ChanCount input_chan= add_route_dialog->channels ();
3166         ChanCount output_chan;
3167         string name_template = add_route_dialog->name_template ();
3168         PluginInfoPtr instrument = add_route_dialog->requested_instrument ();
3169         RouteGroup* route_group = add_route_dialog->route_group ();
3170         AutoConnectOption oac = Config->get_output_auto_connect();
3171
3172         if (oac & AutoConnectMaster) {
3173                 output_chan.set (DataType::AUDIO, (_session->master_out() ? _session->master_out()->n_inputs().n_audio() : input_chan.n_audio()));
3174                 output_chan.set (DataType::MIDI, 0);
3175         } else {
3176                 output_chan = input_chan;
3177         }
3178
3179         /* XXX do something with name template */
3180
3181         switch (add_route_dialog->type_wanted()) {
3182         case AddRouteDialog::AudioTrack:
3183                 session_add_audio_track (input_chan.n_audio(), output_chan.n_audio(), add_route_dialog->mode(), route_group, count, name_template);
3184                 break;
3185         case AddRouteDialog::MidiTrack:
3186                 session_add_midi_track (route_group, count, name_template, instrument);
3187                 break;
3188         case AddRouteDialog::MixedTrack:
3189                 session_add_mixed_track (input_chan, output_chan, route_group, count, name_template, instrument);
3190                 break;
3191         case AddRouteDialog::AudioBus:
3192                 session_add_audio_bus (input_chan.n_audio(), output_chan.n_audio(), route_group, count, name_template);
3193                 break;
3194         }
3195
3196         /* idle connection will end at scope end */
3197 }
3198
3199 XMLNode*
3200 ARDOUR_UI::mixer_settings () const
3201 {
3202         XMLNode* node = 0;
3203
3204         if (_session) {
3205                 node = _session->instant_xml(X_("Mixer"));
3206         } else {
3207                 node = Config->instant_xml(X_("Mixer"));
3208         }
3209
3210         if (!node) {
3211                 node = new XMLNode (X_("Mixer"));
3212         }
3213
3214         return node;
3215 }
3216
3217 XMLNode*
3218 ARDOUR_UI::editor_settings () const
3219 {
3220         XMLNode* node = 0;
3221
3222         if (_session) {
3223                 node = _session->instant_xml(X_("Editor"));
3224         } else {
3225                 node = Config->instant_xml(X_("Editor"));
3226         }
3227
3228         if (!node) {
3229                 if (getenv("ARDOUR_INSTANT_XML_PATH")) {
3230                         node = Config->instant_xml(getenv("ARDOUR_INSTANT_XML_PATH"));
3231                 }
3232         }
3233
3234         if (!node) {
3235                 node = new XMLNode (X_("Editor"));
3236         }
3237
3238         return node;
3239 }
3240
3241 XMLNode*
3242 ARDOUR_UI::keyboard_settings () const
3243 {
3244         XMLNode* node = 0;
3245
3246         node = Config->extra_xml(X_("Keyboard"));
3247
3248         if (!node) {
3249                 node = new XMLNode (X_("Keyboard"));
3250         }
3251
3252         return node;
3253 }
3254
3255 void
3256 ARDOUR_UI::create_xrun_marker (framepos_t where)
3257 {
3258         editor->mouse_add_new_marker (where, false, true);
3259 }
3260
3261 void
3262 ARDOUR_UI::halt_on_xrun_message ()
3263 {
3264         MessageDialog msg (*editor,
3265                            _("Recording was stopped because your system could not keep up."));
3266         msg.run ();
3267 }
3268
3269 void
3270 ARDOUR_UI::xrun_handler (framepos_t where)
3271 {
3272         if (!_session) {
3273                 return;
3274         }
3275
3276         ENSURE_GUI_THREAD (*this, &ARDOUR_UI::xrun_handler, where)
3277
3278         if (_session && Config->get_create_xrun_marker() && _session->actively_recording()) {
3279                 create_xrun_marker(where);
3280         }
3281
3282         if (_session && Config->get_stop_recording_on_xrun() && _session->actively_recording()) {
3283                 halt_on_xrun_message ();
3284         }
3285 }
3286
3287 void
3288 ARDOUR_UI::disk_overrun_handler ()
3289 {
3290         ENSURE_GUI_THREAD (*this, &ARDOUR_UI::disk_overrun_handler)
3291
3292         if (!have_disk_speed_dialog_displayed) {
3293                 have_disk_speed_dialog_displayed = true;
3294                 MessageDialog* msg = new MessageDialog (*editor, string_compose (_("\
3295 The disk system on your computer\n\
3296 was not able to keep up with %1.\n\
3297 \n\
3298 Specifically, it failed to write data to disk\n\
3299 quickly enough to keep up with recording.\n"), PROGRAM_NAME));
3300                 msg->signal_response().connect (sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::disk_speed_dialog_gone), msg));
3301                 msg->show ();
3302         }
3303 }
3304
3305 void
3306 ARDOUR_UI::disk_underrun_handler ()
3307 {
3308         ENSURE_GUI_THREAD (*this, &ARDOUR_UI::disk_underrun_handler)
3309
3310         if (!have_disk_speed_dialog_displayed) {
3311                 have_disk_speed_dialog_displayed = true;
3312                 MessageDialog* msg = new MessageDialog (
3313                         *editor, string_compose (_("The disk system on your computer\n\
3314 was not able to keep up with %1.\n\
3315 \n\
3316 Specifically, it failed to read data from disk\n\
3317 quickly enough to keep up with playback.\n"), PROGRAM_NAME));
3318                 msg->signal_response().connect (sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::disk_speed_dialog_gone), msg));
3319                 msg->show ();
3320         }
3321 }
3322
3323 void
3324 ARDOUR_UI::disk_speed_dialog_gone (int /*ignored_response*/, MessageDialog* msg)
3325 {
3326         have_disk_speed_dialog_displayed = false;
3327         delete msg;
3328 }
3329
3330 void
3331 ARDOUR_UI::session_dialog (std::string msg)
3332 {
3333         ENSURE_GUI_THREAD (*this, &ARDOUR_UI::session_dialog, msg)
3334
3335         MessageDialog* d;
3336
3337         if (editor) {
3338                 d = new MessageDialog (*editor, msg, false, MESSAGE_INFO, BUTTONS_OK, true);
3339         } else {
3340                 d = new MessageDialog (msg, false, MESSAGE_INFO, BUTTONS_OK, true);
3341         }
3342
3343         d->show_all ();
3344         d->run ();
3345         delete d;
3346 }
3347
3348 int
3349 ARDOUR_UI::pending_state_dialog ()
3350 {
3351         HBox* hbox = new HBox();
3352         Image* image = new Image (Stock::DIALOG_QUESTION, ICON_SIZE_DIALOG);
3353         ArdourDialog dialog (_("Crash Recovery"), true);
3354         Label  message (string_compose (_("\
3355 This session appears to have been in the\n\
3356 middle of recording when %1 or\n\
3357 the computer was shutdown.\n\
3358 \n\
3359 %1 can recover any captured audio for\n\
3360 you, or it can ignore it. Please decide\n\
3361 what you would like to do.\n"), PROGRAM_NAME));
3362         image->set_alignment(ALIGN_CENTER, ALIGN_TOP);
3363         hbox->pack_start (*image, PACK_EXPAND_WIDGET, 12);
3364         hbox->pack_end (message, PACK_EXPAND_PADDING, 12);
3365         dialog.get_vbox()->pack_start(*hbox, PACK_EXPAND_PADDING, 6);
3366         dialog.add_button (_("Ignore crash data"), RESPONSE_REJECT);
3367         dialog.add_button (_("Recover from crash"), RESPONSE_ACCEPT);
3368         dialog.set_default_response (RESPONSE_ACCEPT);
3369         dialog.set_position (WIN_POS_CENTER);
3370         message.show();
3371         image->show();
3372         hbox->show();
3373
3374         switch (dialog.run ()) {
3375         case RESPONSE_ACCEPT:
3376                 return 1;
3377         default:
3378                 return 0;
3379         }
3380 }
3381
3382 int
3383 ARDOUR_UI::sr_mismatch_dialog (framecnt_t desired, framecnt_t actual)
3384 {
3385         HBox* hbox = new HBox();
3386         Image* image = new Image (Stock::DIALOG_QUESTION, ICON_SIZE_DIALOG);
3387         ArdourDialog dialog (_("Sample Rate Mismatch"), true);
3388         Label  message (string_compose (_("\
3389 This session was created with a sample rate of %1 Hz, but\n\
3390 %2 is currently running at %3 Hz.  If you load this session,\n\
3391 audio may be played at the wrong sample rate.\n"), desired, PROGRAM_NAME, actual));
3392
3393         image->set_alignment(ALIGN_CENTER, ALIGN_TOP);
3394         hbox->pack_start (*image, PACK_EXPAND_WIDGET, 12);
3395         hbox->pack_end (message, PACK_EXPAND_PADDING, 12);
3396         dialog.get_vbox()->pack_start(*hbox, PACK_EXPAND_PADDING, 6);
3397         dialog.add_button (_("Do not load session"), RESPONSE_REJECT);
3398         dialog.add_button (_("Load session anyway"), RESPONSE_ACCEPT);
3399         dialog.set_default_response (RESPONSE_ACCEPT);
3400         dialog.set_position (WIN_POS_CENTER);
3401         message.show();
3402         image->show();
3403         hbox->show();
3404
3405         switch (dialog.run ()) {
3406         case RESPONSE_ACCEPT:
3407                 return 0;
3408         default:
3409                 return 1;
3410         }
3411 }
3412
3413
3414 void
3415 ARDOUR_UI::disconnect_from_jack ()
3416 {
3417         if (engine) {
3418                 if (engine->disconnect_from_jack ()) {
3419                         MessageDialog msg (*editor, _("Could not disconnect from JACK"));
3420                         msg.run ();
3421                 }
3422
3423                 update_sample_rate (0);
3424         }
3425 }
3426
3427 void
3428 ARDOUR_UI::reconnect_to_jack ()
3429 {
3430         if (engine) {
3431                 if (engine->reconnect_to_jack ()) {
3432                         MessageDialog msg (*editor,  _("Could not reconnect to JACK"));
3433                         msg.run ();
3434                 }
3435
3436                 update_sample_rate (0);
3437         }
3438 }
3439
3440 void
3441 ARDOUR_UI::use_config ()
3442 {
3443         XMLNode* node = Config->extra_xml (X_("TransportControllables"));
3444         if (node) {
3445                 set_transport_controllable_state (*node);
3446         }
3447 }
3448
3449 void
3450 ARDOUR_UI::update_transport_clocks (framepos_t pos)
3451 {
3452         if (Config->get_primary_clock_delta_edit_cursor()) {
3453                 primary_clock->set (pos, false, editor->get_preferred_edit_position());
3454         } else {
3455                 primary_clock->set (pos);
3456         }
3457
3458         if (Config->get_secondary_clock_delta_edit_cursor()) {
3459                 secondary_clock->set (pos, false, editor->get_preferred_edit_position());
3460         } else {
3461                 secondary_clock->set (pos);
3462         }
3463
3464         if (big_clock_window->get()) {
3465                 big_clock->set (pos);
3466         }
3467 }
3468
3469
3470 void
3471 ARDOUR_UI::step_edit_status_change (bool yn)
3472 {
3473         // XXX should really store pre-step edit status of things
3474         // we make insensitive
3475
3476         if (yn) {
3477                 rec_button.set_active_state (Gtkmm2ext::ImplicitActive);
3478                 rec_button.set_sensitive (false);
3479         } else {
3480                 rec_button.unset_active_state ();;
3481                 rec_button.set_sensitive (true);
3482         }
3483 }
3484
3485 void
3486 ARDOUR_UI::record_state_changed ()
3487 {
3488         ENSURE_GUI_THREAD (*this, &ARDOUR_UI::record_state_changed);
3489
3490         if (!_session || !big_clock_window->get()) {
3491                 /* why bother - the clock isn't visible */
3492                 return;
3493         }
3494
3495         if (_session->record_status () == Session::Recording && _session->have_rec_enabled_track ()) {
3496                 big_clock->set_active (true);
3497         } else {
3498                 big_clock->set_active (false);
3499         }
3500 }
3501
3502 bool
3503 ARDOUR_UI::first_idle ()
3504 {
3505         if (_session) {
3506                 _session->allow_auto_play (true);
3507         }
3508
3509         if (editor) {
3510                 editor->first_idle();
3511         }
3512
3513         Keyboard::set_can_save_keybindings (true);
3514         return false;
3515 }
3516
3517 void
3518 ARDOUR_UI::store_clock_modes ()
3519 {
3520         XMLNode* node = new XMLNode(X_("ClockModes"));
3521
3522         for (vector<AudioClock*>::iterator x = AudioClock::clocks.begin(); x != AudioClock::clocks.end(); ++x) {
3523                 XMLNode* child = new XMLNode (X_("Clock"));
3524                 
3525                 child->add_property (X_("name"), (*x)->name());
3526                 child->add_property (X_("mode"), enum_2_string ((*x)->mode()));
3527                 child->add_property (X_("on"), ((*x)->off() ? X_("no") : X_("yes")));
3528
3529                 node->add_child_nocopy (*child);
3530         }
3531
3532         _session->add_extra_xml (*node);
3533         _session->set_dirty ();
3534 }
3535
3536 ARDOUR_UI::TransportControllable::TransportControllable (std::string name, ARDOUR_UI& u, ToggleType tp)
3537         : Controllable (name), ui (u), type(tp)
3538 {
3539
3540 }
3541
3542 void
3543 ARDOUR_UI::TransportControllable::set_value (double val)
3544 {
3545         if (val < 0.5) {
3546                 /* do nothing: these are radio-style actions */
3547                 return;
3548         }
3549
3550         const char *action = 0;
3551
3552         switch (type) {
3553         case Roll:
3554                 action = X_("Roll");
3555                 break;
3556         case Stop:
3557                 action = X_("Stop");
3558                 break;
3559         case GotoStart:
3560                 action = X_("GotoStart");
3561                 break;
3562         case GotoEnd:
3563                 action = X_("GotoEnd");
3564                 break;
3565         case AutoLoop:
3566                 action = X_("Loop");
3567                 break;
3568         case PlaySelection:
3569                 action = X_("PlaySelection");
3570                 break;
3571         case RecordEnable:
3572                 action = X_("Record");
3573                 break;
3574         default:
3575                 break;
3576         }
3577
3578         if (action == 0) {
3579                 return;
3580         }
3581
3582         Glib::RefPtr<Action> act = ActionManager::get_action ("Transport", action);
3583
3584         if (act) {
3585                 act->activate ();
3586         }
3587 }
3588
3589 double
3590 ARDOUR_UI::TransportControllable::get_value (void) const
3591 {
3592         float val = 0.0;
3593
3594         switch (type) {
3595         case Roll:
3596                 break;
3597         case Stop:
3598                 break;
3599         case GotoStart:
3600                 break;
3601         case GotoEnd:
3602                 break;
3603         case AutoLoop:
3604                 break;
3605         case PlaySelection:
3606                 break;
3607         case RecordEnable:
3608                 break;
3609         default:
3610                 break;
3611         }
3612
3613         return val;
3614 }
3615
3616 void
3617 ARDOUR_UI::setup_profile ()
3618 {
3619         if (gdk_screen_width() < 1200 || getenv ("ARDOUR_NARROW_SCREEN")) {
3620                 Profile->set_small_screen ();
3621         }
3622
3623         if (getenv ("ARDOUR_SAE")) {
3624                 Profile->set_sae ();
3625                 Profile->set_single_package ();
3626         }
3627 }
3628
3629 void
3630 ARDOUR_UI::toggle_translations ()
3631 {
3632         using namespace Glib;
3633
3634         RefPtr<Action> act = ActionManager::get_action (X_("Main"), X_("EnableTranslation"));
3635         if (act) {
3636                 RefPtr<ToggleAction> ract = RefPtr<ToggleAction>::cast_dynamic (act);
3637                 if (ract) {
3638
3639                         string i18n_killer = ARDOUR::translation_kill_path();
3640
3641                         bool already_enabled = !ARDOUR::translations_are_disabled ();
3642
3643                         if (ract->get_active ()) {
3644                                 /* we don't care about errors */
3645                                 int fd = ::open (i18n_killer.c_str(), O_RDONLY|O_CREAT, 0644);
3646                                 close (fd);
3647                         } else {
3648                                 /* we don't care about errors */
3649                                 unlink (i18n_killer.c_str());
3650                         }
3651
3652                         if (already_enabled != ract->get_active()) {
3653                                 MessageDialog win (already_enabled ? _("Translations disabled") : _("Translations enabled"),
3654                                                    false,
3655                                                    Gtk::MESSAGE_WARNING,
3656                                                    Gtk::BUTTONS_OK);
3657                                 win.set_secondary_text (string_compose (_("You must restart %1 for this to take effect."), PROGRAM_NAME));
3658                                 win.set_position (Gtk::WIN_POS_CENTER);
3659                                 win.present ();
3660                                 win.run ();
3661                         }
3662                 }
3663         }
3664 }
3665
3666 /** Add a window proxy to our list, so that its state will be saved.
3667  *  This call also causes the window to be created and opened if its
3668  *  state was saved as `visible'.
3669  */
3670 void
3671 ARDOUR_UI::add_window_proxy (WindowProxyBase* p)
3672 {
3673         _window_proxies.push_back (p);
3674         p->maybe_show ();
3675 }
3676
3677 /** Remove a window proxy from our list.  Must be called if a WindowProxy
3678  *  is deleted, to prevent hanging pointers.
3679  */
3680 void
3681 ARDOUR_UI::remove_window_proxy (WindowProxyBase* p)
3682 {
3683         _window_proxies.remove (p);
3684 }
3685
3686 int
3687 ARDOUR_UI::missing_file (Session*s, std::string str, DataType type)
3688 {
3689         MissingFileDialog dialog (s, str, type);
3690
3691         dialog.show ();
3692         dialog.present ();
3693
3694         int result = dialog.run ();
3695         dialog.hide ();
3696
3697         switch (result) {
3698         case RESPONSE_OK:
3699                 break;
3700         default:
3701                 return 1; // quit entire session load
3702         }
3703
3704         result = dialog.get_action ();
3705
3706         return result;
3707 }
3708
3709 int
3710 ARDOUR_UI::ambiguous_file (std::string file, std::string /*path*/, std::vector<std::string> hits)
3711 {
3712         AmbiguousFileDialog dialog (file, hits);
3713
3714         dialog.show ();
3715         dialog.present ();
3716
3717         dialog.run ();
3718         return dialog.get_which ();
3719 }
3720
3721 /** Allocate our thread-local buffers */
3722 void
3723 ARDOUR_UI::get_process_buffers ()
3724 {
3725         _process_thread->get_buffers ();
3726 }
3727
3728 /** Drop our thread-local buffers */
3729 void
3730 ARDOUR_UI::drop_process_buffers ()
3731 {
3732         _process_thread->drop_buffers ();
3733 }
3734
3735 void
3736 ARDOUR_UI::feedback_detected ()
3737 {
3738         _feedback_exists = true;
3739 }
3740
3741 void
3742 ARDOUR_UI::successful_graph_sort ()
3743 {
3744         _feedback_exists = false;
3745 }
3746
3747 void
3748 ARDOUR_UI::midi_panic ()
3749 {
3750         if (_session) {
3751                 _session->midi_panic();
3752         }
3753 }