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