2 Copyright (C) 2002-2003 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.
24 #include <pbd/lockmonitor.h>
26 #include <ardour/io.h>
27 #include <ardour/route.h>
28 #include <ardour/audioengine.h>
29 #include <ardour/port.h>
30 #include <ardour/insert.h>
31 #include <ardour/session.h>
32 #include <ardour/diskstream.h>
34 #include <gtkmm2ext/doi.h>
35 #include <gtkmm2ext/gtk_ui.h>
38 #include "ardour_message.h"
39 #include "io_selector.h"
41 #include "gui_thread.h"
49 using namespace ARDOUR;
51 IOSelectorWindow::IOSelectorWindow (Session& sess, IO& ior, bool input, bool can_cancel)
52 : ArdourDialog ("i/o selector"),
53 _selector (sess, ior, input),
54 ok_button (can_cancel ? _("OK"): _("Close")),
55 cancel_button (_("Cancel")),
56 rescan_button (_("Rescan"))
59 add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
60 set_name ("IOSelectorWindow");
64 title = string_compose(_("%1 input"), ior.name());
66 title = string_compose(_("%1 output"), ior.name());
69 ok_button.set_name ("IOSelectorButton");
70 cancel_button.set_name ("IOSelectorButton");
71 rescan_button.set_name ("IOSelectorButton");
73 button_box.set_spacing (5);
74 button_box.set_border_width (5);
75 button_box.set_homogeneous (true);
76 button_box.pack_start (rescan_button);
79 button_box.pack_start (cancel_button);
84 button_box.pack_start (ok_button);
86 get_vbox()->pack_start (_selector);
87 get_vbox()->pack_start (button_box, false, false);
89 ok_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::accept));
90 cancel_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::cancel));
91 rescan_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::rescan));
94 set_position (WIN_POS_MOUSE);
96 signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this)));
99 IOSelectorWindow::~IOSelectorWindow()
104 IOSelectorWindow::rescan ()
106 _selector.redisplay ();
110 IOSelectorWindow::cancel ()
112 _selector.Finished(IOSelector::Cancelled);
117 IOSelectorWindow::accept ()
119 _selector.Finished(IOSelector::Accepted);
124 IOSelectorWindow::on_map ()
126 _selector.redisplay ();
130 /*************************
131 The IO Selector "widget"
132 *************************/
134 IOSelector::IOSelector (Session& sess, IO& ior, bool input)
138 port_frame (for_input? _("Inputs") : _("Outputs")),
139 add_port_button (for_input? _("Add Input") : _("Add Output")),
140 remove_port_button (for_input? _("Remove Input") : _("Remove Output")),
141 clear_connections_button (_("Disconnect All"))
145 notebook.set_name ("IOSelectorNotebook");
146 notebook.set_size_request (-1, 125);
148 clear_connections_button.set_name ("IOSelectorButton");
149 add_port_button.set_name ("IOSelectorButton");
150 remove_port_button.set_name ("IOSelectorButton");
152 selector_frame.set_name ("IOSelectorFrame");
153 port_frame.set_name ("IOSelectorFrame");
155 selector_frame.set_label (_("Available connections"));
157 selector_button_box.set_spacing (5);
158 selector_button_box.set_border_width (5);
160 selector_box.set_spacing (5);
161 selector_box.set_border_width (5);
162 selector_box.pack_start (notebook);
163 selector_box.pack_start (selector_button_box, false, false);
165 selector_frame.add (selector_box);
167 port_box.set_spacing (5);
168 port_box.set_border_width (5);
170 port_display_scroller.set_name ("IOSelectorNotebook");
171 port_display_scroller.set_border_width (0);
172 port_display_scroller.set_size_request (-1, 170);
173 port_display_scroller.add (port_box);
174 port_display_scroller.set_policy (POLICY_NEVER,
177 port_button_box.set_spacing (5);
178 port_button_box.set_border_width (5);
180 port_button_box.pack_start (add_port_button, false, false);
183 if (io.input_maximum() < 0 || io.input_maximum() > (int) io.n_inputs()) {
184 add_port_button.set_sensitive (true);
186 add_port_button.set_sensitive (false);
190 if (io.output_maximum() < 0 || io.output_maximum() > (int) io.n_outputs()) {
191 add_port_button.set_sensitive (true);
193 add_port_button.set_sensitive (false);
198 port_button_box.pack_start (remove_port_button, false, false);
201 if (io.input_minimum() < 0 || io.input_minimum() < (int) io.n_inputs()) {
202 remove_port_button.set_sensitive (true);
204 remove_port_button.set_sensitive (false);
208 if (io.output_minimum() < 0 || io.output_minimum() < (int) io.n_outputs()) {
209 remove_port_button.set_sensitive (true);
211 remove_port_button.set_sensitive (false);
215 port_button_box.pack_start (clear_connections_button, false, false);
217 port_and_button_box.set_border_width (5);
218 port_and_button_box.pack_start (port_button_box, false, false);
219 port_and_button_box.pack_start (port_display_scroller);
221 port_frame.add (port_and_button_box);
223 port_and_selector_box.set_spacing (5);
224 port_and_selector_box.pack_start (port_frame);
225 port_and_selector_box.pack_start (selector_frame);
228 set_border_width (5);
229 pack_start (port_and_selector_box);
234 clear_connections_button.signal_clicked().connect (mem_fun(*this, &IOSelector::clear_connections));
236 add_port_button.signal_clicked().connect (mem_fun(*this, &IOSelector::add_port));
237 remove_port_button.signal_clicked().connect (mem_fun(*this, &IOSelector::remove_port));
240 io.input_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
242 io.output_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
245 io.name_changed.connect (mem_fun(*this, &IOSelector::name_changed));
248 IOSelector::~IOSelector ()
253 IOSelector::name_changed (void* src)
255 ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelector::name_changed), src));
261 IOSelector::clear_connections ()
264 io.disconnect_inputs (this);
266 io.disconnect_outputs (this);
271 IOSelector::rescan ()
273 using namespace Notebook_Helpers;
275 typedef std::map<string,vector<pair<string,string> > > PortMap;
278 PageList& pages = notebook.pages();
280 vector<string> rowdata;
282 current_page = notebook.get_current_page ();
285 /* get relevant current JACK ports */
287 ports = session.engine().get_ports ("", JACK_DEFAULT_AUDIO_TYPE, for_input?JackPortIsOutput:JackPortIsInput);
293 /* get relevant current JACK ports */
295 ports = session.engine().get_ports ("", JACK_DEFAULT_AUDIO_TYPE, for_input?JackPortIsOutput:JackPortIsInput);
301 /* find all the client names and group their ports into a list-by-client */
303 for (int n = 0; ports[n]; ++n) {
305 pair<string,vector<pair<string,string> > > newpair;
306 pair<string,string> strpair;
307 pair<PortMap::iterator,bool> result;
309 string str = ports[n];
310 string::size_type pos;
313 pos = str.find (':');
315 newpair.first = str.substr (0, pos);
316 portname = str.substr (pos+1);
318 result = portmap.insert (newpair);
320 strpair.first = portname;
321 strpair.second = str;
323 result.first->second.push_back (strpair);
328 for (i = portmap.begin(); i != portmap.end(); ++i) {
330 Box *client_box = manage (new VBox);
331 TreeView *display = manage (new TreeView);
332 RefPtr<ListStore> model = ListStore::create (port_display_columns);
333 ScrolledWindow *scroller = manage (new ScrolledWindow);
335 display->set_model (model);
336 display->append_column (X_("notvisible"), port_display_columns.displayed_name);
337 display->set_headers_visible (false);
338 display->get_selection()->set_mode (SELECTION_SINGLE);
339 display->set_name ("IOSelectorList");
341 for (vector<pair<string,string> >::iterator s = i->second.begin(); s != i->second.end(); ++s) {
343 TreeModel::Row row = *(model->append ());
345 row[port_display_columns.displayed_name] = s->first;
346 row[port_display_columns.full_name] = s->second;
349 display->get_selection()->signal_changed().connect
350 (bind (mem_fun(*this, &IOSelector::port_selection_changed), display));
352 Label *tab_label = manage (new Label);
354 tab_label->set_name ("IOSelectorNotebookTab");
355 tab_label->set_text ((*i).first);
357 scroller->add (*display);
358 scroller->set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC);
360 client_box->pack_start (*scroller);
362 pages.push_back (TabElem (*client_box, *tab_label));
365 notebook.set_current_page (current_page);
366 notebook.signal_show().connect (bind (mem_fun (notebook, &Notebook::set_current_page), current_page));
367 selector_box.show_all ();
371 IOSelector::display_ports ()
373 TreeView *firsttview = 0;
374 TreeView *selected_port_tview = 0;
377 LockMonitor lm (port_display_lock, __LINE__, __FILE__);
382 limit = io.n_inputs();
384 limit = io.n_outputs();
387 for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ) {
389 slist<TreeView *>::iterator tmp;
394 port_box.remove (**i);
396 port_displays.erase (i);
402 for (uint32_t n = 0; n < limit; ++n) {
405 ScrolledWindow *scroller;
406 string really_short_name;
411 port = io.output (n);
414 /* we know there is '/' because we put it there */
416 really_short_name = port->short_name();
417 really_short_name = really_short_name.substr (really_short_name.find ('/') + 1);
419 tview = manage (new TreeView());
420 RefPtr<ListStore> port_model = ListStore::create (port_display_columns);
426 tview->set_model (port_model);
427 tview->append_column (really_short_name, port_display_columns.displayed_name);
428 tview->get_selection()->set_mode (SELECTION_SINGLE);
429 tview->set_data ("port", port);
430 tview->set_headers_visible (true);
431 tview->set_name ("IOSelectorPortList");
433 scroller = manage (new ScrolledWindow);
435 scroller->add (*tview);
436 scroller->set_policy (POLICY_NEVER, POLICY_AUTOMATIC);
438 port_displays.insert (port_displays.end(), tview);
439 port_box.pack_start (*scroller);
441 scroller->set_size_request (-1, 75);
443 /* now fill the clist with the current connections */
446 const char **connections = port->get_connections ();
449 for (uint32_t c = 0; connections[c]; ++c) {
450 TreeModel::Row row = *(port_model->append());
451 row[port_display_columns.displayed_name] = connections[c];
457 if (io.input_maximum() == 1) {
458 selected_port = port;
459 selected_port_tview = tview;
461 if (port == selected_port) {
462 selected_port_tview = tview;
468 if (io.output_maximum() == 1) {
469 selected_port = port;
470 selected_port_tview = tview;
472 if (port == selected_port) {
473 selected_port_tview = tview;
478 TreeViewColumn* col = tview->get_column (0);
480 col->set_clickable (true);
482 /* handle button events on the column header and within the treeview itself */
484 //col->get_widget()->signal_button_release_event().connect (bind (mem_fun(*this, &IOSelector::port_column_button_release), tview));
485 tview->signal_button_release_event().connect (bind (mem_fun(*this, &IOSelector::connection_button_release), tview));
488 port_box.show_all ();
490 if (selected_port_tview) {
492 // selected_port_tview->click_column(0);
493 selected_port_tview->set_name ("IOSelectorPortListSelected");
494 for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
495 if (*i != selected_port_tview) {
496 (*i)->set_name ("IOSelectorPortList");
502 selector_box.hide_all ();
506 if (selected_port_tview) {
507 select_treeview (selected_port_tview);
508 } else if (firsttview) {
510 select_treeview (firsttview);
515 IOSelector::port_selection_changed (TreeView* treeview)
517 TreeModel::iterator i = treeview->get_selection()->get_selected();
524 if (selected_port == 0) {
528 ustring other_port_name = (*i)[port_display_columns.full_name];
531 if ((status = io.connect_input (selected_port, other_port_name, this)) == 0) {
532 Port *p = session.engine().get_port_by_name (other_port_name);
533 p->enable_metering();
536 status = io.connect_output (selected_port, other_port_name, this);
540 select_next_treeview ();
545 IOSelector::ports_changed (IOChange change, void *src)
547 ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelector::ports_changed), change, src));
553 IOSelector::add_port ()
555 /* add a new port, then hide the button if we're up to the maximum allowed */
560 io.add_input_port ("", this);
563 catch (AudioEngine::PortRegistrationFailure& err) {
564 ArdourMessage msg (0, X_("noport dialog"), _("There are no more JACK ports available."));
567 if (io.input_maximum() >= 0 && io.input_maximum() <= (int) io.n_inputs()) {
568 add_port_button.set_sensitive (false);
571 if (io.input_minimum() < (int) io.n_inputs()) {
572 remove_port_button.set_sensitive (true);
578 io.add_output_port ("", this);
581 catch (AudioEngine::PortRegistrationFailure& err) {
582 ArdourMessage msg (0, X_("noport dialog"),
583 _("There are no more JACK ports available."));
586 if (io.output_maximum() >= 0 && io.output_maximum() <= (int) io.n_outputs()) {
587 add_port_button.set_sensitive (false);
593 IOSelector::remove_port ()
597 // always remove last port
600 if ((nports = io.n_inputs()) > 0) {
601 io.remove_input_port (io.input(nports-1), this);
603 if (io.input_minimum() == (int) io.n_inputs()) {
604 remove_port_button.set_sensitive (false);
607 if ((nports = io.n_outputs()) > 0) {
608 io.remove_output_port (io.output(nports-1), this);
614 IOSelector::remove_port_when_idle (Port *port)
617 io.remove_input_port (port, this);
619 io.remove_output_port (port, this);
626 IOSelector::connection_button_release (GdkEventButton *ev, TreeView *treeview)
628 /* this handles button release on a port name row: i.e. a connection
629 between the named port and the port represented by the treeview.
633 TreeModel::Path path;
634 TreeViewColumn* column;
638 /* only handle button1 events here */
640 if (ev->button != 1) {
644 if (!(Keyboard::is_delete_event (ev))) {
648 if (!treeview->get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
652 if ((iter = treeview->get_model()->get_iter (path))) {
656 ustring connected_port_name = (*iter)[port_display_columns.full_name];
657 Port *port = reinterpret_cast<Port *> (treeview->get_data (_("port")));
660 Port *p = session.engine().get_port_by_name (connected_port_name);
661 p->disable_metering();
662 io.disconnect_input (port, connected_port_name, this);
664 io.disconnect_output (port, connected_port_name, this);
672 IOSelector::port_column_button_release (GdkEventButton *event, TreeView* treeview)
674 /* this handles button release on the button at the top of a single-column
675 treeview (representing a port)
678 if (Keyboard::is_delete_event (event)) {
681 LockMonitor lm (port_display_lock, __LINE__, __FILE__);
683 port = static_cast<Port *> (treeview->get_data (_("port")));
685 if (port == selected_port) {
687 treeview->set_name ("IOSelectorPortList");
688 treeview->queue_draw();
692 /* remove the port when idle - if we do it here, we will destroy the widget
693 for whom we are handling an event. not good.
696 signal_idle().connect (bind (mem_fun(*this, &IOSelector::remove_port_when_idle), port));
699 select_treeview (treeview);
706 IOSelector::select_next_treeview ()
708 slist<TreeView*>::iterator next;
710 for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
712 if ((*i)->get_name() == "IOSelectorPortListSelected") {
716 if (i == port_displays.end()) {
717 select_treeview (port_displays.front());
719 select_treeview (*i);
728 IOSelector::select_treeview (TreeView* tview)
730 /* Gack. TreeView's don't respond visually to a change
731 in their state, so rename them to force a style
735 LockMonitor lm (port_display_lock, __LINE__, __FILE__);
736 Port* port = reinterpret_cast<Port *> (tview->get_data (_("port")));
738 if (port != selected_port) {
739 selected_port = port;
741 tview->set_name ("IOSelectorPortListSelected");
743 for (slist<TreeView*>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
745 (*i)->set_name ("IOSelectorPortList");
749 selector_box.show_all ();
754 IOSelector::redisplay ()
759 if (io.input_maximum() != 0) {
763 if (io.output_maximum() != 0) {
769 PortInsertUI::PortInsertUI (Session& sess, PortInsert& pi)
770 : input_selector (sess, pi, true),
771 output_selector (sess, pi, false)
773 hbox.pack_start (output_selector, true, true);
774 hbox.pack_start (input_selector, true, true);
781 PortInsertUI::redisplay()
784 input_selector.redisplay();
785 output_selector.redisplay();
789 PortInsertUI::finished(IOSelector::Result r)
791 input_selector.Finished (r);
792 output_selector.Finished (r);
796 PortInsertWindow::PortInsertWindow (Session& sess, PortInsert& pi, bool can_cancel)
797 : ArdourDialog ("port insert dialog"),
798 _portinsertui(sess, pi),
799 ok_button (can_cancel ? _("OK"): _("Close")),
800 cancel_button (_("Cancel")),
801 rescan_button (_("Rescan"))
804 set_name ("IOSelectorWindow");
805 string title = _("ardour: ");
809 ok_button.set_name ("IOSelectorButton");
810 cancel_button.set_name ("IOSelectorButton");
811 rescan_button.set_name ("IOSelectorButton");
813 button_box.set_spacing (5);
814 button_box.set_border_width (5);
815 button_box.set_homogeneous (true);
816 button_box.pack_start (rescan_button);
818 button_box.pack_start (cancel_button);
821 cancel_button.hide();
823 button_box.pack_start (ok_button);
825 vbox.pack_start (_portinsertui);
826 vbox.pack_start (button_box, false, false);
830 ok_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::accept));
831 cancel_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::cancel));
832 rescan_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::rescan));
834 signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this)));
835 pi.GoingAway.connect (mem_fun(*this, &PortInsertWindow::plugin_going_away));
839 PortInsertWindow::plugin_going_away (ARDOUR::Redirect* ignored)
841 ENSURE_GUI_THREAD(bind (mem_fun(*this, &PortInsertWindow::plugin_going_away), ignored));
843 delete_when_idle (this);
847 PortInsertWindow::on_map ()
849 _portinsertui.redisplay ();
855 PortInsertWindow::rescan ()
857 _portinsertui.redisplay();
861 PortInsertWindow::cancel ()
863 _portinsertui.finished(IOSelector::Cancelled);
868 PortInsertWindow::accept ()
870 _portinsertui.finished(IOSelector::Accepted);