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