use new syntax for connecting to backend signals that enforces explicit connection...
[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 "pbd/xml++.h"
26
27 #include "midi++/types.h"
28 #include "midi++/parser.h"
29
30 namespace MIDI {
31
32 class Channel;
33 class PortRequest;
34
35 class Port {
36   public:
37         enum Type {
38                 Unknown,
39                 JACK_Midi,
40                 ALSA_RawMidi,
41                 ALSA_Sequencer,
42                 CoreMidi_MidiPort,
43                 Null,
44                 FIFO
45         };
46
47
48         Port (const XMLNode&);
49         virtual ~Port ();
50
51         virtual XMLNode& get_state () const;
52         virtual 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(nframes_t nframes);
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 (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         void parse (nframes_t timestamp);
77         
78         /** Write a message to port.
79          * @return true on success.
80          * FIXME: describe semantics here
81          */
82         int midimsg (byte *msg, size_t len, timestamp_t timestamp) {
83                 return !(write (msg, len, timestamp) == (int) len);
84         } 
85
86         int three_byte_msg (byte a, byte b, byte c, timestamp_t timestamp) {
87                 byte msg[3];
88
89                 msg[0] = a;
90                 msg[1] = b;
91                 msg[2] = c;
92
93                 return !(write (msg, 3, timestamp) == 3);
94         } 
95         
96         bool clock (timestamp_t timestamp);
97         
98         /* slowdown i/o to a loop of single byte emissions
99            interspersed with a busy loop of 10000 * this value.
100
101            This may be ignored by a particular instance
102            of this virtual class. See FD_MidiPort for an 
103            example of where it used.  
104         */
105
106         void set_slowdown (size_t n) { slowdown = n; }
107
108         /* select(2)/poll(2)-based I/O */
109
110         /** Get the file descriptor for port.
111          * @return File descriptor, or -1 if not selectable. 
112          */
113         virtual int selectable() const = 0;
114         virtual bool must_drain_selectable() const { return false; }
115
116         static void gtk_read_callback (void *ptr, int fd, int cond);
117         static void write_callback (byte *msg, unsigned int len, void *);
118         
119         Channel *channel (channel_t chn) { 
120                 return _channel[chn&0x7F];
121         }
122         
123         Parser *input()     { return input_parser; }
124         Parser *output()    { return output_parser; }
125
126         void iostat (int *written, int *read, 
127                      const size_t **in_counts,
128                      const size_t **out_counts) {
129
130                 *written = bytes_written;
131                 *read = bytes_read;
132                 if (input_parser) {
133                         *in_counts = input_parser->message_counts();
134                 } else {
135                         *in_counts = 0;
136                 }
137                 if (output_parser) {
138                         *out_counts = output_parser->message_counts();
139                 } else {
140                         *out_counts = 0;
141                 }
142         }
143         
144         const char *device () const { return _devname.c_str(); }
145         const char *name () const   { return _tagname.c_str(); }
146         Type   type () const        { return _type; }
147         int    mode () const        { return _mode; }
148         bool   ok ()   const        { return _ok; }
149
150         struct Descriptor {
151             std::string tag;
152             std::string device;
153             int mode;
154             Port::Type type;
155
156             Descriptor (const XMLNode&);
157             XMLNode& get_state();
158         };
159
160   protected:
161         bool             _ok;
162         bool             _currently_in_cycle;
163         nframes_t        _nframes_this_cycle;
164         Type             _type;
165         std::string      _devname;
166         std::string      _tagname;
167         int              _mode;
168         size_t           _number;
169         Channel          *_channel[16];
170         unsigned int     bytes_written;
171         unsigned int     bytes_read;
172         Parser           *input_parser;
173         Parser           *output_parser;
174         size_t           slowdown;
175
176         virtual std::string get_typestring () const = 0;
177
178   private:
179         static size_t nports;
180 };
181
182 struct PortSet {
183     PortSet (std::string str) : owner (str) { }
184     
185     std::string owner;
186     std::list<XMLNode> ports;
187 };
188
189 std::ostream & operator << ( std::ostream & os, const Port & port );
190
191 } // namespace MIDI
192
193 #endif // __libmidi_port_h__