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