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 /* find all the client names and group their ports into a list-by-client */
295 for (int n = 0; ports[n]; ++n) {
297 pair<string,vector<pair<string,string> > > newpair;
298 pair<string,string> strpair;
299 pair<PortMap::iterator,bool> result;
301 string str = ports[n];
302 string::size_type pos;
305 pos = str.find (':');
307 newpair.first = str.substr (0, pos);
308 portname = str.substr (pos+1);
310 result = portmap.insert (newpair);
312 strpair.first = portname;
313 strpair.second = str;
315 result.first->second.push_back (strpair);
320 for (i = portmap.begin(); i != portmap.end(); ++i) {
322 Box *client_box = manage (new VBox);
323 TreeView *display = manage (new TreeView);
324 RefPtr<ListStore> model = ListStore::create (port_display_columns);
325 ScrolledWindow *scroller = manage (new ScrolledWindow);
327 display->set_model (model);
328 display->append_column (X_("notvisible"), port_display_columns.displayed_name);
329 display->set_headers_visible (false);
330 display->get_selection()->set_mode (SELECTION_SINGLE);
331 display->set_name ("IOSelectorList");
333 for (vector<pair<string,string> >::iterator s = i->second.begin(); s != i->second.end(); ++s) {
335 TreeModel::Row row = *(model->append ());
337 row[port_display_columns.displayed_name] = s->first;
338 row[port_display_columns.full_name] = s->second;
341 display->get_selection()->signal_changed().connect
342 (bind (mem_fun(*this, &IOSelector::port_selection_changed), display));
344 Label *tab_label = manage (new Label);
346 tab_label->set_name ("IOSelectorNotebookTab");
347 tab_label->set_text ((*i).first);
349 scroller->add (*display);
350 scroller->set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC);
352 client_box->pack_start (*scroller);
354 pages.push_back (TabElem (*client_box, *tab_label));
357 notebook.set_current_page (current_page);
358 notebook.signal_show().connect (bind (mem_fun (notebook, &Notebook::set_current_page), current_page));
359 selector_box.show_all ();
363 IOSelector::display_ports ()
365 TreeView *firsttview = 0;
366 TreeView *selected_port_tview = 0;
369 LockMonitor lm (port_display_lock, __LINE__, __FILE__);
374 limit = io.n_inputs();
376 limit = io.n_outputs();
379 for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ) {
381 slist<TreeView *>::iterator tmp;
386 port_box.remove (**i);
388 port_displays.erase (i);
394 for (uint32_t n = 0; n < limit; ++n) {
397 //ScrolledWindow *scroller;
398 string really_short_name;
403 port = io.output (n);
406 /* we know there is '/' because we put it there */
408 really_short_name = port->short_name();
409 really_short_name = really_short_name.substr (really_short_name.find ('/') + 1);
411 tview = manage (new TreeView());
412 RefPtr<ListStore> port_model = ListStore::create (port_display_columns);
418 tview->set_model (port_model);
419 tview->append_column (really_short_name, port_display_columns.displayed_name);
420 tview->get_selection()->set_mode (SELECTION_SINGLE);
421 tview->set_data ("port", port);
422 tview->set_headers_visible (true);
423 tview->set_name ("IOSelectorPortList");
425 port_box.pack_start (*tview);
426 //scroller = manage (new ScrolledWindow);
428 //scroller->add (*tview);
429 //scroller->set_policy (POLICY_NEVER, POLICY_NEVER);
431 port_displays.insert (port_displays.end(), tview);
432 //port_box.pack_start (*scroller);
434 //scroller->set_size_request (-1, 75);
436 /* now fill the clist with the current connections */
439 const char **connections = port->get_connections ();
442 for (uint32_t c = 0; connections[c]; ++c) {
443 TreeModel::Row row = *(port_model->append());
444 row[port_display_columns.displayed_name] = connections[c];
445 row[port_display_columns.full_name] = connections[c];
451 if (io.input_maximum() == 1) {
452 selected_port = port;
453 selected_port_tview = tview;
455 if (port == selected_port) {
456 selected_port_tview = tview;
462 if (io.output_maximum() == 1) {
463 selected_port = port;
464 selected_port_tview = tview;
466 if (port == selected_port) {
467 selected_port_tview = tview;
472 TreeViewColumn* col = tview->get_column (0);
474 col->set_clickable (true);
475 //col->set_widget(0);
476 /* handle button events on the column header and within the treeview itself */
478 //col->get_widget()->signal_button_release_event().connect (bind (mem_fun(*this, &IOSelector::port_column_button_release), tview));
479 tview->signal_button_release_event().connect (bind (mem_fun(*this, &IOSelector::connection_button_release), tview));
482 port_box.show_all ();
484 if (selected_port_tview) {
486 // selected_port_tview->click_column(0);
487 selected_port_tview->set_name ("IOSelectorPortListSelected");
488 for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
489 if (*i != selected_port_tview) {
490 (*i)->set_name ("IOSelectorPortList");
496 selector_box.hide_all ();
500 if (selected_port_tview) {
501 select_treeview (selected_port_tview);
502 } else if (firsttview) {
504 select_treeview (firsttview);
509 IOSelector::port_selection_changed (TreeView* treeview)
511 TreeModel::iterator i = treeview->get_selection()->get_selected();
518 if (selected_port == 0) {
522 ustring other_port_name = (*i)[port_display_columns.full_name];
525 if ((status = io.connect_input (selected_port, other_port_name, this)) == 0) {
526 Port *p = session.engine().get_port_by_name (other_port_name);
527 p->enable_metering();
530 status = io.connect_output (selected_port, other_port_name, this);
534 select_next_treeview ();
539 IOSelector::ports_changed (IOChange change, void *src)
541 ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelector::ports_changed), change, src));
547 IOSelector::add_port ()
549 /* add a new port, then hide the button if we're up to the maximum allowed */
554 io.add_input_port ("", this);
557 catch (AudioEngine::PortRegistrationFailure& err) {
558 ArdourMessage msg (0, X_("noport dialog"), _("There are no more JACK ports available."));
561 if (io.input_maximum() >= 0 && io.input_maximum() <= (int) io.n_inputs()) {
562 add_port_button.set_sensitive (false);
565 if (io.input_minimum() < (int) io.n_inputs()) {
566 remove_port_button.set_sensitive (true);
572 io.add_output_port ("", this);
575 catch (AudioEngine::PortRegistrationFailure& err) {
576 ArdourMessage msg (0, X_("noport dialog"),
577 _("There are no more JACK ports available."));
580 if (io.output_maximum() >= 0 && io.output_maximum() <= (int) io.n_outputs()) {
581 add_port_button.set_sensitive (false);
587 IOSelector::remove_port ()
591 // always remove last port
594 if ((nports = io.n_inputs()) > 0) {
595 io.remove_input_port (io.input(nports-1), this);
597 if (io.input_minimum() == (int) io.n_inputs()) {
598 remove_port_button.set_sensitive (false);
601 if ((nports = io.n_outputs()) > 0) {
602 io.remove_output_port (io.output(nports-1), this);
608 IOSelector::remove_port_when_idle (Port *port)
611 io.remove_input_port (port, this);
613 io.remove_output_port (port, this);
620 IOSelector::connection_button_release (GdkEventButton *ev, TreeView *treeview)
622 /* this handles button release on a port name row: i.e. a connection
623 between the named port and the port represented by the treeview.
626 Gtk::TreeModel::iterator iter;
627 TreeModel::Path path;
628 TreeViewColumn* column;
632 /* only handle button1 events here */
634 if (ev->button != 1) {
638 if (!(Keyboard::is_delete_event (ev))) {
642 if (!treeview->get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
645 cerr << "path = " << path.to_string() << endl;
647 if ((iter = treeview->get_model()->get_iter (path.to_string()))) {
650 ustring connected_port_name = (*iter)[port_display_columns.full_name];
651 cerr << "selected row displayed_name: " << (*iter)[port_display_columns.displayed_name] << endl;
652 cerr << "selected row string was " << connected_port_name << endl;
653 Port *port = reinterpret_cast<Port *> (treeview->get_data (_("port")));
656 Port *p = session.engine().get_port_by_name (connected_port_name);
657 p->disable_metering();
658 io.disconnect_input (port, connected_port_name, this);
660 io.disconnect_output (port, connected_port_name, this);
668 IOSelector::port_column_button_release (GdkEventButton* event, TreeView* treeview)
670 /* this handles button release on the button at the top of a single-column
671 treeview (representing a port)
673 cerr << "IOSelector::port_column_button_release() called" << endl;
675 if (Keyboard::is_delete_event (event)) {
678 LockMonitor lm (port_display_lock, __LINE__, __FILE__);
680 port = static_cast<Port *> (treeview->get_data (_("port")));
682 if (port == selected_port) {
684 treeview->set_name ("IOSelectorPortList");
685 treeview->queue_draw();
689 /* remove the port when idle - if we do it here, we will destroy the widget
690 for whom we are handling an event. not good.
693 signal_idle().connect (bind (mem_fun(*this, &IOSelector::remove_port_when_idle), port));
696 select_treeview (treeview);
703 IOSelector::select_next_treeview ()
705 slist<TreeView*>::iterator next;
707 for (slist<TreeView *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
709 if ((*i)->get_name() == "IOSelectorPortListSelected") {
713 if (i == port_displays.end()) {
714 select_treeview (port_displays.front());
716 select_treeview (*i);
725 IOSelector::select_treeview (TreeView* tview)
727 /* Gack. TreeView's don't respond visually to a change
728 in their state, so rename them to force a style
732 LockMonitor lm (port_display_lock, __LINE__, __FILE__);
733 Port* port = reinterpret_cast<Port *> (tview->get_data (_("port")));
735 if (port != selected_port) {
736 selected_port = port;
738 tview->set_name ("IOSelectorPortListSelected");
740 for (slist<TreeView*>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
742 (*i)->set_name ("IOSelectorPortList");
746 selector_box.show_all ();
751 IOSelector::redisplay ()
756 if (io.input_maximum() != 0) {
760 if (io.output_maximum() != 0) {
766 PortInsertUI::PortInsertUI (Session& sess, PortInsert& pi)
767 : input_selector (sess, pi, true),
768 output_selector (sess, pi, false)
770 hbox.pack_start (output_selector, true, true);
771 hbox.pack_start (input_selector, true, true);
778 PortInsertUI::redisplay()
781 input_selector.redisplay();
782 output_selector.redisplay();
786 PortInsertUI::finished(IOSelector::Result r)
788 input_selector.Finished (r);
789 output_selector.Finished (r);
793 PortInsertWindow::PortInsertWindow (Session& sess, PortInsert& pi, bool can_cancel)
794 : ArdourDialog ("port insert dialog"),
795 _portinsertui(sess, pi),
796 ok_button (can_cancel ? _("OK"): _("Close")),
797 cancel_button (_("Cancel")),
798 rescan_button (_("Rescan"))
801 set_name ("IOSelectorWindow");
802 string title = _("ardour: ");
806 ok_button.set_name ("IOSelectorButton");
807 cancel_button.set_name ("IOSelectorButton");
808 rescan_button.set_name ("IOSelectorButton");
810 button_box.set_spacing (5);
811 button_box.set_border_width (5);
812 button_box.set_homogeneous (true);
813 button_box.pack_start (rescan_button);
815 button_box.pack_start (cancel_button);
818 cancel_button.hide();
820 button_box.pack_start (ok_button);
822 vbox.pack_start (_portinsertui);
823 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);