Home | History | Annotate | Line # | Download | only in talk
      1 /*	$NetBSD: display.c,v 1.9 2011/09/06 18:32:03 joerg Exp $	*/
      2 
      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 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)display.c	8.1 (Berkeley) 6/6/93";
     36 #endif
     37 __RCSID("$NetBSD: display.c,v 1.9 2011/09/06 18:32:03 joerg Exp $");
     38 #endif /* not lint */
     39 
     40 /*
     41  * The window 'manager', initializes curses and handles the actual
     42  * displaying of text
     43  */
     44 #include "talk.h"
     45 #include <ctype.h>
     46 
     47 xwin_t	my_win;
     48 xwin_t	his_win;
     49 WINDOW	*line_win;
     50 
     51 int	curses_initialized = 0;
     52 
     53 /*
     54  * max HAS to be a function, it is called with
     55  * a argument of the form --foo at least once.
     56  */
     57 int
     58 max(int a, int b)
     59 {
     60 
     61 	return (a > b ? a : b);
     62 }
     63 
     64 /*
     65  * Display some text on somebody's window, processing some control
     66  * characters while we are at it.
     67  */
     68 void
     69 display(xwin_t *win, char *text, int size)
     70 {
     71 	int i;
     72 	char cch;
     73 
     74 	for (i = 0; i < size; i++) {
     75 		if (*text == '\n') {
     76 			xscroll(win, 0);
     77 			text++;
     78 			continue;
     79 		}
     80 		/* erase character */
     81 		if (*text == win->cerase) {
     82 			wmove(win->x_win, win->x_line, max(--win->x_col, 0));
     83 			getyx(win->x_win, win->x_line, win->x_col);
     84 			waddch(win->x_win, ' ');
     85 			wmove(win->x_win, win->x_line, win->x_col);
     86 			getyx(win->x_win, win->x_line, win->x_col);
     87 			text++;
     88 			continue;
     89 		}
     90 		/*
     91 		 * On word erase search backwards until we find
     92 		 * the beginning of a word or the beginning of
     93 		 * the line.
     94 		 */
     95 		if (*text == win->werase) {
     96 			int endcol, xcol, j, c;
     97 
     98 			endcol = win->x_col;
     99 			xcol = endcol - 1;
    100 			while (xcol >= 0) {
    101 				c = readwin(win->x_win, win->x_line, xcol);
    102 				if (c != ' ')
    103 					break;
    104 				xcol--;
    105 			}
    106 			while (xcol >= 0) {
    107 				c = readwin(win->x_win, win->x_line, xcol);
    108 				if (c == ' ')
    109 					break;
    110 				xcol--;
    111 			}
    112 			wmove(win->x_win, win->x_line, xcol + 1);
    113 			for (j = xcol + 1; j < endcol; j++)
    114 				waddch(win->x_win, ' ');
    115 			wmove(win->x_win, win->x_line, xcol + 1);
    116 			getyx(win->x_win, win->x_line, win->x_col);
    117 			text++;
    118 			continue;
    119 		}
    120 		/* line kill */
    121 		if (*text == win->kill) {
    122 			wmove(win->x_win, win->x_line, 0);
    123 			wclrtoeol(win->x_win);
    124 			getyx(win->x_win, win->x_line, win->x_col);
    125 			text++;
    126 			continue;
    127 		}
    128 		if (*text == '\f') {
    129 			if (win == &my_win)
    130 				wrefresh(curscr);
    131 			text++;
    132 			continue;
    133 		}
    134 		if (*text == '\07') {
    135 			beep();
    136 			text++;
    137 			continue;
    138 		}
    139 		if (win->x_col == COLS-1) {
    140 			/* check for wraparound */
    141 			xscroll(win, 0);
    142 		}
    143 		if ( !isprint((u_char)*text) && *text != '\t') {
    144 			waddch(win->x_win, '^');
    145 			getyx(win->x_win, win->x_line, win->x_col);
    146 			if (win->x_col == COLS-1) /* check for wraparound */
    147 				xscroll(win, 0);
    148 			cch = (*text & 63) + 64;
    149 			waddch(win->x_win, cch);
    150 		} else
    151 			waddch(win->x_win, *text);
    152 		getyx(win->x_win, win->x_line, win->x_col);
    153 		text++;
    154 	}
    155 	wrefresh(win->x_win);
    156 }
    157 
    158 /*
    159  * Read the character at the indicated position in win
    160  */
    161 int
    162 readwin(WINDOW *win, int line, int col)
    163 {
    164 	int oldline, oldcol;
    165 	int c;
    166 
    167 	getyx(win, oldline, oldcol);
    168 	wmove(win, line, col);
    169 	c = winch(win);
    170 	wmove(win, oldline, oldcol);
    171 	return (c);
    172 }
    173 
    174 /*
    175  * Scroll a window, blanking out the line following the current line
    176  * so that the current position is obvious
    177  */
    178 void
    179 xscroll(xwin_t *win, int flag)
    180 {
    181 
    182 	if (flag == -1) {
    183 		wmove(win->x_win, 0, 0);
    184 		win->x_line = 0;
    185 		win->x_col = 0;
    186 		return;
    187 	}
    188 	win->x_line = (win->x_line + 1) % win->x_nlines;
    189 	win->x_col = 0;
    190 	wmove(win->x_win, win->x_line, win->x_col);
    191 	wclrtoeol(win->x_win);
    192 	wmove(win->x_win, (win->x_line + 1) % win->x_nlines, win->x_col);
    193 	wclrtoeol(win->x_win);
    194 	wmove(win->x_win, win->x_line, win->x_col);
    195 }
    196