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