xref: /illumos-gate/usr/src/lib/libcurses/screen/init_pair.c (revision 24da5b34f49324ed742a340010ed5bd3d4e06625)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*LINTLIBRARY*/
43 
44 #include <sys/types.h>
45 #include "curses_inc.h"
46 
47 int
48 init_pair(short pair, short f, short b)
49 {
50 	_Color_pair *ptp;	/* pairs table pointer */
51 
52 	/* check the validity of arguments	*/
53 
54 	if (pair < 1 || pair >= COLOR_PAIRS ||
55 	    f < 0 || b < 0 || f >= COLORS || b >= COLORS)
56 		return (ERR);
57 
58 	ptp = cur_term->_pairs_tbl + pair;
59 
60 	/* update the pairs table (if no changes just return)   */
61 
62 	if (ptp->foreground == f && ptp->background == b)
63 		return (OK);
64 
65 	ptp->foreground = f;
66 	ptp->background = b;
67 
68 	/* if we are on terminal that cannot define color pairs (Tek)	*/
69 	/* and "pair" was previously defined, go through the curscr	*/
70 	/* and erase information from the color field at all positions	*/
71 	/* that use that color pair (this way when refresh will be	*/
72 	/* called next time, it will be forced to change the color at	*/
73 	/* these positions						*/
74 
75 	if (!initialize_pair) {
76 		if (ptp->init) {
77 			short  i, j;
78 			short  lin = curscr->_maxy;
79 			chtype **y = curscr->_y;
80 			bool change;
81 			short top = -1;
82 			short bottom = -1;
83 			chtype new_pair = COLOR_PAIR(pair);
84 
85 		/* must use lin=curscr->_maxy rather then LINES, because */
86 		/* LINES could have been decremented by ripoffline()	*/
87 
88 			for (i = 0; i < lin; i++) {
89 				change = FALSE;
90 				for (j = 0; j < COLS; j++) {
91 					if ((y[i][j] & A_COLOR) == new_pair) {
92 						y[i][j] &= ~A_COLOR;
93 						change = TRUE;
94 					}
95 					if (change) {
96 						(void) wtouchln(_virtscr,
97 						    i, 1, -1);
98 						if (top == -1)
99 							top = i;
100 						bottom = i;
101 						curscr->_attrs &= ~A_COLOR;
102 					}
103 				}
104 				if (top != -1) {
105 					_VIRTTOP = top;
106 					_VIRTBOT = bottom;
107 				}
108 			}
109 
110 		}
111 	}
112 
113 	/* on terminals that can initialize color pairs (HP)    */
114 	/* send an escape to initialize the new pair		*/
115 
116 	else
117 		_init_HP_pair(pair, f, b);
118 
119 	/* if that pair has not been previously initialized, it could not */
120 	/* have been  used on the screen, so we don't have to do refresh  */
121 
122 	if (ptp->init)
123 		(void) wrefresh(_virtscr);
124 	else
125 		ptp->init = TRUE;
126 
127 	return (OK);
128 }
129 
130 
131 
132 void
133 _init_HP_pair(short pair, short f, short b)
134 {
135 	_Color *ctp = cur_term->_color_tbl;  /* color table pointer */
136 
137 	if (initialize_pair)
138 		(void) tputs(tparm_p7(initialize_pair, (long) pair,
139 		    (long) ctp[f].r, (long) ctp[f].g, (long) ctp[f].b,
140 		    (long) ctp[b].r, (long) ctp[b].g, (long) ctp[b].b),
141 		    1, _outch);
142 }
143