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 <sigc++/bind.h>
26 #include <gtkmm/messagedialog.h>
28 #include <glibmm/thread.h>
30 #include <ardour/io.h>
31 #include <ardour/route.h>
32 #include <ardour/audioengine.h>
33 #include <ardour/port.h>
34 #include <ardour/insert.h>
35 #include <ardour/session.h>
36 #include <ardour/audio_diskstream.h>
38 #include <gtkmm2ext/doi.h>
39 #include <gtkmm2ext/gtk_ui.h>
40 #include <gtkmm2ext/utils.h>
43 #include "io_selector.h"
45 #include "gui_thread.h"
53 using namespace ARDOUR;
55 using namespace Gtkmm2ext;
57 IOSelectorWindow::IOSelectorWindow (Session& sess, boost::shared_ptr<IO> ior, bool input, bool can_cancel)
58 : ArdourDialog ("i/o selector"),
59 _selector (sess, ior, input),
60 ok_button (can_cancel ? _("OK"): _("Close")),
61 cancel_button (_("Cancel")),
62 rescan_button (_("Rescan"))
65 add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
66 set_name ("IOSelectorWindow");
70 title = string_compose(_("%1 input"), ior->name());
72 title = string_compose(_("%1 output"), ior->name());
75 ok_button.set_name ("IOSelectorButton");
76 cancel_button.set_name ("IOSelectorButton");
77 rescan_button.set_name ("IOSelectorButton");
79 button_box.set_spacing (5);
80 button_box.set_border_width (5);
81 button_box.set_homogeneous (true);
82 button_box.pack_start (rescan_button);
85 button_box.pack_start (cancel_button);
90 button_box.pack_start (ok_button);
92 get_vbox()->pack_start (_selector);
93 get_vbox()->pack_start (button_box, false, false);
95 ok_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::accept));
96 cancel_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::cancel));
97 rescan_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::rescan));
100 set_position (WIN_POS_MOUSE);
102 signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this)));
105 IOSelectorWindow::~IOSelectorWindow()
110 IOSelectorWindow::rescan ()
112 _selector.redisplay ();
116 IOSelectorWindow::cancel ()
118 _selector.Finished(IOSelector::Cancelled);
123 IOSelectorWindow::accept ()
125 _selector.Finished(IOSelector::Accepted);
130 IOSelectorWindow::on_map ()
132 _selector.redisplay ();
136 /*************************
137 The IO Selector "widget"
138 *************************/
140 IOSelector::IOSelector (Session& sess, boost::shared_ptr<IO> ior, bool input)
144 port_frame (for_input? _("Inputs") : _("Outputs")),
145 add_port_button (for_input? _("Add Input") : _("Add Output")),
146 remove_port_button (for_input? _("Remove Input") : _("Remove Output")),
147 clear_connections_button (_("Disconnect All"))
151 notebook.set_name ("IOSelectorNotebook");
152 notebook.set_size_request (-1, 125);
154 clear_connections_button.set_name ("IOSelectorButton");
155 add_port_button.set_name ("IOSelectorButton");
156 remove_port_button.set_name ("IOSelectorButton");
158 selector_frame.set_name ("IOSelectorFrame");
159 port_frame.set_name ("IOSelectorFrame");
161 selector_frame.set_label (_("Available connections"));
163 selector_button_box.set_spacing (5);
164 selector_button_box.set_border_width (5);
166 selector_box.set_spacing (5);
167 selector_box.set_border_width (5);
168 selector_box.pack_start (notebook);
169 selector_box.pack_start (selector_button_box, false, false);
171 selector_frame.add (selector_box);
173 port_box.set_spacing (5);
174 port_box.set_border_width (5);
176 port_display_scroller.set_name ("IOSelectorNotebook");
177 port_display_scroller.set_border_width (0);
178 port_display_scroller.set_size_request (-1, 170);
179 port_display_scroller.add (port_box);
180 port_display_scroller.set_policy (POLICY_NEVER,
183 port_button_box.set_spacing (5);
184 port_button_box.set_border_width (5);
186 port_button_box.pack_start (add_port_button, false, false);
189 if (io->input_maximum() < 0 || io->input_maximum() > (int) io->n_inputs()) {
190 add_port_button.set_sensitive (true);
192 add_port_button.set_sensitive (false);
196 if (io->output_maximum() < 0 || io->output_maximum() > (int) io->n_outputs()) {
197 add_port_button.set_sensitive (true);
199 add_port_button.set_sensitive (false);
204 port_button_box.pack_start (remove_port_button, false, false);
207 if (io->input_minimum() < 0 || io->input_minimum() < (int) io->n_inputs()) {
208 remove_port_button.set_sensitive (true);
210 remove_port_button.set_sensitive (false);
214 if (io->output_minimum() < 0 || io->output_minimum() < (int) io->n_outputs()) {
215 remove_port_button.set_sensitive (true);
217 remove_port_button.set_sensitive (false);
221 port_button_box.pack_start (clear_connections_button, false, false);
223 port_and_button_box.set_border_width (5);
224 port_and_button_box.pack_start (port_button_box, false, false);
225 port_and_button_box.pack_start (port_display_scroller);
227 port_frame.add (port_and_button_box);
229 port_and_selector_box.set_spacing (5);
230 port_and_selector_box.pack_start (port_frame);
231 port_and_selector_box.pack_start (selector_frame);
234 set_border_width (5);
235 pack_start (port_and_selector_box);
240 clear_connections_button.signal_clicked().connect (mem_fun(*this, &IOSelector::clear_connections));
242 add_port_button.signal_clicked().connect (mem_fun(*this, &IOSelector::add_port));
243 remove_port_button.signal_clicked().connect (mem_fun(*this, &IOSelector::remove_port));
246 io->input_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
248 io->output_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
251 io->name_changed.connect (mem_fun(*this, &IOSelector::name_changed));
254 IOSelector::~IOSelector ()
259 IOSelector::name_changed (void* src)
261 ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelector::name_changed), src));
267 IOSelector::clear_connections ()
270 io->disconnect_inputs (this);
272 io->disconnect_outputs (this);
277 IOSelector::rescan ()
279 using namespace Notebook_Helpers;
281 typedef std::map<string,vector<pair<string,string> > > PortMap;
284 PageList& pages = notebook.pages();
286 vector<string> rowdata;
288 current_page = notebook.get_current_page ();
291 /* get relevant current JACK ports */
293 ports = session.engine().get_ports ("", JACK_DEFAULT_AUDIO_TYPE, for_input ? JackPortIsOutput : JackPortIsInput);
299 /* find all the client names and group their ports into a list-by-client */
301 for (int n = 0; ports[n]; ++n) {
303 pair<string,vector<pair<string,string> > > newpair;
304 pair<string,string> strpair;
305 pair<PortMap::iterator,bool> result;
307 string str = ports[n];
308 string::size_type pos;
311 pos = str.find (':');
313 newpair.first = str.substr (0, pos);
314 portname = str.substr (pos+1);
316 result = portmap.insert (newpair);
318 strpair.first = portname;
319 strpair.second = str;
321 result.first->second.push_back (strpair);
326 for (i = portmap.begin(); i != portmap.end(); ++i) {
328 Box *client_box = manage (new VBox);
329 TreeView *display = manage (new TreeView);
330 RefPtr<ListStore> model = ListStore::create (port_display_columns);
331 ScrolledWindow *scroller = manage (new ScrolledWindow);
333 display->set_model (model);
334 display->append_column (X_("notvisible"), port_display_columns.displayed_name);
335 display->set_headers_visible (false);
336 display->get_selection()->set_mode (SELECTION_SINGLE);
337 display->set_name ("IOSelectorList");
339 for (vector<pair<string,string> >::iterator s = i->second.begin(); s != i->second.end(); ++s) {
341 TreeModel::Row row = *(model->append ());
343 row[port_display_columns.displayed_name] = s->first;
344 row[port_display_columns.full_name] = s->second;
347 display->signal_button_release_event().connect (bind (mem_fun(*this, &IOSelector::port_selection_changed), display));
348 Label *tab_label = manage (new Label);
350 tab_label->set_name ("IOSelectorNotebookTab");
351 tab_label->set_text ((*i).first);
353 scroller->add (*display);
354 scroller->set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC);
356 client_box->pack_start (*scroller);
358 pages.push_back (TabElem (*client_box, *tab_label));
361 notebook.set_current_page (current_page);
362 notebook.signal_show().connect (bind (mem_fun (notebook, &Notebook::set_current_page), current_page));
363 selector_box.show_all ();
367 IOSelector::display_ports ()
369 TreeView *firsttview = 0;
370 TreeView *selected_port_tview = 0;
372 Glib::Mutex::Lock lm (port_display_lock);
377 limit = io->n_inputs();
379 limit = io->n_outputs();
382 for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ) {
384 slist<TreeView *>::iterator tmp;
389 port_box.remove (**i);
391 port_displays.erase (i);
396 for (uint32_t n = 0; n < limit; ++n) {
399 //ScrolledWindow *scroller;
400 string really_short_name;
403 port = io->input (n);
405 port = io->output (n);
408 /* we know there is '/' because we put it there */
410 really_short_name = port->short_name();
411 really_short_name = really_short_name.substr (really_short_name.find ('/') + 1);
413 tview = manage (new TreeView());
414 RefPtr<ListStore> port_model = ListStore::create (port_display_columns);
420 tview->set_model (port_model);
421 tview->append_column (really_short_name, port_display_columns.displayed_name);
422 tview->get_selection()->set_mode (SELECTION_SINGLE);
423 tview->set_data ("port", port);
424 tview->set_headers_visible (true);
425 tview->set_name ("IOSelectorPortList");
427 port_box.pack_start (*tview);
428 port_displays.insert (port_displays.end(), tview);
430 /* now fill the clist with the current connections */
433 const char **connections = port->get_connections ();
436 for (uint32_t c = 0; connections[c]; ++c) {
437 TreeModel::Row row = *(port_model->append());
438 row[port_display_columns.displayed_name] = connections[c];
439 row[port_display_columns.full_name] = connections[c];
445 if (io->input_maximum() == 1) {
446 selected_port = port;
447 selected_port_tview = tview;
449 if (port == selected_port) {
450 selected_port_tview = tview;
456 if (io->output_maximum() == 1) {
457 selected_port = port;
458 selected_port_tview = tview;
460 if (port == selected_port) {
461 selected_port_tview = tview;
466 TreeViewColumn* col = tview->get_column (0);
468 col->set_clickable (true);
470 /* handle button events on the column header ... */
471 col->signal_clicked().connect (bind (mem_fun(*this, &IOSelector::select_treeview), tview));
473 /* ... and within the treeview itself */
474 tview->signal_button_release_event().connect (bind (mem_fun(*this, &IOSelector::connection_button_release), tview));
477 port_box.show_all ();
480 if (!selected_port_tview) {
481 selected_port_tview = firsttview;
484 select_treeview (selected_port_tview);
488 IOSelector::port_selection_changed (GdkEventButton *ev, TreeView* treeview)
490 TreeModel::iterator i = treeview->get_selection()->get_selected();
497 if (selected_port == 0) {
501 ustring other_port_name = (*i)[port_display_columns.full_name];
504 if ((status = io->connect_input (selected_port, other_port_name, this)) == 0) {
505 Port *p = session.engine().get_port_by_name (other_port_name);
506 p->enable_metering();
509 status = io->connect_output (selected_port, other_port_name, this);
513 select_next_treeview ();
516 treeview->get_selection()->unselect_all();
521 IOSelector::ports_changed (IOChange change, void *src)
523 ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelector::ports_changed), change, src));
529 IOSelector::add_port ()
531 /* add a new port, then hide the button if we're up to the maximum allowed */
536 io->add_input_port ("", this);
539 catch (AudioEngine::PortRegistrationFailure& err) {
540 MessageDialog msg (0, _("There are no more JACK ports available."));
544 if (io->input_maximum() >= 0 && io->input_maximum() <= (int) io->n_inputs()) {
545 add_port_button.set_sensitive (false);
548 if (io->input_minimum() < (int) io->n_inputs()) {
549 remove_port_button.set_sensitive (true);
555 io->add_output_port ("", this);
558 catch (AudioEngine::PortRegistrationFailure& err) {
559 MessageDialog msg (0, _("There are no more JACK ports available."));
563 if (io->output_maximum() >= 0 && io->output_maximum() <= (int) io->n_outputs()) {
564 add_port_button.set_sensitive (false);
570 IOSelector::remove_port ()
574 // always remove last port
577 if ((nports = io->n_inputs()) > 0) {
578 io->remove_input_port (io->input(nports-1), this);
580 if (io->input_minimum() == (int) io->n_inputs()) {
581 remove_port_button.set_sensitive (false);
584 if ((nports = io->n_outputs()) > 0) {
585 io->remove_output_port (io->output(nports-1), this);
591 IOSelector::remove_port_when_idle (Port *port)
594 io->remove_input_port (port, this);
596 io->remove_output_port (port, this);
603 IOSelector::connection_button_release (GdkEventButton *ev, TreeView *treeview)
605 /* this handles button release on a port name row: i.e. a connection
606 between the named port and the port represented by the treeview.
609 Gtk::TreeModel::iterator iter;
610 TreeModel::Path path;
611 TreeViewColumn* column;
615 /* only handle button1 events here */
617 if (ev->button != 1) {
621 if (!treeview->get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
625 if ((iter = treeview->get_model()->get_iter (path.to_string()))) {
628 ustring connected_port_name = (*iter)[port_display_columns.full_name];
629 Port *port = reinterpret_cast<Port *> (treeview->get_data (_("port")));
632 Port *p = session.engine().get_port_by_name (connected_port_name);
633 p->disable_metering();
634 io->disconnect_input (port, connected_port_name, this);
636 io->disconnect_output (port, connected_port_name, this);
644 IOSelector::select_next_treeview ()
646 slist<TreeView*>::iterator next;
648 for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
650 if ((*i)->get_name() == "IOSelectorPortListSelected") {
654 if (i == port_displays.end()) {
655 select_treeview (port_displays.front());
657 select_treeview (*i);
666 IOSelector::select_treeview (TreeView* tview)
668 /* Gack. TreeView's don't respond visually to a change
669 in their state, so rename them to force a style
673 Glib::Mutex::Lock lm (port_display_lock);
674 Port* port = reinterpret_cast<Port *> (tview->get_data (_("port")));
676 selected_port = port;
678 tview->set_name ("IOSelectorPortListSelected");
679 tview->queue_draw ();
681 /* ugly hack to force the column header button to change as well */
683 TreeViewColumn* col = tview->get_column (0);
684 GtkTreeViewColumn* ccol = col->gobj();
687 gtk_widget_set_name (ccol->button, "IOSelectorPortListSelected");
688 gtk_widget_queue_draw (ccol->button);
691 for (slist<TreeView*>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
696 col = (*i)->get_column (0);
700 gtk_widget_set_name (ccol->button, "IOSelectorPortList");
701 gtk_widget_queue_draw (ccol->button);
704 Port* port = reinterpret_cast<Port *> ((*i)->get_data (_("port")));
706 (*i)->set_name ("IOSelectorPortList");
710 selector_box.show_all ();
714 IOSelector::redisplay ()
719 if (io->input_maximum() != 0) {
723 if (io->output_maximum() != 0) {
729 PortInsertUI::PortInsertUI (Session& sess, boost::shared_ptr<PortInsert> pi)
730 : input_selector (sess, pi, true),
731 output_selector (sess, pi, false)
733 hbox.pack_start (output_selector, true, true);
734 hbox.pack_start (input_selector, true, true);
741 PortInsertUI::redisplay()
744 input_selector.redisplay();
745 output_selector.redisplay();
749 PortInsertUI::finished(IOSelector::Result r)
751 input_selector.Finished (r);
752 output_selector.Finished (r);
756 PortInsertWindow::PortInsertWindow (Session& sess, boost::shared_ptr<PortInsert> pi, bool can_cancel)
757 : ArdourDialog ("port insert dialog"),
758 _portinsertui (sess, pi),
759 ok_button (can_cancel ? _("OK"): _("Close")),
760 cancel_button (_("Cancel")),
761 rescan_button (_("Rescan"))
764 set_name ("IOSelectorWindow");
765 string title = _("ardour: ");
769 ok_button.set_name ("IOSelectorButton");
770 cancel_button.set_name ("IOSelectorButton");
771 rescan_button.set_name ("IOSelectorButton");
773 button_box.set_spacing (5);
774 button_box.set_border_width (5);
775 button_box.set_homogeneous (true);
776 button_box.pack_start (rescan_button);
778 button_box.pack_start (cancel_button);
781 cancel_button.hide();
783 button_box.pack_start (ok_button);
785 get_vbox()->pack_start (_portinsertui);
786 get_vbox()->pack_start (button_box, false, false);
788 ok_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::accept));
789 cancel_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::cancel));
790 rescan_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::rescan));
792 signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this)));
793 pi->GoingAway.connect (mem_fun(*this, &PortInsertWindow::plugin_going_away));
797 PortInsertWindow::plugin_going_away ()
799 ENSURE_GUI_THREAD(mem_fun(*this, &PortInsertWindow::plugin_going_away));
801 delete_when_idle (this);
805 PortInsertWindow::on_map ()
807 _portinsertui.redisplay ();
813 PortInsertWindow::rescan ()
815 _portinsertui.redisplay();
819 PortInsertWindow::cancel ()
821 _portinsertui.finished(IOSelector::Cancelled);
826 PortInsertWindow::accept ()
828 _portinsertui.finished(IOSelector::Accepted);