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