init_disp.c revision 17698
11590Srgrimes/*
21590Srgrimes * Copyright (c) 1983, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
341590Srgrimes#ifndef lint
351590Srgrimesstatic char sccsid[] = "@(#)init_disp.c	8.2 (Berkeley) 2/16/94";
361590Srgrimes#endif /* not lint */
371590Srgrimes
381590Srgrimes/*
391590Srgrimes * Initialization code for the display package,
401590Srgrimes * as well as the signal handling routines.
411590Srgrimes */
421590Srgrimes
435297Sache#include <sys/types.h>
445297Sache#include <sys/stat.h>
4517676Speter#include <sys/termios.h>
4617676Speter#include <sys/ttydefaults.h>
471590Srgrimes
485297Sache#include <unistd.h>
491590Srgrimes#include <signal.h>
501590Srgrimes#include <err.h>
511590Srgrimes#include "talk.h"
521590Srgrimes
538874Srgrimes/*
545297Sache * Make sure the callee can write to the screen
555297Sache */
5614443Sjoergvoid
5714443Sjoergcheck_writeable()
585297Sache{
595297Sache	char *tty;
605297Sache	struct stat sb;
615297Sache
625297Sache	if ((tty = ttyname(STDERR_FILENO)) == NULL)
635297Sache		err(1, "ttyname");
645297Sache	if (stat(tty, &sb) < 0)
655297Sache		err(1, "%s", tty);
665297Sache	if (!(sb.st_mode & S_IWGRP))
675297Sache		errx(1, "The callee cannot write to this terminal, use \"mesg y\".");
685297Sache}
695297Sache
708874Srgrimes/*
711590Srgrimes * Set up curses, catch the appropriate signals,
721590Srgrimes * and build the various windows.
731590Srgrimes */
7414443Sjoergvoid
751590Srgrimesinit_display()
761590Srgrimes{
7717676Speter	struct sigaction sa;
781590Srgrimes
791590Srgrimes	if (initscr() == NULL)
801590Srgrimes		errx(1, "Terminal type unset or lacking necessary features.");
8117676Speter	(void) sigaction(SIGTSTP, (struct sigaction *)0, &sa);
8217676Speter	sigaddset(&sa.sa_mask, SIGALRM);
8317676Speter	(void) sigaction(SIGTSTP, &sa, (struct sigaction *)0);
841590Srgrimes	curses_initialized = 1;
851590Srgrimes	clear();
861590Srgrimes	refresh();
871590Srgrimes	noecho();
881590Srgrimes	crmode();
891590Srgrimes	signal(SIGINT, sig_sent);
901590Srgrimes	signal(SIGPIPE, sig_sent);
911590Srgrimes	/* curses takes care of ^Z */
921590Srgrimes	my_win.x_nlines = LINES / 2;
931590Srgrimes	my_win.x_ncols = COLS;
941590Srgrimes	my_win.x_win = newwin(my_win.x_nlines, my_win.x_ncols, 0, 0);
952929Sache	scrollok(my_win.x_win, TRUE);
961590Srgrimes	wclear(my_win.x_win);
971590Srgrimes
981590Srgrimes	his_win.x_nlines = LINES / 2 - 1;
991590Srgrimes	his_win.x_ncols = COLS;
1001590Srgrimes	his_win.x_win = newwin(his_win.x_nlines, his_win.x_ncols,
1011590Srgrimes	    my_win.x_nlines+1, 0);
1022929Sache	scrollok(his_win.x_win, TRUE);
1031590Srgrimes	wclear(his_win.x_win);
1041590Srgrimes
1051590Srgrimes	line_win = newwin(1, COLS, my_win.x_nlines, 0);
10617698Speter#if defined(hline) || defined(whline) || defined(NCURSES_VERSION)
10717698Speter	whline(line_win, 0, COLS);
10817698Speter#else
10917698Speter	box(line_win, '-', '-');
11017698Speter#endif
1111590Srgrimes	wrefresh(line_win);
1121590Srgrimes	/* let them know we are working on it */
1131590Srgrimes	current_state = "No connection yet";
1141590Srgrimes}
1151590Srgrimes
1161590Srgrimes/*
1171590Srgrimes * Trade edit characters with the other talk. By agreement
1181590Srgrimes * the first three characters each talk transmits after
1191590Srgrimes * connection are the three edit characters.
1201590Srgrimes */
12114443Sjoergvoid
1221590Srgrimesset_edit_chars()
1231590Srgrimes{
1241590Srgrimes	char buf[3];
1251590Srgrimes	int cc;
12617676Speter	struct termios tio;
1278874Srgrimes
12817676Speter	tcgetattr(0, &tio);
12917676Speter	my_win.cerase = tio.c_cc[VERASE];
13017676Speter	my_win.kill = tio.c_cc[VKILL];
13117676Speter	my_win.werase = tio.c_cc[VWERASE];
13217676Speter	if (my_win.cerase == (char)_POSIX_VDISABLE)
13317676Speter		my_win.kill = CERASE;
13417676Speter	if (my_win.kill == (char)_POSIX_VDISABLE)
13517676Speter		my_win.kill = CKILL;
13617676Speter	if (my_win.werase == (char)_POSIX_VDISABLE)
13717676Speter		my_win.werase = CWERASE;
1381590Srgrimes	buf[0] = my_win.cerase;
1391590Srgrimes	buf[1] = my_win.kill;
1401590Srgrimes	buf[2] = my_win.werase;
1411590Srgrimes	cc = write(sockt, buf, sizeof(buf));
1421590Srgrimes	if (cc != sizeof(buf) )
1431590Srgrimes		p_error("Lost the connection");
1441590Srgrimes	cc = read(sockt, buf, sizeof(buf));
1451590Srgrimes	if (cc != sizeof(buf) )
1461590Srgrimes		p_error("Lost the connection");
1471590Srgrimes	his_win.cerase = buf[0];
1481590Srgrimes	his_win.kill = buf[1];
1491590Srgrimes	his_win.werase = buf[2];
1501590Srgrimes}
1511590Srgrimes
15214443Sjoerg/* ARGSUSED */
1531590Srgrimesvoid
15414443Sjoergsig_sent(signo)
15514443Sjoerg	int signo;
1561590Srgrimes{
1571590Srgrimes
1581590Srgrimes	message("Connection closing. Exiting");
1591590Srgrimes	quit();
1601590Srgrimes}
1611590Srgrimes
1621590Srgrimes/*
1631590Srgrimes * All done talking...hang up the phone and reset terminal thingy's
1641590Srgrimes */
16514443Sjoergvoid
1661590Srgrimesquit()
1671590Srgrimes{
1681590Srgrimes
1691590Srgrimes	if (curses_initialized) {
1701590Srgrimes		wmove(his_win.x_win, his_win.x_nlines-1, 0);
1711590Srgrimes		wclrtoeol(his_win.x_win);
1721590Srgrimes		wrefresh(his_win.x_win);
1731590Srgrimes		endwin();
1741590Srgrimes	}
1751590Srgrimes	if (invitation_waiting)
1761590Srgrimes		send_delete();
1771590Srgrimes	exit(0);
1781590Srgrimes}
179