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