Home | History | Annotate | Line # | Download | only in libcurses
screen.c revision 1.31
      1 /*	$NetBSD: screen.c,v 1.31 2017/01/31 09:17:53 roy Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1981, 1993, 1994
      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[] = "@(#)screen.c	8.2 (blymn) 11/27/2001";
     36 #else
     37 __RCSID("$NetBSD: screen.c,v 1.31 2017/01/31 09:17:53 roy Exp $");
     38 #endif
     39 #endif					/* not lint */
     40 
     41 #include <stdlib.h>
     42 
     43 #include "curses.h"
     44 #include "curses_private.h"
     45 
     46 static int filtered;
     47 
     48 
     49 static void	 __delscreen(SCREEN *);
     50 
     51 /*
     52  * set_term --
     53  *      Change the term to the given screen.
     54  *
     55  */
     56 SCREEN *
     57 set_term(SCREEN *new)
     58 {
     59 	SCREEN *old_screen = _cursesi_screen;
     60 
     61 	if (_cursesi_screen != NULL) {
     62 		  /* save changes made to the current screen... */
     63 		old_screen->echoit = __echoit;
     64 		old_screen->pfast = __pfast;
     65 		old_screen->rawmode = __rawmode;
     66 		old_screen->noqch = __noqch;
     67 		old_screen->COLS = COLS;
     68 		old_screen->LINES = LINES + __rippedlines(old_screen);
     69 		old_screen->ESCDELAY = ESCDELAY;
     70 		old_screen->TABSIZE = TABSIZE;
     71 		old_screen->COLORS = COLORS;
     72 		old_screen->COLOR_PAIRS = COLOR_PAIRS;
     73 		old_screen->GT = __GT;
     74 		old_screen->NONL = __NONL;
     75 		old_screen->UPPERCASE = __UPPERCASE;
     76 	}
     77 
     78 	_cursesi_screen = new;
     79 
     80 	__echoit = new->echoit;
     81         __pfast = new->pfast;
     82 	__rawmode = new->rawmode;
     83 	__noqch = new->noqch;
     84 	COLS = new->COLS;
     85 	LINES = new->LINES - __rippedlines(new);
     86 	ESCDELAY = new->ESCDELAY;
     87 	TABSIZE = new->TABSIZE;
     88 	COLORS = new->COLORS;
     89 	COLOR_PAIRS = new->COLOR_PAIRS;
     90 	__GT = new->GT;
     91 	__NONL = new->NONL;
     92 	__UPPERCASE = new->UPPERCASE;
     93 
     94 	_cursesi_resetterm(new);
     95 
     96 	curscr = new->curscr;
     97 	clearok(curscr, new->clearok);
     98 	stdscr = new->stdscr;
     99 	__virtscr = new->__virtscr;
    100 
    101 	_cursesi_reset_acs(new);
    102 #ifdef HAVE_WCHAR
    103 	_cursesi_reset_wacs(new);
    104 #endif /* HAVE_WCHAR */
    105 
    106 #ifdef DEBUG
    107 	__CTRACE(__CTRACE_SCREEN, "set_term: LINES = %d, COLS = %d\n",
    108 	    LINES, COLS);
    109 #endif
    110 
    111 	return old_screen;
    112 }
    113 
    114 /*
    115  * newterm --
    116  *      Set up a new screen.
    117  *
    118  */
    119 SCREEN *
    120 newterm(char *type, FILE *outfd, FILE *infd)
    121 {
    122 	SCREEN *new_screen;
    123 	char *sp;
    124 	int rtop;
    125 
    126 	sp = type;
    127 	if (type == NULL && (sp = getenv("TERM")) == NULL)
    128 		return NULL;
    129 
    130 	if ((new_screen = calloc(1, sizeof(SCREEN))) == NULL)
    131 		return NULL;
    132 
    133 #ifdef DEBUG
    134 	__CTRACE(__CTRACE_INIT, "newterm\n");
    135 #endif
    136 
    137 	new_screen->infd = infd;
    138 	new_screen->checkfd = fileno(infd);
    139 	new_screen->outfd = outfd;
    140 	new_screen->echoit = new_screen->nl = 1;
    141 	new_screen->pfast = new_screen->rawmode = new_screen->noqch = 0;
    142 	new_screen->filtered = filtered;
    143 	filtered = FALSE; /* filter() must preceed each newterm() */
    144 	new_screen->nca = A_NORMAL;
    145 	new_screen->color_type = COLOR_NONE;
    146 	new_screen->COLOR_PAIRS = 0;
    147 	new_screen->old_mode = 2;
    148 	new_screen->stdbuf = NULL;
    149 	new_screen->stdscr = NULL;
    150 	new_screen->curscr = NULL;
    151 	new_screen->__virtscr = NULL;
    152 	new_screen->curwin = 0;
    153 	new_screen->notty = FALSE;
    154 	new_screen->half_delay = FALSE;
    155 	new_screen->resized = 0;
    156 	new_screen->unget_len = 32;
    157 
    158 	if ((new_screen->unget_list =
    159 	    malloc(sizeof(wchar_t) * new_screen->unget_len)) == NULL)
    160 	{
    161 		goto error_exit;
    162 	}
    163 	new_screen->unget_pos = 0;
    164 
    165 	if (_cursesi_gettmode(new_screen) == ERR)
    166 		goto error_exit;
    167 
    168 	if (_cursesi_setterm((char *)sp, new_screen) == ERR)
    169 		goto error_exit;
    170 
    171 	/* Need either homing or cursor motion for refreshes */
    172 	if (!t_cursor_home(new_screen->term) &&
    173 	    !t_cursor_address(new_screen->term))
    174 		goto error_exit;
    175 
    176 	new_screen->winlistp = NULL;
    177 
    178 	if ((new_screen->curscr = __newwin(new_screen, 0,
    179 	    0, 0, 0, FALSE)) == NULL)
    180 		goto error_exit;
    181 
    182 	if ((new_screen->__virtscr = __newwin(new_screen, 0,
    183 	    0, 0, 0, FALSE)) == NULL)
    184 		goto error_exit;
    185 
    186 	/* If Soft Label Keys are setup, they will ripoffline. */
    187 	if (__slk_init(new_screen) == ERR)
    188 		goto error_exit;
    189 
    190 	if (__ripoffscreen(new_screen, &rtop) == ERR)
    191 		goto error_exit;
    192 
    193 	new_screen->stdscr = __newwin(new_screen, LINES, 0, rtop, 0, FALSE);
    194 	if (new_screen->stdscr == NULL)
    195 		goto error_exit;
    196 
    197 	clearok(new_screen->stdscr, 1);
    198 
    199 	__init_getch(new_screen);
    200 	__init_acs(new_screen);
    201 #ifdef HAVE_WCHAR
    202 	__init_get_wch(new_screen);
    203 	__init_wacs(new_screen);
    204 #endif /* HAVE_WCHAR */
    205 
    206 	__set_stophandler();
    207 	__set_winchhandler();
    208 
    209 	  /*
    210 	   * bleh - it seems that apps expect the first newterm to set
    211 	   * up the curses screen.... emulate this.
    212 	   */
    213 	if (_cursesi_screen == NULL || _cursesi_screen->endwin) {
    214 		set_term(new_screen);
    215 	}
    216 
    217 #ifdef DEBUG
    218 	__CTRACE(__CTRACE_SCREEN, "newterm: LINES = %d, COLS = %d\n",
    219 	    LINES, COLS);
    220 #endif
    221 	__startwin(new_screen);
    222 
    223 	return new_screen;
    224 
    225   error_exit:
    226 	__delscreen(new_screen);
    227 	free(new_screen->unget_list);
    228 
    229 	free(new_screen);
    230 	return NULL;
    231 }
    232 
    233 /*
    234  * delscreen --
    235  *   Free resources used by the given screen and destroy it.
    236  *
    237  */
    238 void
    239 delscreen(SCREEN *screen)
    240 {
    241 
    242 #ifdef DEBUG
    243 	__CTRACE(__CTRACE_SCREEN, "delscreen(%p)\n", screen);
    244 #endif
    245 
    246 	__delscreen(screen);
    247 
    248 	  /* free the storage of the keymaps */
    249 	_cursesi_free_keymap(screen->base_keymap);
    250 
    251 	  /* free the Soft Label Keys */
    252 	__slk_free(screen);
    253 
    254 	free(screen->stdbuf);
    255 	free(screen->unget_list);
    256 	if (_cursesi_screen == screen)
    257 		_cursesi_screen = NULL;
    258 	free(screen);
    259 }
    260 
    261 static void
    262 __delscreen(SCREEN *screen)
    263 {
    264         struct __winlist *list;
    265 
    266 	  /* free up the terminfo stuff */
    267 	if (screen->term != NULL)
    268 		del_curterm(screen->term);
    269 
    270 	  /* walk the window list and kill all the parent windows */
    271 	while ((list = screen->winlistp) != NULL) {
    272 		delwin(list->winp);
    273 		if (list == screen->winlistp)
    274 			/* sanity - abort if window didn't remove itself */
    275 			break;
    276 	}
    277 }
    278