]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.bin/talk/init_disp.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / usr.bin / talk / init_disp.c
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 #include <sys/cdefs.h>
35
36 __FBSDID("$FreeBSD$");
37
38 #ifndef lint
39 static const char sccsid[] = "@(#)init_disp.c   8.2 (Berkeley) 2/16/94";
40 #endif
41
42 /*
43  * Initialization code for the display package,
44  * as well as the signal handling routines.
45  */
46
47 #include <sys/stat.h>
48
49 #include <err.h>
50 #include <signal.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53 #include <termios.h>
54
55 #include "talk.h"
56
57 extern volatile sig_atomic_t gotwinch;
58
59 /*
60  * Make sure the callee can write to the screen
61  */
62 void
63 check_writeable()
64 {
65         char *tty;
66         struct stat sb;
67
68         if ((tty = ttyname(STDERR_FILENO)) == NULL)
69                 err(1, "ttyname");
70         if (stat(tty, &sb) < 0)
71                 err(1, "%s", tty);
72         if (!(sb.st_mode & S_IWGRP))
73                 errx(1, "The callee cannot write to this terminal, use \"mesg y\".");
74 }
75
76 /*
77  * Set up curses, catch the appropriate signals,
78  * and build the various windows.
79  */
80 void
81 init_display()
82 {
83         struct sigaction sa;
84
85         if (initscr() == NULL)
86                 errx(1, "Terminal type unset or lacking necessary features.");
87         (void) sigaction(SIGTSTP, (struct sigaction *)0, &sa);
88         sigaddset(&sa.sa_mask, SIGALRM);
89         (void) sigaction(SIGTSTP, &sa, (struct sigaction *)0);
90         curses_initialized = 1;
91         clear();
92         refresh();
93         noecho();
94         crmode();
95         signal(SIGINT, sig_sent);
96         signal(SIGPIPE, sig_sent);
97         signal(SIGWINCH, sig_winch);
98         /* curses takes care of ^Z */
99         my_win.x_nlines = LINES / 2;
100         my_win.x_ncols = COLS;
101         my_win.x_win = newwin(my_win.x_nlines, my_win.x_ncols, 0, 0);
102         idlok(my_win.x_win, TRUE);
103         scrollok(my_win.x_win, TRUE);
104         wclear(my_win.x_win);
105
106         his_win.x_nlines = LINES / 2 - 1;
107         his_win.x_ncols = COLS;
108         his_win.x_win = newwin(his_win.x_nlines, his_win.x_ncols,
109             my_win.x_nlines+1, 0);
110         idlok(my_win.x_win, TRUE);
111         scrollok(his_win.x_win, TRUE);
112         wclear(his_win.x_win);
113
114         line_win = newwin(1, COLS, my_win.x_nlines, 0);
115 #if defined(hline) || defined(whline) || defined(NCURSES_VERSION)
116         whline(line_win, 0, COLS);
117 #else
118         box(line_win, '-', '-');
119 #endif
120         wrefresh(line_win);
121         /* let them know we are working on it */
122         current_state = "No connection yet";
123 }
124
125 /*
126  * Trade edit characters with the other talk. By agreement
127  * the first three characters each talk transmits after
128  * connection are the three edit characters.
129  */
130 void
131 set_edit_chars()
132 {
133         char buf[3];
134         int cc;
135         struct termios tio;
136
137         tcgetattr(0, &tio);
138         my_win.cerase = tio.c_cc[VERASE];
139         my_win.kill = tio.c_cc[VKILL];
140         my_win.werase = tio.c_cc[VWERASE];
141         if (my_win.cerase == (char)_POSIX_VDISABLE)
142                 my_win.kill = CERASE;
143         if (my_win.kill == (char)_POSIX_VDISABLE)
144                 my_win.kill = CKILL;
145         if (my_win.werase == (char)_POSIX_VDISABLE)
146                 my_win.werase = CWERASE;
147         buf[0] = my_win.cerase;
148         buf[1] = my_win.kill;
149         buf[2] = my_win.werase;
150         cc = write(sockt, buf, sizeof(buf));
151         if (cc != sizeof(buf) )
152                 p_error("Lost the connection");
153         cc = read(sockt, buf, sizeof(buf));
154         if (cc != sizeof(buf) )
155                 p_error("Lost the connection");
156         his_win.cerase = buf[0];
157         his_win.kill = buf[1];
158         his_win.werase = buf[2];
159 }
160
161 /* ARGSUSED */
162 void
163 sig_sent(signo)
164         int signo __unused;
165 {
166
167         message("Connection closing. Exiting");
168         quit();
169 }
170
171 void
172 sig_winch(int dummy)
173 {
174  
175         gotwinch = 1;
176 }
177
178 /*
179  * All done talking...hang up the phone and reset terminal thingy's
180  */
181 void
182 quit()
183 {
184
185         if (curses_initialized) {
186                 wmove(his_win.x_win, his_win.x_nlines-1, 0);
187                 wclrtoeol(his_win.x_win);
188                 wrefresh(his_win.x_win);
189                 endwin();
190         }
191         if (invitation_waiting)
192                 send_delete();
193         exit(0);
194 }
195
196 /*
197  * If we get SIGWINCH, recompute both window sizes and refresh things.
198  */
199 void
200 resize_display(void)
201 {
202         struct winsize ws;
203
204         if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) < 0 ||
205             (ws.ws_row == LINES && ws.ws_col == COLS))
206                 return;
207
208         /* Update curses' internal state with new window size. */
209         resizeterm(ws.ws_row, ws.ws_col);
210
211         /*
212          * Resize each window but wait to refresh the screen until
213          * everything has been drawn so the cursor is in the right spot.
214          */
215         my_win.x_nlines = LINES / 2;
216         my_win.x_ncols = COLS;
217         wresize(my_win.x_win, my_win.x_nlines, my_win.x_ncols);
218         mvwin(my_win.x_win, 0, 0);
219         clearok(my_win.x_win, TRUE);
220
221         his_win.x_nlines = LINES / 2 - 1;
222         his_win.x_ncols = COLS;
223         wresize(his_win.x_win, his_win.x_nlines, his_win.x_ncols);
224         mvwin(his_win.x_win, my_win.x_nlines + 1, 0);
225         clearok(his_win.x_win, TRUE);
226
227         wresize(line_win, 1, COLS);
228         mvwin(line_win, my_win.x_nlines, 0);
229 #if defined(NCURSES_VERSION) || defined(whline)
230         whline(line_win, '-', COLS);
231 #else
232         wmove(line_win, my_win.x_nlines, 0);
233         box(line_win, '-', '-');
234 #endif
235
236         /* Now redraw the screen. */
237         wrefresh(his_win.x_win);
238         wrefresh(line_win);
239         wrefresh(my_win.x_win);
240 }