VKeybd: Pass on primary (Ctrl/Cmd) shortcuts
[ardour.git] / gtk2_ardour / global_port_matrix.cc
1 /*
2  * Copyright (C) 2009-2011 David Robillard <d@drobilla.net>
3  * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
4  * Copyright (C) 2010-2016 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2013-2014 Robin Gareus <robin@gareus.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <gtkmm/image.h>
23 #include <gtkmm/stock.h>
24
25 #include "global_port_matrix.h"
26 #include "utils.h"
27
28 #include "ardour/bundle.h"
29 #include "ardour/session.h"
30 #include "ardour/audioengine.h"
31 #include "ardour/port.h"
32
33 #include "pbd/i18n.h"
34
35 using namespace std;
36 using namespace ARDOUR;
37 using namespace ARDOUR_UI_UTILS;
38
39 GlobalPortMatrix::GlobalPortMatrix (Gtk::Window* p, Session* s, DataType t)
40         : PortMatrix (p, s, t)
41 {
42         setup_all_ports ();
43         init ();
44 }
45
46 void
47 GlobalPortMatrix::setup_ports (int dim)
48 {
49         if (!_session) {
50                 return;
51         }
52
53         _ports[dim].suspend_signals ();
54         _ports[dim].gather (_session, type(), dim == FLOW_IN, false, show_only_bundles ());
55         _ports[dim].resume_signals ();
56 }
57
58 void
59 GlobalPortMatrix::set_state (BundleChannel c[2], bool s)
60 {
61         if (!_session) {
62                 return;
63         }
64
65         Bundle::PortList const & in_ports = c[FLOW_IN].bundle->channel_ports (c[FLOW_IN].channel);
66         Bundle::PortList const & out_ports = c[FLOW_OUT].bundle->channel_ports (c[FLOW_OUT].channel);
67
68         for (Bundle::PortList::const_iterator i = in_ports.begin(); i != in_ports.end(); ++i) {
69                 for (Bundle::PortList::const_iterator j = out_ports.begin(); j != out_ports.end(); ++j) {
70
71                         boost::shared_ptr<Port> p = _session->engine().get_port_by_name (*i);
72                         boost::shared_ptr<Port> q = _session->engine().get_port_by_name (*j);
73
74                         if (p) {
75                                 if (s) {
76                                         p->connect (*j);
77                                 } else {
78                                         p->disconnect (*j);
79                                 }
80                         } else if (q) {
81                                 if (s) {
82                                         q->connect (*i);
83                                 } else {
84                                         q->disconnect (*i);
85                                 }
86                         } else {
87                                 /* two non-Ardour ports */
88                                 if (s) {
89                                         AudioEngine::instance()->connect (*j, *i);
90                                 } else {
91                                         AudioEngine::instance()->disconnect (*j, *i);
92                                 }
93                         }
94                 }
95         }
96 }
97
98 PortMatrixNode::State
99 GlobalPortMatrix::get_state (BundleChannel c[2]) const
100 {
101         if (_session == 0) {
102                 return PortMatrixNode::NOT_ASSOCIATED;
103         }
104
105         if (c[0].bundle->nchannels() == ChanCount::ZERO || c[1].bundle->nchannels() == ChanCount::ZERO) {
106                 return PortMatrixNode::NOT_ASSOCIATED;
107         }
108
109         Bundle::PortList const & in_ports = c[FLOW_IN].bundle->channel_ports (c[FLOW_IN].channel);
110         Bundle::PortList const & out_ports = c[FLOW_OUT].bundle->channel_ports (c[FLOW_OUT].channel);
111         if (in_ports.empty() || out_ports.empty()) {
112                 /* we're looking at a bundle with no parts associated with this channel,
113                    so nothing to connect */
114                 return PortMatrixNode::NOT_ASSOCIATED;
115         }
116
117         for (Bundle::PortList::const_iterator i = in_ports.begin(); i != in_ports.end(); ++i) {
118                 for (Bundle::PortList::const_iterator j = out_ports.begin(); j != out_ports.end(); ++j) {
119
120                         boost::shared_ptr<Port> p = AudioEngine::instance()->get_port_by_name (*i);
121                         boost::shared_ptr<Port> q = AudioEngine::instance()->get_port_by_name (*j);
122
123                         if (!p && !q) {
124                                 /* two non-Ardour ports; things are slightly more involved */
125
126                                 /* get a port handle for one of them .. */
127
128                                 PortEngine::PortHandle ph = AudioEngine::instance()->port_engine().get_port_by_name (*i);
129                                 if (!ph) {
130                                         return PortMatrixNode::NOT_ASSOCIATED;
131                                 }
132
133                                 /* see if it is connected to the other one ... */
134
135                                 if (AudioEngine::instance()->port_engine().connected_to (ph, *j, false)) {
136                                         return PortMatrixNode::ASSOCIATED;
137                                 }
138
139                                 return PortMatrixNode::NOT_ASSOCIATED;
140                         }
141
142                         if (p && p->connected_to (*j) == false) {
143                                 return PortMatrixNode::NOT_ASSOCIATED;
144                         } else if (q && q->connected_to (*i) == false) {
145                                 return PortMatrixNode::NOT_ASSOCIATED;
146                         }
147
148                 }
149         }
150
151         return PortMatrixNode::ASSOCIATED;
152 }
153
154 GlobalPortMatrixWindow::GlobalPortMatrixWindow (Session* s, DataType t)
155         : ArdourWindow (X_("reset me soon"))
156         , _port_matrix (this, s, t)
157 {
158         switch (t) {
159         case DataType::AUDIO:
160                 set_title (_("Audio Connection Manager"));
161                 break;
162         case DataType::MIDI:
163                 set_title (_("MIDI Connection Manager"));
164                 break;
165         }
166
167         signal_key_press_event().connect (sigc::mem_fun (_port_matrix, &PortMatrix::key_press));
168
169         add (_port_matrix);
170         _port_matrix.show ();
171 }
172
173 void
174 GlobalPortMatrixWindow::on_show ()
175 {
176         Gtk::Window::on_show ();
177         pair<uint32_t, uint32_t> const pm_max = _port_matrix.max_size ();
178         resize_window_to_proportion_of_monitor (this, pm_max.first, pm_max.second);
179 }
180
181 void
182 GlobalPortMatrixWindow::set_session (Session* s)
183 {
184         _port_matrix.set_session (s);
185
186         if (!s) {
187                 hide ();
188         }
189 }
190
191 void
192 GlobalPortMatrix::set_session (Session *s)
193 {
194         SessionHandlePtr::set_session (s);
195         if (!s) return;
196         setup_all_ports ();
197         init();
198 }
199
200 string
201 GlobalPortMatrix::disassociation_verb () const
202 {
203         return _("Disconnect");
204 }
205
206 string
207 GlobalPortMatrix::channel_noun () const
208 {
209         return _("port");
210 }
211