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"
40 #include "extra_bind.h"
42 #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 = compose(_("%1 input"), ior.name());
66 title = 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);
85 button_box.pack_start (ok_button);
87 vbox.pack_start (_selector);
88 vbox.pack_start (button_box, false, false);
90 ok_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::accept));
91 cancel_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::cancel));
92 rescan_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::rescan));
95 set_position (Gtk::WIN_POS_MOUSE);
98 delete_event.connect (bind (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);
127 IOSelectorWindow::on_map (GdkEventAny *ev)
129 _selector.redisplay ();
130 return Window::on_map (ev);
133 /*************************
134 The IO Selector "widget"
135 *************************/
137 IOSelector::IOSelector (Session& sess, IO& ior, bool input)
141 port_frame (for_input? _("Inputs") : _("Outputs")),
142 add_port_button (for_input? _("Add Input") : _("Add Output")),
143 remove_port_button (for_input? _("Remove Input") : _("Remove Output")),
144 clear_connections_button (_("Disconnect All"))
148 notebook.set_name ("IOSelectorNotebook");
149 notebook.set_size_request (-1, 125);
151 clear_connections_button.set_name ("IOSelectorButton");
152 add_port_button.set_name ("IOSelectorButton");
153 remove_port_button.set_name ("IOSelectorButton");
155 selector_frame.set_name ("IOSelectorFrame");
156 port_frame.set_name ("IOSelectorFrame");
158 selector_frame.set_label (_("Available connections"));
160 selector_button_box.set_spacing (5);
161 selector_button_box.set_border_width (5);
163 selector_box.set_spacing (5);
164 selector_box.set_border_width (5);
165 selector_box.pack_start (notebook);
166 selector_box.pack_start (selector_button_box, false, false);
168 selector_frame.add (selector_box);
170 port_box.set_spacing (5);
171 port_box.set_border_width (5);
173 port_display_scroller.set_name ("IOSelectorNotebook");
174 port_display_scroller.set_border_width (0);
175 port_display_scroller.set_size_request (-1, 170);
176 port_display_scroller.add_with_viewport (port_box);
177 port_display_scroller.set_policy (Gtk::POLICY_NEVER,
178 Gtk::POLICY_AUTOMATIC);
180 port_button_box.set_spacing (5);
181 port_button_box.set_border_width (5);
183 port_button_box.pack_start (add_port_button, false, false);
186 if (io.input_maximum() < 0 || io.input_maximum() > (int) io.n_inputs()) {
187 add_port_button.set_sensitive (true);
189 add_port_button.set_sensitive (false);
193 if (io.output_maximum() < 0 || io.output_maximum() > (int) io.n_outputs()) {
194 add_port_button.set_sensitive (true);
196 add_port_button.set_sensitive (false);
201 port_button_box.pack_start (remove_port_button, false, false);
204 if (io.input_minimum() < 0 || io.input_minimum() < (int) io.n_inputs()) {
205 remove_port_button.set_sensitive (true);
207 remove_port_button.set_sensitive (false);
211 if (io.output_minimum() < 0 || io.output_minimum() < (int) io.n_outputs()) {
212 remove_port_button.set_sensitive (true);
214 remove_port_button.set_sensitive (false);
218 port_button_box.pack_start (clear_connections_button, false, false);
220 port_and_button_box.set_border_width (5);
221 port_and_button_box.pack_start (port_button_box, false, false);
222 port_and_button_box.pack_start (port_display_scroller);
224 port_frame.add (port_and_button_box);
226 port_and_selector_box.set_spacing (5);
227 port_and_selector_box.pack_start (port_frame);
228 port_and_selector_box.pack_start (selector_frame);
231 set_border_width (5);
232 pack_start (port_and_selector_box);
237 clear_connections_button.signal_clicked().connect (mem_fun(*this, &IOSelector::clear_connections));
239 add_port_button.signal_clicked().connect (mem_fun(*this, &IOSelector::add_port));
240 remove_port_button.signal_clicked().connect (mem_fun(*this, &IOSelector::remove_port));
243 io.input_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
245 io.output_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
248 io.name_changed.connect (mem_fun(*this, &IOSelector::name_changed));
251 IOSelector::~IOSelector ()
256 IOSelector::name_changed (void* src)
258 ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelector::name_changed), src));
264 IOSelector::clear_connections ()
267 io.disconnect_inputs (this);
269 io.disconnect_outputs (this);
274 IOSelector::rescan ()
276 using namespace Notebook_Helpers;
277 using namespace CList_Helpers;
279 typedef map<string,vector<pair<string,string> > > PortMap;
282 PageList& pages = notebook.pages();
284 vector<string> rowdata;
286 current_page = notebook.get_current_page_num ();
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 /* this may or may not succeed at actually inserting.
315 we don't care, however: we just want an iterator
316 that gives us either the inserted element or
317 the existing one with the same name.
320 result = portmap.insert (newpair);
322 strpair.first = portname;
323 strpair.second = str;
325 result.first->second.push_back (strpair);
330 for (i = portmap.begin(); i != portmap.end(); ++i) {
332 Box *client_box = manage (new VBox);
333 Gtk::CList *client_port_display = manage (new Gtk::CList (1));
334 ScrolledWindow *scroller = manage (new ScrolledWindow);
336 scroller->add_with_viewport (*client_port_display);
337 scroller->set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
339 client_box->pack_start (*scroller);
341 client_port_display->set_selection_mode (GTK_SELECTION_BROWSE);
342 client_port_display->set_name ("IOSelectorList");
344 for (vector<pair<string,string> >::iterator s = i->second.begin(); s != i->second.end(); ++s) {
347 rowdata.push_back (s->first);
348 client_port_display->rows().push_back (rowdata);
349 client_port_display->rows().back().set_data (g_strdup (s->second.c_str()), free);
352 client_port_display->columns_autosize ();
353 client_port_display->select_row.connect (bind (mem_fun(*this, &IOSelector::port_selection_handler), client_port_display));
355 Label *tab_label = manage (new Label);
357 tab_label->set_name ("IOSelectorNotebookTab");
358 tab_label->set_text ((*i).first);
360 pages.push_back (TabElem (*client_box, *tab_label));
363 notebook.set_page (current_page);
364 notebook.show.connect (bind (mem_fun (notebook, &Notebook::set_page), current_page));
365 selector_box.show_all ();
369 IOSelector::display_ports ()
372 CList *firstclist = 0;
373 CList *selected_port_list = 0;
376 LockMonitor lm (port_display_lock, __LINE__, __FILE__);
381 limit = io.n_inputs();
383 limit = io.n_outputs();
386 for (slist<CList *>::iterator i = port_displays.begin(); i != port_displays.end(); ) {
388 slist<CList *>::iterator tmp;
393 port_box.remove (**i);
395 port_displays.erase (i);
400 for (uint32_t n = 0; n < limit; ++n) {
401 const gchar *title[1];
402 string really_short_name;
407 port = io.output (n);
410 /* we know there is '/' because we put it there */
412 really_short_name = port->short_name();
413 really_short_name = really_short_name.substr (really_short_name.find ('/') + 1);
415 title[0] = really_short_name.c_str();
416 clist = new CList (1, title);
421 port_displays.insert (port_displays.end(), clist);
422 port_box.pack_start (*clist);
424 clist->set_data (_("port"), port);
426 /* XXX THIS IS A DIGUSTING AND DIRTY HACK, FORCED UPON US BECAUSE
427 GtkCList DOESN'T PROVIDE ANY WAY TO CONNECT TO BUTTON_PRESS_EVENTS
428 FOR THE COLUMN TITLE BUTTON.
431 clist->column(0).get_widget(); // force the column title button to be created
432 GtkButton *b = GTK_BUTTON(clist->gobj()->column[0].button); // no API to access this
433 Gtk::Button *B = wrap (b); // make C++ signal handling easier.
435 clist->column_titles_show ();
436 clist->column_titles_active ();
440 if (io.input_maximum() == 1) {
441 selected_port = port;
442 selected_port_list = clist;
444 if (port == selected_port) {
445 selected_port_list = clist;
449 B->signal_button_release_event.connect
450 (bind (mem_fun(*this, &IOSelector::port_column_button_release), clist));
454 if (io.output_maximum() == 1) {
455 selected_port = port;
456 selected_port_list = clist;
458 if (port == selected_port) {
459 selected_port_list = clist;
463 B->signal_button_release_event.connect
464 (bind (mem_fun(*this, &IOSelector::port_column_button_release), clist));
467 clist->set_name ("IOSelectorPortList");
468 clist->set_selection_mode (GTK_SELECTION_SINGLE);
469 clist->set_shadow_type (Gtk::SHADOW_IN);
470 clist->set_size_request (-1, 75);
472 /* now fill the clist with the current connections */
474 const char **connections = port->get_connections ();
478 for (uint32_t c = 0; connections[c]; ++c) {
482 txt[0] = connections[c];
484 clist->rows().push_back (txt);
490 clist->columns_autosize ();
491 clist->signal_button_release_event.connect (bind (mem_fun(*this, &IOSelector::connection_click), clist));
494 port_box.show_all ();
496 if (selected_port_list) {
497 selected_port_list->click_column(0);
498 selected_port_list->set_name ("IOSelectorPortListSelected");
499 for (slist<CList *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
500 if (*i != selected_port_list) {
501 (*i)->set_name ("IOSelectorPortList");
507 selector_box.hide_all ();
511 if (selected_port_list) {
512 select_clist (selected_port_list);
513 } else if (firstclist) {
515 select_clist (firstclist);
520 IOSelector::port_selection_handler (gint row, gint col, GdkEvent *ev, Gtk::CList *clist)
522 using namespace CList_Helpers;
525 if (selected_port == 0) {
529 string other_port_name = (char *) clist->rows()[row].get_data();
532 if ((status = io.connect_input (selected_port, other_port_name, this)) == 0) {
533 Port *p = session.engine().get_port_by_name (other_port_name);
534 p->enable_metering();
537 status = io.connect_output (selected_port, other_port_name, this);
541 select_next_clist ();
546 IOSelector::ports_changed (IOChange change, void *src)
548 ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelector::ports_changed), change, src));
554 IOSelector::add_port ()
556 /* add a new port, then hide the button if we're up to the maximum allowed */
563 io.add_input_port ("", this);
566 catch (AudioEngine::PortRegistrationFailure& err) {
567 ArdourMessage msg (0, X_("noport dialog"),
568 _("There are no more JACK ports available."));
571 if (io.input_maximum() >= 0 && io.input_maximum() <= (int) io.n_inputs()) {
572 add_port_button.set_sensitive (false);
575 if (io.input_minimum() < (int) io.n_inputs()) {
576 remove_port_button.set_sensitive (true);
582 io.add_output_port ("", this);
585 catch (AudioEngine::PortRegistrationFailure& err) {
586 ArdourMessage msg (0, X_("noport dialog"),
587 _("There are no more JACK ports available."));
590 if (io.output_maximum() >= 0 && io.output_maximum() <= (int) io.n_outputs()) {
591 add_port_button.set_sensitive (false);
597 IOSelector::remove_port ()
599 // always remove last port
603 if ((nports = io.n_inputs()) > 0) {
604 io.remove_input_port (io.input(nports-1), this);
606 if (io.input_minimum() == (int) io.n_inputs()) {
607 remove_port_button.set_sensitive (false);
610 if ((nports = io.n_outputs()) > 0) {
611 io.remove_output_port (io.output(nports-1), this);
617 IOSelector::remove_port_when_idle (Port *port)
620 io.remove_input_port (port, this);
622 io.remove_output_port (port, this);
629 IOSelector::port_column_button_release (GdkEventButton *event, CList *clist)
631 if (Keyboard::is_delete_event (event)) {
634 LockMonitor lm (port_display_lock, __LINE__, __FILE__);
636 port = reinterpret_cast<Port *> (clist->get_data (_("port")));
638 if (port == selected_port) {
640 clist->set_name ("IOSelectorPortList");
645 /* remove the port when idle - if we do it here, we will destroy the widget
646 for whom we are handling an event. not good.
649 Gtk::Main::idle.connect (bind (mem_fun(*this, &IOSelector::remove_port_when_idle), port));
659 IOSelector::select_next_clist ()
661 slist<CList*>::iterator next;
663 for (slist<CList *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
665 if ((*i)->get_name() == "IOSelectorPortListSelected") {
669 if (i == port_displays.end()) {
670 select_clist (port_displays.front());
681 IOSelector::select_clist(Gtk::CList* clist)
683 /* Gack. CList's don't respond visually to a change
684 in their state, so rename them to force a style
687 LockMonitor lm (port_display_lock, __LINE__, __FILE__);
688 Port* port = reinterpret_cast<Port *> (clist->get_data (_("port")));
690 if (port != selected_port) {
691 selected_port = port;
693 clist->set_name ("IOSelectorPortListSelected");
695 for (slist<CList *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
697 (*i)->set_name ("IOSelectorPortList");
701 selector_box.show_all ();
706 IOSelector::connection_click (GdkEventButton *ev, CList *clist)
710 /* only handle button1 events here */
712 if (ev->button != 1) {
716 if (clist->get_selection_info ((int)ev->x, (int)ev->y, &row, &col) == 0) {
720 if (row < 0 || col < 0) {
724 Port *port = reinterpret_cast<Port *> (clist->get_data (_("port")));
725 string connected_port_name = clist->cell(row,col).get_text ();
728 Port *p = session.engine().get_port_by_name (connected_port_name);
729 p->disable_metering();
730 io.disconnect_input (port, connected_port_name, this);
732 io.disconnect_output (port, connected_port_name, this);
739 IOSelector::redisplay ()
744 if (io.input_maximum() != 0) {
748 if (io.output_maximum() != 0) {
754 PortInsertUI::PortInsertUI (Session& sess, PortInsert& pi)
755 : input_selector (sess, pi, true),
756 output_selector (sess, pi, false)
758 hbox.pack_start (output_selector, true, true);
759 hbox.pack_start (input_selector, true, true);
766 PortInsertUI::redisplay()
769 input_selector.redisplay();
770 output_selector.redisplay();
774 PortInsertUI::finished(IOSelector::Result r)
776 input_selector.Finished (r);
777 output_selector.Finished (r);
781 PortInsertWindow::PortInsertWindow (Session& sess, PortInsert& pi, bool can_cancel)
782 : ArdourDialog ("port insert dialog"),
783 _portinsertui(sess, pi),
784 ok_button (can_cancel ? _("OK"): _("Close")),
785 cancel_button (_("Cancel")),
786 rescan_button (_("Rescan"))
789 set_name ("IOSelectorWindow");
790 string title = _("ardour: ");
794 ok_button.set_name ("IOSelectorButton");
795 cancel_button.set_name ("IOSelectorButton");
796 rescan_button.set_name ("IOSelectorButton");
798 button_box.set_spacing (5);
799 button_box.set_border_width (5);
800 button_box.set_homogeneous (true);
801 button_box.pack_start (rescan_button);
803 button_box.pack_start (cancel_button);
806 cancel_button.hide();
808 button_box.pack_start (ok_button);
810 vbox.pack_start (_portinsertui);
811 vbox.pack_start (button_box, false, false);
815 ok_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::accept));
816 cancel_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::cancel));
817 rescan_button.signal_clicked().connect (mem_fun(*this, &PortInsertWindow::rescan));
819 delete_event.connect (bind (ptr_fun (just_hide_it), reinterpret_cast<Window *> (this)));
820 pi.GoingAway.connect (mem_fun(*this, &PortInsertWindow::plugin_going_away));
824 PortInsertWindow::plugin_going_away (ARDOUR::Redirect* ignored)
826 ENSURE_GUI_THREAD(bind (mem_fun(*this, &PortInsertWindow::plugin_going_away), ignored));
828 delete_when_idle (this);
832 PortInsertWindow::on_map (GdkEventAny *ev)
834 _portinsertui.redisplay ();
835 return Window::on_map (ev);
840 PortInsertWindow::rescan ()
842 _portinsertui.redisplay();
846 PortInsertWindow::cancel ()
848 _portinsertui.finished(IOSelector::Cancelled);
853 PortInsertWindow::accept ()
855 _portinsertui.finished(IOSelector::Accepted);