0c5fe4ae37570e7fe13faff47b82b8671442af7d
[ardour.git] / libs / midi++2 / midi++ / port.h
1 /*
2     Copyright (C) 1998-99 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_h__
20 #define  __libmidi_port_h__
21
22 #include <string>
23 #include <iostream>
24
25 #include <boost/signals2.hpp>
26 #include "pbd/xml++.h"
27
28 #include "midi++/types.h"
29 #include "midi++/parser.h"
30
31 namespace MIDI {
32
33 class Channel;
34 class PortRequest;
35
36 class Port {
37   public:
38         enum Type {
39                 Unknown,
40                 JACK_Midi,
41                 ALSA_RawMidi,
42                 ALSA_Sequencer,
43                 CoreMidi_MidiPort,
44                 Null,
45                 FIFO
46         };
47
48
49         Port (const XMLNode&);
50         virtual ~Port ();
51
52         virtual XMLNode& get_state () const;
53         virtual void set_state (const XMLNode&);
54
55         // FIXME: make Manager a friend of port so these can be hidden?
56
57         /* Only for use by MidiManager.  Don't ever call this. */
58         virtual void cycle_start(nframes_t nframes);
59         /* Only for use by MidiManager.  Don't ever call this. */
60         virtual void cycle_end();
61
62         /** Write a message to port.
63          * @param msg Raw MIDI message to send
64          * @param msglen Size of @a msg
65          * @param timestamp Time stamp in frames of this message (relative to cycle start)
66          * @return number of bytes successfully written
67          */
68         virtual int write (byte *msg, size_t msglen, timestamp_t timestamp) = 0;        
69
70         /** Read raw bytes from a port.
71          * @param buf memory to store read data in
72          * @param bufsize size of @a buf
73          * @return number of bytes successfully read, negative if error
74          */
75         virtual int read (byte *buf, size_t bufsize) = 0;
76
77         void parse (nframes_t timestamp);
78         
79         /** Write a message to port.
80          * @return true on success.
81          * FIXME: describe semantics here
82          */
83         int midimsg (byte *msg, size_t len, timestamp_t timestamp) {
84                 return !(write (msg, len, timestamp) == (int) len);
85         } 
86
87         int three_byte_msg (byte a, byte b, byte c, timestamp_t timestamp) {
88                 byte msg[3];
89
90                 msg[0] = a;
91                 msg[1] = b;
92                 msg[2] = c;
93
94                 return !(write (msg, 3, timestamp) == 3);
95         } 
96         
97         bool clock (timestamp_t timestamp);
98         
99         /* slowdown i/o to a loop of single byte emissions
100            interspersed with a busy loop of 10000 * this value.
101
102            This may be ignored by a particular instance
103            of this virtual class. See FD_MidiPort for an 
104            example of where it used.  
105         */
106
107         void set_slowdown (size_t n) { slowdown = n; }
108
109         /* select(2)/poll(2)-based I/O */
110
111         /** Get the file descriptor for port.
112          * @return File descriptor, or -1 if not selectable. 
113          */
114         virtual int selectable() const = 0;
115         virtual bool must_drain_selectable() const { return false; }
116
117         static void gtk_read_callback (void *ptr, int fd, int cond);
118         static void write_callback (byte *msg, unsigned int len, void *);
119         
120         Channel *channel (channel_t chn) { 
121                 return _channel[chn&0x7F];
122         }
123         
124         Parser *input()     { return input_parser; }
125         Parser *output()    { return output_parser; }
126
127         void iostat (int *written, int *read, 
128                      const size_t **in_counts,
129                      const size_t **out_counts) {
130
131                 *written = bytes_written;
132                 *read = bytes_read;
133                 if (input_parser) {
134                         *in_counts = input_parser->message_counts();
135                 } else {
136                         *in_counts = 0;
137                 }
138                 if (output_parser) {
139                         *out_counts = output_parser->message_counts();
140                 } else {
141                         *out_counts = 0;
142                 }
143         }
144         
145         const char *device () const { return _devname.c_str(); }
146         const char *name () const   { return _tagname.c_str(); }
147         Type   type () const        { return _type; }
148         int    mode () const        { return _mode; }
149         bool   ok ()   const        { return _ok; }
150
151         struct Descriptor {
152             std::string tag;
153             std::string device;
154             int mode;
155             Port::Type type;
156
157             Descriptor (const XMLNode&);
158             XMLNode& get_state();
159         };
160
161   protected:
162         bool             _ok;
163         bool             _currently_in_cycle;
164         nframes_t        _nframes_this_cycle;
165         Type             _type;
166         std::string      _devname;
167         std::string      _tagname;
168         int              _mode;
169         size_t           _number;
170         Channel          *_channel[16];
171         unsigned int     bytes_written;
172         unsigned int     bytes_read;
173         Parser           *input_parser;
174         Parser           *output_parser;
175         size_t           slowdown;
176
177         virtual std::string get_typestring () const = 0;
178
179   private:
180         static size_t nports;
181 };
182
183 struct PortSet {
184     PortSet (std::string str) : owner (str) { }
185     
186     std::string owner;
187     std::list<XMLNode> ports;
188 };
189
190 std::ostream & operator << ( std::ostream & os, const Port & port );
191
192 } // namespace MIDI
193
194 #endif // __libmidi_port_h__