5c464fa060a6c2142a1259155015430ee72869c7
[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++/channel.h"
29 #include "midi++/port.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         Port *port;
64         PortList::iterator p;
65
66         for (p = _ports.begin(); p != _ports.end(); ++p) {
67
68                 if (desc.tag == (*p)->name()) {
69                         break;
70                 }
71                 
72         }
73
74         if (p != _ports.end()) {
75                 return 0;
76         }
77         
78         port = new Port (node, (jack_client_t *) api_data);
79         
80         if (port == 0) {
81                 return 0;
82         }
83
84         if (!port->ok()) {
85                 delete port;
86                 return 0;
87         }
88
89         _ports.push_back (port);
90
91         /* first port added becomes the default input
92            port.
93         */
94
95         if (inputPort == 0) {
96                 inputPort = port;
97         } 
98
99         if (outputPort == 0) {
100                 outputPort = port;
101         }
102
103         PortsChanged (); /* EMIT SIGNAL */
104
105         return port;
106 }
107
108 int 
109 Manager::remove_port (Port* port)
110 {
111         if (inputPort == port) {
112                 inputPort = 0;
113         }
114
115         if (outputPort == port) {
116                 outputPort = 0;
117         }
118
119         _ports.remove (port);
120         delete port;
121
122         PortsChanged (); /* EMIT SIGNAL */
123
124         return 0;
125 }
126
127 int
128 Manager::set_input_port (string tag)
129 {
130         for (PortList::iterator p = _ports.begin(); p != _ports.end(); ++p) {
131                 if ((*p)->name() == tag) {
132                         inputPort = (*p);
133                         return 0;
134                 }
135         }
136
137         return -1;
138 }
139
140 int
141 Manager::set_output_port (string tag)
142 {
143         PortList::iterator p;
144
145         for (p = _ports.begin(); p != _ports.end(); ++p) {
146                 if ((*p)->name() == tag) {
147                         inputPort = (*p);
148                         break;
149                 }
150         }
151
152         if (p == _ports.end()) {
153                 return -1;
154         }
155
156         // XXX send a signal to say we're about to change output ports
157
158         if (outputPort) {
159                 for (channel_t chan = 0; chan < 16; chan++) {
160                         outputPort->channel (chan)->all_notes_off (0);
161                 }
162         }
163         
164         outputPort = (*p);
165
166         // XXX send a signal to say we've changed output ports
167
168         return 0;
169 }
170
171 Port *
172 Manager::port (string name)
173 {
174         for (PortList::iterator p = _ports.begin(); p != _ports.end(); ++p) {
175                 if (name == (*p)->name()) {
176                         return (*p);
177                 }
178         }
179
180         return 0;
181 }
182
183 int
184 Manager::foreach_port (int (*func)(const Port &, size_t, void *),
185                            void *arg)
186 {
187         int n = 0;
188                 
189         for (PortList::const_iterator p = _ports.begin(); p != _ports.end(); ++p, ++n) {
190                 int retval;
191
192                 if ((retval = func (**p, n, arg)) != 0) {
193                         return retval;
194                 }
195         }
196
197         return 0;
198 }
199
200 void
201 Manager::cycle_start(nframes_t nframes)
202 {
203         for (PortList::iterator p = _ports.begin(); p != _ports.end(); ++p) {
204                 (*p)->cycle_start (nframes);
205         }
206 }
207
208 void
209 Manager::cycle_end()
210 {
211         for (PortList::iterator p = _ports.begin(); p != _ports.end(); ++p) {
212                 (*p)->cycle_end ();
213         }
214 }
215
216 /** Re-register ports that disappear on JACK shutdown */
217 void
218 Manager::reestablish (void* a)
219 {
220         for (PortList::const_iterator p = _ports.begin(); p != _ports.end(); ++p) {
221                 (*p)->reestablish (a);
222         }
223 }
224
225 /** Re-connect ports after a reestablish () */
226 void
227 Manager::reconnect ()
228 {
229         for (PortList::const_iterator p = _ports.begin(); p != _ports.end(); ++p) {
230                 (*p)->reconnect ();
231         }
232 }