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