add solo isolate indicator in editor route list
[ardour.git] / libs / midi++2 / port.cc
1 /*
2     Copyright (C) 1998 Paul Barton-Davis
3     
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id$
19 */
20 #include <iostream>
21 #include <cstdio>
22 #include <fcntl.h>
23 #include <errno.h>
24
25 #include "pbd/xml++.h"
26 #include "pbd/error.h"
27 #include "pbd/failed_constructor.h"
28
29 #include "midi++/types.h"
30 #include "midi++/port.h"
31 #include "midi++/channel.h"
32 #include "midi++/factory.h"
33
34 using namespace MIDI;
35 using namespace std;
36 using namespace PBD;
37
38 size_t Port::nports = 0;
39
40 Port::Port (const XMLNode& node)
41         : _currently_in_cycle(false)
42         , _nframes_this_cycle(0)
43 {
44         Descriptor desc (node);
45
46         _ok = false;  /* derived class must set to true if constructor
47                          succeeds.
48                       */
49
50         bytes_written = 0;
51         bytes_read = 0;
52         input_parser = 0;
53         output_parser = 0;
54         slowdown = 0;
55
56         _devname = desc.device;
57         _tagname = desc.tag;
58         _mode = desc.mode;
59
60         if (_mode == O_RDONLY || _mode == O_RDWR) {
61                 input_parser = new Parser (*this);
62         } else {
63                 input_parser = 0;
64         }
65
66         if (_mode == O_WRONLY || _mode == O_RDWR) {
67                 output_parser = new Parser (*this);
68         } else {
69                 output_parser = 0;
70         }
71
72         for (int i = 0; i < 16; i++) {
73                 _channel[i] =  new Channel (i, *this);
74
75                 if (input_parser) {
76                         _channel[i]->connect_input_signals ();
77                 }
78
79                 if (output_parser) {
80                         _channel[i]->connect_output_signals ();
81                 }
82         }
83 }
84
85
86 Port::~Port ()
87 {
88         for (int i = 0; i < 16; i++) {
89                 delete _channel[i];
90         }
91 }
92
93 void
94 Port::parse (nframes_t timestamp)
95 {
96         byte buf[512];
97
98         /* NOTE: parsing is done (if at all) by initiating a read from 
99            the port. Each port implementation calls on the parser
100            once it has data ready.
101         */
102         
103         if (input_parser) {
104                 input_parser->set_timestamp (timestamp);
105         }
106
107         while (1) {
108                 
109                 // cerr << "+++ READ ON " << name() << endl;
110
111                 int nread = read (buf, sizeof (buf));
112
113                 // cerr << "-- READ (" << nread << " ON " << name() << endl;
114                 
115                 if (nread > 0) {
116                         if ((size_t) nread < sizeof (buf)) {
117                                 break;
118                         } else {
119                                 continue;
120                         }
121                 } else if (nread == 0) {
122                         break;
123                 } else if (errno == EAGAIN) {
124                         break;
125                 } else {
126                         fatal << "Error reading from MIDI port " << name() << endmsg;
127                         /*NOTREACHED*/
128                 }
129         }
130 }
131
132 /** Send a clock tick message.
133  * \return true on success.
134  */
135 bool
136 Port::clock (timestamp_t timestamp)
137 {
138         static byte clockmsg = 0xf8;
139         
140         if (_mode != O_RDONLY) {
141                 return midimsg (&clockmsg, 1, timestamp);
142         }
143         
144         return false;
145 }
146
147 void
148 Port::cycle_start (nframes_t nframes)
149 {
150         _currently_in_cycle = true;
151         _nframes_this_cycle = nframes;
152 }
153
154 void
155 Port::cycle_end ()
156 {
157         _currently_in_cycle = false;
158         _nframes_this_cycle = 0;
159 }
160
161 XMLNode&
162 Port::get_state () const
163 {
164         XMLNode* node = new XMLNode ("MIDI-port");
165         node->add_property ("tag", _tagname);
166         node->add_property ("device", _devname);
167         node->add_property ("mode", PortFactory::mode_to_string (_mode));
168         node->add_property ("type", get_typestring());
169
170 #if 0
171         byte device_inquiry[6];
172
173         device_inquiry[0] = 0xf0;
174         device_inquiry[0] = 0x7e;
175         device_inquiry[0] = 0x7f;
176         device_inquiry[0] = 0x06;
177         device_inquiry[0] = 0x02;
178         device_inquiry[0] = 0xf7;
179         
180         write (device_inquiry, sizeof (device_inquiry), 0);
181 #endif
182
183         return *node;
184 }
185
186 void
187 Port::set_state (const XMLNode& /*node*/)
188 {
189         // relax
190 }
191
192 void
193 Port::gtk_read_callback (void *ptr, int /*fd*/, int /*cond*/)
194 {
195         byte buf[64];
196         ((Port *)ptr)->read (buf, sizeof (buf));
197 }
198
199 void
200 Port::write_callback (byte *msg, unsigned int len, void *ptr)
201 {
202         ((Port *)ptr)->write (msg, len, 0);
203 }
204
205 std::ostream & MIDI::operator << ( std::ostream & os, const MIDI::Port & port )
206 {
207         using namespace std;
208         os << "MIDI::Port { ";
209         os << "device: " << port.device();
210         os << "; ";
211         os << "name: " << port.name();
212         os << "; ";
213         os << "type: " << port.type();
214         os << "; ";
215         os << "mode: " << port.mode();
216         os << "; ";
217         os << "ok: " << port.ok();
218         os << "; ";
219         os << " }";
220         return os;
221 }
222
223 Port::Descriptor::Descriptor (const XMLNode& node)
224 {
225         const XMLProperty *prop;
226         bool have_tag = false;
227         bool have_device = false;
228         bool have_type = false;
229         bool have_mode = false;
230
231         if ((prop = node.property ("tag")) != 0) {
232                 tag = prop->value();
233                 have_tag = true;
234         }
235
236         if ((prop = node.property ("device")) != 0) {
237                 device = prop->value();
238                 have_device = true;
239         }
240
241         if ((prop = node.property ("type")) != 0) {
242                 type = PortFactory::string_to_type (prop->value());
243                 have_type = true;
244         }
245
246         if ((prop = node.property ("mode")) != 0) {
247                 mode = PortFactory::string_to_mode (prop->value());
248                 have_mode = true;
249         }
250
251         if (!have_tag || !have_device || !have_type || !have_mode) {
252                 throw failed_constructor();
253         }
254 }
255