xref: /freebsd/usr.bin/talk/init_disp.c (revision 405c3050f102b8c74782f0366c8ead927bd07b68)
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 #include <sys/cdefs.h>
33 
34 __FBSDID("$FreeBSD$");
35 
36 #ifndef lint
37 static const char sccsid[] = "@(#)init_disp.c	8.2 (Berkeley) 2/16/94";
38 #endif
39 
40 /*
41  * Initialization code for the display package,
42  * as well as the signal handling routines.
43  */
44 
45 #include <sys/stat.h>
46 
47 #include <err.h>
48 #include <signal.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <termios.h>
52 
53 #include "talk.h"
54 
55 /*
56  * Make sure the callee can write to the screen
57  */
58 void
59 check_writeable(void)
60 {
61 	char *tty;
62 	struct stat sb;
63 
64 	if ((tty = ttyname(STDERR_FILENO)) == NULL)
65 		err(1, "ttyname");
66 	if (stat(tty, &sb) < 0)
67 		err(1, "%s", tty);
68 	if (!(sb.st_mode & S_IWGRP))
69 		errx(1, "The callee cannot write to this terminal, use \"mesg y\".");
70 }
71 
72 /*
73  * Set up curses, catch the appropriate signals,
74  * and build the various windows.
75  */
76 void
77 init_display(void)
78 {
79 	struct sigaction sa;
80 
81 	if (initscr() == NULL)
82 		errx(1, "Terminal type unset or lacking necessary features.");
83 	(void) sigaction(SIGTSTP, (struct sigaction *)0, &sa);
84 	sigaddset(&sa.sa_mask, SIGALRM);
85 	(void) sigaction(SIGTSTP, &sa, (struct sigaction *)0);
86 	curses_initialized = 1;
87 	clear();
88 	refresh();
89 	noecho();
90 	crmode();
91 	signal(SIGINT, sig_sent);
92 	signal(SIGPIPE, sig_sent);
93 	signal(SIGWINCH, sig_winch);
94 	/* curses takes care of ^Z */
95 	my_win.x_nlines = LINES / 2;
96 	my_win.x_ncols = COLS;
97 	my_win.x_win = newwin(my_win.x_nlines, my_win.x_ncols, 0, 0);
98 	idlok(my_win.x_win, TRUE);
99 	scrollok(my_win.x_win, TRUE);
100 	wclear(my_win.x_win);
101 
102 	his_win.x_nlines = LINES / 2 - 1;
103 	his_win.x_ncols = COLS;
104 	his_win.x_win = newwin(his_win.x_nlines, his_win.x_ncols,
105 	    my_win.x_nlines+1, 0);
106 	idlok(my_win.x_win, TRUE);
107 	scrollok(his_win.x_win, TRUE);
108 	wclear(his_win.x_win);
109 
110 	line_win = newwin(1, COLS, my_win.x_nlines, 0);
111 #if defined(hline) || defined(whline) || defined(NCURSES_VERSION)
112 	whline(line_win, 0, COLS);
113 #else
114 	box(line_win, '-', '-');
115 #endif
116 	wrefresh(line_win);
117 	/* let them know we are working on it */
118 	current_state = "No connection yet";
119 }
120 
121 /*
122  * Trade edit characters with the other talk. By agreement
123  * the first three characters each talk transmits after
124  * connection are the three edit characters.
125  */
126 void
127 set_edit_chars(void)
128 {
129 	char buf[3];
130 	int cc;
131 	struct termios tio;
132 
133 	tcgetattr(0, &tio);
134 	my_win.cerase = tio.c_cc[VERASE];
135 	my_win.kill = tio.c_cc[VKILL];
136 	my_win.werase = tio.c_cc[VWERASE];
137 	if (my_win.cerase == (char)_POSIX_VDISABLE)
138 		my_win.kill = CERASE;
139 	if (my_win.kill == (char)_POSIX_VDISABLE)
140 		my_win.kill = CKILL;
141 	if (my_win.werase == (char)_POSIX_VDISABLE)
142 		my_win.werase = CWERASE;
143 	buf[0] = my_win.cerase;
144 	buf[1] = my_win.kill;
145 	buf[2] = my_win.werase;
146 	cc = write(sockt, buf, sizeof(buf));
147 	if (cc != sizeof(buf) )
148 		p_error("Lost the connection");
149 	cc = read(sockt, buf, sizeof(buf));
150 	if (cc != sizeof(buf) )
151 		p_error("Lost the connection");
152 	his_win.cerase = buf[0];
153 	his_win.kill = buf[1];
154 	his_win.werase = buf[2];
155 }
156 
157 /* ARGSUSED */
158 void
159 sig_sent(int signo __unused)
160 {
161 
162 	message("Connection closing. Exiting");
163 	quit();
164 }
165 
166 void
167 sig_winch(int dummy __unused)
168 {
169 
170 	gotwinch = 1;
171 }
172 
173 /*
174  * All done talking...hang up the phone and reset terminal thingy's
175  */
176 void
177 quit(void)
178 {
179 
180 	if (curses_initialized) {
181 		wmove(his_win.x_win, his_win.x_nlines-1, 0);
182 		wclrtoeol(his_win.x_win);
183 		wrefresh(his_win.x_win);
184 		endwin();
185 	}
186 	if (invitation_waiting)
187 		send_delete();
188 	exit(0);
189 }
190 
191 /*
192  * If we get SIGWINCH, recompute both window sizes and refresh things.
193  */
194 void
195 resize_display(void)
196 {
197 	struct winsize ws;
198 
199 	if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) < 0 ||
200 	    (ws.ws_row == LINES && ws.ws_col == COLS))
201 		return;
202 
203 	/* Update curses' internal state with new window size. */
204 	resizeterm(ws.ws_row, ws.ws_col);
205 
206 	/*
207 	 * Resize each window but wait to refresh the screen until
208 	 * everything has been drawn so the cursor is in the right spot.
209 	 */
210 	my_win.x_nlines = LINES / 2;
211 	my_win.x_ncols = COLS;
212 	wresize(my_win.x_win, my_win.x_nlines, my_win.x_ncols);
213 	mvwin(my_win.x_win, 0, 0);
214 	clearok(my_win.x_win, TRUE);
215 
216 	his_win.x_nlines = LINES / 2 - 1;
217 	his_win.x_ncols = COLS;
218 	wresize(his_win.x_win, his_win.x_nlines, his_win.x_ncols);
219 	mvwin(his_win.x_win, my_win.x_nlines + 1, 0);
220 	clearok(his_win.x_win, TRUE);
221 
222 	wresize(line_win, 1, COLS);
223 	mvwin(line_win, my_win.x_nlines, 0);
224 #if defined(NCURSES_VERSION) || defined(whline)
225 	whline(line_win, '-', COLS);
226 #else
227 	wmove(line_win, my_win.x_nlines, 0);
228 	box(line_win, '-', '-');
229 #endif
230 
231 	/* Now redraw the screen. */
232 	wrefresh(his_win.x_win);
233 	wrefresh(line_win);
234 	wrefresh(my_win.x_win);
235 }
236