Initial revision
[ardour.git] / libs / midi++2 / midiport.cc
1 /*
2     Copyright (C) 1998 Paul Barton-Davis
3     
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id$
19 */
20
21 #include <cstdio>
22 #include <fcntl.h>
23
24 #include <midi++/types.h>
25 #include <midi++/port.h>
26 #include <midi++/channel.h>
27 #include <midi++/port_request.h>
28
29 using namespace Select;
30 using namespace MIDI;
31
32 size_t Port::nports = 0;
33
34 Port::Port (PortRequest &req)
35
36 {
37         _ok = false;  /* derived class must set to true if constructor
38                          succeeds.
39                       */
40
41         bytes_written = 0;
42         bytes_read = 0;
43         input_parser = 0;
44         output_parser = 0;
45         slowdown = 0;
46
47         _devname = req.devname;
48         _tagname = req.tagname;
49         _mode = req.mode;
50         _number = nports++;
51
52         if (_mode == O_RDONLY || _mode == O_RDWR) {
53                 input_parser = new Parser (*this);
54         } else {
55                 input_parser = 0;
56         }
57
58         if (_mode == O_WRONLY || _mode == O_RDWR) {
59                 output_parser = new Parser (*this);
60         } else {
61                 output_parser = 0;
62         }
63
64         for (int i = 0; i < 16; i++) {
65                 _channel[i] =  new Channel (i, *this);
66
67                 if (input_parser) {
68                         _channel[i]->connect_input_signals ();
69                 }
70
71                 if (output_parser) {
72                         _channel[i]->connect_output_signals ();
73                 }
74         }
75 }
76
77
78 Port::~Port ()
79         
80 {
81         for (int i = 0; i < 16; i++) {
82                 delete _channel[i];
83         }
84 }
85
86 int
87 Port::clock ()
88         
89 {
90         static byte clockmsg = 0xf8;
91         
92         if (_mode != O_RDONLY) {
93                 return midimsg (&clockmsg, 1);
94         }
95         
96         return 0;
97 }
98
99 void
100 Port::selector_read_callback (Selectable *s, Select::Condition cond) 
101
102 {
103         byte buf[64];
104         read (buf, sizeof (buf));
105 }
106
107 void
108 Port::xforms_read_callback (int cond, int fd, void *ptr) 
109
110 {
111         byte buf[64];
112         
113         ((Port *)ptr)->read (buf, sizeof (buf));
114 }
115
116 void
117 Port::gtk_read_callback (void *ptr, int fd, int cond)
118
119 {
120         byte buf[64];
121         
122         ((Port *)ptr)->read (buf, sizeof (buf));
123 }
124
125 void
126 Port::write_callback (byte *msg, unsigned int len, void *ptr)
127         
128 {
129         ((Port *)ptr)->write (msg, len);
130 }
131