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