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