Fix corrupt MIDI file writing when meta events are present (fixes missing first note...
[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 */
19
20 #ifndef __fd_midiport_h__
21 #define __fd_midiport_h__
22
23 #include <vector>
24 #include <string>
25 #include <cerrno>
26
27 #include <cerrno>
28 #include <fcntl.h>
29 #include <unistd.h>
30
31 #include <midi++/port.h>
32
33 namespace MIDI {
34
35 class FD_MidiPort : public Port
36
37 {
38   public:
39         FD_MidiPort (const XMLNode& node,
40                      const std::string &dirpath,
41                      const std::string &pattern);
42
43         virtual ~FD_MidiPort () {
44                 ::close (_fd);
45         }
46
47         virtual int selectable() const;
48
49         static std::vector<std::string *> *list_devices ();
50
51   protected:
52         int _fd;
53         virtual void open (const Port::Descriptor&);
54
55         virtual int write (byte *msg, size_t msglen, timestamp_t ignored) {
56                 int nwritten;
57                 
58                 if ((_mode & O_ACCMODE) == O_RDONLY) {
59                         return -EACCES;
60                 }
61                 
62                 if (slowdown) {
63                         return do_slow_write (msg, msglen);
64                 }
65
66                 if ((nwritten = ::write (_fd, msg, msglen)) > 0) {
67                         bytes_written += nwritten;
68                         
69                         if (output_parser) {
70                                 output_parser->raw_preparse 
71                                         (*output_parser, msg, nwritten);
72                                 for (int i = 0; i < nwritten; i++) {
73                                         output_parser->scanner (msg[i]);
74                                 }
75                                 output_parser->raw_postparse 
76                                         (*output_parser, msg, nwritten);
77                         }
78                 }
79                 return nwritten;
80         }
81
82         int read (byte *buf, size_t max);
83
84   private:
85         static std::string *midi_dirpath;
86         static std::string *midi_filename_pattern;
87
88         int do_slow_write (byte *msg, unsigned int msglen);
89 };
90
91 } // namespace MIDI
92
93 #endif  // __fd_midiport_h__