2 Copyright (C) 1998-2010 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_base_h__
20 #define __libmidi_port_base_h__
25 #include <jack/types.h> /* XXX ... desirable to get rid of this but needed for
26 * now due to use of JackPortIsXXXX
29 #include "pbd/xml++.h"
30 #include "pbd/crossthread.h"
31 #include "pbd/signals.h"
32 #include "pbd/ringbuffer.h"
34 #include "midi++/types.h"
35 #include "midi++/parser.h"
45 IsInput = JackPortIsInput,
46 IsOutput = JackPortIsOutput,
49 Port (std::string const &, Flags);
50 Port (const XMLNode&);
53 virtual XMLNode& get_state () const;
54 virtual void set_state (const XMLNode&);
56 /** Write a message to port.
57 * @param msg Raw MIDI message to send
58 * @param msglen Size of @a msg
59 * @param timestamp Time stamp in frames of this message (relative to cycle start)
60 * @return number of bytes successfully written
62 virtual int write (const byte *msg, size_t msglen, timestamp_t timestamp) = 0;
64 /** Read raw bytes from a port.
65 * @param buf memory to store read data in
66 * @param bufsize size of @a buf
67 * @return number of bytes successfully read, negative if error
69 virtual int read (byte *buf, size_t bufsize) = 0;
71 /** block until the output FIFO used by non-process threads
72 * is empty, checking every @a check_interval_usecs usecs
73 * for current status. Not to be called by a thread that
74 * executes any part of a JACK process callback (will
75 * simply return immediately in that situation).
77 virtual void drain (int /* check_interval_usecs */) {}
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 virtual void parse (framecnt_t timestamp) = 0;
89 bool clock (timestamp_t timestamp);
91 /* select(2)/poll(2)-based I/O */
93 /** Get the file descriptor for port.
94 * @return File descriptor, or -1 if not selectable.
96 virtual int selectable () const = 0;
98 Channel *channel (channel_t chn) {
99 return _channel[chn&0x7F];
106 const char *name () const { return _tagname.c_str(); }
107 bool ok () const { return _ok; }
109 virtual bool centrally_parsed() const;
110 void set_centrally_parsed (bool yn) { _centrally_parsed = yn; }
112 bool receives_input () const {
113 return _flags == IsInput;
116 bool sends_output () const {
117 return _flags == IsOutput;
124 Descriptor (const XMLNode&);
125 XMLNode& get_state();
128 static std::string state_node_name;
132 std::string _tagname;
133 Channel* _channel[16];
136 bool _centrally_parsed;
138 void init (std::string const &, Flags);
142 PortSet (std::string str) : owner (str) { }
145 std::list<XMLNode> ports;
148 std::ostream & operator << (std::ostream& os, const Port& port);
152 #endif // __libmidi_port_base_h__