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-1998 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /* LINTLIBRARY */
30 
31 /*
32  * vid_puts.c
33  *
34  * XCurses Library
35  *
36  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
37  *
38  */
39 
40 #ifdef M_RCSID
41 #ifndef lint
42 static char rcsID[] =
43 "$Header: /team/ps/sun_xcurses/archive/local_changes/xcurses/src/lib/"
44 "libxcurses/src/libc/xcurses/rcs/vid_puts.c 1.6 1998/05/28 14:22:43 "
45 "cbates Exp $";
46 #endif
47 #endif
48 
49 #include <private.h>
50 
51 static attr_t	turn_off(int (*)(int), attr_t);
52 static attr_t	turn_on(int (*)(int), attr_t);
53 
54 /*
55  * Return true if attribute X a member of the attribute set A.
56  * no_color_video is the set of attributes that cannot be combined
57  * with colours.
58  */
59 #define	ISATTR(a, x)	(((a) & ~no_color_video & (x)) == (x))
60 
61 /*
62  * Set the desired attribute state for a terminal screen.
63  *
64  * Using set_attributes is the prefered method but requires some care
65  * in writing the proper terminfo string.  Using exit_attribute_mode and
66  * the assorted enter_ attribute mode capabilities is the next best method.
67  * Finally using the assorted exit_ and enter_ attribute mode capabilities
68  * is the last method available and is not necessarily efficent (or smart
69  * because of the needs of ceol_standout_glitch support).
70  */
71 /* ARGSUSED */
72 int
73 vid_puts(attr_t attr, short pair, void *opts, int (*putout)(int))
74 {
75 	long	p1, p2, p3, p4, p5, p6, p7, p8, p9;
76 
77 	if (set_attributes != NULL && ATTR_STATE != attr) {
78 		/*
79 		 * Assume that <set_attributes> disables attributes
80 		 * then re-enables attributes that are to be on.
81 		 */
82 		p1 = (long) ISATTR(attr, WA_STANDOUT);
83 		p2 = (long) ISATTR(attr, WA_UNDERLINE);
84 		p3 = (long) ISATTR(attr, WA_REVERSE);
85 		p4 = (long) ISATTR(attr, WA_BLINK);
86 		p5 = (long) ISATTR(attr, WA_DIM);
87 		p6 = (long) ISATTR(attr, WA_BOLD);
88 		p7 = (long) ISATTR(attr, WA_INVIS);
89 		p8 = (long) ISATTR(attr, WA_PROTECT);
90 		p9 = (long) ISATTR(attr, WA_ALTCHARSET);
91 
92 		(void) TPUTS(tparm(set_attributes,
93 			p1, p2, p3, p4, p5, p6, p7, p8, p9),
94 			1, putout);
95 
96 		ATTR_STATE &= ~WA_SGR_MASK;
97 		ATTR_STATE |= attr & WA_SGR_MASK;
98 
99 		/*
100 		 * Only use <set_a_attributes> when <set_attributes>
101 		 * is defined.  <set_a_attributes> should not disable
102 		 * attributes, as this will have been handled by
103 		 * <set_attributes>.
104 		 * NOT TRUE - C. Bates
105 		 */
106 		if (set_a_attributes != NULL) {
107 			p1 = (long) ISATTR(attr, WA_HORIZONTAL);
108 			p2 = (long) ISATTR(attr, WA_LEFT);
109 			p3 = (long) ISATTR(attr, WA_LOW);
110 			p4 = (long) ISATTR(attr, WA_RIGHT);
111 			p5 = (long) ISATTR(attr, WA_TOP);
112 			p6 = (long) ISATTR(attr, WA_VERTICAL);
113 
114 			(void) TPUTS(tparm(set_a_attributes,
115 				p1, p2, p3, p4, p5, p6, 0L, 0L, 0L),
116 				1, putout);
117 
118 			ATTR_STATE &= ~WA_SGR1_MASK;
119 			ATTR_STATE |= attr & WA_SGR1_MASK;
120 		}
121 	} else if (ATTR_STATE != attr) {
122 		/* Turn off only those attributes that are on. */
123 		(void) turn_off(putout, ATTR_STATE);
124 
125 		/*
126 		 * Turn on attributes regardless if they are already
127 		 * on, because terminals with ceol_standout_glitch, like
128 		 * HP terminals, will have to re-enforce the current
129 		 * attributes in order to change existing attribute
130 		 * cookies on the screen.
131 		 */
132 		ATTR_STATE = turn_on(putout, attr);
133 	}
134 
135 	/*
136 	 * A_NORMAL equals 0, which is all attributes off and
137 	 * COLOR_PAIR(0).  This implies that colour pair 0 is
138 	 * the orig_pair.
139 	 */
140 	if (pair == 0) {
141 		if (orig_pair != NULL) {
142 			(void) TPUTS(orig_pair, 1, putout);
143 		}
144 
145 		pair = 0;
146 	} else if (pair != cur_term->_co && 0 < max_colors) {
147 		short	fg, bg;
148 
149 		if (set_color_pair != NULL) {
150 			(void) TPUTS(tparm(set_color_pair, (long) pair,
151 				0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
152 				1, putout);
153 		} else if (pair_content(pair, &fg, &bg) == OK) {
154 			if (set_a_foreground != NULL) {
155 				(void) TPUTS(tparm(set_a_foreground, (long) fg,
156 					0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
157 					1, putout);
158 			} else if (set_foreground != NULL) {
159 				(void) TPUTS(tparm(set_foreground, (long) fg,
160 					0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
161 					1, putout);
162 			}
163 
164 			if (set_a_background != NULL) {
165 				(void) TPUTS(tparm(set_a_background, (long) bg,
166 					0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
167 					1, putout);
168 			} else if (set_background != NULL) {
169 				(void) TPUTS(tparm(set_background, (long) bg,
170 					0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
171 					1, putout);
172 			}
173 		}
174 	}
175 
176 	/* Remember the current attribute state for the terminal. */
177 	ATTR_STATE = attr;
178 	cur_term->_co = pair;
179 
180 	return (OK);
181 }
182 
183 static attr_t
184 turn_off(int (*putout)(int), attr_t attr)
185 {
186 	attr_t	new = attr;
187 
188 	if (exit_attribute_mode != NULL) {
189 		(void) TPUTS(exit_attribute_mode, 1, putout);
190 		new = WA_NORMAL;
191 	} else {
192 		if (ISATTR(attr, WA_UNDERLINE) &&
193 			exit_underline_mode != NULL) {
194 			(void) TPUTS(exit_underline_mode, 1, putout);
195 			new &= ~WA_UNDERLINE;
196 		}
197 
198 		if (ISATTR(attr, WA_STANDOUT) &&
199 			exit_standout_mode != NULL) {
200 			(void) TPUTS(exit_standout_mode, 1, putout);
201 			new &= ~WA_STANDOUT;
202 		}
203 
204 		if (ISATTR(attr, WA_ALTCHARSET) &&
205 			exit_alt_charset_mode != NULL) {
206 			(void) TPUTS(exit_alt_charset_mode, 1, putout);
207 			new &= ~WA_ALTCHARSET;
208 		}
209 	}
210 
211 	return (new);
212 }
213 
214 static attr_t
215 turn_on(int (*putout)(int), attr_t attr)
216 {
217 	attr_t	new = attr;
218 
219 	if (ISATTR(attr, WA_ALTCHARSET) &&
220 		enter_alt_charset_mode != NULL) {
221 		(void) TPUTS(enter_alt_charset_mode, 1, putout);
222 		new |= WA_ALTCHARSET;
223 	}
224 
225 	if (ISATTR(attr, WA_BLINK) &&
226 		enter_blink_mode != NULL) {
227 		(void) TPUTS(enter_blink_mode, 1, putout);
228 		new |= WA_BLINK;
229 	}
230 
231 	if (ISATTR(attr, WA_BOLD) &&
232 		enter_bold_mode != NULL) {
233 		(void) TPUTS(enter_bold_mode, 1, putout);
234 		new |= WA_BOLD;
235 	}
236 
237 	if (ISATTR(attr, WA_INVIS) &&
238 		enter_secure_mode != NULL) {
239 		(void) TPUTS(enter_secure_mode, 1, putout);
240 		new |= WA_INVIS;
241 	}
242 
243 	if (ISATTR(attr, WA_DIM) &&
244 		enter_dim_mode != NULL) {
245 		(void) TPUTS(enter_dim_mode, 1, putout);
246 		new |= WA_DIM;
247 	}
248 
249 	if (ISATTR(attr, WA_PROTECT) &&
250 		enter_protected_mode != NULL) {
251 		(void) TPUTS(enter_protected_mode, 1, putout);
252 		new |= WA_PROTECT;
253 	}
254 
255 	if (ISATTR(attr, WA_REVERSE) &&
256 		enter_reverse_mode != NULL) {
257 		(void) TPUTS(enter_reverse_mode, 1, putout);
258 		new |= WA_REVERSE;
259 	}
260 
261 	if (ISATTR(attr, WA_STANDOUT) &&
262 		enter_standout_mode != NULL) {
263 		(void) TPUTS(enter_standout_mode, 1, putout);
264 		new |= WA_STANDOUT;
265 	}
266 
267 	if (ISATTR(attr, WA_UNDERLINE) &&
268 		enter_underline_mode != NULL) {
269 		(void) TPUTS(enter_underline_mode, 1, putout);
270 		new |= WA_UNDERLINE;
271 	}
272 
273 	if (ISATTR(attr, WA_HORIZONTAL) &&
274 		enter_horizontal_hl_mode != NULL) {
275 		(void) TPUTS(enter_horizontal_hl_mode, 1, putout);
276 		new |= WA_HORIZONTAL;
277 	}
278 
279 	if (ISATTR(attr, WA_LEFT) &&
280 		enter_left_hl_mode != NULL) {
281 		(void) TPUTS(enter_left_hl_mode, 1, putout);
282 		new |= WA_LEFT;
283 	}
284 
285 	if (ISATTR(attr, WA_LOW) &&
286 		enter_low_hl_mode != NULL) {
287 		(void) TPUTS(enter_low_hl_mode, 1, putout);
288 		new |= WA_LOW;
289 	}
290 
291 	if (ISATTR(attr, WA_RIGHT) &&
292 		enter_right_hl_mode != NULL) {
293 		(void) TPUTS(enter_right_hl_mode, 1, putout);
294 		new |= WA_RIGHT;
295 	}
296 
297 	if (ISATTR(attr, WA_TOP) &&
298 		enter_top_hl_mode != NULL) {
299 		(void) TPUTS(enter_top_hl_mode, 1, putout);
300 		new |= WA_TOP;
301 	}
302 
303 	if (ISATTR(attr, WA_VERTICAL) &&
304 		enter_vertical_hl_mode != NULL) {
305 		(void) TPUTS(enter_vertical_hl_mode, 1, putout);
306 		new |= WA_VERTICAL;
307 	}
308 
309 	return (new);
310 }
311