3ff05b21c84c7da272002e357dfd6bda0e324525
[ardour.git] / libs / midi++2 / midi++ / fd_midiport.h
1 /*
2     Copyright (C) 1999 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 #ifndef __fd_midiport_h__
22 #define __fd_midiport_h__
23
24 #include <vector>
25 #include <string>
26 #include <cerrno>
27
28 #include <cerrno>
29 #include <fcntl.h>
30 #include <unistd.h>
31
32 #include <midi++/port.h>
33 #include <midi++/port_request.h>
34
35 namespace MIDI {
36
37 class FD_MidiPort : public Port
38
39 {
40   public:
41         FD_MidiPort (PortRequest &req, 
42                      const std::string &dirpath,
43                      const std::string &pattern);
44
45         virtual ~FD_MidiPort () {
46                 ::close (_fd);
47         }
48
49         virtual int selectable() const;
50         static std::vector<std::string *> *list_devices ();
51
52   protected:
53         int _fd;
54         virtual void open (PortRequest &req);
55
56         /* Direct I/O */
57         
58         virtual int write (byte *msg, size_t msglen,
59                            timestamp_t timestamp) {
60                 int nwritten;
61                 
62                 if ((_mode & O_ACCMODE) == O_RDONLY) {
63                         return -EACCES;
64                 }
65                 
66                 if (slowdown) {
67                         return do_slow_write (msg, msglen);
68                 }
69
70                 if ((nwritten = ::write (_fd, msg, msglen)) > 0) {
71                         bytes_written += nwritten;
72                         
73                         if (output_parser) {
74                                 output_parser->raw_preparse 
75                                         (*output_parser, msg, nwritten);
76                                 for (int i = 0; i < nwritten; i++) {
77                                         output_parser->scanner (msg[i]);
78                                 }
79                                 output_parser->raw_postparse 
80                                         (*output_parser, msg, nwritten);
81                         }
82                 }
83                 return nwritten;
84         }
85
86         virtual int read (byte *buf, size_t max,
87                           timestamp_t timestamp);
88
89   private:
90         static std::string *midi_dirpath;
91         static std::string *midi_filename_pattern;
92
93         int do_slow_write (byte *msg, unsigned int msglen);
94 };
95
96 } // namespace MIDI
97
98 #endif  // __fd_midiport_h__