color.c revision 1.35 1 /* $NetBSD: color.c,v 1.35 2010/02/03 15:34:40 roy 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: color.c,v 1.35 2010/02/03 15:34:40 roy Exp $");
35 #endif /* not lint */
36
37 #include "curses.h"
38 #include "curses_private.h"
39
40 /* Have we initialised colours? */
41 int __using_color = 0;
42
43 /* Default colour number */
44 attr_t __default_color = 0;
45
46 /* Default colour pair values - white on black. */
47 struct __pair __default_pair = {COLOR_WHITE, COLOR_BLACK, 0};
48
49 /* Default colour values */
50 /* Flags for colours and pairs */
51 #define __USED 0x01
52
53 static void
54 __change_pair(short);
55
56 /*
57 * has_colors --
58 * Check if terminal has colours.
59 */
60 bool
61 has_colors(void)
62 {
63 if (max_colors > 0 && max_pairs > 0 &&
64 ((set_a_foreground != NULL && set_a_background != NULL) ||
65 initialize_pair != NULL || initialize_color != NULL ||
66 (set_background != NULL && set_foreground != NULL)))
67 return(TRUE);
68 else
69 return(FALSE);
70 }
71
72 /*
73 * can_change_color --
74 * Check if terminal can change colours.
75 */
76 bool
77 can_change_color(void)
78 {
79 if (can_change)
80 return(TRUE);
81 else
82 return(FALSE);
83 }
84
85 /*
86 * start_color --
87 * Initialise colour support.
88 */
89 int
90 start_color(void)
91 {
92 int i;
93 attr_t temp_nc;
94 struct __winlist *wlp;
95 WINDOW *win;
96 int y, x;
97
98 if (has_colors() == FALSE)
99 return(ERR);
100
101 /* Max colours and colour pairs */
102 if (max_colors == -1)
103 COLORS = 0;
104 else {
105 COLORS = max_colors > MAX_COLORS ? MAX_COLORS : max_colors;
106 if (max_pairs == -1) {
107 COLOR_PAIRS = 0;
108 COLORS = 0;
109 } else {
110 COLOR_PAIRS = (max_pairs > MAX_PAIRS - 1 ?
111 MAX_PAIRS - 1 : max_pairs);
112 /* Use the last colour pair for curses default. */
113 __default_color = COLOR_PAIR(MAX_PAIRS - 1);
114 }
115 }
116 if (!COLORS)
117 return (ERR);
118
119 _cursesi_screen->COLORS = COLORS;
120 _cursesi_screen->COLOR_PAIRS = COLOR_PAIRS;
121
122 /* Reset terminal colour and colour pairs. */
123 if (orig_colors != NULL)
124 tputs(orig_colors, 0, __cputchar);
125 if (orig_pair != NULL) {
126 tputs(orig_pair, 0, __cputchar);
127 curscr->wattr &= _cursesi_screen->mask_op;
128 }
129
130 /* Type of colour manipulation - ANSI/TEK/HP/other */
131 if (set_a_foreground != NULL && set_a_background != NULL)
132 _cursesi_screen->color_type = COLOR_ANSI;
133 else if (initialize_pair != NULL)
134 _cursesi_screen->color_type = COLOR_HP;
135 else if (initialize_color != NULL)
136 _cursesi_screen->color_type = COLOR_TEK;
137 else if (set_foreground != NULL && set_background != NULL)
138 _cursesi_screen->color_type = COLOR_OTHER;
139 else
140 return(ERR); /* Unsupported colour method */
141
142 #ifdef DEBUG
143 __CTRACE(__CTRACE_COLOR, "start_color: COLORS = %d, COLOR_PAIRS = %d",
144 COLORS, COLOR_PAIRS);
145 switch (_cursesi_screen->color_type) {
146 case COLOR_ANSI:
147 __CTRACE(__CTRACE_COLOR, " (ANSI style)\n");
148 break;
149 case COLOR_HP:
150 __CTRACE(__CTRACE_COLOR, " (HP style)\n");
151 break;
152 case COLOR_TEK:
153 __CTRACE(__CTRACE_COLOR, " (Tektronics style)\n");
154 break;
155 case COLOR_OTHER:
156 __CTRACE(__CTRACE_COLOR, " (Other style)\n");
157 break;
158 }
159 #endif
160
161 /*
162 * Attributes that cannot be used with color.
163 * Store these in an attr_t for wattrset()/wattron().
164 */
165 _cursesi_screen->nca = __NORMAL;
166 if (no_color_video != -1) {
167 temp_nc = (attr_t) t_no_color_video(_cursesi_screen->term);
168 if (temp_nc & 0x0001)
169 _cursesi_screen->nca |= __STANDOUT;
170 if (temp_nc & 0x0002)
171 _cursesi_screen->nca |= __UNDERSCORE;
172 if (temp_nc & 0x0004)
173 _cursesi_screen->nca |= __REVERSE;
174 if (temp_nc & 0x0008)
175 _cursesi_screen->nca |= __BLINK;
176 if (temp_nc & 0x0010)
177 _cursesi_screen->nca |= __DIM;
178 if (temp_nc & 0x0020)
179 _cursesi_screen->nca |= __BOLD;
180 if (temp_nc & 0x0040)
181 _cursesi_screen->nca |= __BLANK;
182 if (temp_nc & 0x0080)
183 _cursesi_screen->nca |= __PROTECT;
184 if (temp_nc & 0x0100)
185 _cursesi_screen->nca |= __ALTCHARSET;
186 }
187 #ifdef DEBUG
188 __CTRACE(__CTRACE_COLOR, "start_color: _cursesi_screen->nca = %08x\n",
189 _cursesi_screen->nca);
190 #endif
191
192 /* Set up initial 8 colours */
193 if (COLORS >= COLOR_BLACK)
194 (void) init_color(COLOR_BLACK, 0, 0, 0);
195 if (COLORS >= COLOR_RED)
196 (void) init_color(COLOR_RED, 1000, 0, 0);
197 if (COLORS >= COLOR_GREEN)
198 (void) init_color(COLOR_GREEN, 0, 1000, 0);
199 if (COLORS >= COLOR_YELLOW)
200 (void) init_color(COLOR_YELLOW, 1000, 1000, 0);
201 if (COLORS >= COLOR_BLUE)
202 (void) init_color(COLOR_BLUE, 0, 0, 1000);
203 if (COLORS >= COLOR_MAGENTA)
204 (void) init_color(COLOR_MAGENTA, 1000, 0, 1000);
205 if (COLORS >= COLOR_CYAN)
206 (void) init_color(COLOR_CYAN, 0, 1000, 1000);
207 if (COLORS >= COLOR_WHITE)
208 (void) init_color(COLOR_WHITE, 1000, 1000, 1000);
209
210 /* Initialise other colours */
211 for (i = 8; i < COLORS; i++) {
212 _cursesi_screen->colours[i].red = 0;
213 _cursesi_screen->colours[i].green = 0;
214 _cursesi_screen->colours[i].blue = 0;
215 _cursesi_screen->colours[i].flags = 0;
216 }
217
218 /* Initialise pair 0 to default colours. */
219 _cursesi_screen->colour_pairs[0].fore = -1;
220 _cursesi_screen->colour_pairs[0].back = -1;
221 _cursesi_screen->colour_pairs[0].flags = 0;
222
223 /* Initialise user colour pairs to default (white on black) */
224 for (i = 0; i < COLOR_PAIRS; i++) {
225 _cursesi_screen->colour_pairs[i].fore = COLOR_WHITE;
226 _cursesi_screen->colour_pairs[i].back = COLOR_BLACK;
227 _cursesi_screen->colour_pairs[i].flags = 0;
228 }
229
230 /* Initialise default colour pair. */
231 _cursesi_screen->colour_pairs[PAIR_NUMBER(__default_color)].fore =
232 __default_pair.fore;
233 _cursesi_screen->colour_pairs[PAIR_NUMBER(__default_color)].back =
234 __default_pair.back;
235 _cursesi_screen->colour_pairs[PAIR_NUMBER(__default_color)].flags =
236 __default_pair.flags;
237
238 __using_color = 1;
239
240 /* Set all positions on all windows to curses default colours. */
241 for (wlp = _cursesi_screen->winlistp; wlp != NULL; wlp = wlp->nextp) {
242 win = wlp->winp;
243 if (wlp->winp != __virtscr && wlp->winp != curscr) {
244 /* Set color attribute on other windows */
245 win->battr |= __default_color;
246 for (y = 0; y < win->maxy; y++) {
247 for (x = 0; x < win->maxx; x++) {
248 win->alines[y]->line[x].attr &= ~__COLOR;
249 win->alines[y]->line[x].attr |= __default_color;
250 }
251 }
252 __touchwin(win);
253 }
254 }
255
256 return(OK);
257 }
258
259 /*
260 * init_pair --
261 * Set pair foreground and background colors.
262 * Our default colour ordering is ANSI - 1 = red, 4 = blue, 3 = yellow,
263 * 6 = cyan. The older style (Sb/Sf) uses 1 = blue, 4 = red, 3 = cyan,
264 * 6 = yellow, so we swap them here and in pair_content().
265 */
266 int
267 init_pair(short pair, short fore, short back)
268 {
269 int changed;
270
271 #ifdef DEBUG
272 __CTRACE(__CTRACE_COLOR, "init_pair: %d, %d, %d\n", pair, fore, back);
273 #endif
274
275 if (pair < 0 || pair >= COLOR_PAIRS)
276 return (ERR);
277 if (fore >= COLORS)
278 return (ERR);
279 if (back >= COLORS)
280 return (ERR);
281
282 /* Swap red/blue and yellow/cyan */
283 if (_cursesi_screen->color_type == COLOR_OTHER) {
284 switch (fore) {
285 case COLOR_RED:
286 fore = COLOR_BLUE;
287 break;
288 case COLOR_BLUE:
289 fore = COLOR_RED;
290 break;
291 case COLOR_YELLOW:
292 fore = COLOR_CYAN;
293 break;
294 case COLOR_CYAN:
295 fore = COLOR_YELLOW;
296 break;
297 }
298 switch (back) {
299 case COLOR_RED:
300 back = COLOR_BLUE;
301 break;
302 case COLOR_BLUE:
303 back = COLOR_RED;
304 break;
305 case COLOR_YELLOW:
306 back = COLOR_CYAN;
307 break;
308 case COLOR_CYAN:
309 back = COLOR_YELLOW;
310 break;
311 }
312 }
313
314 if ((_cursesi_screen->colour_pairs[pair].flags & __USED) &&
315 (fore != _cursesi_screen->colour_pairs[pair].fore ||
316 back != _cursesi_screen->colour_pairs[pair].back))
317 changed = 1;
318 else
319 changed = 0;
320
321 _cursesi_screen->colour_pairs[pair].flags |= __USED;
322 _cursesi_screen->colour_pairs[pair].fore = fore;
323 _cursesi_screen->colour_pairs[pair].back = back;
324
325 /* XXX: need to initialise HP style (Ip) */
326
327 if (changed)
328 __change_pair(pair);
329 return (OK);
330 }
331
332 /*
333 * pair_content --
334 * Get pair foreground and background colours.
335 */
336 int
337 pair_content(short pair, short *forep, short *backp)
338 {
339 if (pair < 0 || pair > _cursesi_screen->COLOR_PAIRS)
340 return(ERR);
341
342 *forep = _cursesi_screen->colour_pairs[pair].fore;
343 *backp = _cursesi_screen->colour_pairs[pair].back;
344
345 /* Swap red/blue and yellow/cyan */
346 if (_cursesi_screen->color_type == COLOR_OTHER) {
347 switch (*forep) {
348 case COLOR_RED:
349 *forep = COLOR_BLUE;
350 break;
351 case COLOR_BLUE:
352 *forep = COLOR_RED;
353 break;
354 case COLOR_YELLOW:
355 *forep = COLOR_CYAN;
356 break;
357 case COLOR_CYAN:
358 *forep = COLOR_YELLOW;
359 break;
360 }
361 switch (*backp) {
362 case COLOR_RED:
363 *backp = COLOR_BLUE;
364 break;
365 case COLOR_BLUE:
366 *backp = COLOR_RED;
367 break;
368 case COLOR_YELLOW:
369 *backp = COLOR_CYAN;
370 break;
371 case COLOR_CYAN:
372 *backp = COLOR_YELLOW;
373 break;
374 }
375 }
376 return(OK);
377 }
378
379 /*
380 * init_color --
381 * Set colour red, green and blue values.
382 */
383 int
384 init_color(short color, short red, short green, short blue)
385 {
386 #ifdef DEBUG
387 __CTRACE(__CTRACE_COLOR, "init_color: %d, %d, %d, %d\n",
388 color, red, green, blue);
389 #endif
390 if (color < 0 || color >= _cursesi_screen->COLORS)
391 return(ERR);
392
393 _cursesi_screen->colours[color].red = red;
394 _cursesi_screen->colours[color].green = green;
395 _cursesi_screen->colours[color].blue = blue;
396 /* XXX Not yet implemented */
397 return(ERR);
398 /* XXX: need to initialise Tek style (Ic) and support HLS */
399 }
400
401 /*
402 * color_content --
403 * Get colour red, green and blue values.
404 */
405 int
406 color_content(short color, short *redp, short *greenp, short *bluep)
407 {
408 if (color < 0 || color >= _cursesi_screen->COLORS)
409 return(ERR);
410
411 *redp = _cursesi_screen->colours[color].red;
412 *greenp = _cursesi_screen->colours[color].green;
413 *bluep = _cursesi_screen->colours[color].blue;
414 return(OK);
415 }
416
417 /*
418 * use_default_colors --
419 * Use terminal default colours instead of curses default colour.
420 */
421 int
422 use_default_colors()
423 {
424 #ifdef DEBUG
425 __CTRACE(__CTRACE_COLOR, "use_default_colors\n");
426 #endif
427
428 return(assume_default_colors(-1, -1));
429 }
430
431 /*
432 * assume_default_colors --
433 * Set the default foreground and background colours.
434 */
435 int
436 assume_default_colors(short fore, short back)
437 {
438 #ifdef DEBUG
439 __CTRACE(__CTRACE_COLOR, "assume_default_colors: %d, %d\n",
440 fore, back);
441 #endif
442 /* Swap red/blue and yellow/cyan */
443 if (_cursesi_screen->color_type == COLOR_OTHER) {
444 switch (fore) {
445 case COLOR_RED:
446 fore = COLOR_BLUE;
447 break;
448 case COLOR_BLUE:
449 fore = COLOR_RED;
450 break;
451 case COLOR_YELLOW:
452 fore = COLOR_CYAN;
453 break;
454 case COLOR_CYAN:
455 fore = COLOR_YELLOW;
456 break;
457 }
458 switch (back) {
459 case COLOR_RED:
460 back = COLOR_BLUE;
461 break;
462 case COLOR_BLUE:
463 back = COLOR_RED;
464 break;
465 case COLOR_YELLOW:
466 back = COLOR_CYAN;
467 break;
468 case COLOR_CYAN:
469 back = COLOR_YELLOW;
470 break;
471 }
472 }
473 __default_pair.fore = fore;
474 __default_pair.back = back;
475 __default_pair.flags = __USED;
476
477 if (COLOR_PAIRS) {
478 _cursesi_screen->colour_pairs[PAIR_NUMBER(__default_color)].fore = fore;
479 _cursesi_screen->colour_pairs[PAIR_NUMBER(__default_color)].back = back;
480 _cursesi_screen->colour_pairs[PAIR_NUMBER(__default_color)].flags = __USED;
481 }
482
483 /*
484 * If we've already called start_color(), make sure all instances
485 * of the curses default colour pair are dirty.
486 */
487 if (__using_color)
488 __change_pair(PAIR_NUMBER(__default_color));
489
490 return(OK);
491 }
492
493 /* no_color_video is a terminfo macro, but we need to retain binary compat */
494 #ifdef __strong_alias
495 #undef no_color_video
496 __strong_alias(no_color_video, no_color_attributes)
497 #endif
498 /*
499 * no_color_attributes --
500 * Return attributes that cannot be combined with color.
501 */
502 attr_t
503 no_color_attributes(void)
504 {
505 return(_cursesi_screen->nca);
506 }
507
508 /*
509 * __set_color --
510 * Set terminal foreground and background colours.
511 */
512 void
513 __set_color( /*ARGSUSED*/ WINDOW *win, attr_t attr)
514 {
515 short pair;
516
517 if ((curscr->wattr & __COLOR) == (attr & __COLOR))
518 return;
519
520 pair = PAIR_NUMBER((u_int32_t)attr);
521 #ifdef DEBUG
522 __CTRACE(__CTRACE_COLOR, "__set_color: %d, %d, %d\n", pair,
523 _cursesi_screen->colour_pairs[pair].fore,
524 _cursesi_screen->colour_pairs[pair].back);
525 #endif
526 switch (_cursesi_screen->color_type) {
527 /* Set ANSI forground and background colours */
528 case COLOR_ANSI:
529 if (_cursesi_screen->colour_pairs[pair].fore < 0 ||
530 _cursesi_screen->colour_pairs[pair].back < 0)
531 __unset_color(curscr);
532 if (_cursesi_screen->colour_pairs[pair].fore >= 0)
533 tputs(vtparm(t_set_a_foreground(_cursesi_screen->term),
534 _cursesi_screen->colour_pairs[pair].fore),
535 0, __cputchar);
536 if (_cursesi_screen->colour_pairs[pair].back >= 0)
537 tputs(vtparm(t_set_a_background(_cursesi_screen->term),
538 _cursesi_screen->colour_pairs[pair].back),
539 0, __cputchar);
540 break;
541 case COLOR_HP:
542 /* XXX: need to support HP style */
543 break;
544 case COLOR_TEK:
545 /* XXX: need to support Tek style */
546 break;
547 case COLOR_OTHER:
548 if (_cursesi_screen->colour_pairs[pair].fore < 0 ||
549 _cursesi_screen->colour_pairs[pair].back < 0)
550 __unset_color(curscr);
551 if (_cursesi_screen->colour_pairs[pair].fore >= 0)
552 tputs(vtparm(t_set_foreground(_cursesi_screen->term),
553 _cursesi_screen->colour_pairs[pair].fore),
554 0, __cputchar);
555 if (_cursesi_screen->colour_pairs[pair].back >= 0)
556 tputs(vtparm(t_set_background(_cursesi_screen->term),
557 _cursesi_screen->colour_pairs[pair].back),
558 0, __cputchar);
559 break;
560 }
561 curscr->wattr &= ~__COLOR;
562 curscr->wattr |= attr & __COLOR;
563 }
564
565 /*
566 * __unset_color --
567 * Clear terminal foreground and background colours.
568 */
569 void
570 __unset_color(WINDOW *win)
571 {
572 #ifdef DEBUG
573 __CTRACE(__CTRACE_COLOR, "__unset_color\n");
574 #endif
575 switch (_cursesi_screen->color_type) {
576 /* Clear ANSI forground and background colours */
577 case COLOR_ANSI:
578 if (orig_pair != NULL) {
579 tputs(orig_pair, 0, __cputchar);
580 win->wattr &= __mask_op;
581 }
582 break;
583 case COLOR_HP:
584 /* XXX: need to support HP style */
585 break;
586 case COLOR_TEK:
587 /* XXX: need to support Tek style */
588 break;
589 case COLOR_OTHER:
590 if (orig_pair != NULL) {
591 tputs(orig_pair, 0, __cputchar);
592 win->wattr &= __mask_op;
593 }
594 break;
595 }
596 }
597
598 /*
599 * __restore_colors --
600 * Redo color definitions after restarting 'curses' mode.
601 */
602 void
603 __restore_colors(void)
604 {
605 if (can_change != 0)
606 switch (_cursesi_screen->color_type) {
607 case COLOR_HP:
608 /* XXX: need to re-initialise HP style (Ip) */
609 break;
610 case COLOR_TEK:
611 /* XXX: need to re-initialise Tek style (Ic) */
612 break;
613 }
614 }
615
616 /*
617 * __change_pair --
618 * Mark dirty all positions using pair.
619 */
620 void
621 __change_pair(short pair)
622 {
623 struct __winlist *wlp;
624 WINDOW *win;
625 int y, x;
626 __LINE *lp;
627 uint32_t cl = COLOR_PAIR(pair);
628
629
630 for (wlp = _cursesi_screen->winlistp; wlp != NULL; wlp = wlp->nextp) {
631 #ifdef DEBUG
632 __CTRACE(__CTRACE_COLOR, "__change_pair: win = %p\n",
633 wlp->winp);
634 #endif
635 win = wlp->winp;
636 if (win == __virtscr)
637 continue;
638 else if (win == curscr) {
639 /* Reset colour attribute on curscr */
640 #ifdef DEBUG
641 __CTRACE(__CTRACE_COLOR,
642 "__change_pair: win == curscr\n");
643 #endif
644 for (y = 0; y < curscr->maxy; y++) {
645 lp = curscr->alines[y];
646 for (x = 0; x < curscr->maxx; x++) {
647 if ((lp->line[x].attr & __COLOR) == cl)
648 lp->line[x].attr &= ~__COLOR;
649 }
650 }
651 } else {
652 /* Mark dirty those positions with colour pair "pair" */
653 for (y = 0; y < win->maxy; y++) {
654 lp = win->alines[y];
655 for (x = 0; x < win->maxx; x++)
656 if ((lp->line[x].attr &
657 __COLOR) == cl) {
658 if (!(lp->flags & __ISDIRTY))
659 lp->flags |= __ISDIRTY;
660 /*
661 * firstchp/lastchp are shared
662 * between parent window and
663 * sub-window.
664 */
665 if (*lp->firstchp > x)
666 *lp->firstchp = x;
667 if (*lp->lastchp < x)
668 *lp->lastchp = x;
669 }
670 #ifdef DEBUG
671 if ((win->alines[y]->flags & __ISDIRTY))
672 __CTRACE(__CTRACE_COLOR,
673 "__change_pair: first = %d, "
674 "last = %d\n",
675 *win->alines[y]->firstchp,
676 *win->alines[y]->lastchp);
677 #endif
678 }
679 }
680 }
681 }
682