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