a84b5ab68a759d60640d519ea531bf285fed8458
[ardour.git] / libs / midi++2 / manager.cc
1 /*
2     Copyright (C) 1998-99 Paul Barton-Davis 
3     This program is free software; you can redistribute it and/or modify
4     it under the terms of the GNU General Public License as published by
5     the Free Software Foundation; either version 2 of the License, or
6     (at your option) any later version.
7
8     This program is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11     GNU General Public License for more details.
12
13     You should have received a copy of the GNU General Public License
14     along with this program; if not, write to the Free Software
15     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
17     $Id$
18 */
19
20 #include <fcntl.h>
21
22 #include <glib.h>
23
24 #include "pbd/error.h"
25
26 #include "midi++/types.h"
27 #include "midi++/manager.h"
28 #include "midi++/factory.h"
29 #include "midi++/channel.h"
30
31 using namespace std;
32 using namespace MIDI;
33 using namespace PBD;
34
35 /* XXX check for strdup leaks */
36
37 Manager *Manager::theManager = 0;
38
39 Manager::Manager () 
40 {
41         inputPort = 0;
42         outputPort = 0;
43         inputChannelNumber = 0;
44         outputChannelNumber = 0;
45         api_data = 0;
46 }
47
48 Manager::~Manager ()
49 {
50         for (PortList::iterator p = _ports.begin(); p != _ports.end(); ++p) {
51                 delete *p;
52         }
53
54         if (theManager == this) {
55                 theManager = 0;
56         }
57 }
58
59 Port *
60 Manager::add_port (const XMLNode& node)
61 {
62         Port::Descriptor desc (node);
63         PortFactory factory;
64         Port *port;
65         PortList::iterator p;
66
67         for (p = _ports.begin(); p != _ports.end(); ++p) {
68
69                 if (desc.tag == (*p)->name()) {
70                         break;
71                 } 
72
73                 if (!PortFactory::ignore_duplicate_devices (desc.type)) {
74                         if (desc.device == (*p)->device()) {
75                                 /* If the existing is duplex, and this request
76                                    is not, then fail, because most drivers won't
77                                    allow opening twice with duplex and non-duplex
78                                    operation.
79                                 */
80
81                                 if ((desc.mode == O_RDWR && (*p)->mode() != O_RDWR) ||
82                                     (desc.mode != O_RDWR && (*p)->mode() == O_RDWR)) {
83                                         break;
84                                 }
85                         }
86                 }
87         }
88
89         if (p != _ports.end()) {
90                 return 0;
91         }
92         
93         port = factory.create_port (node, api_data);
94         
95         if (port == 0) {
96                 return 0;
97         }
98
99         if (!port->ok()) {
100                 delete port;
101                 return 0;
102         }
103
104         _ports.push_back (port);
105
106         /* first port added becomes the default input
107            port.
108         */
109
110         if (inputPort == 0) {
111                 inputPort = port;
112         } 
113
114         if (outputPort == 0) {
115                 outputPort = port;
116         }
117
118         PortsChanged (); /* EMIT SIGNAL */
119
120         return port;
121 }
122
123 int 
124 Manager::remove_port (Port* port)
125 {
126         if (inputPort == port) {
127                 inputPort = 0;
128         }
129
130         if (outputPort == port) {
131                 outputPort = 0;
132         }
133
134         _ports.remove (port);
135         delete port;
136
137         PortsChanged (); /* EMIT SIGNAL */
138
139         return 0;
140 }
141
142 int
143 Manager::set_input_port (string tag)
144 {
145         for (PortList::iterator p = _ports.begin(); p != _ports.end(); ++p) {
146                 if ((*p)->name() == tag) {
147                         inputPort = (*p);
148                         return 0;
149                 }
150         }
151
152         return -1;
153 }
154
155 int
156 Manager::set_output_port (string tag)
157 {
158         PortList::iterator p;
159
160         for (p = _ports.begin(); p != _ports.end(); ++p) {
161                 if ((*p)->name() == tag) {
162                         inputPort = (*p);
163                         break;
164                 }
165         }
166
167         if (p == _ports.end()) {
168                 return -1;
169         }
170
171         // XXX send a signal to say we're about to change output ports
172
173         if (outputPort) {
174                 for (channel_t chan = 0; chan < 16; chan++) {
175                         outputPort->channel (chan)->all_notes_off (0);
176                 }
177         }
178         
179         outputPort = (*p);
180
181         // XXX send a signal to say we've changed output ports
182
183         return 0;
184 }
185
186 Port *
187 Manager::port (string name)
188 {
189         for (PortList::iterator p = _ports.begin(); p != _ports.end(); ++p) {
190                 if (name == (*p)->name()) {
191                         return (*p);
192                 }
193         }
194
195         return 0;
196 }
197
198 int
199 Manager::foreach_port (int (*func)(const Port &, size_t, void *),
200                            void *arg)
201 {
202         int n = 0;
203                 
204         for (PortList::const_iterator p = _ports.begin(); p != _ports.end(); ++p, ++n) {
205                 int retval;
206
207                 if ((retval = func (**p, n, arg)) != 0) {
208                         return retval;
209                 }
210         }
211
212         return 0;
213 }
214
215 void
216 Manager::cycle_start(nframes_t nframes)
217 {
218         for (PortList::iterator p = _ports.begin(); p != _ports.end(); ++p) {
219                 (*p)->cycle_start (nframes);
220         }
221 }
222
223 void
224 Manager::cycle_end()
225 {
226         for (PortList::iterator p = _ports.begin(); p != _ports.end(); ++p) {
227                 (*p)->cycle_end ();
228         }
229 }
230
231
232 int
233 Manager::get_known_ports (vector<PortSet>& ports)
234 {
235         return PortFactory::get_known_ports (ports);
236 }
237
238 /** Re-register ports that disappear on JACK shutdown */
239 void
240 Manager::reestablish (void* a)
241 {
242         for (PortList::const_iterator p = _ports.begin(); p != _ports.end(); ++p) {
243                 (*p)->reestablish (a);
244         }
245 }
246
247 /** Re-connect ports after a reestablish () */
248 void
249 Manager::reconnect ()
250 {
251         for (PortList::const_iterator p = _ports.begin(); p != _ports.end(); ++p) {
252                 (*p)->reconnect ();
253         }
254 }