f16743337c8c7e626b7604929c5a578e94ccdf46
[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> /* XXX ... desirable to get rid of this but needed for
26                          * now due to use of JackPortIsXXXX
27                          */
28
29 #include "pbd/xml++.h"
30 #include "pbd/crossthread.h"
31 #include "pbd/signals.h"
32 #include "pbd/ringbuffer.h"
33
34 #include "midi++/types.h"
35 #include "midi++/parser.h"
36
37 namespace MIDI {
38
39 class Channel;
40 class PortRequest;
41
42 class Port {
43   public:
44         enum Flags {
45                 IsInput = JackPortIsInput,
46                 IsOutput = JackPortIsOutput,
47         };
48         
49         Port (std::string const &, Flags);
50         Port (const XMLNode&);
51         virtual ~Port ();
52
53         virtual XMLNode& get_state () const;
54         virtual void set_state (const XMLNode&);
55
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
61          */
62         virtual int write (const byte *msg, size_t msglen, timestamp_t timestamp) = 0;
63
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
68          */
69         virtual int read (byte *buf, size_t bufsize) = 0;
70
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).
76          */
77         virtual void drain (int /* check_interval_usecs */) {}
78
79         /** Write a message to port.
80          * @return true on success.
81          * FIXME: describe semantics here
82          */
83         int midimsg (byte *msg, size_t len, timestamp_t timestamp) {
84                 return !(write (msg, len, timestamp) == (int) len);
85         } 
86
87         virtual void parse (framecnt_t timestamp) = 0;
88
89         bool clock (timestamp_t timestamp);
90
91         /* select(2)/poll(2)-based I/O */
92
93         /** Get the file descriptor for port.
94          * @return File descriptor, or -1 if not selectable. 
95          */
96         virtual int selectable () const = 0;
97
98         Channel *channel (channel_t chn) { 
99                 return _channel[chn&0x7F];
100         }
101         
102         Parser* parser () {
103                 return _parser;
104         }
105         
106         const char *name () const   { return _tagname.c_str(); }
107         bool   ok ()   const        { return _ok; }
108
109         virtual bool centrally_parsed() const;
110         void set_centrally_parsed (bool yn) { _centrally_parsed = yn; }
111
112         bool receives_input () const {
113                 return _flags == IsInput;
114         }
115
116         bool sends_output () const {
117                 return _flags == IsOutput;
118         }
119
120         struct Descriptor {
121             std::string tag;
122             Flags flags;
123
124             Descriptor (const XMLNode&);
125             XMLNode& get_state();
126         };
127
128         static std::string state_node_name;
129
130   protected:
131         bool              _ok;
132         std::string       _tagname;
133         Channel*          _channel[16];
134         Parser*           _parser;
135         Flags             _flags;
136         bool              _centrally_parsed;
137
138         void init (std::string const &, Flags);
139 };
140
141 struct PortSet {
142     PortSet (std::string str) : owner (str) { }
143     
144     std::string owner;
145     std::list<XMLNode> ports;
146 };
147
148 std::ostream & operator << (std::ostream& os, const Port& port);
149
150 } // namespace MIDI
151
152 #endif // __libmidi_port_base_h__