Home | History | Annotate | Line # | Download | only in libcurses
color.c revision 1.2
      1 /*	$NetBSD: color.c,v 1.2 2000/04/14 17:37:15 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 int	__color_type = COLOR_NONE;
     75 
     76 static void
     77 __change_pair __P((short));
     78 /*
     79  * has_colors --
     80  *	Check if terminal has colours.
     81  */
     82 bool
     83 has_colors()
     84 {
     85 	if (cO > 0)
     86 		return(TRUE);
     87 	else
     88 		return(FALSE);
     89 }
     90 
     91 /*
     92  * can_change_colors --
     93  *	Check if terminal can change colours.
     94  */
     95 bool
     96 can_change_colors()
     97 {
     98 	if (CC)
     99 		return(TRUE);
    100 	else
    101 		return(FALSE);
    102 }
    103 
    104 /*
    105  * start_color --
    106  *	Initialise colour support.
    107  */
    108 int
    109 start_color()
    110 {
    111 	int	i;
    112 	attr_t	temp_nc;
    113 
    114 	if (has_colors() == FALSE)
    115 		return(ERR);
    116 
    117 	/* Max colours and colour pairs */
    118 	if (cO == -1)
    119 		COLORS = 0;
    120 	else {
    121 		COLORS = cO > MAX_COLORS ? MAX_COLORS : cO;
    122 		if (PA == -1) {
    123 			COLOR_PAIRS = 0;
    124 			COLORS = 0;
    125 		} else {
    126 			COLOR_PAIRS = PA > MAX_PAIRS ? MAX_PAIRS : PA;
    127 		}
    128 	}
    129 
    130 	/* Initialise colours and pairs */
    131 	for (i = 0; i < COLORS; i++) {
    132 		colors[i].red = 0;
    133 		colors[i].green = 0;
    134 		colors[i].blue = 0;
    135 		colors[i].flags = 0;
    136 	}
    137 	for (i = 0; i < COLOR_PAIRS; i++) {
    138 		pairs[i].fore = 0;
    139 		pairs[i].back = 0;
    140 		pairs[i].flags = 0;
    141 	}
    142 
    143 	/* Reset terminal colour and colour pairs. */
    144 	if (OC != NULL)
    145 		tputs(OC, 0, __cputchar);
    146 	if (OP != NULL) {
    147 		tputs(OP, 0, __cputchar);
    148 		/* Have we also switched off attributes? */
    149 		if (ME != NULL && !strcmp(OP, ME))
    150 			curscr->wattr &= ~__ATTRIBUTES | __ALTCHARSET;
    151 	}
    152 
    153 	/* Type of colour manipulation - ANSI/TEK/HP */
    154 	if (af != NULL && ab != NULL)
    155 		__color_type = COLOR_ANSI;
    156 	else if (iP != NULL)
    157 		__color_type = COLOR_HP;
    158 	else if (iC != NULL)
    159 		__color_type = COLOR_TEK;
    160 	else
    161 		return(ERR);		/* Unsupported colour method */
    162 
    163 #ifdef DEBUG
    164 	__CTRACE("start_color: %d, %d", COLORS, COLOR_PAIRS);
    165 	switch (__color_type) {
    166 	case COLOR_ANSI:
    167 		__CTRACE(" (ANSI)\n");
    168 		break;
    169 	case COLOR_HP:
    170 		__CTRACE(" (HP)\n");
    171 		break;
    172 	case COLOR_TEK:
    173 		__CTRACE(" (TEK)\n");
    174 		break;
    175 	}
    176 #endif
    177 
    178 	/* Set up initial 8 colours */
    179 	if (COLORS >= COLOR_BLACK)
    180 		(void) init_color(COLOR_BLACK, 0, 0, 0);
    181 	if (COLORS >= COLOR_RED)
    182 		(void) init_color(COLOR_RED, 1000, 0, 0);
    183 	if (COLORS >= COLOR_GREEN)
    184 		(void) init_color(COLOR_GREEN, 0, 1000, 0);
    185 	if (COLORS >= COLOR_YELLOW)
    186 		(void) init_color(COLOR_YELLOW, 1000, 1000, 0);
    187 	if (COLORS >= COLOR_BLUE)
    188 		(void) init_color(COLOR_BLUE, 0, 0, 1000);
    189 	if (COLORS >= COLOR_MAGENTA)
    190 		(void) init_color(COLOR_MAGENTA, 1000, 0, 1000);
    191 	if (COLORS >= COLOR_CYAN)
    192 		(void) init_color(COLOR_CYAN, 0, 1000, 1000);
    193 	if (COLORS >= COLOR_WHITE)
    194 		(void) init_color(COLOR_WHITE, 1000, 1000, 1000);
    195 
    196 	/*
    197 	 * Attributes that cannot be used with color.
    198 	 * Store these in an attr_t for wattrset()/wattron().
    199 	 */
    200 	__nca = __NORMAL;
    201 	if (nc != -1) {
    202 		temp_nc = (attr_t) tgetnum("NC");
    203 		if (temp_nc & 0x0001)
    204 			__nca |= __STANDOUT;
    205 		if (temp_nc & 0x0002)
    206 			__nca |= __UNDERSCORE;
    207 		if (temp_nc & 0x0004)
    208 			__nca |= __REVERSE;
    209 		if (temp_nc & 0x0008)
    210 			__nca |= __BLINK;
    211 		if (temp_nc & 0x0010)
    212 			__nca |= __DIM;
    213 		if (temp_nc & 0x0020)
    214 			__nca |= __BOLD;
    215 		if (temp_nc & 0x0040)
    216 			__nca |= __BLANK;
    217 		if (temp_nc & 0x0080)
    218 			__nca |= __PROTECT;
    219 		if (temp_nc & 0x0100)
    220 			__nca |= __ALTCHARSET;
    221 	}
    222 #ifdef DEBUG
    223 	__CTRACE ("start_color: __nca = %d", __nca);
    224 #endif
    225 	return(OK);
    226 }
    227 
    228 /*
    229  * init_pair --
    230  *	Set pair foreground and background colors.
    231  */
    232 int
    233 init_pair(pair, fore, back)
    234 	short	pair, fore, back;
    235 {
    236 	int	changed;
    237 
    238 #ifdef DEBUG
    239 	__CTRACE("init_pair: %d, %d, %d\n", pair, fore, back);
    240 #endif
    241 	if (pair < 0 || pair >= COLOR_PAIRS)
    242 		return(ERR);
    243 
    244 	if ((pairs[pair].flags & __USED) && (fore != pairs[pair].fore ||
    245 	    back != pairs[pair].back))
    246 		changed = 1;
    247 	else
    248 		changed = 0;
    249 
    250 	if (fore >= 0 && fore < COLORS)
    251 		pairs[pair].fore = fore;
    252 	else
    253 		return(ERR);
    254 
    255 	if (back >= 0 && back < COLOR_PAIRS)
    256 		pairs[pair].back = back;
    257 	else
    258 		return(ERR);
    259 
    260 	pairs[pair].flags |= __USED;
    261 
    262 	/* XXX: need to initialise HP style (iP) */
    263 
    264 	if (changed) {
    265 		__change_pair (pair);
    266 	}
    267 	return(OK);
    268 }
    269 
    270 /*
    271  * pair_content --
    272  *	Get pair foreground and background colours.
    273  */
    274 int
    275 pair_content(pair, forep, backp)
    276 	short	 pair, *forep, *backp;
    277 {
    278 	if (pair < 0 || pair >= COLOR_PAIRS)
    279 		return(ERR);
    280 
    281 	*forep = pairs[pair].fore;
    282 	*backp = pairs[pair].back;
    283 	return(OK);
    284 }
    285 
    286 /*
    287  * init_color --
    288  *	Set colour red, green and blue values.
    289  */
    290 int
    291 init_color(color, red, green, blue)
    292 	short	color, red, green, blue;
    293 {
    294 #ifdef DEBUG
    295 	__CTRACE("init_color: %d, %d, %d, %d\n", color, red, green, blue);
    296 #endif
    297 	if (color < 0 || color >= COLORS)
    298 		return(ERR);
    299 
    300 	colors[color].red = red;
    301 	colors[color].green = green;
    302 	colors[color].blue = blue;
    303 	/* XXX Not yet implemented */
    304 	return(ERR);
    305 	/* XXX: need to initialise Tek style (iC) */
    306 }
    307 
    308 /*
    309  * color_content --
    310  *	Get colour red, green and blue values.
    311  */
    312 int
    313 color_content(color, redp, greenp, bluep)
    314 	short	 color, *redp, *greenp, *bluep;
    315 {
    316 	if (color < 0 || color >= COLORS)
    317 		return(ERR);
    318 
    319 	*redp = colors[color].red;
    320 	*greenp = colors[color].green;
    321 	*bluep = colors[color].blue;
    322 	return(OK);
    323 }
    324 
    325 /*
    326  * __set_color --
    327  *	Set terminal foreground and background colours.
    328  */
    329 void
    330 __set_color(attr)
    331 	attr_t	attr;
    332 {
    333 	short	pair;
    334 
    335 	pair = PAIR_NUMBER(attr);
    336 #ifdef DEBUG
    337 	__CTRACE("__set_color: %d, %d, %d\n", pair, pairs[pair].fore,
    338 	    pairs[pair].back);
    339 #endif
    340 	switch (__color_type) {
    341 	/* Set ANSI forground and background colours */
    342 	case COLOR_ANSI:
    343 		tputs(__parse_cap(af, pairs[pair].fore), 0, __cputchar);
    344 		tputs(__parse_cap(ab, pairs[pair].back), 0, __cputchar);
    345 		break;
    346 	case COLOR_HP:
    347 		/* XXX: need to support HP style */
    348 		break;
    349 	case COLOR_TEK:
    350 		/* XXX: need to support Tek style */
    351 		break;
    352 	}
    353 }
    354 
    355 /*
    356  * __restore_colors --
    357  *	Redo color definitions after restarting 'curses' mode.
    358  */
    359 void
    360 __restore_colors()
    361 {
    362 	if (CC != NULL)
    363 		switch (__color_type) {
    364 		case COLOR_HP:
    365 			/* XXX: need to re-initialise HP style (iP) */
    366 			break;
    367 		case COLOR_TEK:
    368 			/* XXX: need to re-initialise Tek style (iC) */
    369 			break;
    370 		}
    371 }
    372 
    373 /*
    374  * __change_pair --
    375  *	Mark dirty all positions using pair.
    376  */
    377 void
    378 __change_pair(pair)
    379 	short	pair;
    380 {
    381 	struct __winlist	*wlp;
    382 	WINDOW			*win;
    383 	int			 y, x;
    384 
    385 
    386 	for (wlp = __winlistp; wlp != NULL; wlp = wlp->nextp) {
    387 #ifdef DEBUG
    388 		__CTRACE("__change_pair: win = %0.2o\n", wlp->winp);
    389 #endif
    390 		if (wlp->winp == curscr) {
    391 			/* Reset colour attribute on curscr */
    392 #ifdef DEBUG
    393 			__CTRACE("__change_pair: win == curscr\n");
    394 #endif
    395 			for (y = 0; y < curscr->maxy; y++)
    396 				for (x = 0; x < curscr->maxx; x++)
    397 					if ((curscr->lines[y]->line[x].attr &
    398 					    __COLOR) == COLOR_PAIR(pair))
    399 						curscr->lines[y]->line[x].attr
    400 						    &= ~__COLOR;
    401 		} else {
    402 			/* Mark dirty those positions with color pair "pair" */
    403 			win = wlp->winp;
    404 			for (y = 0; y < win->maxy; y++) {
    405 				for (x = 0; x < win->maxx; x++)
    406 					if ((win->lines[y]->line[x].attr &
    407 					    __COLOR) == COLOR_PAIR(pair)) {
    408 						if (!(win->lines[y]->flags &
    409 						    __ISDIRTY)) {
    410 							win->lines[y]->flags |=
    411 							    __ISDIRTY;
    412 							*win->lines[y]->firstchp
    413 							    = x;
    414 							*win->lines[y]->lastchp
    415 							    = x;
    416 						} else {
    417 							if (*win->lines[y]->
    418 							    firstchp > x)
    419 								*win->lines[y]->
    420 								    firstchp =
    421 								    x;
    422 							if (*win->lines[y]->
    423 							    lastchp < x)
    424 								*win->lines[y]->
    425 								    lastchp = x;
    426 						}
    427 					}
    428 #ifdef DEBUG
    429 				if ((win->lines[y]->flags & __ISDIRTY)
    430 					__CTRACE("__change_pair: first = %d, last = %d\n", *win->lines[y]->firstchp, *win->lines[y]->lastchp);
    431 #endif
    432 			}
    433 		}
    434 	}
    435 }
    436