Annotation of src/usr.bin/talk/init_disp.c, Revision 1.16
1.16 ! millert 1: /* $OpenBSD: init_disp.c,v 1.15 2002/06/21 06:16:44 millert Exp $ */
1.1 deraadt 2: /* $NetBSD: init_disp.c,v 1.6 1994/12/09 02:14:17 jtc Exp $ */
3:
4: /*
5: * Copyright (c) 1983, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.16 ! millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: #ifndef lint
34: #if 0
35: static char sccsid[] = "@(#)init_disp.c 8.2 (Berkeley) 2/16/94";
36: #endif
1.16 ! millert 37: static char rcsid[] = "$OpenBSD: init_disp.c,v 1.15 2002/06/21 06:16:44 millert Exp $";
1.1 deraadt 38: #endif /* not lint */
39:
40: /*
41: * Initialization code for the display package,
42: * as well as the signal handling routines.
43: */
44:
1.4 pjanzen 45: #include "talk.h"
1.1 deraadt 46: #include <sys/ioctl.h>
47: #include <sys/ioctl_compat.h>
1.4 pjanzen 48: #include <err.h>
1.1 deraadt 49: #include <termios.h>
1.4 pjanzen 50: #include <unistd.h>
1.1 deraadt 51:
1.5 millert 52: /*
1.1 deraadt 53: * Set up curses, catch the appropriate signals,
54: * and build the various windows.
55: */
1.4 pjanzen 56: void
1.1 deraadt 57: init_display()
58: {
1.5 millert 59: struct sigaction sa;
1.1 deraadt 60:
61: if (initscr() == NULL)
62: errx(1, "Terminal type unset or lacking necessary features.");
1.5 millert 63: (void) sigaction(SIGTSTP, NULL, &sa);
64: sigaddset(&sa.sa_mask, SIGALRM);
65: (void) sigaction(SIGTSTP, &sa, NULL);
1.1 deraadt 66: curses_initialized = 1;
67: clear();
68: refresh();
69: noecho();
1.9 millert 70: cbreak();
1.1 deraadt 71: signal(SIGINT, sig_sent);
72: signal(SIGPIPE, sig_sent);
1.15 millert 73: signal(SIGWINCH, sig_winch);
1.1 deraadt 74: /* curses takes care of ^Z */
75: my_win.x_nlines = LINES / 2;
76: my_win.x_ncols = COLS;
77: my_win.x_win = newwin(my_win.x_nlines, my_win.x_ncols, 0, 0);
1.14 millert 78: scrollok(my_win.x_win, smooth_scroll);
1.1 deraadt 79: wclear(my_win.x_win);
80:
81: his_win.x_nlines = LINES / 2 - 1;
82: his_win.x_ncols = COLS;
83: his_win.x_win = newwin(his_win.x_nlines, his_win.x_ncols,
84: my_win.x_nlines+1, 0);
1.14 millert 85: scrollok(his_win.x_win, smooth_scroll);
1.1 deraadt 86: wclear(his_win.x_win);
87:
88: line_win = newwin(1, COLS, my_win.x_nlines, 0);
1.5 millert 89: #if defined(NCURSES_VERSION) || defined(whline)
1.2 tholo 90: whline(line_win, '-', COLS);
91: #else
1.1 deraadt 92: box(line_win, '-', '-');
1.2 tholo 93: #endif
1.1 deraadt 94: wrefresh(line_win);
95: /* let them know we are working on it */
96: current_state = "No connection yet";
97: }
98:
99: /*
100: * Trade edit characters with the other talk. By agreement
101: * the first three characters each talk transmits after
102: * connection are the three edit characters.
103: */
1.4 pjanzen 104: void
1.1 deraadt 105: set_edit_chars()
106: {
1.6 deraadt 107: u_char buf[3];
1.1 deraadt 108: int cc;
109: struct termios tty;
110:
1.15 millert 111: tcgetattr(STDIN_FILENO, &tty);
1.7 millert 112: buf[0] = my_win.cerase = (tty.c_cc[VERASE] == (u_char)_POSIX_VDISABLE)
1.5 millert 113: ? CERASE : tty.c_cc[VERASE];
1.7 millert 114: buf[1] = my_win.kill = (tty.c_cc[VKILL] == (u_char)_POSIX_VDISABLE)
1.6 deraadt 115: ? CKILL : tty.c_cc[VKILL];
1.7 millert 116: buf[2] = my_win.werase = (tty.c_cc[VWERASE] == (u_char)_POSIX_VDISABLE)
1.5 millert 117: ? CWERASE : tty.c_cc[VWERASE];
1.1 deraadt 118: cc = write(sockt, buf, sizeof(buf));
119: if (cc != sizeof(buf) )
1.8 millert 120: quit("Lost the connection", 1);
1.1 deraadt 121: cc = read(sockt, buf, sizeof(buf));
122: if (cc != sizeof(buf) )
1.8 millert 123: quit("Lost the connection", 1);
1.1 deraadt 124: his_win.cerase = buf[0];
125: his_win.kill = buf[1];
126: his_win.werase = buf[2];
127: }
128:
129: void
1.4 pjanzen 130: sig_sent(dummy)
131: int dummy;
1.1 deraadt 132: {
133:
1.8 millert 134: quit("Connection closing. Exiting", 0);
1.1 deraadt 135: }
136:
1.15 millert 137: void
138: sig_winch(dummy)
139: int dummy;
140: {
141:
142: gotwinch = 1;
143: }
144:
1.1 deraadt 145: /*
146: * All done talking...hang up the phone and reset terminal thingy's
147: */
1.4 pjanzen 148: void
1.8 millert 149: quit(warning, do_perror)
150: char *warning;
151: int do_perror;
1.1 deraadt 152: {
153:
154: if (curses_initialized) {
155: wmove(his_win.x_win, his_win.x_nlines-1, 0);
156: wclrtoeol(his_win.x_win);
157: wrefresh(his_win.x_win);
158: endwin();
159: }
160: if (invitation_waiting)
161: send_delete();
1.8 millert 162: if (warning) {
163: if (do_perror)
1.11 millert 164: warn("%s", warning);
1.8 millert 165: else
1.11 millert 166: warnx("%s", warning);
1.8 millert 167: }
1.1 deraadt 168: exit(0);
1.15 millert 169: }
170:
171: /*
172: * If we get SIGWINCH, recompute both window sizes and refresh things.
173: */
174: void
175: resize_display()
176: {
177: struct winsize ws;
178:
179: if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) < 0 ||
180: (ws.ws_row == LINES && ws.ws_col == COLS))
181: return;
182:
183: /* Update curses' internal state with new window size. */
184: resizeterm(ws.ws_row, ws.ws_col);
185:
186: /*
187: * Resize each window but wait to refresh the screen until
188: * everything has been drawn so the cursor is in the right spot.
189: */
190: my_win.x_nlines = LINES / 2;
191: my_win.x_ncols = COLS;
192: wresize(my_win.x_win, my_win.x_nlines, my_win.x_ncols);
193: mvwin(my_win.x_win, 0, 0);
194: clearok(my_win.x_win, TRUE);
195:
196: his_win.x_nlines = LINES / 2 - 1;
197: his_win.x_ncols = COLS;
198: wresize(his_win.x_win, his_win.x_nlines, his_win.x_ncols);
199: mvwin(his_win.x_win, my_win.x_nlines + 1, 0);
200: clearok(his_win.x_win, TRUE);
201:
202: wresize(line_win, 1, COLS);
203: mvwin(line_win, my_win.x_nlines, 0);
204: #if defined(NCURSES_VERSION) || defined(whline)
205: whline(line_win, '-', COLS);
206: #else
207: wmove(line_win, my_win.x_nlines, 0);
208: box(line_win, '-', '-');
209: #endif
210:
211: /* Now redraw the screen. */
212: wrefresh(his_win.x_win);
213: wrefresh(line_win);
214: wrefresh(my_win.x_win);
1.1 deraadt 215: }