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