/*
 *
 *
 *    tty.h
 *
 *    C++ header file for class 'tty.'
 *
 *    =========================================================================
 *
 *    Copyright (C) 2001-2003 by Matt Roberts, KK5JY.
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation; either
 *    version 2.1 of the License, or (at your option) any later version.
 *
 *    This library 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
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *    =========================================================================
 *
 *    TODO:
 *       1.  Add a way to flush buffers to some base class
 *
 *
 */


#ifndef __TTY_H
#define __TTY_H

#include <my++/multiplex.h>
#include <my++/exception.h>
#include <my++/device.h>
#include <my++/termios.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

	namespace my {

		/*
		 *
		 *
		 *    class my::tty
		 *
		 *
		 */

		class tty : public my::iodevice {
			public:
				typedef my::exception_template<tty> exception;

				enum actiontype {
					apply_now, drain_first, flush_first
				};

				enum flowtype {
					start, send_start,
					stop, send_stop
				};

				enum flushtype {
					flush_in, flush_out, flush_io
				};

			private: // private data
				mutable int fd;
				my::termios old_options;
				bool dont_save;

			public: // from iodevice
				int getfd (void) throw() { return fd; };

				void open (const std::string &name, int modes);
				void close (void) throw();

				ssize_t read_vp  (void *b, size_t l);
				ssize_t write_vp (const void *b, size_t l);

			public: // public ctors / dtors
				tty (const std::string &filename, const my::termios &opts, int o_modes);
				tty (const std::string &filename, const my::termios &opts);
				tty (const std::string &filename, int o_modes);
				tty (const std::string &filename);
				tty (void);
				~tty (void) throw();

			public: // manipulators
				void set_options (const my::termios &t, actiontype a = apply_now);
				void get_options (my::termios &t);

				void drain (void);
				void flush (flushtype f = flush_io);
				void flow (flowtype f);
				void send_break (int intervals = 0);
				void set_pgrp (pid_t p_gid);
				void set_restore (bool b) { dont_save = !b; };

			private: // hidden or disabled functions
				tty (const tty &t);
		};
	}

#endif /* __TTY_H */
