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 vbox.pack_start (_selector);
87 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);
97 signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this)));
100 IOSelectorWindow::~IOSelectorWindow()
105 IOSelectorWindow::rescan ()
107 _selector.redisplay ();
111 IOSelectorWindow::cancel ()
113 _selector.Finished(IOSelector::Cancelled);
118 IOSelectorWindow::accept ()
120 _selector.Finished(IOSelector::Accepted);
125 IOSelectorWindow::on_map ()
127 _selector.redisplay ();
131 /*************************
132 The IO Selector "widget"
133 *************************/
135 IOSelector::IOSelector (Session& sess, IO& ior, bool input)
139 port_frame (for_input? _("Inputs") : _("Outputs")),
140 add_port_button (for_input? _("Add Input") : _("Add Output")),
141 remove_port_button (for_input? _("Remove Input") : _("Remove Output")),
142 clear_connections_button (_("Disconnect All"))
146 notebook.set_name ("IOSelectorNotebook");
147 notebook.set_size_request (-1, 125);
149 clear_connections_button.set_name ("IOSelectorButton");
150 add_port_button.set_name ("IOSelectorButton");
151 remove_port_button.set_name ("IOSelectorButton");
153 selector_frame.set_name ("IOSelectorFrame");
154 port_frame.set_name ("IOSelectorFrame");
156 selector_frame.set_label (_("Available connections"));
158 selector_button_box.set_spacing (5);
159 selector_button_box.set_border_width (5);
161 selector_box.set_spacing (5);
162 selector_box.set_border_width (5);
163 selector_box.pack_start (notebook);
164 selector_box.pack_start (selector_button_box, false, false);
166 selector_frame.add (selector_box);
168 port_box.set_spacing (5);
169 port_box.set_border_width (5);
171 port_display_scroller.set_name ("IOSelectorNotebook");
172 port_display_scroller.set_border_width (0);
173 port_display_scroller.set_size_request (-1, 170);
174 port_display_scroller.add (port_box);
175 port_display_scroller.set_policy (POLICY_NEVER,
178 port_button_box.set_spacing (5);
179 port_button_box.set_border_width (5);
181 port_button_box.pack_start (add_port_button, false, false);
184 if (io.input_maximum() < 0 || io.input_maximum() > (int) io.n_inputs()) {
185 add_port_button.set_sensitive (true);
187 add_port_button.set_sensitive (false);
191 if (io.output_maximum() < 0 || io.output_maximum() > (int) io.n_outputs()) {
192 add_port_button.set_sensitive (true);
194 add_port_button.set_sensitive (false);
199 port_button_box.pack_start (remove_port_button, false, false);
202 if (io.input_minimum() < 0 || io.input_minimum() < (int) io.n_inputs()) {
203 remove_port_button.set_sensitive (true);
205 remove_port_button.set_sensitive (false);
209 if (io.output_minimum() < 0 || io.output_minimum() < (int) io.n_outputs()) {
210 remove_port_button.set_sensitive (true);
212 remove_port_button.set_sensitive (false);
216 port_button_box.pack_start (clear_connections_button, false, false);
218 port_and_button_box.set_border_width (5);
219 port_and_button_box.pack_start (port_button_box, false, false);
220 port_and_button_box.pack_start (port_display_scroller);
222 port_frame.add (port_and_button_box);
224 port_and_selector_box.set_spacing (5);
225 port_and_selector_box.pack_start (port_frame);
226 port_and_selector_box.pack_start (selector_frame);
229 set_border_width (5);
230 pack_start (port_and_selector_box);
235 clear_connections_button.signal_clicked().connect (mem_fun(*this, &IOSelector::clear_connections));
237 add_port_button.signal_clicked().connect (mem_fun(*this, &IOSelector::add_port));
238 remove_port_button.signal_clicked().connect (mem_fun(*this, &IOSelector::remove_port));
241 io.input_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
243 io.output_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
246 io.name_changed.connect (mem_fun(*this, &IOSelector::name_changed));
249 IOSelector::~IOSelector ()
254 IOSelector::name_changed (void* src)
256 ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelector::name_changed), src));
262 IOSelector::clear_connections ()
265 io.disconnect_inputs (this);
267 io.disconnect_outputs (this);
272 IOSelector::rescan ()
274 using namespace Notebook_Helpers;
276 typedef std::map<string,vector<pair<string,string> > > PortMap;
279 PageList& pages = notebook.pages();
281 vector<string> rowdata;
283 current_page = notebook.get_current_page ();
286 /* get relevant current JACK ports */
288 ports = session.engine().get_ports ("", JACK_DEFAULT_AUDIO_TYPE, for_input?JackPortIsOutput:JackPortIsInput);
294 /* get relevant current JACK ports */
296 ports = session.engine().get_ports ("", JACK_DEFAULT_AUDIO_TYPE, for_input?JackPortIsOutput:JackPortIsInput);
302 /* find all the client names and group their ports into a list-by-client */
304 for (int n = 0; ports[n]; ++n) {
306 pair<string,vector<pair<string,string> > > newpair;
307 pair<string,string> strpair;
308 pair<PortMap::iterator,bool> result;
310 string str = ports[n];
311 string::size_type pos;
314 pos = str.find (':');
316 newpair.first = str.substr (0, pos);
317 portname = str.substr (pos+1);
319 result = portmap.insert (newpair);
321 strpair.first = portname;
322 strpair.second = str;
324 result.first->second.push_back (strpair);
329 for (i = portmap.begin(); i != portmap.end(); ++i) {
331 Box *client_box = manage (new VBox);
332 TreeView *display = manage (new TreeView);
333 RefPtr<ListStore> model = ListStore::create (port_display_columns);
334 ScrolledWindow *scroller = manage (new ScrolledWindow);
336 display->set_model (model);
337 display->append_column (X_("notvisible"), port_display_columns.displayed_name);
338 display->set_headers_visible (false);
339 display->get_selection()->set_mode (SELECTION_SINGLE);
340 display->set_name ("IOSelectorList");
342 for (vector<pair<string,string> >::iterator s = i->second.begin(); s != i->second.end(); ++s) {
344 TreeModel::Row row = *(model->append ());
346 row[port_display_columns.displayed_name] = s->first;
347 row[port_display_columns.full_name] = s->second;
350 display->get_selection()->signal_changed().connect
351 (bind (mem_fun(*this, &IOSelector::port_selection_changed), display));
353 Label *tab_label = manage (new Label);
355 tab_label->set_name ("IOSelectorNotebookTab");
356 tab_label->set_text ((*i).first);
358 scroller->add (*display);
359 scroller->set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC);
361 client_box->pack_start (*scroller);
363 pages.push_back (TabElem (*client_box, *tab_label));
366 notebook.set_current_page (current_page);
367 notebook.signal_show().connect (bind (mem_fun (notebook, &Notebook::set_current_page), current_page));
368 selector_box.show_all ();
372 IOSelector::display_ports ()
374 TreeView *firsttview = 0;
375 TreeView *selected_port_tview = 0;
378 LockMonitor lm (port_display_lock, __LINE__, __FILE__);
383 limit = io.n_inputs();
385 limit = io.n_outputs();
388 for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ) {
390 slist<TreeView *>::iterator tmp;
395 port_box.remove (**i);
397 port_displays.erase (i);
403 for (uint32_t n = 0; n < limit; ++n) {
406 ScrolledWindow *scroller;
407 string really_short_name;
409 /* we know there is '/' because we put it there */
411 really_short_name = port->short_name();
412 really_short_name = really_short_name.substr (really_short_name.find ('/') + 1);
414 tview = manage (new TreeView());
415 RefPtr<ListStore> port_model = ListStore::create (port_display_columns);
421 tview->set_model (port_model);
422 tview->append_column (really_short_name, port_display_columns.displayed_name);
423 tview->get_selection()->set_mode (SELECTION_SINGLE);
424 tview->set_data ("port", port);
425 tview->set_headers_visible (true);
426 tview->set_name ("IOSelectorPortList");
428 scroller = manage (new ScrolledWindow);
430 scroller->add (*tview);
431 scroller->set_policy (POLICY_NEVER, POLICY_AUTOMATIC);
433 port_displays.insert (port_displays.end(), tview);
434 port_box.pack_start (*scroller);
436 scroller->set_size_request (-1, 75);
438 /* now fill the clist with the current connections */
441 const char **connections = port->get_connections ();
444 for (uint32_t c = 0; connections[c]; ++c) {
445 TreeModel::Row row = *(port_model->append());
446 row[port_display_columns.displayed_name] = connections[c];
452 if (io.input_maximum() == 1) {
453 selected_port = port;
454 selected_port_tview = tview;
456 if (port == selected_port) {
457 selected_port_tview = tview;
463 if (io.output_maximum() == 1) {
464 selected_port = port;
465 selected_port_tview = tview;
467 if (port == selected_port) {
468 selected_port_tview = tview;
473 TreeViewColumn* col = tview->get_column (0);
475 col->set_clickable (true);
477 /* handle button events on the column header and within the treeview itself */
479 col->get_widget()->signal_button_release_event().connect (bind (mem_fun(*this, &IOSelector::port_column_button_release), tview));
480 tview->signal_button_release_event().connect (bind (mem_fun(*this, &IOSelector::connection_button_release), tview));
483 port_box.show_all ();
485 if (selected_port_tview) {
487 // selected_port_tview->click_column(0);
488 selected_port_tview->set_name ("IOSelectorPortListSelected");
489 for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
490 if (*i != selected_port_tview) {
491 (*i)->set_name ("IOSelectorPortList");
497 selector_box.hide_all ();
501 if (selected_port_tview) {
502 select_treeview (selected_port_tview);
503 } else if (firsttview) {
505 select_treeview (firsttview);
510 IOSelector::port_selection_changed (TreeView* treeview)
512 TreeModel::iterator i = treeview->get_selection()->get_selected();
519 if (selected_port == 0) {
523 ustring other_port_name = (*i)[port_display_columns.full_name];
526 if ((status = io.connect_input (selected_port, other_port_name, this)) == 0) {
527 Port *p = session.engine().get_port_by_name (other_port_name);
528 p->enable_metering();
531 status = io.connect_output (selected_port, other_port_name, this);
535 select_next_treeview ();
540 IOSelector::ports_changed (IOChange change, void *src)
542 ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelector::ports_changed), change, src));
548 IOSelector::add_port ()
550 /* add a new port, then hide the button if we're up to the maximum allowed */
555 io.add_input_port ("", this);
558 catch (AudioEngine::PortRegistrationFailure& err) {
559 ArdourMessage msg (0, X_("noport dialog"), _("There are no more JACK ports available."));
562 if (io.input_maximum() >= 0 && io.input_maximum() <= (int) io.n_inputs()) {
563 add_port_button.set_sensitive (false);
566 if (io.input_minimum() < (int) io.n_inputs()) {
567 remove_port_button.set_sensitive (true);
573 io.add_output_port ("", this);
576 catch (AudioEngine::PortRegistrationFailure& err) {
577 ArdourMessage msg (0, X_("noport dialog"),
578 _("There are no more JACK ports available."));
581 if (io.output_maximum() >= 0 && io.output_maximum() <= (int) io.n_outputs()) {
582 add_port_button.set_sensitive (false);
588 IOSelector::remove_port ()
592 // always remove last port
595 if ((nports = io.n_inputs()) > 0) {
596 io.remove_input_port (io.input(nports-1), this);
598 if (io.input_minimum() == (int) io.n_inputs()) {
599 remove_port_button.set_sensitive (false);
602 if ((nports = io.n_outputs()) > 0) {
603 io.remove_output_port (io.output(nports-1), this);
609 IOSelector::remove_port_when_idle (Port *port)
612 io.remove_input_port (port, this);
614 io.remove_output_port (port, this);
621 IOSelector::connection_button_release (GdkEventButton *ev, TreeView *treeview)
623 /* this handles button release on a port name row: i.e. a connection
624 between the named port and the port represented by the treeview.
628 TreeModel::Path path;
629 TreeViewColumn* column;
633 /* only handle button1 events here */
635 if (ev->button != 1) {
639 if (!(Keyboard::is_delete_event (ev))) {
643 if (!treeview->get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
647 if ((iter = treeview->get_model()->get_iter (path))) {
651 ustring connected_port_name = (*iter)[port_display_columns.full_name];
652 Port *port = reinterpret_cast<Port *> (treeview->get_data (_("port")));
655 Port *p = session.engine().get_port_by_name (connected_port_name);
656 p->disable_metering();
657 io.disconnect_input (port, connected_port_name, this);
659 io.disconnect_output (port, connected_port_name, this);
667 IOSelector::port_column_button_release (GdkEventButton *event, TreeView* treeview)
669 /* this handles button release on the button at the top of a single-column
670 treeview (representing a port)
673 if (Keyboard::is_delete_event (event)) {
676 LockMonitor lm (port_display_lock, __LINE__, __FILE__);
678 port = static_cast<Port *> (treeview->get_data (_("port")));
680 if (port == selected_port) {
682 treeview->set_name ("IOSelectorPortList");
683 treeview->queue_draw();
687 /* remove the port when idle - if we do it here, we will destroy the widget
688 for whom we are handling an event. not good.
691 signal_idle().connect (bind (mem_fun(*this, &IOSelector::remove_port_when_idle), port));
694 select_treeview (treeview);
701 IOSelector::select_next_treeview ()
703 slist<TreeView*>::iterator next;
705 for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
707 if ((*i)->get_name() == "IOSelectorPortListSelected") {
711 if (i == port_displays.end()) {
712 select_treeview (port_displays.front());
714 select_treeview (*i);
723 IOSelector::select_treeview (TreeView* tview)
725 /* Gack. TreeView's don't respond visually to a change
726 in their state, so rename them to force a style
730 LockMonitor lm (port_display_lock, __LINE__, __FILE__);
731 Port* port = reinterpret_cast<Port *> (tview->get_data (_("port")));
733 if (port != selected_port) {
734 selected_port = port;
736 tview->set_name ("IOSelectorPortListSelected");
738 for (slist<TreeView*>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
740 (*i)->set_name ("IOSelectorPortList");
744 selector_box.show_all ();
749 IOSelector::redisplay ()
754 if (io.input_maximum() != 0) {
758 if (io.output_maximum() != 0) {
764 PortInsertUI::PortInsertUI (Session& sess, PortInsert& pi)
765 : input_selector (sess, pi, true),
766 output_selector (sess, pi, false)
768 hbox.pack_start (output_selector, true, true);
769 hbox.pack_start (input_selector, true, true);
776 PortInsertUI::redisplay()
779 input_selector.redisplay();
780 output_selector.redisplay();
784 PortInsertUI::finished(IOSelector::Result r)
786 input_selector.Finished (r);
787 output_selector.Finished (r);
791 PortInsertWindow::PortInsertWindow (Session& sess, PortInsert& pi, bool can_cancel)
792 : ArdourDialog ("port insert dialog"),
793 _portinsertui(sess, pi),
794 ok_button (can_cancel ? _("OK"): _("Close")),
795 cancel_button (_("Cancel")),
796 rescan_button (_("Rescan"))
799 set_name ("IOSelectorWindow");
800 string title = _("ardour: ");
804 ok_button.set_name ("IOSelectorButton");
805 cancel_button.set_name ("IOSelectorButton");
806 rescan_button.set_name ("IOSelectorButton");
808 button_box.set_spacing (5);
809 button_box.set_border_width (5);
810 button_box.set_homogeneous (true);
811 button_box.pack_start (rescan_button);
813 button_box.pack_start (cancel_button);
816 cancel_button.hide();
818 button_box.pack_start (ok_button);
820 vbox.pack_start (_portinsertui);
821 vbox.pack_start (button_box, false, false);
825 ok_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::accept));
826 cancel_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::cancel));
827 rescan_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::rescan));
829 signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this)));
830 pi.GoingAway.connect (mem_fun(*this, &PortInsertWindow::plugin_going_away));
834 PortInsertWindow::plugin_going_away (ARDOUR::Redirect* ignored)
836 ENSURE_GUI_THREAD(bind (mem_fun(*this, &PortInsertWindow::plugin_going_away), ignored));
838 delete_when_idle (this);
842 PortInsertWindow::on_map ()
844 _portinsertui.redisplay ();
850 PortInsertWindow::rescan ()
852 _portinsertui.redisplay();
856 PortInsertWindow::cancel ()
858 _portinsertui.finished(IOSelector::Cancelled);
863 PortInsertWindow::accept ()
865 _portinsertui.finished(IOSelector::Accepted);