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