LV2 support.
[ardour.git] / libs / midi++2 / midiport.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
24 #include <pbd/xml++.h>
25 #include <pbd/failed_constructor.h>
26
27 #include <midi++/types.h>
28 #include <midi++/port.h>
29 #include <midi++/channel.h>
30 #include <midi++/factory.h>
31
32 using namespace MIDI;
33 using namespace std;
34
35 size_t Port::nports = 0;
36
37 Port::Port (const XMLNode& node)
38 {
39         Descriptor desc (node);
40
41         _ok = false;  /* derived class must set to true if constructor
42                          succeeds.
43                       */
44
45         bytes_written = 0;
46         bytes_read = 0;
47         input_parser = 0;
48         output_parser = 0;
49         slowdown = 0;
50
51         _devname = desc.device;
52         _tagname = desc.tag;
53         _mode = desc.mode;
54
55         if (_mode == O_RDONLY || _mode == O_RDWR) {
56                 input_parser = new Parser (*this);
57         } else {
58                 input_parser = 0;
59         }
60
61         if (_mode == O_WRONLY || _mode == O_RDWR) {
62                 output_parser = new Parser (*this);
63         } else {
64                 output_parser = 0;
65         }
66
67         for (int i = 0; i < 16; i++) {
68                 _channel[i] =  new Channel (i, *this);
69
70                 if (input_parser) {
71                         _channel[i]->connect_input_signals ();
72                 }
73
74                 if (output_parser) {
75                         _channel[i]->connect_output_signals ();
76                 }
77         }
78 }
79
80
81 Port::~Port ()
82         
83 {
84         for (int i = 0; i < 16; i++) {
85                 delete _channel[i];
86         }
87 }
88
89 XMLNode&
90 Port::get_state () const
91 {
92         XMLNode* node = new XMLNode ("MIDI-port");
93         node->add_property ("tag", _tagname);
94         node->add_property ("device", _devname);
95         node->add_property ("mode", PortFactory::mode_to_string (_mode));
96         node->add_property ("type", get_typestring());
97
98         return *node;
99 }
100
101 void
102 Port::set_state (const XMLNode& node)
103 {
104         // relax
105 }
106
107 int
108 Port::clock ()
109         
110 {
111         static byte clockmsg = 0xf8;
112         
113         if (_mode != O_RDONLY) {
114                 return midimsg (&clockmsg, 1);
115         }
116         
117         return 0;
118 }
119
120 /*
121 void
122 Port::selector_read_callback (Selectable *s, Select::Condition cond) 
123
124 {
125         byte buf[64];
126         read (buf, sizeof (buf));
127 }
128 */
129
130 void
131 Port::xforms_read_callback (int cond, int fd, void *ptr) 
132
133 {
134         byte buf[64];
135         
136         ((Port *)ptr)->read (buf, sizeof (buf));
137 }
138
139 void
140 Port::gtk_read_callback (void *ptr, int fd, int cond)
141
142 {
143         byte buf[64];
144         
145         ((Port *)ptr)->read (buf, sizeof (buf));
146 }
147
148 void
149 Port::write_callback (byte *msg, unsigned int len, void *ptr)
150         
151 {
152         ((Port *)ptr)->write (msg, len);
153 }
154
155 std::ostream & MIDI::operator << ( std::ostream & os, const MIDI::Port & port )
156 {
157         using namespace std;
158         os << "MIDI::Port { ";
159         os << "device: " << port.device();
160         os << "; ";
161         os << "name: " << port.name();
162         os << "; ";
163         os << "type: " << port.type();
164         os << "; ";
165         os << "mode: " << port.mode();
166         os << "; ";
167         os << "ok: " << port.ok();
168         os << "; ";
169         os << " }";
170         return os;
171 }
172
173 Port::Descriptor::Descriptor (const XMLNode& node)
174 {
175         const XMLProperty *prop;
176         bool have_tag = false;
177         bool have_device = false;
178         bool have_type = false;
179         bool have_mode = false;
180
181         if ((prop = node.property ("tag")) != 0) {
182                 tag = prop->value();
183                 have_tag = true;
184         }
185
186         if ((prop = node.property ("device")) != 0) {
187                 device = prop->value();
188                 have_device = true;
189         }
190
191         if ((prop = node.property ("type")) != 0) {
192                 type = PortFactory::string_to_type (prop->value());
193                 have_type = true;
194         }
195
196         if ((prop = node.property ("mode")) != 0) {
197                 mode = PortFactory::string_to_mode (prop->value());
198                 have_mode = true;
199         }
200
201         if (!have_tag || !have_device || !have_type || !have_mode) {
202                 throw failed_constructor();
203         }
204 }
205