xref: /freebsd/usr.bin/talk/init_disp.c (revision fcb560670601b2a4d87bb31d7531c8dcc37ee71b)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 
32 __FBSDID("$FreeBSD$");
33 
34 #ifndef lint
35 static const char sccsid[] = "@(#)init_disp.c	8.2 (Berkeley) 2/16/94";
36 #endif
37 
38 /*
39  * Initialization code for the display package,
40  * as well as the signal handling routines.
41  */
42 
43 #include <sys/stat.h>
44 
45 #include <err.h>
46 #include <signal.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <termios.h>
50 
51 #include "talk.h"
52 
53 /*
54  * Make sure the callee can write to the screen
55  */
56 void
57 check_writeable(void)
58 {
59 	char *tty;
60 	struct stat sb;
61 
62 	if ((tty = ttyname(STDERR_FILENO)) == NULL)
63 		err(1, "ttyname");
64 	if (stat(tty, &sb) < 0)
65 		err(1, "%s", tty);
66 	if (!(sb.st_mode & S_IWGRP))
67 		errx(1, "The callee cannot write to this terminal, use \"mesg y\".");
68 }
69 
70 /*
71  * Set up curses, catch the appropriate signals,
72  * and build the various windows.
73  */
74 void
75 init_display(void)
76 {
77 	struct sigaction sa;
78 
79 	if (initscr() == NULL)
80 		errx(1, "Terminal type unset or lacking necessary features.");
81 	(void) sigaction(SIGTSTP, (struct sigaction *)0, &sa);
82 	sigaddset(&sa.sa_mask, SIGALRM);
83 	(void) sigaction(SIGTSTP, &sa, (struct sigaction *)0);
84 	curses_initialized = 1;
85 	clear();
86 	refresh();
87 	noecho();
88 	crmode();
89 	signal(SIGINT, sig_sent);
90 	signal(SIGPIPE, sig_sent);
91 	signal(SIGWINCH, sig_winch);
92 	/* curses takes care of ^Z */
93 	my_win.x_nlines = LINES / 2;
94 	my_win.x_ncols = COLS;
95 	my_win.x_win = newwin(my_win.x_nlines, my_win.x_ncols, 0, 0);
96 	idlok(my_win.x_win, TRUE);
97 	scrollok(my_win.x_win, TRUE);
98 	wclear(my_win.x_win);
99 
100 	his_win.x_nlines = LINES / 2 - 1;
101 	his_win.x_ncols = COLS;
102 	his_win.x_win = newwin(his_win.x_nlines, his_win.x_ncols,
103 	    my_win.x_nlines+1, 0);
104 	idlok(my_win.x_win, TRUE);
105 	scrollok(his_win.x_win, TRUE);
106 	wclear(his_win.x_win);
107 
108 	line_win = newwin(1, COLS, my_win.x_nlines, 0);
109 #if defined(hline) || defined(whline) || defined(NCURSES_VERSION)
110 	whline(line_win, 0, COLS);
111 #else
112 	box(line_win, '-', '-');
113 #endif
114 	wrefresh(line_win);
115 	/* let them know we are working on it */
116 	current_state = "No connection yet";
117 }
118 
119 /*
120  * Trade edit characters with the other talk. By agreement
121  * the first three characters each talk transmits after
122  * connection are the three edit characters.
123  */
124 void
125 set_edit_chars(void)
126 {
127 	char buf[3];
128 	int cc;
129 	struct termios tio;
130 
131 	tcgetattr(0, &tio);
132 	my_win.cerase = tio.c_cc[VERASE];
133 	my_win.kill = tio.c_cc[VKILL];
134 	my_win.werase = tio.c_cc[VWERASE];
135 	if (my_win.cerase == (char)_POSIX_VDISABLE)
136 		my_win.kill = CERASE;
137 	if (my_win.kill == (char)_POSIX_VDISABLE)
138 		my_win.kill = CKILL;
139 	if (my_win.werase == (char)_POSIX_VDISABLE)
140 		my_win.werase = CWERASE;
141 	buf[0] = my_win.cerase;
142 	buf[1] = my_win.kill;
143 	buf[2] = my_win.werase;
144 	cc = write(sockt, buf, sizeof(buf));
145 	if (cc != sizeof(buf) )
146 		p_error("Lost the connection");
147 	cc = read(sockt, buf, sizeof(buf));
148 	if (cc != sizeof(buf) )
149 		p_error("Lost the connection");
150 	his_win.cerase = buf[0];
151 	his_win.kill = buf[1];
152 	his_win.werase = buf[2];
153 }
154 
155 /* ARGSUSED */
156 void
157 sig_sent(int signo __unused)
158 {
159 
160 	message("Connection closing. Exiting");
161 	quit();
162 }
163 
164 void
165 sig_winch(int dummy __unused)
166 {
167 
168 	gotwinch = 1;
169 }
170 
171 /*
172  * All done talking...hang up the phone and reset terminal thingy's
173  */
174 void
175 quit(void)
176 {
177 
178 	if (curses_initialized) {
179 		wmove(his_win.x_win, his_win.x_nlines-1, 0);
180 		wclrtoeol(his_win.x_win);
181 		wrefresh(his_win.x_win);
182 		endwin();
183 	}
184 	if (invitation_waiting)
185 		send_delete();
186 	exit(0);
187 }
188 
189 /*
190  * If we get SIGWINCH, recompute both window sizes and refresh things.
191  */
192 void
193 resize_display(void)
194 {
195 	struct winsize ws;
196 
197 	if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) < 0 ||
198 	    (ws.ws_row == LINES && ws.ws_col == COLS))
199 		return;
200 
201 	/* Update curses' internal state with new window size. */
202 	resizeterm(ws.ws_row, ws.ws_col);
203 
204 	/*
205 	 * Resize each window but wait to refresh the screen until
206 	 * everything has been drawn so the cursor is in the right spot.
207 	 */
208 	my_win.x_nlines = LINES / 2;
209 	my_win.x_ncols = COLS;
210 	wresize(my_win.x_win, my_win.x_nlines, my_win.x_ncols);
211 	mvwin(my_win.x_win, 0, 0);
212 	clearok(my_win.x_win, TRUE);
213 
214 	his_win.x_nlines = LINES / 2 - 1;
215 	his_win.x_ncols = COLS;
216 	wresize(his_win.x_win, his_win.x_nlines, his_win.x_ncols);
217 	mvwin(his_win.x_win, my_win.x_nlines + 1, 0);
218 	clearok(his_win.x_win, TRUE);
219 
220 	wresize(line_win, 1, COLS);
221 	mvwin(line_win, my_win.x_nlines, 0);
222 #if defined(NCURSES_VERSION) || defined(whline)
223 	whline(line_win, '-', COLS);
224 #else
225 	wmove(line_win, my_win.x_nlines, 0);
226 	box(line_win, '-', '-');
227 #endif
228 
229 	/* Now redraw the screen. */
230 	wrefresh(his_win.x_win);
231 	wrefresh(line_win);
232 	wrefresh(my_win.x_win);
233 }
234