color.c revision 1.4 1 /* $NetBSD: color.c,v 1.4 2000/04/15 22:53:05 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(void)
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(void)
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(short pair, short fore, short back)
234 {
235 int changed;
236
237 #ifdef DEBUG
238 __CTRACE("init_pair: %d, %d, %d\n", pair, fore, back);
239 #endif
240 if (pair < 0 || pair >= COLOR_PAIRS)
241 return(ERR);
242
243 if ((pairs[pair].flags & __USED) && (fore != pairs[pair].fore ||
244 back != pairs[pair].back))
245 changed = 1;
246 else
247 changed = 0;
248
249 if (fore >= 0 && fore < COLORS)
250 pairs[pair].fore = fore;
251 else
252 return(ERR);
253
254 if (back >= 0 && back < COLOR_PAIRS)
255 pairs[pair].back = back;
256 else
257 return(ERR);
258
259 pairs[pair].flags |= __USED;
260
261 /* XXX: need to initialise HP style (iP) */
262
263 if (changed) {
264 __change_pair (pair);
265 }
266 return(OK);
267 }
268
269 /*
270 * pair_content --
271 * Get pair foreground and background colours.
272 */
273 int
274 pair_content(short pair, short *forep, short *backp)
275 {
276 if (pair < 0 || pair >= COLOR_PAIRS)
277 return(ERR);
278
279 *forep = pairs[pair].fore;
280 *backp = pairs[pair].back;
281 return(OK);
282 }
283
284 /*
285 * init_color --
286 * Set colour red, green and blue values.
287 */
288 int
289 init_color(short color, short red, short green, short blue)
290 {
291 #ifdef DEBUG
292 __CTRACE("init_color: %d, %d, %d, %d\n", color, red, green, blue);
293 #endif
294 if (color < 0 || color >= COLORS)
295 return(ERR);
296
297 colors[color].red = red;
298 colors[color].green = green;
299 colors[color].blue = blue;
300 /* XXX Not yet implemented */
301 return(ERR);
302 /* XXX: need to initialise Tek style (iC) */
303 }
304
305 /*
306 * color_content --
307 * Get colour red, green and blue values.
308 */
309 int
310 color_content(short color, short *redp, short *greenp, short *bluep)
311 {
312 if (color < 0 || color >= COLORS)
313 return(ERR);
314
315 *redp = colors[color].red;
316 *greenp = colors[color].green;
317 *bluep = colors[color].blue;
318 return(OK);
319 }
320
321 /*
322 * __set_color --
323 * Set terminal foreground and background colours.
324 */
325 void
326 __set_color(attr_t attr)
327 {
328 short pair;
329
330 pair = PAIR_NUMBER(attr);
331 #ifdef DEBUG
332 __CTRACE("__set_color: %d, %d, %d\n", pair, pairs[pair].fore,
333 pairs[pair].back);
334 #endif
335 switch (__color_type) {
336 /* Set ANSI forground and background colours */
337 case COLOR_ANSI:
338 tputs(__parse_cap(af, pairs[pair].fore), 0, __cputchar);
339 tputs(__parse_cap(ab, pairs[pair].back), 0, __cputchar);
340 break;
341 case COLOR_HP:
342 /* XXX: need to support HP style */
343 break;
344 case COLOR_TEK:
345 /* XXX: need to support Tek style */
346 break;
347 }
348 }
349
350 /*
351 * __restore_colors --
352 * Redo color definitions after restarting 'curses' mode.
353 */
354 void
355 __restore_colors(void)
356 {
357 if (CC != NULL)
358 switch (__color_type) {
359 case COLOR_HP:
360 /* XXX: need to re-initialise HP style (iP) */
361 break;
362 case COLOR_TEK:
363 /* XXX: need to re-initialise Tek style (iC) */
364 break;
365 }
366 }
367
368 /*
369 * __change_pair --
370 * Mark dirty all positions using pair.
371 */
372 void
373 __change_pair(short pair)
374 {
375 struct __winlist *wlp;
376 WINDOW *win;
377 int y, x;
378
379
380 for (wlp = __winlistp; wlp != NULL; wlp = wlp->nextp) {
381 #ifdef DEBUG
382 __CTRACE("__change_pair: win = %0.2o\n", wlp->winp);
383 #endif
384 if (wlp->winp == curscr) {
385 /* Reset colour attribute on curscr */
386 #ifdef DEBUG
387 __CTRACE("__change_pair: win == curscr\n");
388 #endif
389 for (y = 0; y < curscr->maxy; y++)
390 for (x = 0; x < curscr->maxx; x++)
391 if ((curscr->lines[y]->line[x].attr &
392 __COLOR) == COLOR_PAIR(pair))
393 curscr->lines[y]->line[x].attr
394 &= ~__COLOR;
395 } else {
396 /* Mark dirty those positions with color pair "pair" */
397 win = wlp->winp;
398 for (y = 0; y < win->maxy; y++) {
399 for (x = 0; x < win->maxx; x++)
400 if ((win->lines[y]->line[x].attr &
401 __COLOR) == COLOR_PAIR(pair)) {
402 if (!(win->lines[y]->flags &
403 __ISDIRTY))
404 win->lines[y]->flags |=
405 __ISDIRTY;
406 /*
407 * firstchp/lastchp are shared
408 * between parent window and
409 * sub-window.
410 */
411 if (*win->lines[y]->firstchp >
412 x)
413 *win->lines[y]->firstchp
414 = x;
415 if (*win->lines[y]->lastchp < x)
416 *win->lines[y]->lastchp
417 = x;
418 }
419 #ifdef DEBUG
420 if ((win->lines[y]->flags & __ISDIRTY))
421 __CTRACE("__change_pair: first = %d, last = %d\n", *win->lines[y]->firstchp, *win->lines[y]->lastchp);
422 #endif
423 }
424 }
425 }
426 }
427