Faderport(Classic): Add Record-PreRoll and Record-CountIn actions to the footswitch...
[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 (current_connection, invalidator (*this), boost::bind (&TransportMastersWidget::rebuild, this), gui_context());
87         TransportMasterManager::instance().Removed.connect (current_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         TransportMasterManager::instance().add (LTC, "new ltc");
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         TransportMasterManager::instance().remove (tm->name());
251 }
252
253 void
254 TransportMastersWidget::Row::name_edited (string str, int ignored)
255 {
256         tm->set_name (str);
257         /* floating text entry deletes itself */
258         name_editor = 0;
259 }
260
261 void
262 TransportMastersWidget::Row::prop_change (PropertyChange what_changed)
263 {
264         if (what_changed.contains (Properties::locked)) {
265         }
266
267         if (what_changed.contains (Properties::fr2997)) {
268                 fr2997_button.set_active (boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm)->fr2997());
269         }
270
271         if (what_changed.contains (Properties::sclock_synced)) {
272                 sclock_synced_button.set_active (boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm)->sample_clock_synced());
273         }
274
275         if (what_changed.contains (Properties::collect)) {
276                 collect_button.set_active (tm->collect());
277         }
278
279         if (what_changed.contains (Properties::connected)) {
280                 populate_port_combo ();
281         }
282
283         if (what_changed.contains (Properties::name)) {
284                 label.set_text (tm->name());
285         }
286 }
287
288 void
289 TransportMastersWidget::Row::use_button_toggled ()
290 {
291         if (use_button.get_active()) {
292                 parent.set_transport_master (tm);
293         }
294 }
295
296 void
297 TransportMastersWidget::Row::fr2997_button_toggled ()
298 {
299         boost::dynamic_pointer_cast<TimecodeTransportMaster>(tm)->set_fr2997 (fr2997_button.get_active());
300 }
301
302 void
303 TransportMastersWidget::Row::collect_button_toggled ()
304 {
305         tm->set_collect (collect_button.get_active());
306 }
307
308 void
309 TransportMastersWidget::Row::sync_button_toggled ()
310 {
311         tm->set_sample_clock_synced (sclock_synced_button.get_active());
312 }
313
314 bool
315 TransportMastersWidget::Row::request_option_press (GdkEventButton* ev)
316 {
317         if (ev->button == 1) {
318                 if (!request_option_menu) {
319                         build_request_options ();
320                 }
321                 request_option_menu->popup (1, ev->time);
322                 return true;
323         }
324         return false;
325 }
326
327 void
328 TransportMastersWidget::Row::build_request_options ()
329 {
330         using namespace Gtk::Menu_Helpers;
331
332         request_option_menu = manage (new Menu);
333
334         MenuList& items (request_option_menu->items());
335
336         items.push_back (CheckMenuElem (_("Accept speed-changing commands (start/stop)")));
337         Gtk::CheckMenuItem* i = dynamic_cast<Gtk::CheckMenuItem *> (&items.back ());
338         i->set_active (tm->request_mask() & TR_Speed);
339         items.push_back (CheckMenuElem (_("Accept locate commands")));
340         i = dynamic_cast<Gtk::CheckMenuItem *> (&items.back ());
341         i->set_active (tm->request_mask() & TR_Locate);
342 }
343
344 Glib::RefPtr<Gtk::ListStore>
345 TransportMastersWidget::Row::build_port_list (vector<string> const & ports)
346 {
347         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (port_columns);
348         TreeModel::Row row;
349
350         row = *store->append ();
351         row[port_columns.full_name] = string();
352         row[port_columns.short_name] = _("Disconnected");
353
354         for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
355
356                 if (AudioEngine::instance()->port_is_mine (*p)) {
357                         continue;
358                 }
359
360                 row = *store->append ();
361                 row[port_columns.full_name] = *p;
362
363                 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
364                 if (pn.empty ()) {
365                         pn = (*p).substr ((*p).find (':') + 1);
366                 }
367                 row[port_columns.short_name] = pn;
368         }
369
370         return store;
371 }
372
373 void
374 TransportMastersWidget::Row::populate_port_combo ()
375 {
376         if (!tm->port()) {
377                 port_combo.hide ();
378                 return;
379         } else {
380                 port_combo.show ();
381         }
382
383         vector<string> inputs;
384
385         if (tm->port()->type() == DataType::MIDI) {
386                 ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput), inputs);
387         } else {
388                 ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::AUDIO, ARDOUR::PortFlags (ARDOUR::IsOutput), inputs);
389         }
390
391         Glib::RefPtr<Gtk::ListStore> input = build_port_list (inputs);
392         bool input_found = false;
393         int n;
394
395         port_combo.set_model (input);
396
397         Gtk::TreeModel::Children children = input->children();
398         Gtk::TreeModel::Children::iterator i;
399         i = children.begin();
400         ++i; /* skip "Disconnected" */
401
402
403         for (n = 1;  i != children.end(); ++i, ++n) {
404                 string port_name = (*i)[port_columns.full_name];
405                 if (tm->port()->connected_to (port_name)) {
406                         port_combo.set_active (n);
407                         input_found = true;
408                         break;
409                 }
410         }
411
412         if (!input_found) {
413                 port_combo.set_active (0); /* disconnected */
414         }
415 }
416
417 void
418 TransportMastersWidget::Row::port_choice_changed ()
419 {
420         if (ignore_active_change) {
421                 return;
422         }
423
424         TreeModel::iterator active = port_combo.get_active ();
425         string new_port = (*active)[port_columns.full_name];
426
427         if (new_port.empty()) {
428                 tm->port()->disconnect_all ();
429                 return;
430         }
431
432         if (!tm->port()->connected_to (new_port)) {
433                 tm->port()->disconnect_all ();
434                 tm->port()->connect (new_port);
435         }
436 }
437
438 void
439 TransportMastersWidget::Row::update (Session* s, samplepos_t now)
440 {
441         using namespace Timecode;
442
443         samplepos_t pos;
444         double speed;
445         samplepos_t most_recent;
446         samplepos_t when;
447         stringstream ss;
448         Time t;
449         Time l;
450         boost::shared_ptr<TimecodeTransportMaster> ttm;
451         boost::shared_ptr<MIDIClock_TransportMaster> mtm;
452
453         if (s) {
454
455                 if (tm->speed_and_position (speed, pos, most_recent, when, now)) {
456
457                         sample_to_timecode (pos, t, false, false, 25, false, AudioEngine::instance()->sample_rate(), 100, false, 0);
458                         sample_to_timecode (most_recent, l, false, false, 25, false, AudioEngine::instance()->sample_rate(), 100, false, 0);
459
460                         if ((ttm = boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm))) {
461                                 format.set_text (timecode_format_name (ttm->apparent_timecode_format()));
462                                 last.set_text (Timecode::timecode_format_time (l));
463                         } else if ((mtm = boost::dynamic_pointer_cast<MIDIClock_TransportMaster> (tm))) {
464                                 char buf[8];
465                                 snprintf (buf, sizeof (buf), "%.1f", mtm->bpm());
466                                 format.set_text (buf);
467                                 last.set_text ("");
468                         } else {
469                                 format.set_text ("");
470                                 last.set_text ("");
471                         }
472                         current.set_text (Timecode::timecode_format_time (t));
473                         delta.set_markup (tm->delta_string ());
474
475                         char gap[32];
476                         const float seconds = (when - now) / (float) AudioEngine::instance()->sample_rate();
477                         if (abs (seconds) < 1.0) {
478                                 snprintf (gap, sizeof (gap), "%.3fs", seconds);
479                         } else if (abs (seconds) < 4.0) {
480                                 snprintf (gap, sizeof (gap), "%ds", (int) floor (seconds));
481                         } else {
482                                 snprintf (gap, sizeof (gap), "%s", _(">4s ago"));
483                         }
484                         timestamp.set_text (gap);
485                         save_when = when;
486
487                 } else {
488
489                         if (save_when) {
490                                 char gap[32];
491
492                                 const float seconds = (when - now) / (float) AudioEngine::instance()->sample_rate();
493                                 if (abs (seconds) < 1.0) {
494                                         snprintf (gap, sizeof (gap), "%.3fs", seconds);
495                                 } else if (abs (seconds) < 4.0) {
496                                         snprintf (gap, sizeof (gap), "%ds", (int) floor (seconds));
497                                 } else {
498                                         snprintf (gap, sizeof (gap), "%s", _(">4s ago"));
499                                 }
500                                 timestamp.set_text (gap);
501                                 save_when = when;
502                         }
503                         delta.set_text ("");
504                         current.set_text ("");
505                 }
506         }
507 }
508
509 void
510         TransportMastersWidget::update (samplepos_t /* audible */)
511 {
512         samplepos_t now = AudioEngine::instance()->sample_time ();
513
514         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
515                 (*r)->update (_session, now);
516         }
517 }
518
519 void
520 TransportMastersWidget::on_map ()
521 {
522         update_connection = ARDOUR_UI::Clock.connect (sigc::mem_fun (*this, &TransportMastersWidget::update));
523         Gtk::VBox::on_map ();
524 }
525
526 void
527 TransportMastersWidget::on_unmap ()
528 {
529         update_connection.disconnect ();
530         Gtk::VBox::on_unmap ();
531 }
532
533 TransportMastersWindow::TransportMastersWindow ()
534         : ArdourWindow (_("Transport Masters"))
535 {
536         add (w);
537         w.show ();
538 }
539
540 void
541 TransportMastersWindow::on_realize ()
542 {
543         ArdourWindow::on_realize ();
544         /* (try to) ensure that resizing is possible and the window can be moved (and closed) */
545         get_window()->set_decorations (Gdk::DECOR_BORDER | Gdk::DECOR_RESIZEH | Gdk::DECOR_TITLE | Gdk::DECOR_MENU);
546 }
547
548
549
550 void
551 TransportMastersWindow::set_session (ARDOUR::Session* s)
552 {
553         ArdourWindow::set_session (s);
554         w.set_session (s);
555 }