Fix crash with input (not duplex) control MIDI ports.
[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 ()
95 {
96         byte buf[512];
97
98         /* parsing is done (if at all) by initiating a read from 
99            the port.
100         */
101
102         while (1) {
103                 
104                 // cerr << "+++ READ ON " << name() << endl;
105
106                 int nread = read (buf, sizeof (buf));
107
108                 // cerr << "-- READ (" << nread << " ON " << name() << endl;
109                 
110                 if (nread > 0) {
111                         if ((size_t) nread < sizeof (buf)) {
112                                 break;
113                         } else {
114                                 continue;
115                         }
116                 } else if (nread == 0) {
117                         break;
118                 } else if (errno == EAGAIN) {
119                         break;
120                 } else {
121                         fatal << "Error reading from MIDI port " << name() << endmsg;
122                         /*NOTREACHED*/
123                 }
124         }
125 }
126
127 /** Send a clock tick message.
128  * \return true on success.
129  */
130 bool
131 Port::clock (timestamp_t timestamp)
132 {
133         static byte clockmsg = 0xf8;
134         
135         if (_mode != O_RDONLY) {
136                 return midimsg (&clockmsg, 1, timestamp);
137         }
138         
139         return false;
140 }
141
142 void
143 Port::cycle_start (nframes_t nframes)
144 {
145         _currently_in_cycle = true;
146         _nframes_this_cycle = nframes;
147 }
148
149 void
150 Port::cycle_end ()
151 {
152         _currently_in_cycle = false;
153         _nframes_this_cycle = 0;
154 }
155
156 XMLNode&
157 Port::get_state () const
158 {
159         XMLNode* node = new XMLNode ("MIDI-port");
160         node->add_property ("tag", _tagname);
161         node->add_property ("device", _devname);
162         node->add_property ("mode", PortFactory::mode_to_string (_mode));
163         node->add_property ("type", get_typestring());
164
165         return *node;
166 }
167
168 void
169 Port::set_state (const XMLNode& /*node*/)
170 {
171         // relax
172 }
173
174 void
175 Port::gtk_read_callback (void *ptr, int /*fd*/, int /*cond*/)
176 {
177         byte buf[64];
178         ((Port *)ptr)->read (buf, sizeof (buf));
179 }
180
181 void
182 Port::write_callback (byte *msg, unsigned int len, void *ptr)
183 {
184         ((Port *)ptr)->write (msg, len, 0);
185 }
186
187 std::ostream & MIDI::operator << ( std::ostream & os, const MIDI::Port & port )
188 {
189         using namespace std;
190         os << "MIDI::Port { ";
191         os << "device: " << port.device();
192         os << "; ";
193         os << "name: " << port.name();
194         os << "; ";
195         os << "type: " << port.type();
196         os << "; ";
197         os << "mode: " << port.mode();
198         os << "; ";
199         os << "ok: " << port.ok();
200         os << "; ";
201         os << " }";
202         return os;
203 }
204
205 Port::Descriptor::Descriptor (const XMLNode& node)
206 {
207         const XMLProperty *prop;
208         bool have_tag = false;
209         bool have_device = false;
210         bool have_type = false;
211         bool have_mode = false;
212
213         if ((prop = node.property ("tag")) != 0) {
214                 tag = prop->value();
215                 have_tag = true;
216         }
217
218         if ((prop = node.property ("device")) != 0) {
219                 device = prop->value();
220                 have_device = true;
221         }
222
223         if ((prop = node.property ("type")) != 0) {
224                 type = PortFactory::string_to_type (prop->value());
225                 have_type = true;
226         }
227
228         if ((prop = node.property ("mode")) != 0) {
229                 mode = PortFactory::string_to_mode (prop->value());
230                 have_mode = true;
231         }
232
233         if (!have_tag || !have_device || !have_type || !have_mode) {
234                 throw failed_constructor();
235         }
236 }
237