xref: /csrg-svn/lib/libcurses/tty.c (revision 60627)
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*60627Sbostic static char sccsid[] = "@(#)tty.c	5.16 (Berkeley) 05/30/93";
1055989Sbostic #endif /* not lint */
1155989Sbostic 
1255989Sbostic #include <sys/ioctl.h>
1355989Sbostic 
1455989Sbostic #include <curses.h>
1555989Sbostic #include <termios.h>
1655989Sbostic #include <unistd.h>
1755989Sbostic 
18*60627Sbostic /*
19*60627Sbostic  * In general, curses should leave tty hardware settings alone (speed, parity,
20*60627Sbostic  * word size).  This is most easily done in BSD by using TCSASOFT on all
21*60627Sbostic  * tcsetattr calls.  On other systems, it would be better to get and restore
22*60627Sbostic  * those attributes at each change, or at least when stopped and restarted.
23*60627Sbostic  * See also the comments in getterm().
24*60627Sbostic  */
25*60627Sbostic #ifdef TCSASOFT
26*60627Sbostic #define	TCACTION (TCSASOFT | TCSADRAIN)		/* ignore hardware settings */
27*60627Sbostic #else
28*60627Sbostic #define	TCACTION  TCSADRAIN
29*60627Sbostic #endif
30*60627Sbostic 
3157716Sbostic struct termios __orig_termios;
32*60627Sbostic static struct termios baset, cbreakt, rawt, *curt;
3355989Sbostic static int useraw;
3455989Sbostic 
3560616Sbostic #ifndef	OXTABS
3660616Sbostic #ifdef	XTABS			/* SMI uses XTABS. */
3760616Sbostic #define	OXTABS	XTABS
3860616Sbostic #else
3960616Sbostic #define	OXTABS	0
4060616Sbostic #endif
4160616Sbostic #endif
4260616Sbostic 
4355989Sbostic /*
4455989Sbostic  * gettmode --
4555989Sbostic  *	Do terminal type initialization.
4655989Sbostic  */
4755989Sbostic int
4855989Sbostic gettmode()
4955989Sbostic {
5057286Sbostic 	useraw = 0;
5157286Sbostic 
5257716Sbostic 	if (tcgetattr(STDIN_FILENO, &__orig_termios))
5357472Sbostic 		return (ERR);
5455989Sbostic 
5557716Sbostic 	GT = (__orig_termios.c_oflag & OXTABS) == 0;
5657716Sbostic 	NONL = (__orig_termios.c_oflag & ONLCR) == 0;
5755989Sbostic 
58*60627Sbostic 	baset = __orig_termios;
59*60627Sbostic 	baset.c_oflag &= ~OXTABS;
6060619Sbostic 
6160619Sbostic 	/*
6260619Sbostic 	 * XXX
6360619Sbostic 	 * System V and SMI systems overload VMIN and VTIME, such that
6460619Sbostic 	 * VMIN is the same as the VEOF element, and VTIME is the same
6560619Sbostic 	 * as the VEOL element.  This means that, if VEOF was ^D, the
6660619Sbostic 	 * default VMIN is 4.  Majorly stupid.
6760619Sbostic 	 */
68*60627Sbostic 	cbreakt = baset;
69*60627Sbostic 	cbreakt.c_lflag &= ~ICANON;
70*60627Sbostic 	cbreakt.c_cc[VMIN] = 1;
71*60627Sbostic 	cbreakt.c_cc[VTIME] = 0;
7255989Sbostic 
73*60627Sbostic 	rawt = cbreakt;
74*60627Sbostic 	rawt.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|INLCR|IGNCR|ICRNL|IXON);
75*60627Sbostic 	rawt.c_oflag &= ~OPOST;
76*60627Sbostic 	rawt.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
77*60627Sbostic #if 0
78*60627Sbostic 	/*
79*60627Sbostic 	 * In general, curses should leave hardware-related settings alone.
80*60627Sbostic 	 * This includes parity and word size.  Older versions set the tty
81*60627Sbostic 	 * to 8 bits, no parity in raw(), but this is considered to be an
82*60627Sbostic 	 * artifact of the old tty interface.  If it's desired to change
83*60627Sbostic 	 * parity and word size, the TCSASOFT bit would have to be removed
84*60627Sbostic 	 * from the calls that switch to/from "raw" mode.
85*60627Sbostic 	 */
86*60627Sbostic 	rawt.c_iflag &= ~ISTRIP;
87*60627Sbostic 	rawt.c_cflag &= ~(CSIZE|PARENB);
88*60627Sbostic 	rawt.c_cflag |= CS8;
89*60627Sbostic #endif
90*60627Sbostic 
91*60627Sbostic 	curt = &baset;
92*60627Sbostic 	return (tcsetattr(STDIN_FILENO, TCACTION, &baset) ? ERR : OK);
9355989Sbostic }
9455989Sbostic 
9555989Sbostic int
9655989Sbostic raw()
9755989Sbostic {
9855989Sbostic 	useraw = __pfast = __rawmode = 1;
99*60627Sbostic 	curt = &rawt;
100*60627Sbostic 	return (tcsetattr(STDIN_FILENO, TCACTION, &rawt));
10155989Sbostic }
10255989Sbostic 
10355989Sbostic int
10455989Sbostic noraw()
10555989Sbostic {
10655989Sbostic 	useraw = __pfast = __rawmode = 0;
107*60627Sbostic 	curt = &baset;
108*60627Sbostic 	return (tcsetattr(STDIN_FILENO, TCACTION, &baset));
10955989Sbostic }
11055989Sbostic 
11155989Sbostic int
11255989Sbostic cbreak()
11355989Sbostic {
11455989Sbostic 
11555989Sbostic 	__rawmode = 1;
116*60627Sbostic 	curt = useraw ? &rawt : &cbreakt;
117*60627Sbostic 	return (tcsetattr(STDIN_FILENO, TCACTION, curt));
11855989Sbostic }
11955989Sbostic 
12055989Sbostic int
12155989Sbostic nocbreak()
12255989Sbostic {
12355989Sbostic 
12455989Sbostic 	__rawmode = 0;
125*60627Sbostic 	curt = useraw ? &rawt : &baset;
126*60627Sbostic 	return (tcsetattr(STDIN_FILENO, TCACTION, curt));
12755989Sbostic }
12855989Sbostic 
12955989Sbostic int
13055989Sbostic echo()
13155989Sbostic {
13255989Sbostic 	rawt.c_lflag |= ECHO;
133*60627Sbostic 	cbreakt.c_lflag |= ECHO;
134*60627Sbostic 	baset.c_lflag |= ECHO;
13555989Sbostic 
13655989Sbostic 	__echoit = 1;
137*60627Sbostic 	return (tcsetattr(STDIN_FILENO, TCACTION, curt));
13855989Sbostic }
13955989Sbostic 
14055989Sbostic int
14155989Sbostic noecho()
14255989Sbostic {
14355989Sbostic 	rawt.c_lflag &= ~ECHO;
144*60627Sbostic 	cbreakt.c_lflag &= ~ECHO;
145*60627Sbostic 	baset.c_lflag &= ~ECHO;
14655989Sbostic 
14755989Sbostic 	__echoit = 0;
148*60627Sbostic 	return (tcsetattr(STDIN_FILENO, TCACTION, curt));
14955989Sbostic }
15055989Sbostic 
15155989Sbostic int
15255989Sbostic nl()
15355989Sbostic {
15455989Sbostic 	rawt.c_iflag |= ICRNL;
15555989Sbostic 	rawt.c_oflag |= ONLCR;
156*60627Sbostic 	cbreakt.c_iflag |= ICRNL;
157*60627Sbostic 	cbreakt.c_oflag |= ONLCR;
158*60627Sbostic 	baset.c_iflag |= ICRNL;
159*60627Sbostic 	baset.c_oflag |= ONLCR;
16055989Sbostic 
16155989Sbostic 	__pfast = __rawmode;
162*60627Sbostic 	return (tcsetattr(STDIN_FILENO, TCACTION, curt));
16355989Sbostic }
16455989Sbostic 
16555989Sbostic int
16655989Sbostic nonl()
16755989Sbostic {
16855989Sbostic 	rawt.c_iflag &= ~ICRNL;
16955989Sbostic 	rawt.c_oflag &= ~ONLCR;
170*60627Sbostic 	cbreakt.c_iflag &= ~ICRNL;
171*60627Sbostic 	cbreakt.c_oflag &= ~ONLCR;
172*60627Sbostic 	baset.c_iflag &= ~ICRNL;
173*60627Sbostic 	baset.c_oflag &= ~ONLCR;
17455989Sbostic 
17555989Sbostic 	__pfast = 1;
176*60627Sbostic 	return (tcsetattr(STDIN_FILENO, TCACTION, curt));
17755989Sbostic }
17855989Sbostic 
17959862Sbostic void
18059862Sbostic __startwin()
18159862Sbostic {
18259862Sbostic 	(void)fflush(stdout);
18359862Sbostic 	(void)setvbuf(stdout, NULL, _IOFBF, 0);
18459862Sbostic 
18559862Sbostic 	tputs(TI, 0, __cputchar);
18659862Sbostic 	tputs(VS, 0, __cputchar);
18759862Sbostic }
18859862Sbostic 
18955989Sbostic int
19055989Sbostic endwin()
19155989Sbostic {
19257716Sbostic 	if (curscr != NULL) {
19357716Sbostic 		if (curscr->flags & __WSTANDOUT) {
19457716Sbostic 			tputs(SE, 0, __cputchar);
19557716Sbostic 			curscr->flags &= ~__WSTANDOUT;
19657716Sbostic 		}
19760071Sbostic 		__mvcur(curscr->cury, curscr->cury, curscr->maxy - 1, 0, 0);
19855989Sbostic 	}
19955989Sbostic 
20055989Sbostic 	(void)tputs(VE, 0, __cputchar);
20155989Sbostic 	(void)tputs(TE, 0, __cputchar);
20255989Sbostic 	(void)fflush(stdout);
20359862Sbostic 	(void)setvbuf(stdout, NULL, _IOLBF, 0);
20455989Sbostic 
205*60627Sbostic 	return (tcsetattr(STDIN_FILENO, TCACTION, &__orig_termios));
20655989Sbostic }
20755989Sbostic 
20855989Sbostic /*
20955989Sbostic  * The following routines, savetty and resetty are completely useless and
21055989Sbostic  * are left in only as stubs.  If people actually use them they will almost
21155989Sbostic  * certainly screw up the state of the world.
21255989Sbostic  */
21355989Sbostic static struct termios savedtty;
21455989Sbostic int
21555989Sbostic savetty()
21655989Sbostic {
21755989Sbostic 	return (tcgetattr(STDIN_FILENO, &savedtty));
21855989Sbostic }
21955989Sbostic 
22055989Sbostic int
22155989Sbostic resetty()
22255989Sbostic {
223*60627Sbostic 	return (tcsetattr(STDIN_FILENO, TCACTION, &savedtty));
22455989Sbostic }
225