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