/*
    Copyright (C) 1999 Paul Barton-Davis 

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    $Id: fd_midiport.h,v 1.2 2002/06/01 03:32:49 trutkin Exp $
*/

#ifndef __aseq_midiport_h__
#define __aseq_midiport_h__

#include <vector>
#include <string>

#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#include <midi++/port.h>
#include <midi++/port_request.h>

namespace MIDI {

class ASEQ_MidiPort : public Port

{
  public:
	ASEQ_MidiPort (PortRequest &req); 
	virtual ~ASEQ_MidiPort ();

	virtual int write (byte* msg, size_t msglen);
	virtual int read (byte* buf, size_t max);
	virtual int selectable() const;

  protected:
	snd_seq_handle_t handle;
		}
		
		if (slowdown) {
			return do_slow_write (msg, msglen);
		}

		if ((nwritten = ::write (_fd, msg, msglen)) > 0) {
			bytes_written += nwritten;
			
			if (output_parser) {
				output_parser->raw_preparse 
					(*output_parser, msg, nwritten);
				for (int i = 0; i < nwritten; i++) {
					output_parser->scanner (msg[i]);
				}
				output_parser->raw_postparse 
					(*output_parser, msg, nwritten);
			}
		}
		return nwritten;
	}

	virtual int read (byte *buf, size_t max) {
		int nread;

		if ((_mode & O_ACCMODE) == O_WRONLY) {
			return -EACCES;
		}
		
		if ((nread = ::read (_fd, buf, max)) > 0) {
			bytes_read += nread;

			if (input_parser) {
				input_parser->raw_preparse 
					(*input_parser, buf, nread);
				for (int i = 0; i < nread; i++) {
					input_parser->scanner (buf[i]);
				}	
				input_parser->raw_postparse 
					(*input_parser, buf, nread);
			}
		}
		return nread;
	}
  private:
	static std::string *midi_dirpath;
	static std::string *midi_filename_pattern;

	int do_slow_write (byte *msg, unsigned int msglen);
};

}; /*namespace MIDI */

#endif  // __aseq_midiport_h__
