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