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 "pbd/xml++.h"
27 #include "midi++/types.h"
28 #include "midi++/parser.h"
48 Port (const XMLNode&);
51 virtual XMLNode& get_state () const;
52 virtual void set_state (const XMLNode&);
54 // FIXME: make Manager a friend of port so these can be hidden?
56 /* Only for use by MidiManager. Don't ever call this. */
57 virtual void cycle_start(nframes_t nframes);
58 /* Only for use by MidiManager. Don't ever call this. */
59 virtual void cycle_end();
61 /** Write a message to port.
62 * @param msg Raw MIDI message to send
63 * @param msglen Size of @a msg
64 * @param timestamp Time stamp in frames of this message (relative to cycle start)
65 * @return number of bytes successfully written
67 virtual int write (byte *msg, size_t msglen, timestamp_t timestamp) = 0;
69 /** Read raw bytes from a port.
70 * @param buf memory to store read data in
71 * @param bufsize size of @a buf
72 * @return number of bytes successfully read, negative if error
74 virtual int read (byte *buf, size_t bufsize) = 0;
76 void parse (nframes_t timestamp);
78 /** Write a message to port.
79 * @return true on success.
80 * FIXME: describe semantics here
82 int midimsg (byte *msg, size_t len, timestamp_t timestamp) {
83 return !(write (msg, len, timestamp) == (int) len);
86 int three_byte_msg (byte a, byte b, byte c, timestamp_t timestamp) {
93 return !(write (msg, 3, timestamp) == 3);
96 bool clock (timestamp_t timestamp);
98 /* slowdown i/o to a loop of single byte emissions
99 interspersed with a busy loop of 10000 * this value.
101 This may be ignored by a particular instance
102 of this virtual class. See FD_MidiPort for an
103 example of where it used.
106 void set_slowdown (size_t n) { slowdown = n; }
108 /* select(2)/poll(2)-based I/O */
110 /** Get the file descriptor for port.
111 * @return File descriptor, or -1 if not selectable.
113 virtual int selectable() const = 0;
114 virtual bool must_drain_selectable() const { return false; }
116 static void gtk_read_callback (void *ptr, int fd, int cond);
117 static void write_callback (byte *msg, unsigned int len, void *);
119 Channel *channel (channel_t chn) {
120 return _channel[chn&0x7F];
123 Parser *input() { return input_parser; }
124 Parser *output() { return output_parser; }
126 void iostat (int *written, int *read,
127 const size_t **in_counts,
128 const size_t **out_counts) {
130 *written = bytes_written;
133 *in_counts = input_parser->message_counts();
138 *out_counts = output_parser->message_counts();
144 const char *device () const { return _devname.c_str(); }
145 const char *name () const { return _tagname.c_str(); }
146 Type type () const { return _type; }
147 int mode () const { return _mode; }
148 bool ok () const { return _ok; }
156 Descriptor (const XMLNode&);
157 XMLNode& get_state();
162 bool _currently_in_cycle;
163 nframes_t _nframes_this_cycle;
165 std::string _devname;
166 std::string _tagname;
169 Channel *_channel[16];
170 unsigned int bytes_written;
171 unsigned int bytes_read;
172 Parser *input_parser;
173 Parser *output_parser;
176 virtual std::string get_typestring () const = 0;
179 static size_t nports;
183 PortSet (std::string str) : owner (str) { }
186 std::list<XMLNode> ports;
189 std::ostream & operator << ( std::ostream & os, const Port & port );
193 #endif // __libmidi_port_h__