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