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