Home | History | Annotate | Line # | Download | only in libcurses
screen.c revision 1.27
      1  1.27       roy /*	$NetBSD: screen.c,v 1.27 2017/01/06 13:53:18 roy Exp $	*/
      2   1.1     blymn 
      3   1.1     blymn /*
      4   1.1     blymn  * Copyright (c) 1981, 1993, 1994
      5   1.1     blymn  *	The Regents of the University of California.  All rights reserved.
      6   1.1     blymn  *
      7   1.1     blymn  * Redistribution and use in source and binary forms, with or without
      8   1.1     blymn  * modification, are permitted provided that the following conditions
      9   1.1     blymn  * are met:
     10   1.1     blymn  * 1. Redistributions of source code must retain the above copyright
     11   1.1     blymn  *    notice, this list of conditions and the following disclaimer.
     12   1.1     blymn  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1     blymn  *    notice, this list of conditions and the following disclaimer in the
     14   1.1     blymn  *    documentation and/or other materials provided with the distribution.
     15  1.15       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1     blymn  *    may be used to endorse or promote products derived from this software
     17   1.1     blymn  *    without specific prior written permission.
     18   1.1     blymn  *
     19   1.1     blymn  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1     blymn  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1     blymn  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1     blymn  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1     blymn  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1     blymn  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1     blymn  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1     blymn  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1     blymn  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1     blymn  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1     blymn  * SUCH DAMAGE.
     30   1.1     blymn  */
     31   1.1     blymn 
     32   1.1     blymn #include <sys/cdefs.h>
     33   1.1     blymn #ifndef lint
     34   1.1     blymn #if 0
     35   1.1     blymn static char sccsid[] = "@(#)screen.c	8.2 (blymn) 11/27/2001";
     36   1.1     blymn #else
     37  1.27       roy __RCSID("$NetBSD: screen.c,v 1.27 2017/01/06 13:53:18 roy Exp $");
     38   1.1     blymn #endif
     39   1.1     blymn #endif					/* not lint */
     40   1.1     blymn 
     41   1.1     blymn #include <stdlib.h>
     42   1.1     blymn 
     43   1.1     blymn #include "curses.h"
     44   1.1     blymn #include "curses_private.h"
     45   1.1     blymn 
     46  1.25       roy static int filtered;
     47  1.25       roy 
     48  1.25       roy /*
     49  1.25       roy  * filter has to be called before either initscr or newterm.
     50  1.25       roy  */
     51  1.25       roy void
     52  1.25       roy filter(void)
     53  1.25       roy {
     54  1.25       roy 
     55  1.25       roy 	filtered = TRUE;
     56  1.25       roy }
     57  1.25       roy 
     58   1.1     blymn /*
     59   1.1     blymn  * set_term --
     60   1.1     blymn  *      Change the term to the given screen.
     61   1.1     blymn  *
     62   1.1     blymn  */
     63   1.1     blymn SCREEN *
     64   1.1     blymn set_term(SCREEN *new)
     65   1.1     blymn {
     66   1.4     blymn 	SCREEN *old_screen = _cursesi_screen;
     67   1.1     blymn 
     68   1.1     blymn 	if (_cursesi_screen != NULL) {
     69   1.1     blymn 		  /* save changes made to the current screen... */
     70   1.1     blymn 		old_screen->echoit = __echoit;
     71   1.1     blymn 		old_screen->pfast = __pfast;
     72   1.1     blymn 		old_screen->rawmode = __rawmode;
     73   1.1     blymn 		old_screen->noqch = __noqch;
     74   1.1     blymn 		old_screen->COLS = COLS;
     75   1.1     blymn 		old_screen->LINES = LINES;
     76   1.1     blymn 		old_screen->COLORS = COLORS;
     77   1.1     blymn 		old_screen->COLOR_PAIRS = COLOR_PAIRS;
     78   1.1     blymn 		old_screen->GT = __GT;
     79   1.1     blymn 		old_screen->NONL = __NONL;
     80   1.1     blymn 		old_screen->UPPERCASE = __UPPERCASE;
     81   1.1     blymn 	}
     82   1.5     blymn 
     83   1.1     blymn 	_cursesi_screen = new;
     84   1.5     blymn 
     85   1.1     blymn 	__echoit = new->echoit;
     86   1.1     blymn         __pfast = new->pfast;
     87   1.1     blymn 	__rawmode = new->rawmode;
     88   1.1     blymn 	__noqch = new->noqch;
     89   1.1     blymn 	COLS = new->COLS;
     90   1.1     blymn 	LINES = new->LINES;
     91   1.1     blymn 	COLORS = new->COLORS;
     92   1.1     blymn 	COLOR_PAIRS = new->COLOR_PAIRS;
     93   1.1     blymn 	__GT = new->GT;
     94   1.1     blymn 	__NONL = new->NONL;
     95   1.1     blymn 	__UPPERCASE = new->UPPERCASE;
     96   1.1     blymn 
     97   1.1     blymn 	_cursesi_resetterm(new);
     98   1.5     blymn 
     99   1.1     blymn 	curscr = new->curscr;
    100   1.1     blymn 	clearok(curscr, new->clearok);
    101   1.1     blymn 	stdscr = new->stdscr;
    102   1.1     blymn 	__virtscr = new->__virtscr;
    103   1.1     blymn 
    104   1.1     blymn 	_cursesi_reset_acs(new);
    105  1.20     blymn #ifdef HAVE_WCHAR
    106  1.27       roy 	_cursesi_reset_wacs(new);
    107  1.20     blymn #endif /* HAVE_WCHAR */
    108   1.1     blymn 
    109   1.1     blymn #ifdef DEBUG
    110  1.19       jdc 	__CTRACE(__CTRACE_SCREEN, "set_term: LINES = %d, COLS = %d\n",
    111  1.19       jdc 	    LINES, COLS);
    112   1.1     blymn #endif
    113   1.1     blymn 
    114   1.1     blymn 	return old_screen;
    115   1.1     blymn }
    116   1.1     blymn 
    117   1.1     blymn /*
    118   1.1     blymn  * newterm --
    119   1.1     blymn  *      Set up a new screen.
    120   1.1     blymn  *
    121   1.1     blymn  */
    122   1.1     blymn SCREEN *
    123   1.1     blymn newterm(char *type, FILE *outfd, FILE *infd)
    124   1.1     blymn {
    125   1.1     blymn 	SCREEN *new_screen;
    126   1.1     blymn 	char *sp;
    127   1.1     blymn 
    128   1.1     blymn 	sp = type;
    129  1.27       roy 	if (type == NULL && (sp = getenv("TERM")) == NULL)
    130   1.1     blymn 		return NULL;
    131   1.5     blymn 
    132  1.23  dholland 	if ((new_screen = calloc(1, sizeof(SCREEN))) == NULL)
    133   1.1     blymn 		return NULL;
    134   1.1     blymn 
    135  1.20     blymn #ifdef DEBUG
    136  1.20     blymn 	__CTRACE(__CTRACE_INIT, "newterm\n");
    137  1.20     blymn #endif
    138  1.20     blymn 
    139   1.1     blymn 	new_screen->infd = infd;
    140  1.26       roy 	new_screen->checkfd = fileno(infd);
    141   1.1     blymn 	new_screen->outfd = outfd;
    142  1.13       jdc 	new_screen->echoit = new_screen->nl = 1;
    143  1.20     blymn 	new_screen->pfast = new_screen->rawmode = new_screen->noqch = 0;
    144  1.25       roy 	new_screen->filtered = filtered;
    145  1.25       roy 	filtered = FALSE; /* filter() must preceed each newterm() */
    146   1.1     blymn 	new_screen->nca = A_NORMAL;
    147   1.1     blymn 	new_screen->color_type = COLOR_NONE;
    148   1.7       jdc 	new_screen->COLOR_PAIRS = 0;
    149   1.1     blymn 	new_screen->old_mode = 2;
    150   1.1     blymn 	new_screen->stdbuf = NULL;
    151   1.1     blymn 	new_screen->stdscr = NULL;
    152   1.1     blymn 	new_screen->curscr = NULL;
    153   1.1     blymn 	new_screen->__virtscr = NULL;
    154   1.1     blymn 	new_screen->curwin = 0;
    155   1.6    itojun 	new_screen->notty = FALSE;
    156   1.9     blymn 	new_screen->half_delay = FALSE;
    157  1.16       jdc 	new_screen->resized = 0;
    158  1.21       jdc 	new_screen->unget_len = 32;
    159  1.21       jdc 
    160  1.21       jdc 	if ((new_screen->unget_list =
    161  1.21       jdc 	    malloc(sizeof(wchar_t) * new_screen->unget_len)) == NULL) {
    162  1.21       jdc 		goto error_exit;
    163  1.21       jdc 	}
    164  1.21       jdc 	new_screen->unget_pos = 0;
    165   1.5     blymn 
    166   1.1     blymn 	if (_cursesi_gettmode(new_screen) == ERR)
    167   1.1     blymn 		goto error_exit;
    168   1.1     blymn 
    169   1.1     blymn 	if (_cursesi_setterm((char *)sp, new_screen) == ERR)
    170   1.1     blymn 		goto error_exit;
    171   1.1     blymn 
    172   1.1     blymn 	/* Need either homing or cursor motion for refreshes */
    173  1.22       roy 	if (!t_cursor_home(new_screen->term) &&
    174  1.22       roy 	    !t_cursor_address(new_screen->term))
    175   1.1     blymn 		goto error_exit;
    176   1.5     blymn 
    177   1.1     blymn 	new_screen->winlistp = NULL;
    178   1.1     blymn 
    179  1.12       dsl 	if ((new_screen->curscr = __newwin(new_screen, 0,
    180  1.17       jdc 	    0, 0, 0, FALSE)) == NULL)
    181   1.1     blymn 		goto error_exit;
    182   1.1     blymn 
    183  1.12       dsl 	if ((new_screen->stdscr = __newwin(new_screen, 0,
    184  1.17       jdc 	    0, 0, 0, FALSE)) == NULL) {
    185   1.1     blymn 		delwin(new_screen->curscr);
    186   1.1     blymn 		goto error_exit;
    187   1.1     blymn 	}
    188   1.1     blymn 
    189  1.11       jdc 	clearok(new_screen->stdscr, 1);
    190  1.11       jdc 
    191  1.12       dsl 	if ((new_screen->__virtscr = __newwin(new_screen, 0,
    192  1.17       jdc 	    0, 0, 0, FALSE)) == NULL) {
    193   1.1     blymn 		delwin(new_screen->curscr);
    194   1.1     blymn 		delwin(new_screen->stdscr);
    195   1.1     blymn 		goto error_exit;
    196   1.1     blymn 	}
    197   1.1     blymn 
    198   1.1     blymn 	__init_getch(new_screen);
    199   1.1     blymn 	__init_acs(new_screen);
    200  1.20     blymn #ifdef HAVE_WCHAR
    201  1.20     blymn 	__init_get_wch( new_screen );
    202  1.20     blymn 	__init_wacs(new_screen);
    203  1.20     blymn #endif /* HAVE_WCHAR */
    204   1.1     blymn 
    205   1.1     blymn 	__set_stophandler();
    206  1.16       jdc 	__set_winchhandler();
    207   1.1     blymn 
    208   1.1     blymn 	  /*
    209   1.1     blymn 	   * bleh - it seems that apps expect the first newterm to set
    210   1.1     blymn 	   * up the curses screen.... emulate this.
    211   1.1     blymn 	   */
    212   1.3     blymn 	if (_cursesi_screen == NULL || _cursesi_screen->endwin) {
    213   1.1     blymn 		set_term(new_screen);
    214   1.1     blymn 	}
    215   1.5     blymn 
    216   1.1     blymn #ifdef DEBUG
    217  1.19       jdc 	__CTRACE(__CTRACE_SCREEN, "newterm: LINES = %d, COLS = %d\n",
    218  1.19       jdc 	    LINES, COLS);
    219   1.1     blymn #endif
    220   1.1     blymn 	__startwin(new_screen);
    221   1.1     blymn 
    222   1.1     blymn 	return new_screen;
    223   1.1     blymn 
    224   1.1     blymn   error_exit:
    225  1.24  christos 	if (new_screen->term != NULL)
    226  1.24  christos 		(void)del_curterm(new_screen->term);
    227  1.24  christos 	free(new_screen->unget_list);
    228  1.24  christos 
    229   1.1     blymn 	free(new_screen);
    230   1.1     blymn 	return NULL;
    231   1.1     blymn }
    232   1.1     blymn 
    233   1.1     blymn /*
    234   1.1     blymn  * delscreen --
    235   1.1     blymn  *   Free resources used by the given screen and destroy it.
    236   1.1     blymn  *
    237   1.1     blymn  */
    238   1.1     blymn void
    239   1.1     blymn delscreen(SCREEN *screen)
    240   1.1     blymn {
    241  1.14       dsl         struct __winlist *list;
    242   1.1     blymn 
    243  1.18       jdc #ifdef DEBUG
    244  1.19       jdc 	__CTRACE(__CTRACE_SCREEN, "delscreen(%p)\n", screen);
    245  1.18       jdc #endif
    246  1.22       roy 	  /* free up the terminfo stuff */
    247  1.22       roy 	del_curterm(screen->term);
    248   1.1     blymn 
    249   1.1     blymn 	  /* walk the window list and kill all the parent windows */
    250  1.17       jdc 	while ((list = screen->winlistp) != NULL) {
    251  1.14       dsl 		delwin(list->winp);
    252  1.14       dsl 		if (list == screen->winlistp)
    253  1.14       dsl 			/* sanity - abort if window didn't remove itself */
    254  1.14       dsl 			break;
    255   1.1     blymn 	}
    256   1.5     blymn 
    257   1.1     blymn 	  /* free the storage of the keymaps */
    258   1.1     blymn 	_cursesi_free_keymap(screen->base_keymap);
    259   1.1     blymn 
    260   1.1     blymn 	free(screen->stdbuf);
    261  1.24  christos 	free(screen->unget_list);
    262  1.18       jdc 	if (_cursesi_screen == screen)
    263  1.18       jdc 		_cursesi_screen = NULL;
    264   1.1     blymn 	free(screen);
    265   1.1     blymn }
    266