xref: /freebsd/usr.bin/talk/display.c (revision 06c3fb27)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 
33 
34 /*
35  * The window 'manager', initializes curses and handles the actual
36  * displaying of text
37  */
38 #include <ctype.h>
39 #include <unistd.h>
40 #include <wctype.h>
41 #define _XOPEN_SOURCE_EXTENDED
42 #include <curses.h>
43 
44 #include "talk.h"
45 
46 void	display(xwin_t *, wchar_t *);
47 
48 xwin_t	my_win;
49 xwin_t	his_win;
50 WINDOW	*line_win;
51 
52 int	curses_initialized = 0;
53 
54 /*
55  * max HAS to be a function, it is called with
56  * an argument of the form --foo at least once.
57  */
58 int
59 max(int a, int b)
60 {
61 
62 	return (a > b ? a : b);
63 }
64 
65 static cchar_t *
66 makecchar(wchar_t in)
67 {
68 	static cchar_t cc;
69 	wchar_t wc[2];
70 
71 	wc[0] = in;
72 	wc[1] = L'\0';
73 
74 	if (setcchar(&cc, wc, A_NORMAL, 0, NULL) != OK)
75 		p_error("settchar(3) failure");
76 
77 	return (&cc);
78 }
79 
80 /*
81  * Display a symbol on somebody's window, processing some control
82  * characters while we are at it.
83  */
84 void
85 display(xwin_t *win, wchar_t *wc)
86 {
87 
88 	/*
89 	 * Alas, can't use variables in C switch statement.
90 	 * Workaround these 3 cases with goto.
91 	 */
92 	if (*wc == win->kill)
93 		goto kill;
94 	else if (*wc == win->cerase)
95 		goto cerase;
96 	else if (*wc == win->werase)
97 		goto werase;
98 
99 	switch (*wc) {
100 	case L'\n':
101 	case L'\r':
102 		wadd_wch(win->x_win, makecchar(L'\n'));
103 		getyx(win->x_win, win->x_line, win->x_col);
104 		wrefresh(win->x_win);
105 		return;
106 
107 	case 004:
108 		if (win == &my_win) {
109 			/* Ctrl-D clears the screen. */
110 			werase(my_win.x_win);
111 			getyx(my_win.x_win, my_win.x_line, my_win.x_col);
112 			wrefresh(my_win.x_win);
113 			werase(his_win.x_win);
114 			getyx(his_win.x_win, his_win.x_line, his_win.x_col);
115 			wrefresh(his_win.x_win);
116 		}
117 		return;
118 
119 	/* Erase character. */
120 	case 010:	/* BS */
121 	case 0177:	/* DEL */
122 cerase:
123 		wmove(win->x_win, win->x_line, max(--win->x_col, 0));
124 		getyx(win->x_win, win->x_line, win->x_col);
125 		waddch(win->x_win, ' ');
126 		wmove(win->x_win, win->x_line, win->x_col);
127 		getyx(win->x_win, win->x_line, win->x_col);
128 		wrefresh(win->x_win);
129 		return;
130 
131 	case 027:	/* ^W */
132 werase:
133 	    {
134 		/*
135 		 * On word erase search backwards until we find
136 		 * the beginning of a word or the beginning of
137 		 * the line.
138 		 */
139 		int endcol, xcol, c;
140 
141 		endcol = win->x_col;
142 		xcol = endcol - 1;
143 		while (xcol >= 0) {
144 			c = readwin(win->x_win, win->x_line, xcol);
145 			if (c != ' ')
146 				break;
147 			xcol--;
148 		}
149 		while (xcol >= 0) {
150 			c = readwin(win->x_win, win->x_line, xcol);
151 			if (c == ' ')
152 				break;
153 			xcol--;
154 		}
155 		wmove(win->x_win, win->x_line, xcol + 1);
156 		for (int i = xcol + 1; i < endcol; i++)
157 			waddch(win->x_win, ' ');
158 		wmove(win->x_win, win->x_line, xcol + 1);
159 		getyx(win->x_win, win->x_line, win->x_col);
160 		wrefresh(win->x_win);
161 		return;
162 	    }
163 
164 	case 025:	/* ^U */
165 kill:
166 		wmove(win->x_win, win->x_line, 0);
167 		wclrtoeol(win->x_win);
168 		getyx(win->x_win, win->x_line, win->x_col);
169 		wrefresh(win->x_win);
170 		return;
171 
172 	case L'\f':
173 		if (win == &my_win)
174 			wrefresh(curscr);
175 		return;
176 
177 	case L'\7':
178 		write(STDOUT_FILENO, wc, sizeof(*wc));
179 		return;
180 	}
181 
182 
183 	if (iswprint(*wc) || *wc == L'\t')
184 		wadd_wch(win->x_win, makecchar(*wc));
185 	else
186 		beep();
187 
188 	getyx(win->x_win, win->x_line, win->x_col);
189 	wrefresh(win->x_win);
190 }
191 
192 /*
193  * Read the character at the indicated position in win
194  */
195 int
196 readwin(WINDOW *win, int line, int col)
197 {
198 	int oldline, oldcol;
199 	int c;
200 
201 	getyx(win, oldline, oldcol);
202 	wmove(win, line, col);
203 	c = winch(win);
204 	wmove(win, oldline, oldcol);
205 	return (c);
206 }
207