ec2a028ba477d1adc6791c0a161f6176fdb51ef4
[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 #include <gtkmm/stock.h>
20
21 #include "pbd/enumwriter.h"
22 #include "pbd/i18n.h"
23
24 #include "temporal/time.h"
25
26 #include "ardour/audioengine.h"
27 #include "ardour/session.h"
28 #include "ardour/transport_master.h"
29 #include "ardour/transport_master_manager.h"
30
31 #include "widgets/tooltips.h"
32
33 #include "gtkmm2ext/utils.h"
34 #include "gtkmm2ext/gui_thread.h"
35
36 #include "ardour_ui.h"
37 #include "floating_text_entry.h"
38 #include "transport_masters_dialog.h"
39
40
41 using namespace std;
42 using namespace Gtk;
43 using namespace Gtkmm2ext;
44 using namespace ARDOUR;
45 using namespace PBD;
46 using namespace ArdourWidgets;
47
48 TransportMastersWidget::TransportMastersWidget ()
49         : table (4, 13)
50         , add_button (_("Add a new Transport Master"))
51 {
52         pack_start (table, PACK_EXPAND_WIDGET, 12);
53         pack_start (add_button, FALSE, FALSE);
54
55         add_button.signal_clicked ().connect (sigc::mem_fun (*this, &TransportMastersWidget::add_master));
56
57         col_title[0].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Use")));
58         col_title[1].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Name")));
59         col_title[2].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Type")));
60         col_title[3].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Format/\nBPM")));
61         col_title[4].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Current")));
62         col_title[5].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Last")));
63         col_title[6].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Timestamp")));
64         col_title[7].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Delta")));
65         col_title[8].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Collect")));
66         col_title[9].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Data Source")));
67         col_title[10].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Active\nCommands")));
68         col_title[11].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Clock\nSynced")));
69         col_title[12].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("29.97/\n30")));
70         col_title[13].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Remove")));
71
72         set_tooltip (col_title[12], _("<b>When enabled</b> the external timecode source is assumed to use 29.97 fps instead of 30000/1001.\n"
73                                       "SMPTE 12M-1999 specifies 29.97df as 30000/1001. The spec further mentions that "
74                                       "drop-sample timecode has an accumulated error of -86ms over a 24-hour period.\n"
75                                       "Drop-sample timecode would compensate exactly for a NTSC color frame rate of 30 * 0.9990 (ie 29.970000). "
76                                       "That is not the actual rate. However, some vendors use that rate - despite it being against the specs - "
77                                       "because the variant of using exactly 29.97 fps has zero timecode drift.\n"
78                              ));
79
80         set_tooltip (col_title[11], string_compose (_("<b>When enabled</b> the external timecode source is assumed to be sample-clock synced to the audio interface\n"
81                                                       "being used by %1."), PROGRAM_NAME));
82
83         table.set_spacings (6);
84
85         TransportMasterManager::instance().CurrentChanged.connect (current_connection, invalidator (*this), boost::bind (&TransportMastersWidget::current_changed, this, _1, _2), gui_context());
86         TransportMasterManager::instance().Added.connect (add_connection, invalidator (*this), boost::bind (&TransportMastersWidget::rebuild, this), gui_context());
87         TransportMasterManager::instance().Removed.connect (remove_connection, invalidator (*this), boost::bind (&TransportMastersWidget::rebuild, this), gui_context());
88
89         rebuild ();
90 }
91
92 TransportMastersWidget::~TransportMastersWidget ()
93 {
94         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
95                 delete *r;
96         }
97 }
98
99 void
100 TransportMastersWidget::set_transport_master (boost::shared_ptr<TransportMaster> tm)
101 {
102         _session->request_sync_source (tm);
103 }
104
105 void
106 TransportMastersWidget::current_changed (boost::shared_ptr<TransportMaster> old_master, boost::shared_ptr<TransportMaster> new_master)
107 {
108         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
109                 if ((*r)->tm == new_master) {
110                         (*r)->use_button.set_active (true);
111                         break; /* there can only be one */
112                 }
113         }
114 }
115
116 void
117 TransportMastersWidget::add_master ()
118 {
119         AddTransportMasterDialog d;
120
121         d.present ();
122         int r = d.run ();
123
124         switch (r) {
125         case RESPONSE_ACCEPT:
126                 break;
127         default:
128                 return;
129         }
130
131         TransportMasterManager::instance().add (d.get_type(), d.get_name());
132 }
133
134 void
135 TransportMastersWidget::rebuild ()
136 {
137         TransportMasterManager::TransportMasters const & masters (TransportMasterManager::instance().transport_masters());
138
139         container_clear (table);
140
141         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
142                 delete *r;
143         }
144
145         rows.clear ();
146         table.resize (masters.size()+1, 14);
147
148         for (size_t col = 0; col < sizeof (col_title) / sizeof (col_title[0]); ++col) {
149                 table.attach (col_title[col], col, col+1, 0, 1);
150         }
151
152         uint32_t n = 1;
153
154         Gtk::RadioButtonGroup use_button_group;
155
156         for (TransportMasterManager::TransportMasters::const_iterator m = masters.begin(); m != masters.end(); ++m, ++n) {
157
158                 Row* r = new Row (*this);
159                 rows.push_back (r);
160
161                 r->tm = *m;
162                 r->label.set_text ((*m)->name());
163                 r->type.set_text (enum_2_string  ((*m)->type()));
164
165                 r->use_button.set_group (use_button_group);
166
167                 if (TransportMasterManager::instance().current() == r->tm) {
168                         r->use_button.set_active (true);
169                 }
170
171                 int col = 0;
172
173                 r->label_box.add (r->label);
174
175                 table.attach (r->use_button, col, col+1, n, n+1); ++col;
176                 table.attach (r->label_box, col, col+1, n, n+1); ++col;
177                 table.attach (r->type, col, col+1, n, n+1); ++col;
178                 table.attach (r->format, col, col+1, n, n+1); ++col;
179                 table.attach (r->current, col, col+1, n, n+1); ++col;
180                 table.attach (r->last, col, col+1, n, n+1); ++col;
181                 table.attach (r->timestamp, col, col+1, n, n+1); ++col;
182                 table.attach (r->delta, col, col+1, n, n+1); ++col;
183                 table.attach (r->collect_button, col, col+1, n, n+1); ++col;
184                 table.attach (r->port_combo, col, col+1, n, n+1); ++col;
185                 table.attach (r->request_options, col, col+1, n, n+1); ++col;
186
187                 boost::shared_ptr<TimecodeTransportMaster> ttm (boost::dynamic_pointer_cast<TimecodeTransportMaster> (r->tm));
188
189                 if (ttm) {
190                         table.attach (r->sclock_synced_button, col, col+1, n, n+1); ++col;
191                         table.attach (r->fr2997_button, col, col+1, n, n+1); ++col;
192                         r->fr2997_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::fr2997_button_toggled));
193                 } else {
194                         col += 2;
195                 }
196
197                 if (r->tm->removeable()) {
198                         table.attach (r->remove_button, col, col+1, n, n+1); ++col;
199                 } else {
200                         col++;
201                 }
202
203                 table.show_all ();
204
205                 // r->label_box.set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
206                 r->label_box.signal_button_press_event().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::name_press));
207                 r->port_combo.signal_changed().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::port_choice_changed));
208                 r->use_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::use_button_toggled));
209                 r->collect_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::collect_button_toggled));
210                 r->request_options.signal_button_press_event().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::request_option_press), false);
211                 r->remove_button.signal_clicked().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::remove_clicked));
212
213                 if (ttm) {
214                         r->sclock_synced_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::sync_button_toggled));
215                 }
216
217                 r->tm->PropertyChanged.connect (r->property_change_connection, invalidator (*this), boost::bind (&TransportMastersWidget::Row::prop_change, r, _1), gui_context());
218
219                 PropertyChange all_change;
220                 all_change.add (Properties::locked);
221                 all_change.add (Properties::collect);
222                 all_change.add (Properties::connected);
223
224                 if (ttm) {
225                         all_change.add (Properties::fr2997);
226                         all_change.add (Properties::sclock_synced);
227                 }
228
229                 r->prop_change (all_change);
230         }
231 }
232
233 TransportMastersWidget::Row::Row (TransportMastersWidget& p)
234         : parent (p)
235         , request_option_menu (0)
236         , remove_button (X_("x"))
237         , name_editor (0)
238         , save_when (0)
239         , ignore_active_change (false)
240 {
241 }
242
243 bool
244 TransportMastersWidget::Row::name_press (GdkEventButton* ev)
245 {
246         if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
247                 Gtk::Window* toplevel = dynamic_cast<Gtk::Window*> (label.get_toplevel());
248                 if (!toplevel) {
249                         return false;
250                 }
251                 name_editor = new FloatingTextEntry (toplevel, tm->name());
252                 name_editor->use_text.connect (sigc::mem_fun (*this, &TransportMastersWidget::Row::name_edited));
253                 name_editor->show ();
254                 return true;
255         }
256         return false;
257 }
258
259 gboolean
260 TransportMastersWidget::Row::_idle_remove (gpointer arg)
261 {
262         TransportMastersWidget::Row* row = (TransportMastersWidget::Row*) arg;
263         TransportMasterManager::instance().remove (row->tm->name());
264
265         return FALSE; /* do not call again */
266 }
267
268 void
269 TransportMastersWidget::Row::remove_clicked ()
270 {
271         /* have to do this via an idle callback, because it will destroy the
272            widget from which this callback was initiated.
273         */
274         g_idle_add_full (G_PRIORITY_HIGH_IDLE + 10, _idle_remove, this, NULL);
275 }
276
277 void
278 TransportMastersWidget::Row::name_edited (string str, int ignored)
279 {
280         tm->set_name (str);
281         /* floating text entry deletes itself */
282         name_editor = 0;
283 }
284
285 void
286 TransportMastersWidget::Row::prop_change (PropertyChange what_changed)
287 {
288         if (what_changed.contains (Properties::locked)) {
289         }
290
291         if (what_changed.contains (Properties::fr2997)) {
292                 fr2997_button.set_active (boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm)->fr2997());
293         }
294
295         if (what_changed.contains (Properties::sclock_synced)) {
296                 sclock_synced_button.set_active (boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm)->sample_clock_synced());
297         }
298
299         if (what_changed.contains (Properties::collect)) {
300                 collect_button.set_active (tm->collect());
301         }
302
303         if (what_changed.contains (Properties::connected)) {
304                 populate_port_combo ();
305         }
306
307         if (what_changed.contains (Properties::name)) {
308                 label.set_text (tm->name());
309         }
310 }
311
312 void
313 TransportMastersWidget::Row::use_button_toggled ()
314 {
315         if (use_button.get_active()) {
316                 parent.set_transport_master (tm);
317         }
318 }
319
320 void
321 TransportMastersWidget::Row::fr2997_button_toggled ()
322 {
323         boost::dynamic_pointer_cast<TimecodeTransportMaster>(tm)->set_fr2997 (fr2997_button.get_active());
324 }
325
326 void
327 TransportMastersWidget::Row::collect_button_toggled ()
328 {
329         tm->set_collect (collect_button.get_active());
330 }
331
332 void
333 TransportMastersWidget::Row::sync_button_toggled ()
334 {
335         tm->set_sample_clock_synced (sclock_synced_button.get_active());
336 }
337
338 bool
339 TransportMastersWidget::Row::request_option_press (GdkEventButton* ev)
340 {
341         if (ev->button == 1) {
342                 if (!request_option_menu) {
343                         build_request_options ();
344                 }
345                 request_option_menu->popup (1, ev->time);
346                 return true;
347         }
348         return false;
349 }
350
351 void
352 TransportMastersWidget::Row::build_request_options ()
353 {
354         using namespace Gtk::Menu_Helpers;
355
356         request_option_menu = manage (new Menu);
357
358         MenuList& items (request_option_menu->items());
359
360         items.push_back (CheckMenuElem (_("Accept speed-changing commands (start/stop)")));
361         Gtk::CheckMenuItem* i = dynamic_cast<Gtk::CheckMenuItem *> (&items.back ());
362         i->set_active (tm->request_mask() & TR_Speed);
363         items.push_back (CheckMenuElem (_("Accept locate commands")));
364         i = dynamic_cast<Gtk::CheckMenuItem *> (&items.back ());
365         i->set_active (tm->request_mask() & TR_Locate);
366 }
367
368 Glib::RefPtr<Gtk::ListStore>
369 TransportMastersWidget::Row::build_port_list (vector<string> const & ports)
370 {
371         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (port_columns);
372         TreeModel::Row row;
373
374         row = *store->append ();
375         row[port_columns.full_name] = string();
376         row[port_columns.short_name] = _("Disconnected");
377
378         for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
379
380                 if (AudioEngine::instance()->port_is_mine (*p)) {
381                         continue;
382                 }
383
384                 row = *store->append ();
385                 row[port_columns.full_name] = *p;
386
387                 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
388                 if (pn.empty ()) {
389                         pn = (*p).substr ((*p).find (':') + 1);
390                 }
391                 row[port_columns.short_name] = pn;
392         }
393
394         return store;
395 }
396
397 void
398 TransportMastersWidget::Row::populate_port_combo ()
399 {
400         if (!tm->port()) {
401                 port_combo.hide ();
402                 return;
403         } else {
404                 port_combo.show ();
405         }
406
407         vector<string> inputs;
408
409         if (tm->port()->type() == DataType::MIDI) {
410                 ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput), inputs);
411         } else {
412                 ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::AUDIO, ARDOUR::PortFlags (ARDOUR::IsOutput), inputs);
413         }
414
415         Glib::RefPtr<Gtk::ListStore> input = build_port_list (inputs);
416         bool input_found = false;
417         int n;
418
419         port_combo.set_model (input);
420
421         Gtk::TreeModel::Children children = input->children();
422         Gtk::TreeModel::Children::iterator i;
423         i = children.begin();
424         ++i; /* skip "Disconnected" */
425
426
427         for (n = 1;  i != children.end(); ++i, ++n) {
428                 string port_name = (*i)[port_columns.full_name];
429                 if (tm->port()->connected_to (port_name)) {
430                         port_combo.set_active (n);
431                         input_found = true;
432                         break;
433                 }
434         }
435
436         if (!input_found) {
437                 port_combo.set_active (0); /* disconnected */
438         }
439 }
440
441 void
442 TransportMastersWidget::Row::port_choice_changed ()
443 {
444         if (ignore_active_change) {
445                 return;
446         }
447
448         TreeModel::iterator active = port_combo.get_active ();
449         string new_port = (*active)[port_columns.full_name];
450
451         if (new_port.empty()) {
452                 tm->port()->disconnect_all ();
453                 return;
454         }
455
456         if (!tm->port()->connected_to (new_port)) {
457                 tm->port()->disconnect_all ();
458                 tm->port()->connect (new_port);
459         }
460 }
461
462 void
463 TransportMastersWidget::Row::update (Session* s, samplepos_t now)
464 {
465         using namespace Timecode;
466
467         samplepos_t pos;
468         double speed;
469         samplepos_t most_recent;
470         samplepos_t when;
471         stringstream ss;
472         Time t;
473         Time l;
474         boost::shared_ptr<TimecodeTransportMaster> ttm;
475         boost::shared_ptr<MIDIClock_TransportMaster> mtm;
476
477         if (s) {
478
479                 if (tm->speed_and_position (speed, pos, most_recent, when, now)) {
480
481                         sample_to_timecode (pos, t, false, false, 25, false, AudioEngine::instance()->sample_rate(), 100, false, 0);
482                         sample_to_timecode (most_recent, l, false, false, 25, false, AudioEngine::instance()->sample_rate(), 100, false, 0);
483
484                         if ((ttm = boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm))) {
485                                 format.set_text (timecode_format_name (ttm->apparent_timecode_format()));
486                                 last.set_text (Timecode::timecode_format_time (l));
487                         } else if ((mtm = boost::dynamic_pointer_cast<MIDIClock_TransportMaster> (tm))) {
488                                 char buf[8];
489                                 snprintf (buf, sizeof (buf), "%.1f", mtm->bpm());
490                                 format.set_text (buf);
491                                 last.set_text ("");
492                         } else {
493                                 format.set_text ("");
494                                 last.set_text ("");
495                         }
496                         current.set_text (Timecode::timecode_format_time (t));
497                         delta.set_markup (tm->delta_string ());
498
499                         char gap[32];
500                         const float seconds = (when - now) / (float) AudioEngine::instance()->sample_rate();
501                         if (abs (seconds) < 1.0) {
502                                 snprintf (gap, sizeof (gap), "%.3fs", seconds);
503                         } else if (abs (seconds) < 4.0) {
504                                 snprintf (gap, sizeof (gap), "%ds", (int) floor (seconds));
505                         } else {
506                                 snprintf (gap, sizeof (gap), "%s", _(">4s ago"));
507                         }
508                         timestamp.set_text (gap);
509                         save_when = when;
510
511                 } else {
512
513                         if (save_when) {
514                                 char gap[32];
515
516                                 const float seconds = (when - now) / (float) AudioEngine::instance()->sample_rate();
517                                 if (abs (seconds) < 1.0) {
518                                         snprintf (gap, sizeof (gap), "%.3fs", seconds);
519                                 } else if (abs (seconds) < 4.0) {
520                                         snprintf (gap, sizeof (gap), "%ds", (int) floor (seconds));
521                                 } else {
522                                         snprintf (gap, sizeof (gap), "%s", _(">4s ago"));
523                                 }
524                                 timestamp.set_text (gap);
525                                 save_when = when;
526                         }
527                         delta.set_text ("");
528                         current.set_text ("");
529                 }
530         }
531 }
532
533 void
534 TransportMastersWidget::update (samplepos_t /* audible */)
535 {
536         samplepos_t now = AudioEngine::instance()->sample_time ();
537
538         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
539                 (*r)->update (_session, now);
540         }
541 }
542
543 void
544 TransportMastersWidget::on_map ()
545 {
546         update_connection = ARDOUR_UI::Clock.connect (sigc::mem_fun (*this, &TransportMastersWidget::update));
547         Gtk::VBox::on_map ();
548 }
549
550 void
551 TransportMastersWidget::on_unmap ()
552 {
553         update_connection.disconnect ();
554         Gtk::VBox::on_unmap ();
555 }
556
557 TransportMastersWindow::TransportMastersWindow ()
558         : ArdourWindow (_("Transport Masters"))
559 {
560         add (w);
561         w.show ();
562 }
563
564 void
565 TransportMastersWindow::on_realize ()
566 {
567         ArdourWindow::on_realize ();
568         /* (try to) ensure that resizing is possible and the window can be moved (and closed) */
569         get_window()->set_decorations (Gdk::DECOR_BORDER | Gdk::DECOR_RESIZEH | Gdk::DECOR_TITLE | Gdk::DECOR_MENU);
570 }
571
572
573
574 void
575 TransportMastersWindow::set_session (ARDOUR::Session* s)
576 {
577         ArdourWindow::set_session (s);
578         w.set_session (s);
579 }
580
581 TransportMastersWidget::AddTransportMasterDialog::AddTransportMasterDialog ()
582         : ArdourDialog (_("Add Transport Master"), true, false)
583         , name_label (_("Name"))
584         , type_label (_("Type"))
585 {
586         name_hbox.set_spacing (6);
587         name_hbox.pack_start (name_label, false, false);
588         name_hbox.pack_start (name_entry, true, true);
589
590         type_hbox.set_spacing (6);
591         type_hbox.pack_start (type_label, false, false);
592         type_hbox.pack_start (type_combo, true, true);
593
594         vector<string> s;
595
596         s.push_back (X_("MTC"));
597         s.push_back (X_("LTC"));
598         s.push_back (X_("MIDI Clock"));
599
600         set_popdown_strings (type_combo, s);
601         type_combo.set_active_text (X_("LTC"));
602
603         get_vbox()->pack_start (name_hbox, false, false);
604         get_vbox()->pack_start (type_hbox, false, false);
605
606         add_button (_("Cancel"), RESPONSE_CANCEL);
607         add_button (_("Add"), RESPONSE_ACCEPT);
608
609         name_entry.show ();
610         type_combo.show ();
611         name_label.show ();
612         type_label.show ();
613         name_hbox.show ();
614         type_hbox.show ();
615 }
616
617 string
618 TransportMastersWidget::AddTransportMasterDialog::get_name () const
619 {
620         return name_entry.get_text ();
621 }
622
623 SyncSource
624 TransportMastersWidget::AddTransportMasterDialog::get_type() const
625 {
626         string t = type_combo.get_active_text ();
627
628         if (t == X_("MTC")) {
629                 return MTC;
630         } else if (t == X_("MIDI Clock")) {
631                 return MIDIClock;
632         }
633
634         return LTC;
635 }