Annotation of src/usr.bin/talk/init_disp.c, Revision 1.21
1.21 ! naddy 1: /* $OpenBSD: init_disp.c,v 1.20 2009/10/27 23:59:44 deraadt 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: /*
34: * Initialization code for the display package,
35: * as well as the signal handling routines.
36: */
37:
1.4 pjanzen 38: #include "talk.h"
1.1 deraadt 39: #include <sys/ioctl.h>
1.4 pjanzen 40: #include <err.h>
1.18 david 41: #include <stdlib.h>
1.1 deraadt 42: #include <termios.h>
1.4 pjanzen 43: #include <unistd.h>
1.1 deraadt 44:
1.5 millert 45: /*
1.1 deraadt 46: * Set up curses, catch the appropriate signals,
47: * and build the various windows.
48: */
1.4 pjanzen 49: void
1.17 deraadt 50: init_display(void)
1.1 deraadt 51: {
1.5 millert 52: struct sigaction sa;
1.1 deraadt 53:
54: if (initscr() == NULL)
55: errx(1, "Terminal type unset or lacking necessary features.");
1.5 millert 56: (void) sigaction(SIGTSTP, NULL, &sa);
57: sigaddset(&sa.sa_mask, SIGALRM);
58: (void) sigaction(SIGTSTP, &sa, NULL);
1.1 deraadt 59: curses_initialized = 1;
60: clear();
61: refresh();
62: noecho();
1.9 millert 63: cbreak();
1.1 deraadt 64: signal(SIGINT, sig_sent);
65: signal(SIGPIPE, sig_sent);
1.15 millert 66: signal(SIGWINCH, sig_winch);
1.1 deraadt 67: /* curses takes care of ^Z */
68: my_win.x_nlines = LINES / 2;
69: my_win.x_ncols = COLS;
70: my_win.x_win = newwin(my_win.x_nlines, my_win.x_ncols, 0, 0);
1.14 millert 71: scrollok(my_win.x_win, smooth_scroll);
1.1 deraadt 72: wclear(my_win.x_win);
73:
74: his_win.x_nlines = LINES / 2 - 1;
75: his_win.x_ncols = COLS;
76: his_win.x_win = newwin(his_win.x_nlines, his_win.x_ncols,
77: my_win.x_nlines+1, 0);
1.14 millert 78: scrollok(his_win.x_win, smooth_scroll);
1.1 deraadt 79: wclear(his_win.x_win);
80:
81: line_win = newwin(1, COLS, my_win.x_nlines, 0);
1.5 millert 82: #if defined(NCURSES_VERSION) || defined(whline)
1.2 tholo 83: whline(line_win, '-', COLS);
84: #else
1.1 deraadt 85: box(line_win, '-', '-');
1.2 tholo 86: #endif
1.1 deraadt 87: wrefresh(line_win);
88: /* let them know we are working on it */
89: current_state = "No connection yet";
90: }
91:
92: /*
93: * Trade edit characters with the other talk. By agreement
94: * the first three characters each talk transmits after
95: * connection are the three edit characters.
96: */
1.4 pjanzen 97: void
1.17 deraadt 98: set_edit_chars(void)
1.1 deraadt 99: {
1.6 deraadt 100: u_char buf[3];
1.1 deraadt 101: int cc;
102: struct termios tty;
103:
1.15 millert 104: tcgetattr(STDIN_FILENO, &tty);
1.7 millert 105: buf[0] = my_win.cerase = (tty.c_cc[VERASE] == (u_char)_POSIX_VDISABLE)
1.5 millert 106: ? CERASE : tty.c_cc[VERASE];
1.7 millert 107: buf[1] = my_win.kill = (tty.c_cc[VKILL] == (u_char)_POSIX_VDISABLE)
1.6 deraadt 108: ? CKILL : tty.c_cc[VKILL];
1.7 millert 109: buf[2] = my_win.werase = (tty.c_cc[VWERASE] == (u_char)_POSIX_VDISABLE)
1.5 millert 110: ? CWERASE : tty.c_cc[VWERASE];
1.1 deraadt 111: cc = write(sockt, buf, sizeof(buf));
112: if (cc != sizeof(buf) )
1.8 millert 113: quit("Lost the connection", 1);
1.1 deraadt 114: cc = read(sockt, buf, sizeof(buf));
115: if (cc != sizeof(buf) )
1.8 millert 116: quit("Lost the connection", 1);
1.1 deraadt 117: his_win.cerase = buf[0];
118: his_win.kill = buf[1];
119: his_win.werase = buf[2];
120: }
121:
122: void
1.17 deraadt 123: sig_sent(int dummy)
1.1 deraadt 124: {
125:
1.8 millert 126: quit("Connection closing. Exiting", 0);
1.1 deraadt 127: }
128:
1.15 millert 129: void
1.17 deraadt 130: sig_winch(int dummy)
1.15 millert 131: {
132:
133: gotwinch = 1;
134: }
135:
1.1 deraadt 136: /*
137: * All done talking...hang up the phone and reset terminal thingy's
138: */
1.4 pjanzen 139: void
1.17 deraadt 140: quit(char *warning, int do_perror)
1.1 deraadt 141: {
142:
143: if (curses_initialized) {
144: wmove(his_win.x_win, his_win.x_nlines-1, 0);
145: wclrtoeol(his_win.x_win);
146: wrefresh(his_win.x_win);
147: endwin();
148: }
149: if (invitation_waiting)
150: send_delete();
1.8 millert 151: if (warning) {
152: if (do_perror)
1.11 millert 153: warn("%s", warning);
1.8 millert 154: else
1.11 millert 155: warnx("%s", warning);
1.8 millert 156: }
1.1 deraadt 157: exit(0);
1.15 millert 158: }
159:
160: /*
161: * If we get SIGWINCH, recompute both window sizes and refresh things.
162: */
163: void
1.17 deraadt 164: resize_display(void)
1.15 millert 165: {
166: struct winsize ws;
167:
168: if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) < 0 ||
169: (ws.ws_row == LINES && ws.ws_col == COLS))
170: return;
171:
172: /* Update curses' internal state with new window size. */
173: resizeterm(ws.ws_row, ws.ws_col);
174:
175: /*
176: * Resize each window but wait to refresh the screen until
177: * everything has been drawn so the cursor is in the right spot.
178: */
179: my_win.x_nlines = LINES / 2;
180: my_win.x_ncols = COLS;
181: wresize(my_win.x_win, my_win.x_nlines, my_win.x_ncols);
182: mvwin(my_win.x_win, 0, 0);
183: clearok(my_win.x_win, TRUE);
184:
185: his_win.x_nlines = LINES / 2 - 1;
186: his_win.x_ncols = COLS;
187: wresize(his_win.x_win, his_win.x_nlines, his_win.x_ncols);
188: mvwin(his_win.x_win, my_win.x_nlines + 1, 0);
189: clearok(his_win.x_win, TRUE);
190:
191: wresize(line_win, 1, COLS);
192: mvwin(line_win, my_win.x_nlines, 0);
193: #if defined(NCURSES_VERSION) || defined(whline)
194: whline(line_win, '-', COLS);
195: #else
196: wmove(line_win, my_win.x_nlines, 0);
197: box(line_win, '-', '-');
198: #endif
199:
200: /* Now redraw the screen. */
201: wrefresh(his_win.x_win);
202: wrefresh(line_win);
203: wrefresh(my_win.x_win);
1.1 deraadt 204: }