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