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