add missing parts of previous commit re: libmidi++ and JACK (files not saved in emacs...
[ardour.git] / libs / midi++2 / midi++ / port.h
1 /*
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.
7
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.
12
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.
16
17 */
18
19 #ifndef  __libmidi_port_base_h__
20 #define  __libmidi_port_base_h__
21
22 #include <string>
23 #include <iostream>
24
25 #include "pbd/xml++.h"
26 #include "pbd/crossthread.h"
27 #include "pbd/signals.h"
28 #include "pbd/ringbuffer.h"
29
30 #include "midi++/types.h"
31 #include "midi++/parser.h"
32
33 namespace MIDI {
34
35 class Channel;
36 class PortRequest;
37
38 class Port {
39   public:
40         enum Flags {
41                 IsInput = JackPortIsInput,
42                 IsOutput = JackPortIsOutput,
43         };
44         
45         Port (std::string const &, Flags);
46         Port (const XMLNode&);
47         virtual ~Port ();
48
49         virtual XMLNode& get_state () const;
50         virtual void set_state (const XMLNode&);
51
52         /** Write a message to port.
53          * @param msg Raw MIDI message to send
54          * @param msglen Size of @a msg
55          * @param timestamp Time stamp in frames of this message (relative to cycle start)
56          * @return number of bytes successfully written
57          */
58         virtual int write (const byte *msg, size_t msglen, timestamp_t timestamp) = 0;
59
60         /** Read raw bytes from a port.
61          * @param buf memory to store read data in
62          * @param bufsize size of @a buf
63          * @return number of bytes successfully read, negative if error
64          */
65         virtual int read (byte *buf, size_t bufsize) = 0;
66
67         /** block until the output FIFO used by non-process threads
68          * is empty, checking every @a check_interval_usecs usecs
69          * for current status. Not to be called by a thread that
70          * executes any part of a JACK process callback (will 
71          * simply return immediately in that situation).
72          */
73         virtual void drain (int /* check_interval_usecs */) {}
74
75         /** Write a message to port.
76          * @return true on success.
77          * FIXME: describe semantics here
78          */
79         int midimsg (byte *msg, size_t len, timestamp_t timestamp) {
80                 return !(write (msg, len, timestamp) == (int) len);
81         } 
82
83         virtual void parse (framecnt_t timestamp) = 0;
84
85         bool clock (timestamp_t timestamp);
86
87         /* select(2)/poll(2)-based I/O */
88
89         /** Get the file descriptor for port.
90          * @return File descriptor, or -1 if not selectable. 
91          */
92         virtual int selectable () const = 0;
93
94         Channel *channel (channel_t chn) { 
95                 return _channel[chn&0x7F];
96         }
97         
98         Parser* parser () {
99                 return _parser;
100         }
101         
102         const char *name () const   { return _tagname.c_str(); }
103         bool   ok ()   const        { return _ok; }
104
105         virtual bool centrally_parsed() const;
106         void set_centrally_parsed (bool yn) { _centrally_parsed = yn; }
107
108         bool receives_input () const {
109                 return _flags == IsInput;
110         }
111
112         bool sends_output () const {
113                 return _flags == IsOutput;
114         }
115
116         struct Descriptor {
117             std::string tag;
118             Flags flags;
119
120             Descriptor (const XMLNode&);
121             XMLNode& get_state();
122         };
123
124         static std::string state_node_name;
125
126   protected:
127         bool              _ok;
128         std::string       _tagname;
129         Channel*          _channel[16];
130         Parser*           _parser;
131         Flags             _flags;
132         bool              _centrally_parsed;
133
134         void init (std::string const &, Flags);
135 };
136
137 struct PortSet {
138     PortSet (std::string str) : owner (str) { }
139     
140     std::string owner;
141     std::list<XMLNode> ports;
142 };
143
144 std::ostream & operator << (std::ostream& os, const Port& port);
145
146 } // namespace MIDI
147
148 #endif // __libmidi_port_base_h__