xref: /illumos-gate/usr/src/lib/libcurses/screen/init_pair.c (revision 7a6d80f1660abd4755c68cbd094d4a914681d26e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 /*LINTLIBRARY*/
41 
42 #include <sys/types.h>
43 #include "curses_inc.h"
44 
45 int
46 init_pair(short pair, short f, short b)
47 {
48 	_Color_pair *ptp;	/* pairs table pointer */
49 
50 	/* check the validity of arguments	*/
51 
52 	if (pair < 1 || pair >= COLOR_PAIRS ||
53 	    f < 0 || b < 0 || f >= COLORS || b >= COLORS)
54 		return (ERR);
55 
56 	ptp = cur_term->_pairs_tbl + pair;
57 
58 	/* update the pairs table (if no changes just return)   */
59 
60 	if (ptp->foreground == f && ptp->background == b)
61 		return (OK);
62 
63 	ptp->foreground = f;
64 	ptp->background = b;
65 
66 	/* if we are on terminal that cannot define color pairs (Tek)	*/
67 	/* and "pair" was previously defined, go through the curscr	*/
68 	/* and erase information from the color field at all positions	*/
69 	/* that use that color pair (this way when refresh will be	*/
70 	/* called next time, it will be forced to change the color at	*/
71 	/* these positions						*/
72 
73 	if (!initialize_pair) {
74 		if (ptp->init) {
75 			short  i, j;
76 			short  lin = curscr->_maxy;
77 			chtype **y = curscr->_y;
78 			bool change;
79 			short top = -1;
80 			short bottom = -1;
81 			chtype new_pair = COLOR_PAIR(pair);
82 
83 		/* must use lin=curscr->_maxy rather then LINES, because */
84 		/* LINES could have been decremented by ripoffline()	*/
85 
86 			for (i = 0; i < lin; i++) {
87 				change = FALSE;
88 				for (j = 0; j < COLS; j++) {
89 					if ((y[i][j] & A_COLOR) == new_pair) {
90 						y[i][j] &= ~A_COLOR;
91 						change = TRUE;
92 					}
93 					if (change) {
94 						(void) wtouchln(_virtscr,
95 						    i, 1, -1);
96 						if (top == -1)
97 							top = i;
98 						bottom = i;
99 						curscr->_attrs &= ~A_COLOR;
100 					}
101 				}
102 				if (top != -1) {
103 					_VIRTTOP = top;
104 					_VIRTBOT = bottom;
105 				}
106 			}
107 
108 		}
109 	}
110 
111 	/* on terminals that can initialize color pairs (HP)    */
112 	/* send an escape to initialize the new pair		*/
113 
114 	else
115 		_init_HP_pair(pair, f, b);
116 
117 	/* if that pair has not been previously initialized, it could not */
118 	/* have been  used on the screen, so we don't have to do refresh  */
119 
120 	if (ptp->init)
121 		(void) wrefresh(_virtscr);
122 	else
123 		ptp->init = TRUE;
124 
125 	return (OK);
126 }
127 
128 
129 
130 void
131 _init_HP_pair(short pair, short f, short b)
132 {
133 	_Color *ctp = cur_term->_color_tbl;  /* color table pointer */
134 
135 	if (initialize_pair)
136 		(void) tputs(tparm_p7(initialize_pair, (long) pair,
137 		    (long) ctp[f].r, (long) ctp[f].g, (long) ctp[f].b,
138 		    (long) ctp[b].r, (long) ctp[b].g, (long) ctp[b].b),
139 		    1, _outch);
140 }
141