Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[dragonfly.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  * @(#)init_disp.c      8.2 (Berkeley) 2/16/94
34  * $FreeBSD: src/usr.bin/talk/init_disp.c,v 1.11.2.1 2001/07/30 10:31:29 dd Exp $
35  * $DragonFly: src/usr.bin/talk/init_disp.c,v 1.3 2003/10/04 20:36:52 hmp Exp $
36  */
37
38 /*
39  * Initialization code for the display package,
40  * as well as the signal handling routines.
41  */
42
43 #include <err.h>
44 #include <signal.h>
45 #include <stdlib.h>
46 #include <sys/stat.h>
47 #include <unistd.h>
48 #include <termios.h>
49 #include "talk.h"
50
51 /*
52  * Make sure the callee can write to the screen
53  */
54 void
55 check_writeable(void)
56 {
57         char *tty;
58         struct stat sb;
59
60         if ((tty = ttyname(STDERR_FILENO)) == NULL)
61                 err(1, "ttyname");
62         if (stat(tty, &sb) < 0)
63                 err(1, "%s", tty);
64         if (!(sb.st_mode & S_IWGRP))
65                 errx(1, "The callee cannot write to this terminal, use \"mesg y\".");
66 }
67
68 /*
69  * Set up curses, catch the appropriate signals,
70  * and build the various windows.
71  */
72 void
73 init_display(void)
74 {
75         struct sigaction sa;
76
77         if (initscr() == NULL)
78                 errx(1, "Terminal type unset or lacking necessary features.");
79         (void) sigaction(SIGTSTP, NULL, &sa);
80         sigaddset(&sa.sa_mask, SIGALRM);
81         (void) sigaction(SIGTSTP, &sa, NULL);
82         curses_initialized = 1;
83         clear();
84         refresh();
85         noecho();
86         crmode();
87         signal(SIGINT, sig_sent);
88         signal(SIGPIPE, sig_sent);
89         /* curses takes care of ^Z */
90         my_win.x_nlines = LINES / 2;
91         my_win.x_ncols = COLS;
92         my_win.x_win = newwin(my_win.x_nlines, my_win.x_ncols, 0, 0);
93         idlok(my_win.x_win, TRUE);
94         scrollok(my_win.x_win, TRUE);
95         wclear(my_win.x_win);
96
97         his_win.x_nlines = LINES / 2 - 1;
98         his_win.x_ncols = COLS;
99         his_win.x_win = newwin(his_win.x_nlines, his_win.x_ncols,
100             my_win.x_nlines+1, 0);
101         idlok(my_win.x_win, TRUE);
102         scrollok(his_win.x_win, TRUE);
103         wclear(his_win.x_win);
104
105         line_win = newwin(1, COLS, my_win.x_nlines, 0);
106 #if defined(hline) || defined(whline) || defined(NCURSES_VERSION)
107         whline(line_win, 0, COLS);
108 #else
109         box(line_win, '-', '-');
110 #endif
111         wrefresh(line_win);
112         /* let them know we are working on it */
113         current_state = "No connection yet";
114 }
115
116 /*
117  * Trade edit characters with the other talk. By agreement
118  * the first three characters each talk transmits after
119  * connection are the three edit characters.
120  */
121 void
122 set_edit_chars(void)
123 {
124         char buf[3];
125         int cc;
126         struct termios tio;
127
128         tcgetattr(0, &tio);
129         my_win.cerase = tio.c_cc[VERASE];
130         my_win.kill = tio.c_cc[VKILL];
131         my_win.werase = tio.c_cc[VWERASE];
132         if (my_win.cerase == (char)_POSIX_VDISABLE)
133                 my_win.kill = CERASE;
134         if (my_win.kill == (char)_POSIX_VDISABLE)
135                 my_win.kill = CKILL;
136         if (my_win.werase == (char)_POSIX_VDISABLE)
137                 my_win.werase = CWERASE;
138         buf[0] = my_win.cerase;
139         buf[1] = my_win.kill;
140         buf[2] = my_win.werase;
141         cc = write(sockt, buf, sizeof(buf));
142         if (cc != sizeof(buf) )
143                 p_error("Lost the connection");
144         cc = read(sockt, buf, sizeof(buf));
145         if (cc != sizeof(buf) )
146                 p_error("Lost the connection");
147         his_win.cerase = buf[0];
148         his_win.kill = buf[1];
149         his_win.werase = buf[2];
150 }
151
152 /* ARGSUSED */
153 void
154 sig_sent(int signo)
155 {
156
157         message("Connection closing. Exiting");
158         quit();
159 }
160
161 /*
162  * All done talking...hang up the phone and reset terminal thingy's
163  */
164 void
165 quit(void)
166 {
167
168         if (curses_initialized) {
169                 wmove(his_win.x_win, his_win.x_nlines-1, 0);
170                 wclrtoeol(his_win.x_win);
171                 wrefresh(his_win.x_win);
172                 endwin();
173         }
174         if (invitation_waiting)
175                 send_delete();
176         exit(0);
177 }