xref: /netbsd-src/usr.bin/talk/init_disp.c (revision 68168df687839776c137f90832075bcd1dead31d)
1 /*	$NetBSD: init_disp.c,v 1.12 2011/09/06 18:32:03 joerg Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)init_disp.c	8.2 (Berkeley) 2/16/94";
36 #endif
37 __RCSID("$NetBSD: init_disp.c,v 1.12 2011/09/06 18:32:03 joerg 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 <signal.h>
50 #include <termios.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53 
54 /*
55  * Set up curses, catch the appropriate signals,
56  * and build the various windows.
57  */
58 void
init_display(void)59 init_display(void)
60 {
61 	struct sigaction sa;
62 
63 	if (initscr() == NULL)
64 		errx(1, "Terminal type unset or lacking necessary features.");
65 	(void)sigaction(SIGTSTP, NULL, &sa);
66 	sigaddset(&sa.sa_mask, SIGALRM);
67 	(void)sigaction(SIGTSTP, &sa, NULL);
68 	curses_initialized = 1;
69 	clear();
70 	refresh();
71 	noecho();
72 	cbreak();
73 	signal(SIGINT, sig_sent);
74 	signal(SIGPIPE, sig_sent);
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, FALSE);
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, FALSE);
87 	wclear(his_win.x_win);
88 
89 	line_win = newwin(1, COLS, my_win.x_nlines, 0);
90 	box(line_win, '-', '-');
91 	wrefresh(line_win);
92 	/* let them know we are working on it */
93 	current_state = "No connection yet";
94 }
95 
96 /*
97  * Trade edit characters with the other talk. By agreement
98  * the first three characters each talk transmits after
99  * connection are the three edit characters.
100  */
101 void
set_edit_chars(void)102 set_edit_chars(void)
103 {
104 	char buf[3];
105 	int cc;
106 	struct termios tty;
107 
108 	tcgetattr(0, &tty);
109 	my_win.cerase = tty.c_cc[VERASE];
110 	my_win.kill = tty.c_cc[VKILL];
111 	if (tty.c_cc[VWERASE] == (unsigned char) -1)
112 		my_win.werase = '\027';	 /* control W */
113 	else
114 		my_win.werase = tty.c_cc[VWERASE];
115 	buf[0] = my_win.cerase;
116 	buf[1] = my_win.kill;
117 	buf[2] = my_win.werase;
118 	cc = write(sockt, buf, sizeof(buf));
119 	if (cc != sizeof(buf) )
120 		p_error("Lost the connection");
121 	cc = read(sockt, buf, sizeof(buf));
122 	if (cc != sizeof(buf) )
123 		p_error("Lost the connection");
124 	his_win.cerase = buf[0];
125 	his_win.kill = buf[1];
126 	his_win.werase = buf[2];
127 }
128 
129 void
sig_sent(int dummy)130 sig_sent(int dummy)
131 {
132 
133 	message("Connection closing. Exiting");
134 	quit();
135 }
136 
137 /*
138  * All done talking...hang up the phone and reset terminal thingy's
139  */
140 void
quit(void)141 quit(void)
142 {
143 
144 	if (curses_initialized) {
145 		wmove(his_win.x_win, his_win.x_nlines-1, 0);
146 		wclrtoeol(his_win.x_win);
147 		wrefresh(his_win.x_win);
148 		endwin();
149 	}
150 	if (invitation_waiting)
151 		send_delete();
152 	exit(0);
153 }
154