Home | History | Annotate | Line # | Download | only in libcurses
color.c revision 1.20
      1  1.20     blymn /*	$NetBSD: color.c,v 1.20 2002/10/22 11:37:34 blymn Exp $	*/
      2   1.1       jdc 
      3   1.1       jdc /*
      4   1.1       jdc  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5   1.1       jdc  * All rights reserved.
      6   1.1       jdc  *
      7   1.1       jdc  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1       jdc  * by Julian Coleman.
      9   1.1       jdc  *
     10   1.1       jdc  * Redistribution and use in source and binary forms, with or without
     11   1.1       jdc  * modification, are permitted provided that the following conditions
     12   1.1       jdc  * are met:
     13   1.1       jdc  * 1. Redistributions of source code must retain the above copyright
     14   1.1       jdc  *    notice, this list of conditions and the following disclaimer.
     15   1.1       jdc  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       jdc  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       jdc  *    documentation and/or other materials provided with the distribution.
     18   1.1       jdc  * 3. All advertising materials mentioning features or use of this software
     19   1.1       jdc  *    must display the following acknowledgement:
     20   1.1       jdc  *        This product includes software developed by the NetBSD
     21   1.1       jdc  *        Foundation, Inc. and its contributors.
     22   1.1       jdc  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1       jdc  *    contributors may be used to endorse or promote products derived
     24   1.1       jdc  *    from this software without specific prior written permission.
     25   1.1       jdc  *
     26   1.1       jdc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1       jdc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1       jdc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1       jdc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1       jdc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1       jdc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1       jdc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1       jdc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1       jdc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1       jdc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1       jdc  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1       jdc  */
     38   1.8     blymn 
     39   1.8     blymn #include <sys/cdefs.h>
     40   1.8     blymn #ifndef lint
     41  1.20     blymn __RCSID("$NetBSD: color.c,v 1.20 2002/10/22 11:37:34 blymn Exp $");
     42   1.8     blymn #endif				/* not lint */
     43   1.1       jdc 
     44   1.1       jdc #include "curses.h"
     45   1.1       jdc #include "curses_private.h"
     46   1.1       jdc 
     47  1.19       jdc /* Have we initialised colours? */
     48  1.19       jdc int	__using_color = 0;
     49  1.19       jdc 
     50  1.19       jdc /* Default colour number */
     51  1.19       jdc attr_t	__default_color = 0;
     52  1.19       jdc 
     53  1.19       jdc /* Default colour pair values - white on black. */
     54  1.19       jdc struct __pair	__default_pair = {COLOR_WHITE, COLOR_BLACK, 0};
     55  1.19       jdc 
     56  1.19       jdc /* Default colour values */
     57   1.1       jdc /* Flags for colours and pairs */
     58   1.1       jdc #define	__USED		0x01
     59   1.1       jdc 
     60   1.1       jdc /* Attributes that clash with colours */
     61   1.1       jdc attr_t	__nca;
     62   1.1       jdc 
     63   1.2       jdc static void
     64  1.19       jdc __change_pair(short);
     65  1.19       jdc 
     66   1.1       jdc /*
     67   1.1       jdc  * has_colors --
     68   1.1       jdc  *	Check if terminal has colours.
     69   1.1       jdc  */
     70   1.1       jdc bool
     71   1.3     blymn has_colors(void)
     72   1.1       jdc {
     73  1.13       jdc 	if (__tc_Co > 0 && __tc_pa > 0 && ((__tc_AF != NULL &&
     74  1.13       jdc 	    __tc_AB != NULL) || __tc_Ip != NULL || __tc_Ic != NULL ||
     75  1.13       jdc 	    (__tc_Sb != NULL && __tc_Sf != NULL)))
     76   1.1       jdc 		return(TRUE);
     77   1.1       jdc 	else
     78   1.1       jdc 		return(FALSE);
     79   1.1       jdc }
     80   1.1       jdc 
     81   1.1       jdc /*
     82   1.1       jdc  * can_change_colors --
     83   1.1       jdc  *	Check if terminal can change colours.
     84   1.1       jdc  */
     85   1.1       jdc bool
     86   1.3     blymn can_change_colors(void)
     87   1.1       jdc {
     88  1.13       jdc 	if (__tc_cc)
     89   1.1       jdc 		return(TRUE);
     90   1.1       jdc 	else
     91   1.1       jdc 		return(FALSE);
     92   1.1       jdc }
     93   1.1       jdc 
     94   1.1       jdc /*
     95   1.1       jdc  * start_color --
     96   1.1       jdc  *	Initialise colour support.
     97   1.1       jdc  */
     98   1.1       jdc int
     99   1.5     blymn start_color(void)
    100   1.1       jdc {
    101  1.19       jdc 	int			 i;
    102  1.19       jdc 	attr_t			 temp_nc;
    103  1.19       jdc 	struct __winlist	*wlp;
    104  1.19       jdc 	WINDOW			*win;
    105  1.19       jdc 	int			 y, x;
    106   1.1       jdc 
    107   1.1       jdc 	if (has_colors() == FALSE)
    108   1.1       jdc 		return(ERR);
    109   1.1       jdc 
    110   1.1       jdc 	/* Max colours and colour pairs */
    111  1.13       jdc 	if (__tc_Co == -1)
    112   1.1       jdc 		COLORS = 0;
    113   1.1       jdc 	else {
    114  1.13       jdc 		COLORS = __tc_Co > MAX_COLORS ? MAX_COLORS : __tc_Co;
    115  1.13       jdc 		if (__tc_pa == -1) {
    116   1.1       jdc 			COLOR_PAIRS = 0;
    117   1.1       jdc 			COLORS = 0;
    118   1.1       jdc 		} else {
    119  1.19       jdc 			COLOR_PAIRS = (__tc_pa > MAX_PAIRS ?
    120  1.19       jdc 			    MAX_PAIRS : __tc_pa) - 1;
    121  1.19       jdc 			 /* Use the last colour pair for curses default. */
    122  1.19       jdc 			__default_color = COLOR_PAIR(COLOR_PAIRS);
    123   1.1       jdc 		}
    124   1.1       jdc 	}
    125   1.6       jdc 	if (!COLORS)
    126   1.6       jdc 		return (ERR);
    127   1.1       jdc 
    128  1.15     blymn 	_cursesi_screen->COLORS = COLORS;
    129  1.15     blymn 	_cursesi_screen->COLOR_PAIRS = COLOR_PAIRS;
    130  1.17     blymn 
    131   1.1       jdc 	/* Reset terminal colour and colour pairs. */
    132  1.13       jdc 	if (__tc_oc != NULL)
    133  1.13       jdc 		tputs(__tc_oc, 0, __cputchar);
    134  1.13       jdc 	if (__tc_op != NULL) {
    135  1.13       jdc 		tputs(__tc_op, 0, __cputchar);
    136  1.15     blymn 		curscr->wattr &= _cursesi_screen->mask_op;
    137   1.1       jdc 	}
    138   1.1       jdc 
    139   1.7       jdc 	/* Type of colour manipulation - ANSI/TEK/HP/other */
    140  1.13       jdc 	if (__tc_AF != NULL && __tc_AB != NULL)
    141  1.15     blymn 		_cursesi_screen->color_type = COLOR_ANSI;
    142  1.13       jdc 	else if (__tc_Ip != NULL)
    143  1.15     blymn 		_cursesi_screen->color_type = COLOR_HP;
    144  1.13       jdc 	else if (__tc_Ic != NULL)
    145  1.15     blymn 		_cursesi_screen->color_type = COLOR_TEK;
    146  1.13       jdc 	else if (__tc_Sb != NULL && __tc_Sf != NULL)
    147  1.15     blymn 		_cursesi_screen->color_type = COLOR_OTHER;
    148   1.1       jdc 	else
    149   1.1       jdc 		return(ERR);		/* Unsupported colour method */
    150   1.1       jdc 
    151   1.1       jdc #ifdef DEBUG
    152   1.6       jdc 	__CTRACE("start_color: COLORS = %d, COLOR_PAIRS = %d",
    153   1.6       jdc 	    COLORS, COLOR_PAIRS);
    154  1.15     blymn 	switch (_cursesi_screen->color_type) {
    155   1.1       jdc 	case COLOR_ANSI:
    156   1.6       jdc 		__CTRACE(" (ANSI style)\n");
    157   1.1       jdc 		break;
    158   1.1       jdc 	case COLOR_HP:
    159   1.6       jdc 		__CTRACE(" (HP style)\n");
    160   1.1       jdc 		break;
    161   1.1       jdc 	case COLOR_TEK:
    162   1.6       jdc 		__CTRACE(" (Tektronics style)\n");
    163   1.6       jdc 		break;
    164   1.6       jdc 	case COLOR_OTHER:
    165   1.6       jdc 		__CTRACE(" (Other style)\n");
    166   1.1       jdc 		break;
    167   1.1       jdc 	}
    168   1.1       jdc #endif
    169  1.17     blymn 
    170   1.1       jdc 	/*
    171   1.1       jdc 	 * Attributes that cannot be used with color.
    172   1.1       jdc 	 * Store these in an attr_t for wattrset()/wattron().
    173   1.1       jdc 	 */
    174  1.15     blymn 	_cursesi_screen->nca = __NORMAL;
    175  1.13       jdc 	if (__tc_NC != -1) {
    176  1.15     blymn 		temp_nc = (attr_t) t_getnum(_cursesi_screen->cursesi_genbuf, "NC");
    177   1.1       jdc 		if (temp_nc & 0x0001)
    178  1.15     blymn 			_cursesi_screen->nca |= __STANDOUT;
    179   1.1       jdc 		if (temp_nc & 0x0002)
    180  1.15     blymn 			_cursesi_screen->nca |= __UNDERSCORE;
    181   1.1       jdc 		if (temp_nc & 0x0004)
    182  1.15     blymn 			_cursesi_screen->nca |= __REVERSE;
    183   1.1       jdc 		if (temp_nc & 0x0008)
    184  1.15     blymn 			_cursesi_screen->nca |= __BLINK;
    185   1.1       jdc 		if (temp_nc & 0x0010)
    186  1.15     blymn 			_cursesi_screen->nca |= __DIM;
    187   1.1       jdc 		if (temp_nc & 0x0020)
    188  1.15     blymn 			_cursesi_screen->nca |= __BOLD;
    189   1.1       jdc 		if (temp_nc & 0x0040)
    190  1.15     blymn 			_cursesi_screen->nca |= __BLANK;
    191   1.1       jdc 		if (temp_nc & 0x0080)
    192  1.15     blymn 			_cursesi_screen->nca |= __PROTECT;
    193   1.1       jdc 		if (temp_nc & 0x0100)
    194  1.15     blymn 			_cursesi_screen->nca |= __ALTCHARSET;
    195   1.1       jdc 	}
    196   1.1       jdc #ifdef DEBUG
    197  1.15     blymn 	__CTRACE ("start_color: __nca = %d\n", _cursesi_screen->nca);
    198   1.1       jdc #endif
    199   1.9       jdc 
    200   1.9       jdc 	/* Set up initial 8 colours */
    201   1.9       jdc 	if (COLORS >= COLOR_BLACK)
    202   1.9       jdc 		(void) init_color(COLOR_BLACK, 0, 0, 0);
    203   1.9       jdc 	if (COLORS >= COLOR_RED)
    204   1.9       jdc 		(void) init_color(COLOR_RED, 1000, 0, 0);
    205   1.9       jdc 	if (COLORS >= COLOR_GREEN)
    206   1.9       jdc 		(void) init_color(COLOR_GREEN, 0, 1000, 0);
    207   1.9       jdc 	if (COLORS >= COLOR_YELLOW)
    208   1.9       jdc 		(void) init_color(COLOR_YELLOW, 1000, 1000, 0);
    209   1.9       jdc 	if (COLORS >= COLOR_BLUE)
    210   1.9       jdc 		(void) init_color(COLOR_BLUE, 0, 0, 1000);
    211   1.9       jdc 	if (COLORS >= COLOR_MAGENTA)
    212   1.9       jdc 		(void) init_color(COLOR_MAGENTA, 1000, 0, 1000);
    213   1.9       jdc 	if (COLORS >= COLOR_CYAN)
    214   1.9       jdc 		(void) init_color(COLOR_CYAN, 0, 1000, 1000);
    215   1.9       jdc 	if (COLORS >= COLOR_WHITE)
    216   1.9       jdc 		(void) init_color(COLOR_WHITE, 1000, 1000, 1000);
    217   1.9       jdc 
    218   1.9       jdc 	/* Initialise other colours */
    219   1.9       jdc 	for (i = 8; i < COLORS; i++) {
    220  1.15     blymn 		_cursesi_screen->colours[i].red = 0;
    221  1.15     blymn 		_cursesi_screen->colours[i].green = 0;
    222  1.15     blymn 		_cursesi_screen->colours[i].blue = 0;
    223  1.15     blymn 		_cursesi_screen->colours[i].flags = 0;
    224   1.9       jdc 	}
    225   1.9       jdc 
    226  1.19       jdc 	/* Initialise pair 0 to default colours. */
    227  1.19       jdc 	_cursesi_screen->colour_pairs[0].fore = -1;
    228  1.19       jdc 	_cursesi_screen->colour_pairs[0].back = -1;
    229  1.19       jdc 	_cursesi_screen->colour_pairs[0].flags = 0;
    230  1.19       jdc 
    231  1.19       jdc 	/* Initialise user colour pairs to default (white on black) */
    232  1.19       jdc 	for (i = 1; i < COLOR_PAIRS; i++) {
    233  1.15     blymn 		_cursesi_screen->colour_pairs[i].fore = COLOR_WHITE;
    234  1.15     blymn 		_cursesi_screen->colour_pairs[i].back = COLOR_BLACK;
    235  1.15     blymn 		_cursesi_screen->colour_pairs[i].flags = 0;
    236   1.9       jdc 	}
    237   1.9       jdc 
    238  1.19       jdc 	/* Initialise default colour pair. */
    239  1.19       jdc 	_cursesi_screen->colour_pairs[PAIR_NUMBER(__default_color)].fore =
    240  1.19       jdc 	    __default_pair.fore;
    241  1.19       jdc 	_cursesi_screen->colour_pairs[PAIR_NUMBER(__default_color)].back =
    242  1.19       jdc 	    __default_pair.back;
    243  1.19       jdc 	_cursesi_screen->colour_pairs[PAIR_NUMBER(__default_color)].flags =
    244  1.19       jdc 	    __default_pair.flags;
    245  1.19       jdc 
    246  1.19       jdc 	__using_color = 1;
    247  1.19       jdc 
    248  1.19       jdc 	/* Set all positions on all windows to curses default colours. */
    249  1.19       jdc 	for (wlp = __winlistp; wlp != NULL; wlp = wlp->nextp) {
    250  1.19       jdc 		win = wlp->winp;
    251  1.19       jdc 		if (wlp->winp == curscr) {
    252  1.19       jdc 			/* Reset colour attribute on curscr */
    253  1.19       jdc 			for (y = 0; y < curscr->maxy; y++)
    254  1.19       jdc 				for (x = 0; x < curscr->maxx; x++) {
    255  1.19       jdc 					if ((curscr->lines[y]->line[x].battr & __COLOR) == __default_color)
    256  1.19       jdc 						curscr->lines[y]->line[x].battr &= ~__COLOR;
    257  1.19       jdc 				}
    258  1.19       jdc 		} else if (wlp->winp != __virtscr) {
    259  1.19       jdc 			/* Set background attribute on other windows */
    260  1.19       jdc 			if (!(win->battr & __COLOR))
    261  1.19       jdc 				win->battr |= __default_color;
    262  1.19       jdc 			for (y = 0; y < win->maxy; y++) {
    263  1.19       jdc 				for (x = 0; x < win->maxx; x++)
    264  1.19       jdc 					if (!(win->lines[y]->line[x].battr & __COLOR))
    265  1.19       jdc 						win->lines[y]->line[x].battr |= __default_color;
    266  1.19       jdc 			}
    267  1.19       jdc 			__touchwin(win);
    268  1.19       jdc 		}
    269  1.19       jdc 	}
    270  1.19       jdc 
    271   1.1       jdc 	return(OK);
    272   1.1       jdc }
    273   1.1       jdc 
    274   1.1       jdc /*
    275   1.1       jdc  * init_pair --
    276   1.1       jdc  *	Set pair foreground and background colors.
    277   1.1       jdc  */
    278   1.1       jdc int
    279   1.3     blymn init_pair(short pair, short fore, short back)
    280   1.1       jdc {
    281   1.1       jdc 	int	changed;
    282   1.1       jdc 
    283   1.1       jdc #ifdef DEBUG
    284   1.1       jdc 	__CTRACE("init_pair: %d, %d, %d\n", pair, fore, back);
    285   1.1       jdc #endif
    286  1.10   mycroft 
    287  1.19       jdc 	if (pair < 0 || pair > COLOR_PAIRS)
    288  1.10   mycroft 		return (ERR);
    289  1.19       jdc 	if (fore < -1 || fore >= COLORS)
    290  1.10   mycroft 		return (ERR);
    291  1.19       jdc 	if (back < -1 || back >= COLORS)
    292  1.10   mycroft 		return (ERR);
    293   1.1       jdc 
    294  1.15     blymn 	if ((_cursesi_screen->colour_pairs[pair].flags & __USED) &&
    295  1.15     blymn 	    (fore != _cursesi_screen->colour_pairs[pair].fore ||
    296  1.15     blymn 	     back != _cursesi_screen->colour_pairs[pair].back))
    297   1.1       jdc 		changed = 1;
    298   1.1       jdc 	else
    299   1.1       jdc 		changed = 0;
    300   1.1       jdc 
    301  1.15     blymn 	_cursesi_screen->colour_pairs[pair].flags |= __USED;
    302  1.15     blymn 	_cursesi_screen->colour_pairs[pair].fore = fore;
    303  1.15     blymn 	_cursesi_screen->colour_pairs[pair].back = back;
    304   1.1       jdc 
    305  1.13       jdc 	/* XXX: need to initialise HP style (Ip) */
    306   1.1       jdc 
    307  1.10   mycroft 	if (changed)
    308  1.10   mycroft 		__change_pair(pair);
    309  1.10   mycroft 	return (OK);
    310   1.1       jdc }
    311   1.1       jdc 
    312   1.1       jdc /*
    313   1.1       jdc  * pair_content --
    314   1.1       jdc  *	Get pair foreground and background colours.
    315   1.1       jdc  */
    316   1.1       jdc int
    317   1.3     blymn pair_content(short pair, short *forep, short *backp)
    318   1.1       jdc {
    319  1.19       jdc 	if (pair < 0 || pair > _cursesi_screen->COLOR_PAIRS)
    320   1.1       jdc 		return(ERR);
    321   1.1       jdc 
    322  1.15     blymn 	*forep = _cursesi_screen->colour_pairs[pair].fore;
    323  1.15     blymn 	*backp = _cursesi_screen->colour_pairs[pair].back;
    324   1.1       jdc 	return(OK);
    325   1.1       jdc }
    326   1.1       jdc 
    327   1.1       jdc /*
    328   1.1       jdc  * init_color --
    329   1.1       jdc  *	Set colour red, green and blue values.
    330   1.1       jdc  */
    331   1.1       jdc int
    332   1.3     blymn init_color(short color, short red, short green, short blue)
    333   1.1       jdc {
    334   1.1       jdc #ifdef DEBUG
    335   1.1       jdc 	__CTRACE("init_color: %d, %d, %d, %d\n", color, red, green, blue);
    336   1.1       jdc #endif
    337  1.19       jdc 	if (color < 0 || color >= _cursesi_screen->COLORS)
    338   1.1       jdc 		return(ERR);
    339   1.1       jdc 
    340  1.15     blymn 	_cursesi_screen->colours[color].red = red;
    341  1.15     blymn 	_cursesi_screen->colours[color].green = green;
    342  1.15     blymn 	_cursesi_screen->colours[color].blue = blue;
    343   1.1       jdc 	/* XXX Not yet implemented */
    344   1.1       jdc 	return(ERR);
    345  1.13       jdc 	/* XXX: need to initialise Tek style (Ic) and support HLS */
    346   1.1       jdc }
    347   1.1       jdc 
    348   1.1       jdc /*
    349   1.1       jdc  * color_content --
    350   1.1       jdc  *	Get colour red, green and blue values.
    351   1.1       jdc  */
    352   1.1       jdc int
    353   1.3     blymn color_content(short color, short *redp, short *greenp, short *bluep)
    354   1.1       jdc {
    355  1.15     blymn 	if (color < 0 || color >= _cursesi_screen->COLORS)
    356   1.1       jdc 		return(ERR);
    357   1.1       jdc 
    358  1.15     blymn 	*redp = _cursesi_screen->colours[color].red;
    359  1.15     blymn 	*greenp = _cursesi_screen->colours[color].green;
    360  1.15     blymn 	*bluep = _cursesi_screen->colours[color].blue;
    361   1.1       jdc 	return(OK);
    362   1.1       jdc }
    363   1.1       jdc 
    364   1.1       jdc /*
    365  1.19       jdc  * use_default_colors --
    366  1.19       jdc  *	Use terminal default colours instead of curses default colour.
    367  1.19       jdc   */
    368  1.19       jdc int
    369  1.19       jdc use_default_colors()
    370  1.19       jdc {
    371  1.19       jdc #ifdef DEBUG
    372  1.19       jdc 	__CTRACE("use_default_colors\n");
    373  1.19       jdc #endif
    374  1.19       jdc 
    375  1.19       jdc 	return(assume_default_colors(-1, -1));
    376  1.19       jdc }
    377  1.19       jdc 
    378  1.19       jdc /*
    379  1.19       jdc  * assume_default_colors --
    380  1.19       jdc  *	Set the default foreground and background colours.
    381  1.19       jdc  */
    382  1.19       jdc int
    383  1.19       jdc assume_default_colors(short fore, short back)
    384  1.19       jdc {
    385  1.19       jdc #ifdef DEBUG
    386  1.19       jdc 	__CTRACE("assume_default_colors: %d, %d, %d\n", fore, back);
    387  1.19       jdc #endif
    388  1.19       jdc 	__default_pair.fore = fore;
    389  1.19       jdc 	__default_pair.back = back;
    390  1.19       jdc 	__default_pair.flags = __USED;
    391  1.19       jdc 
    392  1.19       jdc 	if (COLOR_PAIRS) {
    393  1.19       jdc 		_cursesi_screen->colour_pairs[PAIR_NUMBER(__default_color)].fore = fore;
    394  1.19       jdc 		_cursesi_screen->colour_pairs[PAIR_NUMBER(__default_color)].back = back;
    395  1.19       jdc 		_cursesi_screen->colour_pairs[PAIR_NUMBER(__default_color)].flags = __USED;
    396  1.19       jdc 	}
    397  1.19       jdc 
    398  1.19       jdc 	/*
    399  1.19       jdc 	 * If we've already called start_color(), make sure all instances
    400  1.19       jdc 	 * of the curses default colour pair are dirty.
    401  1.19       jdc 	 */
    402  1.19       jdc 	if (__using_color)
    403  1.19       jdc 		__change_pair(PAIR_NUMBER(__default_color));
    404  1.19       jdc 
    405  1.19       jdc 	return(OK);
    406  1.19       jdc }
    407  1.19       jdc 
    408  1.19       jdc 
    409  1.19       jdc /*
    410   1.1       jdc  * __set_color --
    411   1.1       jdc  *	Set terminal foreground and background colours.
    412   1.1       jdc  */
    413   1.1       jdc void
    414  1.20     blymn __set_color( /*ARGSUSED*/ WINDOW *win, attr_t attr)
    415   1.1       jdc {
    416   1.1       jdc 	short	pair;
    417   1.1       jdc 
    418  1.19       jdc 	if ((curscr->wattr & __COLOR) == (attr & __COLOR))
    419  1.19       jdc 		return;
    420  1.19       jdc 
    421  1.14  christos 	pair = PAIR_NUMBER((u_int32_t)attr);
    422   1.1       jdc #ifdef DEBUG
    423  1.15     blymn 	__CTRACE("__set_color: %d, %d, %d\n", pair,
    424  1.18  christos 		 _cursesi_screen->colour_pairs[pair].fore,
    425  1.18  christos 		 _cursesi_screen->colour_pairs[pair].back);
    426   1.1       jdc #endif
    427  1.15     blymn 	switch (_cursesi_screen->color_type) {
    428   1.1       jdc 	/* Set ANSI forground and background colours */
    429   1.1       jdc 	case COLOR_ANSI:
    430  1.19       jdc 		if (_cursesi_screen->colour_pairs[pair].fore == -1 ||
    431  1.19       jdc 		    _cursesi_screen->colour_pairs[pair].back == -1)
    432  1.19       jdc 			__unset_color(curscr);
    433  1.19       jdc 		if (_cursesi_screen->colour_pairs[pair].fore != -1)
    434  1.19       jdc 			tputs(__parse_cap(_cursesi_screen->tc_AF,
    435  1.19       jdc 			    _cursesi_screen->colour_pairs[pair].fore),
    436  1.19       jdc 			    0, __cputchar);
    437  1.19       jdc 		if (_cursesi_screen->colour_pairs[pair].back != -1)
    438  1.19       jdc 			tputs(__parse_cap(_cursesi_screen->tc_AB,
    439  1.19       jdc 			    _cursesi_screen->colour_pairs[pair].back),
    440  1.19       jdc 			    0, __cputchar);
    441  1.19       jdc 		break;
    442  1.19       jdc 	case COLOR_HP:
    443  1.19       jdc 		/* XXX: need to support HP style */
    444  1.19       jdc 		break;
    445  1.19       jdc 	case COLOR_TEK:
    446  1.19       jdc 		/* XXX: need to support Tek style */
    447  1.19       jdc 		break;
    448  1.19       jdc 	case COLOR_OTHER:
    449  1.19       jdc 		if (_cursesi_screen->colour_pairs[pair].fore == -1 ||
    450  1.19       jdc 		    _cursesi_screen->colour_pairs[pair].back == -1)
    451  1.19       jdc 			__unset_color(curscr);
    452  1.19       jdc 		if (_cursesi_screen->colour_pairs[pair].fore != -1)
    453  1.19       jdc 			tputs(__parse_cap(_cursesi_screen->tc_Sf,
    454  1.19       jdc 			    _cursesi_screen->colour_pairs[pair].fore),
    455  1.19       jdc 			    0, __cputchar);
    456  1.19       jdc 		if (_cursesi_screen->colour_pairs[pair].back != -1)
    457  1.19       jdc 			tputs(__parse_cap(_cursesi_screen->tc_Sb,
    458  1.19       jdc 			    _cursesi_screen->colour_pairs[pair].back),
    459  1.19       jdc 			    0, __cputchar);
    460  1.19       jdc 		break;
    461  1.19       jdc 	}
    462  1.19       jdc 	curscr->wattr &= ~__COLOR;
    463  1.19       jdc 	curscr->wattr |= attr & __COLOR;
    464  1.19       jdc }
    465  1.19       jdc 
    466  1.19       jdc /*
    467  1.19       jdc  * __unset_color --
    468  1.19       jdc  *	Clear terminal foreground and background colours.
    469  1.19       jdc  */
    470  1.19       jdc void
    471  1.19       jdc __unset_color(WINDOW *win)
    472  1.19       jdc {
    473  1.19       jdc #ifdef DEBUG
    474  1.19       jdc 	__CTRACE("__unset_color\n");
    475  1.19       jdc #endif
    476  1.19       jdc 	switch (_cursesi_screen->color_type) {
    477  1.19       jdc 	/* Clear ANSI forground and background colours */
    478  1.19       jdc 	case COLOR_ANSI:
    479  1.19       jdc 		if (__tc_op != NULL) {
    480  1.19       jdc 			tputs(__tc_op, 0, __cputchar);
    481  1.19       jdc 			win->wattr &= __mask_op;
    482  1.19       jdc 		}
    483   1.1       jdc 		break;
    484   1.1       jdc 	case COLOR_HP:
    485   1.1       jdc 		/* XXX: need to support HP style */
    486   1.1       jdc 		break;
    487   1.1       jdc 	case COLOR_TEK:
    488   1.1       jdc 		/* XXX: need to support Tek style */
    489   1.6       jdc 		break;
    490   1.6       jdc 	case COLOR_OTHER:
    491  1.19       jdc 		if (__tc_op != NULL) {
    492  1.19       jdc 			tputs(__tc_op, 0, __cputchar);
    493  1.19       jdc 			win->wattr &= __mask_op;
    494  1.19       jdc 		}
    495   1.1       jdc 		break;
    496   1.1       jdc 	}
    497   1.1       jdc }
    498   1.1       jdc 
    499   1.1       jdc /*
    500   1.1       jdc  * __restore_colors --
    501   1.1       jdc  *	Redo color definitions after restarting 'curses' mode.
    502   1.1       jdc  */
    503   1.1       jdc void
    504   1.3     blymn __restore_colors(void)
    505   1.1       jdc {
    506  1.13       jdc 	if (__tc_cc != NULL)
    507  1.15     blymn 		switch (_cursesi_screen->color_type) {
    508   1.1       jdc 		case COLOR_HP:
    509  1.13       jdc 			/* XXX: need to re-initialise HP style (Ip) */
    510   1.1       jdc 			break;
    511   1.1       jdc 		case COLOR_TEK:
    512  1.13       jdc 			/* XXX: need to re-initialise Tek style (Ic) */
    513   1.1       jdc 			break;
    514   1.1       jdc 		}
    515   1.2       jdc }
    516   1.2       jdc 
    517   1.2       jdc /*
    518   1.2       jdc  * __change_pair --
    519   1.2       jdc  *	Mark dirty all positions using pair.
    520   1.2       jdc  */
    521   1.2       jdc void
    522   1.3     blymn __change_pair(short pair)
    523   1.2       jdc {
    524   1.2       jdc 	struct __winlist	*wlp;
    525   1.2       jdc 	WINDOW			*win;
    526   1.2       jdc 	int			 y, x;
    527   1.2       jdc 
    528  1.17     blymn 
    529   1.2       jdc 	for (wlp = __winlistp; wlp != NULL; wlp = wlp->nextp) {
    530   1.2       jdc #ifdef DEBUG
    531   1.2       jdc 		__CTRACE("__change_pair: win = %0.2o\n", wlp->winp);
    532   1.2       jdc #endif
    533  1.19       jdc 		win = wlp->winp;
    534  1.19       jdc 		if (win == curscr) {
    535   1.2       jdc 			/* Reset colour attribute on curscr */
    536   1.2       jdc #ifdef DEBUG
    537   1.2       jdc 			__CTRACE("__change_pair: win == curscr\n");
    538   1.2       jdc #endif
    539   1.2       jdc 			for (y = 0; y < curscr->maxy; y++)
    540  1.19       jdc 				for (x = 0; x < curscr->maxx; x++) {
    541   1.2       jdc 					if ((curscr->lines[y]->line[x].attr &
    542   1.2       jdc 					    __COLOR) == COLOR_PAIR(pair))
    543   1.2       jdc 						curscr->lines[y]->line[x].attr
    544   1.2       jdc 						    &= ~__COLOR;
    545  1.19       jdc 					if ((curscr->lines[y]->line[x].battr &
    546  1.19       jdc 					    __COLOR) == COLOR_PAIR(pair))
    547  1.19       jdc 						curscr->lines[y]->line[x].battr
    548  1.19       jdc 						    &= ~__COLOR;
    549  1.19       jdc 				}
    550  1.19       jdc 		} else if (win != __virtscr) {
    551  1.19       jdc 			/* Mark dirty those positions with colour pair "pair" */
    552   1.2       jdc 			for (y = 0; y < win->maxy; y++) {
    553   1.2       jdc 				for (x = 0; x < win->maxx; x++)
    554   1.2       jdc 					if ((win->lines[y]->line[x].attr &
    555  1.19       jdc 					    __COLOR) == COLOR_PAIR(pair) ||
    556  1.19       jdc 					    (win->lines[y]->line[x].battr &
    557   1.2       jdc 					    __COLOR) == COLOR_PAIR(pair)) {
    558   1.2       jdc 						if (!(win->lines[y]->flags &
    559   1.4       jdc 						    __ISDIRTY))
    560   1.2       jdc 							win->lines[y]->flags |=
    561   1.2       jdc 							    __ISDIRTY;
    562   1.4       jdc 						/*
    563   1.4       jdc 						 * firstchp/lastchp are shared
    564   1.4       jdc 						 * between parent window and
    565   1.4       jdc 						 * sub-window.
    566   1.4       jdc 						 */
    567   1.4       jdc 						if (*win->lines[y]->firstchp >
    568   1.4       jdc 						    x)
    569   1.2       jdc 							*win->lines[y]->firstchp
    570   1.2       jdc 							    = x;
    571   1.4       jdc 						if (*win->lines[y]->lastchp < x)
    572   1.2       jdc 							*win->lines[y]->lastchp
    573   1.2       jdc 							    = x;
    574   1.2       jdc 					}
    575   1.2       jdc #ifdef DEBUG
    576   1.4       jdc 				if ((win->lines[y]->flags & __ISDIRTY))
    577   1.2       jdc 					__CTRACE("__change_pair: first = %d, last = %d\n", *win->lines[y]->firstchp, *win->lines[y]->lastchp);
    578   1.2       jdc #endif
    579   1.2       jdc 			}
    580   1.2       jdc 		}
    581   1.2       jdc 	}
    582   1.1       jdc }
    583