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