use new syntax for connecting to backend signals that enforces explicit connection...
[ardour.git] / libs / midi++2 / factory.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 <cassert>
21 #include <stdint.h>
22
23 #include "pbd/error.h"
24 #include "pbd/convert.h"
25
26 #include "midi++/types.h"
27 #include "midi++/factory.h"
28 #include "midi++/fifomidi.h"
29
30 #ifdef WITH_JACK_MIDI
31 #include "midi++/jack.h"
32
33 std::string MIDI::JACK_MidiPort::typestring = "jack";
34 #endif // WITH_JACK_MIDI
35
36 std::string MIDI::FIFO_MidiPort::typestring = "fifo";
37
38 #ifdef WITH_ALSA
39 #include "midi++/alsa_sequencer.h"
40 #include "midi++/alsa_rawmidi.h"
41
42 std::string MIDI::ALSA_SequencerMidiPort::typestring = "alsa/sequencer";
43 std::string MIDI::ALSA_RawMidiPort::typestring = "alsa/raw";
44
45 #endif // WITH_ALSA
46
47 #ifdef WITH_COREMIDI
48 #include "midi++/coremidi_midiport.h"
49
50 std::string MIDI::CoreMidi_MidiPort::typestring = "coremidi";
51
52 #endif // WITH_COREMIDI
53
54 using namespace std;
55 using namespace MIDI;
56 using namespace PBD;
57
58 // FIXME: void* data pointer, filthy
59 Port *
60 PortFactory::create_port (const XMLNode& node, void* data)
61
62 {
63         Port::Descriptor desc (node);
64         Port *port;
65         
66         switch (desc.type) {
67 #ifdef WITH_JACK_MIDI
68         case Port::JACK_Midi:
69                 assert(data != NULL);
70                 port = new JACK_MidiPort (node, (jack_client_t*) data);
71                 break;
72 #endif // WITH_JACK_MIDI
73         
74 #ifdef WITH_ALSA
75         case Port::ALSA_RawMidi:
76                 port = new ALSA_RawMidiPort (node);
77                 break;
78
79         case Port::ALSA_Sequencer:
80                 port = new ALSA_SequencerMidiPort (node);
81                 break;
82 #endif // WITH_ALSA
83
84 #ifdef WITH_COREMIDI
85         case Port::CoreMidi_MidiPort:
86                 port = new CoreMidi_MidiPort (node);
87                 break;
88 #endif // WITH_COREMIDI
89
90         case Port::FIFO:
91                 port = new FIFO_MidiPort (node);
92                 break;
93
94         default:
95                 return 0;
96         }
97
98         return port;
99 }
100
101 bool 
102 PortFactory::ignore_duplicate_devices (Port::Type type)
103 {
104         bool ret = false;
105
106         switch (type) {
107 #ifdef WITH_ALSA
108         case Port::ALSA_Sequencer:
109                 ret = true;
110                 break;
111 #endif // WITH_ALSA
112
113 #ifdef WITH_JACK_MIDI
114         case Port::JACK_Midi:
115                 ret = true;
116                 break;
117 #endif // WITH_JACK_MIDI
118
119 #ifdef WITH_COREMIDI
120         case Port::CoreMidi_MidiPort:
121                 ret = true;
122                 break;
123 #endif // WITH_COREMIDI
124
125         default:
126                 break;
127         }
128
129         return ret;
130 }
131
132 int
133 #if defined (WITH_ALSA) || defined (WITH_COREMIDI)
134 PortFactory::get_known_ports (vector<PortSet>& ports)
135 #else
136 PortFactory::get_known_ports (vector<PortSet>&)
137 #endif  
138 {
139         int n = 0;
140 #ifdef WITH_ALSA
141         n += ALSA_SequencerMidiPort::discover (ports);
142 #endif // WITH_ALSA
143
144 #ifdef WITH_COREMIDI
145         n += CoreMidi_MidiPort::discover (ports);
146 #endif // WITH_COREMIDI
147         
148         return n;
149 }
150
151 std::string
152 PortFactory::default_port_type ()
153 {
154 #ifdef WITH_JACK_MIDI
155         return "jack";
156 #endif
157
158 #ifdef WITH_ALSA
159         return "alsa/sequencer";
160 #endif
161
162 #ifdef WITH_COREMIDI
163         return "coremidi";
164 #endif // WITH_COREMIDI
165         
166         PBD::fatal << "programming error: no default port type defined in midifactory.cc" << endmsg;
167         /*NOTREACHED*/
168         return "";
169 }
170
171 Port::Type
172 PortFactory::string_to_type (const string& xtype)
173 {
174         if (0){ 
175 #ifdef WITH_ALSA
176         } else if (strings_equal_ignore_case (xtype, ALSA_RawMidiPort::typestring)) {
177                 return Port::ALSA_RawMidi;
178         } else if (strings_equal_ignore_case (xtype, ALSA_SequencerMidiPort::typestring)) {
179                 return Port::ALSA_Sequencer;
180 #endif 
181 #ifdef WITH_COREMIDI
182         } else if (strings_equal_ignore_case (xtype, CoreMidi_MidiPort::typestring)) {
183                 return Port::CoreMidi_MidiPort;
184 #endif
185         } else if (strings_equal_ignore_case (xtype, FIFO_MidiPort::typestring)) {
186                 return Port::FIFO;
187 #ifdef WITH_JACK_MIDI
188         } else if (strings_equal_ignore_case (xtype, JACK_MidiPort::typestring)) {
189                 return Port::JACK_Midi;
190 #endif
191         }
192
193         return Port::Unknown;
194 }
195
196 string
197 PortFactory::mode_to_string (int mode)
198 {
199         if (mode == O_RDONLY) {
200                 return "input";
201         } else if (mode == O_WRONLY) {
202                 return "output";
203         } 
204
205         return "duplex";
206 }
207
208 int
209 PortFactory::string_to_mode (const string& str)
210 {
211         if (strings_equal_ignore_case (str, "output") || strings_equal_ignore_case (str, "out")) {
212                 return O_WRONLY;
213         } else if (strings_equal_ignore_case (str, "input") || strings_equal_ignore_case (str, "in")) {
214                 return O_RDONLY;
215         }
216
217         return O_RDWR;
218 }