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