Home | History | Annotate | Line # | Download | only in libcurses
color.c revision 1.7
      1 /*	$NetBSD: color.c,v 1.7 2000/04/21 15:54:42 jdc Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Julian Coleman.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include "curses.h"
     40 #include "curses_private.h"
     41 
     42 /* Maximum colours */
     43 #define	MAX_COLORS	64
     44 /* Maximum colour pairs - determined by number of colour bits in attr_t */
     45 #define	MAX_PAIRS	64	/* PAIR_NUMBER(__COLOR) + 1 */
     46 
     47 /* Flags for colours and pairs */
     48 #define	__USED		0x01
     49 
     50 /* List of colours */
     51 struct color {
     52 	short	num;
     53 	short	red;
     54 	short	green;
     55 	short	blue;
     56 	int	flags;
     57 } colors[MAX_COLORS];
     58 
     59 /* List of colour pairs */
     60 struct pair {
     61 	short	fore;
     62 	short	back;
     63 	int	flags;
     64 } pairs[MAX_PAIRS];
     65 
     66 /* Attributes that clash with colours */
     67 attr_t	__nca;
     68 
     69 /* Style of colour manipulation */
     70 #define COLOR_NONE	0
     71 #define COLOR_ANSI	1	/* ANSI/DEC-style colour manipulation */
     72 #define COLOR_HP	2	/* HP-style colour manipulation */
     73 #define COLOR_TEK	3	/* Tektronix-style colour manipulation */
     74 #define COLOR_OTHER	4	/* None of the others but can set fore/back */
     75 int	__color_type = COLOR_NONE;
     76 
     77 static void
     78 __change_pair __P((short));
     79 /*
     80  * has_colors --
     81  *	Check if terminal has colours.
     82  */
     83 bool
     84 has_colors(void)
     85 {
     86 	if (cO > 0)
     87 		return(TRUE);
     88 	else
     89 		return(FALSE);
     90 }
     91 
     92 /*
     93  * can_change_colors --
     94  *	Check if terminal can change colours.
     95  */
     96 bool
     97 can_change_colors(void)
     98 {
     99 	if (CC)
    100 		return(TRUE);
    101 	else
    102 		return(FALSE);
    103 }
    104 
    105 /*
    106  * start_color --
    107  *	Initialise colour support.
    108  */
    109 int
    110 start_color(void)
    111 {
    112 	int	i;
    113 	attr_t	temp_nc;
    114 
    115 	if (has_colors() == FALSE)
    116 		return(ERR);
    117 
    118 	/* Max colours and colour pairs */
    119 	if (cO == -1)
    120 		COLORS = 0;
    121 	else {
    122 		COLORS = cO > MAX_COLORS ? MAX_COLORS : cO;
    123 		if (PA == -1) {
    124 			COLOR_PAIRS = 0;
    125 			COLORS = 0;
    126 		} else {
    127 			COLOR_PAIRS = PA > MAX_PAIRS ? MAX_PAIRS : PA;
    128 		}
    129 	}
    130 	if (!COLORS)
    131 		return (ERR);
    132 
    133 	/* Set up initial 8 colours */
    134 	if (COLORS >= COLOR_BLACK)
    135 		(void) init_color(COLOR_BLACK, 0, 0, 0);
    136 	if (COLORS >= COLOR_RED)
    137 		(void) init_color(COLOR_RED, 1000, 0, 0);
    138 	if (COLORS >= COLOR_GREEN)
    139 		(void) init_color(COLOR_GREEN, 0, 1000, 0);
    140 	if (COLORS >= COLOR_YELLOW)
    141 		(void) init_color(COLOR_YELLOW, 1000, 1000, 0);
    142 	if (COLORS >= COLOR_BLUE)
    143 		(void) init_color(COLOR_BLUE, 0, 0, 1000);
    144 	if (COLORS >= COLOR_MAGENTA)
    145 		(void) init_color(COLOR_MAGENTA, 1000, 0, 1000);
    146 	if (COLORS >= COLOR_CYAN)
    147 		(void) init_color(COLOR_CYAN, 0, 1000, 1000);
    148 	if (COLORS >= COLOR_WHITE)
    149 		(void) init_color(COLOR_WHITE, 1000, 1000, 1000);
    150 
    151 	/* Initialise other colours */
    152 	for (i = 8; i < COLORS; i++) {
    153 		colors[i].red = 0;
    154 		colors[i].green = 0;
    155 		colors[i].blue = 0;
    156 		colors[i].flags = 0;
    157 	}
    158 
    159 	/* Initialise colour pairs to default (white on black) */
    160 	for (i = 0; i < COLOR_PAIRS; i++) {
    161 		pairs[i].fore = COLOR_WHITE;
    162 		pairs[i].back = COLOR_BLACK;
    163 		pairs[i].flags = 0;
    164 	}
    165 
    166 	/* Reset terminal colour and colour pairs. */
    167 	if (OC != NULL)
    168 		tputs(OC, 0, __cputchar);
    169 	if (OP != NULL) {
    170 		tputs(OP, 0, __cputchar);
    171 		/* Have we also switched off attributes? */
    172 		if (ME != NULL && !strcmp(OP, ME))
    173 			curscr->wattr &= ~__ATTRIBUTES | __ALTCHARSET;
    174 	}
    175 
    176 	/* Type of colour manipulation - ANSI/TEK/HP/other */
    177 	if (af != NULL && ab != NULL)
    178 		__color_type = COLOR_ANSI;
    179 	else if (iP != NULL)
    180 		__color_type = COLOR_HP;
    181 	else if (iC != NULL)
    182 		__color_type = COLOR_TEK;
    183 	else if (sB != NULL && sF != NULL)
    184 		__color_type = COLOR_OTHER;
    185 	else
    186 		return(ERR);		/* Unsupported colour method */
    187 
    188 #ifdef DEBUG
    189 	__CTRACE("start_color: COLORS = %d, COLOR_PAIRS = %d",
    190 	    COLORS, COLOR_PAIRS);
    191 	switch (__color_type) {
    192 	case COLOR_ANSI:
    193 		__CTRACE(" (ANSI style)\n");
    194 		break;
    195 	case COLOR_HP:
    196 		__CTRACE(" (HP style)\n");
    197 		break;
    198 	case COLOR_TEK:
    199 		__CTRACE(" (Tektronics style)\n");
    200 		break;
    201 	case COLOR_OTHER:
    202 		__CTRACE(" (Other style)\n");
    203 		break;
    204 	}
    205 #endif
    206 
    207 	/*
    208 	 * Attributes that cannot be used with color.
    209 	 * Store these in an attr_t for wattrset()/wattron().
    210 	 */
    211 	__nca = __NORMAL;
    212 	if (nc != -1) {
    213 		temp_nc = (attr_t) tgetnum("NC");
    214 		if (temp_nc & 0x0001)
    215 			__nca |= __STANDOUT;
    216 		if (temp_nc & 0x0002)
    217 			__nca |= __UNDERSCORE;
    218 		if (temp_nc & 0x0004)
    219 			__nca |= __REVERSE;
    220 		if (temp_nc & 0x0008)
    221 			__nca |= __BLINK;
    222 		if (temp_nc & 0x0010)
    223 			__nca |= __DIM;
    224 		if (temp_nc & 0x0020)
    225 			__nca |= __BOLD;
    226 		if (temp_nc & 0x0040)
    227 			__nca |= __BLANK;
    228 		if (temp_nc & 0x0080)
    229 			__nca |= __PROTECT;
    230 		if (temp_nc & 0x0100)
    231 			__nca |= __ALTCHARSET;
    232 	}
    233 #ifdef DEBUG
    234 	__CTRACE ("start_color: __nca = %d\n", __nca);
    235 #endif
    236 	return(OK);
    237 }
    238 
    239 /*
    240  * init_pair --
    241  *	Set pair foreground and background colors.
    242  */
    243 int
    244 init_pair(short pair, short fore, short back)
    245 {
    246 	int	changed;
    247 
    248 #ifdef DEBUG
    249 	__CTRACE("init_pair: %d, %d, %d\n", pair, fore, back);
    250 #endif
    251 	if (pair < 0 || pair >= COLOR_PAIRS)
    252 		return(ERR);
    253 
    254 	if ((pairs[pair].flags & __USED) && (fore != pairs[pair].fore ||
    255 	    back != pairs[pair].back))
    256 		changed = 1;
    257 	else
    258 		changed = 0;
    259 
    260 	if (fore >= 0 && fore < COLORS)
    261 		pairs[pair].fore = fore;
    262 	else
    263 		return(ERR);
    264 
    265 	if (back >= 0 && back < COLOR_PAIRS)
    266 		pairs[pair].back = back;
    267 	else
    268 		return(ERR);
    269 
    270 	pairs[pair].flags |= __USED;
    271 
    272 	/* XXX: need to initialise HP style (iP) */
    273 
    274 	if (changed) {
    275 		__change_pair (pair);
    276 	}
    277 	return(OK);
    278 }
    279 
    280 /*
    281  * pair_content --
    282  *	Get pair foreground and background colours.
    283  */
    284 int
    285 pair_content(short pair, short *forep, short *backp)
    286 {
    287 	if (pair < 0 || pair >= COLOR_PAIRS)
    288 		return(ERR);
    289 
    290 	*forep = pairs[pair].fore;
    291 	*backp = pairs[pair].back;
    292 	return(OK);
    293 }
    294 
    295 /*
    296  * init_color --
    297  *	Set colour red, green and blue values.
    298  */
    299 int
    300 init_color(short color, short red, short green, short blue)
    301 {
    302 #ifdef DEBUG
    303 	__CTRACE("init_color: %d, %d, %d, %d\n", color, red, green, blue);
    304 #endif
    305 	if (color < 0 || color >= COLORS)
    306 		return(ERR);
    307 
    308 	colors[color].red = red;
    309 	colors[color].green = green;
    310 	colors[color].blue = blue;
    311 	/* XXX Not yet implemented */
    312 	return(ERR);
    313 	/* XXX: need to initialise Tek style (iC) and support HLS */
    314 }
    315 
    316 /*
    317  * color_content --
    318  *	Get colour red, green and blue values.
    319  */
    320 int
    321 color_content(short color, short *redp, short *greenp, short *bluep)
    322 {
    323 	if (color < 0 || color >= COLORS)
    324 		return(ERR);
    325 
    326 	*redp = colors[color].red;
    327 	*greenp = colors[color].green;
    328 	*bluep = colors[color].blue;
    329 	return(OK);
    330 }
    331 
    332 /*
    333  * __set_color --
    334  *	Set terminal foreground and background colours.
    335  */
    336 void
    337 __set_color(attr_t attr)
    338 {
    339 	short	pair;
    340 
    341 	pair = PAIR_NUMBER(attr);
    342 #ifdef DEBUG
    343 	__CTRACE("__set_color: %d, %d, %d\n", pair, pairs[pair].fore,
    344 	    pairs[pair].back);
    345 #endif
    346 	switch (__color_type) {
    347 	/* Set ANSI forground and background colours */
    348 	case COLOR_ANSI:
    349 		tputs(__parse_cap(af, pairs[pair].fore), 0, __cputchar);
    350 		tputs(__parse_cap(ab, pairs[pair].back), 0, __cputchar);
    351 		break;
    352 	case COLOR_HP:
    353 		/* XXX: need to support HP style */
    354 		break;
    355 	case COLOR_TEK:
    356 		/* XXX: need to support Tek style */
    357 		break;
    358 	case COLOR_OTHER:
    359 		tputs(__parse_cap(sF, pairs[pair].fore), 0, __cputchar);
    360 		tputs(__parse_cap(sB, pairs[pair].back), 0, __cputchar);
    361 		break;
    362 	}
    363 }
    364 
    365 /*
    366  * __restore_colors --
    367  *	Redo color definitions after restarting 'curses' mode.
    368  */
    369 void
    370 __restore_colors(void)
    371 {
    372 	if (CC != NULL)
    373 		switch (__color_type) {
    374 		case COLOR_HP:
    375 			/* XXX: need to re-initialise HP style (iP) */
    376 			break;
    377 		case COLOR_TEK:
    378 			/* XXX: need to re-initialise Tek style (iC) */
    379 			break;
    380 		}
    381 }
    382 
    383 /*
    384  * __change_pair --
    385  *	Mark dirty all positions using pair.
    386  */
    387 void
    388 __change_pair(short pair)
    389 {
    390 	struct __winlist	*wlp;
    391 	WINDOW			*win;
    392 	int			 y, x;
    393 
    394 
    395 	for (wlp = __winlistp; wlp != NULL; wlp = wlp->nextp) {
    396 #ifdef DEBUG
    397 		__CTRACE("__change_pair: win = %0.2o\n", wlp->winp);
    398 #endif
    399 		if (wlp->winp == curscr) {
    400 			/* Reset colour attribute on curscr */
    401 #ifdef DEBUG
    402 			__CTRACE("__change_pair: win == curscr\n");
    403 #endif
    404 			for (y = 0; y < curscr->maxy; y++)
    405 				for (x = 0; x < curscr->maxx; x++)
    406 					if ((curscr->lines[y]->line[x].attr &
    407 					    __COLOR) == COLOR_PAIR(pair))
    408 						curscr->lines[y]->line[x].attr
    409 						    &= ~__COLOR;
    410 		} else {
    411 			/* Mark dirty those positions with color pair "pair" */
    412 			win = wlp->winp;
    413 			for (y = 0; y < win->maxy; y++) {
    414 				for (x = 0; x < win->maxx; x++)
    415 					if ((win->lines[y]->line[x].attr &
    416 					    __COLOR) == COLOR_PAIR(pair)) {
    417 						if (!(win->lines[y]->flags &
    418 						    __ISDIRTY))
    419 							win->lines[y]->flags |=
    420 							    __ISDIRTY;
    421 						/*
    422 						 * firstchp/lastchp are shared
    423 						 * between parent window and
    424 						 * sub-window.
    425 						 */
    426 						if (*win->lines[y]->firstchp >
    427 						    x)
    428 							*win->lines[y]->firstchp
    429 							    = x;
    430 						if (*win->lines[y]->lastchp < x)
    431 							*win->lines[y]->lastchp
    432 							    = x;
    433 					}
    434 #ifdef DEBUG
    435 				if ((win->lines[y]->flags & __ISDIRTY))
    436 					__CTRACE("__change_pair: first = %d, last = %d\n", *win->lines[y]->firstchp, *win->lines[y]->lastchp);
    437 #endif
    438 			}
    439 		}
    440 	}
    441 }
    442