take process lock in global port matrix' set_state() method; use namespace ARDOUR too
[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         _ports[dim].suspend_signals ();
46         _ports[dim].gather (_session, type(), dim == IN, false);
47         _ports[dim].resume_signals ();
48 }
49
50 void
51 GlobalPortMatrix::set_state (BundleChannel c[2], bool s)
52 {
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);
55
56         Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock());
57
58         for (Bundle::PortList::const_iterator i = in_ports.begin(); i != in_ports.end(); ++i) {
59                 for (Bundle::PortList::const_iterator j = out_ports.begin(); j != out_ports.end(); ++j) {
60
61                         Port* p = _session->engine().get_port_by_name_locked (*i);
62                         Port* q = _session->engine().get_port_by_name_locked (*j);
63
64                         if (p) {
65                                 if (s) {
66                                         p->connect (*j);
67                                 } else {
68                                         p->disconnect (*j);
69                                 }
70                         } else if (q) {
71                                 if (s) {
72                                         q->connect (*i);
73                                 } else {
74                                         q->disconnect (*i);
75                                 }
76                         } else {
77                                 /* two non-Ardour ports */
78                                 if (s) {
79                                         jack_connect (_session->engine().jack (), j->c_str(), i->c_str());
80                                 } else {
81                                         jack_disconnect (_session->engine().jack (), j->c_str(), i->c_str());
82                                 }
83                         }
84                 }
85         }
86 }
87
88 PortMatrixNode::State
89 GlobalPortMatrix::get_state (BundleChannel c[2]) const
90 {
91         if (_session == 0) {
92                 return PortMatrixNode::NOT_ASSOCIATED;
93         }
94         
95         Bundle::PortList const & in_ports = c[IN].bundle->channel_ports (c[IN].channel);
96         Bundle::PortList const & out_ports = c[OUT].bundle->channel_ports (c[OUT].channel);
97         if (in_ports.empty() || out_ports.empty()) {
98                 /* we're looking at a bundle with no parts associated with this channel,
99                    so nothing to connect */
100                 return PortMatrixNode::NOT_ASSOCIATED;
101         }
102
103         for (Bundle::PortList::const_iterator i = in_ports.begin(); i != in_ports.end(); ++i) {
104                 for (Bundle::PortList::const_iterator j = out_ports.begin(); j != out_ports.end(); ++j) {
105
106                         Port* p = _session->engine().get_port_by_name (*i);
107                         Port* q = _session->engine().get_port_by_name (*j);
108
109                         if (!p && !q) {
110                                 /* two non-Ardour ports; things are slightly more involved */
111                                 /* XXX: is this the easiest way to do this? */
112                                 /* XXX: isn't this very inefficient? */
113
114                                 jack_client_t* jack = _session->engine().jack ();
115                                 jack_port_t* jp = jack_port_by_name (jack, i->c_str());
116                                 if (jp == 0) {
117                                         return PortMatrixNode::NOT_ASSOCIATED;
118                                 }
119                                 
120                                 char const ** c = jack_port_get_all_connections (jack, jp);
121
122                                 char const ** p = c;
123                                 
124                                 while (p && *p != 0) {
125                                         if (strcmp (*p, j->c_str()) == 0) {
126                                                 free (c);
127                                                 return PortMatrixNode::ASSOCIATED;
128                                         }
129                                         ++p;
130                                 }
131
132                                 free (c);
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         : _port_matrix (this, s, t)
150 {
151         switch (t) {
152         case DataType::AUDIO:
153                 set_title (_("Audio Connection Manager"));
154                 break;
155         case DataType::MIDI:
156                 set_title (_("MIDI Connection Manager"));
157                 break;
158         }
159
160         add (_port_matrix);
161         _port_matrix.show ();
162 }
163
164 void
165 GlobalPortMatrixWindow::on_show ()
166 {
167         Gtk::Window::on_show ();
168         pair<uint32_t, uint32_t> const pm_max = _port_matrix.max_size ();
169         resize_window_to_proportion_of_monitor (this, pm_max.first, pm_max.second);
170 }
171
172 void
173 GlobalPortMatrixWindow::set_session (Session* s)
174 {
175         _port_matrix.set_session (s);
176 }
177
178 string
179 GlobalPortMatrix::disassociation_verb () const
180 {
181         return _("Disconnect");
182 }
183
184 string
185 GlobalPortMatrix::channel_noun () const
186 {
187         return _("port");
188 }
189