4a5319b120da103e5a75a8957046a0513da101ec
[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     $Id$
18 */
19
20 #ifndef  __libmidi_port_h__
21 #define  __libmidi_port_h__
22
23 #include <string>
24
25 #include <sigc++/sigc++.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 : public sigc::trackable {
36   public:
37         enum Type {
38                 Unknown,
39                 ALSA_RawMidi,
40                 ALSA_Sequencer,
41                 CoreMidi_MidiPort,
42                 Null,
43                 FIFO
44         };
45
46
47         Port (PortRequest &);
48         virtual ~Port ();
49
50         /* Direct I/O */
51
52         virtual int write (byte *msg, size_t msglen) = 0;       
53         virtual int read (byte *buf, size_t max) = 0;
54
55         /* slowdown i/o to a loop of single byte emissions
56            interspersed with a busy loop of 10000 * this value.
57
58            This may be ignored by a particular instance
59            of this virtual class. See FD_MidiPort for an 
60            example of where it used.  
61         */
62
63         void set_slowdown (size_t n) { slowdown = n; }
64
65         /* select(2)/poll(2)-based I/O */
66
67         virtual int selectable() const = 0;
68
69         //void selector_read_callback (Select::Selectable *, Select::Condition);
70
71         static void xforms_read_callback (int cond, int fd, void *ptr);
72         static void gtk_read_callback (void *ptr, int fd, int cond);
73         
74         static void write_callback (byte *msg, unsigned int len, void *);
75         
76         Channel *channel (channel_t chn) { 
77                 return _channel[chn&0x7F];
78         }
79         
80         Parser *input()     { return input_parser; }
81         Parser *output()    { return output_parser; }
82
83         void iostat (int *written, int *read, 
84                      const size_t **in_counts,
85                      const size_t **out_counts) {
86
87                 *written = bytes_written;
88                 *read = bytes_read;
89                 if (input_parser) {
90                         *in_counts = input_parser->message_counts();
91                 } else {
92                         *in_counts = 0;
93                 }
94                 if (output_parser) {
95                         *out_counts = output_parser->message_counts();
96                 } else {
97                         *out_counts = 0;
98                 }
99         }
100         
101         int midimsg (byte *msg, size_t len) {
102                 return !(write (msg, len) == (int) len);
103         } 
104
105         int three_byte_msg (byte a, byte b, byte c) {
106                 byte msg[3];
107
108                 msg[0] = a;
109                 msg[1] = b;
110                 msg[2] = c;
111
112                 return !(write (msg, 3) == 3);
113         } 
114         
115         int clock ();
116         
117         const char *device () const { return _devname.c_str(); }
118         const char *name () const   { return _tagname.c_str(); }
119         Type   type () const        { return _type; }
120         int    mode () const        { return _mode; }
121         bool   ok ()   const        { return _ok; }
122         size_t number () const      { return _number; }
123
124   protected:
125         bool _ok;
126         Type _type;
127         std::string _devname;
128         std::string _tagname;
129         int _mode;
130         size_t _number;
131         Channel *_channel[16];
132         sigc::connection thru_connection;
133         unsigned int bytes_written;
134         unsigned int bytes_read;
135         Parser *input_parser;
136         Parser *output_parser;
137         size_t slowdown;
138
139   private:
140         static size_t nports;
141 };
142
143 } // namespace MIDI
144
145 #endif // __libmidi_port_h__