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