553a9f9be59f321cd90f7fc6d46a6ad0e4bf944e
[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     void 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     Gtk::Button     control_app_button;
86     ArdourButton    midi_devices_button;
87
88     Gtk::Button     connect_disconnect_button;
89
90     /* latency measurement */
91
92     Gtk::ComboBoxText lm_output_channel_combo;
93     Gtk::ComboBoxText lm_input_channel_combo;
94     Gtk::Label        lm_measure_label;
95     Gtk::Button       lm_measure_button;
96     Gtk::Button       lm_use_button;
97     Gtk::Button       lm_back_button;
98     ArdourButton      lm_button_audio;
99     Gtk::Label        lm_title;
100     Gtk::Label        lm_preamble;
101     Gtk::Label        lm_results;
102     Gtk::Table        lm_table;
103     Gtk::VBox         lm_vbox;
104     bool              have_lm_results;
105     bool              lm_running;
106
107     Gtk::Button* cancel_button;
108     Gtk::Button* ok_button;
109     Gtk::Button* apply_button;
110
111     /* MIDI Tab */
112
113     Gtk::VBox midi_vbox;
114     Gtk::Button midi_back_button;
115     Gtk::Table midi_device_table;
116
117     /* MIDI ... JACK */
118
119     Gtk::CheckButton aj_button;
120
121     uint32_t ignore_changes;
122     uint32_t _desired_sample_rate;
123     bool     started_at_least_once;
124     bool     queue_device_changed;
125
126     void driver_changed ();
127     void backend_changed ();
128     void sample_rate_changed ();
129     void buffer_size_changed ();
130     void parameter_changed ();
131     void midi_option_changed ();
132
133     void setup_midi_tab_for_backend ();
134     void setup_midi_tab_for_jack ();
135     void refresh_midi_display (std::string focus = "");
136
137     std::string bufsize_as_string (uint32_t);
138
139     float get_rate() const;
140     uint32_t get_buffer_size() const;
141     uint32_t get_input_channels() const;
142     uint32_t get_output_channels() const;
143     uint32_t get_input_latency() const;
144     uint32_t get_output_latency() const;
145     std::string get_device_name() const;
146     std::string get_input_device_name() const;
147     std::string get_output_device_name() const;
148     std::string get_driver() const;
149     std::string get_backend() const;
150     std::string get_midi_option () const;
151
152     void device_changed ();
153     void input_device_changed ();
154     void output_device_changed ();
155     bool set_driver_popdown_strings ();
156     bool set_device_popdown_strings ();
157     bool set_input_device_popdown_strings ();
158     bool set_output_device_popdown_strings ();
159     void set_samplerate_popdown_strings (const std::string& dev_name);
160     void set_buffersize_popdown_strings (const std::string& dev_name);
161     void list_devices ();
162     void show_buffer_duration ();
163
164     void configure_midi_devices ();
165
166     struct MidiDeviceSetting {
167         std::string name;
168         bool enabled;
169         uint32_t input_latency;
170         uint32_t output_latency;
171
172         MidiDeviceSetting (std::string n, bool en = true, uint32_t inl = 0, uint32_t oul = 0)
173             : name (n)
174             , enabled (en)
175             , input_latency (inl)
176             , output_latency (oul)
177         {}
178     };
179
180     typedef boost::shared_ptr<MidiDeviceSetting> MidiDeviceSettings;
181     bool _can_set_midi_latencies;
182     std::vector<MidiDeviceSettings> _midi_devices;
183
184     MidiDeviceSettings find_midi_device(std::string devicename) const {
185         for (std::vector<MidiDeviceSettings>::const_iterator p = _midi_devices.begin(); p != _midi_devices.end(); ++p) {
186             if ((*p)->name == devicename) {
187                 return *p;
188             }
189         }
190         return MidiDeviceSettings();
191     }
192
193     struct StateStruct {
194         std::string backend;
195         std::string driver;
196         std::string device;
197         std::string input_device;
198         std::string output_device;
199         float sample_rate;
200         uint32_t buffer_size;
201         uint32_t input_latency;
202         uint32_t output_latency;
203         uint32_t input_channels;
204         uint32_t output_channels;
205         bool active;
206         std::string midi_option;
207         std::vector<MidiDeviceSettings> midi_devices;
208
209         StateStruct()
210                 : sample_rate (48000)
211                 , buffer_size (1024)
212                 , input_latency (0)
213                 , output_latency (0)
214                 , input_channels (0)
215                 , output_channels (0)
216                 , active (false) {}
217
218     };
219
220     typedef boost::shared_ptr<StateStruct> State;
221     typedef std::list<State> StateList;
222
223     StateList states;
224
225     State get_matching_state (const std::string& backend,
226                                const std::string& driver,
227                                const std::string& device);
228     State get_matching_state (const std::string& backend,
229                                const std::string& driver,
230                                const std::string& input_device,
231                                const std::string& output_device);
232     State get_saved_state_for_currently_displayed_backend_and_device ();
233     void maybe_display_saved_state ();
234     State save_state ();
235     void store_state (State);
236     bool equivalent_states (const State&, const State&);
237
238         void set_current_state (const State& state);
239
240     bool  _have_control;
241
242     static bool print_channel_count (Gtk::SpinButton*);
243
244     void build_notebook ();
245     void build_full_control_notebook ();
246     void build_no_control_notebook ();
247
248         void connect_changed_signals ();
249
250     void on_show ();
251     void on_response (int);
252     void control_app_button_clicked ();
253     void use_latency_button_clicked ();
254     void manage_control_app_sensitivity ();
255     int push_state_to_backend (bool start);
256     void post_push ();
257
258     /* latency measurement */
259     void latency_button_clicked ();
260     bool check_audio_latency_measurement ();
261     bool check_midi_latency_measurement ();
262     sigc::connection latency_timeout;
263     void enable_latency_tab ();
264     void disable_latency_tab ();
265     void start_latency_detection ();
266     void end_latency_detection ();
267
268     void on_switch_page (GtkNotebookPage*, guint page_num);
269     bool on_delete_event (GdkEventAny*);
270
271     void engine_running ();
272     void engine_stopped ();
273     void device_list_changed ();
274
275     PBD::ScopedConnection running_connection;
276     PBD::ScopedConnectionList stopped_connection;
277     PBD::ScopedConnection devicelist_connection;
278
279     void connect_disconnect_click ();
280     void calibrate_audio_latency ();
281     void calibrate_midi_latency (MidiDeviceSettings);
282
283     MidiDeviceSettings _measure_midi;
284     void midi_latency_adjustment_changed(Gtk::Adjustment *, MidiDeviceSettings, bool);
285     void midi_device_enabled_toggled(ArdourButton *, MidiDeviceSettings);
286     sigc::connection lm_back_button_signal;
287 };
288
289 #endif /* __gtk2_ardour_engine_dialog_h__ */