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