fix crash when copy'ing latent plugins
[ardour.git] / gtk2_ardour / engine_dialog.h
1 /*
2     Copyright (C) 2010 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 __gtk2_ardour_engine_dialog_h__
21 #define __gtk2_ardour_engine_dialog_h__
22
23 #include <map>
24 #include <vector>
25 #include <string>
26
27 #include <gtkmm/checkbutton.h>
28 #include <gtkmm/spinbutton.h>
29 #include <gtkmm/comboboxtext.h>
30 #include <gtkmm/table.h>
31 #include <gtkmm/expander.h>
32 #include <gtkmm/box.h>
33 #include <gtkmm/buttonbox.h>
34 #include <gtkmm/button.h>
35
36 #include "pbd/signals.h"
37
38 #include "ardour_dialog.h"
39 #include "ardour_button.h"
40
41 class EngineControl : public ArdourDialog, public PBD::ScopedConnectionList {
42   public:
43     EngineControl ();
44     ~EngineControl ();
45
46     static bool need_setup ();
47
48     XMLNode& get_state ();
49     bool set_state (const XMLNode&);
50
51     void set_desired_sample_rate (uint32_t);
52     bool try_autostart ();
53
54   private:
55     Gtk::Notebook notebook;
56
57     Gtk::Label engine_status;
58
59     /* core fields used by all backends */
60
61     Gtk::Table basic_packer;
62     Gtk::HBox basic_hbox;
63     Gtk::VBox basic_vbox;
64
65     Gtk::ComboBoxText backend_combo;
66     Gtk::ComboBoxText driver_combo;
67     Gtk::ComboBoxText device_combo;
68     Gtk::ComboBoxText input_device_combo;
69     Gtk::ComboBoxText output_device_combo;
70     Gtk::ComboBoxText sample_rate_combo;
71     Gtk::ComboBoxText midi_option_combo;
72     Gtk::ComboBoxText buffer_size_combo;
73     Gtk::Label        buffer_size_duration_label;
74     Gtk::ComboBoxText nperiods_combo;
75     Gtk::Adjustment input_latency_adjustment;
76     Gtk::SpinButton input_latency;
77     Gtk::Adjustment output_latency_adjustment;
78     Gtk::SpinButton output_latency;
79     Gtk::Adjustment input_channels_adjustment;
80     Gtk::SpinButton input_channels;
81     Gtk::Adjustment output_channels_adjustment;
82     Gtk::SpinButton output_channels;
83     Gtk::Adjustment ports_adjustment;
84     Gtk::SpinButton ports_spinner;
85
86     Gtk::Label      have_control_text;
87     ArdourButton    control_app_button;
88     ArdourButton    midi_devices_button;
89     ArdourButton    start_stop_button;
90     ArdourButton    update_devices_button;
91     ArdourButton    use_buffered_io_button;
92
93     Gtk::Button     connect_disconnect_button;
94
95     /* latency measurement */
96
97     Gtk::ComboBoxText lm_output_channel_combo;
98     Gtk::ComboBoxText lm_input_channel_combo;
99     Gtk::Label        lm_measure_label;
100     Gtk::Button       lm_measure_button;
101     Gtk::Button       lm_use_button;
102     Gtk::Button       lm_back_button;
103     ArdourButton      lm_button_audio;
104     Gtk::Label        lm_title;
105     Gtk::Label        lm_preamble;
106     Gtk::Label        lm_results;
107     Gtk::Table        lm_table;
108     Gtk::VBox         lm_vbox;
109     bool              have_lm_results;
110     bool              lm_running;
111
112     Gtk::Button* cancel_button;
113     Gtk::Button* ok_button;
114
115     /* MIDI Tab */
116
117     Gtk::VBox midi_vbox;
118     Gtk::Button midi_back_button;
119     Gtk::Table midi_device_table;
120
121     /* MIDI ... JACK */
122
123     Gtk::CheckButton aj_button;
124
125     uint32_t ignore_changes; // state save/load
126     uint32_t ignore_device_changes; // AudioEngine::DeviceListChanged
127     uint32_t _desired_sample_rate;
128     bool     started_at_least_once;
129     bool     queue_device_changed;
130
131     void driver_changed ();
132     void backend_changed ();
133     void sample_rate_changed ();
134     void buffer_size_changed ();
135     void nperiods_changed ();
136     void parameter_changed ();
137     void midi_option_changed ();
138
139     void setup_midi_tab_for_backend ();
140     void setup_midi_tab_for_jack ();
141     void refresh_midi_display (std::string focus = "");
142
143         void update_midi_options ();
144
145         std::string bufsize_as_string (uint32_t);
146         std::string nperiods_as_string (uint32_t);
147
148         std::vector<float> get_default_sample_rates ();
149         std::vector<uint32_t> get_default_buffer_sizes ();
150
151         std::vector<float> get_sample_rates_for_all_devices ();
152         std::vector<uint32_t> get_buffer_sizes_for_all_devices ();
153
154     float get_rate() const;
155     uint32_t get_buffer_size() const;
156     uint32_t get_nperiods() const;
157     uint32_t get_input_channels() const;
158     uint32_t get_output_channels() const;
159     uint32_t get_input_latency() const;
160     uint32_t get_output_latency() const;
161     std::string get_device_name() const;
162     std::string get_input_device_name() const;
163     std::string get_output_device_name() const;
164     std::string get_driver() const;
165     std::string get_backend() const;
166     std::string get_midi_option () const;
167     bool get_use_buffered_io () const;
168
169         std::string get_default_device (const std::string&,
170                                         const std::vector<std::string>&);
171
172     void device_changed ();
173     void input_device_changed ();
174     void output_device_changed ();
175     bool set_driver_popdown_strings ();
176     bool set_device_popdown_strings ();
177     bool set_input_device_popdown_strings ();
178     bool set_output_device_popdown_strings ();
179     void set_samplerate_popdown_strings ();
180     void set_buffersize_popdown_strings ();
181     void set_nperiods_popdown_strings ();
182     void list_devices ();
183     void show_buffer_duration ();
184
185     void configure_midi_devices ();
186
187     struct MidiDeviceSetting {
188         std::string name;
189         bool enabled;
190         uint32_t input_latency;
191         uint32_t output_latency;
192
193         MidiDeviceSetting (std::string n, bool en = true, uint32_t inl = 0, uint32_t oul = 0)
194             : name (n)
195             , enabled (en)
196             , input_latency (inl)
197             , output_latency (oul)
198         {}
199     };
200
201     typedef boost::shared_ptr<MidiDeviceSetting> MidiDeviceSettings;
202     bool _can_set_midi_latencies;
203     std::vector<MidiDeviceSettings> _midi_devices;
204
205     MidiDeviceSettings find_midi_device(std::string devicename) const {
206         for (std::vector<MidiDeviceSettings>::const_iterator p = _midi_devices.begin(); p != _midi_devices.end(); ++p) {
207             if ((*p)->name == devicename) {
208                 return *p;
209             }
210         }
211         return MidiDeviceSettings();
212     }
213
214     struct StateStruct {
215         std::string backend;
216         std::string driver;
217         std::string device;
218         std::string input_device;
219         std::string output_device;
220         float sample_rate;
221         uint32_t buffer_size;
222         uint32_t n_periods;
223         uint32_t input_latency;
224         uint32_t output_latency;
225         uint32_t input_channels;
226         uint32_t output_channels;
227         bool active;
228         bool use_buffered_io;
229         std::string midi_option;
230         std::vector<MidiDeviceSettings> midi_devices;
231         time_t lru;
232
233         StateStruct()
234                 : sample_rate (48000)
235                 , buffer_size (1024)
236                 , input_latency (0)
237                 , output_latency (0)
238                 , input_channels (0)
239                 , output_channels (0)
240                 , active (false)
241                 , use_buffered_io (false)
242                 , lru (0) {}
243
244     };
245
246     typedef boost::shared_ptr<StateStruct> State;
247     typedef std::list<State> StateList;
248     static bool state_sort_cmp (const State &a, const State &b);
249
250     StateList states;
251
252     State get_matching_state (const std::string& backend);
253     State get_matching_state (const std::string& backend,
254                                const std::string& driver,
255                                const std::string& device);
256     State get_matching_state (const std::string& backend,
257                                const std::string& driver,
258                                const std::string& input_device,
259                                const std::string& output_device);
260     State get_saved_state_for_currently_displayed_backend_and_device ();
261     void maybe_display_saved_state ();
262     State save_state ();
263     void store_state (State);
264     bool equivalent_states (const State&, const State&);
265
266         bool set_current_state (const State& state);
267         void set_default_state ();
268
269     bool  _have_control;
270
271     static bool print_channel_count (Gtk::SpinButton*);
272
273     void build_notebook ();
274     void build_full_control_notebook ();
275     void build_no_control_notebook ();
276
277         void connect_changed_signals ();
278         void block_changed_signals ();
279         void unblock_changed_signals ();
280
281         class SignalBlocker
282         {
283         public:
284                 SignalBlocker (EngineControl& engine_control, const std::string& reason);
285
286                 ~SignalBlocker ();
287
288         private:
289                 EngineControl& ec;
290                 std::string m_reason;
291         };
292
293         uint32_t block_signals;
294
295         sigc::connection backend_combo_connection;
296         sigc::connection driver_combo_connection;
297         sigc::connection sample_rate_combo_connection;
298         sigc::connection buffer_size_combo_connection;
299         sigc::connection nperiods_combo_connection;
300         sigc::connection device_combo_connection;
301         sigc::connection input_device_combo_connection;
302         sigc::connection output_device_combo_connection;
303         sigc::connection midi_option_combo_connection;
304         sigc::connection input_latency_connection;
305         sigc::connection output_latency_connection;
306         sigc::connection input_channels_connection;
307         sigc::connection output_channels_connection;
308
309     void on_show ();
310     void on_response (int);
311     void control_app_button_clicked ();
312     void start_stop_button_clicked ();
313     void update_devices_button_clicked ();
314     void use_buffered_io_button_clicked ();
315     void use_latency_button_clicked ();
316     void manage_control_app_sensitivity ();
317     int push_state_to_backend (bool start);
318     void post_push ();
319     void update_sensitivity ();
320         bool start_engine ();
321         bool stop_engine (bool for_latency = false);
322
323     /* latency measurement */
324     void latency_button_clicked ();
325     void latency_back_button_clicked ();
326     bool check_audio_latency_measurement ();
327     bool check_midi_latency_measurement ();
328     sigc::connection latency_timeout;
329     void enable_latency_tab ();
330     void disable_latency_tab ();
331     void start_latency_detection ();
332     void end_latency_detection ();
333
334     void on_switch_page (GtkNotebookPage*, guint page_num);
335     bool on_delete_event (GdkEventAny*);
336
337     void engine_running ();
338     void engine_stopped ();
339     void device_list_changed ();
340
341     PBD::ScopedConnection running_connection;
342     PBD::ScopedConnectionList stopped_connection;
343     PBD::ScopedConnection devicelist_connection;
344
345     void connect_disconnect_click ();
346     void calibrate_audio_latency ();
347     void calibrate_midi_latency (MidiDeviceSettings);
348
349     MidiDeviceSettings _measure_midi;
350     void midi_latency_adjustment_changed(Gtk::Adjustment *, MidiDeviceSettings, bool);
351     void midi_device_enabled_toggled(ArdourButton *, MidiDeviceSettings);
352     sigc::connection lm_back_button_signal;
353 };
354
355 #endif /* __gtk2_ardour_engine_dialog_h__ */