Update GPL boilerplate and (C)
[ardour.git] / gtk2_ardour / transport_masters_dialog.cc
1 /*
2  * Copyright (C) 2018-2019 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2018-2019 Robin Gareus <robin@gareus.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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         string name;
123
124         while (name.empty()) {
125
126                 int r = d.run ();
127
128                 switch (r) {
129                 case RESPONSE_ACCEPT:
130                         name = d.get_name();
131                         break;
132                 default:
133                         return;
134                 }
135         }
136
137         d.hide ();
138
139         if (TransportMasterManager::instance().add (d.get_type(), name)) {
140                 MessageDialog msg (_("New transport master not added - check error log for details"));
141                 msg.run ();
142         }
143 }
144
145 void
146 TransportMastersWidget::clear ()
147 {
148         container_clear (table);
149
150         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
151                 delete *r;
152         }
153
154         rows.clear ();
155 }
156
157 void
158 TransportMastersWidget::rebuild ()
159 {
160         TransportMasterManager::TransportMasters const & masters (TransportMasterManager::instance().transport_masters());
161
162         clear ();
163         table.resize (masters.size()+1, 14);
164
165         for (size_t col = 0; col < sizeof (col_title) / sizeof (col_title[0]); ++col) {
166                 table.attach (col_title[col], col, col+1, 0, 1);
167         }
168
169         uint32_t n = 1;
170
171         Gtk::RadioButtonGroup use_button_group;
172
173         for (TransportMasterManager::TransportMasters::const_iterator m = masters.begin(); m != masters.end(); ++m, ++n) {
174
175                 Row* r = new Row (*this);
176                 rows.push_back (r);
177
178                 r->tm = *m;
179                 r->label.set_text ((*m)->name());
180                 r->type.set_text (enum_2_string  ((*m)->type()));
181
182                 r->use_button.set_group (use_button_group);
183
184                 if (TransportMasterManager::instance().current() == r->tm) {
185                         r->use_button.set_active (true);
186                 }
187
188                 int col = 0;
189
190                 r->label_box.add (r->label);
191
192                 table.attach (r->use_button, col, col+1, n, n+1); ++col;
193                 table.attach (r->label_box, col, col+1, n, n+1); ++col;
194                 table.attach (r->type, col, col+1, n, n+1); ++col;
195                 table.attach (r->format, col, col+1, n, n+1); ++col;
196                 table.attach (r->current, col, col+1, n, n+1); ++col;
197                 table.attach (r->last, col, col+1, n, n+1); ++col;
198                 table.attach (r->timestamp, col, col+1, n, n+1); ++col;
199                 table.attach (r->delta, col, col+1, n, n+1); ++col;
200                 table.attach (r->collect_button, col, col+1, n, n+1); ++col;
201                 table.attach (r->port_combo, col, col+1, n, n+1); ++col;
202                 table.attach (r->request_options, col, col+1, n, n+1); ++col;
203
204                 boost::shared_ptr<TimecodeTransportMaster> ttm (boost::dynamic_pointer_cast<TimecodeTransportMaster> (r->tm));
205
206                 if (ttm) {
207                         table.attach (r->sclock_synced_button, col, col+1, n, n+1); ++col;
208                         table.attach (r->fr2997_button, col, col+1, n, n+1); ++col;
209                         r->fr2997_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::fr2997_button_toggled));
210                 } else {
211                         col += 2;
212                 }
213
214                 if (r->tm->removeable()) {
215                         table.attach (r->remove_button, col, col+1, n, n+1); ++col;
216                 } else {
217                         col++;
218                 }
219
220                 table.show_all ();
221
222                 r->label_box.signal_button_press_event().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::name_press));
223                 r->port_combo.signal_changed().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::port_choice_changed));
224                 r->use_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::use_button_toggled));
225                 r->collect_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::collect_button_toggled));
226                 r->request_options.signal_button_press_event().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::request_option_press), false);
227                 r->remove_button.signal_clicked().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::remove_clicked));
228
229                 if (ttm) {
230                         r->sclock_synced_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::sync_button_toggled));
231                 }
232
233                 r->tm->PropertyChanged.connect (r->property_change_connection, invalidator (*this), boost::bind (&TransportMastersWidget::Row::prop_change, r, _1), gui_context());
234
235                 PropertyChange all_change;
236                 all_change.add (Properties::locked);
237                 all_change.add (Properties::collect);
238                 all_change.add (Properties::connected);
239
240                 if (ttm) {
241                         all_change.add (Properties::fr2997);
242                         all_change.add (Properties::sclock_synced);
243                 }
244
245                 r->prop_change (all_change);
246         }
247 }
248
249 TransportMastersWidget::Row::Row (TransportMastersWidget& p)
250         : parent (p)
251         , request_option_menu (0)
252         , remove_button (X_("x"))
253         , name_editor (0)
254         , save_when (0)
255         , ignore_active_change (false)
256 {
257 }
258
259 TransportMastersWidget::Row::~Row ()
260 {
261         delete request_option_menu;
262 }
263
264 bool
265 TransportMastersWidget::Row::name_press (GdkEventButton* ev)
266 {
267         if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
268                 Gtk::Window* toplevel = dynamic_cast<Gtk::Window*> (label.get_toplevel());
269                 if (!toplevel) {
270                         return false;
271                 }
272                 name_editor = new FloatingTextEntry (toplevel, tm->name());
273                 name_editor->use_text.connect (sigc::mem_fun (*this, &TransportMastersWidget::Row::name_edited));
274                 name_editor->show ();
275                 return true;
276         }
277         return false;
278 }
279
280 bool
281 TransportMastersWidget::idle_remove (TransportMastersWidget::Row* row)
282 {
283         TransportMasterManager::instance().remove (row->tm->name());
284         return false;
285 }
286
287 void
288 TransportMastersWidget::Row::remove_clicked ()
289 {
290         /* have to do this via an idle callback, because it will destroy the
291            widget from which this callback was initiated.
292         */
293         Glib::signal_idle().connect (sigc::bind (sigc::mem_fun (parent, &TransportMastersWidget::idle_remove), this));
294 }
295
296 void
297 TransportMastersWidget::Row::name_edited (string str, int ignored)
298 {
299         tm->set_name (str);
300         /* floating text entry deletes itself */
301         name_editor = 0;
302 }
303
304 void
305 TransportMastersWidget::Row::prop_change (PropertyChange what_changed)
306 {
307         if (what_changed.contains (Properties::locked)) {
308         }
309
310         if (what_changed.contains (Properties::fr2997)) {
311                 fr2997_button.set_active (boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm)->fr2997());
312         }
313
314         if (what_changed.contains (Properties::sclock_synced)) {
315                 sclock_synced_button.set_active (boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm)->sample_clock_synced());
316         }
317
318         if (what_changed.contains (Properties::collect)) {
319                 collect_button.set_active (tm->collect());
320         }
321
322         if (what_changed.contains (Properties::connected)) {
323                 populate_port_combo ();
324         }
325
326         if (what_changed.contains (Properties::name)) {
327                 label.set_text (tm->name());
328         }
329 }
330
331 void
332 TransportMastersWidget::Row::use_button_toggled ()
333 {
334         if (use_button.get_active()) {
335                 parent.set_transport_master (tm);
336         }
337 }
338
339 void
340 TransportMastersWidget::Row::fr2997_button_toggled ()
341 {
342         boost::dynamic_pointer_cast<TimecodeTransportMaster>(tm)->set_fr2997 (fr2997_button.get_active());
343 }
344
345 void
346 TransportMastersWidget::Row::collect_button_toggled ()
347 {
348         tm->set_collect (collect_button.get_active());
349 }
350
351 void
352 TransportMastersWidget::Row::sync_button_toggled ()
353 {
354         tm->set_sample_clock_synced (sclock_synced_button.get_active());
355 }
356
357 bool
358 TransportMastersWidget::Row::request_option_press (GdkEventButton* ev)
359 {
360         if (ev->button == 1) {
361                 if (!request_option_menu) {
362                         build_request_options ();
363                 }
364                 request_option_menu->popup (1, ev->time);
365                 return true;
366         }
367         return false;
368 }
369
370 void
371 TransportMastersWidget::Row::build_request_options ()
372 {
373         using namespace Gtk::Menu_Helpers;
374
375         request_option_menu = new Menu;
376
377         MenuList& items (request_option_menu->items());
378
379         items.push_back (CheckMenuElem (_("Accept speed-changing commands (start/stop)")));
380         Gtk::CheckMenuItem* i = dynamic_cast<Gtk::CheckMenuItem *> (&items.back ());
381         i->set_active (tm->request_mask() & TR_Speed);
382         items.push_back (CheckMenuElem (_("Accept locate commands")));
383         i = dynamic_cast<Gtk::CheckMenuItem *> (&items.back ());
384         i->set_active (tm->request_mask() & TR_Locate);
385 }
386
387 Glib::RefPtr<Gtk::ListStore>
388 TransportMastersWidget::Row::build_port_list (vector<string> const & ports)
389 {
390         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (port_columns);
391         TreeModel::Row row;
392
393         row = *store->append ();
394         row[port_columns.full_name] = string();
395         row[port_columns.short_name] = _("Disconnected");
396
397         for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
398
399                 if (AudioEngine::instance()->port_is_mine (*p)) {
400                         continue;
401                 }
402
403                 row = *store->append ();
404                 row[port_columns.full_name] = *p;
405
406                 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
407                 if (pn.empty ()) {
408                         pn = (*p).substr ((*p).find (':') + 1);
409                 }
410                 row[port_columns.short_name] = pn;
411         }
412
413         return store;
414 }
415
416 void
417 TransportMastersWidget::Row::populate_port_combo ()
418 {
419         if (!tm->port()) {
420                 port_combo.hide ();
421                 return;
422         } else {
423                 port_combo.show ();
424         }
425
426         vector<string> inputs;
427
428         if (tm->port()->type() == DataType::MIDI) {
429                 ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput), inputs);
430         } else {
431                 ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::AUDIO, ARDOUR::PortFlags (ARDOUR::IsOutput), inputs);
432         }
433
434         Glib::RefPtr<Gtk::ListStore> input = build_port_list (inputs);
435         bool input_found = false;
436         int n;
437
438         port_combo.set_model (input);
439
440         Gtk::TreeModel::Children children = input->children();
441         Gtk::TreeModel::Children::iterator i;
442         i = children.begin();
443         ++i; /* skip "Disconnected" */
444
445
446         for (n = 1;  i != children.end(); ++i, ++n) {
447                 string port_name = (*i)[port_columns.full_name];
448                 if (tm->port()->connected_to (port_name)) {
449                         port_combo.set_active (n);
450                         input_found = true;
451                         break;
452                 }
453         }
454
455         if (!input_found) {
456                 port_combo.set_active (0); /* disconnected */
457         }
458 }
459
460 void
461 TransportMastersWidget::Row::port_choice_changed ()
462 {
463         if (ignore_active_change) {
464                 return;
465         }
466
467         TreeModel::iterator active = port_combo.get_active ();
468         string new_port = (*active)[port_columns.full_name];
469
470         if (new_port.empty()) {
471                 tm->port()->disconnect_all ();
472                 return;
473         }
474
475         if (!tm->port()->connected_to (new_port)) {
476                 tm->port()->disconnect_all ();
477                 tm->port()->connect (new_port);
478         }
479 }
480
481 void
482 TransportMastersWidget::Row::update (Session* s, samplepos_t now)
483 {
484         using namespace Timecode;
485
486         samplepos_t pos;
487         double speed;
488         samplepos_t most_recent;
489         samplepos_t when;
490         stringstream ss;
491         Time t;
492         Time l;
493         boost::shared_ptr<TimecodeTransportMaster> ttm;
494         boost::shared_ptr<MIDIClock_TransportMaster> mtm;
495
496         if (!AudioEngine::instance()->running()) {
497                 return;
498         }
499
500         if (s) {
501
502                 if (tm->speed_and_position (speed, pos, most_recent, when, now)) {
503
504                         sample_to_timecode (pos, t, false, false, 25, false, AudioEngine::instance()->sample_rate(), 100, false, 0);
505                         sample_to_timecode (most_recent, l, false, false, 25, false, AudioEngine::instance()->sample_rate(), 100, false, 0);
506
507                         if ((ttm = boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm))) {
508                                 format.set_text (timecode_format_name (ttm->apparent_timecode_format()));
509                                 last.set_text (Timecode::timecode_format_time (l));
510                         } else if ((mtm = boost::dynamic_pointer_cast<MIDIClock_TransportMaster> (tm))) {
511                                 char buf[8];
512                                 snprintf (buf, sizeof (buf), "%.1f", mtm->bpm());
513                                 format.set_text (buf);
514                                 last.set_text ("");
515                         } else {
516                                 format.set_text ("");
517                                 last.set_text ("");
518                         }
519                         current.set_text (Timecode::timecode_format_time (t));
520                         delta.set_markup (tm->delta_string ());
521
522                         char gap[32];
523                         const float seconds = (when - now) / (float) AudioEngine::instance()->sample_rate();
524                         if (abs (seconds) < 1.0) {
525                                 snprintf (gap, sizeof (gap), "%.3fs", seconds);
526                         } else if (abs (seconds) < 4.0) {
527                                 snprintf (gap, sizeof (gap), "%ds", (int) floor (seconds));
528                         } else {
529                                 snprintf (gap, sizeof (gap), "%s", _(">4s ago"));
530                         }
531                         timestamp.set_text (gap);
532                         save_when = when;
533
534                 } else {
535
536                         if (save_when) {
537                                 char gap[32];
538
539                                 const float seconds = (when - now) / (float) AudioEngine::instance()->sample_rate();
540                                 if (abs (seconds) < 1.0) {
541                                         snprintf (gap, sizeof (gap), "%.3fs", seconds);
542                                 } else if (abs (seconds) < 4.0) {
543                                         snprintf (gap, sizeof (gap), "%ds", (int) floor (seconds));
544                                 } else {
545                                         snprintf (gap, sizeof (gap), "%s", _(">4s ago"));
546                                 }
547                                 timestamp.set_text (gap);
548                                 save_when = when;
549                         }
550                         delta.set_text ("");
551                         current.set_text ("");
552                 }
553         }
554 }
555
556 void
557 TransportMastersWidget::update (samplepos_t /* audible */)
558 {
559         samplepos_t now = AudioEngine::instance()->sample_time ();
560
561         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
562                 (*r)->update (_session, now);
563         }
564 }
565
566 void
567 TransportMastersWidget::on_map ()
568 {
569         update_connection = ARDOUR_UI::Clock.connect (sigc::mem_fun (*this, &TransportMastersWidget::update));
570         Gtk::VBox::on_map ();
571 }
572
573 void
574 TransportMastersWidget::on_unmap ()
575 {
576         update_connection.disconnect ();
577         Gtk::VBox::on_unmap ();
578 }
579
580 TransportMastersWindow::TransportMastersWindow ()
581         : ArdourWindow (_("Transport Masters"))
582 {
583         add (w);
584         w.show ();
585 }
586
587 void
588 TransportMastersWindow::on_realize ()
589 {
590         ArdourWindow::on_realize ();
591         /* (try to) ensure that resizing is possible and the window can be moved (and closed) */
592         get_window()->set_decorations (Gdk::DECOR_BORDER | Gdk::DECOR_RESIZEH | Gdk::DECOR_TITLE | Gdk::DECOR_MENU);
593 }
594
595
596
597 void
598 TransportMastersWindow::set_session (ARDOUR::Session* s)
599 {
600         ArdourWindow::set_session (s);
601         w.set_session (s);
602 }
603
604 TransportMastersWidget::AddTransportMasterDialog::AddTransportMasterDialog ()
605         : ArdourDialog (_("Add Transport Master"), true, false)
606         , name_label (_("Name"))
607         , type_label (_("Type"))
608 {
609         name_hbox.set_spacing (6);
610         name_hbox.pack_start (name_label, false, false);
611         name_hbox.pack_start (name_entry, true, true);
612
613         type_hbox.set_spacing (6);
614         type_hbox.pack_start (type_label, false, false);
615         type_hbox.pack_start (type_combo, true, true);
616
617         vector<string> s;
618
619         s.push_back (X_("MTC"));
620         s.push_back (X_("LTC"));
621         s.push_back (X_("MIDI Clock"));
622
623         set_popdown_strings (type_combo, s);
624         type_combo.set_active_text (X_("LTC"));
625
626         get_vbox()->pack_start (name_hbox, false, false);
627         get_vbox()->pack_start (type_hbox, false, false);
628
629         add_button (_("Cancel"), RESPONSE_CANCEL);
630         add_button (_("Add"), RESPONSE_ACCEPT);
631
632         name_entry.show ();
633         type_combo.show ();
634         name_label.show ();
635         type_label.show ();
636         name_hbox.show ();
637         type_hbox.show ();
638
639         name_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &Gtk::Dialog::response), Gtk::RESPONSE_ACCEPT));
640 }
641
642 string
643 TransportMastersWidget::AddTransportMasterDialog::get_name () const
644 {
645         return name_entry.get_text ();
646 }
647
648 SyncSource
649 TransportMastersWidget::AddTransportMasterDialog::get_type() const
650 {
651         string t = type_combo.get_active_text ();
652
653         if (t == X_("MTC")) {
654                 return MTC;
655         } else if (t == X_("MIDI Clock")) {
656                 return MIDIClock;
657         }
658
659         return LTC;
660 }