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