color.c revision 1.43 1 /* $NetBSD: color.c,v 1.43 2021/09/06 07:03:49 rin 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.43 2021/09/06 07:03:49 rin 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 static int
57 init_color_value(short, short, short, short);
58
59 /*
60 * has_colors --
61 * Check if terminal has colours.
62 */
63 bool
64 has_colors(void)
65 {
66 if (max_colors > 0 && max_pairs > 0 &&
67 ((set_a_foreground != NULL && set_a_background != NULL) ||
68 initialize_pair != NULL || initialize_color != NULL ||
69 (set_background != NULL && set_foreground != NULL)))
70 return true;
71 else
72 return false;
73 }
74
75 /*
76 * can_change_color --
77 * Check if terminal can change colours.
78 */
79 bool
80 can_change_color(void)
81 {
82 return can_change ? true : 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 __CTRACE(__CTRACE_COLOR, "start_color: _cursesi_screen->nca = %08x\n",
188 _cursesi_screen->nca);
189
190 /* Set up initial 8 colours */
191 #define RGB_ON 680 /* Allow for bright colours */
192 if (COLORS >= COLOR_BLACK)
193 (void)init_color_value(COLOR_BLACK, 0, 0, 0);
194 if (COLORS >= COLOR_RED)
195 (void)init_color_value(COLOR_RED, RGB_ON, 0, 0);
196 if (COLORS >= COLOR_GREEN)
197 (void)init_color_value(COLOR_GREEN, 0, RGB_ON, 0);
198 if (COLORS >= COLOR_YELLOW)
199 (void)init_color_value(COLOR_YELLOW, RGB_ON, RGB_ON, 0);
200 if (COLORS >= COLOR_BLUE)
201 (void)init_color_value(COLOR_BLUE, 0, 0, RGB_ON);
202 if (COLORS >= COLOR_MAGENTA)
203 (void)init_color_value(COLOR_MAGENTA, RGB_ON, 0, RGB_ON);
204 if (COLORS >= COLOR_CYAN)
205 (void)init_color_value(COLOR_CYAN, 0, RGB_ON, RGB_ON);
206 if (COLORS >= COLOR_WHITE)
207 (void)init_color_value(COLOR_WHITE, RGB_ON, RGB_ON, RGB_ON);
208
209 /* Initialise other colours */
210 for (i = 8; i < COLORS; i++) {
211 _cursesi_screen->colours[i].red = 0;
212 _cursesi_screen->colours[i].green = 0;
213 _cursesi_screen->colours[i].blue = 0;
214 _cursesi_screen->colours[i].flags = 0;
215 }
216
217 /* Initialise pair 0 to default colours. */
218 _cursesi_screen->colour_pairs[0].fore = -1;
219 _cursesi_screen->colour_pairs[0].back = -1;
220 _cursesi_screen->colour_pairs[0].flags = 0;
221
222 /* Initialise user colour pairs to default (white on black) */
223 for (i = 0; i < COLOR_PAIRS; i++) {
224 _cursesi_screen->colour_pairs[i].fore = COLOR_WHITE;
225 _cursesi_screen->colour_pairs[i].back = COLOR_BLACK;
226 _cursesi_screen->colour_pairs[i].flags = 0;
227 }
228
229 /* Initialise default colour pair. */
230 _cursesi_screen->colour_pairs[PAIR_NUMBER(__default_color)].fore =
231 __default_pair.fore;
232 _cursesi_screen->colour_pairs[PAIR_NUMBER(__default_color)].back =
233 __default_pair.back;
234 _cursesi_screen->colour_pairs[PAIR_NUMBER(__default_color)].flags =
235 __default_pair.flags;
236
237 __using_color = 1;
238
239 /* Set all positions on all windows to curses default colours. */
240 for (wlp = _cursesi_screen->winlistp; wlp != NULL; wlp = wlp->nextp) {
241 win = wlp->winp;
242 if (wlp->winp != __virtscr && wlp->winp != curscr) {
243 /* Set color attribute on other windows */
244 win->battr |= __default_color;
245 for (y = 0; y < win->maxy; y++) {
246 for (x = 0; x < win->maxx; x++) {
247 win->alines[y]->line[x].attr &= ~__COLOR;
248 win->alines[y]->line[x].attr |= __default_color;
249 }
250 }
251 __touchwin(win);
252 }
253 }
254
255 return(OK);
256 }
257
258 /*
259 * init_pair --
260 * Set pair foreground and background colors.
261 * Our default colour ordering is ANSI - 1 = red, 4 = blue, 3 = yellow,
262 * 6 = cyan. The older style (Sb/Sf) uses 1 = blue, 4 = red, 3 = cyan,
263 * 6 = yellow, so we swap them here and in pair_content().
264 */
265 int
266 init_pair(short pair, short fore, short back)
267 {
268 int changed;
269
270 __CTRACE(__CTRACE_COLOR, "init_pair: %d, %d, %d\n", pair, fore, back);
271
272 if (pair < 0 || pair >= COLOR_PAIRS)
273 return ERR;
274
275 if (pair == 0) /* Ignore request for pair 0, it is default. */
276 return OK;
277
278 if (fore >= COLORS)
279 return ERR;
280 if (back >= COLORS)
281 return ERR;
282
283 /* Swap red/blue and yellow/cyan */
284 if (_cursesi_screen->color_type == COLOR_OTHER) {
285 switch (fore) {
286 case COLOR_RED:
287 fore = COLOR_BLUE;
288 break;
289 case COLOR_BLUE:
290 fore = COLOR_RED;
291 break;
292 case COLOR_YELLOW:
293 fore = COLOR_CYAN;
294 break;
295 case COLOR_CYAN:
296 fore = COLOR_YELLOW;
297 break;
298 }
299 switch (back) {
300 case COLOR_RED:
301 back = COLOR_BLUE;
302 break;
303 case COLOR_BLUE:
304 back = COLOR_RED;
305 break;
306 case COLOR_YELLOW:
307 back = COLOR_CYAN;
308 break;
309 case COLOR_CYAN:
310 back = COLOR_YELLOW;
311 break;
312 }
313 }
314
315 if ((_cursesi_screen->colour_pairs[pair].flags & __USED) &&
316 (fore != _cursesi_screen->colour_pairs[pair].fore ||
317 back != _cursesi_screen->colour_pairs[pair].back))
318 changed = 1;
319 else
320 changed = 0;
321
322 _cursesi_screen->colour_pairs[pair].flags |= __USED;
323 _cursesi_screen->colour_pairs[pair].fore = fore;
324 _cursesi_screen->colour_pairs[pair].back = back;
325
326 /* XXX: need to initialise HP style (Ip) */
327
328 if (changed)
329 __change_pair(pair);
330 return OK;
331 }
332
333 /*
334 * pair_content --
335 * Get pair foreground and background colours.
336 */
337 int
338 pair_content(short pair, short *forep, short *backp)
339 {
340 if (pair < 0 || pair > _cursesi_screen->COLOR_PAIRS)
341 return ERR;
342
343 *forep = _cursesi_screen->colour_pairs[pair].fore;
344 *backp = _cursesi_screen->colour_pairs[pair].back;
345
346 /* Swap red/blue and yellow/cyan */
347 if (_cursesi_screen->color_type == COLOR_OTHER) {
348 switch (*forep) {
349 case COLOR_RED:
350 *forep = COLOR_BLUE;
351 break;
352 case COLOR_BLUE:
353 *forep = COLOR_RED;
354 break;
355 case COLOR_YELLOW:
356 *forep = COLOR_CYAN;
357 break;
358 case COLOR_CYAN:
359 *forep = COLOR_YELLOW;
360 break;
361 }
362 switch (*backp) {
363 case COLOR_RED:
364 *backp = COLOR_BLUE;
365 break;
366 case COLOR_BLUE:
367 *backp = COLOR_RED;
368 break;
369 case COLOR_YELLOW:
370 *backp = COLOR_CYAN;
371 break;
372 case COLOR_CYAN:
373 *backp = COLOR_YELLOW;
374 break;
375 }
376 }
377 return OK;
378 }
379
380 /*
381 * init_color_Value --
382 * Set colour red, green and blue values.
383 */
384 static int
385 init_color_value(short color, short red, short green, short blue)
386 {
387 if (color < 0 || color >= _cursesi_screen->COLORS)
388 return ERR;
389
390 _cursesi_screen->colours[color].red = red;
391 _cursesi_screen->colours[color].green = green;
392 _cursesi_screen->colours[color].blue = blue;
393 return OK;
394 }
395
396 /*
397 * init_color --
398 * Set colour red, green and blue values.
399 * Change color on screen.
400 */
401 int
402 init_color(short color, short red, short green, short blue)
403 {
404 __CTRACE(__CTRACE_COLOR, "init_color: %d, %d, %d, %d\n",
405 color, red, green, blue);
406 if (init_color_value(color, red, green, blue) == ERR)
407 return ERR;
408 if (!can_change || t_initialize_color(_cursesi_screen->term) == NULL)
409 return ERR;
410 tputs(tiparm(t_initialize_color(_cursesi_screen->term),
411 color, red, green, blue), 0, __cputchar);
412 return OK;
413 }
414
415 /*
416 * color_content --
417 * Get colour red, green and blue values.
418 */
419 int
420 color_content(short color, short *redp, short *greenp, short *bluep)
421 {
422 if (color < 0 || color >= _cursesi_screen->COLORS)
423 return ERR;
424
425 *redp = _cursesi_screen->colours[color].red;
426 *greenp = _cursesi_screen->colours[color].green;
427 *bluep = _cursesi_screen->colours[color].blue;
428 return OK;
429 }
430
431 /*
432 * use_default_colors --
433 * Use terminal default colours instead of curses default colour.
434 */
435 int
436 use_default_colors(void)
437 {
438 __CTRACE(__CTRACE_COLOR, "use_default_colors\n");
439
440 return (assume_default_colors(-1, -1));
441 }
442
443 /*
444 * assume_default_colors --
445 * Set the default foreground and background colours.
446 */
447 int
448 assume_default_colors(short fore, short back)
449 {
450 __CTRACE(__CTRACE_COLOR, "assume_default_colors: %d, %d\n",
451 fore, back);
452 __CTRACE(__CTRACE_COLOR, "assume_default_colors: default_colour = %d, pair_number = %d\n", __default_color, PAIR_NUMBER(__default_color));
453
454 /* Swap red/blue and yellow/cyan */
455 if (_cursesi_screen->color_type == COLOR_OTHER) {
456 switch (fore) {
457 case COLOR_RED:
458 fore = COLOR_BLUE;
459 break;
460 case COLOR_BLUE:
461 fore = COLOR_RED;
462 break;
463 case COLOR_YELLOW:
464 fore = COLOR_CYAN;
465 break;
466 case COLOR_CYAN:
467 fore = COLOR_YELLOW;
468 break;
469 }
470 switch (back) {
471 case COLOR_RED:
472 back = COLOR_BLUE;
473 break;
474 case COLOR_BLUE:
475 back = COLOR_RED;
476 break;
477 case COLOR_YELLOW:
478 back = COLOR_CYAN;
479 break;
480 case COLOR_CYAN:
481 back = COLOR_YELLOW;
482 break;
483 }
484 }
485 __default_pair.fore = fore;
486 __default_pair.back = back;
487 __default_pair.flags = __USED;
488
489 if (COLOR_PAIRS) {
490 _cursesi_screen->colour_pairs[PAIR_NUMBER(__default_color)].fore = fore;
491 _cursesi_screen->colour_pairs[PAIR_NUMBER(__default_color)].back = back;
492 _cursesi_screen->colour_pairs[PAIR_NUMBER(__default_color)].flags = __USED;
493 }
494
495 /*
496 * If we've already called start_color(), make sure all instances
497 * of the curses default colour pair are dirty.
498 */
499 if (__using_color)
500 __change_pair(PAIR_NUMBER(__default_color));
501
502 return(OK);
503 }
504
505 /* no_color_video is a terminfo macro, but we need to retain binary compat */
506 #ifdef __strong_alias
507 #undef no_color_video
508 __strong_alias(no_color_video, no_color_attributes)
509 #endif
510 /*
511 * no_color_attributes --
512 * Return attributes that cannot be combined with color.
513 */
514 attr_t
515 no_color_attributes(void)
516 {
517 return(_cursesi_screen->nca);
518 }
519
520 /*
521 * __set_color --
522 * Set terminal foreground and background colours.
523 */
524 void
525 __set_color( /*ARGSUSED*/ WINDOW *win, attr_t attr)
526 {
527 short pair;
528
529 if ((curscr->wattr & __COLOR) == (attr & __COLOR))
530 return;
531
532 pair = PAIR_NUMBER((uint32_t)attr);
533 __CTRACE(__CTRACE_COLOR, "__set_color: %d, %d, %d\n", pair,
534 _cursesi_screen->colour_pairs[pair].fore,
535 _cursesi_screen->colour_pairs[pair].back);
536 switch (_cursesi_screen->color_type) {
537 /* Set ANSI forground and background colours */
538 case COLOR_ANSI:
539 if (_cursesi_screen->colour_pairs[pair].fore < 0 ||
540 _cursesi_screen->colour_pairs[pair].back < 0)
541 __unset_color(curscr);
542 if (_cursesi_screen->colour_pairs[pair].fore >= 0)
543 tputs(tiparm(t_set_a_foreground(_cursesi_screen->term),
544 (int)_cursesi_screen->colour_pairs[pair].fore),
545 0, __cputchar);
546 if (_cursesi_screen->colour_pairs[pair].back >= 0)
547 tputs(tiparm(t_set_a_background(_cursesi_screen->term),
548 (int)_cursesi_screen->colour_pairs[pair].back),
549 0, __cputchar);
550 break;
551 case COLOR_HP:
552 /* XXX: need to support HP style */
553 break;
554 case COLOR_TEK:
555 /* XXX: need to support Tek style */
556 break;
557 case COLOR_OTHER:
558 if (_cursesi_screen->colour_pairs[pair].fore < 0 ||
559 _cursesi_screen->colour_pairs[pair].back < 0)
560 __unset_color(curscr);
561 if (_cursesi_screen->colour_pairs[pair].fore >= 0)
562 tputs(tiparm(t_set_foreground(_cursesi_screen->term),
563 (int)_cursesi_screen->colour_pairs[pair].fore),
564 0, __cputchar);
565 if (_cursesi_screen->colour_pairs[pair].back >= 0)
566 tputs(tiparm(t_set_background(_cursesi_screen->term),
567 (int)_cursesi_screen->colour_pairs[pair].back),
568 0, __cputchar);
569 break;
570 }
571 curscr->wattr &= ~__COLOR;
572 curscr->wattr |= attr & __COLOR;
573 }
574
575 /*
576 * __unset_color --
577 * Clear terminal foreground and background colours.
578 */
579 void
580 __unset_color(WINDOW *win)
581 {
582 __CTRACE(__CTRACE_COLOR, "__unset_color\n");
583 switch (_cursesi_screen->color_type) {
584 /* Clear ANSI forground and background colours */
585 case COLOR_ANSI:
586 if (orig_pair != NULL) {
587 tputs(orig_pair, 0, __cputchar);
588 win->wattr &= __mask_op;
589 }
590 break;
591 case COLOR_HP:
592 /* XXX: need to support HP style */
593 break;
594 case COLOR_TEK:
595 /* XXX: need to support Tek style */
596 break;
597 case COLOR_OTHER:
598 if (orig_pair != NULL) {
599 tputs(orig_pair, 0, __cputchar);
600 win->wattr &= __mask_op;
601 }
602 break;
603 }
604 }
605
606 /*
607 * __restore_colors --
608 * Redo color definitions after restarting 'curses' mode.
609 */
610 void
611 __restore_colors(void)
612 {
613 if (can_change != 0)
614 switch (_cursesi_screen->color_type) {
615 case COLOR_HP:
616 /* XXX: need to re-initialise HP style (Ip) */
617 break;
618 case COLOR_TEK:
619 /* XXX: need to re-initialise Tek style (Ic) */
620 break;
621 }
622 }
623
624 /*
625 * __change_pair --
626 * Mark dirty all positions using pair.
627 */
628 void
629 __change_pair(short pair)
630 {
631 struct __winlist *wlp;
632 WINDOW *win;
633 int y, x;
634 __LINE *lp;
635 uint32_t cl = COLOR_PAIR(pair);
636
637
638 for (wlp = _cursesi_screen->winlistp; wlp != NULL; wlp = wlp->nextp) {
639 __CTRACE(__CTRACE_COLOR, "__change_pair: win = %p\n",
640 wlp->winp);
641 win = wlp->winp;
642 if (win == __virtscr)
643 continue;
644 else if (win == curscr) {
645 /* Reset colour attribute on curscr */
646 __CTRACE(__CTRACE_COLOR,
647 "__change_pair: win == curscr\n");
648 for (y = 0; y < curscr->maxy; y++) {
649 lp = curscr->alines[y];
650 for (x = 0; x < curscr->maxx; x++) {
651 if ((lp->line[x].attr & __COLOR) == cl)
652 lp->line[x].attr &= ~__COLOR;
653 }
654 }
655 } else {
656 /* Mark dirty those positions with colour pair "pair" */
657 for (y = 0; y < win->maxy; y++) {
658 lp = win->alines[y];
659 for (x = 0; x < win->maxx; x++)
660 if ((lp->line[x].attr &
661 __COLOR) == cl) {
662 if (!(lp->flags & __ISDIRTY))
663 lp->flags |= __ISDIRTY;
664 /*
665 * firstchp/lastchp are shared
666 * between parent window and
667 * sub-window.
668 */
669 if (*lp->firstchp > x)
670 *lp->firstchp = x;
671 if (*lp->lastchp < x)
672 *lp->lastchp = x;
673 }
674 #ifdef DEBUG
675 if ((win->alines[y]->flags & __ISDIRTY))
676 __CTRACE(__CTRACE_COLOR,
677 "__change_pair: first = %d, "
678 "last = %d\n",
679 *win->alines[y]->firstchp,
680 *win->alines[y]->lastchp);
681 #endif
682 }
683 }
684 }
685 }
686