various tweaks to the transport masters dialog
[ardour.git] / gtk2_ardour / transport_masters_dialog.cc
1 /*
2     Copyright (C) 2018 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 #include "pbd/enumwriter.h"
21
22 #include "temporal/time.h"
23
24 #include "ardour/audioengine.h"
25 #include "ardour/session.h"
26 #include "ardour/transport_master.h"
27 #include "ardour/transport_master_manager.h"
28
29 #include "widgets/tooltips.h"
30
31 #include "gtkmm2ext/utils.h"
32 #include "gtkmm2ext/gui_thread.h"
33
34 #include "ardour_ui.h"
35 #include "transport_masters_dialog.h"
36
37 #include "pbd/i18n.h"
38
39 using namespace std;
40 using namespace Gtk;
41 using namespace Gtkmm2ext;
42 using namespace ARDOUR;
43 using namespace PBD;
44 using namespace ArdourWidgets;
45
46 TransportMastersWidget::TransportMastersWidget ()
47         : table (4, 9)
48 {
49         pack_start (table, PACK_EXPAND_WIDGET, 12);
50
51         col_title[0].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Name")));
52         col_title[0].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Name")));
53         col_title[1].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Type")));
54         col_title[2].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Format")));
55         col_title[3].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Current")));
56         col_title[4].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Timestamp")));
57         col_title[5].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Delta")));
58         col_title[6].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Collect")));
59         col_title[7].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Use")));
60         col_title[8].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Data Source")));
61         col_title[9].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Accept")));
62         col_title[10].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Clock Synced")));
63         col_title[11].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("29.97/30")));
64
65         set_tooltip (col_title[11], _("<b>When enabled</b> the external timecode source is assumed to use 29.97 fps instead of 30000/1001.\n"
66                                      "SMPTE 12M-1999 specifies 29.97df as 30000/1001. The spec further mentions that "
67                                      "drop-sample timecode has an accumulated error of -86ms over a 24-hour period.\n"
68                                      "Drop-sample timecode would compensate exactly for a NTSC color frame rate of 30 * 0.9990 (ie 29.970000). "
69                                      "That is not the actual rate. However, some vendors use that rate - despite it being against the specs - "
70                                      "because the variant of using exactly 29.97 fps has zero timecode drift.\n"
71                              ));
72
73
74         table.set_spacings (6);
75
76         TransportMasterManager::instance().CurrentChanged.connect (current_connection, invalidator (*this), boost::bind (&TransportMastersWidget::current_changed, this, _1, _2), gui_context());
77
78         rebuild ();
79 }
80
81 TransportMastersWidget::~TransportMastersWidget ()
82 {
83         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
84                 delete *r;
85         }
86 }
87
88 void
89 TransportMastersWidget::current_changed (boost::shared_ptr<TransportMaster> old_master, boost::shared_ptr<TransportMaster> new_master)
90 {
91         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
92                 if ((*r)->tm == new_master) {
93                         (*r)->use_button.set_active (true);
94                         break; /* there can only be one */
95                 }
96         }
97 }
98
99 void
100 TransportMastersWidget::rebuild ()
101 {
102         TransportMasterManager::TransportMasters const & masters (TransportMasterManager::instance().transport_masters());
103
104         container_clear (table);
105
106         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
107                 delete *r;
108         }
109
110         rows.clear ();
111         table.resize (masters.size()+1, 12);
112
113         table.attach (col_title[0], 0, 1, 0, 1);
114         table.attach (col_title[1], 1, 2, 0, 1);
115         table.attach (col_title[2], 2, 3, 0, 1);
116         table.attach (col_title[3], 3, 4, 0, 1);
117         table.attach (col_title[4], 4, 5, 0, 1);
118         table.attach (col_title[5], 5, 6, 0, 1);
119         table.attach (col_title[6], 6, 7, 0, 1);
120         table.attach (col_title[7], 7, 8, 0, 1);
121         table.attach (col_title[8], 8, 9, 0, 1);
122         table.attach (col_title[9], 9, 10, 0, 1);
123         table.attach (col_title[10], 10, 11, 0, 1);
124         table.attach (col_title[11], 11, 12, 0, 1);
125
126         uint32_t n = 1;
127
128         for (TransportMasterManager::TransportMasters::const_iterator m = masters.begin(); m != masters.end(); ++m, ++n) {
129
130                 Row* r = new Row;
131                 rows.push_back (r);
132
133                 r->tm = *m;
134                 r->label.set_text ((*m)->name());
135                 r->type.set_text (enum_2_string  ((*m)->type()));
136
137                 r->use_button.set_group (use_button_group);
138
139                 if (TransportMasterManager::instance().current() == r->tm) {
140                         r->use_button.set_active (true);
141                 }
142
143                 table.attach (r->type, 0, 1, n, n+1);
144                 table.attach (r->label, 1, 2, n, n+1);
145                 table.attach (r->format, 2, 3, n, n+1);
146                 table.attach (r->current, 3, 4, n, n+1);
147                 table.attach (r->timestamp, 4, 5, n, n+1);
148                 table.attach (r->delta, 5, 6, n, n+1);
149                 table.attach (r->collect_button, 6, 7, n, n+1);
150                 table.attach (r->use_button, 7, 8, n, n+1);
151                 table.attach (r->port_combo, 8, 9, n, n+1);
152                 table.attach (r->request_options, 9, 10, n, n+1);
153
154                 boost::shared_ptr<TimecodeTransportMaster> ttm (boost::dynamic_pointer_cast<TimecodeTransportMaster> (r->tm));
155
156                 if (ttm) {
157                         table.attach (r->sclock_synced_button, 10, 11, n, n+1);
158                         table.attach (r->fr2997_button, 11, 12, n, n+1);
159                         r->fr2997_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::fr2997_button_toggled));
160                 }
161
162                 r->port_combo.signal_changed().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::port_choice_changed));
163                 r->use_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::use_button_toggled));
164                 r->collect_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::collect_button_toggled));
165                 r->request_options.signal_button_press_event().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::request_option_press), false);
166
167                 if (ttm) {
168                         r->sclock_synced_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::sync_button_toggled));
169                 }
170
171                 r->tm->PropertyChanged.connect (r->property_change_connection, invalidator (*this), boost::bind (&TransportMastersWidget::Row::prop_change, r, _1), gui_context());
172
173                 PropertyChange all_change;
174                 all_change.add (Properties::locked);
175                 all_change.add (Properties::collect);
176                 all_change.add (Properties::connected);
177
178                 if (ttm) {
179                         all_change.add (Properties::fr2997);
180                         all_change.add (Properties::sclock_synced);
181                 }
182
183                 r->prop_change (all_change);
184         }
185 }
186
187 TransportMastersWidget::Row::Row ()
188         : request_option_menu (0)
189         , ignore_active_change (false)
190 {
191 }
192
193 void
194 TransportMastersWidget::Row::prop_change (PropertyChange what_changed)
195 {
196         if (what_changed.contains (Properties::locked)) {
197         }
198
199         if (what_changed.contains (Properties::fr2997)) {
200                 fr2997_button.set_active (boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm)->fr2997());
201         }
202
203         if (what_changed.contains (Properties::sclock_synced)) {
204                 sclock_synced_button.set_active (boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm)->sample_clock_synced());
205         }
206
207         if (what_changed.contains (Properties::collect)) {
208                 collect_button.set_active (tm->collect());
209         }
210
211         if (what_changed.contains (Properties::connected)) {
212                 populate_port_combo ();
213         }
214
215         if (what_changed.contains (Properties::name)) {
216                 label.set_text (tm->name());
217         }
218 }
219
220 void
221 TransportMastersWidget::Row::use_button_toggled ()
222 {
223         if (use_button.get_active()) {
224                 Config->set_sync_source (tm->type());
225         }
226 }
227
228 void
229 TransportMastersWidget::Row::fr2997_button_toggled ()
230 {
231         boost::dynamic_pointer_cast<TimecodeTransportMaster>(tm)->set_fr2997 (fr2997_button.get_active());
232 }
233
234 void
235 TransportMastersWidget::Row::collect_button_toggled ()
236 {
237         tm->set_collect (collect_button.get_active());
238 }
239
240 void
241 TransportMastersWidget::Row::sync_button_toggled ()
242 {
243         tm->set_sample_clock_synced (sclock_synced_button.get_active());
244 }
245
246 bool
247 TransportMastersWidget::Row::request_option_press (GdkEventButton* ev)
248 {
249         if (ev->button == 1) {
250                 if (!request_option_menu) {
251                         build_request_options ();
252                 }
253                 request_option_menu->popup (1, ev->time);
254                 return true;
255         }
256         return false;
257 }
258
259 void
260 TransportMastersWidget::Row::build_request_options ()
261 {
262         using namespace Gtk::Menu_Helpers;
263
264         request_option_menu = manage (new Menu);
265
266         MenuList& items (request_option_menu->items());
267
268         items.push_back (CheckMenuElem (_("Accept speed-changing commands (start/stop)")));
269         Gtk::CheckMenuItem* i = dynamic_cast<Gtk::CheckMenuItem *> (&items.back ());
270         i->set_active (tm->request_mask() & TR_Speed);
271         items.push_back (CheckMenuElem (_("Accept locate commands")));
272         i = dynamic_cast<Gtk::CheckMenuItem *> (&items.back ());
273         i->set_active (tm->request_mask() & TR_Locate);
274 }
275
276 Glib::RefPtr<Gtk::ListStore>
277 TransportMastersWidget::Row::build_port_list (vector<string> const & ports)
278 {
279         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (port_columns);
280         TreeModel::Row row;
281
282         row = *store->append ();
283         row[port_columns.full_name] = string();
284         row[port_columns.short_name] = _("Disconnected");
285
286         for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
287
288                 if (AudioEngine::instance()->port_is_mine (*p)) {
289                         continue;
290                 }
291
292                 row = *store->append ();
293                 row[port_columns.full_name] = *p;
294
295                 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
296                 if (pn.empty ()) {
297                         pn = (*p).substr ((*p).find (':') + 1);
298                 }
299                 row[port_columns.short_name] = pn;
300         }
301
302         return store;
303 }
304
305 void
306 TransportMastersWidget::Row::populate_port_combo ()
307 {
308         if (!tm->port()) {
309                 port_combo.hide ();
310                 return;
311         } else {
312                 port_combo.show ();
313         }
314
315         vector<string> inputs;
316
317         if (tm->port()->type() == DataType::MIDI) {
318                 ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput), inputs);
319         } else {
320                 ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::AUDIO, ARDOUR::PortFlags (ARDOUR::IsOutput), inputs);
321         }
322
323         Glib::RefPtr<Gtk::ListStore> input = build_port_list (inputs);
324         bool input_found = false;
325         int n;
326
327         port_combo.set_model (input);
328
329         Gtk::TreeModel::Children children = input->children();
330         Gtk::TreeModel::Children::iterator i;
331         i = children.begin();
332         ++i; /* skip "Disconnected" */
333
334
335         for (n = 1;  i != children.end(); ++i, ++n) {
336                 string port_name = (*i)[port_columns.full_name];
337                 if (tm->port()->connected_to (port_name)) {
338                         port_combo.set_active (n);
339                         input_found = true;
340                         break;
341                 }
342         }
343
344         if (!input_found) {
345                 port_combo.set_active (0); /* disconnected */
346         }
347 }
348
349 void
350 TransportMastersWidget::Row::port_choice_changed ()
351 {
352         if (ignore_active_change) {
353                 return;
354         }
355
356         TreeModel::iterator active = port_combo.get_active ();
357         string new_port = (*active)[port_columns.full_name];
358
359         if (new_port.empty()) {
360                 tm->port()->disconnect_all ();
361                 return;
362         }
363
364         if (!tm->port()->connected_to (new_port)) {
365                 tm->port()->disconnect_all ();
366                 tm->port()->connect (new_port);
367         }
368 }
369
370 void
371 TransportMastersWidget::Row::update (Session* s, samplepos_t now)
372 {
373         using namespace Timecode;
374
375         samplepos_t pos;
376         double speed;
377         stringstream ss;
378         Time t;
379         boost::shared_ptr<TimecodeTransportMaster> ttm;
380         boost::shared_ptr<MIDIClock_TransportMaster> mtm;
381
382         if (s) {
383
384                 if (tm->speed_and_position (speed, pos, now)) {
385
386                         sample_to_timecode (pos, t, false, false, 25, false, AudioEngine::instance()->sample_rate(), 100, false, 0);
387
388                         if ((ttm = boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm))) {
389                                 format.set_text (timecode_format_name (ttm->apparent_timecode_format()));
390                         } else if ((mtm = boost::dynamic_pointer_cast<MIDIClock_TransportMaster> (tm))) {
391                                 char buf[8];
392                                 snprintf (buf, sizeof (buf), "%.1f", mtm->bpm());
393                                 format.set_text (buf);
394                         } else {
395                                 format.set_text ("");
396                         }
397                         current.set_text (Timecode::timecode_format_time (t));
398                         timestamp.set_markup (tm->position_string());
399                         delta.set_markup (tm->delta_string ());
400
401                 }
402         }
403
404         populate_port_combo ();
405 }
406
407 void
408 TransportMastersWidget::update (samplepos_t audible)
409 {
410         samplepos_t now = AudioEngine::instance()->sample_time ();
411
412         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
413                 (*r)->update (_session, now);
414         }
415 }
416
417 void
418 TransportMastersWidget::on_map ()
419 {
420         update_connection = ARDOUR_UI::Clock.connect (sigc::mem_fun (*this, &TransportMastersWidget::update));
421         Gtk::VBox::on_map ();
422 }
423
424 void
425 TransportMastersWidget::on_unmap ()
426 {
427         update_connection.disconnect ();
428         Gtk::VBox::on_unmap ();
429 }
430
431 TransportMastersWindow::TransportMastersWindow ()
432         : ArdourWindow (_("Transport Masters"))
433 {
434         add (w);
435         w.show ();
436 }
437
438 void
439 TransportMastersWindow::on_realize ()
440 {
441         std::cerr << "TMD realized!\n";
442         ArdourWindow::on_realize ();
443         /* (try to) ensure that resizing is possible and the window can be moved (and closed) */
444         get_window()->set_decorations (Gdk::DECOR_BORDER | Gdk::DECOR_RESIZEH | Gdk::DECOR_TITLE | Gdk::DECOR_MENU);
445 }
446
447
448
449 void
450 TransportMastersWindow::set_session (ARDOUR::Session* s)
451 {
452         ArdourWindow::set_session (s);
453         w.set_session (s);
454 }