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