ac2066cebac64eff8ac5524fda5aa6ec561097b5
[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
87         rebuild ();
88 }
89
90 TransportMastersWidget::~TransportMastersWidget ()
91 {
92         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
93                 delete *r;
94         }
95 }
96
97 void
98 TransportMastersWidget::set_transport_master (boost::shared_ptr<TransportMaster> tm)
99 {
100         _session->request_sync_source (tm);
101 }
102
103 void
104 TransportMastersWidget::current_changed (boost::shared_ptr<TransportMaster> old_master, boost::shared_ptr<TransportMaster> new_master)
105 {
106         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
107                 if ((*r)->tm == new_master) {
108                         (*r)->use_button.set_active (true);
109                         break; /* there can only be one */
110                 }
111         }
112 }
113
114 void
115 TransportMastersWidget::add_master ()
116 {
117         if (!TransportMasterManager::instance().add (LTC, "new ltc")) {
118                 rebuild ();
119         }
120 }
121
122 void
123 TransportMastersWidget::rebuild ()
124 {
125         TransportMasterManager::TransportMasters const & masters (TransportMasterManager::instance().transport_masters());
126
127         container_clear (table);
128
129         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
130                 delete *r;
131         }
132
133         rows.clear ();
134         table.resize (masters.size()+1, 14);
135
136         for (size_t col = 0; col < sizeof (col_title) / sizeof (col_title[0]); ++col) {
137                 table.attach (col_title[col], col, col+1, 0, 1);
138         }
139
140         uint32_t n = 1;
141
142         Gtk::RadioButtonGroup use_button_group;
143
144         for (TransportMasterManager::TransportMasters::const_iterator m = masters.begin(); m != masters.end(); ++m, ++n) {
145
146                 Row* r = new Row (*this);
147                 rows.push_back (r);
148
149                 r->tm = *m;
150                 r->label.set_text ((*m)->name());
151                 r->type.set_text (enum_2_string  ((*m)->type()));
152
153                 r->use_button.set_group (use_button_group);
154
155                 if (TransportMasterManager::instance().current() == r->tm) {
156                         r->use_button.set_active (true);
157                 }
158
159                 int col = 0;
160
161                 r->label_box.add (r->label);
162
163                 table.attach (r->use_button, col, col+1, n, n+1); ++col;
164                 table.attach (r->label_box, col, col+1, n, n+1); ++col;
165                 table.attach (r->type, col, col+1, n, n+1); ++col;
166                 table.attach (r->format, col, col+1, n, n+1); ++col;
167                 table.attach (r->current, col, col+1, n, n+1); ++col;
168                 table.attach (r->last, col, col+1, n, n+1); ++col;
169                 table.attach (r->timestamp, col, col+1, n, n+1); ++col;
170                 table.attach (r->delta, col, col+1, n, n+1); ++col;
171                 table.attach (r->collect_button, col, col+1, n, n+1); ++col;
172                 table.attach (r->port_combo, col, col+1, n, n+1); ++col;
173                 table.attach (r->request_options, col, col+1, n, n+1); ++col;
174
175                 boost::shared_ptr<TimecodeTransportMaster> ttm (boost::dynamic_pointer_cast<TimecodeTransportMaster> (r->tm));
176
177                 if (ttm) {
178                         table.attach (r->sclock_synced_button, col, col+1, n, n+1); ++col;
179                         table.attach (r->fr2997_button, col, col+1, n, n+1); ++col;
180                         r->fr2997_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::fr2997_button_toggled));
181                 } else {
182                         col += 2;
183                 }
184
185                 if (r->tm->removeable()) {
186                         table.attach (r->remove_button, col, col+1, n, n+1); ++col;
187                 } else {
188                         col++;
189                 }
190
191                 table.show_all ();
192
193                 // r->label_box.set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
194                 r->label_box.signal_button_press_event().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::name_press));
195                 r->port_combo.signal_changed().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::port_choice_changed));
196                 r->use_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::use_button_toggled));
197                 r->collect_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::collect_button_toggled));
198                 r->request_options.signal_button_press_event().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::request_option_press), false);
199                 r->remove_button.signal_clicked().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::remove_clicked));
200
201                 if (ttm) {
202                         r->sclock_synced_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::sync_button_toggled));
203                 }
204
205                 r->tm->PropertyChanged.connect (r->property_change_connection, invalidator (*this), boost::bind (&TransportMastersWidget::Row::prop_change, r, _1), gui_context());
206
207                 PropertyChange all_change;
208                 all_change.add (Properties::locked);
209                 all_change.add (Properties::collect);
210                 all_change.add (Properties::connected);
211
212                 if (ttm) {
213                         all_change.add (Properties::fr2997);
214                         all_change.add (Properties::sclock_synced);
215                 }
216
217                 r->prop_change (all_change);
218         }
219 }
220
221 TransportMastersWidget::Row::Row (TransportMastersWidget& p)
222         : parent (p)
223         , request_option_menu (0)
224         , remove_button (X_("x"))
225         , name_editor (0)
226         , save_when (0)
227         , ignore_active_change (false)
228 {
229 }
230
231 bool
232 TransportMastersWidget::Row::name_press (GdkEventButton* ev)
233 {
234         if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
235                 Gtk::Window* toplevel = dynamic_cast<Gtk::Window*> (label.get_toplevel());
236                 if (!toplevel) {
237                         return false;
238                 }
239                 name_editor = new FloatingTextEntry (toplevel, tm->name());
240                 name_editor->use_text.connect (sigc::mem_fun (*this, &TransportMastersWidget::Row::name_edited));
241                 name_editor->show ();
242                 return true;
243         }
244         return false;
245 }
246
247 void
248 TransportMastersWidget::Row::remove_clicked ()
249 {
250 }
251
252 void
253 TransportMastersWidget::Row::name_edited (string str, int ignored)
254 {
255         tm->set_name (str);
256         /* floating text entry deletes itself */
257         name_editor = 0;
258 }
259
260 void
261 TransportMastersWidget::Row::prop_change (PropertyChange what_changed)
262 {
263         if (what_changed.contains (Properties::locked)) {
264         }
265
266         if (what_changed.contains (Properties::fr2997)) {
267                 fr2997_button.set_active (boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm)->fr2997());
268         }
269
270         if (what_changed.contains (Properties::sclock_synced)) {
271                 sclock_synced_button.set_active (boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm)->sample_clock_synced());
272         }
273
274         if (what_changed.contains (Properties::collect)) {
275                 collect_button.set_active (tm->collect());
276         }
277
278         if (what_changed.contains (Properties::connected)) {
279                 populate_port_combo ();
280         }
281
282         if (what_changed.contains (Properties::name)) {
283                 label.set_text (tm->name());
284         }
285 }
286
287 void
288 TransportMastersWidget::Row::use_button_toggled ()
289 {
290         if (use_button.get_active()) {
291                 parent.set_transport_master (tm);
292         }
293 }
294
295 void
296 TransportMastersWidget::Row::fr2997_button_toggled ()
297 {
298         boost::dynamic_pointer_cast<TimecodeTransportMaster>(tm)->set_fr2997 (fr2997_button.get_active());
299 }
300
301 void
302 TransportMastersWidget::Row::collect_button_toggled ()
303 {
304         tm->set_collect (collect_button.get_active());
305 }
306
307 void
308 TransportMastersWidget::Row::sync_button_toggled ()
309 {
310         tm->set_sample_clock_synced (sclock_synced_button.get_active());
311 }
312
313 bool
314 TransportMastersWidget::Row::request_option_press (GdkEventButton* ev)
315 {
316         if (ev->button == 1) {
317                 if (!request_option_menu) {
318                         build_request_options ();
319                 }
320                 request_option_menu->popup (1, ev->time);
321                 return true;
322         }
323         return false;
324 }
325
326 void
327 TransportMastersWidget::Row::build_request_options ()
328 {
329         using namespace Gtk::Menu_Helpers;
330
331         request_option_menu = manage (new Menu);
332
333         MenuList& items (request_option_menu->items());
334
335         items.push_back (CheckMenuElem (_("Accept speed-changing commands (start/stop)")));
336         Gtk::CheckMenuItem* i = dynamic_cast<Gtk::CheckMenuItem *> (&items.back ());
337         i->set_active (tm->request_mask() & TR_Speed);
338         items.push_back (CheckMenuElem (_("Accept locate commands")));
339         i = dynamic_cast<Gtk::CheckMenuItem *> (&items.back ());
340         i->set_active (tm->request_mask() & TR_Locate);
341 }
342
343 Glib::RefPtr<Gtk::ListStore>
344 TransportMastersWidget::Row::build_port_list (vector<string> const & ports)
345 {
346         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (port_columns);
347         TreeModel::Row row;
348
349         row = *store->append ();
350         row[port_columns.full_name] = string();
351         row[port_columns.short_name] = _("Disconnected");
352
353         for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
354
355                 if (AudioEngine::instance()->port_is_mine (*p)) {
356                         continue;
357                 }
358
359                 row = *store->append ();
360                 row[port_columns.full_name] = *p;
361
362                 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
363                 if (pn.empty ()) {
364                         pn = (*p).substr ((*p).find (':') + 1);
365                 }
366                 row[port_columns.short_name] = pn;
367         }
368
369         return store;
370 }
371
372 void
373 TransportMastersWidget::Row::populate_port_combo ()
374 {
375         if (!tm->port()) {
376                 port_combo.hide ();
377                 return;
378         } else {
379                 port_combo.show ();
380         }
381
382         vector<string> inputs;
383
384         if (tm->port()->type() == DataType::MIDI) {
385                 ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput), inputs);
386         } else {
387                 ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::AUDIO, ARDOUR::PortFlags (ARDOUR::IsOutput), inputs);
388         }
389
390         Glib::RefPtr<Gtk::ListStore> input = build_port_list (inputs);
391         bool input_found = false;
392         int n;
393
394         port_combo.set_model (input);
395
396         Gtk::TreeModel::Children children = input->children();
397         Gtk::TreeModel::Children::iterator i;
398         i = children.begin();
399         ++i; /* skip "Disconnected" */
400
401
402         for (n = 1;  i != children.end(); ++i, ++n) {
403                 string port_name = (*i)[port_columns.full_name];
404                 if (tm->port()->connected_to (port_name)) {
405                         port_combo.set_active (n);
406                         input_found = true;
407                         break;
408                 }
409         }
410
411         if (!input_found) {
412                 port_combo.set_active (0); /* disconnected */
413         }
414 }
415
416 void
417 TransportMastersWidget::Row::port_choice_changed ()
418 {
419         if (ignore_active_change) {
420                 return;
421         }
422
423         TreeModel::iterator active = port_combo.get_active ();
424         string new_port = (*active)[port_columns.full_name];
425
426         if (new_port.empty()) {
427                 tm->port()->disconnect_all ();
428                 return;
429         }
430
431         if (!tm->port()->connected_to (new_port)) {
432                 tm->port()->disconnect_all ();
433                 tm->port()->connect (new_port);
434         }
435 }
436
437 void
438 TransportMastersWidget::Row::update (Session* s, samplepos_t now)
439 {
440         using namespace Timecode;
441
442         samplepos_t pos;
443         double speed;
444         samplepos_t most_recent;
445         samplepos_t when;
446         stringstream ss;
447         Time t;
448         Time l;
449         boost::shared_ptr<TimecodeTransportMaster> ttm;
450         boost::shared_ptr<MIDIClock_TransportMaster> mtm;
451
452         if (s) {
453
454                 if (tm->speed_and_position (speed, pos, most_recent, when, now)) {
455
456                         sample_to_timecode (pos, t, false, false, 25, false, AudioEngine::instance()->sample_rate(), 100, false, 0);
457                         sample_to_timecode (most_recent, l, false, false, 25, false, AudioEngine::instance()->sample_rate(), 100, false, 0);
458
459                         if ((ttm = boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm))) {
460                                 format.set_text (timecode_format_name (ttm->apparent_timecode_format()));
461                                 last.set_text (Timecode::timecode_format_time (l));
462                         } else if ((mtm = boost::dynamic_pointer_cast<MIDIClock_TransportMaster> (tm))) {
463                                 char buf[8];
464                                 snprintf (buf, sizeof (buf), "%.1f", mtm->bpm());
465                                 format.set_text (buf);
466                                 last.set_text ("");
467                         } else {
468                                 format.set_text ("");
469                                 last.set_text ("");
470                         }
471                         current.set_text (Timecode::timecode_format_time (t));
472                         delta.set_markup (tm->delta_string ());
473
474                         char gap[32];
475                         const float seconds = (when - now) / (float) AudioEngine::instance()->sample_rate();
476                         if (abs (seconds) < 1.0) {
477                                 snprintf (gap, sizeof (gap), "%.3fs", seconds);
478                         } else if (abs (seconds) < 4.0) {
479                                 snprintf (gap, sizeof (gap), "%ds", (int) floor (seconds));
480                         } else {
481                                 snprintf (gap, sizeof (gap), "%s", _(">4s ago"));
482                         }
483                         timestamp.set_text (gap);
484                         save_when = when;
485
486                 } else {
487
488                         if (save_when) {
489                                 char gap[32];
490
491                                 const float seconds = (when - now) / (float) AudioEngine::instance()->sample_rate();
492                                 if (abs (seconds) < 1.0) {
493                                         snprintf (gap, sizeof (gap), "%.3fs", seconds);
494                                 } else if (abs (seconds) < 4.0) {
495                                         snprintf (gap, sizeof (gap), "%ds", (int) floor (seconds));
496                                 } else {
497                                         snprintf (gap, sizeof (gap), "%s", _(">4s ago"));
498                                 }
499                                 timestamp.set_text (gap);
500                                 save_when = when;
501                         }
502                         delta.set_text ("");
503                         current.set_text ("");
504                 }
505         }
506 }
507
508 void
509         TransportMastersWidget::update (samplepos_t /* audible */)
510 {
511         samplepos_t now = AudioEngine::instance()->sample_time ();
512
513         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
514                 (*r)->update (_session, now);
515         }
516 }
517
518 void
519 TransportMastersWidget::on_map ()
520 {
521         update_connection = ARDOUR_UI::Clock.connect (sigc::mem_fun (*this, &TransportMastersWidget::update));
522         Gtk::VBox::on_map ();
523 }
524
525 void
526 TransportMastersWidget::on_unmap ()
527 {
528         update_connection.disconnect ();
529         Gtk::VBox::on_unmap ();
530 }
531
532 TransportMastersWindow::TransportMastersWindow ()
533         : ArdourWindow (_("Transport Masters"))
534 {
535         add (w);
536         w.show ();
537 }
538
539 void
540 TransportMastersWindow::on_realize ()
541 {
542         ArdourWindow::on_realize ();
543         /* (try to) ensure that resizing is possible and the window can be moved (and closed) */
544         get_window()->set_decorations (Gdk::DECOR_BORDER | Gdk::DECOR_RESIZEH | Gdk::DECOR_TITLE | Gdk::DECOR_MENU);
545 }
546
547
548
549 void
550 TransportMastersWindow::set_session (ARDOUR::Session* s)
551 {
552         ArdourWindow::set_session (s);
553         w.set_session (s);
554 }