init_disp.c revision 32503
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 3532503Scharnier#if 0 361590Srgrimesstatic char sccsid[] = "@(#)init_disp.c 8.2 (Berkeley) 2/16/94"; 3732503Scharnier#endif 3832503Scharnierstatic const char rcsid[] = 3932503Scharnier "$Id$"; 401590Srgrimes#endif /* not lint */ 411590Srgrimes 421590Srgrimes/* 431590Srgrimes * Initialization code for the display package, 441590Srgrimes * as well as the signal handling routines. 451590Srgrimes */ 461590Srgrimes 4732503Scharnier#include <err.h> 4832503Scharnier#include <signal.h> 495297Sache#include <sys/stat.h> 505297Sache#include <unistd.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); 9520988Speter idlok(my_win.x_win, TRUE); 962929Sache scrollok(my_win.x_win, TRUE); 971590Srgrimes wclear(my_win.x_win); 981590Srgrimes 991590Srgrimes his_win.x_nlines = LINES / 2 - 1; 1001590Srgrimes his_win.x_ncols = COLS; 1011590Srgrimes his_win.x_win = newwin(his_win.x_nlines, his_win.x_ncols, 1021590Srgrimes my_win.x_nlines+1, 0); 10320988Speter idlok(my_win.x_win, TRUE); 1042929Sache scrollok(his_win.x_win, TRUE); 1051590Srgrimes wclear(his_win.x_win); 1061590Srgrimes 1071590Srgrimes line_win = newwin(1, COLS, my_win.x_nlines, 0); 10817698Speter#if defined(hline) || defined(whline) || defined(NCURSES_VERSION) 10917698Speter whline(line_win, 0, COLS); 11017698Speter#else 11117698Speter box(line_win, '-', '-'); 11217698Speter#endif 1131590Srgrimes wrefresh(line_win); 1141590Srgrimes /* let them know we are working on it */ 1151590Srgrimes current_state = "No connection yet"; 1161590Srgrimes} 1171590Srgrimes 1181590Srgrimes/* 1191590Srgrimes * Trade edit characters with the other talk. By agreement 1201590Srgrimes * the first three characters each talk transmits after 1211590Srgrimes * connection are the three edit characters. 1221590Srgrimes */ 12314443Sjoergvoid 1241590Srgrimesset_edit_chars() 1251590Srgrimes{ 1261590Srgrimes char buf[3]; 1271590Srgrimes int cc; 12817676Speter struct termios tio; 1298874Srgrimes 13017676Speter tcgetattr(0, &tio); 13117676Speter my_win.cerase = tio.c_cc[VERASE]; 13217676Speter my_win.kill = tio.c_cc[VKILL]; 13317676Speter my_win.werase = tio.c_cc[VWERASE]; 13417676Speter if (my_win.cerase == (char)_POSIX_VDISABLE) 13517676Speter my_win.kill = CERASE; 13617676Speter if (my_win.kill == (char)_POSIX_VDISABLE) 13717676Speter my_win.kill = CKILL; 13817676Speter if (my_win.werase == (char)_POSIX_VDISABLE) 13917676Speter my_win.werase = CWERASE; 1401590Srgrimes buf[0] = my_win.cerase; 1411590Srgrimes buf[1] = my_win.kill; 1421590Srgrimes buf[2] = my_win.werase; 1431590Srgrimes cc = write(sockt, buf, sizeof(buf)); 1441590Srgrimes if (cc != sizeof(buf) ) 1451590Srgrimes p_error("Lost the connection"); 1461590Srgrimes cc = read(sockt, buf, sizeof(buf)); 1471590Srgrimes if (cc != sizeof(buf) ) 1481590Srgrimes p_error("Lost the connection"); 1491590Srgrimes his_win.cerase = buf[0]; 1501590Srgrimes his_win.kill = buf[1]; 1511590Srgrimes his_win.werase = buf[2]; 1521590Srgrimes} 1531590Srgrimes 15414443Sjoerg/* ARGSUSED */ 1551590Srgrimesvoid 15614443Sjoergsig_sent(signo) 15714443Sjoerg int signo; 1581590Srgrimes{ 1591590Srgrimes 1601590Srgrimes message("Connection closing. Exiting"); 1611590Srgrimes quit(); 1621590Srgrimes} 1631590Srgrimes 1641590Srgrimes/* 1651590Srgrimes * All done talking...hang up the phone and reset terminal thingy's 1661590Srgrimes */ 16714443Sjoergvoid 1681590Srgrimesquit() 1691590Srgrimes{ 1701590Srgrimes 1711590Srgrimes if (curses_initialized) { 1721590Srgrimes wmove(his_win.x_win, his_win.x_nlines-1, 0); 1731590Srgrimes wclrtoeol(his_win.x_win); 1741590Srgrimes wrefresh(his_win.x_win); 1751590Srgrimes endwin(); 1761590Srgrimes } 1771590Srgrimes if (invitation_waiting) 1781590Srgrimes send_delete(); 1791590Srgrimes exit(0); 1801590Srgrimes} 181