color.c revision 1.10 1 1.10 mycroft /* $NetBSD: color.c,v 1.10 2000/04/27 05:03:22 mycroft 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.10 mycroft __RCSID("$NetBSD: color.c,v 1.10 2000/04/27 05:03:22 mycroft 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.9 jdc /* the following is defined and set up in setterm.c */
48 1.9 jdc extern struct tinfo *_cursesi_genbuf;
49 1.9 jdc
50 1.1 jdc /* Maximum colours */
51 1.1 jdc #define MAX_COLORS 64
52 1.1 jdc /* Maximum colour pairs - determined by number of colour bits in attr_t */
53 1.1 jdc #define MAX_PAIRS 64 /* PAIR_NUMBER(__COLOR) + 1 */
54 1.1 jdc
55 1.1 jdc /* Flags for colours and pairs */
56 1.1 jdc #define __USED 0x01
57 1.1 jdc
58 1.1 jdc /* List of colours */
59 1.1 jdc struct color {
60 1.1 jdc short num;
61 1.1 jdc short red;
62 1.1 jdc short green;
63 1.1 jdc short blue;
64 1.1 jdc int flags;
65 1.1 jdc } colors[MAX_COLORS];
66 1.1 jdc
67 1.1 jdc /* List of colour pairs */
68 1.1 jdc struct pair {
69 1.1 jdc short fore;
70 1.1 jdc short back;
71 1.1 jdc int flags;
72 1.1 jdc } pairs[MAX_PAIRS];
73 1.1 jdc
74 1.1 jdc /* Attributes that clash with colours */
75 1.1 jdc attr_t __nca;
76 1.1 jdc
77 1.1 jdc /* Style of colour manipulation */
78 1.1 jdc #define COLOR_NONE 0
79 1.1 jdc #define COLOR_ANSI 1 /* ANSI/DEC-style colour manipulation */
80 1.1 jdc #define COLOR_HP 2 /* HP-style colour manipulation */
81 1.1 jdc #define COLOR_TEK 3 /* Tektronix-style colour manipulation */
82 1.6 jdc #define COLOR_OTHER 4 /* None of the others but can set fore/back */
83 1.1 jdc int __color_type = COLOR_NONE;
84 1.1 jdc
85 1.2 jdc static void
86 1.2 jdc __change_pair __P((short));
87 1.1 jdc /*
88 1.1 jdc * has_colors --
89 1.1 jdc * Check if terminal has colours.
90 1.1 jdc */
91 1.1 jdc bool
92 1.3 blymn has_colors(void)
93 1.1 jdc {
94 1.1 jdc if (cO > 0)
95 1.1 jdc return(TRUE);
96 1.1 jdc else
97 1.1 jdc return(FALSE);
98 1.1 jdc }
99 1.1 jdc
100 1.1 jdc /*
101 1.1 jdc * can_change_colors --
102 1.1 jdc * Check if terminal can change colours.
103 1.1 jdc */
104 1.1 jdc bool
105 1.3 blymn can_change_colors(void)
106 1.1 jdc {
107 1.1 jdc if (CC)
108 1.1 jdc return(TRUE);
109 1.1 jdc else
110 1.1 jdc return(FALSE);
111 1.1 jdc }
112 1.1 jdc
113 1.1 jdc /*
114 1.1 jdc * start_color --
115 1.1 jdc * Initialise colour support.
116 1.1 jdc */
117 1.1 jdc int
118 1.5 blymn start_color(void)
119 1.1 jdc {
120 1.1 jdc int i;
121 1.1 jdc attr_t temp_nc;
122 1.1 jdc
123 1.1 jdc if (has_colors() == FALSE)
124 1.1 jdc return(ERR);
125 1.1 jdc
126 1.1 jdc /* Max colours and colour pairs */
127 1.1 jdc if (cO == -1)
128 1.1 jdc COLORS = 0;
129 1.1 jdc else {
130 1.1 jdc COLORS = cO > MAX_COLORS ? MAX_COLORS : cO;
131 1.1 jdc if (PA == -1) {
132 1.1 jdc COLOR_PAIRS = 0;
133 1.1 jdc COLORS = 0;
134 1.1 jdc } else {
135 1.1 jdc COLOR_PAIRS = PA > MAX_PAIRS ? MAX_PAIRS : PA;
136 1.1 jdc }
137 1.1 jdc }
138 1.6 jdc if (!COLORS)
139 1.6 jdc return (ERR);
140 1.1 jdc
141 1.1 jdc /* Reset terminal colour and colour pairs. */
142 1.1 jdc if (OC != NULL)
143 1.1 jdc tputs(OC, 0, __cputchar);
144 1.1 jdc if (OP != NULL) {
145 1.1 jdc tputs(OP, 0, __cputchar);
146 1.1 jdc /* Have we also switched off attributes? */
147 1.1 jdc if (ME != NULL && !strcmp(OP, ME))
148 1.9 jdc curscr->wattr &= ~__TERMATTR;
149 1.1 jdc }
150 1.1 jdc
151 1.7 jdc /* Type of colour manipulation - ANSI/TEK/HP/other */
152 1.1 jdc if (af != NULL && ab != NULL)
153 1.1 jdc __color_type = COLOR_ANSI;
154 1.1 jdc else if (iP != NULL)
155 1.1 jdc __color_type = COLOR_HP;
156 1.1 jdc else if (iC != NULL)
157 1.1 jdc __color_type = COLOR_TEK;
158 1.6 jdc else if (sB != NULL && sF != NULL)
159 1.6 jdc __color_type = COLOR_OTHER;
160 1.1 jdc else
161 1.1 jdc return(ERR); /* Unsupported colour method */
162 1.1 jdc
163 1.1 jdc #ifdef DEBUG
164 1.6 jdc __CTRACE("start_color: COLORS = %d, COLOR_PAIRS = %d",
165 1.6 jdc COLORS, COLOR_PAIRS);
166 1.1 jdc switch (__color_type) {
167 1.1 jdc case COLOR_ANSI:
168 1.6 jdc __CTRACE(" (ANSI style)\n");
169 1.1 jdc break;
170 1.1 jdc case COLOR_HP:
171 1.6 jdc __CTRACE(" (HP style)\n");
172 1.1 jdc break;
173 1.1 jdc case COLOR_TEK:
174 1.6 jdc __CTRACE(" (Tektronics style)\n");
175 1.6 jdc break;
176 1.6 jdc case COLOR_OTHER:
177 1.6 jdc __CTRACE(" (Other style)\n");
178 1.1 jdc break;
179 1.1 jdc }
180 1.1 jdc #endif
181 1.1 jdc
182 1.1 jdc /*
183 1.1 jdc * Attributes that cannot be used with color.
184 1.1 jdc * Store these in an attr_t for wattrset()/wattron().
185 1.1 jdc */
186 1.1 jdc __nca = __NORMAL;
187 1.1 jdc if (nc != -1) {
188 1.9 jdc temp_nc = (attr_t) t_getnum(_cursesi_genbuf, "NC");
189 1.1 jdc if (temp_nc & 0x0001)
190 1.1 jdc __nca |= __STANDOUT;
191 1.1 jdc if (temp_nc & 0x0002)
192 1.1 jdc __nca |= __UNDERSCORE;
193 1.1 jdc if (temp_nc & 0x0004)
194 1.1 jdc __nca |= __REVERSE;
195 1.1 jdc if (temp_nc & 0x0008)
196 1.1 jdc __nca |= __BLINK;
197 1.1 jdc if (temp_nc & 0x0010)
198 1.1 jdc __nca |= __DIM;
199 1.1 jdc if (temp_nc & 0x0020)
200 1.1 jdc __nca |= __BOLD;
201 1.1 jdc if (temp_nc & 0x0040)
202 1.1 jdc __nca |= __BLANK;
203 1.1 jdc if (temp_nc & 0x0080)
204 1.1 jdc __nca |= __PROTECT;
205 1.1 jdc if (temp_nc & 0x0100)
206 1.1 jdc __nca |= __ALTCHARSET;
207 1.1 jdc }
208 1.1 jdc #ifdef DEBUG
209 1.6 jdc __CTRACE ("start_color: __nca = %d\n", __nca);
210 1.1 jdc #endif
211 1.9 jdc
212 1.9 jdc /* Set up initial 8 colours */
213 1.9 jdc if (COLORS >= COLOR_BLACK)
214 1.9 jdc (void) init_color(COLOR_BLACK, 0, 0, 0);
215 1.9 jdc if (COLORS >= COLOR_RED)
216 1.9 jdc (void) init_color(COLOR_RED, 1000, 0, 0);
217 1.9 jdc if (COLORS >= COLOR_GREEN)
218 1.9 jdc (void) init_color(COLOR_GREEN, 0, 1000, 0);
219 1.9 jdc if (COLORS >= COLOR_YELLOW)
220 1.9 jdc (void) init_color(COLOR_YELLOW, 1000, 1000, 0);
221 1.9 jdc if (COLORS >= COLOR_BLUE)
222 1.9 jdc (void) init_color(COLOR_BLUE, 0, 0, 1000);
223 1.9 jdc if (COLORS >= COLOR_MAGENTA)
224 1.9 jdc (void) init_color(COLOR_MAGENTA, 1000, 0, 1000);
225 1.9 jdc if (COLORS >= COLOR_CYAN)
226 1.9 jdc (void) init_color(COLOR_CYAN, 0, 1000, 1000);
227 1.9 jdc if (COLORS >= COLOR_WHITE)
228 1.9 jdc (void) init_color(COLOR_WHITE, 1000, 1000, 1000);
229 1.9 jdc
230 1.9 jdc /* Initialise other colours */
231 1.9 jdc for (i = 8; i < COLORS; i++) {
232 1.9 jdc colors[i].red = 0;
233 1.9 jdc colors[i].green = 0;
234 1.9 jdc colors[i].blue = 0;
235 1.9 jdc colors[i].flags = 0;
236 1.9 jdc }
237 1.9 jdc
238 1.9 jdc /* Initialise colour pairs to default (white on black) */
239 1.9 jdc for (i = 0; i < COLOR_PAIRS; i++) {
240 1.9 jdc pairs[i].fore = COLOR_WHITE;
241 1.9 jdc pairs[i].back = COLOR_BLACK;
242 1.9 jdc pairs[i].flags = 0;
243 1.9 jdc }
244 1.9 jdc
245 1.1 jdc return(OK);
246 1.1 jdc }
247 1.1 jdc
248 1.1 jdc /*
249 1.1 jdc * init_pair --
250 1.1 jdc * Set pair foreground and background colors.
251 1.1 jdc */
252 1.1 jdc int
253 1.3 blymn init_pair(short pair, short fore, short back)
254 1.1 jdc {
255 1.1 jdc int changed;
256 1.1 jdc
257 1.1 jdc #ifdef DEBUG
258 1.1 jdc __CTRACE("init_pair: %d, %d, %d\n", pair, fore, back);
259 1.1 jdc #endif
260 1.10 mycroft
261 1.1 jdc if (pair < 0 || pair >= COLOR_PAIRS)
262 1.10 mycroft return (ERR);
263 1.10 mycroft if (fore < 0 || fore >= COLORS)
264 1.10 mycroft return (ERR);
265 1.10 mycroft if (back < 0 || back >= COLORS)
266 1.10 mycroft return (ERR);
267 1.1 jdc
268 1.1 jdc if ((pairs[pair].flags & __USED) && (fore != pairs[pair].fore ||
269 1.1 jdc back != pairs[pair].back))
270 1.1 jdc changed = 1;
271 1.1 jdc else
272 1.1 jdc changed = 0;
273 1.1 jdc
274 1.1 jdc pairs[pair].flags |= __USED;
275 1.10 mycroft pairs[pair].fore = fore;
276 1.10 mycroft pairs[pair].back = back;
277 1.1 jdc
278 1.1 jdc /* XXX: need to initialise HP style (iP) */
279 1.1 jdc
280 1.10 mycroft if (changed)
281 1.10 mycroft __change_pair(pair);
282 1.10 mycroft 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