xref: /csrg-svn/lib/libcurses/tty.c (revision 57716)
155989Sbostic /*-
255989Sbostic  * Copyright (c) 1992 The Regents of the University of California.
355989Sbostic  * All rights reserved.
455989Sbostic  *
555989Sbostic  * %sccs.include.redist.c%
655989Sbostic  */
755989Sbostic 
855989Sbostic #ifndef lint
9*57716Sbostic static char sccsid[] = "@(#)tty.c	5.9 (Berkeley) 01/24/93";
1055989Sbostic #endif /* not lint */
1155989Sbostic 
1255989Sbostic /*
1355989Sbostic  * Terminal initialization routines.
1455989Sbostic  */
1555989Sbostic #include <sys/ioctl.h>
1655989Sbostic 
1755989Sbostic #include <curses.h>
1855989Sbostic #include <termios.h>
1955989Sbostic #include <unistd.h>
2055989Sbostic 
21*57716Sbostic struct termios __orig_termios;
2255989Sbostic static struct termios norawt, rawt;
2355989Sbostic static int useraw;
2455989Sbostic 
2555989Sbostic /*
2655989Sbostic  * gettmode --
2755989Sbostic  *	Do terminal type initialization.
2855989Sbostic  */
2955989Sbostic int
3055989Sbostic gettmode()
3155989Sbostic {
3257286Sbostic 	useraw = 0;
3357286Sbostic 
34*57716Sbostic 	if (tcgetattr(STDIN_FILENO, &__orig_termios))
3557472Sbostic 		return (ERR);
3655989Sbostic 
37*57716Sbostic 	GT = (__orig_termios.c_oflag & OXTABS) == 0;
38*57716Sbostic 	NONL = (__orig_termios.c_oflag & ONLCR) == 0;
3955989Sbostic 
40*57716Sbostic 	norawt = __orig_termios;
4155989Sbostic 	norawt.c_oflag &= ~OXTABS;
4255989Sbostic 	rawt = norawt;
4355989Sbostic 	cfmakeraw(&rawt);
4455989Sbostic 
4557472Sbostic 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt) ? ERR : OK);
4655989Sbostic }
4755989Sbostic 
4855989Sbostic int
4955989Sbostic raw()
5055989Sbostic {
5155989Sbostic 	useraw = __pfast = __rawmode = 1;
5255989Sbostic 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
5355989Sbostic }
5455989Sbostic 
5555989Sbostic int
5655989Sbostic noraw()
5755989Sbostic {
5855989Sbostic 	useraw = __pfast = __rawmode = 0;
5955989Sbostic 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
6055989Sbostic }
6155989Sbostic 
6255989Sbostic int
6355989Sbostic cbreak()
6455989Sbostic {
6555989Sbostic 	rawt.c_lflag &= ~ICANON;
6655989Sbostic 	norawt.c_lflag &= ~ICANON;
6755989Sbostic 
6855989Sbostic 	__rawmode = 1;
6955989Sbostic 	if (useraw)
7055989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
7155989Sbostic 	else
7255989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
7355989Sbostic }
7455989Sbostic 
7555989Sbostic int
7655989Sbostic nocbreak()
7755989Sbostic {
7855989Sbostic 	rawt.c_lflag |= ICANON;
7955989Sbostic 	norawt.c_lflag |= ICANON;
8055989Sbostic 
8155989Sbostic 	__rawmode = 0;
8255989Sbostic 	if (useraw)
8355989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
8455989Sbostic 	else
8555989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
8655989Sbostic }
8755989Sbostic 
8855989Sbostic int
8955989Sbostic echo()
9055989Sbostic {
9155989Sbostic 	rawt.c_lflag |= ECHO;
9255989Sbostic 	norawt.c_lflag |= ECHO;
9355989Sbostic 
9455989Sbostic 	__echoit = 1;
9555989Sbostic 	if (useraw)
9655989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
9755989Sbostic 	else
9855989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
9955989Sbostic }
10055989Sbostic 
10155989Sbostic int
10255989Sbostic noecho()
10355989Sbostic {
10455989Sbostic 	rawt.c_lflag &= ~ECHO;
10555989Sbostic 	norawt.c_lflag &= ~ECHO;
10655989Sbostic 
10755989Sbostic 	__echoit = 0;
10855989Sbostic 	if (useraw)
10955989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
11055989Sbostic 	else
11155989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
11255989Sbostic }
11355989Sbostic 
11455989Sbostic int
11555989Sbostic nl()
11655989Sbostic {
11755989Sbostic 	rawt.c_iflag |= ICRNL;
11855989Sbostic 	rawt.c_oflag |= ONLCR;
11956115Selan 	norawt.c_iflag |= ICRNL;
12056115Selan 	norawt.c_oflag |= ONLCR;
12155989Sbostic 
12255989Sbostic 	__pfast = __rawmode;
12355989Sbostic 	if (useraw)
12455989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
12555989Sbostic 	else
12655989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
12755989Sbostic }
12855989Sbostic 
12955989Sbostic int
13055989Sbostic nonl()
13155989Sbostic {
13255989Sbostic 	rawt.c_iflag &= ~ICRNL;
13355989Sbostic 	rawt.c_oflag &= ~ONLCR;
13456115Selan 	norawt.c_iflag &= ~ICRNL;
13556115Selan 	norawt.c_oflag &= ~ONLCR;
13655989Sbostic 
13755989Sbostic 	__pfast = 1;
13855989Sbostic 	if (useraw)
13955989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
14055989Sbostic 	else
14155989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
14255989Sbostic }
14355989Sbostic 
14455989Sbostic int
14555989Sbostic endwin()
14655989Sbostic {
147*57716Sbostic 	if (curscr != NULL) {
148*57716Sbostic 		if (curscr->flags & __WSTANDOUT) {
149*57716Sbostic 			tputs(SE, 0, __cputchar);
150*57716Sbostic 			curscr->flags &= ~__WSTANDOUT;
151*57716Sbostic 		}
152*57716Sbostic 		mvcur(curscr->cury, curscr->cury, curscr->maxy - 1, 0);
15355989Sbostic 	}
15455989Sbostic 
15555989Sbostic 	(void)tputs(VE, 0, __cputchar);
15655989Sbostic 	(void)tputs(TE, 0, __cputchar);
15755989Sbostic 	(void)fflush(stdout);
15855989Sbostic 
159*57716Sbostic 	__echoit = __orig_termios.c_lflag & ECHO;
160*57716Sbostic 	__rawmode = __orig_termios.c_lflag & ICANON;
161*57716Sbostic 	__pfast = __orig_termios.c_iflag & ICRNL ? __rawmode : 1;
162*57716Sbostic 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &__orig_termios));
16355989Sbostic }
16455989Sbostic 
165*57716Sbostic void
166*57716Sbostic __startwin()
167*57716Sbostic {
168*57716Sbostic 	tputs(TI, 0, __cputchar);
169*57716Sbostic 	tputs(VS, 0, __cputchar);
170*57716Sbostic }
171*57716Sbostic 
17255989Sbostic /*
17355989Sbostic  * The following routines, savetty and resetty are completely useless and
17455989Sbostic  * are left in only as stubs.  If people actually use them they will almost
17555989Sbostic  * certainly screw up the state of the world.
17655989Sbostic  */
17755989Sbostic static struct termios savedtty;
17855989Sbostic int
17955989Sbostic savetty()
18055989Sbostic {
18155989Sbostic 	return (tcgetattr(STDIN_FILENO, &savedtty));
18255989Sbostic }
18355989Sbostic 
18455989Sbostic int
18555989Sbostic resetty()
18655989Sbostic {
18755989Sbostic 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &savedtty));
18855989Sbostic }
189