fix conflicts after merge with master
[ardour.git] / gtk2_ardour / global_port_matrix.cc
1 /*
2     Copyright (C) 2009 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <gtkmm/image.h>
21 #include <gtkmm/stock.h>
22 #include "global_port_matrix.h"
23 #include "utils.h"
24
25 #include "ardour/bundle.h"
26 #include "ardour/session.h"
27 #include "ardour/audioengine.h"
28 #include "ardour/port.h"
29
30 #include "i18n.h"
31
32 using namespace std;
33 using namespace ARDOUR;
34
35 GlobalPortMatrix::GlobalPortMatrix (Gtk::Window* p, Session* s, DataType t)
36         : PortMatrix (p, s, t)
37 {
38         setup_all_ports ();
39         init ();
40 }
41
42 void
43 GlobalPortMatrix::setup_ports (int dim)
44 {
45         if (!_session) {
46                 return;
47         }
48
49         _ports[dim].suspend_signals ();
50         _ports[dim].gather (_session, type(), dim == IN, false, show_only_bundles ());
51         _ports[dim].resume_signals ();
52 }
53
54 void
55 GlobalPortMatrix::set_state (BundleChannel c[2], bool s)
56 {
57         if (!_session) {
58                 return;
59         }
60
61         Bundle::PortList const & in_ports = c[IN].bundle->channel_ports (c[IN].channel);
62         Bundle::PortList const & out_ports = c[OUT].bundle->channel_ports (c[OUT].channel);
63
64         for (Bundle::PortList::const_iterator i = in_ports.begin(); i != in_ports.end(); ++i) {
65                 for (Bundle::PortList::const_iterator j = out_ports.begin(); j != out_ports.end(); ++j) {
66
67                         boost::shared_ptr<Port> p = _session->engine().get_port_by_name (*i);
68                         boost::shared_ptr<Port> q = _session->engine().get_port_by_name (*j);
69
70                         if (p) {
71                                 if (s) {
72                                         p->connect (*j);
73                                 } else {
74                                         p->disconnect (*j);
75                                 }
76                         } else if (q) {
77                                 if (s) {
78                                         q->connect (*i);
79                                 } else {
80                                         q->disconnect (*i);
81                                 }
82                         } else {
83                                 /* two non-Ardour ports */
84                                 if (s) {
85                                         AudioEngine::instance()->connect (*j, *i);
86                                 } else {
87                                         AudioEngine::instance()->disconnect (*j, *i);
88                                 }
89                         }
90                 }
91         }
92 }
93
94 PortMatrixNode::State
95 GlobalPortMatrix::get_state (BundleChannel c[2]) const
96 {
97         if (_session == 0) {
98                 return PortMatrixNode::NOT_ASSOCIATED;
99         }
100
101         if (c[0].bundle->nchannels() == ChanCount::ZERO || c[1].bundle->nchannels() == ChanCount::ZERO) {
102                 return PortMatrixNode::NOT_ASSOCIATED;
103         }
104
105         Bundle::PortList const & in_ports = c[IN].bundle->channel_ports (c[IN].channel);
106         Bundle::PortList const & out_ports = c[OUT].bundle->channel_ports (c[OUT].channel);
107         if (in_ports.empty() || out_ports.empty()) {
108                 /* we're looking at a bundle with no parts associated with this channel,
109                    so nothing to connect */
110                 return PortMatrixNode::NOT_ASSOCIATED;
111         }
112
113         for (Bundle::PortList::const_iterator i = in_ports.begin(); i != in_ports.end(); ++i) {
114                 for (Bundle::PortList::const_iterator j = out_ports.begin(); j != out_ports.end(); ++j) {
115
116                         boost::shared_ptr<Port> p = AudioEngine::instance()->get_port_by_name (*i);
117                         boost::shared_ptr<Port> q = AudioEngine::instance()->get_port_by_name (*j);
118
119                         if (!p && !q) {
120                                 /* two non-Ardour ports; things are slightly more involved */
121                                 /* XXX: is this the easiest way to do this? */
122                                 /* XXX: isn't this very inefficient? */
123
124                                 PortEngine::PortHandle ph = AudioEngine::instance()->port_engine().get_port_by_name (*i);
125                                 if (!ph) {
126                                         return PortMatrixNode::NOT_ASSOCIATED;
127                                 }
128
129                                 if (AudioEngine::instance()->port_engine().connected (ph)) {
130                                         return PortMatrixNode::ASSOCIATED;
131                                 }
132
133                                 return PortMatrixNode::NOT_ASSOCIATED;
134                         }
135
136                         if (p && p->connected_to (*j) == false) {
137                                 return PortMatrixNode::NOT_ASSOCIATED;
138                         } else if (q && q->connected_to (*i) == false) {
139                                 return PortMatrixNode::NOT_ASSOCIATED;
140                         }
141
142                 }
143         }
144
145         return PortMatrixNode::ASSOCIATED;
146 }
147
148 GlobalPortMatrixWindow::GlobalPortMatrixWindow (Session* s, DataType t)
149         : ArdourWindow (X_("reset me soon"))
150         , _port_matrix (this, s, t)
151 {
152         switch (t) {
153         case DataType::AUDIO:
154                 set_title (_("Audio Connection Manager"));
155                 break;
156         case DataType::MIDI:
157                 set_title (_("MIDI Connection Manager"));
158                 break;
159         }
160
161         signal_key_press_event().connect (sigc::mem_fun (_port_matrix, &PortMatrix::key_press));
162
163         add (_port_matrix);
164         _port_matrix.show ();
165 }
166
167 void
168 GlobalPortMatrixWindow::on_show ()
169 {
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);
173 }
174
175 void
176 GlobalPortMatrixWindow::set_session (Session* s)
177 {
178         _port_matrix.set_session (s);
179
180         if (!s) {
181                 hide ();
182         }
183 }
184
185 void
186 GlobalPortMatrix::set_session (Session *s)
187 {
188         SessionHandlePtr::set_session (s);
189         if (!s) return;
190         setup_all_ports ();
191         init();
192 }
193
194 string
195 GlobalPortMatrix::disassociation_verb () const
196 {
197         return _("Disconnect");
198 }
199
200 string
201 GlobalPortMatrix::channel_noun () const
202 {
203         return _("port");
204 }
205