xref: /netbsd-src/usr.bin/talk/init_disp.c (revision 68168df687839776c137f90832075bcd1dead31d)
1*68168df6Sjoerg /*	$NetBSD: init_disp.c,v 1.12 2011/09/06 18:32:03 joerg Exp $	*/
251207773Sjtc 
361f28255Scgd /*
451207773Sjtc  * Copyright (c) 1983, 1993
551207773Sjtc  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
1589aaa1bbSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
3232a9e65fSlukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3451207773Sjtc #if 0
3551207773Sjtc static char sccsid[] = "@(#)init_disp.c	8.2 (Berkeley) 2/16/94";
3651207773Sjtc #endif
37*68168df6Sjoerg __RCSID("$NetBSD: init_disp.c,v 1.12 2011/09/06 18:32:03 joerg Exp $");
3861f28255Scgd #endif /* not lint */
3961f28255Scgd 
4061f28255Scgd /*
4161f28255Scgd  * Initialization code for the display package,
4261f28255Scgd  * as well as the signal handling routines.
4361f28255Scgd  */
4461f28255Scgd 
4532a9e65fSlukem #include "talk.h"
4651207773Sjtc #include <sys/ioctl.h>
4751207773Sjtc #include <sys/ioctl_compat.h>
4851207773Sjtc #include <err.h>
4932a9e65fSlukem #include <signal.h>
5032a9e65fSlukem #include <termios.h>
51fcd0fb11Smatt #include <stdlib.h>
5232a9e65fSlukem #include <unistd.h>
5361f28255Scgd 
5461f28255Scgd /*
5561f28255Scgd  * Set up curses, catch the appropriate signals,
5661f28255Scgd  * and build the various windows.
5761f28255Scgd  */
5832a9e65fSlukem void
init_display(void)59*68168df6Sjoerg init_display(void)
6061f28255Scgd {
61719b1920Schristos 	struct sigaction sa;
6261f28255Scgd 
6351207773Sjtc 	if (initscr() == NULL)
6451207773Sjtc 		errx(1, "Terminal type unset or lacking necessary features.");
65719b1920Schristos 	(void)sigaction(SIGTSTP, NULL, &sa);
66719b1920Schristos 	sigaddset(&sa.sa_mask, SIGALRM);
67719b1920Schristos 	(void)sigaction(SIGTSTP, &sa, NULL);
6861f28255Scgd 	curses_initialized = 1;
6961f28255Scgd 	clear();
7061f28255Scgd 	refresh();
7161f28255Scgd 	noecho();
72531ada40Sblymn 	cbreak();
7361f28255Scgd 	signal(SIGINT, sig_sent);
7461f28255Scgd 	signal(SIGPIPE, sig_sent);
7561f28255Scgd 	/* curses takes care of ^Z */
7661f28255Scgd 	my_win.x_nlines = LINES / 2;
7761f28255Scgd 	my_win.x_ncols = COLS;
7861f28255Scgd 	my_win.x_win = newwin(my_win.x_nlines, my_win.x_ncols, 0, 0);
7961f28255Scgd 	scrollok(my_win.x_win, FALSE);
8061f28255Scgd 	wclear(my_win.x_win);
8161f28255Scgd 
8261f28255Scgd 	his_win.x_nlines = LINES / 2 - 1;
8361f28255Scgd 	his_win.x_ncols = COLS;
8461f28255Scgd 	his_win.x_win = newwin(his_win.x_nlines, his_win.x_ncols,
8561f28255Scgd 	    my_win.x_nlines+1, 0);
8661f28255Scgd 	scrollok(his_win.x_win, FALSE);
8761f28255Scgd 	wclear(his_win.x_win);
8861f28255Scgd 
8961f28255Scgd 	line_win = newwin(1, COLS, my_win.x_nlines, 0);
9061f28255Scgd 	box(line_win, '-', '-');
9161f28255Scgd 	wrefresh(line_win);
9261f28255Scgd 	/* let them know we are working on it */
9361f28255Scgd 	current_state = "No connection yet";
9461f28255Scgd }
9561f28255Scgd 
9661f28255Scgd /*
9761f28255Scgd  * Trade edit characters with the other talk. By agreement
9861f28255Scgd  * the first three characters each talk transmits after
9961f28255Scgd  * connection are the three edit characters.
10061f28255Scgd  */
10132a9e65fSlukem void
set_edit_chars(void)102*68168df6Sjoerg set_edit_chars(void)
10361f28255Scgd {
10461f28255Scgd 	char buf[3];
10561f28255Scgd 	int cc;
106330f6713Smycroft 	struct termios tty;
10761f28255Scgd 
108330f6713Smycroft 	tcgetattr(0, &tty);
109330f6713Smycroft 	my_win.cerase = tty.c_cc[VERASE];
110330f6713Smycroft 	my_win.kill = tty.c_cc[VKILL];
1112b12186eScgd 	if (tty.c_cc[VWERASE] == (unsigned char) -1)
11261f28255Scgd 		my_win.werase = '\027';	 /* control W */
11361f28255Scgd 	else
114330f6713Smycroft 		my_win.werase = tty.c_cc[VWERASE];
11561f28255Scgd 	buf[0] = my_win.cerase;
11661f28255Scgd 	buf[1] = my_win.kill;
11761f28255Scgd 	buf[2] = my_win.werase;
11861f28255Scgd 	cc = write(sockt, buf, sizeof(buf));
11961f28255Scgd 	if (cc != sizeof(buf) )
12061f28255Scgd 		p_error("Lost the connection");
12161f28255Scgd 	cc = read(sockt, buf, sizeof(buf));
12261f28255Scgd 	if (cc != sizeof(buf) )
12361f28255Scgd 		p_error("Lost the connection");
12461f28255Scgd 	his_win.cerase = buf[0];
12561f28255Scgd 	his_win.kill = buf[1];
12661f28255Scgd 	his_win.werase = buf[2];
12761f28255Scgd }
12861f28255Scgd 
12961f28255Scgd void
sig_sent(int dummy)130*68168df6Sjoerg sig_sent(int dummy)
13161f28255Scgd {
13261f28255Scgd 
13361f28255Scgd 	message("Connection closing. Exiting");
13461f28255Scgd 	quit();
13561f28255Scgd }
13661f28255Scgd 
13761f28255Scgd /*
13861f28255Scgd  * All done talking...hang up the phone and reset terminal thingy's
13961f28255Scgd  */
14032a9e65fSlukem void
quit(void)141*68168df6Sjoerg quit(void)
14261f28255Scgd {
14361f28255Scgd 
14461f28255Scgd 	if (curses_initialized) {
14561f28255Scgd 		wmove(his_win.x_win, his_win.x_nlines-1, 0);
14661f28255Scgd 		wclrtoeol(his_win.x_win);
14761f28255Scgd 		wrefresh(his_win.x_win);
14861f28255Scgd 		endwin();
14961f28255Scgd 	}
15061f28255Scgd 	if (invitation_waiting)
15161f28255Scgd 		send_delete();
15261f28255Scgd 	exit(0);
15361f28255Scgd }
154