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