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