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 <gtkmm/messagedialog.h>
26 #include <glibmm/thread.h>
28 #include <ardour/io.h>
29 #include <ardour/route.h>
30 #include <ardour/audioengine.h>
31 #include <ardour/port.h>
32 #include <ardour/insert.h>
33 #include <ardour/session.h>
34 #include <ardour/audio_diskstream.h>
36 #include <gtkmm2ext/doi.h>
37 #include <gtkmm2ext/gtk_ui.h>
38 #include <gtkmm2ext/utils.h>
41 #include "io_selector.h"
43 #include "gui_thread.h"
51 using namespace ARDOUR;
53 using namespace Gtkmm2ext;
55 IOSelectorWindow::IOSelectorWindow (Session& sess, boost::shared_ptr<IO> ior, bool input, bool can_cancel)
56 : ArdourDialog ("i/o selector"),
57 _selector (sess, ior, input),
58 ok_button (can_cancel ? _("OK"): _("Close")),
59 cancel_button (_("Cancel")),
60 rescan_button (_("Rescan"))
63 add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
64 set_name ("IOSelectorWindow");
68 title = string_compose(_("%1 input"), ior->name());
70 title = string_compose(_("%1 output"), ior->name());
73 ok_button.set_name ("IOSelectorButton");
74 cancel_button.set_name ("IOSelectorButton");
75 rescan_button.set_name ("IOSelectorButton");
77 button_box.set_spacing (5);
78 button_box.set_border_width (5);
79 button_box.set_homogeneous (true);
80 button_box.pack_start (rescan_button);
83 button_box.pack_start (cancel_button);
88 button_box.pack_start (ok_button);
90 get_vbox()->pack_start (_selector);
91 get_vbox()->pack_start (button_box, false, false);
93 ok_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::accept));
94 cancel_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::cancel));
95 rescan_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::rescan));
98 set_position (WIN_POS_MOUSE);
100 signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this)));
103 IOSelectorWindow::~IOSelectorWindow()
108 IOSelectorWindow::rescan ()
110 _selector.redisplay ();
114 IOSelectorWindow::cancel ()
116 _selector.Finished(IOSelector::Cancelled);
121 IOSelectorWindow::accept ()
123 _selector.Finished(IOSelector::Accepted);
128 IOSelectorWindow::on_map ()
130 _selector.redisplay ();
134 /*************************
135 The IO Selector "widget"
136 *************************/
138 IOSelector::IOSelector (Session& sess, boost::shared_ptr<IO> ior, bool input)
142 port_frame (for_input? _("Inputs") : _("Outputs")),
143 add_port_button (for_input? _("Add Input") : _("Add Output")),
144 remove_port_button (for_input? _("Remove Input") : _("Remove Output")),
145 clear_connections_button (_("Disconnect All"))
149 notebook.set_name ("IOSelectorNotebook");
150 notebook.set_size_request (-1, 125);
152 clear_connections_button.set_name ("IOSelectorButton");
153 add_port_button.set_name ("IOSelectorButton");
154 remove_port_button.set_name ("IOSelectorButton");
156 selector_frame.set_name ("IOSelectorFrame");
157 port_frame.set_name ("IOSelectorFrame");
159 selector_frame.set_label (_("Available connections"));
161 selector_button_box.set_spacing (5);
162 selector_button_box.set_border_width (5);
164 selector_box.set_spacing (5);
165 selector_box.set_border_width (5);
166 selector_box.pack_start (notebook);
167 selector_box.pack_start (selector_button_box, false, false);
169 selector_frame.add (selector_box);
171 port_box.set_spacing (5);
172 port_box.set_border_width (5);
174 port_display_scroller.set_name ("IOSelectorNotebook");
175 port_display_scroller.set_border_width (0);
176 port_display_scroller.set_size_request (-1, 170);
177 port_display_scroller.add (port_box);
178 port_display_scroller.set_policy (POLICY_NEVER,
181 port_button_box.set_spacing (5);
182 port_button_box.set_border_width (5);
184 port_button_box.pack_start (add_port_button, false, false);
187 if (io->input_maximum() < 0 || io->input_maximum() > (int) io->n_inputs()) {
188 add_port_button.set_sensitive (true);
190 add_port_button.set_sensitive (false);
194 if (io->output_maximum() < 0 || io->output_maximum() > (int) io->n_outputs()) {
195 add_port_button.set_sensitive (true);
197 add_port_button.set_sensitive (false);
202 port_button_box.pack_start (remove_port_button, false, false);
205 if (io->input_minimum() < 0 || io->input_minimum() < (int) io->n_inputs()) {
206 remove_port_button.set_sensitive (true);
208 remove_port_button.set_sensitive (false);
212 if (io->output_minimum() < 0 || io->output_minimum() < (int) io->n_outputs()) {
213 remove_port_button.set_sensitive (true);
215 remove_port_button.set_sensitive (false);
219 port_button_box.pack_start (clear_connections_button, false, false);
221 port_and_button_box.set_border_width (5);
222 port_and_button_box.pack_start (port_button_box, false, false);
223 port_and_button_box.pack_start (port_display_scroller);
225 port_frame.add (port_and_button_box);
227 port_and_selector_box.set_spacing (5);
228 port_and_selector_box.pack_start (port_frame);
229 port_and_selector_box.pack_start (selector_frame);
232 set_border_width (5);
233 pack_start (port_and_selector_box);
238 clear_connections_button.signal_clicked().connect (mem_fun(*this, &IOSelector::clear_connections));
240 add_port_button.signal_clicked().connect (mem_fun(*this, &IOSelector::add_port));
241 remove_port_button.signal_clicked().connect (mem_fun(*this, &IOSelector::remove_port));
244 io->input_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
246 io->output_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
249 io->name_changed.connect (mem_fun(*this, &IOSelector::name_changed));
252 IOSelector::~IOSelector ()
257 IOSelector::name_changed (void* src)
259 ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelector::name_changed), src));
265 IOSelector::clear_connections ()
268 io->disconnect_inputs (this);
270 io->disconnect_outputs (this);
275 IOSelector::rescan ()
277 using namespace Notebook_Helpers;
279 typedef std::map<string,vector<pair<string,string> > > PortMap;
282 PageList& pages = notebook.pages();
284 vector<string> rowdata;
286 current_page = notebook.get_current_page ();
289 /* get relevant current JACK ports */
291 ports = session.engine().get_ports ("", JACK_DEFAULT_AUDIO_TYPE, for_input ? JackPortIsOutput : JackPortIsInput);
297 /* find all the client names and group their ports into a list-by-client */
299 for (int n = 0; ports[n]; ++n) {
301 pair<string,vector<pair<string,string> > > newpair;
302 pair<string,string> strpair;
303 pair<PortMap::iterator,bool> result;
305 string str = ports[n];
306 string::size_type pos;
309 pos = str.find (':');
311 newpair.first = str.substr (0, pos);
312 portname = str.substr (pos+1);
314 result = portmap.insert (newpair);
316 strpair.first = portname;
317 strpair.second = str;
319 result.first->second.push_back (strpair);
324 for (i = portmap.begin(); i != portmap.end(); ++i) {
326 Box *client_box = manage (new VBox);
327 TreeView *display = manage (new TreeView);
328 RefPtr<ListStore> model = ListStore::create (port_display_columns);
329 ScrolledWindow *scroller = manage (new ScrolledWindow);
331 display->set_model (model);
332 display->append_column (X_("notvisible"), port_display_columns.displayed_name);
333 display->set_headers_visible (false);
334 display->get_selection()->set_mode (SELECTION_SINGLE);
335 display->set_name ("IOSelectorList");
337 for (vector<pair<string,string> >::iterator s = i->second.begin(); s != i->second.end(); ++s) {
339 TreeModel::Row row = *(model->append ());
341 row[port_display_columns.displayed_name] = s->first;
342 row[port_display_columns.full_name] = s->second;
345 //display->get_selection()->signal_changed().connect (bind (mem_fun(*this, &IOSelector::port_selection_changed), display));
346 display->signal_button_release_event().connect (bind (mem_fun(*this, &IOSelector::port_selection_changed), display));
347 Label *tab_label = manage (new Label);
349 tab_label->set_name ("IOSelectorNotebookTab");
350 tab_label->set_text ((*i).first);
352 scroller->add (*display);
353 scroller->set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC);
355 client_box->pack_start (*scroller);
357 pages.push_back (TabElem (*client_box, *tab_label));
360 notebook.set_current_page (current_page);
361 //notebook.signal_show().connect (bind (mem_fun (notebook, &Notebook::set_current_page), current_page));
362 selector_box.show_all ();
366 IOSelector::display_ports ()
368 TreeView *firsttview = 0;
369 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);
397 for (uint32_t n = 0; n < limit; ++n) {
400 //ScrolledWindow *scroller;
401 string really_short_name;
404 port = io->input (n);
406 port = io->output (n);
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 port_box.pack_start (*tview);
429 port_displays.insert (port_displays.end(), tview);
431 /* now fill the clist with the current connections */
434 const char **connections = port->get_connections ();
437 for (uint32_t c = 0; connections[c]; ++c) {
438 TreeModel::Row row = *(port_model->append());
439 row[port_display_columns.displayed_name] = connections[c];
440 row[port_display_columns.full_name] = connections[c];
446 if (io->input_maximum() == 1) {
447 selected_port = port;
448 selected_port_tview = tview;
450 if (port == selected_port) {
451 selected_port_tview = tview;
457 if (io->output_maximum() == 1) {
458 selected_port = port;
459 selected_port_tview = tview;
461 if (port == selected_port) {
462 selected_port_tview = tview;
467 TreeViewColumn* col = tview->get_column (0);
469 col->set_clickable (true);
471 /* handle button events on the column header and within the treeview itself */
472 col->signal_clicked().connect (bind (mem_fun(*this, &IOSelector::select_treeview), tview));
473 tview->signal_button_release_event().connect (bind (mem_fun(*this, &IOSelector::connection_button_release), tview));
476 port_box.show_all ();
478 if (selected_port_tview) {
480 // selected_port_tview->click_column(0);
481 selected_port_tview->set_name ("IOSelectorPortListSelected");
482 for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
483 if (*i != selected_port_tview) {
484 (*i)->set_name ("IOSelectorPortList");
490 selector_box.hide_all ();
494 if (selected_port_tview) {
495 select_treeview (selected_port_tview);
496 } else if (firsttview) {
498 select_treeview (firsttview);
503 IOSelector::port_selection_changed (GdkEventButton *ev, TreeView* treeview)
505 TreeModel::iterator i = treeview->get_selection()->get_selected();
512 if (selected_port == 0) {
516 ustring other_port_name = (*i)[port_display_columns.full_name];
519 if ((status = io->connect_input (selected_port, other_port_name, this)) == 0) {
520 Port *p = session.engine().get_port_by_name (other_port_name);
521 p->enable_metering();
524 status = io->connect_output (selected_port, other_port_name, this);
528 select_next_treeview ();
531 treeview->get_selection()->unselect_all();
536 IOSelector::ports_changed (IOChange change, void *src)
538 ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelector::ports_changed), change, src));
544 IOSelector::add_port ()
546 /* add a new port, then hide the button if we're up to the maximum allowed */
551 io->add_input_port ("", this);
554 catch (AudioEngine::PortRegistrationFailure& err) {
555 MessageDialog msg (0, _("There are no more JACK ports available."));
559 if (io->input_maximum() >= 0 && io->input_maximum() <= (int) io->n_inputs()) {
560 add_port_button.set_sensitive (false);
563 if (io->input_minimum() < (int) io->n_inputs()) {
564 remove_port_button.set_sensitive (true);
570 io->add_output_port ("", this);
573 catch (AudioEngine::PortRegistrationFailure& err) {
574 MessageDialog msg (0, _("There are no more JACK ports available."));
578 if (io->output_maximum() >= 0 && io->output_maximum() <= (int) io->n_outputs()) {
579 add_port_button.set_sensitive (false);
585 IOSelector::remove_port ()
589 // always remove last port
592 if ((nports = io->n_inputs()) > 0) {
593 io->remove_input_port (io->input(nports-1), this);
595 if (io->input_minimum() == (int) io->n_inputs()) {
596 remove_port_button.set_sensitive (false);
599 if ((nports = io->n_outputs()) > 0) {
600 io->remove_output_port (io->output(nports-1), this);
606 IOSelector::remove_port_when_idle (Port *port)
609 io->remove_input_port (port, this);
611 io->remove_output_port (port, this);
618 IOSelector::connection_button_release (GdkEventButton *ev, TreeView *treeview)
620 /* this handles button release on a port name row: i.e. a connection
621 between the named port and the port represented by the treeview.
624 Gtk::TreeModel::iterator iter;
625 TreeModel::Path path;
626 TreeViewColumn* column;
630 /* only handle button1 events here */
632 if (ev->button != 1) {
636 if (!(Keyboard::is_delete_event (ev))) {
640 if (!treeview->get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
643 cerr << "path = " << path.to_string() << endl;
645 if ((iter = treeview->get_model()->get_iter (path.to_string()))) {
648 ustring connected_port_name = (*iter)[port_display_columns.full_name];
649 Port *port = reinterpret_cast<Port *> (treeview->get_data (_("port")));
652 Port *p = session.engine().get_port_by_name (connected_port_name);
653 p->disable_metering();
654 io->disconnect_input (port, connected_port_name, this);
656 io->disconnect_output (port, connected_port_name, this);
664 IOSelector::port_column_button_release (GdkEventButton* event, TreeView* treeview)
666 /* this handles button release on the button at the top of a single-column
667 treeview (representing a port)
669 cerr << "IOSelector::port_column_button_release() called" << endl;
671 if (Keyboard::is_delete_event (event)) {
674 Glib::Mutex::Lock lm (port_display_lock);
676 port = static_cast<Port *> (treeview->get_data (_("port")));
678 if (port == selected_port) {
680 treeview->set_name ("IOSelectorPortList");
681 treeview->queue_draw();
685 /* remove the port when idle - if we do it here, we will destroy the widget
686 for whom we are handling an event. not good.
689 signal_idle().connect (bind (mem_fun(*this, &IOSelector::remove_port_when_idle), port));
692 select_treeview (treeview);
699 IOSelector::select_next_treeview ()
701 slist<TreeView*>::iterator next;
703 for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
705 if ((*i)->get_name() == "IOSelectorPortListSelected") {
709 if (i == port_displays.end()) {
710 select_treeview (port_displays.front());
712 select_treeview (*i);
721 IOSelector::select_treeview (TreeView* tview)
723 /* Gack. TreeView's don't respond visually to a change
724 in their state, so rename them to force a style
728 Glib::Mutex::Lock lm (port_display_lock);
729 Port* port = reinterpret_cast<Port *> (tview->get_data (_("port")));
731 if (port != selected_port) {
732 selected_port = port;
734 tview->set_name ("IOSelectorPortListSelected");
736 for (slist<TreeView*>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
738 (*i)->set_name ("IOSelectorPortList");
742 selector_box.show_all ();
747 IOSelector::redisplay ()
752 if (io->input_maximum() != 0) {
756 if (io->output_maximum() != 0) {
762 PortInsertUI::PortInsertUI (Session& sess, boost::shared_ptr<PortInsert> pi)
763 : input_selector (sess, pi, true),
764 output_selector (sess, pi, false)
766 hbox.pack_start (output_selector, true, true);
767 hbox.pack_start (input_selector, true, true);
774 PortInsertUI::redisplay()
777 input_selector.redisplay();
778 output_selector.redisplay();
782 PortInsertUI::finished(IOSelector::Result r)
784 input_selector.Finished (r);
785 output_selector.Finished (r);
789 PortInsertWindow::PortInsertWindow (Session& sess, boost::shared_ptr<PortInsert> pi, bool can_cancel)
790 : ArdourDialog ("port insert dialog"),
791 _portinsertui (sess, pi),
792 ok_button (can_cancel ? _("OK"): _("Close")),
793 cancel_button (_("Cancel")),
794 rescan_button (_("Rescan"))
797 set_name ("IOSelectorWindow");
798 string title = _("ardour: ");
802 ok_button.set_name ("IOSelectorButton");
803 cancel_button.set_name ("IOSelectorButton");
804 rescan_button.set_name ("IOSelectorButton");
806 button_box.set_spacing (5);
807 button_box.set_border_width (5);
808 button_box.set_homogeneous (true);
809 button_box.pack_start (rescan_button);
811 button_box.pack_start (cancel_button);
814 cancel_button.hide();
816 button_box.pack_start (ok_button);
818 get_vbox()->pack_start (_portinsertui);
819 get_vbox()->pack_start (button_box, false, false);
821 ok_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::accept));
822 cancel_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::cancel));
823 rescan_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::rescan));
825 signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this)));
826 pi->GoingAway.connect (mem_fun(*this, &PortInsertWindow::plugin_going_away));
830 PortInsertWindow::plugin_going_away (ARDOUR::Redirect* ignored)
832 ENSURE_GUI_THREAD(bind (mem_fun(*this, &PortInsertWindow::plugin_going_away), ignored));
834 delete_when_idle (this);
838 PortInsertWindow::on_map ()
840 _portinsertui.redisplay ();
846 PortInsertWindow::rescan ()
848 _portinsertui.redisplay();
852 PortInsertWindow::cancel ()
854 _portinsertui.finished(IOSelector::Cancelled);
859 PortInsertWindow::accept ()
861 _portinsertui.finished(IOSelector::Accepted);