a7c4ba09401f6ea333f0092321c041dbe670b6f3
[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 /** Send a clock tick message.
87  * \return true on success.
88  */
89 bool
90 Port::clock ()
91         
92 {
93         static byte clockmsg = 0xf8;
94         
95         if (_mode != O_RDONLY) {
96                 return midimsg (&clockmsg, 1);
97         }
98         
99         return false;
100 }
101
102 void
103 Port::selector_read_callback (Selectable *s, Select::Condition cond) 
104
105 {
106         byte buf[64];
107         read (buf, sizeof (buf));
108 }
109
110 void
111 Port::xforms_read_callback (int cond, int fd, void *ptr) 
112
113 {
114         byte buf[64];
115         
116         ((Port *)ptr)->read (buf, sizeof (buf));
117 }
118
119 void
120 Port::gtk_read_callback (void *ptr, int fd, int cond)
121
122 {
123         byte buf[64];
124         
125         ((Port *)ptr)->read (buf, sizeof (buf));
126 }
127
128 void
129 Port::write_callback (byte *msg, unsigned int len, void *ptr)
130         
131 {
132         ((Port *)ptr)->write (msg, len);
133 }
134