color.c revision 1.8 1 1.8 blymn /* $NetBSD: color.c,v 1.8 2000/04/24 14:09:42 blymn Exp $ */
2 1.1 jdc
3 1.1 jdc /*
4 1.1 jdc * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.1 jdc * All rights reserved.
6 1.1 jdc *
7 1.1 jdc * This code is derived from software contributed to The NetBSD Foundation
8 1.1 jdc * by Julian Coleman.
9 1.1 jdc *
10 1.1 jdc * Redistribution and use in source and binary forms, with or without
11 1.1 jdc * modification, are permitted provided that the following conditions
12 1.1 jdc * are met:
13 1.1 jdc * 1. Redistributions of source code must retain the above copyright
14 1.1 jdc * notice, this list of conditions and the following disclaimer.
15 1.1 jdc * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 jdc * notice, this list of conditions and the following disclaimer in the
17 1.1 jdc * documentation and/or other materials provided with the distribution.
18 1.1 jdc * 3. All advertising materials mentioning features or use of this software
19 1.1 jdc * must display the following acknowledgement:
20 1.1 jdc * This product includes software developed by the NetBSD
21 1.1 jdc * Foundation, Inc. and its contributors.
22 1.1 jdc * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 jdc * contributors may be used to endorse or promote products derived
24 1.1 jdc * from this software without specific prior written permission.
25 1.1 jdc *
26 1.1 jdc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 jdc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 jdc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 jdc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 jdc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 jdc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 jdc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 jdc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 jdc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 jdc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 jdc * POSSIBILITY OF SUCH DAMAGE.
37 1.1 jdc */
38 1.8 blymn
39 1.8 blymn #include <sys/cdefs.h>
40 1.8 blymn #ifndef lint
41 1.8 blymn __RCSID("$NetBSD: color.c,v 1.8 2000/04/24 14:09:42 blymn Exp $");
42 1.8 blymn #endif /* not lint */
43 1.1 jdc
44 1.1 jdc #include "curses.h"
45 1.1 jdc #include "curses_private.h"
46 1.1 jdc
47 1.1 jdc /* Maximum colours */
48 1.1 jdc #define MAX_COLORS 64
49 1.1 jdc /* Maximum colour pairs - determined by number of colour bits in attr_t */
50 1.1 jdc #define MAX_PAIRS 64 /* PAIR_NUMBER(__COLOR) + 1 */
51 1.1 jdc
52 1.1 jdc /* Flags for colours and pairs */
53 1.1 jdc #define __USED 0x01
54 1.1 jdc
55 1.1 jdc /* List of colours */
56 1.1 jdc struct color {
57 1.1 jdc short num;
58 1.1 jdc short red;
59 1.1 jdc short green;
60 1.1 jdc short blue;
61 1.1 jdc int flags;
62 1.1 jdc } colors[MAX_COLORS];
63 1.1 jdc
64 1.1 jdc /* List of colour pairs */
65 1.1 jdc struct pair {
66 1.1 jdc short fore;
67 1.1 jdc short back;
68 1.1 jdc int flags;
69 1.1 jdc } pairs[MAX_PAIRS];
70 1.1 jdc
71 1.1 jdc /* Attributes that clash with colours */
72 1.1 jdc attr_t __nca;
73 1.1 jdc
74 1.1 jdc /* Style of colour manipulation */
75 1.1 jdc #define COLOR_NONE 0
76 1.1 jdc #define COLOR_ANSI 1 /* ANSI/DEC-style colour manipulation */
77 1.1 jdc #define COLOR_HP 2 /* HP-style colour manipulation */
78 1.1 jdc #define COLOR_TEK 3 /* Tektronix-style colour manipulation */
79 1.6 jdc #define COLOR_OTHER 4 /* None of the others but can set fore/back */
80 1.1 jdc int __color_type = COLOR_NONE;
81 1.1 jdc
82 1.2 jdc static void
83 1.2 jdc __change_pair __P((short));
84 1.1 jdc /*
85 1.1 jdc * has_colors --
86 1.1 jdc * Check if terminal has colours.
87 1.1 jdc */
88 1.1 jdc bool
89 1.3 blymn has_colors(void)
90 1.1 jdc {
91 1.1 jdc if (cO > 0)
92 1.1 jdc return(TRUE);
93 1.1 jdc else
94 1.1 jdc return(FALSE);
95 1.1 jdc }
96 1.1 jdc
97 1.1 jdc /*
98 1.1 jdc * can_change_colors --
99 1.1 jdc * Check if terminal can change colours.
100 1.1 jdc */
101 1.1 jdc bool
102 1.3 blymn can_change_colors(void)
103 1.1 jdc {
104 1.1 jdc if (CC)
105 1.1 jdc return(TRUE);
106 1.1 jdc else
107 1.1 jdc return(FALSE);
108 1.1 jdc }
109 1.1 jdc
110 1.1 jdc /*
111 1.1 jdc * start_color --
112 1.1 jdc * Initialise colour support.
113 1.1 jdc */
114 1.1 jdc int
115 1.5 blymn start_color(void)
116 1.1 jdc {
117 1.1 jdc int i;
118 1.1 jdc attr_t temp_nc;
119 1.1 jdc
120 1.1 jdc if (has_colors() == FALSE)
121 1.1 jdc return(ERR);
122 1.1 jdc
123 1.1 jdc /* Max colours and colour pairs */
124 1.1 jdc if (cO == -1)
125 1.1 jdc COLORS = 0;
126 1.1 jdc else {
127 1.1 jdc COLORS = cO > MAX_COLORS ? MAX_COLORS : cO;
128 1.1 jdc if (PA == -1) {
129 1.1 jdc COLOR_PAIRS = 0;
130 1.1 jdc COLORS = 0;
131 1.1 jdc } else {
132 1.1 jdc COLOR_PAIRS = PA > MAX_PAIRS ? MAX_PAIRS : PA;
133 1.1 jdc }
134 1.1 jdc }
135 1.6 jdc if (!COLORS)
136 1.6 jdc return (ERR);
137 1.1 jdc
138 1.7 jdc /* Set up initial 8 colours */
139 1.7 jdc if (COLORS >= COLOR_BLACK)
140 1.7 jdc (void) init_color(COLOR_BLACK, 0, 0, 0);
141 1.7 jdc if (COLORS >= COLOR_RED)
142 1.7 jdc (void) init_color(COLOR_RED, 1000, 0, 0);
143 1.7 jdc if (COLORS >= COLOR_GREEN)
144 1.7 jdc (void) init_color(COLOR_GREEN, 0, 1000, 0);
145 1.7 jdc if (COLORS >= COLOR_YELLOW)
146 1.7 jdc (void) init_color(COLOR_YELLOW, 1000, 1000, 0);
147 1.7 jdc if (COLORS >= COLOR_BLUE)
148 1.7 jdc (void) init_color(COLOR_BLUE, 0, 0, 1000);
149 1.7 jdc if (COLORS >= COLOR_MAGENTA)
150 1.7 jdc (void) init_color(COLOR_MAGENTA, 1000, 0, 1000);
151 1.7 jdc if (COLORS >= COLOR_CYAN)
152 1.7 jdc (void) init_color(COLOR_CYAN, 0, 1000, 1000);
153 1.7 jdc if (COLORS >= COLOR_WHITE)
154 1.7 jdc (void) init_color(COLOR_WHITE, 1000, 1000, 1000);
155 1.7 jdc
156 1.7 jdc /* Initialise other colours */
157 1.7 jdc for (i = 8; i < COLORS; i++) {
158 1.1 jdc colors[i].red = 0;
159 1.1 jdc colors[i].green = 0;
160 1.1 jdc colors[i].blue = 0;
161 1.1 jdc colors[i].flags = 0;
162 1.1 jdc }
163 1.7 jdc
164 1.7 jdc /* Initialise colour pairs to default (white on black) */
165 1.1 jdc for (i = 0; i < COLOR_PAIRS; i++) {
166 1.7 jdc pairs[i].fore = COLOR_WHITE;
167 1.7 jdc pairs[i].back = COLOR_BLACK;
168 1.1 jdc pairs[i].flags = 0;
169 1.1 jdc }
170 1.1 jdc
171 1.1 jdc /* Reset terminal colour and colour pairs. */
172 1.1 jdc if (OC != NULL)
173 1.1 jdc tputs(OC, 0, __cputchar);
174 1.1 jdc if (OP != NULL) {
175 1.1 jdc tputs(OP, 0, __cputchar);
176 1.1 jdc /* Have we also switched off attributes? */
177 1.1 jdc if (ME != NULL && !strcmp(OP, ME))
178 1.1 jdc curscr->wattr &= ~__ATTRIBUTES | __ALTCHARSET;
179 1.1 jdc }
180 1.1 jdc
181 1.7 jdc /* Type of colour manipulation - ANSI/TEK/HP/other */
182 1.1 jdc if (af != NULL && ab != NULL)
183 1.1 jdc __color_type = COLOR_ANSI;
184 1.1 jdc else if (iP != NULL)
185 1.1 jdc __color_type = COLOR_HP;
186 1.1 jdc else if (iC != NULL)
187 1.1 jdc __color_type = COLOR_TEK;
188 1.6 jdc else if (sB != NULL && sF != NULL)
189 1.6 jdc __color_type = COLOR_OTHER;
190 1.1 jdc else
191 1.1 jdc return(ERR); /* Unsupported colour method */
192 1.1 jdc
193 1.1 jdc #ifdef DEBUG
194 1.6 jdc __CTRACE("start_color: COLORS = %d, COLOR_PAIRS = %d",
195 1.6 jdc COLORS, COLOR_PAIRS);
196 1.1 jdc switch (__color_type) {
197 1.1 jdc case COLOR_ANSI:
198 1.6 jdc __CTRACE(" (ANSI style)\n");
199 1.1 jdc break;
200 1.1 jdc case COLOR_HP:
201 1.6 jdc __CTRACE(" (HP style)\n");
202 1.1 jdc break;
203 1.1 jdc case COLOR_TEK:
204 1.6 jdc __CTRACE(" (Tektronics style)\n");
205 1.6 jdc break;
206 1.6 jdc case COLOR_OTHER:
207 1.6 jdc __CTRACE(" (Other style)\n");
208 1.1 jdc break;
209 1.1 jdc }
210 1.1 jdc #endif
211 1.1 jdc
212 1.1 jdc /*
213 1.1 jdc * Attributes that cannot be used with color.
214 1.1 jdc * Store these in an attr_t for wattrset()/wattron().
215 1.1 jdc */
216 1.1 jdc __nca = __NORMAL;
217 1.1 jdc if (nc != -1) {
218 1.1 jdc temp_nc = (attr_t) tgetnum("NC");
219 1.1 jdc if (temp_nc & 0x0001)
220 1.1 jdc __nca |= __STANDOUT;
221 1.1 jdc if (temp_nc & 0x0002)
222 1.1 jdc __nca |= __UNDERSCORE;
223 1.1 jdc if (temp_nc & 0x0004)
224 1.1 jdc __nca |= __REVERSE;
225 1.1 jdc if (temp_nc & 0x0008)
226 1.1 jdc __nca |= __BLINK;
227 1.1 jdc if (temp_nc & 0x0010)
228 1.1 jdc __nca |= __DIM;
229 1.1 jdc if (temp_nc & 0x0020)
230 1.1 jdc __nca |= __BOLD;
231 1.1 jdc if (temp_nc & 0x0040)
232 1.1 jdc __nca |= __BLANK;
233 1.1 jdc if (temp_nc & 0x0080)
234 1.1 jdc __nca |= __PROTECT;
235 1.1 jdc if (temp_nc & 0x0100)
236 1.1 jdc __nca |= __ALTCHARSET;
237 1.1 jdc }
238 1.1 jdc #ifdef DEBUG
239 1.6 jdc __CTRACE ("start_color: __nca = %d\n", __nca);
240 1.1 jdc #endif
241 1.1 jdc return(OK);
242 1.1 jdc }
243 1.1 jdc
244 1.1 jdc /*
245 1.1 jdc * init_pair --
246 1.1 jdc * Set pair foreground and background colors.
247 1.1 jdc */
248 1.1 jdc int
249 1.3 blymn init_pair(short pair, short fore, short back)
250 1.1 jdc {
251 1.1 jdc int changed;
252 1.1 jdc
253 1.1 jdc #ifdef DEBUG
254 1.1 jdc __CTRACE("init_pair: %d, %d, %d\n", pair, fore, back);
255 1.1 jdc #endif
256 1.1 jdc if (pair < 0 || pair >= COLOR_PAIRS)
257 1.1 jdc return(ERR);
258 1.1 jdc
259 1.1 jdc if ((pairs[pair].flags & __USED) && (fore != pairs[pair].fore ||
260 1.1 jdc back != pairs[pair].back))
261 1.1 jdc changed = 1;
262 1.1 jdc else
263 1.1 jdc changed = 0;
264 1.1 jdc
265 1.1 jdc if (fore >= 0 && fore < COLORS)
266 1.1 jdc pairs[pair].fore = fore;
267 1.1 jdc else
268 1.1 jdc return(ERR);
269 1.1 jdc
270 1.1 jdc if (back >= 0 && back < COLOR_PAIRS)
271 1.1 jdc pairs[pair].back = back;
272 1.1 jdc else
273 1.1 jdc return(ERR);
274 1.1 jdc
275 1.1 jdc pairs[pair].flags |= __USED;
276 1.1 jdc
277 1.1 jdc /* XXX: need to initialise HP style (iP) */
278 1.1 jdc
279 1.1 jdc if (changed) {
280 1.2 jdc __change_pair (pair);
281 1.1 jdc }
282 1.1 jdc return(OK);
283 1.1 jdc }
284 1.1 jdc
285 1.1 jdc /*
286 1.1 jdc * pair_content --
287 1.1 jdc * Get pair foreground and background colours.
288 1.1 jdc */
289 1.1 jdc int
290 1.3 blymn pair_content(short pair, short *forep, short *backp)
291 1.1 jdc {
292 1.1 jdc if (pair < 0 || pair >= COLOR_PAIRS)
293 1.1 jdc return(ERR);
294 1.1 jdc
295 1.1 jdc *forep = pairs[pair].fore;
296 1.1 jdc *backp = pairs[pair].back;
297 1.1 jdc return(OK);
298 1.1 jdc }
299 1.1 jdc
300 1.1 jdc /*
301 1.1 jdc * init_color --
302 1.1 jdc * Set colour red, green and blue values.
303 1.1 jdc */
304 1.1 jdc int
305 1.3 blymn init_color(short color, short red, short green, short blue)
306 1.1 jdc {
307 1.1 jdc #ifdef DEBUG
308 1.1 jdc __CTRACE("init_color: %d, %d, %d, %d\n", color, red, green, blue);
309 1.1 jdc #endif
310 1.1 jdc if (color < 0 || color >= COLORS)
311 1.1 jdc return(ERR);
312 1.1 jdc
313 1.1 jdc colors[color].red = red;
314 1.1 jdc colors[color].green = green;
315 1.1 jdc colors[color].blue = blue;
316 1.1 jdc /* XXX Not yet implemented */
317 1.1 jdc return(ERR);
318 1.6 jdc /* XXX: need to initialise Tek style (iC) and support HLS */
319 1.1 jdc }
320 1.1 jdc
321 1.1 jdc /*
322 1.1 jdc * color_content --
323 1.1 jdc * Get colour red, green and blue values.
324 1.1 jdc */
325 1.1 jdc int
326 1.3 blymn color_content(short color, short *redp, short *greenp, short *bluep)
327 1.1 jdc {
328 1.1 jdc if (color < 0 || color >= COLORS)
329 1.1 jdc return(ERR);
330 1.1 jdc
331 1.1 jdc *redp = colors[color].red;
332 1.1 jdc *greenp = colors[color].green;
333 1.1 jdc *bluep = colors[color].blue;
334 1.1 jdc return(OK);
335 1.1 jdc }
336 1.1 jdc
337 1.1 jdc /*
338 1.1 jdc * __set_color --
339 1.1 jdc * Set terminal foreground and background colours.
340 1.1 jdc */
341 1.1 jdc void
342 1.3 blymn __set_color(attr_t attr)
343 1.1 jdc {
344 1.1 jdc short pair;
345 1.1 jdc
346 1.1 jdc pair = PAIR_NUMBER(attr);
347 1.1 jdc #ifdef DEBUG
348 1.1 jdc __CTRACE("__set_color: %d, %d, %d\n", pair, pairs[pair].fore,
349 1.1 jdc pairs[pair].back);
350 1.1 jdc #endif
351 1.1 jdc switch (__color_type) {
352 1.1 jdc /* Set ANSI forground and background colours */
353 1.1 jdc case COLOR_ANSI:
354 1.1 jdc tputs(__parse_cap(af, pairs[pair].fore), 0, __cputchar);
355 1.1 jdc tputs(__parse_cap(ab, pairs[pair].back), 0, __cputchar);
356 1.1 jdc break;
357 1.1 jdc case COLOR_HP:
358 1.1 jdc /* XXX: need to support HP style */
359 1.1 jdc break;
360 1.1 jdc case COLOR_TEK:
361 1.1 jdc /* XXX: need to support Tek style */
362 1.6 jdc break;
363 1.6 jdc case COLOR_OTHER:
364 1.6 jdc tputs(__parse_cap(sF, pairs[pair].fore), 0, __cputchar);
365 1.6 jdc tputs(__parse_cap(sB, pairs[pair].back), 0, __cputchar);
366 1.1 jdc break;
367 1.1 jdc }
368 1.1 jdc }
369 1.1 jdc
370 1.1 jdc /*
371 1.1 jdc * __restore_colors --
372 1.1 jdc * Redo color definitions after restarting 'curses' mode.
373 1.1 jdc */
374 1.1 jdc void
375 1.3 blymn __restore_colors(void)
376 1.1 jdc {
377 1.1 jdc if (CC != NULL)
378 1.1 jdc switch (__color_type) {
379 1.1 jdc case COLOR_HP:
380 1.1 jdc /* XXX: need to re-initialise HP style (iP) */
381 1.1 jdc break;
382 1.1 jdc case COLOR_TEK:
383 1.1 jdc /* XXX: need to re-initialise Tek style (iC) */
384 1.1 jdc break;
385 1.1 jdc }
386 1.2 jdc }
387 1.2 jdc
388 1.2 jdc /*
389 1.2 jdc * __change_pair --
390 1.2 jdc * Mark dirty all positions using pair.
391 1.2 jdc */
392 1.2 jdc void
393 1.3 blymn __change_pair(short pair)
394 1.2 jdc {
395 1.2 jdc struct __winlist *wlp;
396 1.2 jdc WINDOW *win;
397 1.2 jdc int y, x;
398 1.2 jdc
399 1.2 jdc
400 1.2 jdc for (wlp = __winlistp; wlp != NULL; wlp = wlp->nextp) {
401 1.2 jdc #ifdef DEBUG
402 1.2 jdc __CTRACE("__change_pair: win = %0.2o\n", wlp->winp);
403 1.2 jdc #endif
404 1.2 jdc if (wlp->winp == curscr) {
405 1.2 jdc /* Reset colour attribute on curscr */
406 1.2 jdc #ifdef DEBUG
407 1.2 jdc __CTRACE("__change_pair: win == curscr\n");
408 1.2 jdc #endif
409 1.2 jdc for (y = 0; y < curscr->maxy; y++)
410 1.2 jdc for (x = 0; x < curscr->maxx; x++)
411 1.2 jdc if ((curscr->lines[y]->line[x].attr &
412 1.2 jdc __COLOR) == COLOR_PAIR(pair))
413 1.2 jdc curscr->lines[y]->line[x].attr
414 1.2 jdc &= ~__COLOR;
415 1.2 jdc } else {
416 1.2 jdc /* Mark dirty those positions with color pair "pair" */
417 1.2 jdc win = wlp->winp;
418 1.2 jdc for (y = 0; y < win->maxy; y++) {
419 1.2 jdc for (x = 0; x < win->maxx; x++)
420 1.2 jdc if ((win->lines[y]->line[x].attr &
421 1.2 jdc __COLOR) == COLOR_PAIR(pair)) {
422 1.2 jdc if (!(win->lines[y]->flags &
423 1.4 jdc __ISDIRTY))
424 1.2 jdc win->lines[y]->flags |=
425 1.2 jdc __ISDIRTY;
426 1.4 jdc /*
427 1.4 jdc * firstchp/lastchp are shared
428 1.4 jdc * between parent window and
429 1.4 jdc * sub-window.
430 1.4 jdc */
431 1.4 jdc if (*win->lines[y]->firstchp >
432 1.4 jdc x)
433 1.2 jdc *win->lines[y]->firstchp
434 1.2 jdc = x;
435 1.4 jdc if (*win->lines[y]->lastchp < x)
436 1.2 jdc *win->lines[y]->lastchp
437 1.2 jdc = x;
438 1.2 jdc }
439 1.2 jdc #ifdef DEBUG
440 1.4 jdc if ((win->lines[y]->flags & __ISDIRTY))
441 1.2 jdc __CTRACE("__change_pair: first = %d, last = %d\n", *win->lines[y]->firstchp, *win->lines[y]->lastchp);
442 1.2 jdc #endif
443 1.2 jdc }
444 1.2 jdc }
445 1.2 jdc }
446 1.1 jdc }
447