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