xref: /freebsd/usr.bin/talk/init_disp.c (revision a316b26e50bbed7cf655fbba726ab87d8ab7599d)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static char sccsid[] = "@(#)init_disp.c	8.2 (Berkeley) 2/16/94";
36 #endif /* not lint */
37 
38 /*
39  * Initialization code for the display package,
40  * as well as the signal handling routines.
41  */
42 
43 #include <sys/ioctl.h>
44 #include <sys/ioctl_compat.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 
48 #include <unistd.h>
49 #include <signal.h>
50 #include <err.h>
51 #include "talk.h"
52 
53 /*
54  * Make sure the callee can write to the screen
55  */
56 void check_writeable()
57 {
58 	char *tty;
59 	struct stat sb;
60 
61 	if ((tty = ttyname(STDERR_FILENO)) == NULL)
62 		err(1, "ttyname");
63 	if (stat(tty, &sb) < 0)
64 		err(1, "%s", tty);
65 	if (!(sb.st_mode & S_IWGRP))
66 		errx(1, "The callee cannot write to this terminal, use \"mesg y\".");
67 }
68 
69 /*
70  * Set up curses, catch the appropriate signals,
71  * and build the various windows.
72  */
73 init_display()
74 {
75 	void sig_sent();
76 	struct sigvec sigv;
77 
78 	if (initscr() == NULL)
79 		errx(1, "Terminal type unset or lacking necessary features.");
80 	(void) sigvec(SIGTSTP, (struct sigvec *)0, &sigv);
81 	sigv.sv_mask |= sigmask(SIGALRM);
82 	(void) sigvec(SIGTSTP, &sigv, (struct sigvec *)0);
83 	curses_initialized = 1;
84 	clear();
85 	refresh();
86 	noecho();
87 	crmode();
88 	signal(SIGINT, sig_sent);
89 	signal(SIGPIPE, sig_sent);
90 	/* curses takes care of ^Z */
91 	my_win.x_nlines = LINES / 2;
92 	my_win.x_ncols = COLS;
93 	my_win.x_win = newwin(my_win.x_nlines, my_win.x_ncols, 0, 0);
94 	scrollok(my_win.x_win, TRUE);
95 	wclear(my_win.x_win);
96 
97 	his_win.x_nlines = LINES / 2 - 1;
98 	his_win.x_ncols = COLS;
99 	his_win.x_win = newwin(his_win.x_nlines, his_win.x_ncols,
100 	    my_win.x_nlines+1, 0);
101 	scrollok(his_win.x_win, TRUE);
102 	wclear(his_win.x_win);
103 
104 	line_win = newwin(1, COLS, my_win.x_nlines, 0);
105 	box(line_win, '-', '-');
106 	wrefresh(line_win);
107 	/* let them know we are working on it */
108 	current_state = "No connection yet";
109 }
110 
111 /*
112  * Trade edit characters with the other talk. By agreement
113  * the first three characters each talk transmits after
114  * connection are the three edit characters.
115  */
116 set_edit_chars()
117 {
118 	char buf[3];
119 	int cc;
120 	struct sgttyb tty;
121 	struct ltchars ltc;
122 
123 	ioctl(0, TIOCGETP, &tty);
124 	ioctl(0, TIOCGLTC, (struct sgttyb *)&ltc);
125 	my_win.cerase = tty.sg_erase;
126 	my_win.kill = tty.sg_kill;
127 	if (ltc.t_werasc == (char) -1)
128 		my_win.werase = '\027';	 /* control W */
129 	else
130 		my_win.werase = ltc.t_werasc;
131 	buf[0] = my_win.cerase;
132 	buf[1] = my_win.kill;
133 	buf[2] = my_win.werase;
134 	cc = write(sockt, buf, sizeof(buf));
135 	if (cc != sizeof(buf) )
136 		p_error("Lost the connection");
137 	cc = read(sockt, buf, sizeof(buf));
138 	if (cc != sizeof(buf) )
139 		p_error("Lost the connection");
140 	his_win.cerase = buf[0];
141 	his_win.kill = buf[1];
142 	his_win.werase = buf[2];
143 }
144 
145 void
146 sig_sent()
147 {
148 
149 	message("Connection closing. Exiting");
150 	quit();
151 }
152 
153 /*
154  * All done talking...hang up the phone and reset terminal thingy's
155  */
156 quit()
157 {
158 
159 	if (curses_initialized) {
160 		wmove(his_win.x_win, his_win.x_nlines-1, 0);
161 		wclrtoeol(his_win.x_win);
162 		wrefresh(his_win.x_win);
163 		endwin();
164 	}
165 	if (invitation_waiting)
166 		send_delete();
167 	exit(0);
168 }
169