xref: /openbsd-src/usr.bin/talk/display.c (revision 62a742911104f98b9185b2c6b6007d9b1c36396c)
1 /*	$OpenBSD: display.c,v 1.6 1999/03/23 17:00:38 millert Exp $	*/
2 /*	$NetBSD: display.c,v 1.3 1994/12/09 02:14:13 jtc Exp $	*/
3 
4 /*
5  * Copyright (c) 1983, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)display.c	8.1 (Berkeley) 6/6/93";
40 #endif
41 static char rcsid[] = "$OpenBSD: display.c,v 1.6 1999/03/23 17:00:38 millert Exp $";
42 #endif /* not lint */
43 
44 /*
45  * The window 'manager', initializes curses and handles the actual
46  * displaying of text
47  */
48 #include "talk.h"
49 #include <ctype.h>
50 
51 xwin_t	my_win;
52 xwin_t	his_win;
53 WINDOW	*line_win;
54 
55 int	curses_initialized = 0;
56 
57 /*
58  * max HAS to be a function, it is called with
59  * a argument of the form --foo at least once.
60  */
61 int
62 max(a,b)
63 	int a, b;
64 {
65 
66 	return (a > b ? a : b);
67 }
68 
69 /*
70  * Display some text on somebody's window, processing some control
71  * characters while we are at it.
72  */
73 void
74 display(win, text, size)
75 	register xwin_t *win;
76 	register char *text;
77 	int size;
78 {
79 	register int i;
80 	char cch;
81 
82 	for (i = 0; i < size; i++) {
83 		/*
84 		 * Since we do not use curses's input routines we must
85 		 * convert '\r' -> '\n' ourselves.
86 		 */
87 		if (*text == '\r')
88 			*text = '\n';
89 		if (*text == '\n') {
90 			xscroll(win, 0);
91 			text++;
92 			continue;
93 		}
94 		/* erase character */
95 		if (*text == win->cerase) {
96 			wmove(win->x_win, win->x_line, max(--win->x_col, 0));
97 			getyx(win->x_win, win->x_line, win->x_col);
98 			waddch(win->x_win, ' ');
99 			wmove(win->x_win, win->x_line, win->x_col);
100 			getyx(win->x_win, win->x_line, win->x_col);
101 			text++;
102 			continue;
103 		}
104 		/*
105 		 * On word erase search backwards until we find
106 		 * the beginning of a word or the beginning of
107 		 * the line.
108 		 */
109 		if (*text == win->werase) {
110 			int endcol, xcol, i, c;
111 
112 			endcol = win->x_col;
113 			xcol = endcol - 1;
114 			while (xcol >= 0) {
115 				c = readwin(win->x_win, win->x_line, xcol);
116 				if (c != ' ')
117 					break;
118 				xcol--;
119 			}
120 			while (xcol >= 0) {
121 				c = readwin(win->x_win, win->x_line, xcol);
122 				if (c == ' ')
123 					break;
124 				xcol--;
125 			}
126 			wmove(win->x_win, win->x_line, xcol + 1);
127 			for (i = xcol + 1; i < endcol; i++)
128 				waddch(win->x_win, ' ');
129 			wmove(win->x_win, win->x_line, xcol + 1);
130 			getyx(win->x_win, win->x_line, win->x_col);
131 			text++;
132 			continue;
133 		}
134 		/* line kill */
135 		if (*text == win->kill) {
136 			wmove(win->x_win, win->x_line, 0);
137 			wclrtoeol(win->x_win);
138 			getyx(win->x_win, win->x_line, win->x_col);
139 			text++;
140 			continue;
141 		}
142 		if (*text == '\f') {
143 			if (win == &my_win)
144 				wrefresh(curscr);
145 			text++;
146 			continue;
147 		}
148 		if (win->x_col == COLS-1) {
149 			/* check for wraparound */
150 			xscroll(win, 0);
151 		}
152 		if (!isprint(*text) && *text != '\t') {
153 			waddch(win->x_win, '^');
154 			getyx(win->x_win, win->x_line, win->x_col);
155 			if (win->x_col == COLS-1) /* check for wraparound */
156 				xscroll(win, 0);
157 			cch = (*text & 63) + 64;
158 			waddch(win->x_win, cch);
159 		} else
160 			waddch(win->x_win, *text);
161 		getyx(win->x_win, win->x_line, win->x_col);
162 		text++;
163 	}
164 	wrefresh(win->x_win);
165 }
166 
167 /*
168  * Read the character at the indicated position in win
169  */
170 int
171 readwin(win, line, col)
172 	WINDOW *win;
173 	int line, col;
174 {
175 	int oldline, oldcol;
176 	register int c;
177 
178 	getyx(win, oldline, oldcol);
179 	wmove(win, line, col);
180 	c = winch(win);
181 	wmove(win, oldline, oldcol);
182 	return (c);
183 }
184 
185 /*
186  * Scroll a window, blanking out the line following the current line
187  * so that the current position is obvious
188  */
189 void
190 xscroll(win, flag)
191 	register xwin_t *win;
192 	int flag;
193 {
194 
195 	if (flag == -1) {
196 		wmove(win->x_win, 0, 0);
197 		win->x_line = 0;
198 		win->x_col = 0;
199 		return;
200 	}
201 	win->x_line = (win->x_line + 1) % win->x_nlines;
202 	win->x_col = 0;
203 	wmove(win->x_win, win->x_line, win->x_col);
204 	wclrtoeol(win->x_win);
205 	wmove(win->x_win, (win->x_line + 1) % win->x_nlines, win->x_col);
206 	wclrtoeol(win->x_win);
207 	wmove(win->x_win, win->x_line, win->x_col);
208 }
209