Home | History | Annotate | Line # | Download | only in libcurses
screen.c revision 1.16
      1 /*	$NetBSD: screen.c,v 1.16 2004/03/22 18:57:10 jdc 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.16 2004/03/22 18:57:10 jdc Exp $");
     38 #endif
     39 #endif					/* not lint */
     40 
     41 #include <stdlib.h>
     42 
     43 #include "curses.h"
     44 #include "curses_private.h"
     45 
     46 /*
     47  * set_term --
     48  *      Change the term to the given screen.
     49  *
     50  */
     51 SCREEN *
     52 set_term(SCREEN *new)
     53 {
     54 	SCREEN *old_screen = _cursesi_screen;
     55 
     56 	if (_cursesi_screen != NULL) {
     57 		  /* save changes made to the current screen... */
     58 		old_screen->echoit = __echoit;
     59 		old_screen->pfast = __pfast;
     60 		old_screen->rawmode = __rawmode;
     61 		old_screen->noqch = __noqch;
     62 		old_screen->COLS = COLS;
     63 		old_screen->LINES = LINES;
     64 		old_screen->COLORS = COLORS;
     65 		old_screen->COLOR_PAIRS = COLOR_PAIRS;
     66 		old_screen->GT = __GT;
     67 		old_screen->NONL = __NONL;
     68 		old_screen->UPPERCASE = __UPPERCASE;
     69 	}
     70 
     71 	_cursesi_screen = new;
     72 
     73 	__echoit = new->echoit;
     74         __pfast = new->pfast;
     75 	__rawmode = new->rawmode;
     76 	__noqch = new->noqch;
     77 	COLS = new->COLS;
     78 	LINES = new->LINES;
     79 	COLORS = new->COLORS;
     80 	COLOR_PAIRS = new->COLOR_PAIRS;
     81 	__GT = new->GT;
     82 	__NONL = new->NONL;
     83 	__UPPERCASE = new->UPPERCASE;
     84 
     85 	_cursesi_resetterm(new);
     86 
     87 	curscr = new->curscr;
     88 	clearok(curscr, new->clearok);
     89 	stdscr = new->stdscr;
     90 	__virtscr = new->__virtscr;
     91 
     92 	_cursesi_reset_acs(new);
     93 
     94 #ifdef DEBUG
     95 	__CTRACE("initscr: LINES = %d, COLS = %d\n", LINES, COLS);
     96 #endif
     97 
     98 	return old_screen;
     99 }
    100 
    101 /*
    102  * newterm --
    103  *      Set up a new screen.
    104  *
    105  */
    106 SCREEN *
    107 newterm(char *type, FILE *outfd, FILE *infd)
    108 {
    109 	SCREEN *new_screen;
    110 	char *sp;
    111 
    112 	sp = type;
    113 	if ((type == NULL) && (sp = getenv("TERM")) == NULL)
    114 		return NULL;
    115 
    116 	if ((new_screen = (SCREEN *) malloc(sizeof(SCREEN))) == NULL)
    117 		return NULL;
    118 
    119 	new_screen->infd = infd;
    120 	new_screen->outfd = outfd;
    121 	new_screen->echoit = new_screen->nl = 1;
    122         new_screen->pfast = new_screen->rawmode = new_screen->noqch = 0;
    123 	new_screen->nca = A_NORMAL;
    124 	new_screen->color_type = COLOR_NONE;
    125 	new_screen->COLOR_PAIRS = 0;
    126 	new_screen->old_mode = 2;
    127 	new_screen->stdbuf = NULL;
    128 	new_screen->stdscr = NULL;
    129 	new_screen->curscr = NULL;
    130 	new_screen->__virtscr = NULL;
    131 	new_screen->curwin = 0;
    132 	new_screen->notty = FALSE;
    133 	new_screen->half_delay = FALSE;
    134 	new_screen->resized = 0;
    135 
    136 	if (_cursesi_gettmode(new_screen) == ERR)
    137 		goto error_exit;
    138 
    139 	if (_cursesi_setterm((char *)sp, new_screen) == ERR)
    140 		goto error_exit;
    141 
    142 	/* Need either homing or cursor motion for refreshes */
    143 	if (!new_screen->tc_ho && !new_screen->tc_cm)
    144 		goto error_exit;
    145 
    146 	new_screen->winlistp = NULL;
    147 
    148 	if ((new_screen->curscr = __newwin(new_screen, 0,
    149 	    0, 0, 0, FALSE)) == ERR)
    150 		goto error_exit;
    151 
    152 	if ((new_screen->stdscr = __newwin(new_screen, 0,
    153 	    0, 0, 0, FALSE)) == ERR) {
    154 		delwin(new_screen->curscr);
    155 		goto error_exit;
    156 	}
    157 
    158 	clearok(new_screen->stdscr, 1);
    159 
    160 	if ((new_screen->__virtscr = __newwin(new_screen, 0,
    161 	    0, 0, 0, FALSE)) == ERR) {
    162 		delwin(new_screen->curscr);
    163 		delwin(new_screen->stdscr);
    164 		goto error_exit;
    165 	}
    166 
    167 	__init_getch(new_screen);
    168 
    169 	__init_acs(new_screen);
    170 
    171 	__set_stophandler();
    172 	__set_winchhandler();
    173 
    174 	  /*
    175 	   * bleh - it seems that apps expect the first newterm to set
    176 	   * up the curses screen.... emulate this.
    177 	   */
    178 	if (_cursesi_screen == NULL || _cursesi_screen->endwin) {
    179 		set_term(new_screen);
    180 	}
    181 
    182 #ifdef DEBUG
    183 	__CTRACE("newterm: LINES = %d, COLS = %d\n", LINES, COLS);
    184 #endif
    185 	__startwin(new_screen);
    186 
    187 	return new_screen;
    188 
    189   error_exit:
    190 	free(new_screen);
    191 	return NULL;
    192 }
    193 
    194 /*
    195  * delscreen --
    196  *   Free resources used by the given screen and destroy it.
    197  *
    198  */
    199 void
    200 delscreen(SCREEN *screen)
    201 {
    202         struct __winlist *list;
    203 
    204 	  /* free up the termcap entry stuff */
    205 	t_freent(screen->cursesi_genbuf);
    206 
    207 	  /* walk the window list and kill all the parent windows */
    208 	while ((list = screen->winlistp)) {
    209 		delwin(list->winp);
    210 		if (list == screen->winlistp)
    211 			/* sanity - abort if window didn't remove itself */
    212 			break;
    213 	}
    214 
    215 	  /* free the storage of the keymaps */
    216 	_cursesi_free_keymap(screen->base_keymap);
    217 
    218 	free(screen->stdbuf);
    219 	free(screen);
    220 }
    221