Home | History | Annotate | Line # | Download | only in libcurses
color.c revision 1.23
      1  1.23       dsl /*	$NetBSD: color.c,v 1.23 2003/02/17 11:07:19 dsl 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.23       dsl __RCSID("$NetBSD: color.c,v 1.23 2003/02/17 11:07:19 dsl 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.2       jdc static void
     61  1.19       jdc __change_pair(short);
     62  1.19       jdc 
     63   1.1       jdc /*
     64   1.1       jdc  * has_colors --
     65   1.1       jdc  *	Check if terminal has colours.
     66   1.1       jdc  */
     67   1.1       jdc bool
     68   1.3     blymn has_colors(void)
     69   1.1       jdc {
     70  1.13       jdc 	if (__tc_Co > 0 && __tc_pa > 0 && ((__tc_AF != NULL &&
     71  1.13       jdc 	    __tc_AB != NULL) || __tc_Ip != NULL || __tc_Ic != NULL ||
     72  1.13       jdc 	    (__tc_Sb != NULL && __tc_Sf != NULL)))
     73   1.1       jdc 		return(TRUE);
     74   1.1       jdc 	else
     75   1.1       jdc 		return(FALSE);
     76   1.1       jdc }
     77   1.1       jdc 
     78   1.1       jdc /*
     79  1.22       jdc  * can_change_color --
     80   1.1       jdc  *	Check if terminal can change colours.
     81   1.1       jdc  */
     82   1.1       jdc bool
     83  1.22       jdc can_change_color(void)
     84   1.1       jdc {
     85  1.13       jdc 	if (__tc_cc)
     86   1.1       jdc 		return(TRUE);
     87   1.1       jdc 	else
     88   1.1       jdc 		return(FALSE);
     89   1.1       jdc }
     90   1.1       jdc 
     91   1.1       jdc /*
     92  1.22       jdc  * can_change_colors --
     93  1.22       jdc  *	Alias for can_change_color().
     94  1.22       jdc  *	To be removed at next major number increment.
     95  1.22       jdc  */
     96  1.22       jdc bool
     97  1.22       jdc can_change_colors(void)
     98  1.22       jdc {
     99  1.22       jdc 	return can_change_color();
    100  1.22       jdc }
    101  1.22       jdc 
    102  1.22       jdc /*
    103   1.1       jdc  * start_color --
    104   1.1       jdc  *	Initialise colour support.
    105   1.1       jdc  */
    106   1.1       jdc int
    107   1.5     blymn start_color(void)
    108   1.1       jdc {
    109  1.19       jdc 	int			 i;
    110  1.19       jdc 	attr_t			 temp_nc;
    111  1.19       jdc 	struct __winlist	*wlp;
    112  1.19       jdc 	WINDOW			*win;
    113  1.19       jdc 	int			 y, x;
    114   1.1       jdc 
    115   1.1       jdc 	if (has_colors() == FALSE)
    116   1.1       jdc 		return(ERR);
    117   1.1       jdc 
    118   1.1       jdc 	/* Max colours and colour pairs */
    119  1.13       jdc 	if (__tc_Co == -1)
    120   1.1       jdc 		COLORS = 0;
    121   1.1       jdc 	else {
    122  1.13       jdc 		COLORS = __tc_Co > MAX_COLORS ? MAX_COLORS : __tc_Co;
    123  1.13       jdc 		if (__tc_pa == -1) {
    124   1.1       jdc 			COLOR_PAIRS = 0;
    125   1.1       jdc 			COLORS = 0;
    126   1.1       jdc 		} else {
    127  1.19       jdc 			COLOR_PAIRS = (__tc_pa > MAX_PAIRS ?
    128  1.19       jdc 			    MAX_PAIRS : __tc_pa) - 1;
    129  1.19       jdc 			 /* Use the last colour pair for curses default. */
    130  1.19       jdc 			__default_color = COLOR_PAIR(COLOR_PAIRS);
    131   1.1       jdc 		}
    132   1.1       jdc 	}
    133   1.6       jdc 	if (!COLORS)
    134   1.6       jdc 		return (ERR);
    135   1.1       jdc 
    136  1.15     blymn 	_cursesi_screen->COLORS = COLORS;
    137  1.15     blymn 	_cursesi_screen->COLOR_PAIRS = COLOR_PAIRS;
    138  1.17     blymn 
    139   1.1       jdc 	/* Reset terminal colour and colour pairs. */
    140  1.13       jdc 	if (__tc_oc != NULL)
    141  1.13       jdc 		tputs(__tc_oc, 0, __cputchar);
    142  1.13       jdc 	if (__tc_op != NULL) {
    143  1.13       jdc 		tputs(__tc_op, 0, __cputchar);
    144  1.15     blymn 		curscr->wattr &= _cursesi_screen->mask_op;
    145   1.1       jdc 	}
    146   1.1       jdc 
    147   1.7       jdc 	/* Type of colour manipulation - ANSI/TEK/HP/other */
    148  1.13       jdc 	if (__tc_AF != NULL && __tc_AB != NULL)
    149  1.15     blymn 		_cursesi_screen->color_type = COLOR_ANSI;
    150  1.13       jdc 	else if (__tc_Ip != NULL)
    151  1.15     blymn 		_cursesi_screen->color_type = COLOR_HP;
    152  1.13       jdc 	else if (__tc_Ic != NULL)
    153  1.15     blymn 		_cursesi_screen->color_type = COLOR_TEK;
    154  1.13       jdc 	else if (__tc_Sb != NULL && __tc_Sf != NULL)
    155  1.15     blymn 		_cursesi_screen->color_type = COLOR_OTHER;
    156   1.1       jdc 	else
    157   1.1       jdc 		return(ERR);		/* Unsupported colour method */
    158   1.1       jdc 
    159   1.1       jdc #ifdef DEBUG
    160   1.6       jdc 	__CTRACE("start_color: COLORS = %d, COLOR_PAIRS = %d",
    161   1.6       jdc 	    COLORS, COLOR_PAIRS);
    162  1.15     blymn 	switch (_cursesi_screen->color_type) {
    163   1.1       jdc 	case COLOR_ANSI:
    164   1.6       jdc 		__CTRACE(" (ANSI style)\n");
    165   1.1       jdc 		break;
    166   1.1       jdc 	case COLOR_HP:
    167   1.6       jdc 		__CTRACE(" (HP style)\n");
    168   1.1       jdc 		break;
    169   1.1       jdc 	case COLOR_TEK:
    170   1.6       jdc 		__CTRACE(" (Tektronics style)\n");
    171   1.6       jdc 		break;
    172   1.6       jdc 	case COLOR_OTHER:
    173   1.6       jdc 		__CTRACE(" (Other style)\n");
    174   1.1       jdc 		break;
    175   1.1       jdc 	}
    176   1.1       jdc #endif
    177  1.17     blymn 
    178   1.1       jdc 	/*
    179   1.1       jdc 	 * Attributes that cannot be used with color.
    180   1.1       jdc 	 * Store these in an attr_t for wattrset()/wattron().
    181   1.1       jdc 	 */
    182  1.15     blymn 	_cursesi_screen->nca = __NORMAL;
    183  1.13       jdc 	if (__tc_NC != -1) {
    184  1.15     blymn 		temp_nc = (attr_t) t_getnum(_cursesi_screen->cursesi_genbuf, "NC");
    185   1.1       jdc 		if (temp_nc & 0x0001)
    186  1.15     blymn 			_cursesi_screen->nca |= __STANDOUT;
    187   1.1       jdc 		if (temp_nc & 0x0002)
    188  1.15     blymn 			_cursesi_screen->nca |= __UNDERSCORE;
    189   1.1       jdc 		if (temp_nc & 0x0004)
    190  1.15     blymn 			_cursesi_screen->nca |= __REVERSE;
    191   1.1       jdc 		if (temp_nc & 0x0008)
    192  1.15     blymn 			_cursesi_screen->nca |= __BLINK;
    193   1.1       jdc 		if (temp_nc & 0x0010)
    194  1.15     blymn 			_cursesi_screen->nca |= __DIM;
    195   1.1       jdc 		if (temp_nc & 0x0020)
    196  1.15     blymn 			_cursesi_screen->nca |= __BOLD;
    197   1.1       jdc 		if (temp_nc & 0x0040)
    198  1.15     blymn 			_cursesi_screen->nca |= __BLANK;
    199   1.1       jdc 		if (temp_nc & 0x0080)
    200  1.15     blymn 			_cursesi_screen->nca |= __PROTECT;
    201   1.1       jdc 		if (temp_nc & 0x0100)
    202  1.15     blymn 			_cursesi_screen->nca |= __ALTCHARSET;
    203   1.1       jdc 	}
    204   1.1       jdc #ifdef DEBUG
    205  1.22       jdc 	__CTRACE ("start_color: _cursesi_screen->nca = %08x\n",
    206  1.22       jdc 	    _cursesi_screen->nca);
    207   1.1       jdc #endif
    208   1.9       jdc 
    209   1.9       jdc 	/* Set up initial 8 colours */
    210   1.9       jdc 	if (COLORS >= COLOR_BLACK)
    211   1.9       jdc 		(void) init_color(COLOR_BLACK, 0, 0, 0);
    212   1.9       jdc 	if (COLORS >= COLOR_RED)
    213   1.9       jdc 		(void) init_color(COLOR_RED, 1000, 0, 0);
    214   1.9       jdc 	if (COLORS >= COLOR_GREEN)
    215   1.9       jdc 		(void) init_color(COLOR_GREEN, 0, 1000, 0);
    216   1.9       jdc 	if (COLORS >= COLOR_YELLOW)
    217   1.9       jdc 		(void) init_color(COLOR_YELLOW, 1000, 1000, 0);
    218   1.9       jdc 	if (COLORS >= COLOR_BLUE)
    219   1.9       jdc 		(void) init_color(COLOR_BLUE, 0, 0, 1000);
    220   1.9       jdc 	if (COLORS >= COLOR_MAGENTA)
    221   1.9       jdc 		(void) init_color(COLOR_MAGENTA, 1000, 0, 1000);
    222   1.9       jdc 	if (COLORS >= COLOR_CYAN)
    223   1.9       jdc 		(void) init_color(COLOR_CYAN, 0, 1000, 1000);
    224   1.9       jdc 	if (COLORS >= COLOR_WHITE)
    225   1.9       jdc 		(void) init_color(COLOR_WHITE, 1000, 1000, 1000);
    226   1.9       jdc 
    227   1.9       jdc 	/* Initialise other colours */
    228   1.9       jdc 	for (i = 8; i < COLORS; i++) {
    229  1.15     blymn 		_cursesi_screen->colours[i].red = 0;
    230  1.15     blymn 		_cursesi_screen->colours[i].green = 0;
    231  1.15     blymn 		_cursesi_screen->colours[i].blue = 0;
    232  1.15     blymn 		_cursesi_screen->colours[i].flags = 0;
    233   1.9       jdc 	}
    234   1.9       jdc 
    235  1.19       jdc 	/* Initialise pair 0 to default colours. */
    236  1.19       jdc 	_cursesi_screen->colour_pairs[0].fore = -1;
    237  1.19       jdc 	_cursesi_screen->colour_pairs[0].back = -1;
    238  1.19       jdc 	_cursesi_screen->colour_pairs[0].flags = 0;
    239  1.19       jdc 
    240  1.19       jdc 	/* Initialise user colour pairs to default (white on black) */
    241  1.19       jdc 	for (i = 1; i < COLOR_PAIRS; i++) {
    242  1.15     blymn 		_cursesi_screen->colour_pairs[i].fore = COLOR_WHITE;
    243  1.15     blymn 		_cursesi_screen->colour_pairs[i].back = COLOR_BLACK;
    244  1.15     blymn 		_cursesi_screen->colour_pairs[i].flags = 0;
    245   1.9       jdc 	}
    246   1.9       jdc 
    247  1.19       jdc 	/* Initialise default colour pair. */
    248  1.19       jdc 	_cursesi_screen->colour_pairs[PAIR_NUMBER(__default_color)].fore =
    249  1.19       jdc 	    __default_pair.fore;
    250  1.19       jdc 	_cursesi_screen->colour_pairs[PAIR_NUMBER(__default_color)].back =
    251  1.19       jdc 	    __default_pair.back;
    252  1.19       jdc 	_cursesi_screen->colour_pairs[PAIR_NUMBER(__default_color)].flags =
    253  1.19       jdc 	    __default_pair.flags;
    254  1.19       jdc 
    255  1.19       jdc 	__using_color = 1;
    256  1.19       jdc 
    257  1.19       jdc 	/* Set all positions on all windows to curses default colours. */
    258  1.19       jdc 	for (wlp = __winlistp; wlp != NULL; wlp = wlp->nextp) {
    259  1.19       jdc 		win = wlp->winp;
    260  1.19       jdc 		if (wlp->winp == curscr) {
    261  1.19       jdc 			/* Reset colour attribute on curscr */
    262  1.19       jdc 			for (y = 0; y < curscr->maxy; y++)
    263  1.19       jdc 				for (x = 0; x < curscr->maxx; x++) {
    264  1.19       jdc 					if ((curscr->lines[y]->line[x].battr & __COLOR) == __default_color)
    265  1.19       jdc 						curscr->lines[y]->line[x].battr &= ~__COLOR;
    266  1.19       jdc 				}
    267  1.19       jdc 		} else if (wlp->winp != __virtscr) {
    268  1.19       jdc 			/* Set background attribute on other windows */
    269  1.19       jdc 			if (!(win->battr & __COLOR))
    270  1.19       jdc 				win->battr |= __default_color;
    271  1.19       jdc 			for (y = 0; y < win->maxy; y++) {
    272  1.19       jdc 				for (x = 0; x < win->maxx; x++)
    273  1.19       jdc 					if (!(win->lines[y]->line[x].battr & __COLOR))
    274  1.19       jdc 						win->lines[y]->line[x].battr |= __default_color;
    275  1.19       jdc 			}
    276  1.19       jdc 			__touchwin(win);
    277  1.19       jdc 		}
    278  1.19       jdc 	}
    279  1.19       jdc 
    280   1.1       jdc 	return(OK);
    281   1.1       jdc }
    282   1.1       jdc 
    283   1.1       jdc /*
    284   1.1       jdc  * init_pair --
    285   1.1       jdc  *	Set pair foreground and background colors.
    286   1.1       jdc  */
    287   1.1       jdc int
    288   1.3     blymn init_pair(short pair, short fore, short back)
    289   1.1       jdc {
    290   1.1       jdc 	int	changed;
    291   1.1       jdc 
    292   1.1       jdc #ifdef DEBUG
    293   1.1       jdc 	__CTRACE("init_pair: %d, %d, %d\n", pair, fore, back);
    294   1.1       jdc #endif
    295  1.10   mycroft 
    296  1.21       jdc 	if (pair < 0 || pair >= COLOR_PAIRS)
    297  1.10   mycroft 		return (ERR);
    298  1.19       jdc 	if (fore < -1 || fore >= COLORS)
    299  1.10   mycroft 		return (ERR);
    300  1.19       jdc 	if (back < -1 || back >= COLORS)
    301  1.10   mycroft 		return (ERR);
    302   1.1       jdc 
    303  1.15     blymn 	if ((_cursesi_screen->colour_pairs[pair].flags & __USED) &&
    304  1.15     blymn 	    (fore != _cursesi_screen->colour_pairs[pair].fore ||
    305  1.15     blymn 	     back != _cursesi_screen->colour_pairs[pair].back))
    306   1.1       jdc 		changed = 1;
    307   1.1       jdc 	else
    308   1.1       jdc 		changed = 0;
    309   1.1       jdc 
    310  1.15     blymn 	_cursesi_screen->colour_pairs[pair].flags |= __USED;
    311  1.15     blymn 	_cursesi_screen->colour_pairs[pair].fore = fore;
    312  1.15     blymn 	_cursesi_screen->colour_pairs[pair].back = back;
    313   1.1       jdc 
    314  1.13       jdc 	/* XXX: need to initialise HP style (Ip) */
    315   1.1       jdc 
    316  1.10   mycroft 	if (changed)
    317  1.10   mycroft 		__change_pair(pair);
    318  1.10   mycroft 	return (OK);
    319   1.1       jdc }
    320   1.1       jdc 
    321   1.1       jdc /*
    322   1.1       jdc  * pair_content --
    323   1.1       jdc  *	Get pair foreground and background colours.
    324   1.1       jdc  */
    325   1.1       jdc int
    326   1.3     blymn pair_content(short pair, short *forep, short *backp)
    327   1.1       jdc {
    328  1.19       jdc 	if (pair < 0 || pair > _cursesi_screen->COLOR_PAIRS)
    329   1.1       jdc 		return(ERR);
    330   1.1       jdc 
    331  1.15     blymn 	*forep = _cursesi_screen->colour_pairs[pair].fore;
    332  1.15     blymn 	*backp = _cursesi_screen->colour_pairs[pair].back;
    333   1.1       jdc 	return(OK);
    334   1.1       jdc }
    335   1.1       jdc 
    336   1.1       jdc /*
    337   1.1       jdc  * init_color --
    338   1.1       jdc  *	Set colour red, green and blue values.
    339   1.1       jdc  */
    340   1.1       jdc int
    341   1.3     blymn init_color(short color, short red, short green, short blue)
    342   1.1       jdc {
    343   1.1       jdc #ifdef DEBUG
    344   1.1       jdc 	__CTRACE("init_color: %d, %d, %d, %d\n", color, red, green, blue);
    345   1.1       jdc #endif
    346  1.19       jdc 	if (color < 0 || color >= _cursesi_screen->COLORS)
    347   1.1       jdc 		return(ERR);
    348   1.1       jdc 
    349  1.15     blymn 	_cursesi_screen->colours[color].red = red;
    350  1.15     blymn 	_cursesi_screen->colours[color].green = green;
    351  1.15     blymn 	_cursesi_screen->colours[color].blue = blue;
    352   1.1       jdc 	/* XXX Not yet implemented */
    353   1.1       jdc 	return(ERR);
    354  1.13       jdc 	/* XXX: need to initialise Tek style (Ic) and support HLS */
    355   1.1       jdc }
    356   1.1       jdc 
    357   1.1       jdc /*
    358   1.1       jdc  * color_content --
    359   1.1       jdc  *	Get colour red, green and blue values.
    360   1.1       jdc  */
    361   1.1       jdc int
    362   1.3     blymn color_content(short color, short *redp, short *greenp, short *bluep)
    363   1.1       jdc {
    364  1.15     blymn 	if (color < 0 || color >= _cursesi_screen->COLORS)
    365   1.1       jdc 		return(ERR);
    366   1.1       jdc 
    367  1.15     blymn 	*redp = _cursesi_screen->colours[color].red;
    368  1.15     blymn 	*greenp = _cursesi_screen->colours[color].green;
    369  1.15     blymn 	*bluep = _cursesi_screen->colours[color].blue;
    370   1.1       jdc 	return(OK);
    371   1.1       jdc }
    372   1.1       jdc 
    373   1.1       jdc /*
    374  1.19       jdc  * use_default_colors --
    375  1.19       jdc  *	Use terminal default colours instead of curses default colour.
    376  1.19       jdc   */
    377  1.19       jdc int
    378  1.19       jdc use_default_colors()
    379  1.19       jdc {
    380  1.19       jdc #ifdef DEBUG
    381  1.19       jdc 	__CTRACE("use_default_colors\n");
    382  1.19       jdc #endif
    383  1.19       jdc 
    384  1.19       jdc 	return(assume_default_colors(-1, -1));
    385  1.19       jdc }
    386  1.19       jdc 
    387  1.19       jdc /*
    388  1.19       jdc  * assume_default_colors --
    389  1.19       jdc  *	Set the default foreground and background colours.
    390  1.19       jdc  */
    391  1.19       jdc int
    392  1.19       jdc assume_default_colors(short fore, short back)
    393  1.19       jdc {
    394  1.19       jdc #ifdef DEBUG
    395  1.19       jdc 	__CTRACE("assume_default_colors: %d, %d, %d\n", fore, back);
    396  1.19       jdc #endif
    397  1.19       jdc 	__default_pair.fore = fore;
    398  1.19       jdc 	__default_pair.back = back;
    399  1.19       jdc 	__default_pair.flags = __USED;
    400  1.19       jdc 
    401  1.19       jdc 	if (COLOR_PAIRS) {
    402  1.19       jdc 		_cursesi_screen->colour_pairs[PAIR_NUMBER(__default_color)].fore = fore;
    403  1.19       jdc 		_cursesi_screen->colour_pairs[PAIR_NUMBER(__default_color)].back = back;
    404  1.19       jdc 		_cursesi_screen->colour_pairs[PAIR_NUMBER(__default_color)].flags = __USED;
    405  1.19       jdc 	}
    406  1.19       jdc 
    407  1.19       jdc 	/*
    408  1.19       jdc 	 * If we've already called start_color(), make sure all instances
    409  1.19       jdc 	 * of the curses default colour pair are dirty.
    410  1.19       jdc 	 */
    411  1.19       jdc 	if (__using_color)
    412  1.19       jdc 		__change_pair(PAIR_NUMBER(__default_color));
    413  1.19       jdc 
    414  1.19       jdc 	return(OK);
    415  1.19       jdc }
    416  1.19       jdc 
    417  1.22       jdc /*
    418  1.22       jdc  * no_color_video --
    419  1.22       jdc  *	Return attributes that cannot be combined with color.
    420  1.22       jdc  */
    421  1.22       jdc attr_t
    422  1.22       jdc no_color_video(void)
    423  1.22       jdc {
    424  1.22       jdc 	return(_cursesi_screen->nca);
    425  1.22       jdc }
    426  1.19       jdc 
    427  1.19       jdc /*
    428   1.1       jdc  * __set_color --
    429   1.1       jdc  *	Set terminal foreground and background colours.
    430   1.1       jdc  */
    431   1.1       jdc void
    432  1.20     blymn __set_color( /*ARGSUSED*/ WINDOW *win, attr_t attr)
    433   1.1       jdc {
    434   1.1       jdc 	short	pair;
    435   1.1       jdc 
    436  1.19       jdc 	if ((curscr->wattr & __COLOR) == (attr & __COLOR))
    437  1.19       jdc 		return;
    438  1.19       jdc 
    439  1.14  christos 	pair = PAIR_NUMBER((u_int32_t)attr);
    440   1.1       jdc #ifdef DEBUG
    441  1.15     blymn 	__CTRACE("__set_color: %d, %d, %d\n", pair,
    442  1.18  christos 		 _cursesi_screen->colour_pairs[pair].fore,
    443  1.18  christos 		 _cursesi_screen->colour_pairs[pair].back);
    444   1.1       jdc #endif
    445  1.15     blymn 	switch (_cursesi_screen->color_type) {
    446   1.1       jdc 	/* Set ANSI forground and background colours */
    447   1.1       jdc 	case COLOR_ANSI:
    448  1.19       jdc 		if (_cursesi_screen->colour_pairs[pair].fore == -1 ||
    449  1.19       jdc 		    _cursesi_screen->colour_pairs[pair].back == -1)
    450  1.19       jdc 			__unset_color(curscr);
    451  1.19       jdc 		if (_cursesi_screen->colour_pairs[pair].fore != -1)
    452  1.19       jdc 			tputs(__parse_cap(_cursesi_screen->tc_AF,
    453  1.19       jdc 			    _cursesi_screen->colour_pairs[pair].fore),
    454  1.19       jdc 			    0, __cputchar);
    455  1.19       jdc 		if (_cursesi_screen->colour_pairs[pair].back != -1)
    456  1.19       jdc 			tputs(__parse_cap(_cursesi_screen->tc_AB,
    457  1.19       jdc 			    _cursesi_screen->colour_pairs[pair].back),
    458  1.19       jdc 			    0, __cputchar);
    459  1.19       jdc 		break;
    460  1.19       jdc 	case COLOR_HP:
    461  1.19       jdc 		/* XXX: need to support HP style */
    462  1.19       jdc 		break;
    463  1.19       jdc 	case COLOR_TEK:
    464  1.19       jdc 		/* XXX: need to support Tek style */
    465  1.19       jdc 		break;
    466  1.19       jdc 	case COLOR_OTHER:
    467  1.19       jdc 		if (_cursesi_screen->colour_pairs[pair].fore == -1 ||
    468  1.19       jdc 		    _cursesi_screen->colour_pairs[pair].back == -1)
    469  1.19       jdc 			__unset_color(curscr);
    470  1.19       jdc 		if (_cursesi_screen->colour_pairs[pair].fore != -1)
    471  1.19       jdc 			tputs(__parse_cap(_cursesi_screen->tc_Sf,
    472  1.19       jdc 			    _cursesi_screen->colour_pairs[pair].fore),
    473  1.19       jdc 			    0, __cputchar);
    474  1.19       jdc 		if (_cursesi_screen->colour_pairs[pair].back != -1)
    475  1.19       jdc 			tputs(__parse_cap(_cursesi_screen->tc_Sb,
    476  1.19       jdc 			    _cursesi_screen->colour_pairs[pair].back),
    477  1.19       jdc 			    0, __cputchar);
    478  1.19       jdc 		break;
    479  1.19       jdc 	}
    480  1.19       jdc 	curscr->wattr &= ~__COLOR;
    481  1.19       jdc 	curscr->wattr |= attr & __COLOR;
    482  1.19       jdc }
    483  1.19       jdc 
    484  1.19       jdc /*
    485  1.19       jdc  * __unset_color --
    486  1.19       jdc  *	Clear terminal foreground and background colours.
    487  1.19       jdc  */
    488  1.19       jdc void
    489  1.19       jdc __unset_color(WINDOW *win)
    490  1.19       jdc {
    491  1.19       jdc #ifdef DEBUG
    492  1.19       jdc 	__CTRACE("__unset_color\n");
    493  1.19       jdc #endif
    494  1.19       jdc 	switch (_cursesi_screen->color_type) {
    495  1.19       jdc 	/* Clear ANSI forground and background colours */
    496  1.19       jdc 	case COLOR_ANSI:
    497  1.19       jdc 		if (__tc_op != NULL) {
    498  1.19       jdc 			tputs(__tc_op, 0, __cputchar);
    499  1.19       jdc 			win->wattr &= __mask_op;
    500  1.19       jdc 		}
    501   1.1       jdc 		break;
    502   1.1       jdc 	case COLOR_HP:
    503   1.1       jdc 		/* XXX: need to support HP style */
    504   1.1       jdc 		break;
    505   1.1       jdc 	case COLOR_TEK:
    506   1.1       jdc 		/* XXX: need to support Tek style */
    507   1.6       jdc 		break;
    508   1.6       jdc 	case COLOR_OTHER:
    509  1.19       jdc 		if (__tc_op != NULL) {
    510  1.19       jdc 			tputs(__tc_op, 0, __cputchar);
    511  1.19       jdc 			win->wattr &= __mask_op;
    512  1.19       jdc 		}
    513   1.1       jdc 		break;
    514   1.1       jdc 	}
    515   1.1       jdc }
    516   1.1       jdc 
    517   1.1       jdc /*
    518   1.1       jdc  * __restore_colors --
    519   1.1       jdc  *	Redo color definitions after restarting 'curses' mode.
    520   1.1       jdc  */
    521   1.1       jdc void
    522   1.3     blymn __restore_colors(void)
    523   1.1       jdc {
    524  1.13       jdc 	if (__tc_cc != NULL)
    525  1.15     blymn 		switch (_cursesi_screen->color_type) {
    526   1.1       jdc 		case COLOR_HP:
    527  1.13       jdc 			/* XXX: need to re-initialise HP style (Ip) */
    528   1.1       jdc 			break;
    529   1.1       jdc 		case COLOR_TEK:
    530  1.13       jdc 			/* XXX: need to re-initialise Tek style (Ic) */
    531   1.1       jdc 			break;
    532   1.1       jdc 		}
    533   1.2       jdc }
    534   1.2       jdc 
    535   1.2       jdc /*
    536   1.2       jdc  * __change_pair --
    537   1.2       jdc  *	Mark dirty all positions using pair.
    538   1.2       jdc  */
    539   1.2       jdc void
    540   1.3     blymn __change_pair(short pair)
    541   1.2       jdc {
    542   1.2       jdc 	struct __winlist	*wlp;
    543   1.2       jdc 	WINDOW			*win;
    544   1.2       jdc 	int			 y, x;
    545   1.2       jdc 
    546  1.17     blymn 
    547   1.2       jdc 	for (wlp = __winlistp; wlp != NULL; wlp = wlp->nextp) {
    548   1.2       jdc #ifdef DEBUG
    549  1.23       dsl 		__CTRACE("__change_pair: win = %p\n", wlp->winp);
    550   1.2       jdc #endif
    551  1.19       jdc 		win = wlp->winp;
    552  1.19       jdc 		if (win == curscr) {
    553   1.2       jdc 			/* Reset colour attribute on curscr */
    554   1.2       jdc #ifdef DEBUG
    555   1.2       jdc 			__CTRACE("__change_pair: win == curscr\n");
    556   1.2       jdc #endif
    557   1.2       jdc 			for (y = 0; y < curscr->maxy; y++)
    558  1.19       jdc 				for (x = 0; x < curscr->maxx; x++) {
    559   1.2       jdc 					if ((curscr->lines[y]->line[x].attr &
    560   1.2       jdc 					    __COLOR) == COLOR_PAIR(pair))
    561   1.2       jdc 						curscr->lines[y]->line[x].attr
    562   1.2       jdc 						    &= ~__COLOR;
    563  1.19       jdc 					if ((curscr->lines[y]->line[x].battr &
    564  1.19       jdc 					    __COLOR) == COLOR_PAIR(pair))
    565  1.19       jdc 						curscr->lines[y]->line[x].battr
    566  1.19       jdc 						    &= ~__COLOR;
    567  1.19       jdc 				}
    568  1.19       jdc 		} else if (win != __virtscr) {
    569  1.19       jdc 			/* Mark dirty those positions with colour pair "pair" */
    570   1.2       jdc 			for (y = 0; y < win->maxy; y++) {
    571   1.2       jdc 				for (x = 0; x < win->maxx; x++)
    572   1.2       jdc 					if ((win->lines[y]->line[x].attr &
    573  1.19       jdc 					    __COLOR) == COLOR_PAIR(pair) ||
    574  1.19       jdc 					    (win->lines[y]->line[x].battr &
    575   1.2       jdc 					    __COLOR) == COLOR_PAIR(pair)) {
    576   1.2       jdc 						if (!(win->lines[y]->flags &
    577   1.4       jdc 						    __ISDIRTY))
    578   1.2       jdc 							win->lines[y]->flags |=
    579   1.2       jdc 							    __ISDIRTY;
    580   1.4       jdc 						/*
    581   1.4       jdc 						 * firstchp/lastchp are shared
    582   1.4       jdc 						 * between parent window and
    583   1.4       jdc 						 * sub-window.
    584   1.4       jdc 						 */
    585   1.4       jdc 						if (*win->lines[y]->firstchp >
    586   1.4       jdc 						    x)
    587   1.2       jdc 							*win->lines[y]->firstchp
    588   1.2       jdc 							    = x;
    589   1.4       jdc 						if (*win->lines[y]->lastchp < x)
    590   1.2       jdc 							*win->lines[y]->lastchp
    591   1.2       jdc 							    = x;
    592   1.2       jdc 					}
    593   1.2       jdc #ifdef DEBUG
    594   1.4       jdc 				if ((win->lines[y]->flags & __ISDIRTY))
    595   1.2       jdc 					__CTRACE("__change_pair: first = %d, last = %d\n", *win->lines[y]->firstchp, *win->lines[y]->lastchp);
    596   1.2       jdc #endif
    597   1.2       jdc 			}
    598   1.2       jdc 		}
    599   1.2       jdc 	}
    600   1.1       jdc }
    601