Fix thinkos in cubasish theme
[ardour.git] / gtk2_ardour / engine_dialog.h
1 /*
2  * Copyright (C) 2007-2012 Carl Hetherington <carl@carlh.net>
3  * Copyright (C) 2007-2016 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2008-2009 David Robillard <d@drobilla.net>
5  * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
6  * Copyright (C) 2015-2016 Tim Mayberry <mojofunk@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #ifndef __gtk2_ardour_engine_dialog_h__
24 #define __gtk2_ardour_engine_dialog_h__
25
26 #include <map>
27 #include <vector>
28 #include <string>
29
30 #include <gtkmm/box.h>
31 #include <gtkmm/button.h>
32 #include <gtkmm/buttonbox.h>
33 #include <gtkmm/comboboxtext.h>
34 #include <gtkmm/checkbutton.h>
35 #include <gtkmm/expander.h>
36 #include <gtkmm/notebook.h>
37 #include <gtkmm/spinbutton.h>
38 #include <gtkmm/table.h>
39
40 #include "pbd/signals.h"
41
42 #include "widgets/ardour_button.h"
43
44 #include "ardour_dialog.h"
45
46 class EngineControl : public ArdourDialog, public PBD::ScopedConnectionList
47 {
48 public:
49         EngineControl ();
50         ~EngineControl ();
51
52         static bool need_setup ();
53
54         XMLNode& get_state ();
55         bool set_state (const XMLNode&);
56
57         void set_desired_sample_rate (uint32_t);
58
59 private:
60         Gtk::Notebook notebook;
61
62         Gtk::Label engine_status;
63
64         /* core fields used by all backends */
65
66         Gtk::Table basic_packer;
67         Gtk::HBox basic_hbox;
68         Gtk::VBox basic_vbox;
69
70         Gtk::ComboBoxText backend_combo;
71         Gtk::ComboBoxText driver_combo;
72         Gtk::ComboBoxText device_combo;
73         Gtk::ComboBoxText input_device_combo;
74         Gtk::ComboBoxText output_device_combo;
75         Gtk::ComboBoxText sample_rate_combo;
76         Gtk::ComboBoxText midi_option_combo;
77         Gtk::ComboBoxText buffer_size_combo;
78         Gtk::Label        buffer_size_duration_label;
79         Gtk::ComboBoxText nperiods_combo;
80         Gtk::Adjustment input_latency_adjustment;
81         Gtk::SpinButton input_latency;
82         Gtk::Adjustment output_latency_adjustment;
83         Gtk::SpinButton output_latency;
84         Gtk::Adjustment input_channels_adjustment;
85         Gtk::SpinButton input_channels;
86         Gtk::Adjustment output_channels_adjustment;
87         Gtk::SpinButton output_channels;
88         Gtk::Adjustment ports_adjustment;
89         Gtk::SpinButton ports_spinner;
90
91         Gtk::Label                  have_control_text;
92         ArdourWidgets::ArdourButton control_app_button;
93         ArdourWidgets::ArdourButton midi_devices_button;
94         ArdourWidgets::ArdourButton start_stop_button;
95         ArdourWidgets::ArdourButton update_devices_button;
96         ArdourWidgets::ArdourButton use_buffered_io_button;
97         ArdourWidgets::ArdourButton try_autostart_button;
98
99         Gtk::Button     connect_disconnect_button;
100
101         /* latency measurement */
102
103         class ChannelNameCols : public Gtk::TreeModelColumnRecord
104         {
105                 public:
106                         ChannelNameCols () {
107                                 add (pretty_name);
108                                 add (port_name);
109                         }
110                         Gtk::TreeModelColumn<std::string> pretty_name;
111                         Gtk::TreeModelColumn<std::string> port_name;
112         };
113
114         ChannelNameCols              lm_output_channel_cols;
115         Glib::RefPtr<Gtk::ListStore> lm_output_channel_list;
116         Gtk::ComboBox                lm_output_channel_combo;
117
118         ChannelNameCols              lm_input_channel_cols;
119         Glib::RefPtr<Gtk::ListStore> lm_input_channel_list;
120         Gtk::ComboBox                lm_input_channel_combo;
121
122         Gtk::Label                  lm_measure_label;
123         Gtk::Button                 lm_measure_button;
124         Gtk::Button                 lm_use_button;
125         Gtk::Button                 lm_back_button;
126         ArdourWidgets::ArdourButton lm_button_audio;
127         Gtk::Label                  lm_title;
128         Gtk::Label                  lm_preamble;
129         Gtk::Label                  lm_results;
130         Gtk::Table                  lm_table;
131         Gtk::VBox                   lm_vbox;
132         bool                        have_lm_results;
133         bool                        lm_running;
134
135         /* MIDI Tab */
136
137         Gtk::VBox midi_vbox;
138         Gtk::Button midi_back_button;
139         Gtk::Table midi_device_table;
140
141         /* MIDI ... JACK */
142
143         Gtk::CheckButton aj_button;
144
145         uint32_t ignore_changes; // state save/load
146         uint32_t ignore_device_changes; // AudioEngine::DeviceListChanged
147         uint32_t _desired_sample_rate;
148         bool     started_at_least_once;
149         bool     queue_device_changed;
150
151         void driver_changed ();
152         void backend_changed ();
153         void sample_rate_changed ();
154         void buffer_size_changed ();
155         void nperiods_changed ();
156         void parameter_changed ();
157         void midi_option_changed ();
158
159         void setup_midi_tab_for_backend ();
160         void setup_midi_tab_for_jack ();
161         void refresh_midi_display (std::string focus = "");
162
163         void update_midi_options ();
164
165         std::string bufsize_as_string (uint32_t);
166         std::string nperiods_as_string (uint32_t);
167
168         std::vector<float> get_default_sample_rates ();
169         std::vector<uint32_t> get_default_buffer_sizes ();
170
171         std::vector<float> get_sample_rates_for_all_devices ();
172         std::vector<uint32_t> get_buffer_sizes_for_all_devices ();
173
174         float get_rate() const;
175         uint32_t get_buffer_size() const;
176         uint32_t get_nperiods() const;
177         uint32_t get_input_channels() const;
178         uint32_t get_output_channels() const;
179         uint32_t get_input_latency() const;
180         uint32_t get_output_latency() const;
181         std::string get_device_name() const;
182         std::string get_input_device_name() const;
183         std::string get_output_device_name() const;
184         std::string get_driver() const;
185         std::string get_backend() const;
186         std::string get_midi_option () const;
187         bool get_use_buffered_io () const;
188
189         std::string get_default_device (const std::string&,
190                                         const std::vector<std::string>&);
191
192         void device_changed ();
193         void input_device_changed ();
194         void output_device_changed ();
195         bool set_driver_popdown_strings ();
196         bool set_device_popdown_strings ();
197         bool set_input_device_popdown_strings ();
198         bool set_output_device_popdown_strings ();
199         void set_samplerate_popdown_strings ();
200         void set_buffersize_popdown_strings ();
201         void set_nperiods_popdown_strings ();
202         void list_devices ();
203         void show_buffer_duration ();
204
205         void configure_midi_devices ();
206
207         struct MidiDeviceSetting {
208                 std::string name;
209                 bool enabled;
210                 uint32_t input_latency;
211                 uint32_t output_latency;
212
213                 MidiDeviceSetting (std::string n, bool en = true, uint32_t inl = 0, uint32_t oul = 0)
214                         : name (n)
215                         , enabled (en)
216                         , input_latency (inl)
217                         , output_latency (oul)
218                 {}
219         };
220
221         typedef boost::shared_ptr<MidiDeviceSetting> MidiDeviceSettings;
222         bool _can_set_midi_latencies;
223         std::vector<MidiDeviceSettings> _midi_devices;
224
225         MidiDeviceSettings find_midi_device(std::string devicename) const {
226                 for (std::vector<MidiDeviceSettings>::const_iterator p = _midi_devices.begin(); p != _midi_devices.end(); ++p) {
227                         if ((*p)->name == devicename) {
228                                 return *p;
229                         }
230                 }
231                 return MidiDeviceSettings();
232         }
233
234         struct StateStruct {
235                 std::string backend;
236                 std::string driver;
237                 std::string device;
238                 std::string input_device;
239                 std::string output_device;
240                 float sample_rate;
241                 uint32_t buffer_size;
242                 uint32_t n_periods;
243                 uint32_t input_latency;
244                 uint32_t output_latency;
245                 uint32_t input_channels;
246                 uint32_t output_channels;
247                 bool active;
248                 bool use_buffered_io;
249                 std::string midi_option;
250                 std::vector<MidiDeviceSettings> midi_devices;
251                 time_t lru;
252
253                 StateStruct()
254                         : sample_rate (48000)
255                         , buffer_size (1024)
256                         , input_latency (0)
257                         , output_latency (0)
258                         , input_channels (0)
259                         , output_channels (0)
260                         , active (false)
261                         , use_buffered_io (false)
262                         , lru (0)
263                 {}
264         };
265
266         typedef boost::shared_ptr<StateStruct> State;
267         typedef std::list<State> StateList;
268         static bool state_sort_cmp (const State &a, const State &b);
269
270         StateList states;
271
272         State get_matching_state (const std::string& backend);
273         State get_matching_state (const std::string& backend,
274                                   const std::string& driver,
275                                   const std::string& device);
276         State get_matching_state (const std::string& backend,
277                                   const std::string& driver,
278                                   const std::string& input_device,
279                                   const std::string& output_device);
280         State get_saved_state_for_currently_displayed_backend_and_device ();
281         void maybe_display_saved_state ();
282         State save_state ();
283         void store_state (State);
284         bool equivalent_states (const State&, const State&);
285
286         bool set_current_state (const State& state);
287         void set_default_state ();
288
289         bool  _have_control;
290
291         static bool print_channel_count (Gtk::SpinButton*);
292
293         void build_notebook ();
294         void build_full_control_notebook ();
295         void build_no_control_notebook ();
296
297         void connect_changed_signals ();
298         void block_changed_signals ();
299         void unblock_changed_signals ();
300
301         class SignalBlocker
302         {
303         public:
304                 SignalBlocker (EngineControl& engine_control, const std::string& reason);
305
306                 ~SignalBlocker ();
307
308         private:
309                 EngineControl& ec;
310                 std::string m_reason;
311         };
312
313         uint32_t block_signals;
314
315         sigc::connection backend_combo_connection;
316         sigc::connection driver_combo_connection;
317         sigc::connection sample_rate_combo_connection;
318         sigc::connection buffer_size_combo_connection;
319         sigc::connection nperiods_combo_connection;
320         sigc::connection device_combo_connection;
321         sigc::connection input_device_combo_connection;
322         sigc::connection output_device_combo_connection;
323         sigc::connection midi_option_combo_connection;
324         sigc::connection input_latency_connection;
325         sigc::connection output_latency_connection;
326         sigc::connection input_channels_connection;
327         sigc::connection output_channels_connection;
328
329         void on_show ();
330         void on_map ();
331         void config_parameter_changed (std::string const&);
332         void control_app_button_clicked ();
333         void start_stop_button_clicked ();
334         void update_devices_button_clicked ();
335         void use_buffered_io_button_clicked ();
336         void try_autostart_button_clicked ();
337         void use_latency_button_clicked ();
338         void manage_control_app_sensitivity ();
339         int push_state_to_backend (bool start);
340         void post_push ();
341         void update_sensitivity ();
342         bool start_engine ();
343         bool stop_engine (bool for_latency = false);
344
345         /* latency measurement */
346         void latency_button_clicked ();
347         void latency_back_button_clicked ();
348         bool check_audio_latency_measurement ();
349         bool check_midi_latency_measurement ();
350         sigc::connection latency_timeout;
351         void enable_latency_tab ();
352         void disable_latency_tab ();
353         void start_latency_detection ();
354         void end_latency_detection ();
355
356         void on_switch_page (GtkNotebookPage*, guint page_num);
357         bool on_delete_event (GdkEventAny*);
358         void on_response (int);
359
360         void engine_running ();
361         void engine_stopped ();
362         void device_list_changed ();
363
364         PBD::ScopedConnection running_connection;
365         PBD::ScopedConnectionList stopped_connection;
366         PBD::ScopedConnection devicelist_connection;
367
368         void connect_disconnect_click ();
369         void calibrate_audio_latency ();
370         void calibrate_midi_latency (MidiDeviceSettings);
371
372         MidiDeviceSettings _measure_midi;
373         void midi_latency_adjustment_changed(Gtk::Adjustment *, MidiDeviceSettings, bool);
374         void midi_device_enabled_toggled(ArdourWidgets::ArdourButton *, MidiDeviceSettings);
375         sigc::connection lm_back_button_signal;
376 };
377
378 #endif /* __gtk2_ardour_engine_dialog_h__ */