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.
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.
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.
19 #ifndef __libmidi_port_h__
20 #define __libmidi_port_h__
25 #include <boost/signals2.hpp>
26 #include "pbd/xml++.h"
28 #include "midi++/types.h"
29 #include "midi++/parser.h"
49 Port (const XMLNode&);
52 virtual XMLNode& get_state () const;
53 virtual void set_state (const XMLNode&);
55 // FIXME: make Manager a friend of port so these can be hidden?
57 /* Only for use by MidiManager. Don't ever call this. */
58 virtual void cycle_start(nframes_t nframes);
59 /* Only for use by MidiManager. Don't ever call this. */
60 virtual void cycle_end();
62 /** Write a message to port.
63 * @param msg Raw MIDI message to send
64 * @param msglen Size of @a msg
65 * @param timestamp Time stamp in frames of this message (relative to cycle start)
66 * @return number of bytes successfully written
68 virtual int write (byte *msg, size_t msglen, timestamp_t timestamp) = 0;
70 /** Read raw bytes from a port.
71 * @param buf memory to store read data in
72 * @param bufsize size of @a buf
73 * @return number of bytes successfully read, negative if error
75 virtual int read (byte *buf, size_t bufsize) = 0;
77 void parse (nframes_t timestamp);
79 /** Write a message to port.
80 * @return true on success.
81 * FIXME: describe semantics here
83 int midimsg (byte *msg, size_t len, timestamp_t timestamp) {
84 return !(write (msg, len, timestamp) == (int) len);
87 int three_byte_msg (byte a, byte b, byte c, timestamp_t timestamp) {
94 return !(write (msg, 3, timestamp) == 3);
97 bool clock (timestamp_t timestamp);
99 /* slowdown i/o to a loop of single byte emissions
100 interspersed with a busy loop of 10000 * this value.
102 This may be ignored by a particular instance
103 of this virtual class. See FD_MidiPort for an
104 example of where it used.
107 void set_slowdown (size_t n) { slowdown = n; }
109 /* select(2)/poll(2)-based I/O */
111 /** Get the file descriptor for port.
112 * @return File descriptor, or -1 if not selectable.
114 virtual int selectable() const = 0;
115 virtual bool must_drain_selectable() const { return false; }
117 static void gtk_read_callback (void *ptr, int fd, int cond);
118 static void write_callback (byte *msg, unsigned int len, void *);
120 Channel *channel (channel_t chn) {
121 return _channel[chn&0x7F];
124 Parser *input() { return input_parser; }
125 Parser *output() { return output_parser; }
127 void iostat (int *written, int *read,
128 const size_t **in_counts,
129 const size_t **out_counts) {
131 *written = bytes_written;
134 *in_counts = input_parser->message_counts();
139 *out_counts = output_parser->message_counts();
145 const char *device () const { return _devname.c_str(); }
146 const char *name () const { return _tagname.c_str(); }
147 Type type () const { return _type; }
148 int mode () const { return _mode; }
149 bool ok () const { return _ok; }
157 Descriptor (const XMLNode&);
158 XMLNode& get_state();
163 bool _currently_in_cycle;
164 nframes_t _nframes_this_cycle;
166 std::string _devname;
167 std::string _tagname;
170 Channel *_channel[16];
171 unsigned int bytes_written;
172 unsigned int bytes_read;
173 Parser *input_parser;
174 Parser *output_parser;
177 virtual std::string get_typestring () const = 0;
180 static size_t nports;
184 PortSet (std::string str) : owner (str) { }
187 std::list<XMLNode> ports;
190 std::ostream & operator << ( std::ostream & os, const Port & port );
194 #endif // __libmidi_port_h__