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