Home | History | Annotate | Line # | Download | only in libcurses
tty.c revision 1.4
      1  1.1  mycroft /*-
      2  1.2      cgd  * Copyright (c) 1992, 1993
      3  1.2      cgd  *	The Regents of the University of California.  All rights reserved.
      4  1.1  mycroft  *
      5  1.1  mycroft  * Redistribution and use in source and binary forms, with or without
      6  1.1  mycroft  * modification, are permitted provided that the following conditions
      7  1.1  mycroft  * are met:
      8  1.1  mycroft  * 1. Redistributions of source code must retain the above copyright
      9  1.1  mycroft  *    notice, this list of conditions and the following disclaimer.
     10  1.1  mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  mycroft  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  mycroft  *    documentation and/or other materials provided with the distribution.
     13  1.1  mycroft  * 3. All advertising materials mentioning features or use of this software
     14  1.1  mycroft  *    must display the following acknowledgement:
     15  1.1  mycroft  *	This product includes software developed by the University of
     16  1.1  mycroft  *	California, Berkeley and its contributors.
     17  1.1  mycroft  * 4. Neither the name of the University nor the names of its contributors
     18  1.1  mycroft  *    may be used to endorse or promote products derived from this software
     19  1.1  mycroft  *    without specific prior written permission.
     20  1.1  mycroft  *
     21  1.1  mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1  mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1  mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1  mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  mycroft  * SUCH DAMAGE.
     32  1.1  mycroft  */
     33  1.1  mycroft 
     34  1.1  mycroft #ifndef lint
     35  1.4      cgd /* from: static char sccsid[] = "@(#)tty.c	8.2 (Berkeley) 1/2/94"; */
     36  1.4      cgd static char *rcsid = "$Id: tty.c,v 1.4 1994/01/24 08:36:59 cgd Exp $";
     37  1.1  mycroft #endif /* not lint */
     38  1.1  mycroft 
     39  1.1  mycroft #include <sys/ioctl.h>
     40  1.1  mycroft 
     41  1.1  mycroft #include <curses.h>
     42  1.1  mycroft #include <termios.h>
     43  1.1  mycroft #include <unistd.h>
     44  1.1  mycroft 
     45  1.2      cgd /*
     46  1.2      cgd  * In general, curses should leave tty hardware settings alone (speed, parity,
     47  1.2      cgd  * word size).  This is most easily done in BSD by using TCSASOFT on all
     48  1.2      cgd  * tcsetattr calls.  On other systems, it would be better to get and restore
     49  1.2      cgd  * those attributes at each change, or at least when stopped and restarted.
     50  1.2      cgd  * See also the comments in getterm().
     51  1.2      cgd  */
     52  1.2      cgd #ifdef TCSASOFT
     53  1.2      cgd int __tcaction = 1;			/* Ignore hardware settings. */
     54  1.2      cgd #else
     55  1.2      cgd int __tcaction = 0;
     56  1.2      cgd #endif
     57  1.2      cgd 
     58  1.2      cgd struct termios __orig_termios, __baset;
     59  1.2      cgd static struct termios cbreakt, rawt, *curt;
     60  1.1  mycroft static int useraw;
     61  1.1  mycroft 
     62  1.2      cgd #ifndef	OXTABS
     63  1.2      cgd #ifdef	XTABS			/* SMI uses XTABS. */
     64  1.2      cgd #define	OXTABS	XTABS
     65  1.2      cgd #else
     66  1.2      cgd #define	OXTABS	0
     67  1.2      cgd #endif
     68  1.2      cgd #endif
     69  1.2      cgd 
     70  1.1  mycroft /*
     71  1.1  mycroft  * gettmode --
     72  1.1  mycroft  *	Do terminal type initialization.
     73  1.1  mycroft  */
     74  1.1  mycroft int
     75  1.1  mycroft gettmode()
     76  1.1  mycroft {
     77  1.2      cgd 	useraw = 0;
     78  1.2      cgd 
     79  1.2      cgd 	if (tcgetattr(STDIN_FILENO, &__orig_termios))
     80  1.2      cgd 		return (ERR);
     81  1.2      cgd 
     82  1.2      cgd 	__baset = __orig_termios;
     83  1.2      cgd 	__baset.c_oflag &= ~OXTABS;
     84  1.1  mycroft 
     85  1.2      cgd 	GT = 0;		/* historical. was used before we wired OXTABS off */
     86  1.2      cgd 	NONL = (__baset.c_oflag & ONLCR) == 0;
     87  1.1  mycroft 
     88  1.2      cgd 	/*
     89  1.2      cgd 	 * XXX
     90  1.2      cgd 	 * System V and SMI systems overload VMIN and VTIME, such that
     91  1.2      cgd 	 * VMIN is the same as the VEOF element, and VTIME is the same
     92  1.2      cgd 	 * as the VEOL element.  This means that, if VEOF was ^D, the
     93  1.2      cgd 	 * default VMIN is 4.  Majorly stupid.
     94  1.2      cgd 	 */
     95  1.2      cgd 	cbreakt = __baset;
     96  1.2      cgd 	cbreakt.c_lflag &= ~ICANON;
     97  1.2      cgd 	cbreakt.c_cc[VMIN] = 1;
     98  1.2      cgd 	cbreakt.c_cc[VTIME] = 0;
     99  1.2      cgd 
    100  1.2      cgd 	rawt = cbreakt;
    101  1.2      cgd 	rawt.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|INLCR|IGNCR|ICRNL|IXON);
    102  1.2      cgd 	rawt.c_oflag &= ~OPOST;
    103  1.2      cgd 	rawt.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
    104  1.2      cgd 
    105  1.2      cgd 	/*
    106  1.2      cgd 	 * In general, curses should leave hardware-related settings alone.
    107  1.2      cgd 	 * This includes parity and word size.  Older versions set the tty
    108  1.2      cgd 	 * to 8 bits, no parity in raw(), but this is considered to be an
    109  1.2      cgd 	 * artifact of the old tty interface.  If it's desired to change
    110  1.2      cgd 	 * parity and word size, the TCSASOFT bit has to be removed from the
    111  1.2      cgd 	 * calls that switch to/from "raw" mode.
    112  1.2      cgd 	 */
    113  1.2      cgd 	if (!__tcaction) {
    114  1.2      cgd 		rawt.c_iflag &= ~ISTRIP;
    115  1.2      cgd 		rawt.c_cflag &= ~(CSIZE|PARENB);
    116  1.2      cgd 		rawt.c_cflag |= CS8;
    117  1.2      cgd 	}
    118  1.1  mycroft 
    119  1.2      cgd 	curt = &__baset;
    120  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    121  1.2      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    122  1.1  mycroft }
    123  1.1  mycroft 
    124  1.1  mycroft int
    125  1.1  mycroft raw()
    126  1.1  mycroft {
    127  1.1  mycroft 	useraw = __pfast = __rawmode = 1;
    128  1.2      cgd 	curt = &rawt;
    129  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    130  1.2      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt));
    131  1.1  mycroft }
    132  1.1  mycroft 
    133  1.1  mycroft int
    134  1.1  mycroft noraw()
    135  1.1  mycroft {
    136  1.1  mycroft 	useraw = __pfast = __rawmode = 0;
    137  1.2      cgd 	curt = &__baset;
    138  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    139  1.2      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt));
    140  1.1  mycroft }
    141  1.1  mycroft 
    142  1.1  mycroft int
    143  1.1  mycroft cbreak()
    144  1.1  mycroft {
    145  1.1  mycroft 
    146  1.1  mycroft 	__rawmode = 1;
    147  1.2      cgd 	curt = useraw ? &rawt : &cbreakt;
    148  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    149  1.2      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt));
    150  1.1  mycroft }
    151  1.1  mycroft 
    152  1.1  mycroft int
    153  1.1  mycroft nocbreak()
    154  1.1  mycroft {
    155  1.1  mycroft 
    156  1.1  mycroft 	__rawmode = 0;
    157  1.2      cgd 	curt = useraw ? &rawt : &__baset;
    158  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    159  1.2      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt));
    160  1.1  mycroft }
    161  1.1  mycroft 
    162  1.1  mycroft int
    163  1.1  mycroft echo()
    164  1.1  mycroft {
    165  1.1  mycroft 	rawt.c_lflag |= ECHO;
    166  1.2      cgd 	cbreakt.c_lflag |= ECHO;
    167  1.2      cgd 	__baset.c_lflag |= ECHO;
    168  1.1  mycroft 
    169  1.1  mycroft 	__echoit = 1;
    170  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    171  1.2      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt));
    172  1.1  mycroft }
    173  1.1  mycroft 
    174  1.1  mycroft int
    175  1.1  mycroft noecho()
    176  1.1  mycroft {
    177  1.1  mycroft 	rawt.c_lflag &= ~ECHO;
    178  1.2      cgd 	cbreakt.c_lflag &= ~ECHO;
    179  1.2      cgd 	__baset.c_lflag &= ~ECHO;
    180  1.1  mycroft 
    181  1.1  mycroft 	__echoit = 0;
    182  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    183  1.2      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt));
    184  1.1  mycroft }
    185  1.1  mycroft 
    186  1.1  mycroft int
    187  1.1  mycroft nl()
    188  1.1  mycroft {
    189  1.1  mycroft 	rawt.c_iflag |= ICRNL;
    190  1.1  mycroft 	rawt.c_oflag |= ONLCR;
    191  1.2      cgd 	cbreakt.c_iflag |= ICRNL;
    192  1.2      cgd 	cbreakt.c_oflag |= ONLCR;
    193  1.2      cgd 	__baset.c_iflag |= ICRNL;
    194  1.2      cgd 	__baset.c_oflag |= ONLCR;
    195  1.1  mycroft 
    196  1.1  mycroft 	__pfast = __rawmode;
    197  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    198  1.2      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt));
    199  1.1  mycroft }
    200  1.1  mycroft 
    201  1.1  mycroft int
    202  1.1  mycroft nonl()
    203  1.1  mycroft {
    204  1.1  mycroft 	rawt.c_iflag &= ~ICRNL;
    205  1.1  mycroft 	rawt.c_oflag &= ~ONLCR;
    206  1.2      cgd 	cbreakt.c_iflag &= ~ICRNL;
    207  1.2      cgd 	cbreakt.c_oflag &= ~ONLCR;
    208  1.2      cgd 	__baset.c_iflag &= ~ICRNL;
    209  1.2      cgd 	__baset.c_oflag &= ~ONLCR;
    210  1.1  mycroft 
    211  1.1  mycroft 	__pfast = 1;
    212  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    213  1.2      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt));
    214  1.2      cgd }
    215  1.2      cgd 
    216  1.2      cgd void
    217  1.2      cgd __startwin()
    218  1.2      cgd {
    219  1.2      cgd 	(void)fflush(stdout);
    220  1.2      cgd 	(void)setvbuf(stdout, NULL, _IOFBF, 0);
    221  1.2      cgd 
    222  1.2      cgd 	tputs(TI, 0, __cputchar);
    223  1.2      cgd 	tputs(VS, 0, __cputchar);
    224  1.1  mycroft }
    225  1.1  mycroft 
    226  1.1  mycroft int
    227  1.1  mycroft endwin()
    228  1.1  mycroft {
    229  1.2      cgd 	__restore_stophandler();
    230  1.2      cgd 
    231  1.2      cgd 	if (curscr != NULL) {
    232  1.2      cgd 		if (curscr->flags & __WSTANDOUT) {
    233  1.1  mycroft 			tputs(SE, 0, __cputchar);
    234  1.2      cgd 			curscr->flags &= ~__WSTANDOUT;
    235  1.1  mycroft 		}
    236  1.2      cgd 		__mvcur(curscr->cury, curscr->cury, curscr->maxy - 1, 0, 0);
    237  1.1  mycroft 	}
    238  1.1  mycroft 
    239  1.1  mycroft 	(void)tputs(VE, 0, __cputchar);
    240  1.1  mycroft 	(void)tputs(TE, 0, __cputchar);
    241  1.1  mycroft 	(void)fflush(stdout);
    242  1.2      cgd 	(void)setvbuf(stdout, NULL, _IOLBF, 0);
    243  1.1  mycroft 
    244  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    245  1.2      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, &__orig_termios));
    246  1.1  mycroft }
    247  1.1  mycroft 
    248  1.1  mycroft /*
    249  1.1  mycroft  * The following routines, savetty and resetty are completely useless and
    250  1.1  mycroft  * are left in only as stubs.  If people actually use them they will almost
    251  1.1  mycroft  * certainly screw up the state of the world.
    252  1.1  mycroft  */
    253  1.1  mycroft static struct termios savedtty;
    254  1.1  mycroft int
    255  1.1  mycroft savetty()
    256  1.1  mycroft {
    257  1.1  mycroft 	return (tcgetattr(STDIN_FILENO, &savedtty));
    258  1.1  mycroft }
    259  1.1  mycroft 
    260  1.1  mycroft int
    261  1.1  mycroft resetty()
    262  1.1  mycroft {
    263  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    264  1.2      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, &savedtty));
    265  1.1  mycroft }
    266