silence extra buffers not handled/touched/filled by an AU (e.g. 2in/1out - silence...
[ardour2.git] / gtk2_ardour / connection_editor.cc
blob79d7605497c4ce849cdd51c83f2fc108d6819cb2
1 /*
2 Copyright (C) 2002 Paul Davis
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.
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.
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.
20 #include <map>
21 #include <vector>
22 #include <stdint.h>
24 #include <gtkmm2ext/gtk_ui.h>
25 #include <gtkmm2ext/utils.h>
26 #include <sigc++/bind.h>
28 #include "connection_editor.h"
30 #include <ardour/session.h>
31 #include <ardour/session_connection.h>
32 #include <ardour/audioengine.h>
33 #include <ardour/connection.h>
35 #include "utils.h"
36 #include "keyboard.h"
37 #include "prompter.h"
39 #include "i18n.h"
41 #include <inttypes.h>
43 using namespace std;
44 using namespace ARDOUR;
45 using namespace PBD;
46 using namespace Gtk;
47 using namespace sigc;
49 ConnectionEditor::ConnectionEditor ()
50 : ArdourDialog (_("ardour: connections")),
51 input_frame (_("Input Connections")),
52 output_frame (_("Output Connections")),
53 new_input_connection_button (_("New Input")),
54 new_output_connection_button (_("New Output")),
55 delete_connection_button (_("Delete")),
56 clear_button (_("Clear")),
57 add_port_button (_("Add Port")),
58 ok_button (_("Close")),
59 cancel_button (_("Cancel")),
60 rescan_button (_("Rescan"))
63 add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
65 session = 0;
66 selected_port = -1;
67 current_connection = 0;
68 push_at_front = false;
70 set_name ("ConnectionEditorWindow");
72 ok_button.set_name ("ConnectionEditorButton");
73 cancel_button.set_name ("ConnectionEditorButton");
74 rescan_button.set_name ("ConnectionEditorButton");
75 new_input_connection_button.set_name ("ConnectionEditorButton");
76 new_output_connection_button.set_name ("ConnectionEditorButton");
77 clear_button.set_name ("ConnectionEditorButton");
79 button_frame.set_name ("ConnectionEditorFrame");
80 input_frame.set_name ("ConnectionEditorFrame");
81 output_frame.set_name ("ConnectionEditorFrame");
83 button_box.set_spacing (15);
84 button_box.set_border_width (5);
85 Gtkmm2ext::set_size_request_to_display_given_text (ok_button, _("OK"), 40, 15);
86 button_box.pack_end (ok_button, false, false);
87 // button_box.pack_end (cancel_button, false, false);
88 cancel_button.hide();
89 button_frame.add (button_box);
91 ok_button.signal_clicked().connect (mem_fun(*this, &ConnectionEditor::accept));
92 cancel_button.signal_clicked().connect (mem_fun(*this, &ConnectionEditor::cancel));
93 cancel_button.signal_clicked().connect (mem_fun(*this, &ConnectionEditor::rescan));
95 notebook.set_name ("ConnectionEditorNotebook");
96 notebook.set_size_request (-1, 125);
98 clear_button.set_name ("ConnectionEditorButton");
99 add_port_button.set_name ("ConnectionEditorButton");
100 Gtkmm2ext::set_size_request_to_display_given_text (add_port_button, _("Add Port"), 35, 15);
102 selector_frame.set_name ("ConnectionEditorFrame");
103 port_frame.set_name ("ConnectionEditorFrame");
105 selector_frame.set_label (_("Available Ports"));
107 selector_button_box.set_spacing (5);
108 selector_button_box.set_border_width (5);
109 Gtkmm2ext::set_size_request_to_display_given_text (rescan_button, _("Rescan"), 35, 15);
110 selector_button_box.pack_start (rescan_button, false, false);
112 selector_box.set_spacing (5);
113 selector_box.set_border_width (5);
114 selector_box.pack_start (notebook);
115 selector_box.pack_start (selector_button_box);
117 selector_frame.add (selector_box);
119 port_box.set_spacing (5);
120 port_box.set_border_width (3);
122 port_button_box.set_spacing (5);
123 port_button_box.set_border_width (2);
125 port_button_box.pack_start (add_port_button, false, false);
126 port_and_button_box.set_border_width (5);
127 port_and_button_box.pack_start (port_button_box, false, false);
128 port_and_button_box.pack_start (port_box);
130 port_frame.add (port_and_button_box);
132 port_and_selector_box.set_spacing (5);
133 port_and_selector_box.pack_start (port_frame);
134 port_and_selector_box.pack_start (selector_frame);
136 right_vbox.set_spacing (5);
137 right_vbox.set_border_width (5);
138 right_vbox.pack_start (port_and_selector_box);
140 input_connection_model = ListStore::create (connection_columns);
141 output_connection_model = ListStore::create (connection_columns);
143 input_connection_display.set_model (input_connection_model);
144 output_connection_display.set_model (output_connection_model);
146 input_connection_display.append_column (_("Connections"), connection_columns.name);
147 output_connection_display.append_column (_("Connections"), connection_columns.name);
149 input_connection_display.get_selection()->set_mode(Gtk::SELECTION_SINGLE);
150 input_connection_display.set_size_request (80, -1);
151 input_connection_display.set_name ("ConnectionEditorConnectionList");
153 output_connection_display.get_selection()->set_mode(Gtk::SELECTION_SINGLE);
154 output_connection_display.set_size_request (80, -1);
155 output_connection_display.set_name ("ConnectionEditorConnectionList");
157 input_connection_display.get_selection()->signal_changed().connect (bind (mem_fun(*this, &ConnectionEditor::selection_changed), &input_connection_display));
158 output_connection_display.get_selection()->signal_changed().connect (bind (mem_fun(*this, &ConnectionEditor::selection_changed), &output_connection_display));
161 input_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
162 output_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
164 input_scroller.add (input_connection_display);
165 output_scroller.add (output_connection_display);
167 input_box.set_border_width (5);
168 input_box.set_spacing (5);
169 input_box.pack_start (input_scroller);
170 input_box.pack_start (new_input_connection_button, false, false);
171 input_frame.add (input_box);
173 output_box.set_border_width (5);
174 output_box.set_spacing (5);
175 output_box.pack_start (output_scroller);
176 output_box.pack_start (new_output_connection_button, false, false);
177 output_frame.add (output_box);
179 connection_box.set_spacing (5);
180 connection_box.pack_start (input_frame);
181 connection_box.pack_start (output_frame);
183 left_vbox.set_spacing (5);
184 left_vbox.pack_start (connection_box);
186 main_hbox.set_border_width (10);
187 main_hbox.set_spacing (5);
188 main_hbox.pack_start (left_vbox);
189 main_hbox.pack_start (right_vbox);
191 main_vbox.set_border_width (10);
192 main_vbox.set_spacing (5);
193 main_vbox.pack_start (main_hbox);
194 main_vbox.pack_start (button_frame, false, false);
196 get_vbox()->pack_start (main_vbox);
198 clear_button.signal_clicked().connect (mem_fun(*this, &ConnectionEditor::clear));
199 add_port_button.signal_clicked().connect (mem_fun(*this, &ConnectionEditor::add_port));
200 new_input_connection_button.signal_clicked().connect (bind (mem_fun(*this, &ConnectionEditor::new_connection), true));
201 new_output_connection_button.signal_clicked().connect (bind (mem_fun(*this, &ConnectionEditor::new_connection), false));
202 delete_connection_button.signal_clicked().connect (mem_fun(*this, &ConnectionEditor::delete_connection));
205 ConnectionEditor::~ConnectionEditor()
209 void
210 ConnectionEditor::set_session (Session *s)
212 if (s != session) {
214 ArdourDialog::set_session (s);
216 if (session) {
217 session->ConnectionAdded.connect (mem_fun(*this, &ConnectionEditor::proxy_add_connection_and_select));
218 session->ConnectionRemoved.connect (mem_fun(*this, &ConnectionEditor::proxy_remove_connection));
219 } else {
220 hide ();
225 void
226 ConnectionEditor::rescan ()
228 refill_connection_display ();
229 display_ports ();
232 void
233 ConnectionEditor::cancel ()
235 hide ();
238 void
239 ConnectionEditor::accept ()
241 hide ();
244 void
245 ConnectionEditor::clear ()
247 if (current_connection) {
248 current_connection->clear ();
252 void
253 ConnectionEditor::on_map ()
255 refill_connection_display ();
256 Window::on_map ();
259 void
260 ConnectionEditor::add_connection (ARDOUR::Connection *connection)
262 TreeModel::Row row;
264 if (dynamic_cast<InputConnection *> (connection)) {
266 if (push_at_front) {
267 row = *(input_connection_model->prepend());
268 } else {
269 row = *(input_connection_model->append());
272 } else {
274 if (push_at_front) {
275 row = *(output_connection_model->prepend());
276 } else {
277 row = *(output_connection_model->append());
281 row[connection_columns.connection] = connection;
282 row[connection_columns.name] = connection->name();
285 void
286 ConnectionEditor::remove_connection (ARDOUR::Connection *connection)
288 TreeModel::iterator i;
289 Glib::RefPtr<TreeModel> model = input_connection_model;
291 if (dynamic_cast<InputConnection *> (connection) == 0) {
292 model = output_connection_model;
295 TreeModel::Children rows = model->children();
297 for (i = rows.begin(); i != rows.end(); ++i) {
298 if ((*i)[connection_columns.connection] == connection) {
299 // model->erase (i);
300 break;
305 void
306 ConnectionEditor::proxy_add_connection_and_select (ARDOUR::Connection *connection)
308 Gtkmm2ext::UI::instance()->call_slot (bind (mem_fun(*this, &ConnectionEditor::add_connection_and_select), connection));
311 void
312 ConnectionEditor::proxy_remove_connection (ARDOUR::Connection *connection)
314 Gtkmm2ext::UI::instance()->call_slot (bind (mem_fun(*this, &ConnectionEditor::remove_connection), connection));
317 void
318 ConnectionEditor::add_connection_and_select (ARDOUR::Connection *connection)
320 add_connection (connection);
322 // GTK2FIX
323 // if (dynamic_cast<InputConnection *> (connection)) {
324 // input_connection_display.rows().front().select ();
325 // } else {
326 // output_connection_display.rows().front().select ();
330 void
331 ConnectionEditor::refill_connection_display ()
333 input_connection_display.set_model (Glib::RefPtr<TreeModel>(0));
334 output_connection_display.set_model (Glib::RefPtr<TreeModel>(0));
336 input_connection_model->clear();
337 output_connection_model->clear();
339 current_connection = 0;
341 if (session) {
342 session->foreach_connection (this, &ConnectionEditor::add_connection);
345 input_connection_display.set_model (input_connection_model);
346 output_connection_display.set_model (output_connection_model);
350 void
351 ConnectionEditor::selection_changed (TreeView* view)
353 ARDOUR::Connection *old_current = current_connection;
355 TreeIter iter;
356 TreeModel::Path path;
357 Glib::RefPtr<TreeView::Selection> selection = view->get_selection();
358 Glib::RefPtr<TreeModel> model = view->get_model();
359 bool input = (view == &input_connection_display);
361 iter = model->get_iter (path);
363 current_connection = (*iter)[connection_columns.connection];
365 if (old_current != current_connection) {
366 config_connection.disconnect ();
367 connect_connection.disconnect ();
370 if (current_connection) {
371 config_connection = current_connection->ConfigurationChanged.connect
372 (bind (mem_fun(*this, &ConnectionEditor::configuration_changed), input));
373 connect_connection = current_connection->ConnectionsChanged.connect
374 (bind (mem_fun(*this, &ConnectionEditor::connections_changed), input));
377 display_connection_state (input);
378 display_ports ();
381 void
382 ConnectionEditor::configuration_changed (bool for_input)
384 display_connection_state (for_input);
387 void
388 ConnectionEditor::connections_changed (int which_port, bool for_input)
390 display_connection_state (for_input);
393 void
394 ConnectionEditor::display_ports ()
396 if (session == 0 || current_connection == 0) {
397 return;
400 using namespace Notebook_Helpers;
402 typedef std::map<std::string,std::vector<std::pair<std::string,std::string> > > PortMap;
403 PortMap portmap;
404 const char **ports;
405 PageList& pages = notebook.pages();
406 gint current_page;
407 vector<string> rowdata;
408 bool for_input;
410 current_page = notebook.get_current_page ();
411 pages.clear ();
413 /* get relevant current JACK ports */
415 for_input = (dynamic_cast<InputConnection *> (current_connection) != 0);
417 ports = session->engine().get_ports ("", JACK_DEFAULT_AUDIO_TYPE, for_input?JackPortIsOutput:JackPortIsInput);
419 if (ports == 0) {
420 return;
423 /* find all the client names and group their ports into a list-by-client */
425 for (int n = 0; ports[n]; ++n) {
427 pair<string,vector<pair<string,string> > > newpair;
428 pair<string,string> strpair;
429 pair<PortMap::iterator,bool> result;
431 string str = ports[n];
432 string::size_type pos;
433 string portname;
435 pos = str.find (':');
437 newpair.first = str.substr (0, pos);
438 portname = str.substr (pos+1);
440 result = portmap.insert (newpair);
442 strpair.first = portname;
443 strpair.second = str;
445 result.first->second.push_back (strpair);
448 PortMap::iterator i;
450 for (i = portmap.begin(); i != portmap.end(); ++i) {
452 Box *client_box = manage (new VBox);
453 Gtk::CTreeView *display = manage (new Gtk::TreeView);
454 RefPtr<TreeModel> model = TreeModel::create (columns);
455 ScrolledWindow *scroller = manage (new ScrolledWindow);
457 display->set_selection_mode (GTK_SELECTION_SINGLE);
458 display->set_name ("ConnectionEditorList");
460 for (vector<pair<string,string> >::iterator s = i->second.begin(); s != i->second.end(); ++s) {
462 Row row = model->append ();
464 row[displayed_name] = s->first;
465 row[full_name] = s->second;
468 display->get_selection()->signal_changed().connect (bind (mem_fun(*this, &ConnectionEditor::port_selection_handler), display));
470 Label *tab_label = manage (new Label);
472 tab_label->set_name ("ConnectionEditorNotebookTab");
473 tab_label->set_text ((*i).first);
475 display->set_model (model);
477 scroller->add (*client_port_display);
478 scroller->set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
480 client_box->pack_start (*scroller);
482 pages.push_back (TabElem (*client_box, *tab_label));
485 notebook.set_page (current_page);
486 notebook.show.connect (bind (mem_fun (notebook, &Notebook::set_page), current_page));
487 selector_box.show_all ();
490 void
491 ConnectionEditor::display_connection_state (bool for_input)
493 Glib::Mutex::Lock lm (port_display_lock);
494 uint32_t limit;
496 if (session == 0 || current_connection == 0) {
497 return;
500 string frame_label = _("Connection \"");
501 frame_label += current_connection->name();
502 frame_label += _("\"");
503 port_frame.set_label (frame_label);
505 for (slist<ScrolledWindow *>::iterator i = port_displays.begin(); i != port_displays.end(); ) {
507 slist<ScrolledWindow *>::iterator tmp;
509 tmp = i;
510 tmp++;
512 port_box.remove (**i);
513 delete *i;
514 port_displays.erase (i);
516 i = tmp;
519 limit = current_connection->nports();
521 for (uint32_t n = 0; n < limit; ++n) {
523 CList *clist;
524 ScrolledWindow *scroller;
526 const gchar *title[1];
527 char buf[32];
528 string really_short_name;
530 if (for_input) {
531 snprintf(buf, sizeof(buf)-1, _("in %d"), n+1);
532 } else {
533 snprintf(buf, sizeof(buf)-1, _("out %d"), n+1);
536 tview = manage (new TreeView());
537 Glib::RefPtr<ListStore> port_model = ListStore::create (*port_display_columns);
539 tview->set_model (port_model);
540 tview->append_column (_(buf), port_display_columns->name);
541 tview->set_selection()->set_mode (Gtk::SELECTION_SINGLE);
542 tview->set_data ("port", (gpointer) ((intptr_t) n));
543 tview->set_headers_visible (true);
544 tview->set_name ("ConnectionEditorPortList");
545 tview->signal_button_press_event().connect (bind (mem_fun(*this, &ConnectionEditor::port_column_click), clist));
547 scroller = manage (new ScrolledWindow);
549 scroller->add (*tview);
550 scroller->set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
552 port_displays.insert (port_displays.end(), scroller);
553 port_box.pack_start (*scroller);
555 scroller->set_size_request (-1, 75);
557 /* now fill the clist with the current connections */
559 const ARDOUR::Connection::PortList& connections = current_connection->port_connections (n);
561 for (ARDOUR::Connection::PortList::const_iterator i = connections.begin(); i != connections.end(); ++i) {
563 TreeModel::Row row = *(model->append());
565 row[port_connection_columns.name] = (*i)->name();
569 port_box.show_all ();
572 void
573 ConnectionEditor::port_selection_changed (TreeView* tview)
575 Glib::RefPtr<TreeView::Selection> sel = tview->get_selection();
576 TreeModel::iterator iter = sel->get_selected();
578 if (!current_connection) {
579 return;
582 if (iter) {
583 TreeModel::Row row = *iter;
584 string other_port_name = row[port_display_columns.full_name];
587 if (current_connection && selected_port >= 0) {
588 current_connection->add_connection (selected_port, other_port_name);
593 void
594 ConnectionEditor::add_port ()
596 if (current_connection) {
597 current_connection->add_port ();
601 void
602 ConnectionEditor::connection_port_button_press_event (GdkEventButton* ev, TreeView* tview)
604 Glib::Mutex::Lock lm (port_display_lock);
606 int which_port = reinterpret_cast<intptr_t> (treeview->get_data ("port"));
608 if (which_port != selected_port) {
610 selected_port = which_port;
611 display_ports ();
613 tview->set_name ("ConnectionEditorPortListSelected");
615 for (slist<ScrolledWindow *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
617 Widget *child = (*i)->get_child();
619 if (static_cast<TreeView *> (child) != tview) {
620 child->set_name ("ConnectionEditorPortList");
621 child->queue_draw ();
625 } else {
627 selected_port = -1;
628 clist->set_name ("ConnectionEditorPortList");
629 clist->queue_draw();
633 void
634 ConnectionEditor::connection_selection_changed (TreeView* tview);
636 Glib::RefPtr<TreeView::Selection> sel = tview->get_selection();
637 TreeModel::iterator iter = sel->get_selected();
639 if (iter) {
640 TreeModel::Row row = *iter;
641 current_connection = row[XXXX_display_columns.connection];
642 } else {
643 current_connection = 0;
647 void
648 ConnectionEditor::new_connection (bool for_input)
650 string name;
652 if (session == 0) {
653 return;
656 ArdourPrompter prompter (true);
657 prompter.set_prompt (_("Name for new connection:"));
658 prompter.done.connect (Gtk::Main::quit.slot());
660 switch (prompter.run()) {
661 case Gtk::RESPONSE_ACCEPT:
662 prompter.get_result (name);
663 push_at_front = true;
664 if (name.length()) {
665 if (for_input) {
666 session->add_connection (new ARDOUR::InputConnection (name));
667 } else {
668 session->add_connection (new ARDOUR::OutputConnection (name));
671 push_at_front = false;
672 break;
674 default:
675 break;
679 void
680 ConnectionEditor::delete_connection ()
682 if (session && current_connection) {
683 session->remove_connection (current_connection);
684 current_connection = 0;
688 gint
689 ConnectionEditor::port_button_event (GdkEventButton *ev, Treeview* treeview)
691 int row, col;
692 TreeIter iter;
693 TreeModel::Path path;
694 TreeViewColumn* column;
695 int cellx;
696 int celly;
698 if (current_connection == 0) {
699 return false;
702 if (!(Keyboard::is_delete_event (ev))) {
703 return false;
706 if (!treeview->get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
707 return false;
710 if ((iter = treeview->get_model()->get_iter (path))) {
711 /* path is valid */
713 string port_name = (*iter)[columns.full_name];
714 int which_port = (intptr_t) treeview->get_data ("port");
716 current_connection->remove_connection (which_port, port_name);
719 return true;