xref: /openbsd-src/usr.bin/talk/display.c (revision 5054e3e78af0749a9bb00ba9a024b3ee2d90290f)
1 /*	$OpenBSD: display.c,v 1.15 2009/10/27 23:59:44 deraadt 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 /*
34  * The window 'manager', initializes curses and handles the actual
35  * displaying of text
36  */
37 #include "talk.h"
38 #include <ctype.h>
39 
40 xwin_t	my_win;
41 xwin_t	his_win;
42 WINDOW	*line_win;
43 
44 int	curses_initialized = 0;
45 int	high_print = 0;
46 bool	smooth_scroll = FALSE;
47 
48 /*
49  * max HAS to be a function, it is called with
50  * a argument of the form --foo at least once.
51  */
52 int
53 max(a,b)
54 	int a, b;
55 {
56 
57 	return (a > b ? a : b);
58 }
59 
60 /*
61  * Display some text on somebody's window, processing some control
62  * characters while we are at it.
63  */
64 void
65 display(win, text, size)
66 	xwin_t *win;
67 	char *text;
68 	int size;
69 {
70 	int i;
71 	char cch;
72 
73 	for (i = 0; i < size; i++) {
74 		/*
75 		 * Since we do not use curses's input routines we must
76 		 * convert '\r' -> '\n' ourselves.
77 		 */
78 		if (*text == '\r')
79 			*text = '\n';
80 		if (*text == '\n') {
81 			xscroll(win, 0);
82 			text++;
83 			continue;
84 		}
85 		/* erase character */
86 		if (*text == win->cerase) {
87 			wmove(win->x_win, win->x_line, max(--win->x_col, 0));
88 			getyx(win->x_win, win->x_line, win->x_col);
89 			waddch(win->x_win, ' ');
90 			wmove(win->x_win, win->x_line, win->x_col);
91 			getyx(win->x_win, win->x_line, win->x_col);
92 			text++;
93 			continue;
94 		}
95 		/*
96 		 * On word erase search backwards until we find
97 		 * the beginning of a word or the beginning of
98 		 * the line.
99 		 */
100 		if (*text == win->werase) {
101 			int endcol, xcol, i, c;
102 
103 			endcol = win->x_col;
104 			xcol = endcol - 1;
105 			while (xcol >= 0) {
106 				c = readwin(win->x_win, win->x_line, xcol);
107 				if (c != ' ')
108 					break;
109 				xcol--;
110 			}
111 			while (xcol >= 0) {
112 				c = readwin(win->x_win, win->x_line, xcol);
113 				if (c == ' ')
114 					break;
115 				xcol--;
116 			}
117 			wmove(win->x_win, win->x_line, xcol + 1);
118 			for (i = xcol + 1; i < endcol; i++)
119 				waddch(win->x_win, ' ');
120 			wmove(win->x_win, win->x_line, xcol + 1);
121 			getyx(win->x_win, win->x_line, win->x_col);
122 			text++;
123 			continue;
124 		}
125 		/* line kill */
126 		if (*text == win->kill) {
127 			wmove(win->x_win, win->x_line, 0);
128 			wclrtoeol(win->x_win);
129 			getyx(win->x_win, win->x_line, win->x_col);
130 			text++;
131 			continue;
132 		}
133 		if (*text == '\f') {
134 			if (win == &my_win)
135 				wrefresh(curscr);
136 			text++;
137 			continue;
138 		}
139 		if (win->x_col == COLS-1) {
140 			/* check for wraparound */
141 			xscroll(win, 0);
142 		}
143 		if (*text != '\t' &&
144 		    ((!high_print && !isprint(*text)) || iscntrl(*text))) {
145 			waddch(win->x_win, '^');
146 			getyx(win->x_win, win->x_line, win->x_col);
147 			if (win->x_col == COLS-1) /* check for wraparound */
148 				xscroll(win, 0);
149 			cch = (*text & 63) + 64;
150 			waddch(win->x_win, cch);
151 		} else
152 			waddch(win->x_win, (unsigned char)(*text));
153 		getyx(win->x_win, win->x_line, win->x_col);
154 		text++;
155 	}
156 	wrefresh(win->x_win);
157 }
158 
159 /*
160  * Read the character at the indicated position in win
161  */
162 int
163 readwin(win, line, col)
164 	WINDOW *win;
165 	int line, col;
166 {
167 	int oldline, oldcol;
168 	int c;
169 
170 	getyx(win, oldline, oldcol);
171 	wmove(win, line, col);
172 	c = winch(win);
173 	wmove(win, oldline, oldcol);
174 	return (c);
175 }
176 
177 /*
178  * Scroll a window, blanking out the line following the current line
179  * so that the current position is obvious
180  */
181 void
182 xscroll(win, flag)
183 	xwin_t *win;
184 	int flag;
185 {
186 
187 	if (flag == -1) {
188 		wmove(win->x_win, 0, 0);
189 		win->x_line = 0;
190 		win->x_col = 0;
191 		return;
192 	}
193 	win->x_col = 0;
194 	if (smooth_scroll) {
195 		if (++win->x_line == win->x_nlines) {
196 			--win->x_line;
197 			scroll(win->x_win);
198 		}
199 	} else {
200 		win->x_line = (win->x_line + 1) % win->x_nlines;
201 		wmove(win->x_win, win->x_line, win->x_col);
202 		wclrtoeol(win->x_win);
203 		wmove(win->x_win, (win->x_line + 1) % win->x_nlines,
204 		    win->x_col);
205 		wclrtoeol(win->x_win);
206 	}
207 	wmove(win->x_win, win->x_line, win->x_col);
208 }
209