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