Home | History | Annotate | Line # | Download | only in libcurses
screen.c revision 1.14
      1  1.14     dsl /*	$NetBSD: screen.c,v 1.14 2003/07/30 11:19:01 dsl 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.1   blymn  * 3. All advertising materials mentioning features or use of this software
     16   1.1   blymn  *    must display the following acknowledgement:
     17   1.1   blymn  *	This product includes software developed by the University of
     18   1.1   blymn  *	California, Berkeley and its contributors.
     19   1.1   blymn  * 4. Neither the name of the University nor the names of its contributors
     20   1.1   blymn  *    may be used to endorse or promote products derived from this software
     21   1.1   blymn  *    without specific prior written permission.
     22   1.1   blymn  *
     23   1.1   blymn  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1   blymn  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1   blymn  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1   blymn  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1   blymn  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1   blymn  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1   blymn  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1   blymn  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1   blymn  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1   blymn  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1   blymn  * SUCH DAMAGE.
     34   1.1   blymn  */
     35   1.1   blymn 
     36   1.1   blymn #include <sys/cdefs.h>
     37   1.1   blymn #ifndef lint
     38   1.1   blymn #if 0
     39   1.1   blymn static char sccsid[] = "@(#)screen.c	8.2 (blymn) 11/27/2001";
     40   1.1   blymn #else
     41  1.14     dsl __RCSID("$NetBSD: screen.c,v 1.14 2003/07/30 11:19:01 dsl Exp $");
     42   1.1   blymn #endif
     43   1.1   blymn #endif					/* not lint */
     44   1.1   blymn 
     45   1.1   blymn #include <stdlib.h>
     46   1.1   blymn 
     47   1.1   blymn #include "curses.h"
     48   1.1   blymn #include "curses_private.h"
     49   1.1   blymn 
     50   1.1   blymn /*
     51   1.1   blymn  * set_term --
     52   1.1   blymn  *      Change the term to the given screen.
     53   1.1   blymn  *
     54   1.1   blymn  */
     55   1.1   blymn SCREEN *
     56   1.1   blymn set_term(SCREEN *new)
     57   1.1   blymn {
     58   1.4   blymn 	SCREEN *old_screen = _cursesi_screen;
     59   1.1   blymn 
     60   1.1   blymn 	if (_cursesi_screen != NULL) {
     61   1.1   blymn 		  /* save changes made to the current screen... */
     62   1.1   blymn 		old_screen->echoit = __echoit;
     63   1.1   blymn 		old_screen->pfast = __pfast;
     64   1.1   blymn 		old_screen->rawmode = __rawmode;
     65   1.1   blymn 		old_screen->noqch = __noqch;
     66   1.1   blymn 		old_screen->COLS = COLS;
     67   1.1   blymn 		old_screen->LINES = LINES;
     68   1.1   blymn 		old_screen->COLORS = COLORS;
     69   1.1   blymn 		old_screen->COLOR_PAIRS = COLOR_PAIRS;
     70   1.1   blymn 		old_screen->GT = __GT;
     71   1.1   blymn 		old_screen->NONL = __NONL;
     72   1.1   blymn 		old_screen->UPPERCASE = __UPPERCASE;
     73   1.1   blymn 	}
     74   1.5   blymn 
     75   1.1   blymn 	_cursesi_screen = new;
     76   1.5   blymn 
     77   1.1   blymn 	__echoit = new->echoit;
     78   1.1   blymn         __pfast = new->pfast;
     79   1.1   blymn 	__rawmode = new->rawmode;
     80   1.1   blymn 	__noqch = new->noqch;
     81   1.1   blymn 	COLS = new->COLS;
     82   1.1   blymn 	LINES = new->LINES;
     83   1.1   blymn 	COLORS = new->COLORS;
     84   1.1   blymn 	COLOR_PAIRS = new->COLOR_PAIRS;
     85   1.1   blymn 	__GT = new->GT;
     86   1.1   blymn 	__NONL = new->NONL;
     87   1.1   blymn 	__UPPERCASE = new->UPPERCASE;
     88   1.1   blymn 
     89   1.1   blymn 	_cursesi_resetterm(new);
     90   1.5   blymn 
     91   1.1   blymn 	curscr = new->curscr;
     92   1.1   blymn 	clearok(curscr, new->clearok);
     93   1.1   blymn 	stdscr = new->stdscr;
     94   1.1   blymn 	__virtscr = new->__virtscr;
     95   1.1   blymn 
     96   1.1   blymn 	_cursesi_reset_acs(new);
     97   1.1   blymn 
     98   1.1   blymn #ifdef DEBUG
     99   1.1   blymn 	__CTRACE("initscr: LINES = %d, COLS = %d\n", LINES, COLS);
    100   1.1   blymn #endif
    101   1.1   blymn 
    102   1.1   blymn 	return old_screen;
    103   1.1   blymn }
    104   1.1   blymn 
    105   1.1   blymn /*
    106   1.1   blymn  * newterm --
    107   1.1   blymn  *      Set up a new screen.
    108   1.1   blymn  *
    109   1.1   blymn  */
    110   1.1   blymn SCREEN *
    111   1.1   blymn newterm(char *type, FILE *outfd, FILE *infd)
    112   1.1   blymn {
    113   1.1   blymn 	SCREEN *new_screen;
    114   1.1   blymn 	char *sp;
    115   1.1   blymn 
    116   1.1   blymn 	sp = type;
    117   1.1   blymn 	if ((type == NULL) && (sp = getenv("TERM")) == NULL)
    118   1.1   blymn 		return NULL;
    119   1.5   blymn 
    120   1.1   blymn 	if ((new_screen = (SCREEN *) malloc(sizeof(SCREEN))) == NULL)
    121   1.1   blymn 		return NULL;
    122   1.1   blymn 
    123   1.1   blymn 	new_screen->infd = infd;
    124   1.1   blymn 	new_screen->outfd = outfd;
    125  1.13     jdc 	new_screen->echoit = new_screen->nl = 1;
    126   1.1   blymn         new_screen->pfast = new_screen->rawmode = new_screen->noqch = 0;
    127   1.1   blymn 	new_screen->nca = A_NORMAL;
    128   1.1   blymn 	new_screen->color_type = COLOR_NONE;
    129   1.7     jdc 	new_screen->COLOR_PAIRS = 0;
    130   1.1   blymn 	new_screen->old_mode = 2;
    131   1.1   blymn 	new_screen->stdbuf = NULL;
    132   1.1   blymn 	new_screen->stdscr = NULL;
    133   1.1   blymn 	new_screen->curscr = NULL;
    134   1.1   blymn 	new_screen->__virtscr = NULL;
    135   1.1   blymn 	new_screen->curwin = 0;
    136   1.6  itojun 	new_screen->notty = FALSE;
    137   1.9   blymn 	new_screen->half_delay = FALSE;
    138   1.5   blymn 
    139   1.1   blymn 	if (_cursesi_gettmode(new_screen) == ERR)
    140   1.1   blymn 		goto error_exit;
    141   1.1   blymn 
    142   1.1   blymn 	if (_cursesi_setterm((char *)sp, new_screen) == ERR)
    143   1.1   blymn 		goto error_exit;
    144   1.1   blymn 
    145   1.1   blymn 	/* Need either homing or cursor motion for refreshes */
    146   1.1   blymn 	if (!new_screen->tc_ho && !new_screen->tc_cm)
    147   1.1   blymn 		goto error_exit;
    148   1.5   blymn 
    149   1.1   blymn 	new_screen->winlistp = NULL;
    150   1.1   blymn 
    151  1.12     dsl 	if ((new_screen->curscr = __newwin(new_screen, 0,
    152  1.12     dsl 	    0, 0, 0, FALSE)) == ERR)
    153   1.1   blymn 		goto error_exit;
    154   1.1   blymn 
    155  1.12     dsl 	if ((new_screen->stdscr = __newwin(new_screen, 0,
    156  1.12     dsl 	    0, 0, 0, FALSE)) == ERR) {
    157   1.1   blymn 		delwin(new_screen->curscr);
    158   1.1   blymn 		goto error_exit;
    159   1.1   blymn 	}
    160   1.1   blymn 
    161  1.11     jdc 	clearok(new_screen->stdscr, 1);
    162  1.11     jdc 
    163  1.12     dsl 	if ((new_screen->__virtscr = __newwin(new_screen, 0,
    164  1.12     dsl 	    0, 0, 0, FALSE)) == ERR) {
    165   1.1   blymn 		delwin(new_screen->curscr);
    166   1.1   blymn 		delwin(new_screen->stdscr);
    167   1.1   blymn 		goto error_exit;
    168   1.1   blymn 	}
    169   1.1   blymn 
    170   1.1   blymn 	__init_getch(new_screen);
    171   1.1   blymn 
    172   1.1   blymn 	__init_acs(new_screen);
    173   1.1   blymn 
    174   1.1   blymn 	__set_stophandler();
    175   1.1   blymn 
    176   1.1   blymn 	  /*
    177   1.1   blymn 	   * bleh - it seems that apps expect the first newterm to set
    178   1.1   blymn 	   * up the curses screen.... emulate this.
    179   1.1   blymn 	   */
    180   1.3   blymn 	if (_cursesi_screen == NULL || _cursesi_screen->endwin) {
    181   1.1   blymn 		set_term(new_screen);
    182   1.1   blymn 	}
    183   1.5   blymn 
    184   1.1   blymn #ifdef DEBUG
    185   1.1   blymn 	__CTRACE("newterm: LINES = %d, COLS = %d\n", LINES, COLS);
    186   1.1   blymn #endif
    187   1.1   blymn 	__startwin(new_screen);
    188   1.1   blymn 
    189   1.1   blymn 	return new_screen;
    190   1.1   blymn 
    191   1.1   blymn   error_exit:
    192   1.1   blymn 	free(new_screen);
    193   1.1   blymn 	return NULL;
    194   1.1   blymn }
    195   1.1   blymn 
    196   1.1   blymn /*
    197   1.1   blymn  * delscreen --
    198   1.1   blymn  *   Free resources used by the given screen and destroy it.
    199   1.1   blymn  *
    200   1.1   blymn  */
    201   1.1   blymn void
    202   1.1   blymn delscreen(SCREEN *screen)
    203   1.1   blymn {
    204  1.14     dsl         struct __winlist *list;
    205   1.1   blymn 
    206   1.1   blymn 	  /* free up the termcap entry stuff */
    207   1.1   blymn 	t_freent(screen->cursesi_genbuf);
    208   1.1   blymn 
    209   1.1   blymn 	  /* walk the window list and kill all the parent windows */
    210  1.14     dsl 	while ((list = screen->winlistp)) {
    211  1.14     dsl 		delwin(list->winp);
    212  1.14     dsl 		if (list == screen->winlistp)
    213  1.14     dsl 			/* sanity - abort if window didn't remove itself */
    214  1.14     dsl 			break;
    215   1.1   blymn 	}
    216   1.5   blymn 
    217   1.1   blymn 	  /* free the storage of the keymaps */
    218   1.1   blymn 	_cursesi_free_keymap(screen->base_keymap);
    219   1.1   blymn 
    220   1.1   blymn 	free(screen->stdbuf);
    221   1.1   blymn 	free(screen);
    222   1.1   blymn }
    223