use XML state to store processors in mixer (strips) and fixup crash caused by missing...
[ardour.git] / gtk2_ardour / ardour_ui.h
1 /*
2     Copyright (C) 1999-2002 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 #ifndef __ardour_gui_h__
21 #define __ardour_gui_h__
22
23 /* need _BSD_SOURCE to get timersub macros */
24
25 #ifdef _BSD_SOURCE
26 #include <sys/time.h>
27 #else
28 #define _BSD_SOURCE
29 #include <sys/time.h>
30 #undef _BSD_SOURCE
31 #endif
32
33 #include <list>
34
35 #include <cmath>
36
37 #include <libgnomecanvasmm/canvas.h>
38
39 #include <pbd/xml++.h>
40 #include <gtkmm/box.h>
41 #include <gtkmm/frame.h>
42 #include <gtkmm/label.h>
43 #include <gtkmm/table.h>
44 #include <gtkmm/fixed.h>
45 #include <gtkmm/drawingarea.h>
46 #include <gtkmm/eventbox.h>
47 #include <gtkmm/menu.h>
48 #include <gtkmm/menuitem.h>
49 #include <gtkmm/button.h>
50 #include <gtkmm/togglebutton.h>
51 #include <gtkmm/treeview.h>
52 #include <gtkmm/menubar.h>
53 #include <gtkmm/adjustment.h>
54 #include <gtkmm2ext/gtk_ui.h>
55 #include <gtkmm2ext/click_box.h>
56 #include <gtkmm2ext/stateful_button.h>
57 #include <gtkmm2ext/bindable_button.h>
58 #include <ardour/ardour.h>
59 #include <ardour/session.h>
60 #include <ardour/configuration.h>
61
62 #include "audio_clock.h"
63 #include "ardour_dialog.h"
64 #include "editing.h"
65 #include "ui_config.h"
66
67 class AudioClock;
68 class PublicEditor;
69 class Keyboard;
70 class OptionEditor;
71 class Mixer_UI;
72 class ConnectionEditor;
73 class RouteParams_UI;
74 class About;
75 class AddRouteDialog;
76 class NewSessionDialog;
77 class LocationUI;
78 class ThemeManager;
79
80 namespace Gtkmm2ext {
81         class TearOff;
82 }
83
84 namespace ARDOUR {
85         class AudioEngine;
86         class Route;
87         class Port;
88         class IO;
89         class ControlProtocolInfo;
90 }
91
92 namespace ALSA {
93         class MultiChannelDevice;
94 }
95
96 #define FRAME_NAME "BaseFrame"
97
98 extern sigc::signal<void>  ColorsChanged;
99
100 class ARDOUR_UI : public Gtkmm2ext::UI
101 {
102   public:
103         ARDOUR_UI (int *argcp, char **argvp[]);
104         ~ARDOUR_UI();
105
106         void show ();
107         bool shown() { return shown_flag; }
108         
109         void show_splash ();
110         void hide_splash ();
111         
112         int load_session (const string & path, const string & snapshot, string* mix_template = 0);
113         bool session_loaded;
114         /// @return true if building the session was successful
115         bool build_session (const string & path, const string & snapshot, 
116                            uint32_t ctl_chns, 
117                            uint32_t master_chns,
118                            ARDOUR::AutoConnectOption input_connect,
119                            ARDOUR::AutoConnectOption output_connect,
120                            uint32_t nphysin,
121                            uint32_t nphysout,
122                            nframes_t initial_length);
123         bool session_is_new() const { return _session_is_new; }
124
125         ARDOUR::Session* the_session() { return session; }
126
127         bool new_session(std::string path = string());
128         gint cmdline_new_session (string path);
129         
130         /// @return true if session was successfully unloaded.
131         bool unload_session ();
132         void close_session(); 
133
134         int  save_state_canfail (string state_name = "");
135         void save_state (const string & state_name = "");
136         void restore_state (string state_name = "");
137
138         static double gain_to_slider_position (ARDOUR::gain_t g);
139         static ARDOUR::gain_t slider_position_to_gain (double pos);
140
141         static ARDOUR_UI *instance () { return theArdourUI; }
142         static UIConfiguration *config () { return ui_config; }
143
144         PublicEditor&     the_editor(){return *editor;}
145         Mixer_UI* the_mixer() { return mixer; }
146         
147         void toggle_location_window ();
148         void toggle_theme_manager ();
149         void toggle_big_clock_window ();
150         void toggle_connection_editor ();
151         void toggle_route_params_window ();
152         void toggle_editing_space();
153
154         Gtk::Tooltips& tooltips() { return _tooltips; }
155
156         static sigc::signal<void,bool> Blink;
157         static sigc::signal<void>      RapidScreenUpdate;
158         static sigc::signal<void>      SuperRapidScreenUpdate;
159         static sigc::signal<void,nframes_t, bool, nframes_t> Clock;
160
161         /* this is a helper function to centralize the (complex) logic for
162            blinking rec-enable buttons.
163         */
164
165         void rec_enable_button_blink (bool onoff, ARDOUR::AudioDiskstream *, Gtk::Widget *w);
166
167         void name_io_setup (ARDOUR::AudioEngine&, string&, ARDOUR::IO& io, bool in);
168
169         static gint hide_and_quit (GdkEventAny *ev, ArdourDialog *);
170
171         XMLNode* editor_settings() const;
172         XMLNode* mixer_settings () const;
173         XMLNode* keyboard_settings () const;
174
175         void save_ardour_state ();
176         gboolean configure_handler (GdkEventConfigure* conf);
177
178         void do_transport_locate (nframes_t position);
179         void halt_on_xrun_message ();
180
181         AudioClock primary_clock;
182         AudioClock secondary_clock;
183         AudioClock preroll_clock;
184         AudioClock postroll_clock;
185
186         void store_clock_modes ();
187         void restore_clock_modes ();
188
189         void add_route (Gtk::Window* float_window);
190         
191         void session_add_audio_track (int input_channels, int32_t output_channels, ARDOUR::TrackMode mode, uint32_t how_many) {
192                 session_add_audio_route (true, input_channels, output_channels, mode, how_many);
193         }
194
195         void session_add_audio_bus (int input_channels, int32_t output_channels, uint32_t how_many) {
196                 session_add_audio_route (false, input_channels, output_channels, ARDOUR::Normal, how_many);
197         }
198
199         void session_add_midi_track (uint32_t how_many) {
200                 session_add_midi_route (true, how_many);
201         }
202
203         /*void session_add_midi_bus () {
204                 session_add_midi_route (false);
205         }*/
206
207         void set_engine (ARDOUR::AudioEngine&);
208         gint start_engine ();
209
210         gint exit_on_main_window_close (GdkEventAny *);
211
212         void maximise_editing_space ();
213         void restore_editing_space ();
214
215         void set_native_file_header_format (ARDOUR::HeaderFormat sf);
216         void set_native_file_data_format (ARDOUR::SampleFormat sf);
217
218         void set_keybindings_path (std::string path);
219         void save_keybindings ();
220
221         void setup_profile ();
222         void setup_theme ();
223
224   protected:
225         friend class PublicEditor;
226
227         void toggle_clocking ();
228         void toggle_auto_play ();
229         void toggle_auto_input ();
230         void toggle_punch_in ();
231         void toggle_punch_out ();
232         void toggle_auto_return ();
233         void toggle_click ();
234
235         void toggle_session_auto_loop ();
236         
237         void toggle_options_window ();
238
239   private:
240         struct GlobalClickBox : public Gtk::VBox {
241             Gtkmm2ext::ClickBox  *box;
242             Gtk::Frame      frame;
243             Gtk::Label      label;
244             vector<string> &strings;
245             Gtk::Adjustment adjustment;
246
247             static void printer (char buf[32], Gtk::Adjustment &adj, void *arg);
248
249             GlobalClickBox (const string &str, vector<string> &vs)
250                     : strings (vs),
251                       adjustment (0, 0, vs.size() - 1, 1, 1, 0) {
252                     box = new Gtkmm2ext::ClickBox (&adjustment, "ClickButton");
253                     label.set_text (str);
254                     label.set_name ("GlobalButtonLabel");
255                     frame.add (*box);
256                     frame.set_shadow_type (Gtk::SHADOW_IN);
257                     pack_start (label);
258                     pack_start (frame);
259                     box->set_print_func (printer, this);
260                     box->set_wrap (true);
261             };
262         };
263
264         ARDOUR::AudioEngine                 *engine;
265         ARDOUR::Session                     *session;
266
267         bool check_audioengine();
268
269         Gtk::Tooltips          _tooltips;
270
271         void                     goto_editor_window ();
272         void                     goto_mixer_window ();
273         
274         Gtk::Table               adjuster_table;
275         Gtk::Frame               adjuster_frame;
276         Gtk::Fixed               adjuster_base;
277
278         GlobalClickBox     *online_control_button;
279         vector<string>      online_control_strings;
280
281         GlobalClickBox    *crossfade_time_button;
282         vector<string>     crossfade_time_strings;
283
284         Gtk::ToggleButton   preroll_button;
285         Gtk::ToggleButton   postroll_button;
286
287         Gtk::Table          transport_table;
288         Gtk::Table          option_table;
289
290         int  setup_windows ();
291         void setup_transport ();
292         void setup_clock ();
293
294         static ARDOUR_UI *theArdourUI;
295
296         void startup ();
297         void shutdown ();
298
299         void finish();
300         int  ask_about_saving_session (const string & why);
301         int  save_the_session;
302
303         /* periodic safety backup, to be precise */
304         gint autosave_session();
305         void update_autosave();
306         sigc::connection _autosave_connection;
307
308         void queue_transport_change ();
309         void map_transport_state ();
310         int32_t do_engine_start ();
311         
312         void engine_halted ();
313         void engine_stopped ();
314         void engine_running ();
315
316         void use_config ();
317
318         static gint _blink  (void *);
319         void blink ();
320         gint blink_timeout_tag;
321         bool blink_on;
322         void start_blinking ();
323         void stop_blinking ();
324
325         void about_signal_response(int response);
326
327   private:
328         Gtk::VBox     top_packer;
329
330         sigc::connection clock_signal_connection;
331         void         update_clocks ();
332         void         start_clocking ();
333         void         stop_clocking ();
334
335         void manage_window (Gtk::Window&);
336         
337         AudioClock   big_clock;
338         Gtk::Frame   big_clock_frame;
339         Gtk::Window* big_clock_window;
340
341         void update_transport_clocks (nframes_t pos);
342         void record_state_changed ();
343
344         /* Transport Control */
345
346         void detach_tearoff (Gtk::Box* parent, Gtk::Widget* contents);
347         void reattach_tearoff (Gtk::Box* parent, Gtk::Widget* contents, int32_t order);
348
349         Gtkmm2ext::TearOff*      transport_tearoff;
350         Gtk::Frame               transport_frame;
351         Gtk::HBox                transport_tearoff_hbox;
352         Gtk::HBox                transport_hbox;
353         Gtk::Fixed               transport_base;
354         Gtk::Fixed               transport_button_base;
355         Gtk::Frame               transport_button_frame;
356         Gtk::HBox                transport_button_hbox;
357         Gtk::VBox                transport_button_vbox;
358         Gtk::HBox                transport_option_button_hbox;
359         Gtk::VBox                transport_option_button_vbox;
360         Gtk::HBox                transport_clock_hbox;
361         Gtk::VBox                transport_clock_vbox;
362         Gtk::HBox                primary_clock_hbox;
363         Gtk::HBox                secondary_clock_hbox;
364
365
366         struct TransportControllable : public PBD::Controllable {
367             enum ToggleType {
368                     Roll = 0,
369                     Stop,
370                     RecordEnable,
371                     GotoStart,
372                     GotoEnd,
373                     AutoLoop,
374                     PlaySelection,
375                     ShuttleControl
376                     
377             };
378             
379             TransportControllable (std::string name, ARDOUR_UI&, ToggleType);
380             void set_value (float);
381             float get_value (void) const;
382             
383             void set_id (const std::string&);
384             
385             ARDOUR_UI& ui;
386             ToggleType type;
387         };
388
389         TransportControllable roll_controllable;
390         TransportControllable stop_controllable;
391         TransportControllable goto_start_controllable;
392         TransportControllable goto_end_controllable;
393         TransportControllable auto_loop_controllable;
394         TransportControllable play_selection_controllable;
395         TransportControllable rec_controllable;
396         TransportControllable shuttle_controllable;
397         BindingProxy shuttle_controller_binding_proxy;
398
399         void set_transport_controllable_state (const XMLNode&);
400         XMLNode& get_transport_controllable_state ();
401
402         BindableButton roll_button;
403         BindableButton stop_button;
404         BindableButton goto_start_button;
405         BindableButton goto_end_button;
406         BindableButton auto_loop_button;
407         BindableButton play_selection_button;
408         BindableButton rec_button;
409
410         Gtk::ComboBoxText sync_option_combo;
411
412         void sync_option_changed ();
413         void toggle_time_master ();
414         void toggle_video_sync ();
415
416         Gtk::DrawingArea  shuttle_box;
417         Gtk::EventBox     speed_display_box;
418         Gtk::Label        speed_display_label;
419         Gtk::Button       shuttle_units_button;
420         Gtk::ComboBoxText shuttle_style_button;
421         Gtk::Menu*        shuttle_unit_menu;
422         Gtk::Menu*        shuttle_style_menu;
423         float             shuttle_max_speed;
424         Gtk::Menu*        shuttle_context_menu;
425
426         void build_shuttle_context_menu ();
427         void show_shuttle_context_menu ();
428         void shuttle_style_changed();
429         void shuttle_unit_clicked ();
430         void set_shuttle_max_speed (float);
431         void update_speed_display ();
432         float last_speed_displayed;
433
434         gint shuttle_box_button_press (GdkEventButton*);
435         gint shuttle_box_button_release (GdkEventButton*);
436         gint shuttle_box_scroll (GdkEventScroll*);
437         gint shuttle_box_motion (GdkEventMotion*);
438         gint shuttle_box_expose (GdkEventExpose*);
439         gint mouse_shuttle (double x, bool force);
440         void use_shuttle_fract (bool force);
441         void set_shuttle_fract (double);
442
443         bool   shuttle_grabbed;
444         double shuttle_fract;
445
446         Gtkmm2ext::StatefulToggleButton punch_in_button;
447         Gtkmm2ext::StatefulToggleButton punch_out_button;
448         Gtkmm2ext::StatefulToggleButton auto_return_button;
449         Gtkmm2ext::StatefulToggleButton auto_play_button;
450         Gtkmm2ext::StatefulToggleButton auto_input_button;
451         Gtkmm2ext::StatefulToggleButton click_button;
452         Gtkmm2ext::StatefulToggleButton time_master_button;
453
454         Gtk::ToggleButton auditioning_alert_button;
455         Gtk::ToggleButton solo_alert_button;
456
457         Gtk::VBox alert_box;
458
459         void solo_blink (bool);
460         void audition_blink (bool);
461
462         void soloing_changed (bool);
463         void auditioning_changed (bool);
464         void _auditioning_changed (bool);
465
466         void solo_alert_toggle ();
467         void audition_alert_toggle ();
468
469         void primary_clock_value_changed ();
470         void secondary_clock_value_changed ();
471
472         /* called by Blink signal */
473
474         void transport_rec_enable_blink (bool onoff);
475
476         Gtk::Menu*        session_popup_menu;
477
478         struct RecentSessionModelColumns : public Gtk::TreeModel::ColumnRecord {
479             RecentSessionModelColumns() { 
480                     add (visible_name);
481                     add (fullpath);
482             }
483             Gtk::TreeModelColumn<Glib::ustring> visible_name;
484             Gtk::TreeModelColumn<Glib::ustring> fullpath;
485         };
486
487         RecentSessionModelColumns    recent_session_columns;
488         Gtk::TreeView                recent_session_display;
489         Glib::RefPtr<Gtk::TreeStore> recent_session_model;
490
491         ArdourDialog*     session_selector_window;
492         Gtk::FileChooserDialog* open_session_selector;
493         
494         void build_session_selector();
495         void redisplay_recent_sessions();
496         void recent_session_row_activated (const Gtk::TreePath& path, Gtk::TreeViewColumn* col);
497
498         struct RecentSessionsSorter {
499             bool operator() (std::pair<string,string> a, std::pair<string,string> b) const {
500                     return cmp_nocase(a.first, b.first) == -1;
501             }
502         };
503
504         /* menu bar and associated stuff */
505
506         Gtk::MenuBar* menu_bar;
507         Gtk::EventBox menu_bar_base;
508         Gtk::HBox     menu_hbox;
509
510         void build_menu_bar ();
511         void build_control_surface_menu ();
512
513         Gtk::Label   wall_clock_label;
514         Gtk::EventBox wall_clock_box;
515         gint update_wall_clock ();
516
517         Gtk::Label   disk_space_label;
518         Gtk::EventBox disk_space_box;
519         void update_disk_space ();
520
521         Gtk::Label   cpu_load_label;
522         Gtk::EventBox cpu_load_box;
523         void update_cpu_load ();
524
525         Gtk::Label   buffer_load_label;
526         Gtk::EventBox buffer_load_box;
527         void update_buffer_load ();
528
529         Gtk::Label   sample_rate_label;
530         Gtk::EventBox sample_rate_box;
531         void update_sample_rate (nframes_t);
532
533         gint every_second ();
534         gint every_point_one_seconds ();
535         gint every_point_zero_one_seconds ();
536
537         sigc::connection second_connection;
538         sigc::connection point_one_second_connection;
539         sigc::connection point_oh_five_second_connection;
540         sigc::connection point_zero_one_second_connection;
541
542         gint session_menu (GdkEventButton *);
543
544         NewSessionDialog* new_session_dialog;
545         
546         void open_session ();
547         void open_recent_session ();
548         void save_template ();
549
550         void session_add_audio_route (bool disk, int32_t input_channels, int32_t output_channels, ARDOUR::TrackMode mode, uint32_t how_many);
551         void session_add_midi_route (bool disk, uint32_t how_many);
552
553         void set_transport_sensitivity (bool);
554
555         void remove_last_capture ();
556
557         void transport_goto_zero ();
558         void transport_goto_start ();
559         void transport_goto_end ();
560         void transport_stop ();
561         void transport_stop_and_forget_capture ();
562         void transport_record ();
563         void transport_roll ();
564         void transport_play_selection(); 
565         void transport_forward (int option);
566         void transport_rewind (int option);
567         void transport_loop ();
568
569         void transport_rolling ();
570         void transport_rewinding ();
571         void transport_forwarding ();
572         void transport_stopped ();
573
574         bool _session_is_new;
575         void connect_to_session (ARDOUR::Session *);
576         void connect_dependents_to_session (ARDOUR::Session *);
577         void we_have_dependents ();
578         
579         std::string keybindings_path;
580         std::string user_keybindings_path;
581
582         void setup_keybindings ();
583         void setup_session_options ();
584         
585         guint32  last_key_press_time;
586
587         void snapshot_session ();
588
589         Mixer_UI   *mixer;
590         int         create_mixer ();
591         
592         PublicEditor     *editor;
593         int         create_editor ();
594
595         RouteParams_UI *route_params;
596         int             create_route_params ();
597
598         ConnectionEditor *connection_editor;
599         int               create_connection_editor ();
600
601         LocationUI *location_ui;
602         int         create_location_ui ();
603         void        handle_locations_change (ARDOUR::Location*);
604
605         static UIConfiguration *ui_config;
606         ThemeManager *theme_manager;
607
608         /* Options window */
609         
610         OptionEditor *option_editor;
611         
612         /* route dialog */
613
614         AddRouteDialog *add_route_dialog;
615         
616         /* Keyboard Handling */
617         
618         Keyboard* keyboard;
619
620         /* Keymap handling */
621
622         void install_actions ();
623
624         void toggle_record_enable (uint32_t);
625
626         uint32_t rec_enabled_streams;
627         void count_recenabled_streams (ARDOUR::Route&);
628
629         About* about;
630         bool shown_flag;
631         /* cleanup */
632
633         Gtk::MenuItem *cleanup_item;
634
635         void display_cleanup_results (ARDOUR::Session::cleanup_report& rep, const gchar* list_title, const string & msg);
636         void cleanup ();
637         void flush_trash ();
638
639         bool have_configure_timeout;
640         struct timeval last_configure_time;
641         gint configure_timeout ();
642
643         struct timeval last_peak_grab;
644         struct timeval last_shuttle_request;
645
646         bool have_disk_speed_dialog_displayed;
647         void disk_speed_dialog_gone (int ignored_response, Gtk::MessageDialog*);
648         void disk_overrun_handler ();
649         void disk_underrun_handler ();
650
651         int pending_state_dialog ();
652         
653         void disconnect_from_jack ();
654         void reconnect_to_jack ();
655         void set_jack_buffer_size (nframes_t);
656
657         Gtk::MenuItem* jack_disconnect_item;
658         Gtk::MenuItem* jack_reconnect_item;
659         Gtk::Menu*     jack_bufsize_menu;
660
661         int make_session_clean ();
662         bool filter_ardour_session_dirs (const Gtk::FileFilter::Info&);
663
664         Glib::RefPtr<Gtk::ActionGroup> common_actions;
665
666         void editor_realized ();
667
668         std::vector<std::string> positional_sync_strings;
669
670         void toggle_send_midi_feedback ();
671         void toggle_use_mmc ();
672         void toggle_send_mmc ();
673         void toggle_send_mtc ();
674
675         void toggle_use_osc ();
676
677         void toggle_denormal_protection ();
678
679         void set_input_auto_connect (ARDOUR::AutoConnectOption);
680         void set_output_auto_connect (ARDOUR::AutoConnectOption);
681         void set_solo_model (ARDOUR::SoloModel);
682         void set_monitor_model (ARDOUR::MonitorModel);
683         void set_remote_model (ARDOUR::RemoteModel);
684         void set_denormal_model (ARDOUR::DenormalModel);
685
686         void toggle_StopPluginsWithTransport();
687         void toggle_DoNotRunPluginsWhileRecording();
688         void toggle_VerifyRemoveLastCapture();
689         void toggle_PeriodicSafetyBackups();
690         void toggle_StopRecordingOnXrun();
691         void toggle_StopTransportAtEndOfSession();
692         void toggle_GainReduceFastTransport();
693         void toggle_LatchedSolo();
694         void toggle_ShowSoloMutes();
695         void toggle_LatchedRecordEnable ();
696         void toggle_RegionEquivalentsOverlap ();
697         void toggle_PrimaryClockDeltaEditCursor ();
698         void toggle_SecondaryClockDeltaEditCursor ();
699
700         void mtc_port_changed ();
701         void map_solo_model ();
702         void map_monitor_model ();
703         void map_denormal_model ();
704         void map_remote_model ();
705         void map_file_header_format ();
706         void map_file_data_format ();
707         void map_input_auto_connect ();
708         void map_output_auto_connect ();
709         void parameter_changed (const char*);
710
711         void set_meter_hold (ARDOUR::MeterHold);
712         void set_meter_falloff (ARDOUR::MeterFalloff);
713         void map_meter_hold ();
714         void map_meter_falloff ();
715
716         void toggle_control_protocol (ARDOUR::ControlProtocolInfo*);
717         void toggle_control_protocol_feedback (ARDOUR::ControlProtocolInfo*, const char* group_name, std::string action_name);
718
719         bool can_save_keybindings;
720         bool first_idle ();
721
722         void no_memory_warning ();
723         void check_memory_locking ();
724
725         bool ab_direction;
726         void disable_all_plugins ();
727         void ab_all_plugins ();
728 };
729
730 #endif /* __ardour_gui_h__ */
731