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