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