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