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>
36 #include <gtkmm2ext/utils.h>
39 #include "ardour_message.h"
40 #include "io_selector.h"
42 #include "gui_thread.h"
50 using namespace ARDOUR;
51 using namespace Gtkmm2ext;
53 IOSelectorWindow::IOSelectorWindow (Session& sess, IO& ior, bool input, bool can_cancel)
54 : ArdourDialog ("i/o selector"),
55 _selector (sess, ior, input),
56 ok_button (can_cancel ? _("OK"): _("Close")),
57 cancel_button (_("Cancel")),
58 rescan_button (_("Rescan"))
61 add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
62 set_name ("IOSelectorWindow");
66 title = string_compose(_("%1 input"), ior.name());
68 title = string_compose(_("%1 output"), ior.name());
71 ok_button.set_name ("IOSelectorButton");
72 cancel_button.set_name ("IOSelectorButton");
73 rescan_button.set_name ("IOSelectorButton");
75 button_box.set_spacing (5);
76 button_box.set_border_width (5);
77 button_box.set_homogeneous (true);
78 button_box.pack_start (rescan_button);
81 button_box.pack_start (cancel_button);
86 button_box.pack_start (ok_button);
88 get_vbox()->pack_start (_selector);
89 get_vbox()->pack_start (button_box, false, false);
91 ok_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::accept));
92 cancel_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::cancel));
93 rescan_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::rescan));
96 set_position (WIN_POS_MOUSE);
98 signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this)));
101 IOSelectorWindow::~IOSelectorWindow()
106 IOSelectorWindow::rescan ()
108 _selector.redisplay ();
112 IOSelectorWindow::cancel ()
114 _selector.Finished(IOSelector::Cancelled);
119 IOSelectorWindow::accept ()
121 _selector.Finished(IOSelector::Accepted);
126 IOSelectorWindow::on_map ()
128 _selector.redisplay ();
132 /*************************
133 The IO Selector "widget"
134 *************************/
136 IOSelector::IOSelector (Session& sess, IO& ior, bool input)
140 port_frame (for_input? _("Inputs") : _("Outputs")),
141 add_port_button (for_input? _("Add Input") : _("Add Output")),
142 remove_port_button (for_input? _("Remove Input") : _("Remove Output")),
143 clear_connections_button (_("Disconnect All"))
147 notebook.set_name ("IOSelectorNotebook");
148 notebook.set_size_request (-1, 125);
150 clear_connections_button.set_name ("IOSelectorButton");
151 add_port_button.set_name ("IOSelectorButton");
152 remove_port_button.set_name ("IOSelectorButton");
154 selector_frame.set_name ("IOSelectorFrame");
155 port_frame.set_name ("IOSelectorFrame");
157 selector_frame.set_label (_("Available connections"));
159 selector_button_box.set_spacing (5);
160 selector_button_box.set_border_width (5);
162 selector_box.set_spacing (5);
163 selector_box.set_border_width (5);
164 selector_box.pack_start (notebook);
165 selector_box.pack_start (selector_button_box, false, false);
167 selector_frame.add (selector_box);
169 port_box.set_spacing (5);
170 port_box.set_border_width (5);
172 port_display_scroller.set_name ("IOSelectorNotebook");
173 port_display_scroller.set_border_width (0);
174 port_display_scroller.set_size_request (-1, 170);
175 port_display_scroller.add (port_box);
176 port_display_scroller.set_policy (POLICY_NEVER,
179 port_button_box.set_spacing (5);
180 port_button_box.set_border_width (5);
182 port_button_box.pack_start (add_port_button, false, false);
185 if (io.input_maximum() < 0 || io.input_maximum() > (int) io.n_inputs()) {
186 add_port_button.set_sensitive (true);
188 add_port_button.set_sensitive (false);
192 if (io.output_maximum() < 0 || io.output_maximum() > (int) io.n_outputs()) {
193 add_port_button.set_sensitive (true);
195 add_port_button.set_sensitive (false);
200 port_button_box.pack_start (remove_port_button, false, false);
203 if (io.input_minimum() < 0 || io.input_minimum() < (int) io.n_inputs()) {
204 remove_port_button.set_sensitive (true);
206 remove_port_button.set_sensitive (false);
210 if (io.output_minimum() < 0 || io.output_minimum() < (int) io.n_outputs()) {
211 remove_port_button.set_sensitive (true);
213 remove_port_button.set_sensitive (false);
217 port_button_box.pack_start (clear_connections_button, false, false);
219 port_and_button_box.set_border_width (5);
220 port_and_button_box.pack_start (port_button_box, false, false);
221 port_and_button_box.pack_start (port_display_scroller);
223 port_frame.add (port_and_button_box);
225 port_and_selector_box.set_spacing (5);
226 port_and_selector_box.pack_start (port_frame);
227 port_and_selector_box.pack_start (selector_frame);
230 set_border_width (5);
231 pack_start (port_and_selector_box);
236 clear_connections_button.signal_clicked().connect (mem_fun(*this, &IOSelector::clear_connections));
238 add_port_button.signal_clicked().connect (mem_fun(*this, &IOSelector::add_port));
239 remove_port_button.signal_clicked().connect (mem_fun(*this, &IOSelector::remove_port));
242 io.input_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
244 io.output_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
247 io.name_changed.connect (mem_fun(*this, &IOSelector::name_changed));
250 IOSelector::~IOSelector ()
255 IOSelector::name_changed (void* src)
257 ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelector::name_changed), src));
263 IOSelector::clear_connections ()
266 io.disconnect_inputs (this);
268 io.disconnect_outputs (this);
273 IOSelector::rescan ()
275 using namespace Notebook_Helpers;
277 typedef std::map<string,vector<pair<string,string> > > PortMap;
280 PageList& pages = notebook.pages();
282 vector<string> rowdata;
284 current_page = notebook.get_current_page ();
287 /* get relevant current JACK ports */
289 ports = session.engine().get_ports ("", JACK_DEFAULT_AUDIO_TYPE, for_input ? JackPortIsOutput : JackPortIsInput);
295 /* find all the client names and group their ports into a list-by-client */
297 for (int n = 0; ports[n]; ++n) {
299 pair<string,vector<pair<string,string> > > newpair;
300 pair<string,string> strpair;
301 pair<PortMap::iterator,bool> result;
303 string str = ports[n];
304 string::size_type pos;
307 pos = str.find (':');
309 newpair.first = str.substr (0, pos);
310 portname = str.substr (pos+1);
312 result = portmap.insert (newpair);
314 strpair.first = portname;
315 strpair.second = str;
317 result.first->second.push_back (strpair);
322 for (i = portmap.begin(); i != portmap.end(); ++i) {
324 Box *client_box = manage (new VBox);
325 TreeView *display = manage (new TreeView);
326 RefPtr<ListStore> model = ListStore::create (port_display_columns);
327 ScrolledWindow *scroller = manage (new ScrolledWindow);
329 display->set_model (model);
330 display->append_column (X_("notvisible"), port_display_columns.displayed_name);
331 display->set_headers_visible (false);
332 display->get_selection()->set_mode (SELECTION_SINGLE);
333 display->set_name ("IOSelectorList");
335 for (vector<pair<string,string> >::iterator s = i->second.begin(); s != i->second.end(); ++s) {
337 TreeModel::Row row = *(model->append ());
339 row[port_display_columns.displayed_name] = s->first;
340 row[port_display_columns.full_name] = s->second;
343 display->get_selection()->signal_changed().connect
344 (bind (mem_fun(*this, &IOSelector::port_selection_changed), display));
346 Label *tab_label = manage (new Label);
348 tab_label->set_name ("IOSelectorNotebookTab");
349 tab_label->set_text ((*i).first);
351 scroller->add (*display);
352 scroller->set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC);
354 client_box->pack_start (*scroller);
356 pages.push_back (TabElem (*client_box, *tab_label));
359 notebook.set_current_page (current_page);
360 notebook.signal_show().connect (bind (mem_fun (notebook, &Notebook::set_current_page), current_page));
361 selector_box.show_all ();
365 IOSelector::display_ports ()
367 TreeView *firsttview = 0;
368 TreeView *selected_port_tview = 0;
371 LockMonitor lm (port_display_lock, __LINE__, __FILE__);
376 limit = io.n_inputs();
378 limit = io.n_outputs();
381 for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ) {
383 slist<TreeView *>::iterator tmp;
388 port_box.remove (**i);
390 port_displays.erase (i);
396 for (uint32_t n = 0; n < limit; ++n) {
399 //ScrolledWindow *scroller;
400 string really_short_name;
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 //scroller = manage (new ScrolledWindow);
430 //scroller->add (*tview);
431 //scroller->set_policy (POLICY_NEVER, POLICY_NEVER);
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];
447 row[port_display_columns.full_name] = connections[c];
453 if (io.input_maximum() == 1) {
454 selected_port = port;
455 selected_port_tview = tview;
457 if (port == selected_port) {
458 selected_port_tview = tview;
464 if (io.output_maximum() == 1) {
465 selected_port = port;
466 selected_port_tview = tview;
468 if (port == selected_port) {
469 selected_port_tview = tview;
474 TreeViewColumn* col = tview->get_column (0);
476 col->set_clickable (true);
477 //set_treeview_header_as_default_label(col);
478 /* handle button events on the column header and within the treeview itself */
480 //col->signal_button_release_event().connect (bind (mem_fun(*this, &IOSelector::port_column_button_release), tview));
481 tview->signal_button_release_event().connect (bind (mem_fun(*this, &IOSelector::connection_button_release), tview));
484 port_box.show_all ();
486 if (selected_port_tview) {
488 // selected_port_tview->click_column(0);
489 selected_port_tview->set_name ("IOSelectorPortListSelected");
490 for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
491 if (*i != selected_port_tview) {
492 (*i)->set_name ("IOSelectorPortList");
498 selector_box.hide_all ();
502 if (selected_port_tview) {
503 select_treeview (selected_port_tview);
504 } else if (firsttview) {
506 select_treeview (firsttview);
511 IOSelector::port_selection_changed (TreeView* treeview)
513 TreeModel::iterator i = treeview->get_selection()->get_selected();
520 if (selected_port == 0) {
524 ustring other_port_name = (*i)[port_display_columns.full_name];
527 if ((status = io.connect_input (selected_port, other_port_name, this)) == 0) {
528 Port *p = session.engine().get_port_by_name (other_port_name);
529 p->enable_metering();
532 status = io.connect_output (selected_port, other_port_name, this);
536 select_next_treeview ();
541 IOSelector::ports_changed (IOChange change, void *src)
543 ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelector::ports_changed), change, src));
549 IOSelector::add_port ()
551 /* add a new port, then hide the button if we're up to the maximum allowed */
556 io.add_input_port ("", this);
559 catch (AudioEngine::PortRegistrationFailure& err) {
560 ArdourMessage msg (0, X_("noport dialog"), _("There are no more JACK ports available."));
563 if (io.input_maximum() >= 0 && io.input_maximum() <= (int) io.n_inputs()) {
564 add_port_button.set_sensitive (false);
567 if (io.input_minimum() < (int) io.n_inputs()) {
568 remove_port_button.set_sensitive (true);
574 io.add_output_port ("", this);
577 catch (AudioEngine::PortRegistrationFailure& err) {
578 ArdourMessage msg (0, X_("noport dialog"),
579 _("There are no more JACK ports available."));
582 if (io.output_maximum() >= 0 && io.output_maximum() <= (int) io.n_outputs()) {
583 add_port_button.set_sensitive (false);
589 IOSelector::remove_port ()
593 // always remove last port
596 if ((nports = io.n_inputs()) > 0) {
597 io.remove_input_port (io.input(nports-1), this);
599 if (io.input_minimum() == (int) io.n_inputs()) {
600 remove_port_button.set_sensitive (false);
603 if ((nports = io.n_outputs()) > 0) {
604 io.remove_output_port (io.output(nports-1), this);
610 IOSelector::remove_port_when_idle (Port *port)
613 io.remove_input_port (port, this);
615 io.remove_output_port (port, this);
622 IOSelector::connection_button_release (GdkEventButton *ev, TreeView *treeview)
624 /* this handles button release on a port name row: i.e. a connection
625 between the named port and the port represented by the treeview.
628 Gtk::TreeModel::iterator iter;
629 TreeModel::Path path;
630 TreeViewColumn* column;
634 /* only handle button1 events here */
636 if (ev->button != 1) {
640 if (!(Keyboard::is_delete_event (ev))) {
644 if (!treeview->get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
647 cerr << "path = " << path.to_string() << endl;
649 if ((iter = treeview->get_model()->get_iter (path.to_string()))) {
652 ustring connected_port_name = (*iter)[port_display_columns.full_name];
653 cerr << "selected row displayed_name: " << (*iter)[port_display_columns.displayed_name] << endl;
654 cerr << "selected row string was " << connected_port_name << endl;
655 Port *port = reinterpret_cast<Port *> (treeview->get_data (_("port")));
658 Port *p = session.engine().get_port_by_name (connected_port_name);
659 p->disable_metering();
660 io.disconnect_input (port, connected_port_name, this);
662 io.disconnect_output (port, connected_port_name, this);
670 IOSelector::port_column_button_release (GdkEventButton* event, TreeView* treeview)
672 /* this handles button release on the button at the top of a single-column
673 treeview (representing a port)
675 cerr << "IOSelector::port_column_button_release() called" << endl;
677 if (Keyboard::is_delete_event (event)) {
680 LockMonitor lm (port_display_lock, __LINE__, __FILE__);
682 port = static_cast<Port *> (treeview->get_data (_("port")));
684 if (port == selected_port) {
686 treeview->set_name ("IOSelectorPortList");
687 treeview->queue_draw();
691 /* remove the port when idle - if we do it here, we will destroy the widget
692 for whom we are handling an event. not good.
695 signal_idle().connect (bind (mem_fun(*this, &IOSelector::remove_port_when_idle), port));
698 select_treeview (treeview);
705 IOSelector::select_next_treeview ()
707 slist<TreeView*>::iterator next;
709 for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
711 if ((*i)->get_name() == "IOSelectorPortListSelected") {
715 if (i == port_displays.end()) {
716 select_treeview (port_displays.front());
718 select_treeview (*i);
727 IOSelector::select_treeview (TreeView* tview)
729 /* Gack. TreeView's don't respond visually to a change
730 in their state, so rename them to force a style
734 LockMonitor lm (port_display_lock, __LINE__, __FILE__);
735 Port* port = reinterpret_cast<Port *> (tview->get_data (_("port")));
737 if (port != selected_port) {
738 selected_port = port;
740 tview->set_name ("IOSelectorPortListSelected");
742 for (slist<TreeView*>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
744 (*i)->set_name ("IOSelectorPortList");
748 selector_box.show_all ();
753 IOSelector::redisplay ()
758 if (io.input_maximum() != 0) {
762 if (io.output_maximum() != 0) {
768 PortInsertUI::PortInsertUI (Session& sess, PortInsert& pi)
769 : input_selector (sess, pi, true),
770 output_selector (sess, pi, false)
772 hbox.pack_start (output_selector, true, true);
773 hbox.pack_start (input_selector, true, true);
780 PortInsertUI::redisplay()
783 input_selector.redisplay();
784 output_selector.redisplay();
788 PortInsertUI::finished(IOSelector::Result r)
790 input_selector.Finished (r);
791 output_selector.Finished (r);
795 PortInsertWindow::PortInsertWindow (Session& sess, PortInsert& pi, bool can_cancel)
796 : ArdourDialog ("port insert dialog"),
797 _portinsertui(sess, pi),
798 ok_button (can_cancel ? _("OK"): _("Close")),
799 cancel_button (_("Cancel")),
800 rescan_button (_("Rescan"))
803 set_name ("IOSelectorWindow");
804 string title = _("ardour: ");
808 ok_button.set_name ("IOSelectorButton");
809 cancel_button.set_name ("IOSelectorButton");
810 rescan_button.set_name ("IOSelectorButton");
812 button_box.set_spacing (5);
813 button_box.set_border_width (5);
814 button_box.set_homogeneous (true);
815 button_box.pack_start (rescan_button);
817 button_box.pack_start (cancel_button);
820 cancel_button.hide();
822 button_box.pack_start (ok_button);
824 get_vbox()->pack_start (_portinsertui);
825 get_vbox()->pack_start (button_box, false, false);
827 ok_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::accept));
828 cancel_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::cancel));
829 rescan_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::rescan));
831 signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this)));
832 pi.GoingAway.connect (mem_fun(*this, &PortInsertWindow::plugin_going_away));
836 PortInsertWindow::plugin_going_away (ARDOUR::Redirect* ignored)
838 ENSURE_GUI_THREAD(bind (mem_fun(*this, &PortInsertWindow::plugin_going_away), ignored));
840 delete_when_idle (this);
844 PortInsertWindow::on_map ()
846 _portinsertui.redisplay ();
852 PortInsertWindow::rescan ()
854 _portinsertui.redisplay();
858 PortInsertWindow::cancel ()
860 _portinsertui.finished(IOSelector::Cancelled);
865 PortInsertWindow::accept ()
867 _portinsertui.finished(IOSelector::Accepted);