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