Home | History | Annotate | Line # | Download | only in libcurses
tty.c revision 1.1
      1  1.1  mycroft /*-
      2  1.1  mycroft  * Copyright (c) 1992 The Regents of the University of California.
      3  1.1  mycroft  * 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.1  mycroft /*static char sccsid[] = "from: @(#)tty.c	5.2 (Berkeley) 8/31/92";*/
     36  1.1  mycroft static char rcsid[] = "$Id: tty.c,v 1.1 1993/08/07 05:51:16 mycroft Exp $";
     37  1.1  mycroft #endif /* not lint */
     38  1.1  mycroft 
     39  1.1  mycroft /*
     40  1.1  mycroft  * Terminal initialization routines.
     41  1.1  mycroft  */
     42  1.1  mycroft #include <sys/ioctl.h>
     43  1.1  mycroft 
     44  1.1  mycroft #include <curses.h>
     45  1.1  mycroft #include <termios.h>
     46  1.1  mycroft #include <unistd.h>
     47  1.1  mycroft 
     48  1.1  mycroft struct termios newtermio, origtermio;
     49  1.1  mycroft static struct termios norawt, rawt;
     50  1.1  mycroft static int useraw;
     51  1.1  mycroft 
     52  1.1  mycroft /*
     53  1.1  mycroft  * gettmode --
     54  1.1  mycroft  *	Do terminal type initialization.
     55  1.1  mycroft  */
     56  1.1  mycroft int
     57  1.1  mycroft gettmode()
     58  1.1  mycroft {
     59  1.1  mycroft 	if (tcgetattr(STDIN_FILENO, &origtermio))
     60  1.1  mycroft 		return (OK);
     61  1.1  mycroft 
     62  1.1  mycroft 	GT = (origtermio.c_oflag & OXTABS) == 0;
     63  1.1  mycroft 	NONL = (origtermio.c_oflag & ONLCR) == 0;
     64  1.1  mycroft 
     65  1.1  mycroft 	norawt = origtermio;
     66  1.1  mycroft 	norawt.c_oflag &= ~OXTABS;
     67  1.1  mycroft 	rawt = norawt;
     68  1.1  mycroft 	cfmakeraw(&rawt);
     69  1.1  mycroft 
     70  1.1  mycroft 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt) ? ERR : OK);
     71  1.1  mycroft }
     72  1.1  mycroft 
     73  1.1  mycroft int
     74  1.1  mycroft raw()
     75  1.1  mycroft {
     76  1.1  mycroft 	useraw = __pfast = __rawmode = 1;
     77  1.1  mycroft 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
     78  1.1  mycroft }
     79  1.1  mycroft 
     80  1.1  mycroft int
     81  1.1  mycroft noraw()
     82  1.1  mycroft {
     83  1.1  mycroft 	useraw = __pfast = __rawmode = 0;
     84  1.1  mycroft 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
     85  1.1  mycroft }
     86  1.1  mycroft 
     87  1.1  mycroft int
     88  1.1  mycroft cbreak()
     89  1.1  mycroft {
     90  1.1  mycroft 	rawt.c_lflag &= ~ICANON;
     91  1.1  mycroft 	norawt.c_lflag &= ~ICANON;
     92  1.1  mycroft 
     93  1.1  mycroft 	__rawmode = 1;
     94  1.1  mycroft 	if (useraw)
     95  1.1  mycroft 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
     96  1.1  mycroft 	else
     97  1.1  mycroft 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
     98  1.1  mycroft }
     99  1.1  mycroft 
    100  1.1  mycroft int
    101  1.1  mycroft nocbreak()
    102  1.1  mycroft {
    103  1.1  mycroft 	rawt.c_lflag |= ICANON;
    104  1.1  mycroft 	norawt.c_lflag |= ICANON;
    105  1.1  mycroft 
    106  1.1  mycroft 	__rawmode = 0;
    107  1.1  mycroft 	if (useraw)
    108  1.1  mycroft 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
    109  1.1  mycroft 	else
    110  1.1  mycroft 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
    111  1.1  mycroft }
    112  1.1  mycroft 
    113  1.1  mycroft int
    114  1.1  mycroft echo()
    115  1.1  mycroft {
    116  1.1  mycroft 	rawt.c_lflag |= ECHO;
    117  1.1  mycroft 	norawt.c_lflag |= ECHO;
    118  1.1  mycroft 
    119  1.1  mycroft 	__echoit = 1;
    120  1.1  mycroft 	if (useraw)
    121  1.1  mycroft 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
    122  1.1  mycroft 	else
    123  1.1  mycroft 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
    124  1.1  mycroft }
    125  1.1  mycroft 
    126  1.1  mycroft int
    127  1.1  mycroft noecho()
    128  1.1  mycroft {
    129  1.1  mycroft 	rawt.c_lflag &= ~ECHO;
    130  1.1  mycroft 	norawt.c_lflag &= ~ECHO;
    131  1.1  mycroft 
    132  1.1  mycroft 	__echoit = 0;
    133  1.1  mycroft 	if (useraw)
    134  1.1  mycroft 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
    135  1.1  mycroft 	else
    136  1.1  mycroft 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
    137  1.1  mycroft }
    138  1.1  mycroft 
    139  1.1  mycroft int
    140  1.1  mycroft nl()
    141  1.1  mycroft {
    142  1.1  mycroft 	rawt.c_iflag |= ICRNL;
    143  1.1  mycroft 	rawt.c_oflag |= ONLCR;
    144  1.1  mycroft 	norawt.c_iflag |= ICRNL;
    145  1.1  mycroft 	norawt.c_oflag |= ONLCR;
    146  1.1  mycroft 
    147  1.1  mycroft 	__pfast = __rawmode;
    148  1.1  mycroft 	if (useraw)
    149  1.1  mycroft 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
    150  1.1  mycroft 	else
    151  1.1  mycroft 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
    152  1.1  mycroft }
    153  1.1  mycroft 
    154  1.1  mycroft int
    155  1.1  mycroft nonl()
    156  1.1  mycroft {
    157  1.1  mycroft 	rawt.c_iflag &= ~ICRNL;
    158  1.1  mycroft 	rawt.c_oflag &= ~ONLCR;
    159  1.1  mycroft 	norawt.c_iflag &= ~ICRNL;
    160  1.1  mycroft 	norawt.c_oflag &= ~ONLCR;
    161  1.1  mycroft 
    162  1.1  mycroft 	__pfast = 1;
    163  1.1  mycroft 	if (useraw)
    164  1.1  mycroft 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
    165  1.1  mycroft 	else
    166  1.1  mycroft 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
    167  1.1  mycroft }
    168  1.1  mycroft 
    169  1.1  mycroft int
    170  1.1  mycroft endwin()
    171  1.1  mycroft {
    172  1.1  mycroft 	if (curscr) {
    173  1.1  mycroft 		if (curscr->_flags & _STANDOUT) {
    174  1.1  mycroft 			tputs(SE, 0, __cputchar);
    175  1.1  mycroft 			curscr->_flags &= ~_STANDOUT;
    176  1.1  mycroft 		}
    177  1.1  mycroft 		__endwin = 1;
    178  1.1  mycroft 	}
    179  1.1  mycroft 
    180  1.1  mycroft 	(void)tputs(VE, 0, __cputchar);
    181  1.1  mycroft 	(void)tputs(TE, 0, __cputchar);
    182  1.1  mycroft 	(void)fflush(stdout);
    183  1.1  mycroft 
    184  1.1  mycroft 	__echoit = origtermio.c_lflag & ECHO;
    185  1.1  mycroft 	__rawmode = origtermio.c_lflag & ICANON;
    186  1.1  mycroft 	__pfast = origtermio.c_iflag & ICRNL ? __rawmode : 1;
    187  1.1  mycroft 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &origtermio));
    188  1.1  mycroft }
    189  1.1  mycroft 
    190  1.1  mycroft /*
    191  1.1  mycroft  * The following routines, savetty and resetty are completely useless and
    192  1.1  mycroft  * are left in only as stubs.  If people actually use them they will almost
    193  1.1  mycroft  * certainly screw up the state of the world.
    194  1.1  mycroft  */
    195  1.1  mycroft static struct termios savedtty;
    196  1.1  mycroft int
    197  1.1  mycroft savetty()
    198  1.1  mycroft {
    199  1.1  mycroft 	return (tcgetattr(STDIN_FILENO, &savedtty));
    200  1.1  mycroft }
    201  1.1  mycroft 
    202  1.1  mycroft int
    203  1.1  mycroft resetty()
    204  1.1  mycroft {
    205  1.1  mycroft 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &savedtty));
    206  1.1  mycroft }
    207