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 (c) 1995, by Sun Microsystems, Inc.
24 * All rights reserved.
25 */
26
27 /*
28 * color.c
29 *
30 * XCurses Library
31 *
32 * Copyright 1990, 1995 by Mortice Kern Systems Inc. All rights reserved.
33 *
34 */
35
36 #ifdef M_RCSID
37 #ifndef lint
38 static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/color.c 1.2 1995/10/02 15:15:02 ant Exp $";
39 #endif
40 #endif
41
42 #include <private.h>
43 #include <stdlib.h>
44
45 int
start_color()46 start_color()
47 {
48 int code = ERR;
49
50 #ifdef M_CURSES_TRACE
51 __m_trace("start_color(void)");
52 #endif
53
54 COLORS = max_colors;
55 COLOR_PAIRS = max_pairs;
56
57 if (orig_colors != (char *) 0)
58 (void) tputs(orig_colors, 1, __m_outc);
59
60 if (orig_pair != (char *) 0)
61 (void) tputs(orig_pair, 1, __m_outc);
62
63 if (0 < max_colors) {
64 cur_term->_color = calloc(max_colors, sizeof *cur_term->_color);
65 if (cur_term->_color == (short (*)[3]) 0)
66 goto error1;
67 }
68
69 if (0 < max_pairs) {
70 cur_term->_pair = calloc(max_pairs, sizeof *cur_term->_pair);
71 if (cur_term->_pair == (short (*)[2]) 0)
72 goto error2;
73 }
74
75 return __m_return_code("start_color", OK);
76 error2:
77 if (cur_term->_color != (short (*)[3]) 0)
78 free(cur_term->_color);
79 error1:
80 return __m_return_code("start_color", ERR);
81 }
82
83 int
init_color(short color,short r,short g,short b)84 init_color(short color, short r, short g, short b)
85 {
86 int code = ERR;
87
88 #ifdef M_CURSES_TRACE
89 __m_trace("init_color(%d, %d, %d, %d)", color, r, g, b);
90 #endif
91
92 if (!can_change || color < 0 || max_colors <= color
93 || r < 0 || 1000 < r
94 || g < 0 || 1000 < g
95 || b < 0 || 1000 < b)
96 goto error;
97
98 /* Remember color settings for future queries. */
99 cur_term->_color[color][0] = r;
100 cur_term->_color[color][1] = g;
101 cur_term->_color[color][2] = b;
102
103 code = OK;
104
105 /* Set the color. */
106 if (initialize_color != (char *) 0) {
107 code = tputs(
108 tparm(
109 initialize_color, (long) color,
110 (long) r, (long) g, (long) b,
111 0L, 0L, 0L, 0L, 0L
112 ), 1, __m_outc
113 );
114 }
115 error:
116 return __m_return_code("init_color", code);
117 }
118
119 int
init_pair(short pair,short f,short b)120 init_pair(short pair, short f, short b)
121 {
122 int code = ERR;
123
124 #ifdef M_CURSES_TRACE
125 __m_trace("init_pair(%d, %d, %d)", pair, f, b);
126 #endif
127
128 if (pair < 0 || max_pairs <= pair
129 || f < 0 || max_colors <= f
130 || b < 0 || max_colors <= b)
131 goto error;
132
133 /* Remember color-pair settings for future queries. */
134 cur_term->_pair[pair][0] = f;
135 cur_term->_pair[pair][1] = b;
136
137 code = OK;
138
139 /* Set color-pair (foreground-background). */
140 if (initialize_pair == (char *) 0) {
141 code = tputs(
142 tparm(
143 initialize_pair,
144 (long) cur_term->_pair[f][0],
145 (long) cur_term->_pair[f][1],
146 (long) cur_term->_pair[f][2],
147 (long) cur_term->_pair[b][0],
148 (long) cur_term->_pair[b][1],
149 (long) cur_term->_pair[b][2],
150 0L, 0L, 0L
151 ), 1, __m_outc
152 );
153 }
154 error:
155 return __m_return_code("init_pair", code);
156 }
157
158 int
color_content(short color,short * r,short * g,short * b)159 color_content(short color, short *r, short *g, short *b)
160 {
161 #ifdef M_CURSES_TRACE
162 __m_trace("color_content(%d, %p, %p, %p)", color, r, g, b);
163 #endif
164
165 if (color < 0 || max_colors <= color)
166 return __m_return_code("color_content", ERR);
167
168 /* There does not appear to be a terminfo entry to query the
169 * color settings, so we retain them in an array for quick
170 * access.
171 */
172 *r = cur_term->_color[color][0];
173 *g = cur_term->_color[color][1];
174 *b = cur_term->_color[color][2];
175
176 return __m_return_code("color_content", OK);
177 }
178
179 int
pair_content(short pair,short * f,short * b)180 pair_content(short pair, short *f, short *b)
181 {
182 #ifdef M_CURSES_TRACE
183 __m_trace("pair_content(%d, %p, %p)", pair, f, b);
184 #endif
185 if (pair < 0 || max_pairs <= pair)
186 return __m_return_code("pair_content", ERR);
187
188 /* There does not appear to be a terminfo entry to query the
189 * color-pair settings, so we retain them in an array for quick
190 * access.
191 */
192 *f = cur_term->_pair[pair][0];
193 *b = cur_term->_pair[pair][1];
194
195 return __m_return_code("pair_content", OK);
196 }
197
198