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