various fixes to MidiRegionView selection handling, key handling, drawing of ghost...
[ardour2.git] / gtk2_ardour / global_port_matrix.cc
blob565def896e0618395b998a0286f942ecef8e7a4e
1 /*
2 Copyright (C) 2009 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.
20 #include <gtkmm/image.h>
21 #include <gtkmm/stock.h>
22 #include "global_port_matrix.h"
23 #include "utils.h"
25 #include "ardour/bundle.h"
26 #include "ardour/session.h"
27 #include "ardour/audioengine.h"
28 #include "ardour/port.h"
30 #include "i18n.h"
32 using namespace std;
33 using namespace ARDOUR;
35 GlobalPortMatrix::GlobalPortMatrix (Gtk::Window* p, Session* s, DataType t)
36 : PortMatrix (p, s, t)
38 setup_all_ports ();
39 init ();
42 void
43 GlobalPortMatrix::setup_ports (int dim)
45 _ports[dim].suspend_signals ();
46 _ports[dim].gather (_session, type(), dim == IN, false);
47 _ports[dim].resume_signals ();
50 void
51 GlobalPortMatrix::set_state (BundleChannel c[2], bool s)
53 Bundle::PortList const & in_ports = c[IN].bundle->channel_ports (c[IN].channel);
54 Bundle::PortList const & out_ports = c[OUT].bundle->channel_ports (c[OUT].channel);
56 for (Bundle::PortList::const_iterator i = in_ports.begin(); i != in_ports.end(); ++i) {
57 for (Bundle::PortList::const_iterator j = out_ports.begin(); j != out_ports.end(); ++j) {
59 Port* p = _session->engine().get_port_by_name (*i);
60 Port* q = _session->engine().get_port_by_name (*j);
62 if (p) {
63 if (s) {
64 p->connect (*j);
65 } else {
66 p->disconnect (*j);
68 } else if (q) {
69 if (s) {
70 q->connect (*i);
71 } else {
72 q->disconnect (*i);
74 } else {
75 /* two non-Ardour ports */
76 if (s) {
77 jack_connect (_session->engine().jack (), j->c_str(), i->c_str());
78 } else {
79 jack_disconnect (_session->engine().jack (), j->c_str(), i->c_str());
86 PortMatrixNode::State
87 GlobalPortMatrix::get_state (BundleChannel c[2]) const
89 if (_session == 0) {
90 return PortMatrixNode::NOT_ASSOCIATED;
93 if (c[0].bundle->nchannels() == ChanCount::ZERO || c[1].bundle->nchannels() == ChanCount::ZERO) {
94 return PortMatrixNode::NOT_ASSOCIATED;
97 Bundle::PortList const & in_ports = c[IN].bundle->channel_ports (c[IN].channel);
98 Bundle::PortList const & out_ports = c[OUT].bundle->channel_ports (c[OUT].channel);
99 if (in_ports.empty() || out_ports.empty()) {
100 /* we're looking at a bundle with no parts associated with this channel,
101 so nothing to connect */
102 return PortMatrixNode::NOT_ASSOCIATED;
105 for (Bundle::PortList::const_iterator i = in_ports.begin(); i != in_ports.end(); ++i) {
106 for (Bundle::PortList::const_iterator j = out_ports.begin(); j != out_ports.end(); ++j) {
108 Port* p = _session->engine().get_port_by_name (*i);
109 Port* q = _session->engine().get_port_by_name (*j);
111 if (!p && !q) {
112 /* two non-Ardour ports; things are slightly more involved */
113 /* XXX: is this the easiest way to do this? */
114 /* XXX: isn't this very inefficient? */
116 jack_client_t* jack = _session->engine().jack ();
117 jack_port_t* jp = jack_port_by_name (jack, i->c_str());
118 if (jp == 0) {
119 return PortMatrixNode::NOT_ASSOCIATED;
122 char const ** c = jack_port_get_all_connections (jack, jp);
124 char const ** p = c;
126 while (p && *p != 0) {
127 if (strcmp (*p, j->c_str()) == 0) {
128 free (c);
129 return PortMatrixNode::ASSOCIATED;
131 ++p;
134 free (c);
135 return PortMatrixNode::NOT_ASSOCIATED;
138 if (p && p->connected_to (*j) == false) {
139 return PortMatrixNode::NOT_ASSOCIATED;
140 } else if (q && q->connected_to (*i) == false) {
141 return PortMatrixNode::NOT_ASSOCIATED;
147 return PortMatrixNode::ASSOCIATED;
150 GlobalPortMatrixWindow::GlobalPortMatrixWindow (Session* s, DataType t)
151 : ArdourDialog (X_("reset me soon"))
152 , _port_matrix (this, s, t)
154 switch (t) {
155 case DataType::AUDIO:
156 set_title (_("Audio Connection Manager"));
157 break;
158 case DataType::MIDI:
159 set_title (_("MIDI Connection Manager"));
160 break;
163 get_vbox()->pack_start (_port_matrix, true, true);
164 _port_matrix.show ();
167 void
168 GlobalPortMatrixWindow::on_show ()
170 Gtk::Window::on_show ();
171 pair<uint32_t, uint32_t> const pm_max = _port_matrix.max_size ();
172 resize_window_to_proportion_of_monitor (this, pm_max.first, pm_max.second);
175 void
176 GlobalPortMatrixWindow::set_session (Session* s)
178 _port_matrix.set_session (s);
181 string
182 GlobalPortMatrix::disassociation_verb () const
184 return _("Disconnect");
187 string
188 GlobalPortMatrix::channel_noun () const
190 return _("port");