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"
48 using namespace ARDOUR;
50 IOSelectorWindow::IOSelectorWindow (Session& sess, IO& ior, bool input, bool can_cancel)
51 : ArdourDialog ("i/o selector"),
52 _selector (sess, ior, input),
53 ok_button (can_cancel ? _("OK"): _("Close")),
54 cancel_button (_("Cancel")),
55 rescan_button (_("Rescan"))
58 add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
59 set_name ("IOSelectorWindow");
63 title = string_compose(_("%1 input"), ior.name());
65 title = string_compose(_("%1 output"), ior.name());
68 ok_button.set_name ("IOSelectorButton");
69 cancel_button.set_name ("IOSelectorButton");
70 rescan_button.set_name ("IOSelectorButton");
72 button_box.set_spacing (5);
73 button_box.set_border_width (5);
74 button_box.set_homogeneous (true);
75 button_box.pack_start (rescan_button);
78 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 (Gtk::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);
126 IOSelectorWindow::on_map (GdkEventAny *ev)
128 _selector.redisplay ();
129 return Window::on_map (ev);
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 (Gtk::POLICY_NEVER,
177 Gtk::POLICY_AUTOMATIC);
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;
276 using namespace CList_Helpers;
278 typedef std::map<string,vector<pair<string,string> > > PortMap;
281 PageList& pages = notebook.pages();
283 vector<string> rowdata;
285 current_page = notebook.get_current_page_num ();
288 /* get relevant current JACK ports */
290 ports = session.engine().get_ports ("", JACK_DEFAULT_AUDIO_TYPE, for_input?JackPortIsOutput:JackPortIsInput);
296 /* find all the client names and group their ports into a list-by-client */
298 for (int n = 0; ports[n]; ++n) {
300 pair<string,vector<pair<string,string> > > newpair;
301 pair<string,string> strpair;
302 std::pair<PortMap::iterator,bool> result;
304 string str = ports[n];
305 string::size_type pos;
308 pos = str.find (':');
310 newpair.first = str.substr (0, pos);
311 portname = str.substr (pos+1);
313 /* this may or may not succeed at actually inserting.
314 we don't care, however: we just want an iterator
315 that gives us either the inserted element or
316 the existing one with the same name.
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 Gtk::CList *client_port_display = manage (new Gtk::CList (1));
333 ScrolledWindow *scroller = manage (new ScrolledWindow);
335 scroller->add (*client_port_display);
336 scroller->set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
338 client_box->pack_start (*scroller);
340 client_port_display->set_selection_mode (Gtk::SELECTION_BROWSE);
341 client_port_display->set_name ("IOSelectorList");
343 for (vector<pair<string,string> >::iterator s = i->second.begin(); s != i->second.end(); ++s) {
346 rowdata.push_back (s->first);
347 client_port_display->rows().push_back (rowdata);
348 client_port_display->rows().back().set_data (g_strdup (s->second.c_str()), free);
351 client_port_display->columns_autosize ();
352 client_port_display->select_row.connect (bind (mem_fun(*this, &IOSelector::port_selection_handler), client_port_display));
354 Label *tab_label = manage (new Label);
356 tab_label->set_name ("IOSelectorNotebookTab");
357 tab_label->set_text ((*i).first);
359 pages.push_back (TabElem (*client_box, *tab_label));
362 notebook.set_page (current_page);
363 notebook.signal_show().connect (bind (mem_fun (notebook, &Notebook::set_current_page), current_page));
364 selector_box.show_all ();
368 IOSelector::display_ports ()
371 CList *firstclist = 0;
372 CList *selected_port_list = 0;
375 LockMonitor lm (port_display_lock, __LINE__, __FILE__);
380 limit = io.n_inputs();
382 limit = io.n_outputs();
385 for (slist<CList *>::iterator i = port_displays.begin(); i != port_displays.end(); ) {
387 slist<CList *>::iterator tmp;
392 port_box.remove (**i);
394 port_displays.erase (i);
399 for (uint32_t n = 0; n < limit; ++n) {
400 const gchar *title[1];
401 string really_short_name;
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 title[0] = really_short_name.c_str();
415 clist = new CList (1, title);
420 port_displays.insert (port_displays.end(), clist);
421 port_box.pack_start (*clist);
423 clist->set_data (_("port"), port);
425 /* XXX THIS IS A DIGUSTING AND DIRTY HACK, FORCED UPON US BECAUSE
426 GtkCList DOESN'T PROVIDE ANY WAY TO CONNECT TO BUTTON_PRESS_EVENTS
427 FOR THE COLUMN TITLE BUTTON.
430 clist->column(0).get_widget(); // force the column title button to be created
431 GtkButton *b = GTK_BUTTON(clist->gobj()->column[0].button); // no API to access this
432 Gtk::Button *B = Glib::wrap (b); // make C++ signal handling easier.
434 clist->column_titles_show ();
435 clist->column_titles_active ();
439 if (io.input_maximum() == 1) {
440 selected_port = port;
441 selected_port_list = clist;
443 if (port == selected_port) {
444 selected_port_list = clist;
448 B->signal_button_release_event().connect
449 (bind (mem_fun(*this, &IOSelector::port_column_button_release), clist));
453 if (io.output_maximum() == 1) {
454 selected_port = port;
455 selected_port_list = clist;
457 if (port == selected_port) {
458 selected_port_list = clist;
462 B->signal_button_release_event().connect
463 (bind (mem_fun(*this, &IOSelector::port_column_button_release), clist));
466 clist->set_name ("IOSelectorPortList");
467 clist->set_selection_mode (Gtk::SELECTION_SINGLE);
468 clist->set_shadow_type (Gtk::SHADOW_IN);
469 clist->set_size_request (-1, 75);
471 /* now fill the clist with the current connections */
473 const char **connections = port->get_connections ();
477 for (uint32_t c = 0; connections[c]; ++c) {
481 txt[0] = connections[c];
483 clist->rows().push_back (txt);
489 clist->columns_autosize ();
490 clist->signal_button_release_event().connect (bind (mem_fun(*this, &IOSelector::connection_click), clist));
493 port_box.show_all ();
495 if (selected_port_list) {
496 selected_port_list->click_column(0);
497 selected_port_list->set_name ("IOSelectorPortListSelected");
498 for (slist<CList *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
499 if (*i != selected_port_list) {
500 (*i)->set_name ("IOSelectorPortList");
506 selector_box.hide_all ();
510 if (selected_port_list) {
511 select_clist (selected_port_list);
512 } else if (firstclist) {
514 select_clist (firstclist);
519 IOSelector::port_selection_handler (gint row, gint col, GdkEvent *ev, Gtk::CList *clist)
521 using namespace CList_Helpers;
524 if (selected_port == 0) {
528 string other_port_name = (char *) clist->rows()[row].get_data();
531 if ((status = io.connect_input (selected_port, other_port_name, this)) == 0) {
532 Port *p = session.engine().get_port_by_name (other_port_name);
533 p->enable_metering();
536 status = io.connect_output (selected_port, other_port_name, this);
540 select_next_clist ();
545 IOSelector::ports_changed (IOChange change, void *src)
547 ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelector::ports_changed), change, src));
553 IOSelector::add_port ()
555 /* add a new port, then hide the button if we're up to the maximum allowed */
562 io.add_input_port ("", this);
565 catch (AudioEngine::PortRegistrationFailure& err) {
566 ArdourMessage msg (0, X_("noport dialog"),
567 _("There are no more JACK ports available."));
570 if (io.input_maximum() >= 0 && io.input_maximum() <= (int) io.n_inputs()) {
571 add_port_button.set_sensitive (false);
574 if (io.input_minimum() < (int) io.n_inputs()) {
575 remove_port_button.set_sensitive (true);
581 io.add_output_port ("", this);
584 catch (AudioEngine::PortRegistrationFailure& err) {
585 ArdourMessage msg (0, X_("noport dialog"),
586 _("There are no more JACK ports available."));
589 if (io.output_maximum() >= 0 && io.output_maximum() <= (int) io.n_outputs()) {
590 add_port_button.set_sensitive (false);
596 IOSelector::remove_port ()
598 // always remove last port
602 if ((nports = io.n_inputs()) > 0) {
603 io.remove_input_port (io.input(nports-1), this);
605 if (io.input_minimum() == (int) io.n_inputs()) {
606 remove_port_button.set_sensitive (false);
609 if ((nports = io.n_outputs()) > 0) {
610 io.remove_output_port (io.output(nports-1), this);
616 IOSelector::remove_port_when_idle (Port *port)
619 io.remove_input_port (port, this);
621 io.remove_output_port (port, this);
628 IOSelector::port_column_button_release (GdkEventButton *event, CList *clist)
630 if (Keyboard::is_delete_event (event)) {
633 LockMonitor lm (port_display_lock, __LINE__, __FILE__);
635 port = reinterpret_cast<Port *> (clist->get_data (_("port")));
637 if (port == selected_port) {
639 clist->set_name ("IOSelectorPortList");
644 /* remove the port when idle - if we do it here, we will destroy the widget
645 for whom we are handling an event. not good.
648 Glib::signal_idle().connect (bind (mem_fun(*this, &IOSelector::remove_port_when_idle), port));
658 IOSelector::select_next_clist ()
660 slist<CList*>::iterator next;
662 for (slist<CList *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
664 if ((*i)->get_name() == "IOSelectorPortListSelected") {
668 if (i == port_displays.end()) {
669 select_clist (port_displays.front());
680 IOSelector::select_clist(Gtk::CList* clist)
682 /* Gack. CList's don't respond visually to a change
683 in their state, so rename them to force a style
686 LockMonitor lm (port_display_lock, __LINE__, __FILE__);
687 Port* port = reinterpret_cast<Port *> (clist->get_data (_("port")));
689 if (port != selected_port) {
690 selected_port = port;
692 clist->set_name ("IOSelectorPortListSelected");
694 for (slist<CList *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
696 (*i)->set_name ("IOSelectorPortList");
700 selector_box.show_all ();
705 IOSelector::connection_click (GdkEventButton *ev, CList *clist)
709 /* only handle button1 events here */
711 if (ev->button != 1) {
715 if (clist->get_selection_info ((int)ev->x, (int)ev->y, &row, &col) == 0) {
719 if (row < 0 || col < 0) {
723 Port *port = reinterpret_cast<Port *> (clist->get_data (_("port")));
724 string connected_port_name = clist->cell(row,col).get_text ();
727 Port *p = session.engine().get_port_by_name (connected_port_name);
728 p->disable_metering();
729 io.disconnect_input (port, connected_port_name, this);
731 io.disconnect_output (port, connected_port_name, this);
738 IOSelector::redisplay ()
743 if (io.input_maximum() != 0) {
747 if (io.output_maximum() != 0) {
753 PortInsertUI::PortInsertUI (Session& sess, PortInsert& pi)
754 : input_selector (sess, pi, true),
755 output_selector (sess, pi, false)
757 hbox.pack_start (output_selector, true, true);
758 hbox.pack_start (input_selector, true, true);
765 PortInsertUI::redisplay()
768 input_selector.redisplay();
769 output_selector.redisplay();
773 PortInsertUI::finished(IOSelector::Result r)
775 input_selector.Finished (r);
776 output_selector.Finished (r);
780 PortInsertWindow::PortInsertWindow (Session& sess, PortInsert& pi, bool can_cancel)
781 : ArdourDialog ("port insert dialog"),
782 _portinsertui(sess, pi),
783 ok_button (can_cancel ? _("OK"): _("Close")),
784 cancel_button (_("Cancel")),
785 rescan_button (_("Rescan"))
788 set_name ("IOSelectorWindow");
789 string title = _("ardour: ");
793 ok_button.set_name ("IOSelectorButton");
794 cancel_button.set_name ("IOSelectorButton");
795 rescan_button.set_name ("IOSelectorButton");
797 button_box.set_spacing (5);
798 button_box.set_border_width (5);
799 button_box.set_homogeneous (true);
800 button_box.pack_start (rescan_button);
802 button_box.pack_start (cancel_button);
805 cancel_button.hide();
807 button_box.pack_start (ok_button);
809 vbox.pack_start (_portinsertui);
810 vbox.pack_start (button_box, false, false);
814 ok_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::accept));
815 cancel_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::cancel));
816 rescan_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::rescan));
818 signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this)));
819 pi.GoingAway.connect (mem_fun(*this, &PortInsertWindow::plugin_going_away));
823 PortInsertWindow::plugin_going_away (ARDOUR::Redirect* ignored)
825 ENSURE_GUI_THREAD(bind (mem_fun(*this, &PortInsertWindow::plugin_going_away), ignored));
827 delete_when_idle (this);
831 PortInsertWindow::on_map (GdkEventAny *ev)
833 _portinsertui.redisplay ();
834 return Window::on_map (ev);
839 PortInsertWindow::rescan ()
841 _portinsertui.redisplay();
845 PortInsertWindow::cancel ()
847 _portinsertui.finished(IOSelector::Cancelled);
852 PortInsertWindow::accept ()
854 _portinsertui.finished(IOSelector::Accepted);